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