LLVM  3.7.0
MachineModuleInfo.h
Go to the documentation of this file.
1 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
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 // Collect meta information for a module. This information should be in a
11 // neutral form that can be used by different debugging and exception handling
12 // schemes.
13 //
14 // The organization of information is primarily clustered around the source
15 // compile units. The main exception is source line correspondence where
16 // inlining may interleave code from various compile units.
17 //
18 // The following information can be retrieved from the MachineModuleInfo.
19 //
20 // -- Source directories - Directories are uniqued based on their canonical
21 // string and assigned a sequential numeric ID (base 1.)
22 // -- Source files - Files are also uniqued based on their name and directory
23 // ID. A file ID is sequential number (base 1.)
24 // -- Source line correspondence - A vector of file ID, line#, column# triples.
25 // A DEBUG_LOCATION instruction is generated by the DAG Legalizer
26 // corresponding to each entry in the source line list. This allows a debug
27 // emitter to generate labels referenced by debug information tables.
28 //
29 //===----------------------------------------------------------------------===//
30 
31 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
32 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
33 
34 #include "llvm/ADT/DenseMap.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/ADT/SmallVector.h"
39 #include "llvm/IR/DebugLoc.h"
40 #include "llvm/IR/Metadata.h"
41 #include "llvm/IR/ValueHandle.h"
42 #include "llvm/MC/MCContext.h"
44 #include "llvm/Pass.h"
45 #include "llvm/Support/DataTypes.h"
46 #include "llvm/Support/Dwarf.h"
47 
48 namespace llvm {
49 
50 //===----------------------------------------------------------------------===//
51 // Forward declarations.
52 class Constant;
53 class GlobalVariable;
54 class BlockAddress;
55 class MDNode;
56 class MMIAddrLabelMap;
57 class MachineBasicBlock;
58 class MachineFunction;
59 class Module;
60 class PointerType;
61 class StructType;
62 struct WinEHFuncInfo;
63 
64 struct SEHHandler {
65  // Filter or finally function. Null indicates a catch-all.
67 
68  // Address of block to recover at. Null for a finally handler.
70 };
71 
72 //===----------------------------------------------------------------------===//
73 /// LandingPadInfo - This structure is used to retain landing pad info for
74 /// the current function.
75 ///
77  MachineBasicBlock *LandingPadBlock; // Landing pad block.
78  SmallVector<MCSymbol *, 1> BeginLabels; // Labels prior to invoke.
79  SmallVector<MCSymbol *, 1> EndLabels; // Labels after invoke.
80  SmallVector<SEHHandler, 1> SEHHandlers; // SEH handlers active at this lpad.
81  MCSymbol *LandingPadLabel; // Label at beginning of landing pad.
82  const Function *Personality; // Personality function.
83  std::vector<int> TypeIds; // List of type ids (filters negative).
84  int WinEHState; // WinEH specific state number.
85 
87  : LandingPadBlock(MBB), LandingPadLabel(nullptr), Personality(nullptr),
88  WinEHState(-1) {}
89 };
90 
91 //===----------------------------------------------------------------------===//
92 /// MachineModuleInfoImpl - This class can be derived from and used by targets
93 /// to hold private target-specific information for each Module. Objects of
94 /// type are accessed/created with MMI::getInfo and destroyed when the
95 /// MachineModuleInfo is destroyed.
96 ///
98 public:
100  virtual ~MachineModuleInfoImpl();
101  typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy;
102 protected:
103 
104  /// Return the entries from a DenseMap in a deterministic sorted orer.
105  /// Clears the map.
107 };
108 
109 //===----------------------------------------------------------------------===//
110 /// MachineModuleInfo - This class contains meta information specific to a
111 /// module. Queries can be made by different debugging and exception handling
112 /// schemes and reformated for specific use.
113 ///
115  /// Context - This is the MCContext used for the entire code generator.
116  MCContext Context;
117 
118  /// TheModule - This is the LLVM Module being worked on.
119  const Module *TheModule;
120 
121  /// ObjFileMMI - This is the object-file-format-specific implementation of
122  /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
123  /// want.
124  MachineModuleInfoImpl *ObjFileMMI;
125 
126  /// List of moves done by a function's prolog. Used to construct frame maps
127  /// by debug and exception handling consumers.
128  std::vector<MCCFIInstruction> FrameInstructions;
129 
130  /// LandingPads - List of LandingPadInfo describing the landing pad
131  /// information in the current function.
132  std::vector<LandingPadInfo> LandingPads;
133 
134  /// LPadToCallSiteMap - Map a landing pad's EH symbol to the call site
135  /// indexes.
137 
138  /// CallSiteMap - Map of invoke call site index values to associated begin
139  /// EH_LABEL for the current function.
140  DenseMap<MCSymbol*, unsigned> CallSiteMap;
141 
142  /// CurCallSite - The current call site index being processed, if any. 0 if
143  /// none.
144  unsigned CurCallSite;
145 
146  /// TypeInfos - List of C++ TypeInfo used in the current function.
147  std::vector<const GlobalValue *> TypeInfos;
148 
149  /// FilterIds - List of typeids encoding filters used in the current function.
150  std::vector<unsigned> FilterIds;
151 
152  /// FilterEnds - List of the indices in FilterIds corresponding to filter
153  /// terminators.
154  std::vector<unsigned> FilterEnds;
155 
156  /// Personalities - Vector of all personality functions ever seen. Used to
157  /// emit common EH frames.
158  std::vector<const Function *> Personalities;
159 
160  /// AddrLabelSymbols - This map keeps track of which symbol is being used for
161  /// the specified basic block's address of label.
162  MMIAddrLabelMap *AddrLabelSymbols;
163 
164  bool CallsEHReturn;
165  bool CallsUnwindInit;
166 
167  /// DbgInfoAvailable - True if debugging information is available
168  /// in this module.
169  bool DbgInfoAvailable;
170 
171  /// UsesVAFloatArgument - True if this module calls VarArg function with
172  /// floating-point arguments. This is used to emit an undefined reference
173  /// to _fltused on Windows targets.
174  bool UsesVAFloatArgument;
175 
176  /// UsesMorestackAddr - True if the module calls the __morestack function
177  /// indirectly, as is required under the large code model on x86. This is used
178  /// to emit a definition of a symbol, __morestack_addr, containing the
179  /// address. See comments in lib/Target/X86/X86FrameLowering.cpp for more
180  /// details.
181  bool UsesMorestackAddr;
182 
183  EHPersonality PersonalityTypeCache;
184 
186 
187 public:
188  static char ID; // Pass identification, replacement for typeid
189 
193  unsigned Slot;
194  const DILocation *Loc;
195 
197  unsigned Slot, const DILocation *Loc)
198  : Var(Var), Expr(Expr), Slot(Slot), Loc(Loc) {}
199  };
202 
203  MachineModuleInfo(); // DUMMY CONSTRUCTOR, DO NOT CALL.
204  // Real constructor.
205  MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
206  const MCObjectFileInfo *MOFI);
207  ~MachineModuleInfo() override;
208 
209  // Initialization and Finalization
210  bool doInitialization(Module &) override;
211  bool doFinalization(Module &) override;
212 
213  /// EndFunction - Discard function meta information.
214  ///
215  void EndFunction();
216 
217  const MCContext &getContext() const { return Context; }
218  MCContext &getContext() { return Context; }
219 
220  void setModule(const Module *M) { TheModule = M; }
221  const Module *getModule() const { return TheModule; }
222 
223  const Function *getWinEHParent(const Function *F) const;
225  bool hasWinEHFuncInfo(const Function *F) const {
226  return FuncInfoMap.count(getWinEHParent(F)) > 0;
227  }
228 
229  /// getInfo - Keep track of various per-function pieces of information for
230  /// backends that would like to do so.
231  ///
232  template<typename Ty>
234  if (ObjFileMMI == nullptr)
235  ObjFileMMI = new Ty(*this);
236  return *static_cast<Ty*>(ObjFileMMI);
237  }
238 
239  template<typename Ty>
240  const Ty &getObjFileInfo() const {
241  return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
242  }
243 
244  /// hasDebugInfo - Returns true if valid debug info is present.
245  ///
246  bool hasDebugInfo() const { return DbgInfoAvailable; }
247  void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
248 
249  bool callsEHReturn() const { return CallsEHReturn; }
250  void setCallsEHReturn(bool b) { CallsEHReturn = b; }
251 
252  bool callsUnwindInit() const { return CallsUnwindInit; }
253  void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
254 
255  bool usesVAFloatArgument() const {
256  return UsesVAFloatArgument;
257  }
258 
259  void setUsesVAFloatArgument(bool b) {
260  UsesVAFloatArgument = b;
261  }
262 
263  bool usesMorestackAddr() const {
264  return UsesMorestackAddr;
265  }
266 
267  void setUsesMorestackAddr(bool b) {
268  UsesMorestackAddr = b;
269  }
270 
271  /// \brief Returns a reference to a list of cfi instructions in the current
272  /// function's prologue. Used to construct frame maps for debug and exception
273  /// handling comsumers.
274  const std::vector<MCCFIInstruction> &getFrameInstructions() const {
275  return FrameInstructions;
276  }
277 
280  FrameInstructions.push_back(Inst);
281  return FrameInstructions.size() - 1;
282  }
283 
284  /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
285  /// block when its address is taken. This cannot be its normal LBB label
286  /// because the block may be accessed outside its containing function.
288  return getAddrLabelSymbolToEmit(BB).front();
289  }
290 
291  /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
292  /// basic block when its address is taken. If other blocks were RAUW'd to
293  /// this one, we may have to emit them as well, return the whole set.
295 
296  /// takeDeletedSymbolsForFunction - If the specified function has had any
297  /// references to address-taken blocks generated, but the block got deleted,
298  /// return the symbol now so we can emit it. This prevents emitting a
299  /// reference to a symbol that has no definition.
301  std::vector<MCSymbol*> &Result);
302 
303 
304  //===- EH ---------------------------------------------------------------===//
305 
306  /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
307  /// specified MachineBasicBlock.
309 
310  /// addInvoke - Provide the begin and end labels of an invoke style call and
311  /// associate it with a try landing pad block.
312  void addInvoke(MachineBasicBlock *LandingPad,
313  MCSymbol *BeginLabel, MCSymbol *EndLabel);
314 
315  /// addLandingPad - Add a new panding pad. Returns the label ID for the
316  /// landing pad entry.
318 
319  /// addPersonality - Provide the personality function for the exception
320  /// information.
321  void addPersonality(MachineBasicBlock *LandingPad,
322  const Function *Personality);
323  void addPersonality(const Function *Personality);
324 
325  void addWinEHState(MachineBasicBlock *LandingPad, int State);
326 
327  /// getPersonalityIndex - Get index of the current personality function inside
328  /// Personalitites array
329  unsigned getPersonalityIndex() const;
330 
331  /// getPersonalities - Return array of personality functions ever seen.
332  const std::vector<const Function *>& getPersonalities() const {
333  return Personalities;
334  }
335 
336  /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
337  ///
338  void addCatchTypeInfo(MachineBasicBlock *LandingPad,
340 
341  /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
342  ///
343  void addFilterTypeInfo(MachineBasicBlock *LandingPad,
345 
346  /// addCleanup - Add a cleanup action for a landing pad.
347  ///
348  void addCleanup(MachineBasicBlock *LandingPad);
349 
350  void addSEHCatchHandler(MachineBasicBlock *LandingPad, const Function *Filter,
351  const BlockAddress *RecoverLabel);
352 
353  void addSEHCleanupHandler(MachineBasicBlock *LandingPad,
354  const Function *Cleanup);
355 
356  /// getTypeIDFor - Return the type id for the specified typeinfo. This is
357  /// function wide.
358  unsigned getTypeIDFor(const GlobalValue *TI);
359 
360  /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is
361  /// function wide.
362  int getFilterIDFor(std::vector<unsigned> &TyIds);
363 
364  /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
365  /// pads.
366  void TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap = nullptr);
367 
368  /// getLandingPads - Return a reference to the landing pad info for the
369  /// current function.
370  const std::vector<LandingPadInfo> &getLandingPads() const {
371  return LandingPads;
372  }
373 
374  /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call
375  /// site indexes.
377 
378  /// getCallSiteLandingPad - Get the call site indexes for a landing pad EH
379  /// symbol.
381  assert(hasCallSiteLandingPad(Sym) &&
382  "missing call site number for landing pad!");
383  return LPadToCallSiteMap[Sym];
384  }
385 
386  /// hasCallSiteLandingPad - Return true if the landing pad Eh symbol has an
387  /// associated call site.
389  return !LPadToCallSiteMap[Sym].empty();
390  }
391 
392  /// setCallSiteBeginLabel - Map the begin label for a call site.
393  void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
394  CallSiteMap[BeginLabel] = Site;
395  }
396 
397  /// getCallSiteBeginLabel - Get the call site number for a begin label.
398  unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) {
399  assert(hasCallSiteBeginLabel(BeginLabel) &&
400  "Missing call site number for EH_LABEL!");
401  return CallSiteMap[BeginLabel];
402  }
403 
404  /// hasCallSiteBeginLabel - Return true if the begin label has a call site
405  /// number associated with it.
406  bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) {
407  return CallSiteMap[BeginLabel] != 0;
408  }
409 
410  /// setCurrentCallSite - Set the call site currently being processed.
411  void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
412 
413  /// getCurrentCallSite - Get the call site currently being processed, if any.
414  /// return zero if none.
415  unsigned getCurrentCallSite() { return CurCallSite; }
416 
417  /// getTypeInfos - Return a reference to the C++ typeinfo for the current
418  /// function.
419  const std::vector<const GlobalValue *> &getTypeInfos() const {
420  return TypeInfos;
421  }
422 
423  /// getFilterIds - Return a reference to the typeids encoding filters used in
424  /// the current function.
425  const std::vector<unsigned> &getFilterIds() const {
426  return FilterIds;
427  }
428 
429  /// getPersonality - Return a personality function if available. The presence
430  /// of one is required to emit exception handling info.
431  const Function *getPersonality() const;
432 
433  /// Classify the personality function amongst known EH styles.
435 
436  /// setVariableDbgInfo - Collect information used to emit debugging
437  /// information of a variable.
438  void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
439  unsigned Slot, const DILocation *Loc) {
440  VariableDbgInfos.emplace_back(Var, Expr, Slot, Loc);
441  }
442 
444 
445 }; // End class MachineModuleInfo
446 
447 } // End llvm namespace
448 
449 #endif
const Ty & getObjFileInfo() const
PointerIntPair< MCSymbol *, 1, bool > StubValueTy
unsigned getTypeIDFor(const GlobalValue *TI)
getTypeIDFor - Return the type id for the specified typeinfo.
bool doFinalization(Module &) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
#define LLVM_ATTRIBUTE_UNUSED_RESULT
Definition: Compiler.h:128
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
WinEHFuncInfo & getWinEHFuncInfo(const Function *F)
void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef< unsigned > Sites)
setCallSiteLandingPad - Map the landing pad's EH symbol to the call site indexes. ...
SmallVector< VariableDbgInfo, 4 > VariableDbgInfoMapTy
const Function * FilterOrFinally
const Function * Personality
This file contains the declarations for metadata subclasses.
void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr, unsigned Slot, const DILocation *Loc)
setVariableDbgInfo - Collect information used to emit debugging information of a variable.
unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel)
getCallSiteBeginLabel - Get the call site number for a begin label.
F(f)
bool hasWinEHFuncInfo(const Function *F) const
std::vector< std::pair< MCSymbol *, StubValueTy > > SymbolListTy
VariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr, unsigned Slot, const DILocation *Loc)
void setCurrentCallSite(unsigned Site)
setCurrentCallSite - Set the call site currently being processed.
BlockAddress - The address of a basic block.
Definition: Constants.h:802
void takeDeletedSymbolsForFunction(const Function *F, std::vector< MCSymbol * > &Result)
takeDeletedSymbolsForFunction - If the specified function has had any references to address-taken blo...
SmallVector< MCSymbol *, 1 > EndLabels
const std::vector< unsigned > & getFilterIds() const
getFilterIds - Return a reference to the typeids encoding filters used in the current function...
SmallVector< SEHHandler, 1 > SEHHandlers
const Module * getModule() const
bool hasDebugInfo() const
hasDebugInfo - Returns true if valid debug info is present.
bool usesVAFloatArgument() const
Context object for machine code objects.
Definition: MCContext.h:48
LandingPadInfo - This structure is used to retain landing pad info for the current function...
const std::vector< const GlobalValue * > & getTypeInfos() const
getTypeInfos - Return a reference to the C++ typeinfo for the current function.
void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site)
setCallSiteBeginLabel - Map the begin label for a call site.
bool hasCallSiteLandingPad(MCSymbol *Sym)
hasCallSiteLandingPad - Return true if the landing pad Eh symbol has an associated call site...
unsigned LLVM_ATTRIBUTE_UNUSED_RESULT addFrameInst(const MCCFIInstruction &Inst)
LandingPadInfo & getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad)
getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the specified MachineBasicBlock.
unsigned getCurrentCallSite()
getCurrentCallSite - Get the call site currently being processed, if any.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
const std::vector< MCCFIInstruction > & getFrameInstructions() const
Returns a reference to a list of cfi instructions in the current function's prologue.
void addPersonality(MachineBasicBlock *LandingPad, const Function *Personality)
addPersonality - Provide the personality function for the exception information.
ArrayRef< MCSymbol * > getAddrLabelSymbolToEmit(const BasicBlock *BB)
getAddrLabelSymbolToEmit - Return the symbol to be used for the specified basic block when its addres...
Debug location.
void addFilterTypeInfo(MachineBasicBlock *LandingPad, ArrayRef< const GlobalValue * > TyInfo)
addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:58
bool hasCallSiteBeginLabel(MCSymbol *BeginLabel)
hasCallSiteBeginLabel - Return true if the begin label has a call site number associated with it...
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
PointerIntPair - This class implements a pair of a pointer and small integer.
VariableDbgInfoMapTy & getVariableDbgInfo()
void addCatchTypeInfo(MachineBasicBlock *LandingPad, ArrayRef< const GlobalValue * > TyInfo)
addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
SmallVector< MCSymbol *, 1 > BeginLabels
MCSymbol * getAddrLabelSymbol(const BasicBlock *BB)
getAddrLabelSymbol - Return the symbol to be used for the specified basic block when its address is t...
EHPersonality getPersonalityType()
Classify the personality function amongst known EH styles.
const std::vector< const Function * > & getPersonalities() const
getPersonalities - Return array of personality functions ever seen.
std::vector< int > TypeIds
VariableDbgInfoMapTy VariableDbgInfos
MCSymbol * addLandingPad(MachineBasicBlock *LandingPad)
addLandingPad - Add a new panding pad.
void addInvoke(MachineBasicBlock *LandingPad, MCSymbol *BeginLabel, MCSymbol *EndLabel)
addInvoke - Provide the begin and end labels of an invoke style call and associate it with a try land...
Ty & getObjFileInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
SmallVectorImpl< unsigned > & getCallSiteLandingPad(MCSymbol *Sym)
getCallSiteLandingPad - Get the call site indexes for a landing pad EH symbol.
void addSEHCatchHandler(MachineBasicBlock *LandingPad, const Function *Filter, const BlockAddress *RecoverLabel)
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:262
void setDebugInfoAvailability(bool avail)
const MCContext & getContext() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
void addSEHCleanupHandler(MachineBasicBlock *LandingPad, const Function *Cleanup)
void addCleanup(MachineBasicBlock *LandingPad)
addCleanup - Add a cleanup action for a landing pad.
DWARF expression.
void addWinEHState(MachineBasicBlock *LandingPad, int State)
MachineBasicBlock * LandingPadBlock
bool doInitialization(Module &) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
const Function * getWinEHParent(const Function *F) const
void emplace_back(ArgTypes &&...Args)
Definition: SmallVector.h:652
const std::vector< LandingPadInfo > & getLandingPads() const
getLandingPads - Return a reference to the landing pad info for the current function.
LandingPadInfo(MachineBasicBlock *MBB)
void setModule(const Module *M)
int getFilterIDFor(std::vector< unsigned > &TyIds)
getFilterIDFor - Return the id of the filter encoded by TyIds.
void setUsesVAFloatArgument(bool b)
unsigned getPersonalityIndex() const
getPersonalityIndex - Get index of the current personality function inside Personalitites array ...
void TidyLandingPads(DenseMap< MCSymbol *, uintptr_t > *LPMap=nullptr)
TidyLandingPads - Remap landing pad labels and remove any deleted landing pads.
const BlockAddress * RecoverBA
void EndFunction()
EndFunction - Discard function meta information.
MachineModuleInfoImpl - This class can be derived from and used by targets to hold private target-spe...
static SymbolListTy getSortedStubs(DenseMap< MCSymbol *, StubValueTy > &)
Return the entries from a DenseMap in a deterministic sorted orer.
MachineModuleInfo - This class contains meta information specific to a module.
const Function * getPersonality() const
getPersonality - Return a personality function if available.