Index: llvm/include/llvm/Analysis/CFG.h =================================================================== --- llvm/include/llvm/Analysis/CFG.h +++ llvm/include/llvm/Analysis/CFG.h @@ -162,6 +162,13 @@ return false; } + +/// Return a predecessor of BB (which may not be an immediate predecessor) +/// which has exactly one successor from which BB is reachable, or null if +/// no such block is found. +std::pair +getPredecessorWithUniqueSuccessorForBB(const BasicBlock *BB, + const LoopInfo &LI); } // End llvm namespace #endif Index: llvm/lib/Analysis/CFG.cpp =================================================================== --- llvm/lib/Analysis/CFG.cpp +++ llvm/lib/Analysis/CFG.cpp @@ -272,3 +272,21 @@ return isPotentiallyReachable( A->getParent(), B->getParent(), ExclusionSet, DT, LI); } + +std::pair +llvm::getPredecessorWithUniqueSuccessorForBB(const BasicBlock *BB, + const LoopInfo &LI) { + // If the block has a unique predecessor, then there is no path from the + // predecessor to the block that does not go through the direct edge + // from the predecessor to the block. + if (const BasicBlock *Pred = BB->getSinglePredecessor()) + return {Pred, BB}; + + // A loop's header is defined to be a block that dominates the loop. + // If the header has a unique predecessor outside the loop, it must be + // a block that has exactly one successor that can reach the loop. + if (const Loop *L = LI.getLoopFor(BB)) + return {L->getLoopPredecessor(), L->getHeader()}; + + return {nullptr, nullptr}; +} Index: llvm/lib/Analysis/ScalarEvolution.cpp =================================================================== --- llvm/lib/Analysis/ScalarEvolution.cpp +++ llvm/lib/Analysis/ScalarEvolution.cpp @@ -73,6 +73,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/ADT/StringRef.h" #include "llvm/Analysis/AssumptionCache.h" +#include "llvm/Analysis/CFG.h" #include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/InstructionSimplify.h" #include "llvm/Analysis/LoopInfo.h" @@ -10548,24 +10549,6 @@ return getCouldNotCompute(); } -std::pair -ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(const BasicBlock *BB) - const { - // If the block has a unique predecessor, then there is no path from the - // predecessor to the block that does not go through the direct edge - // from the predecessor to the block. - if (const BasicBlock *Pred = BB->getSinglePredecessor()) - return {Pred, BB}; - - // A loop's header is defined to be a block that dominates the loop. - // If the header has a unique predecessor outside the loop, it must be - // a block that has exactly one successor that can reach the loop. - if (const Loop *L = LI.getLoopFor(BB)) - return {L->getLoopPredecessor(), L->getHeader()}; - - return {nullptr, nullptr}; -} - /// SCEV structural equivalence is usually sufficient for testing whether two /// expressions are equal, however for the purposes of looking for a condition /// guarding a loop, it can be useful to be a little more general, since a @@ -11511,7 +11494,8 @@ else PredBB = BB->getSinglePredecessor(); for (std::pair Pair(PredBB, BB); - Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { + Pair.first; + Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first, LI)) { const BranchInst *BlockEntryPredicate = dyn_cast(Pair.first->getTerminator()); if (!BlockEntryPredicate || BlockEntryPredicate->isUnconditional()) @@ -15342,7 +15326,8 @@ // TODO: share this logic with isLoopEntryGuardedByCond. for (std::pair Pair( L->getLoopPredecessor(), Header); - Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { + Pair.first; + Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first, LI)) { const BranchInst *LoopEntryPredicate = dyn_cast(Pair.first->getTerminator());