[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 02:45:58 PDT 2025
https://github.com/ivafanas updated https://github.com/llvm/llvm-project/pull/158634
>From 03107763fcf79a59d98e42e0624731a82bc89f48 Mon Sep 17 00:00:00 2001
From: Ivan Afanasyev <ivafanas at gmail.com>
Date: Thu, 18 Sep 2025 16:41:42 +0700
Subject: [PATCH] [CodeGen] Extract copy-paste on PHI MachineInstr income
removal.
---
llvm/include/llvm/CodeGen/MachineInstrUtils.h | 28 +++++++++++++++
llvm/lib/CodeGen/CMakeLists.txt | 1 +
llvm/lib/CodeGen/MachineInstrUtils.cpp | 34 +++++++++++++++++++
llvm/lib/CodeGen/ModuloSchedule.cpp | 13 ++-----
llvm/lib/CodeGen/TailDuplicator.cpp | 9 ++---
llvm/lib/CodeGen/UnreachableBlockElim.cpp | 12 ++-----
6 files changed, 71 insertions(+), 26 deletions(-)
create mode 100644 llvm/include/llvm/CodeGen/MachineInstrUtils.h
create mode 100644 llvm/lib/CodeGen/MachineInstrUtils.cpp
diff --git a/llvm/include/llvm/CodeGen/MachineInstrUtils.h b/llvm/include/llvm/CodeGen/MachineInstrUtils.h
new file mode 100644
index 0000000000000..a57bf2738caab
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/MachineInstrUtils.h
@@ -0,0 +1,28 @@
+//===- llvm/CodeGen/MachineInstr.h - MachineInstr class ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_MACHINEINSTRUTILS_H
+#define LLVM_CODEGEN_MACHINEINSTRUTILS_H
+
+namespace llvm {
+class MachineBasicBlock;
+class MachineInstr;
+
+/// Remove all incoming values of Phi instruction for the given block.
+///
+/// Return deleted operands count.
+///
+/// Function does not erase PHI instruction itself if its operands list is empty
+/// after deletion and does not replace PHI with COPY if it will have only
+/// single input. It is a caller responsibility.
+unsigned removePHIIncomingValueForMBB(MachineInstr &Phi,
+ const MachineBasicBlock &MBB);
+
+} // end namespace llvm
+
+#endif // LLVM_CODEGEN_MACHINEINSTRUTILS_H
diff --git a/llvm/lib/CodeGen/CMakeLists.txt b/llvm/lib/CodeGen/CMakeLists.txt
index f8f9bbba53e43..6ff4a2f9ab203 100644
--- a/llvm/lib/CodeGen/CMakeLists.txt
+++ b/llvm/lib/CodeGen/CMakeLists.txt
@@ -128,6 +128,7 @@ add_llvm_component_library(LLVMCodeGen
MachineFunctionSplitter.cpp
MachineInstrBundle.cpp
MachineInstr.cpp
+ MachineInstrUtils.cpp
MachineLateInstrsCleanup.cpp
MachineLICM.cpp
MachineLoopInfo.cpp
diff --git a/llvm/lib/CodeGen/MachineInstrUtils.cpp b/llvm/lib/CodeGen/MachineInstrUtils.cpp
new file mode 100644
index 0000000000000..c6ec2d961fb8e
--- /dev/null
+++ b/llvm/lib/CodeGen/MachineInstrUtils.cpp
@@ -0,0 +1,34 @@
+//===- lib/CodeGen/MachineInstr.cpp ---------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/CodeGen/MachineInstrUtils.h"
+
+#include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/CodeGen/MachineOperand.h"
+
+#include <cassert>
+
+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..80696a3a5a802 100644
--- a/llvm/lib/CodeGen/ModuloSchedule.cpp
+++ b/llvm/lib/CodeGen/ModuloSchedule.cpp
@@ -11,6 +11,7 @@
#include "llvm/Analysis/MemoryLocation.h"
#include "llvm/CodeGen/LiveIntervals.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/CodeGen/MachineInstrUtils.h"
#include "llvm/CodeGen/MachineLoopInfo.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/InitializePasses.h"
@@ -861,16 +862,8 @@ 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;
- }
- }
+ for (MachineInstr &Phi : BB->phis())
+ removePHIIncomingValueForMBB(Phi, *Incoming);
}
/// Create branches from each prolog basic block to the appropriate epilog
diff --git a/llvm/lib/CodeGen/TailDuplicator.cpp b/llvm/lib/CodeGen/TailDuplicator.cpp
index 9b1420a94142d..8dac36973a972 100644
--- a/llvm/lib/CodeGen/TailDuplicator.cpp
+++ b/llvm/lib/CodeGen/TailDuplicator.cpp
@@ -24,6 +24,7 @@
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/CodeGen/MachineInstrUtils.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/MachineSSAUpdater.h"
@@ -375,13 +376,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..c88b441537f9e 100644
--- a/llvm/lib/CodeGen/UnreachableBlockElim.cpp
+++ b/llvm/lib/CodeGen/UnreachableBlockElim.cpp
@@ -25,6 +25,7 @@
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/CodeGen/MachineInstrUtils.h"
#include "llvm/CodeGen/MachineLoopInfo.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/Passes.h"
@@ -157,15 +158,8 @@ bool UnreachableMachineBlockElim::run(MachineFunction &F) {
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);
- }
- }
- }
+ for (MachineInstr &Phi : succ->phis())
+ removePHIIncomingValueForMBB(Phi, BB);
BB.removeSuccessor(BB.succ_begin());
}
More information about the llvm-commits
mailing list