aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorEugene Rozenfeld <erozen@microsoft.com>2020-11-05 20:35:45 -0700
committerJeff Law <law@redhat.com>2020-11-05 20:35:45 -0700
commit6483f05989ef31ebf2583deeb4753211ca2b7e17 (patch)
tree8c0622b9a031143be1cf642c310c5af5788b9080 /gcc
parent56ecdc2f02bfa69021003d7ac267dc65f385902f (diff)
downloadgcc-6483f05989ef31ebf2583deeb4753211ca2b7e17.zip
gcc-6483f05989ef31ebf2583deeb4753211ca2b7e17.tar.gz
gcc-6483f05989ef31ebf2583deeb4753211ca2b7e17.tar.bz2
Simplify x >> x to 0. This fixes PR96701.
gcc/ * match.pd (x >> x): New pattern. gcc/testsuite * gcc.dg/self-right-shift.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/match.pd5
-rw-r--r--gcc/testsuite/gcc.dg/self-right-shift.c12
2 files changed, 17 insertions, 0 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 17ba041..d82a620 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -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
index 0000000..c457ee5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/self-right-shift.c
@@ -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" } } */