Re: [PATCH] Optimize n + 1 for automatic n array (PR c++/71537)

2017-01-11 Thread Jakub Jelinek
On Wed, Jan 11, 2017 at 10:27:23PM +0100, John Tytgat wrote:
> On 01/10/2017 11:40 PM, Jakub Jelinek wrote:
> > +constexpr bool
> > +foo ()
> > +{
> > +  constexpr int n[42] = { 1 };
> > +  constexpr int o = n ? 1 : 0;
> > +  constexpr int p = n + 1 ? 1 : 0;
> > +  constexpr int q = "abc" + 1 ? 1 : 0;
> > +  return p + p + q == 3;
> > +}
> 
> Not o + p + q ?

Oops, fixed.

Jakub


Re: [PATCH] Optimize n + 1 for automatic n array (PR c++/71537)

2017-01-11 Thread John Tytgat

On 01/10/2017 11:40 PM, Jakub Jelinek wrote:

+constexpr bool
+foo ()
+{
+  constexpr int n[42] = { 1 };
+  constexpr int o = n ? 1 : 0;
+  constexpr int p = n + 1 ? 1 : 0;
+  constexpr int q = "abc" + 1 ? 1 : 0;
+  return p + p + q == 3;
+}


Not o + p + q ?

John.



Re: [PATCH] Optimize n + 1 for automatic n array (PR c++/71537)

2017-01-11 Thread Richard Biener
On Wed, 11 Jan 2017, Jakub Jelinek wrote:

> On Wed, Jan 11, 2017 at 09:31:38AM +0100, Richard Biener wrote:
> > > Or shall I add the function local address check into maybe_nonzero_address
> > > instead (return 1 for those)?
> > 
> > Yes please, and cleanup the other user of maybe_nonzero_address then
> > (which contains the code you added).
> 
> Ok, I'll bootstrap/regtest following patch then:

LGTM.

Richard.

> 2017-01-11  Jakub Jelinek  
> 
>   PR c++/71537
>   * fold-const.c (maybe_nonzero_address): Return 1 for function
>   local objects.
>   (tree_single_nonzero_warnv_p): Don't handle function local objects
>   here.
> 
>   * g++.dg/cpp1y/constexpr-71537.C: New test.
> 
> --- gcc/fold-const.c.jj   2017-01-11 09:36:36.673864630 +0100
> +++ gcc/fold-const.c  2017-01-11 09:44:20.004660940 +0100
> @@ -8171,7 +8171,8 @@ pointer_may_wrap_p (tree base, tree offs
>  /* Return a positive integer when the symbol DECL is known to have
> a nonzero address, zero when it's known not to (e.g., it's a weak
> symbol), and a negative integer when the symbol is not yet in the
> -   symbol table and so whether or not its address is zero is unknown.  */
> +   symbol table and so whether or not its address is zero is unknown.
> +   For function local objects always return positive integer.  */
>  static int
>  maybe_nonzero_address (tree decl)
>  {
> @@ -8179,6 +8180,13 @@ maybe_nonzero_address (tree decl)
>  if (struct symtab_node *symbol = symtab_node::get_create (decl))
>return symbol->nonzero_address ();
>  
> +  /* Function local objects are never NULL.  */
> +  if (DECL_P (decl)
> +  && (DECL_CONTEXT (decl)
> +  && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
> +  && auto_var_in_fn_p (decl, DECL_CONTEXT (decl
> +return 1;
> +
>return -1;
>  }
>  
> @@ -13276,13 +13284,6 @@ tree_single_nonzero_warnv_p (tree t, boo
>   if (nonzero_addr >= 0)
> return nonzero_addr;
>  
> - /* Function local objects are never NULL.  */
> - if (DECL_P (base)
> - && (DECL_CONTEXT (base)
> - && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
> - && auto_var_in_fn_p (base, DECL_CONTEXT (base
> -   return true;
> -
>   /* Constants are never weak.  */
>   if (CONSTANT_CLASS_P (base))
> return true;
> --- gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C.jj   2017-01-11 
> 09:41:32.103909019 +0100
> +++ gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C  2017-01-11 
> 09:41:32.103909019 +0100
> @@ -0,0 +1,14 @@
> +// PR c++/71537
> +// { dg-do compile { target c++14 } }
> +
> +constexpr bool
> +foo ()
> +{
> +  constexpr int n[42] = { 1 };
> +  constexpr int o = n ? 1 : 0;
> +  constexpr int p = n + 1 ? 1 : 0;
> +  constexpr int q = "abc" + 1 ? 1 : 0;
> +  return p + p + q == 3;
> +}
> +
> +static_assert (foo (), "");
> 
> 
>   Jakub
> 
> 

-- 
Richard Biener 
SUSE LINUX GmbH, GF: Felix Imendoerffer, Jane Smithard, Graham Norton, HRB 
21284 (AG Nuernberg)


Re: [PATCH] Optimize n + 1 for automatic n array (PR c++/71537)

2017-01-11 Thread Jakub Jelinek
On Wed, Jan 11, 2017 at 09:31:38AM +0100, Richard Biener wrote:
> > Or shall I add the function local address check into maybe_nonzero_address
> > instead (return 1 for those)?
> 
> Yes please, and cleanup the other user of maybe_nonzero_address then
> (which contains the code you added).

Ok, I'll bootstrap/regtest following patch then:

2017-01-11  Jakub Jelinek  

PR c++/71537
* fold-const.c (maybe_nonzero_address): Return 1 for function
local objects.
(tree_single_nonzero_warnv_p): Don't handle function local objects
here.

* g++.dg/cpp1y/constexpr-71537.C: New test.

--- gcc/fold-const.c.jj 2017-01-11 09:36:36.673864630 +0100
+++ gcc/fold-const.c2017-01-11 09:44:20.004660940 +0100
@@ -8171,7 +8171,8 @@ pointer_may_wrap_p (tree base, tree offs
 /* Return a positive integer when the symbol DECL is known to have
a nonzero address, zero when it's known not to (e.g., it's a weak
symbol), and a negative integer when the symbol is not yet in the
-   symbol table and so whether or not its address is zero is unknown.  */
+   symbol table and so whether or not its address is zero is unknown.
+   For function local objects always return positive integer.  */
 static int
 maybe_nonzero_address (tree decl)
 {
@@ -8179,6 +8180,13 @@ maybe_nonzero_address (tree decl)
 if (struct symtab_node *symbol = symtab_node::get_create (decl))
   return symbol->nonzero_address ();
 
+  /* Function local objects are never NULL.  */
+  if (DECL_P (decl)
+  && (DECL_CONTEXT (decl)
+  && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
+  && auto_var_in_fn_p (decl, DECL_CONTEXT (decl
+return 1;
+
   return -1;
 }
 
@@ -13276,13 +13284,6 @@ tree_single_nonzero_warnv_p (tree t, boo
if (nonzero_addr >= 0)
  return nonzero_addr;
 
-   /* Function local objects are never NULL.  */
-   if (DECL_P (base)
-   && (DECL_CONTEXT (base)
-   && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
-   && auto_var_in_fn_p (base, DECL_CONTEXT (base
- return true;
-
/* Constants are never weak.  */
if (CONSTANT_CLASS_P (base))
  return true;
--- gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C.jj 2017-01-11 
09:41:32.103909019 +0100
+++ gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C2017-01-11 
09:41:32.103909019 +0100
@@ -0,0 +1,14 @@
+// PR c++/71537
+// { dg-do compile { target c++14 } }
+
+constexpr bool
+foo ()
+{
+  constexpr int n[42] = { 1 };
+  constexpr int o = n ? 1 : 0;
+  constexpr int p = n + 1 ? 1 : 0;
+  constexpr int q = "abc" + 1 ? 1 : 0;
+  return p + p + q == 3;
+}
+
+static_assert (foo (), "");


Jakub


Re: [PATCH] Optimize n + 1 for automatic n array (PR c++/71537)

2017-01-11 Thread Richard Biener
On Tue, 10 Jan 2017, Jakub Jelinek wrote:

> Hi!
> 
> This patch allows to fold n + 1 != 0 into true for automatic array n.
> We already handle it for variables in the symbol table (if not weak),
> but automatic vars are never in the symbol table.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> Or shall I add the function local address check into maybe_nonzero_address
> instead (return 1 for those)?

Yes please, and cleanup the other user of maybe_nonzero_address then
(which contains the code you added).

Richar.

> 2017-01-10  Jakub Jelinek  
> 
>   PR c++/71537
>   * fold-const.c (fold_comparison): Handle also comparison of
>   addresses of local objects against NULL.
> 
>   * g++.dg/cpp1y/constexpr-71537.C: New test.
> 
> --- gcc/fold-const.c.jj   2017-01-01 12:45:38.0 +0100
> +++ gcc/fold-const.c  2017-01-10 12:16:47.005931072 +0100
> @@ -8420,7 +8420,11 @@ fold_comparison (location_t loc, enum tr
>every object pointer compares greater than a null pointer.
>*/
>else if (((DECL_P (base0)
> -  && maybe_nonzero_address (base0) > 0
> +  && (maybe_nonzero_address (base0) > 0
> +  /* Function local objects are never NULL.  */
> +  || (DECL_CONTEXT (base0)
> +  && TREE_CODE (DECL_CONTEXT (base0)) == FUNCTION_DECL
> +  && auto_var_in_fn_p (base0, DECL_CONTEXT (base0
>/* Avoid folding references to struct members at offset 0 to
>   prevent tests like '>firstmember == 0' from getting
>   eliminated.  When ptr is null, although the -> expression
> --- gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C.jj   2017-01-10 
> 12:22:07.102748236 +0100
> +++ gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C  2017-01-10 
> 12:21:41.0 +0100
> @@ -0,0 +1,14 @@
> +// PR c++/71537
> +// { dg-do compile { target c++14 } }
> +
> +constexpr bool
> +foo ()
> +{
> +  constexpr int n[42] = { 1 };
> +  constexpr int o = n ? 1 : 0;
> +  constexpr int p = n + 1 ? 1 : 0;
> +  constexpr int q = "abc" + 1 ? 1 : 0;
> +  return p + p + q == 3;
> +}
> +
> +static_assert (foo (), "");
> 
>   Jakub
> 
> 

-- 
Richard Biener 
SUSE LINUX GmbH, GF: Felix Imendoerffer, Jane Smithard, Graham Norton, HRB 
21284 (AG Nuernberg)


[PATCH] Optimize n + 1 for automatic n array (PR c++/71537)

2017-01-10 Thread Jakub Jelinek
Hi!

This patch allows to fold n + 1 != 0 into true for automatic array n.
We already handle it for variables in the symbol table (if not weak),
but automatic vars are never in the symbol table.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Or shall I add the function local address check into maybe_nonzero_address
instead (return 1 for those)?

2017-01-10  Jakub Jelinek  

PR c++/71537
* fold-const.c (fold_comparison): Handle also comparison of
addresses of local objects against NULL.

* g++.dg/cpp1y/constexpr-71537.C: New test.

--- gcc/fold-const.c.jj 2017-01-01 12:45:38.0 +0100
+++ gcc/fold-const.c2017-01-10 12:16:47.005931072 +0100
@@ -8420,7 +8420,11 @@ fold_comparison (location_t loc, enum tr
 every object pointer compares greater than a null pointer.
   */
   else if (((DECL_P (base0)
-&& maybe_nonzero_address (base0) > 0
+&& (maybe_nonzero_address (base0) > 0
+/* Function local objects are never NULL.  */
+|| (DECL_CONTEXT (base0)
+&& TREE_CODE (DECL_CONTEXT (base0)) == FUNCTION_DECL
+&& auto_var_in_fn_p (base0, DECL_CONTEXT (base0
 /* Avoid folding references to struct members at offset 0 to
prevent tests like '>firstmember == 0' from getting
eliminated.  When ptr is null, although the -> expression
--- gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C.jj 2017-01-10 
12:22:07.102748236 +0100
+++ gcc/testsuite/g++.dg/cpp1y/constexpr-71537.C2017-01-10 
12:21:41.0 +0100
@@ -0,0 +1,14 @@
+// PR c++/71537
+// { dg-do compile { target c++14 } }
+
+constexpr bool
+foo ()
+{
+  constexpr int n[42] = { 1 };
+  constexpr int o = n ? 1 : 0;
+  constexpr int p = n + 1 ? 1 : 0;
+  constexpr int q = "abc" + 1 ? 1 : 0;
+  return p + p + q == 3;
+}
+
+static_assert (foo (), "");

Jakub