gcc/
* match.pd (x >> x): New pattern.
gcc/testsuite
* gcc.dg/self-right-shift.c: New test.
&& (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)
--- /dev/null
+/* { 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" } } */