@@ -69,16 +69,6 @@ static unsigned getPopRetOpcode(unsigned PopOpcode, bool IsReturnZero) {
69
69
}
70
70
}
71
71
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
-
82
72
bool RISCVPushPopOpt::usePopRet (MachineBasicBlock::iterator &MBBI,
83
73
MachineBasicBlock::iterator &NextI,
84
74
bool IsReturnZero) {
@@ -150,19 +140,28 @@ bool RISCVPushPopOpt::runOnMachineFunction(MachineFunction &Fn) {
150
140
151
141
TII = Subtarget->getInstrInfo ();
152
142
TRI = Subtarget->getRegisterInfo ();
143
+
153
144
// Resize the modified and used register unit trackers. We do this once
154
145
// per function and then clear the register units each time we determine
155
146
// correct return value for the POP.
156
147
ModifiedRegUnits.init (*TRI);
157
148
UsedRegUnits.init (*TRI);
149
+
158
150
bool Modified = false ;
159
151
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));
165
163
}
164
+
166
165
return Modified;
167
166
}
168
167
0 commit comments