LLVM  7.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  /// DWO ID for correlating skeleton and split units.
87  uint64_t DWOId = 0;
88 
89  /// Construct a DIE for the given DbgVariable without initializing the
90  /// DbgVariable's DIE reference.
91  DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
92 
93  bool isDwoUnit() const override;
94 
95  DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
96  if (isDwoUnit() && !DD->shareAcrossDWOCUs())
97  return AbstractSPDies;
98  return DU->getAbstractSPDies();
99  }
100 
102  if (isDwoUnit() && !DD->shareAcrossDWOCUs())
103  return AbstractVariables;
104  return DU->getAbstractVariables();
105  }
106 
107 public:
108  DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
109  DwarfDebug *DW, DwarfFile *DWU);
110 
111  unsigned getUniqueID() const { return UniqueID; }
112 
114  return Skeleton;
115  }
116 
117  bool includeMinimalInlineScopes() const;
118 
119  void initStmtList();
120 
121  /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
122  void applyStmtList(DIE &D);
123 
124  /// A pair of GlobalVariable and DIExpression.
125  struct GlobalExpr {
128  };
129 
130  /// Get or create global variable DIE.
131  DIE *
133  ArrayRef<GlobalExpr> GlobalExprs);
134 
135  /// addLabelAddress - Add a dwarf label attribute data and value using
136  /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
138  const MCSymbol *Label);
139 
140  /// addLocalLabelAddress - Add a dwarf label attribute data and value using
141  /// DW_FORM_addr only.
143  const MCSymbol *Label);
144 
145  DwarfCompileUnit &getCU() override { return *this; }
146 
147  unsigned getOrCreateSourceID(const DIFile *File) override;
148 
150  DIScope *Scope = IE->getScope();
151  assert(Scope && "Invalid Scope encoding!");
152  if (!isa<DILocalScope>(Scope))
153  // No need to add imported enities that are not local declaration.
154  return;
155 
156  auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
157  ImportedEntities[LocalScope].push_back(IE);
158  }
159 
160  /// addRange - Add an address range to the list of ranges for this unit.
161  void addRange(RangeSpan Range);
162 
163  void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
164 
165  /// Find DIE for the given subprogram and attach appropriate
166  /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
167  /// variables in this scope then create and insert DIEs for these
168  /// variables.
170 
171  void constructScopeDIE(LexicalScope *Scope,
172  SmallVectorImpl<DIE *> &FinalChildren);
173 
174  /// A helper function to construct a RangeSpanList for a given
175  /// lexical scope.
176  void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
177 
179 
181  const SmallVectorImpl<InsnRange> &Ranges);
182 
183  /// This scope represents inlined body of a function. Construct
184  /// DIE to represent this concrete inlined copy of the function.
186 
187  /// Construct new DW_TAG_lexical_block for this scope and
188  /// attach DW_AT_low_pc/DW_AT_high_pc labels.
190 
191  /// constructVariableDIE - Construct a DIE for the given DbgVariable.
192  DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
193 
195  DIE *&ObjectPointer);
196 
197  /// A helper function to create children of a Scope DIE.
199  SmallVectorImpl<DIE *> &Children,
200  bool *HasNonScopeChildren = nullptr);
201 
202  /// Construct a DIE for this subprogram scope.
203  void constructSubprogramScopeDIE(const DISubprogram *Sub, LexicalScope *Scope);
204 
205  DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
206 
208 
209  /// Construct import_module DIE.
211 
214 
215  /// Find abstract variable associated with Var.
218  const DILocalVariable *&Cleansed);
220  void createAbstractVariable(const DILocalVariable *Var, LexicalScope *Scope);
221 
222  /// Set the skeleton unit associated with this unit.
223  void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
224 
225  unsigned getHeaderSize() const override {
226  // DWARF v5 added the DWO ID to the header for split/skeleton units.
227  unsigned DWOIdSize =
228  DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
229  : 0;
230  return DwarfUnit::getHeaderSize() + DWOIdSize;
231  }
232  unsigned getLength() {
233  return sizeof(uint32_t) + // Length field
235  }
236 
237  void emitHeader(bool UseOffsets) override;
238 
240  assert(getSection());
241  return LabelBegin;
242  }
243 
245  return MacroLabelBegin;
246  }
247 
248  /// Add a new global name to the compile unit.
249  void addGlobalName(StringRef Name, const DIE &Die,
250  const DIScope *Context) override;
251 
252  /// Add a new global name present in a type unit to this compile unit.
253  void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
254 
255  /// Add a new global type to the compile unit.
256  void addGlobalType(const DIType *Ty, const DIE &Die,
257  const DIScope *Context) override;
258 
259  /// Add a new global type present in a type unit to this compile unit.
260  void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
261 
262  const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
263  const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
264 
265  /// Add DW_AT_location attribute for a DbgVariable based on provided
266  /// MachineLocation.
267  void addVariableAddress(const DbgVariable &DV, DIE &Die,
268  MachineLocation Location);
269  /// Add an address attribute to a die based on the location provided.
271  const MachineLocation &Location);
272 
273  /// Start with the address based on the location provided, and generate the
274  /// DWARF information necessary to find the actual variable (navigating the
275  /// extra location information encoded in the type) based on the starting
276  /// location. Add the DWARF information to the die.
277  void addComplexAddress(const DbgVariable &DV, DIE &Die,
278  dwarf::Attribute Attribute,
279  const MachineLocation &Location);
280 
281  /// Add a Dwarf loclistptr attribute data and value.
282  void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
283  void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
284 
285  /// Add a Dwarf expression attribute data and value.
286  void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
287 
289  DIE &SPDie);
290 
291  /// getRangeLists - Get the vector of range lists.
293  return (Skeleton ? Skeleton : this)->CURangeLists;
294  }
295 
296  /// getRanges - Get the list of ranges for this unit.
297  const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
298  SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
299 
300  void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
301  const MCSymbol *getBaseAddress() const { return BaseAddress; }
302 
303  uint64_t getDWOId() const { return DWOId; }
304  void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
305 
306  bool hasDwarfPubSections() const;
307 };
308 
309 } // end namespace llvm
310 
311 #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:284
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:114
Collects and handles dwarf debug information.
Definition: DwarfDebug.h:204
DbgValueHistoryMap::InlinedVariable InlinedVariable
Find abstract variable associated with Var.
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
bool useSplitDwarf() const
Returns whether or not to change the current debug info for the split dwarf proposal support...
Definition: DwarfDebug.h:566
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.
void setDWOId(uint64_t DwoId)
DIE * constructInlinedScopeDIE(LexicalScope *Scope)
This scope represents inlined body of a function.
Subprogram description.
unsigned getUniqueID() const
uint64_t getDWOId() const
DenseMap< const MDNode *, DIE * > & getAbstractSPDies()
Definition: DwarfFile.h:132
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:78
unsigned getOrCreateSourceID(const DIFile *File) override
Look up the source ID for the given file.
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)
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)
unsigned getHeaderSize() const override
Compute the size of a header for this unit, not including the initial length field.
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:136
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:226
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:841
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:220
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)
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:475
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)
uint16_t getDwarfVersion() const
Returns the Dwarf Version.
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
void createAbstractVariable(const DILocalVariable *Var, LexicalScope *Scope)
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...