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