Index: include/llvm/Analysis/LoopInfoImpl.h =================================================================== --- include/llvm/Analysis/LoopInfoImpl.h +++ include/llvm/Analysis/LoopInfoImpl.h @@ -23,6 +23,10 @@ namespace llvm { +// Max number of loop iterations to check whether a loop is a dedicated +// exit loop. +extern cl::opt MaxDedicateExitIterations; + //===----------------------------------------------------------------------===// // APIs for simple analysis of the loop. See header notes. @@ -81,16 +85,26 @@ return nullptr; } +/// hasDedicatedExits - Return false if a non-dedicated successor is +/// found or it cannot finish the check within MaxDedicateExitIterations +/// iterations so return false conservatively. Return true if the check +/// is done and all exits are dedicated. template bool LoopBase::hasDedicatedExits() const { // Each predecessor of each exit block of a normal loop is contained // within the loop. SmallVector ExitBlocks; getExitBlocks(ExitBlocks); - for (BlockT *EB : ExitBlocks) - for (BlockT *Predecessor : children>(EB)) + uint64_t Iterations = 0; + for (BlockT *EB : ExitBlocks) { + for (BlockT *Predecessor : children>(EB)) { if (!contains(Predecessor)) return false; + Iterations++; + } + if (Iterations > MaxDedicateExitIterations) + return false; + } // All the requirements are met. return true; } Index: lib/Analysis/LoopInfo.cpp =================================================================== --- lib/Analysis/LoopInfo.cpp +++ lib/Analysis/LoopInfo.cpp @@ -54,6 +54,13 @@ VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo), cl::Hidden, cl::desc("Verify loop info (time consuming)")); +namespace llvm { +cl::opt MaxDedicateExitIterations( + "max-dedicate-exit-iterations", cl::Hidden, cl::init(100000), + cl::desc("Max number of loop iterations to check whether a loop is a " + "dedicated exit loop. ")); +} + //===----------------------------------------------------------------------===// // Loop implementation //