Line data Source code
1 : //===- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit -----*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This file contains support for writing dwarf compile unit.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15 : #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
16 :
17 : #include "DbgEntityHistoryCalculator.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"
30 : #include <algorithm>
31 : #include <cassert>
32 : #include <cstdint>
33 : #include <memory>
34 :
35 : namespace llvm {
36 :
37 : class AsmPrinter;
38 : class DwarfFile;
39 : class GlobalVariable;
40 : class MCExpr;
41 : class MCSymbol;
42 : class MDNode;
43 :
44 : class DwarfCompileUnit final : public DwarfUnit {
45 : /// A numeric ID unique among all CUs in the module
46 : unsigned UniqueID;
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 :
61 : using ImportedEntityList = SmallVector<const MDNode *, 8>;
62 : using ImportedEntityMap = DenseMap<const MDNode *, ImportedEntityList>;
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 range lists for a given compile unit, separate from the ranges for
73 : // the CU itself.
74 : SmallVector<RangeSpanList, 1> CURangeLists;
75 :
76 : // List of ranges for a given compile unit.
77 : SmallVector<RangeSpan, 2> CURanges;
78 :
79 : // The base address of this unit, if any. Used for relative references in
80 : // ranges/locs.
81 : const MCSymbol *BaseAddress = nullptr;
82 :
83 : DenseMap<const MDNode *, DIE *> AbstractSPDies;
84 : DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
85 :
86 : /// DWO ID for correlating skeleton and split units.
87 : uint64_t DWOId = 0;
88 :
89 : /// Construct a DIE for the given DbgVariable without initializing the
90 : /// DbgVariable's DIE reference.
91 : DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
92 :
93 : bool isDwoUnit() const override;
94 :
95 229302 : DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
96 229302 : if (isDwoUnit() && !DD->shareAcrossDWOCUs())
97 54 : return AbstractSPDies;
98 458496 : return DU->getAbstractSPDies();
99 : }
100 :
101 185298 : DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
102 185298 : if (isDwoUnit() && !DD->shareAcrossDWOCUs())
103 80 : return AbstractEntities;
104 370436 : return DU->getAbstractEntities();
105 : }
106 :
107 : public:
108 : DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
109 : DwarfDebug *DW, DwarfFile *DWU);
110 :
111 0 : unsigned getUniqueID() const { return UniqueID; }
112 :
113 0 : DwarfCompileUnit *getSkeleton() const {
114 0 : return Skeleton;
115 : }
116 :
117 : bool includeMinimalInlineScopes() const;
118 :
119 : void initStmtList();
120 :
121 : /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
122 : void applyStmtList(DIE &D);
123 :
124 : /// A pair of GlobalVariable and DIExpression.
125 : struct GlobalExpr {
126 : const GlobalVariable *Var;
127 : const DIExpression *Expr;
128 : };
129 :
130 : /// Get or create global variable DIE.
131 : DIE *
132 : getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV,
133 : ArrayRef<GlobalExpr> GlobalExprs);
134 :
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);
139 :
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);
144 :
145 39 : DwarfCompileUnit &getCU() override { return *this; }
146 :
147 : unsigned getOrCreateSourceID(const DIFile *File) override;
148 :
149 16639 : 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.
154 : return;
155 :
156 25 : auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
157 25 : ImportedEntities[LocalScope].push_back(IE);
158 : }
159 :
160 : /// addRange - Add an address range to the list of ranges for this unit.
161 : void addRange(RangeSpan Range);
162 :
163 : void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
164 :
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
168 : /// variables.
169 : DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
170 :
171 : void constructScopeDIE(LexicalScope *Scope,
172 : SmallVectorImpl<DIE *> &FinalChildren);
173 :
174 : /// A helper function to construct a RangeSpanList for a given
175 : /// lexical scope.
176 : void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
177 :
178 : void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
179 :
180 : void attachRangesOrLowHighPC(DIE &D,
181 : const SmallVectorImpl<InsnRange> &Ranges);
182 :
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);
186 :
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);
190 :
191 : /// constructVariableDIE - Construct a DIE for the given DbgVariable.
192 : DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
193 :
194 : DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
195 : DIE *&ObjectPointer);
196 :
197 : /// Construct a DIE for the given DbgLabel.
198 : DIE *constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope);
199 :
200 : /// A helper function to create children of a Scope DIE.
201 : DIE *createScopeChildrenDIE(LexicalScope *Scope,
202 : SmallVectorImpl<DIE *> &Children,
203 : bool *HasNonScopeChildren = nullptr);
204 :
205 : /// Construct a DIE for this subprogram scope.
206 : DIE &constructSubprogramScopeDIE(const DISubprogram *Sub,
207 : LexicalScope *Scope);
208 :
209 : DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
210 :
211 : void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
212 :
213 : /// Construct a call site entry DIE describing a call within \p Scope to a
214 : /// callee described by \p CalleeSP. \p IsTail specifies whether the call is
215 : /// a tail call. \p ReturnPC must be non-null for non-tail calls and point
216 : /// to the PC value after the call returns.
217 : DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram &CalleeSP,
218 : bool IsTail, const MCSymbol *ReturnPC);
219 :
220 : /// Construct import_module DIE.
221 : DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
222 :
223 : void finishSubprogramDefinition(const DISubprogram *SP);
224 : void finishEntityDefinition(const DbgEntity *Entity);
225 :
226 : /// Find abstract variable associated with Var.
227 : using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
228 : DbgEntity *getExistingAbstractEntity(const DINode *Node);
229 : void createAbstractEntity(const DINode *Node, LexicalScope *Scope);
230 :
231 : /// Set the skeleton unit associated with this unit.
232 43 : void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
233 :
234 4367 : unsigned getHeaderSize() const override {
235 : // DWARF v5 added the DWO ID to the header for split/skeleton units.
236 : unsigned DWOIdSize =
237 4547 : DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
238 4368 : : 0;
239 4368 : return DwarfUnit::getHeaderSize() + DWOIdSize;
240 : }
241 : unsigned getLength() {
242 : return sizeof(uint32_t) + // Length field
243 1598 : getHeaderSize() + getUnitDie().getSize();
244 : }
245 :
246 : void emitHeader(bool UseOffsets) override;
247 :
248 0 : MCSymbol *getLabelBegin() const {
249 : assert(getSection());
250 0 : return LabelBegin;
251 : }
252 :
253 0 : MCSymbol *getMacroLabelBegin() const {
254 0 : 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 799 : const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
272 799 : 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.
279 : void addAddress(DIE &Die, dwarf::Attribute Attribute,
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 : void applySubprogramAttributesToDefinition(const DISubprogram *SP,
298 : DIE &SPDie);
299 :
300 : void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie);
301 :
302 : /// getRangeLists - Get the vector of range lists.
303 : const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
304 223 : return (Skeleton ? Skeleton : this)->CURangeLists;
305 : }
306 :
307 : /// getRanges - Get the list of ranges for this unit.
308 : const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
309 : SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
310 :
311 751 : void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
312 0 : const MCSymbol *getBaseAddress() const { return BaseAddress; }
313 :
314 0 : uint64_t getDWOId() const { return DWOId; }
315 8 : void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
316 :
317 : bool hasDwarfPubSections() const;
318 : };
319 :
320 : } // end namespace llvm
321 :
322 : #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
|