mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-22 10:42:39 +01:00
Call Frame Information (CFI) Handling for Basic Block Sections
This patch handles CFI with basic block sections, which unlike DebugInfo does not support ranges. The DWARF standard explicitly requires emitting separate CFI Frame Descriptor Entries for each contiguous fragment of a function. Thus, the CFI information for all callee-saved registers (possibly including the frame pointer, if necessary) have to be emitted along with redefining the Call Frame Address (CFA), viz. where the current frame starts. CFI directives are emitted in FDE’s in the object file with a low_pc, high_pc specification. So, a single FDE must point to a contiguous code region unlike debug info which has the support for ranges. This is what complicates CFI for basic block sections. Now, what happens when we start placing individual basic blocks in unique sections: * Basic block sections allow the linker to randomly reorder basic blocks in the address space such that a given basic block can become non-contiguous with the original function. * The different basic block sections can no longer share the cfi_startproc and cfi_endproc directives. So, each basic block section should emit this independently. * Each (cfi_startproc, cfi_endproc) directive will result in a new FDE that caters to that basic block section. * Now, this basic block section needs to duplicate the information from the entry block to compute the CFA as it is an independent entity. It cannot refer to the FDE of the original function and hence must duplicate all the stuff that is needed to compute the CFA on its own. * We are working on a de-duplication patch that can share common information in FDEs in a CIE (Common Information Entry) and we will present this as a follow up patch. This can significantly reduce the duplication overhead and is particularly useful when several basic block sections are created. * The CFI directives are emitted similarly for registers that are pushed onto the stack, like callee saved registers in the prologue. There are cfi directives that emit how to retrieve the value of the register at that point when the push happened. This has to be duplicated too in a basic block that is floated as a separate section. Differential Revision: https://reviews.llvm.org/D79978
This commit is contained in:
parent
518722cd87
commit
8fd1e853a6
@ -202,6 +202,17 @@ public:
|
||||
virtual void emitEpilogue(MachineFunction &MF,
|
||||
MachineBasicBlock &MBB) const = 0;
|
||||
|
||||
/// With basic block sections, emit callee saved frame moves for basic blocks
|
||||
/// that are in a different section.
|
||||
virtual void
|
||||
emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
|
||||
MachineBasicBlock::iterator MBBI) const {}
|
||||
|
||||
virtual void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
|
||||
MachineBasicBlock::iterator MBBI,
|
||||
const DebugLoc &DL,
|
||||
bool IsPrologue) const {}
|
||||
|
||||
/// Replace a StackProbe stub (if any) with the actual probe code inline
|
||||
virtual void inlineStackProbe(MachineFunction &MF,
|
||||
MachineBasicBlock &PrologueMBB) const {}
|
||||
|
@ -3067,18 +3067,30 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
|
||||
if (isVerbose() && MBB.hasLabelMustBeEmitted()) {
|
||||
OutStreamer->AddComment("Label of block must be emitted");
|
||||
}
|
||||
auto *BBSymbol = MBB.getSymbol();
|
||||
// Switch to a new section if this basic block must begin a section.
|
||||
if (MBB.isBeginSection()) {
|
||||
OutStreamer->SwitchSection(
|
||||
getObjFileLowering().getSectionForMachineBasicBlock(MF->getFunction(),
|
||||
MBB, TM));
|
||||
CurrentSectionBeginSym = MBB.getSymbol();
|
||||
CurrentSectionBeginSym = BBSymbol;
|
||||
}
|
||||
OutStreamer->emitLabel(MBB.getSymbol());
|
||||
OutStreamer->emitLabel(BBSymbol);
|
||||
// With BB sections, each basic block must handle CFI information on its own
|
||||
// if it begins a section.
|
||||
if (MBB.isBeginSection())
|
||||
for (const HandlerInfo &HI : Handlers)
|
||||
HI.Handler->beginBasicBlock(MBB);
|
||||
}
|
||||
}
|
||||
|
||||
void AsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) {}
|
||||
void AsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) {
|
||||
// Check if CFI information needs to be updated for this MBB with basic block
|
||||
// sections.
|
||||
if (MBB.isEndSection())
|
||||
for (const HandlerInfo &HI : Handlers)
|
||||
HI.Handler->endBasicBlock(MBB);
|
||||
}
|
||||
|
||||
void AsmPrinter::emitVisibility(MCSymbol *Sym, unsigned Visibility,
|
||||
bool IsDefinition) const {
|
||||
|
@ -47,7 +47,7 @@ void DwarfCFIExceptionBase::markFunctionEnd() {
|
||||
}
|
||||
|
||||
void DwarfCFIExceptionBase::endFragment() {
|
||||
if (shouldEmitCFI)
|
||||
if (shouldEmitCFI && !Asm->MF->hasBBSections())
|
||||
Asm->OutStreamer->emitCFIEndProc();
|
||||
}
|
||||
|
||||
@ -172,3 +172,12 @@ void DwarfCFIException::endFunction(const MachineFunction *MF) {
|
||||
|
||||
emitExceptionTable();
|
||||
}
|
||||
|
||||
void DwarfCFIException::beginBasicBlock(const MachineBasicBlock &MBB) {
|
||||
beginFragment(&MBB, getExceptionSym);
|
||||
}
|
||||
|
||||
void DwarfCFIException::endBasicBlock(const MachineBasicBlock &MBB) {
|
||||
if (shouldEmitCFI)
|
||||
Asm->OutStreamer->emitCFIEndProc();
|
||||
}
|
||||
|
@ -66,6 +66,9 @@ public:
|
||||
|
||||
void beginFragment(const MachineBasicBlock *MBB,
|
||||
ExceptionSymbolProvider ESP) override;
|
||||
|
||||
void beginBasicBlock(const MachineBasicBlock &MBB) override;
|
||||
void endBasicBlock(const MachineBasicBlock &MBB) override;
|
||||
};
|
||||
|
||||
class LLVM_LIBRARY_VISIBILITY ARMException : public DwarfCFIExceptionBase {
|
||||
|
@ -303,28 +303,31 @@ bool CFIInstrInserter::insertCFIInstrs(MachineFunction &MF) {
|
||||
auto MBBI = MBBInfo.MBB->begin();
|
||||
DebugLoc DL = MBBInfo.MBB->findDebugLoc(MBBI);
|
||||
|
||||
if (PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset) {
|
||||
// If the current MBB will be placed in a unique section, a full DefCfa
|
||||
// must be emitted.
|
||||
const bool ForceFullCFA = MBB.isBeginSection();
|
||||
|
||||
if ((PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset &&
|
||||
PrevMBBInfo->OutgoingCFARegister != MBBInfo.IncomingCFARegister) ||
|
||||
ForceFullCFA) {
|
||||
// If both outgoing offset and register of a previous block don't match
|
||||
// incoming offset and register of this block, add a def_cfa instruction
|
||||
// with the correct offset and register for this block.
|
||||
if (PrevMBBInfo->OutgoingCFARegister != MBBInfo.IncomingCFARegister) {
|
||||
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
|
||||
nullptr, MBBInfo.IncomingCFARegister, getCorrectCFAOffset(&MBB)));
|
||||
BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
||||
.addCFIIndex(CFIIndex);
|
||||
// If outgoing offset of a previous block doesn't match incoming offset
|
||||
// of this block, add a def_cfa_offset instruction with the correct
|
||||
// offset for this block.
|
||||
} else {
|
||||
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(
|
||||
nullptr, getCorrectCFAOffset(&MBB)));
|
||||
BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
||||
.addCFIIndex(CFIIndex);
|
||||
}
|
||||
// incoming offset and register of this block, or if this block begins a
|
||||
// section, add a def_cfa instruction with the correct offset and
|
||||
// register for this block.
|
||||
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfa(
|
||||
nullptr, MBBInfo.IncomingCFARegister, getCorrectCFAOffset(&MBB)));
|
||||
BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
||||
.addCFIIndex(CFIIndex);
|
||||
InsertedCFIInstr = true;
|
||||
} else if (PrevMBBInfo->OutgoingCFAOffset != MBBInfo.IncomingCFAOffset) {
|
||||
// If outgoing offset of a previous block doesn't match incoming offset
|
||||
// of this block, add a def_cfa_offset instruction with the correct
|
||||
// offset for this block.
|
||||
unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(
|
||||
nullptr, getCorrectCFAOffset(&MBB)));
|
||||
BuildMI(*MBBInfo.MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
|
||||
.addCFIIndex(CFIIndex);
|
||||
InsertedCFIInstr = true;
|
||||
// If outgoing register of a previous block doesn't match incoming
|
||||
// register of this block, add a def_cfa_register instruction with the
|
||||
// correct register for this block.
|
||||
} else if (PrevMBBInfo->OutgoingCFARegister !=
|
||||
MBBInfo.IncomingCFARegister) {
|
||||
unsigned CFIIndex =
|
||||
@ -335,6 +338,14 @@ bool CFIInstrInserter::insertCFIInstrs(MachineFunction &MF) {
|
||||
InsertedCFIInstr = true;
|
||||
}
|
||||
|
||||
if (ForceFullCFA) {
|
||||
MF.getSubtarget().getFrameLowering()->emitCalleeSavedFrameMoves(
|
||||
*MBBInfo.MBB, MBBI);
|
||||
InsertedCFIInstr = true;
|
||||
PrevMBBInfo = &MBBInfo;
|
||||
continue;
|
||||
}
|
||||
|
||||
BitVector SetDifference = PrevMBBInfo->OutgoingCSRSaved;
|
||||
SetDifference.reset(MBBInfo.IncomingCSRSaved);
|
||||
for (int Reg : SetDifference.set_bits()) {
|
||||
|
@ -24,8 +24,9 @@ public:
|
||||
: TargetFrameLowering(StackGrowsDown, Align(16), 0, Align(16),
|
||||
true /*StackRealignable*/) {}
|
||||
|
||||
void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
|
||||
MachineBasicBlock::iterator MBBI) const;
|
||||
void
|
||||
emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
|
||||
MachineBasicBlock::iterator MBBI) const override;
|
||||
|
||||
MachineBasicBlock::iterator
|
||||
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
|
||||
|
@ -479,6 +479,29 @@ void X86FrameLowering::BuildCFI(MachineBasicBlock &MBB,
|
||||
.addCFIIndex(CFIIndex);
|
||||
}
|
||||
|
||||
/// Emits Dwarf Info specifying offsets of callee saved registers and
|
||||
/// frame pointer. This is called only when basic block sections are enabled.
|
||||
void X86FrameLowering::emitCalleeSavedFrameMoves(
|
||||
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) const {
|
||||
MachineFunction &MF = *MBB.getParent();
|
||||
if (!hasFP(MF)) {
|
||||
emitCalleeSavedFrameMoves(MBB, MBBI, DebugLoc{}, true);
|
||||
return;
|
||||
}
|
||||
const MachineModuleInfo &MMI = MF.getMMI();
|
||||
const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
|
||||
const unsigned FramePtr = TRI->getFrameRegister(MF);
|
||||
const unsigned MachineFramePtr =
|
||||
STI.isTarget64BitILP32() ? unsigned(getX86SubSuperRegister(FramePtr, 64))
|
||||
: FramePtr;
|
||||
unsigned DwarfReg = MRI->getDwarfRegNum(MachineFramePtr, true);
|
||||
// Offset = space for return address + size of the frame pointer itself.
|
||||
unsigned Offset = (Is64Bit ? 8 : 4) + (Uses64BitFramePtr ? 8 : 4);
|
||||
BuildCFI(MBB, MBBI, DebugLoc{},
|
||||
MCCFIInstruction::createOffset(nullptr, DwarfReg, -Offset));
|
||||
emitCalleeSavedFrameMoves(MBB, MBBI, DebugLoc{}, true);
|
||||
}
|
||||
|
||||
void X86FrameLowering::emitCalleeSavedFrameMoves(
|
||||
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
|
||||
const DebugLoc &DL, bool IsPrologue) const {
|
||||
|
@ -58,9 +58,14 @@ public:
|
||||
void inlineStackProbe(MachineFunction &MF,
|
||||
MachineBasicBlock &PrologMBB) const override;
|
||||
|
||||
void
|
||||
emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
|
||||
MachineBasicBlock::iterator MBBI) const override;
|
||||
|
||||
void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
|
||||
MachineBasicBlock::iterator MBBI,
|
||||
const DebugLoc &DL, bool IsPrologue) const;
|
||||
const DebugLoc &DL,
|
||||
bool IsPrologue) const override;
|
||||
|
||||
/// emitProlog/emitEpilog - These methods insert prolog and epilog code into
|
||||
/// the function.
|
||||
|
86
test/CodeGen/X86/cfi-basic-block-sections-1.ll
Normal file
86
test/CodeGen/X86/cfi-basic-block-sections-1.ll
Normal file
@ -0,0 +1,86 @@
|
||||
; RUN: llc -O0 %s --basicblock-sections=all -mtriple=x86_64 -filetype=asm --frame-pointer=all -o - | FileCheck --check-prefix=SECTIONS_CFI %s
|
||||
; RUN: llc -O0 %s --basicblock-sections=all -mtriple=x86_64 -filetype=asm --frame-pointer=none -o - | FileCheck --check-prefix=SECTIONS_NOFP_CFI %s
|
||||
; RUN: llc -O0 %s --basicblock-sections=all -mtriple=x86_64 -filetype=obj --frame-pointer=all -o - | llvm-dwarfdump --eh-frame - | FileCheck --check-prefix=EH_FRAME %s
|
||||
|
||||
;; void f1();
|
||||
;; void f3(bool b) {
|
||||
;; if (b)
|
||||
;; f1();
|
||||
;; }
|
||||
|
||||
|
||||
; SECTIONS_CFI: _Z2f3b:
|
||||
; SECTIONS_CFI: .cfi_startproc
|
||||
; SECTIONS_CFI: .cfi_def_cfa_offset 16
|
||||
; SECTIONS_CFI: .cfi_offset %rbp, -16
|
||||
; SECTIONS_CFI: .cfi_def_cfa_register %rbp
|
||||
; SECTIONS_CFI: .cfi_endproc
|
||||
|
||||
; SECTIONS_CFI: _Z2f3b.1:
|
||||
; SECTIONS_CFI-NEXT: .cfi_startproc
|
||||
; SECTIONS_CFI-NEXT: .cfi_def_cfa %rbp, 16
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset %rbp, -16
|
||||
; SECTIONS_CFI: .cfi_endproc
|
||||
|
||||
; SECTIONS_CFI: _Z2f3b.2:
|
||||
; SECTIONS_CFI-NEXT: .cfi_startproc
|
||||
; SECTIONS_CFI-NEXT: .cfi_def_cfa %rbp, 16
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset %rbp, -16
|
||||
; SECTIONS_CFI: .cfi_def_cfa
|
||||
; SECTIONS_CFI: .cfi_endproc
|
||||
|
||||
|
||||
; SECTIONS_NOFP_CFI: _Z2f3b:
|
||||
; SECTIONS_NOFP_CFI: .cfi_startproc
|
||||
; SECTIONS_NOFP_CFI: .cfi_def_cfa_offset 16
|
||||
; SECTIONS_NOFP_CFI: .cfi_endproc
|
||||
|
||||
; SECTIONS_NOFP_CFI: _Z2f3b.1:
|
||||
; SECTIONS_NOFP_CFI-NEXT: .cfi_startproc
|
||||
; SECTIONS_NOFP_CFI-NEXT: .cfi_def_cfa %rsp, 16
|
||||
; SECTIONS_NOFP_CFI: .cfi_endproc
|
||||
|
||||
; SECTIONS_NOFP_CFI: _Z2f3b.2:
|
||||
; SECTIONS_NOFP_CFI-NEXT: .cfi_startproc
|
||||
; SECTIONS_NOFP_CFI-NEXT: .cfi_def_cfa %rsp, 16
|
||||
; SECTIONS_NOFP_CFI: .cfi_endproc
|
||||
|
||||
|
||||
;; There must be 1 CIE and 3 FDEs.
|
||||
|
||||
; EH_FRAME: CIE
|
||||
; EH_FRAME: DW_CFA_def_cfa
|
||||
; EH_FRAME: DW_CFA_offset
|
||||
|
||||
; EH_FRAME: FDE cie=
|
||||
; EH_FRAME: DW_CFA_def_cfa_offset
|
||||
; EH_FRAME: DW_CFA_offset
|
||||
; EH_FRAME: DW_CFA_def_cfa_register
|
||||
|
||||
; EH_FRAME: FDE cie=
|
||||
; EH_FRAME: DW_CFA_def_cfa
|
||||
; EH_FRAME: DW_CFA_offset
|
||||
|
||||
; EH_FRAME: FDE cie=
|
||||
; EH_FRAME: DW_CFA_def_cfa
|
||||
; EH_FRAME: DW_CFA_offset
|
||||
|
||||
; Function Attrs: noinline optnone uwtable
|
||||
define dso_local void @_Z2f3b(i1 zeroext %b) {
|
||||
entry:
|
||||
%b.addr = alloca i8, align 1
|
||||
%frombool = zext i1 %b to i8
|
||||
store i8 %frombool, i8* %b.addr, align 1
|
||||
%0 = load i8, i8* %b.addr, align 1
|
||||
%tobool = trunc i8 %0 to i1
|
||||
br i1 %tobool, label %if.then, label %if.end
|
||||
|
||||
if.then: ; preds = %entry
|
||||
call void @_Z2f1v()
|
||||
br label %if.end
|
||||
|
||||
if.end: ; preds = %if.then, %entry
|
||||
ret void
|
||||
}
|
||||
|
||||
declare dso_local void @_Z2f1v()
|
@ -0,0 +1,53 @@
|
||||
;; This test checks if CFI instructions for all callee saved registers are emitted
|
||||
;; correctly with basic block sections.
|
||||
; RUN: llc %s -mtriple=x86_64 -filetype=asm --basicblock-sections=all --frame-pointer=all -o - | FileCheck --check-prefix=SECTIONS_CFI %s
|
||||
|
||||
; SECTIONS_CFI: _Z3foob:
|
||||
; SECTIONS_CFI: .cfi_offset %rbp, -16
|
||||
; SECTIONS_CFI: .cfi_offset [[RA:%r.+]], -56
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RB:%r.+]], -48
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RC:%r.+]], -40
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RD:%r.+]], -32
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RE:%r.+]], -24
|
||||
|
||||
; SECTIONS_CFI: _Z3foob.1:
|
||||
; SECTIONS_CFI: .cfi_offset %rbp, -16
|
||||
; SECTIONS_CFI: .cfi_offset [[RA]], -56
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RB]], -48
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RC]], -40
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RD]], -32
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RE]], -24
|
||||
|
||||
; SECTIONS_CFI: _Z3foob.2:
|
||||
; SECTIONS_CFI: .cfi_offset %rbp, -16
|
||||
; SECTIONS_CFI: .cfi_offset [[RA]], -56
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RB]], -48
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RC]], -40
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RD]], -32
|
||||
; SECTIONS_CFI-NEXT: .cfi_offset [[RE]], -24
|
||||
|
||||
|
||||
;; void foo(bool b) {
|
||||
;; if (b) // adds a basic block
|
||||
;; // clobber all callee-save registers to force them to be callee-saved and to
|
||||
;; // be described by cfi_offset directives.
|
||||
;; asm("nop" ::: "r12", "r13", "r14", "r15", "rbx");
|
||||
;; }
|
||||
|
||||
define dso_local void @_Z3foob(i1 zeroext %b) {
|
||||
entry:
|
||||
%b.addr = alloca i8, align 1
|
||||
%frombool = zext i1 %b to i8
|
||||
store i8 %frombool, i8* %b.addr, align 1
|
||||
%0 = load i8, i8* %b.addr, align 1
|
||||
%tobool = trunc i8 %0 to i1
|
||||
br i1 %tobool, label %if.then, label %if.end
|
||||
|
||||
if.then: ; preds = %entry
|
||||
call void asm sideeffect "nop", "~{r12},~{r13},~{r14},~{r15},~{rbx},~{dirflag},~{fpsr},~{flags}"() #1, !srcloc !2
|
||||
br label %if.end
|
||||
|
||||
if.end: ; preds = %if.then, %entry
|
||||
ret void
|
||||
}
|
||||
!2 = !{i32 38}
|
Loading…
Reference in New Issue
Block a user