https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127166
--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Kyrylo Tkachov <[email protected]>: https://gcc.gnu.org/g:da3b0463f66312c9e93a027bae73bf17c24a8833 commit r17-3845-gda3b0463f66312c9e93a027bae73bf17c24a8833 Author: Kyrylo Tkachov <[email protected]> Date: Tue Sep 1 00:07:44 2026 +0200 tree-optimization: Canonicalize conditional signed narrow clips [PR127166] The PR120378 RISC-V tests use this source form: (NT) ((UT) x > NT_MAX ? (-x) >> (PREC - 1) : x) For every defined execution, it is an unsigned saturating truncation of MAX (x, 0). The signed negation is undefined at the signed minimum under the default overflow rules. Loop if-conversion changes the selected negation to unsigned arithmetic. This loses the distinction between the signed source and the explicit unsigned-negation form from PR126981, which must not become a saturating truncation. Canonicalize the signed form while its overflow semantics are still available before loop if-conversion: (NT) MIN ((UT) MAX (x, 0), NT_MAX) Require an unsigned comparison, a signed right shift, undefined and unsanitized negation overflow, exact constants, and no side effects. This keeps the original form with -fwrapv, -ftrapv, sanitization, volatile operands, and explicit unsigned negation. With this patch the tests restore the RISC-V saturation patterns. Update pr103771.c because the direct form is canonicalized before phiopt. Bootstrapped and tested on aarch64-none-linux-gnu. gcc/ChangeLog: PR tree-optimization/127166 * match.pd: Canonicalize conditional signed narrow clips before loop if-conversion. gcc/testsuite/ChangeLog: PR tree-optimization/127166 * gcc.dg/tree-ssa/pr103771.c: Check the earlier canonicalization. * gcc.dg/tree-ssa/pr127166.c: New test. * gcc.dg/tree-ssa/pr127166-3.c: New test. * gcc.dg/vect/pr127166.c: New test. * gcc.target/riscv/rvv/autovec/pr120378-1.c: Update expected dump counts. * gcc.target/riscv/rvv/autovec/pr120378-2.c: Request an optimized dump. * gcc.target/riscv/rvv/autovec/pr120378-3.c: Likewise. Update expected dump counts. * gcc.target/riscv/rvv/autovec/pr120378-4.c: Request an optimized dump. Signed-off-by: Kyrylo Tkachov <[email protected]>
