LLVM  9.0.0svn
DwarfCompileUnit.h
Go to the documentation of this file.
1 //===- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit -----*- 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 dwarf compile unit.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
14 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15 
16 #include "DwarfDebug.h"
17 #include "DwarfUnit.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
25 #include "llvm/CodeGen/DIE.h"
28 #include "llvm/Support/Casting.h"
29 #include <algorithm>
30 #include <cassert>
31 #include <cstdint>
32 #include <memory>
33 
34 namespace llvm {
35 
36 class AsmPrinter;
37 class DwarfFile;
38 class GlobalVariable;
39 class MCExpr;
40 class MCSymbol;
41 class MDNode;
42 
43 class DwarfCompileUnit final : public DwarfUnit {
44  /// A numeric ID unique among all CUs in the module
45  unsigned UniqueID;
46  bool HasRangeLists = false;
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 ranges for a given compile unit.
74 
75  // The base address of this unit, if any. Used for relative references in
76  // ranges/locs.
77  const MCSymbol *BaseAddress = nullptr;
78 
79  DenseMap<const MDNode *, DIE *> AbstractSPDies;
81 
82  /// DWO ID for correlating skeleton and split units.
83  uint64_t DWOId = 0;
84 
85  /// Construct a DIE for the given DbgVariable without initializing the
86  /// DbgVariable's DIE reference.
87  DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
88 
89  bool isDwoUnit() const override;
90 
91  DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
92  if (isDwoUnit() && !DD->shareAcrossDWOCUs())
93  return AbstractSPDies;
94  return DU->getAbstractSPDies();
95  }
96 
98  if (isDwoUnit() && !DD->shareAcrossDWOCUs())
99  return AbstractEntities;
100  return DU->getAbstractEntities();
101  }
102 
103 public:
104  DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
105  DwarfDebug *DW, DwarfFile *DWU);
106 
107  bool hasRangeLists() const { return HasRangeLists; }
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(const DIFile *File) 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  /// 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  /// 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  /// This scope represents inlined body of a function. Construct
181  /// DIE to represent this concrete inlined copy of the function.
183 
184  /// 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  /// Construct a DIE for the given DbgLabel.
195  DIE *constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope);
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.
204  LexicalScope *Scope);
205 
206  DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
207 
209 
210  /// Construct a call site entry DIE describing a call within \p Scope to a
211  /// callee described by \p CalleeSP. \p IsTail specifies whether the call is
212  /// a tail call. \p PCOffset must be non-zero for non-tail calls or be the
213  /// function-local offset to PC value after the call instruction.
214  DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram &CalleeSP,
215  bool IsTail, const MCExpr *PCOffset);
216 
217  /// Construct import_module DIE.
219 
221  void finishEntityDefinition(const DbgEntity *Entity);
222 
223  /// Find abstract variable associated with Var.
226  void createAbstractEntity(const DINode *Node, LexicalScope *Scope);
227 
228  /// Set the skeleton unit associated with this unit.
229  void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
230 
231  unsigned getHeaderSize() const override {
232  // DWARF v5 added the DWO ID to the header for split/skeleton units.
233  unsigned DWOIdSize =
234  DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
235  : 0;
236  return DwarfUnit::getHeaderSize() + DWOIdSize;
237  }
238  unsigned getLength() {
239  return sizeof(uint32_t) + // Length field
241  }
242 
243  void emitHeader(bool UseOffsets) override;
244 
245  /// Add the DW_AT_addr_base attribute to the unit DIE.
246  void addAddrTableBase();
247 
249  assert(getSection());
250  return LabelBegin;
251  }
252 
254  return MacroLabelBegin;
255  }
256 
257  /// Add a new global name to the compile unit.
258  void addGlobalName(StringRef Name, const DIE &Die,
259  const DIScope *Context) override;
260 
261  /// Add a new global name present in a type unit to this compile unit.
262  void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
263 
264  /// Add a new global type to the compile unit.
265  void addGlobalType(const DIType *Ty, const DIE &Die,
266  const DIScope *Context) override;
267 
268  /// Add a new global type present in a type unit to this compile unit.
269  void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
270 
271  const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
272  const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
273 
274  /// Add DW_AT_location attribute for a DbgVariable based on provided
275  /// MachineLocation.
276  void addVariableAddress(const DbgVariable &DV, DIE &Die,
277  MachineLocation Location);
278  /// Add an address attribute to a die based on the location provided.
280  const MachineLocation &Location);
281 
282  /// Start with the address based on the location provided, and generate the
283  /// DWARF information necessary to find the actual variable (navigating the
284  /// extra location information encoded in the type) based on the starting
285  /// location. Add the DWARF information to the die.
286  void addComplexAddress(const DbgVariable &DV, DIE &Die,
287  dwarf::Attribute Attribute,
288  const MachineLocation &Location);
289 
290  /// Add a Dwarf loclistptr attribute data and value.
291  void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
292  void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
293 
294  /// Add a Dwarf expression attribute data and value.
295  void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
296 
297  /// Add an attribute containing an address expression to \p Die.
298  void addAddressExpr(DIE &Die, dwarf::Attribute Attribute, const MCExpr *Expr);
299 
301  DIE &SPDie);
302 
303  void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie);
304 
305  /// getRanges - Get the list of ranges for this unit.
306  const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
307  SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
308 
309  void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
310  const MCSymbol *getBaseAddress() const { return BaseAddress; }
311 
312  uint64_t getDWOId() const { return DWOId; }
313  void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
314 
315  bool hasDwarfPubSections() const;
316 };
317 
318 } // end namespace llvm
319 
320 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
const StringMap< const DIE * > & getGlobalTypes() const
unsigned getSize() const
Definition: DIE.h:700
LLVMContext & Context
virtual unsigned getHeaderSize() const
Compute the size of a header for this unit, not including the initial length field.
Definition: DwarfUnit.h:261
const MCSymbol * getBaseAddress() const
DIELoc - Represents an expression location.
Definition: DIE.h:845
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void addGlobalName(StringRef Name, const DIE &Die, const DIScope *Context) override
Add a new global name to the compile unit.
void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie)
Attribute
Attributes.
Definition: Dwarf.h:114
Collects and handles dwarf debug information.
Definition: DwarfDebug.h:280
This class is defined as the common parent of DbgVariable and DbgLabel such that it could levarage po...
Definition: DwarfDebug.h:67
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:44
Tagged DWARF-like metadata node.
bool useSplitDwarf() const
Returns whether or not to change the current debug info for the split dwarf proposal support...
Definition: DwarfDebug.h:658
DbgEntity * getExistingAbstractEntity(const DINode *Node)
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:41
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
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.
DbgValueHistoryMap::InlinedEntity InlinedEntity
Find abstract variable associated with Var.
void setDWOId(uint64_t DwoId)
void createAbstractEntity(const DINode *Node, LexicalScope *Scope)
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:187
This class is used to track local variable information.
Definition: DwarfDebug.h:116
void setBaseAddress(const MCSymbol *Base)
void addAddrTableBase()
Add the DW_AT_addr_base attribute to the unit DIE.
DIE * constructVariableDIE(DbgVariable &DV, bool Abstract=false)
constructVariableDIE - Construct a DIE for the given DbgVariable.
void addAddressExpr(DIE &Die, dwarf::Attribute Attribute, const MCExpr *Expr)
Add an attribute containing an address expression to Die.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
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 class is used to track label information.
Definition: DwarfDebug.h:232
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:40
void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index)
Add a Dwarf loclistptr attribute data and value.
A structured debug information entry.
Definition: DIE.h:661
DIE & getUnitDie()
Definition: DIE.h:833
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.
void finishEntityDefinition(const DbgEntity *Entity)
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.
DenseMap< const DINode *, std::unique_ptr< DbgEntity > > & getAbstractEntities()
Definition: DwarfFile.h:191
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.
std::pair< const DINode *, const DILocation * > InlinedEntity
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.
DIE * constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope)
Construct a DIE for the given DbgLabel.
SmallVector< RangeSpan, 2 > takeRanges()
void constructAbstractSubprogramScopeDIE(LexicalScope *Scope)
MCSymbol * getMacroLabelBegin() const
const StringMap< const DIE * > & getGlobalNames() const
Base class for types.
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition: DIE.h:826
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
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.
DIE & constructSubprogramScopeDIE(const DISubprogram *Sub, LexicalScope *Scope)
Construct a DIE for this subprogram scope.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:219
This file contains constants used for implementing Dwarf debug support.
DwarfDebug * DD
Definition: DwarfUnit.h:55
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:476
DIE & constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram &CalleeSP, bool IsTail, const MCExpr *PCOffset)
Construct a call site entry DIE describing a call within Scope to a callee described by CalleeSP...
bool includeMinimalInlineScopes() const
DwarfCompileUnit * getSkeleton() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Label)
addLocalLabelAddress - Add a dwarf label attribute data and value using DW_FORM_addr only...
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:48
DwarfFile * DU
Definition: DwarfUnit.h:56
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...