[PATCH] D64869: [SCEV] get more accurate range for AddExpr with NW flag

ChenZheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 8 20:09:11 PDT 2019


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

address @nikic comments


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

https://reviews.llvm.org/D64869

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Analysis/ScalarEvolution/max-trip-count.ll
  llvm/test/Analysis/ScalarEvolution/trip-count12.ll
  llvm/test/Analysis/ScalarEvolution/trip-count9.ll


Index: llvm/test/Analysis/ScalarEvolution/trip-count9.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/trip-count9.ll
+++ llvm/test/Analysis/ScalarEvolution/trip-count9.ll
@@ -180,7 +180,7 @@
 
 ; CHECK: Determining loop execution counts for: @nsw_startx
 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x)<nsw> smax %n))
-; CHECK: Loop %loop: max backedge-taken count is -1
+; CHECK: Loop %loop: max backedge-taken count is -2
 define void @nsw_startx(i4 %n, i4 %x) {
 entry:
   %s = icmp sgt i4 %n, 0
@@ -196,7 +196,7 @@
 
 ; CHECK: Determining loop execution counts for: @nsw_startx_step2
 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x)<nsw> smax %n)) /u 2)
-; CHECK: Loop %loop: max backedge-taken count is 7
+; CHECK: Loop %loop: max backedge-taken count is 6
 define void @nsw_startx_step2(i4 %n, i4 %x) {
 entry:
   %s = icmp sgt i4 %n, 0
@@ -382,7 +382,7 @@
 
 ; CHECK: Determining loop execution counts for: @even_nsw_startx
 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x)<nsw> smax (2 * %n)))
-; CHECK: Loop %loop: max backedge-taken count is -2
+; CHECK: Loop %loop: max backedge-taken count is -3
 define void @even_nsw_startx(i4 %n, i4 %x) {
 entry:
   %m = shl i4 %n, 1
@@ -399,7 +399,7 @@
 
 ; CHECK: Determining loop execution counts for: @even_nsw_startx_step2
 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x)<nsw> smax (2 * %n))) /u 2)
-; CHECK: Loop %loop: max backedge-taken count is 7
+; CHECK: Loop %loop: max backedge-taken count is 6
 define void @even_nsw_startx_step2(i4 %n, i4 %x) {
 entry:
   %m = shl i4 %n, 1
Index: llvm/test/Analysis/ScalarEvolution/trip-count12.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/trip-count12.ll
+++ llvm/test/Analysis/ScalarEvolution/trip-count12.ll
@@ -2,7 +2,7 @@
 
 ; CHECK: Determining loop execution counts for: @test
 ; CHECK: Loop %for.body: backedge-taken count is ((-2 + %len)<nsw> /u 2)
-; CHECK: Loop %for.body: max backedge-taken count is 1073741823
+; CHECK: Loop %for.body: max backedge-taken count is 1073741822
 
 define zeroext i16 @test(i16* nocapture %p, i32 %len) nounwind readonly {
 entry:
Index: llvm/test/Analysis/ScalarEvolution/max-trip-count.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/max-trip-count.ll
+++ llvm/test/Analysis/ScalarEvolution/max-trip-count.ll
@@ -342,7 +342,7 @@
 define void @changing_end_bound3(i32 %start, i32* %n_addr, i32* %addr) {
 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound3
 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
-; CHECK: Loop %loop: max backedge-taken count is 1073741823
+; CHECK: Loop %loop: max backedge-taken count is 1073741822
 entry:
   br label %loop
 
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -5546,6 +5546,7 @@
 
   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
     return setRange(C, SignHint, ConstantRange(C->getAPInt()));
+  using OBO = OverflowingBinaryOperator;
 
   unsigned BitWidth = getTypeSizeInBits(S->getType());
   ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
@@ -5566,8 +5567,16 @@
 
   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
     ConstantRange X = getRangeRef(Add->getOperand(0), SignHint);
+    unsigned WrapType = OBO::AnyWrap;
+    if (Add->hasNoSignedWrap() &&
+        SignHint == ScalarEvolution::HINT_RANGE_SIGNED)
+      WrapType = OBO::NoSignedWrap;
+    else if (Add->hasNoUnsignedWrap() &&
+             SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED)
+      WrapType = OBO::NoUnsignedWrap;
     for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
-      X = X.add(getRangeRef(Add->getOperand(i), SignHint));
+      X = X.addWithNoWrap(getRangeRef(Add->getOperand(i), SignHint),
+                          WrapType);
     return setRange(Add, SignHint,
                     ConservativeResult.intersectWith(X, RangeType));
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64869.219287.patch
Type: text/x-patch
Size: 4240 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190909/535c34e0/attachment.bin>


More information about the llvm-commits mailing list