LLVM  14.0.0git
EHStreamer.h
Go to the documentation of this file.
1 //===- EHStreamer.h - Exception Handling Directive Streamer -----*- C++ -*-===//
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 exception info into assembly files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
14 #define LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
15 
16 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/Support/Compiler.h"
19 
20 namespace llvm {
21 
22 class AsmPrinter;
23 struct LandingPadInfo;
24 class MachineInstr;
25 class MachineModuleInfo;
26 class MCSymbol;
27 template <typename T> class SmallVectorImpl;
28 
29 /// Emits exception handling directives.
31 protected:
32  /// Target of directive emission.
34 
35  /// Collected machine module information.
37 
38  /// How many leading type ids two landing pads have in common.
39  static unsigned sharedTypeIDs(const LandingPadInfo *L,
40  const LandingPadInfo *R);
41 
42  /// Structure holding a try-range and the associated landing pad.
43  struct PadRange {
44  // The index of the landing pad.
45  unsigned PadIndex;
46 
47  // The index of the begin and end labels in the landing pad's label lists.
48  unsigned RangeIndex;
49  };
50 
52 
53  /// Structure describing an entry in the actions table.
54  struct ActionEntry {
55  int ValueForTypeID; // The value to write - may not be equal to the type id.
57  unsigned Previous;
58  };
59 
60  /// Structure describing an entry in the call-site table.
61  struct CallSiteEntry {
62  // The 'try-range' is BeginLabel .. EndLabel.
63  MCSymbol *BeginLabel; // Null indicates the start of the function.
64  MCSymbol *EndLabel; // Null indicates the end of the function.
65 
66  // LPad contains the landing pad start labels.
67  const LandingPadInfo *LPad; // Null indicates that there is no landing pad.
68 
69  unsigned Action;
70  };
71 
72  /// Structure describing a contiguous range of call-sites which reside
73  /// in the same procedure fragment. With -fbasic-block-sections, there will
74  /// be one call site range per basic block section. Otherwise, we will have
75  /// one call site range containing all the call sites in the function.
76  struct CallSiteRange {
77  // Symbol marking the beginning of the precedure fragment.
78  MCSymbol *FragmentBeginLabel = nullptr;
79  // Symbol marking the end of the procedure fragment.
80  MCSymbol *FragmentEndLabel = nullptr;
81  // LSDA symbol for this call-site range.
82  MCSymbol *ExceptionLabel = nullptr;
83  // Index of the first call-site entry in the call-site table which
84  // belongs to this range.
85  size_t CallSiteBeginIdx = 0;
86  // Index just after the last call-site entry in the call-site table which
87  // belongs to this range.
88  size_t CallSiteEndIdx = 0;
89  // Whether this is the call-site range containing all the landing pads.
90  bool IsLPRange = false;
91  };
92 
93  /// Compute the actions table and gather the first action index for each
94  /// landing pad site.
95  void computeActionsTable(
96  const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
98  SmallVectorImpl<unsigned> &FirstActions);
99 
100  void computePadMap(const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
101  RangeMapType &PadMap);
102 
103  /// Compute the call-site table and the call-site ranges. The entry for an
104  /// invoke has a try-range containing the call, a non-zero landing pad and an
105  /// appropriate action. The entry for an ordinary call has a try-range
106  /// containing the call and zero for the landing pad and the action. Calls
107  /// marked 'nounwind' have no entry and must not be contained in the try-range
108  /// of any entry - they form gaps in the table. Entries must be ordered by
109  /// try-range address. CallSiteRanges vector is only populated for Itanium
110  /// exception handling.
111  virtual void computeCallSiteTable(
113  SmallVectorImpl<CallSiteRange> &CallSiteRanges,
114  const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
115  const SmallVectorImpl<unsigned> &FirstActions);
116 
117  /// Emit landing pads and actions.
118  ///
119  /// The general organization of the table is complex, but the basic concepts
120  /// are easy. First there is a header which describes the location and
121  /// organization of the three components that follow.
122  /// 1. The landing pad site information describes the range of code covered
123  /// by the try. In our case it's an accumulation of the ranges covered
124  /// by the invokes in the try. There is also a reference to the landing
125  /// pad that handles the exception once processed. Finally an index into
126  /// the actions table.
127  /// 2. The action table, in our case, is composed of pairs of type ids
128  /// and next action offset. Starting with the action index from the
129  /// landing pad site, each type Id is checked for a match to the current
130  /// exception. If it matches then the exception and type id are passed
131  /// on to the landing pad. Otherwise the next action is looked up. This
132  /// chain is terminated with a next action of zero. If no type id is
133  /// found the frame is unwound and handling continues.
134  /// 3. Type id table contains references to all the C++ typeinfo for all
135  /// catches in the function. This tables is reversed indexed base 1.
136  ///
137  /// Returns the starting symbol of an exception table.
138  MCSymbol *emitExceptionTable();
139 
140  virtual void emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel);
141 
142  // Helpers for identifying what kind of clause an EH typeid or selector
143  // corresponds to. Negative selectors are for filter clauses, the zero
144  // selector is for cleanups, and positive selectors are for catch clauses.
145  static bool isFilterEHSelector(int Selector) { return Selector < 0; }
146  static bool isCleanupEHSelector(int Selector) { return Selector == 0; }
147  static bool isCatchEHSelector(int Selector) { return Selector > 0; }
148 
149 public:
151  ~EHStreamer() override;
152 
153  // Unused.
154  void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {}
155  void beginInstruction(const MachineInstr *MI) override {}
156  void endInstruction() override {}
157 
158  /// Return `true' if this is a call to a function marked `nounwind'. Return
159  /// `false' otherwise.
160  static bool callToNoUnwindFunction(const MachineInstr *MI);
161 };
162 
163 } // end namespace llvm
164 
165 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::EHStreamer::PadRange::RangeIndex
unsigned RangeIndex
Definition: EHStreamer.h:48
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:105
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::EHStreamer::ActionEntry::NextAction
int NextAction
Definition: EHStreamer.h:56
llvm::EHStreamer::ActionEntry::Previous
unsigned Previous
Definition: EHStreamer.h:57
llvm::EHStreamer
Emits exception handling directives.
Definition: EHStreamer.h:30
DenseMap.h
AsmPrinterHandler.h
llvm::EHStreamer::MMI
MachineModuleInfo * MMI
Collected machine module information.
Definition: EHStreamer.h:36
llvm::EHStreamer::CallSiteEntry::LPad
const LandingPadInfo * LPad
Definition: EHStreamer.h:67
llvm::EHStreamer::isCatchEHSelector
static bool isCatchEHSelector(int Selector)
Definition: EHStreamer.h:147
llvm::EHStreamer::ActionEntry::ValueForTypeID
int ValueForTypeID
Definition: EHStreamer.h:55
llvm::MachineModuleInfo
This class contains meta information specific to a module.
Definition: MachineModuleInfo.h:78
llvm::EHStreamer::isFilterEHSelector
static bool isFilterEHSelector(int Selector)
Definition: EHStreamer.h:145
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
uint64_t
llvm::EHStreamer::endInstruction
void endInstruction() override
Process end of an instruction.
Definition: EHStreamer.h:156
llvm::DenseMap
Definition: DenseMap.h:714
llvm::EHStreamer::beginInstruction
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
Definition: EHStreamer.h:155
llvm::EHStreamer::CallSiteEntry::EndLabel
MCSymbol * EndLabel
Definition: EHStreamer.h:64
llvm::EHStreamer::CallSiteRange
Structure describing a contiguous range of call-sites which reside in the same procedure fragment.
Definition: EHStreamer.h:76
llvm::EHStreamer::setSymbolSize
void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override
For symbols that have a size designated (e.g.
Definition: EHStreamer.h:154
llvm::EHStreamer::ActionEntry
Structure describing an entry in the actions table.
Definition: EHStreamer.h:54
llvm::EHStreamer::CallSiteEntry
Structure describing an entry in the call-site table.
Definition: EHStreamer.h:61
llvm::EHStreamer::Asm
AsmPrinter * Asm
Target of directive emission.
Definition: EHStreamer.h:33
Compiler.h
LLVM_LIBRARY_VISIBILITY
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library,...
Definition: Compiler.h:131
llvm::EHStreamer::isCleanupEHSelector
static bool isCleanupEHSelector(int Selector)
Definition: EHStreamer.h:146
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:81
llvm::AsmPrinterHandler
Collects and handles AsmPrinter objects required to build debug or EH information.
Definition: AsmPrinterHandler.h:33
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::LandingPadInfo
This structure is used to retain landing pad info for the current function.
Definition: MachineFunction.h:222
llvm::EHStreamer::CallSiteEntry::Action
unsigned Action
Definition: EHStreamer.h:69
llvm::EHStreamer::PadRange
Structure holding a try-range and the associated landing pad.
Definition: EHStreamer.h:43
llvm::EHStreamer::PadRange::PadIndex
unsigned PadIndex
Definition: EHStreamer.h:45
llvm::ISD::MCSymbol
@ MCSymbol
Definition: ISDOpcodes.h:172
llvm::EHStreamer::CallSiteEntry::BeginLabel
MCSymbol * BeginLabel
Definition: EHStreamer.h:63