On 10/28/26 2:38 AM, Daniel Barboza wrote:
This is done during RTL.  Do it earlier in Gimple to potentially allow
more optimizations opportunities.

For signed types we need to consider that the rshift will preserve the
original sign of its operand, thus if there's a sign change in A during
the lshift we can no longer do the simplification since the end result is
no longer equal to A.  E.g:

- for signed type A = 0x001.. and N = 2:
   (0x001.. << 2) >> 2 --> 0x1.. >> 2 --> 0x111.., which is not A;

- Likewise, for signed type A = 0x110.. and N = 2:
   (0x110.. << 2) >> 2 --> 0x0.. >> 2 --> 0x000.., also not A.

We're ignoring unsigned types because there are match.pd optimizations
like "(x << c) >> c -> x & ((unsigned)-1 >> c)" that already optimizes
them.

Bootstrapped and regression tested in x86_64.

        PR tree-optimization/126644

gcc/ChangeLog:

        * match.pd(`((A << N) >> N) -> N`): New pattern.

gcc/testsuite/ChangeLog:

        * gcc.dg/tree-ssa/pr126644.c: New test.
---

Changes from v2:
- removed the TYPE_UNSIGNED pattern since match.pd already had patterns
   handling it;
- use get_nonzero_bits() and get_known_nonzero_bits() to detect if the
   lshift might change the sign bit.
- v2 link: https://gcc.gnu.org/pipermail/gcc-patches/2026-October/734172.html
LGTM and Sashiko clean.  OK for the trunk.

jeff

Reply via email to