Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / CodeGen / AsmPrinter / DwarfCompileUnit.h
blobc5ec49503c6bca83d743c4df51dcb81112136adb
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
14 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
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"
23 #include "llvm/BinaryFormat/Dwarf.h"
24 #include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
25 #include "llvm/CodeGen/DIE.h"
26 #include "llvm/CodeGen/LexicalScopes.h"
27 #include "llvm/IR/DebugInfoMetadata.h"
28 #include "llvm/Support/Casting.h"
29 #include <algorithm>
30 #include <cassert>
31 #include <cstdint>
32 #include <memory>
34 namespace llvm {
36 class AsmPrinter;
37 class DwarfFile;
38 class GlobalVariable;
39 class MCExpr;
40 class MCSymbol;
41 class MDNode;
43 class DwarfCompileUnit final : public DwarfUnit {
44 /// A numeric ID unique among all CUs in the module
45 unsigned UniqueID;
46 bool HasRangeLists = false;
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.
56 MCSymbol *LabelBegin;
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 ranges for a given compile unit.
73 SmallVector<RangeSpan, 2> CURanges;
75 // The base address of this unit, if any. Used for relative references in
76 // ranges/locs.
77 const MCSymbol *BaseAddress = nullptr;
79 DenseMap<const MDNode *, DIE *> AbstractSPDies;
80 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
82 /// DWO ID for correlating skeleton and split units.
83 uint64_t DWOId = 0;
85 /// Construct a DIE for the given DbgVariable without initializing the
86 /// DbgVariable's DIE reference.
87 DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
89 bool isDwoUnit() const override;
91 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
92 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
93 return AbstractSPDies;
94 return DU->getAbstractSPDies();
97 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
98 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
99 return AbstractEntities;
100 return DU->getAbstractEntities();
103 public:
104 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
105 DwarfDebug *DW, DwarfFile *DWU);
107 bool hasRangeLists() const { return HasRangeLists; }
108 unsigned getUniqueID() const { return UniqueID; }
110 DwarfCompileUnit *getSkeleton() const {
111 return Skeleton;
114 bool includeMinimalInlineScopes() const;
116 void initStmtList();
118 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
119 void applyStmtList(DIE &D);
121 /// A pair of GlobalVariable and DIExpression.
122 struct GlobalExpr {
123 const GlobalVariable *Var;
124 const DIExpression *Expr;
127 /// Get or create global variable DIE.
128 DIE *
129 getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV,
130 ArrayRef<GlobalExpr> GlobalExprs);
132 /// addLabelAddress - Add a dwarf label attribute data and value using
133 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
134 void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
135 const MCSymbol *Label);
137 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
138 /// DW_FORM_addr only.
139 void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
140 const MCSymbol *Label);
142 DwarfCompileUnit &getCU() override { return *this; }
144 unsigned getOrCreateSourceID(const DIFile *File) override;
146 void addImportedEntity(const DIImportedEntity* IE) {
147 DIScope *Scope = IE->getScope();
148 assert(Scope && "Invalid Scope encoding!");
149 if (!isa<DILocalScope>(Scope))
150 // No need to add imported enities that are not local declaration.
151 return;
153 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
154 ImportedEntities[LocalScope].push_back(IE);
157 /// addRange - Add an address range to the list of ranges for this unit.
158 void addRange(RangeSpan Range);
160 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
162 /// Find DIE for the given subprogram and attach appropriate
163 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
164 /// variables in this scope then create and insert DIEs for these
165 /// variables.
166 DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
168 void constructScopeDIE(LexicalScope *Scope,
169 SmallVectorImpl<DIE *> &FinalChildren);
171 /// A helper function to construct a RangeSpanList for a given
172 /// lexical scope.
173 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
175 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
177 void attachRangesOrLowHighPC(DIE &D,
178 const SmallVectorImpl<InsnRange> &Ranges);
180 /// This scope represents inlined body of a function. Construct
181 /// DIE to represent this concrete inlined copy of the function.
182 DIE *constructInlinedScopeDIE(LexicalScope *Scope);
184 /// Construct new DW_TAG_lexical_block for this scope and
185 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
186 DIE *constructLexicalScopeDIE(LexicalScope *Scope);
188 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
189 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
191 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
192 DIE *&ObjectPointer);
194 /// Construct a DIE for the given DbgLabel.
195 DIE *constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope);
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 DIE &constructSubprogramScopeDIE(const DISubprogram *Sub,
204 LexicalScope *Scope);
206 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
208 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
210 /// Construct a call site entry DIE describing a call within \p Scope to a
211 /// callee described by \p CalleeSP. \p IsTail specifies whether the call is
212 /// a tail call. \p PCOffset must be non-zero for non-tail calls or be the
213 /// function-local offset to PC value after the call instruction.
214 DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram &CalleeSP,
215 bool IsTail, const MCExpr *PCOffset);
217 /// Construct import_module DIE.
218 DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
220 void finishSubprogramDefinition(const DISubprogram *SP);
221 void finishEntityDefinition(const DbgEntity *Entity);
223 /// Find abstract variable associated with Var.
224 using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
225 DbgEntity *getExistingAbstractEntity(const DINode *Node);
226 void createAbstractEntity(const DINode *Node, LexicalScope *Scope);
228 /// Set the skeleton unit associated with this unit.
229 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
231 unsigned getHeaderSize() const override {
232 // DWARF v5 added the DWO ID to the header for split/skeleton units.
233 unsigned DWOIdSize =
234 DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
235 : 0;
236 return DwarfUnit::getHeaderSize() + DWOIdSize;
238 unsigned getLength() {
239 return sizeof(uint32_t) + // Length field
240 getHeaderSize() + getUnitDie().getSize();
243 void emitHeader(bool UseOffsets) override;
245 /// Add the DW_AT_addr_base attribute to the unit DIE.
246 void addAddrTableBase();
248 MCSymbol *getLabelBegin() const {
249 assert(getSection());
250 return LabelBegin;
253 MCSymbol *getMacroLabelBegin() const {
254 return MacroLabelBegin;
257 /// Add a new global name to the compile unit.
258 void addGlobalName(StringRef Name, const DIE &Die,
259 const DIScope *Context) override;
261 /// Add a new global name present in a type unit to this compile unit.
262 void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
264 /// Add a new global type to the compile unit.
265 void addGlobalType(const DIType *Ty, const DIE &Die,
266 const DIScope *Context) override;
268 /// Add a new global type present in a type unit to this compile unit.
269 void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
271 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
272 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
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);
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);
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);
294 /// Add a Dwarf expression attribute data and value.
295 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
297 /// Add an attribute containing an address expression to \p Die.
298 void addAddressExpr(DIE &Die, dwarf::Attribute Attribute, const MCExpr *Expr);
300 void applySubprogramAttributesToDefinition(const DISubprogram *SP,
301 DIE &SPDie);
303 void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie);
305 /// getRanges - Get the list of ranges for this unit.
306 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
307 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
309 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
310 const MCSymbol *getBaseAddress() const { return BaseAddress; }
312 uint64_t getDWOId() const { return DWOId; }
313 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
315 bool hasDwarfPubSections() const;
318 } // end namespace llvm
320 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H