[PATCH] D72436: [SCEV] get a more accurate range for AddRecExpr with nsw flag

ChenZheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 10 07:43:37 PST 2020


shchenz updated this revision to Diff 237322.
shchenz marked an inline comment as done.
shchenz added a comment.

operand 0 does not required to be NonPos/NonNeg


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72436/new/

https://reviews.llvm.org/D72436

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Analysis/ScalarEvolution/range_nw_flag.ll


Index: llvm/test/Analysis/ScalarEvolution/range_nw_flag.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/range_nw_flag.ll
+++ llvm/test/Analysis/ScalarEvolution/range_nw_flag.ll
@@ -40,7 +40,7 @@
 }
 
 ; CHECK-LABEL: @test-addrec-nsw
-; CHECK: -->  {(-1 + (-10 smin %offset))<nsw>,+,-1}<nsw><%loop> U: [-2147483648,1) S: [-2147483648,1)
+; CHECK: -->  {(-1 + (-10 smin %offset))<nsw>,+,-1}<nsw><%loop> U: [-2147483648,-10) S: [-2147483648,-10)
 define void @test-addrec-nsw(float* %input, i32 %offset, i32 %numIterations) {
 entry:
   %cmp = icmp slt i32 %offset, -10 
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -5678,23 +5678,37 @@
           ConservativeResult = ConservativeResult.intersectWith(
               ConstantRange(C->getAPInt(), APInt(BitWidth, 0)), RangeType);
 
-    // If there's no signed wrap, and all the operands have the same sign or
-    // zero, the value won't ever change sign.
+    // If there's no signed wrap, and all the operands except initial value have
+    // the same sign or zero, the value won't ever be:
+    // 1: smaller than initial value if operands are non negative,
+    // 2: bigger than initial value if operands are non positive.
+    // For both cases, value can not cross signed min/max boundary.
     if (AddRec->hasNoSignedWrap()) {
       bool AllNonNeg = true;
       bool AllNonPos = true;
-      for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
-        if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false;
-        if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false;
+      for (unsigned i = 1, e = AddRec->getNumOperands(); i != e; ++i) {
+        if (!isKnownNonNegative(AddRec->getOperand(i)))
+          AllNonNeg = false;
+        if (!isKnownNonPositive(AddRec->getOperand(i)))
+          AllNonPos = false;
+      }
+      if (AllNonNeg) {
+        APInt SignedMin = getSignedRangeMin(AddRec->getStart());
+        // If SignedMin is signed min value, [INT_MIN, INT_MIN) is not a valid
+        // constant range.
+        if (SignedMin != APInt::getSignedMinValue(BitWidth))
+          ConservativeResult = ConservativeResult.intersectWith(
+              ConstantRange(SignedMin, APInt::getSignedMinValue(BitWidth)),
+              RangeType);
+      } else if (AllNonPos) {
+        APInt SignedMax = getSignedRangeMax(AddRec->getStart());
+        // If SignedMax is signed max value, [INT_MIN, INT_MAX+1) = [INT_MIN,
+        // INT_MIN) is not a valid constant range.
+        if (SignedMax != APInt::getSignedMaxValue(BitWidth))
+          ConservativeResult = ConservativeResult.intersectWith(
+              ConstantRange(APInt::getSignedMinValue(BitWidth), SignedMax + 1),
+              RangeType);
       }
-      if (AllNonNeg)
-        ConservativeResult = ConservativeResult.intersectWith(
-          ConstantRange(APInt(BitWidth, 0),
-                        APInt::getSignedMinValue(BitWidth)), RangeType);
-      else if (AllNonPos)
-        ConservativeResult = ConservativeResult.intersectWith(
-          ConstantRange(APInt::getSignedMinValue(BitWidth),
-                        APInt(BitWidth, 1)), RangeType);
     }
 
     // TODO: non-affine addrec


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72436.237322.patch
Type: text/x-patch
Size: 3402 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200110/40fcb48c/attachment.bin>


More information about the llvm-commits mailing list