@@ -116,32 +116,6 @@ findPrologueEnd(MachineFunction &MF, MachineBasicBlock::iterator &PrologueEnd) {
116116 return nullptr ;
117117}
118118
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-
145119bool CFIFixup::runOnMachineFunction (MachineFunction &MF) {
146120 const TargetFrameLowering &TFL = *MF.getSubtarget ().getFrameLowering ();
147121 if (!TFL.enableCFIFixup (MF))
@@ -200,10 +174,12 @@ bool CFIFixup::runOnMachineFunction(MachineFunction &MF) {
200174 // Every block inherits the frame state (as recorded in the unwind tables)
201175 // of the previous block. If the intended frame state is different, insert
202176 // compensating CFI instructions.
177+ const TargetInstrInfo &TII = *MF.getSubtarget ().getInstrInfo ();
203178 bool Change = false ;
204179 // `InsertPt` always points to the point in a preceding block where we have to
205180 // insert a `.cfi_remember_state`, in the case that the current block needs a
206181 // `.cfi_restore_state`.
182+ MachineBasicBlock *InsertMBB = PrologueBlock;
207183 MachineBasicBlock::iterator InsertPt = PrologueEnd;
208184
209185 assert (InsertPt != PrologueBlock->begin () &&
@@ -234,10 +210,20 @@ bool CFIFixup::runOnMachineFunction(MachineFunction &MF) {
234210 if (!Info.StrongNoFrameOnEntry && Info.HasFrameOnEntry && !HasFrame) {
235211 // Reset to the "after prologue" state.
236212
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 ());
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;
241227 Change = true ;
242228 } else if ((Info.StrongNoFrameOnEntry || !Info.HasFrameOnEntry ) &&
243229 HasFrame) {
0 commit comments