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(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(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)); } 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 @@ -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) 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) 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