diff mbox series

[2/2] VN: Don't recurse on for the same value of `a != 0` [PR117859]

Message ID 20241201015400.2131214-2-quic_apinski@quicinc.com
State New
Headers show
Series [1/2] gimple-lim: Reuse boolean var when moving PHI | expand

Commit Message

Andrew Pinski Dec. 1, 2024, 1:54 a.m. UTC
Like r15-5063-g6e84a41622f56c, but this is for the `a != 0` case.
After adding vn_valueize to the handle the `a ==/!= 0` case
of insert_predicates_for_cond, it would go into an infinite loop
as the Value number for a could be the same as what it
is for the whole expression. This avoids that recursion so there is
no infinite loop here.

Note lim was introducing `bool_var2 = bool_var1 != 0` originally but
with the gimple testcase in -2, there is no dependency on what passes
before hand will do.

Bootstrapped and tested on x86_64-linux-gnu.

	PR tree-optimization/117859

gcc/ChangeLog:

	* tree-ssa-sccvn.cc (insert_predicates_for_cond): If the
	valueization for the new lhs for `lhs != 0`
	is the same as the old ones, don't recurse.

gcc/testsuite/ChangeLog:

	* gcc.dg/torture/pr117859-1.c: New test.
	* gcc.dg/torture/pr117859-2.c: New test.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
---
 gcc/testsuite/gcc.dg/torture/pr117859-1.c | 23 ++++++++++
 gcc/testsuite/gcc.dg/torture/pr117859-2.c | 51 +++++++++++++++++++++++
 gcc/tree-ssa-sccvn.cc                     |  3 +-
 3 files changed, 76 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr117859-1.c
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr117859-2.c

Comments

Richard Biener Dec. 1, 2024, 7:25 a.m. UTC | #1
> Am 01.12.2024 um 02:59 schrieb Andrew Pinski <quic_apinski@quicinc.com>:
> 
> Like r15-5063-g6e84a41622f56c, but this is for the `a != 0` case.
> After adding vn_valueize to the handle the `a ==/!= 0` case
> of insert_predicates_for_cond, it would go into an infinite loop
> as the Value number for a could be the same as what it
> is for the whole expression. This avoids that recursion so there is
> no infinite loop here.
> 
> Note lim was introducing `bool_var2 = bool_var1 != 0` originally but
> with the gimple testcase in -2, there is no dependency on what passes
> before hand will do.
> 
> Bootstrapped and tested on x86_64-linux-gnu.

Ok

Richard 


>    PR tree-optimization/117859
> 
> gcc/ChangeLog:
> 
>    * tree-ssa-sccvn.cc (insert_predicates_for_cond): If the
>    valueization for the new lhs for `lhs != 0`
>    is the same as the old ones, don't recurse.
> 
> gcc/testsuite/ChangeLog:
> 
>    * gcc.dg/torture/pr117859-1.c: New test.
>    * gcc.dg/torture/pr117859-2.c: New test.
> 
> Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
> ---
> gcc/testsuite/gcc.dg/torture/pr117859-1.c | 23 ++++++++++
> gcc/testsuite/gcc.dg/torture/pr117859-2.c | 51 +++++++++++++++++++++++
> gcc/tree-ssa-sccvn.cc                     |  3 +-
> 3 files changed, 76 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/gcc.dg/torture/pr117859-1.c
> create mode 100644 gcc/testsuite/gcc.dg/torture/pr117859-2.c
> 
> diff --git a/gcc/testsuite/gcc.dg/torture/pr117859-1.c b/gcc/testsuite/gcc.dg/torture/pr117859-1.c
> new file mode 100644
> index 00000000000..21e0a26e5ca
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/torture/pr117859-1.c
> @@ -0,0 +1,23 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fno-ssa-phiopt -fno-expensive-optimizations" } */
> +
> +/* PR tree-optimization/117859 */
> +
> +unsigned char a;
> +int b, c, d, e, g, h, j, k;
> +struct {
> +  int f : 1;
> +} i;
> +char l(int m) { return a > 255 >> m ? 0 : m; }
> +int main() {
> +  if (i.f)
> +    d = b;
> +  k = e = 1;
> +  for (; e; e--)
> +    g = 0;
> +  h = g ? 1 << g : 1;
> +  c = h || i.f;
> +  while (b)
> +    j = l(k && i.f);
> +  return 0;
> +}
> diff --git a/gcc/testsuite/gcc.dg/torture/pr117859-2.c b/gcc/testsuite/gcc.dg/torture/pr117859-2.c
> new file mode 100644
> index 00000000000..83549a80d3e
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/torture/pr117859-2.c
> @@ -0,0 +1,51 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fgimple" } */
> +
> +/* PR tree-optimization/117859 */
> +
> +void __GIMPLE (ssa,startwith("pre"))
> +m (int p, int b)
> +{
> +  int _6;
> +  _Bool _48;
> +  _Bool _50;
> +  int _57;
> +  _Bool _59;
> +
> +  __BB(2):
> +  _6 = 0; // This needs to be prop'ed to make _48 unconditional
> +  if (_6 != 0)
> +    goto __BB5;
> +  else
> +    goto __BB14;
> +
> +  __BB(14):
> +  _48 = p_2(D) != 0;
> +  goto __BB7;
> +
> +  __BB(5):
> +  goto __BB7;
> +
> +  __BB(7):
> +  _59 = __PHI (__BB5: _Literal (_Bool) 1, __BB14: _48);
> +  if (b_36(D) != 0)
> +    goto __BB8;
> +  else
> +    goto __BB13;
> +
> +  __BB(8):
> +  _50 = _59 != _Literal (_Bool) 0; // Important being != 0
> +  _57 = _50 ? 1 : 0; // can be unused but need around for _50 being used
> +  goto __BB9;
> +
> +  __BB(9,loop_header(2)):
> +  if (_59 != _Literal (_Bool) 0)
> +    goto __BB13;
> +  else
> +    goto __BB9;
> +
> +  __BB(13):
> +  return;
> +
> +
> +}
> diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
> index 7ef0bd01d3c..8d74731a891 100644
> --- a/gcc/tree-ssa-sccvn.cc
> +++ b/gcc/tree-ssa-sccvn.cc
> @@ -7964,7 +7964,8 @@ insert_predicates_for_cond (tree_code code, tree lhs, tree rhs,
>        edge nf = false_e;
>        if (code == EQ_EXPR)
>          std::swap (nt, nf);
> -        insert_predicates_for_cond (nc, nlhs, nrhs, nt, nf);
> +        if (lhs != nlhs)
> +          insert_predicates_for_cond (nc, nlhs, nrhs, nt, nf);
>      }
>       /* (a | b) == 0 ->
>        on true edge assert: a == 0 & b == 0. */
> --
> 2.43.0
>
diff mbox series

Patch

diff --git a/gcc/testsuite/gcc.dg/torture/pr117859-1.c b/gcc/testsuite/gcc.dg/torture/pr117859-1.c
new file mode 100644
index 00000000000..21e0a26e5ca
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr117859-1.c
@@ -0,0 +1,23 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-ssa-phiopt -fno-expensive-optimizations" } */
+
+/* PR tree-optimization/117859 */
+
+unsigned char a;
+int b, c, d, e, g, h, j, k;
+struct {
+  int f : 1;
+} i;
+char l(int m) { return a > 255 >> m ? 0 : m; }
+int main() {
+  if (i.f)
+    d = b;
+  k = e = 1;
+  for (; e; e--)
+    g = 0;
+  h = g ? 1 << g : 1;
+  c = h || i.f;
+  while (b)
+    j = l(k && i.f);
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr117859-2.c b/gcc/testsuite/gcc.dg/torture/pr117859-2.c
new file mode 100644
index 00000000000..83549a80d3e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr117859-2.c
@@ -0,0 +1,51 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fgimple" } */
+
+/* PR tree-optimization/117859 */
+
+void __GIMPLE (ssa,startwith("pre"))
+m (int p, int b)
+{
+  int _6;
+  _Bool _48;
+  _Bool _50;
+  int _57;
+  _Bool _59;
+
+  __BB(2):
+  _6 = 0; // This needs to be prop'ed to make _48 unconditional
+  if (_6 != 0)
+    goto __BB5;
+  else
+    goto __BB14;
+
+  __BB(14):
+  _48 = p_2(D) != 0;
+  goto __BB7;
+
+  __BB(5):
+  goto __BB7;
+
+  __BB(7):
+  _59 = __PHI (__BB5: _Literal (_Bool) 1, __BB14: _48);
+  if (b_36(D) != 0)
+    goto __BB8;
+  else
+    goto __BB13;
+
+  __BB(8):
+  _50 = _59 != _Literal (_Bool) 0; // Important being != 0
+  _57 = _50 ? 1 : 0; // can be unused but need around for _50 being used
+  goto __BB9;
+
+  __BB(9,loop_header(2)):
+  if (_59 != _Literal (_Bool) 0)
+    goto __BB13;
+  else
+    goto __BB9;
+
+  __BB(13):
+  return;
+
+
+}
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 7ef0bd01d3c..8d74731a891 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -7964,7 +7964,8 @@  insert_predicates_for_cond (tree_code code, tree lhs, tree rhs,
 	    edge nf = false_e;
 	    if (code == EQ_EXPR)
 	      std::swap (nt, nf);
-	    insert_predicates_for_cond (nc, nlhs, nrhs, nt, nf);
+	    if (lhs != nlhs)
+	      insert_predicates_for_cond (nc, nlhs, nrhs, nt, nf);
 	  }
       /* (a | b) == 0 ->
 	    on true edge assert: a == 0 & b == 0. */