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