Index: lib/Transforms/Utils/LoopUnroll.cpp =================================================================== --- lib/Transforms/Utils/LoopUnroll.cpp +++ lib/Transforms/Utils/LoopUnroll.cpp @@ -62,11 +62,16 @@ } /// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it -/// only has one predecessor, and that predecessor only has one successor. -/// The LoopInfo Analysis that is passed will be kept consistent. -/// Returns the new combined block. +/// only has one predecessor, and that predecessor only has one successor. The +/// LoopInfo Analysis that is passed will be kept consistent. If folding is +/// successful references to the loop must be removed from ScalarEvolution by +/// calling ScalarEvolution::forgetLoop because SE may have references to the +/// eliminated BB. The parameter LoopForgotten indicates that the loop has +/// already been forgotten to prevent redundant, expensive calls to +/// ScalarEvolution::forgetLoop. Returns the new combined block. static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI, - LPPassManager *LPM) { + LPPassManager *LPM, + bool &LoopForgotten) { // Merge basic blocks into their predecessor if there is only one distinct // pred, and if there is only one distinct successor of the predecessor, and // if there are no PHI nodes. @@ -101,10 +106,12 @@ // Erase basic block from the function... // ScalarEvolution holds references to loop exit blocks. - if (LPM) { + if (!LoopForgotten && LPM) { if (ScalarEvolution *SE = LPM->getAnalysisIfAvailable()) { - if (Loop *L = LI->getLoopFor(BB)) + if (Loop *L = LI->getLoopFor(BB)) { SE->forgetLoop(L); + LoopForgotten = true; + } } } LI->removeBlock(BB); @@ -418,11 +425,13 @@ } // Merge adjacent basic blocks, if possible. + bool LoopForgotten = false; for (unsigned i = 0, e = Latches.size(); i != e; ++i) { BranchInst *Term = cast(Latches[i]->getTerminator()); if (Term->isUnconditional()) { BasicBlock *Dest = Term->getSuccessor(0); - if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI, LPM)) + if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI, LPM, + LoopForgotten)) std::replace(Latches.begin(), Latches.end(), Dest, Fold); } }