diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp index 5dbde99baa2..e6e0129fa44 100644 --- a/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -338,9 +338,11 @@ void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) { } void IndVarSimplify::EliminateIVComparisons() { + SmallVector DeadInsts; + // Look for ICmp users. - for (IVUsers::iterator I = IU->begin(), E = IU->end(); I != E;) { - IVStrideUse &UI = *I++; + for (IVUsers::iterator I = IU->begin(), E = IU->end(); I != E; ++I) { + IVStrideUse &UI = *I; ICmpInst *ICmp = dyn_cast(UI.getUser()); if (!ICmp) continue; @@ -367,8 +369,15 @@ void IndVarSimplify::EliminateIVComparisons() { continue; DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); - ICmp->eraseFromParent(); + DeadInsts.push_back(ICmp); } + + // Now that we're done iterating through lists, clean up any instructions + // which are now dead. + while (!DeadInsts.empty()) + if (Instruction *Inst = + dyn_cast_or_null(DeadInsts.pop_back_val())) + RecursivelyDeleteTriviallyDeadInstructions(Inst); } bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {