https://gcc.gnu.org/g:2f3f34ea0945a3cd4eba05ec6c5d3c353e1fbe78
commit r17-6597-g2f3f34ea0945a3cd4eba05ec6c5d3c353e1fbe78 Author: Richard Biener <[email protected]> Date: Mon Nov 23 12:51:36 2026 +0100 tree-optimization/128943 - avoid leaking conditional VN equivalences When value-numbering (T)A +- B against an available narrow operation narrow_val in visit_nary_op the result is only valid where narrow_val is executed since we rely on it not overflowing. Recording the equivalence for the expression of the statement in the global nary table makes it available to computations of the same expression in blocks not dominated by narrow_val, bypassing the availability check. PRE then hoists the narrow operation across its guard, introducing undefined signed overflow. The same applies to matching ([-]a){/,*}([-])b against an available negated operand with undefined overflow. There we still record the equivalence when the type has no undefined overflow since the identity holds unconditionally. PR tree-optimization/128943 * tree-ssa-sccvn.cc (visit_nary_op): Do not insert the statement into the nary table when it was value-numbered using an available narrow operation or an available negated operand with undefined overflow. * gcc.dg/torture/pr128943.c: New testcase. Diff: --- gcc/testsuite/gcc.dg/torture/pr128943.c | 35 +++++++++++++++++++++++++++++++++ gcc/tree-ssa-sccvn.cc | 27 ++++++++++++++++--------- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/gcc/testsuite/gcc.dg/torture/pr128943.c b/gcc/testsuite/gcc.dg/torture/pr128943.c new file mode 100644 index 000000000000..f378f9a53f3f --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr128943.c @@ -0,0 +1,35 @@ +/* { dg-do run } */ +/* { dg-require-effective-target longlong64 } */ + +/* The guarded signed x + 10 was used to value-number the unguarded + (T)((unsigned T)x + 10) in a path not dominated by it. The code + involving g makes jump threading duplicate the join block. */ + +typedef long long T; +typedef unsigned long long UT; + +T X = 0x7ffffffffffffff8, g; +int c, n; + +T __attribute__((noipa)) +f (T x) +{ + if (n) + g = x ? 0 : 5; + x &= 0x7fffffffffffffff; + T r = 0; + if (c) + r = x + 10; + r += (T)((UT)x + 10) / 6; + if (c && g == -1) + return 0; + return r; +} + +int +main (void) +{ + if (f (X) != -1537228672809129301LL) + __builtin_abort (); + return 0; +} diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index 033feb7e3b82..b2020ebe9595 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -5946,12 +5946,12 @@ visit_nary_op (tree lhs, gassign *stmt) NOP_EXPR, type, narrow_val); result = vn_nary_build_or_lookup_avail (&match_op, narrow_val); if (result) - { - const bool changed = set_ssa_val_to (lhs, result); - if (TREE_CODE (result) == SSA_NAME) - vn_nary_op_insert_stmt (stmt, result); - return changed; - } + /* Do not insert (stmt, result) into the VN tables: result is + derived from narrow_val which is only valid where narrow_val + is available (and may have undefined overflow). Inserting + stmt globally would leak narrow_val to blocks not dominated + by it and allow PRE to hoist narrow_val across guards. */ + return set_ssa_val_to (lhs, result); } } break; @@ -6052,12 +6052,16 @@ visit_nary_op (tree lhs, gassign *stmt) tree rhs[2]; rhs[0] = rhs1; rhs[1] = gimple_assign_rhs2 (stmt); + /* With undefined overflow the identity relies on -rhs[i] not + overflowing, which is only known where the negation is + executed. */ + const bool overflow_undef_p = (ANY_INTEGRAL_TYPE_P (type) + && !TYPE_OVERFLOW_WRAPS (type)); for (unsigned i = 0; i <= 1; ++i) { unsigned j = i == 0 ? 1 : 0; tree ops[2]; - if (!ANY_INTEGRAL_TYPE_P (type) - || TYPE_OVERFLOW_WRAPS (type)) + if (!overflow_undef_p) { gimple_match_op match_op (gimple_match_cond::UNCOND, NEGATE_EXPR, type, rhs[i]); @@ -6086,7 +6090,12 @@ visit_nary_op (tree lhs, gassign *stmt) if (result) { bool changed = set_ssa_val_to (lhs, result); - if (TREE_CODE (result) == SSA_NAME) + /* When the equivalence is only valid where the + negation ops[i] is executed do not record it for + the expression of stmt, that would make it + available to computations not dominated by + ops[i]. */ + if (!overflow_undef_p && TREE_CODE (result) == SSA_NAME) vn_nary_op_insert_stmt (stmt, result); return changed; }
