[llvm] 3e63993 - [CodeGen] Extract copy-paste on PHI MachineInstr income removal. (#158634)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 24 22:59:40 PDT 2025
Author: Afanasyev Ivan
Date: 2025-09-25T14:59:36+09:00
New Revision: 3e639930d3ba3d6401992ab1d54dc625d5a299a5
URL: https://github.com/llvm/llvm-project/commit/3e639930d3ba3d6401992ab1d54dc625d5a299a5
DIFF: https://github.com/llvm/llvm-project/commit/3e639930d3ba3d6401992ab1d54dc625d5a299a5.diff
LOG: [CodeGen] Extract copy-paste on PHI MachineInstr income removal. (#158634)
Added:
Modified:
llvm/include/llvm/CodeGen/MachineBasicBlock.h
llvm/include/llvm/CodeGen/MachineInstr.h
llvm/lib/CodeGen/MachineBasicBlock.cpp
llvm/lib/CodeGen/MachineInstr.cpp
llvm/lib/CodeGen/ModuloSchedule.cpp
llvm/lib/CodeGen/TailDuplicator.cpp
llvm/lib/CodeGen/UnreachableBlockElim.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/CodeGen/MachineBasicBlock.h b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
index 94139b64a3e30..80422db61c045 100644
--- a/llvm/include/llvm/CodeGen/MachineBasicBlock.h
+++ b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
@@ -1287,6 +1287,15 @@ class MachineBasicBlock
// Helper function for MIRPrinter.
LLVM_ABI bool canPredictBranchProbabilities() const;
+ /// Iterate over block PHI instructions and remove all incoming values for
+ /// PredMBB.
+ ///
+ /// Method 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.
+ LLVM_ABI void
+ removePHIsIncomingValuesForPredecessor(const MachineBasicBlock &PredMBB);
+
private:
/// Return probability iterator corresponding to the I successor iterator.
probability_iterator getProbabilityIterator(succ_iterator I);
diff --git a/llvm/include/llvm/CodeGen/MachineInstr.h b/llvm/include/llvm/CodeGen/MachineInstr.h
index 63d74047d2e83..4fcb7f36e0238 100644
--- a/llvm/include/llvm/CodeGen/MachineInstr.h
+++ b/llvm/include/llvm/CodeGen/MachineInstr.h
@@ -2000,6 +2000,15 @@ class MachineInstr
/// and point them to \p Reg instead.
LLVM_ABI void changeDebugValuesDefReg(Register Reg);
+ /// Remove all incoming values of Phi instruction for the given block.
+ ///
+ /// Return deleted operands count.
+ ///
+ /// Method 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.
+ LLVM_ABI unsigned removePHIIncomingValueFor(const MachineBasicBlock &MBB);
+
/// Sets all register debug operands in this debug value instruction to be
/// undef.
void setDebugValueUndef() {
diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp
index 08a51b9b0242a..d4b64ab9db6de 100644
--- a/llvm/lib/CodeGen/MachineBasicBlock.cpp
+++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp
@@ -1802,6 +1802,12 @@ bool MachineBasicBlock::sizeWithoutDebugLargerThan(unsigned Limit) const {
return false;
}
+void MachineBasicBlock::removePHIsIncomingValuesForPredecessor(
+ const MachineBasicBlock &PredMBB) {
+ for (MachineInstr &Phi : phis())
+ Phi.removePHIIncomingValueFor(PredMBB);
+}
+
const MBBSectionID MBBSectionID::ColdSectionID(MBBSectionID::SectionType::Cold);
const MBBSectionID
MBBSectionID::ExceptionSectionID(MBBSectionID::SectionType::Exception);
diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp
index 2c06c5ad4a5e4..8ad9245a47684 100644
--- a/llvm/lib/CodeGen/MachineInstr.cpp
+++ b/llvm/lib/CodeGen/MachineInstr.cpp
@@ -2747,3 +2747,18 @@ bool MachineInstr::mayFoldInlineAsmRegOp(unsigned OpId) const {
return F.getRegMayBeFolded();
return false;
}
+
+unsigned MachineInstr::removePHIIncomingValueFor(const MachineBasicBlock &MBB) {
+ assert(isPHI());
+
+ // Phi might have multiple entries for MBB. Need to remove them all.
+ unsigned RemovedCount = 0;
+ for (unsigned N = getNumOperands(); N > 2; N -= 2) {
+ if (getOperand(N - 1).getMBB() == &MBB) {
+ removeOperand(N - 1);
+ removeOperand(N - 2);
+ RemovedCount += 2;
+ }
+ }
+ return RemovedCount;
+}
diff --git a/llvm/lib/CodeGen/ModuloSchedule.cpp b/llvm/lib/CodeGen/ModuloSchedule.cpp
index 21bf052d1fdaf..d47ed65540cf4 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);
+ Epilog->removePHIsIncomingValuesForPredecessor(*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);
+ Epilog->removePHIsIncomingValuesForPredecessor(*Prolog);
}
LastPro = Prolog;
LastEpi = Epilog;
diff --git a/llvm/lib/CodeGen/TailDuplicator.cpp b/llvm/lib/CodeGen/TailDuplicator.cpp
index 9b1420a94142d..8e48d19537165 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);
- }
- }
+ MI->removePHIIncomingValueFor(*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..cf8c1a7bd08d0 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);
- }
- }
- }
-
+ (*BB.succ_begin())->removePHIsIncomingValuesForPredecessor(BB);
BB.removeSuccessor(BB.succ_begin());
}
}
More information about the llvm-commits
mailing list