[NFC] Move code between functions as a preparation step for further improvement

This commit is contained in:
Max Kazantsev 2020-11-13 17:45:42 +07:00
parent 49623fa77a
commit 08016ac32b
1 changed files with 33 additions and 34 deletions

View File

@ -1303,41 +1303,17 @@ enum ExitCondAnalysisResult {
/// output parameters and return CanBeReplacedWithInvariant. /// output parameters and return CanBeReplacedWithInvariant.
/// Otherwise, return CannotOptimize. /// Otherwise, return CannotOptimize.
static ExitCondAnalysisResult static ExitCondAnalysisResult
analyzeCond(const Loop *L, BranchInst *BI, ScalarEvolution *SE, analyzeCond(const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHSS,
bool ProvingLoopExit, const SCEV *MaxIter, const SCEV *RHSS, BranchInst *Context, ScalarEvolution *SE,
ICmpInst::Predicate &InvariantPred, const SCEV *&InvariantLHS, const SCEV *MaxIter, ICmpInst::Predicate &InvariantPred,
const SCEV *&InvariantRHS) { const SCEV *&InvariantLHS, const SCEV *&InvariantRHS) {
ICmpInst::Predicate Pred;
Value *LHS, *RHS;
using namespace PatternMatch;
BasicBlock *TrueSucc, *FalseSucc;
if (!match(BI, m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)),
m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc))))
return CannotOptimize;
assert((L->contains(TrueSucc) != L->contains(FalseSucc)) &&
"Not a loop exit!");
// 'LHS pred RHS' should now mean that we stay in loop.
if (L->contains(FalseSucc))
Pred = CmpInst::getInversePredicate(Pred);
// If we are proving loop exit, invert the predicate.
if (ProvingLoopExit)
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? // Can we prove it to be trivially true?
if (SE->isKnownPredicateAt(Pred, LHSS, RHSS, BI)) if (SE->isKnownPredicateAt(Pred, LHSS, RHSS, Context))
return CanBeRemoved; return CanBeRemoved;
if (ProvingLoopExit)
return CannotOptimize;
// Check if there is a loop-invariant predicate equivalent to our check. // Check if there is a loop-invariant predicate equivalent to our check.
auto LIP = SE->getLoopInvariantExitCondDuringFirstIterations(Pred, LHSS, RHSS, auto LIP = SE->getLoopInvariantExitCondDuringFirstIterations(
L, BI, MaxIter); Pred, LHSS, RHSS, L, Context, MaxIter);
if (!LIP) if (!LIP)
return CannotOptimize; return CannotOptimize;
InvariantPred = LIP->Pred; InvariantPred = LIP->Pred;
@ -1345,7 +1321,8 @@ analyzeCond(const Loop *L, BranchInst *BI, ScalarEvolution *SE,
InvariantRHS = LIP->RHS; InvariantRHS = LIP->RHS;
// Can we prove it to be trivially true? // Can we prove it to be trivially true?
if (SE->isKnownPredicateAt(InvariantPred, InvariantLHS, InvariantRHS, BI)) if (SE->isKnownPredicateAt(InvariantPred, InvariantLHS, InvariantRHS,
Context))
return CanBeRemoved; return CanBeRemoved;
return CanBeReplacedWithInvariant; return CanBeReplacedWithInvariant;
} }
@ -1390,14 +1367,36 @@ static bool optimizeLoopExitWithUnknownExitCount(
const SCEV *MaxIter, bool Inverted, bool SkipLastIter, const SCEV *MaxIter, bool Inverted, bool SkipLastIter,
ScalarEvolution *SE, SCEVExpander &Rewriter, ScalarEvolution *SE, SCEVExpander &Rewriter,
SmallVectorImpl<WeakTrackingVH> &DeadInsts) { SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
ICmpInst::Predicate Pred;
Value *LHS, *RHS;
using namespace PatternMatch;
BasicBlock *TrueSucc, *FalseSucc;
if (!match(BI, m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)),
m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc))))
return false;
assert((L->contains(TrueSucc) != L->contains(FalseSucc)) &&
"Not a loop exit!");
// 'LHS pred RHS' should now mean that we stay in loop.
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);
if (SkipLastIter) { if (SkipLastIter) {
const SCEV *One = SE->getOne(MaxIter->getType()); const SCEV *One = SE->getOne(MaxIter->getType());
MaxIter = SE->getMinusSCEV(MaxIter, One); MaxIter = SE->getMinusSCEV(MaxIter, One);
} }
ICmpInst::Predicate InvariantPred; ICmpInst::Predicate InvariantPred;
const SCEV *InvariantLHS, *InvariantRHS; const SCEV *InvariantLHS, *InvariantRHS;
switch (analyzeCond(L, BI, SE, Inverted, MaxIter, InvariantPred, InvariantLHS, switch (analyzeCond(L, Pred, LHSS, RHSS, BI, SE, MaxIter, InvariantPred,
InvariantRHS)) { InvariantLHS, InvariantRHS)) {
case CanBeRemoved: case CanBeRemoved:
foldExit(L, ExitingBB, Inverted, DeadInsts); foldExit(L, ExitingBB, Inverted, DeadInsts);
return true; return true;