Index: llvm/lib/CodeGen/MachineCombiner.cpp =================================================================== --- llvm/lib/CodeGen/MachineCombiner.cpp +++ llvm/lib/CodeGen/MachineCombiner.cpp @@ -465,6 +465,7 @@ /// \param IncrementalUpdate if true, compute instruction depths incrementally, /// otherwise invalidate the trace static void insertDeleteInstructions(MachineBasicBlock *MBB, MachineInstr &MI, + MachineBasicBlock::iterator &BlockItr, SmallVector InsInstrs, SmallVector DelInstrs, MachineTraceMetrics::Ensemble *MinInstr, @@ -485,6 +486,11 @@ MBB->insert((MachineBasicBlock::iterator)&MI, InstrPtr); for (auto *InstrPtr : DelInstrs) { + // If the instructions to be deleted is pointed by cuurent iterator + // update the iterator. + if (InstrPtr == &*BlockItr) { + BlockItr++; + } InstrPtr->eraseFromParent(); // Erase all LiveRegs defined by the removed instruction for (auto I = RegUnits.begin(); I != RegUnits.end(); ) { @@ -642,8 +648,9 @@ } if (reduceRegisterPressure(MI, MBB, InsInstrs, DelInstrs, P)) { // Replace DelInstrs with InsInstrs. - insertDeleteInstructions(MBB, MI, InsInstrs, DelInstrs, MinInstr, - RegUnits, TII, P, IncrementalUpdate); + insertDeleteInstructions(MBB, MI, BlockIter, InsInstrs, DelInstrs, + MinInstr, RegUnits, TII, P, + IncrementalUpdate); Changed |= true; // Go back to previous instruction as it may have ILP reassociation @@ -659,8 +666,8 @@ // resource pressure. if (SubstituteAlways || doSubstitute(NewInstCount, OldInstCount, OptForSize)) { - insertDeleteInstructions(MBB, MI, InsInstrs, DelInstrs, MinInstr, - RegUnits, TII, P, IncrementalUpdate); + insertDeleteInstructions(MBB, MI, BlockIter, InsInstrs, DelInstrs, + MinInstr, RegUnits, TII, P, IncrementalUpdate); // Eagerly stop after the first pattern fires. Changed = true; break; @@ -682,8 +689,9 @@ LastUpdate = BlockIter; } - insertDeleteInstructions(MBB, MI, InsInstrs, DelInstrs, MinInstr, - RegUnits, TII, P, IncrementalUpdate); + insertDeleteInstructions(MBB, MI, BlockIter, InsInstrs, DelInstrs, + MinInstr, RegUnits, TII, P, + IncrementalUpdate); // Eagerly stop after the first pattern fires. Changed = true;