]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
VN: Don't recurse on for the same value of `a != 0` [PR117859]
authorAndrew Pinski <quic_apinski@quicinc.com>
Sat, 30 Nov 2024 22:09:48 +0000 (14:09 -0800)
committerAndrew Pinski <quic_apinski@quicinc.com>
Sun, 1 Dec 2024 07:34:31 +0000 (23:34 -0800)
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 [new file with mode: 0644]
gcc/testsuite/gcc.dg/torture/pr117859-2.c [new file with mode: 0644]
gcc/tree-ssa-sccvn.cc

diff --git a/gcc/testsuite/gcc.dg/torture/pr117859-1.c b/gcc/testsuite/gcc.dg/torture/pr117859-1.c
new file mode 100644 (file)
index 0000000..21e0a26
--- /dev/null
@@ -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 (file)
index 0000000..83549a8
--- /dev/null
@@ -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;
+
+
+}
index 7ef0bd01d3c9aed6c32e55b553c7c3755ea686ba..8d74731a891cfb76c4b794b92aeae012fec1d632 100644 (file)
@@ -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. */