LLVM 20.0.0git
DwarfCFIException.cpp
Go to the documentation of this file.
1//===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing DWARF exception info into asm files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DwarfException.h"
18#include "llvm/IR/Function.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCStreamer.h"
25using namespace llvm;
26
28
30
31void DwarfCFIException::addPersonality(const GlobalValue *Personality) {
32 if (!llvm::is_contained(Personalities, Personality))
33 Personalities.push_back(Personality);
34}
35
36/// endModule - Emit all exception information that should come after the
37/// content.
39 // SjLj uses this pass and it doesn't need this info.
40 if (!Asm->MAI->usesCFIForEH())
41 return;
42
44
45 unsigned PerEncoding = TLOF.getPersonalityEncoding();
46
47 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
48 return;
49
50 // Emit indirect reference table for all used personality functions
51 for (const GlobalValue *Personality : Personalities) {
52 MCSymbol *Sym = Asm->getSymbol(Personality);
54 Asm->MMI);
55 }
56 Personalities.clear();
57}
58
60 shouldEmitPersonality = shouldEmitLSDA = false;
61 const Function &F = MF->getFunction();
62
63 // If any landing pads survive, we need an EH table.
64 bool hasLandingPads = !MF->getLandingPads().empty();
65
66 // See if we need frame move info.
67 bool shouldEmitMoves =
69
71 unsigned PerEncoding = TLOF.getPersonalityEncoding();
72 const GlobalValue *Per = nullptr;
73 if (F.hasPersonalityFn())
74 Per = dyn_cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts());
75
76 // Emit a personality function even when there are no landing pads
77 forceEmitPersonality =
78 // ...if a personality function is explicitly specified
79 F.hasPersonalityFn() &&
80 // ... and it's not known to be a noop in the absence of invokes
82 // ... and we're not explicitly asked not to emit it
83 F.needsUnwindTableEntry();
84
85 shouldEmitPersonality =
86 (forceEmitPersonality ||
87 (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
88 Per;
89
90 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
91 shouldEmitLSDA = shouldEmitPersonality &&
92 LSDAEncoding != dwarf::DW_EH_PE_omit;
93
94 const MCAsmInfo &MAI = *MF->getContext().getAsmInfo();
96 shouldEmitCFI =
97 MAI.usesCFIForEH() && (shouldEmitPersonality || shouldEmitMoves);
98 else
99 shouldEmitCFI = Asm->usesCFIWithoutEH() && shouldEmitMoves;
100}
101
103 if (!shouldEmitCFI)
104 return;
105
106 if (!hasEmittedCFISections) {
108 // If we don't say anything it implies `.cfi_sections .eh_frame`, so we
109 // chose not to be verbose in that case. And with `ForceDwarfFrameSection`,
110 // we should always emit .debug_frame.
111 if (CFISecType == AsmPrinter::CFISection::Debug ||
113 Asm->OutStreamer->emitCFISections(
114 CFISecType == AsmPrinter::CFISection::EH, true);
115 hasEmittedCFISections = true;
116 }
117
118 Asm->OutStreamer->emitCFIStartProc(/*IsSimple=*/false);
119
120 // Indicate personality routine, if any.
121 if (!shouldEmitPersonality)
122 return;
123
124 auto &F = MBB.getParent()->getFunction();
125 auto *P = dyn_cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts());
126 assert(P && "Expected personality function");
127 // Record the personality function.
128 addPersonality(P);
129
131 unsigned PerEncoding = TLOF.getPersonalityEncoding();
132 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
133 Asm->OutStreamer->emitCFIPersonality(Sym, PerEncoding);
134
135 // Provide LSDA information.
136 if (shouldEmitLSDA)
137 Asm->OutStreamer->emitCFILsda(Asm->getMBBExceptionSym(MBB),
138 TLOF.getLSDAEncoding());
139}
140
142 if (shouldEmitCFI)
143 Asm->OutStreamer->emitCFIEndProc();
144}
145
146/// endFunction - Gather and emit post-function exception information.
147///
149 if (!shouldEmitPersonality)
150 return;
151
153}
MachineBasicBlock & MBB
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains constants used for implementing Dwarf debug support.
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:408
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:701
CFISection getModuleCFISectionType() const
Get the CFISection type for the module.
Definition: AsmPrinter.h:434
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:89
MCSymbol * getMBBExceptionSym(const MachineBasicBlock &MBB)
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:92
CFISection getFunctionCFISectionType(const Function &F) const
Get the CFISection type for a function.
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:107
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:101
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:412
bool usesCFIWithoutEH() const
Since emitting CFI unwind information is entangled with supporting the exceptions,...
@ None
Do not emit either .eh_frame or .debug_frame.
@ Debug
Emit .debug_frame.
void endBasicBlockSection(const MachineBasicBlock &MBB) override
Process the end of a basic-block-section within a function.
~DwarfCFIException() override
void beginBasicBlockSection(const MachineBasicBlock &MBB) override
Process the beginning of a new basic-block-section within a function.
void endFunction(const MachineFunction *) override
Gather and emit post-function exception information.
void endModule() override
Emit all exception information that should come after the content.
DwarfCFIException(AsmPrinter *A)
void beginFunction(const MachineFunction *MF) override
Gather pre-function exception information.
Emits exception handling directives.
Definition: EHStreamer.h:30
AsmPrinter * Asm
Target of directive emission.
Definition: EHStreamer.h:33
MCSymbol * emitExceptionTable()
Emit landing pads and actions.
Definition: EHStreamer.cpp:380
MachineModuleInfo * MMI
Collected machine module information.
Definition: EHStreamer.h:36
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
bool usesCFIForEH() const
Returns true if the exception handling method for the platform uses call frame information to unwind.
Definition: MCAsmInfo.h:753
ExceptionHandling getExceptionHandlingType() const
Definition: MCAsmInfo.h:740
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:412
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MCContext & getContext() const
Function & getFunction()
Return the LLVM function that this machine code represents.
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
virtual MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const
virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, const MCSymbol *Sym, const MachineModuleInfo *MMI) const
TargetOptions Options
unsigned ForceDwarfFrameSection
Emit DWARF debug frame section.
@ DW_EH_PE_indirect
Definition: Dwarf.h:863
@ DW_EH_PE_omit
Definition: Dwarf.h:848
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ None
No exception support.
bool isNoOpWithoutInvoke(EHPersonality Pers)
Return true if this personality may be safely removed if there are no invoke instructions remaining i...
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1903