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

via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 21 05:54:11 PST 2025


https://github.com/Rifet-c updated https://github.com/llvm/llvm-project/pull/127631

>From 3a8618b22ce3faab05b1faf956c5d537ac64f208 Mon Sep 17 00:00:00 2001
From: Aleksandr Levin <aleksandr.levin at codasip.com>
Date: Tue, 18 Feb 2025 14:06:48 +0100
Subject: [PATCH 1/3] Merged two loops that were iterating over the same
 machine basic block into one, also did some minor readability improvements
 (variable renaming, commenting and absorbing if condition into a variable)

---
 llvm/lib/CodeGen/SlotIndexes.cpp | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/CodeGen/SlotIndexes.cpp b/llvm/lib/CodeGen/SlotIndexes.cpp
index 1b92a5aa59d18..7810a813a24a0 100644
--- a/llvm/lib/CodeGen/SlotIndexes.cpp
+++ b/llvm/lib/CodeGen/SlotIndexes.cpp
@@ -212,40 +212,42 @@ void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
   IndexList::iterator ListI = endIdx.listEntry()->getIterator();
   MachineBasicBlock::iterator MBBI = End;
   bool pastStart = false;
+  bool oldIndexesRemoved = false;
   while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
     assert(ListI->getIndex() >= startIdx.getIndex() &&
            (includeStart || !pastStart) &&
            "Decremented past the beginning of region to repair.");
 
-    MachineInstr *SlotMI = ListI->getInstr();
+    MachineInstr *indexedMI = ListI->getInstr();
     MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
     bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
+    bool MIIndexNotFound = MI && mi2iMap.find(MI) == mi2iMap.end();
 
-    if (SlotMI == MI && !MBBIAtBegin) {
+    if (indexedMI == 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)
-        removeMachineInstrFromMaps(*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 (indexedMI)
+        removeMachineInstrFromMaps(*indexedMI);
     }
-  }
-
-  // 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);
+    // Insert isntruction back into the maps after passing it/removing the index
+    if (MIIndexNotFound && !MI->isDebugOrPseudoInstr())
+      insertMachineInstrInMaps(*MI);
   }
 }
 

>From 51cb4ca79ecfbcdde509b9144036b94a7978c25f Mon Sep 17 00:00:00 2001
From: Aleksandr Levin <aleksandr.levin at codasip.com>
Date: Thu, 20 Feb 2025 16:42:45 +0100
Subject: [PATCH 2/3] Fixed case when an instruction is inserted and then
 mistakenly removed afterwards

---
 llvm/lib/CodeGen/SlotIndexes.cpp | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/CodeGen/SlotIndexes.cpp b/llvm/lib/CodeGen/SlotIndexes.cpp
index 7810a813a24a0..520bf0d4e137e 100644
--- a/llvm/lib/CodeGen/SlotIndexes.cpp
+++ b/llvm/lib/CodeGen/SlotIndexes.cpp
@@ -222,6 +222,7 @@ void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
     MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
     bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
     bool MIIndexNotFound = MI && mi2iMap.find(MI) == mi2iMap.end();
+    bool indexedMIRemoved = false;
 
     if (indexedMI == MI && !MBBIAtBegin) {
       --ListI;
@@ -242,12 +243,17 @@ void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
         oldIndexesRemoved = true;
       else
         --ListI;
-      if (indexedMI)
+      if (indexedMI) {
         removeMachineInstrFromMaps(*indexedMI);
+        indexedMIRemoved = true;
+      }
     }
+
+    MachineInstr *instrToInsert = indexedMIRemoved ? indexedMI : MI;
+
     // Insert isntruction back into the maps after passing it/removing the index
-    if (MIIndexNotFound && !MI->isDebugOrPseudoInstr())
-      insertMachineInstrInMaps(*MI);
+    if ((MIIndexNotFound || indexedMIRemoved) && instrToInsert->getParent() != nullptr && !instrToInsert->isDebugOrPseudoInstr())
+      insertMachineInstrInMaps(*instrToInsert);
   }
 }
 

>From 616103e7a64468ef2d672f30911d21a21b7296ff Mon Sep 17 00:00:00 2001
From: Aleksandr Levin <aleksandr.levin at codasip.com>
Date: Fri, 21 Feb 2025 14:54:00 +0100
Subject: [PATCH 3/3] Reverted the variable renaming

---
 llvm/lib/CodeGen/SlotIndexes.cpp | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/CodeGen/SlotIndexes.cpp b/llvm/lib/CodeGen/SlotIndexes.cpp
index 520bf0d4e137e..1d6164132f9ab 100644
--- a/llvm/lib/CodeGen/SlotIndexes.cpp
+++ b/llvm/lib/CodeGen/SlotIndexes.cpp
@@ -218,13 +218,13 @@ void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
            (includeStart || !pastStart) &&
            "Decremented past the beginning of region to repair.");
 
-    MachineInstr *indexedMI = ListI->getInstr();
+    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 indexedMIRemoved = false;
+    bool slotMIRemoved = false;
 
-    if (indexedMI == MI && !MBBIAtBegin) {
+    if (slotMI == MI && !MBBIAtBegin) {
       --ListI;
       if (MBBI != Begin)
         --MBBI;
@@ -243,16 +243,16 @@ void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
         oldIndexesRemoved = true;
       else
         --ListI;
-      if (indexedMI) {
-        removeMachineInstrFromMaps(*indexedMI);
-        indexedMIRemoved = true;
+      if (slotMI) {
+        removeMachineInstrFromMaps(*slotMI);
+        slotMIRemoved = true;
       }
     }
 
-    MachineInstr *instrToInsert = indexedMIRemoved ? indexedMI : MI;
+    MachineInstr *instrToInsert = slotMIRemoved ? slotMI : MI;
 
     // Insert isntruction back into the maps after passing it/removing the index
-    if ((MIIndexNotFound || indexedMIRemoved) && instrToInsert->getParent() != nullptr && !instrToInsert->isDebugOrPseudoInstr())
+    if ((MIIndexNotFound || slotMIRemoved) && instrToInsert->getParent() != nullptr && !instrToInsert->isDebugOrPseudoInstr())
       insertMachineInstrInMaps(*instrToInsert);
   }
 }



More information about the llvm-commits mailing list