LLVM  10.0.0
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 enum class UnitKind { Skeleton, Full };
44 
45 class DwarfCompileUnit final : public DwarfUnit {
46  /// A numeric ID unique among all CUs in the module
47  unsigned UniqueID;
48  bool HasRangeLists = false;
49 
50  /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
51  /// the need to search for it in applyStmtList.
52  DIE::value_iterator StmtListValue;
53 
54  /// Skeleton unit associated with this unit.
55  DwarfCompileUnit *Skeleton = nullptr;
56 
57  /// The start of the unit within its section.
58  MCSymbol *LabelBegin;
59 
60  /// The start of the unit macro info within macro section.
61  MCSymbol *MacroLabelBegin;
62 
65 
66  ImportedEntityMap ImportedEntities;
67 
68  /// GlobalNames - A map of globally visible named entities for this unit.
69  StringMap<const DIE *> GlobalNames;
70 
71  /// GlobalTypes - A map of globally visible types for this unit.
72  StringMap<const DIE *> GlobalTypes;
73 
74  // List of ranges for a given compile unit.
76 
77  // The base address of this unit, if any. Used for relative references in
78  // ranges/locs.
79  const MCSymbol *BaseAddress = nullptr;
80 
81  DenseMap<const MDNode *, DIE *> AbstractSPDies;
83 
84  /// DWO ID for correlating skeleton and split units.
85  uint64_t DWOId = 0;
86 
87  /// Construct a DIE for the given DbgVariable without initializing the
88  /// DbgVariable's DIE reference.
89  DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
90 
91  bool isDwoUnit() const override;
92 
93  DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
94  if (isDwoUnit() && !DD->shareAcrossDWOCUs())
95  return AbstractSPDies;
96  return DU->getAbstractSPDies();
97  }
98 
100  if (isDwoUnit() && !DD->shareAcrossDWOCUs())
101  return AbstractEntities;
102  return DU->getAbstractEntities();
103  }
104 
105  void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override;
106 
107 public:
108  DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
109  DwarfDebug *DW, DwarfFile *DWU,
111 
112  bool hasRangeLists() const { return HasRangeLists; }
113  unsigned getUniqueID() const { return UniqueID; }
114 
116  return Skeleton;
117  }
118 
119  bool includeMinimalInlineScopes() const;
120 
121  void initStmtList();
122 
123  /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
124  void applyStmtList(DIE &D);
125 
126  /// A pair of GlobalVariable and DIExpression.
127  struct GlobalExpr {
130  };
131 
132  struct BaseTypeRef {
133  BaseTypeRef(unsigned BitSize, dwarf::TypeKind Encoding) :
134  BitSize(BitSize), Encoding(Encoding) {}
135  unsigned BitSize;
137  DIE *Die = nullptr;
138  };
139 
140  std::vector<BaseTypeRef> ExprRefedBaseTypes;
141 
142  /// Get or create global variable DIE.
143  DIE *
144  getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV,
145  ArrayRef<GlobalExpr> GlobalExprs);
146 
147  DIE *getOrCreateCommonBlock(const DICommonBlock *CB,
148  ArrayRef<GlobalExpr> GlobalExprs);
149 
150  void addLocationAttribute(DIE *ToDIE, const DIGlobalVariable *GV,
151  ArrayRef<GlobalExpr> GlobalExprs);
152 
153  /// addLabelAddress - Add a dwarf label attribute data and value using
154  /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
155  void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
156  const MCSymbol *Label);
157 
158  /// addLocalLabelAddress - Add a dwarf label attribute data and value using
159  /// DW_FORM_addr only.
160  void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
161  const MCSymbol *Label);
162 
163  DwarfCompileUnit &getCU() override { return *this; }
164 
165  unsigned getOrCreateSourceID(const DIFile *File) override;
166 
168  DIScope *Scope = IE->getScope();
169  assert(Scope && "Invalid Scope encoding!");
170  if (!isa<DILocalScope>(Scope))
171  // No need to add imported enities that are not local declaration.
172  return;
173 
174  auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
175  ImportedEntities[LocalScope].push_back(IE);
176  }
177 
178  /// addRange - Add an address range to the list of ranges for this unit.
179  void addRange(RangeSpan Range);
180 
181  void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
182 
183  /// Find DIE for the given subprogram and attach appropriate
184  /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
185  /// variables in this scope then create and insert DIEs for these
186  /// variables.
187  DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
188 
189  void constructScopeDIE(LexicalScope *Scope,
190  SmallVectorImpl<DIE *> &FinalChildren);
191 
192  /// A helper function to construct a RangeSpanList for a given
193  /// lexical scope.
194  void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
195 
196  void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
197 
198  void attachRangesOrLowHighPC(DIE &D,
199  const SmallVectorImpl<InsnRange> &Ranges);
200 
201  /// This scope represents inlined body of a function. Construct
202  /// DIE to represent this concrete inlined copy of the function.
203  DIE *constructInlinedScopeDIE(LexicalScope *Scope);
204 
205  /// Construct new DW_TAG_lexical_block for this scope and
206  /// attach DW_AT_low_pc/DW_AT_high_pc labels.
207  DIE *constructLexicalScopeDIE(LexicalScope *Scope);
208 
209  /// constructVariableDIE - Construct a DIE for the given DbgVariable.
210  DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
211 
212  DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
213  DIE *&ObjectPointer);
214 
215  /// Construct a DIE for the given DbgLabel.
216  DIE *constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope);
217 
218  /// A helper function to create children of a Scope DIE.
219  DIE *createScopeChildrenDIE(LexicalScope *Scope,
220  SmallVectorImpl<DIE *> &Children,
221  bool *HasNonScopeChildren = nullptr);
222 
223  void createBaseTypeDIEs();
224 
225  /// Construct a DIE for this subprogram scope.
226  DIE &constructSubprogramScopeDIE(const DISubprogram *Sub,
227  LexicalScope *Scope);
228 
229  DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
230 
231  void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
232 
233  /// This takes a DWARF 5 tag and returns it or a GNU analog.
234  dwarf::Tag getDwarf5OrGNUTag(dwarf::Tag Tag) const;
235 
236  /// This takes a DWARF 5 attribute and returns it or a GNU analog.
237  dwarf::Attribute getDwarf5OrGNUAttr(dwarf::Attribute Attr) const;
238 
239  /// This takes a DWARF 5 location atom and either returns it or a GNU analog.
240  dwarf::LocationAtom getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const;
241 
242  /// Construct a call site entry DIE describing a call within \p Scope to a
243  /// callee described by \p CalleeSP.
244  /// \p IsTail specifies whether the call is a tail call.
245  /// \p PCAddr (used for GDB + DWARF 4 tuning) points to the PC value after
246  /// the call instruction.
247  /// \p PCOffset (used for cases other than GDB + DWARF 4 tuning) must be
248  /// non-zero for non-tail calls (in the case of non-gdb tuning, since for
249  /// GDB + DWARF 5 tuning we still generate PC info for tail calls) or be the
250  /// function-local offset to PC value after the call instruction.
251  /// \p CallReg is a register location for an indirect call. For direct calls
252  /// the \p CallReg is set to 0.
253  DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram *CalleeSP,
254  bool IsTail, const MCSymbol *PCAddr,
255  const MCExpr *PCOffset, unsigned CallReg);
256  /// Construct call site parameter DIEs for the \p CallSiteDIE. The \p Params
257  /// were collected by the \ref collectCallSiteParameters.
258  /// Note: The order of parameters does not matter, since debuggers recognize
259  /// call site parameters by the DW_AT_location attribute.
260  void constructCallSiteParmEntryDIEs(DIE &CallSiteDIE,
262 
263  /// Construct import_module DIE.
264  DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
265 
266  void finishSubprogramDefinition(const DISubprogram *SP);
267  void finishEntityDefinition(const DbgEntity *Entity);
268 
269  /// Find abstract variable associated with Var.
271  DbgEntity *getExistingAbstractEntity(const DINode *Node);
272  void createAbstractEntity(const DINode *Node, LexicalScope *Scope);
273 
274  /// Set the skeleton unit associated with this unit.
275  void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
276 
277  unsigned getHeaderSize() const override {
278  // DWARF v5 added the DWO ID to the header for split/skeleton units.
279  unsigned DWOIdSize =
280  DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
281  : 0;
282  return DwarfUnit::getHeaderSize() + DWOIdSize;
283  }
284  unsigned getLength() {
285  return sizeof(uint32_t) + // Length field
286  getHeaderSize() + getUnitDie().getSize();
287  }
288 
289  void emitHeader(bool UseOffsets) override;
290 
291  /// Add the DW_AT_addr_base attribute to the unit DIE.
292  void addAddrTableBase();
293 
295  assert(getSection());
296  return LabelBegin;
297  }
298 
300  return MacroLabelBegin;
301  }
302 
303  /// Add a new global name to the compile unit.
304  void addGlobalName(StringRef Name, const DIE &Die,
305  const DIScope *Context) override;
306 
307  /// Add a new global name present in a type unit to this compile unit.
308  void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
309 
310  /// Add a new global type to the compile unit.
311  void addGlobalType(const DIType *Ty, const DIE &Die,
312  const DIScope *Context) override;
313 
314  /// Add a new global type present in a type unit to this compile unit.
315  void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
316 
317  const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
318  const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
319 
320  /// Add DW_AT_location attribute for a DbgVariable based on provided
321  /// MachineLocation.
322  void addVariableAddress(const DbgVariable &DV, DIE &Die,
323  MachineLocation Location);
324  /// Add an address attribute to a die based on the location provided.
325  void addAddress(DIE &Die, dwarf::Attribute Attribute,
326  const MachineLocation &Location);
327 
328  /// Start with the address based on the location provided, and generate the
329  /// DWARF information necessary to find the actual variable (navigating the
330  /// extra location information encoded in the type) based on the starting
331  /// location. Add the DWARF information to the die.
332  void addComplexAddress(const DbgVariable &DV, DIE &Die,
333  dwarf::Attribute Attribute,
334  const MachineLocation &Location);
335 
336  /// Add a Dwarf loclistptr attribute data and value.
337  void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
338  void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
339 
340  /// Add a Dwarf expression attribute data and value.
341  void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
342 
343  /// Add an attribute containing an address expression to \p Die.
344  void addAddressExpr(DIE &Die, dwarf::Attribute Attribute, const MCExpr *Expr);
345 
346  void applySubprogramAttributesToDefinition(const DISubprogram *SP,
347  DIE &SPDie);
348 
349  void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie);
350 
351  /// getRanges - Get the list of ranges for this unit.
352  const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
353  SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
354 
355  void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
356  const MCSymbol *getBaseAddress() const { return BaseAddress; }
357 
358  uint64_t getDWOId() const { return DWOId; }
359  void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
360 
361  bool hasDwarfPubSections() const;
362 
363  void addBaseTypeRef(DIEValueList &Die, int64_t Idx);
364 };
365 
366 } // end namespace llvm
367 
368 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
const StringMap< const DIE * > & getGlobalTypes() const
LocationAtom
Definition: Dwarf.h:116
std::vector< BaseTypeRef > ExprRefedBaseTypes
LLVMContext & Context
virtual unsigned getHeaderSize() const
Compute the size of a header for this unit, not including the initial length field.
Definition: DwarfUnit.h:258
const MCSymbol * getBaseAddress() const
DIELoc - Represents an expression location.
Definition: DIE.h:903
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:66
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Attribute
Attributes.
Definition: Dwarf.h:103
Collects and handles dwarf debug information.
Definition: DwarfDebug.h:297
This class is defined as the common parent of DbgVariable and DbgLabel such that it could levarage po...
Definition: DwarfDebug.h:69
DwarfCompileUnit & getCU() override
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:44
Tagged DWARF-like metadata node.
DIScope * getScope() const
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:35
DbgValueHistoryMap::InlinedEntity InlinedEntity
Find abstract variable associated with Var.
void setDWOId(uint64_t DwoId)
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: ELF.h:311
Subprogram description.
unsigned getUniqueID() const
uint64_t getDWOId() const
This class is used to track local variable information.
Definition: DwarfDebug.h:118
void setBaseAddress(const MCSymbol *Base)
A list of DIE values.
Definition: DIE.h:635
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
This class is used to track label information.
Definition: DwarfDebug.h:235
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:40
Instrumentation for Order File
A structured debug information entry.
Definition: DIE.h:712
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:80
unsigned getHeaderSize() const override
Compute the size of a header for this unit, not including the initial length field.
A pair of GlobalVariable and DIExpression.
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.
SmallVector< RangeSpan, 2 > takeRanges()
MCSymbol * getMacroLabelBegin() const
const StringMap< const DIE * > & getGlobalNames() const
Base class for types.
BaseTypeRef(unsigned BitSize, dwarf::TypeKind Encoding)
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
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:242
This file contains constants used for implementing Dwarf debug support.
const SmallVectorImpl< RangeSpan > & getRanges() const
getRanges - Get the list of ranges for this unit.
void addImportedEntity(const DIImportedEntity *IE)
DwarfCompileUnit * getSkeleton() const
static void addRange(SmallVectorImpl< ConstantInt *> &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:969
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
MCSymbol * getLabelBegin() const