@@ -45,10 +45,26 @@ char RISCVPushPopOpt::ID = 0;
45
45
INITIALIZE_PASS (RISCVPushPopOpt, " riscv-push-pop-opt" , RISCV_PUSH_POP_OPT_NAME,
46
46
false , false )
47
47
48
+ template <typename IterT>
49
+ static IterT nextNoDebugNoCFIInst(const IterT It, const IterT End) {
50
+ return std::find_if_not (std::next (It), End, [](const auto &Inst) {
51
+ return Inst.isDebugInstr () || Inst.isCFIInstruction () ||
52
+ Inst.isPseudoProbe ();
53
+ });
54
+ }
55
+
56
+ static void eraseCFIInst (const MachineBasicBlock::iterator Begin,
57
+ const MachineBasicBlock::iterator End) {
58
+ for (const auto &Inst :
59
+ llvm::make_early_inc_range (llvm::make_range (Begin, End)))
60
+ if (Inst.isCFIInstruction ())
61
+ Inst.eraseFromParent ();
62
+ }
63
+
48
64
// Check if POP instruction was inserted into the MBB and return iterator to it.
49
65
static MachineBasicBlock::iterator containsPop (MachineBasicBlock &MBB) {
50
66
for (MachineBasicBlock::iterator MBBI = MBB.begin (); MBBI != MBB.end ();
51
- MBBI = next_nodbg (MBBI, MBB.end ()))
67
+ MBBI = nextNoDebugNoCFIInst (MBBI, MBB.end ()))
52
68
if (MBBI->getOpcode () == RISCV::CM_POP)
53
69
return MBBI;
54
70
@@ -76,6 +92,9 @@ bool RISCVPushPopOpt::usePopRet(MachineBasicBlock::iterator &MBBI,
76
92
for (unsigned i = FirstNonDeclaredOp; i < MBBI->getNumOperands (); ++i)
77
93
PopRetBuilder.add (MBBI->getOperand (i));
78
94
95
+ // Remove CFI instructions, they are not needed for cm.popret and cm.popretz
96
+ eraseCFIInst (MBBI, NextI);
97
+
79
98
MBBI->eraseFromParent ();
80
99
NextI->eraseFromParent ();
81
100
return true ;
@@ -92,8 +111,8 @@ bool RISCVPushPopOpt::adjustRetVal(MachineBasicBlock::iterator &MBBI) {
92
111
// Since POP instruction is in Epilogue no normal instructions will follow
93
112
// after it. Therefore search only previous ones to find the return value.
94
113
for (MachineBasicBlock::reverse_iterator I =
95
- next_nodbg (MBBI.getReverse (), RE);
96
- I != RE; I = next_nodbg (I, RE)) {
114
+ nextNoDebugNoCFIInst (MBBI.getReverse (), RE);
115
+ I != RE; I = nextNoDebugNoCFIInst (I, RE)) {
97
116
MachineInstr &MI = *I;
98
117
if (auto OperandPair = TII->isCopyInstrImpl (MI)) {
99
118
Register DestReg = OperandPair->Destination ->getReg ();
@@ -138,7 +157,7 @@ bool RISCVPushPopOpt::runOnMachineFunction(MachineFunction &Fn) {
138
157
bool Modified = false ;
139
158
for (auto &MBB : Fn) {
140
159
MachineBasicBlock::iterator MBBI = containsPop (MBB);
141
- MachineBasicBlock::iterator NextI = next_nodbg (MBBI, MBB.end ());
160
+ MachineBasicBlock::iterator NextI = nextNoDebugNoCFIInst (MBBI, MBB.end ());
142
161
if (MBBI != MBB.end () && NextI != MBB.end () &&
143
162
NextI->getOpcode () == RISCV::PseudoRET)
144
163
Modified |= usePopRet (MBBI, NextI, adjustRetVal (MBBI));
0 commit comments