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
111public:
112 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
113 DwarfDebug *DW, DwarfFile *DWU,
114 UnitKind Kind = UnitKind::Full);
115
116 bool hasRangeLists() const { return HasRangeLists; }
117 unsigned getUniqueID() const { return UniqueID; }
118
120 return Skeleton;
121 }
122
123 bool includeMinimalInlineScopes() const;
124
125 void initStmtList();
126
127 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
128 void applyStmtList(DIE &D);
129
130 /// Get line table start symbol for this unit.
131 MCSymbol *getLineTableStartSym() const { return LineTableStartSym; }
132
133 /// A pair of GlobalVariable and DIExpression.
134 struct GlobalExpr {
137 };
138
139 struct BaseTypeRef {
142 unsigned BitSize;
144 DIE *Die = nullptr;
145 };
146
147 std::vector<BaseTypeRef> ExprRefedBaseTypes;
148
149 /// Get or create global variable DIE.
150 DIE *
152 ArrayRef<GlobalExpr> GlobalExprs);
153
155 ArrayRef<GlobalExpr> GlobalExprs);
156
157 void addLocationAttribute(DIE *ToDIE, const DIGlobalVariable *GV,
158 ArrayRef<GlobalExpr> GlobalExprs);
159
160 /// addLabelAddress - Add a dwarf label attribute data and value using
161 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
163 const MCSymbol *Label);
164
165 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
166 /// DW_FORM_addr only.
168 const MCSymbol *Label);
169
170 DwarfCompileUnit &getCU() override { return *this; }
171
172 unsigned getOrCreateSourceID(const DIFile *File) override;
173
175 DIScope *Scope = IE->getScope();
176 assert(Scope && "Invalid Scope encoding!");
177 if (!isa<DILocalScope>(Scope))
178 // No need to add imported enities that are not local declaration.
179 return;
180
181 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
182 ImportedEntities[LocalScope].push_back(IE);
183 }
184
185 /// addRange - Add an address range to the list of ranges for this unit.
186 void addRange(RangeSpan Range);
187
188 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
189
190 /// Find DIE for the given subprogram and attach appropriate
191 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
192 /// variables in this scope then create and insert DIEs for these
193 /// variables.
196
197 void constructScopeDIE(LexicalScope *Scope, DIE &ParentScopeDIE);
198
199 /// A helper function to construct a RangeSpanList for a given
200 /// lexical scope.
201 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
202
204
206 const SmallVectorImpl<InsnRange> &Ranges);
207
208 /// This scope represents an inlined body of a function. Construct a
209 /// DIE to represent this concrete inlined copy of the function.
210 DIE *constructInlinedScopeDIE(LexicalScope *Scope, DIE &ParentScopeDIE);
211
212 /// Construct new DW_TAG_lexical_block for this scope and
213 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
215
216 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
217 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
218
220 DIE *&ObjectPointer);
221
222 /// Construct a DIE for the given DbgLabel.
224
225 void createBaseTypeDIEs();
226
227 /// Construct a DIE for this subprogram scope.
229 LexicalScope *Scope);
230
231 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
232
234
235 /// Whether to use the GNU analog for a DWARF5 tag, attribute, or location
236 /// atom. Only applicable when emitting otherwise DWARF4-compliant debug info.
237 bool useGNUAnalogForDwarf5Feature() const;
238
239 /// This takes a DWARF 5 tag and returns it or a GNU analog.
241
242 /// This takes a DWARF 5 attribute and returns it or a GNU analog.
244
245 /// This takes a DWARF 5 location atom and either returns it or a GNU analog.
247
248 /// Construct a call site entry DIE describing a call within \p Scope to a
249 /// callee described by \p CalleeSP.
250 /// \p IsTail specifies whether the call is a tail call.
251 /// \p PCAddr points to the PC value after the call instruction.
252 /// \p CallAddr points to the PC value at the call instruction (or is null).
253 /// \p CallReg is a register location for an indirect call. For direct calls
254 /// the \p CallReg is set to 0.
255 DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram *CalleeSP,
256 bool IsTail, const MCSymbol *PCAddr,
257 const MCSymbol *CallAddr, unsigned CallReg);
258 /// Construct call site parameter DIEs for the \p CallSiteDIE. The \p Params
259 /// were collected by the \ref collectCallSiteParameters.
260 /// Note: The order of parameters does not matter, since debuggers recognize
261 /// call site parameters by the DW_AT_location attribute.
262 void constructCallSiteParmEntryDIEs(DIE &CallSiteDIE,
264
265 /// Construct import_module DIE.
267
269 void finishEntityDefinition(const DbgEntity *Entity);
270
271 /// Find abstract variable associated with Var.
274 void createAbstractEntity(const DINode *Node, LexicalScope *Scope);
275
276 /// Set the skeleton unit associated with this unit.
277 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
278
279 unsigned getHeaderSize() const override {
280 // DWARF v5 added the DWO ID to the header for split/skeleton units.
281 unsigned DWOIdSize =
282 DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
283 : 0;
284 return DwarfUnit::getHeaderSize() + DWOIdSize;
285 }
286 unsigned getLength() {
287 return Asm->getUnitLengthFieldByteSize() + // Length field
289 }
290
291 void emitHeader(bool UseOffsets) override;
292
293 /// Add the DW_AT_addr_base attribute to the unit DIE.
294 void addAddrTableBase();
295
297 assert(LabelBegin && "LabelBegin is not initialized");
298 return LabelBegin;
299 }
300
302 return MacroLabelBegin;
303 }
304
305 /// Add a new global name to the compile unit.
306 void addGlobalName(StringRef Name, const DIE &Die,
307 const DIScope *Context) override;
308
309 /// Add a new global name present in a type unit to this compile unit.
310 void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
311
312 /// Add a new global type to the compile unit.
313 void addGlobalType(const DIType *Ty, const DIE &Die,
314 const DIScope *Context) override;
315
316 /// Add a new global type present in a type unit to this compile unit.
317 void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
318
319 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
320 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
321
322 /// Add DW_AT_location attribute for a DbgVariable based on provided
323 /// MachineLocation.
324 void addVariableAddress(const DbgVariable &DV, DIE &Die,
325 MachineLocation Location);
326 /// Add an address attribute to a die based on the location provided.
328 const MachineLocation &Location);
329
330 /// Start with the address based on the location provided, and generate the
331 /// DWARF information necessary to find the actual variable (navigating the
332 /// extra location information encoded in the type) based on the starting
333 /// location. Add the DWARF information to the die.
334 void addComplexAddress(const DbgVariable &DV, DIE &Die,
336 const MachineLocation &Location);
337
338 /// Add a Dwarf loclistptr attribute data and value.
339 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
340 void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
341
342 /// Add a Dwarf expression attribute data and value.
343 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
344
346 DIE &SPDie);
347
348 void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie);
349
350 /// getRanges - Get the list of ranges for this unit.
351 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
352 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
353
354 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
355 const MCSymbol *getBaseAddress() const { return BaseAddress; }
356
357 uint64_t getDWOId() const { return DWOId; }
358 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
359
360 bool hasDwarfPubSections() const;
361
362 void addBaseTypeRef(DIEValueList &Die, int64_t Idx);
363};
364
365} // end namespace llvm
366
367#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
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:930
DIE & getUnitDie()
Definition: DIE.h:919
A list of DIE values.
Definition: DIE.h:662
A structured debug information entry.
Definition: DIE.h:739
unsigned getSize() const
Definition: DIE.h:782
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:558
bool useSplitDwarf() const
Returns whether or not to change the current debug info for the split dwarf proposal support.
Definition: DwarfDebug.h:737
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.