From f0b7d4cc49ddb1c2c7474cc3f61e260aa93a96c0 Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Thu, 14 Oct 2021 10:43:58 -0400 Subject: Simplification for right shift. When the first operand of a signed right shift is zero or negative one, the RHS doesn't matter and the shift can be converted to a copy. PR tree-optimization/102738 gcc/ * vr-values.c (simplify_using_ranges::simplify): Handle RSHIFT_EXPR. gcc/testsuite * gcc.dg/pr102738.c: New. --- gcc/vr-values.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'gcc/vr-values.c') diff --git a/gcc/vr-values.c b/gcc/vr-values.c index 9bf58f4..d0f7cb4 100644 --- a/gcc/vr-values.c +++ b/gcc/vr-values.c @@ -4281,6 +4281,28 @@ simplify_using_ranges::simplify (gimple_stmt_iterator *gsi) case MAX_EXPR: return simplify_min_or_max_using_ranges (gsi, stmt); + case RSHIFT_EXPR: + { + tree op0 = gimple_assign_rhs1 (stmt); + tree type = TREE_TYPE (op0); + int_range_max range; + if (TYPE_SIGN (type) == SIGNED + && query->range_of_expr (range, op0, stmt)) + { + unsigned prec = TYPE_PRECISION (TREE_TYPE (op0)); + int_range<2> nzm1 (type, wi::minus_one (prec), wi::zero (prec), + VR_ANTI_RANGE); + range.intersect (nzm1); + // If there are no ranges other than [-1, 0] remove the shift. + if (range.undefined_p ()) + { + gimple_assign_set_rhs_from_tree (gsi, op0); + return true; + } + return false; + } + break; + } default: break; } -- cgit v1.1