1 //===- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf compile unit.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
17 #include "DbgValueHistoryCalculator.h"
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"
25 #include "llvm/BinaryFormat/Dwarf.h"
26 #include "llvm/CodeGen/DIE.h"
27 #include "llvm/CodeGen/LexicalScopes.h"
28 #include "llvm/IR/DebugInfoMetadata.h"
29 #include "llvm/Support/Casting.h"
44 class DwarfCompileUnit final : public DwarfUnit {
45 /// A numeric ID unique among all CUs in the module
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;
52 /// Skeleton unit associated with this unit.
53 DwarfCompileUnit *Skeleton = nullptr;
55 /// The start of the unit within its section.
58 /// The start of the unit macro info within macro section.
59 MCSymbol *MacroLabelBegin;
61 using ImportedEntityList = SmallVector<const MDNode *, 8>;
62 using ImportedEntityMap = DenseMap<const MDNode *, ImportedEntityList>;
64 ImportedEntityMap ImportedEntities;
66 /// GlobalNames - A map of globally visible named entities for this unit.
67 StringMap<const DIE *> GlobalNames;
69 /// GlobalTypes - A map of globally visible types for this unit.
70 StringMap<const DIE *> GlobalTypes;
72 // List of range lists for a given compile unit, separate from the ranges for
74 SmallVector<RangeSpanList, 1> CURangeLists;
76 // List of ranges for a given compile unit.
77 SmallVector<RangeSpan, 2> CURanges;
79 // The base address of this unit, if any. Used for relative references in
81 const MCSymbol *BaseAddress = nullptr;
83 DenseMap<const MDNode *, DIE *> AbstractSPDies;
84 DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> AbstractVariables;
86 /// DWO ID for correlating skeleton and split units.
89 /// Construct a DIE for the given DbgVariable without initializing the
90 /// DbgVariable's DIE reference.
91 DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
93 bool isDwoUnit() const override;
95 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
96 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
97 return AbstractSPDies;
98 return DU->getAbstractSPDies();
101 DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> &getAbstractVariables() {
102 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
103 return AbstractVariables;
104 return DU->getAbstractVariables();
108 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
109 DwarfDebug *DW, DwarfFile *DWU);
111 unsigned getUniqueID() const { return UniqueID; }
113 DwarfCompileUnit *getSkeleton() const {
117 bool includeMinimalInlineScopes() const;
121 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
122 void applyStmtList(DIE &D);
124 /// A pair of GlobalVariable and DIExpression.
126 const GlobalVariable *Var;
127 const DIExpression *Expr;
130 /// Get or create global variable DIE.
132 getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV,
133 ArrayRef<GlobalExpr> GlobalExprs);
135 /// addLabelAddress - Add a dwarf label attribute data and value using
136 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
137 void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
138 const MCSymbol *Label);
140 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
141 /// DW_FORM_addr only.
142 void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
143 const MCSymbol *Label);
145 DwarfCompileUnit &getCU() override { return *this; }
147 unsigned getOrCreateSourceID(const DIFile *File) override;
149 void addImportedEntity(const DIImportedEntity* IE) {
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.
156 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
157 ImportedEntities[LocalScope].push_back(IE);
160 /// addRange - Add an address range to the list of ranges for this unit.
161 void addRange(RangeSpan Range);
163 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
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
169 DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
171 void constructScopeDIE(LexicalScope *Scope,
172 SmallVectorImpl<DIE *> &FinalChildren);
174 /// A helper function to construct a RangeSpanList for a given
176 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
178 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
180 void attachRangesOrLowHighPC(DIE &D,
181 const SmallVectorImpl<InsnRange> &Ranges);
183 /// This scope represents inlined body of a function. Construct
184 /// DIE to represent this concrete inlined copy of the function.
185 DIE *constructInlinedScopeDIE(LexicalScope *Scope);
187 /// Construct new DW_TAG_lexical_block for this scope and
188 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
189 DIE *constructLexicalScopeDIE(LexicalScope *Scope);
191 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
192 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
194 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
195 DIE *&ObjectPointer);
197 /// A helper function to create children of a Scope DIE.
198 DIE *createScopeChildrenDIE(LexicalScope *Scope,
199 SmallVectorImpl<DIE *> &Children,
200 bool *HasNonScopeChildren = nullptr);
202 /// Construct a DIE for this subprogram scope.
203 void constructSubprogramScopeDIE(const DISubprogram *Sub, LexicalScope *Scope);
205 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
207 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
209 /// Construct import_module DIE.
210 DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
212 void finishSubprogramDefinition(const DISubprogram *SP);
213 void finishVariableDefinition(const DbgVariable &Var);
215 /// Find abstract variable associated with Var.
216 using InlinedVariable = DbgValueHistoryMap::InlinedVariable;
217 DbgVariable *getExistingAbstractVariable(InlinedVariable IV,
218 const DILocalVariable *&Cleansed);
219 DbgVariable *getExistingAbstractVariable(InlinedVariable IV);
220 void createAbstractVariable(const DILocalVariable *Var, LexicalScope *Scope);
222 /// Set the skeleton unit associated with this unit.
223 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
225 unsigned getHeaderSize() const override {
226 // DWARF v5 added the DWO ID to the header for split/skeleton units.
228 DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
230 return DwarfUnit::getHeaderSize() + DWOIdSize;
232 unsigned getLength() {
233 return sizeof(uint32_t) + // Length field
234 getHeaderSize() + getUnitDie().getSize();
237 void emitHeader(bool UseOffsets) override;
239 MCSymbol *getLabelBegin() const {
240 assert(getSection());
244 MCSymbol *getMacroLabelBegin() const {
245 return MacroLabelBegin;
248 /// Add a new global name to the compile unit.
249 void addGlobalName(StringRef Name, const DIE &Die,
250 const DIScope *Context) override;
252 /// Add a new global name present in a type unit to this compile unit.
253 void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
255 /// Add a new global type to the compile unit.
256 void addGlobalType(const DIType *Ty, const DIE &Die,
257 const DIScope *Context) override;
259 /// Add a new global type present in a type unit to this compile unit.
260 void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
262 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
263 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
265 /// Add DW_AT_location attribute for a DbgVariable based on provided
267 void addVariableAddress(const DbgVariable &DV, DIE &Die,
268 MachineLocation Location);
269 /// Add an address attribute to a die based on the location provided.
270 void addAddress(DIE &Die, dwarf::Attribute Attribute,
271 const MachineLocation &Location);
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);
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);
285 /// Add a Dwarf expression attribute data and value.
286 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
288 void applySubprogramAttributesToDefinition(const DISubprogram *SP,
291 /// getRangeLists - Get the vector of range lists.
292 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
293 return (Skeleton ? Skeleton : this)->CURangeLists;
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); }
300 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
301 const MCSymbol *getBaseAddress() const { return BaseAddress; }
303 uint64_t getDWOId() const { return DWOId; }
304 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
306 bool hasDwarfPubSections() const;
309 } // end namespace llvm
311 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H