diff --git a/llvm/include/llvm/Analysis/LoopInfo.h b/llvm/include/llvm/Analysis/LoopInfo.h --- a/llvm/include/llvm/Analysis/LoopInfo.h +++ b/llvm/include/llvm/Analysis/LoopInfo.h @@ -589,6 +589,9 @@ /// PHINode *getCanonicalInductionVariable() const; + /// Get the latch condition instruction. + ICmpInst *getLatchCmpInst() const; + /// Obtain the unique incoming and back edge. Return false if they are /// non-unique or the loop is dead; otherwise, return true. bool getIncomingAndBackEdge(BasicBlock *&Incoming, diff --git a/llvm/lib/Analysis/LoopInfo.cpp b/llvm/lib/Analysis/LoopInfo.cpp --- a/llvm/lib/Analysis/LoopInfo.cpp +++ b/llvm/lib/Analysis/LoopInfo.cpp @@ -171,8 +171,8 @@ } /// Get the latch condition instruction. -static ICmpInst *getLatchCmpInst(const Loop &L) { - if (BasicBlock *Latch = L.getLoopLatch()) +ICmpInst *Loop::getLatchCmpInst() const { + if (BasicBlock *Latch = getLoopLatch()) if (BranchInst *BI = dyn_cast_or_null(Latch->getTerminator())) if (BI->isConditional()) return dyn_cast(BI->getCondition()); @@ -183,7 +183,7 @@ /// Return the final value of the loop induction variable if found. static Value *findFinalIVValue(const Loop &L, const PHINode &IndVar, const Instruction &StepInst) { - ICmpInst *LatchCmpInst = getLatchCmpInst(L); + ICmpInst *LatchCmpInst = L.getLatchCmpInst(); if (!LatchCmpInst) return nullptr; @@ -297,7 +297,7 @@ BasicBlock *Header = getHeader(); assert(Header && "Expected a valid loop header"); - ICmpInst *CmpInst = getLatchCmpInst(*this); + ICmpInst *CmpInst = getLatchCmpInst(); if (!CmpInst) return nullptr; diff --git a/llvm/lib/Transforms/Scalar/LoopFlatten.cpp b/llvm/lib/Transforms/Scalar/LoopFlatten.cpp --- a/llvm/lib/Transforms/Scalar/LoopFlatten.cpp +++ b/llvm/lib/Transforms/Scalar/LoopFlatten.cpp @@ -111,15 +111,6 @@ LLVM_DEBUG(dbgs() << "Exiting and latch block are different\n"); return false; } - // Latch block must end in a conditional branch. - BackBranch = dyn_cast(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 // transformation. TODO: could other variables trigger this? Do we have to @@ -131,6 +122,7 @@ } LLVM_DEBUG(dbgs() << "Found induction PHI: "; InductionPHI->dump()); + bool ContinueOnTrue = L->contains(Latch->getTerminator()->getSuccessor(0)); auto IsValidPredicate = [&](ICmpInst::Predicate Pred) { if (ContinueOnTrue) return Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_ULT; @@ -138,13 +130,17 @@ return Pred == CmpInst::ICMP_EQ; }; - // Find Compare and make sure it is valid - ICmpInst *Compare = dyn_cast(BackBranch->getCondition()); + // Find Compare and make sure it is valid. getLatchCmpInst checks that the + // back branch of the latch is conditional. + ICmpInst *Compare = L->getLatchCmpInst(); if (!Compare || !IsValidPredicate(Compare->getUnsignedPredicate()) || Compare->hasNUsesOrMore(2)) { LLVM_DEBUG(dbgs() << "Could not find valid comparison\n"); return false; } + BackBranch = cast(Latch->getTerminator()); + IterationInstructions.insert(BackBranch); + LLVM_DEBUG(dbgs() << "Found back branch: "; BackBranch->dump()); IterationInstructions.insert(Compare); LLVM_DEBUG(dbgs() << "Found comparison: "; Compare->dump());