xref: /llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp (revision 19032bfe87fa0f4a3a7b3e68daafc93331b71e0d)
1 //===- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing dwarf debug info into asm files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "DwarfDebug.h"
14 #include "ByteStreamer.h"
15 #include "DIEHash.h"
16 #include "DwarfCompileUnit.h"
17 #include "DwarfExpression.h"
18 #include "DwarfUnit.h"
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DIE.h"
25 #include "llvm/CodeGen/LexicalScopes.h"
26 #include "llvm/CodeGen/MachineBasicBlock.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineOperand.h"
30 #include "llvm/CodeGen/TargetInstrInfo.h"
31 #include "llvm/CodeGen/TargetLowering.h"
32 #include "llvm/CodeGen/TargetRegisterInfo.h"
33 #include "llvm/CodeGen/TargetSubtargetInfo.h"
34 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
35 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
36 #include "llvm/IR/Constants.h"
37 #include "llvm/IR/DebugInfoMetadata.h"
38 #include "llvm/IR/Function.h"
39 #include "llvm/IR/GlobalVariable.h"
40 #include "llvm/IR/Module.h"
41 #include "llvm/MC/MCAsmInfo.h"
42 #include "llvm/MC/MCContext.h"
43 #include "llvm/MC/MCSection.h"
44 #include "llvm/MC/MCStreamer.h"
45 #include "llvm/MC/MCSymbol.h"
46 #include "llvm/MC/MCTargetOptions.h"
47 #include "llvm/MC/MachineLocation.h"
48 #include "llvm/Support/Casting.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/Debug.h"
51 #include "llvm/Support/ErrorHandling.h"
52 #include "llvm/Support/MD5.h"
53 #include "llvm/Support/raw_ostream.h"
54 #include "llvm/Target/TargetLoweringObjectFile.h"
55 #include "llvm/Target/TargetMachine.h"
56 #include "llvm/TargetParser/Triple.h"
57 #include <algorithm>
58 #include <cstddef>
59 #include <iterator>
60 #include <optional>
61 #include <string>
62 
63 using namespace llvm;
64 
65 #define DEBUG_TYPE "dwarfdebug"
66 
67 STATISTIC(NumCSParams, "Number of dbg call site params created");
68 
69 static cl::opt<bool> UseDwarfRangesBaseAddressSpecifier(
70     "use-dwarf-ranges-base-address-specifier", cl::Hidden,
71     cl::desc("Use base address specifiers in debug_ranges"), cl::init(false));
72 
73 static cl::opt<bool> GenerateARangeSection("generate-arange-section",
74                                            cl::Hidden,
75                                            cl::desc("Generate dwarf aranges"),
76                                            cl::init(false));
77 
78 static cl::opt<bool>
79     GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
80                            cl::desc("Generate DWARF4 type units."),
81                            cl::init(false));
82 
83 static cl::opt<bool> SplitDwarfCrossCuReferences(
84     "split-dwarf-cross-cu-references", cl::Hidden,
85     cl::desc("Enable cross-cu references in DWO files"), cl::init(false));
86 
87 enum DefaultOnOff { Default, Enable, Disable };
88 
89 static cl::opt<DefaultOnOff> UnknownLocations(
90     "use-unknown-locations", cl::Hidden,
91     cl::desc("Make an absence of debug location information explicit."),
92     cl::values(clEnumVal(Default, "At top of block or after label"),
93                clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")),
94     cl::init(Default));
95 
96 static cl::opt<AccelTableKind> AccelTables(
97     "accel-tables", cl::Hidden, cl::desc("Output dwarf accelerator tables."),
98     cl::values(clEnumValN(AccelTableKind::Default, "Default",
99                           "Default for platform"),
100                clEnumValN(AccelTableKind::None, "Disable", "Disabled."),
101                clEnumValN(AccelTableKind::Apple, "Apple", "Apple"),
102                clEnumValN(AccelTableKind::Dwarf, "Dwarf", "DWARF")),
103     cl::init(AccelTableKind::Default));
104 
105 static cl::opt<DefaultOnOff>
106 DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden,
107                  cl::desc("Use inlined strings rather than string section."),
108                  cl::values(clEnumVal(Default, "Default for platform"),
109                             clEnumVal(Enable, "Enabled"),
110                             clEnumVal(Disable, "Disabled")),
111                  cl::init(Default));
112 
113 static cl::opt<bool>
114     NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden,
115                          cl::desc("Disable emission .debug_ranges section."),
116                          cl::init(false));
117 
118 static cl::opt<DefaultOnOff> DwarfSectionsAsReferences(
119     "dwarf-sections-as-references", cl::Hidden,
120     cl::desc("Use sections+offset as references rather than labels."),
121     cl::values(clEnumVal(Default, "Default for platform"),
122                clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
123     cl::init(Default));
124 
125 static cl::opt<bool>
126     UseGNUDebugMacro("use-gnu-debug-macro", cl::Hidden,
127                      cl::desc("Emit the GNU .debug_macro format with DWARF <5"),
128                      cl::init(false));
129 
130 static cl::opt<DefaultOnOff> DwarfOpConvert(
131     "dwarf-op-convert", cl::Hidden,
132     cl::desc("Enable use of the DWARFv5 DW_OP_convert operator"),
133     cl::values(clEnumVal(Default, "Default for platform"),
134                clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
135     cl::init(Default));
136 
137 enum LinkageNameOption {
138   DefaultLinkageNames,
139   AllLinkageNames,
140   AbstractLinkageNames
141 };
142 
143 static cl::opt<LinkageNameOption>
144     DwarfLinkageNames("dwarf-linkage-names", cl::Hidden,
145                       cl::desc("Which DWARF linkage-name attributes to emit."),
146                       cl::values(clEnumValN(DefaultLinkageNames, "Default",
147                                             "Default for platform"),
148                                  clEnumValN(AllLinkageNames, "All", "All"),
149                                  clEnumValN(AbstractLinkageNames, "Abstract",
150                                             "Abstract subprograms")),
151                       cl::init(DefaultLinkageNames));
152 
153 static cl::opt<DwarfDebug::MinimizeAddrInV5> MinimizeAddrInV5Option(
154     "minimize-addr-in-v5", cl::Hidden,
155     cl::desc("Always use DW_AT_ranges in DWARFv5 whenever it could allow more "
156              "address pool entry sharing to reduce relocations/object size"),
157     cl::values(clEnumValN(DwarfDebug::MinimizeAddrInV5::Default, "Default",
158                           "Default address minimization strategy"),
159                clEnumValN(DwarfDebug::MinimizeAddrInV5::Ranges, "Ranges",
160                           "Use rnglists for contiguous ranges if that allows "
161                           "using a pre-existing base address"),
162                clEnumValN(DwarfDebug::MinimizeAddrInV5::Expressions,
163                           "Expressions",
164                           "Use exprloc addrx+offset expressions for any "
165                           "address with a prior base address"),
166                clEnumValN(DwarfDebug::MinimizeAddrInV5::Form, "Form",
167                           "Use addrx+offset extension form for any address "
168                           "with a prior base address"),
169                clEnumValN(DwarfDebug::MinimizeAddrInV5::Disabled, "Disabled",
170                           "Stuff")),
171     cl::init(DwarfDebug::MinimizeAddrInV5::Default));
172 
173 static constexpr unsigned ULEB128PadSize = 4;
174 
175 void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) {
176   getActiveStreamer().emitInt8(
177       Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
178                   : dwarf::OperationEncodingString(Op));
179 }
180 
181 void DebugLocDwarfExpression::emitSigned(int64_t Value) {
182   getActiveStreamer().emitSLEB128(Value, Twine(Value));
183 }
184 
185 void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) {
186   getActiveStreamer().emitULEB128(Value, Twine(Value));
187 }
188 
189 void DebugLocDwarfExpression::emitData1(uint8_t Value) {
190   getActiveStreamer().emitInt8(Value, Twine(Value));
191 }
192 
193 void DebugLocDwarfExpression::emitBaseTypeRef(uint64_t Idx) {
194   assert(Idx < (1ULL << (ULEB128PadSize * 7)) && "Idx wont fit");
195   getActiveStreamer().emitULEB128(Idx, Twine(Idx), ULEB128PadSize);
196 }
197 
198 bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
199                                               llvm::Register MachineReg) {
200   // This information is not available while emitting .debug_loc entries.
201   return false;
202 }
203 
204 void DebugLocDwarfExpression::enableTemporaryBuffer() {
205   assert(!IsBuffering && "Already buffering?");
206   if (!TmpBuf)
207     TmpBuf = std::make_unique<TempBuffer>(OutBS.GenerateComments);
208   IsBuffering = true;
209 }
210 
211 void DebugLocDwarfExpression::disableTemporaryBuffer() { IsBuffering = false; }
212 
213 unsigned DebugLocDwarfExpression::getTemporaryBufferSize() {
214   return TmpBuf ? TmpBuf->Bytes.size() : 0;
215 }
216 
217 void DebugLocDwarfExpression::commitTemporaryBuffer() {
218   if (!TmpBuf)
219     return;
220   for (auto Byte : enumerate(TmpBuf->Bytes)) {
221     const char *Comment = (Byte.index() < TmpBuf->Comments.size())
222                               ? TmpBuf->Comments[Byte.index()].c_str()
223                               : "";
224     OutBS.emitInt8(Byte.value(), Comment);
225   }
226   TmpBuf->Bytes.clear();
227   TmpBuf->Comments.clear();
228 }
229 
230 const DIType *DbgVariable::getType() const {
231   return getVariable()->getType();
232 }
233 
234 /// Get .debug_loc entry for the instruction range starting at MI.
235 static DbgValueLoc getDebugLocValue(const MachineInstr *MI) {
236   const DIExpression *Expr = MI->getDebugExpression();
237   auto SingleLocExprOpt = DIExpression::convertToNonVariadicExpression(Expr);
238   const bool IsVariadic = !SingleLocExprOpt;
239   // If we have a variadic debug value instruction that is equivalent to a
240   // non-variadic instruction, then convert it to non-variadic form here.
241   if (!IsVariadic && !MI->isNonListDebugValue()) {
242     assert(MI->getNumDebugOperands() == 1 &&
243            "Mismatched DIExpression and debug operands for debug instruction.");
244     Expr = *SingleLocExprOpt;
245   }
246   assert(MI->getNumOperands() >= 3);
247   SmallVector<DbgValueLocEntry, 4> DbgValueLocEntries;
248   for (const MachineOperand &Op : MI->debug_operands()) {
249     if (Op.isReg()) {
250       MachineLocation MLoc(Op.getReg(),
251                            MI->isNonListDebugValue() && MI->isDebugOffsetImm());
252       DbgValueLocEntries.push_back(DbgValueLocEntry(MLoc));
253     } else if (Op.isTargetIndex()) {
254       DbgValueLocEntries.push_back(
255           DbgValueLocEntry(TargetIndexLocation(Op.getIndex(), Op.getOffset())));
256     } else if (Op.isImm())
257       DbgValueLocEntries.push_back(DbgValueLocEntry(Op.getImm()));
258     else if (Op.isFPImm())
259       DbgValueLocEntries.push_back(DbgValueLocEntry(Op.getFPImm()));
260     else if (Op.isCImm())
261       DbgValueLocEntries.push_back(DbgValueLocEntry(Op.getCImm()));
262     else
263       llvm_unreachable("Unexpected debug operand in DBG_VALUE* instruction!");
264   }
265   return DbgValueLoc(Expr, DbgValueLocEntries, IsVariadic);
266 }
267 
268 static uint64_t getFragmentOffsetInBits(const DIExpression &Expr) {
269   std::optional<DIExpression::FragmentInfo> Fragment = Expr.getFragmentInfo();
270   return Fragment ? Fragment->OffsetInBits : 0;
271 }
272 
273 bool llvm::operator<(const FrameIndexExpr &LHS, const FrameIndexExpr &RHS) {
274   return getFragmentOffsetInBits(*LHS.Expr) <
275          getFragmentOffsetInBits(*RHS.Expr);
276 }
277 
278 bool llvm::operator<(const EntryValueInfo &LHS, const EntryValueInfo &RHS) {
279   return getFragmentOffsetInBits(LHS.Expr) < getFragmentOffsetInBits(RHS.Expr);
280 }
281 
282 Loc::Single::Single(DbgValueLoc ValueLoc)
283     : ValueLoc(std::make_unique<DbgValueLoc>(ValueLoc)),
284       Expr(ValueLoc.getExpression()) {
285   if (!Expr->getNumElements())
286     Expr = nullptr;
287 }
288 
289 Loc::Single::Single(const MachineInstr *DbgValue)
290     : Single(getDebugLocValue(DbgValue)) {}
291 
292 const std::set<FrameIndexExpr> &Loc::MMI::getFrameIndexExprs() const {
293   return FrameIndexExprs;
294 }
295 
296 void Loc::MMI::addFrameIndexExpr(const DIExpression *Expr, int FI) {
297   FrameIndexExprs.insert({FI, Expr});
298   assert((FrameIndexExprs.size() == 1 ||
299           llvm::all_of(FrameIndexExprs,
300                        [](const FrameIndexExpr &FIE) {
301                          return FIE.Expr && FIE.Expr->isFragment();
302                        })) &&
303          "conflicting locations for variable");
304 }
305 
306 static AccelTableKind computeAccelTableKind(unsigned DwarfVersion,
307                                             bool GenerateTypeUnits,
308                                             DebuggerKind Tuning,
309                                             const Triple &TT) {
310   // Honor an explicit request.
311   if (AccelTables != AccelTableKind::Default)
312     return AccelTables;
313 
314   // Generating DWARF5 acceleration table.
315   // Currently Split dwarf and non ELF format is not supported.
316   if (GenerateTypeUnits && (DwarfVersion < 5 || !TT.isOSBinFormatELF()))
317     return AccelTableKind::None;
318 
319   // Accelerator tables get emitted if targetting DWARF v5 or LLDB.  DWARF v5
320   // always implies debug_names. For lower standard versions we use apple
321   // accelerator tables on apple platforms and debug_names elsewhere.
322   if (DwarfVersion >= 5)
323     return AccelTableKind::Dwarf;
324   if (Tuning == DebuggerKind::LLDB)
325     return TT.isOSBinFormatMachO() ? AccelTableKind::Apple
326                                    : AccelTableKind::Dwarf;
327   return AccelTableKind::None;
328 }
329 
330 DwarfDebug::DwarfDebug(AsmPrinter *A)
331     : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()),
332       InfoHolder(A, "info_string", DIEValueAllocator),
333       SkeletonHolder(A, "skel_string", DIEValueAllocator),
334       IsDarwin(A->TM.getTargetTriple().isOSDarwin()) {
335   const Triple &TT = Asm->TM.getTargetTriple();
336 
337   // Make sure we know our "debugger tuning".  The target option takes
338   // precedence; fall back to triple-based defaults.
339   if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default)
340     DebuggerTuning = Asm->TM.Options.DebuggerTuning;
341   else if (IsDarwin)
342     DebuggerTuning = DebuggerKind::LLDB;
343   else if (TT.isPS())
344     DebuggerTuning = DebuggerKind::SCE;
345   else if (TT.isOSAIX())
346     DebuggerTuning = DebuggerKind::DBX;
347   else
348     DebuggerTuning = DebuggerKind::GDB;
349 
350   if (DwarfInlinedStrings == Default)
351     UseInlineStrings = TT.isNVPTX() || tuneForDBX();
352   else
353     UseInlineStrings = DwarfInlinedStrings == Enable;
354 
355   // Always emit .debug_aranges for SCE tuning.
356   UseARangesSection = GenerateARangeSection || tuneForSCE();
357 
358   HasAppleExtensionAttributes = tuneForLLDB();
359 
360   // Handle split DWARF.
361   HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty();
362 
363   // SCE defaults to linkage names only for abstract subprograms.
364   if (DwarfLinkageNames == DefaultLinkageNames)
365     UseAllLinkageNames = !tuneForSCE();
366   else
367     UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames;
368 
369   unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
370   unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
371                                     : MMI->getModule()->getDwarfVersion();
372   // Use dwarf 4 by default if nothing is requested. For NVPTX, use dwarf 2.
373   DwarfVersion =
374       TT.isNVPTX() ? 2 : (DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION);
375 
376   bool Dwarf64 = DwarfVersion >= 3 && // DWARF64 was introduced in DWARFv3.
377                  TT.isArch64Bit();    // DWARF64 requires 64-bit relocations.
378 
379   // Support DWARF64
380   // 1: For ELF when requested.
381   // 2: For XCOFF64: the AIX assembler will fill in debug section lengths
382   //    according to the DWARF64 format for 64-bit assembly, so we must use
383   //    DWARF64 in the compiler too for 64-bit mode.
384   Dwarf64 &=
385       ((Asm->TM.Options.MCOptions.Dwarf64 || MMI->getModule()->isDwarf64()) &&
386        TT.isOSBinFormatELF()) ||
387       TT.isOSBinFormatXCOFF();
388 
389   if (!Dwarf64 && TT.isArch64Bit() && TT.isOSBinFormatXCOFF())
390     report_fatal_error("XCOFF requires DWARF64 for 64-bit mode!");
391 
392   UseRangesSection = !NoDwarfRangesSection && !TT.isNVPTX();
393 
394   // Use sections as references. Force for NVPTX.
395   if (DwarfSectionsAsReferences == Default)
396     UseSectionsAsReferences = TT.isNVPTX();
397   else
398     UseSectionsAsReferences = DwarfSectionsAsReferences == Enable;
399 
400   // Don't generate type units for unsupported object file formats.
401   GenerateTypeUnits = (A->TM.getTargetTriple().isOSBinFormatELF() ||
402                        A->TM.getTargetTriple().isOSBinFormatWasm()) &&
403                       GenerateDwarfTypeUnits;
404 
405   TheAccelTableKind = computeAccelTableKind(
406       DwarfVersion, GenerateTypeUnits, DebuggerTuning, A->TM.getTargetTriple());
407 
408   // Work around a GDB bug. GDB doesn't support the standard opcode;
409   // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
410   // is defined as of DWARF 3.
411   // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
412   // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
413   UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3;
414 
415   UseDWARF2Bitfields = DwarfVersion < 4;
416 
417   // The DWARF v5 string offsets table has - possibly shared - contributions
418   // from each compile and type unit each preceded by a header. The string
419   // offsets table used by the pre-DWARF v5 split-DWARF implementation uses
420   // a monolithic string offsets table without any header.
421   UseSegmentedStringOffsetsTable = DwarfVersion >= 5;
422 
423   // Emit call-site-param debug info for GDB and LLDB, if the target supports
424   // the debug entry values feature. It can also be enabled explicitly.
425   EmitDebugEntryValues = Asm->TM.Options.ShouldEmitDebugEntryValues();
426 
427   // It is unclear if the GCC .debug_macro extension is well-specified
428   // for split DWARF. For now, do not allow LLVM to emit it.
429   UseDebugMacroSection =
430       DwarfVersion >= 5 || (UseGNUDebugMacro && !useSplitDwarf());
431   if (DwarfOpConvert == Default)
432     EnableOpConvert = !((tuneForGDB() && useSplitDwarf()) || (tuneForLLDB() && !TT.isOSBinFormatMachO()));
433   else
434     EnableOpConvert = (DwarfOpConvert == Enable);
435 
436   // Split DWARF would benefit object size significantly by trading reductions
437   // in address pool usage for slightly increased range list encodings.
438   if (DwarfVersion >= 5)
439     MinimizeAddr = MinimizeAddrInV5Option;
440 
441   Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion);
442   Asm->OutStreamer->getContext().setDwarfFormat(Dwarf64 ? dwarf::DWARF64
443                                                         : dwarf::DWARF32);
444 }
445 
446 // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
447 DwarfDebug::~DwarfDebug() = default;
448 
449 static bool isObjCClass(StringRef Name) {
450   return Name.starts_with("+") || Name.starts_with("-");
451 }
452 
453 static bool hasObjCCategory(StringRef Name) {
454   if (!isObjCClass(Name))
455     return false;
456 
457   return Name.contains(") ");
458 }
459 
460 static void getObjCClassCategory(StringRef In, StringRef &Class,
461                                  StringRef &Category) {
462   if (!hasObjCCategory(In)) {
463     Class = In.slice(In.find('[') + 1, In.find(' '));
464     Category = "";
465     return;
466   }
467 
468   Class = In.slice(In.find('[') + 1, In.find('('));
469   Category = In.slice(In.find('[') + 1, In.find(' '));
470 }
471 
472 static StringRef getObjCMethodName(StringRef In) {
473   return In.slice(In.find(' ') + 1, In.find(']'));
474 }
475 
476 // Add the various names to the Dwarf accelerator table names.
477 void DwarfDebug::addSubprogramNames(
478     const DwarfUnit &Unit,
479     const DICompileUnit::DebugNameTableKind NameTableKind,
480     const DISubprogram *SP, DIE &Die) {
481   if (getAccelTableKind() != AccelTableKind::Apple &&
482       NameTableKind != DICompileUnit::DebugNameTableKind::Apple &&
483       NameTableKind == DICompileUnit::DebugNameTableKind::None)
484     return;
485 
486   if (!SP->isDefinition())
487     return;
488 
489   if (SP->getName() != "")
490     addAccelName(Unit, NameTableKind, SP->getName(), Die);
491 
492   // If the linkage name is different than the name, go ahead and output that as
493   // well into the name table. Only do that if we are going to actually emit
494   // that name.
495   if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName() &&
496       (useAllLinkageNames() || InfoHolder.getAbstractScopeDIEs().lookup(SP)))
497     addAccelName(Unit, NameTableKind, SP->getLinkageName(), Die);
498 
499   // If this is an Objective-C selector name add it to the ObjC accelerator
500   // too.
501   if (isObjCClass(SP->getName())) {
502     StringRef Class, Category;
503     getObjCClassCategory(SP->getName(), Class, Category);
504     addAccelObjC(Unit, NameTableKind, Class, Die);
505     if (Category != "")
506       addAccelObjC(Unit, NameTableKind, Category, Die);
507     // Also add the base method name to the name table.
508     addAccelName(Unit, NameTableKind, getObjCMethodName(SP->getName()), Die);
509   }
510 }
511 
512 /// Check whether we should create a DIE for the given Scope, return true
513 /// if we don't create a DIE (the corresponding DIE is null).
514 bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
515   if (Scope->isAbstractScope())
516     return false;
517 
518   // We don't create a DIE if there is no Range.
519   const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
520   if (Ranges.empty())
521     return true;
522 
523   if (Ranges.size() > 1)
524     return false;
525 
526   // We don't create a DIE if we have a single Range and the end label
527   // is null.
528   return !getLabelAfterInsn(Ranges.front().second);
529 }
530 
531 template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) {
532   F(CU);
533   if (auto *SkelCU = CU.getSkeleton())
534     if (CU.getCUNode()->getSplitDebugInlining())
535       F(*SkelCU);
536 }
537 
538 bool DwarfDebug::shareAcrossDWOCUs() const {
539   return SplitDwarfCrossCuReferences;
540 }
541 
542 void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU,
543                                                      LexicalScope *Scope) {
544   assert(Scope && Scope->getScopeNode());
545   assert(Scope->isAbstractScope());
546   assert(!Scope->getInlinedAt());
547 
548   auto *SP = cast<DISubprogram>(Scope->getScopeNode());
549 
550   // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
551   // was inlined from another compile unit.
552   if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining())
553     // Avoid building the original CU if it won't be used
554     SrcCU.constructAbstractSubprogramScopeDIE(Scope);
555   else {
556     auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
557     if (auto *SkelCU = CU.getSkeleton()) {
558       (shareAcrossDWOCUs() ? CU : SrcCU)
559           .constructAbstractSubprogramScopeDIE(Scope);
560       if (CU.getCUNode()->getSplitDebugInlining())
561         SkelCU->constructAbstractSubprogramScopeDIE(Scope);
562     } else
563       CU.constructAbstractSubprogramScopeDIE(Scope);
564   }
565 }
566 
567 /// Represents a parameter whose call site value can be described by applying a
568 /// debug expression to a register in the forwarded register worklist.
569 struct FwdRegParamInfo {
570   /// The described parameter register.
571   uint64_t ParamReg;
572 
573   /// Debug expression that has been built up when walking through the
574   /// instruction chain that produces the parameter's value.
575   const DIExpression *Expr;
576 };
577 
578 /// Register worklist for finding call site values.
579 using FwdRegWorklist = MapVector<uint64_t, SmallVector<FwdRegParamInfo, 2>>;
580 /// Container for the set of registers known to be clobbered on the path to a
581 /// call site.
582 using ClobberedRegSet = SmallSet<Register, 16>;
583 
584 /// Append the expression \p Addition to \p Original and return the result.
585 static const DIExpression *combineDIExpressions(const DIExpression *Original,
586                                                 const DIExpression *Addition) {
587   std::vector<uint64_t> Elts = Addition->getElements().vec();
588   // Avoid multiple DW_OP_stack_values.
589   if (Original->isImplicit() && Addition->isImplicit())
590     llvm::erase(Elts, dwarf::DW_OP_stack_value);
591   const DIExpression *CombinedExpr =
592       (Elts.size() > 0) ? DIExpression::append(Original, Elts) : Original;
593   return CombinedExpr;
594 }
595 
596 /// Emit call site parameter entries that are described by the given value and
597 /// debug expression.
598 template <typename ValT>
599 static void finishCallSiteParams(ValT Val, const DIExpression *Expr,
600                                  ArrayRef<FwdRegParamInfo> DescribedParams,
601                                  ParamSet &Params) {
602   for (auto Param : DescribedParams) {
603     bool ShouldCombineExpressions = Expr && Param.Expr->getNumElements() > 0;
604 
605     // TODO: Entry value operations can currently not be combined with any
606     // other expressions, so we can't emit call site entries in those cases.
607     if (ShouldCombineExpressions && Expr->isEntryValue())
608       continue;
609 
610     // If a parameter's call site value is produced by a chain of
611     // instructions we may have already created an expression for the
612     // parameter when walking through the instructions. Append that to the
613     // base expression.
614     const DIExpression *CombinedExpr =
615         ShouldCombineExpressions ? combineDIExpressions(Expr, Param.Expr)
616                                  : Expr;
617     assert((!CombinedExpr || CombinedExpr->isValid()) &&
618            "Combined debug expression is invalid");
619 
620     DbgValueLoc DbgLocVal(CombinedExpr, DbgValueLocEntry(Val));
621     DbgCallSiteParam CSParm(Param.ParamReg, DbgLocVal);
622     Params.push_back(CSParm);
623     ++NumCSParams;
624   }
625 }
626 
627 /// Add \p Reg to the worklist, if it's not already present, and mark that the
628 /// given parameter registers' values can (potentially) be described using
629 /// that register and an debug expression.
630 static void addToFwdRegWorklist(FwdRegWorklist &Worklist, unsigned Reg,
631                                 const DIExpression *Expr,
632                                 ArrayRef<FwdRegParamInfo> ParamsToAdd) {
633   auto &ParamsForFwdReg = Worklist[Reg];
634   for (auto Param : ParamsToAdd) {
635     assert(none_of(ParamsForFwdReg,
636                    [Param](const FwdRegParamInfo &D) {
637                      return D.ParamReg == Param.ParamReg;
638                    }) &&
639            "Same parameter described twice by forwarding reg");
640 
641     // If a parameter's call site value is produced by a chain of
642     // instructions we may have already created an expression for the
643     // parameter when walking through the instructions. Append that to the
644     // new expression.
645     const DIExpression *CombinedExpr = combineDIExpressions(Expr, Param.Expr);
646     ParamsForFwdReg.push_back({Param.ParamReg, CombinedExpr});
647   }
648 }
649 
650 /// Interpret values loaded into registers by \p CurMI.
651 static void interpretValues(const MachineInstr *CurMI,
652                             FwdRegWorklist &ForwardedRegWorklist,
653                             ParamSet &Params,
654                             ClobberedRegSet &ClobberedRegUnits) {
655 
656   const MachineFunction *MF = CurMI->getMF();
657   const DIExpression *EmptyExpr =
658       DIExpression::get(MF->getFunction().getContext(), {});
659   const auto &TRI = *MF->getSubtarget().getRegisterInfo();
660   const auto &TII = *MF->getSubtarget().getInstrInfo();
661   const auto &TLI = *MF->getSubtarget().getTargetLowering();
662 
663   // If an instruction defines more than one item in the worklist, we may run
664   // into situations where a worklist register's value is (potentially)
665   // described by the previous value of another register that is also defined
666   // by that instruction.
667   //
668   // This can for example occur in cases like this:
669   //
670   //   $r1 = mov 123
671   //   $r0, $r1 = mvrr $r1, 456
672   //   call @foo, $r0, $r1
673   //
674   // When describing $r1's value for the mvrr instruction, we need to make sure
675   // that we don't finalize an entry value for $r0, as that is dependent on the
676   // previous value of $r1 (123 rather than 456).
677   //
678   // In order to not have to distinguish between those cases when finalizing
679   // entry values, we simply postpone adding new parameter registers to the
680   // worklist, by first keeping them in this temporary container until the
681   // instruction has been handled.
682   FwdRegWorklist TmpWorklistItems;
683 
684   // If the MI is an instruction defining one or more parameters' forwarding
685   // registers, add those defines.
686   ClobberedRegSet NewClobberedRegUnits;
687   auto getForwardingRegsDefinedByMI = [&](const MachineInstr &MI,
688                                           SmallSetVector<unsigned, 4> &Defs) {
689     if (MI.isDebugInstr())
690       return;
691 
692     for (const MachineOperand &MO : MI.all_defs()) {
693       if (MO.getReg().isPhysical()) {
694         for (auto &FwdReg : ForwardedRegWorklist)
695           if (TRI.regsOverlap(FwdReg.first, MO.getReg()))
696             Defs.insert(FwdReg.first);
697         for (MCRegUnit Unit : TRI.regunits(MO.getReg()))
698           NewClobberedRegUnits.insert(Unit);
699       }
700     }
701   };
702 
703   // Set of worklist registers that are defined by this instruction.
704   SmallSetVector<unsigned, 4> FwdRegDefs;
705 
706   getForwardingRegsDefinedByMI(*CurMI, FwdRegDefs);
707   if (FwdRegDefs.empty()) {
708     // Any definitions by this instruction will clobber earlier reg movements.
709     ClobberedRegUnits.insert(NewClobberedRegUnits.begin(),
710                              NewClobberedRegUnits.end());
711     return;
712   }
713 
714   // It's possible that we find a copy from a non-volatile register to the param
715   // register, which is clobbered in the meantime. Test for clobbered reg unit
716   // overlaps before completing.
717   auto IsRegClobberedInMeantime = [&](Register Reg) -> bool {
718     for (auto &RegUnit : ClobberedRegUnits)
719       if (TRI.hasRegUnit(Reg, RegUnit))
720         return true;
721     return false;
722   };
723 
724   for (auto ParamFwdReg : FwdRegDefs) {
725     if (auto ParamValue = TII.describeLoadedValue(*CurMI, ParamFwdReg)) {
726       if (ParamValue->first.isImm()) {
727         int64_t Val = ParamValue->first.getImm();
728         finishCallSiteParams(Val, ParamValue->second,
729                              ForwardedRegWorklist[ParamFwdReg], Params);
730       } else if (ParamValue->first.isReg()) {
731         Register RegLoc = ParamValue->first.getReg();
732         Register SP = TLI.getStackPointerRegisterToSaveRestore();
733         Register FP = TRI.getFrameRegister(*MF);
734         bool IsSPorFP = (RegLoc == SP) || (RegLoc == FP);
735         if (!IsRegClobberedInMeantime(RegLoc) &&
736             (TRI.isCalleeSavedPhysReg(RegLoc, *MF) || IsSPorFP)) {
737           MachineLocation MLoc(RegLoc, /*Indirect=*/IsSPorFP);
738           finishCallSiteParams(MLoc, ParamValue->second,
739                                ForwardedRegWorklist[ParamFwdReg], Params);
740         } else {
741           // ParamFwdReg was described by the non-callee saved register
742           // RegLoc. Mark that the call site values for the parameters are
743           // dependent on that register instead of ParamFwdReg. Since RegLoc
744           // may be a register that will be handled in this iteration, we
745           // postpone adding the items to the worklist, and instead keep them
746           // in a temporary container.
747           addToFwdRegWorklist(TmpWorklistItems, RegLoc, ParamValue->second,
748                               ForwardedRegWorklist[ParamFwdReg]);
749         }
750       }
751     }
752   }
753 
754   // Remove all registers that this instruction defines from the worklist.
755   for (auto ParamFwdReg : FwdRegDefs)
756     ForwardedRegWorklist.erase(ParamFwdReg);
757 
758   // Any definitions by this instruction will clobber earlier reg movements.
759   ClobberedRegUnits.insert(NewClobberedRegUnits.begin(),
760                            NewClobberedRegUnits.end());
761 
762   // Now that we are done handling this instruction, add items from the
763   // temporary worklist to the real one.
764   for (auto &New : TmpWorklistItems)
765     addToFwdRegWorklist(ForwardedRegWorklist, New.first, EmptyExpr, New.second);
766   TmpWorklistItems.clear();
767 }
768 
769 static bool interpretNextInstr(const MachineInstr *CurMI,
770                                FwdRegWorklist &ForwardedRegWorklist,
771                                ParamSet &Params,
772                                ClobberedRegSet &ClobberedRegUnits) {
773   // Skip bundle headers.
774   if (CurMI->isBundle())
775     return true;
776 
777   // If the next instruction is a call we can not interpret parameter's
778   // forwarding registers or we finished the interpretation of all
779   // parameters.
780   if (CurMI->isCall())
781     return false;
782 
783   if (ForwardedRegWorklist.empty())
784     return false;
785 
786   // Avoid NOP description.
787   if (CurMI->getNumOperands() == 0)
788     return true;
789 
790   interpretValues(CurMI, ForwardedRegWorklist, Params, ClobberedRegUnits);
791 
792   return true;
793 }
794 
795 /// Try to interpret values loaded into registers that forward parameters
796 /// for \p CallMI. Store parameters with interpreted value into \p Params.
797 static void collectCallSiteParameters(const MachineInstr *CallMI,
798                                       ParamSet &Params) {
799   const MachineFunction *MF = CallMI->getMF();
800   const auto &CalleesMap = MF->getCallSitesInfo();
801   auto CSInfo = CalleesMap.find(CallMI);
802 
803   // There is no information for the call instruction.
804   if (CSInfo == CalleesMap.end())
805     return;
806 
807   const MachineBasicBlock *MBB = CallMI->getParent();
808 
809   // Skip the call instruction.
810   auto I = std::next(CallMI->getReverseIterator());
811 
812   FwdRegWorklist ForwardedRegWorklist;
813 
814   const DIExpression *EmptyExpr =
815       DIExpression::get(MF->getFunction().getContext(), {});
816 
817   // Add all the forwarding registers into the ForwardedRegWorklist.
818   for (const auto &ArgReg : CSInfo->second.ArgRegPairs) {
819     bool InsertedReg =
820         ForwardedRegWorklist.insert({ArgReg.Reg, {{ArgReg.Reg, EmptyExpr}}})
821             .second;
822     assert(InsertedReg && "Single register used to forward two arguments?");
823     (void)InsertedReg;
824   }
825 
826   // Do not emit CSInfo for undef forwarding registers.
827   for (const auto &MO : CallMI->uses())
828     if (MO.isReg() && MO.isUndef())
829       ForwardedRegWorklist.erase(MO.getReg());
830 
831   // We erase, from the ForwardedRegWorklist, those forwarding registers for
832   // which we successfully describe a loaded value (by using
833   // the describeLoadedValue()). For those remaining arguments in the working
834   // list, for which we do not describe a loaded value by
835   // the describeLoadedValue(), we try to generate an entry value expression
836   // for their call site value description, if the call is within the entry MBB.
837   // TODO: Handle situations when call site parameter value can be described
838   // as the entry value within basic blocks other than the first one.
839   bool ShouldTryEmitEntryVals = MBB->getIterator() == MF->begin();
840 
841   // Search for a loading value in forwarding registers inside call delay slot.
842   ClobberedRegSet ClobberedRegUnits;
843   if (CallMI->hasDelaySlot()) {
844     auto Suc = std::next(CallMI->getIterator());
845     // Only one-instruction delay slot is supported.
846     auto BundleEnd = llvm::getBundleEnd(CallMI->getIterator());
847     (void)BundleEnd;
848     assert(std::next(Suc) == BundleEnd &&
849            "More than one instruction in call delay slot");
850     // Try to interpret value loaded by instruction.
851     if (!interpretNextInstr(&*Suc, ForwardedRegWorklist, Params, ClobberedRegUnits))
852       return;
853   }
854 
855   // Search for a loading value in forwarding registers.
856   for (; I != MBB->rend(); ++I) {
857     // Try to interpret values loaded by instruction.
858     if (!interpretNextInstr(&*I, ForwardedRegWorklist, Params, ClobberedRegUnits))
859       return;
860   }
861 
862   // Emit the call site parameter's value as an entry value.
863   if (ShouldTryEmitEntryVals) {
864     // Create an expression where the register's entry value is used.
865     DIExpression *EntryExpr = DIExpression::get(
866         MF->getFunction().getContext(), {dwarf::DW_OP_LLVM_entry_value, 1});
867     for (auto &RegEntry : ForwardedRegWorklist) {
868       MachineLocation MLoc(RegEntry.first);
869       finishCallSiteParams(MLoc, EntryExpr, RegEntry.second, Params);
870     }
871   }
872 }
873 
874 void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP,
875                                             DwarfCompileUnit &CU, DIE &ScopeDIE,
876                                             const MachineFunction &MF) {
877   // Add a call site-related attribute (DWARF5, Sec. 3.3.1.3). Do this only if
878   // the subprogram is required to have one.
879   if (!SP.areAllCallsDescribed() || !SP.isDefinition())
880     return;
881 
882   // Use DW_AT_call_all_calls to express that call site entries are present
883   // for both tail and non-tail calls. Don't use DW_AT_call_all_source_calls
884   // because one of its requirements is not met: call site entries for
885   // optimized-out calls are elided.
886   CU.addFlag(ScopeDIE, CU.getDwarf5OrGNUAttr(dwarf::DW_AT_call_all_calls));
887 
888   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
889   assert(TII && "TargetInstrInfo not found: cannot label tail calls");
890 
891   // Delay slot support check.
892   auto delaySlotSupported = [&](const MachineInstr &MI) {
893     if (!MI.isBundledWithSucc())
894       return false;
895     auto Suc = std::next(MI.getIterator());
896     auto CallInstrBundle = getBundleStart(MI.getIterator());
897     (void)CallInstrBundle;
898     auto DelaySlotBundle = getBundleStart(Suc);
899     (void)DelaySlotBundle;
900     // Ensure that label after call is following delay slot instruction.
901     // Ex. CALL_INSTRUCTION {
902     //       DELAY_SLOT_INSTRUCTION }
903     //      LABEL_AFTER_CALL
904     assert(getLabelAfterInsn(&*CallInstrBundle) ==
905                getLabelAfterInsn(&*DelaySlotBundle) &&
906            "Call and its successor instruction don't have same label after.");
907     return true;
908   };
909 
910   // Emit call site entries for each call or tail call in the function.
911   for (const MachineBasicBlock &MBB : MF) {
912     for (const MachineInstr &MI : MBB.instrs()) {
913       // Bundles with call in them will pass the isCall() test below but do not
914       // have callee operand information so skip them here. Iterator will
915       // eventually reach the call MI.
916       if (MI.isBundle())
917         continue;
918 
919       // Skip instructions which aren't calls. Both calls and tail-calling jump
920       // instructions (e.g TAILJMPd64) are classified correctly here.
921       if (!MI.isCandidateForAdditionalCallInfo())
922         continue;
923 
924       // Skip instructions marked as frame setup, as they are not interesting to
925       // the user.
926       if (MI.getFlag(MachineInstr::FrameSetup))
927         continue;
928 
929       // Check if delay slot support is enabled.
930       if (MI.hasDelaySlot() && !delaySlotSupported(*&MI))
931         return;
932 
933       // If this is a direct call, find the callee's subprogram.
934       // In the case of an indirect call find the register that holds
935       // the callee.
936       const MachineOperand &CalleeOp = TII->getCalleeOperand(MI);
937       if (!CalleeOp.isGlobal() &&
938           (!CalleeOp.isReg() || !CalleeOp.getReg().isPhysical()))
939         continue;
940 
941       unsigned CallReg = 0;
942       const DISubprogram *CalleeSP = nullptr;
943       const Function *CalleeDecl = nullptr;
944       if (CalleeOp.isReg()) {
945         CallReg = CalleeOp.getReg();
946         if (!CallReg)
947           continue;
948       } else {
949         CalleeDecl = dyn_cast<Function>(CalleeOp.getGlobal());
950         if (!CalleeDecl || !CalleeDecl->getSubprogram())
951           continue;
952         CalleeSP = CalleeDecl->getSubprogram();
953       }
954 
955       // TODO: Omit call site entries for runtime calls (objc_msgSend, etc).
956 
957       bool IsTail = TII->isTailCall(MI);
958 
959       // If MI is in a bundle, the label was created after the bundle since
960       // EmitFunctionBody iterates over top-level MIs. Get that top-level MI
961       // to search for that label below.
962       const MachineInstr *TopLevelCallMI =
963           MI.isInsideBundle() ? &*getBundleStart(MI.getIterator()) : &MI;
964 
965       // For non-tail calls, the return PC is needed to disambiguate paths in
966       // the call graph which could lead to some target function. For tail
967       // calls, no return PC information is needed, unless tuning for GDB in
968       // DWARF4 mode in which case we fake a return PC for compatibility.
969       const MCSymbol *PCAddr =
970           (!IsTail || CU.useGNUAnalogForDwarf5Feature())
971               ? const_cast<MCSymbol *>(getLabelAfterInsn(TopLevelCallMI))
972               : nullptr;
973 
974       // For tail calls, it's necessary to record the address of the branch
975       // instruction so that the debugger can show where the tail call occurred.
976       const MCSymbol *CallAddr =
977           IsTail ? getLabelBeforeInsn(TopLevelCallMI) : nullptr;
978 
979       assert((IsTail || PCAddr) && "Non-tail call without return PC");
980 
981       LLVM_DEBUG(dbgs() << "CallSiteEntry: " << MF.getName() << " -> "
982                         << (CalleeDecl ? CalleeDecl->getName()
983                                        : StringRef(MF.getSubtarget()
984                                                        .getRegisterInfo()
985                                                        ->getName(CallReg)))
986                         << (IsTail ? " [IsTail]" : "") << "\n");
987 
988       DIE &CallSiteDIE = CU.constructCallSiteEntryDIE(
989           ScopeDIE, CalleeSP, IsTail, PCAddr, CallAddr, CallReg);
990 
991       // Optionally emit call-site-param debug info.
992       if (emitDebugEntryValues()) {
993         ParamSet Params;
994         // Try to interpret values of call site parameters.
995         collectCallSiteParameters(&MI, Params);
996         CU.constructCallSiteParmEntryDIEs(CallSiteDIE, Params);
997       }
998     }
999   }
1000 }
1001 
1002 void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const {
1003   if (!U.hasDwarfPubSections())
1004     return;
1005 
1006   U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
1007 }
1008 
1009 void DwarfDebug::finishUnitAttributes(const DICompileUnit *DIUnit,
1010                                       DwarfCompileUnit &NewCU) {
1011   DIE &Die = NewCU.getUnitDie();
1012   StringRef FN = DIUnit->getFilename();
1013 
1014   StringRef Producer = DIUnit->getProducer();
1015   StringRef Flags = DIUnit->getFlags();
1016   if (!Flags.empty() && !useAppleExtensionAttributes()) {
1017     std::string ProducerWithFlags = Producer.str() + " " + Flags.str();
1018     NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags);
1019   } else
1020     NewCU.addString(Die, dwarf::DW_AT_producer, Producer);
1021 
1022   NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
1023                 DIUnit->getSourceLanguage());
1024   NewCU.addString(Die, dwarf::DW_AT_name, FN);
1025   StringRef SysRoot = DIUnit->getSysRoot();
1026   if (!SysRoot.empty())
1027     NewCU.addString(Die, dwarf::DW_AT_LLVM_sysroot, SysRoot);
1028   StringRef SDK = DIUnit->getSDK();
1029   if (!SDK.empty())
1030     NewCU.addString(Die, dwarf::DW_AT_APPLE_sdk, SDK);
1031 
1032   if (!useSplitDwarf()) {
1033     // Add DW_str_offsets_base to the unit DIE, except for split units.
1034     if (useSegmentedStringOffsetsTable())
1035       NewCU.addStringOffsetsStart();
1036 
1037     NewCU.initStmtList();
1038 
1039     // If we're using split dwarf the compilation dir is going to be in the
1040     // skeleton CU and so we don't need to duplicate it here.
1041     if (!CompilationDir.empty())
1042       NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
1043     addGnuPubAttributes(NewCU, Die);
1044   }
1045 
1046   if (useAppleExtensionAttributes()) {
1047     if (DIUnit->isOptimized())
1048       NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
1049 
1050     StringRef Flags = DIUnit->getFlags();
1051     if (!Flags.empty())
1052       NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
1053 
1054     if (unsigned RVer = DIUnit->getRuntimeVersion())
1055       NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
1056                     dwarf::DW_FORM_data1, RVer);
1057   }
1058 
1059   if (DIUnit->getDWOId()) {
1060     // This CU is either a clang module DWO or a skeleton CU.
1061     NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8,
1062                   DIUnit->getDWOId());
1063     if (!DIUnit->getSplitDebugFilename().empty()) {
1064       // This is a prefabricated skeleton CU.
1065       dwarf::Attribute attrDWOName = getDwarfVersion() >= 5
1066                                          ? dwarf::DW_AT_dwo_name
1067                                          : dwarf::DW_AT_GNU_dwo_name;
1068       NewCU.addString(Die, attrDWOName, DIUnit->getSplitDebugFilename());
1069     }
1070   }
1071 }
1072 // Create new DwarfCompileUnit for the given metadata node with tag
1073 // DW_TAG_compile_unit.
1074 DwarfCompileUnit &
1075 DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) {
1076   if (auto *CU = CUMap.lookup(DIUnit))
1077     return *CU;
1078 
1079   if (useSplitDwarf() &&
1080       !shareAcrossDWOCUs() &&
1081       (!DIUnit->getSplitDebugInlining() ||
1082        DIUnit->getEmissionKind() == DICompileUnit::FullDebug) &&
1083       !CUMap.empty()) {
1084     return *CUMap.begin()->second;
1085   }
1086   CompilationDir = DIUnit->getDirectory();
1087 
1088   auto OwnedUnit = std::make_unique<DwarfCompileUnit>(
1089       InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
1090   DwarfCompileUnit &NewCU = *OwnedUnit;
1091   InfoHolder.addUnit(std::move(OwnedUnit));
1092 
1093   // LTO with assembly output shares a single line table amongst multiple CUs.
1094   // To avoid the compilation directory being ambiguous, let the line table
1095   // explicitly describe the directory of all files, never relying on the
1096   // compilation directory.
1097   if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU)
1098     Asm->OutStreamer->emitDwarfFile0Directive(
1099         CompilationDir, DIUnit->getFilename(), getMD5AsBytes(DIUnit->getFile()),
1100         DIUnit->getSource(), NewCU.getUniqueID());
1101 
1102   if (useSplitDwarf()) {
1103     NewCU.setSkeleton(constructSkeletonCU(NewCU));
1104     NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
1105   } else {
1106     finishUnitAttributes(DIUnit, NewCU);
1107     NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
1108   }
1109 
1110   CUMap.insert({DIUnit, &NewCU});
1111   CUDieMap.insert({&NewCU.getUnitDie(), &NewCU});
1112   return NewCU;
1113 }
1114 
1115 /// Sort and unique GVEs by comparing their fragment offset.
1116 static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &
1117 sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) {
1118   llvm::sort(
1119       GVEs, [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) {
1120         // Sort order: first null exprs, then exprs without fragment
1121         // info, then sort by fragment offset in bits.
1122         // FIXME: Come up with a more comprehensive comparator so
1123         // the sorting isn't non-deterministic, and so the following
1124         // std::unique call works correctly.
1125         if (!A.Expr || !B.Expr)
1126           return !!B.Expr;
1127         auto FragmentA = A.Expr->getFragmentInfo();
1128         auto FragmentB = B.Expr->getFragmentInfo();
1129         if (!FragmentA || !FragmentB)
1130           return !!FragmentB;
1131         return FragmentA->OffsetInBits < FragmentB->OffsetInBits;
1132       });
1133   GVEs.erase(llvm::unique(GVEs,
1134                           [](DwarfCompileUnit::GlobalExpr A,
1135                              DwarfCompileUnit::GlobalExpr B) {
1136                             return A.Expr == B.Expr;
1137                           }),
1138              GVEs.end());
1139   return GVEs;
1140 }
1141 
1142 // Emit all Dwarf sections that should come prior to the content. Create
1143 // global DIEs and emit initial debug info sections. This is invoked by
1144 // the target AsmPrinter.
1145 void DwarfDebug::beginModule(Module *M) {
1146   DebugHandlerBase::beginModule(M);
1147 
1148   if (!Asm)
1149     return;
1150 
1151   unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(),
1152                                        M->debug_compile_units_end());
1153   if (NumDebugCUs == 0)
1154     return;
1155 
1156   assert(NumDebugCUs > 0 && "Asm unexpectedly initialized");
1157   SingleCU = NumDebugCUs == 1;
1158   DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>>
1159       GVMap;
1160   for (const GlobalVariable &Global : M->globals()) {
1161     SmallVector<DIGlobalVariableExpression *, 1> GVs;
1162     Global.getDebugInfo(GVs);
1163     for (auto *GVE : GVs)
1164       GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()});
1165   }
1166 
1167   // Create the symbol that designates the start of the unit's contribution
1168   // to the string offsets table. In a split DWARF scenario, only the skeleton
1169   // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol).
1170   if (useSegmentedStringOffsetsTable())
1171     (useSplitDwarf() ? SkeletonHolder : InfoHolder)
1172         .setStringOffsetsStartSym(Asm->createTempSymbol("str_offsets_base"));
1173 
1174 
1175   // Create the symbols that designates the start of the DWARF v5 range list
1176   // and locations list tables. They are located past the table headers.
1177   if (getDwarfVersion() >= 5) {
1178     DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1179     Holder.setRnglistsTableBaseSym(
1180         Asm->createTempSymbol("rnglists_table_base"));
1181 
1182     if (useSplitDwarf())
1183       InfoHolder.setRnglistsTableBaseSym(
1184           Asm->createTempSymbol("rnglists_dwo_table_base"));
1185   }
1186 
1187   // Create the symbol that points to the first entry following the debug
1188   // address table (.debug_addr) header.
1189   AddrPool.setLabel(Asm->createTempSymbol("addr_table_base"));
1190   DebugLocs.setSym(Asm->createTempSymbol("loclists_table_base"));
1191 
1192   for (DICompileUnit *CUNode : M->debug_compile_units()) {
1193     if (CUNode->getImportedEntities().empty() &&
1194         CUNode->getEnumTypes().empty() && CUNode->getRetainedTypes().empty() &&
1195         CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty())
1196       continue;
1197 
1198     DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode);
1199 
1200     // Global Variables.
1201     for (auto *GVE : CUNode->getGlobalVariables()) {
1202       // Don't bother adding DIGlobalVariableExpressions listed in the CU if we
1203       // already know about the variable and it isn't adding a constant
1204       // expression.
1205       auto &GVMapEntry = GVMap[GVE->getVariable()];
1206       auto *Expr = GVE->getExpression();
1207       if (!GVMapEntry.size() || (Expr && Expr->isConstant()))
1208         GVMapEntry.push_back({nullptr, Expr});
1209     }
1210 
1211     DenseSet<DIGlobalVariable *> Processed;
1212     for (auto *GVE : CUNode->getGlobalVariables()) {
1213       DIGlobalVariable *GV = GVE->getVariable();
1214       if (Processed.insert(GV).second)
1215         CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV]));
1216     }
1217 
1218     for (auto *Ty : CUNode->getEnumTypes())
1219       CU.getOrCreateTypeDIE(cast<DIType>(Ty));
1220 
1221     for (auto *Ty : CUNode->getRetainedTypes()) {
1222       // The retained types array by design contains pointers to
1223       // MDNodes rather than DIRefs. Unique them here.
1224       if (DIType *RT = dyn_cast<DIType>(Ty))
1225         // There is no point in force-emitting a forward declaration.
1226         CU.getOrCreateTypeDIE(RT);
1227     }
1228   }
1229 }
1230 
1231 void DwarfDebug::finishEntityDefinitions() {
1232   for (const auto &Entity : ConcreteEntities) {
1233     DIE *Die = Entity->getDIE();
1234     assert(Die);
1235     // FIXME: Consider the time-space tradeoff of just storing the unit pointer
1236     // in the ConcreteEntities list, rather than looking it up again here.
1237     // DIE::getUnit isn't simple - it walks parent pointers, etc.
1238     DwarfCompileUnit *Unit = CUDieMap.lookup(Die->getUnitDie());
1239     assert(Unit);
1240     Unit->finishEntityDefinition(Entity.get());
1241   }
1242 }
1243 
1244 void DwarfDebug::finishSubprogramDefinitions() {
1245   for (const DISubprogram *SP : ProcessedSPNodes) {
1246     assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug);
1247     forBothCUs(
1248         getOrCreateDwarfCompileUnit(SP->getUnit()),
1249         [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); });
1250   }
1251 }
1252 
1253 void DwarfDebug::finalizeModuleInfo() {
1254   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1255 
1256   finishSubprogramDefinitions();
1257 
1258   finishEntityDefinitions();
1259 
1260   bool HasEmittedSplitCU = false;
1261 
1262   // Handle anything that needs to be done on a per-unit basis after
1263   // all other generation.
1264   for (const auto &P : CUMap) {
1265     auto &TheCU = *P.second;
1266     if (TheCU.getCUNode()->isDebugDirectivesOnly())
1267       continue;
1268     // Emit DW_AT_containing_type attribute to connect types with their
1269     // vtable holding type.
1270     TheCU.constructContainingTypeDIEs();
1271 
1272     // Add CU specific attributes if we need to add any.
1273     // If we're splitting the dwarf out now that we've got the entire
1274     // CU then add the dwo id to it.
1275     auto *SkCU = TheCU.getSkeleton();
1276 
1277     bool HasSplitUnit = SkCU && !TheCU.getUnitDie().children().empty();
1278 
1279     if (HasSplitUnit) {
1280       (void)HasEmittedSplitCU;
1281       assert((shareAcrossDWOCUs() || !HasEmittedSplitCU) &&
1282              "Multiple CUs emitted into a single dwo file");
1283       HasEmittedSplitCU = true;
1284       dwarf::Attribute attrDWOName = getDwarfVersion() >= 5
1285                                          ? dwarf::DW_AT_dwo_name
1286                                          : dwarf::DW_AT_GNU_dwo_name;
1287       finishUnitAttributes(TheCU.getCUNode(), TheCU);
1288       StringRef DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile;
1289       TheCU.addString(TheCU.getUnitDie(), attrDWOName, DWOName);
1290       SkCU->addString(SkCU->getUnitDie(), attrDWOName, DWOName);
1291       // Emit a unique identifier for this CU. Include the DWO file name in the
1292       // hash to avoid the case where two (almost) empty compile units have the
1293       // same contents. This can happen if link-time optimization removes nearly
1294       // all (unused) code from a CU.
1295       uint64_t ID =
1296           DIEHash(Asm, &TheCU).computeCUSignature(DWOName, TheCU.getUnitDie());
1297       if (getDwarfVersion() >= 5) {
1298         TheCU.setDWOId(ID);
1299         SkCU->setDWOId(ID);
1300       } else {
1301         TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
1302                       dwarf::DW_FORM_data8, ID);
1303         SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
1304                       dwarf::DW_FORM_data8, ID);
1305       }
1306 
1307       if (getDwarfVersion() < 5 && !SkeletonHolder.getRangeLists().empty()) {
1308         const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
1309         SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
1310                               Sym, Sym);
1311       }
1312     } else if (SkCU) {
1313       finishUnitAttributes(SkCU->getCUNode(), *SkCU);
1314     }
1315 
1316     // If we have code split among multiple sections or non-contiguous
1317     // ranges of code then emit a DW_AT_ranges attribute on the unit that will
1318     // remain in the .o file, otherwise add a DW_AT_low_pc.
1319     // FIXME: We should use ranges allow reordering of code ala
1320     // .subsections_via_symbols in mach-o. This would mean turning on
1321     // ranges for all subprogram DIEs for mach-o.
1322     DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
1323 
1324     if (unsigned NumRanges = TheCU.getRanges().size()) {
1325       // PTX does not support subtracting labels from the code section in the
1326       // debug_loc section.  To work around this, the NVPTX backend needs the
1327       // compile unit to have no low_pc in order to have a zero base_address
1328       // when handling debug_loc in cuda-gdb.
1329       if (!(Asm->TM.getTargetTriple().isNVPTX() && tuneForGDB())) {
1330         if (NumRanges > 1 && useRangesSection())
1331           // A DW_AT_low_pc attribute may also be specified in combination with
1332           // DW_AT_ranges to specify the default base address for use in
1333           // location lists (see Section 2.6.2) and range lists (see Section
1334           // 2.17.3).
1335           U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1336                     0);
1337         else
1338           U.setBaseAddress(TheCU.getRanges().front().Begin);
1339         U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
1340       }
1341     }
1342 
1343     // We don't keep track of which addresses are used in which CU so this
1344     // is a bit pessimistic under LTO.
1345     if ((HasSplitUnit || getDwarfVersion() >= 5) && !AddrPool.isEmpty())
1346       U.addAddrTableBase();
1347 
1348     if (getDwarfVersion() >= 5) {
1349       if (U.hasRangeLists())
1350         U.addRnglistsBase();
1351 
1352       if (!DebugLocs.getLists().empty() && !useSplitDwarf()) {
1353         U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_loclists_base,
1354                           DebugLocs.getSym(),
1355                           TLOF.getDwarfLoclistsSection()->getBeginSymbol());
1356       }
1357     }
1358 
1359     auto *CUNode = cast<DICompileUnit>(P.first);
1360     // If compile Unit has macros, emit "DW_AT_macro_info/DW_AT_macros"
1361     // attribute.
1362     if (CUNode->getMacros()) {
1363       if (UseDebugMacroSection) {
1364         if (useSplitDwarf())
1365           TheCU.addSectionDelta(
1366               TheCU.getUnitDie(), dwarf::DW_AT_macros, U.getMacroLabelBegin(),
1367               TLOF.getDwarfMacroDWOSection()->getBeginSymbol());
1368         else {
1369           dwarf::Attribute MacrosAttr = getDwarfVersion() >= 5
1370                                             ? dwarf::DW_AT_macros
1371                                             : dwarf::DW_AT_GNU_macros;
1372           U.addSectionLabel(U.getUnitDie(), MacrosAttr, U.getMacroLabelBegin(),
1373                             TLOF.getDwarfMacroSection()->getBeginSymbol());
1374         }
1375       } else {
1376         if (useSplitDwarf())
1377           TheCU.addSectionDelta(
1378               TheCU.getUnitDie(), dwarf::DW_AT_macro_info,
1379               U.getMacroLabelBegin(),
1380               TLOF.getDwarfMacinfoDWOSection()->getBeginSymbol());
1381         else
1382           U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info,
1383                             U.getMacroLabelBegin(),
1384                             TLOF.getDwarfMacinfoSection()->getBeginSymbol());
1385       }
1386     }
1387     }
1388 
1389   // Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
1390   for (auto *CUNode : MMI->getModule()->debug_compile_units())
1391     if (CUNode->getDWOId())
1392       getOrCreateDwarfCompileUnit(CUNode);
1393 
1394   // Compute DIE offsets and sizes.
1395   InfoHolder.computeSizeAndOffsets();
1396   if (useSplitDwarf())
1397     SkeletonHolder.computeSizeAndOffsets();
1398 
1399   // Now that offsets are computed, can replace DIEs in debug_names Entry with
1400   // an actual offset.
1401   AccelDebugNames.convertDieToOffset();
1402 }
1403 
1404 // Emit all Dwarf sections that should come after the content.
1405 void DwarfDebug::endModule() {
1406   // Terminate the pending line table.
1407   if (PrevCU)
1408     terminateLineTable(PrevCU);
1409   PrevCU = nullptr;
1410   assert(CurFn == nullptr);
1411   assert(CurMI == nullptr);
1412 
1413   for (const auto &P : CUMap) {
1414     const auto *CUNode = cast<DICompileUnit>(P.first);
1415     DwarfCompileUnit *CU = &*P.second;
1416 
1417     // Emit imported entities.
1418     for (auto *IE : CUNode->getImportedEntities()) {
1419       assert(!isa_and_nonnull<DILocalScope>(IE->getScope()) &&
1420              "Unexpected function-local entity in 'imports' CU field.");
1421       CU->getOrCreateImportedEntityDIE(IE);
1422     }
1423     for (const auto *D : CU->getDeferredLocalDecls()) {
1424       if (auto *IE = dyn_cast<DIImportedEntity>(D))
1425         CU->getOrCreateImportedEntityDIE(IE);
1426       else
1427         llvm_unreachable("Unexpected local retained node!");
1428     }
1429 
1430     // Emit base types.
1431     CU->createBaseTypeDIEs();
1432   }
1433 
1434   // If we aren't actually generating debug info (check beginModule -
1435   // conditionalized on the presence of the llvm.dbg.cu metadata node)
1436   if (!Asm || !Asm->hasDebugInfo())
1437     return;
1438 
1439   // Finalize the debug info for the module.
1440   finalizeModuleInfo();
1441 
1442   if (useSplitDwarf())
1443     // Emit debug_loc.dwo/debug_loclists.dwo section.
1444     emitDebugLocDWO();
1445   else
1446     // Emit debug_loc/debug_loclists section.
1447     emitDebugLoc();
1448 
1449   // Corresponding abbreviations into a abbrev section.
1450   emitAbbreviations();
1451 
1452   // Emit all the DIEs into a debug info section.
1453   emitDebugInfo();
1454 
1455   // Emit info into a debug aranges section.
1456   if (UseARangesSection)
1457     emitDebugARanges();
1458 
1459   // Emit info into a debug ranges section.
1460   emitDebugRanges();
1461 
1462   if (useSplitDwarf())
1463   // Emit info into a debug macinfo.dwo section.
1464     emitDebugMacinfoDWO();
1465   else
1466     // Emit info into a debug macinfo/macro section.
1467     emitDebugMacinfo();
1468 
1469   emitDebugStr();
1470 
1471   if (useSplitDwarf()) {
1472     emitDebugStrDWO();
1473     emitDebugInfoDWO();
1474     emitDebugAbbrevDWO();
1475     emitDebugLineDWO();
1476     emitDebugRangesDWO();
1477   }
1478 
1479   emitDebugAddr();
1480 
1481   // Emit info into the dwarf accelerator table sections.
1482   switch (getAccelTableKind()) {
1483   case AccelTableKind::Apple:
1484     emitAccelNames();
1485     emitAccelObjC();
1486     emitAccelNamespaces();
1487     emitAccelTypes();
1488     break;
1489   case AccelTableKind::Dwarf:
1490     emitAccelDebugNames();
1491     break;
1492   case AccelTableKind::None:
1493     break;
1494   case AccelTableKind::Default:
1495     llvm_unreachable("Default should have already been resolved.");
1496   }
1497 
1498   // Emit the pubnames and pubtypes sections if requested.
1499   emitDebugPubSections();
1500 
1501   // clean up.
1502   // FIXME: AbstractVariables.clear();
1503 }
1504 
1505 void DwarfDebug::ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU,
1506     const DINode *Node, const MDNode *ScopeNode) {
1507   if (CU.getExistingAbstractEntity(Node))
1508     return;
1509 
1510   if (LexicalScope *Scope =
1511           LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode)))
1512     CU.createAbstractEntity(Node, Scope);
1513 }
1514 
1515 static const DILocalScope *getRetainedNodeScope(const MDNode *N) {
1516   const DIScope *S;
1517   if (const auto *LV = dyn_cast<DILocalVariable>(N))
1518     S = LV->getScope();
1519   else if (const auto *L = dyn_cast<DILabel>(N))
1520     S = L->getScope();
1521   else if (const auto *IE = dyn_cast<DIImportedEntity>(N))
1522     S = IE->getScope();
1523   else
1524     llvm_unreachable("Unexpected retained node!");
1525 
1526   // Ensure the scope is not a DILexicalBlockFile.
1527   return cast<DILocalScope>(S)->getNonLexicalBlockFileScope();
1528 }
1529 
1530 // Collect variable information from side table maintained by MF.
1531 void DwarfDebug::collectVariableInfoFromMFTable(
1532     DwarfCompileUnit &TheCU, DenseSet<InlinedEntity> &Processed) {
1533   SmallDenseMap<InlinedEntity, DbgVariable *> MFVars;
1534   LLVM_DEBUG(dbgs() << "DwarfDebug: collecting variables from MF side table\n");
1535   for (const auto &VI : Asm->MF->getVariableDbgInfo()) {
1536     if (!VI.Var)
1537       continue;
1538     assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
1539            "Expected inlined-at fields to agree");
1540 
1541     InlinedEntity Var(VI.Var, VI.Loc->getInlinedAt());
1542     Processed.insert(Var);
1543     LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
1544 
1545     // If variable scope is not found then skip this variable.
1546     if (!Scope) {
1547       LLVM_DEBUG(dbgs() << "Dropping debug info for " << VI.Var->getName()
1548                         << ", no variable scope found\n");
1549       continue;
1550     }
1551 
1552     ensureAbstractEntityIsCreatedIfScoped(TheCU, Var.first, Scope->getScopeNode());
1553 
1554     // If we have already seen information for this variable, add to what we
1555     // already know.
1556     if (DbgVariable *PreviousLoc = MFVars.lookup(Var)) {
1557       auto *PreviousMMI = std::get_if<Loc::MMI>(PreviousLoc);
1558       auto *PreviousEntryValue = std::get_if<Loc::EntryValue>(PreviousLoc);
1559       // Previous and new locations are both stack slots (MMI).
1560       if (PreviousMMI && VI.inStackSlot())
1561         PreviousMMI->addFrameIndexExpr(VI.Expr, VI.getStackSlot());
1562       // Previous and new locations are both entry values.
1563       else if (PreviousEntryValue && VI.inEntryValueRegister())
1564         PreviousEntryValue->addExpr(VI.getEntryValueRegister(), *VI.Expr);
1565       else {
1566         // Locations differ, this should (rarely) happen in optimized async
1567         // coroutines.
1568         // Prefer whichever location has an EntryValue.
1569         if (PreviousLoc->holds<Loc::MMI>())
1570           PreviousLoc->emplace<Loc::EntryValue>(VI.getEntryValueRegister(),
1571                                                 *VI.Expr);
1572         LLVM_DEBUG(dbgs() << "Dropping debug info for " << VI.Var->getName()
1573                           << ", conflicting fragment location types\n");
1574       }
1575       continue;
1576     }
1577 
1578     auto RegVar = std::make_unique<DbgVariable>(
1579                     cast<DILocalVariable>(Var.first), Var.second);
1580     if (VI.inStackSlot())
1581       RegVar->emplace<Loc::MMI>(VI.Expr, VI.getStackSlot());
1582     else
1583       RegVar->emplace<Loc::EntryValue>(VI.getEntryValueRegister(), *VI.Expr);
1584     LLVM_DEBUG(dbgs() << "Created DbgVariable for " << VI.Var->getName()
1585                       << "\n");
1586     InfoHolder.addScopeVariable(Scope, RegVar.get());
1587     MFVars.insert({Var, RegVar.get()});
1588     ConcreteEntities.push_back(std::move(RegVar));
1589   }
1590 }
1591 
1592 /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its
1593 /// enclosing lexical scope. The check ensures there are no other instructions
1594 /// in the same lexical scope preceding the DBG_VALUE and that its range is
1595 /// either open or otherwise rolls off the end of the scope.
1596 static bool validThroughout(LexicalScopes &LScopes,
1597                             const MachineInstr *DbgValue,
1598                             const MachineInstr *RangeEnd,
1599                             const InstructionOrdering &Ordering) {
1600   assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location");
1601   auto MBB = DbgValue->getParent();
1602   auto DL = DbgValue->getDebugLoc();
1603   auto *LScope = LScopes.findLexicalScope(DL);
1604   // Scope doesn't exist; this is a dead DBG_VALUE.
1605   if (!LScope)
1606     return false;
1607   auto &LSRange = LScope->getRanges();
1608   if (LSRange.size() == 0)
1609     return false;
1610 
1611   const MachineInstr *LScopeBegin = LSRange.front().first;
1612   // If the scope starts before the DBG_VALUE then we may have a negative
1613   // result. Otherwise the location is live coming into the scope and we
1614   // can skip the following checks.
1615   if (!Ordering.isBefore(DbgValue, LScopeBegin)) {
1616     // Exit if the lexical scope begins outside of the current block.
1617     if (LScopeBegin->getParent() != MBB)
1618       return false;
1619 
1620     MachineBasicBlock::const_reverse_iterator Pred(DbgValue);
1621     for (++Pred; Pred != MBB->rend(); ++Pred) {
1622       if (Pred->getFlag(MachineInstr::FrameSetup))
1623         break;
1624       auto PredDL = Pred->getDebugLoc();
1625       if (!PredDL || Pred->isMetaInstruction())
1626         continue;
1627       // Check whether the instruction preceding the DBG_VALUE is in the same
1628       // (sub)scope as the DBG_VALUE.
1629       if (DL->getScope() == PredDL->getScope())
1630         return false;
1631       auto *PredScope = LScopes.findLexicalScope(PredDL);
1632       if (!PredScope || LScope->dominates(PredScope))
1633         return false;
1634     }
1635   }
1636 
1637   // If the range of the DBG_VALUE is open-ended, report success.
1638   if (!RangeEnd)
1639     return true;
1640 
1641   // Single, constant DBG_VALUEs in the prologue are promoted to be live
1642   // throughout the function. This is a hack, presumably for DWARF v2 and not
1643   // necessarily correct. It would be much better to use a dbg.declare instead
1644   // if we know the constant is live throughout the scope.
1645   if (MBB->pred_empty() &&
1646       all_of(DbgValue->debug_operands(),
1647              [](const MachineOperand &Op) { return Op.isImm(); }))
1648     return true;
1649 
1650   // Test if the location terminates before the end of the scope.
1651   const MachineInstr *LScopeEnd = LSRange.back().second;
1652   if (Ordering.isBefore(RangeEnd, LScopeEnd))
1653     return false;
1654 
1655   // There's a single location which starts at the scope start, and ends at or
1656   // after the scope end.
1657   return true;
1658 }
1659 
1660 /// Build the location list for all DBG_VALUEs in the function that
1661 /// describe the same variable. The resulting DebugLocEntries will have
1662 /// strict monotonically increasing begin addresses and will never
1663 /// overlap. If the resulting list has only one entry that is valid
1664 /// throughout variable's scope return true.
1665 //
1666 // See the definition of DbgValueHistoryMap::Entry for an explanation of the
1667 // different kinds of history map entries. One thing to be aware of is that if
1668 // a debug value is ended by another entry (rather than being valid until the
1669 // end of the function), that entry's instruction may or may not be included in
1670 // the range, depending on if the entry is a clobbering entry (it has an
1671 // instruction that clobbers one or more preceding locations), or if it is an
1672 // (overlapping) debug value entry. This distinction can be seen in the example
1673 // below. The first debug value is ended by the clobbering entry 2, and the
1674 // second and third debug values are ended by the overlapping debug value entry
1675 // 4.
1676 //
1677 // Input:
1678 //
1679 //   History map entries [type, end index, mi]
1680 //
1681 // 0 |      [DbgValue, 2, DBG_VALUE $reg0, [...] (fragment 0, 32)]
1682 // 1 | |    [DbgValue, 4, DBG_VALUE $reg1, [...] (fragment 32, 32)]
1683 // 2 | |    [Clobber, $reg0 = [...], -, -]
1684 // 3   | |  [DbgValue, 4, DBG_VALUE 123, [...] (fragment 64, 32)]
1685 // 4        [DbgValue, ~0, DBG_VALUE @g, [...] (fragment 0, 96)]
1686 //
1687 // Output [start, end) [Value...]:
1688 //
1689 // [0-1)    [(reg0, fragment 0, 32)]
1690 // [1-3)    [(reg0, fragment 0, 32), (reg1, fragment 32, 32)]
1691 // [3-4)    [(reg1, fragment 32, 32), (123, fragment 64, 32)]
1692 // [4-)     [(@g, fragment 0, 96)]
1693 bool DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
1694                                    const DbgValueHistoryMap::Entries &Entries) {
1695   using OpenRange =
1696       std::pair<DbgValueHistoryMap::EntryIndex, DbgValueLoc>;
1697   SmallVector<OpenRange, 4> OpenRanges;
1698   bool isSafeForSingleLocation = true;
1699   const MachineInstr *StartDebugMI = nullptr;
1700   const MachineInstr *EndMI = nullptr;
1701 
1702   for (auto EB = Entries.begin(), EI = EB, EE = Entries.end(); EI != EE; ++EI) {
1703     const MachineInstr *Instr = EI->getInstr();
1704 
1705     // Remove all values that are no longer live.
1706     size_t Index = std::distance(EB, EI);
1707     erase_if(OpenRanges, [&](OpenRange &R) { return R.first <= Index; });
1708 
1709     // If we are dealing with a clobbering entry, this iteration will result in
1710     // a location list entry starting after the clobbering instruction.
1711     const MCSymbol *StartLabel =
1712         EI->isClobber() ? getLabelAfterInsn(Instr) : getLabelBeforeInsn(Instr);
1713     assert(StartLabel &&
1714            "Forgot label before/after instruction starting a range!");
1715 
1716     const MCSymbol *EndLabel;
1717     if (std::next(EI) == Entries.end()) {
1718       const MachineBasicBlock &EndMBB = Asm->MF->back();
1719       EndLabel = Asm->MBBSectionRanges[EndMBB.getSectionID()].EndLabel;
1720       if (EI->isClobber())
1721         EndMI = EI->getInstr();
1722     }
1723     else if (std::next(EI)->isClobber())
1724       EndLabel = getLabelAfterInsn(std::next(EI)->getInstr());
1725     else
1726       EndLabel = getLabelBeforeInsn(std::next(EI)->getInstr());
1727     assert(EndLabel && "Forgot label after instruction ending a range!");
1728 
1729     if (EI->isDbgValue())
1730       LLVM_DEBUG(dbgs() << "DotDebugLoc: " << *Instr << "\n");
1731 
1732     // If this history map entry has a debug value, add that to the list of
1733     // open ranges and check if its location is valid for a single value
1734     // location.
1735     if (EI->isDbgValue()) {
1736       // Do not add undef debug values, as they are redundant information in
1737       // the location list entries. An undef debug results in an empty location
1738       // description. If there are any non-undef fragments then padding pieces
1739       // with empty location descriptions will automatically be inserted, and if
1740       // all fragments are undef then the whole location list entry is
1741       // redundant.
1742       if (!Instr->isUndefDebugValue()) {
1743         auto Value = getDebugLocValue(Instr);
1744         OpenRanges.emplace_back(EI->getEndIndex(), Value);
1745 
1746         // TODO: Add support for single value fragment locations.
1747         if (Instr->getDebugExpression()->isFragment())
1748           isSafeForSingleLocation = false;
1749 
1750         if (!StartDebugMI)
1751           StartDebugMI = Instr;
1752       } else {
1753         isSafeForSingleLocation = false;
1754       }
1755     }
1756 
1757     // Location list entries with empty location descriptions are redundant
1758     // information in DWARF, so do not emit those.
1759     if (OpenRanges.empty())
1760       continue;
1761 
1762     // Omit entries with empty ranges as they do not have any effect in DWARF.
1763     if (StartLabel == EndLabel) {
1764       LLVM_DEBUG(dbgs() << "Omitting location list entry with empty range.\n");
1765       continue;
1766     }
1767 
1768     SmallVector<DbgValueLoc, 4> Values;
1769     for (auto &R : OpenRanges)
1770       Values.push_back(R.second);
1771 
1772     // With Basic block sections, it is posssible that the StartLabel and the
1773     // Instr are not in the same section.  This happens when the StartLabel is
1774     // the function begin label and the dbg value appears in a basic block
1775     // that is not the entry.  In this case, the range needs to be split to
1776     // span each individual section in the range from StartLabel to EndLabel.
1777     if (Asm->MF->hasBBSections() && StartLabel == Asm->getFunctionBegin() &&
1778         !Instr->getParent()->sameSection(&Asm->MF->front())) {
1779       for (const auto &[MBBSectionId, MBBSectionRange] :
1780            Asm->MBBSectionRanges) {
1781         if (Instr->getParent()->getSectionID() == MBBSectionId) {
1782           DebugLoc.emplace_back(MBBSectionRange.BeginLabel, EndLabel, Values);
1783           break;
1784         }
1785         DebugLoc.emplace_back(MBBSectionRange.BeginLabel,
1786                               MBBSectionRange.EndLabel, Values);
1787       }
1788     } else {
1789       DebugLoc.emplace_back(StartLabel, EndLabel, Values);
1790     }
1791 
1792     // Attempt to coalesce the ranges of two otherwise identical
1793     // DebugLocEntries.
1794     auto CurEntry = DebugLoc.rbegin();
1795     LLVM_DEBUG({
1796       dbgs() << CurEntry->getValues().size() << " Values:\n";
1797       for (auto &Value : CurEntry->getValues())
1798         Value.dump();
1799       dbgs() << "-----\n";
1800     });
1801 
1802     auto PrevEntry = std::next(CurEntry);
1803     if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
1804       DebugLoc.pop_back();
1805   }
1806 
1807   if (!isSafeForSingleLocation ||
1808       !validThroughout(LScopes, StartDebugMI, EndMI, getInstOrdering()))
1809     return false;
1810 
1811   if (DebugLoc.size() == 1)
1812     return true;
1813 
1814   if (!Asm->MF->hasBBSections())
1815     return false;
1816 
1817   // Check here to see if loclist can be merged into a single range. If not,
1818   // we must keep the split loclists per section.  This does exactly what
1819   // MergeRanges does without sections.  We don't actually merge the ranges
1820   // as the split ranges must be kept intact if this cannot be collapsed
1821   // into a single range.
1822   const MachineBasicBlock *RangeMBB = nullptr;
1823   if (DebugLoc[0].getBeginSym() == Asm->getFunctionBegin())
1824     RangeMBB = &Asm->MF->front();
1825   else
1826     RangeMBB = Entries.begin()->getInstr()->getParent();
1827   auto RangeIt = Asm->MBBSectionRanges.find(RangeMBB->getSectionID());
1828   assert(RangeIt != Asm->MBBSectionRanges.end() &&
1829          "Range MBB not found in MBBSectionRanges!");
1830   auto *CurEntry = DebugLoc.begin();
1831   auto *NextEntry = std::next(CurEntry);
1832   auto NextRangeIt = std::next(RangeIt);
1833   while (NextEntry != DebugLoc.end()) {
1834     if (NextRangeIt == Asm->MBBSectionRanges.end())
1835       return false;
1836     // CurEntry should end the current section and NextEntry should start
1837     // the next section and the Values must match for these two ranges to be
1838     // merged.  Do not match the section label end if it is the entry block
1839     // section.  This is because the end label for the Debug Loc and the
1840     // Function end label could be different.
1841     if ((RangeIt->second.EndLabel != Asm->getFunctionEnd() &&
1842          CurEntry->getEndSym() != RangeIt->second.EndLabel) ||
1843         NextEntry->getBeginSym() != NextRangeIt->second.BeginLabel ||
1844         CurEntry->getValues() != NextEntry->getValues())
1845       return false;
1846     RangeIt = NextRangeIt;
1847     NextRangeIt = std::next(RangeIt);
1848     CurEntry = NextEntry;
1849     NextEntry = std::next(CurEntry);
1850   }
1851   return true;
1852 }
1853 
1854 DbgEntity *DwarfDebug::createConcreteEntity(DwarfCompileUnit &TheCU,
1855                                             LexicalScope &Scope,
1856                                             const DINode *Node,
1857                                             const DILocation *Location,
1858                                             const MCSymbol *Sym) {
1859   ensureAbstractEntityIsCreatedIfScoped(TheCU, Node, Scope.getScopeNode());
1860   if (isa<const DILocalVariable>(Node)) {
1861     ConcreteEntities.push_back(
1862         std::make_unique<DbgVariable>(cast<const DILocalVariable>(Node),
1863                                        Location));
1864     InfoHolder.addScopeVariable(&Scope,
1865         cast<DbgVariable>(ConcreteEntities.back().get()));
1866   } else if (isa<const DILabel>(Node)) {
1867     ConcreteEntities.push_back(
1868         std::make_unique<DbgLabel>(cast<const DILabel>(Node),
1869                                     Location, Sym));
1870     InfoHolder.addScopeLabel(&Scope,
1871         cast<DbgLabel>(ConcreteEntities.back().get()));
1872   }
1873   return ConcreteEntities.back().get();
1874 }
1875 
1876 // Find variables for each lexical scope.
1877 void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU,
1878                                    const DISubprogram *SP,
1879                                    DenseSet<InlinedEntity> &Processed) {
1880   // Grab the variable info that was squirreled away in the MMI side-table.
1881   collectVariableInfoFromMFTable(TheCU, Processed);
1882 
1883   for (const auto &I : DbgValues) {
1884     InlinedEntity IV = I.first;
1885     if (Processed.count(IV))
1886       continue;
1887 
1888     // Instruction ranges, specifying where IV is accessible.
1889     const auto &HistoryMapEntries = I.second;
1890 
1891     // Try to find any non-empty variable location. Do not create a concrete
1892     // entity if there are no locations.
1893     if (!DbgValues.hasNonEmptyLocation(HistoryMapEntries))
1894       continue;
1895 
1896     LexicalScope *Scope = nullptr;
1897     const DILocalVariable *LocalVar = cast<DILocalVariable>(IV.first);
1898     if (const DILocation *IA = IV.second)
1899       Scope = LScopes.findInlinedScope(LocalVar->getScope(), IA);
1900     else
1901       Scope = LScopes.findLexicalScope(LocalVar->getScope());
1902     // If variable scope is not found then skip this variable.
1903     if (!Scope)
1904       continue;
1905 
1906     Processed.insert(IV);
1907     DbgVariable *RegVar = cast<DbgVariable>(createConcreteEntity(TheCU,
1908                                             *Scope, LocalVar, IV.second));
1909 
1910     const MachineInstr *MInsn = HistoryMapEntries.front().getInstr();
1911     assert(MInsn->isDebugValue() && "History must begin with debug value");
1912 
1913     // Check if there is a single DBG_VALUE, valid throughout the var's scope.
1914     // If the history map contains a single debug value, there may be an
1915     // additional entry which clobbers the debug value.
1916     size_t HistSize = HistoryMapEntries.size();
1917     bool SingleValueWithClobber =
1918         HistSize == 2 && HistoryMapEntries[1].isClobber();
1919     if (HistSize == 1 || SingleValueWithClobber) {
1920       const auto *End =
1921           SingleValueWithClobber ? HistoryMapEntries[1].getInstr() : nullptr;
1922       if (validThroughout(LScopes, MInsn, End, getInstOrdering())) {
1923         RegVar->emplace<Loc::Single>(MInsn);
1924         continue;
1925       }
1926     }
1927 
1928     // Handle multiple DBG_VALUE instructions describing one variable.
1929     DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar);
1930 
1931     // Build the location list for this variable.
1932     SmallVector<DebugLocEntry, 8> Entries;
1933     bool isValidSingleLocation = buildLocationList(Entries, HistoryMapEntries);
1934 
1935     // Check whether buildLocationList managed to merge all locations to one
1936     // that is valid throughout the variable's scope. If so, produce single
1937     // value location.
1938     if (isValidSingleLocation) {
1939       RegVar->emplace<Loc::Single>(Entries[0].getValues()[0]);
1940       continue;
1941     }
1942 
1943     // If the variable has a DIBasicType, extract it.  Basic types cannot have
1944     // unique identifiers, so don't bother resolving the type with the
1945     // identifier map.
1946     const DIBasicType *BT = dyn_cast<DIBasicType>(
1947         static_cast<const Metadata *>(LocalVar->getType()));
1948 
1949     // Finalize the entry by lowering it into a DWARF bytestream.
1950     for (auto &Entry : Entries)
1951       Entry.finalize(*Asm, List, BT, TheCU);
1952   }
1953 
1954   // For each InlinedEntity collected from DBG_LABEL instructions, convert to
1955   // DWARF-related DbgLabel.
1956   for (const auto &I : DbgLabels) {
1957     InlinedEntity IL = I.first;
1958     const MachineInstr *MI = I.second;
1959     if (MI == nullptr)
1960       continue;
1961 
1962     LexicalScope *Scope = nullptr;
1963     const DILabel *Label = cast<DILabel>(IL.first);
1964     // The scope could have an extra lexical block file.
1965     const DILocalScope *LocalScope =
1966         Label->getScope()->getNonLexicalBlockFileScope();
1967     // Get inlined DILocation if it is inlined label.
1968     if (const DILocation *IA = IL.second)
1969       Scope = LScopes.findInlinedScope(LocalScope, IA);
1970     else
1971       Scope = LScopes.findLexicalScope(LocalScope);
1972     // If label scope is not found then skip this label.
1973     if (!Scope)
1974       continue;
1975 
1976     Processed.insert(IL);
1977     /// At this point, the temporary label is created.
1978     /// Save the temporary label to DbgLabel entity to get the
1979     /// actually address when generating Dwarf DIE.
1980     MCSymbol *Sym = getLabelBeforeInsn(MI);
1981     createConcreteEntity(TheCU, *Scope, Label, IL.second, Sym);
1982   }
1983 
1984   // Collect info for retained nodes.
1985   for (const DINode *DN : SP->getRetainedNodes()) {
1986     const auto *LS = getRetainedNodeScope(DN);
1987     if (isa<DILocalVariable>(DN) || isa<DILabel>(DN)) {
1988       if (!Processed.insert(InlinedEntity(DN, nullptr)).second)
1989         continue;
1990       LexicalScope *LexS = LScopes.findLexicalScope(LS);
1991       if (LexS)
1992         createConcreteEntity(TheCU, *LexS, DN, nullptr);
1993     } else {
1994       LocalDeclsPerLS[LS].insert(DN);
1995     }
1996   }
1997 }
1998 
1999 // Process beginning of an instruction.
2000 void DwarfDebug::beginInstruction(const MachineInstr *MI) {
2001   const MachineFunction &MF = *MI->getMF();
2002   const auto *SP = MF.getFunction().getSubprogram();
2003   bool NoDebug =
2004       !SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug;
2005 
2006   // Delay slot support check.
2007   auto delaySlotSupported = [](const MachineInstr &MI) {
2008     if (!MI.isBundledWithSucc())
2009       return false;
2010     auto Suc = std::next(MI.getIterator());
2011     (void)Suc;
2012     // Ensure that delay slot instruction is successor of the call instruction.
2013     // Ex. CALL_INSTRUCTION {
2014     //        DELAY_SLOT_INSTRUCTION }
2015     assert(Suc->isBundledWithPred() &&
2016            "Call bundle instructions are out of order");
2017     return true;
2018   };
2019 
2020   // When describing calls, we need a label for the call instruction.
2021   if (!NoDebug && SP->areAllCallsDescribed() &&
2022       MI->isCandidateForAdditionalCallInfo(MachineInstr::AnyInBundle) &&
2023       (!MI->hasDelaySlot() || delaySlotSupported(*MI))) {
2024     const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
2025     bool IsTail = TII->isTailCall(*MI);
2026     // For tail calls, we need the address of the branch instruction for
2027     // DW_AT_call_pc.
2028     if (IsTail)
2029       requestLabelBeforeInsn(MI);
2030     // For non-tail calls, we need the return address for the call for
2031     // DW_AT_call_return_pc. Under GDB tuning, this information is needed for
2032     // tail calls as well.
2033     requestLabelAfterInsn(MI);
2034   }
2035 
2036   DebugHandlerBase::beginInstruction(MI);
2037   if (!CurMI)
2038     return;
2039 
2040   if (NoDebug)
2041     return;
2042 
2043   // Check if source location changes, but ignore DBG_VALUE and CFI locations.
2044   // If the instruction is part of the function frame setup code, do not emit
2045   // any line record, as there is no correspondence with any user code.
2046   if (MI->isMetaInstruction() || MI->getFlag(MachineInstr::FrameSetup))
2047     return;
2048   const DebugLoc &DL = MI->getDebugLoc();
2049   unsigned Flags = 0;
2050 
2051   if (MI->getFlag(MachineInstr::FrameDestroy) && DL) {
2052     const MachineBasicBlock *MBB = MI->getParent();
2053     if (MBB && (MBB != EpilogBeginBlock)) {
2054       // First time FrameDestroy has been seen in this basic block
2055       EpilogBeginBlock = MBB;
2056       Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2057     }
2058   }
2059 
2060   // When we emit a line-0 record, we don't update PrevInstLoc; so look at
2061   // the last line number actually emitted, to see if it was line 0.
2062   unsigned LastAsmLine =
2063       Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
2064 
2065   if (!DL && MI == PrologEndLoc) {
2066     // In rare situations, we might want to place the end of the prologue
2067     // somewhere that doesn't have a source location already. It should be in
2068     // the entry block.
2069     assert(MI->getParent() == &*MI->getMF()->begin());
2070     recordSourceLine(SP->getScopeLine(), 0, SP,
2071                      DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT);
2072     return;
2073   }
2074 
2075   bool PrevInstInSameSection =
2076       (!PrevInstBB ||
2077        PrevInstBB->getSectionID() == MI->getParent()->getSectionID());
2078   bool ForceIsStmt = ForceIsStmtInstrs.contains(MI);
2079   if (DL == PrevInstLoc && PrevInstInSameSection && !ForceIsStmt) {
2080     // If we have an ongoing unspecified location, nothing to do here.
2081     if (!DL)
2082       return;
2083     // We have an explicit location, same as the previous location.
2084     // But we might be coming back to it after a line 0 record.
2085     if ((LastAsmLine == 0 && DL.getLine() != 0) || Flags) {
2086       // Reinstate the source location but not marked as a statement.
2087       const MDNode *Scope = DL.getScope();
2088       recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
2089     }
2090     return;
2091   }
2092 
2093   if (!DL) {
2094     // We have an unspecified location, which might want to be line 0.
2095     // If we have already emitted a line-0 record, don't repeat it.
2096     if (LastAsmLine == 0)
2097       return;
2098     // If user said Don't Do That, don't do that.
2099     if (UnknownLocations == Disable)
2100       return;
2101     // See if we have a reason to emit a line-0 record now.
2102     // Reasons to emit a line-0 record include:
2103     // - User asked for it (UnknownLocations).
2104     // - Instruction has a label, so it's referenced from somewhere else,
2105     //   possibly debug information; we want it to have a source location.
2106     // - Instruction is at the top of a block; we don't want to inherit the
2107     //   location from the physically previous (maybe unrelated) block.
2108     if (UnknownLocations == Enable || PrevLabel ||
2109         (PrevInstBB && PrevInstBB != MI->getParent())) {
2110       // Preserve the file and column numbers, if we can, to save space in
2111       // the encoded line table.
2112       // Do not update PrevInstLoc, it remembers the last non-0 line.
2113       const MDNode *Scope = nullptr;
2114       unsigned Column = 0;
2115       if (PrevInstLoc) {
2116         Scope = PrevInstLoc.getScope();
2117         Column = PrevInstLoc.getCol();
2118       }
2119       recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0);
2120     }
2121     return;
2122   }
2123 
2124   // We have an explicit location, different from the previous location.
2125   // Don't repeat a line-0 record, but otherwise emit the new location.
2126   // (The new location might be an explicit line 0, which we do emit.)
2127   if (DL.getLine() == 0 && LastAsmLine == 0)
2128     return;
2129   if (MI == PrologEndLoc) {
2130     Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT;
2131     PrologEndLoc = nullptr;
2132   }
2133   // If the line changed, we call that a new statement; unless we went to
2134   // line 0 and came back, in which case it is not a new statement.
2135   unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
2136   if (DL.getLine() && (DL.getLine() != OldLine || ForceIsStmt))
2137     Flags |= DWARF2_FLAG_IS_STMT;
2138 
2139   const MDNode *Scope = DL.getScope();
2140   recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
2141 
2142   // If we're not at line 0, remember this location.
2143   if (DL.getLine())
2144     PrevInstLoc = DL;
2145 }
2146 
2147 static std::pair<const MachineInstr *, bool>
2148 findPrologueEndLoc(const MachineFunction *MF) {
2149   // First known non-DBG_VALUE and non-frame setup location marks
2150   // the beginning of the function body.
2151   const auto &TII = *MF->getSubtarget().getInstrInfo();
2152   const MachineInstr *NonTrivialInst = nullptr;
2153   const Function &F = MF->getFunction();
2154 
2155   // Some instructions may be inserted into prologue after this function. Must
2156   // keep prologue for these cases.
2157   bool IsEmptyPrologue =
2158       !(F.hasPrologueData() || F.getMetadata(LLVMContext::MD_func_sanitize));
2159 
2160   // Helper lambda to examine each instruction and potentially return it
2161   // as the prologue_end point.
2162   auto ExamineInst = [&](const MachineInstr &MI)
2163       -> std::optional<std::pair<const MachineInstr *, bool>> {
2164     // Is this instruction trivial data shuffling or frame-setup?
2165     bool isCopy = (TII.isCopyInstr(MI) ? true : false);
2166     bool isTrivRemat = TII.isTriviallyReMaterializable(MI);
2167     bool isFrameSetup = MI.getFlag(MachineInstr::FrameSetup);
2168 
2169     if (!isFrameSetup && MI.getDebugLoc()) {
2170       // Scan forward to try to find a non-zero line number. The
2171       // prologue_end marks the first breakpoint in the function after the
2172       // frame setup, and a compiler-generated line 0 location is not a
2173       // meaningful breakpoint. If none is found, return the first
2174       // location after the frame setup.
2175       if (MI.getDebugLoc().getLine())
2176         return std::make_pair(&MI, IsEmptyPrologue);
2177     }
2178 
2179     // Keep track of the first "non-trivial" instruction seen, i.e. anything
2180     // that doesn't involve shuffling data around or is a frame-setup.
2181     if (!isCopy && !isTrivRemat && !isFrameSetup && !NonTrivialInst)
2182       NonTrivialInst = &MI;
2183 
2184     IsEmptyPrologue = false;
2185     return std::nullopt;
2186   };
2187 
2188   // Examine all the instructions at the start of the function. This doesn't
2189   // necessarily mean just the entry block: unoptimised code can fall-through
2190   // into an initial loop, and it makes sense to put the initial breakpoint on
2191   // the first instruction of such a loop. However, if we pass branches, we're
2192   // better off synthesising an early prologue_end.
2193   auto CurBlock = MF->begin();
2194   auto CurInst = CurBlock->begin();
2195 
2196   // Find the initial instruction, we're guaranteed one by the caller, but not
2197   // which block it's in.
2198   while (CurBlock->empty())
2199     CurInst = (++CurBlock)->begin();
2200   assert(CurInst != CurBlock->end());
2201 
2202   // Helper function for stepping through the initial sequence of
2203   // unconditionally executed instructions.
2204   auto getNextInst = [&CurBlock, &CurInst, MF]() -> bool {
2205     // We've reached the end of the block. Did we just look at a terminator?
2206     if (CurInst->isTerminator()) {
2207       // Some kind of "real" control flow is occurring. At the very least
2208       // we would have to start exploring the CFG, a good signal that the
2209       // prologue is over.
2210       return false;
2211     }
2212 
2213     // If we've already fallen through into a loop, don't fall through
2214     // further, use a backup-location.
2215     if (CurBlock->pred_size() > 1)
2216       return false;
2217 
2218     // Fall-through from entry to the next block. This is common at -O0 when
2219     // there's no initialisation in the function. Bail if we're also at the
2220     // end of the function, or the remaining blocks have no instructions.
2221     // Skip empty blocks, in rare cases the entry can be empty, and
2222     // other optimisations may add empty blocks that the control flow falls
2223     // through.
2224     do {
2225       ++CurBlock;
2226       if (CurBlock == MF->end())
2227         return false;
2228     } while (CurBlock->empty());
2229     CurInst = CurBlock->begin();
2230     return true;
2231   };
2232 
2233   while (true) {
2234     // Check whether this non-meta instruction a good position for prologue_end.
2235     if (!CurInst->isMetaInstruction()) {
2236       auto FoundInst = ExamineInst(*CurInst);
2237       if (FoundInst)
2238         return *FoundInst;
2239     }
2240 
2241     // Try to continue searching, but use a backup-location if substantive
2242     // computation is happening.
2243     auto NextInst = std::next(CurInst);
2244     if (NextInst != CurInst->getParent()->end()) {
2245       // Continue examining the current block.
2246       CurInst = NextInst;
2247       continue;
2248     }
2249 
2250     if (!getNextInst())
2251       break;
2252   }
2253 
2254   // We couldn't find any source-location, suggesting all meaningful information
2255   // got optimised away. Set the prologue_end to be the first non-trivial
2256   // instruction, which will get the scope line number. This is better than
2257   // nothing.
2258   // Only do this in the entry block, as we'll be giving it the scope line for
2259   // the function. Return IsEmptyPrologue==true if we've picked the first
2260   // instruction.
2261   if (NonTrivialInst && NonTrivialInst->getParent() == &*MF->begin()) {
2262     IsEmptyPrologue = NonTrivialInst == &*MF->begin()->begin();
2263     return std::make_pair(NonTrivialInst, IsEmptyPrologue);
2264   }
2265 
2266   // If the entry path is empty, just don't have a prologue_end at all.
2267   return std::make_pair(nullptr, IsEmptyPrologue);
2268 }
2269 
2270 /// Register a source line with debug info. Returns the  unique label that was
2271 /// emitted and which provides correspondence to the source line list.
2272 static void recordSourceLine(AsmPrinter &Asm, unsigned Line, unsigned Col,
2273                              const MDNode *S, unsigned Flags, unsigned CUID,
2274                              uint16_t DwarfVersion,
2275                              ArrayRef<std::unique_ptr<DwarfCompileUnit>> DCUs) {
2276   StringRef Fn;
2277   unsigned FileNo = 1;
2278   unsigned Discriminator = 0;
2279   if (auto *Scope = cast_or_null<DIScope>(S)) {
2280     Fn = Scope->getFilename();
2281     if (Line != 0 && DwarfVersion >= 4)
2282       if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
2283         Discriminator = LBF->getDiscriminator();
2284 
2285     FileNo = static_cast<DwarfCompileUnit &>(*DCUs[CUID])
2286                  .getOrCreateSourceID(Scope->getFile());
2287   }
2288   Asm.OutStreamer->emitDwarfLocDirective(FileNo, Line, Col, Flags, 0,
2289                                          Discriminator, Fn);
2290 }
2291 
2292 const MachineInstr *
2293 DwarfDebug::emitInitialLocDirective(const MachineFunction &MF, unsigned CUID) {
2294   // Don't deal with functions that have no instructions.
2295   if (llvm::all_of(MF, [](const MachineBasicBlock &MBB) { return MBB.empty(); }))
2296     return nullptr;
2297 
2298   std::pair<const MachineInstr *, bool> PrologEnd = findPrologueEndLoc(&MF);
2299   const MachineInstr *PrologEndLoc = PrologEnd.first;
2300   bool IsEmptyPrologue = PrologEnd.second;
2301 
2302   // If the prolog is empty, no need to generate scope line for the proc.
2303   if (IsEmptyPrologue) {
2304     // If there's nowhere to put a prologue_end flag, emit a scope line in case
2305     // there are simply no source locations anywhere in the function.
2306     if (PrologEndLoc) {
2307       // Avoid trying to assign prologue_end to a line-zero location.
2308       // Instructions with no DebugLoc at all are fine, they'll be given the
2309       // scope line nuumber.
2310       const DebugLoc &DL = PrologEndLoc->getDebugLoc();
2311       if (!DL || DL->getLine() != 0)
2312         return PrologEndLoc;
2313 
2314       // Later, don't place the prologue_end flag on this line-zero location.
2315       PrologEndLoc = nullptr;
2316     }
2317   }
2318 
2319   // Ensure the compile unit is created if the function is called before
2320   // beginFunction().
2321   DISubprogram *SP = MF.getFunction().getSubprogram();
2322   (void)getOrCreateDwarfCompileUnit(SP->getUnit());
2323   // We'd like to list the prologue as "not statements" but GDB behaves
2324   // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
2325   ::recordSourceLine(*Asm, SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT,
2326                      CUID, getDwarfVersion(), getUnits());
2327   return PrologEndLoc;
2328 }
2329 
2330 /// For the function \p MF, finds the set of instructions which may represent a
2331 /// change in line number from one or more of the preceding MBBs. Stores the
2332 /// resulting set of instructions, which should have is_stmt set, in
2333 /// ForceIsStmtInstrs.
2334 void DwarfDebug::findForceIsStmtInstrs(const MachineFunction *MF) {
2335   ForceIsStmtInstrs.clear();
2336 
2337   // For this function, we try to find MBBs where the last source line in every
2338   // block predecessor matches the first line seen in the block itself; for
2339   // every such MBB, we set is_stmt=false on the first line in the block, and
2340   // for every other block we set is_stmt=true on the first line.
2341   // For example, if we have the block %bb.3, which has 2 predecesors %bb.1 and
2342   // %bb.2:
2343   //   bb.1:
2344   //     $r3 = MOV64ri 12, debug-location !DILocation(line: 4)
2345   //     JMP %bb.3, debug-location !DILocation(line: 5)
2346   //   bb.2:
2347   //     $r3 = MOV64ri 24, debug-location !DILocation(line: 5)
2348   //     JMP %bb.3
2349   //   bb.3:
2350   //     $r2 = MOV64ri 1
2351   //     $r1 = ADD $r2, $r3, debug-location !DILocation(line: 5)
2352   // When we examine %bb.3, we first check to see if it contains any
2353   // instructions with debug locations, and select the first such instruction;
2354   // in this case, the ADD, with line=5. We then examine both of its
2355   // predecessors to see what the last debug-location in them is. For each
2356   // predecessor, if they do not contain any debug-locations, or if the last
2357   // debug-location before jumping to %bb.3 does not have line=5, then the ADD
2358   // in %bb.3 must use IsStmt. In this case, all predecessors have a
2359   // debug-location with line=5 as the last debug-location before jumping to
2360   // %bb.3, so we do not set is_stmt for the ADD instruction - we know that
2361   // whichever MBB we have arrived from, the line has not changed.
2362 
2363   const auto *TII = MF->getSubtarget().getInstrInfo();
2364 
2365   // We only need to the predecessors of MBBs that could have is_stmt set by
2366   // this logic.
2367   SmallDenseSet<MachineBasicBlock *, 4> PredMBBsToExamine;
2368   SmallDenseMap<MachineBasicBlock *, MachineInstr *> PotentialIsStmtMBBInstrs;
2369   // We use const_cast even though we won't actually modify MF, because some
2370   // methods we need take a non-const MBB.
2371   for (auto &MBB : *const_cast<MachineFunction *>(MF)) {
2372     if (MBB.empty() || MBB.pred_empty())
2373       continue;
2374     for (auto &MI : MBB) {
2375       if (MI.getDebugLoc() && MI.getDebugLoc()->getLine()) {
2376         for (auto *Pred : MBB.predecessors())
2377           PredMBBsToExamine.insert(Pred);
2378         PotentialIsStmtMBBInstrs.insert({&MBB, &MI});
2379         break;
2380       }
2381     }
2382   }
2383 
2384   // For each predecessor MBB, we examine the last line seen before each branch
2385   // or logical fallthrough. We use analyzeBranch to handle cases where
2386   // different branches have different outgoing lines (i.e. if there are
2387   // multiple branches that each have their own source location); otherwise we
2388   // just use the last line in the block.
2389   for (auto *MBB : PredMBBsToExamine) {
2390     auto CheckMBBEdge = [&](MachineBasicBlock *Succ, unsigned OutgoingLine) {
2391       auto MBBInstrIt = PotentialIsStmtMBBInstrs.find(Succ);
2392       if (MBBInstrIt == PotentialIsStmtMBBInstrs.end())
2393         return;
2394       MachineInstr *MI = MBBInstrIt->second;
2395       if (MI->getDebugLoc()->getLine() == OutgoingLine)
2396         return;
2397       PotentialIsStmtMBBInstrs.erase(MBBInstrIt);
2398       ForceIsStmtInstrs.insert(MI);
2399     };
2400     // If this block is empty, we conservatively assume that its fallthrough
2401     // successor needs is_stmt; we could check MBB's predecessors to see if it
2402     // has a consistent entry line, but this seems unlikely to be worthwhile.
2403     if (MBB->empty()) {
2404       for (auto *Succ : MBB->successors())
2405         CheckMBBEdge(Succ, 0);
2406       continue;
2407     }
2408     // If MBB has no successors that are in the "potential" set, due to one or
2409     // more of them having confirmed is_stmt, we can skip this check early.
2410     if (none_of(MBB->successors(), [&](auto *SuccMBB) {
2411           return PotentialIsStmtMBBInstrs.contains(SuccMBB);
2412         }))
2413       continue;
2414     // If we can't determine what DLs this branch's successors use, just treat
2415     // all the successors as coming from the last DebugLoc.
2416     SmallVector<MachineBasicBlock *, 2> SuccessorBBs;
2417     auto MIIt = MBB->rbegin();
2418     {
2419       MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
2420       SmallVector<MachineOperand, 4> Cond;
2421       bool AnalyzeFailed = TII->analyzeBranch(*MBB, TBB, FBB, Cond);
2422       // For a conditional branch followed by unconditional branch where the
2423       // unconditional branch has a DebugLoc, that loc is the outgoing loc to
2424       // the the false destination only; otherwise, both destinations share an
2425       // outgoing loc.
2426       if (!AnalyzeFailed && !Cond.empty() && FBB != nullptr &&
2427           MBB->back().getDebugLoc() && MBB->back().getDebugLoc()->getLine()) {
2428         unsigned FBBLine = MBB->back().getDebugLoc()->getLine();
2429         assert(MIIt->isBranch() && "Bad result from analyzeBranch?");
2430         CheckMBBEdge(FBB, FBBLine);
2431         ++MIIt;
2432         SuccessorBBs.push_back(TBB);
2433       } else {
2434         // For all other cases, all successors share the last outgoing DebugLoc.
2435         SuccessorBBs.assign(MBB->succ_begin(), MBB->succ_end());
2436       }
2437     }
2438 
2439     // If we don't find an outgoing loc, this block will start with a line 0.
2440     // It is possible that we have a block that has no DebugLoc, but acts as a
2441     // simple passthrough between two blocks that end and start with the same
2442     // line, e.g.:
2443     //   bb.1:
2444     //     JMP %bb.2, debug-location !10
2445     //   bb.2:
2446     //     JMP %bb.3
2447     //   bb.3:
2448     //     $r1 = ADD $r2, $r3, debug-location !10
2449     // If these blocks were merged into a single block, we would not attach
2450     // is_stmt to the ADD, but with this logic that only checks the immediate
2451     // predecessor, we will; we make this tradeoff because doing a full dataflow
2452     // analysis would be expensive, and these situations are probably not common
2453     // enough for this to be worthwhile.
2454     unsigned LastLine = 0;
2455     while (MIIt != MBB->rend()) {
2456       if (auto DL = MIIt->getDebugLoc(); DL && DL->getLine()) {
2457         LastLine = DL->getLine();
2458         break;
2459       }
2460       ++MIIt;
2461     }
2462     for (auto *Succ : SuccessorBBs)
2463       CheckMBBEdge(Succ, LastLine);
2464   }
2465 }
2466 
2467 // Gather pre-function debug information.  Assumes being called immediately
2468 // after the function entry point has been emitted.
2469 void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) {
2470   CurFn = MF;
2471 
2472   auto *SP = MF->getFunction().getSubprogram();
2473   assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode());
2474   if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
2475     return;
2476 
2477   DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
2478   FunctionLineTableLabel = CU.emitFuncLineTableOffsets()
2479                                ? Asm->OutStreamer->emitLineTableLabel()
2480                                : nullptr;
2481 
2482   Asm->OutStreamer->getContext().setDwarfCompileUnitID(
2483       getDwarfCompileUnitIDForLineTable(CU));
2484 
2485   // Record beginning of function.
2486   PrologEndLoc = emitInitialLocDirective(
2487       *MF, Asm->OutStreamer->getContext().getDwarfCompileUnitID());
2488 
2489   findForceIsStmtInstrs(MF);
2490 }
2491 
2492 unsigned
2493 DwarfDebug::getDwarfCompileUnitIDForLineTable(const DwarfCompileUnit &CU) {
2494   // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
2495   // belongs to so that we add to the correct per-cu line table in the
2496   // non-asm case.
2497   if (Asm->OutStreamer->hasRawTextSupport())
2498     // Use a single line table if we are generating assembly.
2499     return 0;
2500   else
2501     return CU.getUniqueID();
2502 }
2503 
2504 void DwarfDebug::terminateLineTable(const DwarfCompileUnit *CU) {
2505   const auto &CURanges = CU->getRanges();
2506   auto &LineTable = Asm->OutStreamer->getContext().getMCDwarfLineTable(
2507       getDwarfCompileUnitIDForLineTable(*CU));
2508   // Add the last range label for the given CU.
2509   LineTable.getMCLineSections().addEndEntry(
2510       const_cast<MCSymbol *>(CURanges.back().End));
2511 }
2512 
2513 void DwarfDebug::skippedNonDebugFunction() {
2514   // If we don't have a subprogram for this function then there will be a hole
2515   // in the range information. Keep note of this by setting the previously used
2516   // section to nullptr.
2517   // Terminate the pending line table.
2518   if (PrevCU)
2519     terminateLineTable(PrevCU);
2520   PrevCU = nullptr;
2521   CurFn = nullptr;
2522 }
2523 
2524 // Gather and emit post-function debug information.
2525 void DwarfDebug::endFunctionImpl(const MachineFunction *MF) {
2526   const DISubprogram *SP = MF->getFunction().getSubprogram();
2527 
2528   assert(CurFn == MF &&
2529       "endFunction should be called with the same function as beginFunction");
2530 
2531   // Set DwarfDwarfCompileUnitID in MCContext to default value.
2532   Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
2533 
2534   LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
2535   assert(!FnScope || SP == FnScope->getScopeNode());
2536   DwarfCompileUnit &TheCU = getOrCreateDwarfCompileUnit(SP->getUnit());
2537   if (TheCU.getCUNode()->isDebugDirectivesOnly()) {
2538     PrevLabel = nullptr;
2539     CurFn = nullptr;
2540     return;
2541   }
2542 
2543   DenseSet<InlinedEntity> Processed;
2544   collectEntityInfo(TheCU, SP, Processed);
2545 
2546   // Add the range of this function to the list of ranges for the CU.
2547   // With basic block sections, add ranges for all basic block sections.
2548   for (const auto &R : Asm->MBBSectionRanges)
2549     TheCU.addRange({R.second.BeginLabel, R.second.EndLabel});
2550 
2551   // Under -gmlt, skip building the subprogram if there are no inlined
2552   // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram
2553   // is still needed as we need its source location.
2554   if (!TheCU.getCUNode()->getDebugInfoForProfiling() &&
2555       TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly &&
2556       LScopes.getAbstractScopesList().empty() && !IsDarwin) {
2557     for (const auto &R : Asm->MBBSectionRanges)
2558       addArangeLabel(SymbolCU(&TheCU, R.second.BeginLabel));
2559 
2560     assert(InfoHolder.getScopeVariables().empty());
2561     PrevLabel = nullptr;
2562     CurFn = nullptr;
2563     return;
2564   }
2565 
2566 #ifndef NDEBUG
2567   size_t NumAbstractSubprograms = LScopes.getAbstractScopesList().size();
2568 #endif
2569   for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
2570     const auto *SP = cast<DISubprogram>(AScope->getScopeNode());
2571     for (const DINode *DN : SP->getRetainedNodes()) {
2572       const auto *LS = getRetainedNodeScope(DN);
2573       // Ensure LexicalScope is created for the scope of this node.
2574       auto *LexS = LScopes.getOrCreateAbstractScope(LS);
2575       assert(LexS && "Expected the LexicalScope to be created.");
2576       if (isa<DILocalVariable>(DN) || isa<DILabel>(DN)) {
2577         // Collect info for variables/labels that were optimized out.
2578         if (!Processed.insert(InlinedEntity(DN, nullptr)).second ||
2579             TheCU.getExistingAbstractEntity(DN))
2580           continue;
2581         TheCU.createAbstractEntity(DN, LexS);
2582       } else {
2583         // Remember the node if this is a local declarations.
2584         LocalDeclsPerLS[LS].insert(DN);
2585       }
2586       assert(
2587           LScopes.getAbstractScopesList().size() == NumAbstractSubprograms &&
2588           "getOrCreateAbstractScope() inserted an abstract subprogram scope");
2589     }
2590     constructAbstractSubprogramScopeDIE(TheCU, AScope);
2591   }
2592 
2593   ProcessedSPNodes.insert(SP);
2594   DIE &ScopeDIE =
2595       TheCU.constructSubprogramScopeDIE(SP, FnScope, FunctionLineTableLabel);
2596   if (auto *SkelCU = TheCU.getSkeleton())
2597     if (!LScopes.getAbstractScopesList().empty() &&
2598         TheCU.getCUNode()->getSplitDebugInlining())
2599       SkelCU->constructSubprogramScopeDIE(SP, FnScope, FunctionLineTableLabel);
2600 
2601   FunctionLineTableLabel = nullptr;
2602 
2603   // Construct call site entries.
2604   constructCallSiteEntryDIEs(*SP, TheCU, ScopeDIE, *MF);
2605 
2606   // Clear debug info
2607   // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
2608   // DbgVariables except those that are also in AbstractVariables (since they
2609   // can be used cross-function)
2610   InfoHolder.getScopeVariables().clear();
2611   InfoHolder.getScopeLabels().clear();
2612   LocalDeclsPerLS.clear();
2613   PrevLabel = nullptr;
2614   CurFn = nullptr;
2615 }
2616 
2617 // Register a source line with debug info. Returns the  unique label that was
2618 // emitted and which provides correspondence to the source line list.
2619 void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
2620                                   unsigned Flags) {
2621   ::recordSourceLine(*Asm, Line, Col, S, Flags,
2622                      Asm->OutStreamer->getContext().getDwarfCompileUnitID(),
2623                      getDwarfVersion(), getUnits());
2624 }
2625 
2626 //===----------------------------------------------------------------------===//
2627 // Emit Methods
2628 //===----------------------------------------------------------------------===//
2629 
2630 // Emit the debug info section.
2631 void DwarfDebug::emitDebugInfo() {
2632   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2633   Holder.emitUnits(/* UseOffsets */ false);
2634 }
2635 
2636 // Emit the abbreviation section.
2637 void DwarfDebug::emitAbbreviations() {
2638   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2639 
2640   Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
2641 }
2642 
2643 void DwarfDebug::emitStringOffsetsTableHeader() {
2644   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2645   Holder.getStringPool().emitStringOffsetsTableHeader(
2646       *Asm, Asm->getObjFileLowering().getDwarfStrOffSection(),
2647       Holder.getStringOffsetsStartSym());
2648 }
2649 
2650 template <typename AccelTableT>
2651 void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section,
2652                            StringRef TableName) {
2653   Asm->OutStreamer->switchSection(Section);
2654 
2655   // Emit the full data.
2656   emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol());
2657 }
2658 
2659 void DwarfDebug::emitAccelDebugNames() {
2660   // Don't emit anything if we have no compilation units to index.
2661   if (getUnits().empty())
2662     return;
2663 
2664   emitDWARF5AccelTable(Asm, AccelDebugNames, *this, getUnits());
2665 }
2666 
2667 // Emit visible names into a hashed accelerator table section.
2668 void DwarfDebug::emitAccelNames() {
2669   emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
2670             "Names");
2671 }
2672 
2673 // Emit objective C classes and categories into a hashed accelerator table
2674 // section.
2675 void DwarfDebug::emitAccelObjC() {
2676   emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
2677             "ObjC");
2678 }
2679 
2680 // Emit namespace dies into a hashed accelerator table.
2681 void DwarfDebug::emitAccelNamespaces() {
2682   emitAccel(AccelNamespace,
2683             Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
2684             "namespac");
2685 }
2686 
2687 // Emit type dies into a hashed accelerator table.
2688 void DwarfDebug::emitAccelTypes() {
2689   emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
2690             "types");
2691 }
2692 
2693 // Public name handling.
2694 // The format for the various pubnames:
2695 //
2696 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2697 // for the DIE that is named.
2698 //
2699 // gnu pubnames - offset/index value/name tuples where the offset is the offset
2700 // into the CU and the index value is computed according to the type of value
2701 // for the DIE that is named.
2702 //
2703 // For type units the offset is the offset of the skeleton DIE. For split dwarf
2704 // it's the offset within the debug_info/debug_types dwo section, however, the
2705 // reference in the pubname header doesn't change.
2706 
2707 /// computeIndexValue - Compute the gdb index value for the DIE and CU.
2708 static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
2709                                                         const DIE *Die) {
2710   // Entities that ended up only in a Type Unit reference the CU instead (since
2711   // the pub entry has offsets within the CU there's no real offset that can be
2712   // provided anyway). As it happens all such entities (namespaces and types,
2713   // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out
2714   // not to be true it would be necessary to persist this information from the
2715   // point at which the entry is added to the index data structure - since by
2716   // the time the index is built from that, the original type/namespace DIE in a
2717   // type unit has already been destroyed so it can't be queried for properties
2718   // like tag, etc.
2719   if (Die->getTag() == dwarf::DW_TAG_compile_unit)
2720     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE,
2721                                           dwarf::GIEL_EXTERNAL);
2722   dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
2723 
2724   // We could have a specification DIE that has our most of our knowledge,
2725   // look for that now.
2726   if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
2727     DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
2728     if (SpecDIE.findAttribute(dwarf::DW_AT_external))
2729       Linkage = dwarf::GIEL_EXTERNAL;
2730   } else if (Die->findAttribute(dwarf::DW_AT_external))
2731     Linkage = dwarf::GIEL_EXTERNAL;
2732 
2733   switch (Die->getTag()) {
2734   case dwarf::DW_TAG_class_type:
2735   case dwarf::DW_TAG_structure_type:
2736   case dwarf::DW_TAG_union_type:
2737   case dwarf::DW_TAG_enumeration_type:
2738     return dwarf::PubIndexEntryDescriptor(
2739         dwarf::GIEK_TYPE,
2740         dwarf::isCPlusPlus((dwarf::SourceLanguage)CU->getLanguage())
2741             ? dwarf::GIEL_EXTERNAL
2742             : dwarf::GIEL_STATIC);
2743   case dwarf::DW_TAG_typedef:
2744   case dwarf::DW_TAG_base_type:
2745   case dwarf::DW_TAG_subrange_type:
2746   case dwarf::DW_TAG_template_alias:
2747     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
2748   case dwarf::DW_TAG_namespace:
2749     return dwarf::GIEK_TYPE;
2750   case dwarf::DW_TAG_subprogram:
2751     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
2752   case dwarf::DW_TAG_variable:
2753     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
2754   case dwarf::DW_TAG_enumerator:
2755     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2756                                           dwarf::GIEL_STATIC);
2757   default:
2758     return dwarf::GIEK_NONE;
2759   }
2760 }
2761 
2762 /// emitDebugPubSections - Emit visible names and types into debug pubnames and
2763 /// pubtypes sections.
2764 void DwarfDebug::emitDebugPubSections() {
2765   for (const auto &NU : CUMap) {
2766     DwarfCompileUnit *TheU = NU.second;
2767     if (!TheU->hasDwarfPubSections())
2768       continue;
2769 
2770     bool GnuStyle = TheU->getCUNode()->getNameTableKind() ==
2771                     DICompileUnit::DebugNameTableKind::GNU;
2772 
2773     Asm->OutStreamer->switchSection(
2774         GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2775                  : Asm->getObjFileLowering().getDwarfPubNamesSection());
2776     emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames());
2777 
2778     Asm->OutStreamer->switchSection(
2779         GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
2780                  : Asm->getObjFileLowering().getDwarfPubTypesSection());
2781     emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes());
2782   }
2783 }
2784 
2785 void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) {
2786   if (useSectionsAsReferences())
2787     Asm->emitDwarfOffset(CU.getSection()->getBeginSymbol(),
2788                          CU.getDebugSectionOffset());
2789   else
2790     Asm->emitDwarfSymbolReference(CU.getLabelBegin());
2791 }
2792 
2793 void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name,
2794                                      DwarfCompileUnit *TheU,
2795                                      const StringMap<const DIE *> &Globals) {
2796   if (auto *Skeleton = TheU->getSkeleton())
2797     TheU = Skeleton;
2798 
2799   // Emit the header.
2800   MCSymbol *EndLabel = Asm->emitDwarfUnitLength(
2801       "pub" + Name, "Length of Public " + Name + " Info");
2802 
2803   Asm->OutStreamer->AddComment("DWARF Version");
2804   Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION);
2805 
2806   Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
2807   emitSectionReference(*TheU);
2808 
2809   Asm->OutStreamer->AddComment("Compilation Unit Length");
2810   Asm->emitDwarfLengthOrOffset(TheU->getLength());
2811 
2812   // Emit the pubnames for this compilation unit.
2813   SmallVector<std::pair<StringRef, const DIE *>, 0> Vec;
2814   for (const auto &GI : Globals)
2815     Vec.emplace_back(GI.first(), GI.second);
2816   llvm::sort(Vec, [](auto &A, auto &B) {
2817     return A.second->getOffset() < B.second->getOffset();
2818   });
2819   for (const auto &[Name, Entity] : Vec) {
2820     Asm->OutStreamer->AddComment("DIE offset");
2821     Asm->emitDwarfLengthOrOffset(Entity->getOffset());
2822 
2823     if (GnuStyle) {
2824       dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
2825       Asm->OutStreamer->AddComment(
2826           Twine("Attributes: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) +
2827           ", " + dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
2828       Asm->emitInt8(Desc.toBits());
2829     }
2830 
2831     Asm->OutStreamer->AddComment("External Name");
2832     Asm->OutStreamer->emitBytes(StringRef(Name.data(), Name.size() + 1));
2833   }
2834 
2835   Asm->OutStreamer->AddComment("End Mark");
2836   Asm->emitDwarfLengthOrOffset(0);
2837   Asm->OutStreamer->emitLabel(EndLabel);
2838 }
2839 
2840 /// Emit null-terminated strings into a debug str section.
2841 void DwarfDebug::emitDebugStr() {
2842   MCSection *StringOffsetsSection = nullptr;
2843   if (useSegmentedStringOffsetsTable()) {
2844     emitStringOffsetsTableHeader();
2845     StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection();
2846   }
2847   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2848   Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection(),
2849                      StringOffsetsSection, /* UseRelativeOffsets = */ true);
2850 }
2851 
2852 void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
2853                                    const DebugLocStream::Entry &Entry,
2854                                    const DwarfCompileUnit *CU) {
2855   auto &&Comments = DebugLocs.getComments(Entry);
2856   auto Comment = Comments.begin();
2857   auto End = Comments.end();
2858 
2859   // The expressions are inserted into a byte stream rather early (see
2860   // DwarfExpression::addExpression) so for those ops (e.g. DW_OP_convert) that
2861   // need to reference a base_type DIE the offset of that DIE is not yet known.
2862   // To deal with this we instead insert a placeholder early and then extract
2863   // it here and replace it with the real reference.
2864   unsigned PtrSize = Asm->MAI->getCodePointerSize();
2865   DWARFDataExtractor Data(StringRef(DebugLocs.getBytes(Entry).data(),
2866                                     DebugLocs.getBytes(Entry).size()),
2867                           Asm->getDataLayout().isLittleEndian(), PtrSize);
2868   DWARFExpression Expr(Data, PtrSize, Asm->OutContext.getDwarfFormat());
2869 
2870   using Encoding = DWARFExpression::Operation::Encoding;
2871   uint64_t Offset = 0;
2872   for (const auto &Op : Expr) {
2873     assert(Op.getCode() != dwarf::DW_OP_const_type &&
2874            "3 operand ops not yet supported");
2875     assert(!Op.getSubCode() && "SubOps not yet supported");
2876     Streamer.emitInt8(Op.getCode(), Comment != End ? *(Comment++) : "");
2877     Offset++;
2878     for (unsigned I = 0; I < Op.getDescription().Op.size(); ++I) {
2879       if (Op.getDescription().Op[I] == Encoding::BaseTypeRef) {
2880         unsigned Length =
2881           Streamer.emitDIERef(*CU->ExprRefedBaseTypes[Op.getRawOperand(I)].Die);
2882         // Make sure comments stay aligned.
2883         for (unsigned J = 0; J < Length; ++J)
2884           if (Comment != End)
2885             Comment++;
2886       } else {
2887         for (uint64_t J = Offset; J < Op.getOperandEndOffset(I); ++J)
2888           Streamer.emitInt8(Data.getData()[J], Comment != End ? *(Comment++) : "");
2889       }
2890       Offset = Op.getOperandEndOffset(I);
2891     }
2892     assert(Offset == Op.getEndOffset());
2893   }
2894 }
2895 
2896 void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
2897                                    const DbgValueLoc &Value,
2898                                    DwarfExpression &DwarfExpr) {
2899   auto *DIExpr = Value.getExpression();
2900   DIExpressionCursor ExprCursor(DIExpr);
2901   DwarfExpr.addFragmentOffset(DIExpr);
2902 
2903   // If the DIExpr is an Entry Value, we want to follow the same code path
2904   // regardless of whether the DBG_VALUE is variadic or not.
2905   if (DIExpr && DIExpr->isEntryValue()) {
2906     // Entry values can only be a single register with no additional DIExpr,
2907     // so just add it directly.
2908     assert(Value.getLocEntries().size() == 1);
2909     assert(Value.getLocEntries()[0].isLocation());
2910     MachineLocation Location = Value.getLocEntries()[0].getLoc();
2911     DwarfExpr.setLocation(Location, DIExpr);
2912 
2913     DwarfExpr.beginEntryValueExpression(ExprCursor);
2914 
2915     const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
2916     if (!DwarfExpr.addMachineRegExpression(TRI, ExprCursor, Location.getReg()))
2917       return;
2918     return DwarfExpr.addExpression(std::move(ExprCursor));
2919   }
2920 
2921   // Regular entry.
2922   auto EmitValueLocEntry = [&DwarfExpr, &BT,
2923                             &AP](const DbgValueLocEntry &Entry,
2924                                  DIExpressionCursor &Cursor) -> bool {
2925     if (Entry.isInt()) {
2926       if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
2927                  BT->getEncoding() == dwarf::DW_ATE_signed_char))
2928         DwarfExpr.addSignedConstant(Entry.getInt());
2929       else
2930         DwarfExpr.addUnsignedConstant(Entry.getInt());
2931     } else if (Entry.isLocation()) {
2932       MachineLocation Location = Entry.getLoc();
2933       if (Location.isIndirect())
2934         DwarfExpr.setMemoryLocationKind();
2935 
2936       const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
2937       if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
2938         return false;
2939     } else if (Entry.isTargetIndexLocation()) {
2940       TargetIndexLocation Loc = Entry.getTargetIndexLocation();
2941       // TODO TargetIndexLocation is a target-independent. Currently only the
2942       // WebAssembly-specific encoding is supported.
2943       assert(AP.TM.getTargetTriple().isWasm());
2944       DwarfExpr.addWasmLocation(Loc.Index, static_cast<uint64_t>(Loc.Offset));
2945     } else if (Entry.isConstantFP()) {
2946       if (AP.getDwarfVersion() >= 4 && !AP.getDwarfDebug()->tuneForSCE() &&
2947           !Cursor) {
2948         DwarfExpr.addConstantFP(Entry.getConstantFP()->getValueAPF(), AP);
2949       } else if (Entry.getConstantFP()
2950                      ->getValueAPF()
2951                      .bitcastToAPInt()
2952                      .getBitWidth() <= 64 /*bits*/) {
2953         DwarfExpr.addUnsignedConstant(
2954             Entry.getConstantFP()->getValueAPF().bitcastToAPInt());
2955       } else {
2956         LLVM_DEBUG(
2957             dbgs() << "Skipped DwarfExpression creation for ConstantFP of size"
2958                    << Entry.getConstantFP()
2959                           ->getValueAPF()
2960                           .bitcastToAPInt()
2961                           .getBitWidth()
2962                    << " bits\n");
2963         return false;
2964       }
2965     }
2966     return true;
2967   };
2968 
2969   if (!Value.isVariadic()) {
2970     if (!EmitValueLocEntry(Value.getLocEntries()[0], ExprCursor))
2971       return;
2972     DwarfExpr.addExpression(std::move(ExprCursor));
2973     return;
2974   }
2975 
2976   // If any of the location entries are registers with the value 0, then the
2977   // location is undefined.
2978   if (any_of(Value.getLocEntries(), [](const DbgValueLocEntry &Entry) {
2979         return Entry.isLocation() && !Entry.getLoc().getReg();
2980       }))
2981     return;
2982 
2983   DwarfExpr.addExpression(
2984       std::move(ExprCursor),
2985       [EmitValueLocEntry, &Value](unsigned Idx,
2986                                   DIExpressionCursor &Cursor) -> bool {
2987         return EmitValueLocEntry(Value.getLocEntries()[Idx], Cursor);
2988       });
2989 }
2990 
2991 void DebugLocEntry::finalize(const AsmPrinter &AP,
2992                              DebugLocStream::ListBuilder &List,
2993                              const DIBasicType *BT,
2994                              DwarfCompileUnit &TheCU) {
2995   assert(!Values.empty() &&
2996          "location list entries without values are redundant");
2997   assert(Begin != End && "unexpected location list entry with empty range");
2998   DebugLocStream::EntryBuilder Entry(List, Begin, End);
2999   BufferByteStreamer Streamer = Entry.getStreamer();
3000   DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer, TheCU);
3001   const DbgValueLoc &Value = Values[0];
3002   if (Value.isFragment()) {
3003     // Emit all fragments that belong to the same variable and range.
3004     assert(llvm::all_of(Values, [](DbgValueLoc P) {
3005           return P.isFragment();
3006         }) && "all values are expected to be fragments");
3007     assert(llvm::is_sorted(Values) && "fragments are expected to be sorted");
3008 
3009     for (const auto &Fragment : Values)
3010       DwarfDebug::emitDebugLocValue(AP, BT, Fragment, DwarfExpr);
3011 
3012   } else {
3013     assert(Values.size() == 1 && "only fragments may have >1 value");
3014     DwarfDebug::emitDebugLocValue(AP, BT, Value, DwarfExpr);
3015   }
3016   DwarfExpr.finalize();
3017   if (DwarfExpr.TagOffset)
3018     List.setTagOffset(*DwarfExpr.TagOffset);
3019 }
3020 
3021 void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry,
3022                                            const DwarfCompileUnit *CU) {
3023   // Emit the size.
3024   Asm->OutStreamer->AddComment("Loc expr size");
3025   if (getDwarfVersion() >= 5)
3026     Asm->emitULEB128(DebugLocs.getBytes(Entry).size());
3027   else if (DebugLocs.getBytes(Entry).size() <= std::numeric_limits<uint16_t>::max())
3028     Asm->emitInt16(DebugLocs.getBytes(Entry).size());
3029   else {
3030     // The entry is too big to fit into 16 bit, drop it as there is nothing we
3031     // can do.
3032     Asm->emitInt16(0);
3033     return;
3034   }
3035   // Emit the entry.
3036   APByteStreamer Streamer(*Asm);
3037   emitDebugLocEntry(Streamer, Entry, CU);
3038 }
3039 
3040 // Emit the header of a DWARF 5 range list table list table. Returns the symbol
3041 // that designates the end of the table for the caller to emit when the table is
3042 // complete.
3043 static MCSymbol *emitRnglistsTableHeader(AsmPrinter *Asm,
3044                                          const DwarfFile &Holder) {
3045   MCSymbol *TableEnd = mcdwarf::emitListsTableHeaderStart(*Asm->OutStreamer);
3046 
3047   Asm->OutStreamer->AddComment("Offset entry count");
3048   Asm->emitInt32(Holder.getRangeLists().size());
3049   Asm->OutStreamer->emitLabel(Holder.getRnglistsTableBaseSym());
3050 
3051   for (const RangeSpanList &List : Holder.getRangeLists())
3052     Asm->emitLabelDifference(List.Label, Holder.getRnglistsTableBaseSym(),
3053                              Asm->getDwarfOffsetByteSize());
3054 
3055   return TableEnd;
3056 }
3057 
3058 // Emit the header of a DWARF 5 locations list table. Returns the symbol that
3059 // designates the end of the table for the caller to emit when the table is
3060 // complete.
3061 static MCSymbol *emitLoclistsTableHeader(AsmPrinter *Asm,
3062                                          const DwarfDebug &DD) {
3063   MCSymbol *TableEnd = mcdwarf::emitListsTableHeaderStart(*Asm->OutStreamer);
3064 
3065   const auto &DebugLocs = DD.getDebugLocs();
3066 
3067   Asm->OutStreamer->AddComment("Offset entry count");
3068   Asm->emitInt32(DebugLocs.getLists().size());
3069   Asm->OutStreamer->emitLabel(DebugLocs.getSym());
3070 
3071   for (const auto &List : DebugLocs.getLists())
3072     Asm->emitLabelDifference(List.Label, DebugLocs.getSym(),
3073                              Asm->getDwarfOffsetByteSize());
3074 
3075   return TableEnd;
3076 }
3077 
3078 template <typename Ranges, typename PayloadEmitter>
3079 static void emitRangeList(
3080     DwarfDebug &DD, AsmPrinter *Asm, MCSymbol *Sym, const Ranges &R,
3081     const DwarfCompileUnit &CU, unsigned BaseAddressx, unsigned OffsetPair,
3082     unsigned StartxLength, unsigned EndOfList,
3083     StringRef (*StringifyEnum)(unsigned),
3084     bool ShouldUseBaseAddress,
3085     PayloadEmitter EmitPayload) {
3086 
3087   auto Size = Asm->MAI->getCodePointerSize();
3088   bool UseDwarf5 = DD.getDwarfVersion() >= 5;
3089 
3090   // Emit our symbol so we can find the beginning of the range.
3091   Asm->OutStreamer->emitLabel(Sym);
3092 
3093   // Gather all the ranges that apply to the same section so they can share
3094   // a base address entry.
3095   SmallMapVector<const MCSection *, std::vector<decltype(&*R.begin())>, 16>
3096       SectionRanges;
3097 
3098   for (const auto &Range : R)
3099     SectionRanges[&Range.Begin->getSection()].push_back(&Range);
3100 
3101   const MCSymbol *CUBase = CU.getBaseAddress();
3102   bool BaseIsSet = false;
3103   for (const auto &P : SectionRanges) {
3104     auto *Base = CUBase;
3105     if ((Asm->TM.getTargetTriple().isNVPTX() && DD.tuneForGDB())) {
3106       // PTX does not support subtracting labels from the code section in the
3107       // debug_loc section.  To work around this, the NVPTX backend needs the
3108       // compile unit to have no low_pc in order to have a zero base_address
3109       // when handling debug_loc in cuda-gdb.  Additionally, cuda-gdb doesn't
3110       // seem to handle setting a per-variable base to zero.  To make cuda-gdb
3111       // happy, just emit labels with no base while having no compile unit
3112       // low_pc.
3113       BaseIsSet = false;
3114       Base = nullptr;
3115     } else if (!Base && ShouldUseBaseAddress) {
3116       const MCSymbol *Begin = P.second.front()->Begin;
3117       const MCSymbol *NewBase = DD.getSectionLabel(&Begin->getSection());
3118       if (!UseDwarf5) {
3119         Base = NewBase;
3120         BaseIsSet = true;
3121         Asm->OutStreamer->emitIntValue(-1, Size);
3122         Asm->OutStreamer->AddComment("  base address");
3123         Asm->OutStreamer->emitSymbolValue(Base, Size);
3124       } else if (NewBase != Begin || P.second.size() > 1) {
3125         // Only use a base address if
3126         //  * the existing pool address doesn't match (NewBase != Begin)
3127         //  * or, there's more than one entry to share the base address
3128         Base = NewBase;
3129         BaseIsSet = true;
3130         Asm->OutStreamer->AddComment(StringifyEnum(BaseAddressx));
3131         Asm->emitInt8(BaseAddressx);
3132         Asm->OutStreamer->AddComment("  base address index");
3133         Asm->emitULEB128(DD.getAddressPool().getIndex(Base));
3134       }
3135     } else if (BaseIsSet && !UseDwarf5) {
3136       BaseIsSet = false;
3137       assert(!Base);
3138       Asm->OutStreamer->emitIntValue(-1, Size);
3139       Asm->OutStreamer->emitIntValue(0, Size);
3140     }
3141 
3142     for (const auto *RS : P.second) {
3143       const MCSymbol *Begin = RS->Begin;
3144       const MCSymbol *End = RS->End;
3145       assert(Begin && "Range without a begin symbol?");
3146       assert(End && "Range without an end symbol?");
3147       if (Base) {
3148         if (UseDwarf5) {
3149           // Emit offset_pair when we have a base.
3150           Asm->OutStreamer->AddComment(StringifyEnum(OffsetPair));
3151           Asm->emitInt8(OffsetPair);
3152           Asm->OutStreamer->AddComment("  starting offset");
3153           Asm->emitLabelDifferenceAsULEB128(Begin, Base);
3154           Asm->OutStreamer->AddComment("  ending offset");
3155           Asm->emitLabelDifferenceAsULEB128(End, Base);
3156         } else {
3157           Asm->emitLabelDifference(Begin, Base, Size);
3158           Asm->emitLabelDifference(End, Base, Size);
3159         }
3160       } else if (UseDwarf5) {
3161         Asm->OutStreamer->AddComment(StringifyEnum(StartxLength));
3162         Asm->emitInt8(StartxLength);
3163         Asm->OutStreamer->AddComment("  start index");
3164         Asm->emitULEB128(DD.getAddressPool().getIndex(Begin));
3165         Asm->OutStreamer->AddComment("  length");
3166         Asm->emitLabelDifferenceAsULEB128(End, Begin);
3167       } else {
3168         Asm->OutStreamer->emitSymbolValue(Begin, Size);
3169         Asm->OutStreamer->emitSymbolValue(End, Size);
3170       }
3171       EmitPayload(*RS);
3172     }
3173   }
3174 
3175   if (UseDwarf5) {
3176     Asm->OutStreamer->AddComment(StringifyEnum(EndOfList));
3177     Asm->emitInt8(EndOfList);
3178   } else {
3179     // Terminate the list with two 0 values.
3180     Asm->OutStreamer->emitIntValue(0, Size);
3181     Asm->OutStreamer->emitIntValue(0, Size);
3182   }
3183 }
3184 
3185 // Handles emission of both debug_loclist / debug_loclist.dwo
3186 static void emitLocList(DwarfDebug &DD, AsmPrinter *Asm, const DebugLocStream::List &List) {
3187   emitRangeList(DD, Asm, List.Label, DD.getDebugLocs().getEntries(List),
3188                 *List.CU, dwarf::DW_LLE_base_addressx,
3189                 dwarf::DW_LLE_offset_pair, dwarf::DW_LLE_startx_length,
3190                 dwarf::DW_LLE_end_of_list, llvm::dwarf::LocListEncodingString,
3191                 /* ShouldUseBaseAddress */ true,
3192                 [&](const DebugLocStream::Entry &E) {
3193                   DD.emitDebugLocEntryLocation(E, List.CU);
3194                 });
3195 }
3196 
3197 void DwarfDebug::emitDebugLocImpl(MCSection *Sec) {
3198   if (DebugLocs.getLists().empty())
3199     return;
3200 
3201   Asm->OutStreamer->switchSection(Sec);
3202 
3203   MCSymbol *TableEnd = nullptr;
3204   if (getDwarfVersion() >= 5)
3205     TableEnd = emitLoclistsTableHeader(Asm, *this);
3206 
3207   for (const auto &List : DebugLocs.getLists())
3208     emitLocList(*this, Asm, List);
3209 
3210   if (TableEnd)
3211     Asm->OutStreamer->emitLabel(TableEnd);
3212 }
3213 
3214 // Emit locations into the .debug_loc/.debug_loclists section.
3215 void DwarfDebug::emitDebugLoc() {
3216   emitDebugLocImpl(
3217       getDwarfVersion() >= 5
3218           ? Asm->getObjFileLowering().getDwarfLoclistsSection()
3219           : Asm->getObjFileLowering().getDwarfLocSection());
3220 }
3221 
3222 // Emit locations into the .debug_loc.dwo/.debug_loclists.dwo section.
3223 void DwarfDebug::emitDebugLocDWO() {
3224   if (getDwarfVersion() >= 5) {
3225     emitDebugLocImpl(
3226         Asm->getObjFileLowering().getDwarfLoclistsDWOSection());
3227 
3228     return;
3229   }
3230 
3231   for (const auto &List : DebugLocs.getLists()) {
3232     Asm->OutStreamer->switchSection(
3233         Asm->getObjFileLowering().getDwarfLocDWOSection());
3234     Asm->OutStreamer->emitLabel(List.Label);
3235 
3236     for (const auto &Entry : DebugLocs.getEntries(List)) {
3237       // GDB only supports startx_length in pre-standard split-DWARF.
3238       // (in v5 standard loclists, it currently* /only/ supports base_address +
3239       // offset_pair, so the implementations can't really share much since they
3240       // need to use different representations)
3241       // * as of October 2018, at least
3242       //
3243       // In v5 (see emitLocList), this uses SectionLabels to reuse existing
3244       // addresses in the address pool to minimize object size/relocations.
3245       Asm->emitInt8(dwarf::DW_LLE_startx_length);
3246       unsigned idx = AddrPool.getIndex(Entry.Begin);
3247       Asm->emitULEB128(idx);
3248       // Also the pre-standard encoding is slightly different, emitting this as
3249       // an address-length entry here, but its a ULEB128 in DWARFv5 loclists.
3250       Asm->emitLabelDifference(Entry.End, Entry.Begin, 4);
3251       emitDebugLocEntryLocation(Entry, List.CU);
3252     }
3253     Asm->emitInt8(dwarf::DW_LLE_end_of_list);
3254   }
3255 }
3256 
3257 struct ArangeSpan {
3258   const MCSymbol *Start, *End;
3259 };
3260 
3261 // Emit a debug aranges section, containing a CU lookup for any
3262 // address we can tie back to a CU.
3263 void DwarfDebug::emitDebugARanges() {
3264   if (ArangeLabels.empty())
3265     return;
3266 
3267   // Provides a unique id per text section.
3268   MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
3269 
3270   // Filter labels by section.
3271   for (const SymbolCU &SCU : ArangeLabels) {
3272     if (SCU.Sym->isInSection()) {
3273       // Make a note of this symbol and it's section.
3274       MCSection *Section = &SCU.Sym->getSection();
3275       SectionMap[Section].push_back(SCU);
3276     } else {
3277       // Some symbols (e.g. common/bss on mach-o) can have no section but still
3278       // appear in the output. This sucks as we rely on sections to build
3279       // arange spans. We can do it without, but it's icky.
3280       SectionMap[nullptr].push_back(SCU);
3281     }
3282   }
3283 
3284   DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
3285 
3286   for (auto &I : SectionMap) {
3287     MCSection *Section = I.first;
3288     SmallVector<SymbolCU, 8> &List = I.second;
3289     assert(!List.empty());
3290 
3291     // If we have no section (e.g. common), just write out
3292     // individual spans for each symbol.
3293     if (!Section) {
3294       for (const SymbolCU &Cur : List) {
3295         ArangeSpan Span;
3296         Span.Start = Cur.Sym;
3297         Span.End = nullptr;
3298         assert(Cur.CU);
3299         Spans[Cur.CU].push_back(Span);
3300       }
3301       continue;
3302     }
3303 
3304     // Insert a final terminator.
3305     List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section)));
3306 
3307     // Build spans between each label.
3308     const MCSymbol *StartSym = List[0].Sym;
3309     for (size_t n = 1, e = List.size(); n < e; n++) {
3310       const SymbolCU &Prev = List[n - 1];
3311       const SymbolCU &Cur = List[n];
3312 
3313       // Try and build the longest span we can within the same CU.
3314       if (Cur.CU != Prev.CU) {
3315         ArangeSpan Span;
3316         Span.Start = StartSym;
3317         Span.End = Cur.Sym;
3318         assert(Prev.CU);
3319         Spans[Prev.CU].push_back(Span);
3320         StartSym = Cur.Sym;
3321       }
3322     }
3323   }
3324 
3325   // Start the dwarf aranges section.
3326   Asm->OutStreamer->switchSection(
3327       Asm->getObjFileLowering().getDwarfARangesSection());
3328 
3329   unsigned PtrSize = Asm->MAI->getCodePointerSize();
3330 
3331   // Build a list of CUs used.
3332   std::vector<DwarfCompileUnit *> CUs;
3333   for (const auto &it : Spans) {
3334     DwarfCompileUnit *CU = it.first;
3335     CUs.push_back(CU);
3336   }
3337 
3338   // Sort the CU list (again, to ensure consistent output order).
3339   llvm::sort(CUs, [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) {
3340     return A->getUniqueID() < B->getUniqueID();
3341   });
3342 
3343   // Emit an arange table for each CU we used.
3344   for (DwarfCompileUnit *CU : CUs) {
3345     std::vector<ArangeSpan> &List = Spans[CU];
3346 
3347     // Describe the skeleton CU's offset and length, not the dwo file's.
3348     if (auto *Skel = CU->getSkeleton())
3349       CU = Skel;
3350 
3351     // Emit size of content not including length itself.
3352     unsigned ContentSize =
3353         sizeof(int16_t) +               // DWARF ARange version number
3354         Asm->getDwarfOffsetByteSize() + // Offset of CU in the .debug_info
3355                                         // section
3356         sizeof(int8_t) +                // Pointer Size (in bytes)
3357         sizeof(int8_t);                 // Segment Size (in bytes)
3358 
3359     unsigned TupleSize = PtrSize * 2;
3360 
3361     // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
3362     unsigned Padding = offsetToAlignment(
3363         Asm->getUnitLengthFieldByteSize() + ContentSize, Align(TupleSize));
3364 
3365     ContentSize += Padding;
3366     ContentSize += (List.size() + 1) * TupleSize;
3367 
3368     // For each compile unit, write the list of spans it covers.
3369     Asm->emitDwarfUnitLength(ContentSize, "Length of ARange Set");
3370     Asm->OutStreamer->AddComment("DWARF Arange version number");
3371     Asm->emitInt16(dwarf::DW_ARANGES_VERSION);
3372     Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
3373     emitSectionReference(*CU);
3374     Asm->OutStreamer->AddComment("Address Size (in bytes)");
3375     Asm->emitInt8(PtrSize);
3376     Asm->OutStreamer->AddComment("Segment Size (in bytes)");
3377     Asm->emitInt8(0);
3378 
3379     Asm->OutStreamer->emitFill(Padding, 0xff);
3380 
3381     for (const ArangeSpan &Span : List) {
3382       Asm->emitLabelReference(Span.Start, PtrSize);
3383 
3384       // Calculate the size as being from the span start to its end.
3385       //
3386       // If the size is zero, then round it up to one byte. The DWARF
3387       // specification requires that entries in this table have nonzero
3388       // lengths.
3389       auto SizeRef = SymSize.find(Span.Start);
3390       if ((SizeRef == SymSize.end() || SizeRef->second != 0) && Span.End) {
3391         Asm->emitLabelDifference(Span.End, Span.Start, PtrSize);
3392       } else {
3393         // For symbols without an end marker (e.g. common), we
3394         // write a single arange entry containing just that one symbol.
3395         uint64_t Size;
3396         if (SizeRef == SymSize.end() || SizeRef->second == 0)
3397           Size = 1;
3398         else
3399           Size = SizeRef->second;
3400 
3401         Asm->OutStreamer->emitIntValue(Size, PtrSize);
3402       }
3403     }
3404 
3405     Asm->OutStreamer->AddComment("ARange terminator");
3406     Asm->OutStreamer->emitIntValue(0, PtrSize);
3407     Asm->OutStreamer->emitIntValue(0, PtrSize);
3408   }
3409 }
3410 
3411 /// Emit a single range list. We handle both DWARF v5 and earlier.
3412 static void emitRangeList(DwarfDebug &DD, AsmPrinter *Asm,
3413                           const RangeSpanList &List) {
3414   emitRangeList(DD, Asm, List.Label, List.Ranges, *List.CU,
3415                 dwarf::DW_RLE_base_addressx, dwarf::DW_RLE_offset_pair,
3416                 dwarf::DW_RLE_startx_length, dwarf::DW_RLE_end_of_list,
3417                 llvm::dwarf::RangeListEncodingString,
3418                 List.CU->getCUNode()->getRangesBaseAddress() ||
3419                     DD.getDwarfVersion() >= 5,
3420                 [](auto) {});
3421 }
3422 
3423 void DwarfDebug::emitDebugRangesImpl(const DwarfFile &Holder, MCSection *Section) {
3424   if (Holder.getRangeLists().empty())
3425     return;
3426 
3427   assert(useRangesSection());
3428   assert(!CUMap.empty());
3429   assert(llvm::any_of(CUMap, [](const decltype(CUMap)::value_type &Pair) {
3430     return !Pair.second->getCUNode()->isDebugDirectivesOnly();
3431   }));
3432 
3433   Asm->OutStreamer->switchSection(Section);
3434 
3435   MCSymbol *TableEnd = nullptr;
3436   if (getDwarfVersion() >= 5)
3437     TableEnd = emitRnglistsTableHeader(Asm, Holder);
3438 
3439   for (const RangeSpanList &List : Holder.getRangeLists())
3440     emitRangeList(*this, Asm, List);
3441 
3442   if (TableEnd)
3443     Asm->OutStreamer->emitLabel(TableEnd);
3444 }
3445 
3446 /// Emit address ranges into the .debug_ranges section or into the DWARF v5
3447 /// .debug_rnglists section.
3448 void DwarfDebug::emitDebugRanges() {
3449   const auto &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
3450 
3451   emitDebugRangesImpl(Holder,
3452                       getDwarfVersion() >= 5
3453                           ? Asm->getObjFileLowering().getDwarfRnglistsSection()
3454                           : Asm->getObjFileLowering().getDwarfRangesSection());
3455 }
3456 
3457 void DwarfDebug::emitDebugRangesDWO() {
3458   emitDebugRangesImpl(InfoHolder,
3459                       Asm->getObjFileLowering().getDwarfRnglistsDWOSection());
3460 }
3461 
3462 /// Emit the header of a DWARF 5 macro section, or the GNU extension for
3463 /// DWARF 4.
3464 static void emitMacroHeader(AsmPrinter *Asm, const DwarfDebug &DD,
3465                             const DwarfCompileUnit &CU, uint16_t DwarfVersion) {
3466   enum HeaderFlagMask {
3467 #define HANDLE_MACRO_FLAG(ID, NAME) MACRO_FLAG_##NAME = ID,
3468 #include "llvm/BinaryFormat/Dwarf.def"
3469   };
3470   Asm->OutStreamer->AddComment("Macro information version");
3471   Asm->emitInt16(DwarfVersion >= 5 ? DwarfVersion : 4);
3472   // We emit the line offset flag unconditionally here, since line offset should
3473   // be mostly present.
3474   if (Asm->isDwarf64()) {
3475     Asm->OutStreamer->AddComment("Flags: 64 bit, debug_line_offset present");
3476     Asm->emitInt8(MACRO_FLAG_OFFSET_SIZE | MACRO_FLAG_DEBUG_LINE_OFFSET);
3477   } else {
3478     Asm->OutStreamer->AddComment("Flags: 32 bit, debug_line_offset present");
3479     Asm->emitInt8(MACRO_FLAG_DEBUG_LINE_OFFSET);
3480   }
3481   Asm->OutStreamer->AddComment("debug_line_offset");
3482   if (DD.useSplitDwarf())
3483     Asm->emitDwarfLengthOrOffset(0);
3484   else
3485     Asm->emitDwarfSymbolReference(CU.getLineTableStartSym());
3486 }
3487 
3488 void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
3489   for (auto *MN : Nodes) {
3490     if (auto *M = dyn_cast<DIMacro>(MN))
3491       emitMacro(*M);
3492     else if (auto *F = dyn_cast<DIMacroFile>(MN))
3493       emitMacroFile(*F, U);
3494     else
3495       llvm_unreachable("Unexpected DI type!");
3496   }
3497 }
3498 
3499 void DwarfDebug::emitMacro(DIMacro &M) {
3500   StringRef Name = M.getName();
3501   StringRef Value = M.getValue();
3502 
3503   // There should be one space between the macro name and the macro value in
3504   // define entries. In undef entries, only the macro name is emitted.
3505   std::string Str = Value.empty() ? Name.str() : (Name + " " + Value).str();
3506 
3507   if (UseDebugMacroSection) {
3508     if (getDwarfVersion() >= 5) {
3509       unsigned Type = M.getMacinfoType() == dwarf::DW_MACINFO_define
3510                           ? dwarf::DW_MACRO_define_strx
3511                           : dwarf::DW_MACRO_undef_strx;
3512       Asm->OutStreamer->AddComment(dwarf::MacroString(Type));
3513       Asm->emitULEB128(Type);
3514       Asm->OutStreamer->AddComment("Line Number");
3515       Asm->emitULEB128(M.getLine());
3516       Asm->OutStreamer->AddComment("Macro String");
3517       Asm->emitULEB128(
3518           InfoHolder.getStringPool().getIndexedEntry(*Asm, Str).getIndex());
3519     } else {
3520       unsigned Type = M.getMacinfoType() == dwarf::DW_MACINFO_define
3521                           ? dwarf::DW_MACRO_GNU_define_indirect
3522                           : dwarf::DW_MACRO_GNU_undef_indirect;
3523       Asm->OutStreamer->AddComment(dwarf::GnuMacroString(Type));
3524       Asm->emitULEB128(Type);
3525       Asm->OutStreamer->AddComment("Line Number");
3526       Asm->emitULEB128(M.getLine());
3527       Asm->OutStreamer->AddComment("Macro String");
3528       Asm->emitDwarfSymbolReference(
3529           InfoHolder.getStringPool().getEntry(*Asm, Str).getSymbol());
3530     }
3531   } else {
3532     Asm->OutStreamer->AddComment(dwarf::MacinfoString(M.getMacinfoType()));
3533     Asm->emitULEB128(M.getMacinfoType());
3534     Asm->OutStreamer->AddComment("Line Number");
3535     Asm->emitULEB128(M.getLine());
3536     Asm->OutStreamer->AddComment("Macro String");
3537     Asm->OutStreamer->emitBytes(Str);
3538     Asm->emitInt8('\0');
3539   }
3540 }
3541 
3542 void DwarfDebug::emitMacroFileImpl(
3543     DIMacroFile &MF, DwarfCompileUnit &U, unsigned StartFile, unsigned EndFile,
3544     StringRef (*MacroFormToString)(unsigned Form)) {
3545 
3546   Asm->OutStreamer->AddComment(MacroFormToString(StartFile));
3547   Asm->emitULEB128(StartFile);
3548   Asm->OutStreamer->AddComment("Line Number");
3549   Asm->emitULEB128(MF.getLine());
3550   Asm->OutStreamer->AddComment("File Number");
3551   DIFile &F = *MF.getFile();
3552   if (useSplitDwarf())
3553     Asm->emitULEB128(getDwoLineTable(U)->getFile(
3554         F.getDirectory(), F.getFilename(), getMD5AsBytes(&F),
3555         Asm->OutContext.getDwarfVersion(), F.getSource()));
3556   else
3557     Asm->emitULEB128(U.getOrCreateSourceID(&F));
3558   handleMacroNodes(MF.getElements(), U);
3559   Asm->OutStreamer->AddComment(MacroFormToString(EndFile));
3560   Asm->emitULEB128(EndFile);
3561 }
3562 
3563 void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) {
3564   // DWARFv5 macro and DWARFv4 macinfo share some common encodings,
3565   // so for readibility/uniformity, We are explicitly emitting those.
3566   assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file);
3567   if (UseDebugMacroSection)
3568     emitMacroFileImpl(
3569         F, U, dwarf::DW_MACRO_start_file, dwarf::DW_MACRO_end_file,
3570         (getDwarfVersion() >= 5) ? dwarf::MacroString : dwarf::GnuMacroString);
3571   else
3572     emitMacroFileImpl(F, U, dwarf::DW_MACINFO_start_file,
3573                       dwarf::DW_MACINFO_end_file, dwarf::MacinfoString);
3574 }
3575 
3576 void DwarfDebug::emitDebugMacinfoImpl(MCSection *Section) {
3577   for (const auto &P : CUMap) {
3578     auto &TheCU = *P.second;
3579     auto *SkCU = TheCU.getSkeleton();
3580     DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
3581     auto *CUNode = cast<DICompileUnit>(P.first);
3582     DIMacroNodeArray Macros = CUNode->getMacros();
3583     if (Macros.empty())
3584       continue;
3585     Asm->OutStreamer->switchSection(Section);
3586     Asm->OutStreamer->emitLabel(U.getMacroLabelBegin());
3587     if (UseDebugMacroSection)
3588       emitMacroHeader(Asm, *this, U, getDwarfVersion());
3589     handleMacroNodes(Macros, U);
3590     Asm->OutStreamer->AddComment("End Of Macro List Mark");
3591     Asm->emitInt8(0);
3592   }
3593 }
3594 
3595 /// Emit macros into a debug macinfo/macro section.
3596 void DwarfDebug::emitDebugMacinfo() {
3597   auto &ObjLower = Asm->getObjFileLowering();
3598   emitDebugMacinfoImpl(UseDebugMacroSection
3599                            ? ObjLower.getDwarfMacroSection()
3600                            : ObjLower.getDwarfMacinfoSection());
3601 }
3602 
3603 void DwarfDebug::emitDebugMacinfoDWO() {
3604   auto &ObjLower = Asm->getObjFileLowering();
3605   emitDebugMacinfoImpl(UseDebugMacroSection
3606                            ? ObjLower.getDwarfMacroDWOSection()
3607                            : ObjLower.getDwarfMacinfoDWOSection());
3608 }
3609 
3610 // DWARF5 Experimental Separate Dwarf emitters.
3611 
3612 void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
3613                                   std::unique_ptr<DwarfCompileUnit> NewU) {
3614 
3615   if (!CompilationDir.empty())
3616     NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
3617   addGnuPubAttributes(*NewU, Die);
3618 
3619   SkeletonHolder.addUnit(std::move(NewU));
3620 }
3621 
3622 DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
3623 
3624   auto OwnedUnit = std::make_unique<DwarfCompileUnit>(
3625       CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder,
3626       UnitKind::Skeleton);
3627   DwarfCompileUnit &NewCU = *OwnedUnit;
3628   NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
3629 
3630   NewCU.initStmtList();
3631 
3632   if (useSegmentedStringOffsetsTable())
3633     NewCU.addStringOffsetsStart();
3634 
3635   initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
3636 
3637   return NewCU;
3638 }
3639 
3640 // Emit the .debug_info.dwo section for separated dwarf. This contains the
3641 // compile units that would normally be in debug_info.
3642 void DwarfDebug::emitDebugInfoDWO() {
3643   assert(useSplitDwarf() && "No split dwarf debug info?");
3644   // Don't emit relocations into the dwo file.
3645   InfoHolder.emitUnits(/* UseOffsets */ true);
3646 }
3647 
3648 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
3649 // abbreviations for the .debug_info.dwo section.
3650 void DwarfDebug::emitDebugAbbrevDWO() {
3651   assert(useSplitDwarf() && "No split dwarf?");
3652   InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
3653 }
3654 
3655 void DwarfDebug::emitDebugLineDWO() {
3656   assert(useSplitDwarf() && "No split dwarf?");
3657   SplitTypeUnitFileTable.Emit(
3658       *Asm->OutStreamer, MCDwarfLineTableParams(),
3659       Asm->getObjFileLowering().getDwarfLineDWOSection());
3660 }
3661 
3662 void DwarfDebug::emitStringOffsetsTableHeaderDWO() {
3663   assert(useSplitDwarf() && "No split dwarf?");
3664   InfoHolder.getStringPool().emitStringOffsetsTableHeader(
3665       *Asm, Asm->getObjFileLowering().getDwarfStrOffDWOSection(),
3666       InfoHolder.getStringOffsetsStartSym());
3667 }
3668 
3669 // Emit the .debug_str.dwo section for separated dwarf. This contains the
3670 // string section and is identical in format to traditional .debug_str
3671 // sections.
3672 void DwarfDebug::emitDebugStrDWO() {
3673   if (useSegmentedStringOffsetsTable())
3674     emitStringOffsetsTableHeaderDWO();
3675   assert(useSplitDwarf() && "No split dwarf?");
3676   MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
3677   InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
3678                          OffSec, /* UseRelativeOffsets = */ false);
3679 }
3680 
3681 // Emit address pool.
3682 void DwarfDebug::emitDebugAddr() {
3683   AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
3684 }
3685 
3686 MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
3687   if (!useSplitDwarf())
3688     return nullptr;
3689   const DICompileUnit *DIUnit = CU.getCUNode();
3690   SplitTypeUnitFileTable.maybeSetRootFile(
3691       DIUnit->getDirectory(), DIUnit->getFilename(),
3692       getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource());
3693   return &SplitTypeUnitFileTable;
3694 }
3695 
3696 uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) {
3697   MD5 Hash;
3698   Hash.update(Identifier);
3699   // ... take the least significant 8 bytes and return those. Our MD5
3700   // implementation always returns its results in little endian, so we actually
3701   // need the "high" word.
3702   MD5::MD5Result Result;
3703   Hash.final(Result);
3704   return Result.high();
3705 }
3706 
3707 void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
3708                                       StringRef Identifier, DIE &RefDie,
3709                                       const DICompositeType *CTy) {
3710   // Fast path if we're building some type units and one has already used the
3711   // address pool we know we're going to throw away all this work anyway, so
3712   // don't bother building dependent types.
3713   if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
3714     return;
3715 
3716   auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0));
3717   if (!Ins.second) {
3718     CU.addDIETypeSignature(RefDie, Ins.first->second);
3719     return;
3720   }
3721 
3722   setCurrentDWARF5AccelTable(DWARF5AccelTableKind::TU);
3723   bool TopLevelType = TypeUnitsUnderConstruction.empty();
3724   AddrPool.resetUsedFlag();
3725 
3726   auto OwnedUnit = std::make_unique<DwarfTypeUnit>(
3727       CU, Asm, this, &InfoHolder, NumTypeUnitsCreated++, getDwoLineTable(CU));
3728   DwarfTypeUnit &NewTU = *OwnedUnit;
3729   DIE &UnitDie = NewTU.getUnitDie();
3730   TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy);
3731 
3732   NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
3733                 CU.getLanguage());
3734 
3735   uint64_t Signature = makeTypeSignature(Identifier);
3736   NewTU.setTypeSignature(Signature);
3737   Ins.first->second = Signature;
3738 
3739   if (useSplitDwarf()) {
3740     // Although multiple type units can have the same signature, they are not
3741     // guranteed to be bit identical. When LLDB uses .debug_names it needs to
3742     // know from which CU a type unit came from. These two attrbutes help it to
3743     // figure that out.
3744     if (getDwarfVersion() >= 5) {
3745       if (!CompilationDir.empty())
3746         NewTU.addString(UnitDie, dwarf::DW_AT_comp_dir, CompilationDir);
3747       NewTU.addString(UnitDie, dwarf::DW_AT_dwo_name,
3748                       Asm->TM.Options.MCOptions.SplitDwarfFile);
3749     }
3750     MCSection *Section =
3751         getDwarfVersion() <= 4
3752             ? Asm->getObjFileLowering().getDwarfTypesDWOSection()
3753             : Asm->getObjFileLowering().getDwarfInfoDWOSection();
3754     NewTU.setSection(Section);
3755   } else {
3756     MCSection *Section =
3757         getDwarfVersion() <= 4
3758             ? Asm->getObjFileLowering().getDwarfTypesSection(Signature)
3759             : Asm->getObjFileLowering().getDwarfInfoSection(Signature);
3760     NewTU.setSection(Section);
3761     // Non-split type units reuse the compile unit's line table.
3762     CU.applyStmtList(UnitDie);
3763   }
3764 
3765   // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type
3766   // units.
3767   if (useSegmentedStringOffsetsTable() && !useSplitDwarf())
3768     NewTU.addStringOffsetsStart();
3769 
3770   NewTU.setType(NewTU.createTypeDIE(CTy));
3771 
3772   if (TopLevelType) {
3773     auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
3774     TypeUnitsUnderConstruction.clear();
3775 
3776     // Types referencing entries in the address table cannot be placed in type
3777     // units.
3778     if (AddrPool.hasBeenUsed()) {
3779       AccelTypeUnitsDebugNames.clear();
3780       // Remove all the types built while building this type.
3781       // This is pessimistic as some of these types might not be dependent on
3782       // the type that used an address.
3783       for (const auto &TU : TypeUnitsToAdd)
3784         TypeSignatures.erase(TU.second);
3785 
3786       // Construct this type in the CU directly.
3787       // This is inefficient because all the dependent types will be rebuilt
3788       // from scratch, including building them in type units, discovering that
3789       // they depend on addresses, throwing them out and rebuilding them.
3790       setCurrentDWARF5AccelTable(DWARF5AccelTableKind::CU);
3791       CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
3792       CU.updateAcceleratorTables(CTy->getScope(), CTy, RefDie);
3793       return;
3794     }
3795 
3796     // If the type wasn't dependent on fission addresses, finish adding the type
3797     // and all its dependent types.
3798     for (auto &TU : TypeUnitsToAdd) {
3799       InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get());
3800       InfoHolder.emitUnit(TU.first.get(), useSplitDwarf());
3801       if (getDwarfVersion() >= 5 &&
3802           getAccelTableKind() == AccelTableKind::Dwarf) {
3803         if (useSplitDwarf())
3804           AccelDebugNames.addTypeUnitSignature(*TU.first);
3805         else
3806           AccelDebugNames.addTypeUnitSymbol(*TU.first);
3807       }
3808     }
3809     AccelTypeUnitsDebugNames.convertDieToOffset();
3810     AccelDebugNames.addTypeEntries(AccelTypeUnitsDebugNames);
3811     AccelTypeUnitsDebugNames.clear();
3812     setCurrentDWARF5AccelTable(DWARF5AccelTableKind::CU);
3813   }
3814   CU.addDIETypeSignature(RefDie, Signature);
3815 }
3816 
3817 // Add the Name along with its companion DIE to the appropriate accelerator
3818 // table (for AccelTableKind::Dwarf it's always AccelDebugNames, for
3819 // AccelTableKind::Apple, we use the table we got as an argument). If
3820 // accelerator tables are disabled, this function does nothing.
3821 template <typename DataT>
3822 void DwarfDebug::addAccelNameImpl(
3823     const DwarfUnit &Unit,
3824     const DICompileUnit::DebugNameTableKind NameTableKind,
3825     AccelTable<DataT> &AppleAccel, StringRef Name, const DIE &Die) {
3826   if (getAccelTableKind() == AccelTableKind::None ||
3827       Unit.getUnitDie().getTag() == dwarf::DW_TAG_skeleton_unit || Name.empty())
3828     return;
3829 
3830   if (getAccelTableKind() != AccelTableKind::Apple &&
3831       NameTableKind != DICompileUnit::DebugNameTableKind::Apple &&
3832       NameTableKind != DICompileUnit::DebugNameTableKind::Default)
3833     return;
3834 
3835   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
3836   DwarfStringPoolEntryRef Ref = Holder.getStringPool().getEntry(*Asm, Name);
3837 
3838   switch (getAccelTableKind()) {
3839   case AccelTableKind::Apple:
3840     AppleAccel.addName(Ref, Die);
3841     break;
3842   case AccelTableKind::Dwarf: {
3843     DWARF5AccelTable &Current = getCurrentDWARF5AccelTable();
3844     assert(((&Current == &AccelTypeUnitsDebugNames) ||
3845             ((&Current == &AccelDebugNames) &&
3846              (Unit.getUnitDie().getTag() != dwarf::DW_TAG_type_unit))) &&
3847                "Kind is CU but TU is being processed.");
3848     assert(((&Current == &AccelDebugNames) ||
3849             ((&Current == &AccelTypeUnitsDebugNames) &&
3850              (Unit.getUnitDie().getTag() == dwarf::DW_TAG_type_unit))) &&
3851                "Kind is TU but CU is being processed.");
3852     // The type unit can be discarded, so need to add references to final
3853     // acceleration table once we know it's complete and we emit it.
3854     Current.addName(Ref, Die, Unit.getUniqueID(),
3855                     Unit.getUnitDie().getTag() == dwarf::DW_TAG_type_unit);
3856     break;
3857   }
3858   case AccelTableKind::Default:
3859     llvm_unreachable("Default should have already been resolved.");
3860   case AccelTableKind::None:
3861     llvm_unreachable("None handled above");
3862   }
3863 }
3864 
3865 void DwarfDebug::addAccelName(
3866     const DwarfUnit &Unit,
3867     const DICompileUnit::DebugNameTableKind NameTableKind, StringRef Name,
3868     const DIE &Die) {
3869   addAccelNameImpl(Unit, NameTableKind, AccelNames, Name, Die);
3870 }
3871 
3872 void DwarfDebug::addAccelObjC(
3873     const DwarfUnit &Unit,
3874     const DICompileUnit::DebugNameTableKind NameTableKind, StringRef Name,
3875     const DIE &Die) {
3876   // ObjC names go only into the Apple accelerator tables.
3877   if (getAccelTableKind() == AccelTableKind::Apple)
3878     addAccelNameImpl(Unit, NameTableKind, AccelObjC, Name, Die);
3879 }
3880 
3881 void DwarfDebug::addAccelNamespace(
3882     const DwarfUnit &Unit,
3883     const DICompileUnit::DebugNameTableKind NameTableKind, StringRef Name,
3884     const DIE &Die) {
3885   addAccelNameImpl(Unit, NameTableKind, AccelNamespace, Name, Die);
3886 }
3887 
3888 void DwarfDebug::addAccelType(
3889     const DwarfUnit &Unit,
3890     const DICompileUnit::DebugNameTableKind NameTableKind, StringRef Name,
3891     const DIE &Die, char Flags) {
3892   addAccelNameImpl(Unit, NameTableKind, AccelTypes, Name, Die);
3893 }
3894 
3895 uint16_t DwarfDebug::getDwarfVersion() const {
3896   return Asm->OutStreamer->getContext().getDwarfVersion();
3897 }
3898 
3899 dwarf::Form DwarfDebug::getDwarfSectionOffsetForm() const {
3900   if (Asm->getDwarfVersion() >= 4)
3901     return dwarf::Form::DW_FORM_sec_offset;
3902   assert((!Asm->isDwarf64() || (Asm->getDwarfVersion() == 3)) &&
3903          "DWARF64 is not defined prior DWARFv3");
3904   return Asm->isDwarf64() ? dwarf::Form::DW_FORM_data8
3905                           : dwarf::Form::DW_FORM_data4;
3906 }
3907 
3908 const MCSymbol *DwarfDebug::getSectionLabel(const MCSection *S) {
3909   return SectionLabels.lookup(S);
3910 }
3911 
3912 void DwarfDebug::insertSectionLabel(const MCSymbol *S) {
3913   if (SectionLabels.insert(std::make_pair(&S->getSection(), S)).second)
3914     if (useSplitDwarf() || getDwarfVersion() >= 5)
3915       AddrPool.getIndex(S);
3916 }
3917 
3918 std::optional<MD5::MD5Result>
3919 DwarfDebug::getMD5AsBytes(const DIFile *File) const {
3920   assert(File);
3921   if (getDwarfVersion() < 5)
3922     return std::nullopt;
3923   std::optional<DIFile::ChecksumInfo<StringRef>> Checksum = File->getChecksum();
3924   if (!Checksum || Checksum->Kind != DIFile::CSK_MD5)
3925     return std::nullopt;
3926 
3927   // Convert the string checksum to an MD5Result for the streamer.
3928   // The verifier validates the checksum so we assume it's okay.
3929   // An MD5 checksum is 16 bytes.
3930   std::string ChecksumString = fromHex(Checksum->Value);
3931   MD5::MD5Result CKMem;
3932   std::copy(ChecksumString.begin(), ChecksumString.end(), CKMem.data());
3933   return CKMem;
3934 }
3935 
3936 bool DwarfDebug::alwaysUseRanges(const DwarfCompileUnit &CU) const {
3937   if (MinimizeAddr == MinimizeAddrInV5::Ranges)
3938     return true;
3939   if (MinimizeAddr != MinimizeAddrInV5::Default)
3940     return false;
3941   if (useSplitDwarf())
3942     return true;
3943   return false;
3944 }
3945 
3946 void DwarfDebug::beginCodeAlignment(const MachineBasicBlock &MBB) {
3947   if (MBB.getAlignment() == Align(1))
3948     return;
3949 
3950   auto *SP = MBB.getParent()->getFunction().getSubprogram();
3951   bool NoDebug =
3952       !SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug;
3953 
3954   if (NoDebug)
3955     return;
3956 
3957   auto PrevLoc = Asm->OutStreamer->getContext().getCurrentDwarfLoc();
3958   if (PrevLoc.getLine()) {
3959     Asm->OutStreamer->emitDwarfLocDirective(
3960         PrevLoc.getFileNum(), 0, PrevLoc.getColumn(), 0, 0, 0, StringRef());
3961     MCDwarfLineEntry::make(Asm->OutStreamer.get(),
3962                            Asm->OutStreamer->getCurrentSectionOnly());
3963   }
3964 }
3965