https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124808
--- Comment #9 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Daniel Barboza <[email protected]>: https://gcc.gnu.org/g:875a6c6192a68f75ad213f2a1b3804e7724073e7 commit r17-2307-g875a6c6192a68f75ad213f2a1b3804e7724073e7 Author: Daniel Barboza <[email protected]> Date: Thu Apr 9 08:40:11 2026 -0300 match.pd: right shift compare canonicalization [PR124808] Canonicalize right shift non-equality comparisons with constants by turn them into a comparison with a left shifted constant. Assuming the generic format: (A >> CST1) CMP CST2 For CMP (<, >=) we'll compare A with CST2 left shifted by CST1: - (A >> CST1) < CST2 -> A < (CST2 << CST1) - (A >> CST1) >= CST2 -> A >= (CST2 << CST1) And for CMP (<=, >) we need to IOR the lower CST1 bits from the left shift: - (A >> CST1) <= CST2 -> A <= (CST2 << CST1) | mask - (A >> CST1) > CST2 -> A > (CST2 << CST1) | mask Given that the right hand side changes involves just constants, in the end we'll replace a rshift + cmp with just a cmp. Bootstrapped and regression tested in x86, aarch64 and RISC-V. PR tree-optimization/124808 gcc/ChangeLog: * match.pd(`(A >> CST1) CMP CST2`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr124808-2.c: New test. * gcc.dg/tree-ssa/pr124808.c: New test.
