[llvm] [CodeGen] Extract copy-paste on PHI MachineInstr income removal. (PR #158634)

Afanasyev Ivan via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 18 15:45:56 PDT 2025


https://github.com/ivafanas updated https://github.com/llvm/llvm-project/pull/158634

>From e1a30e73e810bb9e4aee5a61ee3b3bb350ab026f Mon Sep 17 00:00:00 2001
From: Ivan Afanasyev <ivafanas at gmail.com>
Date: Thu, 18 Sep 2025 22:36:19 +0700
Subject: [PATCH] [CodeGen] Extract copy-paste on PHI MachineInstr income
 removal.

---
 llvm/include/llvm/CodeGen/MachineBasicBlock.h |  9 +++++++++
 llvm/include/llvm/CodeGen/MachineInstr.h      | 10 ++++++++++
 llvm/lib/CodeGen/MachineBasicBlock.cpp        | 10 ++++++++++
 llvm/lib/CodeGen/MachineInstr.cpp             | 20 +++++++++++++++++++
 llvm/lib/CodeGen/ModuloSchedule.cpp           | 19 +++---------------
 llvm/lib/CodeGen/TailDuplicator.cpp           |  8 +-------
 llvm/lib/CodeGen/UnreachableBlockElim.cpp     | 14 ++-----------
 7 files changed, 55 insertions(+), 35 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineBasicBlock.h b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
index 94139b64a3e30..25be58081cfd0 100644
--- a/llvm/include/llvm/CodeGen/MachineBasicBlock.h
+++ b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
@@ -1503,6 +1503,15 @@ inline auto instructionsWithoutDebug(IterT It, IterT End,
   });
 }
 
+/// Iterate over block PHI instructions and remove all incoming values for
+/// PredMBB.
+///
+/// Function does not erase PHI instructions even if they have single income or
+/// do not have incoming values ar all. It is a caller responsibility to make
+/// decision how to process PHI instructions after incoming values removal.
+void removePHIsIncomingValuesForMBB(MachineBasicBlock &MBB,
+                                    const MachineBasicBlock &PredMBB);
+
 } // end namespace llvm
 
 #endif // LLVM_CODEGEN_MACHINEBASICBLOCK_H
diff --git a/llvm/include/llvm/CodeGen/MachineInstr.h b/llvm/include/llvm/CodeGen/MachineInstr.h
index 10a9b1ff1411d..297131eefbf66 100644
--- a/llvm/include/llvm/CodeGen/MachineInstr.h
+++ b/llvm/include/llvm/CodeGen/MachineInstr.h
@@ -2115,6 +2115,16 @@ inline raw_ostream& operator<<(raw_ostream &OS, const MachineInstr &MI) {
   return OS;
 }
 
+/// Remove all incoming values of Phi instruction for the given block.
+///
+/// Return deleted operands count.
+///
+/// Function does not erase PHI instruction even if it has single income or does
+/// not have incoming values at all. It is a caller responsibility to make
+/// decision how to process PHI instruction after incoming values removed.
+unsigned removePHIIncomingValueForMBB(MachineInstr &Phi,
+                                      const MachineBasicBlock &MBB);
+
 } // end namespace llvm
 
 #endif // LLVM_CODEGEN_MACHINEINSTR_H
diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp
index 08a51b9b0242a..7c8ac9c5153d4 100644
--- a/llvm/lib/CodeGen/MachineBasicBlock.cpp
+++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp
@@ -1805,3 +1805,13 @@ bool MachineBasicBlock::sizeWithoutDebugLargerThan(unsigned Limit) const {
 const MBBSectionID MBBSectionID::ColdSectionID(MBBSectionID::SectionType::Cold);
 const MBBSectionID
     MBBSectionID::ExceptionSectionID(MBBSectionID::SectionType::Exception);
+
+namespace llvm {
+
+void removePHIsIncomingValuesForMBB(MachineBasicBlock &MBB,
+                                    const MachineBasicBlock &PredMBB) {
+  for (MachineInstr &Phi : MBB.phis())
+    removePHIIncomingValueForMBB(Phi, PredMBB);
+}
+
+} // end namespace llvm
diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp
index 2c06c5ad4a5e4..1dd76c1bbf808 100644
--- a/llvm/lib/CodeGen/MachineInstr.cpp
+++ b/llvm/lib/CodeGen/MachineInstr.cpp
@@ -2747,3 +2747,23 @@ bool MachineInstr::mayFoldInlineAsmRegOp(unsigned OpId) const {
     return F.getRegMayBeFolded();
   return false;
 }
+
+namespace llvm {
+
+unsigned removePHIIncomingValueForMBB(MachineInstr &Phi,
+                                      const MachineBasicBlock &MBB) {
+  assert(Phi.isPHI());
+
+  // Phi might have multiple entries for MBB. Need to remove them all.
+  unsigned RemovedCount = 0;
+  for (unsigned N = Phi.getNumOperands(); N > 2; N -= 2) {
+    if (Phi.getOperand(N - 1).getMBB() == &MBB) {
+      Phi.removeOperand(N - 1);
+      Phi.removeOperand(N - 2);
+      RemovedCount += 2;
+    }
+  }
+  return RemovedCount;
+}
+
+} // end namespace llvm
diff --git a/llvm/lib/CodeGen/ModuloSchedule.cpp b/llvm/lib/CodeGen/ModuloSchedule.cpp
index 21bf052d1fdaf..bc768cdbea73a 100644
--- a/llvm/lib/CodeGen/ModuloSchedule.cpp
+++ b/llvm/lib/CodeGen/ModuloSchedule.cpp
@@ -10,6 +10,7 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Analysis/MemoryLocation.h"
 #include "llvm/CodeGen/LiveIntervals.h"
+#include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineLoopInfo.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
@@ -859,20 +860,6 @@ void ModuloScheduleExpander::splitLifetimes(MachineBasicBlock *KernelBB,
   }
 }
 
-/// Remove the incoming block from the Phis in a basic block.
-static void removePhis(MachineBasicBlock *BB, MachineBasicBlock *Incoming) {
-  for (MachineInstr &MI : *BB) {
-    if (!MI.isPHI())
-      break;
-    for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2)
-      if (MI.getOperand(i + 1).getMBB() == Incoming) {
-        MI.removeOperand(i + 1);
-        MI.removeOperand(i);
-        break;
-      }
-  }
-}
-
 /// Create branches from each prolog basic block to the appropriate epilog
 /// block.  These edges are needed if the loop ends before reaching the
 /// kernel.
@@ -906,7 +893,7 @@ void ModuloScheduleExpander::addBranches(MachineBasicBlock &PreheaderBB,
       Prolog->removeSuccessor(LastPro);
       LastEpi->removeSuccessor(Epilog);
       numAdded = TII->insertBranch(*Prolog, Epilog, nullptr, Cond, DebugLoc());
-      removePhis(Epilog, LastEpi);
+      removePHIsIncomingValuesForMBB(*Epilog, *LastEpi);
       // Remove the blocks that are no longer referenced.
       if (LastPro != LastEpi) {
         for (auto &MI : *LastEpi)
@@ -924,7 +911,7 @@ void ModuloScheduleExpander::addBranches(MachineBasicBlock &PreheaderBB,
       LastPro->eraseFromParent();
     } else {
       numAdded = TII->insertBranch(*Prolog, LastPro, nullptr, Cond, DebugLoc());
-      removePhis(Epilog, Prolog);
+      removePHIsIncomingValuesForMBB(*Epilog, *Prolog);
     }
     LastPro = Prolog;
     LastEpi = Epilog;
diff --git a/llvm/lib/CodeGen/TailDuplicator.cpp b/llvm/lib/CodeGen/TailDuplicator.cpp
index 9b1420a94142d..df91a08c9d4eb 100644
--- a/llvm/lib/CodeGen/TailDuplicator.cpp
+++ b/llvm/lib/CodeGen/TailDuplicator.cpp
@@ -375,13 +375,7 @@ void TailDuplicator::processPHI(
   if (!Remove)
     return;
 
-  // MI might have multiple entries for PredBB. Need to remove them all.
-  for (unsigned N = MI->getNumOperands(); N > 2; N -= 2) {
-    if (MI->getOperand(N - 1).getMBB() == PredBB) {
-      MI->removeOperand(N - 1);
-      MI->removeOperand(N - 2);
-    }
-  }
+  removePHIIncomingValueForMBB(*MI, *PredBB);
 
   if (MI->getNumOperands() == 1 && !TailBB->hasAddressTaken())
     MI->eraseFromParent();
diff --git a/llvm/lib/CodeGen/UnreachableBlockElim.cpp b/llvm/lib/CodeGen/UnreachableBlockElim.cpp
index 512e83db40a5a..e5756d9a31ad3 100644
--- a/llvm/lib/CodeGen/UnreachableBlockElim.cpp
+++ b/llvm/lib/CodeGen/UnreachableBlockElim.cpp
@@ -22,6 +22,7 @@
 #include "llvm/CodeGen/UnreachableBlockElim.h"
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/CodeGen/MachineDominators.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
@@ -155,18 +156,7 @@ bool UnreachableMachineBlockElim::run(MachineFunction &F) {
       if (MDT && MDT->getNode(&BB)) MDT->eraseNode(&BB);
 
       while (!BB.succ_empty()) {
-        MachineBasicBlock* succ = *BB.succ_begin();
-
-        for (MachineInstr &Phi : succ->phis()) {
-          for (unsigned i = Phi.getNumOperands() - 1; i >= 2; i -= 2) {
-            if (Phi.getOperand(i).isMBB() &&
-                Phi.getOperand(i).getMBB() == &BB) {
-              Phi.removeOperand(i);
-              Phi.removeOperand(i - 1);
-            }
-          }
-        }
-
+        removePHIsIncomingValuesForMBB(**BB.succ_begin(), BB);
         BB.removeSuccessor(BB.succ_begin());
       }
     }



More information about the llvm-commits mailing list