Simplify x >> x to 0. This fixes PR96701.
authorEugene Rozenfeld <erozen@microsoft.com>
Fri, 6 Nov 2020 03:35:45 +0000 (20:35 -0700)
committerJeff Law <law@redhat.com>
Fri, 6 Nov 2020 03:35:45 +0000 (20:35 -0700)
gcc/
* match.pd (x >> x): New pattern.

gcc/testsuite

* gcc.dg/self-right-shift.c: New test.

gcc/match.pd
gcc/testsuite/gcc.dg/self-right-shift.c [new file with mode: 0644]

index 17ba04100c784681b475aef620d60e89be2460ae..d82a62052fc77c372b2899063525fa36387e0bea 100644 (file)
@@ -2925,6 +2925,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
 
+/* Optimize x >> x into 0 */
+(simplify
+ (rshift @0 @0)
+  { build_zero_cst (type); })
+
 (for shiftrotate (lrotate rrotate lshift rshift)
  (simplify
   (shiftrotate @0 integer_zerop)
diff --git a/gcc/testsuite/gcc.dg/self-right-shift.c b/gcc/testsuite/gcc.dg/self-right-shift.c
new file mode 100644 (file)
index 0000000..c457ee5
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* Self right-shift should be optimized to 0. */
+
+int
+foo (int i)
+{
+  return i >> i;
+}
+
+/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */