[PATCH v2] match.pd: (A << N) >> N -> A [PR126644]
Daniel Barboza
daniel.barboza@oss.qualcomm.com
Sun Oct 11 11:12:09 GMT 2026
This is done during RTL. Do it earlier in Gimple to potentially allow
more optimizations opportunities.
The same pattern have two distinct handlings depending on whether A is
unsigned or not. For unsigned types we'll check if there will be any
upper bits being lost during the lshift step, e.g. if A = 0x001.. we can
do the simplication for N=1,2 only. N = 3 and above would waste original
A bits and the end result wouldn't be the same as A anymore.
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 simplfication 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.
Bootstrapped and regression tested in x86_64, aarch64 and riscv64.
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 v1:
- document match.pd changes properly in commit msg;
- remove unneeded INTEGRAL_TYPE_P check;
- handle signed cases by checking if the lshift will
cause a signal change in the original SSA_NAME.
- v1 link: https://gcc.gnu.org/pipermail/gcc-patches/2026-October/733787.html
gcc/match.pd | 58 ++++++++++++++++++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr126644.c | 55 ++++++++++++++++++++++
2 files changed, 113 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126644.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 2d1161b860b..62e982cb9c2 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1561,6 +1561,64 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
{ wide_int_to_tree (type,
wi::lshift (wone, wi::to_wide (@2))); }))))
+#if GIMPLE
+/* PR126644: A << N >> N -> A
+ As long as the upper CST bits of A are known to be zero. */
+(simplify
+ (rshift (lshift@0 @A INTEGER_CST@N) @N)
+ (if (INTEGRAL_TYPE_P (type)
+ && tree_fits_uhwi_p (@N)
+ && tree_to_uhwi (@N) < TYPE_PRECISION (type)
+ && single_use (@0))
+ (switch
+ /* For unsigned types it's a matter of checking if we'll
+ lose any of the upper N bits during A << N. */
+ (if (TYPE_UNSIGNED (type))
+ (with {
+ unsigned int prec = TYPE_PRECISION (type);
+ wide_int mask = wi::mask (prec - tree_to_uhwi (@N),
+ true, prec);
+ }
+ (if ((tree_nonzero_bits (@A) & mask) == 0)
+ @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. */
+ (if (!TYPE_UNSIGNED (type))
+ (with {
+ bool no_sign_bit_change = true;
+ int_range_max vr;
+
+ if (gimple_match_range_of_expr (vr, @A)
+ && !vr.varying_p ()
+ && !vr.undefined_p ()
+ && vr.num_pairs () > 0)
+ {
+ int shift_amount = tree_to_uhwi (@N);
+ for (unsigned int i = 0; i < vr.num_pairs (); i++)
+ {
+ wide_int lb = vr.lower_bound (i);
+ wide_int ub = vr.upper_bound (i);
+
+ if (wi::clrsb (lb) < shift_amount
+ || wi::clrsb (ub) < shift_amount)
+ {
+ no_sign_bit_change = false;
+ break;
+ }
+ }
+ }
+ else
+ no_sign_bit_change = false;
+ }
+ (if (no_sign_bit_change)
+ @A))))))
+#endif
+
/* 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..b14fe905fa5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126644.c
@@ -0,0 +1,55 @@
+/* { 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;
+}
+
+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
More information about the Gcc-patches
mailing list