Skip to content

Commit 5207168

Browse files
committed
[SCEV] Clarify behavior around max backedge taken count
This change makes the split between the "exact" backedge taken count and the "maximum" backedge taken count a bit more obvious. Both of these are upper bounds on the number of times the loop header executes (since SCEV does not account for most kinds of abnormal control flow), but the latter is guaranteed to be a constant. There were a few places where the max backedge taken count *was* a non-constant; I've changed those to compute constants instead. At this point, I'm not sure if the constant max backedge count can be computed by calling `getUnsignedRange(Exact).getUnsignedMax()` without losing precision. If it can, we can simplify even further by making `getMaxBackedgeTakenCount` a thin wrapper around `getBackedgeTakenCount` and `getUnsignedRange`. llvm-svn: 303497
1 parent 9fbfeef commit 5207168

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,12 @@ class ScalarEvolution {
656656
/// Test whether this BackedgeTakenInfo contains complete information.
657657
bool hasFullInfo() const { return isComplete(); }
658658

659-
/// Return an expression indicating the exact backedge-taken count of the
660-
/// loop if it is known or SCEVCouldNotCompute otherwise. This is the
661-
/// number of times the loop header can be guaranteed to execute, minus
662-
/// one.
659+
/// Return an expression indicating the exact *backedge-taken*
660+
/// count of the loop if it is known or SCEVCouldNotCompute
661+
/// otherwise. If execution makes it to the backedge on every
662+
/// iteration (i.e. there are no abnormal exists like exception
663+
/// throws and thread exits) then this is the number of times the
664+
/// loop header will execute minus one.
663665
///
664666
/// If the SCEV predicate associated with the answer can be different
665667
/// from AlwaysTrue, we must add a (non null) Predicates argument.
@@ -1398,11 +1400,11 @@ class ScalarEvolution {
13981400
const SCEV *getExitCount(const Loop *L, BasicBlock *ExitingBlock);
13991401

14001402
/// If the specified loop has a predictable backedge-taken count, return it,
1401-
/// otherwise return a SCEVCouldNotCompute object. The backedge-taken count
1402-
/// is the number of times the loop header will be branched to from within
1403-
/// the loop. This is one less than the trip count of the loop, since it
1404-
/// doesn't count the first iteration, when the header is branched to from
1405-
/// outside the loop.
1403+
/// otherwise return a SCEVCouldNotCompute object. The backedge-taken count is
1404+
/// the number of times the loop header will be branched to from within the
1405+
/// loop, assuming there are no abnormal exists like exception throws. This is
1406+
/// one less than the trip count of the loop, since it doesn't count the first
1407+
/// iteration, when the header is branched to from outside the loop.
14061408
///
14071409
/// Note that it is not valid to call this method on a loop without a
14081410
/// loop-invariant backedge-taken count (see
@@ -1417,8 +1419,10 @@ class ScalarEvolution {
14171419
const SCEV *getPredicatedBackedgeTakenCount(const Loop *L,
14181420
SCEVUnionPredicate &Predicates);
14191421

1420-
/// Similar to getBackedgeTakenCount, except return the least SCEV value
1421-
/// that is known never to be less than the actual backedge taken count.
1422+
/// When successful, this returns a SCEVConstant that is greater than or equal
1423+
/// to (i.e. a "conservative over-approximation") of the value returend by
1424+
/// getBackedgeTakenCount. If such a value cannot be computed, it returns the
1425+
/// SCEVCouldNotCompute object.
14221426
const SCEV *getMaxBackedgeTakenCount(const Loop *L);
14231427

14241428
/// Return true if the backedge taken count is either the value returned by

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5947,6 +5947,8 @@ ScalarEvolution::BackedgeTakenInfo::getMax(ScalarEvolution *SE) const {
59475947
if (any_of(ExitNotTaken, PredicateNotAlwaysTrue) || !getMax())
59485948
return SE->getCouldNotCompute();
59495949

5950+
assert((isa<SCEVCouldNotCompute>(getMax()) || isa<SCEVConstant>(getMax())) &&
5951+
"No point in having a non-constant max backedge taken count!");
59505952
return getMax();
59515953
}
59525954

@@ -5972,7 +5974,11 @@ bool ScalarEvolution::BackedgeTakenInfo::hasOperand(const SCEV *S,
59725974
}
59735975

59745976
ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E)
5975-
: ExactNotTaken(E), MaxNotTaken(E), MaxOrZero(false) {}
5977+
: ExactNotTaken(E), MaxNotTaken(E), MaxOrZero(false) {
5978+
assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
5979+
isa<SCEVConstant>(MaxNotTaken)) &&
5980+
"No point in having a non-constant max backedge taken count!");
5981+
}
59765982

59775983
ScalarEvolution::ExitLimit::ExitLimit(
59785984
const SCEV *E, const SCEV *M, bool MaxOrZero,
@@ -5981,6 +5987,9 @@ ScalarEvolution::ExitLimit::ExitLimit(
59815987
assert((isa<SCEVCouldNotCompute>(ExactNotTaken) ||
59825988
!isa<SCEVCouldNotCompute>(MaxNotTaken)) &&
59835989
"Exact is not allowed to be less precise than Max");
5990+
assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
5991+
isa<SCEVConstant>(MaxNotTaken)) &&
5992+
"No point in having a non-constant max backedge taken count!");
59845993
for (auto *PredSet : PredSetList)
59855994
for (auto *P : *PredSet)
59865995
addPredicate(P);
@@ -5989,11 +5998,19 @@ ScalarEvolution::ExitLimit::ExitLimit(
59895998
ScalarEvolution::ExitLimit::ExitLimit(
59905999
const SCEV *E, const SCEV *M, bool MaxOrZero,
59916000
const SmallPtrSetImpl<const SCEVPredicate *> &PredSet)
5992-
: ExitLimit(E, M, MaxOrZero, {&PredSet}) {}
6001+
: ExitLimit(E, M, MaxOrZero, {&PredSet}) {
6002+
assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6003+
isa<SCEVConstant>(MaxNotTaken)) &&
6004+
"No point in having a non-constant max backedge taken count!");
6005+
}
59936006

59946007
ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E, const SCEV *M,
59956008
bool MaxOrZero)
5996-
: ExitLimit(E, M, MaxOrZero, None) {}
6009+
: ExitLimit(E, M, MaxOrZero, None) {
6010+
assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6011+
isa<SCEVConstant>(MaxNotTaken)) &&
6012+
"No point in having a non-constant max backedge taken count!");
6013+
}
59976014

59986015
/// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each
59996016
/// computable exit into a persistent ExitNotTakenInfo array.
@@ -6018,6 +6035,8 @@ ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo(
60186035

60196036
return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, std::move(Predicate));
60206037
});
6038+
assert((isa<SCEVCouldNotCompute>(MaxCount) || isa<SCEVConstant>(MaxCount)) &&
6039+
"No point in having a non-constant max backedge taken count!");
60216040
}
60226041

60236042
/// Invalidate this result and free the ExitNotTakenInfo array.
@@ -6279,7 +6298,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl(
62796298
// to not.
62806299
if (isa<SCEVCouldNotCompute>(MaxBECount) &&
62816300
!isa<SCEVCouldNotCompute>(BECount))
6282-
MaxBECount = BECount;
6301+
MaxBECount = getConstant(getUnsignedRange(BECount).getUnsignedMax());
62836302

62846303
return ExitLimit(BECount, MaxBECount, false,
62856304
{&EL0.Predicates, &EL1.Predicates});
@@ -7583,13 +7602,20 @@ ScalarEvolution::howFarToZero(const SCEV *V, const Loop *L, bool ControlsExit,
75837602
loopHasNoAbnormalExits(AddRec->getLoop())) {
75847603
const SCEV *Exact =
75857604
getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
7586-
return ExitLimit(Exact, Exact, false, Predicates);
7605+
const SCEV *Max =
7606+
Exact == getCouldNotCompute()
7607+
? Exact
7608+
: getConstant(getUnsignedRange(Exact).getUnsignedMax());
7609+
return ExitLimit(Exact, Max, false, Predicates);
75877610
}
75887611

75897612
// Solve the general equation.
7590-
const SCEV *E = SolveLinEquationWithOverflow(
7591-
StepC->getAPInt(), getNegativeSCEV(Start), *this);
7592-
return ExitLimit(E, E, false, Predicates);
7613+
const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(),
7614+
getNegativeSCEV(Start), *this);
7615+
const SCEV *M = E == getCouldNotCompute()
7616+
? E
7617+
: getConstant(getUnsignedRange(E).getUnsignedMax());
7618+
return ExitLimit(E, M, false, Predicates);
75937619
}
75947620

75957621
ScalarEvolution::ExitLimit
@@ -9218,8 +9244,9 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
92189244
getConstant(StrideForMaxBECount), false);
92199245
}
92209246

9221-
if (isa<SCEVCouldNotCompute>(MaxBECount))
9222-
MaxBECount = BECount;
9247+
if (isa<SCEVCouldNotCompute>(MaxBECount) &&
9248+
!isa<SCEVCouldNotCompute>(BECount))
9249+
MaxBECount = getConstant(getUnsignedRange(BECount).getUnsignedMax());
92239250

92249251
return ExitLimit(BECount, MaxBECount, MaxOrZero, Predicates);
92259252
}

llvm/test/Analysis/ScalarEvolution/nsw.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ for.body.i.i: ; preds = %entry, %for.body.i.
102102
%cmp.i.i = icmp eq i32* %ptrincdec.i.i, %end
103103
br i1 %cmp.i.i, label %_ZSt4fillIPiiEvT_S1_RKT0_.exit, label %for.body.i.i
104104
; CHECK: Loop %for.body.i.i: backedge-taken count is ((-4 + (-1 * %begin) + %end) /u 4)
105-
; CHECK: Loop %for.body.i.i: max backedge-taken count is ((-4 + (-1 * %begin) + %end) /u 4)
105+
; CHECK: Loop %for.body.i.i: max backedge-taken count is 4611686018427387903
106106
_ZSt4fillIPiiEvT_S1_RKT0_.exit: ; preds = %for.body.i.i, %entry
107107
ret void
108108
}

llvm/test/Analysis/ScalarEvolution/trip-count-pow2.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exit:
1414

1515
; CHECK-LABEL: @test1
1616
; CHECK: Loop %loop: backedge-taken count is ((-32 + (96 * %n)) /u 32)
17-
; CHECK: Loop %loop: max backedge-taken count is ((-32 + (96 * %n)) /u 32)
17+
; CHECK: Loop %loop: max backedge-taken count is 134217727
1818
}
1919

2020
; PR19183
@@ -32,7 +32,7 @@ exit:
3232

3333
; CHECK-LABEL: @test2
3434
; CHECK: Loop %loop: backedge-taken count is ((-32 + (32 * (%n /u 32))) /u 32)
35-
; CHECK: Loop %loop: max backedge-taken count is ((-32 + (32 * (%n /u 32))) /u 32)
35+
; CHECK: Loop %loop: max backedge-taken count is 134217727
3636
}
3737

3838
define void @test3(i32 %n) {
@@ -49,7 +49,7 @@ exit:
4949

5050
; CHECK-LABEL: @test3
5151
; CHECK: Loop %loop: backedge-taken count is ((-32 + (32 * %n)) /u 32)
52-
; CHECK: Loop %loop: max backedge-taken count is ((-32 + (32 * %n)) /u 32)
52+
; CHECK: Loop %loop: max backedge-taken count is 134217727
5353
}
5454

5555
define void @test4(i32 %n) {
@@ -66,7 +66,7 @@ exit:
6666

6767
; CHECK-LABEL: @test4
6868
; CHECK: Loop %loop: backedge-taken count is ((-4 + (-1431655764 * %n)) /u 4)
69-
; CHECK: Loop %loop: max backedge-taken count is ((-4 + (-1431655764 * %n)) /u 4)
69+
; CHECK: Loop %loop: max backedge-taken count is 1073741823
7070
}
7171

7272
define void @test5(i32 %n) {
@@ -83,5 +83,5 @@ exit:
8383

8484
; CHECK-LABEL: @test5
8585
; CHECK: Loop %loop: backedge-taken count is ((-4 + (4 * %n)) /u 4)
86-
; CHECK: Loop %loop: max backedge-taken count is ((-4 + (4 * %n)) /u 4)
86+
; CHECK: Loop %loop: max backedge-taken count is 1073741823
8787
}

0 commit comments

Comments
 (0)