Skip to content

Commit 00fcc03

Browse files
committed
[SCEV] Fix incorrect loop exit count analysis.
In computeLoadConstantCompareExitLimit, the addrec used to compute the exit count should be from the loop which the exiting block belongs to. Reviewed by: mkazantsev Differential Revision: https://reviews.llvm.org/D92367
1 parent 30d9ca1 commit 00fcc03

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7843,9 +7843,10 @@ ScalarEvolution::computeLoadConstantCompareExitLimit(
78437843
Idx = getSCEVAtScope(Idx, L);
78447844

78457845
// We can only recognize very limited forms of loop index expressions, in
7846-
// particular, only affine AddRec's like {C1,+,C2}.
7846+
// particular, only affine AddRec's like {C1,+,C2}<L>.
78477847
const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
7848-
if (!IdxExpr || !IdxExpr->isAffine() || isLoopInvariant(IdxExpr, L) ||
7848+
if (!IdxExpr || IdxExpr->getLoop() != L || !IdxExpr->isAffine() ||
7849+
isLoopInvariant(IdxExpr, L) ||
78497850
!isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
78507851
!isa<SCEVConstant>(IdxExpr->getOperand(1)))
78517852
return getCouldNotCompute();

llvm/test/Analysis/ScalarEvolution/incorrect-exit-count.ll

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ define dso_local i32 @f() {
1616
; CHECK-LABEL: 'f'
1717
; CHECK-NEXT: Classifying expressions for: @f
1818
; CHECK-NEXT: %storemerge23 = phi i32 [ 3, %entry ], [ %dec16, %for.inc13.3 ]
19-
; CHECK-NEXT: --> {3,+,-1}<nsw><%outer.loop> U: [-2147483648,4) S: [-2147483648,4) Exits: 3 LoopDispositions: { %outer.loop: Computable, %for.cond6: Invariant, %inner.loop: Invariant }
19+
; CHECK-NEXT: --> {3,+,-1}<nsw><%outer.loop> U: [1,4) S: [1,4) Exits: <<Unknown>> LoopDispositions: { %outer.loop: Computable, %for.cond6: Invariant, %inner.loop: Invariant }
2020
; CHECK-NEXT: %storemerge1921 = phi i32 [ 3, %outer.loop ], [ %dec, %for.end ]
2121
; CHECK-NEXT: --> {3,+,-1}<nuw><nsw><%for.cond6> U: [3,4) S: [3,4) Exits: <<Unknown>> LoopDispositions: { %for.cond6: Computable, %outer.loop: Variant }
2222
; CHECK-NEXT: %idxprom20 = zext i32 %storemerge1921 to i64
@@ -56,7 +56,7 @@ define dso_local i32 @f() {
5656
; CHECK-NEXT: %storemerge1921.lcssa25.3 = phi i32 [ %storemerge1921.3, %for.end.3 ]
5757
; CHECK-NEXT: --> %storemerge1921.lcssa25.3 U: [3,4) S: [3,4) Exits: <<Unknown>> LoopDispositions: { %outer.loop: Variant, %for.cond6: Invariant, %inner.loop: Invariant }
5858
; CHECK-NEXT: %dec16 = add nsw i32 %storemerge23, -1
59-
; CHECK-NEXT: --> {2,+,-1}<nw><%outer.loop> U: full-set S: full-set Exits: 2 LoopDispositions: { %outer.loop: Computable, %for.cond6: Invariant, %inner.loop: Invariant }
59+
; CHECK-NEXT: --> {2,+,-1}<nsw><%outer.loop> U: [0,3) S: [0,3) Exits: <<Unknown>> LoopDispositions: { %outer.loop: Computable, %for.cond6: Invariant, %inner.loop: Invariant }
6060
; CHECK-NEXT: Determining loop execution counts for: @f
6161
; CHECK-NEXT: Loop %for.cond6: <multiple exits> Unpredictable backedge-taken count.
6262
; CHECK-NEXT: exit count for for.cond6: 0
@@ -68,14 +68,12 @@ define dso_local i32 @f() {
6868
; CHECK-NEXT: exit count for for.end.3: ***COULDNOTCOMPUTE***
6969
; CHECK-NEXT: Loop %inner.loop: max backedge-taken count is 0
7070
; CHECK-NEXT: Loop %inner.loop: Unpredictable predicated backedge-taken count.
71-
; CHECK-NEXT: Loop %outer.loop: <multiple exits> backedge-taken count is 0
72-
; CHECK-NEXT: exit count for for.cond6: 0
73-
; CHECK-NEXT: exit count for inner.loop: 0
71+
; CHECK-NEXT: Loop %outer.loop: <multiple exits> Unpredictable backedge-taken count.
72+
; CHECK-NEXT: exit count for for.cond6: ***COULDNOTCOMPUTE***
73+
; CHECK-NEXT: exit count for inner.loop: ***COULDNOTCOMPUTE***
7474
; CHECK-NEXT: exit count for for.inc13.3: 2
75-
; CHECK-NEXT: Loop %outer.loop: max backedge-taken count is 0
76-
; CHECK-NEXT: Loop %outer.loop: Predicated backedge-taken count is 0
77-
; CHECK-NEXT: Predicates:
78-
; CHECK: Loop %outer.loop: Trip multiple is 0
75+
; CHECK-NEXT: Loop %outer.loop: max backedge-taken count is 2
76+
; CHECK-NEXT: Loop %outer.loop: Unpredictable predicated backedge-taken count.
7977
;
8078
entry:
8179
store i32 3, i32* @a, align 4

0 commit comments

Comments
 (0)