[PATCH] D119070: [MachineCombiner] Update iterator while deleting instructions

Darshan Bhat via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 5 22:27:54 PST 2022


DarshanRamakant created this revision.
DarshanRamakant added reviewers: MatzeB, aivchenk, alexfh_, andreadb, avt77, chandlerc, craig.topper, dsanders, dblaikie, dexonsmith, evandro, fhahn, Gerolf, hfinkel, yamauchi, jackoalan, kuhar, junmo.park, mehdi_amini, Nicola, rnk, abdulras, spatel, RKSimon.
DarshanRamakant added a project: LLVM.
Herald added a subscriber: hiraditya.
DarshanRamakant requested review of this revision.
Herald added a subscriber: llvm-commits.

This change will fix the accidental deletion of the instruction
which is pointed by the instruction iterator of the current MBB.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119070

Files:
  llvm/lib/CodeGen/MachineCombiner.cpp


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<MachineInstr *, 16> InsInstrs,
                                      SmallVector<MachineInstr *, 16> DelInstrs,
                                      MachineTraceMetrics::Ensemble *MinInstr,
@@ -485,6 +486,12 @@
     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 +649,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 +667,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 +690,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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119070.406227.patch
Type: text/x-patch
Size: 2874 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220206/57faaf87/attachment.bin>


More information about the llvm-commits mailing list