[PATCH] range-ops: (nonzero | X) is nonzero

Aldy Hernandez aldyh@redhat.com
Tue Jun 15 14:14:54 GMT 2021



On 6/15/21 2:39 PM, Bernhard Reutner-Fischer wrote:
> On 15 June 2021 13:48:39 CEST, Aldy Hernandez via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
>> For bitwise or, nonzero|X is always nonzero.  Make sure we don't drop
>> to
>> varying in this case.
>>
>> This was found while examining differences between VRP/DOM threaders
>> and
>> the upcoming work, but it could be useful for any user of range-ops.
>>
>> Tested on x86-64 Linux.
>>
>> OK?
>>
>> gcc/ChangeLog:
>>
>> 	* range-op.cc (operator_bitwise_or::wi_fold): Make sure
>> 	nonzero|X is nonzero.
>> 	(range_op_bitwise_and_tests): Add tests for above.
>> ---
>> gcc/range-op.cc | 28 ++++++++++++++++++++++++----
>> 1 file changed, 24 insertions(+), 4 deletions(-)
>>
>> diff --git a/gcc/range-op.cc b/gcc/range-op.cc
>> index 742e54686b4..59978466b45 100644
>> --- a/gcc/range-op.cc
>> +++ b/gcc/range-op.cc
>> @@ -2534,11 +2534,20 @@ operator_bitwise_or::wi_fold (irange &r, tree
>> type,
>>      new_lb = wi::max (new_lb, lh_lb, sign);
>>    if (wi::lt_p (rh_ub, 0, sign))
>>      new_lb = wi::max (new_lb, rh_lb, sign);
>> -  // If the limits got swapped around, return varying.
>> +  // If the limits got swapped around, return a conservative range.
>>    if (wi::gt_p (new_lb, new_ub,sign))
> 
> Missing space before sign above?

Fixed.

> 
>> -    r.set_varying (type);
>> -  else
>> -    value_range_with_overflow (r, type, new_lb, new_ub);
>> +    {
>> +      // Make sure that nonzero|X is nonzero.
>> +      if (wi::gt_p (lh_lb, 0, sign)
>> +	  || wi::gt_p (rh_lb, 0, sign)
>> +	  || wi::lt_p (lh_ub, 0, sign)
>> +	  || wi::lt_p (rh_ub, 0, sign))
>> +	r.set_nonzero (type);
>> +      else
>> +	r.set_varying (type);
>> +      return;
>> +    }
>> +  value_range_with_overflow (r, type, new_lb, new_ub);
>> }
>>
>> bool
>> @@ -3744,6 +3753,17 @@ range_op_bitwise_and_tests ()
>>    i1 = int_range<1> (integer_type_node);
>>    op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
>>    ASSERT_TRUE (res == int_range<1> (integer_type_node));
>> +
>> +  // (NONZERO | X) is nonzero.
>> +  i1.set_nonzero (integer_type_node);
>> +  i2.set_varying (integer_type_node);
>> +  op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
>> +  ASSERT_TRUE (res.nonzero_p ());
>> +
>> +  // (NEGATIVE | X) is nonzero.
>> +  i1 = int_range<1> (INT (-5), INT (-3));
>> +  i2.set_varying (integer_type_node);
>> +  op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
> 
> Wouldn't you want to assert something here?

Whoops.  Thanks.

Aldy

For bitwise or, nonzero|X is always nonzero.  Make sure we don't drop to
varying in this case.

gcc/ChangeLog:

	* range-op.cc (operator_bitwise_or::wi_fold): Make sure
	nonzero|X is nonzero.
	(range_op_bitwise_and_tests): Add tests for above.
---
  gcc/range-op.cc | 31 ++++++++++++++++++++++++++-----
  1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 742e54686b4..e805f26a333 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -2534,11 +2534,20 @@ operator_bitwise_or::wi_fold (irange &r, tree type,
      new_lb = wi::max (new_lb, lh_lb, sign);
    if (wi::lt_p (rh_ub, 0, sign))
      new_lb = wi::max (new_lb, rh_lb, sign);
-  // If the limits got swapped around, return varying.
-  if (wi::gt_p (new_lb, new_ub,sign))
-    r.set_varying (type);
-  else
-    value_range_with_overflow (r, type, new_lb, new_ub);
+  // If the limits got swapped around, return a conservative range.
+  if (wi::gt_p (new_lb, new_ub, sign))
+    {
+      // Make sure that nonzero|X is nonzero.
+      if (wi::gt_p (lh_lb, 0, sign)
+	  || wi::gt_p (rh_lb, 0, sign)
+	  || wi::lt_p (lh_ub, 0, sign)
+	  || wi::lt_p (rh_ub, 0, sign))
+	r.set_nonzero (type);
+      else
+	r.set_varying (type);
+      return;
+    }
+  value_range_with_overflow (r, type, new_lb, new_ub);
  }

  bool
@@ -3744,6 +3753,18 @@ range_op_bitwise_and_tests ()
    i1 = int_range<1> (integer_type_node);
    op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
    ASSERT_TRUE (res == int_range<1> (integer_type_node));
+
+  // (NONZERO | X) is nonzero.
+  i1.set_nonzero (integer_type_node);
+  i2.set_varying (integer_type_node);
+  op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
+  ASSERT_TRUE (res.nonzero_p ());
+
+  // (NEGATIVE | X) is nonzero.
+  i1 = int_range<1> (INT (-5), INT (-3));
+  i2.set_varying (integer_type_node);
+  op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
+  ASSERT_FALSE (res.contains_p (INT (0)));
  }

  void
-- 
2.31.1




More information about the Gcc-patches mailing list