LLVM  6.0.0svn
DwarfCompileUnit.h
Go to the documentation of this file.
1 //===- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit -----*- 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 // This file contains support for writing dwarf compile unit.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
16 
18 #include "DwarfDebug.h"
19 #include "DwarfUnit.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringRef.h"
26 #include "llvm/CodeGen/DIE.h"
29 #include "llvm/Support/Casting.h"
30 #include <algorithm>
31 #include <cassert>
32 #include <cstdint>
33 #include <memory>
34 
35 namespace llvm {
36 
37 class AsmPrinter;
38 class DwarfFile;
39 class GlobalVariable;
40 class MCExpr;
41 class MCSymbol;
42 class MDNode;
43 
44 class DwarfCompileUnit final : public DwarfUnit {
45  /// A numeric ID unique among all CUs in the module
46  unsigned UniqueID;
47 
48  /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
49  /// the need to search for it in applyStmtList.
50  DIE::value_iterator StmtListValue;
51 
52  /// Skeleton unit associated with this unit.
53  DwarfCompileUnit *Skeleton = nullptr;
54 
55  /// The start of the unit within its section.
56  MCSymbol *LabelBegin;
57 
58  /// The start of the unit macro info within macro section.
59  MCSymbol *MacroLabelBegin;
60 
63 
64  ImportedEntityMap ImportedEntities;
65 
66  /// GlobalNames - A map of globally visible named entities for this unit.
67  StringMap<const DIE *> GlobalNames;
68 
69  /// GlobalTypes - A map of globally visible types for this unit.
70  StringMap<const DIE *> GlobalTypes;
71 
72  // List of range lists for a given compile unit, separate from the ranges for
73  // the CU itself.
74  SmallVector<RangeSpanList, 1> CURangeLists;
75 
76  // List of ranges for a given compile unit.
78 
79  // The base address of this unit, if any. Used for relative references in
80  // ranges/locs.
81  const MCSymbol *BaseAddress = nullptr;
82 
83  DenseMap<const MDNode *, DIE *> AbstractSPDies;
85 
86  /// \brief Construct a DIE for the given DbgVariable without initializing the
87  /// DbgVariable's DIE reference.
88  DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
89 
90  bool isDwoUnit() const override;
91 
92  DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
93  if (isDwoUnit() && !DD->shareAcrossDWOCUs())
94  return AbstractSPDies;
95  return DU->getAbstractSPDies();
96  }
97 
99  if (isDwoUnit() && !DD->shareAcrossDWOCUs())
100  return AbstractVariables;
101  return DU->getAbstractVariables();
102  }
103 
104 public:
105  DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
106  DwarfDebug *DW, DwarfFile *DWU);
107 
108  unsigned getUniqueID() const { return UniqueID; }
109 
111  return Skeleton;
112  }
113 
114  bool includeMinimalInlineScopes() const;
115 
116  void initStmtList();
117 
118  /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
119  void applyStmtList(DIE &D);
120 
121  /// A pair of GlobalVariable and DIExpression.
122  struct GlobalExpr {
125  };
126 
127  /// Get or create global variable DIE.
128  DIE *
130  ArrayRef<GlobalExpr> GlobalExprs);
131 
132  /// addLabelAddress - Add a dwarf label attribute data and value using
133  /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
135  const MCSymbol *Label);
136 
137  /// addLocalLabelAddress - Add a dwarf label attribute data and value using
138  /// DW_FORM_addr only.
140  const MCSymbol *Label);
141 
142  DwarfCompileUnit &getCU() override { return *this; }
143 
144  unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
145 
147  DIScope *Scope = IE->getScope();
148  assert(Scope && "Invalid Scope encoding!");
149  if (!isa<DILocalScope>(Scope))
150  // No need to add imported enities that are not local declaration.
151  return;
152 
153  auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
154  ImportedEntities[LocalScope].push_back(IE);
155  }
156 
157  /// addRange - Add an address range to the list of ranges for this unit.
158  void addRange(RangeSpan Range);
159 
160  void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
161 
162  /// \brief Find DIE for the given subprogram and attach appropriate
163  /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
164  /// variables in this scope then create and insert DIEs for these
165  /// variables.
167 
168  void constructScopeDIE(LexicalScope *Scope,
169  SmallVectorImpl<DIE *> &FinalChildren);
170 
171  /// \brief A helper function to construct a RangeSpanList for a given
172  /// lexical scope.
173  void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
174 
176 
178  const SmallVectorImpl<InsnRange> &Ranges);
179 
180  /// \brief This scope represents inlined body of a function. Construct
181  /// DIE to represent this concrete inlined copy of the function.
183 
184  /// \brief Construct new DW_TAG_lexical_block for this scope and
185  /// attach DW_AT_low_pc/DW_AT_high_pc labels.
187 
188  /// constructVariableDIE - Construct a DIE for the given DbgVariable.
189  DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
190 
192  DIE *&ObjectPointer);
193 
194  /// A helper function to create children of a Scope DIE.
196  SmallVectorImpl<DIE *> &Children,
197  bool *HasNonScopeChildren = nullptr);
198 
199  /// \brief Construct a DIE for this subprogram scope.
200  void constructSubprogramScopeDIE(const DISubprogram *Sub, LexicalScope *Scope);
201 
202  DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
203 
205 
206  /// \brief Construct import_module DIE.
208 
211 
212  /// Find abstract variable associated with Var.
215  const DILocalVariable *&Cleansed);
217  void createAbstractVariable(const DILocalVariable *DV, LexicalScope *Scope);
218 
219  /// Set the skeleton unit associated with this unit.
220  void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
221 
222  unsigned getLength() {
223  return sizeof(uint32_t) + // Length field
225  }
226 
227  void emitHeader(bool UseOffsets) override;
228 
230  assert(getSection());
231  return LabelBegin;
232  }
233 
235  return MacroLabelBegin;
236  }
237 
238  /// Add a new global name to the compile unit.
239  void addGlobalName(StringRef Name, const DIE &Die,
240  const DIScope *Context) override;
241 
242  /// Add a new global name present in a type unit to this compile unit.
243  void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
244 
245  /// Add a new global type to the compile unit.
246  void addGlobalType(const DIType *Ty, const DIE &Die,
247  const DIScope *Context) override;
248 
249  /// Add a new global type present in a type unit to this compile unit.
250  void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
251 
252  const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
253  const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
254 
255  /// Add DW_AT_location attribute for a DbgVariable based on provided
256  /// MachineLocation.
257  void addVariableAddress(const DbgVariable &DV, DIE &Die,
258  MachineLocation Location);
259  /// Add an address attribute to a die based on the location provided.
261  const MachineLocation &Location);
262 
263  /// Start with the address based on the location provided, and generate the
264  /// DWARF information necessary to find the actual variable (navigating the
265  /// extra location information encoded in the type) based on the starting
266  /// location. Add the DWARF information to the die.
267  void addComplexAddress(const DbgVariable &DV, DIE &Die,
268  dwarf::Attribute Attribute,
269  const MachineLocation &Location);
270 
271  /// Add a Dwarf loclistptr attribute data and value.
272  void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
273  void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
274 
275  /// Add a Dwarf expression attribute data and value.
276  void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
277 
279  DIE &SPDie);
280 
281  /// getRangeLists - Get the vector of range lists.
283  return (Skeleton ? Skeleton : this)->CURangeLists;
284  }
285 
286  /// getRanges - Get the list of ranges for this unit.
287  const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
288  SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
289 
290  void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
291  const MCSymbol *getBaseAddress() const { return BaseAddress; }
292 
293  bool hasDwarfPubSections() const;
294 };
295 
296 } // end namespace llvm
297 
298 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
const StringMap< const DIE * > & getGlobalTypes() const
unsigned getSize() const
Definition: DIE.h:701
const SmallVectorImpl< RangeSpanList > & getRangeLists() const
getRangeLists - Get the vector of range lists.
LLVMContext & Context
virtual unsigned getHeaderSize() const
Compute the size of a header for this unit, not including the initial length field.
Definition: DwarfUnit.h:279
const MCSymbol * getBaseAddress() const
DIELoc - Represents an expression location.
Definition: DIE.h:846
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
std::pair< const DILocalVariable *, const DILocation * > InlinedVariable
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
void addGlobalName(StringRef Name, const DIE &Die, const DIScope *Context) override
Add a new global name to the compile unit.
Attribute
Attributes.
Definition: Dwarf.h:107
Collects and handles dwarf debug information.
Definition: DwarfDebug.h:196
DbgValueHistoryMap::InlinedVariable InlinedVariable
Find abstract variable associated with Var.
void createAbstractVariable(const DILocalVariable *DV, LexicalScope *Scope)
void addRange(RangeSpan Range)
addRange - Add an address range to the list of ranges for this unit.
void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End)
DwarfCompileUnit & getCU() override
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:45
void attachRangesOrLowHighPC(DIE &D, SmallVector< RangeSpan, 2 > Ranges)
DIScope * getScope() const
void addComplexAddress(const DbgVariable &DV, DIE &Die, dwarf::Attribute Attribute, const MachineLocation &Location)
Start with the address based on the location provided, and generate the DWARF information necessary t...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
void emitHeader(bool UseOffsets) override
Emit the header for this unit, not including the initial length field.
void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context)
Add a new global type present in a type unit to this compile unit.
DIE * constructInlinedScopeDIE(LexicalScope *Scope)
This scope represents inlined body of a function.
Subprogram description.
unsigned getUniqueID() const
DenseMap< const MDNode *, DIE * > & getAbstractSPDies()
Definition: DwarfFile.h:101
This class is used to track local variable information.
Definition: DwarfDebug.h:76
void setBaseAddress(const MCSymbol *Base)
DIE * constructVariableDIE(DbgVariable &DV, bool Abstract=false)
constructVariableDIE - Construct a DIE for the given DbgVariable.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context)
Add a new global name present in a type unit to this compile unit.
void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr)
Add a Dwarf expression attribute data and value.
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:68
void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index)
Add a Dwarf loclistptr attribute data and value.
void constructSubprogramScopeDIE(const DISubprogram *Sub, LexicalScope *Scope)
Construct a DIE for this subprogram scope.
A structured debug information entry.
Definition: DIE.h:662
DIE & getUnitDie()
Definition: DIE.h:834
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:77
DIE * createScopeChildrenDIE(LexicalScope *Scope, SmallVectorImpl< DIE *> &Children, bool *HasNonScopeChildren=nullptr)
A helper function to create children of a Scope DIE.
DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
static const unsigned End
void addAddress(DIE &Die, dwarf::Attribute Attribute, const MachineLocation &Location)
Add an address attribute to a die based on the location provided.
DIE * createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE)
void addLabelAddress(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Label)
addLabelAddress - Add a dwarf label attribute data and value using either DW_FORM_addr or DW_FORM_GNU...
A pair of GlobalVariable and DIExpression.
void addScopeRangeList(DIE &ScopeDIE, SmallVector< RangeSpan, 2 > Range)
A helper function to construct a RangeSpanList for a given lexical scope.
An imported module (C++ using directive or similar).
void setSkeleton(DwarfCompileUnit &Skel)
Set the skeleton unit associated with this unit.
Base class for scope-like contexts.
DIE * constructImportedEntityDIE(const DIImportedEntity *Module)
Construct import_module DIE.
DenseMap< const MDNode *, std::unique_ptr< DbgVariable > > & getAbstractVariables()
Definition: DwarfFile.h:105
SmallVector< RangeSpan, 2 > takeRanges()
void constructAbstractSubprogramScopeDIE(LexicalScope *Scope)
MCSymbol * getMacroLabelBegin() const
const StringMap< const DIE * > & getGlobalNames() const
Base class for types.
Represents base address of the CU.
Definition: DWARFUnit.h:163
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition: DIE.h:827
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
void applySubprogramAttributesToDefinition(const DISubprogram *SP, DIE &SPDie)
void finishSubprogramDefinition(const DISubprogram *SP)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
DWARF expression.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:224
This file contains constants used for implementing Dwarf debug support.
DwarfDebug * DD
Definition: DwarfUnit.h:80
const SmallVectorImpl< RangeSpan > & getRanges() const
getRanges - Get the list of ranges for this unit.
void addImportedEntity(const DIImportedEntity *IE)
unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override
Look up the source ID with the given directory and source file names.
DIE * getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV, ArrayRef< GlobalExpr > GlobalExprs)
Get or create global variable DIE.
void addVariableAddress(const DbgVariable &DV, DIE &Die, MachineLocation Location)
Add DW_AT_location attribute for a DbgVariable based on provided MachineLocation. ...
void addGlobalType(const DIType *Ty, const DIE &Die, const DIScope *Context) override
Add a new global type to the compile unit.
void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie)
bool shareAcrossDWOCUs() const
Definition: DwarfDebug.cpp:410
bool includeMinimalInlineScopes() const
DwarfCompileUnit * getSkeleton() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void finishVariableDefinition(const DbgVariable &Var)
void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Label)
addLocalLabelAddress - Add a dwarf label attribute data and value using DW_FORM_addr only...
DbgVariable * getExistingAbstractVariable(InlinedVariable IV, const DILocalVariable *&Cleansed)
DIE & updateSubprogramScopeDIE(const DISubprogram *SP)
Find DIE for the given subprogram and attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes...
void constructScopeDIE(LexicalScope *Scope, SmallVectorImpl< DIE *> &FinalChildren)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
DwarfFile * DU
Definition: DwarfUnit.h:81
MCSymbol * getLabelBegin() const
void applyStmtList(DIE &D)
Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
DIE * constructLexicalScopeDIE(LexicalScope *Scope)
Construct new DW_TAG_lexical_block for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels...