blob: 0f3d8c689fa0303679176d4a655f52d8433cd500 [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Units ------------===//
Paul Robinson34435752017-03-01 00:14:42 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Paul Robinson34435752017-03-01 00:14:42 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for constructing a dwarf compile unit.
10//
11//===----------------------------------------------------------------------===//
12
David Blaikie37c52312014-10-04 15:49:50 +000013#include "DwarfCompileUnit.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000014#include "AddressPool.h"
15#include "DwarfDebug.h"
Adrian Prantl7813d9c2015-01-14 01:01:30 +000016#include "DwarfExpression.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000017#include "DwarfUnit.h"
18#include "llvm/ADT/None.h"
19#include "llvm/ADT/STLExtras.h"
Markus Lavinb86ce212019-03-19 13:16:28 +000020#include "llvm/ADT/SmallString.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/BinaryFormat/Dwarf.h"
24#include "llvm/CodeGen/AsmPrinter.h"
25#include "llvm/CodeGen/DIE.h"
26#include "llvm/CodeGen/LexicalScopes.h"
David Blaikiecda2aa82014-10-04 16:24:00 +000027#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000028#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/CodeGen/MachineOperand.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000030#include "llvm/CodeGen/TargetFrameLowering.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000031#include "llvm/CodeGen/TargetRegisterInfo.h"
32#include "llvm/CodeGen/TargetSubtargetInfo.h"
David Blaikie37c52312014-10-04 15:49:50 +000033#include "llvm/IR/DataLayout.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000034#include "llvm/IR/DebugInfo.h"
35#include "llvm/IR/DebugInfoMetadata.h"
David Blaikie37c52312014-10-04 15:49:50 +000036#include "llvm/IR/GlobalVariable.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000037#include "llvm/MC/MCSection.h"
David Blaikie37c52312014-10-04 15:49:50 +000038#include "llvm/MC/MCStreamer.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000039#include "llvm/MC/MCSymbol.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000040#include "llvm/MC/MachineLocation.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000041#include "llvm/Support/Casting.h"
David Blaikie6054e652018-03-23 23:58:19 +000042#include "llvm/Target/TargetLoweringObjectFile.h"
David Blaikiecda2aa82014-10-04 16:24:00 +000043#include "llvm/Target/TargetMachine.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000044#include "llvm/Target/TargetOptions.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000045#include <algorithm>
46#include <cassert>
47#include <cstdint>
48#include <iterator>
49#include <memory>
50#include <string>
51#include <utility>
David Blaikie37c52312014-10-04 15:49:50 +000052
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000053using namespace llvm;
David Blaikie37c52312014-10-04 15:49:50 +000054
Alexey Lapshin789e2572019-11-30 23:48:32 +030055static dwarf::Tag GetCompileUnitType(UnitKind Kind, DwarfDebug *DW) {
56
57 // According to DWARF Debugging Information Format Version 5,
58 // 3.1.2 Skeleton Compilation Unit Entries:
59 // "When generating a split DWARF object file (see Section 7.3.2
60 // on page 187), the compilation unit in the .debug_info section
61 // is a "skeleton" compilation unit with the tag DW_TAG_skeleton_unit"
62 if (DW->getDwarfVersion() >= 5 && Kind == UnitKind::Skeleton)
63 return dwarf::DW_TAG_skeleton_unit;
64
65 return dwarf::DW_TAG_compile_unit;
66}
67
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000068DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node,
David Blaikie37c52312014-10-04 15:49:50 +000069 AsmPrinter *A, DwarfDebug *DW,
Alexey Lapshin789e2572019-11-30 23:48:32 +030070 DwarfFile *DWU, UnitKind Kind)
71 : DwarfUnit(GetCompileUnitType(Kind, DW), Node, A, DW, DWU), UniqueID(UID) {
David Blaikie37c52312014-10-04 15:49:50 +000072 insertDIE(Node, &getUnitDie());
Amjad Aboud8bbce8a2016-02-01 14:09:41 +000073 MacroLabelBegin = Asm->createTempSymbol("cu_macro_begin");
David Blaikie37c52312014-10-04 15:49:50 +000074}
75
76/// addLabelAddress - Add a dwarf label attribute data and value using
77/// DW_FORM_addr or DW_FORM_GNU_addr_index.
David Blaikie37c52312014-10-04 15:49:50 +000078void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
79 const MCSymbol *Label) {
David Blaikie37c52312014-10-04 15:49:50 +000080 // Don't use the address pool in non-fission or in the skeleton unit itself.
81 // FIXME: Once GDB supports this, it's probably worthwhile using the address
82 // pool from the skeleton - maybe even in non-fission (possibly fewer
83 // relocations by sharing them in the pool, but we have other ideas about how
84 // to reduce the number of relocations as well/instead).
David Blaikie161dd3c2018-10-20 06:02:15 +000085 if ((!DD->useSplitDwarf() || !Skeleton) && DD->getDwarfVersion() < 5)
David Blaikie37c52312014-10-04 15:49:50 +000086 return addLocalLabelAddress(Die, Attribute, Label);
87
88 if (Label)
89 DD->addArangeLabel(SymbolCU(this, Label));
90
91 unsigned idx = DD->getAddressPool().getIndex(Label);
David Blaikie161dd3c2018-10-20 06:02:15 +000092 Die.addValue(DIEValueAllocator, Attribute,
93 DD->getDwarfVersion() >= 5 ? dwarf::DW_FORM_addrx
94 : dwarf::DW_FORM_GNU_addr_index,
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +000095 DIEInteger(idx));
David Blaikie37c52312014-10-04 15:49:50 +000096}
97
98void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
99 dwarf::Attribute Attribute,
100 const MCSymbol *Label) {
101 if (Label)
102 DD->addArangeLabel(SymbolCU(this, Label));
103
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000104 if (Label)
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000105 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr,
106 DIELabel(Label));
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000107 else
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000108 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr,
109 DIEInteger(0));
David Blaikie37c52312014-10-04 15:49:50 +0000110}
111
Paul Robinson612e89d2018-01-12 19:17:50 +0000112unsigned DwarfCompileUnit::getOrCreateSourceID(const DIFile *File) {
David Blaikie37c52312014-10-04 15:49:50 +0000113 // If we print assembly, we can't separate .file entries according to
114 // compile units. Thus all files will belong to the default compile unit.
115
116 // FIXME: add a better feature test than hasRawTextSupport. Even better,
117 // extend .file to support this.
Paul Robinson612e89d2018-01-12 19:17:50 +0000118 unsigned CUID = Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID();
119 if (!File)
Eric Christopher798e83b2019-04-04 23:34:38 +0000120 return Asm->OutStreamer->EmitDwarfFileDirective(0, "", "", None, None, CUID);
Lang Hames9ff69c82015-04-24 19:11:51 +0000121 return Asm->OutStreamer->EmitDwarfFileDirective(
Scott Linder16c7bda2018-02-23 23:01:06 +0000122 0, File->getDirectory(), File->getFilename(), getMD5AsBytes(File),
123 File->getSource(), CUID);
David Blaikie37c52312014-10-04 15:49:50 +0000124}
125
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000126DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000127 const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
David Blaikie37c52312014-10-04 15:49:50 +0000128 // Check for pre-existence.
129 if (DIE *Die = getDIE(GV))
130 return Die;
131
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000132 assert(GV);
David Blaikie37c52312014-10-04 15:49:50 +0000133
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000134 auto *GVContext = GV->getScope();
Fangrui Songda82ce92019-05-07 02:06:37 +0000135 const DIType *GTy = GV->getType();
David Blaikie37c52312014-10-04 15:49:50 +0000136
David Blaikie49cfc8c2014-10-23 19:12:43 +0000137 // Construct the context before querying for the existence of the DIE in
138 // case such construction creates the DIE.
Adrian Prantl6ed57062019-04-08 19:13:55 +0000139 auto *CB = GVContext ? dyn_cast<DICommonBlock>(GVContext) : nullptr;
140 DIE *ContextDIE = CB ? getOrCreateCommonBlock(CB, GlobalExprs)
141 : getOrCreateContextDIE(GVContext);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000142
Amjad Aboud72da9392016-04-30 01:44:07 +0000143 // Add to map.
144 DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000145 DIScope *DeclContext;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000146 if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) {
Fangrui Songda82ce92019-05-07 02:06:37 +0000147 DeclContext = SDMDecl->getScope();
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000148 assert(SDMDecl->isStaticMember() && "Expected static member decl");
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000149 assert(GV->isDefinition());
David Blaikie37c52312014-10-04 15:49:50 +0000150 // We need the declaration DIE that is in the static member's class.
David Blaikie49cfc8c2014-10-23 19:12:43 +0000151 DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl);
152 addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE);
Adrian Prantl3502f202016-11-08 22:11:38 +0000153 // If the global variable's type is different from the one in the class
154 // member type, assume that it's more specific and also emit it.
Fangrui Songda82ce92019-05-07 02:06:37 +0000155 if (GTy != SDMDecl->getBaseType())
Adrian Prantl3502f202016-11-08 22:11:38 +0000156 addType(*VariableDIE, GTy);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000157 } else {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000158 DeclContext = GV->getScope();
David Blaikie37c52312014-10-04 15:49:50 +0000159 // Add name and type.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000160 addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName());
David Blaikie37c52312014-10-04 15:49:50 +0000161 addType(*VariableDIE, GTy);
162
163 // Add scoping info.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000164 if (!GV->isLocalToUnit())
David Blaikie37c52312014-10-04 15:49:50 +0000165 addFlag(*VariableDIE, dwarf::DW_AT_external);
166
167 // Add line number info.
168 addSourceLine(*VariableDIE, GV);
169 }
170
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000171 if (!GV->isDefinition())
David Blaikie49cfc8c2014-10-23 19:12:43 +0000172 addFlag(*VariableDIE, dwarf::DW_AT_declaration);
David Blaikie877354a2015-04-14 18:08:25 +0000173 else
174 addGlobalName(GV->getName(), *VariableDIE, DeclContext);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000175
Victor Leschuk3c989982016-10-26 11:59:03 +0000176 if (uint32_t AlignInBytes = GV->getAlignInBytes())
177 addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
178 AlignInBytes);
179
Matthew Vossf8ab35a2018-10-03 18:44:53 +0000180 if (MDTuple *TP = GV->getTemplateParams())
181 addTemplateParams(*VariableDIE, DINodeArray(TP));
182
David Blaikie37c52312014-10-04 15:49:50 +0000183 // Add location.
Adrian Prantl6ed57062019-04-08 19:13:55 +0000184 addLocationAttribute(VariableDIE, GV, GlobalExprs);
185
186 return VariableDIE;
187}
188
189void DwarfCompileUnit::addLocationAttribute(
190 DIE *VariableDIE, const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
David Blaikie37c52312014-10-04 15:49:50 +0000191 bool addToAccelTable = false;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000192 DIELoc *Loc = nullptr;
Alexey Bataevf3a91502019-02-05 19:33:47 +0000193 Optional<unsigned> NVPTXAddressSpace;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000194 std::unique_ptr<DIEDwarfExpression> DwarfExpr;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000195 for (const auto &GE : GlobalExprs) {
196 const GlobalVariable *Global = GE.Var;
197 const DIExpression *Expr = GE.Expr;
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000198
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000199 // For compatibility with DWARF 3 and earlier,
200 // DW_AT_location(DW_OP_constu, X, DW_OP_stack_value) becomes
201 // DW_AT_const_value(X).
202 if (GlobalExprs.size() == 1 && Expr && Expr->isConstant()) {
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000203 addToAccelTable = true;
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000204 addConstantValue(*VariableDIE, /*Unsigned=*/true, Expr->getElement(1));
205 break;
206 }
207
208 // We cannot describe the location of dllimport'd variables: the
209 // computation of their address requires loads from the IAT.
210 if (Global && Global->hasDLLImportStorageClass())
211 continue;
212
213 // Nothing to describe without address or constant.
214 if (!Global && (!Expr || !Expr->isConstant()))
215 continue;
216
Lei Liu8e422b82018-08-01 23:46:49 +0000217 if (Global && Global->isThreadLocal() &&
218 !Asm->getObjFileLowering().supportDebugThreadLocalLocation())
219 continue;
220
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000221 if (!Loc) {
222 addToAccelTable = true;
223 Loc = new (DIEValueAllocator) DIELoc;
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000224 DwarfExpr = std::make_unique<DIEDwarfExpression>(*Asm, *this, *Loc);
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000225 }
226
Alexey Bataevf3a91502019-02-05 19:33:47 +0000227 if (Expr) {
228 // According to
229 // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
230 // cuda-gdb requires DW_AT_address_class for all variables to be able to
231 // correctly interpret address space of the variable address.
232 // Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef
233 // sequence for the NVPTX + gdb target.
234 unsigned LocalNVPTXAddressSpace;
235 if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
236 const DIExpression *NewExpr =
237 DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace);
238 if (NewExpr != Expr) {
239 Expr = NewExpr;
240 NVPTXAddressSpace = LocalNVPTXAddressSpace;
241 }
242 }
Mikael Holmena9e31532018-01-29 12:37:30 +0000243 DwarfExpr->addFragmentOffset(Expr);
Alexey Bataevf3a91502019-02-05 19:33:47 +0000244 }
Mikael Holmena9e31532018-01-29 12:37:30 +0000245
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000246 if (Global) {
247 const MCSymbol *Sym = Asm->getSymbol(Global);
248 if (Global->isThreadLocal()) {
Chih-Hung Hsieh9f9e4682018-02-28 17:48:55 +0000249 if (Asm->TM.useEmulatedTLS()) {
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000250 // TODO: add debug info for emulated thread local mode.
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000251 } else {
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000252 // FIXME: Make this work with -gsplit-dwarf.
253 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
254 assert((PointerSize == 4 || PointerSize == 8) &&
255 "Add support for other sizes if necessary");
256 // Based on GCC's support for TLS:
257 if (!DD->useSplitDwarf()) {
258 // 1) Start with a constNu of the appropriate pointer size
259 addUInt(*Loc, dwarf::DW_FORM_data1,
260 PointerSize == 4 ? dwarf::DW_OP_const4u
261 : dwarf::DW_OP_const8u);
262 // 2) containing the (relocated) offset of the TLS variable
263 // within the module's TLS block.
264 addExpr(*Loc, dwarf::DW_FORM_udata,
265 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
266 } else {
267 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
268 addUInt(*Loc, dwarf::DW_FORM_udata,
269 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
270 }
271 // 3) followed by an OP to make the debugger do a TLS lookup.
272 addUInt(*Loc, dwarf::DW_FORM_data1,
273 DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address
274 : dwarf::DW_OP_form_tls_address);
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000275 }
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000276 } else {
277 DD->addArangeLabel(SymbolCU(this, Sym));
278 addOpAddress(*Loc, Sym);
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000279 }
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000280 }
Adrian Prantl4b542c62018-04-27 22:05:31 +0000281 // Global variables attached to symbols are memory locations.
282 // It would be better if this were unconditional, but malformed input that
283 // mixes non-fragments and fragments for the same variable is too expensive
284 // to detect in the verifier.
Adrian Prantl3edc63a2018-05-04 16:10:43 +0000285 if (DwarfExpr->isUnknownLocation())
Adrian Prantl4b542c62018-04-27 22:05:31 +0000286 DwarfExpr->setMemoryLocationKind();
287 DwarfExpr->addExpression(Expr);
Adrian Prantl73ec0652016-12-16 19:39:01 +0000288 }
Alexey Bataevf3a91502019-02-05 19:33:47 +0000289 if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
290 // According to
291 // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
292 // cuda-gdb requires DW_AT_address_class for all variables to be able to
293 // correctly interpret address space of the variable address.
294 const unsigned NVPTX_ADDR_global_space = 5;
295 addUInt(*VariableDIE, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
296 NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_global_space);
297 }
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000298 if (Loc)
299 addBlock(*VariableDIE, dwarf::DW_AT_location, DwarfExpr->finalize());
300
301 if (DD->useAllLinkageNames())
302 addLinkageName(*VariableDIE, GV->getLinkageName());
David Blaikie37c52312014-10-04 15:49:50 +0000303
David Blaikie37c52312014-10-04 15:49:50 +0000304 if (addToAccelTable) {
David Blaikie66cf14d2018-08-16 21:29:55 +0000305 DD->addAccelName(*CUNode, GV->getName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000306
307 // If the linkage name is different than the name, go ahead and output
308 // that as well into the name table.
Pavel Labath2a6afe52018-05-14 14:13:20 +0000309 if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName() &&
310 DD->useAllLinkageNames())
David Blaikie66cf14d2018-08-16 21:29:55 +0000311 DD->addAccelName(*CUNode, GV->getLinkageName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000312 }
Adrian Prantl6ed57062019-04-08 19:13:55 +0000313}
David Blaikie37c52312014-10-04 15:49:50 +0000314
Adrian Prantl6ed57062019-04-08 19:13:55 +0000315DIE *DwarfCompileUnit::getOrCreateCommonBlock(
316 const DICommonBlock *CB, ArrayRef<GlobalExpr> GlobalExprs) {
317 // Construct the context before querying for the existence of the DIE in case
318 // such construction creates the DIE.
319 DIE *ContextDIE = getOrCreateContextDIE(CB->getScope());
320
321 if (DIE *NDie = getDIE(CB))
322 return NDie;
323 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_common_block, *ContextDIE, CB);
324 StringRef Name = CB->getName().empty() ? "_BLNK_" : CB->getName();
325 addString(NDie, dwarf::DW_AT_name, Name);
326 addGlobalName(Name, NDie, CB->getScope());
327 if (CB->getFile())
328 addSourceLine(NDie, CB->getLineNo(), CB->getFile());
329 if (DIGlobalVariable *V = CB->getDecl())
330 getCU().addLocationAttribute(&NDie, V, GlobalExprs);
331 return &NDie;
David Blaikie37c52312014-10-04 15:49:50 +0000332}
333
334void DwarfCompileUnit::addRange(RangeSpan Range) {
335 bool SameAsPrevCU = this == DD->getPrevCU();
336 DD->setPrevCU(this);
337 // If we have no current ranges just add the range and return, otherwise,
338 // check the current section and CU against the previous section and CU we
339 // emitted into and the subprogram was contained within. If these are the
340 // same then extend our current range, otherwise add this as a new range.
341 if (CURanges.empty() || !SameAsPrevCU ||
David Blaikieb677cb8d2019-10-02 22:27:24 +0000342 (&CURanges.back().End->getSection() !=
343 &Range.End->getSection())) {
David Blaikie37c52312014-10-04 15:49:50 +0000344 CURanges.push_back(Range);
345 return;
346 }
347
David Blaikieb677cb8d2019-10-02 22:27:24 +0000348 CURanges.back().End = Range.End;
David Blaikie37c52312014-10-04 15:49:50 +0000349}
350
Rafael Espindola063d7252015-03-10 16:58:10 +0000351void DwarfCompileUnit::initStmtList() {
Alexey Bataevd4dd7212018-08-01 19:38:20 +0000352 if (CUNode->isDebugDirectivesOnly())
353 return;
354
David Blaikie37c52312014-10-04 15:49:50 +0000355 // Define start line table label for each Compile Unit.
Alexey Bataevbff36082018-03-23 13:35:54 +0000356 MCSymbol *LineTableStartSym;
357 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
358 if (DD->useSectionsAsReferences()) {
359 LineTableStartSym = TLOF.getDwarfLineSection()->getBeginSymbol();
360 } else {
361 LineTableStartSym =
362 Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID());
363 }
David Blaikie37c52312014-10-04 15:49:50 +0000364
David Blaikie37c52312014-10-04 15:49:50 +0000365 // DW_AT_stmt_list is a offset of line number information for this
366 // compile unit in debug_line section. For split dwarf this is
367 // left in the skeleton CU and so not included.
368 // The line table entries are not always emitted in assembly, so it
369 // is not okay to use line_table_start here.
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000370 StmtListValue =
Greg Clayton35630c32016-12-01 18:56:29 +0000371 addSectionLabel(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym,
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000372 TLOF.getDwarfLineSection()->getBeginSymbol());
David Blaikie37c52312014-10-04 15:49:50 +0000373}
374
375void DwarfCompileUnit::applyStmtList(DIE &D) {
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000376 D.addValue(DIEValueAllocator, *StmtListValue);
David Blaikie37c52312014-10-04 15:49:50 +0000377}
378
David Blaikie14499a72014-10-04 15:58:47 +0000379void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,
380 const MCSymbol *End) {
381 assert(Begin && "Begin label should not be null!");
382 assert(End && "End label should not be null!");
383 assert(Begin->isDefined() && "Invalid starting label");
384 assert(End->isDefined() && "Invalid end label");
385
386 addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
387 if (DD->getDwarfVersion() < 4)
388 addLabelAddress(D, dwarf::DW_AT_high_pc, End);
389 else
390 addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
391}
392
David Blaikiecda2aa82014-10-04 16:24:00 +0000393// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
394// and DW_AT_high_pc attributes. If there are global variables in this
395// scope then create and insert DIEs for these variables.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000396DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) {
David Blaikie3a443c22014-11-04 22:12:25 +0000397 DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes());
David Blaikiecda2aa82014-10-04 16:24:00 +0000398
Rafael Espindola07c03d32015-03-05 02:05:42 +0000399 attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd());
David Blaikiec53e18d2016-05-24 21:19:28 +0000400 if (DD->useAppleExtensionAttributes() &&
401 !DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim(
David Blaikiecda2aa82014-10-04 16:24:00 +0000402 *DD->getCurrentFunction()))
403 addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr);
404
405 // Only include DW_AT_frame_base in full debug info
David Blaikie3a443c22014-11-04 22:12:25 +0000406 if (!includeMinimalInlineScopes()) {
Alexey Bataev7ae86fe2018-07-26 16:10:05 +0000407 if (Asm->MF->getTarget().getTargetTriple().isNVPTX()) {
408 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
409 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_call_frame_cfa);
410 addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc);
411 } else {
412 const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo();
413 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000414 if (Register::isPhysicalRegister(Location.getReg()))
Alexey Bataev7ae86fe2018-07-26 16:10:05 +0000415 addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
416 }
David Blaikiecda2aa82014-10-04 16:24:00 +0000417 }
418
419 // Add name to the name table, we do this here because we're guaranteed
420 // to have concrete versions of our DW_TAG_subprogram nodes.
David Blaikie66cf14d2018-08-16 21:29:55 +0000421 DD->addSubprogramNames(*CUNode, SP, *SPDie);
David Blaikiecda2aa82014-10-04 16:24:00 +0000422
423 return *SPDie;
424}
425
David Blaikie9c65b132014-10-08 22:20:02 +0000426// Construct a DIE for this scope.
427void DwarfCompileUnit::constructScopeDIE(
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000428 LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) {
David Blaikie9c65b132014-10-08 22:20:02 +0000429 if (!Scope || !Scope->getScopeNode())
430 return;
431
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000432 auto *DS = Scope->getScopeNode();
David Blaikie9c65b132014-10-08 22:20:02 +0000433
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000434 assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) &&
David Blaikie9c65b132014-10-08 22:20:02 +0000435 "Only handle inlined subprograms here, use "
436 "constructSubprogramScopeDIE for non-inlined "
437 "subprograms");
438
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000439 SmallVector<DIE *, 8> Children;
David Blaikie9c65b132014-10-08 22:20:02 +0000440
441 // We try to create the scope DIE first, then the children DIEs. This will
442 // avoid creating un-used children then removing them later when we find out
443 // the scope DIE is null.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000444 DIE *ScopeDIE;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000445 if (Scope->getParent() && isa<DISubprogram>(DS)) {
David Blaikie01b48a82014-10-09 16:50:53 +0000446 ScopeDIE = constructInlinedScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000447 if (!ScopeDIE)
448 return;
449 // We create children when the scope DIE is not null.
David Blaikie8b2fdb82014-10-09 18:24:28 +0000450 createScopeChildrenDIE(Scope, Children);
David Blaikie9c65b132014-10-08 22:20:02 +0000451 } else {
452 // Early exit when we know the scope DIE is going to be null.
453 if (DD->isLexicalScopeDIENull(Scope))
454 return;
455
David Blaikie2195e132017-07-27 00:06:53 +0000456 bool HasNonScopeChildren = false;
David Blaikie9c65b132014-10-08 22:20:02 +0000457
458 // We create children here when we know the scope DIE is not going to be
459 // null and the children will be added to the scope DIE.
David Blaikie2195e132017-07-27 00:06:53 +0000460 createScopeChildrenDIE(Scope, Children, &HasNonScopeChildren);
David Blaikie3a443c22014-11-04 22:12:25 +0000461
David Blaikie9c65b132014-10-08 22:20:02 +0000462 // If there are only other scopes as children, put them directly in the
463 // parent instead, as this scope would serve no purpose.
David Blaikie2195e132017-07-27 00:06:53 +0000464 if (!HasNonScopeChildren) {
David Blaikie9c65b132014-10-08 22:20:02 +0000465 FinalChildren.insert(FinalChildren.end(),
466 std::make_move_iterator(Children.begin()),
467 std::make_move_iterator(Children.end()));
468 return;
469 }
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000470 ScopeDIE = constructLexicalScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000471 assert(ScopeDIE && "Scope DIE should not be null.");
472 }
473
474 // Add children
475 for (auto &I : Children)
476 ScopeDIE->addChild(std::move(I));
477
478 FinalChildren.push_back(std::move(ScopeDIE));
479}
480
David Blaikie5b02a192014-11-03 23:10:59 +0000481void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE,
482 SmallVector<RangeSpan, 2> Range) {
David Blaikie52400202014-10-09 00:11:39 +0000483
David Blaikiec4af8bf2018-10-20 07:36:39 +0000484 HasRangeLists = true;
485
486 // Add the range list to the set of ranges to be emitted.
487 auto IndexAndList =
488 (DD->getDwarfVersion() < 5 && Skeleton ? Skeleton->DU : DU)
David Blaikiec8f7e6c2018-11-08 00:35:54 +0000489 ->addRange(*(Skeleton ? Skeleton : this), std::move(Range));
David Blaikiec4af8bf2018-10-20 07:36:39 +0000490
491 uint32_t Index = IndexAndList.first;
492 auto &List = *IndexAndList.second;
David Blaikie5b02a192014-11-03 23:10:59 +0000493
David Blaikie52400202014-10-09 00:11:39 +0000494 // Under fission, ranges are specified by constant offsets relative to the
495 // CU's DW_AT_GNU_ranges_base.
Wolfgang Piebfcf38102018-07-12 18:18:21 +0000496 // FIXME: For DWARF v5, do not generate the DW_AT_ranges attribute under
497 // fission until we support the forms using the .debug_addr section
498 // (DW_RLE_startx_endx etc.).
David Blaikie32e09de2018-10-20 08:12:36 +0000499 if (DD->getDwarfVersion() >= 5)
500 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_rnglistx, Index);
David Blaikiede9aa372019-10-11 23:51:24 +0000501 else {
502 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
503 const MCSymbol *RangeSectionSym =
504 TLOF.getDwarfRangesSection()->getBeginSymbol();
505 if (isDwoUnit())
Eric Christophera8154e52019-12-12 17:55:41 -0800506 addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(),
David Blaikiede9aa372019-10-11 23:51:24 +0000507 RangeSectionSym);
508 else
Eric Christophera8154e52019-12-12 17:55:41 -0800509 addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(),
David Blaikiede9aa372019-10-11 23:51:24 +0000510 RangeSectionSym);
511 }
David Blaikie52400202014-10-09 00:11:39 +0000512}
513
David Blaikiede123752014-10-09 00:21:42 +0000514void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikie5b02a192014-11-03 23:10:59 +0000515 DIE &Die, SmallVector<RangeSpan, 2> Ranges) {
Alexey Bataev858a7dd2018-03-20 20:21:38 +0000516 if (Ranges.size() == 1 || !DD->useRangesSection()) {
517 const RangeSpan &Front = Ranges.front();
518 const RangeSpan &Back = Ranges.back();
David Blaikieb677cb8d2019-10-02 22:27:24 +0000519 attachLowHighPC(Die, Front.Begin, Back.End);
David Blaikie5b02a192014-11-03 23:10:59 +0000520 } else
521 addScopeRangeList(Die, std::move(Ranges));
522}
523
524void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikiede123752014-10-09 00:21:42 +0000525 DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) {
David Blaikie5b02a192014-11-03 23:10:59 +0000526 SmallVector<RangeSpan, 2> List;
527 List.reserve(Ranges.size());
528 for (const InsnRange &R : Ranges)
David Blaikieb677cb8d2019-10-02 22:27:24 +0000529 List.push_back(
530 {DD->getLabelBeforeInsn(R.first), DD->getLabelAfterInsn(R.second)});
David Blaikie5b02a192014-11-03 23:10:59 +0000531 attachRangesOrLowHighPC(Die, std::move(List));
David Blaikiede123752014-10-09 00:21:42 +0000532}
533
David Blaikie01b48a82014-10-09 16:50:53 +0000534// This scope represents inlined body of a function. Construct DIE to
535// represent this concrete inlined copy of the function.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000536DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) {
David Blaikie01b48a82014-10-09 16:50:53 +0000537 assert(Scope->getScopeNode());
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000538 auto *DS = Scope->getScopeNode();
539 auto *InlinedSP = getDISubprogram(DS);
David Blaikie01b48a82014-10-09 16:50:53 +0000540 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
541 // was inlined from another compile unit.
David Blaikie488393f2017-05-12 01:13:45 +0000542 DIE *OriginDIE = getAbstractSPDies()[InlinedSP];
David Blaikie01b48a82014-10-09 16:50:53 +0000543 assert(OriginDIE && "Unable to find original DIE for an inlined subprogram.");
544
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000545 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine);
David Blaikie01b48a82014-10-09 16:50:53 +0000546 addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE);
547
548 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
549
550 // Add the call site information to the DIE.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000551 const DILocation *IA = Scope->getInlinedAt();
David Blaikie01b48a82014-10-09 16:50:53 +0000552 addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None,
Paul Robinson612e89d2018-01-12 19:17:50 +0000553 getOrCreateSourceID(IA->getFile()));
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000554 addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine());
David Blaikie40580d32019-07-16 21:15:19 +0000555 if (IA->getColumn())
556 addUInt(*ScopeDIE, dwarf::DW_AT_call_column, None, IA->getColumn());
Dehao Chen6e0c8442016-10-07 15:21:31 +0000557 if (IA->getDiscriminator() && DD->getDwarfVersion() >= 4)
Dehao Chen54511352015-11-11 18:09:47 +0000558 addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None,
559 IA->getDiscriminator());
David Blaikie01b48a82014-10-09 16:50:53 +0000560
561 // Add name to the name table, we do this here because we're guaranteed
562 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
David Blaikie66cf14d2018-08-16 21:29:55 +0000563 DD->addSubprogramNames(*CUNode, InlinedSP, *ScopeDIE);
David Blaikie01b48a82014-10-09 16:50:53 +0000564
565 return ScopeDIE;
566}
567
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000568// Construct new DW_TAG_lexical_block for this scope and attach
569// DW_AT_low_pc/DW_AT_high_pc labels.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000570DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) {
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000571 if (DD->isLexicalScopeDIENull(Scope))
572 return nullptr;
573
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000574 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block);
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000575 if (Scope->isAbstractScope())
576 return ScopeDIE;
577
578 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
579
580 return ScopeDIE;
581}
582
David Blaikieee7df552014-10-09 17:56:36 +0000583/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000584DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) {
David Blaikieee7df552014-10-09 17:56:36 +0000585 auto D = constructVariableDIEImpl(DV, Abstract);
586 DV.setDIE(*D);
587 return D;
588}
589
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000590DIE *DwarfCompileUnit::constructLabelDIE(DbgLabel &DL,
591 const LexicalScope &Scope) {
592 auto LabelDie = DIE::get(DIEValueAllocator, DL.getTag());
593 insertDIE(DL.getLabel(), LabelDie);
594 DL.setDIE(*LabelDie);
595
596 if (Scope.isAbstractScope())
597 applyLabelAttributes(DL, *LabelDie);
598
599 return LabelDie;
600}
601
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000602DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
603 bool Abstract) {
David Blaikieee7df552014-10-09 17:56:36 +0000604 // Define variable debug information entry.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000605 auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag());
Sander de Smalendc00bec2018-01-24 13:35:54 +0000606 insertDIE(DV.getVariable(), VariableDie);
David Blaikieee7df552014-10-09 17:56:36 +0000607
608 if (Abstract) {
609 applyVariableAttributes(DV, *VariableDie);
610 return VariableDie;
611 }
612
613 // Add variable address.
614
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000615 unsigned Offset = DV.getDebugLocListIndex();
David Blaikieee7df552014-10-09 17:56:36 +0000616 if (Offset != ~0U) {
617 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset);
Evgenii Stepanovdabd2622019-11-25 17:47:30 -0800618 auto TagOffset = DV.getDebugLocListTagOffset();
619 if (TagOffset)
620 addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
621 *TagOffset);
David Blaikieee7df552014-10-09 17:56:36 +0000622 return VariableDie;
623 }
624
Nikola Pricaabc1dff2019-06-10 08:41:06 +0000625 // Check if variable has a single location description.
626 if (auto *DVal = DV.getValueLoc()) {
627 if (DVal->isLocation())
628 addVariableAddress(DV, *VariableDie, DVal->getLoc());
629 else if (DVal->isInt()) {
Adrian Prantl3b89e662016-02-17 22:20:08 +0000630 auto *Expr = DV.getSingleExpression();
631 if (Expr && Expr->getNumElements()) {
632 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
633 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
634 // If there is an expression, emit raw unsigned bytes.
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000635 DwarfExpr.addFragmentOffset(Expr);
Nikola Pricaabc1dff2019-06-10 08:41:06 +0000636 DwarfExpr.addUnsignedConstant(DVal->getInt());
Adrian Prantla63b8e82017-03-16 17:42:45 +0000637 DwarfExpr.addExpression(Expr);
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000638 addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
Evgenii Stepanovdabd2622019-11-25 17:47:30 -0800639 if (DwarfExpr.TagOffset)
640 addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset,
641 dwarf::DW_FORM_data1, *DwarfExpr.TagOffset);
642
Adrian Prantl3b89e662016-02-17 22:20:08 +0000643 } else
Nikola Pricaabc1dff2019-06-10 08:41:06 +0000644 addConstantValue(*VariableDie, DVal->getInt(), DV.getType());
645 } else if (DVal->isConstantFP()) {
646 addConstantFPValue(*VariableDie, DVal->getConstantFP());
647 } else if (DVal->isConstantInt()) {
648 addConstantValue(*VariableDie, DVal->getConstantInt(), DV.getType());
649 }
David Blaikieee7df552014-10-09 17:56:36 +0000650 return VariableDie;
651 }
652
653 // .. else use frame index.
Adrian Prantl67c24422017-02-17 19:42:32 +0000654 if (!DV.hasFrameIndexExprs())
Adrian Prantlca7e4702015-02-10 23:18:28 +0000655 return VariableDie;
656
Alexey Bataevf3a91502019-02-05 19:33:47 +0000657 Optional<unsigned> NVPTXAddressSpace;
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000658 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000659 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
Adrian Prantl67c24422017-02-17 19:42:32 +0000660 for (auto &Fragment : DV.getFrameIndexExprs()) {
David Blaikieee7df552014-10-09 17:56:36 +0000661 unsigned FrameReg = 0;
Adrian Prantl6825fb62017-04-18 01:21:53 +0000662 const DIExpression *Expr = Fragment.Expr;
Eric Christopherd4e723f2015-02-20 22:36:11 +0000663 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
Adrian Prantl67c24422017-02-17 19:42:32 +0000664 int Offset = TFI->getFrameIndexReference(*Asm->MF, Fragment.FI, FrameReg);
Adrian Prantl6825fb62017-04-18 01:21:53 +0000665 DwarfExpr.addFragmentOffset(Expr);
Adrian Prantl956484b2017-03-20 21:35:09 +0000666 SmallVector<uint64_t, 8> Ops;
Bjorn Pettersson2b698a12019-09-07 11:40:10 +0000667 DIExpression::appendOffset(Ops, Offset);
Alexey Bataevf3a91502019-02-05 19:33:47 +0000668 // According to
669 // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
670 // cuda-gdb requires DW_AT_address_class for all variables to be able to
671 // correctly interpret address space of the variable address.
672 // Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef
673 // sequence for the NVPTX + gdb target.
674 unsigned LocalNVPTXAddressSpace;
675 if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
676 const DIExpression *NewExpr =
677 DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace);
678 if (NewExpr != Expr) {
679 Expr = NewExpr;
680 NVPTXAddressSpace = LocalNVPTXAddressSpace;
681 }
682 }
683 if (Expr)
684 Ops.append(Expr->elements_begin(), Expr->elements_end());
Adrian Prantl6825fb62017-04-18 01:21:53 +0000685 DIExpressionCursor Cursor(Ops);
Adrian Prantlc12cee32017-04-19 23:42:25 +0000686 DwarfExpr.setMemoryLocationKind();
Alexey Bataev4dd75582018-07-26 16:29:52 +0000687 if (const MCSymbol *FrameSymbol = Asm->getFunctionFrameSymbol())
688 addOpAddress(*Loc, FrameSymbol);
689 else
690 DwarfExpr.addMachineRegExpression(
691 *Asm->MF->getSubtarget().getRegisterInfo(), Cursor, FrameReg);
Adrian Prantl6825fb62017-04-18 01:21:53 +0000692 DwarfExpr.addExpression(std::move(Cursor));
David Blaikieee7df552014-10-09 17:56:36 +0000693 }
Alexey Bataevf3a91502019-02-05 19:33:47 +0000694 if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
695 // According to
696 // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
697 // cuda-gdb requires DW_AT_address_class for all variables to be able to
698 // correctly interpret address space of the variable address.
699 const unsigned NVPTX_ADDR_local_space = 6;
700 addUInt(*VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
701 NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_local_space);
702 }
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000703 addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
Peter Collingbournefb9ce102019-06-17 23:39:41 +0000704 if (DwarfExpr.TagOffset)
705 addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
706 *DwarfExpr.TagOffset);
David Blaikieee7df552014-10-09 17:56:36 +0000707
708 return VariableDie;
709}
710
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000711DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV,
712 const LexicalScope &Scope,
713 DIE *&ObjectPointer) {
David Blaikie4a1a44e2014-10-09 17:56:39 +0000714 auto Var = constructVariableDIE(DV, Scope.isAbstractScope());
715 if (DV.isObjectPointer())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000716 ObjectPointer = Var;
David Blaikie4a1a44e2014-10-09 17:56:39 +0000717 return Var;
718}
719
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000720/// Return all DIVariables that appear in count: expressions.
721static SmallVector<const DIVariable *, 2> dependencies(DbgVariable *Var) {
722 SmallVector<const DIVariable *, 2> Result;
723 auto *Array = dyn_cast<DICompositeType>(Var->getType());
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000724 if (!Array || Array->getTag() != dwarf::DW_TAG_array_type)
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000725 return Result;
726 for (auto *El : Array->getElements()) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000727 if (auto *Subrange = dyn_cast<DISubrange>(El)) {
728 auto Count = Subrange->getCount();
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000729 if (auto *Dependency = Count.dyn_cast<DIVariable *>())
730 Result.push_back(Dependency);
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000731 }
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000732 }
733 return Result;
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000734}
735
736/// Sort local variables so that variables appearing inside of helper
737/// expressions come first.
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000738static SmallVector<DbgVariable *, 8>
739sortLocalVars(SmallVectorImpl<DbgVariable *> &Input) {
740 SmallVector<DbgVariable *, 8> Result;
741 SmallVector<PointerIntPair<DbgVariable *, 1>, 8> WorkList;
742 // Map back from a DIVariable to its containing DbgVariable.
743 SmallDenseMap<const DILocalVariable *, DbgVariable *> DbgVar;
744 // Set of DbgVariables in Result.
745 SmallDenseSet<DbgVariable *, 8> Visited;
746 // For cycle detection.
747 SmallDenseSet<DbgVariable *, 8> Visiting;
748
749 // Initialize the worklist and the DIVariable lookup table.
750 for (auto Var : reverse(Input)) {
751 DbgVar.insert({Var->getVariable(), Var});
752 WorkList.push_back({Var, 0});
753 }
754
755 // Perform a stable topological sort by doing a DFS.
756 while (!WorkList.empty()) {
757 auto Item = WorkList.back();
758 DbgVariable *Var = Item.getPointer();
759 bool visitedAllDependencies = Item.getInt();
760 WorkList.pop_back();
761
762 // Dependency is in a different lexical scope or a global.
763 if (!Var)
764 continue;
765
766 // Already handled.
767 if (Visited.count(Var))
768 continue;
769
770 // Add to Result if all dependencies are visited.
771 if (visitedAllDependencies) {
772 Visited.insert(Var);
773 Result.push_back(Var);
774 continue;
775 }
776
777 // Detect cycles.
778 auto Res = Visiting.insert(Var);
779 if (!Res.second) {
780 assert(false && "dependency cycle in local variables");
781 return Result;
782 }
783
784 // Push dependencies and this node onto the worklist, so that this node is
785 // visited again after all of its dependencies are handled.
786 WorkList.push_back({Var, 1});
787 for (auto *Dependency : dependencies(Var)) {
788 auto Dep = dyn_cast_or_null<const DILocalVariable>(Dependency);
789 WorkList.push_back({DbgVar[Dep], 0});
790 }
791 }
792 return Result;
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000793}
794
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000795DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope,
796 SmallVectorImpl<DIE *> &Children,
David Blaikie2195e132017-07-27 00:06:53 +0000797 bool *HasNonScopeChildren) {
798 assert(Children.empty());
David Blaikie8b2fdb82014-10-09 18:24:28 +0000799 DIE *ObjectPointer = nullptr;
800
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000801 // Emit function arguments (order is significant).
802 auto Vars = DU->getScopeVariables().lookup(Scope);
803 for (auto &DV : Vars.Args)
804 Children.push_back(constructVariableDIE(*DV.second, *Scope, ObjectPointer));
805
806 // Emit local variables.
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000807 auto Locals = sortLocalVars(Vars.Locals);
808 for (DbgVariable *DV : Locals)
David Blaikie8b2fdb82014-10-09 18:24:28 +0000809 Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer));
810
David Blaikie2195e132017-07-27 00:06:53 +0000811 // Skip imported directives in gmlt-like data.
812 if (!includeMinimalInlineScopes()) {
813 // There is no need to emit empty lexical block DIE.
814 for (const auto *IE : ImportedEntities[Scope->getScopeNode()])
815 Children.push_back(
816 constructImportedEntityDIE(cast<DIImportedEntity>(IE)));
817 }
818
819 if (HasNonScopeChildren)
820 *HasNonScopeChildren = !Children.empty();
David Blaikie8b2fdb82014-10-09 18:24:28 +0000821
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000822 for (DbgLabel *DL : DU->getScopeLabels().lookup(Scope))
823 Children.push_back(constructLabelDIE(*DL, *Scope));
824
David Blaikie8b2fdb82014-10-09 18:24:28 +0000825 for (LexicalScope *LS : Scope->getChildren())
826 constructScopeDIE(LS, Children);
827
David Blaikie8b2fdb82014-10-09 18:24:28 +0000828 return ObjectPointer;
829}
830
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000831DIE &DwarfCompileUnit::constructSubprogramScopeDIE(const DISubprogram *Sub,
832 LexicalScope *Scope) {
David Blaikie1d072342014-10-09 20:21:36 +0000833 DIE &ScopeDIE = updateSubprogramScopeDIE(Sub);
834
David Blaikie3e3eb332016-12-15 23:17:52 +0000835 if (Scope) {
836 assert(!Scope->getInlinedAt());
837 assert(!Scope->isAbstractScope());
838 // Collect lexical scope children first.
839 // ObjectPointer might be a local (non-argument) local variable if it's a
840 // block's synthetic this pointer.
841 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE))
842 addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer);
843 }
844
David Blaikie1d072342014-10-09 20:21:36 +0000845 // If this is a variadic function, add an unspecified parameter.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000846 DITypeRefArray FnArgs = Sub->getType()->getTypeArray();
David Blaikie1dd573d2014-10-23 22:27:50 +0000847
David Blaikie1d072342014-10-09 20:21:36 +0000848 // If we have a single element of null, it is a function that returns void.
849 // If we have more than one elements and the last one is null, it is a
850 // variadic function.
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000851 if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] &&
David Blaikie3a443c22014-11-04 22:12:25 +0000852 !includeMinimalInlineScopes())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000853 ScopeDIE.addChild(
854 DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters));
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000855
856 return ScopeDIE;
David Blaikie1d072342014-10-09 20:21:36 +0000857}
858
David Blaikie78b65b62014-10-09 20:26:15 +0000859DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope,
860 DIE &ScopeDIE) {
861 // We create children when the scope DIE is not null.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000862 SmallVector<DIE *, 8> Children;
David Blaikie78b65b62014-10-09 20:26:15 +0000863 DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children);
864
865 // Add children
866 for (auto &I : Children)
867 ScopeDIE.addChild(std::move(I));
868
869 return ObjectPointer;
870}
871
Dehao Chen54511352015-11-11 18:09:47 +0000872void DwarfCompileUnit::constructAbstractSubprogramScopeDIE(
873 LexicalScope *Scope) {
David Blaikie488393f2017-05-12 01:13:45 +0000874 DIE *&AbsDef = getAbstractSPDies()[Scope->getScopeNode()];
David Blaikie49be5b32014-10-31 21:57:02 +0000875 if (AbsDef)
876 return;
877
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000878 auto *SP = cast<DISubprogram>(Scope->getScopeNode());
David Blaikie58410f22014-10-10 06:39:26 +0000879
880 DIE *ContextDIE;
Jonas Devlieghere294e6892017-11-15 10:57:05 +0000881 DwarfCompileUnit *ContextCU = this;
David Blaikie58410f22014-10-10 06:39:26 +0000882
David Blaikie3a443c22014-11-04 22:12:25 +0000883 if (includeMinimalInlineScopes())
884 ContextDIE = &getUnitDie();
David Blaikie58410f22014-10-10 06:39:26 +0000885 // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000886 // the important distinction that the debug node is not associated with the
887 // DIE (since the debug node will be associated with the concrete DIE, if
David Blaikie58410f22014-10-10 06:39:26 +0000888 // any). It could be refactored to some common utility function.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000889 else if (auto *SPDecl = SP->getDeclaration()) {
David Blaikie58410f22014-10-10 06:39:26 +0000890 ContextDIE = &getUnitDie();
891 getOrCreateSubprogramDIE(SPDecl);
Jonas Devlieghere294e6892017-11-15 10:57:05 +0000892 } else {
Fangrui Songda82ce92019-05-07 02:06:37 +0000893 ContextDIE = getOrCreateContextDIE(SP->getScope());
Jonas Devlieghere294e6892017-11-15 10:57:05 +0000894 // The scope may be shared with a subprogram that has already been
895 // constructed in another CU, in which case we need to construct this
896 // subprogram in the same CU.
897 ContextCU = DD->lookupCU(ContextDIE->getUnitDie());
898 }
David Blaikie58410f22014-10-10 06:39:26 +0000899
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000900 // Passing null as the associated node because the abstract definition
David Blaikie58410f22014-10-10 06:39:26 +0000901 // shouldn't be found by lookup.
Jonas Devlieghere294e6892017-11-15 10:57:05 +0000902 AbsDef = &ContextCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr);
903 ContextCU->applySubprogramAttributesToDefinition(SP, *AbsDef);
David Blaikie58410f22014-10-10 06:39:26 +0000904
Jonas Devlieghere294e6892017-11-15 10:57:05 +0000905 if (!ContextCU->includeMinimalInlineScopes())
906 ContextCU->addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
907 if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren(Scope, *AbsDef))
908 ContextCU->addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
David Blaikie58410f22014-10-10 06:39:26 +0000909}
910
Vedant Kumar533dd022019-08-26 20:53:12 +0000911/// Whether to use the GNU analog for a DWARF5 tag, attribute, or location atom.
912static bool useGNUAnalogForDwarf5Feature(DwarfDebug *DD) {
913 return DD->getDwarfVersion() == 4 && DD->tuneForGDB();
914}
915
Vedant Kumar58a07142019-08-26 20:53:34 +0000916dwarf::Tag DwarfCompileUnit::getDwarf5OrGNUTag(dwarf::Tag Tag) const {
Vedant Kumar533dd022019-08-26 20:53:12 +0000917 if (!useGNUAnalogForDwarf5Feature(DD))
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000918 return Tag;
919 switch (Tag) {
920 case dwarf::DW_TAG_call_site:
921 return dwarf::DW_TAG_GNU_call_site;
922 case dwarf::DW_TAG_call_site_parameter:
923 return dwarf::DW_TAG_GNU_call_site_parameter;
924 default:
Vedant Kumar58a07142019-08-26 20:53:34 +0000925 llvm_unreachable("DWARF5 tag with no GNU analog");
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000926 }
927}
928
929dwarf::Attribute
Vedant Kumar58a07142019-08-26 20:53:34 +0000930DwarfCompileUnit::getDwarf5OrGNUAttr(dwarf::Attribute Attr) const {
Vedant Kumar533dd022019-08-26 20:53:12 +0000931 if (!useGNUAnalogForDwarf5Feature(DD))
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000932 return Attr;
933 switch (Attr) {
934 case dwarf::DW_AT_call_all_calls:
935 return dwarf::DW_AT_GNU_all_call_sites;
936 case dwarf::DW_AT_call_target:
937 return dwarf::DW_AT_GNU_call_site_target;
938 case dwarf::DW_AT_call_origin:
939 return dwarf::DW_AT_abstract_origin;
940 case dwarf::DW_AT_call_pc:
941 return dwarf::DW_AT_low_pc;
942 case dwarf::DW_AT_call_value:
943 return dwarf::DW_AT_GNU_call_site_value;
944 case dwarf::DW_AT_call_tail_call:
945 return dwarf::DW_AT_GNU_tail_call;
946 default:
Vedant Kumar58a07142019-08-26 20:53:34 +0000947 llvm_unreachable("DWARF5 attribute with no GNU analog");
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000948 }
949}
950
Vedant Kumar533dd022019-08-26 20:53:12 +0000951dwarf::LocationAtom
952DwarfCompileUnit::getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const {
953 if (!useGNUAnalogForDwarf5Feature(DD))
954 return Loc;
955 switch (Loc) {
956 case dwarf::DW_OP_entry_value:
957 return dwarf::DW_OP_GNU_entry_value;
958 default:
959 llvm_unreachable("DWARF5 location atom with no GNU analog");
960 }
961}
962
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000963DIE &DwarfCompileUnit::constructCallSiteEntryDIE(
964 DIE &ScopeDIE, const DISubprogram *CalleeSP, bool IsTail,
965 const MCSymbol *PCAddr, const MCExpr *PCOffset, unsigned CallReg) {
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000966 // Insert a call site entry DIE within ScopeDIE.
Vedant Kumar58a07142019-08-26 20:53:34 +0000967 DIE &CallSiteDIE = createAndAddDIE(getDwarf5OrGNUTag(dwarf::DW_TAG_call_site),
968 ScopeDIE, nullptr);
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000969
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000970 if (CallReg) {
971 // Indirect call.
Vedant Kumar58a07142019-08-26 20:53:34 +0000972 addAddress(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_target),
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000973 MachineLocation(CallReg));
Djordje Todorovic0739ccd2019-07-12 09:45:12 +0000974 } else {
Vedant Kumar56232f92019-12-11 15:55:48 -0800975 DIE *CalleeDIE = getOrCreateSubprogramDIE(CalleeSP);
976 assert(CalleeDIE && "Could not create DIE for call site entry origin");
Vedant Kumar58a07142019-08-26 20:53:34 +0000977 addDIEEntry(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_origin),
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000978 *CalleeDIE);
979 }
980
981 if (IsTail)
982 // Attach DW_AT_call_tail_call to tail calls for standards compliance.
Vedant Kumar58a07142019-08-26 20:53:34 +0000983 addFlag(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_tail_call));
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000984
985 // Attach the return PC to allow the debugger to disambiguate call paths
986 // from one function to another.
987 if (DD->getDwarfVersion() == 4 && DD->tuneForGDB()) {
988 assert(PCAddr && "Missing PC information for a call");
989 addLabelAddress(CallSiteDIE, dwarf::DW_AT_low_pc, PCAddr);
990 } else if (!IsTail || DD->tuneForGDB()) {
Vedant Kumar74533bd2018-10-22 21:44:21 +0000991 assert(PCOffset && "Missing return PC information for a call");
992 addAddressExpr(CallSiteDIE, dwarf::DW_AT_call_return_pc, PCOffset);
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000993 }
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000994
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000995 return CallSiteDIE;
996}
997
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000998void DwarfCompileUnit::constructCallSiteParmEntryDIEs(
999 DIE &CallSiteDIE, SmallVector<DbgCallSiteParam, 4> &Params) {
1000 for (const auto &Param : Params) {
1001 unsigned Register = Param.getRegister();
1002 auto CallSiteDieParam =
1003 DIE::get(DIEValueAllocator,
Vedant Kumar58a07142019-08-26 20:53:34 +00001004 getDwarf5OrGNUTag(dwarf::DW_TAG_call_site_parameter));
Djordje Todorovicb9973f82019-07-31 16:51:28 +00001005 insertDIE(CallSiteDieParam);
1006 addAddress(*CallSiteDieParam, dwarf::DW_AT_location,
1007 MachineLocation(Register));
1008
1009 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1010 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
1011 DwarfExpr.setCallSiteParamValueFlag();
1012
1013 DwarfDebug::emitDebugLocValue(*Asm, nullptr, Param.getValue(), DwarfExpr);
1014
Vedant Kumar58a07142019-08-26 20:53:34 +00001015 addBlock(*CallSiteDieParam, getDwarf5OrGNUAttr(dwarf::DW_AT_call_value),
Djordje Todorovicb9973f82019-07-31 16:51:28 +00001016 DwarfExpr.finalize());
1017
1018 CallSiteDIE.addChild(CallSiteDieParam);
1019 }
1020}
1021
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +00001022DIE *DwarfCompileUnit::constructImportedEntityDIE(
1023 const DIImportedEntity *Module) {
Amjad Aboud72da9392016-04-30 01:44:07 +00001024 DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag());
1025 insertDIE(Module, IMDie);
Frederic Riss987fe222014-10-24 21:31:09 +00001026 DIE *EntityDie;
Fangrui Songda82ce92019-05-07 02:06:37 +00001027 auto *Entity = Module->getEntity();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001028 if (auto *NS = dyn_cast<DINamespace>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001029 EntityDie = getOrCreateNameSpace(NS);
Adrian Prantl08a388b2015-06-30 02:13:04 +00001030 else if (auto *M = dyn_cast<DIModule>(Entity))
1031 EntityDie = getOrCreateModule(M);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001032 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001033 EntityDie = getOrCreateSubprogramDIE(SP);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001034 else if (auto *T = dyn_cast<DIType>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001035 EntityDie = getOrCreateTypeDIE(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001036 else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity))
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001037 EntityDie = getOrCreateGlobalVariableDIE(GV, {});
Frederic Riss987fe222014-10-24 21:31:09 +00001038 else
1039 EntityDie = getDIE(Entity);
1040 assert(EntityDie);
Paul Robinson612e89d2018-01-12 19:17:50 +00001041 addSourceLine(*IMDie, Module->getLine(), Module->getFile());
Frederic Riss987fe222014-10-24 21:31:09 +00001042 addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +00001043 StringRef Name = Module->getName();
Frederic Riss987fe222014-10-24 21:31:09 +00001044 if (!Name.empty())
1045 addString(*IMDie, dwarf::DW_AT_name, Name);
1046
1047 return IMDie;
1048}
1049
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001050void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) {
Amjad Aboud72da9392016-04-30 01:44:07 +00001051 DIE *D = getDIE(SP);
David Blaikie488393f2017-05-12 01:13:45 +00001052 if (DIE *AbsSPDIE = getAbstractSPDies().lookup(SP)) {
Amjad Aboud72da9392016-04-30 01:44:07 +00001053 if (D)
David Blaikie4191cbc2014-10-10 06:39:29 +00001054 // If this subprogram has an abstract definition, reference that
1055 addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE);
Amjad Aboud72da9392016-04-30 01:44:07 +00001056 } else {
David Blaikie3e3eb332016-12-15 23:17:52 +00001057 assert(D || includeMinimalInlineScopes());
Amjad Aboud72da9392016-04-30 01:44:07 +00001058 if (D)
David Blaikie4191cbc2014-10-10 06:39:29 +00001059 // And attach the attributes
1060 applySubprogramAttributesToDefinition(SP, *D);
1061 }
1062}
1063
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001064void DwarfCompileUnit::finishEntityDefinition(const DbgEntity *Entity) {
1065 DbgEntity *AbsEntity = getExistingAbstractEntity(Entity->getEntity());
1066
1067 auto *Die = Entity->getDIE();
1068 /// Label may be used to generate DW_AT_low_pc, so put it outside
1069 /// if/else block.
1070 const DbgLabel *Label = nullptr;
1071 if (AbsEntity && AbsEntity->getDIE()) {
1072 addDIEEntry(*Die, dwarf::DW_AT_abstract_origin, *AbsEntity->getDIE());
1073 Label = dyn_cast<const DbgLabel>(Entity);
1074 } else {
1075 if (const DbgVariable *Var = dyn_cast<const DbgVariable>(Entity))
1076 applyVariableAttributes(*Var, *Die);
1077 else if ((Label = dyn_cast<const DbgLabel>(Entity)))
1078 applyLabelAttributes(*Label, *Die);
1079 else
1080 llvm_unreachable("DbgEntity must be DbgVariable or DbgLabel.");
1081 }
1082
Hsiangkai Wang55321d82018-09-26 04:19:23 +00001083 if (Label)
1084 if (const auto *Sym = Label->getSymbol())
1085 addLabelAddress(*Die, dwarf::DW_AT_low_pc, Sym);
David Blaikie488393f2017-05-12 01:13:45 +00001086}
1087
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001088DbgEntity *DwarfCompileUnit::getExistingAbstractEntity(const DINode *Node) {
1089 auto &AbstractEntities = getAbstractEntities();
1090 auto I = AbstractEntities.find(Node);
1091 if (I != AbstractEntities.end())
David Blaikie488393f2017-05-12 01:13:45 +00001092 return I->second.get();
1093 return nullptr;
1094}
1095
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001096void DwarfCompileUnit::createAbstractEntity(const DINode *Node,
1097 LexicalScope *Scope) {
David Blaikie488393f2017-05-12 01:13:45 +00001098 assert(Scope && Scope->isAbstractScope());
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001099 auto &Entity = getAbstractEntities()[Node];
1100 if (isa<const DILocalVariable>(Node)) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001101 Entity = std::make_unique<DbgVariable>(
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001102 cast<const DILocalVariable>(Node), nullptr /* IA */);;
1103 DU->addScopeVariable(Scope, cast<DbgVariable>(Entity.get()));
1104 } else if (isa<const DILabel>(Node)) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001105 Entity = std::make_unique<DbgLabel>(
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001106 cast<const DILabel>(Node), nullptr /* IA */);
1107 DU->addScopeLabel(Scope, cast<DbgLabel>(Entity.get()));
1108 }
David Blaikie488393f2017-05-12 01:13:45 +00001109}
1110
Rafael Espindola063d7252015-03-10 16:58:10 +00001111void DwarfCompileUnit::emitHeader(bool UseOffsets) {
David Blaikieb6726a92014-11-02 02:26:24 +00001112 // Don't bother labeling the .dwo unit, as its offset isn't used.
Alexey Bataevbff36082018-03-23 13:35:54 +00001113 if (!Skeleton && !DD->useSectionsAsReferences()) {
Rafael Espindola9ab09232015-03-17 20:07:06 +00001114 LabelBegin = Asm->createTempSymbol("cu_begin");
Lang Hames9ff69c82015-04-24 19:11:51 +00001115 Asm->OutStreamer->EmitLabel(LabelBegin);
Rafael Espindola3c311142015-03-10 03:11:11 +00001116 }
David Blaikieae57e662014-11-01 23:59:23 +00001117
Paul Robinsoncddd6042017-02-28 20:24:55 +00001118 dwarf::UnitType UT = Skeleton ? dwarf::DW_UT_split_compile
1119 : DD->useSplitDwarf() ? dwarf::DW_UT_skeleton
1120 : dwarf::DW_UT_compile;
1121 DwarfUnit::emitCommonHeader(UseOffsets, UT);
Paul Robinson543c0e12018-05-22 17:27:31 +00001122 if (DD->getDwarfVersion() >= 5 && UT != dwarf::DW_UT_compile)
1123 Asm->emitInt64(getDWOId());
David Blaikieae57e662014-11-01 23:59:23 +00001124}
1125
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001126bool DwarfCompileUnit::hasDwarfPubSections() const {
David Blaikie66cf14d2018-08-16 21:29:55 +00001127 switch (CUNode->getNameTableKind()) {
1128 case DICompileUnit::DebugNameTableKind::None:
1129 return false;
1130 // Opting in to GNU Pubnames/types overrides the default to ensure these are
1131 // generated for things like Gold's gdb_index generation.
1132 case DICompileUnit::DebugNameTableKind::GNU:
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001133 return true;
David Blaikie66cf14d2018-08-16 21:29:55 +00001134 case DICompileUnit::DebugNameTableKind::Default:
David Blaikie0e030472018-08-16 23:57:15 +00001135 return DD->tuneForGDB() && !includeMinimalInlineScopes() &&
David Blaikie2f511762019-04-23 19:00:45 +00001136 !CUNode->isDebugDirectivesOnly() &&
1137 DD->getAccelTableKind() != AccelTableKind::Apple &&
1138 DD->getDwarfVersion() < 5;
David Blaikie66cf14d2018-08-16 21:29:55 +00001139 }
Simon Pilgrim22d580f22018-08-17 09:47:52 +00001140 llvm_unreachable("Unhandled DICompileUnit::DebugNameTableKind enum");
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001141}
1142
David Blaikie192b45c2014-11-02 06:16:39 +00001143/// addGlobalName - Add a new global name to the compile unit.
David Blaikiea0e3c752017-02-03 00:44:18 +00001144void DwarfCompileUnit::addGlobalName(StringRef Name, const DIE &Die,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001145 const DIScope *Context) {
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001146 if (!hasDwarfPubSections())
David Blaikie192b45c2014-11-02 06:16:39 +00001147 return;
1148 std::string FullName = getParentContextString(Context) + Name.str();
1149 GlobalNames[FullName] = &Die;
1150}
1151
David Blaikiea0e3c752017-02-03 00:44:18 +00001152void DwarfCompileUnit::addGlobalNameForTypeUnit(StringRef Name,
1153 const DIScope *Context) {
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001154 if (!hasDwarfPubSections())
David Blaikiea0e3c752017-02-03 00:44:18 +00001155 return;
1156 std::string FullName = getParentContextString(Context) + Name.str();
1157 // Insert, allowing the entry to remain as-is if it's already present
1158 // This way the CU-level type DIE is preferred over the "can't describe this
1159 // type as a unit offset because it's not really in the CU at all, it's only
1160 // in a type unit"
1161 GlobalNames.insert(std::make_pair(std::move(FullName), &getUnitDie()));
1162}
1163
David Blaikie192b45c2014-11-02 06:16:39 +00001164/// Add a new global type to the unit.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001165void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die,
1166 const DIScope *Context) {
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001167 if (!hasDwarfPubSections())
David Blaikie192b45c2014-11-02 06:16:39 +00001168 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +00001169 std::string FullName = getParentContextString(Context) + Ty->getName().str();
David Blaikie192b45c2014-11-02 06:16:39 +00001170 GlobalTypes[FullName] = &Die;
1171}
David Blaikieae57e662014-11-01 23:59:23 +00001172
David Blaikiea0e3c752017-02-03 00:44:18 +00001173void DwarfCompileUnit::addGlobalTypeUnitType(const DIType *Ty,
1174 const DIScope *Context) {
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001175 if (!hasDwarfPubSections())
David Blaikiea0e3c752017-02-03 00:44:18 +00001176 return;
1177 std::string FullName = getParentContextString(Context) + Ty->getName().str();
1178 // Insert, allowing the entry to remain as-is if it's already present
1179 // This way the CU-level type DIE is preferred over the "can't describe this
1180 // type as a unit offset because it's not really in the CU at all, it's only
1181 // in a type unit"
1182 GlobalTypes.insert(std::make_pair(std::move(FullName), &getUnitDie()));
1183}
1184
David Blaikie7d48be22014-11-02 06:37:23 +00001185void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
1186 MachineLocation Location) {
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +00001187 if (DV.hasComplexAddress())
David Blaikie7d48be22014-11-02 06:37:23 +00001188 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
David Blaikie7d48be22014-11-02 06:37:23 +00001189 else
Adrian Prantl5883af32015-01-19 17:57:29 +00001190 addAddress(Die, dwarf::DW_AT_location, Location);
David Blaikie7d48be22014-11-02 06:37:23 +00001191}
David Blaikief7435ee2014-11-02 06:46:40 +00001192
1193/// Add an address attribute to a die based on the location provided.
1194void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
Adrian Prantl5883af32015-01-19 17:57:29 +00001195 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +00001196 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantl956484b2017-03-20 21:35:09 +00001197 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
Adrian Prantlc12cee32017-04-19 23:42:25 +00001198 if (Location.isIndirect())
1199 DwarfExpr.setMemoryLocationKind();
David Blaikief7435ee2014-11-02 06:46:40 +00001200
Adrian Prantl2049c0d2017-08-02 15:22:17 +00001201 DIExpressionCursor Cursor({});
Adrian Prantl956484b2017-03-20 21:35:09 +00001202 const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo();
Adrian Prantlc12cee32017-04-19 23:42:25 +00001203 if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
Adrian Prantlab255fc2014-12-05 01:02:46 +00001204 return;
Adrian Prantl956484b2017-03-20 21:35:09 +00001205 DwarfExpr.addExpression(std::move(Cursor));
Adrian Prantlab255fc2014-12-05 01:02:46 +00001206
David Blaikief7435ee2014-11-02 06:46:40 +00001207 // Now attach the location information to the DIE.
Adrian Prantl956484b2017-03-20 21:35:09 +00001208 addBlock(Die, Attribute, DwarfExpr.finalize());
Evgenii Stepanovdabd2622019-11-25 17:47:30 -08001209
1210 if (DwarfExpr.TagOffset)
1211 addUInt(Die, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
1212 *DwarfExpr.TagOffset);
David Blaikief7435ee2014-11-02 06:46:40 +00001213}
David Blaikie77895fb2014-11-02 06:58:44 +00001214
1215/// Start with the address based on the location provided, and generate the
1216/// DWARF information necessary to find the actual variable given the extra
1217/// address information encoded in the DbgVariable, starting from the starting
1218/// location. Add the DWARF information to the die.
1219void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die,
1220 dwarf::Attribute Attribute,
1221 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +00001222 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantl7813d9c2015-01-14 01:01:30 +00001223 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
Adrian Prantl956484b2017-03-20 21:35:09 +00001224 const DIExpression *DIExpr = DV.getSingleExpression();
1225 DwarfExpr.addFragmentOffset(DIExpr);
Adrian Prantlc12cee32017-04-19 23:42:25 +00001226 if (Location.isIndirect())
1227 DwarfExpr.setMemoryLocationKind();
Adrian Prantl956484b2017-03-20 21:35:09 +00001228
Adrian Prantl2049c0d2017-08-02 15:22:17 +00001229 DIExpressionCursor Cursor(DIExpr);
Djordje Todorovic12aca5d2019-07-09 08:36:34 +00001230
1231 if (DIExpr->isEntryValue()) {
1232 DwarfExpr.setEntryValueFlag();
David Stenberg1ae2d9a2019-10-15 11:31:21 +00001233 DwarfExpr.beginEntryValueExpression(Cursor);
Djordje Todorovic12aca5d2019-07-09 08:36:34 +00001234 }
1235
Peter Collingbourne96c9ae62016-05-20 19:35:17 +00001236 const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo();
Adrian Prantlc12cee32017-04-19 23:42:25 +00001237 if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
Adrian Prantl54286bd2016-11-02 16:12:20 +00001238 return;
Adrian Prantl956484b2017-03-20 21:35:09 +00001239 DwarfExpr.addExpression(std::move(Cursor));
David Blaikie77895fb2014-11-02 06:58:44 +00001240
1241 // Now attach the location information to the DIE.
Adrian Prantl956484b2017-03-20 21:35:09 +00001242 addBlock(Die, Attribute, DwarfExpr.finalize());
Evgenii Stepanovdabd2622019-11-25 17:47:30 -08001243
1244 if (DwarfExpr.TagOffset)
1245 addUInt(Die, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
1246 *DwarfExpr.TagOffset);
David Blaikie77895fb2014-11-02 06:58:44 +00001247}
David Blaikie4bc08812014-11-02 07:03:19 +00001248
1249/// Add a Dwarf loclistptr attribute data and value.
1250void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute,
1251 unsigned Index) {
David Blaikie77cfcd72019-11-12 14:15:37 -08001252 dwarf::Form Form = dwarf::DW_FORM_data4;
1253 if (DD->getDwarfVersion() == 4)
1254 Form =dwarf::DW_FORM_sec_offset;
1255 if (DD->getDwarfVersion() >= 5)
1256 Form =dwarf::DW_FORM_loclistx;
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +00001257 Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index));
David Blaikie4bc08812014-11-02 07:03:19 +00001258}
David Blaikie02a63332014-11-02 07:06:51 +00001259
David Blaikie8c485b52014-11-02 07:08:12 +00001260void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var,
1261 DIE &VariableDie) {
David Blaikie02a63332014-11-02 07:06:51 +00001262 StringRef Name = Var.getName();
1263 if (!Name.empty())
1264 addString(VariableDie, dwarf::DW_AT_name, Name);
Victor Leschuk3c989982016-10-26 11:59:03 +00001265 const auto *DIVar = Var.getVariable();
1266 if (DIVar)
1267 if (uint32_t AlignInBytes = DIVar->getAlignInBytes())
1268 addUInt(VariableDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1269 AlignInBytes);
1270
1271 addSourceLine(VariableDie, DIVar);
David Blaikie02a63332014-11-02 07:06:51 +00001272 addType(VariableDie, Var.getType());
1273 if (Var.isArtificial())
1274 addFlag(VariableDie, dwarf::DW_AT_artificial);
1275}
David Blaikie97802082014-11-02 07:11:55 +00001276
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001277void DwarfCompileUnit::applyLabelAttributes(const DbgLabel &Label,
1278 DIE &LabelDie) {
1279 StringRef Name = Label.getName();
1280 if (!Name.empty())
1281 addString(LabelDie, dwarf::DW_AT_name, Name);
1282 const auto *DILabel = Label.getLabel();
1283 addSourceLine(LabelDie, DILabel);
1284}
1285
David Blaikie97802082014-11-02 07:11:55 +00001286/// Add a Dwarf expression attribute data and value.
1287void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form,
1288 const MCExpr *Expr) {
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +00001289 Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr));
David Blaikie97802082014-11-02 07:11:55 +00001290}
David Blaikie3363a572014-11-02 08:09:09 +00001291
Vedant Kumar74533bd2018-10-22 21:44:21 +00001292void DwarfCompileUnit::addAddressExpr(DIE &Die, dwarf::Attribute Attribute,
1293 const MCExpr *Expr) {
1294 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr,
1295 DIEExpr(Expr));
1296}
1297
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +00001298void DwarfCompileUnit::applySubprogramAttributesToDefinition(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001299 const DISubprogram *SP, DIE &SPDie) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +00001300 auto *SPDecl = SP->getDeclaration();
Fangrui Songda82ce92019-05-07 02:06:37 +00001301 auto *Context = SPDecl ? SPDecl->getScope() : SP->getScope();
David Blaikie3a443c22014-11-04 22:12:25 +00001302 applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +00001303 addGlobalName(SP->getName(), SPDie, Context);
David Blaikie3363a572014-11-02 08:09:09 +00001304}
David Blaikiecafd9622014-11-02 08:51:37 +00001305
1306bool DwarfCompileUnit::isDwoUnit() const {
1307 return DD->useSplitDwarf() && Skeleton;
1308}
David Blaikie3a443c22014-11-04 22:12:25 +00001309
David Blaikie832c7d92019-04-24 18:09:44 +00001310void DwarfCompileUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
1311 constructTypeDIE(D, CTy);
1312}
1313
David Blaikie3a443c22014-11-04 22:12:25 +00001314bool DwarfCompileUnit::includeMinimalInlineScopes() const {
Adrian Prantlb939a252016-03-31 23:56:58 +00001315 return getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly ||
David Blaikie3a443c22014-11-04 22:12:25 +00001316 (DD->useSplitDwarf() && !Skeleton);
1317}
David Blaikie61c127c2018-12-14 22:34:03 +00001318
1319void DwarfCompileUnit::addAddrTableBase() {
1320 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1321 MCSymbol *Label = DD->getAddressPool().getLabel();
1322 addSectionLabel(getUnitDie(),
1323 getDwarfVersion() >= 5 ? dwarf::DW_AT_addr_base
1324 : dwarf::DW_AT_GNU_addr_base,
1325 Label, TLOF.getDwarfAddrSection()->getBeginSymbol());
1326}
Markus Lavinb86ce212019-03-19 13:16:28 +00001327
1328void DwarfCompileUnit::addBaseTypeRef(DIEValueList &Die, int64_t Idx) {
1329 Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, dwarf::DW_FORM_udata,
1330 new (DIEValueAllocator) DIEBaseTypeRef(this, Idx));
1331}
1332
1333void DwarfCompileUnit::createBaseTypeDIEs() {
1334 // Insert the base_type DIEs directly after the CU so that their offsets will
1335 // fit in the fixed size ULEB128 used inside the location expressions.
1336 // Maintain order by iterating backwards and inserting to the front of CU
1337 // child list.
1338 for (auto &Btr : reverse(ExprRefedBaseTypes)) {
1339 DIE &Die = getUnitDie().addChildFront(
1340 DIE::get(DIEValueAllocator, dwarf::DW_TAG_base_type));
1341 SmallString<32> Str;
1342 addString(Die, dwarf::DW_AT_name,
1343 Twine(dwarf::AttributeEncodingString(Btr.Encoding) +
1344 "_" + Twine(Btr.BitSize)).toStringRef(Str));
1345 addUInt(Die, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, Btr.Encoding);
1346 addUInt(Die, dwarf::DW_AT_byte_size, None, Btr.BitSize / 8);
1347
1348 Btr.Die = &Die;
1349 }
1350}