LLVM  6.0.0svn
DwarfCFIException.cpp
Go to the documentation of this file.
1 //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing DWARF exception info into asm files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "DwarfException.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/Twine.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Mangler.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/MC/MCContext.h"
30 #include "llvm/MC/MCExpr.h"
31 #include "llvm/MC/MCSection.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MCSymbol.h"
39 using namespace llvm;
40 
42  : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {}
43 
45  endFragment();
46 
47  // Map all labels and get rid of any dead landing pads.
48  if (!Asm->MF->getLandingPads().empty()) {
49  MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
50  NonConstMF->tidyLandingPads();
51  }
52 }
53 
55  if (shouldEmitCFI)
56  Asm->OutStreamer->EmitCFIEndProc();
57 }
58 
60  : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
61  forceEmitPersonality(false), shouldEmitLSDA(false),
62  shouldEmitMoves(false) {}
63 
65 
66 /// endModule - Emit all exception information that should come after the
67 /// content.
69  // SjLj uses this pass and it doesn't need this info.
70  if (!Asm->MAI->usesCFIForEH())
71  return;
72 
74 
75  unsigned PerEncoding = TLOF.getPersonalityEncoding();
76 
77  if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
78  return;
79 
80  // Emit references to all used personality functions
81  for (const Function *Personality : MMI->getPersonalities()) {
82  if (!Personality)
83  continue;
84  MCSymbol *Sym = Asm->getSymbol(Personality);
86  }
87 }
88 
90  return Asm->getCurExceptionSym();
91 }
92 
94  shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
95  const Function *F = MF->getFunction();
96 
97  // If any landing pads survive, we need an EH table.
98  bool hasLandingPads = !MF->getLandingPads().empty();
99 
100  // See if we need frame move info.
102 
103  shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
104 
106  unsigned PerEncoding = TLOF.getPersonalityEncoding();
107  const Function *Per = nullptr;
108  if (F->hasPersonalityFn())
109  Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
110 
111  // Emit a personality function even when there are no landing pads
112  forceEmitPersonality =
113  // ...if a personality function is explicitly specified
114  F->hasPersonalityFn() &&
115  // ... and it's not known to be a noop in the absence of invokes
117  // ... and we're not explicitly asked not to emit it
119 
120  shouldEmitPersonality =
121  (forceEmitPersonality ||
122  (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
123  Per;
124 
125  unsigned LSDAEncoding = TLOF.getLSDAEncoding();
126  shouldEmitLSDA = shouldEmitPersonality &&
127  LSDAEncoding != dwarf::DW_EH_PE_omit;
128 
130  (shouldEmitPersonality || shouldEmitMoves);
132 }
133 
136  if (!shouldEmitCFI)
137  return;
138 
139  if (!hasEmittedCFISections) {
141  Asm->OutStreamer->EmitCFISections(false, true);
142  hasEmittedCFISections = true;
143  }
144 
145  Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
146 
147  // Indicate personality routine, if any.
148  if (!shouldEmitPersonality)
149  return;
150 
151  auto *F = MBB->getParent()->getFunction();
152  auto *P = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
153  assert(P && "Expected personality function");
154 
155  // If we are forced to emit this personality, make sure to record
156  // it because it might not appear in any landingpad
157  if (forceEmitPersonality)
158  MMI->addPersonality(P);
159 
161  unsigned PerEncoding = TLOF.getPersonalityEncoding();
162  const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
163  Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
164 
165  // Provide LSDA information.
166  if (shouldEmitLSDA)
167  Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
168 }
169 
170 /// endFunction - Gather and emit post-function exception information.
171 ///
173  if (!shouldEmitPersonality)
174  return;
175 
177 }
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:283
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:205
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:92
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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:42
bool needsOnlyDebugCFIMoves() const
Returns false if needsCFIMoves() == CFI_M_EH for any function in the module.
Definition: AsmPrinter.h:303
CFIMoveType needsCFIMoves() const
Definition: AsmPrinter.cpp:917
const MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:95
void beginFragment(const MachineBasicBlock *MBB, ExceptionSymbolProvider ESP) override
bool shouldEmitCFI
Per-function flag to indicate if frame CFI info should be emitted.
F(f)
MachineModuleInfo & getMMI() const
DwarfCFIException(AsmPrinter *A)
MachineModuleInfo * MMI
Collected machine module information.
Definition: EHStreamer.h:37
Emits exception handling directives.
Definition: EHStreamer.h:31
void endFunction(const MachineFunction *) override
Gather and emit post-function exception information.
void emitExceptionTable()
Emit landing pads and actions.
Definition: EHStreamer.cpp:349
const MCContext & getContext() const
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:634
void beginFunction(const MachineFunction *MF) override
Gather pre-function exception information.
unsigned getLSDAEncoding() const
#define P(N)
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:83
void addPersonality(const Function *Personality)
Provide the personality function for the exception information.
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:80
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:77
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
unsigned getPersonalityEncoding() const
const Constant * stripPointerCasts() const
Definition: Constant.h:153
AsmPrinter * Asm
Target of directive emission.
Definition: EHStreamer.h:34
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:433
static MCSymbol * getExceptionSym(AsmPrinter *Asm)
Module.h This file contains the declarations for the Module class.
MCSymbol * ExceptionSymbolProvider(AsmPrinter *Asm)
bool usesCFIForEH() const
Returns true if the exception handling method for the platform uses call frame information to unwind...
Definition: MCAsmInfo.h:563
bool needsUnwindTableEntry() const
True if this function needs an unwind table.
Definition: Function.h:505
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:323
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:1260
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:209
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...
void tidyLandingPads(DenseMap< MCSymbol *, uintptr_t > *LPMap=nullptr)
Remap landing pad labels and remove any deleted landing pads.