LLVM  9.0.0svn
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"
14 #include "llvm/ADT/Twine.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSymbol.h"
33 using namespace llvm;
34 
36  : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {}
37 
39  endFragment();
40 
41  // Map all labels and get rid of any dead landing pads.
42  if (!Asm->MF->getLandingPads().empty()) {
43  MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
44  NonConstMF->tidyLandingPads();
45  }
46 }
47 
49  if (shouldEmitCFI)
50  Asm->OutStreamer->EmitCFIEndProc();
51 }
52 
54  : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
55  forceEmitPersonality(false), shouldEmitLSDA(false),
56  shouldEmitMoves(false) {}
57 
59 
60 /// endModule - Emit all exception information that should come after the
61 /// content.
63  // SjLj uses this pass and it doesn't need this info.
64  if (!Asm->MAI->usesCFIForEH())
65  return;
66 
68 
69  unsigned PerEncoding = TLOF.getPersonalityEncoding();
70 
71  if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
72  return;
73 
74  // Emit references to all used personality functions
75  for (const Function *Personality : MMI->getPersonalities()) {
76  if (!Personality)
77  continue;
78  MCSymbol *Sym = Asm->getSymbol(Personality);
80  }
81 }
82 
84  return Asm->getCurExceptionSym();
85 }
86 
88  shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
89  const Function &F = MF->getFunction();
90 
91  // If any landing pads survive, we need an EH table.
92  bool hasLandingPads = !MF->getLandingPads().empty();
93 
94  // See if we need frame move info.
96 
97  shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
98 
100  unsigned PerEncoding = TLOF.getPersonalityEncoding();
101  const Function *Per = nullptr;
102  if (F.hasPersonalityFn())
103  Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
104 
105  // Emit a personality function even when there are no landing pads
106  forceEmitPersonality =
107  // ...if a personality function is explicitly specified
108  F.hasPersonalityFn() &&
109  // ... and it's not known to be a noop in the absence of invokes
111  // ... and we're not explicitly asked not to emit it
113 
114  shouldEmitPersonality =
115  (forceEmitPersonality ||
116  (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
117  Per;
118 
119  unsigned LSDAEncoding = TLOF.getLSDAEncoding();
120  shouldEmitLSDA = shouldEmitPersonality &&
121  LSDAEncoding != dwarf::DW_EH_PE_omit;
122 
124  (shouldEmitPersonality || shouldEmitMoves);
126 }
127 
130  if (!shouldEmitCFI)
131  return;
132 
133  if (!hasEmittedCFISections) {
135  Asm->OutStreamer->EmitCFISections(false, true);
136  hasEmittedCFISections = true;
137  }
138 
139  Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
140 
141  // Indicate personality routine, if any.
142  if (!shouldEmitPersonality)
143  return;
144 
145  auto &F = MBB->getParent()->getFunction();
146  auto *P = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
147  assert(P && "Expected personality function");
148 
149  // If we are forced to emit this personality, make sure to record
150  // it because it might not appear in any landingpad
151  if (forceEmitPersonality)
152  MMI->addPersonality(P);
153 
155  unsigned PerEncoding = TLOF.getPersonalityEncoding();
156  const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
157  Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
158 
159  // Provide LSDA information.
160  if (shouldEmitLSDA)
161  Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
162 }
163 
164 /// endFunction - Gather and emit post-function exception information.
165 ///
167  if (!shouldEmitPersonality)
168  return;
169 
171 }
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:310
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:214
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
This class represents lattice values for constants.
Definition: AllocatorList.h:23
virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, const MCSymbol *Sym) const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
bool needsOnlyDebugCFIMoves() const
Returns false if needsCFIMoves() == CFI_M_EH for any function in the module.
Definition: AsmPrinter.h:325
CFIMoveType needsCFIMoves() const
Definition: AsmPrinter.cpp:925
void beginFragment(const MachineBasicBlock *MBB, ExceptionSymbolProvider ESP) override
bool shouldEmitCFI
Per-function flag to indicate if frame CFI info should be emitted.
F(f)
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:96
MachineModuleInfo & getMMI() const
DwarfCFIException(AsmPrinter *A)
MachineModuleInfo * MMI
Collected machine module information.
Definition: EHStreamer.h:36
Emits exception handling directives.
Definition: EHStreamer.h:30
void endFunction(const MachineFunction *) override
Gather and emit post-function exception information.
void tidyLandingPads(DenseMap< MCSymbol *, uintptr_t > *LPMap=nullptr, bool TidyIfNoBeginLabels=true)
Remap landing pad labels and remove any deleted landing pads.
const MCContext & getContext() const
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:726
void beginFunction(const MachineFunction *MF) override
Gather pre-function exception information.
#define P(N)
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:84
void addPersonality(const Function *Personality)
Provide the personality function for the exception information.
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:81
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
const Constant * stripPointerCasts() const
Definition: Constant.h:177
AsmPrinter * Asm
Target of directive emission.
Definition: EHStreamer.h:33
bool hasEmittedCFISections
Per-module flag to indicate if .cfi_section has beeen emitted.
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:443
static MCSymbol * getExceptionSym(AsmPrinter *Asm)
Module.h This file contains the declarations for the Module class.
MCSymbol * ExceptionSymbolProvider(AsmPrinter *Asm)
const Function & getFunction() const
Return the LLVM function that this machine code represents.
bool usesCFIForEH() const
Returns true if the exception handling method for the platform uses call frame information to unwind...
Definition: MCAsmInfo.h:585
bool needsUnwindTableEntry() const
True if this function needs an unwind table.
Definition: Function.h:594
This file contains constants used for implementing Dwarf debug support.
MCSymbol * getCurExceptionSym()
void endModule() override
Emit all exception information that should come after the content.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSymbol * emitExceptionTable()
Emit landing pads and actions.
Definition: EHStreamer.cpp:349
const std::vector< const Function * > & getPersonalities() const
Return array of personality functions ever seen.
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.cpp:1384
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:218
virtual MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const
bool isNoOpWithoutInvoke(EHPersonality Pers)
Return true if this personality may be safely removed if there are no invoke instructions remaining i...