[PATCH] Handle right shifts by zero in range-ops.

Aldy Hernandez aldyh@redhat.com
Tue Oct 20 08:33:25 GMT 2020



On 10/19/20 4:22 PM, Jakub Jelinek wrote:
> On Mon, Oct 19, 2020 at 10:19:48AM -0400, Andrew MacLeod via Gcc-patches wrote:
>> On 10/19/20 5:38 AM, Aldy Hernandez wrote:
>>> If the shift amount in operator_lshift::op1_range was zero, an invalid range
>>> of [1, 0] was being created.
>>
>> Should we do the same thing with rshift::op1_range?     ie, return the LHS
>> if the shift is 0 instead of trying to figure it out....
> 
> Shift by 0 is a noop, so yes, for all of left/right shifts and rotates
> the result range should be the range of the first operand.

Sounds good.

Tested on x86-64 Linux.

Pushed.

     Special case shifting by zero in operator_rshift::op1_range.

     gcc/ChangeLog:

             * range-op.cc (operator_rshift::op1_range): Special case
             shifting by zero.

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 0efa00186e8..40d45b1ce47 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -1633,6 +1633,11 @@ operator_rshift::op1_range (irange &r,
  		    wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
  		    UNSIGNED))
  	return false;
+      if (wi::to_wide (shift) == 0)
+	{
+	  r = lhs;
+	  return true;
+	}

        // Folding the original operation may discard some impossible
        // ranges from the LHS.



More information about the Gcc-patches mailing list