Skip to content

Reland [CFIFixup] Factor CFI remember/restore insertion into a helper (NFC) #113328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions llvm/lib/CodeGen/CFIFixup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,32 @@ findPrologueEnd(MachineFunction &MF, MachineBasicBlock::iterator &PrologueEnd) {
return nullptr;
}

// Inserts a `.cfi_remember_state` instruction before PrologueEnd and a
// `.cfi_restore_state` instruction before DstInsertPt. Returns an iterator
// to the first instruction after the inserted `.cfi_restore_state` instruction.
static MachineBasicBlock::iterator
insertRememberRestorePair(MachineBasicBlock::iterator RememberInsertPt,
MachineBasicBlock::iterator RestoreInsertPt) {
MachineBasicBlock *RememberMBB = RememberInsertPt->getParent();
MachineBasicBlock *RestoreMBB = RestoreInsertPt->getParent();
MachineFunction &MF = *RememberMBB->getParent();
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();

// Insert the `.cfi_remember_state` instruction.
unsigned CFIIndex =
MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
BuildMI(*RememberMBB, RememberInsertPt, DebugLoc(),
TII.get(TargetOpcode::CFI_INSTRUCTION))
.addCFIIndex(CFIIndex);

// Insert the `.cfi_restore_state` instruction.
CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
BuildMI(*RestoreMBB, RestoreInsertPt, DebugLoc(),
TII.get(TargetOpcode::CFI_INSTRUCTION))
.addCFIIndex(CFIIndex);
return RestoreInsertPt;
}

bool CFIFixup::runOnMachineFunction(MachineFunction &MF) {
const TargetFrameLowering &TFL = *MF.getSubtarget().getFrameLowering();
if (!TFL.enableCFIFixup(MF))
Expand Down Expand Up @@ -174,12 +200,10 @@ bool CFIFixup::runOnMachineFunction(MachineFunction &MF) {
// Every block inherits the frame state (as recorded in the unwind tables)
// of the previous block. If the intended frame state is different, insert
// compensating CFI instructions.
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
bool Change = false;
// `InsertPt` always points to the point in a preceding block where we have to
// insert a `.cfi_remember_state`, in the case that the current block needs a
// `.cfi_restore_state`.
MachineBasicBlock *InsertMBB = PrologueBlock;
MachineBasicBlock::iterator InsertPt = PrologueEnd;

assert(InsertPt != PrologueBlock->begin() &&
Expand Down Expand Up @@ -210,20 +234,10 @@ bool CFIFixup::runOnMachineFunction(MachineFunction &MF) {
if (!Info.StrongNoFrameOnEntry && Info.HasFrameOnEntry && !HasFrame) {
// Reset to the "after prologue" state.

// Insert a `.cfi_remember_state` into the last block known to have a
// stack frame.
unsigned CFIIndex =
MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
BuildMI(*InsertMBB, InsertPt, DebugLoc(),
TII.get(TargetOpcode::CFI_INSTRUCTION))
.addCFIIndex(CFIIndex);
// Insert a `.cfi_restore_state` at the beginning of the current block.
CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
InsertPt = BuildMI(*CurrBB, CurrBB->begin(), DebugLoc(),
TII.get(TargetOpcode::CFI_INSTRUCTION))
.addCFIIndex(CFIIndex);
++InsertPt;
InsertMBB = &*CurrBB;
// There's an earlier block known to have a stack frame. Insert a
// `.cfi_remember_state` instruction into that block and a
// `.cfi_restore_state` instruction at the beginning of the current block.
InsertPt = insertRememberRestorePair(InsertPt, CurrBB->begin());
Change = true;
} else if ((Info.StrongNoFrameOnEntry || !Info.HasFrameOnEntry) &&
HasFrame) {
Expand Down
Loading