Index: llvm/lib/Analysis/ScalarEvolution.cpp =================================================================== --- llvm/lib/Analysis/ScalarEvolution.cpp +++ llvm/lib/Analysis/ScalarEvolution.cpp @@ -5565,9 +5565,21 @@ } if (const SCEVAddExpr *Add = dyn_cast(S)) { - ConstantRange X = getRangeRef(Add->getOperand(0), SignHint); - for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) - X = X.add(getRangeRef(Add->getOperand(i), SignHint)); + ConstantRange X(BitWidth, /*isFullSet=*/true); + if (maskFlags(Add->getNoWrapFlags(), SCEV::FlagNSW) == SCEV::FlagNSW) { + if (auto *C = dyn_cast(Add->getOperand(0))) { + X = getRangeRef(Add->getOperand(1), SignHint); + X = X.addWithNoSignedWrap(C->getAPInt()); + } else if (auto *C = dyn_cast(Add->getOperand(1))) { + X = getRangeRef(Add->getOperand(0), SignHint); + X = X.addWithNoSignedWrap(C->getAPInt()); + } + } else { + //TODO: handle SCEV::FLAGNUW. + X = getRangeRef(Add->getOperand(0), SignHint); + for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) + X = X.add(getRangeRef(Add->getOperand(i), SignHint)); + } return setRange(Add, SignHint, ConservativeResult.intersectWith(X, RangeType)); } 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/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) /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/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) 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) 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 @@ -227,6 +227,7 @@ ret void } +; CHECK-LABEL: even_step2 ; CHECK: Determining loop execution counts for: @even_step2 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (2 * %n)) /u 2) ; CHECK: Loop %loop: max backedge-taken count is 2 @@ -382,7 +383,7 @@ ; CHECK: Determining loop execution counts for: @even_nsw_startx ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) 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 +400,7 @@ ; CHECK: Determining loop execution counts for: @even_nsw_startx_step2 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) 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