https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129147
--- Comment #9 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The trunk branch has been updated by Andrea Pinski <[email protected]>: https://gcc.gnu.org/g:50e6ead8cc56e406259eeb1192c2e161d36ada22 commit r17-6859-g50e6ead8cc56e406259eeb1192c2e161d36ada22 Author: Andrea Pinski <[email protected]> Date: Sun Nov 29 16:18:52 2026 -0800 fold: Fix `a ? (long)-a : (long)a` for undefined overflow [PR129147] fold_cond_expr_with_comparison has code that transforms `a ? -a : a` into a but we strip off the sign changing nops and then do the negate in a possible overflowing type. So `(long long) (a ? -a : a)` becomes `-(long long)a` which has a possible overflow for the signed min case while the original code didn't. This fixes the issue by reusing the original negative/minus expression (arg2), and just convert it to the correct type instead. Note the match pattern is ok as it already reuses the negate expression and the stripping off the sign changing nops don't show up here. Bootstrapped and tested on x86_64-linux-gnu. PR tree-optimization/129147 gcc/ChangeLog: * fold-const.cc (fold_cond_expr_with_comparison): Reuse arg2 for EQ_EXPR/UNEQ_EXPR case. gcc/testsuite/ChangeLog: * gcc.dg/torture/negate-overflow-1.c: New test. Signed-off-by: Andrea Pinski <[email protected]>
