Skip to content

Commit

Permalink
start range
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Apr 6, 2023
1 parent 2caaec6 commit 87d1063
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 64 deletions.
10 changes: 5 additions & 5 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6672,8 +6672,9 @@ const ConstantRange &ScalarEvolution::getRangeRef(
const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(S);
// If there's no unsigned wrap, the value will never be less than its
// initial value.
const SCEV *Start = applyLoopGuards(AddRec->getStart(), AddRec->getLoop());
if (AddRec->hasNoUnsignedWrap()) {
APInt UnsignedMinValue = getUnsignedRangeMin(AddRec->getStart());
APInt UnsignedMinValue = getUnsignedRangeMin(Start);
if (!UnsignedMinValue.isZero())
ConservativeResult = ConservativeResult.intersectWith(
ConstantRange(UnsignedMinValue, APInt(BitWidth, 0)), RangeType);
Expand All @@ -6695,14 +6696,13 @@ const ConstantRange &ScalarEvolution::getRangeRef(
}
if (AllNonNeg)
ConservativeResult = ConservativeResult.intersectWith(
ConstantRange::getNonEmpty(getSignedRangeMin(AddRec->getStart()),
ConstantRange::getNonEmpty(getSignedRangeMin(Start),
APInt::getSignedMinValue(BitWidth)),
RangeType);
else if (AllNonPos)
ConservativeResult = ConservativeResult.intersectWith(
ConstantRange::getNonEmpty(APInt::getSignedMinValue(BitWidth),
getSignedRangeMax(AddRec->getStart()) +
1),
getSignedRangeMax(Start) + 1),
RangeType);
}

Expand All @@ -6713,7 +6713,7 @@ const ConstantRange &ScalarEvolution::getRangeRef(
if (!isa<SCEVCouldNotCompute>(MaxBECount) &&
getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
auto RangeFromAffine = getRangeForAffineAR(
AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount,
Start, AddRec->getStepRecurrence(*this), MaxBECount,
BitWidth);
ConservativeResult =
ConservativeResult.intersectWith(RangeFromAffine, RangeType);
Expand Down
16 changes: 8 additions & 8 deletions llvm/test/Analysis/ScalarEvolution/decrementing_addrecs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ define i32 @test_step_1_flags(i32 %n) {
; DEFAULT-NEXT: %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
; DEFAULT-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,2147483647) S: [0,2147483647) Exits: (-1 + %n) LoopDispositions: { %loop: Computable }
; DEFAULT-NEXT: %j = phi i32 [ %n.minus.1, %entry ], [ %j.next, %loop ]
; DEFAULT-NEXT: --> {(-1 + %n),+,-1}<nsw><%loop> U: full-set S: full-set Exits: 0 LoopDispositions: { %loop: Computable }
; DEFAULT-NEXT: --> {(-1 + %n),+,-1}<nsw><%loop> U: [-2147483646,2147483647) S: [-2147483646,2147483647) Exits: 0 LoopDispositions: { %loop: Computable }
; DEFAULT-NEXT: %a = sub i32 %n, %i
; DEFAULT-NEXT: --> {%n,+,-1}<nw><%loop> U: full-set S: full-set Exits: 1 LoopDispositions: { %loop: Computable }
; DEFAULT-NEXT: --> {%n,+,-1}<nw><%loop> U: [-2147483645,-2147483648) S: [-2147483645,-2147483648) Exits: 1 LoopDispositions: { %loop: Computable }
; DEFAULT-NEXT: %b = sub i32 %n.minus.1, %i
; DEFAULT-NEXT: --> {(-1 + %n),+,-1}<nsw><%loop> U: full-set S: full-set Exits: 0 LoopDispositions: { %loop: Computable }
; DEFAULT-NEXT: --> {(-1 + %n),+,-1}<nsw><%loop> U: [-2147483646,2147483647) S: [-2147483646,2147483647) Exits: 0 LoopDispositions: { %loop: Computable }
; DEFAULT-NEXT: %c = sub i32 2147483647, %i
; DEFAULT-NEXT: --> {2147483647,+,-1}<nw><%loop> U: [1,-2147483648) S: [1,-2147483648) Exits: (-2147483648 + (-1 * %n)) LoopDispositions: { %loop: Computable }
; DEFAULT-NEXT: %i.next = add nuw nsw i32 %i, 1
; DEFAULT-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,-2147483648) S: [1,-2147483648) Exits: %n LoopDispositions: { %loop: Computable }
; DEFAULT-NEXT: %j.next = add nsw i32 %j, -1
; DEFAULT-NEXT: --> {(-2 + %n),+,-1}<nw><%loop> U: full-set S: full-set Exits: -1 LoopDispositions: { %loop: Computable }
; DEFAULT-NEXT: --> {(-2 + %n),+,-1}<nsw><%loop> U: [-2147483647,2147483646) S: [-2147483647,2147483646) Exits: -1 LoopDispositions: { %loop: Computable }
; DEFAULT-NEXT: Determining loop execution counts for: @test_step_1_flags
; DEFAULT-NEXT: Loop %loop: backedge-taken count is (-1 + %n)
; DEFAULT-NEXT: Loop %loop: constant max backedge-taken count is 2147483646
Expand All @@ -60,17 +60,17 @@ define i32 @test_step_1_flags(i32 %n) {
; EXPENSIVE_SHARPENING-NEXT: %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
; EXPENSIVE_SHARPENING-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,2147483647) S: [0,2147483647) Exits: (-1 + %n) LoopDispositions: { %loop: Computable }
; EXPENSIVE_SHARPENING-NEXT: %j = phi i32 [ %n.minus.1, %entry ], [ %j.next, %loop ]
; EXPENSIVE_SHARPENING-NEXT: --> {(-1 + %n),+,-1}<nsw><%loop> U: full-set S: full-set Exits: 0 LoopDispositions: { %loop: Computable }
; EXPENSIVE_SHARPENING-NEXT: --> {(-1 + %n),+,-1}<nsw><%loop> U: [-2147483646,2147483647) S: [-2147483646,2147483647) Exits: 0 LoopDispositions: { %loop: Computable }
; EXPENSIVE_SHARPENING-NEXT: %a = sub i32 %n, %i
; EXPENSIVE_SHARPENING-NEXT: --> {%n,+,-1}<nw><%loop> U: full-set S: full-set Exits: 1 LoopDispositions: { %loop: Computable }
; EXPENSIVE_SHARPENING-NEXT: --> {%n,+,-1}<nw><%loop> U: [-2147483645,-2147483648) S: [-2147483645,-2147483648) Exits: 1 LoopDispositions: { %loop: Computable }
; EXPENSIVE_SHARPENING-NEXT: %b = sub i32 %n.minus.1, %i
; EXPENSIVE_SHARPENING-NEXT: --> {(-1 + %n),+,-1}<nsw><%loop> U: full-set S: full-set Exits: 0 LoopDispositions: { %loop: Computable }
; EXPENSIVE_SHARPENING-NEXT: --> {(-1 + %n),+,-1}<nsw><%loop> U: [-2147483646,2147483647) S: [-2147483646,2147483647) Exits: 0 LoopDispositions: { %loop: Computable }
; EXPENSIVE_SHARPENING-NEXT: %c = sub i32 2147483647, %i
; EXPENSIVE_SHARPENING-NEXT: --> {2147483647,+,-1}<nw><%loop> U: [1,-2147483648) S: [1,-2147483648) Exits: (-2147483648 + (-1 * %n)) LoopDispositions: { %loop: Computable }
; EXPENSIVE_SHARPENING-NEXT: %i.next = add nuw nsw i32 %i, 1
; EXPENSIVE_SHARPENING-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,-2147483648) S: [1,-2147483648) Exits: %n LoopDispositions: { %loop: Computable }
; EXPENSIVE_SHARPENING-NEXT: %j.next = add nsw i32 %j, -1
; EXPENSIVE_SHARPENING-NEXT: --> {(-2 + %n),+,-1}<nw><%loop> U: full-set S: full-set Exits: -1 LoopDispositions: { %loop: Computable }
; EXPENSIVE_SHARPENING-NEXT: --> {(-2 + %n),+,-1}<nsw><%loop> U: [-2147483647,2147483646) S: [-2147483647,2147483646) Exits: -1 LoopDispositions: { %loop: Computable }
; EXPENSIVE_SHARPENING-NEXT: Determining loop execution counts for: @test_step_1_flags
; EXPENSIVE_SHARPENING-NEXT: Loop %loop: backedge-taken count is (-1 + %n)
; EXPENSIVE_SHARPENING-NEXT: Loop %loop: constant max backedge-taken count is 2147483646
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ define void @test_and(i16 %in) {
; CHECK-NEXT: %_tmp25.i = phi i16 [ %in, %bb2 ], [ %_tmp6.i, %bb1.i ]
; CHECK-NEXT: --> {%in,+,1}<nsw><%bb1.i> U: full-set S: full-set Exits: 1 LoopDispositions: { %bb1.i: Computable, %bb2: Variant }
; CHECK-NEXT: %_tmp6.i = add nsw i16 %_tmp25.i, 1
; CHECK-NEXT: --> {(1 + %in),+,1}<nsw><%bb1.i> U: full-set S: full-set Exits: 2 LoopDispositions: { %bb1.i: Computable, %bb2: Variant }
; CHECK-NEXT: --> {(1 + %in),+,1}<nsw><%bb1.i> U: [-32767,-32768) S: [-32767,-32768) Exits: 2 LoopDispositions: { %bb1.i: Computable, %bb2: Variant }
; CHECK-NEXT: %or.cond = and i1 %_tmp10.i, %exitcond.i
; CHECK-NEXT: --> (%_tmp10.i umin %exitcond.i) U: full-set S: full-set Exits: true LoopDispositions: { %bb1.i: Variant, %bb2: Variant }
; CHECK-NEXT: Determining loop execution counts for: @test_and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,9 @@ define void @guard_pessimizes_analysis_step2(i1 %c, i32 %N) {
; CHECK-NEXT: %init = phi i64 [ 2, %entry ], [ 4, %bb1 ]
; CHECK-NEXT: --> %init U: [2,5) S: [2,5)
; CHECK-NEXT: %iv = phi i64 [ %iv.next, %loop ], [ %init, %loop.ph ]
; CHECK-NEXT: --> {%init,+,2}<nuw><nsw><%loop> U: [2,17) S: [2,17) Exits: ((2 * ((14 + (-1 * %init)<nsw>)<nsw> /u 2))<nuw><nsw> + %init) LoopDispositions: { %loop: Computable }
; CHECK-NEXT: --> {%init,+,2}<nuw><nsw><%loop> U: [0,17) S: [0,17) Exits: ((2 * ((14 + (-1 * %init)<nsw>)<nsw> /u 2))<nuw><nsw> + %init) LoopDispositions: { %loop: Computable }
; CHECK-NEXT: %iv.next = add i64 %iv, 2
; CHECK-NEXT: --> {(2 + %init)<nuw><nsw>,+,2}<nuw><nsw><%loop> U: [4,19) S: [4,19) Exits: (2 + (2 * ((14 + (-1 * %init)<nsw>)<nsw> /u 2))<nuw><nsw> + %init) LoopDispositions: { %loop: Computable }
; CHECK-NEXT: --> {(2 + %init)<nuw><nsw>,+,2}<nuw><nsw><%loop> U: [2,19) S: [2,19) Exits: (2 + (2 * ((14 + (-1 * %init)<nsw>)<nsw> /u 2))<nuw><nsw> + %init) LoopDispositions: { %loop: Computable }
; CHECK-NEXT: Determining loop execution counts for: @guard_pessimizes_analysis_step2
; CHECK-NEXT: Loop %loop: backedge-taken count is ((14 + (-1 * %init)<nsw>)<nsw> /u 2)
; CHECK-NEXT: Loop %loop: constant max backedge-taken count is 6
Expand Down
Loading

0 comments on commit 87d1063

Please sign in to comment.