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