blob: 97a944e9b95b8cbbff7006b9b8c2759db5b84ad2 [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit -----*- C++ -*-===//
David Blaikie37c52312014-10-04 15:49:50 +00002//
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
Hsiangkai Wang2532ac82018-08-17 15:22:04 +000017#include "DbgEntityHistoryCalculator.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000018#include "DwarfDebug.h"
David Blaikie37c52312014-10-04 15:49:50 +000019#include "DwarfUnit.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000020#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"
Zachary Turner264b5d92017-06-07 03:48:56 +000025#include "llvm/BinaryFormat/Dwarf.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000026#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>
David Blaikie37c52312014-10-04 15:49:50 +000034
35namespace llvm {
36
37class AsmPrinter;
David Blaikie37c52312014-10-04 15:49:50 +000038class DwarfFile;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000039class GlobalVariable;
40class MCExpr;
David Blaikie37c52312014-10-04 15:49:50 +000041class MCSymbol;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000042class MDNode;
David Blaikie37c52312014-10-04 15:49:50 +000043
David Blaikiec0bb21f2017-04-22 02:18:00 +000044class DwarfCompileUnit final : public DwarfUnit {
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000045 /// A numeric ID unique among all CUs in the module
46 unsigned UniqueID;
David Blaikiec4af8bf2018-10-20 07:36:39 +000047 bool HasRangeLists = false;
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000048
David Blaikie37c52312014-10-04 15:49:50 +000049 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
50 /// the need to search for it in applyStmtList.
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +000051 DIE::value_iterator StmtListValue;
David Blaikie37c52312014-10-04 15:49:50 +000052
David Blaikie7cbf58a2014-11-01 18:18:07 +000053 /// Skeleton unit associated with this unit.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000054 DwarfCompileUnit *Skeleton = nullptr;
David Blaikie7cbf58a2014-11-01 18:18:07 +000055
David Blaikief4bdc312014-11-02 01:21:40 +000056 /// The start of the unit within its section.
57 MCSymbol *LabelBegin;
58
Amjad Aboud8bbce8a2016-02-01 14:09:41 +000059 /// The start of the unit macro info within macro section.
60 MCSymbol *MacroLabelBegin;
61
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000062 using ImportedEntityList = SmallVector<const MDNode *, 8>;
63 using ImportedEntityMap = DenseMap<const MDNode *, ImportedEntityList>;
Ivan Krasin298639a2015-10-26 21:36:35 +000064
Amjad Aboud72da9392016-04-30 01:44:07 +000065 ImportedEntityMap ImportedEntities;
Ivan Krasin298639a2015-10-26 21:36:35 +000066
David Blaikie192b45c2014-11-02 06:16:39 +000067 /// GlobalNames - A map of globally visible named entities for this unit.
68 StringMap<const DIE *> GlobalNames;
69
70 /// GlobalTypes - A map of globally visible types for this unit.
71 StringMap<const DIE *> GlobalTypes;
72
David Blaikiebc532b42014-11-03 16:40:43 +000073 // List of ranges for a given compile unit.
David Blaikie5b02a192014-11-03 23:10:59 +000074 SmallVector<RangeSpan, 2> CURanges;
David Blaikiebc532b42014-11-03 16:40:43 +000075
David Blaikiece343492014-11-03 21:15:30 +000076 // The base address of this unit, if any. Used for relative references in
77 // ranges/locs.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000078 const MCSymbol *BaseAddress = nullptr;
David Blaikiece343492014-11-03 21:15:30 +000079
David Blaikie488393f2017-05-12 01:13:45 +000080 DenseMap<const MDNode *, DIE *> AbstractSPDies;
Hsiangkai Wang2532ac82018-08-17 15:22:04 +000081 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
David Blaikie488393f2017-05-12 01:13:45 +000082
Paul Robinson543c0e12018-05-22 17:27:31 +000083 /// DWO ID for correlating skeleton and split units.
84 uint64_t DWOId = 0;
85
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000086 /// Construct a DIE for the given DbgVariable without initializing the
David Blaikieee7df552014-10-09 17:56:36 +000087 /// DbgVariable's DIE reference.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +000088 DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
David Blaikieee7df552014-10-09 17:56:36 +000089
David Blaikiecafd9622014-11-02 08:51:37 +000090 bool isDwoUnit() const override;
91
David Blaikie488393f2017-05-12 01:13:45 +000092 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
93 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
94 return AbstractSPDies;
95 return DU->getAbstractSPDies();
96 }
97
Hsiangkai Wang2532ac82018-08-17 15:22:04 +000098 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
David Blaikie488393f2017-05-12 01:13:45 +000099 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000100 return AbstractEntities;
101 return DU->getAbstractEntities();
David Blaikie488393f2017-05-12 01:13:45 +0000102 }
103
David Blaikie37c52312014-10-04 15:49:50 +0000104public:
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000105 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
David Blaikie37c52312014-10-04 15:49:50 +0000106 DwarfDebug *DW, DwarfFile *DWU);
107
David Blaikiec4af8bf2018-10-20 07:36:39 +0000108 bool hasRangeLists() const { return HasRangeLists; }
Peter Collingbourne7c384cc2016-02-11 19:57:46 +0000109 unsigned getUniqueID() const { return UniqueID; }
Peter Collingbourne7c384cc2016-02-11 19:57:46 +0000110
David Blaikiea33cd6a2014-11-01 00:50:34 +0000111 DwarfCompileUnit *getSkeleton() const {
David Blaikiea5437b62014-11-01 19:26:05 +0000112 return Skeleton;
David Blaikiea33cd6a2014-11-01 00:50:34 +0000113 }
114
David Blaikieb3cee2f2017-05-25 18:50:28 +0000115 bool includeMinimalInlineScopes() const;
116
Rafael Espindola063d7252015-03-10 16:58:10 +0000117 void initStmtList();
David Blaikie37c52312014-10-04 15:49:50 +0000118
119 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
120 void applyStmtList(DIE &D);
121
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000122 /// A pair of GlobalVariable and DIExpression.
123 struct GlobalExpr {
124 const GlobalVariable *Var;
125 const DIExpression *Expr;
126 };
127
128 /// Get or create global variable DIE.
129 DIE *
130 getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV,
131 ArrayRef<GlobalExpr> GlobalExprs);
David Blaikie37c52312014-10-04 15:49:50 +0000132
133 /// addLabelAddress - Add a dwarf label attribute data and value using
134 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
135 void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
136 const MCSymbol *Label);
137
138 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
139 /// DW_FORM_addr only.
140 void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
141 const MCSymbol *Label);
142
143 DwarfCompileUnit &getCU() override { return *this; }
144
Paul Robinson612e89d2018-01-12 19:17:50 +0000145 unsigned getOrCreateSourceID(const DIFile *File) override;
David Blaikie37c52312014-10-04 15:49:50 +0000146
Amjad Aboud72da9392016-04-30 01:44:07 +0000147 void addImportedEntity(const DIImportedEntity* IE) {
148 DIScope *Scope = IE->getScope();
Amjad Abouda5ba9912016-04-21 16:58:49 +0000149 assert(Scope && "Invalid Scope encoding!");
150 if (!isa<DILocalScope>(Scope))
151 // No need to add imported enities that are not local declaration.
152 return;
153
154 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
Amjad Aboud72da9392016-04-30 01:44:07 +0000155 ImportedEntities[LocalScope].push_back(IE);
Ivan Krasin298639a2015-10-26 21:36:35 +0000156 }
157
David Blaikie37c52312014-10-04 15:49:50 +0000158 /// addRange - Add an address range to the list of ranges for this unit.
159 void addRange(RangeSpan Range);
David Blaikie14499a72014-10-04 15:58:47 +0000160
161 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
David Blaikiecda2aa82014-10-04 16:24:00 +0000162
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000163 /// Find DIE for the given subprogram and attach appropriate
David Blaikiecda2aa82014-10-04 16:24:00 +0000164 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
165 /// variables in this scope then create and insert DIEs for these
166 /// variables.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000167 DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
David Blaikie9c65b132014-10-08 22:20:02 +0000168
169 void constructScopeDIE(LexicalScope *Scope,
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000170 SmallVectorImpl<DIE *> &FinalChildren);
David Blaikie52400202014-10-09 00:11:39 +0000171
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000172 /// A helper function to construct a RangeSpanList for a given
David Blaikie52400202014-10-09 00:11:39 +0000173 /// lexical scope.
David Blaikie5b02a192014-11-03 23:10:59 +0000174 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
175
176 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
David Blaikiede123752014-10-09 00:21:42 +0000177
178 void attachRangesOrLowHighPC(DIE &D,
179 const SmallVectorImpl<InsnRange> &Ranges);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000180
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000181 /// This scope represents inlined body of a function. Construct
David Blaikie01b48a82014-10-09 16:50:53 +0000182 /// DIE to represent this concrete inlined copy of the function.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000183 DIE *constructInlinedScopeDIE(LexicalScope *Scope);
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000184
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000185 /// Construct new DW_TAG_lexical_block for this scope and
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000186 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000187 DIE *constructLexicalScopeDIE(LexicalScope *Scope);
David Blaikieee7df552014-10-09 17:56:36 +0000188
189 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000190 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
David Blaikie4a1a44e2014-10-09 17:56:39 +0000191
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000192 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
193 DIE *&ObjectPointer);
David Blaikie8b2fdb82014-10-09 18:24:28 +0000194
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000195 /// Construct a DIE for the given DbgLabel.
196 DIE *constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope);
197
David Blaikie8b2fdb82014-10-09 18:24:28 +0000198 /// A helper function to create children of a Scope DIE.
199 DIE *createScopeChildrenDIE(LexicalScope *Scope,
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000200 SmallVectorImpl<DIE *> &Children,
David Blaikie2195e132017-07-27 00:06:53 +0000201 bool *HasNonScopeChildren = nullptr);
David Blaikie1d072342014-10-09 20:21:36 +0000202
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000203 /// Construct a DIE for this subprogram scope.
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000204 DIE &constructSubprogramScopeDIE(const DISubprogram *Sub,
205 LexicalScope *Scope);
David Blaikie78b65b62014-10-09 20:26:15 +0000206
207 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
David Blaikie58410f22014-10-10 06:39:26 +0000208
David Blaikie49be5b32014-10-31 21:57:02 +0000209 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
David Blaikie4191cbc2014-10-10 06:39:29 +0000210
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000211 /// Construct a call site entry DIE describing a call within \p Scope to a
212 /// callee described by \p CalleeSP. \p IsTail specifies whether the call is
213 /// a tail call. \p ReturnPC must be non-null for non-tail calls and point
214 /// to the PC value after the call returns.
215 DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram &CalleeSP,
216 bool IsTail, const MCSymbol *ReturnPC);
217
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000218 /// Construct import_module DIE.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000219 DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
Frederic Riss987fe222014-10-24 21:31:09 +0000220
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000221 void finishSubprogramDefinition(const DISubprogram *SP);
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000222 void finishEntityDefinition(const DbgEntity *Entity);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000223
David Blaikie488393f2017-05-12 01:13:45 +0000224 /// Find abstract variable associated with Var.
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000225 using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000226 DbgEntity *getExistingAbstractEntity(const DINode *Node);
227 void createAbstractEntity(const DINode *Node, LexicalScope *Scope);
David Blaikie1d96cc22014-10-31 22:30:30 +0000228
David Blaikie7cbf58a2014-11-01 18:18:07 +0000229 /// Set the skeleton unit associated with this unit.
David Blaikiea5437b62014-11-01 19:26:05 +0000230 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
David Blaikiea34568b2014-11-01 20:06:28 +0000231
Paul Robinson543c0e12018-05-22 17:27:31 +0000232 unsigned getHeaderSize() const override {
233 // DWARF v5 added the DWO ID to the header for split/skeleton units.
234 unsigned DWOIdSize =
235 DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
236 : 0;
237 return DwarfUnit::getHeaderSize() + DWOIdSize;
238 }
David Blaikie983bfea2014-11-01 23:07:14 +0000239 unsigned getLength() {
240 return sizeof(uint32_t) + // Length field
Greg Clayton35630c32016-12-01 18:56:29 +0000241 getHeaderSize() + getUnitDie().getSize();
David Blaikie983bfea2014-11-01 23:07:14 +0000242 }
David Blaikieae57e662014-11-01 23:59:23 +0000243
Rafael Espindola063d7252015-03-10 16:58:10 +0000244 void emitHeader(bool UseOffsets) override;
David Blaikief4bdc312014-11-02 01:21:40 +0000245
246 MCSymbol *getLabelBegin() const {
David Blaikiee40caae2016-12-01 21:59:09 +0000247 assert(getSection());
David Blaikief4bdc312014-11-02 01:21:40 +0000248 return LabelBegin;
249 }
David Blaikie192b45c2014-11-02 06:16:39 +0000250
Amjad Aboud8bbce8a2016-02-01 14:09:41 +0000251 MCSymbol *getMacroLabelBegin() const {
252 return MacroLabelBegin;
253 }
254
David Blaikie192b45c2014-11-02 06:16:39 +0000255 /// Add a new global name to the compile unit.
David Blaikiea0e3c752017-02-03 00:44:18 +0000256 void addGlobalName(StringRef Name, const DIE &Die,
257 const DIScope *Context) override;
258
259 /// Add a new global name present in a type unit to this compile unit.
260 void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
David Blaikie192b45c2014-11-02 06:16:39 +0000261
262 /// Add a new global type to the compile unit.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000263 void addGlobalType(const DIType *Ty, const DIE &Die,
264 const DIScope *Context) override;
David Blaikie192b45c2014-11-02 06:16:39 +0000265
David Blaikiea0e3c752017-02-03 00:44:18 +0000266 /// Add a new global type present in a type unit to this compile unit.
267 void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
268
David Blaikie192b45c2014-11-02 06:16:39 +0000269 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
270 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
David Blaikie7d48be22014-11-02 06:37:23 +0000271
272 /// Add DW_AT_location attribute for a DbgVariable based on provided
273 /// MachineLocation.
274 void addVariableAddress(const DbgVariable &DV, DIE &Die,
275 MachineLocation Location);
David Blaikief7435ee2014-11-02 06:46:40 +0000276 /// Add an address attribute to a die based on the location provided.
277 void addAddress(DIE &Die, dwarf::Attribute Attribute,
Adrian Prantl5883af32015-01-19 17:57:29 +0000278 const MachineLocation &Location);
David Blaikie77895fb2014-11-02 06:58:44 +0000279
280 /// Start with the address based on the location provided, and generate the
281 /// DWARF information necessary to find the actual variable (navigating the
282 /// extra location information encoded in the type) based on the starting
283 /// location. Add the DWARF information to the die.
284 void addComplexAddress(const DbgVariable &DV, DIE &Die,
285 dwarf::Attribute Attribute,
286 const MachineLocation &Location);
David Blaikie4bc08812014-11-02 07:03:19 +0000287
288 /// Add a Dwarf loclistptr attribute data and value.
289 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
David Blaikie02a63332014-11-02 07:06:51 +0000290 void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
David Blaikie97802082014-11-02 07:11:55 +0000291
292 /// Add a Dwarf expression attribute data and value.
293 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
David Blaikie3363a572014-11-02 08:09:09 +0000294
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000295 void applySubprogramAttributesToDefinition(const DISubprogram *SP,
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000296 DIE &SPDie);
David Blaikie89a26f02014-11-03 02:41:49 +0000297
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000298 void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie);
299
David Blaikiebc532b42014-11-03 16:40:43 +0000300 /// getRanges - Get the list of ranges for this unit.
301 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
David Blaikie5b02a192014-11-03 23:10:59 +0000302 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
David Blaikiece343492014-11-03 21:15:30 +0000303
304 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
305 const MCSymbol *getBaseAddress() const { return BaseAddress; }
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000306
Paul Robinson543c0e12018-05-22 17:27:31 +0000307 uint64_t getDWOId() const { return DWOId; }
308 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
309
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000310 bool hasDwarfPubSections() const;
David Blaikie37c52312014-10-04 15:49:50 +0000311};
312
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000313} // end namespace llvm
David Blaikie37c52312014-10-04 15:49:50 +0000314
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000315#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H