https://gcc.gnu.org/g:f61ae2cb8d38929cb3761631160fdfa58e59fdc4
commit r17-6425-gf61ae2cb8d38929cb3761631160fdfa58e59fdc4 Author: Richard Biener <[email protected]> Date: Wed Nov 18 15:20:02 2026 +0100 tree-optimization/128672 - avoid UB in CSE of -x / y When we try to CSE -x / y with x / y we have to avoid presenting a negate as if it were in the IL to the match-and-simplify machinery. Instead we have to check whether the negate is available literally when negation may invoke UB. The identity also doesn't hold for INT_MIN. PR tree-optimization/128672 * tree-ssa-sccvn.cc (visit_nary_op): Check whether a negated operand of a division or multiplication is available, do not just attempt to build or simplify it, when the negation may cause UB. Only handle TRUNC_DIV_EXPR when x cannot be INT_MIN. * gcc.dg/torture/pr128672-1.c: New testcase. * gcc.dg/torture/pr128672-2.c: Likewise. Diff: --- gcc/testsuite/gcc.dg/torture/pr128672-1.c | 20 ++++++++++++++++++++ gcc/testsuite/gcc.dg/torture/pr128672-2.c | 21 +++++++++++++++++++++ gcc/tree-ssa-sccvn.cc | 22 ++++++++++++++++++---- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/gcc/testsuite/gcc.dg/torture/pr128672-1.c b/gcc/testsuite/gcc.dg/torture/pr128672-1.c new file mode 100644 index 000000000000..91a3f878db53 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr128672-1.c @@ -0,0 +1,20 @@ +/* { dg-do run } */ +/* { dg-require-effective-target int32plus } */ + +static volatile unsigned X = 0x80000000; +int g; + +void __attribute__((noipa)) +test (int y) +{ + if (y != -1073741824) + __builtin_abort (); +} + +int main () +{ + unsigned x = X; + g = (int)x / 2; + test ((int)-x / 2); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/torture/pr128672-2.c b/gcc/testsuite/gcc.dg/torture/pr128672-2.c new file mode 100644 index 000000000000..3d6c6248876d --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr128672-2.c @@ -0,0 +1,21 @@ +/* { dg-do run } */ +/* { dg-require-effective-target int32plus } */ +/* { dg-additional-options "-fwrapv" } */ + +static volatile unsigned X = 0x80000000; +int g; + +void __attribute__((noipa)) +test (int y) +{ + if (y != -1073741824) + __builtin_abort (); +} + +int main () +{ + unsigned x = X; + g = (int)x / 2; + test ((int)-x / 2); + return 0; +} diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index e78d54bb898c..57099021efc9 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -6021,7 +6021,8 @@ visit_nary_op (tree lhs, gassign *stmt) } break; case TRUNC_DIV_EXPR: - if (TYPE_UNSIGNED (type)) + /* (-a) / b == -(a / b) does not hold for INT_MIN. */ + if (!TYPE_OVERFLOW_UNDEFINED (type)) break; /* Fallthru. */ case RDIV_EXPR: @@ -6038,9 +6039,22 @@ visit_nary_op (tree lhs, gassign *stmt) { unsigned j = i == 0 ? 1 : 0; tree ops[2]; - gimple_match_op match_op (gimple_match_cond::UNCOND, - NEGATE_EXPR, type, rhs[i]); - ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true); + if (!ANY_INTEGRAL_TYPE_P (type) + || TYPE_OVERFLOW_WRAPS (type)) + { + gimple_match_op match_op (gimple_match_cond::UNCOND, + NEGATE_EXPR, type, rhs[i]); + ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true); + } + else + { + /* Do not simplify as if -rhs[i] were in the IL, instead + see if such an operation is available. */ + ops[i] = vn_nary_op_lookup_pieces (1, NEGATE_EXPR, type, + &rhs[i], NULL); + if (ops[i] && !vn_nary_result_avail_or_insertable_p (ops[i])) + ops[i] = NULL_TREE; + } ops[j] = rhs[j]; if (ops[i] && (ops[0] = vn_nary_op_lookup_pieces (2, code,
