forked from OSchip/llvm-project
[LoopFlatten][LoopInfo] Use Loop to identify latch compare instruction
Make getLatchCmpInst non-static and use it in LoopFlatten as a more robust way of identifying the compare. Differential Revision: https://reviews.llvm.org/D106256
This commit is contained in:
parent
724f0e2abb
commit
44c9adb414
|
@ -589,6 +589,9 @@ public:
|
||||||
///
|
///
|
||||||
PHINode *getCanonicalInductionVariable() const;
|
PHINode *getCanonicalInductionVariable() const;
|
||||||
|
|
||||||
|
/// Get the latch condition instruction.
|
||||||
|
ICmpInst *getLatchCmpInst() const;
|
||||||
|
|
||||||
/// Obtain the unique incoming and back edge. Return false if they are
|
/// Obtain the unique incoming and back edge. Return false if they are
|
||||||
/// non-unique or the loop is dead; otherwise, return true.
|
/// non-unique or the loop is dead; otherwise, return true.
|
||||||
bool getIncomingAndBackEdge(BasicBlock *&Incoming,
|
bool getIncomingAndBackEdge(BasicBlock *&Incoming,
|
||||||
|
|
|
@ -171,8 +171,8 @@ PHINode *Loop::getCanonicalInductionVariable() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the latch condition instruction.
|
/// Get the latch condition instruction.
|
||||||
static ICmpInst *getLatchCmpInst(const Loop &L) {
|
ICmpInst *Loop::getLatchCmpInst() const {
|
||||||
if (BasicBlock *Latch = L.getLoopLatch())
|
if (BasicBlock *Latch = getLoopLatch())
|
||||||
if (BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator()))
|
if (BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator()))
|
||||||
if (BI->isConditional())
|
if (BI->isConditional())
|
||||||
return dyn_cast<ICmpInst>(BI->getCondition());
|
return dyn_cast<ICmpInst>(BI->getCondition());
|
||||||
|
@ -183,7 +183,7 @@ static ICmpInst *getLatchCmpInst(const Loop &L) {
|
||||||
/// Return the final value of the loop induction variable if found.
|
/// Return the final value of the loop induction variable if found.
|
||||||
static Value *findFinalIVValue(const Loop &L, const PHINode &IndVar,
|
static Value *findFinalIVValue(const Loop &L, const PHINode &IndVar,
|
||||||
const Instruction &StepInst) {
|
const Instruction &StepInst) {
|
||||||
ICmpInst *LatchCmpInst = getLatchCmpInst(L);
|
ICmpInst *LatchCmpInst = L.getLatchCmpInst();
|
||||||
if (!LatchCmpInst)
|
if (!LatchCmpInst)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
|
@ -297,7 +297,7 @@ PHINode *Loop::getInductionVariable(ScalarEvolution &SE) const {
|
||||||
|
|
||||||
BasicBlock *Header = getHeader();
|
BasicBlock *Header = getHeader();
|
||||||
assert(Header && "Expected a valid loop header");
|
assert(Header && "Expected a valid loop header");
|
||||||
ICmpInst *CmpInst = getLatchCmpInst(*this);
|
ICmpInst *CmpInst = getLatchCmpInst();
|
||||||
if (!CmpInst)
|
if (!CmpInst)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
|
|
|
@ -111,15 +111,6 @@ static bool findLoopComponents(
|
||||||
LLVM_DEBUG(dbgs() << "Exiting and latch block are different\n");
|
LLVM_DEBUG(dbgs() << "Exiting and latch block are different\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Latch block must end in a conditional branch.
|
|
||||||
BackBranch = dyn_cast<BranchInst>(Latch->getTerminator());
|
|
||||||
if (!BackBranch || !BackBranch->isConditional()) {
|
|
||||||
LLVM_DEBUG(dbgs() << "Could not find back-branch\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
IterationInstructions.insert(BackBranch);
|
|
||||||
LLVM_DEBUG(dbgs() << "Found back branch: "; BackBranch->dump());
|
|
||||||
bool ContinueOnTrue = L->contains(BackBranch->getSuccessor(0));
|
|
||||||
|
|
||||||
// Find the induction PHI. If there is no induction PHI, we can't do the
|
// Find the induction PHI. If there is no induction PHI, we can't do the
|
||||||
// transformation. TODO: could other variables trigger this? Do we have to
|
// transformation. TODO: could other variables trigger this? Do we have to
|
||||||
|
@ -131,6 +122,7 @@ static bool findLoopComponents(
|
||||||
}
|
}
|
||||||
LLVM_DEBUG(dbgs() << "Found induction PHI: "; InductionPHI->dump());
|
LLVM_DEBUG(dbgs() << "Found induction PHI: "; InductionPHI->dump());
|
||||||
|
|
||||||
|
bool ContinueOnTrue = L->contains(Latch->getTerminator()->getSuccessor(0));
|
||||||
auto IsValidPredicate = [&](ICmpInst::Predicate Pred) {
|
auto IsValidPredicate = [&](ICmpInst::Predicate Pred) {
|
||||||
if (ContinueOnTrue)
|
if (ContinueOnTrue)
|
||||||
return Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_ULT;
|
return Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_ULT;
|
||||||
|
@ -138,13 +130,17 @@ static bool findLoopComponents(
|
||||||
return Pred == CmpInst::ICMP_EQ;
|
return Pred == CmpInst::ICMP_EQ;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Find Compare and make sure it is valid
|
// Find Compare and make sure it is valid. getLatchCmpInst checks that the
|
||||||
ICmpInst *Compare = dyn_cast<ICmpInst>(BackBranch->getCondition());
|
// back branch of the latch is conditional.
|
||||||
|
ICmpInst *Compare = L->getLatchCmpInst();
|
||||||
if (!Compare || !IsValidPredicate(Compare->getUnsignedPredicate()) ||
|
if (!Compare || !IsValidPredicate(Compare->getUnsignedPredicate()) ||
|
||||||
Compare->hasNUsesOrMore(2)) {
|
Compare->hasNUsesOrMore(2)) {
|
||||||
LLVM_DEBUG(dbgs() << "Could not find valid comparison\n");
|
LLVM_DEBUG(dbgs() << "Could not find valid comparison\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
BackBranch = cast<BranchInst>(Latch->getTerminator());
|
||||||
|
IterationInstructions.insert(BackBranch);
|
||||||
|
LLVM_DEBUG(dbgs() << "Found back branch: "; BackBranch->dump());
|
||||||
IterationInstructions.insert(Compare);
|
IterationInstructions.insert(Compare);
|
||||||
LLVM_DEBUG(dbgs() << "Found comparison: "; Compare->dump());
|
LLVM_DEBUG(dbgs() << "Found comparison: "; Compare->dump());
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue