Index: lib/Transforms/Utils/LCSSA.cpp =================================================================== --- lib/Transforms/Utils/LCSSA.cpp +++ lib/Transforms/Utils/LCSSA.cpp @@ -63,19 +63,25 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl &Worklist, DominatorTree &DT, LoopInfo &LI) { SmallVector UsesToRewrite; - SmallVector ExitBlocks; SmallSetVector PHIsToRemove; PredIteratorCache PredCache; bool Changed = false; + // Cache the Loop ExitBlocks across this loop. We expect to get a lot of + // instructions within the same loops, computing the exit blocks is + // expensive, and we're not mutating the loop structure. + DenseMap> LoopExitBlocks; + while (!Worklist.empty()) { UsesToRewrite.clear(); - ExitBlocks.clear(); Instruction *I = Worklist.pop_back_val(); BasicBlock *InstBB = I->getParent(); Loop *L = LI.getLoopFor(InstBB); - L->getExitBlocks(ExitBlocks); + if (!LoopExitBlocks.count(L)) + L->getExitBlocks(LoopExitBlocks[L]); + assert(LoopExitBlocks.count(L)); + const SmallVector &ExitBlocks = LoopExitBlocks[L]; if (ExitBlocks.empty()) continue;