@@ -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-
8272bool 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