}
static bool optimizeLoopExitWithUnknownExitCount(
- const Loop *L, BranchInst *BI, BasicBlock *ExitingBB,
- const SCEV *MaxIter, bool Inverted, bool SkipLastIter,
- ScalarEvolution *SE, SCEVExpander &Rewriter,
+ const Loop *L, BranchInst *BI, BasicBlock *ExitingBB, const SCEV *MaxIter,
+ bool SkipLastIter, ScalarEvolution *SE, SCEVExpander &Rewriter,
SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
ICmpInst::Predicate Pred;
Value *LHS, *RHS;
if (L->contains(FalseSucc))
Pred = CmpInst::getInversePredicate(Pred);
- // If we are proving loop exit, invert the predicate.
- if (Inverted)
- Pred = CmpInst::getInversePredicate(Pred);
-
const SCEV *LHSS = SE->getSCEVAtScope(LHS, L);
const SCEV *RHSS = SE->getSCEVAtScope(RHS, L);
- // Can we prove it to be trivially true?
- if (SE->isKnownPredicateAt(Pred, LHSS, RHSS, BI)) {
- foldExit(L, ExitingBB, Inverted, DeadInsts);
+ // Can we prove it to be trivially true or false?
+ if (auto EV = SE->evaluatePredicateAt(Pred, LHSS, RHSS, BI)) {
+ foldExit(L, ExitingBB, /*IsTaken*/ !*EV, DeadInsts);
return true;
}
- // Further logic works for non-inverted condition only.
- if (Inverted)
- return false;
auto *ARTy = LHSS->getType();
auto *MaxIterTy = MaxIter->getType();
// Can we prove it to be trivially true?
if (SE->isKnownPredicateAt(LIP->Pred, LIP->LHS, LIP->RHS, BI))
- foldExit(L, ExitingBB, Inverted, DeadInsts);
+ foldExit(L, ExitingBB, /*IsTaken*/ false, DeadInsts);
else
replaceWithInvariantCond(L, ExitingBB, LIP->Pred, LIP->LHS, LIP->RHS,
Rewriter, DeadInsts);
// Okay, we do not know the exit count here. Can we at least prove that it
// will remain the same within iteration space?
auto *BI = cast<BranchInst>(ExitingBB->getTerminator());
- auto OptimizeCond = [&](bool Inverted, bool SkipLastIter) {
- return optimizeLoopExitWithUnknownExitCount(
- L, BI, ExitingBB, MaxBECount, Inverted, SkipLastIter, SE, Rewriter,
- DeadInsts);
+ auto OptimizeCond = [&](bool SkipLastIter) {
+ return optimizeLoopExitWithUnknownExitCount(L, BI, ExitingBB,
+ MaxBECount, SkipLastIter,
+ SE, Rewriter, DeadInsts);
};
// TODO: We might have proved that we can skip the last iteration for
// hope that we will be able to prove triviality for at least one of
// them. We can stop querying MaxBECount for this case once SCEV
// understands that (MaxBECount - 1) will not overflow here.
- if (OptimizeCond(false, false) || OptimizeCond(true, false))
+ if (OptimizeCond(false))
+ Changed = true;
+ else if (SkipLastIter && OptimizeCond(true))
Changed = true;
- else if (SkipLastIter)
- if (OptimizeCond(false, true) || OptimizeCond(true, true))
- Changed = true;
if (MaxBECount == MaxExitCount)
// If the loop has more than 1 iteration, all further checks will be
// executed 1 iteration less.