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

 gcc/match.pd                             | 32 +++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr126644.c | 58 ++++++++++++++++++++++++
 2 files changed, 90 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126644.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 0bda1efac59..3ec7646aace 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1572,6 +1572,38 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
             { wide_int_to_tree (type,
                                wi::lshift (wone, wi::to_wide (@2))); }))))
 
+/* PR126644: A << N >> N -> A, for signed type A
+
+   For signed types we need to preserve the existing sign bit for
+   all possible values A might have.  Otherwise we can have an
+   originally positive A (no sign bit), A << N ends up setting
+   the sign bit (i.e. it's a negative number now) and now
+   (A << N) >> N becomes an arithmetic shift that will preserve
+   the sign bit.  And vice-versa if A has a sign bit and we ended
+   up clearing it.
+
+   We're not covering the unsigned case because it is already
+   covered by the existing "(x << c) >> c -> x & ((unsigned)-1 >> c)"
+   pattern, transforming it into a bit_and during 'original'.  */
+(simplify
+ (rshift (lshift@0 @A INTEGER_CST@N) @N)
+  (if (INTEGRAL_TYPE_P (type)
+       && !TYPE_UNSIGNED (type)
+       && tree_fits_uhwi_p (@N)
+       && tree_to_uhwi (@N) < TYPE_PRECISION (type)
+       && single_use (@0))
+   (with {
+      unsigned int prec = TYPE_PRECISION (type);
+      wide_int mask = wi::mask (prec - (tree_to_uhwi (@N) + 1),
+                               true, prec);
+    }
+    /* get_nonzero_bits returns '0' for each bit we are certain
+       to be zero.  get_known_nonzero_bits returns '1' for each
+       bit we know to be one.  */
+    (if ((get_nonzero_bits (@A) & mask) == 0
+        || (get_known_nonzero_bits (@A) & mask) == mask)
+     @A))))
+
 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
 (for cst (INTEGER_CST VECTOR_CST)
  (simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126644.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr126644.c
new file mode 100644
index 00000000000..a005b8622ec
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126644.c
@@ -0,0 +1,58 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int f1 (int a)
+{
+  if ((a & 0xF0000000) != 0xF0000000)
+    __builtin_unreachable ();
+
+  return a << 3 >> 3;
+}
+
+/* Can't be optimized (not enough 1s to preserve sign).  */
+int g1 (int a)
+{
+  if ((a & 0xF0000000) != 0xE0000000)
+    __builtin_unreachable ();
+
+  return a << 3 >> 3;
+}
+
+int f2 (int a)
+{
+  if ((a & 0xF0000000) != 0) __builtin_unreachable ();
+
+  return a << 3 >> 3;
+}
+
+/* Can't be optimized (not enough 0s to preserve sign).  */
+int g2 (int a)
+{
+  if ((a & 0x10000000) != 0x10000000)
+    __builtin_unreachable ();
+
+  return a << 3 >> 3;
+}
+
+/* f3 and g3 are being optimized outside of 126644.  We're
+   including them here for test coverage completioness.  */
+
+unsigned int f3 (unsigned int a)
+{
+  if ((a & 0xE0000000) != 0) __builtin_unreachable ();
+
+  return a << 3 >> 3;
+}
+
+/* Can't be optimized (will lose a bit during lshift).
+   This simplifies into a bit_and.  */
+unsigned int g3 (unsigned int a)
+{
+  if ((a & 0xA0000000) != 0) __builtin_unreachable ();
+
+  return a << 3 >> 3;
+}
+
+/* { dg-final { scan-tree-dump-times " << " 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " >> " 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " & " 1 "optimized" } } */
-- 
2.43.0

Reply via email to