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 @@ -521,6 +521,11 @@ /// PHINode *getCanonicalInductionVariable() const; + /// Obtain the unique incoming and back edge, and return true. Return false + /// if they are non-unique or the loop is dead. + bool getIncomingAndBackEdge(BasicBlock *&Incoming, + BasicBlock *&Backedge) const; + /// Return true if the Loop is in LCSSA form. bool isLCSSAForm(DominatorTree &DT) const; 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 @@ -109,24 +109,37 @@ return true; } -PHINode *Loop::getCanonicalInductionVariable() const { +bool Loop::getIncomingAndBackEdge(BasicBlock *&Incoming, + BasicBlock *&Backedge) const { BasicBlock *H = getHeader(); - BasicBlock *Incoming = nullptr, *Backedge = nullptr; + Incoming = nullptr; + Backedge = nullptr; pred_iterator PI = pred_begin(H); assert(PI != pred_end(H) && "Loop must have at least one backedge!"); Backedge = *PI++; if (PI == pred_end(H)) - return nullptr; // dead loop + return false; // dead loop Incoming = *PI++; if (PI != pred_end(H)) - return nullptr; // multiple backedges? + return false; // multiple backedges? if (contains(Incoming)) { if (contains(Backedge)) - return nullptr; + return false; std::swap(Incoming, Backedge); } else if (!contains(Backedge)) + return false; + + assert(Incoming && Backedge && "expected non-null incoming and backedges"); + return true; +} + +PHINode *Loop::getCanonicalInductionVariable() const { + BasicBlock *H = getHeader(); + + BasicBlock *Incoming = nullptr, *Backedge = nullptr; + if (!getIncomingAndBackEdge(Incoming, Backedge)) return nullptr; // Loop over all of the PHI nodes, looking for a canonical indvar.