Index: llvm/lib/Analysis/CFG.cpp =================================================================== --- llvm/lib/Analysis/CFG.cpp +++ llvm/lib/Analysis/CFG.cpp @@ -153,6 +153,7 @@ // times on large CFGs without hampering sensible code. Arbitrarily chosen. unsigned Limit = 32; SmallPtrSet Visited; + SmallPtrSet VisitedLoops; do { BasicBlock *BB = Worklist.pop_back_val(); if (!Visited.insert(BB).second) @@ -169,12 +170,17 @@ const Loop *Outer = nullptr; if (LI) { Outer = getOutermostLoop(LI, BB); + // If we're in a loop with a hole, not all blocks in the loop are // reachable from all other blocks. That implies we can't simply jump to // the loop's exit blocks, as that exit might need to pass through an // excluded block. Clear Outer so we process BB's successors. if (LoopsWithHoles.count(Outer)) Outer = nullptr; + + if (Outer && !VisitedLoops.insert(Outer).second) + continue; + if (StopLoop && Outer == StopLoop) return true; } @@ -189,7 +195,6 @@ // All blocks in a single loop are reachable from all other blocks. From // any of these blocks, we can skip directly to the exits of the loop, // ignoring any other blocks inside the loop body. - Visited.insert(Outer->block_begin(), Outer->block_end()); Outer->getExitBlocks(Worklist); } else if (DTN) { // The dominance check effectively visits all blocks dominated by BB. Skip