https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126644
--- Comment #6 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:7bceace5d3b68d0cf0f3917f71e2cf66e5a3267b commit r17-5946-g7bceace5d3b68d0cf0f3917f71e2cf66e5a3267b Author: Daniel Barboza <[email protected]> Date: Mon Sep 21 18:49:28 2026 -0300 match.pd: (A << N) >> N -> A for signed type A [PR126644] 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.
