Skip to content

Commit bedb907

Browse files
authored
[RISCV] Simplify how we find combinable cm.pop+ret. (#130204)
Instead of scanning the whole basic block for a POP, find the RET and then look backwards for the POP. Using getFirstTerminator, we can do this with less code and it's probably faster.
1 parent c020191 commit bedb907

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

llvm/lib/Target/RISCV/RISCVPushPopOptimizer.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,6 @@ static unsigned getPopRetOpcode(unsigned PopOpcode, bool IsReturnZero) {
6969
}
7070
}
7171

72-
// Check if POP instruction was inserted into the MBB and return iterator to it.
73-
static MachineBasicBlock::iterator containsPop(MachineBasicBlock &MBB) {
74-
for (MachineBasicBlock::iterator MBBI = MBB.begin(); MBBI != MBB.end();
75-
MBBI = next_nodbg(MBBI, MBB.end()))
76-
if (MBBI->getFlag(MachineInstr::FrameDestroy) && isPop(MBBI->getOpcode()))
77-
return MBBI;
78-
79-
return MBB.end();
80-
}
81-
8272
bool RISCVPushPopOpt::usePopRet(MachineBasicBlock::iterator &MBBI,
8373
MachineBasicBlock::iterator &NextI,
8474
bool IsReturnZero) {
@@ -150,19 +140,28 @@ bool RISCVPushPopOpt::runOnMachineFunction(MachineFunction &Fn) {
150140

151141
TII = Subtarget->getInstrInfo();
152142
TRI = Subtarget->getRegisterInfo();
143+
153144
// Resize the modified and used register unit trackers. We do this once
154145
// per function and then clear the register units each time we determine
155146
// correct return value for the POP.
156147
ModifiedRegUnits.init(*TRI);
157148
UsedRegUnits.init(*TRI);
149+
158150
bool Modified = false;
159151
for (auto &MBB : Fn) {
160-
MachineBasicBlock::iterator MBBI = containsPop(MBB);
161-
MachineBasicBlock::iterator NextI = next_nodbg(MBBI, MBB.end());
162-
if (MBBI != MBB.end() && NextI != MBB.end() &&
163-
NextI->getOpcode() == RISCV::PseudoRET)
164-
Modified |= usePopRet(MBBI, NextI, adjustRetVal(MBBI));
152+
// RET should be the only terminator.
153+
auto RetMBBI = MBB.getFirstTerminator();
154+
if (RetMBBI == MBB.end() || RetMBBI->getOpcode() != RISCV::PseudoRET ||
155+
RetMBBI == MBB.begin())
156+
continue;
157+
158+
// The previous instruction should be a POP.
159+
auto PopMBBI = prev_nodbg(RetMBBI, MBB.begin());
160+
if (isPop(PopMBBI->getOpcode()) &&
161+
PopMBBI->getFlag(MachineInstr::FrameDestroy))
162+
Modified |= usePopRet(PopMBBI, RetMBBI, adjustRetVal(PopMBBI));
165163
}
164+
166165
return Modified;
167166
}
168167

0 commit comments

Comments
 (0)