https://gcc.gnu.org/g:684e5ae90b64c3481f8a5cb7b9517daf79c78ab4
commit r15-5023-g684e5ae90b64c3481f8a5cb7b9517daf79c78ab4 Author: Andrew Pinski <quic_apin...@quicinc.com> Date: Fri Nov 1 23:20:22 2024 -0700 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_apin...@quicinc.com> Diff: --- gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-4.c | 38 ++++++++++++++++++++++++ gcc/tree-ssa-sccvn.cc | 10 +++++++ 2 files changed, 48 insertions(+) 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 000000000000..fe9d2e2fb58f --- /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 67ed2cd8ffe1..1967bbdca84d 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) {