[llvm] [CodeGen] Combine two loops in SloIndexes.cpp file (PR #127631)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 28 06:25:04 PST 2025


================
@@ -220,32 +221,39 @@ void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
     MachineInstr *SlotMI = ListI->getInstr();
     MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
     bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
+    bool MIIndexNotFound = MI && mi2iMap.find(MI) == mi2iMap.end();
+    bool SlotMIRemoved = false;
 
     if (SlotMI == MI && !MBBIAtBegin) {
       --ListI;
       if (MBBI != Begin)
         --MBBI;
       else
         pastStart = true;
-    } else if (MI && !mi2iMap.contains(MI)) {
+    } else if (MIIndexNotFound || oldIndexesRemoved) {
       if (MBBI != Begin)
         --MBBI;
       else
         pastStart = true;
     } else {
-      --ListI;
-      if (SlotMI)
+      // We ran through all the indexes on the interval
+      //   -> The only thing left is to go through all the
+      //   remaining MBB instructions and update their indexes
+      if (ListI == ListB)
+        oldIndexesRemoved = true;
+      else
+        --ListI;
+      if (SlotMI) {
         removeMachineInstrFromMaps(*SlotMI);
+        SlotMIRemoved = true;
+      }
     }
-  }
 
-  // In theory this could be combined with the previous loop, but it is tricky
-  // to update the IndexList while we are iterating it.
-  for (MachineBasicBlock::iterator I = End; I != Begin;) {
-    --I;
-    MachineInstr &MI = *I;
-    if (!MI.isDebugOrPseudoInstr() && !mi2iMap.contains(&MI))
-      insertMachineInstrInMaps(MI);
+    MachineInstr *instrToInsert = SlotMIRemoved ? SlotMI : MI;
+
+    // Insert isntruction back into the maps after passing it/removing the index
----------------
arsenm wrote:

```suggestion
    // Insert instruction back into the maps after passing it/removing the index
```

https://github.com/llvm/llvm-project/pull/127631


More information about the llvm-commits mailing list