https://gcc.gnu.org/g:dfd0ced98fcf62c4d24979b74c1d52334ff62bfc
commit r15-7589-gdfd0ced98fcf62c4d24979b74c1d52334ff62bfc Author: Richard Biener <rguent...@suse.de> Date: Mon Feb 17 11:40:01 2025 +0100 tree-optimization/118895 - ICE during PRE When we simplify a NARY during PHI translation we have to make sure to not inject not available operands into it given that might violate the valueization hook constraints and we'd pick up invalid context-sensitive data in further simplification or as in this case later ICE when we try to insert the expression. PR tree-optimization/118895 * tree-ssa-sccvn.cc (vn_nary_build_or_lookup_1): Only allow CSE if we can verify the result is available. * gcc.dg/pr118895.c: New testcase. Diff: --- gcc/testsuite/gcc.dg/pr118895.c | 13 +++++++++++++ gcc/tree-ssa-sccvn.cc | 13 ++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/gcc/testsuite/gcc.dg/pr118895.c b/gcc/testsuite/gcc.dg/pr118895.c new file mode 100644 index 000000000000..ca61d2cc1b19 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr118895.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +unsigned long a; +void fn1() +{ + unsigned long e = a; + int c = e; + int d = c < 100 ? c : 0; + if (d + (int)e & 608) + while (e & 608) + e <<= 1; +} diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index 8bb45780a981..146840664e2e 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -366,6 +366,10 @@ static vn_ssa_aux_t last_pushed_avail; correct. */ static vn_tables_t valid_info; +/* Global RPO state for access from hooks. */ +static class eliminate_dom_walker *rpo_avail; +basic_block vn_context_bb; + /* Valueization hook for simplify_replace_tree. Valueize NAME if it is an SSA name, otherwise just return it. */ @@ -2501,7 +2505,10 @@ vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert, bool res = false; if (i == res_op->num_ops) { - mprts_hook = vn_lookup_simplify_result; + /* Do not leak not available operands into the simplified expression + when called from PRE context. */ + if (rpo_avail) + mprts_hook = vn_lookup_simplify_result; res = res_op->resimplify (NULL, vn_valueize); mprts_hook = NULL; } @@ -2684,10 +2691,6 @@ public: vn_avail *m_avail_freelist; }; -/* Global RPO state for access from hooks. */ -static eliminate_dom_walker *rpo_avail; -basic_block vn_context_bb; - /* Return true if BASE1 and BASE2 can be adjusted so they have the same address and adjust *OFFSET1 and *OFFSET2 accordingly. Otherwise return false. */