Skip to content

Commit 220ca5a

Browse files
ivafanasmahesh-attarde
authored andcommitted
[CodeGen] Extract copy-paste on PHI MachineInstr income removal. (llvm#158634)
1 parent 7627031 commit 220ca5a

File tree

7 files changed

+45
-35
lines changed

7 files changed

+45
-35
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,15 @@ class MachineBasicBlock
12871287
// Helper function for MIRPrinter.
12881288
LLVM_ABI bool canPredictBranchProbabilities() const;
12891289

1290+
/// Iterate over block PHI instructions and remove all incoming values for
1291+
/// PredMBB.
1292+
///
1293+
/// Method does not erase PHI instructions even if they have single income or
1294+
/// do not have incoming values ar all. It is a caller responsibility to make
1295+
/// decision how to process PHI instructions after incoming values removal.
1296+
LLVM_ABI void
1297+
removePHIsIncomingValuesForPredecessor(const MachineBasicBlock &PredMBB);
1298+
12901299
private:
12911300
/// Return probability iterator corresponding to the I successor iterator.
12921301
probability_iterator getProbabilityIterator(succ_iterator I);

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,6 +2000,15 @@ class MachineInstr
20002000
/// and point them to \p Reg instead.
20012001
LLVM_ABI void changeDebugValuesDefReg(Register Reg);
20022002

2003+
/// Remove all incoming values of Phi instruction for the given block.
2004+
///
2005+
/// Return deleted operands count.
2006+
///
2007+
/// Method does not erase PHI instruction even if it has single income or does
2008+
/// not have incoming values at all. It is a caller responsibility to make
2009+
/// decision how to process PHI instruction after incoming values removed.
2010+
LLVM_ABI unsigned removePHIIncomingValueFor(const MachineBasicBlock &MBB);
2011+
20032012
/// Sets all register debug operands in this debug value instruction to be
20042013
/// undef.
20052014
void setDebugValueUndef() {

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,12 @@ bool MachineBasicBlock::sizeWithoutDebugLargerThan(unsigned Limit) const {
18021802
return false;
18031803
}
18041804

1805+
void MachineBasicBlock::removePHIsIncomingValuesForPredecessor(
1806+
const MachineBasicBlock &PredMBB) {
1807+
for (MachineInstr &Phi : phis())
1808+
Phi.removePHIIncomingValueFor(PredMBB);
1809+
}
1810+
18051811
const MBBSectionID MBBSectionID::ColdSectionID(MBBSectionID::SectionType::Cold);
18061812
const MBBSectionID
18071813
MBBSectionID::ExceptionSectionID(MBBSectionID::SectionType::Exception);

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,3 +2747,18 @@ bool MachineInstr::mayFoldInlineAsmRegOp(unsigned OpId) const {
27472747
return F.getRegMayBeFolded();
27482748
return false;
27492749
}
2750+
2751+
unsigned MachineInstr::removePHIIncomingValueFor(const MachineBasicBlock &MBB) {
2752+
assert(isPHI());
2753+
2754+
// Phi might have multiple entries for MBB. Need to remove them all.
2755+
unsigned RemovedCount = 0;
2756+
for (unsigned N = getNumOperands(); N > 2; N -= 2) {
2757+
if (getOperand(N - 1).getMBB() == &MBB) {
2758+
removeOperand(N - 1);
2759+
removeOperand(N - 2);
2760+
RemovedCount += 2;
2761+
}
2762+
}
2763+
return RemovedCount;
2764+
}

llvm/lib/CodeGen/ModuloSchedule.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "llvm/ADT/StringExtras.h"
1111
#include "llvm/Analysis/MemoryLocation.h"
1212
#include "llvm/CodeGen/LiveIntervals.h"
13+
#include "llvm/CodeGen/MachineBasicBlock.h"
1314
#include "llvm/CodeGen/MachineInstrBuilder.h"
1415
#include "llvm/CodeGen/MachineLoopInfo.h"
1516
#include "llvm/CodeGen/MachineRegisterInfo.h"
@@ -859,20 +860,6 @@ void ModuloScheduleExpander::splitLifetimes(MachineBasicBlock *KernelBB,
859860
}
860861
}
861862

862-
/// Remove the incoming block from the Phis in a basic block.
863-
static void removePhis(MachineBasicBlock *BB, MachineBasicBlock *Incoming) {
864-
for (MachineInstr &MI : *BB) {
865-
if (!MI.isPHI())
866-
break;
867-
for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2)
868-
if (MI.getOperand(i + 1).getMBB() == Incoming) {
869-
MI.removeOperand(i + 1);
870-
MI.removeOperand(i);
871-
break;
872-
}
873-
}
874-
}
875-
876863
/// Create branches from each prolog basic block to the appropriate epilog
877864
/// block. These edges are needed if the loop ends before reaching the
878865
/// kernel.
@@ -906,7 +893,7 @@ void ModuloScheduleExpander::addBranches(MachineBasicBlock &PreheaderBB,
906893
Prolog->removeSuccessor(LastPro);
907894
LastEpi->removeSuccessor(Epilog);
908895
numAdded = TII->insertBranch(*Prolog, Epilog, nullptr, Cond, DebugLoc());
909-
removePhis(Epilog, LastEpi);
896+
Epilog->removePHIsIncomingValuesForPredecessor(*LastEpi);
910897
// Remove the blocks that are no longer referenced.
911898
if (LastPro != LastEpi) {
912899
for (auto &MI : *LastEpi)
@@ -924,7 +911,7 @@ void ModuloScheduleExpander::addBranches(MachineBasicBlock &PreheaderBB,
924911
LastPro->eraseFromParent();
925912
} else {
926913
numAdded = TII->insertBranch(*Prolog, LastPro, nullptr, Cond, DebugLoc());
927-
removePhis(Epilog, Prolog);
914+
Epilog->removePHIsIncomingValuesForPredecessor(*Prolog);
928915
}
929916
LastPro = Prolog;
930917
LastEpi = Epilog;

llvm/lib/CodeGen/TailDuplicator.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,7 @@ void TailDuplicator::processPHI(
375375
if (!Remove)
376376
return;
377377

378-
// MI might have multiple entries for PredBB. Need to remove them all.
379-
for (unsigned N = MI->getNumOperands(); N > 2; N -= 2) {
380-
if (MI->getOperand(N - 1).getMBB() == PredBB) {
381-
MI->removeOperand(N - 1);
382-
MI->removeOperand(N - 2);
383-
}
384-
}
378+
MI->removePHIIncomingValueFor(*PredBB);
385379

386380
if (MI->getNumOperands() == 1 && !TailBB->hasAddressTaken())
387381
MI->eraseFromParent();

llvm/lib/CodeGen/UnreachableBlockElim.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/CodeGen/UnreachableBlockElim.h"
2323
#include "llvm/ADT/DepthFirstIterator.h"
2424
#include "llvm/ADT/SmallPtrSet.h"
25+
#include "llvm/CodeGen/MachineBasicBlock.h"
2526
#include "llvm/CodeGen/MachineDominators.h"
2627
#include "llvm/CodeGen/MachineFunctionPass.h"
2728
#include "llvm/CodeGen/MachineInstrBuilder.h"
@@ -155,18 +156,7 @@ bool UnreachableMachineBlockElim::run(MachineFunction &F) {
155156
if (MDT && MDT->getNode(&BB)) MDT->eraseNode(&BB);
156157

157158
while (!BB.succ_empty()) {
158-
MachineBasicBlock* succ = *BB.succ_begin();
159-
160-
for (MachineInstr &Phi : succ->phis()) {
161-
for (unsigned i = Phi.getNumOperands() - 1; i >= 2; i -= 2) {
162-
if (Phi.getOperand(i).isMBB() &&
163-
Phi.getOperand(i).getMBB() == &BB) {
164-
Phi.removeOperand(i);
165-
Phi.removeOperand(i - 1);
166-
}
167-
}
168-
}
169-
159+
(*BB.succ_begin())->removePHIsIncomingValuesForPredecessor(BB);
170160
BB.removeSuccessor(BB.succ_begin());
171161
}
172162
}

0 commit comments

Comments
 (0)