From 6483f05989ef31ebf2583deeb4753211ca2b7e17 Mon Sep 17 00:00:00 2001 From: Eugene Rozenfeld Date: Thu, 5 Nov 2020 20:35:45 -0700 Subject: [PATCH] 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. --- gcc/match.pd | 5 +++++ gcc/testsuite/gcc.dg/self-right-shift.c | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/self-right-shift.c diff --git a/gcc/match.pd b/gcc/match.pd index 17ba04100c7..d82a62052fc 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 00000000000..c457ee53ec4 --- /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" } } */ -- 2.30.2