LLVM 17.0.0git
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"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/StringRef.h"
28#include <cassert>
29#include <cstdint>
30#include <memory>
31
32namespace llvm {
33
34class AsmPrinter;
35class DIE;
36class DIELoc;
37class DIEValueList;
38class DwarfFile;
39class GlobalVariable;
40class MCExpr;
41class MCSymbol;
42class MDNode;
43
44enum class UnitKind { Skeleton, Full };
45
46class DwarfCompileUnit final : public DwarfUnit {
47 /// A numeric ID unique among all CUs in the module
48 unsigned UniqueID;
49 bool HasRangeLists = false;
50
51 /// The start of the unit line section, this is also
52 /// reused in appyStmtList.
53 MCSymbol *LineTableStartSym;
54
55 /// Skeleton unit associated with this unit.
56 DwarfCompileUnit *Skeleton = nullptr;
57
58 /// The start of the unit within its section.
59 MCSymbol *LabelBegin = nullptr;
60
61 /// The start of the unit macro info within macro section.
62 MCSymbol *MacroLabelBegin;
63
66
67 ImportedEntityMap ImportedEntities;
68
69 /// GlobalNames - A map of globally visible named entities for this unit.
70 StringMap<const DIE *> GlobalNames;
71
72 /// GlobalTypes - A map of globally visible types for this unit.
73 StringMap<const DIE *> GlobalTypes;
74
75 // List of ranges for a given compile unit.
77
78 // The base address of this unit, if any. Used for relative references in
79 // ranges/locs.
80 const MCSymbol *BaseAddress = nullptr;
81
84
85 /// DWO ID for correlating skeleton and split units.
86 uint64_t DWOId = 0;
87
88 const DIFile *LastFile = nullptr;
89 unsigned LastFileID;
90
91 /// Construct a DIE for the given DbgVariable without initializing the
92 /// DbgVariable's DIE reference.
93 DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
94
95 bool isDwoUnit() const override;
96
97 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
98 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
99 return AbstractSPDies;
100 return DU->getAbstractSPDies();
101 }
102
104 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
105 return AbstractEntities;
106 return DU->getAbstractEntities();
107 }
108
109 void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override;
110
111 /// Add info for Wasm-global-based relocation.
112 void addWasmRelocBaseGlobal(DIELoc *Loc, StringRef GlobalName,
113 uint64_t GlobalIndex);
114
115public:
116 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
117 DwarfDebug *DW, DwarfFile *DWU,
118 UnitKind Kind = UnitKind::Full);
119
120 bool hasRangeLists() const { return HasRangeLists; }
121 unsigned getUniqueID() const { return UniqueID; }
122
124 return Skeleton;
125 }
126
127 bool includeMinimalInlineScopes() const;
128
129 void initStmtList();
130
131 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
132 void applyStmtList(DIE &D);
133
134 /// Get line table start symbol for this unit.
135 MCSymbol *getLineTableStartSym() const { return LineTableStartSym; }
136
137 /// A pair of GlobalVariable and DIExpression.
138 struct GlobalExpr {
141 };
142
143 struct BaseTypeRef {
146 unsigned BitSize;
148 DIE *Die = nullptr;
149 };
150
151 std::vector<BaseTypeRef> ExprRefedBaseTypes;
152
153 /// Get or create global variable DIE.
154 DIE *
156 ArrayRef<GlobalExpr> GlobalExprs);
157
159 ArrayRef<GlobalExpr> GlobalExprs);
160
161 void addLocationAttribute(DIE *ToDIE, const DIGlobalVariable *GV,
162 ArrayRef<GlobalExpr> GlobalExprs);
163
164 /// addLabelAddress - Add a dwarf label attribute data and value using
165 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
167 const MCSymbol *Label);
168
169 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
170 /// DW_FORM_addr only.
172 const MCSymbol *Label);
173
174 DwarfCompileUnit &getCU() override { return *this; }
175
176 unsigned getOrCreateSourceID(const DIFile *File) override;
177
179 DIScope *Scope = IE->getScope();
180 assert(Scope && "Invalid Scope encoding!");
181 if (!isa<DILocalScope>(Scope))
182 // No need to add imported enities that are not local declaration.
183 return;
184
185 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
186 ImportedEntities[LocalScope].push_back(IE);
187 }
188
189 /// addRange - Add an address range to the list of ranges for this unit.
190 void addRange(RangeSpan Range);
191
192 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
193
194 /// Find DIE for the given subprogram and attach appropriate
195 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
196 /// variables in this scope then create and insert DIEs for these
197 /// variables.
200
201 void constructScopeDIE(LexicalScope *Scope, DIE &ParentScopeDIE);
202
203 /// A helper function to construct a RangeSpanList for a given
204 /// lexical scope.
205 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
206
208
210 const SmallVectorImpl<InsnRange> &Ranges);
211
212 /// This scope represents an inlined body of a function. Construct a
213 /// DIE to represent this concrete inlined copy of the function.
214 DIE *constructInlinedScopeDIE(LexicalScope *Scope, DIE &ParentScopeDIE);
215
216 /// Construct new DW_TAG_lexical_block for this scope and
217 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
219
220 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
221 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
222
224 DIE *&ObjectPointer);
225
226 /// Construct a DIE for the given DbgLabel.
228
229 void createBaseTypeDIEs();
230
231 /// Construct a DIE for this subprogram scope.
233 LexicalScope *Scope);
234
235 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
236
238
239 /// Whether to use the GNU analog for a DWARF5 tag, attribute, or location
240 /// atom. Only applicable when emitting otherwise DWARF4-compliant debug info.
241 bool useGNUAnalogForDwarf5Feature() const;
242
243 /// This takes a DWARF 5 tag and returns it or a GNU analog.
245
246 /// This takes a DWARF 5 attribute and returns it or a GNU analog.
248
249 /// This takes a DWARF 5 location atom and either returns it or a GNU analog.
251
252 /// Construct a call site entry DIE describing a call within \p Scope to a
253 /// callee described by \p CalleeSP.
254 /// \p IsTail specifies whether the call is a tail call.
255 /// \p PCAddr points to the PC value after the call instruction.
256 /// \p CallAddr points to the PC value at the call instruction (or is null).
257 /// \p CallReg is a register location for an indirect call. For direct calls
258 /// the \p CallReg is set to 0.
259 DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram *CalleeSP,
260 bool IsTail, const MCSymbol *PCAddr,
261 const MCSymbol *CallAddr, unsigned CallReg);
262 /// Construct call site parameter DIEs for the \p CallSiteDIE. The \p Params
263 /// were collected by the \ref collectCallSiteParameters.
264 /// Note: The order of parameters does not matter, since debuggers recognize
265 /// call site parameters by the DW_AT_location attribute.
266 void constructCallSiteParmEntryDIEs(DIE &CallSiteDIE,
268
269 /// Construct import_module DIE.
271
273 void finishEntityDefinition(const DbgEntity *Entity);
274
275 /// Find abstract variable associated with Var.
278 void createAbstractEntity(const DINode *Node, LexicalScope *Scope);
279
280 /// Set the skeleton unit associated with this unit.
281 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
282
283 unsigned getHeaderSize() const override {
284 // DWARF v5 added the DWO ID to the header for split/skeleton units.
285 unsigned DWOIdSize =
286 DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
287 : 0;
288 return DwarfUnit::getHeaderSize() + DWOIdSize;
289 }
290 unsigned getLength() {
291 return Asm->getUnitLengthFieldByteSize() + // Length field
293 }
294
295 void emitHeader(bool UseOffsets) override;
296
297 /// Add the DW_AT_addr_base attribute to the unit DIE.
298 void addAddrTableBase();
299
301 assert(LabelBegin && "LabelBegin is not initialized");
302 return LabelBegin;
303 }
304
306 return MacroLabelBegin;
307 }
308
309 /// Add a new global name to the compile unit.
310 void addGlobalName(StringRef Name, const DIE &Die,
311 const DIScope *Context) override;
312
313 /// Add a new global name present in a type unit to this compile unit.
314 void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
315
316 /// Add a new global type to the compile unit.
317 void addGlobalType(const DIType *Ty, const DIE &Die,
318 const DIScope *Context) override;
319
320 /// Add a new global type present in a type unit to this compile unit.
321 void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
322
323 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
324 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
325
326 /// Add DW_AT_location attribute for a DbgVariable based on provided
327 /// MachineLocation.
328 void addVariableAddress(const DbgVariable &DV, DIE &Die,
329 MachineLocation Location);
330 /// Add an address attribute to a die based on the location provided.
332 const MachineLocation &Location);
333
334 /// Start with the address based on the location provided, and generate the
335 /// DWARF information necessary to find the actual variable (navigating the
336 /// extra location information encoded in the type) based on the starting
337 /// location. Add the DWARF information to the die.
338 void addComplexAddress(const DbgVariable &DV, DIE &Die,
340 const MachineLocation &Location);
341
342 /// Add a Dwarf loclistptr attribute data and value.
343 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
344 void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
345
346 /// Add a Dwarf expression attribute data and value.
347 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
348
350 DIE &SPDie);
351
352 void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie);
353
354 /// getRanges - Get the list of ranges for this unit.
355 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
356 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
357
358 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
359 const MCSymbol *getBaseAddress() const { return BaseAddress; }
360
361 uint64_t getDWOId() const { return DWOId; }
362 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
363
364 bool hasDwarfPubSections() const;
365
366 void addBaseTypeRef(DIEValueList &Die, int64_t Idx);
367};
368
369} // end namespace llvm
370
371#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file defines the StringMap class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
This file contains constants used for implementing Dwarf debug support.
std::string Name
bool End
Definition: ELF_riscv.cpp:464
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:84
unsigned int getUnitLengthFieldByteSize() const
Returns 4 for DWARF32 and 12 for DWARF64.
DIELoc - Represents an expression location.
Definition: DIE.h:937
DIE & getUnitDie()
Definition: DIE.h:926
A list of DIE values.
Definition: DIE.h:669
A structured debug information entry.
Definition: DIE.h:746
unsigned getSize() const
Definition: DIE.h:789
DWARF expression.
An imported module (C++ using directive or similar).
Tagged DWARF-like metadata node.
Base class for scope-like contexts.
Subprogram description.
Base class for types.
This class is defined as the common parent of DbgVariable and DbgLabel such that it could levarage po...
Definition: DwarfDebug.h:65
This class is used to track label information.
Definition: DwarfDebug.h:234
std::pair< const DINode *, const DILocation * > InlinedEntity
This class is used to track local variable information.
Definition: DwarfDebug.h:115
bool useGNUAnalogForDwarf5Feature() const
Whether to use the GNU analog for a DWARF5 tag, attribute, or location atom.
void constructCallSiteParmEntryDIEs(DIE &CallSiteDIE, SmallVector< DbgCallSiteParam, 4 > &Params)
Construct call site parameter DIEs for the CallSiteDIE.
void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End)
void emitHeader(bool UseOffsets) override
Emit the header for this unit, not including the initial length field.
unsigned getHeaderSize() const override
Compute the size of a header for this unit, not including the initial length field.
dwarf::Tag getDwarf5OrGNUTag(dwarf::Tag Tag) const
This takes a DWARF 5 tag and returns it or a GNU analog.
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 constructAbstractSubprogramScopeDIE(LexicalScope *Scope)
bool includeMinimalInlineScopes() const
SmallVector< RangeSpan, 2 > takeRanges()
void addBaseTypeRef(DIEValueList &Die, int64_t Idx)
void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context)
Add a new global name present in a type unit to this compile unit.
void finishEntityDefinition(const DbgEntity *Entity)
DIE * constructImportedEntityDIE(const DIImportedEntity *Module)
Construct import_module DIE.
void addRange(RangeSpan Range)
addRange - Add an address range to the list of ranges for this unit.
void addAddrTableBase()
Add the DW_AT_addr_base attribute to the unit DIE.
DIE & constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram *CalleeSP, bool IsTail, const MCSymbol *PCAddr, const MCSymbol *CallAddr, unsigned CallReg)
Construct a call site entry DIE describing a call within Scope to a callee described by CalleeSP.
MCSymbol * getMacroLabelBegin() const
std::vector< BaseTypeRef > ExprRefedBaseTypes
DIE * constructInlinedScopeDIE(LexicalScope *Scope, DIE &ParentScopeDIE)
This scope represents an inlined body of a function.
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...
void addGlobalType(const DIType *Ty, const DIE &Die, const DIScope *Context) override
Add a new global type to the compile unit.
void addScopeRangeList(DIE &ScopeDIE, SmallVector< RangeSpan, 2 > Range)
A helper function to construct a RangeSpanList for a given lexical scope.
uint64_t getDWOId() const
DIE * getOrCreateCommonBlock(const DICommonBlock *CB, ArrayRef< GlobalExpr > GlobalExprs)
void addVariableAddress(const DbgVariable &DV, DIE &Die, MachineLocation Location)
Add DW_AT_location attribute for a DbgVariable based on provided MachineLocation.
DbgValueHistoryMap::InlinedEntity InlinedEntity
Find abstract variable associated with Var.
MCSymbol * getLabelBegin() const
void addGlobalName(StringRef Name, const DIE &Die, const DIScope *Context) override
Add a new global name to the compile unit.
void createAbstractEntity(const DINode *Node, LexicalScope *Scope)
void applyStmtList(DIE &D)
Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
void addImportedEntity(const DIImportedEntity *IE)
DIE * constructVariableDIE(DbgVariable &DV, bool Abstract=false)
constructVariableDIE - Construct a DIE for the given DbgVariable.
void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie)
dwarf::LocationAtom getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const
This takes a DWARF 5 location atom and either returns it or a GNU analog.
DIE & constructSubprogramScopeDIE(const DISubprogram *Sub, LexicalScope *Scope)
Construct a DIE for this subprogram scope.
DIE * getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV, ArrayRef< GlobalExpr > GlobalExprs)
Get or create global variable DIE.
void addLocationAttribute(DIE *ToDIE, const DIGlobalVariable *GV, ArrayRef< GlobalExpr > GlobalExprs)
DwarfCompileUnit * getSkeleton() const
void applySubprogramAttributesToDefinition(const DISubprogram *SP, DIE &SPDie)
DIE * createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE)
DIE & updateSubprogramScopeDIEImpl(const DISubprogram *SP, DIE *SPDie)
void setSkeleton(DwarfCompileUnit &Skel)
Set the skeleton unit associated with this unit.
void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr)
Add a Dwarf expression attribute data and value.
dwarf::Attribute getDwarf5OrGNUAttr(dwarf::Attribute Attr) const
This takes a DWARF 5 attribute and returns it or a GNU analog.
unsigned getUniqueID() const
void addAddress(DIE &Die, dwarf::Attribute Attribute, const MachineLocation &Location)
Add an address attribute to a die based on the location provided.
void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie)
void setDWOId(uint64_t DwoId)
void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Label)
addLocalLabelAddress - Add a dwarf label attribute data and value using DW_FORM_addr only.
DIE * constructLexicalScopeDIE(LexicalScope *Scope)
Construct new DW_TAG_lexical_block for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
MCSymbol * getLineTableStartSym() const
Get line table start symbol for this unit.
const SmallVectorImpl< RangeSpan > & getRanges() const
getRanges - Get the list of ranges for this unit.
unsigned getOrCreateSourceID(const DIFile *File) override
Look up the source ID for the given file.
void constructScopeDIE(LexicalScope *Scope, DIE &ParentScopeDIE)
const MCSymbol * getBaseAddress() const
const StringMap< const DIE * > & getGlobalNames() const
DIE * constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope)
Construct a DIE for the given DbgLabel.
void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context)
Add a new global type present in a type unit to this compile unit.
DbgEntity * getExistingAbstractEntity(const DINode *Node)
const StringMap< const DIE * > & getGlobalTypes() const
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...
void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index)
Add a Dwarf loclistptr attribute data and value.
DwarfCompileUnit & getCU() override
void attachRangesOrLowHighPC(DIE &D, SmallVector< RangeSpan, 2 > Ranges)
void setBaseAddress(const MCSymbol *Base)
void finishSubprogramDefinition(const DISubprogram *SP)
Collects and handles dwarf debug information.
Definition: DwarfDebug.h:296
uint16_t getDwarfVersion() const
Returns the Dwarf Version.
bool shareAcrossDWOCUs() const
Definition: DwarfDebug.cpp:555
bool useSplitDwarf() const
Returns whether or not to change the current debug info for the split dwarf proposal support.
Definition: DwarfDebug.h:735
DenseMap< const DINode *, std::unique_ptr< DbgEntity > > & getAbstractEntities()
Definition: DwarfFile.h:169
DenseMap< const MDNode *, DIE * > & getAbstractSPDies()
Definition: DwarfFile.h:165
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:35
DwarfDebug * DD
Definition: DwarfUnit.h:50
virtual unsigned getHeaderSize() const
Compute the size of a header for this unit, not including the initial length field.
Definition: DwarfUnit.h:269
DwarfFile * DU
Definition: DwarfUnit.h:51
AsmPrinter * Asm
Target of Dwarf emission.
Definition: DwarfUnit.h:44
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:44
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
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:65
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Attribute
Attributes.
Definition: Dwarf.h:123
LocationAtom
Definition: Dwarf.h:136
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
BaseTypeRef(unsigned BitSize, dwarf::TypeKind Encoding)
A pair of GlobalVariable and DIExpression.