xref: /llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp (revision f407dff50cdcbcfee9dd92397d3792627c3ac708)
1 //===- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Units ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for constructing a dwarf compile unit.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "DwarfCompileUnit.h"
14 #include "AddressPool.h"
15 #include "DwarfExpression.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/BinaryFormat/Dwarf.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/DIE.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/TargetFrameLowering.h"
24 #include "llvm/CodeGen/TargetRegisterInfo.h"
25 #include "llvm/CodeGen/TargetSubtargetInfo.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCSection.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/MC/MCSymbolWasm.h"
34 #include "llvm/MC/MachineLocation.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include <optional>
40 #include <string>
41 #include <utility>
42 
43 using namespace llvm;
44 
45 /// Query value using AddLinkageNamesToDeclCallOriginsForTuning.
46 cl::opt<cl::boolOrDefault> AddLinkageNamesToDeclCallOrigins(
47     "add-linkage-names-to-declaration-call-origins", cl::Hidden,
48     cl::desc("Add DW_AT_linkage_name to function declaration DIEs "
49              "referenced by DW_AT_call_origin attributes. Enabled by default "
50              "for -gsce debugger tuning."));
51 
52 static cl::opt<bool> EmitFuncLineTableOffsetsOption(
53     "emit-func-debug-line-table-offsets", cl::Hidden,
54     cl::desc("Include line table offset in function's debug info and emit end "
55              "sequence after each function's line data."),
56     cl::init(false));
57 
58 static bool AddLinkageNamesToDeclCallOriginsForTuning(const DwarfDebug *DD) {
59   bool EnabledByDefault = DD->tuneForSCE();
60   if (EnabledByDefault)
61     return AddLinkageNamesToDeclCallOrigins != cl::boolOrDefault::BOU_FALSE;
62   return AddLinkageNamesToDeclCallOrigins == cl::boolOrDefault::BOU_TRUE;
63 }
64 
65 static dwarf::Tag GetCompileUnitType(UnitKind Kind, DwarfDebug *DW) {
66 
67   //  According to DWARF Debugging Information Format Version 5,
68   //  3.1.2 Skeleton Compilation Unit Entries:
69   //  "When generating a split DWARF object file (see Section 7.3.2
70   //  on page 187), the compilation unit in the .debug_info section
71   //  is a "skeleton" compilation unit with the tag DW_TAG_skeleton_unit"
72   if (DW->getDwarfVersion() >= 5 && Kind == UnitKind::Skeleton)
73     return dwarf::DW_TAG_skeleton_unit;
74 
75   return dwarf::DW_TAG_compile_unit;
76 }
77 
78 DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node,
79                                    AsmPrinter *A, DwarfDebug *DW,
80                                    DwarfFile *DWU, UnitKind Kind)
81     : DwarfUnit(GetCompileUnitType(Kind, DW), Node, A, DW, DWU, UID) {
82   insertDIE(Node, &getUnitDie());
83   MacroLabelBegin = Asm->createTempSymbol("cu_macro_begin");
84 }
85 
86 /// addLabelAddress - Add a dwarf label attribute data and value using
87 /// DW_FORM_addr or DW_FORM_GNU_addr_index.
88 void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
89                                        const MCSymbol *Label) {
90   if ((Skeleton || !DD->useSplitDwarf()) && Label)
91     DD->addArangeLabel(SymbolCU(this, Label));
92 
93   // Don't use the address pool in non-fission or in the skeleton unit itself.
94   if ((!DD->useSplitDwarf() || !Skeleton) && DD->getDwarfVersion() < 5)
95     return addLocalLabelAddress(Die, Attribute, Label);
96 
97   bool UseAddrOffsetFormOrExpressions =
98       DD->useAddrOffsetForm() || DD->useAddrOffsetExpressions();
99 
100   const MCSymbol *Base = nullptr;
101   if (Label->isInSection() && UseAddrOffsetFormOrExpressions)
102     Base = DD->getSectionLabel(&Label->getSection());
103 
104   if (!Base || Base == Label) {
105     unsigned idx = DD->getAddressPool().getIndex(Label);
106     addAttribute(Die, Attribute,
107                  DD->getDwarfVersion() >= 5 ? dwarf::DW_FORM_addrx
108                                             : dwarf::DW_FORM_GNU_addr_index,
109                  DIEInteger(idx));
110     return;
111   }
112 
113   // Could be extended to work with DWARFv4 Split DWARF if that's important for
114   // someone. In that case DW_FORM_data would be used.
115   assert(DD->getDwarfVersion() >= 5 &&
116          "Addr+offset expressions are only valuable when using debug_addr (to "
117          "reduce relocations) available in DWARFv5 or higher");
118   if (DD->useAddrOffsetExpressions()) {
119     auto *Loc = new (DIEValueAllocator) DIEBlock();
120     addPoolOpAddress(*Loc, Label);
121     addBlock(Die, Attribute, dwarf::DW_FORM_exprloc, Loc);
122   } else
123     addAttribute(Die, Attribute, dwarf::DW_FORM_LLVM_addrx_offset,
124                  new (DIEValueAllocator) DIEAddrOffset(
125                      DD->getAddressPool().getIndex(Base), Label, Base));
126 }
127 
128 void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
129                                             dwarf::Attribute Attribute,
130                                             const MCSymbol *Label) {
131   if (Label)
132     addAttribute(Die, Attribute, dwarf::DW_FORM_addr, DIELabel(Label));
133   else
134     addAttribute(Die, Attribute, dwarf::DW_FORM_addr, DIEInteger(0));
135 }
136 
137 unsigned DwarfCompileUnit::getOrCreateSourceID(const DIFile *File) {
138   // If we print assembly, we can't separate .file entries according to
139   // compile units. Thus all files will belong to the default compile unit.
140 
141   // FIXME: add a better feature test than hasRawTextSupport. Even better,
142   // extend .file to support this.
143   unsigned CUID = Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID();
144   if (!File)
145     return Asm->OutStreamer->emitDwarfFileDirective(0, "", "", std::nullopt,
146                                                     std::nullopt, CUID);
147 
148   if (LastFile != File) {
149     LastFile = File;
150     LastFileID = Asm->OutStreamer->emitDwarfFileDirective(
151         0, File->getDirectory(), File->getFilename(), DD->getMD5AsBytes(File),
152         File->getSource(), CUID);
153   }
154   return LastFileID;
155 }
156 
157 DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(
158     const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
159   // Check for pre-existence.
160   if (DIE *Die = getDIE(GV))
161     return Die;
162 
163   assert(GV);
164 
165   auto *GVContext = GV->getScope();
166   const DIType *GTy = GV->getType();
167 
168   auto *CB = GVContext ? dyn_cast<DICommonBlock>(GVContext) : nullptr;
169   DIE *ContextDIE = CB ? getOrCreateCommonBlock(CB, GlobalExprs)
170     : getOrCreateContextDIE(GVContext);
171 
172   // Add to map.
173   DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV);
174   DIScope *DeclContext;
175   if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) {
176     DeclContext = SDMDecl->getScope();
177     assert(SDMDecl->isStaticMember() && "Expected static member decl");
178     assert(GV->isDefinition());
179     // We need the declaration DIE that is in the static member's class.
180     DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl);
181     addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE);
182     // If the global variable's type is different from the one in the class
183     // member type, assume that it's more specific and also emit it.
184     if (GTy != SDMDecl->getBaseType())
185       addType(*VariableDIE, GTy);
186   } else {
187     DeclContext = GV->getScope();
188     // Add name and type.
189     StringRef DisplayName = GV->getDisplayName();
190     if (!DisplayName.empty())
191       addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName());
192     if (GTy)
193       addType(*VariableDIE, GTy);
194 
195     // Add scoping info.
196     if (!GV->isLocalToUnit())
197       addFlag(*VariableDIE, dwarf::DW_AT_external);
198 
199     // Add line number info.
200     addSourceLine(*VariableDIE, GV);
201   }
202 
203   if (!GV->isDefinition())
204     addFlag(*VariableDIE, dwarf::DW_AT_declaration);
205   else
206     addGlobalName(GV->getName(), *VariableDIE, DeclContext);
207 
208   addAnnotation(*VariableDIE, GV->getAnnotations());
209 
210   if (uint32_t AlignInBytes = GV->getAlignInBytes())
211     addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
212             AlignInBytes);
213 
214   if (MDTuple *TP = GV->getTemplateParams())
215     addTemplateParams(*VariableDIE, DINodeArray(TP));
216 
217   // Add location.
218   addLocationAttribute(VariableDIE, GV, GlobalExprs);
219 
220   return VariableDIE;
221 }
222 
223 void DwarfCompileUnit::addLocationAttribute(
224     DIE *VariableDIE, const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
225   bool addToAccelTable = false;
226   DIELoc *Loc = nullptr;
227   std::optional<unsigned> NVPTXAddressSpace;
228   std::unique_ptr<DIEDwarfExpression> DwarfExpr;
229   for (const auto &GE : GlobalExprs) {
230     const GlobalVariable *Global = GE.Var;
231     const DIExpression *Expr = GE.Expr;
232 
233     // For compatibility with DWARF 3 and earlier,
234     // DW_AT_location(DW_OP_constu, X, DW_OP_stack_value) or
235     // DW_AT_location(DW_OP_consts, X, DW_OP_stack_value) becomes
236     // DW_AT_const_value(X).
237     if (GlobalExprs.size() == 1 && Expr && Expr->isConstant()) {
238       addToAccelTable = true;
239       addConstantValue(
240           *VariableDIE,
241           DIExpression::SignedOrUnsignedConstant::UnsignedConstant ==
242               *Expr->isConstant(),
243           Expr->getElement(1));
244       break;
245     }
246 
247     // We cannot describe the location of dllimport'd variables: the
248     // computation of their address requires loads from the IAT.
249     if (Global && Global->hasDLLImportStorageClass())
250       continue;
251 
252     // Nothing to describe without address or constant.
253     if (!Global && (!Expr || !Expr->isConstant()))
254       continue;
255 
256     if (Global && Global->isThreadLocal() &&
257         !Asm->getObjFileLowering().supportDebugThreadLocalLocation())
258       continue;
259 
260     if (!Loc) {
261       addToAccelTable = true;
262       Loc = new (DIEValueAllocator) DIELoc;
263       DwarfExpr = std::make_unique<DIEDwarfExpression>(*Asm, *this, *Loc);
264     }
265 
266     if (Expr) {
267       // According to
268       // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
269       // cuda-gdb requires DW_AT_address_class for all variables to be able to
270       // correctly interpret address space of the variable address.
271       // Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef
272       // sequence for the NVPTX + gdb target.
273       unsigned LocalNVPTXAddressSpace;
274       if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
275         const DIExpression *NewExpr =
276             DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace);
277         if (NewExpr != Expr) {
278           Expr = NewExpr;
279           NVPTXAddressSpace = LocalNVPTXAddressSpace;
280         }
281       }
282       DwarfExpr->addFragmentOffset(Expr);
283     }
284 
285     if (Global) {
286       const MCSymbol *Sym = Asm->getSymbol(Global);
287       // 16-bit platforms like MSP430 and AVR take this path, so sink this
288       // assert to platforms that use it.
289       auto GetPointerSizedFormAndOp = [this]() {
290         unsigned PointerSize = Asm->MAI->getCodePointerSize();
291         assert((PointerSize == 4 || PointerSize == 8) &&
292                "Add support for other sizes if necessary");
293         struct FormAndOp {
294           dwarf::Form Form;
295           dwarf::LocationAtom Op;
296         };
297         return PointerSize == 4
298                    ? FormAndOp{dwarf::DW_FORM_data4, dwarf::DW_OP_const4u}
299                    : FormAndOp{dwarf::DW_FORM_data8, dwarf::DW_OP_const8u};
300       };
301       if (Global->isThreadLocal()) {
302         if (Asm->TM.getTargetTriple().isWasm()) {
303           // FIXME This is not guaranteed, but in practice, in static linking,
304           // if present, __tls_base's index is 1. This doesn't hold for dynamic
305           // linking, so TLS variables used in dynamic linking won't have
306           // correct debug info for now. See
307           // https://github.com/llvm/llvm-project/blob/19afbfe33156d211fa959dadeea46cd17b9c723c/lld/wasm/Driver.cpp#L786-L823
308           addWasmRelocBaseGlobal(Loc, "__tls_base", 1);
309           addOpAddress(*Loc, Sym);
310           addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
311         } else if (Asm->TM.useEmulatedTLS()) {
312           // TODO: add debug info for emulated thread local mode.
313         } else {
314           // FIXME: Make this work with -gsplit-dwarf.
315           // Based on GCC's support for TLS:
316           if (!DD->useSplitDwarf()) {
317             auto FormAndOp = GetPointerSizedFormAndOp();
318             // 1) Start with a constNu of the appropriate pointer size
319             addUInt(*Loc, dwarf::DW_FORM_data1, FormAndOp.Op);
320             // 2) containing the (relocated) offset of the TLS variable
321             //    within the module's TLS block.
322             addExpr(*Loc, FormAndOp.Form,
323                     Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
324           } else {
325             addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
326             addUInt(*Loc, dwarf::DW_FORM_udata,
327                     DD->getAddressPool().getIndex(Sym, /* TLS */ true));
328           }
329           // 3) followed by an OP to make the debugger do a TLS lookup.
330           addUInt(*Loc, dwarf::DW_FORM_data1,
331                   DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address
332                                         : dwarf::DW_OP_form_tls_address);
333         }
334       } else if (Asm->TM.getTargetTriple().isWasm() &&
335                  Asm->TM.getRelocationModel() == Reloc::PIC_) {
336         // FIXME This is not guaranteed, but in practice, if present,
337         // __memory_base's index is 1. See
338         // https://github.com/llvm/llvm-project/blob/19afbfe33156d211fa959dadeea46cd17b9c723c/lld/wasm/Driver.cpp#L786-L823
339         addWasmRelocBaseGlobal(Loc, "__memory_base", 1);
340         addOpAddress(*Loc, Sym);
341         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
342       } else if ((Asm->TM.getRelocationModel() == Reloc::RWPI ||
343                   Asm->TM.getRelocationModel() == Reloc::ROPI_RWPI) &&
344                  !Asm->getObjFileLowering()
345                       .getKindForGlobal(Global, Asm->TM)
346                       .isReadOnly()) {
347         auto FormAndOp = GetPointerSizedFormAndOp();
348         // Constant
349         addUInt(*Loc, dwarf::DW_FORM_data1, FormAndOp.Op);
350         // Relocation offset
351         addExpr(*Loc, FormAndOp.Form,
352                 Asm->getObjFileLowering().getIndirectSymViaRWPI(Sym));
353         // Base register
354         Register BaseReg = Asm->getObjFileLowering().getStaticBase();
355         unsigned DwarfBaseReg =
356             Asm->TM.getMCRegisterInfo()->getDwarfRegNum(BaseReg, false);
357         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DwarfBaseReg);
358         // Offset from base register
359         addSInt(*Loc, dwarf::DW_FORM_sdata, 0);
360         // Operation
361         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
362       } else {
363         DD->addArangeLabel(SymbolCU(this, Sym));
364         addOpAddress(*Loc, Sym);
365       }
366     }
367     // Global variables attached to symbols are memory locations.
368     // It would be better if this were unconditional, but malformed input that
369     // mixes non-fragments and fragments for the same variable is too expensive
370     // to detect in the verifier.
371     if (DwarfExpr->isUnknownLocation())
372       DwarfExpr->setMemoryLocationKind();
373     DwarfExpr->addExpression(Expr);
374   }
375   if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
376     // According to
377     // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
378     // cuda-gdb requires DW_AT_address_class for all variables to be able to
379     // correctly interpret address space of the variable address.
380     const unsigned NVPTX_ADDR_global_space = 5;
381     addUInt(*VariableDIE, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
382             NVPTXAddressSpace.value_or(NVPTX_ADDR_global_space));
383   }
384   if (Loc)
385     addBlock(*VariableDIE, dwarf::DW_AT_location, DwarfExpr->finalize());
386 
387   if (DD->useAllLinkageNames())
388     addLinkageName(*VariableDIE, GV->getLinkageName());
389 
390   if (addToAccelTable) {
391     DD->addAccelName(*this, CUNode->getNameTableKind(), GV->getName(),
392                      *VariableDIE);
393 
394     // If the linkage name is different than the name, go ahead and output
395     // that as well into the name table.
396     if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName() &&
397         DD->useAllLinkageNames())
398       DD->addAccelName(*this, CUNode->getNameTableKind(), GV->getLinkageName(),
399                        *VariableDIE);
400   }
401 }
402 
403 DIE *DwarfCompileUnit::getOrCreateCommonBlock(
404     const DICommonBlock *CB, ArrayRef<GlobalExpr> GlobalExprs) {
405   // Check for pre-existence.
406   if (DIE *NDie = getDIE(CB))
407     return NDie;
408   DIE *ContextDIE = getOrCreateContextDIE(CB->getScope());
409   DIE &NDie = createAndAddDIE(dwarf::DW_TAG_common_block, *ContextDIE, CB);
410   StringRef Name = CB->getName().empty() ? "_BLNK_" : CB->getName();
411   addString(NDie, dwarf::DW_AT_name, Name);
412   addGlobalName(Name, NDie, CB->getScope());
413   if (CB->getFile())
414     addSourceLine(NDie, CB->getLineNo(), CB->getFile());
415   if (DIGlobalVariable *V = CB->getDecl())
416     getCU().addLocationAttribute(&NDie, V, GlobalExprs);
417   return &NDie;
418 }
419 
420 void DwarfCompileUnit::addRange(RangeSpan Range) {
421   DD->insertSectionLabel(Range.Begin);
422 
423   auto *PrevCU = DD->getPrevCU();
424   bool SameAsPrevCU = this == PrevCU;
425   DD->setPrevCU(this);
426   // If we have no current ranges just add the range and return, otherwise,
427   // check the current section and CU against the previous section and CU we
428   // emitted into and the subprogram was contained within. If these are the
429   // same then extend our current range, otherwise add this as a new range.
430   if (CURanges.empty() || !SameAsPrevCU ||
431       (&CURanges.back().End->getSection() !=
432        &Range.End->getSection())) {
433     // Before a new range is added, always terminate the prior line table.
434     if (PrevCU)
435       DD->terminateLineTable(PrevCU);
436     CURanges.push_back(Range);
437     return;
438   }
439 
440   CURanges.back().End = Range.End;
441 }
442 
443 void DwarfCompileUnit::initStmtList() {
444   if (CUNode->isDebugDirectivesOnly())
445     return;
446 
447   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
448   if (DD->useSectionsAsReferences()) {
449     LineTableStartSym = TLOF.getDwarfLineSection()->getBeginSymbol();
450   } else {
451     LineTableStartSym =
452         Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID());
453   }
454 
455   // DW_AT_stmt_list is a offset of line number information for this
456   // compile unit in debug_line section. For split dwarf this is
457   // left in the skeleton CU and so not included.
458   // The line table entries are not always emitted in assembly, so it
459   // is not okay to use line_table_start here.
460       addSectionLabel(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym,
461                       TLOF.getDwarfLineSection()->getBeginSymbol());
462 }
463 
464 void DwarfCompileUnit::applyStmtList(DIE &D) {
465   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
466   addSectionLabel(D, dwarf::DW_AT_stmt_list, LineTableStartSym,
467                   TLOF.getDwarfLineSection()->getBeginSymbol());
468 }
469 
470 void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,
471                                        const MCSymbol *End) {
472   assert(Begin && "Begin label should not be null!");
473   assert(End && "End label should not be null!");
474   assert(Begin->isDefined() && "Invalid starting label");
475   assert(End->isDefined() && "Invalid end label");
476 
477   addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
478   if (DD->getDwarfVersion() < 4)
479     addLabelAddress(D, dwarf::DW_AT_high_pc, End);
480   else
481     addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
482 }
483 
484 // Add info for Wasm-global-based relocation.
485 // 'GlobalIndex' is used for split dwarf, which currently relies on a few
486 // assumptions that are not guaranteed in a formal way but work in practice.
487 void DwarfCompileUnit::addWasmRelocBaseGlobal(DIELoc *Loc, StringRef GlobalName,
488                                               uint64_t GlobalIndex) {
489   // FIXME: duplicated from Target/WebAssembly/WebAssembly.h
490   // don't want to depend on target specific headers in this code?
491   const unsigned TI_GLOBAL_RELOC = 3;
492   unsigned PointerSize = Asm->getDataLayout().getPointerSize();
493   auto *Sym = cast<MCSymbolWasm>(Asm->GetExternalSymbolSymbol(GlobalName));
494   // FIXME: this repeats what WebAssemblyMCInstLower::
495   // GetExternalSymbolSymbol does, since if there's no code that
496   // refers to this symbol, we have to set it here.
497   Sym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
498   Sym->setGlobalType(wasm::WasmGlobalType{
499       static_cast<uint8_t>(PointerSize == 4 ? wasm::WASM_TYPE_I32
500                                             : wasm::WASM_TYPE_I64),
501       true});
502   addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_WASM_location);
503   addSInt(*Loc, dwarf::DW_FORM_sdata, TI_GLOBAL_RELOC);
504   if (!isDwoUnit()) {
505     addLabel(*Loc, dwarf::DW_FORM_data4, Sym);
506   } else {
507     // FIXME: when writing dwo, we need to avoid relocations. Probably
508     // the "right" solution is to treat globals the way func and data
509     // symbols are (with entries in .debug_addr).
510     // For now we hardcode the indices in the callsites. Global indices are not
511     // fixed, but in practice a few are fixed; for example, __stack_pointer is
512     // always index 0.
513     addUInt(*Loc, dwarf::DW_FORM_data4, GlobalIndex);
514   }
515 }
516 
517 // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
518 // and DW_AT_high_pc attributes. If there are global variables in this
519 // scope then create and insert DIEs for these variables.
520 DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP,
521                                                 MCSymbol *LineTableSym) {
522   DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes());
523   SmallVector<RangeSpan, 2> BB_List;
524   // If basic block sections are on, ranges for each basic block section has
525   // to be emitted separately.
526   for (const auto &R : Asm->MBBSectionRanges)
527     BB_List.push_back({R.second.BeginLabel, R.second.EndLabel});
528 
529   attachRangesOrLowHighPC(*SPDie, BB_List);
530 
531   if (DD->useAppleExtensionAttributes() &&
532       !DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim(
533           *DD->getCurrentFunction()))
534     addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr);
535 
536   if (emitFuncLineTableOffsets() && LineTableSym) {
537     addSectionLabel(
538         *SPDie, dwarf::DW_AT_LLVM_stmt_sequence, LineTableSym,
539         Asm->getObjFileLowering().getDwarfLineSection()->getBeginSymbol());
540   }
541 
542   // Only include DW_AT_frame_base in full debug info
543   if (!includeMinimalInlineScopes()) {
544     const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
545     TargetFrameLowering::DwarfFrameBase FrameBase =
546         TFI->getDwarfFrameBase(*Asm->MF);
547     switch (FrameBase.Kind) {
548     case TargetFrameLowering::DwarfFrameBase::Register: {
549       if (Register::isPhysicalRegister(FrameBase.Location.Reg)) {
550         MachineLocation Location(FrameBase.Location.Reg);
551         addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
552       }
553       break;
554     }
555     case TargetFrameLowering::DwarfFrameBase::CFA: {
556       DIELoc *Loc = new (DIEValueAllocator) DIELoc;
557       addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_call_frame_cfa);
558       if (FrameBase.Location.Offset != 0) {
559         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_consts);
560         addSInt(*Loc, dwarf::DW_FORM_sdata, FrameBase.Location.Offset);
561         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
562       }
563       addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc);
564       break;
565     }
566     case TargetFrameLowering::DwarfFrameBase::WasmFrameBase: {
567       // FIXME: duplicated from Target/WebAssembly/WebAssembly.h
568       const unsigned TI_GLOBAL_RELOC = 3;
569       if (FrameBase.Location.WasmLoc.Kind == TI_GLOBAL_RELOC) {
570         // These need to be relocatable.
571         DIELoc *Loc = new (DIEValueAllocator) DIELoc;
572         assert(FrameBase.Location.WasmLoc.Index == 0); // Only SP so far.
573         // For now, since we only ever use index 0, this should work as-is.
574         addWasmRelocBaseGlobal(Loc, "__stack_pointer",
575                                FrameBase.Location.WasmLoc.Index);
576         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
577         addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc);
578       } else {
579         DIELoc *Loc = new (DIEValueAllocator) DIELoc;
580         DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
581         DIExpressionCursor Cursor({});
582         DwarfExpr.addWasmLocation(FrameBase.Location.WasmLoc.Kind,
583             FrameBase.Location.WasmLoc.Index);
584         DwarfExpr.addExpression(std::move(Cursor));
585         addBlock(*SPDie, dwarf::DW_AT_frame_base, DwarfExpr.finalize());
586       }
587       break;
588     }
589     }
590   }
591 
592   // Add name to the name table, we do this here because we're guaranteed
593   // to have concrete versions of our DW_TAG_subprogram nodes.
594   DD->addSubprogramNames(*this, CUNode->getNameTableKind(), SP, *SPDie);
595 
596   return *SPDie;
597 }
598 
599 // Construct a DIE for this scope.
600 void DwarfCompileUnit::constructScopeDIE(LexicalScope *Scope,
601                                          DIE &ParentScopeDIE) {
602   if (!Scope || !Scope->getScopeNode())
603     return;
604 
605   auto *DS = Scope->getScopeNode();
606 
607   assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) &&
608          "Only handle inlined subprograms here, use "
609          "constructSubprogramScopeDIE for non-inlined "
610          "subprograms");
611 
612   // Emit inlined subprograms.
613   if (Scope->getParent() && isa<DISubprogram>(DS)) {
614     DIE *ScopeDIE = constructInlinedScopeDIE(Scope, ParentScopeDIE);
615     assert(ScopeDIE && "Scope DIE should not be null.");
616     createAndAddScopeChildren(Scope, *ScopeDIE);
617     return;
618   }
619 
620   // Early exit when we know the scope DIE is going to be null.
621   if (DD->isLexicalScopeDIENull(Scope))
622     return;
623 
624   // Emit lexical blocks.
625   DIE *ScopeDIE = constructLexicalScopeDIE(Scope);
626   assert(ScopeDIE && "Scope DIE should not be null.");
627 
628   ParentScopeDIE.addChild(ScopeDIE);
629   createAndAddScopeChildren(Scope, *ScopeDIE);
630 }
631 
632 void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE,
633                                          SmallVector<RangeSpan, 2> Range) {
634 
635   HasRangeLists = true;
636 
637   // Add the range list to the set of ranges to be emitted.
638   auto IndexAndList =
639       (DD->getDwarfVersion() < 5 && Skeleton ? Skeleton->DU : DU)
640           ->addRange(*(Skeleton ? Skeleton : this), std::move(Range));
641 
642   uint32_t Index = IndexAndList.first;
643   auto &List = *IndexAndList.second;
644 
645   // Under fission, ranges are specified by constant offsets relative to the
646   // CU's DW_AT_GNU_ranges_base.
647   // FIXME: For DWARF v5, do not generate the DW_AT_ranges attribute under
648   // fission until we support the forms using the .debug_addr section
649   // (DW_RLE_startx_endx etc.).
650   if (DD->getDwarfVersion() >= 5)
651     addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_rnglistx, Index);
652   else {
653     const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
654     const MCSymbol *RangeSectionSym =
655         TLOF.getDwarfRangesSection()->getBeginSymbol();
656     if (isDwoUnit())
657       addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.Label,
658                       RangeSectionSym);
659     else
660       addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.Label,
661                       RangeSectionSym);
662   }
663 }
664 
665 void DwarfCompileUnit::attachRangesOrLowHighPC(
666     DIE &Die, SmallVector<RangeSpan, 2> Ranges) {
667   assert(!Ranges.empty());
668   if (!DD->useRangesSection() ||
669       (Ranges.size() == 1 &&
670        (!DD->alwaysUseRanges(*this) ||
671         DD->getSectionLabel(&Ranges.front().Begin->getSection()) ==
672             Ranges.front().Begin))) {
673     const RangeSpan &Front = Ranges.front();
674     const RangeSpan &Back = Ranges.back();
675     attachLowHighPC(Die, Front.Begin, Back.End);
676   } else
677     addScopeRangeList(Die, std::move(Ranges));
678 }
679 
680 void DwarfCompileUnit::attachRangesOrLowHighPC(
681     DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) {
682   SmallVector<RangeSpan, 2> List;
683   List.reserve(Ranges.size());
684   for (const InsnRange &R : Ranges) {
685     auto *BeginLabel = DD->getLabelBeforeInsn(R.first);
686     auto *EndLabel = DD->getLabelAfterInsn(R.second);
687 
688     const auto *BeginMBB = R.first->getParent();
689     const auto *EndMBB = R.second->getParent();
690 
691     const auto *MBB = BeginMBB;
692     // Basic block sections allows basic block subsets to be placed in unique
693     // sections. For each section, the begin and end label must be added to the
694     // list. If there is more than one range, debug ranges must be used.
695     // Otherwise, low/high PC can be used.
696     // FIXME: Debug Info Emission depends on block order and this assumes that
697     // the order of blocks will be frozen beyond this point.
698     do {
699       if (MBB->sameSection(EndMBB) || MBB->isEndSection()) {
700         auto MBBSectionRange = Asm->MBBSectionRanges[MBB->getSectionID()];
701         List.push_back(
702             {MBB->sameSection(BeginMBB) ? BeginLabel
703                                         : MBBSectionRange.BeginLabel,
704              MBB->sameSection(EndMBB) ? EndLabel : MBBSectionRange.EndLabel});
705       }
706       if (MBB->sameSection(EndMBB))
707         break;
708       MBB = MBB->getNextNode();
709     } while (true);
710   }
711   attachRangesOrLowHighPC(Die, std::move(List));
712 }
713 
714 DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope,
715                                                 DIE &ParentScopeDIE) {
716   assert(Scope->getScopeNode());
717   auto *DS = Scope->getScopeNode();
718   auto *InlinedSP = getDISubprogram(DS);
719   // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
720   // was inlined from another compile unit.
721   DIE *OriginDIE = getAbstractScopeDIEs()[InlinedSP];
722   assert(OriginDIE && "Unable to find original DIE for an inlined subprogram.");
723 
724   auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine);
725   ParentScopeDIE.addChild(ScopeDIE);
726   addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE);
727 
728   attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
729 
730   // Add the call site information to the DIE.
731   const DILocation *IA = Scope->getInlinedAt();
732   addUInt(*ScopeDIE, dwarf::DW_AT_call_file, std::nullopt,
733           getOrCreateSourceID(IA->getFile()));
734   addUInt(*ScopeDIE, dwarf::DW_AT_call_line, std::nullopt, IA->getLine());
735   if (IA->getColumn())
736     addUInt(*ScopeDIE, dwarf::DW_AT_call_column, std::nullopt, IA->getColumn());
737   if (IA->getDiscriminator() && DD->getDwarfVersion() >= 4)
738     addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, std::nullopt,
739             IA->getDiscriminator());
740 
741   // Add name to the name table, we do this here because we're guaranteed
742   // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
743   DD->addSubprogramNames(*this, CUNode->getNameTableKind(), InlinedSP,
744                          *ScopeDIE);
745 
746   return ScopeDIE;
747 }
748 
749 // Construct new DW_TAG_lexical_block for this scope and attach
750 // DW_AT_low_pc/DW_AT_high_pc labels.
751 DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) {
752   if (DD->isLexicalScopeDIENull(Scope))
753     return nullptr;
754   const auto *DS = Scope->getScopeNode();
755 
756   auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block);
757   if (Scope->isAbstractScope()) {
758     assert(!getAbstractScopeDIEs().count(DS) &&
759            "Abstract DIE for this scope exists!");
760     getAbstractScopeDIEs()[DS] = ScopeDIE;
761     return ScopeDIE;
762   }
763   if (!Scope->getInlinedAt()) {
764     assert(!LexicalBlockDIEs.count(DS) &&
765            "Concrete out-of-line DIE for this scope exists!");
766     LexicalBlockDIEs[DS] = ScopeDIE;
767   }
768 
769   attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
770 
771   return ScopeDIE;
772 }
773 
774 DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) {
775   auto *VariableDie = DIE::get(DIEValueAllocator, DV.getTag());
776   insertDIE(DV.getVariable(), VariableDie);
777   DV.setDIE(*VariableDie);
778   // Abstract variables don't get common attributes later, so apply them now.
779   if (Abstract) {
780     applyCommonDbgVariableAttributes(DV, *VariableDie);
781   } else {
782     std::visit(
783         [&](const auto &V) {
784           applyConcreteDbgVariableAttributes(V, DV, *VariableDie);
785         },
786         DV.asVariant());
787   }
788   return VariableDie;
789 }
790 
791 void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
792     const Loc::Single &Single, const DbgVariable &DV, DIE &VariableDie) {
793   const DbgValueLoc *DVal = &Single.getValueLoc();
794   if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB() &&
795       !Single.getExpr()) {
796     // Lack of expression means it is a register.  Registers for PTX need to
797     // be marked with DW_AT_address_class = 2.  See
798     // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
799     addUInt(VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1, 2);
800   }
801   if (!DVal->isVariadic()) {
802     const DbgValueLocEntry *Entry = DVal->getLocEntries().begin();
803     if (Entry->isLocation()) {
804       addVariableAddress(DV, VariableDie, Entry->getLoc());
805     } else if (Entry->isInt()) {
806       auto *Expr = Single.getExpr();
807       if (Expr && Expr->getNumElements()) {
808         DIELoc *Loc = new (DIEValueAllocator) DIELoc;
809         DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
810         // If there is an expression, emit raw unsigned bytes.
811         DwarfExpr.addFragmentOffset(Expr);
812         DwarfExpr.addUnsignedConstant(Entry->getInt());
813         DwarfExpr.addExpression(Expr);
814         addBlock(VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
815         if (DwarfExpr.TagOffset)
816           addUInt(VariableDie, dwarf::DW_AT_LLVM_tag_offset,
817                   dwarf::DW_FORM_data1, *DwarfExpr.TagOffset);
818       } else
819         addConstantValue(VariableDie, Entry->getInt(), DV.getType());
820     } else if (Entry->isConstantFP()) {
821       addConstantFPValue(VariableDie, Entry->getConstantFP());
822     } else if (Entry->isConstantInt()) {
823       addConstantValue(VariableDie, Entry->getConstantInt(), DV.getType());
824     } else if (Entry->isTargetIndexLocation()) {
825       DIELoc *Loc = new (DIEValueAllocator) DIELoc;
826       DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
827       const DIBasicType *BT = dyn_cast<DIBasicType>(
828           static_cast<const Metadata *>(DV.getVariable()->getType()));
829       DwarfDebug::emitDebugLocValue(*Asm, BT, *DVal, DwarfExpr);
830       addBlock(VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
831     }
832     return;
833   }
834   // If any of the location entries are registers with the value 0,
835   // then the location is undefined.
836   if (any_of(DVal->getLocEntries(), [](const DbgValueLocEntry &Entry) {
837         return Entry.isLocation() && !Entry.getLoc().getReg();
838       }))
839     return;
840   const DIExpression *Expr = Single.getExpr();
841   assert(Expr && "Variadic Debug Value must have an Expression.");
842   DIELoc *Loc = new (DIEValueAllocator) DIELoc;
843   DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
844   DwarfExpr.addFragmentOffset(Expr);
845   DIExpressionCursor Cursor(Expr);
846   const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo();
847 
848   auto AddEntry = [&](const DbgValueLocEntry &Entry,
849                       DIExpressionCursor &Cursor) {
850     if (Entry.isLocation()) {
851       if (!DwarfExpr.addMachineRegExpression(TRI, Cursor,
852                                              Entry.getLoc().getReg()))
853         return false;
854     } else if (Entry.isInt()) {
855       // If there is an expression, emit raw unsigned bytes.
856       DwarfExpr.addUnsignedConstant(Entry.getInt());
857     } else if (Entry.isConstantFP()) {
858       // DwarfExpression does not support arguments wider than 64 bits
859       // (see PR52584).
860       // TODO: Consider chunking expressions containing overly wide
861       // arguments into separate pointer-sized fragment expressions.
862       APInt RawBytes = Entry.getConstantFP()->getValueAPF().bitcastToAPInt();
863       if (RawBytes.getBitWidth() > 64)
864         return false;
865       DwarfExpr.addUnsignedConstant(RawBytes.getZExtValue());
866     } else if (Entry.isConstantInt()) {
867       APInt RawBytes = Entry.getConstantInt()->getValue();
868       if (RawBytes.getBitWidth() > 64)
869         return false;
870       DwarfExpr.addUnsignedConstant(RawBytes.getZExtValue());
871     } else if (Entry.isTargetIndexLocation()) {
872       TargetIndexLocation Loc = Entry.getTargetIndexLocation();
873       // TODO TargetIndexLocation is a target-independent. Currently
874       // only the WebAssembly-specific encoding is supported.
875       assert(Asm->TM.getTargetTriple().isWasm());
876       DwarfExpr.addWasmLocation(Loc.Index, static_cast<uint64_t>(Loc.Offset));
877     } else {
878       llvm_unreachable("Unsupported Entry type.");
879     }
880     return true;
881   };
882 
883   if (!DwarfExpr.addExpression(
884           std::move(Cursor),
885           [&](unsigned Idx, DIExpressionCursor &Cursor) -> bool {
886             return AddEntry(DVal->getLocEntries()[Idx], Cursor);
887           }))
888     return;
889 
890   // Now attach the location information to the DIE.
891   addBlock(VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
892   if (DwarfExpr.TagOffset)
893     addUInt(VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
894             *DwarfExpr.TagOffset);
895 }
896 
897 void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
898     const Loc::Multi &Multi, const DbgVariable &DV, DIE &VariableDie) {
899   addLocationList(VariableDie, dwarf::DW_AT_location,
900                   Multi.getDebugLocListIndex());
901   auto TagOffset = Multi.getDebugLocListTagOffset();
902   if (TagOffset)
903     addUInt(VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
904             *TagOffset);
905 }
906 
907 void DwarfCompileUnit::applyConcreteDbgVariableAttributes(const Loc::MMI &MMI,
908                                                           const DbgVariable &DV,
909                                                           DIE &VariableDie) {
910   std::optional<unsigned> NVPTXAddressSpace;
911   DIELoc *Loc = new (DIEValueAllocator) DIELoc;
912   DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
913   for (const auto &Fragment : MMI.getFrameIndexExprs()) {
914     Register FrameReg;
915     const DIExpression *Expr = Fragment.Expr;
916     const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
917     StackOffset Offset =
918         TFI->getFrameIndexReference(*Asm->MF, Fragment.FI, FrameReg);
919     DwarfExpr.addFragmentOffset(Expr);
920 
921     auto *TRI = Asm->MF->getSubtarget().getRegisterInfo();
922     SmallVector<uint64_t, 8> Ops;
923     TRI->getOffsetOpcodes(Offset, Ops);
924 
925     // According to
926     // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
927     // cuda-gdb requires DW_AT_address_class for all variables to be
928     // able to correctly interpret address space of the variable
929     // address. Decode DW_OP_constu <DWARF Address Space> DW_OP_swap
930     // DW_OP_xderef sequence for the NVPTX + gdb target.
931     unsigned LocalNVPTXAddressSpace;
932     if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
933       const DIExpression *NewExpr =
934           DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace);
935       if (NewExpr != Expr) {
936         Expr = NewExpr;
937         NVPTXAddressSpace = LocalNVPTXAddressSpace;
938       }
939     }
940     if (Expr)
941       Ops.append(Expr->elements_begin(), Expr->elements_end());
942     DIExpressionCursor Cursor(Ops);
943     DwarfExpr.setMemoryLocationKind();
944     if (const MCSymbol *FrameSymbol = Asm->getFunctionFrameSymbol())
945       addOpAddress(*Loc, FrameSymbol);
946     else
947       DwarfExpr.addMachineRegExpression(
948           *Asm->MF->getSubtarget().getRegisterInfo(), Cursor, FrameReg);
949     DwarfExpr.addExpression(std::move(Cursor));
950   }
951   if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
952     // According to
953     // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
954     // cuda-gdb requires DW_AT_address_class for all variables to be
955     // able to correctly interpret address space of the variable
956     // address.
957     const unsigned NVPTX_ADDR_local_space = 6;
958     addUInt(VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
959             NVPTXAddressSpace.value_or(NVPTX_ADDR_local_space));
960   }
961   addBlock(VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
962   if (DwarfExpr.TagOffset)
963     addUInt(VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
964             *DwarfExpr.TagOffset);
965 }
966 
967 void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
968     const Loc::EntryValue &EntryValue, const DbgVariable &DV,
969     DIE &VariableDie) {
970   DIELoc *Loc = new (DIEValueAllocator) DIELoc;
971   DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
972   // Emit each expression as: EntryValue(Register) <other ops> <Fragment>.
973   for (auto [Register, Expr] : EntryValue.EntryValues) {
974     DwarfExpr.addFragmentOffset(&Expr);
975     DIExpressionCursor Cursor(Expr.getElements());
976     DwarfExpr.beginEntryValueExpression(Cursor);
977     DwarfExpr.addMachineRegExpression(
978         *Asm->MF->getSubtarget().getRegisterInfo(), Cursor, Register);
979     DwarfExpr.addExpression(std::move(Cursor));
980   }
981   addBlock(VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
982 }
983 
984 void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
985     const std::monostate &, const DbgVariable &DV, DIE &VariableDie) {}
986 
987 DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV,
988                                             const LexicalScope &Scope,
989                                             DIE *&ObjectPointer) {
990   auto Var = constructVariableDIE(DV, Scope.isAbstractScope());
991   if (DV.isObjectPointer())
992     ObjectPointer = Var;
993   return Var;
994 }
995 
996 DIE *DwarfCompileUnit::constructLabelDIE(DbgLabel &DL,
997                                          const LexicalScope &Scope) {
998   auto LabelDie = DIE::get(DIEValueAllocator, DL.getTag());
999   insertDIE(DL.getLabel(), LabelDie);
1000   DL.setDIE(*LabelDie);
1001 
1002   if (Scope.isAbstractScope())
1003     applyLabelAttributes(DL, *LabelDie);
1004 
1005   return LabelDie;
1006 }
1007 
1008 /// Return all DIVariables that appear in count: expressions.
1009 static SmallVector<const DIVariable *, 2> dependencies(DbgVariable *Var) {
1010   SmallVector<const DIVariable *, 2> Result;
1011   auto *Array = dyn_cast<DICompositeType>(Var->getType());
1012   if (!Array || Array->getTag() != dwarf::DW_TAG_array_type)
1013     return Result;
1014   if (auto *DLVar = Array->getDataLocation())
1015     Result.push_back(DLVar);
1016   if (auto *AsVar = Array->getAssociated())
1017     Result.push_back(AsVar);
1018   if (auto *AlVar = Array->getAllocated())
1019     Result.push_back(AlVar);
1020   for (auto *El : Array->getElements()) {
1021     if (auto *Subrange = dyn_cast<DISubrange>(El)) {
1022       if (auto Count = Subrange->getCount())
1023         if (auto *Dependency = dyn_cast_if_present<DIVariable *>(Count))
1024           Result.push_back(Dependency);
1025       if (auto LB = Subrange->getLowerBound())
1026         if (auto *Dependency = dyn_cast_if_present<DIVariable *>(LB))
1027           Result.push_back(Dependency);
1028       if (auto UB = Subrange->getUpperBound())
1029         if (auto *Dependency = dyn_cast_if_present<DIVariable *>(UB))
1030           Result.push_back(Dependency);
1031       if (auto ST = Subrange->getStride())
1032         if (auto *Dependency = dyn_cast_if_present<DIVariable *>(ST))
1033           Result.push_back(Dependency);
1034     } else if (auto *GenericSubrange = dyn_cast<DIGenericSubrange>(El)) {
1035       if (auto Count = GenericSubrange->getCount())
1036         if (auto *Dependency = dyn_cast_if_present<DIVariable *>(Count))
1037           Result.push_back(Dependency);
1038       if (auto LB = GenericSubrange->getLowerBound())
1039         if (auto *Dependency = dyn_cast_if_present<DIVariable *>(LB))
1040           Result.push_back(Dependency);
1041       if (auto UB = GenericSubrange->getUpperBound())
1042         if (auto *Dependency = dyn_cast_if_present<DIVariable *>(UB))
1043           Result.push_back(Dependency);
1044       if (auto ST = GenericSubrange->getStride())
1045         if (auto *Dependency = dyn_cast_if_present<DIVariable *>(ST))
1046           Result.push_back(Dependency);
1047     }
1048   }
1049   return Result;
1050 }
1051 
1052 /// Sort local variables so that variables appearing inside of helper
1053 /// expressions come first.
1054 static SmallVector<DbgVariable *, 8>
1055 sortLocalVars(SmallVectorImpl<DbgVariable *> &Input) {
1056   SmallVector<DbgVariable *, 8> Result;
1057   SmallVector<PointerIntPair<DbgVariable *, 1>, 8> WorkList;
1058   // Map back from a DIVariable to its containing DbgVariable.
1059   SmallDenseMap<const DILocalVariable *, DbgVariable *> DbgVar;
1060   // Set of DbgVariables in Result.
1061   SmallDenseSet<DbgVariable *, 8> Visited;
1062   // For cycle detection.
1063   SmallDenseSet<DbgVariable *, 8> Visiting;
1064 
1065   // Initialize the worklist and the DIVariable lookup table.
1066   for (auto *Var : reverse(Input)) {
1067     DbgVar.insert({Var->getVariable(), Var});
1068     WorkList.push_back({Var, 0});
1069   }
1070 
1071   // Perform a stable topological sort by doing a DFS.
1072   while (!WorkList.empty()) {
1073     auto Item = WorkList.back();
1074     DbgVariable *Var = Item.getPointer();
1075     bool visitedAllDependencies = Item.getInt();
1076     WorkList.pop_back();
1077 
1078     assert(Var);
1079 
1080     // Already handled.
1081     if (Visited.count(Var))
1082       continue;
1083 
1084     // Add to Result if all dependencies are visited.
1085     if (visitedAllDependencies) {
1086       Visited.insert(Var);
1087       Result.push_back(Var);
1088       continue;
1089     }
1090 
1091     // Detect cycles.
1092     auto Res = Visiting.insert(Var);
1093     if (!Res.second) {
1094       assert(false && "dependency cycle in local variables");
1095       return Result;
1096     }
1097 
1098     // Push dependencies and this node onto the worklist, so that this node is
1099     // visited again after all of its dependencies are handled.
1100     WorkList.push_back({Var, 1});
1101     for (const auto *Dependency : dependencies(Var)) {
1102       // Don't add dependency if it is in a different lexical scope or a global.
1103       if (const auto *Dep = dyn_cast<const DILocalVariable>(Dependency))
1104         if (DbgVariable *Var = DbgVar.lookup(Dep))
1105           WorkList.push_back({Var, 0});
1106     }
1107   }
1108   return Result;
1109 }
1110 
1111 DIE &DwarfCompileUnit::constructSubprogramScopeDIE(const DISubprogram *Sub,
1112                                                    LexicalScope *Scope,
1113                                                    MCSymbol *LineTableSym) {
1114   DIE &ScopeDIE = updateSubprogramScopeDIE(Sub, LineTableSym);
1115 
1116   if (Scope) {
1117     assert(!Scope->getInlinedAt());
1118     assert(!Scope->isAbstractScope());
1119     // Collect lexical scope children first.
1120     // ObjectPointer might be a local (non-argument) local variable if it's a
1121     // block's synthetic this pointer.
1122     if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE))
1123       addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer);
1124   }
1125 
1126   // If this is a variadic function, add an unspecified parameter.
1127   DITypeRefArray FnArgs = Sub->getType()->getTypeArray();
1128 
1129   // If we have a single element of null, it is a function that returns void.
1130   // If we have more than one elements and the last one is null, it is a
1131   // variadic function.
1132   if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] &&
1133       !includeMinimalInlineScopes())
1134     ScopeDIE.addChild(
1135         DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters));
1136 
1137   return ScopeDIE;
1138 }
1139 
1140 DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope,
1141                                                  DIE &ScopeDIE) {
1142   DIE *ObjectPointer = nullptr;
1143 
1144   // Emit function arguments (order is significant).
1145   auto Vars = DU->getScopeVariables().lookup(Scope);
1146   for (auto &DV : Vars.Args)
1147     ScopeDIE.addChild(constructVariableDIE(*DV.second, *Scope, ObjectPointer));
1148 
1149   // Emit local variables.
1150   auto Locals = sortLocalVars(Vars.Locals);
1151   for (DbgVariable *DV : Locals)
1152     ScopeDIE.addChild(constructVariableDIE(*DV, *Scope, ObjectPointer));
1153 
1154   // Emit labels.
1155   for (DbgLabel *DL : DU->getScopeLabels().lookup(Scope))
1156     ScopeDIE.addChild(constructLabelDIE(*DL, *Scope));
1157 
1158   // Track other local entities (skipped in gmlt-like data).
1159   // This creates mapping between CU and a set of local declarations that
1160   // should be emitted for subprograms in this CU.
1161   if (!includeMinimalInlineScopes() && !Scope->getInlinedAt()) {
1162     auto &LocalDecls = DD->getLocalDeclsForScope(Scope->getScopeNode());
1163     DeferredLocalDecls.insert(LocalDecls.begin(), LocalDecls.end());
1164   }
1165 
1166   // Emit inner lexical scopes.
1167   auto skipLexicalScope = [this](LexicalScope *S) -> bool {
1168     if (isa<DISubprogram>(S->getScopeNode()))
1169       return false;
1170     auto Vars = DU->getScopeVariables().lookup(S);
1171     if (!Vars.Args.empty() || !Vars.Locals.empty())
1172       return false;
1173     return includeMinimalInlineScopes() ||
1174            DD->getLocalDeclsForScope(S->getScopeNode()).empty();
1175   };
1176   for (LexicalScope *LS : Scope->getChildren()) {
1177     // If the lexical block doesn't have non-scope children, skip
1178     // its emission and put its children directly to the parent scope.
1179     if (skipLexicalScope(LS))
1180       createAndAddScopeChildren(LS, ScopeDIE);
1181     else
1182       constructScopeDIE(LS, ScopeDIE);
1183   }
1184 
1185   return ObjectPointer;
1186 }
1187 
1188 void DwarfCompileUnit::constructAbstractSubprogramScopeDIE(
1189     LexicalScope *Scope) {
1190   auto *SP = cast<DISubprogram>(Scope->getScopeNode());
1191   if (getAbstractScopeDIEs().count(SP))
1192     return;
1193 
1194   DIE *ContextDIE;
1195   DwarfCompileUnit *ContextCU = this;
1196 
1197   if (includeMinimalInlineScopes())
1198     ContextDIE = &getUnitDie();
1199   // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with
1200   // the important distinction that the debug node is not associated with the
1201   // DIE (since the debug node will be associated with the concrete DIE, if
1202   // any). It could be refactored to some common utility function.
1203   else if (auto *SPDecl = SP->getDeclaration()) {
1204     ContextDIE = &getUnitDie();
1205     getOrCreateSubprogramDIE(SPDecl);
1206   } else {
1207     ContextDIE = getOrCreateContextDIE(SP->getScope());
1208     // The scope may be shared with a subprogram that has already been
1209     // constructed in another CU, in which case we need to construct this
1210     // subprogram in the same CU.
1211     ContextCU = DD->lookupCU(ContextDIE->getUnitDie());
1212   }
1213 
1214   // Passing null as the associated node because the abstract definition
1215   // shouldn't be found by lookup.
1216   DIE &AbsDef = ContextCU->createAndAddDIE(dwarf::DW_TAG_subprogram,
1217                                            *ContextDIE, nullptr);
1218 
1219   // Store the DIE before creating children.
1220   ContextCU->getAbstractScopeDIEs()[SP] = &AbsDef;
1221 
1222   ContextCU->applySubprogramAttributesToDefinition(SP, AbsDef);
1223   ContextCU->addSInt(AbsDef, dwarf::DW_AT_inline,
1224                      DD->getDwarfVersion() <= 4 ? std::optional<dwarf::Form>()
1225                                                 : dwarf::DW_FORM_implicit_const,
1226                      dwarf::DW_INL_inlined);
1227   if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren(Scope, AbsDef))
1228     ContextCU->addDIEEntry(AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
1229 }
1230 
1231 bool DwarfCompileUnit::useGNUAnalogForDwarf5Feature() const {
1232   return DD->getDwarfVersion() <= 4 && !DD->tuneForLLDB();
1233 }
1234 
1235 dwarf::Tag DwarfCompileUnit::getDwarf5OrGNUTag(dwarf::Tag Tag) const {
1236   if (!useGNUAnalogForDwarf5Feature())
1237     return Tag;
1238   switch (Tag) {
1239   case dwarf::DW_TAG_call_site:
1240     return dwarf::DW_TAG_GNU_call_site;
1241   case dwarf::DW_TAG_call_site_parameter:
1242     return dwarf::DW_TAG_GNU_call_site_parameter;
1243   default:
1244     llvm_unreachable("DWARF5 tag with no GNU analog");
1245   }
1246 }
1247 
1248 dwarf::Attribute
1249 DwarfCompileUnit::getDwarf5OrGNUAttr(dwarf::Attribute Attr) const {
1250   if (!useGNUAnalogForDwarf5Feature())
1251     return Attr;
1252   switch (Attr) {
1253   case dwarf::DW_AT_call_all_calls:
1254     return dwarf::DW_AT_GNU_all_call_sites;
1255   case dwarf::DW_AT_call_target:
1256     return dwarf::DW_AT_GNU_call_site_target;
1257   case dwarf::DW_AT_call_origin:
1258     return dwarf::DW_AT_abstract_origin;
1259   case dwarf::DW_AT_call_return_pc:
1260     return dwarf::DW_AT_low_pc;
1261   case dwarf::DW_AT_call_value:
1262     return dwarf::DW_AT_GNU_call_site_value;
1263   case dwarf::DW_AT_call_tail_call:
1264     return dwarf::DW_AT_GNU_tail_call;
1265   default:
1266     llvm_unreachable("DWARF5 attribute with no GNU analog");
1267   }
1268 }
1269 
1270 dwarf::LocationAtom
1271 DwarfCompileUnit::getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const {
1272   if (!useGNUAnalogForDwarf5Feature())
1273     return Loc;
1274   switch (Loc) {
1275   case dwarf::DW_OP_entry_value:
1276     return dwarf::DW_OP_GNU_entry_value;
1277   default:
1278     llvm_unreachable("DWARF5 location atom with no GNU analog");
1279   }
1280 }
1281 
1282 DIE &DwarfCompileUnit::constructCallSiteEntryDIE(DIE &ScopeDIE,
1283                                                  const DISubprogram *CalleeSP,
1284                                                  bool IsTail,
1285                                                  const MCSymbol *PCAddr,
1286                                                  const MCSymbol *CallAddr,
1287                                                  unsigned CallReg) {
1288   // Insert a call site entry DIE within ScopeDIE.
1289   DIE &CallSiteDIE = createAndAddDIE(getDwarf5OrGNUTag(dwarf::DW_TAG_call_site),
1290                                      ScopeDIE, nullptr);
1291 
1292   if (CallReg) {
1293     // Indirect call.
1294     addAddress(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_target),
1295                MachineLocation(CallReg));
1296   } else {
1297     DIE *CalleeDIE = getOrCreateSubprogramDIE(CalleeSP);
1298     assert(CalleeDIE && "Could not create DIE for call site entry origin");
1299     if (AddLinkageNamesToDeclCallOriginsForTuning(DD) &&
1300         !CalleeSP->isDefinition() &&
1301         !CalleeDIE->findAttribute(dwarf::DW_AT_linkage_name)) {
1302       addLinkageName(*CalleeDIE, CalleeSP->getLinkageName());
1303     }
1304 
1305     addDIEEntry(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_origin),
1306                 *CalleeDIE);
1307   }
1308 
1309   if (IsTail) {
1310     // Attach DW_AT_call_tail_call to tail calls for standards compliance.
1311     addFlag(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_tail_call));
1312 
1313     // Attach the address of the branch instruction to allow the debugger to
1314     // show where the tail call occurred. This attribute has no GNU analog.
1315     //
1316     // GDB works backwards from non-standard usage of DW_AT_low_pc (in DWARF4
1317     // mode -- equivalently, in DWARF5 mode, DW_AT_call_return_pc) at tail-call
1318     // site entries to figure out the PC of tail-calling branch instructions.
1319     // This means it doesn't need the compiler to emit DW_AT_call_pc, so we
1320     // don't emit it here.
1321     //
1322     // There's no need to tie non-GDB debuggers to this non-standardness, as it
1323     // adds unnecessary complexity to the debugger. For non-GDB debuggers, emit
1324     // the standard DW_AT_call_pc info.
1325     if (!useGNUAnalogForDwarf5Feature())
1326       addLabelAddress(CallSiteDIE, dwarf::DW_AT_call_pc, CallAddr);
1327   }
1328 
1329   // Attach the return PC to allow the debugger to disambiguate call paths
1330   // from one function to another.
1331   //
1332   // The return PC is only really needed when the call /isn't/ a tail call, but
1333   // GDB expects it in DWARF4 mode, even for tail calls (see the comment above
1334   // the DW_AT_call_pc emission logic for an explanation).
1335   if (!IsTail || useGNUAnalogForDwarf5Feature()) {
1336     assert(PCAddr && "Missing return PC information for a call");
1337     addLabelAddress(CallSiteDIE,
1338                     getDwarf5OrGNUAttr(dwarf::DW_AT_call_return_pc), PCAddr);
1339   }
1340 
1341   return CallSiteDIE;
1342 }
1343 
1344 void DwarfCompileUnit::constructCallSiteParmEntryDIEs(
1345     DIE &CallSiteDIE, SmallVector<DbgCallSiteParam, 4> &Params) {
1346   for (const auto &Param : Params) {
1347     unsigned Register = Param.getRegister();
1348     auto CallSiteDieParam =
1349         DIE::get(DIEValueAllocator,
1350                  getDwarf5OrGNUTag(dwarf::DW_TAG_call_site_parameter));
1351     insertDIE(CallSiteDieParam);
1352     addAddress(*CallSiteDieParam, dwarf::DW_AT_location,
1353                MachineLocation(Register));
1354 
1355     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1356     DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
1357     DwarfExpr.setCallSiteParamValueFlag();
1358 
1359     DwarfDebug::emitDebugLocValue(*Asm, nullptr, Param.getValue(), DwarfExpr);
1360 
1361     addBlock(*CallSiteDieParam, getDwarf5OrGNUAttr(dwarf::DW_AT_call_value),
1362              DwarfExpr.finalize());
1363 
1364     CallSiteDIE.addChild(CallSiteDieParam);
1365   }
1366 }
1367 
1368 DIE *DwarfCompileUnit::constructImportedEntityDIE(
1369     const DIImportedEntity *Module) {
1370   DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag());
1371   insertDIE(Module, IMDie);
1372   DIE *EntityDie;
1373   auto *Entity = Module->getEntity();
1374   if (auto *NS = dyn_cast<DINamespace>(Entity))
1375     EntityDie = getOrCreateNameSpace(NS);
1376   else if (auto *M = dyn_cast<DIModule>(Entity))
1377     EntityDie = getOrCreateModule(M);
1378   else if (auto *SP = dyn_cast<DISubprogram>(Entity)) {
1379     // If there is an abstract subprogram, refer to it. Note that this assumes
1380     // that all the abstract subprograms have been already created (which is
1381     // correct until imported entities get emitted in DwarfDebug::endModule()).
1382     if (auto *AbsSPDie = getAbstractScopeDIEs().lookup(SP))
1383       EntityDie = AbsSPDie;
1384     else
1385       EntityDie = getOrCreateSubprogramDIE(SP);
1386   } else if (auto *T = dyn_cast<DIType>(Entity))
1387     EntityDie = getOrCreateTypeDIE(T);
1388   else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity))
1389     EntityDie = getOrCreateGlobalVariableDIE(GV, {});
1390   else if (auto *IE = dyn_cast<DIImportedEntity>(Entity))
1391     EntityDie = getOrCreateImportedEntityDIE(IE);
1392   else
1393     EntityDie = getDIE(Entity);
1394   assert(EntityDie);
1395   addSourceLine(*IMDie, Module->getLine(), Module->getFile());
1396   addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie);
1397   StringRef Name = Module->getName();
1398   if (!Name.empty()) {
1399     addString(*IMDie, dwarf::DW_AT_name, Name);
1400 
1401     // FIXME: if consumers ever start caring about handling
1402     // unnamed import declarations such as `using ::nullptr_t`
1403     // or `using namespace std::ranges`, we could add the
1404     // import declaration into the accelerator table with the
1405     // name being the one of the entity being imported.
1406     DD->addAccelNamespace(*this, CUNode->getNameTableKind(), Name, *IMDie);
1407   }
1408 
1409   // This is for imported module with renamed entities (such as variables and
1410   // subprograms).
1411   DINodeArray Elements = Module->getElements();
1412   for (const auto *Element : Elements) {
1413     if (!Element)
1414       continue;
1415     IMDie->addChild(
1416         constructImportedEntityDIE(cast<DIImportedEntity>(Element)));
1417   }
1418 
1419   return IMDie;
1420 }
1421 
1422 DIE *DwarfCompileUnit::getOrCreateImportedEntityDIE(
1423     const DIImportedEntity *IE) {
1424 
1425   // Check for pre-existence.
1426   if (DIE *Die = getDIE(IE))
1427     return Die;
1428 
1429   DIE *ContextDIE = getOrCreateContextDIE(IE->getScope());
1430   assert(ContextDIE && "Empty scope for the imported entity!");
1431 
1432   DIE *IMDie = constructImportedEntityDIE(IE);
1433   ContextDIE->addChild(IMDie);
1434   return IMDie;
1435 }
1436 
1437 void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) {
1438   DIE *D = getDIE(SP);
1439   if (DIE *AbsSPDIE = getAbstractScopeDIEs().lookup(SP)) {
1440     if (D)
1441       // If this subprogram has an abstract definition, reference that
1442       addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE);
1443   } else {
1444     assert(D || includeMinimalInlineScopes());
1445     if (D)
1446       // And attach the attributes
1447       applySubprogramAttributesToDefinition(SP, *D);
1448   }
1449 }
1450 
1451 void DwarfCompileUnit::finishEntityDefinition(const DbgEntity *Entity) {
1452   DbgEntity *AbsEntity = getExistingAbstractEntity(Entity->getEntity());
1453 
1454   auto *Die = Entity->getDIE();
1455   /// Label may be used to generate DW_AT_low_pc, so put it outside
1456   /// if/else block.
1457   const DbgLabel *Label = nullptr;
1458   if (AbsEntity && AbsEntity->getDIE()) {
1459     addDIEEntry(*Die, dwarf::DW_AT_abstract_origin, *AbsEntity->getDIE());
1460     Label = dyn_cast<const DbgLabel>(Entity);
1461   } else {
1462     if (const DbgVariable *Var = dyn_cast<const DbgVariable>(Entity))
1463       applyCommonDbgVariableAttributes(*Var, *Die);
1464     else if ((Label = dyn_cast<const DbgLabel>(Entity)))
1465       applyLabelAttributes(*Label, *Die);
1466     else
1467       llvm_unreachable("DbgEntity must be DbgVariable or DbgLabel.");
1468   }
1469 
1470   if (!Label)
1471     return;
1472 
1473   const auto *Sym = Label->getSymbol();
1474   if (!Sym)
1475     return;
1476 
1477   addLabelAddress(*Die, dwarf::DW_AT_low_pc, Sym);
1478 
1479   // A TAG_label with a name and an AT_low_pc must be placed in debug_names.
1480   if (StringRef Name = Label->getName(); !Name.empty())
1481     getDwarfDebug().addAccelName(*this, CUNode->getNameTableKind(), Name, *Die);
1482 }
1483 
1484 DbgEntity *DwarfCompileUnit::getExistingAbstractEntity(const DINode *Node) {
1485   auto &AbstractEntities = getAbstractEntities();
1486   auto I = AbstractEntities.find(Node);
1487   if (I != AbstractEntities.end())
1488     return I->second.get();
1489   return nullptr;
1490 }
1491 
1492 void DwarfCompileUnit::createAbstractEntity(const DINode *Node,
1493                                             LexicalScope *Scope) {
1494   assert(Scope && Scope->isAbstractScope());
1495   auto &Entity = getAbstractEntities()[Node];
1496   if (isa<const DILocalVariable>(Node)) {
1497     Entity = std::make_unique<DbgVariable>(cast<const DILocalVariable>(Node),
1498                                            nullptr /* IA */);
1499     DU->addScopeVariable(Scope, cast<DbgVariable>(Entity.get()));
1500   } else if (isa<const DILabel>(Node)) {
1501     Entity = std::make_unique<DbgLabel>(
1502                         cast<const DILabel>(Node), nullptr /* IA */);
1503     DU->addScopeLabel(Scope, cast<DbgLabel>(Entity.get()));
1504   }
1505 }
1506 
1507 void DwarfCompileUnit::emitHeader(bool UseOffsets) {
1508   // Don't bother labeling the .dwo unit, as its offset isn't used.
1509   if (!Skeleton && !DD->useSectionsAsReferences()) {
1510     LabelBegin = Asm->createTempSymbol("cu_begin");
1511     Asm->OutStreamer->emitLabel(LabelBegin);
1512   }
1513 
1514   dwarf::UnitType UT = Skeleton ? dwarf::DW_UT_split_compile
1515                                 : DD->useSplitDwarf() ? dwarf::DW_UT_skeleton
1516                                                       : dwarf::DW_UT_compile;
1517   DwarfUnit::emitCommonHeader(UseOffsets, UT);
1518   if (DD->getDwarfVersion() >= 5 && UT != dwarf::DW_UT_compile)
1519     Asm->emitInt64(getDWOId());
1520 }
1521 
1522 bool DwarfCompileUnit::hasDwarfPubSections() const {
1523   switch (CUNode->getNameTableKind()) {
1524   case DICompileUnit::DebugNameTableKind::None:
1525     return false;
1526     // Opting in to GNU Pubnames/types overrides the default to ensure these are
1527     // generated for things like Gold's gdb_index generation.
1528   case DICompileUnit::DebugNameTableKind::GNU:
1529     return true;
1530   case DICompileUnit::DebugNameTableKind::Apple:
1531     return false;
1532   case DICompileUnit::DebugNameTableKind::Default:
1533     return DD->tuneForGDB() && !includeMinimalInlineScopes() &&
1534            !CUNode->isDebugDirectivesOnly() &&
1535            DD->getAccelTableKind() != AccelTableKind::Apple &&
1536            DD->getDwarfVersion() < 5;
1537   }
1538   llvm_unreachable("Unhandled DICompileUnit::DebugNameTableKind enum");
1539 }
1540 
1541 /// addGlobalName - Add a new global name to the compile unit.
1542 void DwarfCompileUnit::addGlobalName(StringRef Name, const DIE &Die,
1543                                      const DIScope *Context) {
1544   if (!hasDwarfPubSections())
1545     return;
1546   std::string FullName = getParentContextString(Context) + Name.str();
1547   GlobalNames[FullName] = &Die;
1548 }
1549 
1550 void DwarfCompileUnit::addGlobalNameForTypeUnit(StringRef Name,
1551                                                 const DIScope *Context) {
1552   if (!hasDwarfPubSections())
1553     return;
1554   std::string FullName = getParentContextString(Context) + Name.str();
1555   // Insert, allowing the entry to remain as-is if it's already present
1556   // This way the CU-level type DIE is preferred over the "can't describe this
1557   // type as a unit offset because it's not really in the CU at all, it's only
1558   // in a type unit"
1559   GlobalNames.insert(std::make_pair(std::move(FullName), &getUnitDie()));
1560 }
1561 
1562 /// Add a new global type to the unit.
1563 void DwarfCompileUnit::addGlobalTypeImpl(const DIType *Ty, const DIE &Die,
1564                                          const DIScope *Context) {
1565   if (!hasDwarfPubSections())
1566     return;
1567   std::string FullName = getParentContextString(Context) + Ty->getName().str();
1568   GlobalTypes[FullName] = &Die;
1569 }
1570 
1571 void DwarfCompileUnit::addGlobalTypeUnitType(const DIType *Ty,
1572                                              const DIScope *Context) {
1573   if (!hasDwarfPubSections())
1574     return;
1575   std::string FullName = getParentContextString(Context) + Ty->getName().str();
1576   // Insert, allowing the entry to remain as-is if it's already present
1577   // This way the CU-level type DIE is preferred over the "can't describe this
1578   // type as a unit offset because it's not really in the CU at all, it's only
1579   // in a type unit"
1580   GlobalTypes.insert(std::make_pair(std::move(FullName), &getUnitDie()));
1581 }
1582 
1583 void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
1584                                           MachineLocation Location) {
1585   auto *Single = std::get_if<Loc::Single>(&DV);
1586   if (Single && Single->getExpr())
1587     addComplexAddress(Single->getExpr(), Die, dwarf::DW_AT_location, Location);
1588   else
1589     addAddress(Die, dwarf::DW_AT_location, Location);
1590 }
1591 
1592 /// Add an address attribute to a die based on the location provided.
1593 void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
1594                                   const MachineLocation &Location) {
1595   DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1596   DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
1597   if (Location.isIndirect())
1598     DwarfExpr.setMemoryLocationKind();
1599 
1600   DIExpressionCursor Cursor({});
1601   const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo();
1602   if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
1603     return;
1604   DwarfExpr.addExpression(std::move(Cursor));
1605 
1606   // Now attach the location information to the DIE.
1607   addBlock(Die, Attribute, DwarfExpr.finalize());
1608 
1609   if (DwarfExpr.TagOffset)
1610     addUInt(Die, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
1611             *DwarfExpr.TagOffset);
1612 }
1613 
1614 /// Start with the address based on the location provided, and generate the
1615 /// DWARF information necessary to find the actual variable given the extra
1616 /// address information encoded in the DbgVariable, starting from the starting
1617 /// location.  Add the DWARF information to the die.
1618 void DwarfCompileUnit::addComplexAddress(const DIExpression *DIExpr, DIE &Die,
1619                                          dwarf::Attribute Attribute,
1620                                          const MachineLocation &Location) {
1621   DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1622   DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
1623   DwarfExpr.addFragmentOffset(DIExpr);
1624   DwarfExpr.setLocation(Location, DIExpr);
1625 
1626   DIExpressionCursor Cursor(DIExpr);
1627 
1628   if (DIExpr->isEntryValue())
1629     DwarfExpr.beginEntryValueExpression(Cursor);
1630 
1631   const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo();
1632   if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
1633     return;
1634   DwarfExpr.addExpression(std::move(Cursor));
1635 
1636   // Now attach the location information to the DIE.
1637   addBlock(Die, Attribute, DwarfExpr.finalize());
1638 
1639   if (DwarfExpr.TagOffset)
1640     addUInt(Die, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
1641             *DwarfExpr.TagOffset);
1642 }
1643 
1644 /// Add a Dwarf loclistptr attribute data and value.
1645 void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute,
1646                                        unsigned Index) {
1647   dwarf::Form Form = (DD->getDwarfVersion() >= 5)
1648                          ? dwarf::DW_FORM_loclistx
1649                          : DD->getDwarfSectionOffsetForm();
1650   addAttribute(Die, Attribute, Form, DIELocList(Index));
1651 }
1652 
1653 void DwarfCompileUnit::applyCommonDbgVariableAttributes(const DbgVariable &Var,
1654                                                         DIE &VariableDie) {
1655   StringRef Name = Var.getName();
1656   if (!Name.empty())
1657     addString(VariableDie, dwarf::DW_AT_name, Name);
1658   const auto *DIVar = Var.getVariable();
1659   if (DIVar) {
1660     if (uint32_t AlignInBytes = DIVar->getAlignInBytes())
1661       addUInt(VariableDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1662               AlignInBytes);
1663     addAnnotation(VariableDie, DIVar->getAnnotations());
1664   }
1665 
1666   addSourceLine(VariableDie, DIVar);
1667   addType(VariableDie, Var.getType());
1668   if (Var.isArtificial())
1669     addFlag(VariableDie, dwarf::DW_AT_artificial);
1670 }
1671 
1672 void DwarfCompileUnit::applyLabelAttributes(const DbgLabel &Label,
1673                                             DIE &LabelDie) {
1674   StringRef Name = Label.getName();
1675   if (!Name.empty())
1676     addString(LabelDie, dwarf::DW_AT_name, Name);
1677   const auto *DILabel = Label.getLabel();
1678   addSourceLine(LabelDie, DILabel);
1679 }
1680 
1681 /// Add a Dwarf expression attribute data and value.
1682 void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form,
1683                                const MCExpr *Expr) {
1684   addAttribute(Die, (dwarf::Attribute)0, Form, DIEExpr(Expr));
1685 }
1686 
1687 void DwarfCompileUnit::applySubprogramAttributesToDefinition(
1688     const DISubprogram *SP, DIE &SPDie) {
1689   auto *SPDecl = SP->getDeclaration();
1690   auto *Context = SPDecl ? SPDecl->getScope() : SP->getScope();
1691   applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes());
1692   addGlobalName(SP->getName(), SPDie, Context);
1693 }
1694 
1695 bool DwarfCompileUnit::isDwoUnit() const {
1696   return DD->useSplitDwarf() && Skeleton;
1697 }
1698 
1699 void DwarfCompileUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
1700   constructTypeDIE(D, CTy);
1701 }
1702 
1703 bool DwarfCompileUnit::includeMinimalInlineScopes() const {
1704   return getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly ||
1705          (DD->useSplitDwarf() && !Skeleton);
1706 }
1707 
1708 bool DwarfCompileUnit::emitFuncLineTableOffsets() const {
1709   return EmitFuncLineTableOffsetsOption;
1710 }
1711 
1712 void DwarfCompileUnit::addAddrTableBase() {
1713   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1714   MCSymbol *Label = DD->getAddressPool().getLabel();
1715   addSectionLabel(getUnitDie(),
1716                   DD->getDwarfVersion() >= 5 ? dwarf::DW_AT_addr_base
1717                                              : dwarf::DW_AT_GNU_addr_base,
1718                   Label, TLOF.getDwarfAddrSection()->getBeginSymbol());
1719 }
1720 
1721 void DwarfCompileUnit::addBaseTypeRef(DIEValueList &Die, int64_t Idx) {
1722   addAttribute(Die, (dwarf::Attribute)0, dwarf::DW_FORM_udata,
1723                new (DIEValueAllocator) DIEBaseTypeRef(this, Idx));
1724 }
1725 
1726 void DwarfCompileUnit::createBaseTypeDIEs() {
1727   // Insert the base_type DIEs directly after the CU so that their offsets will
1728   // fit in the fixed size ULEB128 used inside the location expressions.
1729   // Maintain order by iterating backwards and inserting to the front of CU
1730   // child list.
1731   for (auto &Btr : reverse(ExprRefedBaseTypes)) {
1732     DIE &Die = getUnitDie().addChildFront(
1733       DIE::get(DIEValueAllocator, dwarf::DW_TAG_base_type));
1734     SmallString<32> Str;
1735     addString(Die, dwarf::DW_AT_name,
1736               Twine(dwarf::AttributeEncodingString(Btr.Encoding) +
1737                     "_" + Twine(Btr.BitSize)).toStringRef(Str));
1738     addUInt(Die, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, Btr.Encoding);
1739     // Round up to smallest number of bytes that contains this number of bits.
1740     addUInt(Die, dwarf::DW_AT_byte_size, std::nullopt,
1741             divideCeil(Btr.BitSize, 8));
1742 
1743     Btr.Die = &Die;
1744   }
1745 }
1746 
1747 DIE *DwarfCompileUnit::getLexicalBlockDIE(const DILexicalBlock *LB) {
1748   // Assume if there is an abstract tree all the DIEs are already emitted.
1749   bool isAbstract = getAbstractScopeDIEs().count(LB->getSubprogram());
1750   if (isAbstract && getAbstractScopeDIEs().count(LB))
1751     return getAbstractScopeDIEs()[LB];
1752   assert(!isAbstract && "Missed lexical block DIE in abstract tree!");
1753 
1754   // Return a concrete DIE if it exists or nullptr otherwise.
1755   return LexicalBlockDIEs.lookup(LB);
1756 }
1757 
1758 DIE *DwarfCompileUnit::getOrCreateContextDIE(const DIScope *Context) {
1759   if (isa_and_nonnull<DILocalScope>(Context)) {
1760     if (auto *LFScope = dyn_cast<DILexicalBlockFile>(Context))
1761       Context = LFScope->getNonLexicalBlockFileScope();
1762     if (auto *LScope = dyn_cast<DILexicalBlock>(Context))
1763       return getLexicalBlockDIE(LScope);
1764 
1765     // Otherwise the context must be a DISubprogram.
1766     auto *SPScope = cast<DISubprogram>(Context);
1767     if (getAbstractScopeDIEs().count(SPScope))
1768       return getAbstractScopeDIEs()[SPScope];
1769   }
1770   return DwarfUnit::getOrCreateContextDIE(Context);
1771 }
1772