@@ -116,6 +116,32 @@ findPrologueEnd(MachineFunction &MF, MachineBasicBlock::iterator &PrologueEnd) {
116
116
return nullptr ;
117
117
}
118
118
119
+ // Inserts a `.cfi_remember_state` instruction before PrologueEnd and a
120
+ // `.cfi_restore_state` instruction before DstInsertPt. Returns an iterator
121
+ // to the first instruction after the inserted `.cfi_restore_state` instruction.
122
+ static MachineBasicBlock::iterator
123
+ insertRememberRestorePair (MachineBasicBlock::iterator RememberInsertPt,
124
+ MachineBasicBlock::iterator RestoreInsertPt) {
125
+ MachineBasicBlock *RememberMBB = RememberInsertPt->getParent ();
126
+ MachineBasicBlock *RestoreMBB = RestoreInsertPt->getParent ();
127
+ MachineFunction &MF = *RememberMBB->getParent ();
128
+ const TargetInstrInfo &TII = *MF.getSubtarget ().getInstrInfo ();
129
+
130
+ // Insert the `.cfi_remember_state` instruction.
131
+ unsigned CFIIndex =
132
+ MF.addFrameInst (MCCFIInstruction::createRememberState (nullptr ));
133
+ BuildMI (*RememberMBB, RememberInsertPt, DebugLoc (),
134
+ TII.get (TargetOpcode::CFI_INSTRUCTION))
135
+ .addCFIIndex (CFIIndex);
136
+
137
+ // Insert the `.cfi_restore_state` instruction.
138
+ CFIIndex = MF.addFrameInst (MCCFIInstruction::createRestoreState (nullptr ));
139
+ BuildMI (*RestoreMBB, RestoreInsertPt, DebugLoc (),
140
+ TII.get (TargetOpcode::CFI_INSTRUCTION))
141
+ .addCFIIndex (CFIIndex);
142
+ return RestoreInsertPt;
143
+ }
144
+
119
145
bool CFIFixup::runOnMachineFunction (MachineFunction &MF) {
120
146
const TargetFrameLowering &TFL = *MF.getSubtarget ().getFrameLowering ();
121
147
if (!TFL.enableCFIFixup (MF))
@@ -174,12 +200,10 @@ bool CFIFixup::runOnMachineFunction(MachineFunction &MF) {
174
200
// Every block inherits the frame state (as recorded in the unwind tables)
175
201
// of the previous block. If the intended frame state is different, insert
176
202
// compensating CFI instructions.
177
- const TargetInstrInfo &TII = *MF.getSubtarget ().getInstrInfo ();
178
203
bool Change = false ;
179
204
// `InsertPt` always points to the point in a preceding block where we have to
180
205
// insert a `.cfi_remember_state`, in the case that the current block needs a
181
206
// `.cfi_restore_state`.
182
- MachineBasicBlock *InsertMBB = PrologueBlock;
183
207
MachineBasicBlock::iterator InsertPt = PrologueEnd;
184
208
185
209
assert (InsertPt != PrologueBlock->begin () &&
@@ -210,20 +234,10 @@ bool CFIFixup::runOnMachineFunction(MachineFunction &MF) {
210
234
if (!Info.StrongNoFrameOnEntry && Info.HasFrameOnEntry && !HasFrame) {
211
235
// Reset to the "after prologue" state.
212
236
213
- // Insert a `.cfi_remember_state` into the last block known to have a
214
- // stack frame.
215
- unsigned CFIIndex =
216
- MF.addFrameInst (MCCFIInstruction::createRememberState (nullptr ));
217
- BuildMI (*InsertMBB, InsertPt, DebugLoc (),
218
- TII.get (TargetOpcode::CFI_INSTRUCTION))
219
- .addCFIIndex (CFIIndex);
220
- // Insert a `.cfi_restore_state` at the beginning of the current block.
221
- CFIIndex = MF.addFrameInst (MCCFIInstruction::createRestoreState (nullptr ));
222
- InsertPt = BuildMI (*CurrBB, CurrBB->begin (), DebugLoc (),
223
- TII.get (TargetOpcode::CFI_INSTRUCTION))
224
- .addCFIIndex (CFIIndex);
225
- ++InsertPt;
226
- InsertMBB = &*CurrBB;
237
+ // There's an earlier block known to have a stack frame. Insert a
238
+ // `.cfi_remember_state` instruction into that block and a
239
+ // `.cfi_restore_state` instruction at the beginning of the current block.
240
+ InsertPt = insertRememberRestorePair (InsertPt, CurrBB->begin ());
227
241
Change = true ;
228
242
} else if ((Info.StrongNoFrameOnEntry || !Info.HasFrameOnEntry ) &&
229
243
HasFrame) {
0 commit comments