aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Pinski <quic_apinski@quicinc.com>2024-11-01 23:20:22 -0700
committerAndrew Pinski <quic_apinski@quicinc.com>2024-11-07 08:24:52 -0800
commit684e5ae90b64c3481f8a5cb7b9517daf79c78ab4 (patch)
tree472a345e1bd5095e4c3e262be42253c3cd67ab06
parentadd4bb94459d6cecae11de279b49f9c1acb14394 (diff)
downloadgcc-684e5ae90b64c3481f8a5cb7b9517daf79c78ab4.zip
gcc-684e5ae90b64c3481f8a5cb7b9517daf79c78ab4.tar.gz
gcc-684e5ae90b64c3481f8a5cb7b9517daf79c78ab4.tar.bz2
VN: Lookup `val != 0` if we got back val when looking up the predicate for GIMPLE_COND [PR117414]
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>
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c38
-rw-r--r--gcc/tree-ssa-sccvn.cc10
2 files changed, 48 insertions, 0 deletions
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
index 0000000..fe9d2e2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c
@@ -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" } } */
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 67ed2cd..1967bbd 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -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)
{