LLVM  16.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/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCStreamer.h"
24 using namespace llvm;
25 
27 
29  // Map all labels and get rid of any dead landing pads.
30  if (!Asm->MF->getLandingPads().empty()) {
31  MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
32  NonConstMF->tidyLandingPads();
33  }
34 }
35 
37  : DwarfCFIExceptionBase(A) {}
38 
40 
41 /// endModule - Emit all exception information that should come after the
42 /// content.
44  // SjLj uses this pass and it doesn't need this info.
45  if (!Asm->MAI->usesCFIForEH())
46  return;
47 
49 
50  unsigned PerEncoding = TLOF.getPersonalityEncoding();
51 
52  if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
53  return;
54 
55  // Emit references to all used personality functions
56  for (const Function *Personality : MMI->getPersonalities()) {
57  if (!Personality)
58  continue;
59  MCSymbol *Sym = Asm->getSymbol(Personality);
61  }
62 }
63 
65  shouldEmitPersonality = shouldEmitLSDA = false;
66  const Function &F = MF->getFunction();
67 
68  // If any landing pads survive, we need an EH table.
69  bool hasLandingPads = !MF->getLandingPads().empty();
70 
71  // See if we need frame move info.
72  bool shouldEmitMoves =
74 
76  unsigned PerEncoding = TLOF.getPersonalityEncoding();
77  const Function *Per = nullptr;
78  if (F.hasPersonalityFn())
79  Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
80 
81  // Emit a personality function even when there are no landing pads
82  forceEmitPersonality =
83  // ...if a personality function is explicitly specified
84  F.hasPersonalityFn() &&
85  // ... and it's not known to be a noop in the absence of invokes
87  // ... and we're not explicitly asked not to emit it
88  F.needsUnwindTableEntry();
89 
90  shouldEmitPersonality =
91  (forceEmitPersonality ||
92  (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
93  Per;
94 
95  unsigned LSDAEncoding = TLOF.getLSDAEncoding();
96  shouldEmitLSDA = shouldEmitPersonality &&
97  LSDAEncoding != dwarf::DW_EH_PE_omit;
98 
99  const MCAsmInfo &MAI = *MF->getMMI().getContext().getAsmInfo();
101  shouldEmitCFI =
102  MAI.usesCFIForEH() && (shouldEmitPersonality || shouldEmitMoves);
103  else
104  shouldEmitCFI = Asm->needsCFIForDebug() && shouldEmitMoves;
105 }
106 
108  if (!shouldEmitCFI)
109  return;
110 
111  if (!hasEmittedCFISections) {
113  // If we don't say anything it implies `.cfi_sections .eh_frame`, so we
114  // chose not to be verbose in that case. And with `ForceDwarfFrameSection`,
115  // we should always emit .debug_frame.
116  if (CFISecType == AsmPrinter::CFISection::Debug ||
118  Asm->OutStreamer->emitCFISections(
119  CFISecType == AsmPrinter::CFISection::EH, true);
120  hasEmittedCFISections = true;
121  }
122 
123  Asm->OutStreamer->emitCFIStartProc(/*IsSimple=*/false);
124 
125  // Indicate personality routine, if any.
126  if (!shouldEmitPersonality)
127  return;
128 
129  auto &F = MBB.getParent()->getFunction();
130  auto *P = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
131  assert(P && "Expected personality function");
132 
133  // If we are forced to emit this personality, make sure to record
134  // it because it might not appear in any landingpad
135  if (forceEmitPersonality)
136  MMI->addPersonality(P);
137 
139  unsigned PerEncoding = TLOF.getPersonalityEncoding();
140  const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
141  Asm->OutStreamer->emitCFIPersonality(Sym, PerEncoding);
142 
143  // Provide LSDA information.
144  if (shouldEmitLSDA)
145  Asm->OutStreamer->emitCFILsda(Asm->getMBBExceptionSym(MBB),
146  TLOF.getLSDAEncoding());
147 }
148 
150  if (shouldEmitCFI)
151  Asm->OutStreamer->emitCFIEndProc();
152 }
153 
154 /// endFunction - Gather and emit post-function exception information.
155 ///
157  if (!shouldEmitPersonality)
158  return;
159 
161 }
AsmPrinter.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::AsmPrinter::CFISection
CFISection
Definition: AsmPrinter.h:163
llvm::DwarfCFIExceptionBase::markFunctionEnd
void markFunctionEnd() override
Definition: DwarfCFIException.cpp:28
llvm::DwarfCFIException::~DwarfCFIException
~DwarfCFIException() override
llvm::MachineModuleInfo::getContext
const MCContext & getContext() const
Definition: MachineModuleInfo.h:143
llvm::Function
Definition: Function.h:60
llvm::EHStreamer
Emits exception handling directives.
Definition: EHStreamer.h:30
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::AsmPrinter::MAI
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:90
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
llvm::AsmPrinter::CFISection::Debug
@ Debug
Emit .debug_frame.
llvm::DwarfCFIException::DwarfCFIException
DwarfCFIException(AsmPrinter *A)
Definition: DwarfCFIException.cpp:36
llvm::AsmPrinter::getFunctionCFISectionType
CFISection getFunctionCFISectionType(const Function &F) const
Get the CFISection type for a function.
Definition: AsmPrinter.cpp:1258
llvm::EHStreamer::MMI
MachineModuleInfo * MMI
Collected machine module information.
Definition: EHStreamer.h:36
llvm::MCAsmInfo::usesCFIForEH
bool usesCFIForEH() const
Returns true if the exception handling method for the platform uses call frame information to unwind.
Definition: MCAsmInfo.h:792
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::DwarfCFIException::beginBasicBlockSection
void beginBasicBlockSection(const MachineBasicBlock &MBB) override
Process the beginning of a new basic-block-section within a function.
Definition: DwarfCFIException.cpp:107
llvm::TargetLoweringObjectFile
Definition: TargetLoweringObjectFile.h:45
llvm::classifyEHPersonality
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
Definition: EHPersonalities.cpp:22
TargetMachine.h
llvm::dwarf::DW_EH_PE_indirect
@ DW_EH_PE_indirect
Definition: Dwarf.h:453
llvm::AsmPrinter::OutStreamer
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:99
llvm::TargetOptions::ForceDwarfFrameSection
unsigned ForceDwarfFrameSection
Emit DWARF debug frame section.
Definition: TargetOptions.h:342
MCContext.h
llvm::EHStreamer::emitExceptionTable
MCSymbol * emitExceptionTable()
Emit landing pads and actions.
Definition: EHStreamer.cpp:375
llvm::isNoOpWithoutInvoke
bool isNoOpWithoutInvoke(EHPersonality Pers)
Return true if this personality may be safely removed if there are no invoke instructions remaining i...
Definition: EHPersonalities.h:95
llvm::AsmPrinter::getModuleCFISectionType
CFISection getModuleCFISectionType() const
Get the CFISection type for the module.
Definition: AsmPrinter.h:434
llvm::MachineFunction::tidyLandingPads
void tidyLandingPads(DenseMap< MCSymbol *, uintptr_t > *LPMap=nullptr, bool TidyIfNoBeginLabels=true)
Remap landing pad labels and remove any deleted landing pads.
Definition: MachineFunction.cpp:810
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
llvm::TargetLoweringObjectFile::emitPersonalityValue
virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, const MCSymbol *Sym) const
Definition: TargetLoweringObjectFile.cpp:144
llvm::AsmPrinter::CFISection::None
@ None
Do not emit either .eh_frame or .debug_frame.
llvm::MachineFunction::getMMI
MachineModuleInfo & getMMI() const
Definition: MachineFunction.h:608
llvm::DwarfCFIExceptionBase
Definition: DwarfException.h:24
llvm::MachineModuleInfo::getPersonalities
const std::vector< const Function * > & getPersonalities() const
Return array of personality functions ever seen.
Definition: MachineModuleInfo.h:202
llvm::DwarfCFIException::endModule
void endModule() override
Emit all exception information that should come after the content.
Definition: DwarfCFIException.cpp:43
llvm::DwarfCFIException::beginFunction
void beginFunction(const MachineFunction *MF) override
Gather pre-function exception information.
Definition: DwarfCFIException.cpp:64
llvm::AsmPrinter::needsCFIForDebug
bool needsCFIForDebug() const
Since emitting CFI unwind information is entangled with supporting the exceptions,...
Definition: AsmPrinter.cpp:1282
llvm::MCContext::getAsmInfo
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:448
llvm::TargetLoweringObjectFile::getPersonalityEncoding
unsigned getPersonalityEncoding() const
Definition: TargetLoweringObjectFile.h:168
llvm::DwarfCFIExceptionBase::hasEmittedCFISections
bool hasEmittedCFISections
Per-module flag to indicate if .cfi_section has beeen emitted.
Definition: DwarfException.h:31
llvm::TargetMachine::Options
TargetOptions Options
Definition: TargetMachine.h:117
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::AsmPrinter::getSymbol
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:656
llvm::MachineBasicBlock::getParent
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Definition: MachineBasicBlock.h:261
llvm::AsmPrinter::getMBBExceptionSym
MCSymbol * getMBBExceptionSym(const MachineBasicBlock &MBB)
Definition: AsmPrinter.cpp:2324
llvm::AsmPrinter::MF
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:102
llvm::MachineFunction::getLandingPads
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
Definition: MachineFunction.h:1111
MachineModuleInfo.h
llvm::MachineFunction
Definition: MachineFunction.h:257
TargetOptions.h
MCAsmInfo.h
llvm::EHStreamer::Asm
AsmPrinter * Asm
Target of directive emission.
Definition: EHStreamer.h:33
llvm::TargetLoweringObjectFile::getLSDAEncoding
unsigned getLSDAEncoding() const
Definition: TargetLoweringObjectFile.h:169
Dwarf.h
TargetLoweringObjectFile.h
llvm::DwarfCFIException::endBasicBlockSection
void endBasicBlockSection(const MachineBasicBlock &MBB) override
Process the end of a basic-block-section within a function.
Definition: DwarfCFIException.cpp:149
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:623
llvm::DwarfCFIExceptionBase::shouldEmitCFI
bool shouldEmitCFI
Per-function flag to indicate if frame CFI info should be emitted.
Definition: DwarfException.h:29
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:84
llvm::DwarfCFIException::endFunction
void endFunction(const MachineFunction *) override
Gather and emit post-function exception information.
Definition: DwarfCFIException.cpp:156
llvm::ExceptionHandling::None
@ None
No exception support.
llvm::MCAsmInfo::getExceptionHandlingType
ExceptionHandling getExceptionHandlingType() const
Definition: MCAsmInfo.h:781
llvm::AsmPrinter::TM
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:87
llvm::MachineModuleInfo::addPersonality
void addPersonality(const Function *Personality)
Provide the personality function for the exception information.
Definition: MachineModuleInfo.cpp:95
llvm::AsmPrinter::getObjFileLowering
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:384
MCStreamer.h
DwarfException.h
llvm::dwarf::DW_EH_PE_omit
@ DW_EH_PE_omit
Definition: Dwarf.h:438
llvm::TargetLoweringObjectFile::getCFIPersonalitySymbol
virtual MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const
Definition: TargetLoweringObjectFile.cpp:138
llvm::AsmPrinter::getDataLayout
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:388
llvm::DwarfCFIExceptionBase::DwarfCFIExceptionBase
DwarfCFIExceptionBase(AsmPrinter *A)
Definition: DwarfCFIException.cpp:26
MachineFunction.h
llvm::AsmPrinter::CFISection::EH
@ EH
Emit .eh_frame.