[PATCH] D71690: [SCEV] get a more accurate range for AddRecExpr with nuw flag

ChenZheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 7 18:19:39 PST 2020


shchenz updated this revision to Diff 236736.
shchenz added a comment.

rebase after D64869 <https://reviews.llvm.org/D64869>


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

https://reviews.llvm.org/D71690

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
@@ -2,7 +2,7 @@
 
 ; copied from flags-from-poison.ll
 ; CHECK-LABEL: @test-add-nuw
-; CHECK: -->  {(1 + %offset)<nuw>,+,1}<nuw><%loop> U: full-set S: full-set
+; CHECK: -->  {(1 + %offset)<nuw>,+,1}<nuw><%loop> U: [1,0) S: full-set
 define void @test-add-nuw(float* %input, i32 %offset, i32 %numIterations) {
 entry:
   br label %loop
@@ -20,7 +20,7 @@
 }
 
 ; CHECK-LABEL: @test-addrec-nuw
-; CHECK: -->  {(1 + (10 smax %offset))<nuw>,+,1}<nuw><%loop> U: full-set S: full-set 
+; CHECK: -->  {(1 + (10 smax %offset))<nuw>,+,1}<nuw><%loop> U: [11,0) S: full-set
 define void @test-addrec-nuw(float* %input, i32 %offset, i32 %numIterations) {
 entry:
   %cmp = icmp sgt i32 %offset, 10
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -5661,11 +5661,17 @@
   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
     // If there's no unsigned wrap, the value will never be less than its
     // initial value.
-    if (AddRec->hasNoUnsignedWrap())
-      if (const SCEVConstant *C = dyn_cast<SCEVConstant>(AddRec->getStart()))
-        if (!C->getValue()->isZero())
-          ConservativeResult = ConservativeResult.intersectWith(
-              ConstantRange(C->getAPInt(), APInt(BitWidth, 0)), RangeType);
+    if (AddRec->hasNoUnsignedWrap()) {
+      APInt MinValue = APInt(BitWidth, 0);
+      const SCEV *Start = AddRec->getStart();
+      if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED)
+        MinValue = getUnsignedRangeMin(Start);
+      else if (isKnownNonNegative(Start) || isKnownNonPositive(Start))
+        MinValue = getSignedRangeMin(Start);
+      if (!MinValue.isNullValue())
+        ConservativeResult = ConservativeResult.intersectWith(
+            ConstantRange(MinValue, 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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71690.236736.patch
Type: text/x-patch
Size: 2275 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200108/d27bd806/attachment-0001.bin>


More information about the llvm-commits mailing list