On 7/10/2026 12:49 AM, Jeffrey Law wrote:



On 7/7/2026 3:39 PM, Daniel Barboza wrote:
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.
---

Re-sending this patch since it got stalled since May 16th.  Rebased
and bootstrapped/regtested again with x86_64.
Thanks for re-raising.  This clearly got lost.

This is OK for the trunk.  And FTR I tested this across the various
embedded targets too with no regressions.

Pushed. Thanks!

Daniel


Jeff

Reply via email to