llvm.org GIT mirror llvm / c83e065
[SCEV] Clarify behavior around max backedge taken count This is a re-application of a r303497 that was reverted in r303498. I thought it had broken a bot when it had not (the breakage did not go away with the revert). 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`. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@303531 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 2 years ago
4 changed file(s) with 58 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
655655 /// Test whether this BackedgeTakenInfo contains complete information.
656656 bool hasFullInfo() const { return isComplete(); }
657657
658 /// Return an expression indicating the exact backedge-taken count of the
659 /// loop if it is known or SCEVCouldNotCompute otherwise. This is the
660 /// number of times the loop header can be guaranteed to execute, minus
661 /// one.
658 /// Return an expression indicating the exact *backedge-taken*
659 /// count of the loop if it is known or SCEVCouldNotCompute
660 /// otherwise. If execution makes it to the backedge on every
661 /// iteration (i.e. there are no abnormal exists like exception
662 /// throws and thread exits) then this is the number of times the
663 /// loop header will execute minus one.
662664 ///
663665 /// If the SCEV predicate associated with the answer can be different
664666 /// from AlwaysTrue, we must add a (non null) Predicates argument.
13971399 const SCEV *getExitCount(const Loop *L, BasicBlock *ExitingBlock);
13981400
13991401 /// If the specified loop has a predictable backedge-taken count, return it,
1400 /// otherwise return a SCEVCouldNotCompute object. The backedge-taken count
1401 /// is the number of times the loop header will be branched to from within
1402 /// the loop. This is one less than the trip count of the loop, since it
1403 /// doesn't count the first iteration, when the header is branched to from
1404 /// outside the loop.
1402 /// otherwise return a SCEVCouldNotCompute object. The backedge-taken count is
1403 /// the number of times the loop header will be branched to from within the
1404 /// loop, assuming there are no abnormal exists like exception throws. This is
1405 /// one less than the trip count of the loop, since it doesn't count the first
1406 /// iteration, when the header is branched to from outside the loop.
14051407 ///
14061408 /// Note that it is not valid to call this method on a loop without a
14071409 /// loop-invariant backedge-taken count (see
14161418 const SCEV *getPredicatedBackedgeTakenCount(const Loop *L,
14171419 SCEVUnionPredicate &Predicates);
14181420
1419 /// Similar to getBackedgeTakenCount, except return the least SCEV value
1420 /// that is known never to be less than the actual backedge taken count.
1421 /// When successful, this returns a SCEVConstant that is greater than or equal
1422 /// to (i.e. a "conservative over-approximation") of the value returend by
1423 /// getBackedgeTakenCount. If such a value cannot be computed, it returns the
1424 /// SCEVCouldNotCompute object.
14211425 const SCEV *getMaxBackedgeTakenCount(const Loop *L);
14221426
14231427 /// Return true if the backedge taken count is either the value returned by
59465946 if (any_of(ExitNotTaken, PredicateNotAlwaysTrue) || !getMax())
59475947 return SE->getCouldNotCompute();
59485948
5949 assert((isa(getMax()) || isa(getMax())) &&
5950 "No point in having a non-constant max backedge taken count!");
59495951 return getMax();
59505952 }
59515953
59715973 }
59725974
59735975 ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E)
5974 : ExactNotTaken(E), MaxNotTaken(E), MaxOrZero(false) {}
5976 : ExactNotTaken(E), MaxNotTaken(E), MaxOrZero(false) {
5977 assert((isa(MaxNotTaken) ||
5978 isa(MaxNotTaken)) &&
5979 "No point in having a non-constant max backedge taken count!");
5980 }
59755981
59765982 ScalarEvolution::ExitLimit::ExitLimit(
59775983 const SCEV *E, const SCEV *M, bool MaxOrZero,
59805986 assert((isa(ExactNotTaken) ||
59815987 !isa(MaxNotTaken)) &&
59825988 "Exact is not allowed to be less precise than Max");
5989 assert((isa(MaxNotTaken) ||
5990 isa(MaxNotTaken)) &&
5991 "No point in having a non-constant max backedge taken count!");
59835992 for (auto *PredSet : PredSetList)
59845993 for (auto *P : *PredSet)
59855994 addPredicate(P);
59885997 ScalarEvolution::ExitLimit::ExitLimit(
59895998 const SCEV *E, const SCEV *M, bool MaxOrZero,
59905999 const SmallPtrSetImpl &PredSet)
5991 : ExitLimit(E, M, MaxOrZero, {&PredSet}) {}
6000 : ExitLimit(E, M, MaxOrZero, {&PredSet}) {
6001 assert((isa(MaxNotTaken) ||
6002 isa(MaxNotTaken)) &&
6003 "No point in having a non-constant max backedge taken count!");
6004 }
59926005
59936006 ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E, const SCEV *M,
59946007 bool MaxOrZero)
5995 : ExitLimit(E, M, MaxOrZero, None) {}
6008 : ExitLimit(E, M, MaxOrZero, None) {
6009 assert((isa(MaxNotTaken) ||
6010 isa(MaxNotTaken)) &&
6011 "No point in having a non-constant max backedge taken count!");
6012 }
59966013
59976014 /// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each
59986015 /// computable exit into a persistent ExitNotTakenInfo array.
60176034
60186035 return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, std::move(Predicate));
60196036 });
6037 assert((isa(MaxCount) || isa(MaxCount)) &&
6038 "No point in having a non-constant max backedge taken count!");
60206039 }
60216040
60226041 /// Invalidate this result and free the ExitNotTakenInfo array.
62786297 // to not.
62796298 if (isa(MaxBECount) &&
62806299 !isa(BECount))
6281 MaxBECount = BECount;
6300 MaxBECount = getConstant(getUnsignedRange(BECount).getUnsignedMax());
62826301
62836302 return ExitLimit(BECount, MaxBECount, false,
62846303 {&EL0.Predicates, &EL1.Predicates});
75827601 loopHasNoAbnormalExits(AddRec->getLoop())) {
75837602 const SCEV *Exact =
75847603 getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
7585 return ExitLimit(Exact, Exact, false, Predicates);
7604 const SCEV *Max =
7605 Exact == getCouldNotCompute()
7606 ? Exact
7607 : getConstant(getUnsignedRange(Exact).getUnsignedMax());
7608 return ExitLimit(Exact, Max, false, Predicates);
75867609 }
75877610
75887611 // Solve the general equation.
7589 const SCEV *E = SolveLinEquationWithOverflow(
7590 StepC->getAPInt(), getNegativeSCEV(Start), *this);
7591 return ExitLimit(E, E, false, Predicates);
7612 const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(),
7613 getNegativeSCEV(Start), *this);
7614 const SCEV *M = E == getCouldNotCompute()
7615 ? E
7616 : getConstant(getUnsignedRange(E).getUnsignedMax());
7617 return ExitLimit(E, M, false, Predicates);
75927618 }
75937619
75947620 ScalarEvolution::ExitLimit
92179243 getConstant(StrideForMaxBECount), false);
92189244 }
92199245
9220 if (isa(MaxBECount))
9221 MaxBECount = BECount;
9246 if (isa(MaxBECount) &&
9247 !isa(BECount))
9248 MaxBECount = getConstant(getUnsignedRange(BECount).getUnsignedMax());
92229249
92239250 return ExitLimit(BECount, MaxBECount, MaxOrZero, Predicates);
92249251 }
101101 %cmp.i.i = icmp eq i32* %ptrincdec.i.i, %end
102102 br i1 %cmp.i.i, label %_ZSt4fillIPiiEvT_S1_RKT0_.exit, label %for.body.i.i
103103 ; CHECK: Loop %for.body.i.i: backedge-taken count is ((-4 + (-1 * %begin) + %end) /u 4)
104 ; CHECK: Loop %for.body.i.i: max backedge-taken count is ((-4 + (-1 * %begin) + %end) /u 4)
104 ; CHECK: Loop %for.body.i.i: max backedge-taken count is 4611686018427387903
105105 _ZSt4fillIPiiEvT_S1_RKT0_.exit: ; preds = %for.body.i.i, %entry
106106 ret void
107107 }
1313
1414 ; CHECK-LABEL: @test1
1515 ; CHECK: Loop %loop: backedge-taken count is ((-32 + (96 * %n)) /u 32)
16 ; CHECK: Loop %loop: max backedge-taken count is ((-32 + (96 * %n)) /u 32)
16 ; CHECK: Loop %loop: max backedge-taken count is 134217727
1717 }
1818
1919 ; PR19183
3131
3232 ; CHECK-LABEL: @test2
3333 ; CHECK: Loop %loop: backedge-taken count is ((-32 + (32 * (%n /u 32))) /u 32)
34 ; CHECK: Loop %loop: max backedge-taken count is ((-32 + (32 * (%n /u 32))) /u 32)
34 ; CHECK: Loop %loop: max backedge-taken count is 134217727
3535 }
3636
3737 define void @test3(i32 %n) {
4848
4949 ; CHECK-LABEL: @test3
5050 ; CHECK: Loop %loop: backedge-taken count is ((-32 + (32 * %n)) /u 32)
51 ; CHECK: Loop %loop: max backedge-taken count is ((-32 + (32 * %n)) /u 32)
51 ; CHECK: Loop %loop: max backedge-taken count is 134217727
5252 }
5353
5454 define void @test4(i32 %n) {
6565
6666 ; CHECK-LABEL: @test4
6767 ; CHECK: Loop %loop: backedge-taken count is ((-4 + (-1431655764 * %n)) /u 4)
68 ; CHECK: Loop %loop: max backedge-taken count is ((-4 + (-1431655764 * %n)) /u 4)
68 ; CHECK: Loop %loop: max backedge-taken count is 1073741823
6969 }
7070
7171 define void @test5(i32 %n) {
8282
8383 ; CHECK-LABEL: @test5
8484 ; CHECK: Loop %loop: backedge-taken count is ((-4 + (4 * %n)) /u 4)
85 ; CHECK: Loop %loop: max backedge-taken count is ((-4 + (4 * %n)) /u 4)
85 ; CHECK: Loop %loop: max backedge-taken count is 1073741823
8686 }