https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128142
--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Igor Shevlyakov <[email protected]>: https://gcc.gnu.org/g:f630eb20113fa594480583e65c1eaf7daac64bf0 commit r17-5589-gf630eb20113fa594480583e65c1eaf7daac64bf0 Author: Igor Shevlyakov <[email protected]> Date: Sat Oct 24 13:03:20 2026 -0700 match.pd: Don't fold wrapping signed (x + y) >> 1 to AVG_FLOOR [PR128142] The fold of (x + y) >> 1 into IFN_AVG_FLOOR (x, y) for vectors checks that each element of x and y has a leading zero bit, so that x + y cannot wrap. That is enough for an unsigned type, or for a signed one whose overflow is undefined, but with -fwrapv two nonnegative signed values can add up to a negative one. For example v4si f (v4si a, v4si b) { return ((a & 0x7fffffff) + (b & 0x7fffffff)) >> 1; } with a = b = { INT_MAX, ... } returns -1 per lane, but became .AVG_FLOOR (a & 0x7fffffff, b & 0x7fffffff), which is INT_MAX: an shadd on AArch64. Require two leading zero bits for a signed type whose overflow is defined. Assisted-by: Claude Opus 5.5 PR tree-optimization/128142 gcc/ChangeLog: * match.pd ((x + y) >> 1 -> IFN_AVG_FLOOR (x, y)): Require two leading zero bits for a signed type whose overflow is defined. gcc/testsuite/ChangeLog: * gcc.dg/torture/pr128142.c: New test. * gcc.target/aarch64/pr128142.c: New test. Signed-off-by: Igor Shevlyakov <[email protected]>
