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 CU.getOrCreateTypeDIE(TyNode: cast<DIType>(Val: Ty));
1294
1295 for (auto *Ty : CUNode->getRetainedTypes()) {
1296 // The retained types array by design contains pointers to
1297 // MDNodes rather than DIRefs. Unique them here.
1298 if (DIType *RT = dyn_cast<DIType>(Val: Ty))
1299 // There is no point in force-emitting a forward declaration.
1300 CU.getOrCreateTypeDIE(TyNode: RT);
1301 }
1302 }
1303}
1304
1305void DwarfDebug::finishEntityDefinitions() {
1306 for (const auto &Entity : ConcreteEntities) {
1307 DIE *Die = Entity->getDIE();
1308 assert(Die);
1309 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
1310 // in the ConcreteEntities list, rather than looking it up again here.
1311 // DIE::getUnit isn't simple - it walks parent pointers, etc.
1312 DwarfCompileUnit *Unit = CUDieMap.lookup(Val: Die->getUnitDie());
1313 assert(Unit);
1314 Unit->finishEntityDefinition(Entity: Entity.get());
1315 }
1316}
1317
1318void DwarfDebug::finishSubprogramDefinitions() {
1319 for (const DISubprogram *SP : ProcessedSPNodes) {
1320 assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug);
1321 forBothCUs(
1322 CU&: getOrCreateDwarfCompileUnit(DIUnit: SP->getUnit()),
1323 F: [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); });
1324 }
1325}
1326
1327void DwarfDebug::finalizeModuleInfo() {
1328 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1329
1330 finishSubprogramDefinitions();
1331
1332 finishEntityDefinitions();
1333
1334 bool HasEmittedSplitCU = false;
1335
1336 // Handle anything that needs to be done on a per-unit basis after
1337 // all other generation.
1338 for (const auto &P : CUMap) {
1339 auto &TheCU = *P.second;
1340 if (TheCU.getCUNode()->isDebugDirectivesOnly())
1341 continue;
1342 TheCU.attachLexicalScopesAbstractOrigins();
1343 // Emit DW_AT_containing_type attribute to connect types with their
1344 // vtable holding type.
1345 TheCU.constructContainingTypeDIEs();
1346
1347 // Add CU specific attributes if we need to add any.
1348 // If we're splitting the dwarf out now that we've got the entire
1349 // CU then add the dwo id to it.
1350 auto *SkCU = TheCU.getSkeleton();
1351
1352 bool HasSplitUnit = SkCU && !TheCU.getUnitDie().children().empty();
1353
1354 if (HasSplitUnit) {
1355 (void)HasEmittedSplitCU;
1356 assert((shareAcrossDWOCUs() || !HasEmittedSplitCU) &&
1357 "Multiple CUs emitted into a single dwo file");
1358 HasEmittedSplitCU = true;
1359 dwarf::Attribute attrDWOName = getDwarfVersion() >= 5
1360 ? dwarf::DW_AT_dwo_name
1361 : dwarf::DW_AT_GNU_dwo_name;
1362 finishUnitAttributes(DIUnit: TheCU.getCUNode(), NewCU&: TheCU);
1363 StringRef DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile;
1364 TheCU.addString(Die&: TheCU.getUnitDie(), Attribute: attrDWOName, Str: DWOName);
1365 SkCU->addString(Die&: SkCU->getUnitDie(), Attribute: attrDWOName, Str: DWOName);
1366 // Emit a unique identifier for this CU. Include the DWO file name in the
1367 // hash to avoid the case where two (almost) empty compile units have the
1368 // same contents. This can happen if link-time optimization removes nearly
1369 // all (unused) code from a CU.
1370 uint64_t ID =
1371 DIEHash(Asm, &TheCU).computeCUSignature(DWOName, Die: TheCU.getUnitDie());
1372 if (getDwarfVersion() >= 5) {
1373 TheCU.setDWOId(ID);
1374 SkCU->setDWOId(ID);
1375 } else {
1376 TheCU.addUInt(Die&: TheCU.getUnitDie(), Attribute: dwarf::DW_AT_GNU_dwo_id,
1377 Form: dwarf::DW_FORM_data8, Integer: ID);
1378 SkCU->addUInt(Die&: SkCU->getUnitDie(), Attribute: dwarf::DW_AT_GNU_dwo_id,
1379 Form: dwarf::DW_FORM_data8, Integer: ID);
1380 }
1381
1382 if (getDwarfVersion() < 5 && !SkeletonHolder.getRangeLists().empty()) {
1383 const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
1384 SkCU->addSectionLabel(Die&: SkCU->getUnitDie(), Attribute: dwarf::DW_AT_GNU_ranges_base,
1385 Label: Sym, Sec: Sym);
1386 }
1387 } else if (SkCU) {
1388 finishUnitAttributes(DIUnit: SkCU->getCUNode(), NewCU&: *SkCU);
1389 }
1390
1391 // If we have code split among multiple sections or non-contiguous
1392 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
1393 // remain in the .o file, otherwise add a DW_AT_low_pc.
1394 // FIXME: We should use ranges allow reordering of code ala
1395 // .subsections_via_symbols in mach-o. This would mean turning on
1396 // ranges for all subprogram DIEs for mach-o.
1397 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
1398
1399 if (unsigned NumRanges = TheCU.getRanges().size()) {
1400 // PTX does not support subtracting labels from the code section in the
1401 // debug_loc section. To work around this, the NVPTX backend needs the
1402 // compile unit to have no low_pc in order to have a zero base_address
1403 // when handling debug_loc in cuda-gdb.
1404 if (!(Asm->TM.getTargetTriple().isNVPTX() && tuneForGDB())) {
1405 if (NumRanges > 1 && useRangesSection())
1406 // A DW_AT_low_pc attribute may also be specified in combination with
1407 // DW_AT_ranges to specify the default base address for use in
1408 // location lists (see Section 2.6.2) and range lists (see Section
1409 // 2.17.3).
1410 U.addUInt(Die&: U.getUnitDie(), Attribute: dwarf::DW_AT_low_pc, Form: dwarf::DW_FORM_addr,
1411 Integer: 0);
1412 else
1413 U.setBaseAddress(TheCU.getRanges().front().Begin);
1414 U.attachRangesOrLowHighPC(D&: U.getUnitDie(), Ranges: TheCU.takeRanges());
1415 }
1416 }
1417
1418 // We don't keep track of which addresses are used in which CU so this
1419 // is a bit pessimistic under LTO.
1420 if ((HasSplitUnit || getDwarfVersion() >= 5) && !AddrPool.isEmpty())
1421 U.addAddrTableBase();
1422
1423 if (getDwarfVersion() >= 5) {
1424 if (U.hasRangeLists())
1425 U.addRnglistsBase();
1426
1427 if (!DebugLocs.getLists().empty() && !useSplitDwarf()) {
1428 U.addSectionLabel(Die&: U.getUnitDie(), Attribute: dwarf::DW_AT_loclists_base,
1429 Label: DebugLocs.getSym(),
1430 Sec: TLOF.getDwarfLoclistsSection()->getBeginSymbol());
1431 }
1432 }
1433
1434 auto *CUNode = cast<DICompileUnit>(Val: P.first);
1435 // If compile Unit has macros, emit "DW_AT_macro_info/DW_AT_macros"
1436 // attribute.
1437 if (CUNode->getMacros()) {
1438 if (UseDebugMacroSection) {
1439 if (useSplitDwarf())
1440 TheCU.addSectionDelta(
1441 Die&: TheCU.getUnitDie(), Attribute: dwarf::DW_AT_macros, Hi: U.getMacroLabelBegin(),
1442 Lo: TLOF.getDwarfMacroDWOSection()->getBeginSymbol());
1443 else {
1444 dwarf::Attribute MacrosAttr = getDwarfVersion() >= 5
1445 ? dwarf::DW_AT_macros
1446 : dwarf::DW_AT_GNU_macros;
1447 U.addSectionLabel(Die&: U.getUnitDie(), Attribute: MacrosAttr, Label: U.getMacroLabelBegin(),
1448 Sec: TLOF.getDwarfMacroSection()->getBeginSymbol());
1449 }
1450 } else {
1451 if (useSplitDwarf())
1452 TheCU.addSectionDelta(
1453 Die&: TheCU.getUnitDie(), Attribute: dwarf::DW_AT_macro_info,
1454 Hi: U.getMacroLabelBegin(),
1455 Lo: TLOF.getDwarfMacinfoDWOSection()->getBeginSymbol());
1456 else
1457 U.addSectionLabel(Die&: U.getUnitDie(), Attribute: dwarf::DW_AT_macro_info,
1458 Label: U.getMacroLabelBegin(),
1459 Sec: TLOF.getDwarfMacinfoSection()->getBeginSymbol());
1460 }
1461 }
1462 }
1463
1464 // Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
1465 for (auto *CUNode : MMI->getModule()->debug_compile_units())
1466 if (CUNode->getDWOId())
1467 getOrCreateDwarfCompileUnit(DIUnit: CUNode);
1468
1469 // Compute DIE offsets and sizes.
1470 InfoHolder.computeSizeAndOffsets();
1471 if (useSplitDwarf())
1472 SkeletonHolder.computeSizeAndOffsets();
1473
1474 // Now that offsets are computed, can replace DIEs in debug_names Entry with
1475 // an actual offset.
1476 AccelDebugNames.convertDieToOffset();
1477}
1478
1479// Emit all Dwarf sections that should come after the content.
1480void DwarfDebug::endModule() {
1481 // Terminate the pending line table.
1482 if (PrevCU)
1483 terminateLineTable(CU: PrevCU);
1484 PrevCU = nullptr;
1485 assert(CurFn == nullptr);
1486 assert(CurMI == nullptr);
1487
1488 for (const auto &P : CUMap) {
1489 const auto *CUNode = cast<DICompileUnit>(Val: P.first);
1490 DwarfCompileUnit *CU = &*P.second;
1491
1492 // Emit imported entities.
1493 for (auto *IE : CUNode->getImportedEntities()) {
1494 assert(!isa_and_nonnull<DILocalScope>(IE->getScope()) &&
1495 "Unexpected function-local entity in 'imports' CU field.");
1496 CU->getOrCreateImportedEntityDIE(IE);
1497 }
1498 for (const auto *D : CU->getDeferredLocalDecls()) {
1499 if (auto *IE = dyn_cast<DIImportedEntity>(Val: D))
1500 CU->getOrCreateImportedEntityDIE(IE);
1501 else
1502 llvm_unreachable("Unexpected local retained node!");
1503 }
1504
1505 // Emit base types.
1506 CU->createBaseTypeDIEs();
1507 }
1508
1509 // If we aren't actually generating debug info (check beginModule -
1510 // conditionalized on the presence of the llvm.dbg.cu metadata node)
1511 if (!Asm || !Asm->hasDebugInfo())
1512 return;
1513
1514 // Finalize the debug info for the module.
1515 finalizeModuleInfo();
1516
1517 if (useSplitDwarf())
1518 // Emit debug_loc.dwo/debug_loclists.dwo section.
1519 emitDebugLocDWO();
1520 else
1521 // Emit debug_loc/debug_loclists section.
1522 emitDebugLoc();
1523
1524 // Corresponding abbreviations into a abbrev section.
1525 emitAbbreviations();
1526
1527 // Emit all the DIEs into a debug info section.
1528 emitDebugInfo();
1529
1530 // Emit info into a debug aranges section.
1531 if (UseARangesSection)
1532 emitDebugARanges();
1533
1534 // Emit info into a debug ranges section.
1535 emitDebugRanges();
1536
1537 if (useSplitDwarf())
1538 // Emit info into a debug macinfo.dwo section.
1539 emitDebugMacinfoDWO();
1540 else
1541 // Emit info into a debug macinfo/macro section.
1542 emitDebugMacinfo();
1543
1544 emitDebugStr();
1545
1546 if (useSplitDwarf()) {
1547 emitDebugStrDWO();
1548 emitDebugInfoDWO();
1549 emitDebugAbbrevDWO();
1550 emitDebugLineDWO();
1551 emitDebugRangesDWO();
1552 }
1553
1554 emitDebugAddr();
1555
1556 // Emit info into the dwarf accelerator table sections.
1557 switch (getAccelTableKind()) {
1558 case AccelTableKind::Apple:
1559 emitAccelNames();
1560 emitAccelObjC();
1561 emitAccelNamespaces();
1562 emitAccelTypes();
1563 break;
1564 case AccelTableKind::Dwarf:
1565 emitAccelDebugNames();
1566 break;
1567 case AccelTableKind::None:
1568 break;
1569 case AccelTableKind::Default:
1570 llvm_unreachable("Default should have already been resolved.");
1571 }
1572
1573 // Emit the pubnames and pubtypes sections if requested.
1574 emitDebugPubSections();
1575
1576 // clean up.
1577 // FIXME: AbstractVariables.clear();
1578}
1579
1580void DwarfDebug::ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU,
1581 const DINode *Node, const MDNode *ScopeNode) {
1582 if (CU.getExistingAbstractEntity(Node))
1583 return;
1584
1585 if (LexicalScope *Scope =
1586 LScopes.findAbstractScope(N: cast_or_null<DILocalScope>(Val: ScopeNode)))
1587 CU.createAbstractEntity(Node, Scope);
1588}
1589
1590static const DILocalScope *getRetainedNodeScope(const MDNode *N) {
1591 // Ensure the scope is not a DILexicalBlockFile.
1592 return DISubprogram::getRetainedNodeScope(N)->getNonLexicalBlockFileScope();
1593}
1594
1595// Collect variable information from side table maintained by MF.
1596void DwarfDebug::collectVariableInfoFromMFTable(
1597 DwarfCompileUnit &TheCU, DenseSet<InlinedEntity> &Processed) {
1598 SmallDenseMap<InlinedEntity, DbgVariable *> MFVars;
1599 LLVM_DEBUG(dbgs() << "DwarfDebug: collecting variables from MF side table\n");
1600 for (const auto &VI : Asm->MF->getVariableDbgInfo()) {
1601 if (!VI.Var)
1602 continue;
1603 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
1604 "Expected inlined-at fields to agree");
1605
1606 InlinedEntity Var(VI.Var, VI.Loc->getInlinedAt());
1607 Processed.insert(V: Var);
1608 LexicalScope *Scope = LScopes.findLexicalScope(DL: VI.Loc);
1609
1610 // If variable scope is not found then skip this variable.
1611 if (!Scope) {
1612 LLVM_DEBUG(dbgs() << "Dropping debug info for " << VI.Var->getName()
1613 << ", no variable scope found\n");
1614 continue;
1615 }
1616
1617 ensureAbstractEntityIsCreatedIfScoped(CU&: TheCU, Node: Var.first, ScopeNode: Scope->getScopeNode());
1618
1619 // If we have already seen information for this variable, add to what we
1620 // already know.
1621 if (DbgVariable *PreviousLoc = MFVars.lookup(Val: Var)) {
1622 auto *PreviousMMI = std::get_if<Loc::MMI>(ptr: PreviousLoc);
1623 auto *PreviousEntryValue = std::get_if<Loc::EntryValue>(ptr: PreviousLoc);
1624 // Previous and new locations are both stack slots (MMI).
1625 if (PreviousMMI && VI.inStackSlot())
1626 PreviousMMI->addFrameIndexExpr(Expr: VI.Expr, FI: VI.getStackSlot());
1627 // Previous and new locations are both entry values.
1628 else if (PreviousEntryValue && VI.inEntryValueRegister())
1629 PreviousEntryValue->addExpr(Reg: VI.getEntryValueRegister(), Expr: *VI.Expr);
1630 else {
1631 // Locations differ, this should (rarely) happen in optimized async
1632 // coroutines.
1633 // Prefer whichever location has an EntryValue.
1634 if (PreviousLoc->holds<Loc::MMI>())
1635 PreviousLoc->emplace<Loc::EntryValue>(args: VI.getEntryValueRegister(),
1636 args: *VI.Expr);
1637 LLVM_DEBUG(dbgs() << "Dropping debug info for " << VI.Var->getName()
1638 << ", conflicting fragment location types\n");
1639 }
1640 continue;
1641 }
1642
1643 auto RegVar = std::make_unique<DbgVariable>(
1644 args: cast<DILocalVariable>(Val: Var.first), args&: Var.second);
1645 if (VI.inStackSlot())
1646 RegVar->emplace<Loc::MMI>(args: VI.Expr, args: VI.getStackSlot());
1647 else
1648 RegVar->emplace<Loc::EntryValue>(args: VI.getEntryValueRegister(), args: *VI.Expr);
1649 LLVM_DEBUG(dbgs() << "Created DbgVariable for " << VI.Var->getName()
1650 << "\n");
1651 InfoHolder.addScopeVariable(LS: Scope, Var: RegVar.get());
1652 MFVars.insert(KV: {Var, RegVar.get()});
1653 ConcreteEntities.push_back(Elt: std::move(RegVar));
1654 }
1655}
1656
1657/// Determine whether a *singular* DBG_VALUE is valid for the entirety of its
1658/// enclosing lexical scope. The check ensures there are no other instructions
1659/// in the same lexical scope preceding the DBG_VALUE and that its range is
1660/// either open or otherwise rolls off the end of the scope.
1661static bool validThroughout(LexicalScopes &LScopes,
1662 const MachineInstr *DbgValue,
1663 const MachineInstr *RangeEnd,
1664 const InstructionOrdering &Ordering) {
1665 assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location");
1666 auto MBB = DbgValue->getParent();
1667 auto DL = DbgValue->getDebugLoc();
1668 auto *LScope = LScopes.findLexicalScope(DL);
1669 // Scope doesn't exist; this is a dead DBG_VALUE.
1670 if (!LScope)
1671 return false;
1672 auto &LSRange = LScope->getRanges();
1673 if (LSRange.size() == 0)
1674 return false;
1675
1676 const MachineInstr *LScopeBegin = LSRange.front().first;
1677 // If the scope starts before the DBG_VALUE then we may have a negative
1678 // result. Otherwise the location is live coming into the scope and we
1679 // can skip the following checks.
1680 if (!Ordering.isBefore(A: DbgValue, B: LScopeBegin)) {
1681 // Exit if the lexical scope begins outside of the current block.
1682 if (LScopeBegin->getParent() != MBB)
1683 return false;
1684
1685 MachineBasicBlock::const_reverse_iterator Pred(DbgValue);
1686 for (++Pred; Pred != MBB->rend(); ++Pred) {
1687 if (Pred->getFlag(Flag: MachineInstr::FrameSetup))
1688 break;
1689 auto PredDL = Pred->getDebugLoc();
1690 if (!PredDL || Pred->isMetaInstruction())
1691 continue;
1692 // Check whether the instruction preceding the DBG_VALUE is in the same
1693 // (sub)scope as the DBG_VALUE.
1694 if (DL->getScope() == PredDL->getScope())
1695 return false;
1696 auto *PredScope = LScopes.findLexicalScope(DL: PredDL);
1697 if (!PredScope || LScope->dominates(S: PredScope))
1698 return false;
1699 }
1700 }
1701
1702 // If the range of the DBG_VALUE is open-ended, report success.
1703 if (!RangeEnd)
1704 return true;
1705
1706 // Single, constant DBG_VALUEs in the prologue are promoted to be live
1707 // throughout the function. This is a hack, presumably for DWARF v2 and not
1708 // necessarily correct. It would be much better to use a dbg.declare instead
1709 // if we know the constant is live throughout the scope.
1710 if (MBB->pred_empty() &&
1711 all_of(Range: DbgValue->debug_operands(),
1712 P: [](const MachineOperand &Op) { return Op.isImm(); }))
1713 return true;
1714
1715 // Test if the location terminates before the end of the scope.
1716 const MachineInstr *LScopeEnd = LSRange.back().second;
1717 if (Ordering.isBefore(A: RangeEnd, B: LScopeEnd))
1718 return false;
1719
1720 // There's a single location which starts at the scope start, and ends at or
1721 // after the scope end.
1722 return true;
1723}
1724
1725/// Build the location list for all DBG_VALUEs in the function that
1726/// describe the same variable. The resulting DebugLocEntries will have
1727/// strict monotonically increasing begin addresses and will never
1728/// overlap. If the resulting list has only one entry that is valid
1729/// throughout variable's scope return true.
1730//
1731// See the definition of DbgValueHistoryMap::Entry for an explanation of the
1732// different kinds of history map entries. One thing to be aware of is that if
1733// a debug value is ended by another entry (rather than being valid until the
1734// end of the function), that entry's instruction may or may not be included in
1735// the range, depending on if the entry is a clobbering entry (it has an
1736// instruction that clobbers one or more preceding locations), or if it is an
1737// (overlapping) debug value entry. This distinction can be seen in the example
1738// below. The first debug value is ended by the clobbering entry 2, and the
1739// second and third debug values are ended by the overlapping debug value entry
1740// 4.
1741//
1742// Input:
1743//
1744// History map entries [type, end index, mi]
1745//
1746// 0 | [DbgValue, 2, DBG_VALUE $reg0, [...] (fragment 0, 32)]
1747// 1 | | [DbgValue, 4, DBG_VALUE $reg1, [...] (fragment 32, 32)]
1748// 2 | | [Clobber, $reg0 = [...], -, -]
1749// 3 | | [DbgValue, 4, DBG_VALUE 123, [...] (fragment 64, 32)]
1750// 4 [DbgValue, ~0, DBG_VALUE @g, [...] (fragment 0, 96)]
1751//
1752// Output [start, end) [Value...]:
1753//
1754// [0-1) [(reg0, fragment 0, 32)]
1755// [1-3) [(reg0, fragment 0, 32), (reg1, fragment 32, 32)]
1756// [3-4) [(reg1, fragment 32, 32), (123, fragment 64, 32)]
1757// [4-) [(@g, fragment 0, 96)]
1758bool DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
1759 const DbgValueHistoryMap::Entries &Entries) {
1760 using OpenRange =
1761 std::pair<DbgValueHistoryMap::EntryIndex, DbgValueLoc>;
1762 SmallVector<OpenRange, 4> OpenRanges;
1763 bool isSafeForSingleLocation = true;
1764 const MachineInstr *StartDebugMI = nullptr;
1765 const MachineInstr *EndMI = nullptr;
1766
1767 for (auto EB = Entries.begin(), EI = EB, EE = Entries.end(); EI != EE; ++EI) {
1768 const MachineInstr *Instr = EI->getInstr();
1769
1770 // Remove all values that are no longer live.
1771 size_t Index = std::distance(first: EB, last: EI);
1772 erase_if(C&: OpenRanges, P: [&](OpenRange &R) { return R.first <= Index; });
1773
1774 // If we are dealing with a clobbering entry, this iteration will result in
1775 // a location list entry starting after the clobbering instruction.
1776 const MCSymbol *StartLabel =
1777 EI->isClobber() ? getLabelAfterInsn(MI: Instr) : getLabelBeforeInsn(MI: Instr);
1778 assert(StartLabel &&
1779 "Forgot label before/after instruction starting a range!");
1780
1781 const MCSymbol *EndLabel;
1782 if (std::next(x: EI) == Entries.end()) {
1783 const MachineBasicBlock &EndMBB = Asm->MF->back();
1784 EndLabel = Asm->MBBSectionRanges[EndMBB.getSectionID()].EndLabel;
1785 if (EI->isClobber())
1786 EndMI = EI->getInstr();
1787 }
1788 else if (std::next(x: EI)->isClobber())
1789 EndLabel = getLabelAfterInsn(MI: std::next(x: EI)->getInstr());
1790 else
1791 EndLabel = getLabelBeforeInsn(MI: std::next(x: EI)->getInstr());
1792 assert(EndLabel && "Forgot label after instruction ending a range!");
1793
1794 if (EI->isDbgValue())
1795 LLVM_DEBUG(dbgs() << "DotDebugLoc: " << *Instr << "\n");
1796
1797 // If this history map entry has a debug value, add that to the list of
1798 // open ranges and check if its location is valid for a single value
1799 // location.
1800 if (EI->isDbgValue()) {
1801 // Do not add undef debug values, as they are redundant information in
1802 // the location list entries. An undef debug results in an empty location
1803 // description. If there are any non-undef fragments then padding pieces
1804 // with empty location descriptions will automatically be inserted, and if
1805 // all fragments are undef then the whole location list entry is
1806 // redundant.
1807 if (!Instr->isUndefDebugValue()) {
1808 auto Value = getDebugLocValue(MI: Instr);
1809 OpenRanges.emplace_back(Args: EI->getEndIndex(), Args&: Value);
1810
1811 // TODO: Add support for single value fragment locations.
1812 if (Instr->getDebugExpression()->isFragment())
1813 isSafeForSingleLocation = false;
1814
1815 if (!StartDebugMI)
1816 StartDebugMI = Instr;
1817 } else {
1818 isSafeForSingleLocation = false;
1819 }
1820 }
1821
1822 // Location list entries with empty location descriptions are redundant
1823 // information in DWARF, so do not emit those.
1824 if (OpenRanges.empty())
1825 continue;
1826
1827 // Omit entries with empty ranges as they do not have any effect in DWARF.
1828 if (StartLabel == EndLabel) {
1829 LLVM_DEBUG(dbgs() << "Omitting location list entry with empty range.\n");
1830 continue;
1831 }
1832
1833 SmallVector<DbgValueLoc, 4> Values;
1834 for (auto &R : OpenRanges)
1835 Values.push_back(Elt: R.second);
1836
1837 // With Basic block sections, it is posssible that the StartLabel and the
1838 // Instr are not in the same section. This happens when the StartLabel is
1839 // the function begin label and the dbg value appears in a basic block
1840 // that is not the entry. In this case, the range needs to be split to
1841 // span each individual section in the range from StartLabel to EndLabel.
1842 if (Asm->MF->hasBBSections() && StartLabel == Asm->getFunctionBegin() &&
1843 !Instr->getParent()->sameSection(MBB: &Asm->MF->front())) {
1844 for (const auto &[MBBSectionId, MBBSectionRange] :
1845 Asm->MBBSectionRanges) {
1846 if (Instr->getParent()->getSectionID() == MBBSectionId) {
1847 DebugLoc.emplace_back(Args: MBBSectionRange.BeginLabel, Args&: EndLabel, Args&: Values);
1848 break;
1849 }
1850 DebugLoc.emplace_back(Args: MBBSectionRange.BeginLabel,
1851 Args: MBBSectionRange.EndLabel, Args&: Values);
1852 }
1853 } else {
1854 DebugLoc.emplace_back(Args&: StartLabel, Args&: EndLabel, Args&: Values);
1855 }
1856
1857 // Attempt to coalesce the ranges of two otherwise identical
1858 // DebugLocEntries.
1859 auto CurEntry = DebugLoc.rbegin();
1860 LLVM_DEBUG({
1861 dbgs() << CurEntry->getValues().size() << " Values:\n";
1862 for (auto &Value : CurEntry->getValues())
1863 Value.dump();
1864 dbgs() << "-----\n";
1865 });
1866
1867 auto PrevEntry = std::next(x: CurEntry);
1868 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(Next: *CurEntry))
1869 DebugLoc.pop_back();
1870 }
1871
1872 if (!isSafeForSingleLocation ||
1873 !validThroughout(LScopes, DbgValue: StartDebugMI, RangeEnd: EndMI, Ordering: getInstOrdering()))
1874 return false;
1875
1876 if (DebugLoc.size() == 1)
1877 return true;
1878
1879 if (!Asm->MF->hasBBSections())
1880 return false;
1881
1882 // Check here to see if loclist can be merged into a single range. If not,
1883 // we must keep the split loclists per section. This does exactly what
1884 // MergeRanges does without sections. We don't actually merge the ranges
1885 // as the split ranges must be kept intact if this cannot be collapsed
1886 // into a single range.
1887 const MachineBasicBlock *RangeMBB = nullptr;
1888 if (DebugLoc[0].getBeginSym() == Asm->getFunctionBegin())
1889 RangeMBB = &Asm->MF->front();
1890 else
1891 RangeMBB = Entries.begin()->getInstr()->getParent();
1892 auto RangeIt = Asm->MBBSectionRanges.find(Key: RangeMBB->getSectionID());
1893 assert(RangeIt != Asm->MBBSectionRanges.end() &&
1894 "Range MBB not found in MBBSectionRanges!");
1895 auto *CurEntry = DebugLoc.begin();
1896 auto *NextEntry = std::next(x: CurEntry);
1897 auto NextRangeIt = std::next(x: RangeIt);
1898 while (NextEntry != DebugLoc.end()) {
1899 if (NextRangeIt == Asm->MBBSectionRanges.end())
1900 return false;
1901 // CurEntry should end the current section and NextEntry should start
1902 // the next section and the Values must match for these two ranges to be
1903 // merged. Do not match the section label end if it is the entry block
1904 // section. This is because the end label for the Debug Loc and the
1905 // Function end label could be different.
1906 if ((RangeIt->second.EndLabel != Asm->getFunctionEnd() &&
1907 CurEntry->getEndSym() != RangeIt->second.EndLabel) ||
1908 NextEntry->getBeginSym() != NextRangeIt->second.BeginLabel ||
1909 CurEntry->getValues() != NextEntry->getValues())
1910 return false;
1911 RangeIt = NextRangeIt;
1912 NextRangeIt = std::next(x: RangeIt);
1913 CurEntry = NextEntry;
1914 NextEntry = std::next(x: CurEntry);
1915 }
1916 return true;
1917}
1918
1919DbgEntity *DwarfDebug::createConcreteEntity(DwarfCompileUnit &TheCU,
1920 LexicalScope &Scope,
1921 const DINode *Node,
1922 const DILocation *Location,
1923 const MCSymbol *Sym) {
1924 ensureAbstractEntityIsCreatedIfScoped(CU&: TheCU, Node, ScopeNode: Scope.getScopeNode());
1925 if (isa<const DILocalVariable>(Val: Node)) {
1926 ConcreteEntities.push_back(
1927 Elt: std::make_unique<DbgVariable>(args: cast<const DILocalVariable>(Val: Node),
1928 args&: Location));
1929 InfoHolder.addScopeVariable(LS: &Scope,
1930 Var: cast<DbgVariable>(Val: ConcreteEntities.back().get()));
1931 } else if (isa<const DILabel>(Val: Node)) {
1932 ConcreteEntities.push_back(
1933 Elt: std::make_unique<DbgLabel>(args: cast<const DILabel>(Val: Node),
1934 args&: Location, args&: Sym));
1935 InfoHolder.addScopeLabel(LS: &Scope,
1936 Label: cast<DbgLabel>(Val: ConcreteEntities.back().get()));
1937 }
1938 return ConcreteEntities.back().get();
1939}
1940
1941// Find variables for each lexical scope.
1942void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU,
1943 const DISubprogram *SP,
1944 DenseSet<InlinedEntity> &Processed) {
1945 // Grab the variable info that was squirreled away in the MMI side-table.
1946 collectVariableInfoFromMFTable(TheCU, Processed);
1947
1948 for (const auto &I : DbgValues) {
1949 InlinedEntity IV = I.first;
1950 if (Processed.count(V: IV))
1951 continue;
1952
1953 // Instruction ranges, specifying where IV is accessible.
1954 const auto &HistoryMapEntries = I.second;
1955
1956 // Try to find any non-empty variable location. Do not create a concrete
1957 // entity if there are no locations.
1958 if (!DbgValues.hasNonEmptyLocation(Entries: HistoryMapEntries))
1959 continue;
1960
1961 LexicalScope *Scope = nullptr;
1962 const DILocalVariable *LocalVar = cast<DILocalVariable>(Val: IV.first);
1963 if (const DILocation *IA = IV.second)
1964 Scope = LScopes.findInlinedScope(N: LocalVar->getScope(), IA);
1965 else
1966 Scope = LScopes.findLexicalScope(N: LocalVar->getScope());
1967 // If variable scope is not found then skip this variable.
1968 if (!Scope)
1969 continue;
1970
1971 Processed.insert(V: IV);
1972 DbgVariable *RegVar = cast<DbgVariable>(Val: createConcreteEntity(TheCU,
1973 Scope&: *Scope, Node: LocalVar, Location: IV.second));
1974
1975 const MachineInstr *MInsn = HistoryMapEntries.front().getInstr();
1976 assert(MInsn->isDebugValue() && "History must begin with debug value");
1977
1978 // Check if there is a single DBG_VALUE, valid throughout the var's scope.
1979 // If the history map contains a single debug value, there may be an
1980 // additional entry which clobbers the debug value.
1981 size_t HistSize = HistoryMapEntries.size();
1982 bool SingleValueWithClobber =
1983 HistSize == 2 && HistoryMapEntries[1].isClobber();
1984 if (HistSize == 1 || SingleValueWithClobber) {
1985 const auto *End =
1986 SingleValueWithClobber ? HistoryMapEntries[1].getInstr() : nullptr;
1987 if (validThroughout(LScopes, DbgValue: MInsn, RangeEnd: End, Ordering: getInstOrdering())) {
1988 RegVar->emplace<Loc::Single>(args&: MInsn);
1989 continue;
1990 }
1991 }
1992
1993 // Handle multiple DBG_VALUE instructions describing one variable.
1994 DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar);
1995
1996 // Build the location list for this variable.
1997 SmallVector<DebugLocEntry, 8> Entries;
1998 bool isValidSingleLocation = buildLocationList(DebugLoc&: Entries, Entries: HistoryMapEntries);
1999
2000 // Check whether buildLocationList managed to merge all locations to one
2001 // that is valid throughout the variable's scope. If so, produce single
2002 // value location.
2003 if (isValidSingleLocation) {
2004 RegVar->emplace<Loc::Single>(args: Entries[0].getValues()[0]);
2005 continue;
2006 }
2007
2008 // If the variable has a DIBasicType, extract it. Basic types cannot have
2009 // unique identifiers, so don't bother resolving the type with the
2010 // identifier map.
2011 const DIBasicType *BT = dyn_cast<DIBasicType>(
2012 Val: static_cast<const Metadata *>(LocalVar->getType()));
2013
2014 // Finalize the entry by lowering it into a DWARF bytestream.
2015 for (auto &Entry : Entries)
2016 Entry.finalize(AP: *Asm, List, BT, TheCU);
2017 }
2018
2019 // For each InlinedEntity collected from DBG_LABEL instructions, convert to
2020 // DWARF-related DbgLabel.
2021 for (const auto &I : DbgLabels) {
2022 InlinedEntity IL = I.first;
2023 const MachineInstr *MI = I.second;
2024 if (MI == nullptr)
2025 continue;
2026
2027 LexicalScope *Scope = nullptr;
2028 const DILabel *Label = cast<DILabel>(Val: IL.first);
2029 // The scope could have an extra lexical block file.
2030 const DILocalScope *LocalScope =
2031 Label->getScope()->getNonLexicalBlockFileScope();
2032 // Get inlined DILocation if it is inlined label.
2033 if (const DILocation *IA = IL.second)
2034 Scope = LScopes.findInlinedScope(N: LocalScope, IA);
2035 else
2036 Scope = LScopes.findLexicalScope(N: LocalScope);
2037 // If label scope is not found then skip this label.
2038 if (!Scope)
2039 continue;
2040
2041 Processed.insert(V: IL);
2042 /// At this point, the temporary label is created.
2043 /// Save the temporary label to DbgLabel entity to get the
2044 /// actually address when generating Dwarf DIE.
2045 MCSymbol *Sym = getLabelBeforeInsn(MI);
2046 createConcreteEntity(TheCU, Scope&: *Scope, Node: Label, Location: IL.second, Sym);
2047 }
2048
2049 // Collect info for retained nodes.
2050 for (const DINode *DN : SP->getRetainedNodes()) {
2051 const auto *LS = getRetainedNodeScope(N: DN);
2052 if (isa<DILocalVariable>(Val: DN) || isa<DILabel>(Val: DN)) {
2053 if (!Processed.insert(V: InlinedEntity(DN, nullptr)).second)
2054 continue;
2055 LexicalScope *LexS = LScopes.findLexicalScope(N: LS);
2056 if (LexS)
2057 createConcreteEntity(TheCU, Scope&: *LexS, Node: DN, Location: nullptr);
2058 } else {
2059 LocalDeclsPerLS[LS].insert(X: DN);
2060 }
2061 }
2062}
2063
2064// Process beginning of an instruction.
2065void DwarfDebug::beginInstruction(const MachineInstr *MI) {
2066 const MachineFunction &MF = *MI->getMF();
2067 const auto *SP = MF.getFunction().getSubprogram();
2068 bool NoDebug =
2069 !SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug;
2070
2071 // Delay slot support check.
2072 auto delaySlotSupported = [](const MachineInstr &MI) {
2073 if (!MI.isBundledWithSucc())
2074 return false;
2075 auto Suc = std::next(x: MI.getIterator());
2076 (void)Suc;
2077 // Ensure that delay slot instruction is successor of the call instruction.
2078 // Ex. CALL_INSTRUCTION {
2079 // DELAY_SLOT_INSTRUCTION }
2080 assert(Suc->isBundledWithPred() &&
2081 "Call bundle instructions are out of order");
2082 return true;
2083 };
2084
2085 // When describing calls, we need a label for the call instruction.
2086 if (!NoDebug && SP->areAllCallsDescribed() &&
2087 MI->isCandidateForAdditionalCallInfo(Type: MachineInstr::AnyInBundle) &&
2088 (!MI->hasDelaySlot() || delaySlotSupported(*MI))) {
2089 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
2090 bool IsTail = TII->isTailCall(Inst: *MI);
2091 // For tail calls, we need the address of the branch instruction for
2092 // DW_AT_call_pc.
2093 if (IsTail)
2094 requestLabelBeforeInsn(MI);
2095 // For non-tail calls, we need the return address for the call for
2096 // DW_AT_call_return_pc. Under GDB tuning, this information is needed for
2097 // tail calls as well.
2098 requestLabelAfterInsn(MI);
2099 }
2100
2101 DebugHandlerBase::beginInstruction(MI);
2102 if (!CurMI)
2103 return;
2104
2105 if (NoDebug)
2106 return;
2107
2108 auto RecordLineZero = [&]() {
2109 // Preserve the file and column numbers, if we can, to save space in
2110 // the encoded line table.
2111 // Do not update PrevInstLoc, it remembers the last non-0 line.
2112 const MDNode *Scope = nullptr;
2113 unsigned Column = 0;
2114 if (PrevInstLoc) {
2115 Scope = PrevInstLoc.getScope();
2116 Column = PrevInstLoc.getCol();
2117 }
2118 recordSourceLine(/*Line=*/0, Col: Column, Scope, /*Flags=*/0);
2119 };
2120
2121 // When we emit a line-0 record, we don't update PrevInstLoc; so look at
2122 // the last line number actually emitted, to see if it was line 0.
2123 unsigned LastAsmLine =
2124 Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
2125
2126 // Check if source location changes, but ignore DBG_VALUE and CFI locations.
2127 // If the instruction is part of the function frame setup code, do not emit
2128 // any line record, as there is no correspondence with any user code.
2129 if (MI->isMetaInstruction())
2130 return;
2131 if (MI->getFlag(Flag: MachineInstr::FrameSetup)) {
2132 // Prevent a loc from the previous block leaking into frame setup instrs.
2133 if (LastAsmLine && PrevInstBB && PrevInstBB != MI->getParent())
2134 RecordLineZero();
2135 return;
2136 }
2137
2138 const DebugLoc &DL = MI->getDebugLoc();
2139 unsigned Flags = 0;
2140
2141 if (MI->getFlag(Flag: MachineInstr::FrameDestroy) && DL) {
2142 const MachineBasicBlock *MBB = MI->getParent();
2143 if (MBB && (MBB != EpilogBeginBlock)) {
2144 // First time FrameDestroy has been seen in this basic block
2145 EpilogBeginBlock = MBB;
2146 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2147 }
2148 }
2149
2150 auto RecordSourceLine = [this](auto &DL, auto Flags) {
2151 SmallString<128> LocationString;
2152 if (Asm->OutStreamer->isVerboseAsm()) {
2153 raw_svector_ostream OS(LocationString);
2154 DL.print(OS);
2155 }
2156 recordSourceLine(Line: DL.getLine(), Col: DL.getCol(), Scope: DL.getScope(), Flags,
2157 Location: LocationString);
2158 };
2159
2160 // There may be a mixture of scopes using and not using Key Instructions.
2161 // Not-Key-Instructions functions inlined into Key Instructions functions
2162 // should use not-key is_stmt handling. Key Instructions functions inlined
2163 // into Not-Key-Instructions functions should use Key Instructions is_stmt
2164 // handling.
2165 bool ScopeUsesKeyInstructions =
2166 KeyInstructionsAreStmts && DL &&
2167 DL->getScope()->getSubprogram()->getKeyInstructionsEnabled();
2168
2169 bool IsKey = false;
2170 if (ScopeUsesKeyInstructions && DL && DL.getLine())
2171 IsKey = KeyInstructions.contains(V: MI);
2172
2173 if (!DL && MI == PrologEndLoc) {
2174 // In rare situations, we might want to place the end of the prologue
2175 // somewhere that doesn't have a source location already. It should be in
2176 // the entry block.
2177 assert(MI->getParent() == &*MI->getMF()->begin());
2178 recordSourceLine(Line: SP->getScopeLine(), Col: 0, Scope: SP,
2179 DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT);
2180 return;
2181 }
2182
2183 bool PrevInstInSameSection =
2184 (!PrevInstBB ||
2185 PrevInstBB->getSectionID() == MI->getParent()->getSectionID());
2186 bool ForceIsStmt = ForceIsStmtInstrs.contains(V: MI);
2187 if (PrevInstInSameSection && !ForceIsStmt && DL.isSameSourceLocation(Other: PrevInstLoc)) {
2188 // If we have an ongoing unspecified location, nothing to do here.
2189 if (!DL)
2190 return;
2191
2192 // Skip this if the instruction is Key, else we might accidentally miss an
2193 // is_stmt.
2194 if (!IsKey) {
2195 // We have an explicit location, same as the previous location.
2196 // But we might be coming back to it after a line 0 record.
2197 if ((LastAsmLine == 0 && DL.getLine() != 0) || Flags) {
2198 // Reinstate the source location but not marked as a statement.
2199 RecordSourceLine(DL, Flags);
2200 }
2201 return;
2202 }
2203 }
2204
2205 if (!DL) {
2206 // FIXME: We could assert that `DL.getKind() != DebugLocKind::Temporary`
2207 // here, or otherwise record any temporary DebugLocs seen to ensure that
2208 // transient compiler-generated instructions aren't leaking their DLs to
2209 // other instructions.
2210 // We have an unspecified location, which might want to be line 0.
2211 // If we have already emitted a line-0 record, don't repeat it.
2212 if (LastAsmLine == 0)
2213 return;
2214 // If user said Don't Do That, don't do that.
2215 if (UnknownLocations == Disable)
2216 return;
2217 // See if we have a reason to emit a line-0 record now.
2218 // Reasons to emit a line-0 record include:
2219 // - User asked for it (UnknownLocations).
2220 // - Instruction has a label, so it's referenced from somewhere else,
2221 // possibly debug information; we want it to have a source location.
2222 // - Instruction is at the top of a block; we don't want to inherit the
2223 // location from the physically previous (maybe unrelated) block.
2224 if (UnknownLocations == Enable || PrevLabel ||
2225 (PrevInstBB && PrevInstBB != MI->getParent()))
2226 RecordLineZero();
2227 return;
2228 }
2229
2230 // We have an explicit location, different from the previous location.
2231 // Don't repeat a line-0 record, but otherwise emit the new location.
2232 // (The new location might be an explicit line 0, which we do emit.)
2233 if (DL.getLine() == 0 && LastAsmLine == 0)
2234 return;
2235 if (MI == PrologEndLoc) {
2236 Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT;
2237 PrologEndLoc = nullptr;
2238 }
2239
2240 if (ScopeUsesKeyInstructions) {
2241 if (IsKey)
2242 Flags |= DWARF2_FLAG_IS_STMT;
2243 } else {
2244 // If the line changed, we call that a new statement; unless we went to
2245 // line 0 and came back, in which case it is not a new statement.
2246 unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
2247 if (DL.getLine() && (DL.getLine() != OldLine || ForceIsStmt))
2248 Flags |= DWARF2_FLAG_IS_STMT;
2249 }
2250
2251 // Call target-specific source line recording.
2252 recordTargetSourceLine(DL, Flags);
2253
2254 // If we're not at line 0, remember this location.
2255 if (DL.getLine())
2256 PrevInstLoc = DL;
2257}
2258
2259/// Default implementation of target-specific source line recording.
2260void DwarfDebug::recordTargetSourceLine(const DebugLoc &DL, unsigned Flags) {
2261 SmallString<128> LocationString;
2262 if (Asm->OutStreamer->isVerboseAsm()) {
2263 raw_svector_ostream OS(LocationString);
2264 DL.print(OS);
2265 }
2266 recordSourceLine(Line: DL.getLine(), Col: DL.getCol(), Scope: DL.getScope(), Flags,
2267 Location: LocationString);
2268}
2269
2270// Returns the position where we should place prologue_end, potentially nullptr,
2271// which means "no good place to put prologue_end". Returns true in the second
2272// return value if there are no setup instructions in this function at all,
2273// meaning we should not emit a start-of-function linetable entry, because it
2274// would be zero-lengthed.
2275static std::pair<const MachineInstr *, bool>
2276findPrologueEndLoc(const MachineFunction *MF) {
2277 // First known non-DBG_VALUE and non-frame setup location marks
2278 // the beginning of the function body.
2279 const auto &TII = *MF->getSubtarget().getInstrInfo();
2280 const MachineInstr *NonTrivialInst = nullptr;
2281 const Function &F = MF->getFunction();
2282 DISubprogram *SP = const_cast<DISubprogram *>(F.getSubprogram());
2283
2284 // Some instructions may be inserted into prologue after this function. Must
2285 // keep prologue for these cases.
2286 bool IsEmptyPrologue =
2287 !(F.hasPrologueData() || F.getMetadata(KindID: LLVMContext::MD_func_sanitize));
2288
2289 // Helper lambda to examine each instruction and potentially return it
2290 // as the prologue_end point.
2291 auto ExamineInst = [&](const MachineInstr &MI)
2292 -> std::optional<std::pair<const MachineInstr *, bool>> {
2293 // Is this instruction trivial data shuffling or frame-setup?
2294 bool isCopy = (TII.isCopyInstr(MI) ? true : false);
2295 bool isTrivRemat = TII.isTriviallyReMaterializable(MI);
2296 bool isFrameSetup = MI.getFlag(Flag: MachineInstr::FrameSetup);
2297
2298 if (!isFrameSetup && MI.getDebugLoc()) {
2299 // Scan forward to try to find a non-zero line number. The
2300 // prologue_end marks the first breakpoint in the function after the
2301 // frame setup, and a compiler-generated line 0 location is not a
2302 // meaningful breakpoint. If none is found, return the first
2303 // location after the frame setup.
2304 if (MI.getDebugLoc().getLine())
2305 return std::make_pair(x: &MI, y&: IsEmptyPrologue);
2306 }
2307
2308 // Keep track of the first "non-trivial" instruction seen, i.e. anything
2309 // that doesn't involve shuffling data around or is a frame-setup.
2310 if (!isCopy && !isTrivRemat && !isFrameSetup && !NonTrivialInst)
2311 NonTrivialInst = &MI;
2312
2313 IsEmptyPrologue = false;
2314 return std::nullopt;
2315 };
2316
2317 // Examine all the instructions at the start of the function. This doesn't
2318 // necessarily mean just the entry block: unoptimised code can fall-through
2319 // into an initial loop, and it makes sense to put the initial breakpoint on
2320 // the first instruction of such a loop. However, if we pass branches, we're
2321 // better off synthesising an early prologue_end.
2322 auto CurBlock = MF->begin();
2323 auto CurInst = CurBlock->begin();
2324
2325 // Find the initial instruction, we're guaranteed one by the caller, but not
2326 // which block it's in.
2327 while (CurBlock->empty())
2328 CurInst = (++CurBlock)->begin();
2329 assert(CurInst != CurBlock->end());
2330
2331 // Helper function for stepping through the initial sequence of
2332 // unconditionally executed instructions.
2333 auto getNextInst = [&CurBlock, &CurInst, MF]() -> bool {
2334 // We've reached the end of the block. Did we just look at a terminator?
2335 if (CurInst->isTerminator()) {
2336 // Some kind of "real" control flow is occurring. At the very least
2337 // we would have to start exploring the CFG, a good signal that the
2338 // prologue is over.
2339 return false;
2340 }
2341
2342 // If we've already fallen through into a loop, don't fall through
2343 // further, use a backup-location.
2344 if (CurBlock->pred_size() > 1)
2345 return false;
2346
2347 // Fall-through from entry to the next block. This is common at -O0 when
2348 // there's no initialisation in the function. Bail if we're also at the
2349 // end of the function, or the remaining blocks have no instructions.
2350 // Skip empty blocks, in rare cases the entry can be empty, and
2351 // other optimisations may add empty blocks that the control flow falls
2352 // through.
2353 do {
2354 ++CurBlock;
2355 if (CurBlock == MF->end())
2356 return false;
2357 } while (CurBlock->empty());
2358 CurInst = CurBlock->begin();
2359 return true;
2360 };
2361
2362 while (true) {
2363 // Check whether this non-meta instruction a good position for prologue_end.
2364 if (!CurInst->isMetaInstruction()) {
2365 auto FoundInst = ExamineInst(*CurInst);
2366 if (FoundInst)
2367 return *FoundInst;
2368 }
2369
2370 // In very rare scenarios function calls can have line zero, and we
2371 // shouldn't step over such a call while trying to reach prologue_end. In
2372 // these extraordinary conditions, force the call to have the scope line
2373 // and put prologue_end there. This isn't ideal, but signals that the call
2374 // is where execution in the function starts, and is less catastrophic than
2375 // stepping over the call.
2376 if (CurInst->isCall()) {
2377 if (const DILocation *Loc = CurInst->getDebugLoc().get();
2378 Loc && Loc->getLine() == 0) {
2379 // Create and assign the scope-line position.
2380 unsigned ScopeLine = SP->getScopeLine();
2381 DILocation *ScopeLineDILoc =
2382 DILocation::get(Context&: SP->getContext(), Line: ScopeLine, Column: 0, Scope: SP);
2383 const_cast<MachineInstr *>(&*CurInst)->setDebugLoc(ScopeLineDILoc);
2384
2385 // Consider this position to be where prologue_end is placed.
2386 return std::make_pair(x: &*CurInst, y: false);
2387 }
2388 }
2389
2390 // Try to continue searching, but use a backup-location if substantive
2391 // computation is happening.
2392 auto NextInst = std::next(x: CurInst);
2393 if (NextInst != CurInst->getParent()->end()) {
2394 // Continue examining the current block.
2395 CurInst = NextInst;
2396 continue;
2397 }
2398
2399 if (!getNextInst())
2400 break;
2401 }
2402
2403 // We couldn't find any source-location, suggesting all meaningful information
2404 // got optimised away. Set the prologue_end to be the first non-trivial
2405 // instruction, which will get the scope line number. This is better than
2406 // nothing.
2407 // Only do this in the entry block, as we'll be giving it the scope line for
2408 // the function. Return IsEmptyPrologue==true if we've picked the first
2409 // instruction.
2410 if (NonTrivialInst && NonTrivialInst->getParent() == &*MF->begin()) {
2411 IsEmptyPrologue = NonTrivialInst == &*MF->begin()->begin();
2412 return std::make_pair(x&: NonTrivialInst, y&: IsEmptyPrologue);
2413 }
2414
2415 // If the entry path is empty, just don't have a prologue_end at all.
2416 return std::make_pair(x: nullptr, y&: IsEmptyPrologue);
2417}
2418
2419/// Register a source line with debug info. Returns the unique label that was
2420/// emitted and which provides correspondence to the source line list.
2421static void recordSourceLine(AsmPrinter &Asm, unsigned Line, unsigned Col,
2422 const MDNode *S, unsigned Flags, unsigned CUID,
2423 uint16_t DwarfVersion,
2424 ArrayRef<std::unique_ptr<DwarfCompileUnit>> DCUs,
2425 StringRef Comment = {}) {
2426 StringRef Fn;
2427 unsigned FileNo = 1;
2428 unsigned Discriminator = 0;
2429 if (auto *Scope = cast_or_null<DIScope>(Val: S)) {
2430 Fn = Scope->getFilename();
2431 if (Line != 0 && DwarfVersion >= 4)
2432 if (auto *LBF = dyn_cast<DILexicalBlockFile>(Val: Scope))
2433 Discriminator = LBF->getDiscriminator();
2434
2435 FileNo = static_cast<DwarfCompileUnit &>(*DCUs[CUID])
2436 .getOrCreateSourceID(File: Scope->getFile());
2437 }
2438 Asm.OutStreamer->emitDwarfLocDirective(FileNo, Line, Column: Col, Flags, Isa: 0,
2439 Discriminator, FileName: Fn, Comment);
2440}
2441
2442const MachineInstr *
2443DwarfDebug::emitInitialLocDirective(const MachineFunction &MF, unsigned CUID) {
2444 // Don't deal with functions that have no instructions.
2445 if (llvm::all_of(Range: MF, P: [](const MachineBasicBlock &MBB) { return MBB.empty(); }))
2446 return nullptr;
2447
2448 std::pair<const MachineInstr *, bool> PrologEnd = findPrologueEndLoc(MF: &MF);
2449 const MachineInstr *PrologEndLoc = PrologEnd.first;
2450 bool IsEmptyPrologue = PrologEnd.second;
2451
2452 // If the prolog is empty, no need to generate scope line for the proc.
2453 if (IsEmptyPrologue) {
2454 // If there's nowhere to put a prologue_end flag, emit a scope line in case
2455 // there are simply no source locations anywhere in the function.
2456 if (PrologEndLoc) {
2457 // Avoid trying to assign prologue_end to a line-zero location.
2458 // Instructions with no DebugLoc at all are fine, they'll be given the
2459 // scope line nuumber.
2460 const DebugLoc &DL = PrologEndLoc->getDebugLoc();
2461 if (!DL || DL->getLine() != 0)
2462 return PrologEndLoc;
2463
2464 // Later, don't place the prologue_end flag on this line-zero location.
2465 PrologEndLoc = nullptr;
2466 }
2467 }
2468
2469 // Ensure the compile unit is created if the function is called before
2470 // beginFunction().
2471 DISubprogram *SP = MF.getFunction().getSubprogram();
2472 (void)getOrCreateDwarfCompileUnit(DIUnit: SP->getUnit());
2473 // We'd like to list the prologue as "not statements" but GDB behaves
2474 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
2475 ::recordSourceLine(Asm&: *Asm, Line: SP->getScopeLine(), Col: 0, S: SP, DWARF2_FLAG_IS_STMT,
2476 CUID, DwarfVersion: getDwarfVersion(), DCUs: getUnits());
2477 return PrologEndLoc;
2478}
2479
2480void DwarfDebug::computeKeyInstructions(const MachineFunction *MF) {
2481 // New function - reset KeyInstructions.
2482 KeyInstructions.clear();
2483
2484 // The current candidate is_stmt instructions for each source atom.
2485 // Map {(InlinedAt, Group): (Rank, Instructions)}.
2486 // NOTE: Anecdotally, for a large C++ blob, 99% of the instruction
2487 // SmallVectors contain 2 or fewer elements; use 2 inline elements.
2488 DenseMap<std::pair<DILocation *, uint64_t>,
2489 std::pair<uint8_t, SmallVector<const MachineInstr *, 2>>>
2490 GroupCandidates;
2491
2492 const auto &TII = *MF->getSubtarget().getInstrInfo();
2493
2494 // For each instruction:
2495 // * Skip insts without DebugLoc, AtomGroup or AtomRank, and line zeros.
2496 // * Check if insts in this group have been seen already in GroupCandidates.
2497 // * If this instr rank is equal, add this instruction to GroupCandidates.
2498 // Remove existing instructions from GroupCandidates if they have the
2499 // same parent.
2500 // * If this instr rank is higher (lower precedence), ignore it.
2501 // * If this instr rank is lower (higher precedence), erase existing
2502 // instructions from GroupCandidates and add this one.
2503 //
2504 // Then insert each GroupCandidates instruction into KeyInstructions.
2505
2506 for (auto &MBB : *MF) {
2507 // Rather than apply is_stmt directly to Key Instructions, we "float"
2508 // is_stmt up to the 1st instruction with the same line number in a
2509 // contiguous block. That instruction is called the "buoy". The
2510 // buoy gets reset if we encouner an instruction with an atom
2511 // group.
2512 const MachineInstr *Buoy = nullptr;
2513 // The atom group number associated with Buoy which may be 0 if we haven't
2514 // encountered an atom group yet in this blob of instructions with the same
2515 // line number.
2516 uint64_t BuoyAtom = 0;
2517
2518 for (auto &MI : MBB) {
2519 if (MI.isMetaInstruction())
2520 continue;
2521
2522 const DILocation *Loc = MI.getDebugLoc().get();
2523 if (!Loc || !Loc->getLine())
2524 continue;
2525
2526 // Reset the Buoy to this instruction if it has a different line number.
2527 if (!Buoy || Buoy->getDebugLoc().getLine() != Loc->getLine()) {
2528 Buoy = &MI;
2529 BuoyAtom = 0; // Set later when we know which atom the buoy is used by.
2530 }
2531
2532 // Call instructions are handled specially - we always mark them as key
2533 // regardless of atom info.
2534 bool IsCallLike = MI.isCall() || TII.isTailCall(Inst: MI);
2535 if (IsCallLike) {
2536 // Calls are always key. Put the buoy (may not be the call) into
2537 // KeyInstructions directly rather than the candidate map to avoid it
2538 // being erased (and we may not have a group number for the call).
2539 KeyInstructions.insert(V: Buoy);
2540
2541 // Avoid floating any future is_stmts up to the call.
2542 Buoy = nullptr;
2543 BuoyAtom = 0;
2544
2545 if (!Loc->getAtomGroup() || !Loc->getAtomRank())
2546 continue;
2547 }
2548
2549 auto *InlinedAt = Loc->getInlinedAt();
2550 uint64_t Group = Loc->getAtomGroup();
2551 uint8_t Rank = Loc->getAtomRank();
2552 if (!Group || !Rank)
2553 continue;
2554
2555 // Don't let is_stmts float past instructions from different source atoms.
2556 if (BuoyAtom && BuoyAtom != Group) {
2557 Buoy = &MI;
2558 BuoyAtom = Group;
2559 }
2560
2561 auto &[CandidateRank, CandidateInsts] =
2562 GroupCandidates[{InlinedAt, Group}];
2563
2564 // If CandidateRank is zero then CandidateInsts should be empty: there
2565 // are no other candidates for this group yet. If CandidateRank is nonzero
2566 // then CandidateInsts shouldn't be empty: we've got existing candidate
2567 // instructions.
2568 assert((CandidateRank == 0 && CandidateInsts.empty()) ||
2569 (CandidateRank != 0 && !CandidateInsts.empty()));
2570
2571 assert(Rank && "expected nonzero rank");
2572 // If we've seen other instructions in this group with higher precedence
2573 // (lower nonzero rank), don't add this one as a candidate.
2574 if (CandidateRank && CandidateRank < Rank)
2575 continue;
2576
2577 // If we've seen other instructions in this group of the same rank,
2578 // discard any from this block (keeping the others). Else if we've
2579 // seen other instructions in this group of lower precedence (higher
2580 // rank), discard them all.
2581 if (CandidateRank == Rank)
2582 llvm::remove_if(Range&: CandidateInsts, P: [&MI](const MachineInstr *Candidate) {
2583 return MI.getParent() == Candidate->getParent();
2584 });
2585 else if (CandidateRank > Rank)
2586 CandidateInsts.clear();
2587
2588 if (Buoy) {
2589 // Add this candidate.
2590 CandidateInsts.push_back(Elt: Buoy);
2591 CandidateRank = Rank;
2592
2593 assert(!BuoyAtom || BuoyAtom == Loc->getAtomGroup());
2594 BuoyAtom = Loc->getAtomGroup();
2595 } else {
2596 // Don't add calls, because they've been dealt with already. This means
2597 // CandidateInsts might now be empty - handle that.
2598 assert(IsCallLike);
2599 if (CandidateInsts.empty())
2600 CandidateRank = 0;
2601 }
2602 }
2603 }
2604
2605 for (const auto &[_, Insts] : GroupCandidates.values())
2606 for (auto *I : Insts)
2607 KeyInstructions.insert(V: I);
2608}
2609
2610/// For the function \p MF, finds the set of instructions which may represent a
2611/// change in line number from one or more of the preceding MBBs. Stores the
2612/// resulting set of instructions, which should have is_stmt set, in
2613/// ForceIsStmtInstrs.
2614void DwarfDebug::findForceIsStmtInstrs(const MachineFunction *MF) {
2615 ForceIsStmtInstrs.clear();
2616
2617 // For this function, we try to find MBBs where the last source line in every
2618 // block predecessor matches the first line seen in the block itself; for
2619 // every such MBB, we set is_stmt=false on the first line in the block, and
2620 // for every other block we set is_stmt=true on the first line.
2621 // For example, if we have the block %bb.3, which has 2 predecesors %bb.1 and
2622 // %bb.2:
2623 // bb.1:
2624 // $r3 = MOV64ri 12, debug-location !DILocation(line: 4)
2625 // JMP %bb.3, debug-location !DILocation(line: 5)
2626 // bb.2:
2627 // $r3 = MOV64ri 24, debug-location !DILocation(line: 5)
2628 // JMP %bb.3
2629 // bb.3:
2630 // $r2 = MOV64ri 1
2631 // $r1 = ADD $r2, $r3, debug-location !DILocation(line: 5)
2632 // When we examine %bb.3, we first check to see if it contains any
2633 // instructions with debug locations, and select the first such instruction;
2634 // in this case, the ADD, with line=5. We then examine both of its
2635 // predecessors to see what the last debug-location in them is. For each
2636 // predecessor, if they do not contain any debug-locations, or if the last
2637 // debug-location before jumping to %bb.3 does not have line=5, then the ADD
2638 // in %bb.3 must use IsStmt. In this case, all predecessors have a
2639 // debug-location with line=5 as the last debug-location before jumping to
2640 // %bb.3, so we do not set is_stmt for the ADD instruction - we know that
2641 // whichever MBB we have arrived from, the line has not changed.
2642
2643 const auto *TII = MF->getSubtarget().getInstrInfo();
2644
2645 // We only need to the predecessors of MBBs that could have is_stmt set by
2646 // this logic.
2647 SmallDenseSet<MachineBasicBlock *, 4> PredMBBsToExamine;
2648 SmallDenseMap<MachineBasicBlock *, MachineInstr *> PotentialIsStmtMBBInstrs;
2649 // We use const_cast even though we won't actually modify MF, because some
2650 // methods we need take a non-const MBB.
2651 for (auto &MBB : *const_cast<MachineFunction *>(MF)) {
2652 if (MBB.empty() || MBB.pred_empty())
2653 continue;
2654 for (auto &MI : MBB) {
2655 if (MI.getDebugLoc() && MI.getDebugLoc()->getLine()) {
2656 PredMBBsToExamine.insert_range(R: MBB.predecessors());
2657 PotentialIsStmtMBBInstrs.insert(KV: {&MBB, &MI});
2658 break;
2659 }
2660 }
2661 }
2662
2663 // For each predecessor MBB, we examine the last line seen before each branch
2664 // or logical fallthrough. We use analyzeBranch to handle cases where
2665 // different branches have different outgoing lines (i.e. if there are
2666 // multiple branches that each have their own source location); otherwise we
2667 // just use the last line in the block.
2668 for (auto *MBB : PredMBBsToExamine) {
2669 auto CheckMBBEdge = [&](MachineBasicBlock *Succ, unsigned OutgoingLine) {
2670 auto MBBInstrIt = PotentialIsStmtMBBInstrs.find(Val: Succ);
2671 if (MBBInstrIt == PotentialIsStmtMBBInstrs.end())
2672 return;
2673 MachineInstr *MI = MBBInstrIt->second;
2674 if (MI->getDebugLoc()->getLine() == OutgoingLine)
2675 return;
2676 PotentialIsStmtMBBInstrs.erase(I: MBBInstrIt);
2677 ForceIsStmtInstrs.insert(V: MI);
2678 };
2679 // If this block is empty, we conservatively assume that its fallthrough
2680 // successor needs is_stmt; we could check MBB's predecessors to see if it
2681 // has a consistent entry line, but this seems unlikely to be worthwhile.
2682 if (MBB->empty()) {
2683 for (auto *Succ : MBB->successors())
2684 CheckMBBEdge(Succ, 0);
2685 continue;
2686 }
2687 // If MBB has no successors that are in the "potential" set, due to one or
2688 // more of them having confirmed is_stmt, we can skip this check early.
2689 if (none_of(Range: MBB->successors(), P: [&](auto *SuccMBB) {
2690 return PotentialIsStmtMBBInstrs.contains(Val: SuccMBB);
2691 }))
2692 continue;
2693 // If we can't determine what DLs this branch's successors use, just treat
2694 // all the successors as coming from the last DebugLoc.
2695 SmallVector<MachineBasicBlock *, 2> SuccessorBBs;
2696 auto MIIt = MBB->rbegin();
2697 {
2698 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
2699 SmallVector<MachineOperand, 4> Cond;
2700 bool AnalyzeFailed = TII->analyzeBranch(MBB&: *MBB, TBB, FBB, Cond);
2701 // For a conditional branch followed by unconditional branch where the
2702 // unconditional branch has a DebugLoc, that loc is the outgoing loc to
2703 // the the false destination only; otherwise, both destinations share an
2704 // outgoing loc.
2705 if (!AnalyzeFailed && !Cond.empty() && FBB != nullptr &&
2706 MBB->back().getDebugLoc() && MBB->back().getDebugLoc()->getLine()) {
2707 unsigned FBBLine = MBB->back().getDebugLoc()->getLine();
2708 assert(MIIt->isBranch() && "Bad result from analyzeBranch?");
2709 CheckMBBEdge(FBB, FBBLine);
2710 ++MIIt;
2711 SuccessorBBs.push_back(Elt: TBB);
2712 } else {
2713 // For all other cases, all successors share the last outgoing DebugLoc.
2714 SuccessorBBs.assign(in_start: MBB->succ_begin(), in_end: MBB->succ_end());
2715 }
2716 }
2717
2718 // If we don't find an outgoing loc, this block will start with a line 0.
2719 // It is possible that we have a block that has no DebugLoc, but acts as a
2720 // simple passthrough between two blocks that end and start with the same
2721 // line, e.g.:
2722 // bb.1:
2723 // JMP %bb.2, debug-location !10
2724 // bb.2:
2725 // JMP %bb.3
2726 // bb.3:
2727 // $r1 = ADD $r2, $r3, debug-location !10
2728 // If these blocks were merged into a single block, we would not attach
2729 // is_stmt to the ADD, but with this logic that only checks the immediate
2730 // predecessor, we will; we make this tradeoff because doing a full dataflow
2731 // analysis would be expensive, and these situations are probably not common
2732 // enough for this to be worthwhile.
2733 unsigned LastLine = 0;
2734 while (MIIt != MBB->rend()) {
2735 if (auto DL = MIIt->getDebugLoc(); DL && DL->getLine()) {
2736 LastLine = DL->getLine();
2737 break;
2738 }
2739 ++MIIt;
2740 }
2741 for (auto *Succ : SuccessorBBs)
2742 CheckMBBEdge(Succ, LastLine);
2743 }
2744}
2745
2746// Gather pre-function debug information. Assumes being called immediately
2747// after the function entry point has been emitted.
2748void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) {
2749 CurFn = MF;
2750
2751 auto *SP = MF->getFunction().getSubprogram();
2752 assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode());
2753 if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
2754 return;
2755
2756 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(DIUnit: SP->getUnit());
2757 FunctionLineTableLabel = CU.emitFuncLineTableOffsets()
2758 ? Asm->OutStreamer->emitLineTableLabel()
2759 : nullptr;
2760
2761 Asm->OutStreamer->getContext().setDwarfCompileUnitID(
2762 getDwarfCompileUnitIDForLineTable(CU));
2763
2764 // Call target-specific debug info initialization.
2765 initializeTargetDebugInfo(MF: *MF);
2766
2767 // Record beginning of function.
2768 PrologEndLoc = emitInitialLocDirective(
2769 MF: *MF, CUID: Asm->OutStreamer->getContext().getDwarfCompileUnitID());
2770
2771 // Run both `findForceIsStmtInstrs` and `computeKeyInstructions` because
2772 // Not-Key-Instructions functions may be inlined into Key Instructions
2773 // functions and vice versa.
2774 if (KeyInstructionsAreStmts)
2775 computeKeyInstructions(MF);
2776 findForceIsStmtInstrs(MF);
2777}
2778
2779unsigned
2780DwarfDebug::getDwarfCompileUnitIDForLineTable(const DwarfCompileUnit &CU) {
2781 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
2782 // belongs to so that we add to the correct per-cu line table in the
2783 // non-asm case.
2784 if (Asm->OutStreamer->hasRawTextSupport())
2785 // Use a single line table if we are generating assembly.
2786 return 0;
2787 else
2788 return CU.getUniqueID();
2789}
2790
2791void DwarfDebug::terminateLineTable(const DwarfCompileUnit *CU) {
2792 const auto &CURanges = CU->getRanges();
2793 auto &LineTable = Asm->OutStreamer->getContext().getMCDwarfLineTable(
2794 CUID: getDwarfCompileUnitIDForLineTable(CU: *CU));
2795 // Add the last range label for the given CU.
2796 LineTable.getMCLineSections().addEndEntry(
2797 EndLabel: const_cast<MCSymbol *>(CURanges.back().End));
2798}
2799
2800void DwarfDebug::skippedNonDebugFunction() {
2801 // If we don't have a subprogram for this function then there will be a hole
2802 // in the range information. Keep note of this by setting the previously used
2803 // section to nullptr.
2804 // Terminate the pending line table.
2805 if (PrevCU)
2806 terminateLineTable(CU: PrevCU);
2807 PrevCU = nullptr;
2808 CurFn = nullptr;
2809}
2810
2811// Gather and emit post-function debug information.
2812void DwarfDebug::endFunctionImpl(const MachineFunction *MF) {
2813 const Function &F = MF->getFunction();
2814 const DISubprogram *SP = F.getSubprogram();
2815
2816 assert(CurFn == MF &&
2817 "endFunction should be called with the same function as beginFunction");
2818
2819 // Set DwarfDwarfCompileUnitID in MCContext to default value.
2820 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
2821
2822 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
2823 assert(!FnScope || SP == FnScope->getScopeNode());
2824 DwarfCompileUnit &TheCU = getOrCreateDwarfCompileUnit(DIUnit: SP->getUnit());
2825 if (TheCU.getCUNode()->isDebugDirectivesOnly()) {
2826 PrevLabel = nullptr;
2827 CurFn = nullptr;
2828 return;
2829 }
2830
2831 DenseSet<InlinedEntity> Processed;
2832 collectEntityInfo(TheCU, SP, Processed);
2833
2834 // Add the range of this function to the list of ranges for the CU.
2835 // With basic block sections, add ranges for all basic block sections.
2836 for (const auto &R : Asm->MBBSectionRanges)
2837 TheCU.addRange(Range: {.Begin: R.second.BeginLabel, .End: R.second.EndLabel});
2838
2839 // Under -gmlt, skip building the subprogram if there are no inlined
2840 // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram
2841 // is still needed as we need its source location.
2842 if (!TheCU.getCUNode()->getDebugInfoForProfiling() &&
2843 TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly &&
2844 LScopes.getAbstractScopesList().empty() && !IsDarwin) {
2845 for (const auto &R : Asm->MBBSectionRanges)
2846 addArangeLabel(SCU: SymbolCU(&TheCU, R.second.BeginLabel));
2847
2848 assert(InfoHolder.getScopeVariables().empty());
2849 PrevLabel = nullptr;
2850 CurFn = nullptr;
2851 return;
2852 }
2853
2854#ifndef NDEBUG
2855 size_t NumAbstractSubprograms = LScopes.getAbstractScopesList().size();
2856#endif
2857 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
2858 const auto *SP = cast<DISubprogram>(Val: AScope->getScopeNode());
2859 for (const DINode *DN : SP->getRetainedNodes()) {
2860 const auto *LS = getRetainedNodeScope(N: DN);
2861 // Ensure LexicalScope is created for the scope of this node.
2862 auto *LexS = LScopes.getOrCreateAbstractScope(Scope: LS);
2863 assert(LexS && "Expected the LexicalScope to be created.");
2864 if (isa<DILocalVariable>(Val: DN) || isa<DILabel>(Val: DN)) {
2865 // Collect info for variables/labels that were optimized out.
2866 if (!Processed.insert(V: InlinedEntity(DN, nullptr)).second ||
2867 TheCU.getExistingAbstractEntity(Node: DN))
2868 continue;
2869 TheCU.createAbstractEntity(Node: DN, Scope: LexS);
2870 } else {
2871 // Remember the node if this is a local declarations.
2872 LocalDeclsPerLS[LS].insert(X: DN);
2873 }
2874 assert(
2875 LScopes.getAbstractScopesList().size() == NumAbstractSubprograms &&
2876 "getOrCreateAbstractScope() inserted an abstract subprogram scope");
2877 }
2878 constructAbstractSubprogramScopeDIE(SrcCU&: TheCU, Scope: AScope);
2879 }
2880
2881 ProcessedSPNodes.insert(X: SP);
2882 DIE &ScopeDIE =
2883 TheCU.constructSubprogramScopeDIE(Sub: SP, F, Scope: FnScope, LineTableSym: FunctionLineTableLabel);
2884 if (auto *SkelCU = TheCU.getSkeleton())
2885 if (!LScopes.getAbstractScopesList().empty() &&
2886 TheCU.getCUNode()->getSplitDebugInlining())
2887 SkelCU->constructSubprogramScopeDIE(Sub: SP, F, Scope: FnScope,
2888 LineTableSym: FunctionLineTableLabel);
2889
2890 FunctionLineTableLabel = nullptr;
2891
2892 // Construct call site entries.
2893 constructCallSiteEntryDIEs(SP: *SP, CU&: TheCU, ScopeDIE, MF: *MF);
2894
2895 // Clear debug info
2896 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
2897 // DbgVariables except those that are also in AbstractVariables (since they
2898 // can be used cross-function)
2899 InfoHolder.getScopeVariables().clear();
2900 InfoHolder.getScopeLabels().clear();
2901 LocalDeclsPerLS.clear();
2902 PrevLabel = nullptr;
2903 CurFn = nullptr;
2904}
2905
2906// Register a source line with debug info. Returns the unique label that was
2907// emitted and which provides correspondence to the source line list.
2908void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
2909 unsigned Flags, StringRef Location) {
2910 ::recordSourceLine(Asm&: *Asm, Line, Col, S, Flags,
2911 CUID: Asm->OutStreamer->getContext().getDwarfCompileUnitID(),
2912 DwarfVersion: getDwarfVersion(), DCUs: getUnits(), Comment: Location);
2913}
2914
2915//===----------------------------------------------------------------------===//
2916// Emit Methods
2917//===----------------------------------------------------------------------===//
2918
2919// Emit the debug info section.
2920void DwarfDebug::emitDebugInfo() {
2921 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2922 Holder.emitUnits(/* UseOffsets */ false);
2923}
2924
2925// Emit the abbreviation section.
2926void DwarfDebug::emitAbbreviations() {
2927 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2928
2929 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
2930}
2931
2932void DwarfDebug::emitStringOffsetsTableHeader() {
2933 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2934 Holder.getStringPool().emitStringOffsetsTableHeader(
2935 Asm&: *Asm, OffsetSection: Asm->getObjFileLowering().getDwarfStrOffSection(),
2936 StartSym: Holder.getStringOffsetsStartSym());
2937}
2938
2939template <typename AccelTableT>
2940void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section,
2941 StringRef TableName) {
2942 Asm->OutStreamer->switchSection(Section);
2943
2944 // Emit the full data.
2945 emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol());
2946}
2947
2948void DwarfDebug::emitAccelDebugNames() {
2949 // Don't emit anything if we have no compilation units to index.
2950 if (getUnits().empty())
2951 return;
2952
2953 emitDWARF5AccelTable(Asm, Contents&: AccelDebugNames, DD: *this, CUs: getUnits());
2954}
2955
2956// Emit visible names into a hashed accelerator table section.
2957void DwarfDebug::emitAccelNames() {
2958 emitAccel(Accel&: AccelNames, Section: Asm->getObjFileLowering().getDwarfAccelNamesSection(),
2959 TableName: "Names");
2960}
2961
2962// Emit objective C classes and categories into a hashed accelerator table
2963// section.
2964void DwarfDebug::emitAccelObjC() {
2965 emitAccel(Accel&: AccelObjC, Section: Asm->getObjFileLowering().getDwarfAccelObjCSection(),
2966 TableName: "ObjC");
2967}
2968
2969// Emit namespace dies into a hashed accelerator table.
2970void DwarfDebug::emitAccelNamespaces() {
2971 emitAccel(Accel&: AccelNamespace,
2972 Section: Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
2973 TableName: "namespac");
2974}
2975
2976// Emit type dies into a hashed accelerator table.
2977void DwarfDebug::emitAccelTypes() {
2978 emitAccel(Accel&: AccelTypes, Section: Asm->getObjFileLowering().getDwarfAccelTypesSection(),
2979 TableName: "types");
2980}
2981
2982// Public name handling.
2983// The format for the various pubnames:
2984//
2985// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2986// for the DIE that is named.
2987//
2988// gnu pubnames - offset/index value/name tuples where the offset is the offset
2989// into the CU and the index value is computed according to the type of value
2990// for the DIE that is named.
2991//
2992// For type units the offset is the offset of the skeleton DIE. For split dwarf
2993// it's the offset within the debug_info/debug_types dwo section, however, the
2994// reference in the pubname header doesn't change.
2995
2996/// computeIndexValue - Compute the gdb index value for the DIE and CU.
2997static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
2998 const DIE *Die) {
2999 // Entities that ended up only in a Type Unit reference the CU instead (since
3000 // the pub entry has offsets within the CU there's no real offset that can be
3001 // provided anyway). As it happens all such entities (namespaces and types,
3002 // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out
3003 // not to be true it would be necessary to persist this information from the
3004 // point at which the entry is added to the index data structure - since by
3005 // the time the index is built from that, the original type/namespace DIE in a
3006 // type unit has already been destroyed so it can't be queried for properties
3007 // like tag, etc.
3008 if (Die->getTag() == dwarf::DW_TAG_compile_unit)
3009 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE,
3010 dwarf::GIEL_EXTERNAL);
3011 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
3012
3013 // We could have a specification DIE that has our most of our knowledge,
3014 // look for that now.
3015 if (DIEValue SpecVal = Die->findAttribute(Attribute: dwarf::DW_AT_specification)) {
3016 DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
3017 if (SpecDIE.findAttribute(Attribute: dwarf::DW_AT_external))
3018 Linkage = dwarf::GIEL_EXTERNAL;
3019 } else if (Die->findAttribute(Attribute: dwarf::DW_AT_external))
3020 Linkage = dwarf::GIEL_EXTERNAL;
3021
3022 switch (Die->getTag()) {
3023 case dwarf::DW_TAG_class_type:
3024 case dwarf::DW_TAG_structure_type:
3025 case dwarf::DW_TAG_union_type:
3026 case dwarf::DW_TAG_enumeration_type:
3027 return dwarf::PubIndexEntryDescriptor(
3028 dwarf::GIEK_TYPE, dwarf::isCPlusPlus(S: CU->getSourceLanguage())
3029 ? dwarf::GIEL_EXTERNAL
3030 : dwarf::GIEL_STATIC);
3031 case dwarf::DW_TAG_typedef:
3032 case dwarf::DW_TAG_base_type:
3033 case dwarf::DW_TAG_subrange_type:
3034 case dwarf::DW_TAG_template_alias:
3035 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
3036 case dwarf::DW_TAG_namespace:
3037 return dwarf::GIEK_TYPE;
3038 case dwarf::DW_TAG_subprogram:
3039 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
3040 case dwarf::DW_TAG_variable:
3041 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
3042 case dwarf::DW_TAG_enumerator:
3043 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
3044 dwarf::GIEL_STATIC);
3045 default:
3046 return dwarf::GIEK_NONE;
3047 }
3048}
3049
3050/// emitDebugPubSections - Emit visible names and types into debug pubnames and
3051/// pubtypes sections.
3052void DwarfDebug::emitDebugPubSections() {
3053 for (const auto &NU : CUMap) {
3054 DwarfCompileUnit *TheU = NU.second;
3055 if (!TheU->hasDwarfPubSections())
3056 continue;
3057
3058 bool GnuStyle = TheU->getCUNode()->getNameTableKind() ==
3059 DICompileUnit::DebugNameTableKind::GNU;
3060
3061 Asm->OutStreamer->switchSection(
3062 Section: GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
3063 : Asm->getObjFileLowering().getDwarfPubNamesSection());
3064 emitDebugPubSection(GnuStyle, Name: "Names", TheU, Globals: TheU->getGlobalNames());
3065
3066 Asm->OutStreamer->switchSection(
3067 Section: GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
3068 : Asm->getObjFileLowering().getDwarfPubTypesSection());
3069 emitDebugPubSection(GnuStyle, Name: "Types", TheU, Globals: TheU->getGlobalTypes());
3070 }
3071}
3072
3073void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) {
3074 if (useSectionsAsReferences())
3075 Asm->emitDwarfOffset(Label: CU.getSection()->getBeginSymbol(),
3076 Offset: CU.getDebugSectionOffset());
3077 else
3078 Asm->emitDwarfSymbolReference(Label: CU.getLabelBegin());
3079}
3080
3081void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name,
3082 DwarfCompileUnit *TheU,
3083 const StringMap<const DIE *> &Globals) {
3084 if (auto *Skeleton = TheU->getSkeleton())
3085 TheU = Skeleton;
3086
3087 // Emit the header.
3088 MCSymbol *EndLabel = Asm->emitDwarfUnitLength(
3089 Prefix: "pub" + Name, Comment: "Length of Public " + Name + " Info");
3090
3091 Asm->OutStreamer->AddComment(T: "DWARF Version");
3092 Asm->emitInt16(Value: dwarf::DW_PUBNAMES_VERSION);
3093
3094 Asm->OutStreamer->AddComment(T: "Offset of Compilation Unit Info");
3095 emitSectionReference(CU: *TheU);
3096
3097 Asm->OutStreamer->AddComment(T: "Compilation Unit Length");
3098 Asm->emitDwarfLengthOrOffset(Value: TheU->getLength());
3099
3100 // Emit the pubnames for this compilation unit.
3101 SmallVector<std::pair<StringRef, const DIE *>, 0> Vec;
3102 for (const auto &GI : Globals)
3103 Vec.emplace_back(Args: GI.first(), Args: GI.second);
3104 llvm::sort(C&: Vec, Comp: [](auto &A, auto &B) {
3105 return A.second->getOffset() < B.second->getOffset();
3106 });
3107 for (const auto &[Name, Entity] : Vec) {
3108 Asm->OutStreamer->AddComment(T: "DIE offset");
3109 Asm->emitDwarfLengthOrOffset(Value: Entity->getOffset());
3110
3111 if (GnuStyle) {
3112 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(CU: TheU, Die: Entity);
3113 Asm->OutStreamer->AddComment(
3114 T: Twine("Attributes: ") + dwarf::GDBIndexEntryKindString(Kind: Desc.Kind) +
3115 ", " + dwarf::GDBIndexEntryLinkageString(Linkage: Desc.Linkage));
3116 Asm->emitInt8(Value: Desc.toBits());
3117 }
3118
3119 Asm->OutStreamer->AddComment(T: "External Name");
3120 Asm->OutStreamer->emitBytes(Data: StringRef(Name.data(), Name.size() + 1));
3121 }
3122
3123 Asm->OutStreamer->AddComment(T: "End Mark");
3124 Asm->emitDwarfLengthOrOffset(Value: 0);
3125 Asm->OutStreamer->emitLabel(Symbol: EndLabel);
3126}
3127
3128/// Emit null-terminated strings into a debug str section.
3129void DwarfDebug::emitDebugStr() {
3130 MCSection *StringOffsetsSection = nullptr;
3131 if (useSegmentedStringOffsetsTable()) {
3132 emitStringOffsetsTableHeader();
3133 StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection();
3134 }
3135 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
3136 Holder.emitStrings(StrSection: Asm->getObjFileLowering().getDwarfStrSection(),
3137 OffsetSection: StringOffsetsSection, /* UseRelativeOffsets = */ true);
3138}
3139
3140void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
3141 const DebugLocStream::Entry &Entry,
3142 const DwarfCompileUnit *CU) {
3143 auto &&Comments = DebugLocs.getComments(E: Entry);
3144 auto Comment = Comments.begin();
3145 auto End = Comments.end();
3146
3147 // The expressions are inserted into a byte stream rather early (see
3148 // DwarfExpression::addExpression) so for those ops (e.g. DW_OP_convert) that
3149 // need to reference a base_type DIE the offset of that DIE is not yet known.
3150 // To deal with this we instead insert a placeholder early and then extract
3151 // it here and replace it with the real reference.
3152 unsigned PtrSize = Asm->MAI->getCodePointerSize();
3153 DWARFDataExtractor Data(StringRef(DebugLocs.getBytes(E: Entry).data(),
3154 DebugLocs.getBytes(E: Entry).size()),
3155 Asm->getDataLayout().isLittleEndian(), PtrSize);
3156 DWARFExpression Expr(Data, PtrSize, Asm->OutContext.getDwarfFormat());
3157
3158 using Encoding = DWARFExpression::Operation::Encoding;
3159 uint64_t Offset = 0;
3160 for (const auto &Op : Expr) {
3161 assert(Op.getCode() != dwarf::DW_OP_const_type &&
3162 "3 operand ops not yet supported");
3163 assert(!Op.getSubCode() && "SubOps not yet supported");
3164 Streamer.emitInt8(Byte: Op.getCode(), Comment: Comment != End ? *(Comment++) : "");
3165 Offset++;
3166 for (unsigned I = 0; I < Op.getDescription().Op.size(); ++I) {
3167 if (Op.getDescription().Op[I] == Encoding::BaseTypeRef) {
3168 unsigned Length =
3169 Streamer.emitDIERef(D: *CU->ExprRefedBaseTypes[Op.getRawOperand(Idx: I)].Die);
3170 // Make sure comments stay aligned.
3171 for (unsigned J = 0; J < Length; ++J)
3172 if (Comment != End)
3173 Comment++;
3174 } else {
3175 for (uint64_t J = Offset; J < Op.getOperandEndOffset(Idx: I); ++J)
3176 Streamer.emitInt8(Byte: Data.getData()[J], Comment: Comment != End ? *(Comment++) : "");
3177 }
3178 Offset = Op.getOperandEndOffset(Idx: I);
3179 }
3180 assert(Offset == Op.getEndOffset());
3181 }
3182}
3183
3184void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
3185 const DbgValueLoc &Value,
3186 DwarfExpression &DwarfExpr) {
3187 auto *DIExpr = Value.getExpression();
3188 DIExpressionCursor ExprCursor(DIExpr);
3189 DwarfExpr.addFragmentOffset(Expr: DIExpr);
3190
3191 // If the DIExpr is an Entry Value, we want to follow the same code path
3192 // regardless of whether the DBG_VALUE is variadic or not.
3193 if (DIExpr && DIExpr->isEntryValue()) {
3194 // Entry values can only be a single register with no additional DIExpr,
3195 // so just add it directly.
3196 assert(Value.getLocEntries().size() == 1);
3197 assert(Value.getLocEntries()[0].isLocation());
3198 MachineLocation Location = Value.getLocEntries()[0].getLoc();
3199 DwarfExpr.setLocation(Loc: Location, DIExpr);
3200
3201 DwarfExpr.beginEntryValueExpression(ExprCursor);
3202
3203 const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
3204 if (!DwarfExpr.addMachineRegExpression(TRI, Expr&: ExprCursor, MachineReg: Location.getReg()))
3205 return;
3206 return DwarfExpr.addExpression(Expr: std::move(ExprCursor));
3207 }
3208
3209 // Regular entry.
3210 auto EmitValueLocEntry = [&DwarfExpr, &BT,
3211 &AP](const DbgValueLocEntry &Entry,
3212 DIExpressionCursor &Cursor) -> bool {
3213 if (Entry.isInt()) {
3214 if (BT && (BT->getEncoding() == dwarf::DW_ATE_boolean))
3215 DwarfExpr.addBooleanConstant(Value: Entry.getInt());
3216 else if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
3217 BT->getEncoding() == dwarf::DW_ATE_signed_char))
3218 DwarfExpr.addSignedConstant(Value: Entry.getInt());
3219 else
3220 DwarfExpr.addUnsignedConstant(Value: Entry.getInt());
3221 } else if (Entry.isLocation()) {
3222 MachineLocation Location = Entry.getLoc();
3223 if (Location.isIndirect())
3224 DwarfExpr.setMemoryLocationKind();
3225
3226 const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
3227 if (!DwarfExpr.addMachineRegExpression(TRI, Expr&: Cursor, MachineReg: Location.getReg()))
3228 return false;
3229 } else if (Entry.isTargetIndexLocation()) {
3230 TargetIndexLocation Loc = Entry.getTargetIndexLocation();
3231 // TODO TargetIndexLocation is a target-independent. Currently only the
3232 // WebAssembly-specific encoding is supported.
3233 assert(AP.TM.getTargetTriple().isWasm());
3234 DwarfExpr.addWasmLocation(Index: Loc.Index, Offset: static_cast<uint64_t>(Loc.Offset));
3235 } else if (Entry.isConstantFP()) {
3236 if (AP.getDwarfVersion() >= 4 && !AP.getDwarfDebug()->tuneForSCE() &&
3237 !Cursor) {
3238 DwarfExpr.addConstantFP(Value: Entry.getConstantFP()->getValueAPF(), AP);
3239 } else if (Entry.getConstantFP()
3240 ->getValueAPF()
3241 .bitcastToAPInt()
3242 .getBitWidth() <= 64 /*bits*/) {
3243 DwarfExpr.addUnsignedConstant(
3244 Value: Entry.getConstantFP()->getValueAPF().bitcastToAPInt());
3245 } else {
3246 LLVM_DEBUG(
3247 dbgs() << "Skipped DwarfExpression creation for ConstantFP of size"
3248 << Entry.getConstantFP()
3249 ->getValueAPF()
3250 .bitcastToAPInt()
3251 .getBitWidth()
3252 << " bits\n");
3253 return false;
3254 }
3255 }
3256 return true;
3257 };
3258
3259 if (!Value.isVariadic()) {
3260 if (!EmitValueLocEntry(Value.getLocEntries()[0], ExprCursor))
3261 return;
3262 DwarfExpr.addExpression(Expr: std::move(ExprCursor));
3263 return;
3264 }
3265
3266 // If any of the location entries are registers with the value 0, then the
3267 // location is undefined.
3268 if (any_of(Range: Value.getLocEntries(), P: [](const DbgValueLocEntry &Entry) {
3269 return Entry.isLocation() && !Entry.getLoc().getReg();
3270 }))
3271 return;
3272
3273 DwarfExpr.addExpression(
3274 Expr: std::move(ExprCursor),
3275 InsertArg: [EmitValueLocEntry, &Value](unsigned Idx,
3276 DIExpressionCursor &Cursor) -> bool {
3277 return EmitValueLocEntry(Value.getLocEntries()[Idx], Cursor);
3278 });
3279}
3280
3281void DebugLocEntry::finalize(const AsmPrinter &AP,
3282 DebugLocStream::ListBuilder &List,
3283 const DIBasicType *BT,
3284 DwarfCompileUnit &TheCU) {
3285 assert(!Values.empty() &&
3286 "location list entries without values are redundant");
3287 assert(Begin != End && "unexpected location list entry with empty range");
3288 DebugLocStream::EntryBuilder Entry(List, Begin, End);
3289 BufferByteStreamer Streamer = Entry.getStreamer();
3290 DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer, TheCU);
3291 const DbgValueLoc &Value = Values[0];
3292 if (Value.isFragment()) {
3293 // Emit all fragments that belong to the same variable and range.
3294 assert(llvm::all_of(Values, [](DbgValueLoc P) {
3295 return P.isFragment();
3296 }) && "all values are expected to be fragments");
3297 assert(llvm::is_sorted(Values) && "fragments are expected to be sorted");
3298
3299 for (const auto &Fragment : Values)
3300 DwarfDebug::emitDebugLocValue(AP, BT, Value: Fragment, DwarfExpr);
3301
3302 } else {
3303 assert(Values.size() == 1 && "only fragments may have >1 value");
3304 DwarfDebug::emitDebugLocValue(AP, BT, Value, DwarfExpr);
3305 }
3306 DwarfExpr.finalize();
3307 if (DwarfExpr.TagOffset)
3308 List.setTagOffset(*DwarfExpr.TagOffset);
3309}
3310
3311void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry,
3312 const DwarfCompileUnit *CU) {
3313 // Emit the size.
3314 Asm->OutStreamer->AddComment(T: "Loc expr size");
3315 if (getDwarfVersion() >= 5)
3316 Asm->emitULEB128(Value: DebugLocs.getBytes(E: Entry).size());
3317 else if (DebugLocs.getBytes(E: Entry).size() <= std::numeric_limits<uint16_t>::max())
3318 Asm->emitInt16(Value: DebugLocs.getBytes(E: Entry).size());
3319 else {
3320 // The entry is too big to fit into 16 bit, drop it as there is nothing we
3321 // can do.
3322 Asm->emitInt16(Value: 0);
3323 return;
3324 }
3325 // Emit the entry.
3326 APByteStreamer Streamer(*Asm);
3327 emitDebugLocEntry(Streamer, Entry, CU);
3328}
3329
3330// Emit the header of a DWARF 5 range list table list table. Returns the symbol
3331// that designates the end of the table for the caller to emit when the table is
3332// complete.
3333static MCSymbol *emitRnglistsTableHeader(AsmPrinter *Asm,
3334 const DwarfFile &Holder) {
3335 MCSymbol *TableEnd = mcdwarf::emitListsTableHeaderStart(S&: *Asm->OutStreamer);
3336
3337 Asm->OutStreamer->AddComment(T: "Offset entry count");
3338 Asm->emitInt32(Value: Holder.getRangeLists().size());
3339 Asm->OutStreamer->emitLabel(Symbol: Holder.getRnglistsTableBaseSym());
3340
3341 for (const RangeSpanList &List : Holder.getRangeLists())
3342 Asm->emitLabelDifference(Hi: List.Label, Lo: Holder.getRnglistsTableBaseSym(),
3343 Size: Asm->getDwarfOffsetByteSize());
3344
3345 return TableEnd;
3346}
3347
3348// Emit the header of a DWARF 5 locations list table. Returns the symbol that
3349// designates the end of the table for the caller to emit when the table is
3350// complete.
3351static MCSymbol *emitLoclistsTableHeader(AsmPrinter *Asm,
3352 const DwarfDebug &DD) {
3353 MCSymbol *TableEnd = mcdwarf::emitListsTableHeaderStart(S&: *Asm->OutStreamer);
3354
3355 const auto &DebugLocs = DD.getDebugLocs();
3356
3357 Asm->OutStreamer->AddComment(T: "Offset entry count");
3358 Asm->emitInt32(Value: DebugLocs.getLists().size());
3359 Asm->OutStreamer->emitLabel(Symbol: DebugLocs.getSym());
3360
3361 for (const auto &List : DebugLocs.getLists())
3362 Asm->emitLabelDifference(Hi: List.Label, Lo: DebugLocs.getSym(),
3363 Size: Asm->getDwarfOffsetByteSize());
3364
3365 return TableEnd;
3366}
3367
3368template <typename Ranges, typename PayloadEmitter>
3369static void
3370emitRangeList(DwarfDebug &DD, AsmPrinter *Asm, MCSymbol *Sym, const Ranges &R,
3371 const DwarfCompileUnit &CU, unsigned BaseAddressx,
3372 unsigned OffsetPair, unsigned StartxLength, unsigned StartxEndx,
3373 unsigned EndOfList, StringRef (*StringifyEnum)(unsigned),
3374 bool ShouldUseBaseAddress, PayloadEmitter EmitPayload) {
3375 auto Size = Asm->MAI->getCodePointerSize();
3376 bool UseDwarf5 = DD.getDwarfVersion() >= 5;
3377
3378 // Emit our symbol so we can find the beginning of the range.
3379 Asm->OutStreamer->emitLabel(Symbol: Sym);
3380
3381 // Gather all the ranges that apply to the same section so they can share
3382 // a base address entry.
3383 SmallMapVector<const MCSection *, std::vector<decltype(&*R.begin())>, 16>
3384 SectionRanges;
3385
3386 for (const auto &Range : R)
3387 SectionRanges[&Range.Begin->getSection()].push_back(&Range);
3388
3389 const MCSymbol *CUBase = CU.getBaseAddress();
3390 bool BaseIsSet = false;
3391 for (const auto &P : SectionRanges) {
3392 auto *Base = CUBase;
3393 if ((Asm->TM.getTargetTriple().isNVPTX() && DD.tuneForGDB()) ||
3394 (DD.useSplitDwarf() && UseDwarf5 && P.first->isLinkerRelaxable())) {
3395 // PTX does not support subtracting labels from the code section in the
3396 // debug_loc section. To work around this, the NVPTX backend needs the
3397 // compile unit to have no low_pc in order to have a zero base_address
3398 // when handling debug_loc in cuda-gdb. Additionally, cuda-gdb doesn't
3399 // seem to handle setting a per-variable base to zero. To make cuda-gdb
3400 // happy, just emit labels with no base while having no compile unit
3401 // low_pc.
3402 BaseIsSet = false;
3403 Base = nullptr;
3404 } else if (!Base && ShouldUseBaseAddress) {
3405 const MCSymbol *Begin = P.second.front()->Begin;
3406 const MCSymbol *NewBase = DD.getSectionLabel(S: &Begin->getSection());
3407 if (!UseDwarf5) {
3408 Base = NewBase;
3409 BaseIsSet = true;
3410 Asm->OutStreamer->emitIntValue(Value: -1, Size);
3411 Asm->OutStreamer->AddComment(T: " base address");
3412 Asm->OutStreamer->emitSymbolValue(Sym: Base, Size);
3413 } else if (NewBase != Begin || P.second.size() > 1) {
3414 // Only use a base address if
3415 // * the existing pool address doesn't match (NewBase != Begin)
3416 // * or, there's more than one entry to share the base address
3417 Base = NewBase;
3418 BaseIsSet = true;
3419 Asm->OutStreamer->AddComment(T: StringifyEnum(BaseAddressx));
3420 Asm->emitInt8(Value: BaseAddressx);
3421 Asm->OutStreamer->AddComment(T: " base address index");
3422 Asm->emitULEB128(Value: DD.getAddressPool().getIndex(Sym: Base));
3423 }
3424 } else if (BaseIsSet && !UseDwarf5) {
3425 BaseIsSet = false;
3426 assert(!Base);
3427 Asm->OutStreamer->emitIntValue(Value: -1, Size);
3428 Asm->OutStreamer->emitIntValue(Value: 0, Size);
3429 }
3430
3431 for (const auto *RS : P.second) {
3432 const MCSymbol *Begin = RS->Begin;
3433 const MCSymbol *End = RS->End;
3434 assert(Begin && "Range without a begin symbol?");
3435 assert(End && "Range without an end symbol?");
3436 if (Base) {
3437 if (UseDwarf5) {
3438 // Emit offset_pair when we have a base.
3439 Asm->OutStreamer->AddComment(T: StringifyEnum(OffsetPair));
3440 Asm->emitInt8(Value: OffsetPair);
3441 Asm->OutStreamer->AddComment(T: " starting offset");
3442 Asm->emitLabelDifferenceAsULEB128(Hi: Begin, Lo: Base);
3443 Asm->OutStreamer->AddComment(T: " ending offset");
3444 Asm->emitLabelDifferenceAsULEB128(Hi: End, Lo: Base);
3445 } else {
3446 Asm->emitLabelDifference(Hi: Begin, Lo: Base, Size);
3447 Asm->emitLabelDifference(Hi: End, Lo: Base, Size);
3448 }
3449 } else if (UseDwarf5) {
3450 // NOTE: We can't use absoluteSymbolDiff here instead of
3451 // isRangeRelaxable. While isRangeRelaxable only checks that the offset
3452 // between labels won't change at link time (which is exactly what we
3453 // need), absoluteSymbolDiff also requires that the offset remain
3454 // unchanged at assembly time, imposing a much stricter condition.
3455 // Consequently, this would lead to less optimal debug info emission.
3456 if (DD.useSplitDwarf() && llvm::isRangeRelaxable(Begin, End)) {
3457 Asm->OutStreamer->AddComment(T: StringifyEnum(StartxEndx));
3458 Asm->emitInt8(Value: StartxEndx);
3459 Asm->OutStreamer->AddComment(T: " start index");
3460 Asm->emitULEB128(Value: DD.getAddressPool().getIndex(Sym: Begin));
3461 Asm->OutStreamer->AddComment(T: " end index");
3462 Asm->emitULEB128(Value: DD.getAddressPool().getIndex(Sym: End));
3463 } else {
3464 Asm->OutStreamer->AddComment(T: StringifyEnum(StartxLength));
3465 Asm->emitInt8(Value: StartxLength);
3466 Asm->OutStreamer->AddComment(T: " start index");
3467 Asm->emitULEB128(Value: DD.getAddressPool().getIndex(Sym: Begin));
3468 Asm->OutStreamer->AddComment(T: " length");
3469 Asm->emitLabelDifferenceAsULEB128(Hi: End, Lo: Begin);
3470 }
3471 } else {
3472 Asm->OutStreamer->emitSymbolValue(Sym: Begin, Size);
3473 Asm->OutStreamer->emitSymbolValue(Sym: End, Size);
3474 }
3475 EmitPayload(*RS);
3476 }
3477 }
3478
3479 if (UseDwarf5) {
3480 Asm->OutStreamer->AddComment(T: StringifyEnum(EndOfList));
3481 Asm->emitInt8(Value: EndOfList);
3482 } else {
3483 // Terminate the list with two 0 values.
3484 Asm->OutStreamer->emitIntValue(Value: 0, Size);
3485 Asm->OutStreamer->emitIntValue(Value: 0, Size);
3486 }
3487}
3488
3489// Handles emission of both debug_loclist / debug_loclist.dwo
3490static void emitLocList(DwarfDebug &DD, AsmPrinter *Asm, const DebugLocStream::List &List) {
3491 emitRangeList(
3492 DD, Asm, Sym: List.Label, R: DD.getDebugLocs().getEntries(L: List), CU: *List.CU,
3493 BaseAddressx: dwarf::DW_LLE_base_addressx, OffsetPair: dwarf::DW_LLE_offset_pair,
3494 StartxLength: dwarf::DW_LLE_startx_length, StartxEndx: dwarf::DW_LLE_startx_endx,
3495 EndOfList: dwarf::DW_LLE_end_of_list, StringifyEnum: llvm::dwarf::LocListEncodingString,
3496 /* ShouldUseBaseAddress */ true, EmitPayload: [&](const DebugLocStream::Entry &E) {
3497 DD.emitDebugLocEntryLocation(Entry: E, CU: List.CU);
3498 });
3499}
3500
3501void DwarfDebug::emitDebugLocImpl(MCSection *Sec) {
3502 if (DebugLocs.getLists().empty())
3503 return;
3504
3505 Asm->OutStreamer->switchSection(Section: Sec);
3506
3507 MCSymbol *TableEnd = nullptr;
3508 if (getDwarfVersion() >= 5)
3509 TableEnd = emitLoclistsTableHeader(Asm, DD: *this);
3510
3511 for (const auto &List : DebugLocs.getLists())
3512 emitLocList(DD&: *this, Asm, List);
3513
3514 if (TableEnd)
3515 Asm->OutStreamer->emitLabel(Symbol: TableEnd);
3516}
3517
3518// Emit locations into the .debug_loc/.debug_loclists section.
3519void DwarfDebug::emitDebugLoc() {
3520 emitDebugLocImpl(
3521 Sec: getDwarfVersion() >= 5
3522 ? Asm->getObjFileLowering().getDwarfLoclistsSection()
3523 : Asm->getObjFileLowering().getDwarfLocSection());
3524}
3525
3526// Emit locations into the .debug_loc.dwo/.debug_loclists.dwo section.
3527void DwarfDebug::emitDebugLocDWO() {
3528 if (getDwarfVersion() >= 5) {
3529 emitDebugLocImpl(
3530 Sec: Asm->getObjFileLowering().getDwarfLoclistsDWOSection());
3531
3532 return;
3533 }
3534
3535 for (const auto &List : DebugLocs.getLists()) {
3536 Asm->OutStreamer->switchSection(
3537 Section: Asm->getObjFileLowering().getDwarfLocDWOSection());
3538 Asm->OutStreamer->emitLabel(Symbol: List.Label);
3539
3540 for (const auto &Entry : DebugLocs.getEntries(L: List)) {
3541 // GDB only supports startx_length in pre-standard split-DWARF.
3542 // (in v5 standard loclists, it currently* /only/ supports base_address +
3543 // offset_pair, so the implementations can't really share much since they
3544 // need to use different representations)
3545 // * as of October 2018, at least
3546 //
3547 // In v5 (see emitLocList), this uses SectionLabels to reuse existing
3548 // addresses in the address pool to minimize object size/relocations.
3549 Asm->emitInt8(Value: dwarf::DW_LLE_startx_length);
3550 unsigned idx = AddrPool.getIndex(Sym: Entry.Begin);
3551 Asm->emitULEB128(Value: idx);
3552 // Also the pre-standard encoding is slightly different, emitting this as
3553 // an address-length entry here, but its a ULEB128 in DWARFv5 loclists.
3554 Asm->emitLabelDifference(Hi: Entry.End, Lo: Entry.Begin, Size: 4);
3555 emitDebugLocEntryLocation(Entry, CU: List.CU);
3556 }
3557 Asm->emitInt8(Value: dwarf::DW_LLE_end_of_list);
3558 }
3559}
3560
3561struct ArangeSpan {
3562 const MCSymbol *Start, *End;
3563};
3564
3565// Emit a debug aranges section, containing a CU lookup for any
3566// address we can tie back to a CU.
3567void DwarfDebug::emitDebugARanges() {
3568 if (ArangeLabels.empty())
3569 return;
3570
3571 // Provides a unique id per text section.
3572 MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
3573
3574 // Filter labels by section.
3575 for (const SymbolCU &SCU : ArangeLabels) {
3576 if (SCU.Sym->isInSection()) {
3577 // Make a note of this symbol and it's section.
3578 MCSection *Section = &SCU.Sym->getSection();
3579 SectionMap[Section].push_back(Elt: SCU);
3580 } else {
3581 // Some symbols (e.g. common/bss on mach-o) can have no section but still
3582 // appear in the output. This sucks as we rely on sections to build
3583 // arange spans. We can do it without, but it's icky.
3584 SectionMap[nullptr].push_back(Elt: SCU);
3585 }
3586 }
3587
3588 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
3589
3590 for (auto &I : SectionMap) {
3591 MCSection *Section = I.first;
3592 SmallVector<SymbolCU, 8> &List = I.second;
3593 assert(!List.empty());
3594
3595 // If we have no section (e.g. common), just write out
3596 // individual spans for each symbol.
3597 if (!Section) {
3598 for (const SymbolCU &Cur : List) {
3599 ArangeSpan Span;
3600 Span.Start = Cur.Sym;
3601 Span.End = nullptr;
3602 assert(Cur.CU);
3603 Spans[Cur.CU].push_back(x: Span);
3604 }
3605 continue;
3606 }
3607
3608 // Insert a final terminator.
3609 List.push_back(Elt: SymbolCU(nullptr, Asm->OutStreamer->endSection(Section)));
3610
3611 // Build spans between each label.
3612 const MCSymbol *StartSym = List[0].Sym;
3613 for (size_t n = 1, e = List.size(); n < e; n++) {
3614 const SymbolCU &Prev = List[n - 1];
3615 const SymbolCU &Cur = List[n];
3616
3617 // Try and build the longest span we can within the same CU.
3618 if (Cur.CU != Prev.CU) {
3619 ArangeSpan Span;
3620 Span.Start = StartSym;
3621 Span.End = Cur.Sym;
3622 assert(Prev.CU);
3623 Spans[Prev.CU].push_back(x: Span);
3624 StartSym = Cur.Sym;
3625 }
3626 }
3627 }
3628
3629 // Start the dwarf aranges section.
3630 Asm->OutStreamer->switchSection(
3631 Section: Asm->getObjFileLowering().getDwarfARangesSection());
3632
3633 unsigned PtrSize = Asm->MAI->getCodePointerSize();
3634
3635 // Build a list of CUs used.
3636 std::vector<DwarfCompileUnit *> CUs;
3637 for (const auto &it : Spans) {
3638 DwarfCompileUnit *CU = it.first;
3639 CUs.push_back(x: CU);
3640 }
3641
3642 // Sort the CU list (again, to ensure consistent output order).
3643 llvm::sort(C&: CUs, Comp: [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) {
3644 return A->getUniqueID() < B->getUniqueID();
3645 });
3646
3647 // Emit an arange table for each CU we used.
3648 for (DwarfCompileUnit *CU : CUs) {
3649 std::vector<ArangeSpan> &List = Spans[CU];
3650
3651 // Describe the skeleton CU's offset and length, not the dwo file's.
3652 if (auto *Skel = CU->getSkeleton())
3653 CU = Skel;
3654
3655 // Emit size of content not including length itself.
3656 unsigned ContentSize =
3657 sizeof(int16_t) + // DWARF ARange version number
3658 Asm->getDwarfOffsetByteSize() + // Offset of CU in the .debug_info
3659 // section
3660 sizeof(int8_t) + // Pointer Size (in bytes)
3661 sizeof(int8_t); // Segment Size (in bytes)
3662
3663 unsigned TupleSize = PtrSize * 2;
3664
3665 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
3666 unsigned Padding = offsetToAlignment(
3667 Value: Asm->getUnitLengthFieldByteSize() + ContentSize, Alignment: Align(TupleSize));
3668
3669 ContentSize += Padding;
3670 ContentSize += (List.size() + 1) * TupleSize;
3671
3672 // For each compile unit, write the list of spans it covers.
3673 Asm->emitDwarfUnitLength(Length: ContentSize, Comment: "Length of ARange Set");
3674 Asm->OutStreamer->AddComment(T: "DWARF Arange version number");
3675 Asm->emitInt16(Value: dwarf::DW_ARANGES_VERSION);
3676 Asm->OutStreamer->AddComment(T: "Offset Into Debug Info Section");
3677 emitSectionReference(CU: *CU);
3678 Asm->OutStreamer->AddComment(T: "Address Size (in bytes)");
3679 Asm->emitInt8(Value: PtrSize);
3680 Asm->OutStreamer->AddComment(T: "Segment Size (in bytes)");
3681 Asm->emitInt8(Value: 0);
3682
3683 Asm->OutStreamer->emitFill(NumBytes: Padding, FillValue: 0xff);
3684
3685 for (const ArangeSpan &Span : List) {
3686 Asm->emitLabelReference(Label: Span.Start, Size: PtrSize);
3687
3688 // Calculate the size as being from the span start to its end.
3689 //
3690 // If the size is zero, then round it up to one byte. The DWARF
3691 // specification requires that entries in this table have nonzero
3692 // lengths.
3693 auto SizeRef = SymSize.find(Val: Span.Start);
3694 if ((SizeRef == SymSize.end() || SizeRef->second != 0) && Span.End) {
3695 Asm->emitLabelDifference(Hi: Span.End, Lo: Span.Start, Size: PtrSize);
3696 } else {
3697 // For symbols without an end marker (e.g. common), we
3698 // write a single arange entry containing just that one symbol.
3699 uint64_t Size;
3700 if (SizeRef == SymSize.end() || SizeRef->second == 0)
3701 Size = 1;
3702 else
3703 Size = SizeRef->second;
3704
3705 Asm->OutStreamer->emitIntValue(Value: Size, Size: PtrSize);
3706 }
3707 }
3708
3709 Asm->OutStreamer->AddComment(T: "ARange terminator");
3710 Asm->OutStreamer->emitIntValue(Value: 0, Size: PtrSize);
3711 Asm->OutStreamer->emitIntValue(Value: 0, Size: PtrSize);
3712 }
3713}
3714
3715/// Emit a single range list. We handle both DWARF v5 and earlier.
3716static void emitRangeList(DwarfDebug &DD, AsmPrinter *Asm,
3717 const RangeSpanList &List) {
3718 emitRangeList(DD, Asm, Sym: List.Label, R: List.Ranges, CU: *List.CU,
3719 BaseAddressx: dwarf::DW_RLE_base_addressx, OffsetPair: dwarf::DW_RLE_offset_pair,
3720 StartxLength: dwarf::DW_RLE_startx_length, StartxEndx: dwarf::DW_RLE_startx_endx,
3721 EndOfList: dwarf::DW_RLE_end_of_list, StringifyEnum: llvm::dwarf::RangeListEncodingString,
3722 ShouldUseBaseAddress: List.CU->getCUNode()->getRangesBaseAddress() ||
3723 DD.getDwarfVersion() >= 5,
3724 EmitPayload: [](auto) {});
3725}
3726
3727void DwarfDebug::emitDebugRangesImpl(const DwarfFile &Holder, MCSection *Section) {
3728 if (Holder.getRangeLists().empty())
3729 return;
3730
3731 assert(useRangesSection());
3732 assert(!CUMap.empty());
3733 assert(llvm::any_of(CUMap, [](const decltype(CUMap)::value_type &Pair) {
3734 return !Pair.second->getCUNode()->isDebugDirectivesOnly();
3735 }));
3736
3737 Asm->OutStreamer->switchSection(Section);
3738
3739 MCSymbol *TableEnd = nullptr;
3740 if (getDwarfVersion() >= 5)
3741 TableEnd = emitRnglistsTableHeader(Asm, Holder);
3742
3743 for (const RangeSpanList &List : Holder.getRangeLists())
3744 emitRangeList(DD&: *this, Asm, List);
3745
3746 if (TableEnd)
3747 Asm->OutStreamer->emitLabel(Symbol: TableEnd);
3748}
3749
3750/// Emit address ranges into the .debug_ranges section or into the DWARF v5
3751/// .debug_rnglists section.
3752void DwarfDebug::emitDebugRanges() {
3753 const auto &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
3754
3755 emitDebugRangesImpl(Holder,
3756 Section: getDwarfVersion() >= 5
3757 ? Asm->getObjFileLowering().getDwarfRnglistsSection()
3758 : Asm->getObjFileLowering().getDwarfRangesSection());
3759}
3760
3761void DwarfDebug::emitDebugRangesDWO() {
3762 emitDebugRangesImpl(Holder: InfoHolder,
3763 Section: Asm->getObjFileLowering().getDwarfRnglistsDWOSection());
3764}
3765
3766/// Emit the header of a DWARF 5 macro section, or the GNU extension for
3767/// DWARF 4.
3768static void emitMacroHeader(AsmPrinter *Asm, const DwarfDebug &DD,
3769 const DwarfCompileUnit &CU, uint16_t DwarfVersion) {
3770 enum HeaderFlagMask {
3771#define HANDLE_MACRO_FLAG(ID, NAME) MACRO_FLAG_##NAME = ID,
3772#include "llvm/BinaryFormat/Dwarf.def"
3773 };
3774 Asm->OutStreamer->AddComment(T: "Macro information version");
3775 Asm->emitInt16(Value: DwarfVersion >= 5 ? DwarfVersion : 4);
3776 // We emit the line offset flag unconditionally here, since line offset should
3777 // be mostly present.
3778 if (Asm->isDwarf64()) {
3779 Asm->OutStreamer->AddComment(T: "Flags: 64 bit, debug_line_offset present");
3780 Asm->emitInt8(Value: MACRO_FLAG_OFFSET_SIZE | MACRO_FLAG_DEBUG_LINE_OFFSET);
3781 } else {
3782 Asm->OutStreamer->AddComment(T: "Flags: 32 bit, debug_line_offset present");
3783 Asm->emitInt8(Value: MACRO_FLAG_DEBUG_LINE_OFFSET);
3784 }
3785 Asm->OutStreamer->AddComment(T: "debug_line_offset");
3786 if (DD.useSplitDwarf())
3787 Asm->emitDwarfLengthOrOffset(Value: 0);
3788 else
3789 Asm->emitDwarfSymbolReference(Label: CU.getLineTableStartSym());
3790}
3791
3792void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
3793 for (auto *MN : Nodes) {
3794 if (auto *M = dyn_cast<DIMacro>(Val: MN))
3795 emitMacro(M&: *M);
3796 else if (auto *F = dyn_cast<DIMacroFile>(Val: MN))
3797 emitMacroFile(F&: *F, U);
3798 else
3799 llvm_unreachable("Unexpected DI type!");
3800 }
3801}
3802
3803void DwarfDebug::emitMacro(DIMacro &M) {
3804 StringRef Name = M.getName();
3805 StringRef Value = M.getValue();
3806
3807 // There should be one space between the macro name and the macro value in
3808 // define entries. In undef entries, only the macro name is emitted.
3809 std::string Str = Value.empty() ? Name.str() : (Name + " " + Value).str();
3810
3811 if (UseDebugMacroSection) {
3812 if (getDwarfVersion() >= 5) {
3813 unsigned Type = M.getMacinfoType() == dwarf::DW_MACINFO_define
3814 ? dwarf::DW_MACRO_define_strx
3815 : dwarf::DW_MACRO_undef_strx;
3816 Asm->OutStreamer->AddComment(T: dwarf::MacroString(Encoding: Type));
3817 Asm->emitULEB128(Value: Type);
3818 Asm->OutStreamer->AddComment(T: "Line Number");
3819 Asm->emitULEB128(Value: M.getLine());
3820 Asm->OutStreamer->AddComment(T: "Macro String");
3821 Asm->emitULEB128(
3822 Value: InfoHolder.getStringPool().getIndexedEntry(Asm&: *Asm, Str).getIndex());
3823 } else {
3824 unsigned Type = M.getMacinfoType() == dwarf::DW_MACINFO_define
3825 ? dwarf::DW_MACRO_GNU_define_indirect
3826 : dwarf::DW_MACRO_GNU_undef_indirect;
3827 Asm->OutStreamer->AddComment(T: dwarf::GnuMacroString(Encoding: Type));
3828 Asm->emitULEB128(Value: Type);
3829 Asm->OutStreamer->AddComment(T: "Line Number");
3830 Asm->emitULEB128(Value: M.getLine());
3831 Asm->OutStreamer->AddComment(T: "Macro String");
3832 Asm->emitDwarfSymbolReference(
3833 Label: InfoHolder.getStringPool().getEntry(Asm&: *Asm, Str).getSymbol());
3834 }
3835 } else {
3836 Asm->OutStreamer->AddComment(T: dwarf::MacinfoString(Encoding: M.getMacinfoType()));
3837 Asm->emitULEB128(Value: M.getMacinfoType());
3838 Asm->OutStreamer->AddComment(T: "Line Number");
3839 Asm->emitULEB128(Value: M.getLine());
3840 Asm->OutStreamer->AddComment(T: "Macro String");
3841 Asm->OutStreamer->emitBytes(Data: Str);
3842 Asm->emitInt8(Value: '\0');
3843 }
3844}
3845
3846void DwarfDebug::emitMacroFileImpl(
3847 DIMacroFile &MF, DwarfCompileUnit &U, unsigned StartFile, unsigned EndFile,
3848 StringRef (*MacroFormToString)(unsigned Form)) {
3849
3850 Asm->OutStreamer->AddComment(T: MacroFormToString(StartFile));
3851 Asm->emitULEB128(Value: StartFile);
3852 Asm->OutStreamer->AddComment(T: "Line Number");
3853 Asm->emitULEB128(Value: MF.getLine());
3854 Asm->OutStreamer->AddComment(T: "File Number");
3855 DIFile &F = *MF.getFile();
3856 if (useSplitDwarf())
3857 Asm->emitULEB128(Value: getDwoLineTable(U)->getFile(
3858 Directory: F.getDirectory(), FileName: F.getFilename(), Checksum: getMD5AsBytes(File: &F),
3859 DwarfVersion: Asm->OutContext.getDwarfVersion(), Source: F.getSource()));
3860 else
3861 Asm->emitULEB128(Value: U.getOrCreateSourceID(File: &F));
3862 handleMacroNodes(Nodes: MF.getElements(), U);
3863 Asm->OutStreamer->AddComment(T: MacroFormToString(EndFile));
3864 Asm->emitULEB128(Value: EndFile);
3865}
3866
3867void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) {
3868 // DWARFv5 macro and DWARFv4 macinfo share some common encodings,
3869 // so for readibility/uniformity, We are explicitly emitting those.
3870 assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file);
3871 if (UseDebugMacroSection)
3872 emitMacroFileImpl(
3873 MF&: F, U, StartFile: dwarf::DW_MACRO_start_file, EndFile: dwarf::DW_MACRO_end_file,
3874 MacroFormToString: (getDwarfVersion() >= 5) ? dwarf::MacroString : dwarf::GnuMacroString);
3875 else
3876 emitMacroFileImpl(MF&: F, U, StartFile: dwarf::DW_MACINFO_start_file,
3877 EndFile: dwarf::DW_MACINFO_end_file, MacroFormToString: dwarf::MacinfoString);
3878}
3879
3880void DwarfDebug::emitDebugMacinfoImpl(MCSection *Section) {
3881 for (const auto &P : CUMap) {
3882 auto &TheCU = *P.second;
3883 auto *SkCU = TheCU.getSkeleton();
3884 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
3885 auto *CUNode = cast<DICompileUnit>(Val: P.first);
3886 DIMacroNodeArray Macros = CUNode->getMacros();
3887 if (Macros.empty())
3888 continue;
3889 Asm->OutStreamer->switchSection(Section);
3890 Asm->OutStreamer->emitLabel(Symbol: U.getMacroLabelBegin());
3891 if (UseDebugMacroSection)
3892 emitMacroHeader(Asm, DD: *this, CU: U, DwarfVersion: getDwarfVersion());
3893 handleMacroNodes(Nodes: Macros, U);
3894 Asm->OutStreamer->AddComment(T: "End Of Macro List Mark");
3895 Asm->emitInt8(Value: 0);
3896 }
3897}
3898
3899/// Emit macros into a debug macinfo/macro section.
3900void DwarfDebug::emitDebugMacinfo() {
3901 auto &ObjLower = Asm->getObjFileLowering();
3902 emitDebugMacinfoImpl(Section: UseDebugMacroSection
3903 ? ObjLower.getDwarfMacroSection()
3904 : ObjLower.getDwarfMacinfoSection());
3905}
3906
3907void DwarfDebug::emitDebugMacinfoDWO() {
3908 auto &ObjLower = Asm->getObjFileLowering();
3909 emitDebugMacinfoImpl(Section: UseDebugMacroSection
3910 ? ObjLower.getDwarfMacroDWOSection()
3911 : ObjLower.getDwarfMacinfoDWOSection());
3912}
3913
3914// DWARF5 Experimental Separate Dwarf emitters.
3915
3916void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
3917 std::unique_ptr<DwarfCompileUnit> NewU) {
3918
3919 if (!CompilationDir.empty())
3920 NewU->addString(Die, Attribute: dwarf::DW_AT_comp_dir, Str: CompilationDir);
3921 addGnuPubAttributes(U&: *NewU, D&: Die);
3922
3923 SkeletonHolder.addUnit(U: std::move(NewU));
3924}
3925
3926DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
3927
3928 auto OwnedUnit = std::make_unique<DwarfCompileUnit>(
3929 args: CU.getUniqueID(), args: CU.getCUNode(), args&: Asm, args: this, args: &SkeletonHolder,
3930 args: UnitKind::Skeleton);
3931 DwarfCompileUnit &NewCU = *OwnedUnit;
3932 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
3933
3934 NewCU.initStmtList();
3935
3936 if (useSegmentedStringOffsetsTable())
3937 NewCU.addStringOffsetsStart();
3938
3939 initSkeletonUnit(U: CU, Die&: NewCU.getUnitDie(), NewU: std::move(OwnedUnit));
3940
3941 return NewCU;
3942}
3943
3944// Emit the .debug_info.dwo section for separated dwarf. This contains the
3945// compile units that would normally be in debug_info.
3946void DwarfDebug::emitDebugInfoDWO() {
3947 assert(useSplitDwarf() && "No split dwarf debug info?");
3948 // Don't emit relocations into the dwo file.
3949 InfoHolder.emitUnits(/* UseOffsets */ true);
3950}
3951
3952// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
3953// abbreviations for the .debug_info.dwo section.
3954void DwarfDebug::emitDebugAbbrevDWO() {
3955 assert(useSplitDwarf() && "No split dwarf?");
3956 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
3957}
3958
3959void DwarfDebug::emitDebugLineDWO() {
3960 assert(useSplitDwarf() && "No split dwarf?");
3961 SplitTypeUnitFileTable.Emit(
3962 MCOS&: *Asm->OutStreamer, Params: MCDwarfLineTableParams(),
3963 Section: Asm->getObjFileLowering().getDwarfLineDWOSection());
3964}
3965
3966void DwarfDebug::emitStringOffsetsTableHeaderDWO() {
3967 assert(useSplitDwarf() && "No split dwarf?");
3968 InfoHolder.getStringPool().emitStringOffsetsTableHeader(
3969 Asm&: *Asm, OffsetSection: Asm->getObjFileLowering().getDwarfStrOffDWOSection(),
3970 StartSym: InfoHolder.getStringOffsetsStartSym());
3971}
3972
3973// Emit the .debug_str.dwo section for separated dwarf. This contains the
3974// string section and is identical in format to traditional .debug_str
3975// sections.
3976void DwarfDebug::emitDebugStrDWO() {
3977 if (useSegmentedStringOffsetsTable())
3978 emitStringOffsetsTableHeaderDWO();
3979 assert(useSplitDwarf() && "No split dwarf?");
3980 MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
3981 InfoHolder.emitStrings(StrSection: Asm->getObjFileLowering().getDwarfStrDWOSection(),
3982 OffsetSection: OffSec, /* UseRelativeOffsets = */ false);
3983}
3984
3985// Emit address pool.
3986void DwarfDebug::emitDebugAddr() {
3987 AddrPool.emit(Asm&: *Asm, AddrSection: Asm->getObjFileLowering().getDwarfAddrSection());
3988}
3989
3990MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
3991 if (!useSplitDwarf())
3992 return nullptr;
3993 const DICompileUnit *DIUnit = CU.getCUNode();
3994 SplitTypeUnitFileTable.maybeSetRootFile(
3995 Directory: DIUnit->getDirectory(), FileName: DIUnit->getFilename(),
3996 Checksum: getMD5AsBytes(File: DIUnit->getFile()), Source: DIUnit->getSource());
3997 return &SplitTypeUnitFileTable;
3998}
3999
4000uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) {
4001 MD5 Hash;
4002 Hash.update(Str: Identifier);
4003 // ... take the least significant 8 bytes and return those. Our MD5
4004 // implementation always returns its results in little endian, so we actually
4005 // need the "high" word.
4006 MD5::MD5Result Result;
4007 Hash.final(Result);
4008 return Result.high();
4009}
4010
4011void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
4012 StringRef Identifier, DIE &RefDie,
4013 const DICompositeType *CTy) {
4014 // Fast path if we're building some type units and one has already used the
4015 // address pool we know we're going to throw away all this work anyway, so
4016 // don't bother building dependent types.
4017 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
4018 return;
4019
4020 auto Ins = TypeSignatures.try_emplace(Key: CTy);
4021 if (!Ins.second) {
4022 CU.addDIETypeSignature(Die&: RefDie, Signature: Ins.first->second);
4023 return;
4024 }
4025
4026 setCurrentDWARF5AccelTable(DWARF5AccelTableKind::TU);
4027 bool TopLevelType = TypeUnitsUnderConstruction.empty();
4028 AddrPool.resetUsedFlag();
4029
4030 auto OwnedUnit = std::make_unique<DwarfTypeUnit>(
4031 args&: CU, args&: Asm, args: this, args: &InfoHolder, args: NumTypeUnitsCreated++, args: getDwoLineTable(CU));
4032 DwarfTypeUnit &NewTU = *OwnedUnit;
4033 DIE &UnitDie = NewTU.getUnitDie();
4034 TypeUnitsUnderConstruction.emplace_back(Args: std::move(OwnedUnit), Args&: CTy);
4035
4036 NewTU.addUInt(Die&: UnitDie, Attribute: dwarf::DW_AT_language, Form: dwarf::DW_FORM_data2,
4037 Integer: CU.getSourceLanguage());
4038
4039 uint64_t Signature = makeTypeSignature(Identifier);
4040 NewTU.setTypeSignature(Signature);
4041 Ins.first->second = Signature;
4042
4043 if (useSplitDwarf()) {
4044 // Although multiple type units can have the same signature, they are not
4045 // guranteed to be bit identical. When LLDB uses .debug_names it needs to
4046 // know from which CU a type unit came from. These two attrbutes help it to
4047 // figure that out.
4048 if (getDwarfVersion() >= 5) {
4049 if (!CompilationDir.empty())
4050 NewTU.addString(Die&: UnitDie, Attribute: dwarf::DW_AT_comp_dir, Str: CompilationDir);
4051 NewTU.addString(Die&: UnitDie, Attribute: dwarf::DW_AT_dwo_name,
4052 Str: Asm->TM.Options.MCOptions.SplitDwarfFile);
4053 }
4054 MCSection *Section =
4055 getDwarfVersion() <= 4
4056 ? Asm->getObjFileLowering().getDwarfTypesDWOSection()
4057 : Asm->getObjFileLowering().getDwarfInfoDWOSection();
4058 NewTU.setSection(Section);
4059 } else {
4060 MCSection *Section =
4061 getDwarfVersion() <= 4
4062 ? Asm->getObjFileLowering().getDwarfTypesSection(Hash: Signature)
4063 : Asm->getObjFileLowering().getDwarfInfoSection(Hash: Signature);
4064 NewTU.setSection(Section);
4065 // Non-split type units reuse the compile unit's line table.
4066 CU.applyStmtList(D&: UnitDie);
4067 }
4068
4069 // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type
4070 // units.
4071 if (useSegmentedStringOffsetsTable() && !useSplitDwarf())
4072 NewTU.addStringOffsetsStart();
4073
4074 NewTU.setType(NewTU.createTypeDIE(Ty: CTy));
4075
4076 if (TopLevelType) {
4077 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
4078 TypeUnitsUnderConstruction.clear();
4079
4080 // Types referencing entries in the address table cannot be placed in type
4081 // units.
4082 if (AddrPool.hasBeenUsed()) {
4083 AccelTypeUnitsDebugNames.clear();
4084 // Remove all the types built while building this type.
4085 // This is pessimistic as some of these types might not be dependent on
4086 // the type that used an address.
4087 for (const auto &TU : TypeUnitsToAdd)
4088 TypeSignatures.erase(Val: TU.second);
4089
4090 // Construct this type in the CU directly.
4091 // This is inefficient because all the dependent types will be rebuilt
4092 // from scratch, including building them in type units, discovering that
4093 // they depend on addresses, throwing them out and rebuilding them.
4094 setCurrentDWARF5AccelTable(DWARF5AccelTableKind::CU);
4095 CU.constructTypeDIE(Buffer&: RefDie, CTy: cast<DICompositeType>(Val: CTy));
4096 CU.updateAcceleratorTables(Context: CTy->getScope(), Ty: CTy, TyDIE: RefDie);
4097 return;
4098 }
4099
4100 // If the type wasn't dependent on fission addresses, finish adding the type
4101 // and all its dependent types.
4102 for (auto &TU : TypeUnitsToAdd) {
4103 InfoHolder.computeSizeAndOffsetsForUnit(TheU: TU.first.get());
4104 InfoHolder.emitUnit(TheU: TU.first.get(), UseOffsets: useSplitDwarf());
4105 if (getDwarfVersion() >= 5 &&
4106 getAccelTableKind() == AccelTableKind::Dwarf) {
4107 if (useSplitDwarf())
4108 AccelDebugNames.addTypeUnitSignature(U&: *TU.first);
4109 else
4110 AccelDebugNames.addTypeUnitSymbol(U&: *TU.first);
4111 }
4112 }
4113 AccelTypeUnitsDebugNames.convertDieToOffset();
4114 AccelDebugNames.addTypeEntries(Table&: AccelTypeUnitsDebugNames);
4115 AccelTypeUnitsDebugNames.clear();
4116 setCurrentDWARF5AccelTable(DWARF5AccelTableKind::CU);
4117 }
4118 CU.addDIETypeSignature(Die&: RefDie, Signature);
4119}
4120
4121// Add the Name along with its companion DIE to the appropriate accelerator
4122// table (for AccelTableKind::Dwarf it's always AccelDebugNames, for
4123// AccelTableKind::Apple, we use the table we got as an argument). If
4124// accelerator tables are disabled, this function does nothing.
4125template <typename DataT>
4126void DwarfDebug::addAccelNameImpl(
4127 const DwarfUnit &Unit,
4128 const DICompileUnit::DebugNameTableKind NameTableKind,
4129 AccelTable<DataT> &AppleAccel, StringRef Name, const DIE &Die) {
4130 if (getAccelTableKind() == AccelTableKind::None ||
4131 Unit.getUnitDie().getTag() == dwarf::DW_TAG_skeleton_unit || Name.empty())
4132 return;
4133
4134 if (getAccelTableKind() != AccelTableKind::Apple &&
4135 NameTableKind != DICompileUnit::DebugNameTableKind::Apple &&
4136 NameTableKind != DICompileUnit::DebugNameTableKind::Default)
4137 return;
4138
4139 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
4140 DwarfStringPoolEntryRef Ref = Holder.getStringPool().getEntry(Asm&: *Asm, Str: Name);
4141
4142 switch (getAccelTableKind()) {
4143 case AccelTableKind::Apple:
4144 AppleAccel.addName(Ref, Die);
4145 break;
4146 case AccelTableKind::Dwarf: {
4147 DWARF5AccelTable &Current = getCurrentDWARF5AccelTable();
4148 assert(((&Current == &AccelTypeUnitsDebugNames) ||
4149 ((&Current == &AccelDebugNames) &&
4150 (Unit.getUnitDie().getTag() != dwarf::DW_TAG_type_unit))) &&
4151 "Kind is CU but TU is being processed.");
4152 assert(((&Current == &AccelDebugNames) ||
4153 ((&Current == &AccelTypeUnitsDebugNames) &&
4154 (Unit.getUnitDie().getTag() == dwarf::DW_TAG_type_unit))) &&
4155 "Kind is TU but CU is being processed.");
4156 // The type unit can be discarded, so need to add references to final
4157 // acceleration table once we know it's complete and we emit it.
4158 Current.addName(Name: Ref, Args: Die, Args: Unit.getUniqueID(),
4159 Args: Unit.getUnitDie().getTag() == dwarf::DW_TAG_type_unit);
4160 break;
4161 }
4162 case AccelTableKind::Default:
4163 llvm_unreachable("Default should have already been resolved.");
4164 case AccelTableKind::None:
4165 llvm_unreachable("None handled above");
4166 }
4167}
4168
4169void DwarfDebug::addAccelName(
4170 const DwarfUnit &Unit,
4171 const DICompileUnit::DebugNameTableKind NameTableKind, StringRef Name,
4172 const DIE &Die) {
4173 addAccelNameImpl(Unit, NameTableKind, AppleAccel&: AccelNames, Name, Die);
4174}
4175
4176void DwarfDebug::addAccelObjC(
4177 const DwarfUnit &Unit,
4178 const DICompileUnit::DebugNameTableKind NameTableKind, StringRef Name,
4179 const DIE &Die) {
4180 // ObjC names go only into the Apple accelerator tables.
4181 if (getAccelTableKind() == AccelTableKind::Apple)
4182 addAccelNameImpl(Unit, NameTableKind, AppleAccel&: AccelObjC, Name, Die);
4183}
4184
4185void DwarfDebug::addAccelNamespace(
4186 const DwarfUnit &Unit,
4187 const DICompileUnit::DebugNameTableKind NameTableKind, StringRef Name,
4188 const DIE &Die) {
4189 addAccelNameImpl(Unit, NameTableKind, AppleAccel&: AccelNamespace, Name, Die);
4190}
4191
4192void DwarfDebug::addAccelType(
4193 const DwarfUnit &Unit,
4194 const DICompileUnit::DebugNameTableKind NameTableKind, StringRef Name,
4195 const DIE &Die, char Flags) {
4196 addAccelNameImpl(Unit, NameTableKind, AppleAccel&: AccelTypes, Name, Die);
4197}
4198
4199uint16_t DwarfDebug::getDwarfVersion() const {
4200 return Asm->OutStreamer->getContext().getDwarfVersion();
4201}
4202
4203dwarf::Form DwarfDebug::getDwarfSectionOffsetForm() const {
4204 if (Asm->getDwarfVersion() >= 4)
4205 return dwarf::Form::DW_FORM_sec_offset;
4206 assert((!Asm->isDwarf64() || (Asm->getDwarfVersion() == 3)) &&
4207 "DWARF64 is not defined prior DWARFv3");
4208 return Asm->isDwarf64() ? dwarf::Form::DW_FORM_data8
4209 : dwarf::Form::DW_FORM_data4;
4210}
4211
4212const MCSymbol *DwarfDebug::getSectionLabel(const MCSection *S) {
4213 return SectionLabels.lookup(Val: S);
4214}
4215
4216void DwarfDebug::insertSectionLabel(const MCSymbol *S) {
4217 if (SectionLabels.insert(KV: std::make_pair(x: &S->getSection(), y&: S)).second)
4218 if (useSplitDwarf() || getDwarfVersion() >= 5)
4219 AddrPool.getIndex(Sym: S);
4220}
4221
4222std::optional<MD5::MD5Result>
4223DwarfDebug::getMD5AsBytes(const DIFile *File) const {
4224 assert(File);
4225 if (getDwarfVersion() < 5)
4226 return std::nullopt;
4227 std::optional<DIFile::ChecksumInfo<StringRef>> Checksum = File->getChecksum();
4228 if (!Checksum || Checksum->Kind != DIFile::CSK_MD5)
4229 return std::nullopt;
4230
4231 // Convert the string checksum to an MD5Result for the streamer.
4232 // The verifier validates the checksum so we assume it's okay.
4233 // An MD5 checksum is 16 bytes.
4234 std::string ChecksumString = fromHex(Input: Checksum->Value);
4235 MD5::MD5Result CKMem;
4236 llvm::copy(Range&: ChecksumString, Out: CKMem.data());
4237 return CKMem;
4238}
4239
4240bool DwarfDebug::alwaysUseRanges(const DwarfCompileUnit &CU) const {
4241 if (MinimizeAddr == MinimizeAddrInV5::Ranges)
4242 return true;
4243 if (MinimizeAddr != MinimizeAddrInV5::Default)
4244 return false;
4245 if (useSplitDwarf())
4246 return true;
4247 return false;
4248}
4249
4250void DwarfDebug::beginCodeAlignment(const MachineBasicBlock &MBB) {
4251 if (MBB.getAlignment() == Align(1))
4252 return;
4253
4254 auto *SP = MBB.getParent()->getFunction().getSubprogram();
4255 bool NoDebug =
4256 !SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug;
4257
4258 if (NoDebug)
4259 return;
4260
4261 auto PrevLoc = Asm->OutStreamer->getContext().getCurrentDwarfLoc();
4262 if (PrevLoc.getLine()) {
4263 Asm->OutStreamer->emitDwarfLocDirective(
4264 FileNo: PrevLoc.getFileNum(), Line: 0, Column: PrevLoc.getColumn(), Flags: 0, Isa: 0, Discriminator: 0, FileName: StringRef());
4265 MCDwarfLineEntry::make(MCOS: Asm->OutStreamer.get(),
4266 Section: Asm->OutStreamer->getCurrentSectionOnly());
4267 }
4268}
4269