]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
VN: Lookup `val != 0` if we got back val when looking up the predicate for GIMPLE_CON...
authorAndrew Pinski <quic_apinski@quicinc.com>
Sat, 2 Nov 2024 06:20:22 +0000 (23:20 -0700)
committerAndrew Pinski <quic_apinski@quicinc.com>
Thu, 7 Nov 2024 16:24:52 +0000 (08:24 -0800)
Sometimes we get back a full ssa name when looking up the comparison of the GIMPLE_COND
rather than a predicate. We then want to lookup the `val != 0` for the predicate.

Note this might happen with other boolean assignments and COND_EXPR but I am not sure
if it is as important; I have not found a testcase yet.

Bootstrapped and tested on x86_64-linux-gnu.

PR tree-optimization/117414

gcc/ChangeLog:

* tree-ssa-sccvn.cc (process_bb): Lookup
`val != 0` if got back a ssa name when looking the comparison.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/fre-predicated-4.c: New test.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c [new file with mode: 0644]
gcc/tree-ssa-sccvn.cc

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c b/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c
new file mode 100644 (file)
index 0000000..fe9d2e2
--- /dev/null
@@ -0,0 +1,38 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* PR tree-optimization/117414 */
+
+/* Fre1 should figure out that `*aaa != 0`
+   For f0 and f1. */
+
+
+void foo();
+int f0(int *aaa, int j, int t)
+{
+  int b = *aaa;
+  if (b == 0 || t == 1)
+    return 0;
+  for(int i = 0; i < j; i++)
+  {
+    if (!*aaa) foo();
+  }
+  return 0;
+}
+
+int f1(int *aaa, int j, int t)
+{
+  int b = *aaa;
+  if (b == 0)
+    return 0;
+  if (t == 1)
+    return 0;
+  for(int i = 0; i < j; i++)
+  {
+    if (!*aaa) foo();
+  }
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-not "foo " "optimized" } } */
+/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */
index 67ed2cd8ffe198485a4e2b8ddc688d3e2d791132..1967bbdca84dbd44684ae0d67fc0a0386c4ef96e 100644 (file)
@@ -8159,6 +8159,16 @@ process_bb (rpo_elim &avail, basic_block bb,
                val = vn_nary_op_lookup_pieces (2, gimple_cond_code (last),
                                                boolean_type_node, ops,
                                                &vnresult);
+               /* Got back a ssa name, then try looking up `val != 0`
+                  as it might have been recorded that way.  */
+               if (val && TREE_CODE (val) == SSA_NAME)
+                 {
+                   ops[0] = val;
+                   ops[1] = build_zero_cst (TREE_TYPE (val));
+                   val = vn_nary_op_lookup_pieces (2, NE_EXPR,
+                                                   boolean_type_node, ops,
+                                                   &vnresult);
+                 }
                /* Did we get a predicated value?  */
                if (! val && vnresult && vnresult->predicated_values)
                  {