@@ -116,6 +116,32 @@ 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+
119145bool CFIFixup::runOnMachineFunction (MachineFunction &MF) {
120146 const TargetFrameLowering &TFL = *MF.getSubtarget ().getFrameLowering ();
121147 if (!TFL.enableCFIFixup (MF))
@@ -174,12 +200,10 @@ bool CFIFixup::runOnMachineFunction(MachineFunction &MF) {
174200 // Every block inherits the frame state (as recorded in the unwind tables)
175201 // of the previous block. If the intended frame state is different, insert
176202 // compensating CFI instructions.
177- const TargetInstrInfo &TII = *MF.getSubtarget ().getInstrInfo ();
178203 bool Change = false ;
179204 // `InsertPt` always points to the point in a preceding block where we have to
180205 // insert a `.cfi_remember_state`, in the case that the current block needs a
181206 // `.cfi_restore_state`.
182- MachineBasicBlock *InsertMBB = PrologueBlock;
183207 MachineBasicBlock::iterator InsertPt = PrologueEnd;
184208
185209 assert (InsertPt != PrologueBlock->begin () &&
@@ -210,20 +234,10 @@ bool CFIFixup::runOnMachineFunction(MachineFunction &MF) {
210234 if (!Info.StrongNoFrameOnEntry && Info.HasFrameOnEntry && !HasFrame) {
211235 // Reset to the "after prologue" state.
212236
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 ());
227241 Change = true ;
228242 } else if ((Info.StrongNoFrameOnEntry || !Info.HasFrameOnEntry ) &&
229243 HasFrame) {
0 commit comments