https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115392
--- Comment #12 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jeff Law <[email protected]>: https://gcc.gnu.org/g:610ffda1010fd9c02ea73382a151c463a44e1d4b commit r17-6557-g610ffda1010fd9c02ea73382a151c463a44e1d4b Author: Daniel Petrovic <[email protected]> Date: Sat Nov 21 08:29:50 2026 -0700 [PATCH] match.pd: Fold (X | A) cmp C and (X ^ A) cmp C into X cmp C [PR115392] If A is known to lie in [0, 2^m-1], then X | A and X ^ A leave the bits of X at and above position m unchanged, so the comparison with C is decided by the high parts alone when the low m bits of C are: all ones: fold (X op A) <= C and (X op A) > C to X <= C and X > C, all zeros: fold (X op A) < C and (X op A) >= C to X < C and X >= C. A may be a constant or a range limited value, for example a bool, a masked value or a value with a known range. The fold is not done when A may touch the sign bit. PR tree-optimization/115392 gcc/ChangeLog: * match.pd: Fold (X | A) cmp C and (X ^ A) cmp C into X cmp C when A is known to be in [0, 2^m-1] and the low m bits of C are all ones (le, gt) or all zeros (lt, ge). gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr115392-1.c: New test. * gcc.dg/tree-ssa/pr115392-2.c: New test. * gcc.dg/tree-ssa/pr115392-3.c: New test. * gcc.dg/tree-ssa/pr115392-4.c: New test. * gcc.dg/tree-ssa/pr115392-5.c: New test. * gcc.dg/ipa/pr91088.c: Use a constant that the new fold does not simplify, so that the XOR is still visible to ipa-cp.
