1*0b57cec5SDimitry Andric //===- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ----------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file contains support for writing dwarf debug info into asm files. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "DwarfDebug.h" 14*0b57cec5SDimitry Andric #include "ByteStreamer.h" 15*0b57cec5SDimitry Andric #include "DIEHash.h" 16*0b57cec5SDimitry Andric #include "DwarfCompileUnit.h" 17*0b57cec5SDimitry Andric #include "DwarfExpression.h" 18*0b57cec5SDimitry Andric #include "DwarfUnit.h" 19*0b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 208bcb0991SDimitry Andric #include "llvm/ADT/Statistic.h" 21*0b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 22*0b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h" 24*0b57cec5SDimitry Andric #include "llvm/CodeGen/DIE.h" 25*0b57cec5SDimitry Andric #include "llvm/CodeGen/LexicalScopes.h" 26*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 27*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 28*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 29*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 30*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 318bcb0991SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 32*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 33*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 34*0b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" 3581ad6265SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFExpression.h" 36*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 37*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 38*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 39*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 40*0b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 41*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 42*0b57cec5SDimitry Andric #include "llvm/MC/MCSection.h" 43*0b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 44*0b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 45*0b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h" 46*0b57cec5SDimitry Andric #include "llvm/MC/MachineLocation.h" 47*0b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h" 48*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 49*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 50*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 51*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 52*0b57cec5SDimitry Andric #include "llvm/Support/MD5.h" 53*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 54*0b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h" 55*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 56*0b57cec5SDimitry Andric #include <algorithm> 57*0b57cec5SDimitry Andric #include <cstddef> 58*0b57cec5SDimitry Andric #include <iterator> 59bdd1243dSDimitry Andric #include <optional> 60*0b57cec5SDimitry Andric #include <string> 61*0b57cec5SDimitry Andric 62*0b57cec5SDimitry Andric using namespace llvm; 63*0b57cec5SDimitry Andric 64*0b57cec5SDimitry Andric #define DEBUG_TYPE "dwarfdebug" 65*0b57cec5SDimitry Andric 668bcb0991SDimitry Andric STATISTIC(NumCSParams, "Number of dbg call site params created"); 678bcb0991SDimitry Andric 68*0b57cec5SDimitry Andric static cl::opt<bool> UseDwarfRangesBaseAddressSpecifier( 69*0b57cec5SDimitry Andric "use-dwarf-ranges-base-address-specifier", cl::Hidden, 70*0b57cec5SDimitry Andric cl::desc("Use base address specifiers in debug_ranges"), cl::init(false)); 71*0b57cec5SDimitry Andric 72*0b57cec5SDimitry Andric static cl::opt<bool> GenerateARangeSection("generate-arange-section", 73*0b57cec5SDimitry Andric cl::Hidden, 74*0b57cec5SDimitry Andric cl::desc("Generate dwarf aranges"), 75*0b57cec5SDimitry Andric cl::init(false)); 76*0b57cec5SDimitry Andric 77*0b57cec5SDimitry Andric static cl::opt<bool> 78*0b57cec5SDimitry Andric GenerateDwarfTypeUnits("generate-type-units", cl::Hidden, 79*0b57cec5SDimitry Andric cl::desc("Generate DWARF4 type units."), 80*0b57cec5SDimitry Andric cl::init(false)); 81*0b57cec5SDimitry Andric 82*0b57cec5SDimitry Andric static cl::opt<bool> SplitDwarfCrossCuReferences( 83*0b57cec5SDimitry Andric "split-dwarf-cross-cu-references", cl::Hidden, 84*0b57cec5SDimitry Andric cl::desc("Enable cross-cu references in DWO files"), cl::init(false)); 85*0b57cec5SDimitry Andric 86*0b57cec5SDimitry Andric enum DefaultOnOff { Default, Enable, Disable }; 87*0b57cec5SDimitry Andric 88*0b57cec5SDimitry Andric static cl::opt<DefaultOnOff> UnknownLocations( 89*0b57cec5SDimitry Andric "use-unknown-locations", cl::Hidden, 90*0b57cec5SDimitry Andric cl::desc("Make an absence of debug location information explicit."), 91*0b57cec5SDimitry Andric cl::values(clEnumVal(Default, "At top of block or after label"), 92*0b57cec5SDimitry Andric clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")), 93*0b57cec5SDimitry Andric cl::init(Default)); 94*0b57cec5SDimitry Andric 95*0b57cec5SDimitry Andric static cl::opt<AccelTableKind> AccelTables( 96*0b57cec5SDimitry Andric "accel-tables", cl::Hidden, cl::desc("Output dwarf accelerator tables."), 97*0b57cec5SDimitry Andric cl::values(clEnumValN(AccelTableKind::Default, "Default", 98*0b57cec5SDimitry Andric "Default for platform"), 99*0b57cec5SDimitry Andric clEnumValN(AccelTableKind::None, "Disable", "Disabled."), 100*0b57cec5SDimitry Andric clEnumValN(AccelTableKind::Apple, "Apple", "Apple"), 101*0b57cec5SDimitry Andric clEnumValN(AccelTableKind::Dwarf, "Dwarf", "DWARF")), 102*0b57cec5SDimitry Andric cl::init(AccelTableKind::Default)); 103*0b57cec5SDimitry Andric 104*0b57cec5SDimitry Andric static cl::opt<DefaultOnOff> 105*0b57cec5SDimitry Andric DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden, 106*0b57cec5SDimitry Andric cl::desc("Use inlined strings rather than string section."), 107*0b57cec5SDimitry Andric cl::values(clEnumVal(Default, "Default for platform"), 108*0b57cec5SDimitry Andric clEnumVal(Enable, "Enabled"), 109*0b57cec5SDimitry Andric clEnumVal(Disable, "Disabled")), 110*0b57cec5SDimitry Andric cl::init(Default)); 111*0b57cec5SDimitry Andric 112*0b57cec5SDimitry Andric static cl::opt<bool> 113*0b57cec5SDimitry Andric NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden, 114*0b57cec5SDimitry Andric cl::desc("Disable emission .debug_ranges section."), 115*0b57cec5SDimitry Andric cl::init(false)); 116*0b57cec5SDimitry Andric 117*0b57cec5SDimitry Andric static cl::opt<DefaultOnOff> DwarfSectionsAsReferences( 118*0b57cec5SDimitry Andric "dwarf-sections-as-references", cl::Hidden, 119*0b57cec5SDimitry Andric cl::desc("Use sections+offset as references rather than labels."), 120*0b57cec5SDimitry Andric cl::values(clEnumVal(Default, "Default for platform"), 121*0b57cec5SDimitry Andric clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")), 122*0b57cec5SDimitry Andric cl::init(Default)); 123*0b57cec5SDimitry Andric 124e8d8bef9SDimitry Andric static cl::opt<bool> 125e8d8bef9SDimitry Andric UseGNUDebugMacro("use-gnu-debug-macro", cl::Hidden, 126e8d8bef9SDimitry Andric cl::desc("Emit the GNU .debug_macro format with DWARF <5"), 127e8d8bef9SDimitry Andric cl::init(false)); 128e8d8bef9SDimitry Andric 129e8d8bef9SDimitry Andric static cl::opt<DefaultOnOff> DwarfOpConvert( 130e8d8bef9SDimitry Andric "dwarf-op-convert", cl::Hidden, 131e8d8bef9SDimitry Andric cl::desc("Enable use of the DWARFv5 DW_OP_convert operator"), 132e8d8bef9SDimitry Andric cl::values(clEnumVal(Default, "Default for platform"), 133e8d8bef9SDimitry Andric clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")), 134e8d8bef9SDimitry Andric cl::init(Default)); 135e8d8bef9SDimitry Andric 136*0b57cec5SDimitry Andric enum LinkageNameOption { 137*0b57cec5SDimitry Andric DefaultLinkageNames, 138*0b57cec5SDimitry Andric AllLinkageNames, 139*0b57cec5SDimitry Andric AbstractLinkageNames 140*0b57cec5SDimitry Andric }; 141*0b57cec5SDimitry Andric 142*0b57cec5SDimitry Andric static cl::opt<LinkageNameOption> 143*0b57cec5SDimitry Andric DwarfLinkageNames("dwarf-linkage-names", cl::Hidden, 144*0b57cec5SDimitry Andric cl::desc("Which DWARF linkage-name attributes to emit."), 145*0b57cec5SDimitry Andric cl::values(clEnumValN(DefaultLinkageNames, "Default", 146*0b57cec5SDimitry Andric "Default for platform"), 147*0b57cec5SDimitry Andric clEnumValN(AllLinkageNames, "All", "All"), 148*0b57cec5SDimitry Andric clEnumValN(AbstractLinkageNames, "Abstract", 149*0b57cec5SDimitry Andric "Abstract subprograms")), 150*0b57cec5SDimitry Andric cl::init(DefaultLinkageNames)); 151*0b57cec5SDimitry Andric 152e8d8bef9SDimitry Andric static cl::opt<DwarfDebug::MinimizeAddrInV5> MinimizeAddrInV5Option( 153e8d8bef9SDimitry Andric "minimize-addr-in-v5", cl::Hidden, 154e8d8bef9SDimitry Andric cl::desc("Always use DW_AT_ranges in DWARFv5 whenever it could allow more " 155e8d8bef9SDimitry Andric "address pool entry sharing to reduce relocations/object size"), 156e8d8bef9SDimitry Andric cl::values(clEnumValN(DwarfDebug::MinimizeAddrInV5::Default, "Default", 157e8d8bef9SDimitry Andric "Default address minimization strategy"), 158e8d8bef9SDimitry Andric clEnumValN(DwarfDebug::MinimizeAddrInV5::Ranges, "Ranges", 159e8d8bef9SDimitry Andric "Use rnglists for contiguous ranges if that allows " 160e8d8bef9SDimitry Andric "using a pre-existing base address"), 161fe6060f1SDimitry Andric clEnumValN(DwarfDebug::MinimizeAddrInV5::Expressions, 162fe6060f1SDimitry Andric "Expressions", 163fe6060f1SDimitry Andric "Use exprloc addrx+offset expressions for any " 164fe6060f1SDimitry Andric "address with a prior base address"), 165fe6060f1SDimitry Andric clEnumValN(DwarfDebug::MinimizeAddrInV5::Form, "Form", 166fe6060f1SDimitry Andric "Use addrx+offset extension form for any address " 167fe6060f1SDimitry Andric "with a prior base address"), 168e8d8bef9SDimitry Andric clEnumValN(DwarfDebug::MinimizeAddrInV5::Disabled, "Disabled", 169e8d8bef9SDimitry Andric "Stuff")), 170e8d8bef9SDimitry Andric cl::init(DwarfDebug::MinimizeAddrInV5::Default)); 1715ffd83dbSDimitry Andric 172*0b57cec5SDimitry Andric static constexpr unsigned ULEB128PadSize = 4; 173*0b57cec5SDimitry Andric 174*0b57cec5SDimitry Andric void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) { 175e8d8bef9SDimitry Andric getActiveStreamer().emitInt8( 176*0b57cec5SDimitry Andric Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op) 177*0b57cec5SDimitry Andric : dwarf::OperationEncodingString(Op)); 178*0b57cec5SDimitry Andric } 179*0b57cec5SDimitry Andric 180*0b57cec5SDimitry Andric void DebugLocDwarfExpression::emitSigned(int64_t Value) { 1815ffd83dbSDimitry Andric getActiveStreamer().emitSLEB128(Value, Twine(Value)); 182*0b57cec5SDimitry Andric } 183*0b57cec5SDimitry Andric 184*0b57cec5SDimitry Andric void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) { 1855ffd83dbSDimitry Andric getActiveStreamer().emitULEB128(Value, Twine(Value)); 186*0b57cec5SDimitry Andric } 187*0b57cec5SDimitry Andric 188*0b57cec5SDimitry Andric void DebugLocDwarfExpression::emitData1(uint8_t Value) { 189e8d8bef9SDimitry Andric getActiveStreamer().emitInt8(Value, Twine(Value)); 190*0b57cec5SDimitry Andric } 191*0b57cec5SDimitry Andric 192*0b57cec5SDimitry Andric void DebugLocDwarfExpression::emitBaseTypeRef(uint64_t Idx) { 193*0b57cec5SDimitry Andric assert(Idx < (1ULL << (ULEB128PadSize * 7)) && "Idx wont fit"); 1945ffd83dbSDimitry Andric getActiveStreamer().emitULEB128(Idx, Twine(Idx), ULEB128PadSize); 195*0b57cec5SDimitry Andric } 196*0b57cec5SDimitry Andric 197*0b57cec5SDimitry Andric bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI, 198e8d8bef9SDimitry Andric llvm::Register MachineReg) { 199*0b57cec5SDimitry Andric // This information is not available while emitting .debug_loc entries. 200*0b57cec5SDimitry Andric return false; 201*0b57cec5SDimitry Andric } 202*0b57cec5SDimitry Andric 2038bcb0991SDimitry Andric void DebugLocDwarfExpression::enableTemporaryBuffer() { 2048bcb0991SDimitry Andric assert(!IsBuffering && "Already buffering?"); 2058bcb0991SDimitry Andric if (!TmpBuf) 2068bcb0991SDimitry Andric TmpBuf = std::make_unique<TempBuffer>(OutBS.GenerateComments); 2078bcb0991SDimitry Andric IsBuffering = true; 2088bcb0991SDimitry Andric } 2098bcb0991SDimitry Andric 2108bcb0991SDimitry Andric void DebugLocDwarfExpression::disableTemporaryBuffer() { IsBuffering = false; } 2118bcb0991SDimitry Andric 2128bcb0991SDimitry Andric unsigned DebugLocDwarfExpression::getTemporaryBufferSize() { 2138bcb0991SDimitry Andric return TmpBuf ? TmpBuf->Bytes.size() : 0; 2148bcb0991SDimitry Andric } 2158bcb0991SDimitry Andric 2168bcb0991SDimitry Andric void DebugLocDwarfExpression::commitTemporaryBuffer() { 2178bcb0991SDimitry Andric if (!TmpBuf) 2188bcb0991SDimitry Andric return; 2198bcb0991SDimitry Andric for (auto Byte : enumerate(TmpBuf->Bytes)) { 2208bcb0991SDimitry Andric const char *Comment = (Byte.index() < TmpBuf->Comments.size()) 2218bcb0991SDimitry Andric ? TmpBuf->Comments[Byte.index()].c_str() 2228bcb0991SDimitry Andric : ""; 223e8d8bef9SDimitry Andric OutBS.emitInt8(Byte.value(), Comment); 2248bcb0991SDimitry Andric } 2258bcb0991SDimitry Andric TmpBuf->Bytes.clear(); 2268bcb0991SDimitry Andric TmpBuf->Comments.clear(); 227*0b57cec5SDimitry Andric } 228*0b57cec5SDimitry Andric 229*0b57cec5SDimitry Andric const DIType *DbgVariable::getType() const { 2308bcb0991SDimitry Andric return getVariable()->getType(); 231*0b57cec5SDimitry Andric } 232*0b57cec5SDimitry Andric 233*0b57cec5SDimitry Andric /// Get .debug_loc entry for the instruction range starting at MI. 234*0b57cec5SDimitry Andric static DbgValueLoc getDebugLocValue(const MachineInstr *MI) { 235*0b57cec5SDimitry Andric const DIExpression *Expr = MI->getDebugExpression(); 236fe6060f1SDimitry Andric const bool IsVariadic = MI->isDebugValueList(); 237fe6060f1SDimitry Andric assert(MI->getNumOperands() >= 3); 238fe6060f1SDimitry Andric SmallVector<DbgValueLocEntry, 4> DbgValueLocEntries; 239fe6060f1SDimitry Andric for (const MachineOperand &Op : MI->debug_operands()) { 240fe6060f1SDimitry Andric if (Op.isReg()) { 241fe6060f1SDimitry Andric MachineLocation MLoc(Op.getReg(), 242fe6060f1SDimitry Andric MI->isNonListDebugValue() && MI->isDebugOffsetImm()); 243fe6060f1SDimitry Andric DbgValueLocEntries.push_back(DbgValueLocEntry(MLoc)); 244fe6060f1SDimitry Andric } else if (Op.isTargetIndex()) { 245fe6060f1SDimitry Andric DbgValueLocEntries.push_back( 246fe6060f1SDimitry Andric DbgValueLocEntry(TargetIndexLocation(Op.getIndex(), Op.getOffset()))); 247fe6060f1SDimitry Andric } else if (Op.isImm()) 248fe6060f1SDimitry Andric DbgValueLocEntries.push_back(DbgValueLocEntry(Op.getImm())); 249fe6060f1SDimitry Andric else if (Op.isFPImm()) 250fe6060f1SDimitry Andric DbgValueLocEntries.push_back(DbgValueLocEntry(Op.getFPImm())); 251fe6060f1SDimitry Andric else if (Op.isCImm()) 252fe6060f1SDimitry Andric DbgValueLocEntries.push_back(DbgValueLocEntry(Op.getCImm())); 253fe6060f1SDimitry Andric else 254fe6060f1SDimitry Andric llvm_unreachable("Unexpected debug operand in DBG_VALUE* instruction!"); 255*0b57cec5SDimitry Andric } 256fe6060f1SDimitry Andric return DbgValueLoc(Expr, DbgValueLocEntries, IsVariadic); 257*0b57cec5SDimitry Andric } 258*0b57cec5SDimitry Andric 259*0b57cec5SDimitry Andric void DbgVariable::initializeDbgValue(const MachineInstr *DbgValue) { 260*0b57cec5SDimitry Andric assert(FrameIndexExprs.empty() && "Already initialized?"); 261*0b57cec5SDimitry Andric assert(!ValueLoc.get() && "Already initialized?"); 262*0b57cec5SDimitry Andric 263*0b57cec5SDimitry Andric assert(getVariable() == DbgValue->getDebugVariable() && "Wrong variable"); 264*0b57cec5SDimitry Andric assert(getInlinedAt() == DbgValue->getDebugLoc()->getInlinedAt() && 265*0b57cec5SDimitry Andric "Wrong inlined-at"); 266*0b57cec5SDimitry Andric 2678bcb0991SDimitry Andric ValueLoc = std::make_unique<DbgValueLoc>(getDebugLocValue(DbgValue)); 268*0b57cec5SDimitry Andric if (auto *E = DbgValue->getDebugExpression()) 269*0b57cec5SDimitry Andric if (E->getNumElements()) 270*0b57cec5SDimitry Andric FrameIndexExprs.push_back({0, E}); 271*0b57cec5SDimitry Andric } 272*0b57cec5SDimitry Andric 273*0b57cec5SDimitry Andric ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const { 274*0b57cec5SDimitry Andric if (FrameIndexExprs.size() == 1) 275*0b57cec5SDimitry Andric return FrameIndexExprs; 276*0b57cec5SDimitry Andric 277*0b57cec5SDimitry Andric assert(llvm::all_of(FrameIndexExprs, 278*0b57cec5SDimitry Andric [](const FrameIndexExpr &A) { 279*0b57cec5SDimitry Andric return A.Expr->isFragment(); 280*0b57cec5SDimitry Andric }) && 281*0b57cec5SDimitry Andric "multiple FI expressions without DW_OP_LLVM_fragment"); 282*0b57cec5SDimitry Andric llvm::sort(FrameIndexExprs, 283*0b57cec5SDimitry Andric [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool { 284*0b57cec5SDimitry Andric return A.Expr->getFragmentInfo()->OffsetInBits < 285*0b57cec5SDimitry Andric B.Expr->getFragmentInfo()->OffsetInBits; 286*0b57cec5SDimitry Andric }); 287*0b57cec5SDimitry Andric 288*0b57cec5SDimitry Andric return FrameIndexExprs; 289*0b57cec5SDimitry Andric } 290*0b57cec5SDimitry Andric 291*0b57cec5SDimitry Andric void DbgVariable::addMMIEntry(const DbgVariable &V) { 292*0b57cec5SDimitry Andric assert(DebugLocListIndex == ~0U && !ValueLoc.get() && "not an MMI entry"); 293*0b57cec5SDimitry Andric assert(V.DebugLocListIndex == ~0U && !V.ValueLoc.get() && "not an MMI entry"); 294*0b57cec5SDimitry Andric assert(V.getVariable() == getVariable() && "conflicting variable"); 295*0b57cec5SDimitry Andric assert(V.getInlinedAt() == getInlinedAt() && "conflicting inlined-at location"); 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric assert(!FrameIndexExprs.empty() && "Expected an MMI entry"); 298*0b57cec5SDimitry Andric assert(!V.FrameIndexExprs.empty() && "Expected an MMI entry"); 299*0b57cec5SDimitry Andric 300*0b57cec5SDimitry Andric // FIXME: This logic should not be necessary anymore, as we now have proper 301*0b57cec5SDimitry Andric // deduplication. However, without it, we currently run into the assertion 302*0b57cec5SDimitry Andric // below, which means that we are likely dealing with broken input, i.e. two 303*0b57cec5SDimitry Andric // non-fragment entries for the same variable at different frame indices. 304*0b57cec5SDimitry Andric if (FrameIndexExprs.size()) { 305*0b57cec5SDimitry Andric auto *Expr = FrameIndexExprs.back().Expr; 306*0b57cec5SDimitry Andric if (!Expr || !Expr->isFragment()) 307*0b57cec5SDimitry Andric return; 308*0b57cec5SDimitry Andric } 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andric for (const auto &FIE : V.FrameIndexExprs) 311*0b57cec5SDimitry Andric // Ignore duplicate entries. 312*0b57cec5SDimitry Andric if (llvm::none_of(FrameIndexExprs, [&](const FrameIndexExpr &Other) { 313*0b57cec5SDimitry Andric return FIE.FI == Other.FI && FIE.Expr == Other.Expr; 314*0b57cec5SDimitry Andric })) 315*0b57cec5SDimitry Andric FrameIndexExprs.push_back(FIE); 316*0b57cec5SDimitry Andric 317*0b57cec5SDimitry Andric assert((FrameIndexExprs.size() == 1 || 318*0b57cec5SDimitry Andric llvm::all_of(FrameIndexExprs, 319*0b57cec5SDimitry Andric [](FrameIndexExpr &FIE) { 320*0b57cec5SDimitry Andric return FIE.Expr && FIE.Expr->isFragment(); 321*0b57cec5SDimitry Andric })) && 322*0b57cec5SDimitry Andric "conflicting locations for variable"); 323*0b57cec5SDimitry Andric } 324*0b57cec5SDimitry Andric 325*0b57cec5SDimitry Andric static AccelTableKind computeAccelTableKind(unsigned DwarfVersion, 326*0b57cec5SDimitry Andric bool GenerateTypeUnits, 327*0b57cec5SDimitry Andric DebuggerKind Tuning, 328*0b57cec5SDimitry Andric const Triple &TT) { 329*0b57cec5SDimitry Andric // Honor an explicit request. 330*0b57cec5SDimitry Andric if (AccelTables != AccelTableKind::Default) 331*0b57cec5SDimitry Andric return AccelTables; 332*0b57cec5SDimitry Andric 333*0b57cec5SDimitry Andric // Accelerator tables with type units are currently not supported. 334*0b57cec5SDimitry Andric if (GenerateTypeUnits) 335*0b57cec5SDimitry Andric return AccelTableKind::None; 336*0b57cec5SDimitry Andric 337*0b57cec5SDimitry Andric // Accelerator tables get emitted if targetting DWARF v5 or LLDB. DWARF v5 338*0b57cec5SDimitry Andric // always implies debug_names. For lower standard versions we use apple 339*0b57cec5SDimitry Andric // accelerator tables on apple platforms and debug_names elsewhere. 340*0b57cec5SDimitry Andric if (DwarfVersion >= 5) 341*0b57cec5SDimitry Andric return AccelTableKind::Dwarf; 342*0b57cec5SDimitry Andric if (Tuning == DebuggerKind::LLDB) 343*0b57cec5SDimitry Andric return TT.isOSBinFormatMachO() ? AccelTableKind::Apple 344*0b57cec5SDimitry Andric : AccelTableKind::Dwarf; 345*0b57cec5SDimitry Andric return AccelTableKind::None; 346*0b57cec5SDimitry Andric } 347*0b57cec5SDimitry Andric 348e8d8bef9SDimitry Andric DwarfDebug::DwarfDebug(AsmPrinter *A) 349*0b57cec5SDimitry Andric : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()), 350*0b57cec5SDimitry Andric InfoHolder(A, "info_string", DIEValueAllocator), 351*0b57cec5SDimitry Andric SkeletonHolder(A, "skel_string", DIEValueAllocator), 352*0b57cec5SDimitry Andric IsDarwin(A->TM.getTargetTriple().isOSDarwin()) { 353*0b57cec5SDimitry Andric const Triple &TT = Asm->TM.getTargetTriple(); 354*0b57cec5SDimitry Andric 355*0b57cec5SDimitry Andric // Make sure we know our "debugger tuning". The target option takes 356*0b57cec5SDimitry Andric // precedence; fall back to triple-based defaults. 357*0b57cec5SDimitry Andric if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default) 358*0b57cec5SDimitry Andric DebuggerTuning = Asm->TM.Options.DebuggerTuning; 359*0b57cec5SDimitry Andric else if (IsDarwin) 360*0b57cec5SDimitry Andric DebuggerTuning = DebuggerKind::LLDB; 36181ad6265SDimitry Andric else if (TT.isPS()) 362*0b57cec5SDimitry Andric DebuggerTuning = DebuggerKind::SCE; 363fe6060f1SDimitry Andric else if (TT.isOSAIX()) 364fe6060f1SDimitry Andric DebuggerTuning = DebuggerKind::DBX; 365*0b57cec5SDimitry Andric else 366*0b57cec5SDimitry Andric DebuggerTuning = DebuggerKind::GDB; 367*0b57cec5SDimitry Andric 368*0b57cec5SDimitry Andric if (DwarfInlinedStrings == Default) 369fe6060f1SDimitry Andric UseInlineStrings = TT.isNVPTX() || tuneForDBX(); 370*0b57cec5SDimitry Andric else 371*0b57cec5SDimitry Andric UseInlineStrings = DwarfInlinedStrings == Enable; 372*0b57cec5SDimitry Andric 373*0b57cec5SDimitry Andric UseLocSection = !TT.isNVPTX(); 374*0b57cec5SDimitry Andric 375*0b57cec5SDimitry Andric HasAppleExtensionAttributes = tuneForLLDB(); 376*0b57cec5SDimitry Andric 377*0b57cec5SDimitry Andric // Handle split DWARF. 378*0b57cec5SDimitry Andric HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty(); 379*0b57cec5SDimitry Andric 380*0b57cec5SDimitry Andric // SCE defaults to linkage names only for abstract subprograms. 381*0b57cec5SDimitry Andric if (DwarfLinkageNames == DefaultLinkageNames) 382*0b57cec5SDimitry Andric UseAllLinkageNames = !tuneForSCE(); 383*0b57cec5SDimitry Andric else 384*0b57cec5SDimitry Andric UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames; 385*0b57cec5SDimitry Andric 386*0b57cec5SDimitry Andric unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion; 387*0b57cec5SDimitry Andric unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber 388*0b57cec5SDimitry Andric : MMI->getModule()->getDwarfVersion(); 389*0b57cec5SDimitry Andric // Use dwarf 4 by default if nothing is requested. For NVPTX, use dwarf 2. 390*0b57cec5SDimitry Andric DwarfVersion = 391*0b57cec5SDimitry Andric TT.isNVPTX() ? 2 : (DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION); 392*0b57cec5SDimitry Andric 393fe6060f1SDimitry Andric bool Dwarf64 = DwarfVersion >= 3 && // DWARF64 was introduced in DWARFv3. 394fe6060f1SDimitry Andric TT.isArch64Bit(); // DWARF64 requires 64-bit relocations. 395fe6060f1SDimitry Andric 396fe6060f1SDimitry Andric // Support DWARF64 397fe6060f1SDimitry Andric // 1: For ELF when requested. 398fe6060f1SDimitry Andric // 2: For XCOFF64: the AIX assembler will fill in debug section lengths 399fe6060f1SDimitry Andric // according to the DWARF64 format for 64-bit assembly, so we must use 400fe6060f1SDimitry Andric // DWARF64 in the compiler too for 64-bit mode. 401fe6060f1SDimitry Andric Dwarf64 &= 402fe6060f1SDimitry Andric ((Asm->TM.Options.MCOptions.Dwarf64 || MMI->getModule()->isDwarf64()) && 403fe6060f1SDimitry Andric TT.isOSBinFormatELF()) || 404fe6060f1SDimitry Andric TT.isOSBinFormatXCOFF(); 405fe6060f1SDimitry Andric 406fe6060f1SDimitry Andric if (!Dwarf64 && TT.isArch64Bit() && TT.isOSBinFormatXCOFF()) 407fe6060f1SDimitry Andric report_fatal_error("XCOFF requires DWARF64 for 64-bit mode!"); 408e8d8bef9SDimitry Andric 409*0b57cec5SDimitry Andric UseRangesSection = !NoDwarfRangesSection && !TT.isNVPTX(); 410*0b57cec5SDimitry Andric 411*0b57cec5SDimitry Andric // Use sections as references. Force for NVPTX. 412*0b57cec5SDimitry Andric if (DwarfSectionsAsReferences == Default) 413*0b57cec5SDimitry Andric UseSectionsAsReferences = TT.isNVPTX(); 414*0b57cec5SDimitry Andric else 415*0b57cec5SDimitry Andric UseSectionsAsReferences = DwarfSectionsAsReferences == Enable; 416*0b57cec5SDimitry Andric 417*0b57cec5SDimitry Andric // Don't generate type units for unsupported object file formats. 418e8d8bef9SDimitry Andric GenerateTypeUnits = (A->TM.getTargetTriple().isOSBinFormatELF() || 419e8d8bef9SDimitry Andric A->TM.getTargetTriple().isOSBinFormatWasm()) && 420e8d8bef9SDimitry Andric GenerateDwarfTypeUnits; 421*0b57cec5SDimitry Andric 422*0b57cec5SDimitry Andric TheAccelTableKind = computeAccelTableKind( 423*0b57cec5SDimitry Andric DwarfVersion, GenerateTypeUnits, DebuggerTuning, A->TM.getTargetTriple()); 424*0b57cec5SDimitry Andric 425*0b57cec5SDimitry Andric // Work around a GDB bug. GDB doesn't support the standard opcode; 426*0b57cec5SDimitry Andric // SCE doesn't support GNU's; LLDB prefers the standard opcode, which 427*0b57cec5SDimitry Andric // is defined as of DWARF 3. 428*0b57cec5SDimitry Andric // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented 429*0b57cec5SDimitry Andric // https://sourceware.org/bugzilla/show_bug.cgi?id=11616 430*0b57cec5SDimitry Andric UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3; 431*0b57cec5SDimitry Andric 432bdd1243dSDimitry Andric UseDWARF2Bitfields = DwarfVersion < 4; 433*0b57cec5SDimitry Andric 434*0b57cec5SDimitry Andric // The DWARF v5 string offsets table has - possibly shared - contributions 435*0b57cec5SDimitry Andric // from each compile and type unit each preceded by a header. The string 436*0b57cec5SDimitry Andric // offsets table used by the pre-DWARF v5 split-DWARF implementation uses 437*0b57cec5SDimitry Andric // a monolithic string offsets table without any header. 438*0b57cec5SDimitry Andric UseSegmentedStringOffsetsTable = DwarfVersion >= 5; 439*0b57cec5SDimitry Andric 4405ffd83dbSDimitry Andric // Emit call-site-param debug info for GDB and LLDB, if the target supports 4415ffd83dbSDimitry Andric // the debug entry values feature. It can also be enabled explicitly. 442e8d8bef9SDimitry Andric EmitDebugEntryValues = Asm->TM.Options.ShouldEmitDebugEntryValues(); 443e8d8bef9SDimitry Andric 444e8d8bef9SDimitry Andric // It is unclear if the GCC .debug_macro extension is well-specified 445e8d8bef9SDimitry Andric // for split DWARF. For now, do not allow LLVM to emit it. 446e8d8bef9SDimitry Andric UseDebugMacroSection = 447e8d8bef9SDimitry Andric DwarfVersion >= 5 || (UseGNUDebugMacro && !useSplitDwarf()); 448e8d8bef9SDimitry Andric if (DwarfOpConvert == Default) 449e8d8bef9SDimitry Andric EnableOpConvert = !((tuneForGDB() && useSplitDwarf()) || (tuneForLLDB() && !TT.isOSBinFormatMachO())); 450e8d8bef9SDimitry Andric else 451e8d8bef9SDimitry Andric EnableOpConvert = (DwarfOpConvert == Enable); 452e8d8bef9SDimitry Andric 453e8d8bef9SDimitry Andric // Split DWARF would benefit object size significantly by trading reductions 454e8d8bef9SDimitry Andric // in address pool usage for slightly increased range list encodings. 455e8d8bef9SDimitry Andric if (DwarfVersion >= 5) { 456e8d8bef9SDimitry Andric MinimizeAddr = MinimizeAddrInV5Option; 457e8d8bef9SDimitry Andric // FIXME: In the future, enable this by default for Split DWARF where the 458e8d8bef9SDimitry Andric // tradeoff is more pronounced due to being able to offload the range 459e8d8bef9SDimitry Andric // lists to the dwo file and shrink object files/reduce relocations there. 460e8d8bef9SDimitry Andric if (MinimizeAddr == MinimizeAddrInV5::Default) 461e8d8bef9SDimitry Andric MinimizeAddr = MinimizeAddrInV5::Disabled; 462e8d8bef9SDimitry Andric } 4635ffd83dbSDimitry Andric 464*0b57cec5SDimitry Andric Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion); 465e8d8bef9SDimitry Andric Asm->OutStreamer->getContext().setDwarfFormat(Dwarf64 ? dwarf::DWARF64 466e8d8bef9SDimitry Andric : dwarf::DWARF32); 467*0b57cec5SDimitry Andric } 468*0b57cec5SDimitry Andric 469*0b57cec5SDimitry Andric // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h. 470*0b57cec5SDimitry Andric DwarfDebug::~DwarfDebug() = default; 471*0b57cec5SDimitry Andric 472*0b57cec5SDimitry Andric static bool isObjCClass(StringRef Name) { 473*0b57cec5SDimitry Andric return Name.startswith("+") || Name.startswith("-"); 474*0b57cec5SDimitry Andric } 475*0b57cec5SDimitry Andric 476*0b57cec5SDimitry Andric static bool hasObjCCategory(StringRef Name) { 477*0b57cec5SDimitry Andric if (!isObjCClass(Name)) 478*0b57cec5SDimitry Andric return false; 479*0b57cec5SDimitry Andric 480349cc55cSDimitry Andric return Name.contains(") "); 481*0b57cec5SDimitry Andric } 482*0b57cec5SDimitry Andric 483*0b57cec5SDimitry Andric static void getObjCClassCategory(StringRef In, StringRef &Class, 484*0b57cec5SDimitry Andric StringRef &Category) { 485*0b57cec5SDimitry Andric if (!hasObjCCategory(In)) { 486*0b57cec5SDimitry Andric Class = In.slice(In.find('[') + 1, In.find(' ')); 487*0b57cec5SDimitry Andric Category = ""; 488*0b57cec5SDimitry Andric return; 489*0b57cec5SDimitry Andric } 490*0b57cec5SDimitry Andric 491*0b57cec5SDimitry Andric Class = In.slice(In.find('[') + 1, In.find('(')); 492*0b57cec5SDimitry Andric Category = In.slice(In.find('[') + 1, In.find(' ')); 493*0b57cec5SDimitry Andric } 494*0b57cec5SDimitry Andric 495*0b57cec5SDimitry Andric static StringRef getObjCMethodName(StringRef In) { 496*0b57cec5SDimitry Andric return In.slice(In.find(' ') + 1, In.find(']')); 497*0b57cec5SDimitry Andric } 498*0b57cec5SDimitry Andric 499*0b57cec5SDimitry Andric // Add the various names to the Dwarf accelerator table names. 500*0b57cec5SDimitry Andric void DwarfDebug::addSubprogramNames(const DICompileUnit &CU, 501*0b57cec5SDimitry Andric const DISubprogram *SP, DIE &Die) { 502*0b57cec5SDimitry Andric if (getAccelTableKind() != AccelTableKind::Apple && 503*0b57cec5SDimitry Andric CU.getNameTableKind() == DICompileUnit::DebugNameTableKind::None) 504*0b57cec5SDimitry Andric return; 505*0b57cec5SDimitry Andric 506*0b57cec5SDimitry Andric if (!SP->isDefinition()) 507*0b57cec5SDimitry Andric return; 508*0b57cec5SDimitry Andric 509*0b57cec5SDimitry Andric if (SP->getName() != "") 510*0b57cec5SDimitry Andric addAccelName(CU, SP->getName(), Die); 511*0b57cec5SDimitry Andric 512*0b57cec5SDimitry Andric // If the linkage name is different than the name, go ahead and output that as 513*0b57cec5SDimitry Andric // well into the name table. Only do that if we are going to actually emit 514*0b57cec5SDimitry Andric // that name. 515*0b57cec5SDimitry Andric if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName() && 516*0b57cec5SDimitry Andric (useAllLinkageNames() || InfoHolder.getAbstractSPDies().lookup(SP))) 517*0b57cec5SDimitry Andric addAccelName(CU, SP->getLinkageName(), Die); 518*0b57cec5SDimitry Andric 519*0b57cec5SDimitry Andric // If this is an Objective-C selector name add it to the ObjC accelerator 520*0b57cec5SDimitry Andric // too. 521*0b57cec5SDimitry Andric if (isObjCClass(SP->getName())) { 522*0b57cec5SDimitry Andric StringRef Class, Category; 523*0b57cec5SDimitry Andric getObjCClassCategory(SP->getName(), Class, Category); 524*0b57cec5SDimitry Andric addAccelObjC(CU, Class, Die); 525*0b57cec5SDimitry Andric if (Category != "") 526*0b57cec5SDimitry Andric addAccelObjC(CU, Category, Die); 527*0b57cec5SDimitry Andric // Also add the base method name to the name table. 528*0b57cec5SDimitry Andric addAccelName(CU, getObjCMethodName(SP->getName()), Die); 529*0b57cec5SDimitry Andric } 530*0b57cec5SDimitry Andric } 531*0b57cec5SDimitry Andric 532*0b57cec5SDimitry Andric /// Check whether we should create a DIE for the given Scope, return true 533*0b57cec5SDimitry Andric /// if we don't create a DIE (the corresponding DIE is null). 534*0b57cec5SDimitry Andric bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) { 535*0b57cec5SDimitry Andric if (Scope->isAbstractScope()) 536*0b57cec5SDimitry Andric return false; 537*0b57cec5SDimitry Andric 538*0b57cec5SDimitry Andric // We don't create a DIE if there is no Range. 539*0b57cec5SDimitry Andric const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges(); 540*0b57cec5SDimitry Andric if (Ranges.empty()) 541*0b57cec5SDimitry Andric return true; 542*0b57cec5SDimitry Andric 543*0b57cec5SDimitry Andric if (Ranges.size() > 1) 544*0b57cec5SDimitry Andric return false; 545*0b57cec5SDimitry Andric 546*0b57cec5SDimitry Andric // We don't create a DIE if we have a single Range and the end label 547*0b57cec5SDimitry Andric // is null. 548*0b57cec5SDimitry Andric return !getLabelAfterInsn(Ranges.front().second); 549*0b57cec5SDimitry Andric } 550*0b57cec5SDimitry Andric 551*0b57cec5SDimitry Andric template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) { 552*0b57cec5SDimitry Andric F(CU); 553*0b57cec5SDimitry Andric if (auto *SkelCU = CU.getSkeleton()) 554*0b57cec5SDimitry Andric if (CU.getCUNode()->getSplitDebugInlining()) 555*0b57cec5SDimitry Andric F(*SkelCU); 556*0b57cec5SDimitry Andric } 557*0b57cec5SDimitry Andric 558*0b57cec5SDimitry Andric bool DwarfDebug::shareAcrossDWOCUs() const { 559*0b57cec5SDimitry Andric return SplitDwarfCrossCuReferences; 560*0b57cec5SDimitry Andric } 561*0b57cec5SDimitry Andric 562*0b57cec5SDimitry Andric void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU, 563*0b57cec5SDimitry Andric LexicalScope *Scope) { 564*0b57cec5SDimitry Andric assert(Scope && Scope->getScopeNode()); 565*0b57cec5SDimitry Andric assert(Scope->isAbstractScope()); 566*0b57cec5SDimitry Andric assert(!Scope->getInlinedAt()); 567*0b57cec5SDimitry Andric 568*0b57cec5SDimitry Andric auto *SP = cast<DISubprogram>(Scope->getScopeNode()); 569*0b57cec5SDimitry Andric 570*0b57cec5SDimitry Andric // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram 571*0b57cec5SDimitry Andric // was inlined from another compile unit. 572*0b57cec5SDimitry Andric if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining()) 573*0b57cec5SDimitry Andric // Avoid building the original CU if it won't be used 574*0b57cec5SDimitry Andric SrcCU.constructAbstractSubprogramScopeDIE(Scope); 575*0b57cec5SDimitry Andric else { 576*0b57cec5SDimitry Andric auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); 577*0b57cec5SDimitry Andric if (auto *SkelCU = CU.getSkeleton()) { 578*0b57cec5SDimitry Andric (shareAcrossDWOCUs() ? CU : SrcCU) 579*0b57cec5SDimitry Andric .constructAbstractSubprogramScopeDIE(Scope); 580*0b57cec5SDimitry Andric if (CU.getCUNode()->getSplitDebugInlining()) 581*0b57cec5SDimitry Andric SkelCU->constructAbstractSubprogramScopeDIE(Scope); 582*0b57cec5SDimitry Andric } else 583*0b57cec5SDimitry Andric CU.constructAbstractSubprogramScopeDIE(Scope); 584*0b57cec5SDimitry Andric } 585*0b57cec5SDimitry Andric } 586*0b57cec5SDimitry Andric 5875ffd83dbSDimitry Andric /// Represents a parameter whose call site value can be described by applying a 5885ffd83dbSDimitry Andric /// debug expression to a register in the forwarded register worklist. 5895ffd83dbSDimitry Andric struct FwdRegParamInfo { 5905ffd83dbSDimitry Andric /// The described parameter register. 5915ffd83dbSDimitry Andric unsigned ParamReg; 5925ffd83dbSDimitry Andric 5935ffd83dbSDimitry Andric /// Debug expression that has been built up when walking through the 5945ffd83dbSDimitry Andric /// instruction chain that produces the parameter's value. 5955ffd83dbSDimitry Andric const DIExpression *Expr; 5965ffd83dbSDimitry Andric }; 5975ffd83dbSDimitry Andric 5985ffd83dbSDimitry Andric /// Register worklist for finding call site values. 5995ffd83dbSDimitry Andric using FwdRegWorklist = MapVector<unsigned, SmallVector<FwdRegParamInfo, 2>>; 600bdd1243dSDimitry Andric /// Container for the set of registers known to be clobbered on the path to a 601bdd1243dSDimitry Andric /// call site. 602bdd1243dSDimitry Andric using ClobberedRegSet = SmallSet<Register, 16>; 6035ffd83dbSDimitry Andric 6045ffd83dbSDimitry Andric /// Append the expression \p Addition to \p Original and return the result. 6055ffd83dbSDimitry Andric static const DIExpression *combineDIExpressions(const DIExpression *Original, 6065ffd83dbSDimitry Andric const DIExpression *Addition) { 6075ffd83dbSDimitry Andric std::vector<uint64_t> Elts = Addition->getElements().vec(); 6085ffd83dbSDimitry Andric // Avoid multiple DW_OP_stack_values. 6095ffd83dbSDimitry Andric if (Original->isImplicit() && Addition->isImplicit()) 610e8d8bef9SDimitry Andric erase_value(Elts, dwarf::DW_OP_stack_value); 6115ffd83dbSDimitry Andric const DIExpression *CombinedExpr = 6125ffd83dbSDimitry Andric (Elts.size() > 0) ? DIExpression::append(Original, Elts) : Original; 6135ffd83dbSDimitry Andric return CombinedExpr; 6145ffd83dbSDimitry Andric } 6155ffd83dbSDimitry Andric 6165ffd83dbSDimitry Andric /// Emit call site parameter entries that are described by the given value and 6175ffd83dbSDimitry Andric /// debug expression. 6185ffd83dbSDimitry Andric template <typename ValT> 6195ffd83dbSDimitry Andric static void finishCallSiteParams(ValT Val, const DIExpression *Expr, 6205ffd83dbSDimitry Andric ArrayRef<FwdRegParamInfo> DescribedParams, 6215ffd83dbSDimitry Andric ParamSet &Params) { 6225ffd83dbSDimitry Andric for (auto Param : DescribedParams) { 6235ffd83dbSDimitry Andric bool ShouldCombineExpressions = Expr && Param.Expr->getNumElements() > 0; 6245ffd83dbSDimitry Andric 6255ffd83dbSDimitry Andric // TODO: Entry value operations can currently not be combined with any 6265ffd83dbSDimitry Andric // other expressions, so we can't emit call site entries in those cases. 6275ffd83dbSDimitry Andric if (ShouldCombineExpressions && Expr->isEntryValue()) 6285ffd83dbSDimitry Andric continue; 6295ffd83dbSDimitry Andric 6305ffd83dbSDimitry Andric // If a parameter's call site value is produced by a chain of 6315ffd83dbSDimitry Andric // instructions we may have already created an expression for the 6325ffd83dbSDimitry Andric // parameter when walking through the instructions. Append that to the 6335ffd83dbSDimitry Andric // base expression. 6345ffd83dbSDimitry Andric const DIExpression *CombinedExpr = 6355ffd83dbSDimitry Andric ShouldCombineExpressions ? combineDIExpressions(Expr, Param.Expr) 6365ffd83dbSDimitry Andric : Expr; 6375ffd83dbSDimitry Andric assert((!CombinedExpr || CombinedExpr->isValid()) && 6385ffd83dbSDimitry Andric "Combined debug expression is invalid"); 6395ffd83dbSDimitry Andric 640fe6060f1SDimitry Andric DbgValueLoc DbgLocVal(CombinedExpr, DbgValueLocEntry(Val)); 6415ffd83dbSDimitry Andric DbgCallSiteParam CSParm(Param.ParamReg, DbgLocVal); 6425ffd83dbSDimitry Andric Params.push_back(CSParm); 6435ffd83dbSDimitry Andric ++NumCSParams; 6445ffd83dbSDimitry Andric } 6455ffd83dbSDimitry Andric } 6465ffd83dbSDimitry Andric 6475ffd83dbSDimitry Andric /// Add \p Reg to the worklist, if it's not already present, and mark that the 6485ffd83dbSDimitry Andric /// given parameter registers' values can (potentially) be described using 6495ffd83dbSDimitry Andric /// that register and an debug expression. 6505ffd83dbSDimitry Andric static void addToFwdRegWorklist(FwdRegWorklist &Worklist, unsigned Reg, 6515ffd83dbSDimitry Andric const DIExpression *Expr, 6525ffd83dbSDimitry Andric ArrayRef<FwdRegParamInfo> ParamsToAdd) { 6535ffd83dbSDimitry Andric auto I = Worklist.insert({Reg, {}}); 6545ffd83dbSDimitry Andric auto &ParamsForFwdReg = I.first->second; 6555ffd83dbSDimitry Andric for (auto Param : ParamsToAdd) { 6565ffd83dbSDimitry Andric assert(none_of(ParamsForFwdReg, 6575ffd83dbSDimitry Andric [Param](const FwdRegParamInfo &D) { 6585ffd83dbSDimitry Andric return D.ParamReg == Param.ParamReg; 6595ffd83dbSDimitry Andric }) && 6605ffd83dbSDimitry Andric "Same parameter described twice by forwarding reg"); 6615ffd83dbSDimitry Andric 6625ffd83dbSDimitry Andric // If a parameter's call site value is produced by a chain of 6635ffd83dbSDimitry Andric // instructions we may have already created an expression for the 6645ffd83dbSDimitry Andric // parameter when walking through the instructions. Append that to the 6655ffd83dbSDimitry Andric // new expression. 6665ffd83dbSDimitry Andric const DIExpression *CombinedExpr = combineDIExpressions(Expr, Param.Expr); 6675ffd83dbSDimitry Andric ParamsForFwdReg.push_back({Param.ParamReg, CombinedExpr}); 6685ffd83dbSDimitry Andric } 6695ffd83dbSDimitry Andric } 6705ffd83dbSDimitry Andric 6715ffd83dbSDimitry Andric /// Interpret values loaded into registers by \p CurMI. 6725ffd83dbSDimitry Andric static void interpretValues(const MachineInstr *CurMI, 6735ffd83dbSDimitry Andric FwdRegWorklist &ForwardedRegWorklist, 674bdd1243dSDimitry Andric ParamSet &Params, 675bdd1243dSDimitry Andric ClobberedRegSet &ClobberedRegUnits) { 6765ffd83dbSDimitry Andric 6775ffd83dbSDimitry Andric const MachineFunction *MF = CurMI->getMF(); 6785ffd83dbSDimitry Andric const DIExpression *EmptyExpr = 6795ffd83dbSDimitry Andric DIExpression::get(MF->getFunction().getContext(), {}); 6805ffd83dbSDimitry Andric const auto &TRI = *MF->getSubtarget().getRegisterInfo(); 6815ffd83dbSDimitry Andric const auto &TII = *MF->getSubtarget().getInstrInfo(); 6825ffd83dbSDimitry Andric const auto &TLI = *MF->getSubtarget().getTargetLowering(); 6835ffd83dbSDimitry Andric 6845ffd83dbSDimitry Andric // If an instruction defines more than one item in the worklist, we may run 6855ffd83dbSDimitry Andric // into situations where a worklist register's value is (potentially) 6865ffd83dbSDimitry Andric // described by the previous value of another register that is also defined 6875ffd83dbSDimitry Andric // by that instruction. 6885ffd83dbSDimitry Andric // 6895ffd83dbSDimitry Andric // This can for example occur in cases like this: 6905ffd83dbSDimitry Andric // 6915ffd83dbSDimitry Andric // $r1 = mov 123 6925ffd83dbSDimitry Andric // $r0, $r1 = mvrr $r1, 456 6935ffd83dbSDimitry Andric // call @foo, $r0, $r1 6945ffd83dbSDimitry Andric // 6955ffd83dbSDimitry Andric // When describing $r1's value for the mvrr instruction, we need to make sure 6965ffd83dbSDimitry Andric // that we don't finalize an entry value for $r0, as that is dependent on the 6975ffd83dbSDimitry Andric // previous value of $r1 (123 rather than 456). 6985ffd83dbSDimitry Andric // 6995ffd83dbSDimitry Andric // In order to not have to distinguish between those cases when finalizing 7005ffd83dbSDimitry Andric // entry values, we simply postpone adding new parameter registers to the 7015ffd83dbSDimitry Andric // worklist, by first keeping them in this temporary container until the 7025ffd83dbSDimitry Andric // instruction has been handled. 7035ffd83dbSDimitry Andric FwdRegWorklist TmpWorklistItems; 7045ffd83dbSDimitry Andric 7055ffd83dbSDimitry Andric // If the MI is an instruction defining one or more parameters' forwarding 7065ffd83dbSDimitry Andric // registers, add those defines. 707bdd1243dSDimitry Andric ClobberedRegSet NewClobberedRegUnits; 7085ffd83dbSDimitry Andric auto getForwardingRegsDefinedByMI = [&](const MachineInstr &MI, 7095ffd83dbSDimitry Andric SmallSetVector<unsigned, 4> &Defs) { 7105ffd83dbSDimitry Andric if (MI.isDebugInstr()) 7115ffd83dbSDimitry Andric return; 7125ffd83dbSDimitry Andric 7135ffd83dbSDimitry Andric for (const MachineOperand &MO : MI.operands()) { 714bdd1243dSDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg().isPhysical()) { 715fe6060f1SDimitry Andric for (auto &FwdReg : ForwardedRegWorklist) 7165ffd83dbSDimitry Andric if (TRI.regsOverlap(FwdReg.first, MO.getReg())) 7175ffd83dbSDimitry Andric Defs.insert(FwdReg.first); 718bdd1243dSDimitry Andric for (MCRegUnitIterator Units(MO.getReg(), &TRI); Units.isValid(); ++Units) 719bdd1243dSDimitry Andric NewClobberedRegUnits.insert(*Units); 7205ffd83dbSDimitry Andric } 7215ffd83dbSDimitry Andric } 7225ffd83dbSDimitry Andric }; 7235ffd83dbSDimitry Andric 7245ffd83dbSDimitry Andric // Set of worklist registers that are defined by this instruction. 7255ffd83dbSDimitry Andric SmallSetVector<unsigned, 4> FwdRegDefs; 7265ffd83dbSDimitry Andric 7275ffd83dbSDimitry Andric getForwardingRegsDefinedByMI(*CurMI, FwdRegDefs); 728bdd1243dSDimitry Andric if (FwdRegDefs.empty()) { 729bdd1243dSDimitry Andric // Any definitions by this instruction will clobber earlier reg movements. 730bdd1243dSDimitry Andric ClobberedRegUnits.insert(NewClobberedRegUnits.begin(), 731bdd1243dSDimitry Andric NewClobberedRegUnits.end()); 7325ffd83dbSDimitry Andric return; 733bdd1243dSDimitry Andric } 734bdd1243dSDimitry Andric 735bdd1243dSDimitry Andric // It's possible that we find a copy from a non-volatile register to the param 736bdd1243dSDimitry Andric // register, which is clobbered in the meantime. Test for clobbered reg unit 737bdd1243dSDimitry Andric // overlaps before completing. 738bdd1243dSDimitry Andric auto IsRegClobberedInMeantime = [&](Register Reg) -> bool { 739bdd1243dSDimitry Andric for (auto &RegUnit : ClobberedRegUnits) 740bdd1243dSDimitry Andric if (TRI.hasRegUnit(Reg, RegUnit)) 741bdd1243dSDimitry Andric return true; 742bdd1243dSDimitry Andric return false; 743bdd1243dSDimitry Andric }; 7445ffd83dbSDimitry Andric 7455ffd83dbSDimitry Andric for (auto ParamFwdReg : FwdRegDefs) { 7465ffd83dbSDimitry Andric if (auto ParamValue = TII.describeLoadedValue(*CurMI, ParamFwdReg)) { 7475ffd83dbSDimitry Andric if (ParamValue->first.isImm()) { 7485ffd83dbSDimitry Andric int64_t Val = ParamValue->first.getImm(); 7495ffd83dbSDimitry Andric finishCallSiteParams(Val, ParamValue->second, 7505ffd83dbSDimitry Andric ForwardedRegWorklist[ParamFwdReg], Params); 7515ffd83dbSDimitry Andric } else if (ParamValue->first.isReg()) { 7525ffd83dbSDimitry Andric Register RegLoc = ParamValue->first.getReg(); 753e8d8bef9SDimitry Andric Register SP = TLI.getStackPointerRegisterToSaveRestore(); 7545ffd83dbSDimitry Andric Register FP = TRI.getFrameRegister(*MF); 7555ffd83dbSDimitry Andric bool IsSPorFP = (RegLoc == SP) || (RegLoc == FP); 756bdd1243dSDimitry Andric if (!IsRegClobberedInMeantime(RegLoc) && 757bdd1243dSDimitry Andric (TRI.isCalleeSavedPhysReg(RegLoc, *MF) || IsSPorFP)) { 758e8d8bef9SDimitry Andric MachineLocation MLoc(RegLoc, /*Indirect=*/IsSPorFP); 7595ffd83dbSDimitry Andric finishCallSiteParams(MLoc, ParamValue->second, 7605ffd83dbSDimitry Andric ForwardedRegWorklist[ParamFwdReg], Params); 7615ffd83dbSDimitry Andric } else { 7625ffd83dbSDimitry Andric // ParamFwdReg was described by the non-callee saved register 7635ffd83dbSDimitry Andric // RegLoc. Mark that the call site values for the parameters are 7645ffd83dbSDimitry Andric // dependent on that register instead of ParamFwdReg. Since RegLoc 7655ffd83dbSDimitry Andric // may be a register that will be handled in this iteration, we 7665ffd83dbSDimitry Andric // postpone adding the items to the worklist, and instead keep them 7675ffd83dbSDimitry Andric // in a temporary container. 7685ffd83dbSDimitry Andric addToFwdRegWorklist(TmpWorklistItems, RegLoc, ParamValue->second, 7695ffd83dbSDimitry Andric ForwardedRegWorklist[ParamFwdReg]); 7705ffd83dbSDimitry Andric } 7715ffd83dbSDimitry Andric } 7725ffd83dbSDimitry Andric } 7735ffd83dbSDimitry Andric } 7745ffd83dbSDimitry Andric 7755ffd83dbSDimitry Andric // Remove all registers that this instruction defines from the worklist. 7765ffd83dbSDimitry Andric for (auto ParamFwdReg : FwdRegDefs) 7775ffd83dbSDimitry Andric ForwardedRegWorklist.erase(ParamFwdReg); 7785ffd83dbSDimitry Andric 779bdd1243dSDimitry Andric // Any definitions by this instruction will clobber earlier reg movements. 780bdd1243dSDimitry Andric ClobberedRegUnits.insert(NewClobberedRegUnits.begin(), 781bdd1243dSDimitry Andric NewClobberedRegUnits.end()); 782bdd1243dSDimitry Andric 7835ffd83dbSDimitry Andric // Now that we are done handling this instruction, add items from the 7845ffd83dbSDimitry Andric // temporary worklist to the real one. 785fe6060f1SDimitry Andric for (auto &New : TmpWorklistItems) 7865ffd83dbSDimitry Andric addToFwdRegWorklist(ForwardedRegWorklist, New.first, EmptyExpr, New.second); 7875ffd83dbSDimitry Andric TmpWorklistItems.clear(); 7885ffd83dbSDimitry Andric } 7895ffd83dbSDimitry Andric 7905ffd83dbSDimitry Andric static bool interpretNextInstr(const MachineInstr *CurMI, 7915ffd83dbSDimitry Andric FwdRegWorklist &ForwardedRegWorklist, 792bdd1243dSDimitry Andric ParamSet &Params, 793bdd1243dSDimitry Andric ClobberedRegSet &ClobberedRegUnits) { 7945ffd83dbSDimitry Andric // Skip bundle headers. 7955ffd83dbSDimitry Andric if (CurMI->isBundle()) 7965ffd83dbSDimitry Andric return true; 7975ffd83dbSDimitry Andric 7985ffd83dbSDimitry Andric // If the next instruction is a call we can not interpret parameter's 7995ffd83dbSDimitry Andric // forwarding registers or we finished the interpretation of all 8005ffd83dbSDimitry Andric // parameters. 8015ffd83dbSDimitry Andric if (CurMI->isCall()) 8025ffd83dbSDimitry Andric return false; 8035ffd83dbSDimitry Andric 8045ffd83dbSDimitry Andric if (ForwardedRegWorklist.empty()) 8055ffd83dbSDimitry Andric return false; 8065ffd83dbSDimitry Andric 8075ffd83dbSDimitry Andric // Avoid NOP description. 8085ffd83dbSDimitry Andric if (CurMI->getNumOperands() == 0) 8095ffd83dbSDimitry Andric return true; 8105ffd83dbSDimitry Andric 811bdd1243dSDimitry Andric interpretValues(CurMI, ForwardedRegWorklist, Params, ClobberedRegUnits); 8125ffd83dbSDimitry Andric 8135ffd83dbSDimitry Andric return true; 8145ffd83dbSDimitry Andric } 8155ffd83dbSDimitry Andric 8168bcb0991SDimitry Andric /// Try to interpret values loaded into registers that forward parameters 8178bcb0991SDimitry Andric /// for \p CallMI. Store parameters with interpreted value into \p Params. 8188bcb0991SDimitry Andric static void collectCallSiteParameters(const MachineInstr *CallMI, 8198bcb0991SDimitry Andric ParamSet &Params) { 8205ffd83dbSDimitry Andric const MachineFunction *MF = CallMI->getMF(); 821fe6060f1SDimitry Andric const auto &CalleesMap = MF->getCallSitesInfo(); 8228bcb0991SDimitry Andric auto CallFwdRegsInfo = CalleesMap.find(CallMI); 8238bcb0991SDimitry Andric 8248bcb0991SDimitry Andric // There is no information for the call instruction. 8258bcb0991SDimitry Andric if (CallFwdRegsInfo == CalleesMap.end()) 8268bcb0991SDimitry Andric return; 8278bcb0991SDimitry Andric 8285ffd83dbSDimitry Andric const MachineBasicBlock *MBB = CallMI->getParent(); 8298bcb0991SDimitry Andric 8308bcb0991SDimitry Andric // Skip the call instruction. 8318bcb0991SDimitry Andric auto I = std::next(CallMI->getReverseIterator()); 8328bcb0991SDimitry Andric 8335ffd83dbSDimitry Andric FwdRegWorklist ForwardedRegWorklist; 8345ffd83dbSDimitry Andric 8355ffd83dbSDimitry Andric const DIExpression *EmptyExpr = 8365ffd83dbSDimitry Andric DIExpression::get(MF->getFunction().getContext(), {}); 8375ffd83dbSDimitry Andric 8388bcb0991SDimitry Andric // Add all the forwarding registers into the ForwardedRegWorklist. 839fe6060f1SDimitry Andric for (const auto &ArgReg : CallFwdRegsInfo->second) { 8405ffd83dbSDimitry Andric bool InsertedReg = 8415ffd83dbSDimitry Andric ForwardedRegWorklist.insert({ArgReg.Reg, {{ArgReg.Reg, EmptyExpr}}}) 8425ffd83dbSDimitry Andric .second; 8438bcb0991SDimitry Andric assert(InsertedReg && "Single register used to forward two arguments?"); 8448bcb0991SDimitry Andric (void)InsertedReg; 8458bcb0991SDimitry Andric } 8468bcb0991SDimitry Andric 847e8d8bef9SDimitry Andric // Do not emit CSInfo for undef forwarding registers. 848fcaf7f86SDimitry Andric for (const auto &MO : CallMI->uses()) 849e8d8bef9SDimitry Andric if (MO.isReg() && MO.isUndef()) 850e8d8bef9SDimitry Andric ForwardedRegWorklist.erase(MO.getReg()); 851e8d8bef9SDimitry Andric 8528bcb0991SDimitry Andric // We erase, from the ForwardedRegWorklist, those forwarding registers for 8538bcb0991SDimitry Andric // which we successfully describe a loaded value (by using 8548bcb0991SDimitry Andric // the describeLoadedValue()). For those remaining arguments in the working 8558bcb0991SDimitry Andric // list, for which we do not describe a loaded value by 8568bcb0991SDimitry Andric // the describeLoadedValue(), we try to generate an entry value expression 8575ffd83dbSDimitry Andric // for their call site value description, if the call is within the entry MBB. 8588bcb0991SDimitry Andric // TODO: Handle situations when call site parameter value can be described 8595ffd83dbSDimitry Andric // as the entry value within basic blocks other than the first one. 8608bcb0991SDimitry Andric bool ShouldTryEmitEntryVals = MBB->getIterator() == MF->begin(); 8618bcb0991SDimitry Andric 8625ffd83dbSDimitry Andric // Search for a loading value in forwarding registers inside call delay slot. 863bdd1243dSDimitry Andric ClobberedRegSet ClobberedRegUnits; 8645ffd83dbSDimitry Andric if (CallMI->hasDelaySlot()) { 8655ffd83dbSDimitry Andric auto Suc = std::next(CallMI->getIterator()); 8665ffd83dbSDimitry Andric // Only one-instruction delay slot is supported. 8675ffd83dbSDimitry Andric auto BundleEnd = llvm::getBundleEnd(CallMI->getIterator()); 8685ffd83dbSDimitry Andric (void)BundleEnd; 8695ffd83dbSDimitry Andric assert(std::next(Suc) == BundleEnd && 8705ffd83dbSDimitry Andric "More than one instruction in call delay slot"); 8715ffd83dbSDimitry Andric // Try to interpret value loaded by instruction. 872bdd1243dSDimitry Andric if (!interpretNextInstr(&*Suc, ForwardedRegWorklist, Params, ClobberedRegUnits)) 8738bcb0991SDimitry Andric return; 8748bcb0991SDimitry Andric } 8758bcb0991SDimitry Andric 876480093f4SDimitry Andric // Search for a loading value in forwarding registers. 8778bcb0991SDimitry Andric for (; I != MBB->rend(); ++I) { 8785ffd83dbSDimitry Andric // Try to interpret values loaded by instruction. 879bdd1243dSDimitry Andric if (!interpretNextInstr(&*I, ForwardedRegWorklist, Params, ClobberedRegUnits)) 8808bcb0991SDimitry Andric return; 8818bcb0991SDimitry Andric } 8828bcb0991SDimitry Andric 8838bcb0991SDimitry Andric // Emit the call site parameter's value as an entry value. 8848bcb0991SDimitry Andric if (ShouldTryEmitEntryVals) { 8858bcb0991SDimitry Andric // Create an expression where the register's entry value is used. 8868bcb0991SDimitry Andric DIExpression *EntryExpr = DIExpression::get( 8878bcb0991SDimitry Andric MF->getFunction().getContext(), {dwarf::DW_OP_LLVM_entry_value, 1}); 888fe6060f1SDimitry Andric for (auto &RegEntry : ForwardedRegWorklist) { 8895ffd83dbSDimitry Andric MachineLocation MLoc(RegEntry.first); 8905ffd83dbSDimitry Andric finishCallSiteParams(MLoc, EntryExpr, RegEntry.second, Params); 8918bcb0991SDimitry Andric } 8928bcb0991SDimitry Andric } 8938bcb0991SDimitry Andric } 8948bcb0991SDimitry Andric 895*0b57cec5SDimitry Andric void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP, 896*0b57cec5SDimitry Andric DwarfCompileUnit &CU, DIE &ScopeDIE, 897*0b57cec5SDimitry Andric const MachineFunction &MF) { 898*0b57cec5SDimitry Andric // Add a call site-related attribute (DWARF5, Sec. 3.3.1.3). Do this only if 899*0b57cec5SDimitry Andric // the subprogram is required to have one. 900*0b57cec5SDimitry Andric if (!SP.areAllCallsDescribed() || !SP.isDefinition()) 901*0b57cec5SDimitry Andric return; 902*0b57cec5SDimitry Andric 903*0b57cec5SDimitry Andric // Use DW_AT_call_all_calls to express that call site entries are present 904*0b57cec5SDimitry Andric // for both tail and non-tail calls. Don't use DW_AT_call_all_source_calls 905*0b57cec5SDimitry Andric // because one of its requirements is not met: call site entries for 906*0b57cec5SDimitry Andric // optimized-out calls are elided. 9078bcb0991SDimitry Andric CU.addFlag(ScopeDIE, CU.getDwarf5OrGNUAttr(dwarf::DW_AT_call_all_calls)); 908*0b57cec5SDimitry Andric 909*0b57cec5SDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 910*0b57cec5SDimitry Andric assert(TII && "TargetInstrInfo not found: cannot label tail calls"); 9115ffd83dbSDimitry Andric 9125ffd83dbSDimitry Andric // Delay slot support check. 9135ffd83dbSDimitry Andric auto delaySlotSupported = [&](const MachineInstr &MI) { 9145ffd83dbSDimitry Andric if (!MI.isBundledWithSucc()) 9155ffd83dbSDimitry Andric return false; 9165ffd83dbSDimitry Andric auto Suc = std::next(MI.getIterator()); 9175ffd83dbSDimitry Andric auto CallInstrBundle = getBundleStart(MI.getIterator()); 9185ffd83dbSDimitry Andric (void)CallInstrBundle; 9195ffd83dbSDimitry Andric auto DelaySlotBundle = getBundleStart(Suc); 9205ffd83dbSDimitry Andric (void)DelaySlotBundle; 9215ffd83dbSDimitry Andric // Ensure that label after call is following delay slot instruction. 9225ffd83dbSDimitry Andric // Ex. CALL_INSTRUCTION { 9235ffd83dbSDimitry Andric // DELAY_SLOT_INSTRUCTION } 9245ffd83dbSDimitry Andric // LABEL_AFTER_CALL 9255ffd83dbSDimitry Andric assert(getLabelAfterInsn(&*CallInstrBundle) == 9265ffd83dbSDimitry Andric getLabelAfterInsn(&*DelaySlotBundle) && 9275ffd83dbSDimitry Andric "Call and its successor instruction don't have same label after."); 9285ffd83dbSDimitry Andric return true; 9295ffd83dbSDimitry Andric }; 930*0b57cec5SDimitry Andric 931*0b57cec5SDimitry Andric // Emit call site entries for each call or tail call in the function. 932*0b57cec5SDimitry Andric for (const MachineBasicBlock &MBB : MF) { 933*0b57cec5SDimitry Andric for (const MachineInstr &MI : MBB.instrs()) { 934480093f4SDimitry Andric // Bundles with call in them will pass the isCall() test below but do not 935480093f4SDimitry Andric // have callee operand information so skip them here. Iterator will 936480093f4SDimitry Andric // eventually reach the call MI. 937480093f4SDimitry Andric if (MI.isBundle()) 938480093f4SDimitry Andric continue; 939480093f4SDimitry Andric 940*0b57cec5SDimitry Andric // Skip instructions which aren't calls. Both calls and tail-calling jump 941*0b57cec5SDimitry Andric // instructions (e.g TAILJMPd64) are classified correctly here. 9425ffd83dbSDimitry Andric if (!MI.isCandidateForCallSiteEntry()) 943*0b57cec5SDimitry Andric continue; 944*0b57cec5SDimitry Andric 9455ffd83dbSDimitry Andric // Skip instructions marked as frame setup, as they are not interesting to 9465ffd83dbSDimitry Andric // the user. 9475ffd83dbSDimitry Andric if (MI.getFlag(MachineInstr::FrameSetup)) 9485ffd83dbSDimitry Andric continue; 9495ffd83dbSDimitry Andric 9505ffd83dbSDimitry Andric // Check if delay slot support is enabled. 9515ffd83dbSDimitry Andric if (MI.hasDelaySlot() && !delaySlotSupported(*&MI)) 952*0b57cec5SDimitry Andric return; 953*0b57cec5SDimitry Andric 954*0b57cec5SDimitry Andric // If this is a direct call, find the callee's subprogram. 9558bcb0991SDimitry Andric // In the case of an indirect call find the register that holds 9568bcb0991SDimitry Andric // the callee. 957fe6060f1SDimitry Andric const MachineOperand &CalleeOp = TII->getCalleeOperand(MI); 958fe6060f1SDimitry Andric if (!CalleeOp.isGlobal() && 959bdd1243dSDimitry Andric (!CalleeOp.isReg() || !CalleeOp.getReg().isPhysical())) 960*0b57cec5SDimitry Andric continue; 961*0b57cec5SDimitry Andric 9628bcb0991SDimitry Andric unsigned CallReg = 0; 96369ade1e0SDimitry Andric const DISubprogram *CalleeSP = nullptr; 9648bcb0991SDimitry Andric const Function *CalleeDecl = nullptr; 9658bcb0991SDimitry Andric if (CalleeOp.isReg()) { 9668bcb0991SDimitry Andric CallReg = CalleeOp.getReg(); 9678bcb0991SDimitry Andric if (!CallReg) 9688bcb0991SDimitry Andric continue; 9698bcb0991SDimitry Andric } else { 9708bcb0991SDimitry Andric CalleeDecl = dyn_cast<Function>(CalleeOp.getGlobal()); 9718bcb0991SDimitry Andric if (!CalleeDecl || !CalleeDecl->getSubprogram()) 9728bcb0991SDimitry Andric continue; 97369ade1e0SDimitry Andric CalleeSP = CalleeDecl->getSubprogram(); 9748bcb0991SDimitry Andric } 9758bcb0991SDimitry Andric 976*0b57cec5SDimitry Andric // TODO: Omit call site entries for runtime calls (objc_msgSend, etc). 977*0b57cec5SDimitry Andric 978*0b57cec5SDimitry Andric bool IsTail = TII->isTailCall(MI); 979*0b57cec5SDimitry Andric 980480093f4SDimitry Andric // If MI is in a bundle, the label was created after the bundle since 981480093f4SDimitry Andric // EmitFunctionBody iterates over top-level MIs. Get that top-level MI 982480093f4SDimitry Andric // to search for that label below. 983480093f4SDimitry Andric const MachineInstr *TopLevelCallMI = 984480093f4SDimitry Andric MI.isInsideBundle() ? &*getBundleStart(MI.getIterator()) : &MI; 985480093f4SDimitry Andric 9865ffd83dbSDimitry Andric // For non-tail calls, the return PC is needed to disambiguate paths in 9875ffd83dbSDimitry Andric // the call graph which could lead to some target function. For tail 9885ffd83dbSDimitry Andric // calls, no return PC information is needed, unless tuning for GDB in 9895ffd83dbSDimitry Andric // DWARF4 mode in which case we fake a return PC for compatibility. 9908bcb0991SDimitry Andric const MCSymbol *PCAddr = 9915ffd83dbSDimitry Andric (!IsTail || CU.useGNUAnalogForDwarf5Feature()) 992480093f4SDimitry Andric ? const_cast<MCSymbol *>(getLabelAfterInsn(TopLevelCallMI)) 9938bcb0991SDimitry Andric : nullptr; 9948bcb0991SDimitry Andric 9955ffd83dbSDimitry Andric // For tail calls, it's necessary to record the address of the branch 9965ffd83dbSDimitry Andric // instruction so that the debugger can show where the tail call occurred. 9975ffd83dbSDimitry Andric const MCSymbol *CallAddr = 9985ffd83dbSDimitry Andric IsTail ? getLabelBeforeInsn(TopLevelCallMI) : nullptr; 9995ffd83dbSDimitry Andric 10005ffd83dbSDimitry Andric assert((IsTail || PCAddr) && "Non-tail call without return PC"); 10018bcb0991SDimitry Andric 1002*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "CallSiteEntry: " << MF.getName() << " -> " 10038bcb0991SDimitry Andric << (CalleeDecl ? CalleeDecl->getName() 10048bcb0991SDimitry Andric : StringRef(MF.getSubtarget() 10058bcb0991SDimitry Andric .getRegisterInfo() 10068bcb0991SDimitry Andric ->getName(CallReg))) 10078bcb0991SDimitry Andric << (IsTail ? " [IsTail]" : "") << "\n"); 10088bcb0991SDimitry Andric 10095ffd83dbSDimitry Andric DIE &CallSiteDIE = CU.constructCallSiteEntryDIE( 101069ade1e0SDimitry Andric ScopeDIE, CalleeSP, IsTail, PCAddr, CallAddr, CallReg); 10118bcb0991SDimitry Andric 10125ffd83dbSDimitry Andric // Optionally emit call-site-param debug info. 10135ffd83dbSDimitry Andric if (emitDebugEntryValues()) { 10148bcb0991SDimitry Andric ParamSet Params; 10158bcb0991SDimitry Andric // Try to interpret values of call site parameters. 10168bcb0991SDimitry Andric collectCallSiteParameters(&MI, Params); 10178bcb0991SDimitry Andric CU.constructCallSiteParmEntryDIEs(CallSiteDIE, Params); 10188bcb0991SDimitry Andric } 1019*0b57cec5SDimitry Andric } 1020*0b57cec5SDimitry Andric } 1021*0b57cec5SDimitry Andric } 1022*0b57cec5SDimitry Andric 1023*0b57cec5SDimitry Andric void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const { 1024*0b57cec5SDimitry Andric if (!U.hasDwarfPubSections()) 1025*0b57cec5SDimitry Andric return; 1026*0b57cec5SDimitry Andric 1027*0b57cec5SDimitry Andric U.addFlag(D, dwarf::DW_AT_GNU_pubnames); 1028*0b57cec5SDimitry Andric } 1029*0b57cec5SDimitry Andric 1030*0b57cec5SDimitry Andric void DwarfDebug::finishUnitAttributes(const DICompileUnit *DIUnit, 1031*0b57cec5SDimitry Andric DwarfCompileUnit &NewCU) { 1032*0b57cec5SDimitry Andric DIE &Die = NewCU.getUnitDie(); 1033*0b57cec5SDimitry Andric StringRef FN = DIUnit->getFilename(); 1034*0b57cec5SDimitry Andric 1035*0b57cec5SDimitry Andric StringRef Producer = DIUnit->getProducer(); 1036*0b57cec5SDimitry Andric StringRef Flags = DIUnit->getFlags(); 1037*0b57cec5SDimitry Andric if (!Flags.empty() && !useAppleExtensionAttributes()) { 1038*0b57cec5SDimitry Andric std::string ProducerWithFlags = Producer.str() + " " + Flags.str(); 1039*0b57cec5SDimitry Andric NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags); 1040*0b57cec5SDimitry Andric } else 1041*0b57cec5SDimitry Andric NewCU.addString(Die, dwarf::DW_AT_producer, Producer); 1042*0b57cec5SDimitry Andric 1043*0b57cec5SDimitry Andric NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2, 1044*0b57cec5SDimitry Andric DIUnit->getSourceLanguage()); 1045*0b57cec5SDimitry Andric NewCU.addString(Die, dwarf::DW_AT_name, FN); 10465ffd83dbSDimitry Andric StringRef SysRoot = DIUnit->getSysRoot(); 10475ffd83dbSDimitry Andric if (!SysRoot.empty()) 10485ffd83dbSDimitry Andric NewCU.addString(Die, dwarf::DW_AT_LLVM_sysroot, SysRoot); 10495ffd83dbSDimitry Andric StringRef SDK = DIUnit->getSDK(); 10505ffd83dbSDimitry Andric if (!SDK.empty()) 10515ffd83dbSDimitry Andric NewCU.addString(Die, dwarf::DW_AT_APPLE_sdk, SDK); 1052*0b57cec5SDimitry Andric 1053*0b57cec5SDimitry Andric // Add DW_str_offsets_base to the unit DIE, except for split units. 1054*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable() && !useSplitDwarf()) 1055*0b57cec5SDimitry Andric NewCU.addStringOffsetsStart(); 1056*0b57cec5SDimitry Andric 1057*0b57cec5SDimitry Andric if (!useSplitDwarf()) { 1058*0b57cec5SDimitry Andric NewCU.initStmtList(); 1059*0b57cec5SDimitry Andric 1060*0b57cec5SDimitry Andric // If we're using split dwarf the compilation dir is going to be in the 1061*0b57cec5SDimitry Andric // skeleton CU and so we don't need to duplicate it here. 1062*0b57cec5SDimitry Andric if (!CompilationDir.empty()) 1063*0b57cec5SDimitry Andric NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); 1064*0b57cec5SDimitry Andric addGnuPubAttributes(NewCU, Die); 1065*0b57cec5SDimitry Andric } 1066*0b57cec5SDimitry Andric 1067*0b57cec5SDimitry Andric if (useAppleExtensionAttributes()) { 1068*0b57cec5SDimitry Andric if (DIUnit->isOptimized()) 1069*0b57cec5SDimitry Andric NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized); 1070*0b57cec5SDimitry Andric 1071*0b57cec5SDimitry Andric StringRef Flags = DIUnit->getFlags(); 1072*0b57cec5SDimitry Andric if (!Flags.empty()) 1073*0b57cec5SDimitry Andric NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags); 1074*0b57cec5SDimitry Andric 1075*0b57cec5SDimitry Andric if (unsigned RVer = DIUnit->getRuntimeVersion()) 1076*0b57cec5SDimitry Andric NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, 1077*0b57cec5SDimitry Andric dwarf::DW_FORM_data1, RVer); 1078*0b57cec5SDimitry Andric } 1079*0b57cec5SDimitry Andric 1080*0b57cec5SDimitry Andric if (DIUnit->getDWOId()) { 1081*0b57cec5SDimitry Andric // This CU is either a clang module DWO or a skeleton CU. 1082*0b57cec5SDimitry Andric NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8, 1083*0b57cec5SDimitry Andric DIUnit->getDWOId()); 1084480093f4SDimitry Andric if (!DIUnit->getSplitDebugFilename().empty()) { 1085*0b57cec5SDimitry Andric // This is a prefabricated skeleton CU. 1086480093f4SDimitry Andric dwarf::Attribute attrDWOName = getDwarfVersion() >= 5 1087480093f4SDimitry Andric ? dwarf::DW_AT_dwo_name 1088480093f4SDimitry Andric : dwarf::DW_AT_GNU_dwo_name; 1089480093f4SDimitry Andric NewCU.addString(Die, attrDWOName, DIUnit->getSplitDebugFilename()); 1090480093f4SDimitry Andric } 1091*0b57cec5SDimitry Andric } 1092*0b57cec5SDimitry Andric } 1093*0b57cec5SDimitry Andric // Create new DwarfCompileUnit for the given metadata node with tag 1094*0b57cec5SDimitry Andric // DW_TAG_compile_unit. 1095*0b57cec5SDimitry Andric DwarfCompileUnit & 1096*0b57cec5SDimitry Andric DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) { 1097*0b57cec5SDimitry Andric if (auto *CU = CUMap.lookup(DIUnit)) 1098*0b57cec5SDimitry Andric return *CU; 1099*0b57cec5SDimitry Andric 1100*0b57cec5SDimitry Andric CompilationDir = DIUnit->getDirectory(); 1101*0b57cec5SDimitry Andric 11028bcb0991SDimitry Andric auto OwnedUnit = std::make_unique<DwarfCompileUnit>( 1103*0b57cec5SDimitry Andric InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder); 1104*0b57cec5SDimitry Andric DwarfCompileUnit &NewCU = *OwnedUnit; 1105*0b57cec5SDimitry Andric InfoHolder.addUnit(std::move(OwnedUnit)); 1106*0b57cec5SDimitry Andric 1107*0b57cec5SDimitry Andric for (auto *IE : DIUnit->getImportedEntities()) 1108*0b57cec5SDimitry Andric NewCU.addImportedEntity(IE); 1109*0b57cec5SDimitry Andric 1110*0b57cec5SDimitry Andric // LTO with assembly output shares a single line table amongst multiple CUs. 1111*0b57cec5SDimitry Andric // To avoid the compilation directory being ambiguous, let the line table 1112*0b57cec5SDimitry Andric // explicitly describe the directory of all files, never relying on the 1113*0b57cec5SDimitry Andric // compilation directory. 1114*0b57cec5SDimitry Andric if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU) 1115*0b57cec5SDimitry Andric Asm->OutStreamer->emitDwarfFile0Directive( 1116e8d8bef9SDimitry Andric CompilationDir, DIUnit->getFilename(), getMD5AsBytes(DIUnit->getFile()), 1117e8d8bef9SDimitry Andric DIUnit->getSource(), NewCU.getUniqueID()); 1118*0b57cec5SDimitry Andric 1119*0b57cec5SDimitry Andric if (useSplitDwarf()) { 1120*0b57cec5SDimitry Andric NewCU.setSkeleton(constructSkeletonCU(NewCU)); 1121*0b57cec5SDimitry Andric NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection()); 1122*0b57cec5SDimitry Andric } else { 1123*0b57cec5SDimitry Andric finishUnitAttributes(DIUnit, NewCU); 1124*0b57cec5SDimitry Andric NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); 1125*0b57cec5SDimitry Andric } 1126*0b57cec5SDimitry Andric 1127*0b57cec5SDimitry Andric CUMap.insert({DIUnit, &NewCU}); 1128*0b57cec5SDimitry Andric CUDieMap.insert({&NewCU.getUnitDie(), &NewCU}); 1129*0b57cec5SDimitry Andric return NewCU; 1130*0b57cec5SDimitry Andric } 1131*0b57cec5SDimitry Andric 1132*0b57cec5SDimitry Andric void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU, 1133*0b57cec5SDimitry Andric const DIImportedEntity *N) { 1134*0b57cec5SDimitry Andric if (isa<DILocalScope>(N->getScope())) 1135*0b57cec5SDimitry Andric return; 1136*0b57cec5SDimitry Andric if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope())) 1137*0b57cec5SDimitry Andric D->addChild(TheCU.constructImportedEntityDIE(N)); 1138*0b57cec5SDimitry Andric } 1139*0b57cec5SDimitry Andric 1140*0b57cec5SDimitry Andric /// Sort and unique GVEs by comparing their fragment offset. 1141*0b57cec5SDimitry Andric static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> & 1142*0b57cec5SDimitry Andric sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) { 1143*0b57cec5SDimitry Andric llvm::sort( 1144*0b57cec5SDimitry Andric GVEs, [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) { 1145*0b57cec5SDimitry Andric // Sort order: first null exprs, then exprs without fragment 1146*0b57cec5SDimitry Andric // info, then sort by fragment offset in bits. 1147*0b57cec5SDimitry Andric // FIXME: Come up with a more comprehensive comparator so 1148*0b57cec5SDimitry Andric // the sorting isn't non-deterministic, and so the following 1149*0b57cec5SDimitry Andric // std::unique call works correctly. 1150*0b57cec5SDimitry Andric if (!A.Expr || !B.Expr) 1151*0b57cec5SDimitry Andric return !!B.Expr; 1152*0b57cec5SDimitry Andric auto FragmentA = A.Expr->getFragmentInfo(); 1153*0b57cec5SDimitry Andric auto FragmentB = B.Expr->getFragmentInfo(); 1154*0b57cec5SDimitry Andric if (!FragmentA || !FragmentB) 1155*0b57cec5SDimitry Andric return !!FragmentB; 1156*0b57cec5SDimitry Andric return FragmentA->OffsetInBits < FragmentB->OffsetInBits; 1157*0b57cec5SDimitry Andric }); 1158*0b57cec5SDimitry Andric GVEs.erase(std::unique(GVEs.begin(), GVEs.end(), 1159*0b57cec5SDimitry Andric [](DwarfCompileUnit::GlobalExpr A, 1160*0b57cec5SDimitry Andric DwarfCompileUnit::GlobalExpr B) { 1161*0b57cec5SDimitry Andric return A.Expr == B.Expr; 1162*0b57cec5SDimitry Andric }), 1163*0b57cec5SDimitry Andric GVEs.end()); 1164*0b57cec5SDimitry Andric return GVEs; 1165*0b57cec5SDimitry Andric } 1166*0b57cec5SDimitry Andric 1167*0b57cec5SDimitry Andric // Emit all Dwarf sections that should come prior to the content. Create 1168*0b57cec5SDimitry Andric // global DIEs and emit initial debug info sections. This is invoked by 1169*0b57cec5SDimitry Andric // the target AsmPrinter. 1170e8d8bef9SDimitry Andric void DwarfDebug::beginModule(Module *M) { 1171e8d8bef9SDimitry Andric DebugHandlerBase::beginModule(M); 1172*0b57cec5SDimitry Andric 1173e8d8bef9SDimitry Andric if (!Asm || !MMI->hasDebugInfo()) 1174e8d8bef9SDimitry Andric return; 1175*0b57cec5SDimitry Andric 1176*0b57cec5SDimitry Andric unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(), 1177*0b57cec5SDimitry Andric M->debug_compile_units_end()); 1178e8d8bef9SDimitry Andric assert(NumDebugCUs > 0 && "Asm unexpectedly initialized"); 1179e8d8bef9SDimitry Andric assert(MMI->hasDebugInfo() && 1180e8d8bef9SDimitry Andric "DebugInfoAvailabilty unexpectedly not initialized"); 1181*0b57cec5SDimitry Andric SingleCU = NumDebugCUs == 1; 1182*0b57cec5SDimitry Andric DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>> 1183*0b57cec5SDimitry Andric GVMap; 1184*0b57cec5SDimitry Andric for (const GlobalVariable &Global : M->globals()) { 1185*0b57cec5SDimitry Andric SmallVector<DIGlobalVariableExpression *, 1> GVs; 1186*0b57cec5SDimitry Andric Global.getDebugInfo(GVs); 1187*0b57cec5SDimitry Andric for (auto *GVE : GVs) 1188*0b57cec5SDimitry Andric GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()}); 1189*0b57cec5SDimitry Andric } 1190*0b57cec5SDimitry Andric 1191*0b57cec5SDimitry Andric // Create the symbol that designates the start of the unit's contribution 1192*0b57cec5SDimitry Andric // to the string offsets table. In a split DWARF scenario, only the skeleton 1193*0b57cec5SDimitry Andric // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol). 1194*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable()) 1195*0b57cec5SDimitry Andric (useSplitDwarf() ? SkeletonHolder : InfoHolder) 1196*0b57cec5SDimitry Andric .setStringOffsetsStartSym(Asm->createTempSymbol("str_offsets_base")); 1197*0b57cec5SDimitry Andric 1198*0b57cec5SDimitry Andric 1199*0b57cec5SDimitry Andric // Create the symbols that designates the start of the DWARF v5 range list 1200*0b57cec5SDimitry Andric // and locations list tables. They are located past the table headers. 1201*0b57cec5SDimitry Andric if (getDwarfVersion() >= 5) { 1202*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1203*0b57cec5SDimitry Andric Holder.setRnglistsTableBaseSym( 1204*0b57cec5SDimitry Andric Asm->createTempSymbol("rnglists_table_base")); 1205*0b57cec5SDimitry Andric 1206*0b57cec5SDimitry Andric if (useSplitDwarf()) 1207*0b57cec5SDimitry Andric InfoHolder.setRnglistsTableBaseSym( 1208*0b57cec5SDimitry Andric Asm->createTempSymbol("rnglists_dwo_table_base")); 1209*0b57cec5SDimitry Andric } 1210*0b57cec5SDimitry Andric 1211*0b57cec5SDimitry Andric // Create the symbol that points to the first entry following the debug 1212*0b57cec5SDimitry Andric // address table (.debug_addr) header. 1213*0b57cec5SDimitry Andric AddrPool.setLabel(Asm->createTempSymbol("addr_table_base")); 1214480093f4SDimitry Andric DebugLocs.setSym(Asm->createTempSymbol("loclists_table_base")); 1215*0b57cec5SDimitry Andric 1216*0b57cec5SDimitry Andric for (DICompileUnit *CUNode : M->debug_compile_units()) { 1217*0b57cec5SDimitry Andric // FIXME: Move local imported entities into a list attached to the 1218*0b57cec5SDimitry Andric // subprogram, then this search won't be needed and a 1219*0b57cec5SDimitry Andric // getImportedEntities().empty() test should go below with the rest. 1220*0b57cec5SDimitry Andric bool HasNonLocalImportedEntities = llvm::any_of( 1221*0b57cec5SDimitry Andric CUNode->getImportedEntities(), [](const DIImportedEntity *IE) { 1222*0b57cec5SDimitry Andric return !isa<DILocalScope>(IE->getScope()); 1223*0b57cec5SDimitry Andric }); 1224*0b57cec5SDimitry Andric 1225*0b57cec5SDimitry Andric if (!HasNonLocalImportedEntities && CUNode->getEnumTypes().empty() && 1226*0b57cec5SDimitry Andric CUNode->getRetainedTypes().empty() && 1227*0b57cec5SDimitry Andric CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty()) 1228*0b57cec5SDimitry Andric continue; 1229*0b57cec5SDimitry Andric 1230*0b57cec5SDimitry Andric DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode); 1231*0b57cec5SDimitry Andric 1232*0b57cec5SDimitry Andric // Global Variables. 1233*0b57cec5SDimitry Andric for (auto *GVE : CUNode->getGlobalVariables()) { 1234*0b57cec5SDimitry Andric // Don't bother adding DIGlobalVariableExpressions listed in the CU if we 1235*0b57cec5SDimitry Andric // already know about the variable and it isn't adding a constant 1236*0b57cec5SDimitry Andric // expression. 1237*0b57cec5SDimitry Andric auto &GVMapEntry = GVMap[GVE->getVariable()]; 1238*0b57cec5SDimitry Andric auto *Expr = GVE->getExpression(); 1239*0b57cec5SDimitry Andric if (!GVMapEntry.size() || (Expr && Expr->isConstant())) 1240*0b57cec5SDimitry Andric GVMapEntry.push_back({nullptr, Expr}); 1241*0b57cec5SDimitry Andric } 1242fe6060f1SDimitry Andric 1243*0b57cec5SDimitry Andric DenseSet<DIGlobalVariable *> Processed; 1244*0b57cec5SDimitry Andric for (auto *GVE : CUNode->getGlobalVariables()) { 1245*0b57cec5SDimitry Andric DIGlobalVariable *GV = GVE->getVariable(); 1246*0b57cec5SDimitry Andric if (Processed.insert(GV).second) 1247*0b57cec5SDimitry Andric CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV])); 1248*0b57cec5SDimitry Andric } 1249*0b57cec5SDimitry Andric 12500eae32dcSDimitry Andric for (auto *Ty : CUNode->getEnumTypes()) 1251*0b57cec5SDimitry Andric CU.getOrCreateTypeDIE(cast<DIType>(Ty)); 12520eae32dcSDimitry Andric 1253*0b57cec5SDimitry Andric for (auto *Ty : CUNode->getRetainedTypes()) { 1254*0b57cec5SDimitry Andric // The retained types array by design contains pointers to 1255*0b57cec5SDimitry Andric // MDNodes rather than DIRefs. Unique them here. 1256*0b57cec5SDimitry Andric if (DIType *RT = dyn_cast<DIType>(Ty)) 1257*0b57cec5SDimitry Andric // There is no point in force-emitting a forward declaration. 1258*0b57cec5SDimitry Andric CU.getOrCreateTypeDIE(RT); 1259*0b57cec5SDimitry Andric } 1260*0b57cec5SDimitry Andric // Emit imported_modules last so that the relevant context is already 1261*0b57cec5SDimitry Andric // available. 1262*0b57cec5SDimitry Andric for (auto *IE : CUNode->getImportedEntities()) 1263*0b57cec5SDimitry Andric constructAndAddImportedEntityDIE(CU, IE); 1264*0b57cec5SDimitry Andric } 1265*0b57cec5SDimitry Andric } 1266*0b57cec5SDimitry Andric 1267*0b57cec5SDimitry Andric void DwarfDebug::finishEntityDefinitions() { 1268*0b57cec5SDimitry Andric for (const auto &Entity : ConcreteEntities) { 1269*0b57cec5SDimitry Andric DIE *Die = Entity->getDIE(); 1270*0b57cec5SDimitry Andric assert(Die); 1271*0b57cec5SDimitry Andric // FIXME: Consider the time-space tradeoff of just storing the unit pointer 1272*0b57cec5SDimitry Andric // in the ConcreteEntities list, rather than looking it up again here. 1273*0b57cec5SDimitry Andric // DIE::getUnit isn't simple - it walks parent pointers, etc. 1274*0b57cec5SDimitry Andric DwarfCompileUnit *Unit = CUDieMap.lookup(Die->getUnitDie()); 1275*0b57cec5SDimitry Andric assert(Unit); 1276*0b57cec5SDimitry Andric Unit->finishEntityDefinition(Entity.get()); 1277*0b57cec5SDimitry Andric } 1278*0b57cec5SDimitry Andric } 1279*0b57cec5SDimitry Andric 1280*0b57cec5SDimitry Andric void DwarfDebug::finishSubprogramDefinitions() { 1281*0b57cec5SDimitry Andric for (const DISubprogram *SP : ProcessedSPNodes) { 1282*0b57cec5SDimitry Andric assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug); 1283*0b57cec5SDimitry Andric forBothCUs( 1284*0b57cec5SDimitry Andric getOrCreateDwarfCompileUnit(SP->getUnit()), 1285*0b57cec5SDimitry Andric [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); }); 1286*0b57cec5SDimitry Andric } 1287*0b57cec5SDimitry Andric } 1288*0b57cec5SDimitry Andric 1289*0b57cec5SDimitry Andric void DwarfDebug::finalizeModuleInfo() { 1290*0b57cec5SDimitry Andric const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 1291*0b57cec5SDimitry Andric 1292*0b57cec5SDimitry Andric finishSubprogramDefinitions(); 1293*0b57cec5SDimitry Andric 1294*0b57cec5SDimitry Andric finishEntityDefinitions(); 1295*0b57cec5SDimitry Andric 1296*0b57cec5SDimitry Andric // Include the DWO file name in the hash if there's more than one CU. 1297*0b57cec5SDimitry Andric // This handles ThinLTO's situation where imported CUs may very easily be 1298*0b57cec5SDimitry Andric // duplicate with the same CU partially imported into another ThinLTO unit. 1299*0b57cec5SDimitry Andric StringRef DWOName; 1300*0b57cec5SDimitry Andric if (CUMap.size() > 1) 1301*0b57cec5SDimitry Andric DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile; 1302*0b57cec5SDimitry Andric 1303*0b57cec5SDimitry Andric // Handle anything that needs to be done on a per-unit basis after 1304*0b57cec5SDimitry Andric // all other generation. 1305*0b57cec5SDimitry Andric for (const auto &P : CUMap) { 1306*0b57cec5SDimitry Andric auto &TheCU = *P.second; 1307*0b57cec5SDimitry Andric if (TheCU.getCUNode()->isDebugDirectivesOnly()) 1308*0b57cec5SDimitry Andric continue; 1309*0b57cec5SDimitry Andric // Emit DW_AT_containing_type attribute to connect types with their 1310*0b57cec5SDimitry Andric // vtable holding type. 1311*0b57cec5SDimitry Andric TheCU.constructContainingTypeDIEs(); 1312*0b57cec5SDimitry Andric 1313*0b57cec5SDimitry Andric // Add CU specific attributes if we need to add any. 1314*0b57cec5SDimitry Andric // If we're splitting the dwarf out now that we've got the entire 1315*0b57cec5SDimitry Andric // CU then add the dwo id to it. 1316*0b57cec5SDimitry Andric auto *SkCU = TheCU.getSkeleton(); 1317480093f4SDimitry Andric 1318480093f4SDimitry Andric bool HasSplitUnit = SkCU && !TheCU.getUnitDie().children().empty(); 1319480093f4SDimitry Andric 1320480093f4SDimitry Andric if (HasSplitUnit) { 1321480093f4SDimitry Andric dwarf::Attribute attrDWOName = getDwarfVersion() >= 5 1322480093f4SDimitry Andric ? dwarf::DW_AT_dwo_name 1323480093f4SDimitry Andric : dwarf::DW_AT_GNU_dwo_name; 1324*0b57cec5SDimitry Andric finishUnitAttributes(TheCU.getCUNode(), TheCU); 1325480093f4SDimitry Andric TheCU.addString(TheCU.getUnitDie(), attrDWOName, 1326*0b57cec5SDimitry Andric Asm->TM.Options.MCOptions.SplitDwarfFile); 1327480093f4SDimitry Andric SkCU->addString(SkCU->getUnitDie(), attrDWOName, 1328*0b57cec5SDimitry Andric Asm->TM.Options.MCOptions.SplitDwarfFile); 1329*0b57cec5SDimitry Andric // Emit a unique identifier for this CU. 1330*0b57cec5SDimitry Andric uint64_t ID = 1331e8d8bef9SDimitry Andric DIEHash(Asm, &TheCU).computeCUSignature(DWOName, TheCU.getUnitDie()); 1332*0b57cec5SDimitry Andric if (getDwarfVersion() >= 5) { 1333*0b57cec5SDimitry Andric TheCU.setDWOId(ID); 1334*0b57cec5SDimitry Andric SkCU->setDWOId(ID); 1335*0b57cec5SDimitry Andric } else { 1336*0b57cec5SDimitry Andric TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id, 1337*0b57cec5SDimitry Andric dwarf::DW_FORM_data8, ID); 1338*0b57cec5SDimitry Andric SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id, 1339*0b57cec5SDimitry Andric dwarf::DW_FORM_data8, ID); 1340*0b57cec5SDimitry Andric } 1341*0b57cec5SDimitry Andric 1342*0b57cec5SDimitry Andric if (getDwarfVersion() < 5 && !SkeletonHolder.getRangeLists().empty()) { 1343*0b57cec5SDimitry Andric const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol(); 1344*0b57cec5SDimitry Andric SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base, 1345*0b57cec5SDimitry Andric Sym, Sym); 1346*0b57cec5SDimitry Andric } 1347*0b57cec5SDimitry Andric } else if (SkCU) { 1348*0b57cec5SDimitry Andric finishUnitAttributes(SkCU->getCUNode(), *SkCU); 1349*0b57cec5SDimitry Andric } 1350*0b57cec5SDimitry Andric 1351*0b57cec5SDimitry Andric // If we have code split among multiple sections or non-contiguous 1352*0b57cec5SDimitry Andric // ranges of code then emit a DW_AT_ranges attribute on the unit that will 1353*0b57cec5SDimitry Andric // remain in the .o file, otherwise add a DW_AT_low_pc. 1354*0b57cec5SDimitry Andric // FIXME: We should use ranges allow reordering of code ala 1355*0b57cec5SDimitry Andric // .subsections_via_symbols in mach-o. This would mean turning on 1356*0b57cec5SDimitry Andric // ranges for all subprogram DIEs for mach-o. 1357*0b57cec5SDimitry Andric DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; 1358*0b57cec5SDimitry Andric 1359*0b57cec5SDimitry Andric if (unsigned NumRanges = TheCU.getRanges().size()) { 1360*0b57cec5SDimitry Andric if (NumRanges > 1 && useRangesSection()) 1361*0b57cec5SDimitry Andric // A DW_AT_low_pc attribute may also be specified in combination with 1362*0b57cec5SDimitry Andric // DW_AT_ranges to specify the default base address for use in 1363*0b57cec5SDimitry Andric // location lists (see Section 2.6.2) and range lists (see Section 1364*0b57cec5SDimitry Andric // 2.17.3). 1365*0b57cec5SDimitry Andric U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0); 1366*0b57cec5SDimitry Andric else 13678bcb0991SDimitry Andric U.setBaseAddress(TheCU.getRanges().front().Begin); 1368*0b57cec5SDimitry Andric U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges()); 1369*0b57cec5SDimitry Andric } 1370*0b57cec5SDimitry Andric 1371*0b57cec5SDimitry Andric // We don't keep track of which addresses are used in which CU so this 1372*0b57cec5SDimitry Andric // is a bit pessimistic under LTO. 13735ffd83dbSDimitry Andric if ((HasSplitUnit || getDwarfVersion() >= 5) && !AddrPool.isEmpty()) 1374*0b57cec5SDimitry Andric U.addAddrTableBase(); 1375*0b57cec5SDimitry Andric 1376*0b57cec5SDimitry Andric if (getDwarfVersion() >= 5) { 1377*0b57cec5SDimitry Andric if (U.hasRangeLists()) 1378*0b57cec5SDimitry Andric U.addRnglistsBase(); 1379*0b57cec5SDimitry Andric 1380480093f4SDimitry Andric if (!DebugLocs.getLists().empty()) { 1381480093f4SDimitry Andric if (!useSplitDwarf()) 13828bcb0991SDimitry Andric U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_loclists_base, 13838bcb0991SDimitry Andric DebugLocs.getSym(), 13848bcb0991SDimitry Andric TLOF.getDwarfLoclistsSection()->getBeginSymbol()); 13858bcb0991SDimitry Andric } 1386*0b57cec5SDimitry Andric } 1387*0b57cec5SDimitry Andric 1388*0b57cec5SDimitry Andric auto *CUNode = cast<DICompileUnit>(P.first); 13895ffd83dbSDimitry Andric // If compile Unit has macros, emit "DW_AT_macro_info/DW_AT_macros" 13905ffd83dbSDimitry Andric // attribute. 1391480093f4SDimitry Andric if (CUNode->getMacros()) { 1392e8d8bef9SDimitry Andric if (UseDebugMacroSection) { 1393480093f4SDimitry Andric if (useSplitDwarf()) 13945ffd83dbSDimitry Andric TheCU.addSectionDelta( 13955ffd83dbSDimitry Andric TheCU.getUnitDie(), dwarf::DW_AT_macros, U.getMacroLabelBegin(), 13965ffd83dbSDimitry Andric TLOF.getDwarfMacroDWOSection()->getBeginSymbol()); 1397e8d8bef9SDimitry Andric else { 1398e8d8bef9SDimitry Andric dwarf::Attribute MacrosAttr = getDwarfVersion() >= 5 1399e8d8bef9SDimitry Andric ? dwarf::DW_AT_macros 1400e8d8bef9SDimitry Andric : dwarf::DW_AT_GNU_macros; 1401e8d8bef9SDimitry Andric U.addSectionLabel(U.getUnitDie(), MacrosAttr, U.getMacroLabelBegin(), 14025ffd83dbSDimitry Andric TLOF.getDwarfMacroSection()->getBeginSymbol()); 1403e8d8bef9SDimitry Andric } 14045ffd83dbSDimitry Andric } else { 14055ffd83dbSDimitry Andric if (useSplitDwarf()) 14065ffd83dbSDimitry Andric TheCU.addSectionDelta( 14075ffd83dbSDimitry Andric TheCU.getUnitDie(), dwarf::DW_AT_macro_info, 1408480093f4SDimitry Andric U.getMacroLabelBegin(), 1409480093f4SDimitry Andric TLOF.getDwarfMacinfoDWOSection()->getBeginSymbol()); 1410480093f4SDimitry Andric else 1411*0b57cec5SDimitry Andric U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info, 1412*0b57cec5SDimitry Andric U.getMacroLabelBegin(), 1413*0b57cec5SDimitry Andric TLOF.getDwarfMacinfoSection()->getBeginSymbol()); 1414*0b57cec5SDimitry Andric } 1415480093f4SDimitry Andric } 14165ffd83dbSDimitry Andric } 1417*0b57cec5SDimitry Andric 1418*0b57cec5SDimitry Andric // Emit all frontend-produced Skeleton CUs, i.e., Clang modules. 1419*0b57cec5SDimitry Andric for (auto *CUNode : MMI->getModule()->debug_compile_units()) 1420*0b57cec5SDimitry Andric if (CUNode->getDWOId()) 1421*0b57cec5SDimitry Andric getOrCreateDwarfCompileUnit(CUNode); 1422*0b57cec5SDimitry Andric 1423*0b57cec5SDimitry Andric // Compute DIE offsets and sizes. 1424*0b57cec5SDimitry Andric InfoHolder.computeSizeAndOffsets(); 1425*0b57cec5SDimitry Andric if (useSplitDwarf()) 1426*0b57cec5SDimitry Andric SkeletonHolder.computeSizeAndOffsets(); 1427*0b57cec5SDimitry Andric } 1428*0b57cec5SDimitry Andric 1429*0b57cec5SDimitry Andric // Emit all Dwarf sections that should come after the content. 1430*0b57cec5SDimitry Andric void DwarfDebug::endModule() { 1431349cc55cSDimitry Andric // Terminate the pending line table. 1432349cc55cSDimitry Andric if (PrevCU) 1433349cc55cSDimitry Andric terminateLineTable(PrevCU); 1434349cc55cSDimitry Andric PrevCU = nullptr; 1435*0b57cec5SDimitry Andric assert(CurFn == nullptr); 1436*0b57cec5SDimitry Andric assert(CurMI == nullptr); 1437*0b57cec5SDimitry Andric 1438*0b57cec5SDimitry Andric for (const auto &P : CUMap) { 1439*0b57cec5SDimitry Andric auto &CU = *P.second; 1440*0b57cec5SDimitry Andric CU.createBaseTypeDIEs(); 1441*0b57cec5SDimitry Andric } 1442*0b57cec5SDimitry Andric 1443*0b57cec5SDimitry Andric // If we aren't actually generating debug info (check beginModule - 1444e8d8bef9SDimitry Andric // conditionalized on the presence of the llvm.dbg.cu metadata node) 1445e8d8bef9SDimitry Andric if (!Asm || !MMI->hasDebugInfo()) 1446*0b57cec5SDimitry Andric return; 1447*0b57cec5SDimitry Andric 1448*0b57cec5SDimitry Andric // Finalize the debug info for the module. 1449*0b57cec5SDimitry Andric finalizeModuleInfo(); 1450*0b57cec5SDimitry Andric 1451*0b57cec5SDimitry Andric if (useSplitDwarf()) 1452480093f4SDimitry Andric // Emit debug_loc.dwo/debug_loclists.dwo section. 1453*0b57cec5SDimitry Andric emitDebugLocDWO(); 1454*0b57cec5SDimitry Andric else 1455480093f4SDimitry Andric // Emit debug_loc/debug_loclists section. 1456*0b57cec5SDimitry Andric emitDebugLoc(); 1457*0b57cec5SDimitry Andric 1458*0b57cec5SDimitry Andric // Corresponding abbreviations into a abbrev section. 1459*0b57cec5SDimitry Andric emitAbbreviations(); 1460*0b57cec5SDimitry Andric 1461*0b57cec5SDimitry Andric // Emit all the DIEs into a debug info section. 1462*0b57cec5SDimitry Andric emitDebugInfo(); 1463*0b57cec5SDimitry Andric 1464*0b57cec5SDimitry Andric // Emit info into a debug aranges section. 1465*0b57cec5SDimitry Andric if (GenerateARangeSection) 1466*0b57cec5SDimitry Andric emitDebugARanges(); 1467*0b57cec5SDimitry Andric 1468*0b57cec5SDimitry Andric // Emit info into a debug ranges section. 1469*0b57cec5SDimitry Andric emitDebugRanges(); 1470*0b57cec5SDimitry Andric 1471480093f4SDimitry Andric if (useSplitDwarf()) 1472480093f4SDimitry Andric // Emit info into a debug macinfo.dwo section. 1473480093f4SDimitry Andric emitDebugMacinfoDWO(); 1474480093f4SDimitry Andric else 14755ffd83dbSDimitry Andric // Emit info into a debug macinfo/macro section. 1476*0b57cec5SDimitry Andric emitDebugMacinfo(); 1477*0b57cec5SDimitry Andric 14785ffd83dbSDimitry Andric emitDebugStr(); 14795ffd83dbSDimitry Andric 1480*0b57cec5SDimitry Andric if (useSplitDwarf()) { 1481*0b57cec5SDimitry Andric emitDebugStrDWO(); 1482*0b57cec5SDimitry Andric emitDebugInfoDWO(); 1483*0b57cec5SDimitry Andric emitDebugAbbrevDWO(); 1484*0b57cec5SDimitry Andric emitDebugLineDWO(); 1485*0b57cec5SDimitry Andric emitDebugRangesDWO(); 1486*0b57cec5SDimitry Andric } 1487*0b57cec5SDimitry Andric 1488*0b57cec5SDimitry Andric emitDebugAddr(); 1489*0b57cec5SDimitry Andric 1490*0b57cec5SDimitry Andric // Emit info into the dwarf accelerator table sections. 1491*0b57cec5SDimitry Andric switch (getAccelTableKind()) { 1492*0b57cec5SDimitry Andric case AccelTableKind::Apple: 1493*0b57cec5SDimitry Andric emitAccelNames(); 1494*0b57cec5SDimitry Andric emitAccelObjC(); 1495*0b57cec5SDimitry Andric emitAccelNamespaces(); 1496*0b57cec5SDimitry Andric emitAccelTypes(); 1497*0b57cec5SDimitry Andric break; 1498*0b57cec5SDimitry Andric case AccelTableKind::Dwarf: 1499*0b57cec5SDimitry Andric emitAccelDebugNames(); 1500*0b57cec5SDimitry Andric break; 1501*0b57cec5SDimitry Andric case AccelTableKind::None: 1502*0b57cec5SDimitry Andric break; 1503*0b57cec5SDimitry Andric case AccelTableKind::Default: 1504*0b57cec5SDimitry Andric llvm_unreachable("Default should have already been resolved."); 1505*0b57cec5SDimitry Andric } 1506*0b57cec5SDimitry Andric 1507*0b57cec5SDimitry Andric // Emit the pubnames and pubtypes sections if requested. 1508*0b57cec5SDimitry Andric emitDebugPubSections(); 1509*0b57cec5SDimitry Andric 1510*0b57cec5SDimitry Andric // clean up. 1511*0b57cec5SDimitry Andric // FIXME: AbstractVariables.clear(); 1512*0b57cec5SDimitry Andric } 1513*0b57cec5SDimitry Andric 1514*0b57cec5SDimitry Andric void DwarfDebug::ensureAbstractEntityIsCreated(DwarfCompileUnit &CU, 1515*0b57cec5SDimitry Andric const DINode *Node, 1516*0b57cec5SDimitry Andric const MDNode *ScopeNode) { 1517*0b57cec5SDimitry Andric if (CU.getExistingAbstractEntity(Node)) 1518*0b57cec5SDimitry Andric return; 1519*0b57cec5SDimitry Andric 1520*0b57cec5SDimitry Andric CU.createAbstractEntity(Node, LScopes.getOrCreateAbstractScope( 1521*0b57cec5SDimitry Andric cast<DILocalScope>(ScopeNode))); 1522*0b57cec5SDimitry Andric } 1523*0b57cec5SDimitry Andric 1524*0b57cec5SDimitry Andric void DwarfDebug::ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU, 1525*0b57cec5SDimitry Andric const DINode *Node, const MDNode *ScopeNode) { 1526*0b57cec5SDimitry Andric if (CU.getExistingAbstractEntity(Node)) 1527*0b57cec5SDimitry Andric return; 1528*0b57cec5SDimitry Andric 1529*0b57cec5SDimitry Andric if (LexicalScope *Scope = 1530*0b57cec5SDimitry Andric LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode))) 1531*0b57cec5SDimitry Andric CU.createAbstractEntity(Node, Scope); 1532*0b57cec5SDimitry Andric } 1533*0b57cec5SDimitry Andric 1534*0b57cec5SDimitry Andric // Collect variable information from side table maintained by MF. 1535*0b57cec5SDimitry Andric void DwarfDebug::collectVariableInfoFromMFTable( 1536*0b57cec5SDimitry Andric DwarfCompileUnit &TheCU, DenseSet<InlinedEntity> &Processed) { 1537*0b57cec5SDimitry Andric SmallDenseMap<InlinedEntity, DbgVariable *> MFVars; 15385ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "DwarfDebug: collecting variables from MF side table\n"); 1539*0b57cec5SDimitry Andric for (const auto &VI : Asm->MF->getVariableDbgInfo()) { 1540*0b57cec5SDimitry Andric if (!VI.Var) 1541*0b57cec5SDimitry Andric continue; 1542*0b57cec5SDimitry Andric assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && 1543*0b57cec5SDimitry Andric "Expected inlined-at fields to agree"); 1544*0b57cec5SDimitry Andric 1545*0b57cec5SDimitry Andric InlinedEntity Var(VI.Var, VI.Loc->getInlinedAt()); 1546*0b57cec5SDimitry Andric Processed.insert(Var); 1547*0b57cec5SDimitry Andric LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); 1548*0b57cec5SDimitry Andric 1549*0b57cec5SDimitry Andric // If variable scope is not found then skip this variable. 15505ffd83dbSDimitry Andric if (!Scope) { 15515ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Dropping debug info for " << VI.Var->getName() 15525ffd83dbSDimitry Andric << ", no variable scope found\n"); 1553*0b57cec5SDimitry Andric continue; 15545ffd83dbSDimitry Andric } 1555*0b57cec5SDimitry Andric 1556*0b57cec5SDimitry Andric ensureAbstractEntityIsCreatedIfScoped(TheCU, Var.first, Scope->getScopeNode()); 15578bcb0991SDimitry Andric auto RegVar = std::make_unique<DbgVariable>( 1558*0b57cec5SDimitry Andric cast<DILocalVariable>(Var.first), Var.second); 1559*0b57cec5SDimitry Andric RegVar->initializeMMI(VI.Expr, VI.Slot); 15605ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Created DbgVariable for " << VI.Var->getName() 15615ffd83dbSDimitry Andric << "\n"); 1562fe6060f1SDimitry Andric 1563*0b57cec5SDimitry Andric if (DbgVariable *DbgVar = MFVars.lookup(Var)) 1564*0b57cec5SDimitry Andric DbgVar->addMMIEntry(*RegVar); 1565*0b57cec5SDimitry Andric else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) { 1566*0b57cec5SDimitry Andric MFVars.insert({Var, RegVar.get()}); 1567*0b57cec5SDimitry Andric ConcreteEntities.push_back(std::move(RegVar)); 1568*0b57cec5SDimitry Andric } 1569*0b57cec5SDimitry Andric } 1570*0b57cec5SDimitry Andric } 1571*0b57cec5SDimitry Andric 1572*0b57cec5SDimitry Andric /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its 1573*0b57cec5SDimitry Andric /// enclosing lexical scope. The check ensures there are no other instructions 1574*0b57cec5SDimitry Andric /// in the same lexical scope preceding the DBG_VALUE and that its range is 1575*0b57cec5SDimitry Andric /// either open or otherwise rolls off the end of the scope. 1576*0b57cec5SDimitry Andric static bool validThroughout(LexicalScopes &LScopes, 1577*0b57cec5SDimitry Andric const MachineInstr *DbgValue, 1578e8d8bef9SDimitry Andric const MachineInstr *RangeEnd, 1579e8d8bef9SDimitry Andric const InstructionOrdering &Ordering) { 1580*0b57cec5SDimitry Andric assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location"); 1581*0b57cec5SDimitry Andric auto MBB = DbgValue->getParent(); 1582*0b57cec5SDimitry Andric auto DL = DbgValue->getDebugLoc(); 1583*0b57cec5SDimitry Andric auto *LScope = LScopes.findLexicalScope(DL); 1584*0b57cec5SDimitry Andric // Scope doesn't exist; this is a dead DBG_VALUE. 1585*0b57cec5SDimitry Andric if (!LScope) 1586*0b57cec5SDimitry Andric return false; 1587*0b57cec5SDimitry Andric auto &LSRange = LScope->getRanges(); 1588*0b57cec5SDimitry Andric if (LSRange.size() == 0) 1589*0b57cec5SDimitry Andric return false; 1590*0b57cec5SDimitry Andric 1591*0b57cec5SDimitry Andric const MachineInstr *LScopeBegin = LSRange.front().first; 1592e8d8bef9SDimitry Andric // If the scope starts before the DBG_VALUE then we may have a negative 1593e8d8bef9SDimitry Andric // result. Otherwise the location is live coming into the scope and we 1594e8d8bef9SDimitry Andric // can skip the following checks. 1595e8d8bef9SDimitry Andric if (!Ordering.isBefore(DbgValue, LScopeBegin)) { 1596e8d8bef9SDimitry Andric // Exit if the lexical scope begins outside of the current block. 1597*0b57cec5SDimitry Andric if (LScopeBegin->getParent() != MBB) 1598*0b57cec5SDimitry Andric return false; 15995ffd83dbSDimitry Andric 1600*0b57cec5SDimitry Andric MachineBasicBlock::const_reverse_iterator Pred(DbgValue); 1601*0b57cec5SDimitry Andric for (++Pred; Pred != MBB->rend(); ++Pred) { 1602*0b57cec5SDimitry Andric if (Pred->getFlag(MachineInstr::FrameSetup)) 1603*0b57cec5SDimitry Andric break; 1604*0b57cec5SDimitry Andric auto PredDL = Pred->getDebugLoc(); 1605*0b57cec5SDimitry Andric if (!PredDL || Pred->isMetaInstruction()) 1606*0b57cec5SDimitry Andric continue; 1607*0b57cec5SDimitry Andric // Check whether the instruction preceding the DBG_VALUE is in the same 1608*0b57cec5SDimitry Andric // (sub)scope as the DBG_VALUE. 1609*0b57cec5SDimitry Andric if (DL->getScope() == PredDL->getScope()) 1610*0b57cec5SDimitry Andric return false; 1611*0b57cec5SDimitry Andric auto *PredScope = LScopes.findLexicalScope(PredDL); 1612*0b57cec5SDimitry Andric if (!PredScope || LScope->dominates(PredScope)) 1613*0b57cec5SDimitry Andric return false; 1614*0b57cec5SDimitry Andric } 1615e8d8bef9SDimitry Andric } 1616*0b57cec5SDimitry Andric 1617*0b57cec5SDimitry Andric // If the range of the DBG_VALUE is open-ended, report success. 1618*0b57cec5SDimitry Andric if (!RangeEnd) 1619*0b57cec5SDimitry Andric return true; 1620*0b57cec5SDimitry Andric 1621*0b57cec5SDimitry Andric // Single, constant DBG_VALUEs in the prologue are promoted to be live 1622*0b57cec5SDimitry Andric // throughout the function. This is a hack, presumably for DWARF v2 and not 1623*0b57cec5SDimitry Andric // necessarily correct. It would be much better to use a dbg.declare instead 1624*0b57cec5SDimitry Andric // if we know the constant is live throughout the scope. 1625fe6060f1SDimitry Andric if (MBB->pred_empty() && 1626fe6060f1SDimitry Andric all_of(DbgValue->debug_operands(), 1627fe6060f1SDimitry Andric [](const MachineOperand &Op) { return Op.isImm(); })) 1628*0b57cec5SDimitry Andric return true; 1629*0b57cec5SDimitry Andric 1630e8d8bef9SDimitry Andric // Test if the location terminates before the end of the scope. 1631e8d8bef9SDimitry Andric const MachineInstr *LScopeEnd = LSRange.back().second; 1632e8d8bef9SDimitry Andric if (Ordering.isBefore(RangeEnd, LScopeEnd)) 1633*0b57cec5SDimitry Andric return false; 16345ffd83dbSDimitry Andric 16355ffd83dbSDimitry Andric // There's a single location which starts at the scope start, and ends at or 16365ffd83dbSDimitry Andric // after the scope end. 16375ffd83dbSDimitry Andric return true; 1638*0b57cec5SDimitry Andric } 1639*0b57cec5SDimitry Andric 1640*0b57cec5SDimitry Andric /// Build the location list for all DBG_VALUEs in the function that 1641*0b57cec5SDimitry Andric /// describe the same variable. The resulting DebugLocEntries will have 1642*0b57cec5SDimitry Andric /// strict monotonically increasing begin addresses and will never 1643*0b57cec5SDimitry Andric /// overlap. If the resulting list has only one entry that is valid 1644*0b57cec5SDimitry Andric /// throughout variable's scope return true. 1645*0b57cec5SDimitry Andric // 1646*0b57cec5SDimitry Andric // See the definition of DbgValueHistoryMap::Entry for an explanation of the 1647*0b57cec5SDimitry Andric // different kinds of history map entries. One thing to be aware of is that if 1648*0b57cec5SDimitry Andric // a debug value is ended by another entry (rather than being valid until the 1649*0b57cec5SDimitry Andric // end of the function), that entry's instruction may or may not be included in 1650*0b57cec5SDimitry Andric // the range, depending on if the entry is a clobbering entry (it has an 1651*0b57cec5SDimitry Andric // instruction that clobbers one or more preceding locations), or if it is an 1652*0b57cec5SDimitry Andric // (overlapping) debug value entry. This distinction can be seen in the example 1653*0b57cec5SDimitry Andric // below. The first debug value is ended by the clobbering entry 2, and the 1654*0b57cec5SDimitry Andric // second and third debug values are ended by the overlapping debug value entry 1655*0b57cec5SDimitry Andric // 4. 1656*0b57cec5SDimitry Andric // 1657*0b57cec5SDimitry Andric // Input: 1658*0b57cec5SDimitry Andric // 1659*0b57cec5SDimitry Andric // History map entries [type, end index, mi] 1660*0b57cec5SDimitry Andric // 1661*0b57cec5SDimitry Andric // 0 | [DbgValue, 2, DBG_VALUE $reg0, [...] (fragment 0, 32)] 1662*0b57cec5SDimitry Andric // 1 | | [DbgValue, 4, DBG_VALUE $reg1, [...] (fragment 32, 32)] 1663*0b57cec5SDimitry Andric // 2 | | [Clobber, $reg0 = [...], -, -] 1664*0b57cec5SDimitry Andric // 3 | | [DbgValue, 4, DBG_VALUE 123, [...] (fragment 64, 32)] 1665*0b57cec5SDimitry Andric // 4 [DbgValue, ~0, DBG_VALUE @g, [...] (fragment 0, 96)] 1666*0b57cec5SDimitry Andric // 1667*0b57cec5SDimitry Andric // Output [start, end) [Value...]: 1668*0b57cec5SDimitry Andric // 1669*0b57cec5SDimitry Andric // [0-1) [(reg0, fragment 0, 32)] 1670*0b57cec5SDimitry Andric // [1-3) [(reg0, fragment 0, 32), (reg1, fragment 32, 32)] 1671*0b57cec5SDimitry Andric // [3-4) [(reg1, fragment 32, 32), (123, fragment 64, 32)] 1672*0b57cec5SDimitry Andric // [4-) [(@g, fragment 0, 96)] 1673e8d8bef9SDimitry Andric bool DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc, 1674e8d8bef9SDimitry Andric const DbgValueHistoryMap::Entries &Entries) { 1675*0b57cec5SDimitry Andric using OpenRange = 1676*0b57cec5SDimitry Andric std::pair<DbgValueHistoryMap::EntryIndex, DbgValueLoc>; 1677*0b57cec5SDimitry Andric SmallVector<OpenRange, 4> OpenRanges; 1678*0b57cec5SDimitry Andric bool isSafeForSingleLocation = true; 1679*0b57cec5SDimitry Andric const MachineInstr *StartDebugMI = nullptr; 1680*0b57cec5SDimitry Andric const MachineInstr *EndMI = nullptr; 1681*0b57cec5SDimitry Andric 1682*0b57cec5SDimitry Andric for (auto EB = Entries.begin(), EI = EB, EE = Entries.end(); EI != EE; ++EI) { 1683*0b57cec5SDimitry Andric const MachineInstr *Instr = EI->getInstr(); 1684*0b57cec5SDimitry Andric 1685*0b57cec5SDimitry Andric // Remove all values that are no longer live. 1686*0b57cec5SDimitry Andric size_t Index = std::distance(EB, EI); 1687e8d8bef9SDimitry Andric erase_if(OpenRanges, [&](OpenRange &R) { return R.first <= Index; }); 1688*0b57cec5SDimitry Andric 1689*0b57cec5SDimitry Andric // If we are dealing with a clobbering entry, this iteration will result in 1690*0b57cec5SDimitry Andric // a location list entry starting after the clobbering instruction. 1691*0b57cec5SDimitry Andric const MCSymbol *StartLabel = 1692*0b57cec5SDimitry Andric EI->isClobber() ? getLabelAfterInsn(Instr) : getLabelBeforeInsn(Instr); 1693*0b57cec5SDimitry Andric assert(StartLabel && 1694*0b57cec5SDimitry Andric "Forgot label before/after instruction starting a range!"); 1695*0b57cec5SDimitry Andric 1696*0b57cec5SDimitry Andric const MCSymbol *EndLabel; 1697*0b57cec5SDimitry Andric if (std::next(EI) == Entries.end()) { 16985ffd83dbSDimitry Andric const MachineBasicBlock &EndMBB = Asm->MF->back(); 16995ffd83dbSDimitry Andric EndLabel = Asm->MBBSectionRanges[EndMBB.getSectionIDNum()].EndLabel; 1700*0b57cec5SDimitry Andric if (EI->isClobber()) 1701*0b57cec5SDimitry Andric EndMI = EI->getInstr(); 1702*0b57cec5SDimitry Andric } 1703*0b57cec5SDimitry Andric else if (std::next(EI)->isClobber()) 1704*0b57cec5SDimitry Andric EndLabel = getLabelAfterInsn(std::next(EI)->getInstr()); 1705*0b57cec5SDimitry Andric else 1706*0b57cec5SDimitry Andric EndLabel = getLabelBeforeInsn(std::next(EI)->getInstr()); 1707*0b57cec5SDimitry Andric assert(EndLabel && "Forgot label after instruction ending a range!"); 1708*0b57cec5SDimitry Andric 1709*0b57cec5SDimitry Andric if (EI->isDbgValue()) 1710*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "DotDebugLoc: " << *Instr << "\n"); 1711*0b57cec5SDimitry Andric 1712*0b57cec5SDimitry Andric // If this history map entry has a debug value, add that to the list of 1713*0b57cec5SDimitry Andric // open ranges and check if its location is valid for a single value 1714*0b57cec5SDimitry Andric // location. 1715*0b57cec5SDimitry Andric if (EI->isDbgValue()) { 1716*0b57cec5SDimitry Andric // Do not add undef debug values, as they are redundant information in 1717*0b57cec5SDimitry Andric // the location list entries. An undef debug results in an empty location 1718*0b57cec5SDimitry Andric // description. If there are any non-undef fragments then padding pieces 1719*0b57cec5SDimitry Andric // with empty location descriptions will automatically be inserted, and if 1720*0b57cec5SDimitry Andric // all fragments are undef then the whole location list entry is 1721*0b57cec5SDimitry Andric // redundant. 1722*0b57cec5SDimitry Andric if (!Instr->isUndefDebugValue()) { 1723*0b57cec5SDimitry Andric auto Value = getDebugLocValue(Instr); 1724*0b57cec5SDimitry Andric OpenRanges.emplace_back(EI->getEndIndex(), Value); 1725*0b57cec5SDimitry Andric 1726*0b57cec5SDimitry Andric // TODO: Add support for single value fragment locations. 1727*0b57cec5SDimitry Andric if (Instr->getDebugExpression()->isFragment()) 1728*0b57cec5SDimitry Andric isSafeForSingleLocation = false; 1729*0b57cec5SDimitry Andric 1730*0b57cec5SDimitry Andric if (!StartDebugMI) 1731*0b57cec5SDimitry Andric StartDebugMI = Instr; 1732*0b57cec5SDimitry Andric } else { 1733*0b57cec5SDimitry Andric isSafeForSingleLocation = false; 1734*0b57cec5SDimitry Andric } 1735*0b57cec5SDimitry Andric } 1736*0b57cec5SDimitry Andric 1737*0b57cec5SDimitry Andric // Location list entries with empty location descriptions are redundant 1738*0b57cec5SDimitry Andric // information in DWARF, so do not emit those. 1739*0b57cec5SDimitry Andric if (OpenRanges.empty()) 1740*0b57cec5SDimitry Andric continue; 1741*0b57cec5SDimitry Andric 1742*0b57cec5SDimitry Andric // Omit entries with empty ranges as they do not have any effect in DWARF. 1743*0b57cec5SDimitry Andric if (StartLabel == EndLabel) { 1744*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Omitting location list entry with empty range.\n"); 1745*0b57cec5SDimitry Andric continue; 1746*0b57cec5SDimitry Andric } 1747*0b57cec5SDimitry Andric 1748*0b57cec5SDimitry Andric SmallVector<DbgValueLoc, 4> Values; 1749*0b57cec5SDimitry Andric for (auto &R : OpenRanges) 1750*0b57cec5SDimitry Andric Values.push_back(R.second); 1751fe6060f1SDimitry Andric 1752fe6060f1SDimitry Andric // With Basic block sections, it is posssible that the StartLabel and the 1753fe6060f1SDimitry Andric // Instr are not in the same section. This happens when the StartLabel is 1754fe6060f1SDimitry Andric // the function begin label and the dbg value appears in a basic block 1755fe6060f1SDimitry Andric // that is not the entry. In this case, the range needs to be split to 1756fe6060f1SDimitry Andric // span each individual section in the range from StartLabel to EndLabel. 1757fe6060f1SDimitry Andric if (Asm->MF->hasBBSections() && StartLabel == Asm->getFunctionBegin() && 1758fe6060f1SDimitry Andric !Instr->getParent()->sameSection(&Asm->MF->front())) { 1759fe6060f1SDimitry Andric const MCSymbol *BeginSectionLabel = StartLabel; 1760fe6060f1SDimitry Andric 1761fe6060f1SDimitry Andric for (const MachineBasicBlock &MBB : *Asm->MF) { 1762fe6060f1SDimitry Andric if (MBB.isBeginSection() && &MBB != &Asm->MF->front()) 1763fe6060f1SDimitry Andric BeginSectionLabel = MBB.getSymbol(); 1764fe6060f1SDimitry Andric 1765fe6060f1SDimitry Andric if (MBB.sameSection(Instr->getParent())) { 1766fe6060f1SDimitry Andric DebugLoc.emplace_back(BeginSectionLabel, EndLabel, Values); 1767fe6060f1SDimitry Andric break; 1768fe6060f1SDimitry Andric } 1769fe6060f1SDimitry Andric if (MBB.isEndSection()) 1770fe6060f1SDimitry Andric DebugLoc.emplace_back(BeginSectionLabel, MBB.getEndSymbol(), Values); 1771fe6060f1SDimitry Andric } 1772fe6060f1SDimitry Andric } else { 1773*0b57cec5SDimitry Andric DebugLoc.emplace_back(StartLabel, EndLabel, Values); 1774fe6060f1SDimitry Andric } 1775*0b57cec5SDimitry Andric 1776*0b57cec5SDimitry Andric // Attempt to coalesce the ranges of two otherwise identical 1777*0b57cec5SDimitry Andric // DebugLocEntries. 1778*0b57cec5SDimitry Andric auto CurEntry = DebugLoc.rbegin(); 1779*0b57cec5SDimitry Andric LLVM_DEBUG({ 1780*0b57cec5SDimitry Andric dbgs() << CurEntry->getValues().size() << " Values:\n"; 1781*0b57cec5SDimitry Andric for (auto &Value : CurEntry->getValues()) 1782*0b57cec5SDimitry Andric Value.dump(); 1783*0b57cec5SDimitry Andric dbgs() << "-----\n"; 1784*0b57cec5SDimitry Andric }); 1785*0b57cec5SDimitry Andric 1786*0b57cec5SDimitry Andric auto PrevEntry = std::next(CurEntry); 1787*0b57cec5SDimitry Andric if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry)) 1788*0b57cec5SDimitry Andric DebugLoc.pop_back(); 1789*0b57cec5SDimitry Andric } 1790*0b57cec5SDimitry Andric 1791fe6060f1SDimitry Andric if (!isSafeForSingleLocation || 1792fe6060f1SDimitry Andric !validThroughout(LScopes, StartDebugMI, EndMI, getInstOrdering())) 1793fe6060f1SDimitry Andric return false; 1794fe6060f1SDimitry Andric 1795fe6060f1SDimitry Andric if (DebugLoc.size() == 1) 1796fe6060f1SDimitry Andric return true; 1797fe6060f1SDimitry Andric 1798fe6060f1SDimitry Andric if (!Asm->MF->hasBBSections()) 1799fe6060f1SDimitry Andric return false; 1800fe6060f1SDimitry Andric 1801fe6060f1SDimitry Andric // Check here to see if loclist can be merged into a single range. If not, 1802fe6060f1SDimitry Andric // we must keep the split loclists per section. This does exactly what 1803fe6060f1SDimitry Andric // MergeRanges does without sections. We don't actually merge the ranges 1804fe6060f1SDimitry Andric // as the split ranges must be kept intact if this cannot be collapsed 1805fe6060f1SDimitry Andric // into a single range. 1806fe6060f1SDimitry Andric const MachineBasicBlock *RangeMBB = nullptr; 1807fe6060f1SDimitry Andric if (DebugLoc[0].getBeginSym() == Asm->getFunctionBegin()) 1808fe6060f1SDimitry Andric RangeMBB = &Asm->MF->front(); 1809fe6060f1SDimitry Andric else 1810fe6060f1SDimitry Andric RangeMBB = Entries.begin()->getInstr()->getParent(); 1811fe6060f1SDimitry Andric auto *CurEntry = DebugLoc.begin(); 1812fe6060f1SDimitry Andric auto *NextEntry = std::next(CurEntry); 1813fe6060f1SDimitry Andric while (NextEntry != DebugLoc.end()) { 1814fe6060f1SDimitry Andric // Get the last machine basic block of this section. 1815fe6060f1SDimitry Andric while (!RangeMBB->isEndSection()) 1816fe6060f1SDimitry Andric RangeMBB = RangeMBB->getNextNode(); 1817fe6060f1SDimitry Andric if (!RangeMBB->getNextNode()) 1818fe6060f1SDimitry Andric return false; 1819fe6060f1SDimitry Andric // CurEntry should end the current section and NextEntry should start 1820fe6060f1SDimitry Andric // the next section and the Values must match for these two ranges to be 1821fe6060f1SDimitry Andric // merged. 1822fe6060f1SDimitry Andric if (CurEntry->getEndSym() != RangeMBB->getEndSymbol() || 1823fe6060f1SDimitry Andric NextEntry->getBeginSym() != RangeMBB->getNextNode()->getSymbol() || 1824fe6060f1SDimitry Andric CurEntry->getValues() != NextEntry->getValues()) 1825fe6060f1SDimitry Andric return false; 1826fe6060f1SDimitry Andric RangeMBB = RangeMBB->getNextNode(); 1827fe6060f1SDimitry Andric CurEntry = NextEntry; 1828fe6060f1SDimitry Andric NextEntry = std::next(CurEntry); 1829fe6060f1SDimitry Andric } 1830fe6060f1SDimitry Andric return true; 1831*0b57cec5SDimitry Andric } 1832*0b57cec5SDimitry Andric 1833*0b57cec5SDimitry Andric DbgEntity *DwarfDebug::createConcreteEntity(DwarfCompileUnit &TheCU, 1834*0b57cec5SDimitry Andric LexicalScope &Scope, 1835*0b57cec5SDimitry Andric const DINode *Node, 1836*0b57cec5SDimitry Andric const DILocation *Location, 1837*0b57cec5SDimitry Andric const MCSymbol *Sym) { 1838*0b57cec5SDimitry Andric ensureAbstractEntityIsCreatedIfScoped(TheCU, Node, Scope.getScopeNode()); 1839*0b57cec5SDimitry Andric if (isa<const DILocalVariable>(Node)) { 1840*0b57cec5SDimitry Andric ConcreteEntities.push_back( 18418bcb0991SDimitry Andric std::make_unique<DbgVariable>(cast<const DILocalVariable>(Node), 1842*0b57cec5SDimitry Andric Location)); 1843*0b57cec5SDimitry Andric InfoHolder.addScopeVariable(&Scope, 1844*0b57cec5SDimitry Andric cast<DbgVariable>(ConcreteEntities.back().get())); 1845*0b57cec5SDimitry Andric } else if (isa<const DILabel>(Node)) { 1846*0b57cec5SDimitry Andric ConcreteEntities.push_back( 18478bcb0991SDimitry Andric std::make_unique<DbgLabel>(cast<const DILabel>(Node), 1848*0b57cec5SDimitry Andric Location, Sym)); 1849*0b57cec5SDimitry Andric InfoHolder.addScopeLabel(&Scope, 1850*0b57cec5SDimitry Andric cast<DbgLabel>(ConcreteEntities.back().get())); 1851*0b57cec5SDimitry Andric } 1852*0b57cec5SDimitry Andric return ConcreteEntities.back().get(); 1853*0b57cec5SDimitry Andric } 1854*0b57cec5SDimitry Andric 1855*0b57cec5SDimitry Andric // Find variables for each lexical scope. 1856*0b57cec5SDimitry Andric void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU, 1857*0b57cec5SDimitry Andric const DISubprogram *SP, 1858*0b57cec5SDimitry Andric DenseSet<InlinedEntity> &Processed) { 1859*0b57cec5SDimitry Andric // Grab the variable info that was squirreled away in the MMI side-table. 1860*0b57cec5SDimitry Andric collectVariableInfoFromMFTable(TheCU, Processed); 1861*0b57cec5SDimitry Andric 1862*0b57cec5SDimitry Andric for (const auto &I : DbgValues) { 1863*0b57cec5SDimitry Andric InlinedEntity IV = I.first; 1864*0b57cec5SDimitry Andric if (Processed.count(IV)) 1865*0b57cec5SDimitry Andric continue; 1866*0b57cec5SDimitry Andric 1867*0b57cec5SDimitry Andric // Instruction ranges, specifying where IV is accessible. 1868*0b57cec5SDimitry Andric const auto &HistoryMapEntries = I.second; 1869fe6060f1SDimitry Andric 1870fe6060f1SDimitry Andric // Try to find any non-empty variable location. Do not create a concrete 1871fe6060f1SDimitry Andric // entity if there are no locations. 1872fe6060f1SDimitry Andric if (!DbgValues.hasNonEmptyLocation(HistoryMapEntries)) 1873*0b57cec5SDimitry Andric continue; 1874*0b57cec5SDimitry Andric 1875*0b57cec5SDimitry Andric LexicalScope *Scope = nullptr; 1876*0b57cec5SDimitry Andric const DILocalVariable *LocalVar = cast<DILocalVariable>(IV.first); 1877*0b57cec5SDimitry Andric if (const DILocation *IA = IV.second) 1878*0b57cec5SDimitry Andric Scope = LScopes.findInlinedScope(LocalVar->getScope(), IA); 1879*0b57cec5SDimitry Andric else 1880*0b57cec5SDimitry Andric Scope = LScopes.findLexicalScope(LocalVar->getScope()); 1881*0b57cec5SDimitry Andric // If variable scope is not found then skip this variable. 1882*0b57cec5SDimitry Andric if (!Scope) 1883*0b57cec5SDimitry Andric continue; 1884*0b57cec5SDimitry Andric 1885*0b57cec5SDimitry Andric Processed.insert(IV); 1886*0b57cec5SDimitry Andric DbgVariable *RegVar = cast<DbgVariable>(createConcreteEntity(TheCU, 1887*0b57cec5SDimitry Andric *Scope, LocalVar, IV.second)); 1888*0b57cec5SDimitry Andric 1889*0b57cec5SDimitry Andric const MachineInstr *MInsn = HistoryMapEntries.front().getInstr(); 1890*0b57cec5SDimitry Andric assert(MInsn->isDebugValue() && "History must begin with debug value"); 1891*0b57cec5SDimitry Andric 1892*0b57cec5SDimitry Andric // Check if there is a single DBG_VALUE, valid throughout the var's scope. 1893*0b57cec5SDimitry Andric // If the history map contains a single debug value, there may be an 1894*0b57cec5SDimitry Andric // additional entry which clobbers the debug value. 1895*0b57cec5SDimitry Andric size_t HistSize = HistoryMapEntries.size(); 1896*0b57cec5SDimitry Andric bool SingleValueWithClobber = 1897*0b57cec5SDimitry Andric HistSize == 2 && HistoryMapEntries[1].isClobber(); 1898*0b57cec5SDimitry Andric if (HistSize == 1 || SingleValueWithClobber) { 1899*0b57cec5SDimitry Andric const auto *End = 1900*0b57cec5SDimitry Andric SingleValueWithClobber ? HistoryMapEntries[1].getInstr() : nullptr; 1901e8d8bef9SDimitry Andric if (validThroughout(LScopes, MInsn, End, getInstOrdering())) { 1902*0b57cec5SDimitry Andric RegVar->initializeDbgValue(MInsn); 1903*0b57cec5SDimitry Andric continue; 1904*0b57cec5SDimitry Andric } 1905*0b57cec5SDimitry Andric } 1906*0b57cec5SDimitry Andric 1907*0b57cec5SDimitry Andric // Do not emit location lists if .debug_loc secton is disabled. 1908*0b57cec5SDimitry Andric if (!useLocSection()) 1909*0b57cec5SDimitry Andric continue; 1910*0b57cec5SDimitry Andric 1911*0b57cec5SDimitry Andric // Handle multiple DBG_VALUE instructions describing one variable. 1912*0b57cec5SDimitry Andric DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn); 1913*0b57cec5SDimitry Andric 1914*0b57cec5SDimitry Andric // Build the location list for this variable. 1915*0b57cec5SDimitry Andric SmallVector<DebugLocEntry, 8> Entries; 1916e8d8bef9SDimitry Andric bool isValidSingleLocation = buildLocationList(Entries, HistoryMapEntries); 1917*0b57cec5SDimitry Andric 1918*0b57cec5SDimitry Andric // Check whether buildLocationList managed to merge all locations to one 1919*0b57cec5SDimitry Andric // that is valid throughout the variable's scope. If so, produce single 1920*0b57cec5SDimitry Andric // value location. 1921*0b57cec5SDimitry Andric if (isValidSingleLocation) { 1922*0b57cec5SDimitry Andric RegVar->initializeDbgValue(Entries[0].getValues()[0]); 1923*0b57cec5SDimitry Andric continue; 1924*0b57cec5SDimitry Andric } 1925*0b57cec5SDimitry Andric 1926*0b57cec5SDimitry Andric // If the variable has a DIBasicType, extract it. Basic types cannot have 1927*0b57cec5SDimitry Andric // unique identifiers, so don't bother resolving the type with the 1928*0b57cec5SDimitry Andric // identifier map. 1929*0b57cec5SDimitry Andric const DIBasicType *BT = dyn_cast<DIBasicType>( 1930*0b57cec5SDimitry Andric static_cast<const Metadata *>(LocalVar->getType())); 1931*0b57cec5SDimitry Andric 1932*0b57cec5SDimitry Andric // Finalize the entry by lowering it into a DWARF bytestream. 1933*0b57cec5SDimitry Andric for (auto &Entry : Entries) 1934*0b57cec5SDimitry Andric Entry.finalize(*Asm, List, BT, TheCU); 1935*0b57cec5SDimitry Andric } 1936*0b57cec5SDimitry Andric 1937*0b57cec5SDimitry Andric // For each InlinedEntity collected from DBG_LABEL instructions, convert to 1938*0b57cec5SDimitry Andric // DWARF-related DbgLabel. 1939*0b57cec5SDimitry Andric for (const auto &I : DbgLabels) { 1940*0b57cec5SDimitry Andric InlinedEntity IL = I.first; 1941*0b57cec5SDimitry Andric const MachineInstr *MI = I.second; 1942*0b57cec5SDimitry Andric if (MI == nullptr) 1943*0b57cec5SDimitry Andric continue; 1944*0b57cec5SDimitry Andric 1945*0b57cec5SDimitry Andric LexicalScope *Scope = nullptr; 1946*0b57cec5SDimitry Andric const DILabel *Label = cast<DILabel>(IL.first); 19478bcb0991SDimitry Andric // The scope could have an extra lexical block file. 19488bcb0991SDimitry Andric const DILocalScope *LocalScope = 19498bcb0991SDimitry Andric Label->getScope()->getNonLexicalBlockFileScope(); 1950*0b57cec5SDimitry Andric // Get inlined DILocation if it is inlined label. 1951*0b57cec5SDimitry Andric if (const DILocation *IA = IL.second) 19528bcb0991SDimitry Andric Scope = LScopes.findInlinedScope(LocalScope, IA); 1953*0b57cec5SDimitry Andric else 19548bcb0991SDimitry Andric Scope = LScopes.findLexicalScope(LocalScope); 1955*0b57cec5SDimitry Andric // If label scope is not found then skip this label. 1956*0b57cec5SDimitry Andric if (!Scope) 1957*0b57cec5SDimitry Andric continue; 1958*0b57cec5SDimitry Andric 1959*0b57cec5SDimitry Andric Processed.insert(IL); 1960*0b57cec5SDimitry Andric /// At this point, the temporary label is created. 1961*0b57cec5SDimitry Andric /// Save the temporary label to DbgLabel entity to get the 1962*0b57cec5SDimitry Andric /// actually address when generating Dwarf DIE. 1963*0b57cec5SDimitry Andric MCSymbol *Sym = getLabelBeforeInsn(MI); 1964*0b57cec5SDimitry Andric createConcreteEntity(TheCU, *Scope, Label, IL.second, Sym); 1965*0b57cec5SDimitry Andric } 1966*0b57cec5SDimitry Andric 1967*0b57cec5SDimitry Andric // Collect info for variables/labels that were optimized out. 1968*0b57cec5SDimitry Andric for (const DINode *DN : SP->getRetainedNodes()) { 1969*0b57cec5SDimitry Andric if (!Processed.insert(InlinedEntity(DN, nullptr)).second) 1970*0b57cec5SDimitry Andric continue; 1971*0b57cec5SDimitry Andric LexicalScope *Scope = nullptr; 1972*0b57cec5SDimitry Andric if (auto *DV = dyn_cast<DILocalVariable>(DN)) { 1973*0b57cec5SDimitry Andric Scope = LScopes.findLexicalScope(DV->getScope()); 1974*0b57cec5SDimitry Andric } else if (auto *DL = dyn_cast<DILabel>(DN)) { 1975*0b57cec5SDimitry Andric Scope = LScopes.findLexicalScope(DL->getScope()); 1976*0b57cec5SDimitry Andric } 1977*0b57cec5SDimitry Andric 1978*0b57cec5SDimitry Andric if (Scope) 1979*0b57cec5SDimitry Andric createConcreteEntity(TheCU, *Scope, DN, nullptr); 1980*0b57cec5SDimitry Andric } 1981*0b57cec5SDimitry Andric } 1982*0b57cec5SDimitry Andric 1983*0b57cec5SDimitry Andric // Process beginning of an instruction. 1984*0b57cec5SDimitry Andric void DwarfDebug::beginInstruction(const MachineInstr *MI) { 19855ffd83dbSDimitry Andric const MachineFunction &MF = *MI->getMF(); 19865ffd83dbSDimitry Andric const auto *SP = MF.getFunction().getSubprogram(); 19875ffd83dbSDimitry Andric bool NoDebug = 19885ffd83dbSDimitry Andric !SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug; 19895ffd83dbSDimitry Andric 19905ffd83dbSDimitry Andric // Delay slot support check. 19915ffd83dbSDimitry Andric auto delaySlotSupported = [](const MachineInstr &MI) { 19925ffd83dbSDimitry Andric if (!MI.isBundledWithSucc()) 19935ffd83dbSDimitry Andric return false; 19945ffd83dbSDimitry Andric auto Suc = std::next(MI.getIterator()); 19955ffd83dbSDimitry Andric (void)Suc; 19965ffd83dbSDimitry Andric // Ensure that delay slot instruction is successor of the call instruction. 19975ffd83dbSDimitry Andric // Ex. CALL_INSTRUCTION { 19985ffd83dbSDimitry Andric // DELAY_SLOT_INSTRUCTION } 19995ffd83dbSDimitry Andric assert(Suc->isBundledWithPred() && 20005ffd83dbSDimitry Andric "Call bundle instructions are out of order"); 20015ffd83dbSDimitry Andric return true; 20025ffd83dbSDimitry Andric }; 20035ffd83dbSDimitry Andric 20045ffd83dbSDimitry Andric // When describing calls, we need a label for the call instruction. 20055ffd83dbSDimitry Andric if (!NoDebug && SP->areAllCallsDescribed() && 20065ffd83dbSDimitry Andric MI->isCandidateForCallSiteEntry(MachineInstr::AnyInBundle) && 20075ffd83dbSDimitry Andric (!MI->hasDelaySlot() || delaySlotSupported(*MI))) { 20085ffd83dbSDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 20095ffd83dbSDimitry Andric bool IsTail = TII->isTailCall(*MI); 20105ffd83dbSDimitry Andric // For tail calls, we need the address of the branch instruction for 20115ffd83dbSDimitry Andric // DW_AT_call_pc. 20125ffd83dbSDimitry Andric if (IsTail) 20135ffd83dbSDimitry Andric requestLabelBeforeInsn(MI); 20145ffd83dbSDimitry Andric // For non-tail calls, we need the return address for the call for 20155ffd83dbSDimitry Andric // DW_AT_call_return_pc. Under GDB tuning, this information is needed for 20165ffd83dbSDimitry Andric // tail calls as well. 20175ffd83dbSDimitry Andric requestLabelAfterInsn(MI); 20185ffd83dbSDimitry Andric } 20195ffd83dbSDimitry Andric 2020*0b57cec5SDimitry Andric DebugHandlerBase::beginInstruction(MI); 2021e8d8bef9SDimitry Andric if (!CurMI) 2022e8d8bef9SDimitry Andric return; 2023*0b57cec5SDimitry Andric 20245ffd83dbSDimitry Andric if (NoDebug) 2025*0b57cec5SDimitry Andric return; 2026*0b57cec5SDimitry Andric 2027*0b57cec5SDimitry Andric // Check if source location changes, but ignore DBG_VALUE and CFI locations. 2028*0b57cec5SDimitry Andric // If the instruction is part of the function frame setup code, do not emit 2029*0b57cec5SDimitry Andric // any line record, as there is no correspondence with any user code. 2030*0b57cec5SDimitry Andric if (MI->isMetaInstruction() || MI->getFlag(MachineInstr::FrameSetup)) 2031*0b57cec5SDimitry Andric return; 2032*0b57cec5SDimitry Andric const DebugLoc &DL = MI->getDebugLoc(); 2033bdd1243dSDimitry Andric unsigned Flags = 0; 2034bdd1243dSDimitry Andric 2035bdd1243dSDimitry Andric if (MI->getFlag(MachineInstr::FrameDestroy) && DL) { 2036bdd1243dSDimitry Andric const MachineBasicBlock *MBB = MI->getParent(); 2037bdd1243dSDimitry Andric if (MBB && (MBB != EpilogBeginBlock)) { 2038bdd1243dSDimitry Andric // First time FrameDestroy has been seen in this basic block 2039bdd1243dSDimitry Andric EpilogBeginBlock = MBB; 2040bdd1243dSDimitry Andric Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; 2041bdd1243dSDimitry Andric } 2042bdd1243dSDimitry Andric } 2043bdd1243dSDimitry Andric 2044*0b57cec5SDimitry Andric // When we emit a line-0 record, we don't update PrevInstLoc; so look at 2045*0b57cec5SDimitry Andric // the last line number actually emitted, to see if it was line 0. 2046*0b57cec5SDimitry Andric unsigned LastAsmLine = 2047*0b57cec5SDimitry Andric Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine(); 2048*0b57cec5SDimitry Andric 2049*0b57cec5SDimitry Andric if (DL == PrevInstLoc) { 2050*0b57cec5SDimitry Andric // If we have an ongoing unspecified location, nothing to do here. 2051*0b57cec5SDimitry Andric if (!DL) 2052*0b57cec5SDimitry Andric return; 2053*0b57cec5SDimitry Andric // We have an explicit location, same as the previous location. 2054*0b57cec5SDimitry Andric // But we might be coming back to it after a line 0 record. 2055bdd1243dSDimitry Andric if ((LastAsmLine == 0 && DL.getLine() != 0) || Flags) { 2056*0b57cec5SDimitry Andric // Reinstate the source location but not marked as a statement. 2057*0b57cec5SDimitry Andric const MDNode *Scope = DL.getScope(); 2058bdd1243dSDimitry Andric recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags); 2059*0b57cec5SDimitry Andric } 2060*0b57cec5SDimitry Andric return; 2061*0b57cec5SDimitry Andric } 2062*0b57cec5SDimitry Andric 2063*0b57cec5SDimitry Andric if (!DL) { 2064*0b57cec5SDimitry Andric // We have an unspecified location, which might want to be line 0. 2065*0b57cec5SDimitry Andric // If we have already emitted a line-0 record, don't repeat it. 2066*0b57cec5SDimitry Andric if (LastAsmLine == 0) 2067*0b57cec5SDimitry Andric return; 2068*0b57cec5SDimitry Andric // If user said Don't Do That, don't do that. 2069*0b57cec5SDimitry Andric if (UnknownLocations == Disable) 2070*0b57cec5SDimitry Andric return; 2071*0b57cec5SDimitry Andric // See if we have a reason to emit a line-0 record now. 2072*0b57cec5SDimitry Andric // Reasons to emit a line-0 record include: 2073*0b57cec5SDimitry Andric // - User asked for it (UnknownLocations). 2074*0b57cec5SDimitry Andric // - Instruction has a label, so it's referenced from somewhere else, 2075*0b57cec5SDimitry Andric // possibly debug information; we want it to have a source location. 2076*0b57cec5SDimitry Andric // - Instruction is at the top of a block; we don't want to inherit the 2077*0b57cec5SDimitry Andric // location from the physically previous (maybe unrelated) block. 2078*0b57cec5SDimitry Andric if (UnknownLocations == Enable || PrevLabel || 2079*0b57cec5SDimitry Andric (PrevInstBB && PrevInstBB != MI->getParent())) { 2080*0b57cec5SDimitry Andric // Preserve the file and column numbers, if we can, to save space in 2081*0b57cec5SDimitry Andric // the encoded line table. 2082*0b57cec5SDimitry Andric // Do not update PrevInstLoc, it remembers the last non-0 line. 2083*0b57cec5SDimitry Andric const MDNode *Scope = nullptr; 2084*0b57cec5SDimitry Andric unsigned Column = 0; 2085*0b57cec5SDimitry Andric if (PrevInstLoc) { 2086*0b57cec5SDimitry Andric Scope = PrevInstLoc.getScope(); 2087*0b57cec5SDimitry Andric Column = PrevInstLoc.getCol(); 2088*0b57cec5SDimitry Andric } 2089*0b57cec5SDimitry Andric recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0); 2090*0b57cec5SDimitry Andric } 2091*0b57cec5SDimitry Andric return; 2092*0b57cec5SDimitry Andric } 2093*0b57cec5SDimitry Andric 2094*0b57cec5SDimitry Andric // We have an explicit location, different from the previous location. 2095*0b57cec5SDimitry Andric // Don't repeat a line-0 record, but otherwise emit the new location. 2096*0b57cec5SDimitry Andric // (The new location might be an explicit line 0, which we do emit.) 2097*0b57cec5SDimitry Andric if (DL.getLine() == 0 && LastAsmLine == 0) 2098*0b57cec5SDimitry Andric return; 2099*0b57cec5SDimitry Andric if (DL == PrologEndLoc) { 2100*0b57cec5SDimitry Andric Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT; 2101*0b57cec5SDimitry Andric PrologEndLoc = DebugLoc(); 2102*0b57cec5SDimitry Andric } 2103*0b57cec5SDimitry Andric // If the line changed, we call that a new statement; unless we went to 2104*0b57cec5SDimitry Andric // line 0 and came back, in which case it is not a new statement. 2105*0b57cec5SDimitry Andric unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine; 2106*0b57cec5SDimitry Andric if (DL.getLine() && DL.getLine() != OldLine) 2107*0b57cec5SDimitry Andric Flags |= DWARF2_FLAG_IS_STMT; 2108*0b57cec5SDimitry Andric 2109*0b57cec5SDimitry Andric const MDNode *Scope = DL.getScope(); 2110*0b57cec5SDimitry Andric recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags); 2111*0b57cec5SDimitry Andric 2112*0b57cec5SDimitry Andric // If we're not at line 0, remember this location. 2113*0b57cec5SDimitry Andric if (DL.getLine()) 2114*0b57cec5SDimitry Andric PrevInstLoc = DL; 2115*0b57cec5SDimitry Andric } 2116*0b57cec5SDimitry Andric 2117*0b57cec5SDimitry Andric static DebugLoc findPrologueEndLoc(const MachineFunction *MF) { 2118*0b57cec5SDimitry Andric // First known non-DBG_VALUE and non-frame setup location marks 2119*0b57cec5SDimitry Andric // the beginning of the function body. 2120349cc55cSDimitry Andric DebugLoc LineZeroLoc; 2121349cc55cSDimitry Andric for (const auto &MBB : *MF) { 2122349cc55cSDimitry Andric for (const auto &MI : MBB) { 2123*0b57cec5SDimitry Andric if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) && 2124349cc55cSDimitry Andric MI.getDebugLoc()) { 2125349cc55cSDimitry Andric // Scan forward to try to find a non-zero line number. The prologue_end 2126349cc55cSDimitry Andric // marks the first breakpoint in the function after the frame setup, and 2127349cc55cSDimitry Andric // a compiler-generated line 0 location is not a meaningful breakpoint. 2128349cc55cSDimitry Andric // If none is found, return the first location after the frame setup. 2129349cc55cSDimitry Andric if (MI.getDebugLoc().getLine()) 2130*0b57cec5SDimitry Andric return MI.getDebugLoc(); 2131349cc55cSDimitry Andric LineZeroLoc = MI.getDebugLoc(); 2132349cc55cSDimitry Andric } 2133349cc55cSDimitry Andric } 2134349cc55cSDimitry Andric } 2135349cc55cSDimitry Andric return LineZeroLoc; 2136*0b57cec5SDimitry Andric } 2137*0b57cec5SDimitry Andric 2138*0b57cec5SDimitry Andric /// Register a source line with debug info. Returns the unique label that was 2139*0b57cec5SDimitry Andric /// emitted and which provides correspondence to the source line list. 2140*0b57cec5SDimitry Andric static void recordSourceLine(AsmPrinter &Asm, unsigned Line, unsigned Col, 2141*0b57cec5SDimitry Andric const MDNode *S, unsigned Flags, unsigned CUID, 2142*0b57cec5SDimitry Andric uint16_t DwarfVersion, 2143*0b57cec5SDimitry Andric ArrayRef<std::unique_ptr<DwarfCompileUnit>> DCUs) { 2144*0b57cec5SDimitry Andric StringRef Fn; 2145*0b57cec5SDimitry Andric unsigned FileNo = 1; 2146*0b57cec5SDimitry Andric unsigned Discriminator = 0; 2147*0b57cec5SDimitry Andric if (auto *Scope = cast_or_null<DIScope>(S)) { 2148*0b57cec5SDimitry Andric Fn = Scope->getFilename(); 2149*0b57cec5SDimitry Andric if (Line != 0 && DwarfVersion >= 4) 2150*0b57cec5SDimitry Andric if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope)) 2151*0b57cec5SDimitry Andric Discriminator = LBF->getDiscriminator(); 2152*0b57cec5SDimitry Andric 2153*0b57cec5SDimitry Andric FileNo = static_cast<DwarfCompileUnit &>(*DCUs[CUID]) 2154*0b57cec5SDimitry Andric .getOrCreateSourceID(Scope->getFile()); 2155*0b57cec5SDimitry Andric } 21565ffd83dbSDimitry Andric Asm.OutStreamer->emitDwarfLocDirective(FileNo, Line, Col, Flags, 0, 2157*0b57cec5SDimitry Andric Discriminator, Fn); 2158*0b57cec5SDimitry Andric } 2159*0b57cec5SDimitry Andric 2160*0b57cec5SDimitry Andric DebugLoc DwarfDebug::emitInitialLocDirective(const MachineFunction &MF, 2161*0b57cec5SDimitry Andric unsigned CUID) { 2162*0b57cec5SDimitry Andric // Get beginning of function. 2163*0b57cec5SDimitry Andric if (DebugLoc PrologEndLoc = findPrologueEndLoc(&MF)) { 2164*0b57cec5SDimitry Andric // Ensure the compile unit is created if the function is called before 2165*0b57cec5SDimitry Andric // beginFunction(). 2166*0b57cec5SDimitry Andric (void)getOrCreateDwarfCompileUnit( 2167*0b57cec5SDimitry Andric MF.getFunction().getSubprogram()->getUnit()); 2168*0b57cec5SDimitry Andric // We'd like to list the prologue as "not statements" but GDB behaves 2169*0b57cec5SDimitry Andric // poorly if we do that. Revisit this with caution/GDB (7.5+) testing. 2170*0b57cec5SDimitry Andric const DISubprogram *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram(); 2171*0b57cec5SDimitry Andric ::recordSourceLine(*Asm, SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT, 2172*0b57cec5SDimitry Andric CUID, getDwarfVersion(), getUnits()); 2173*0b57cec5SDimitry Andric return PrologEndLoc; 2174*0b57cec5SDimitry Andric } 2175*0b57cec5SDimitry Andric return DebugLoc(); 2176*0b57cec5SDimitry Andric } 2177*0b57cec5SDimitry Andric 2178*0b57cec5SDimitry Andric // Gather pre-function debug information. Assumes being called immediately 2179*0b57cec5SDimitry Andric // after the function entry point has been emitted. 2180*0b57cec5SDimitry Andric void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) { 2181*0b57cec5SDimitry Andric CurFn = MF; 2182*0b57cec5SDimitry Andric 2183*0b57cec5SDimitry Andric auto *SP = MF->getFunction().getSubprogram(); 2184*0b57cec5SDimitry Andric assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode()); 2185*0b57cec5SDimitry Andric if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) 2186*0b57cec5SDimitry Andric return; 2187*0b57cec5SDimitry Andric 2188*0b57cec5SDimitry Andric DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); 2189*0b57cec5SDimitry Andric 2190349cc55cSDimitry Andric Asm->OutStreamer->getContext().setDwarfCompileUnitID( 2191349cc55cSDimitry Andric getDwarfCompileUnitIDForLineTable(CU)); 2192*0b57cec5SDimitry Andric 2193*0b57cec5SDimitry Andric // Record beginning of function. 2194*0b57cec5SDimitry Andric PrologEndLoc = emitInitialLocDirective( 2195*0b57cec5SDimitry Andric *MF, Asm->OutStreamer->getContext().getDwarfCompileUnitID()); 2196*0b57cec5SDimitry Andric } 2197*0b57cec5SDimitry Andric 2198349cc55cSDimitry Andric unsigned 2199349cc55cSDimitry Andric DwarfDebug::getDwarfCompileUnitIDForLineTable(const DwarfCompileUnit &CU) { 2200349cc55cSDimitry Andric // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function 2201349cc55cSDimitry Andric // belongs to so that we add to the correct per-cu line table in the 2202349cc55cSDimitry Andric // non-asm case. 2203349cc55cSDimitry Andric if (Asm->OutStreamer->hasRawTextSupport()) 2204349cc55cSDimitry Andric // Use a single line table if we are generating assembly. 2205349cc55cSDimitry Andric return 0; 2206349cc55cSDimitry Andric else 2207349cc55cSDimitry Andric return CU.getUniqueID(); 2208349cc55cSDimitry Andric } 2209349cc55cSDimitry Andric 2210349cc55cSDimitry Andric void DwarfDebug::terminateLineTable(const DwarfCompileUnit *CU) { 2211349cc55cSDimitry Andric const auto &CURanges = CU->getRanges(); 2212349cc55cSDimitry Andric auto &LineTable = Asm->OutStreamer->getContext().getMCDwarfLineTable( 2213349cc55cSDimitry Andric getDwarfCompileUnitIDForLineTable(*CU)); 2214349cc55cSDimitry Andric // Add the last range label for the given CU. 2215349cc55cSDimitry Andric LineTable.getMCLineSections().addEndEntry( 2216349cc55cSDimitry Andric const_cast<MCSymbol *>(CURanges.back().End)); 2217349cc55cSDimitry Andric } 2218349cc55cSDimitry Andric 2219*0b57cec5SDimitry Andric void DwarfDebug::skippedNonDebugFunction() { 2220*0b57cec5SDimitry Andric // If we don't have a subprogram for this function then there will be a hole 2221*0b57cec5SDimitry Andric // in the range information. Keep note of this by setting the previously used 2222*0b57cec5SDimitry Andric // section to nullptr. 2223349cc55cSDimitry Andric // Terminate the pending line table. 2224349cc55cSDimitry Andric if (PrevCU) 2225349cc55cSDimitry Andric terminateLineTable(PrevCU); 2226*0b57cec5SDimitry Andric PrevCU = nullptr; 2227*0b57cec5SDimitry Andric CurFn = nullptr; 2228*0b57cec5SDimitry Andric } 2229*0b57cec5SDimitry Andric 2230*0b57cec5SDimitry Andric // Gather and emit post-function debug information. 2231*0b57cec5SDimitry Andric void DwarfDebug::endFunctionImpl(const MachineFunction *MF) { 2232*0b57cec5SDimitry Andric const DISubprogram *SP = MF->getFunction().getSubprogram(); 2233*0b57cec5SDimitry Andric 2234*0b57cec5SDimitry Andric assert(CurFn == MF && 2235*0b57cec5SDimitry Andric "endFunction should be called with the same function as beginFunction"); 2236*0b57cec5SDimitry Andric 2237*0b57cec5SDimitry Andric // Set DwarfDwarfCompileUnitID in MCContext to default value. 2238*0b57cec5SDimitry Andric Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); 2239*0b57cec5SDimitry Andric 2240*0b57cec5SDimitry Andric LexicalScope *FnScope = LScopes.getCurrentFunctionScope(); 2241*0b57cec5SDimitry Andric assert(!FnScope || SP == FnScope->getScopeNode()); 2242*0b57cec5SDimitry Andric DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit()); 2243*0b57cec5SDimitry Andric if (TheCU.getCUNode()->isDebugDirectivesOnly()) { 2244*0b57cec5SDimitry Andric PrevLabel = nullptr; 2245*0b57cec5SDimitry Andric CurFn = nullptr; 2246*0b57cec5SDimitry Andric return; 2247*0b57cec5SDimitry Andric } 2248*0b57cec5SDimitry Andric 2249*0b57cec5SDimitry Andric DenseSet<InlinedEntity> Processed; 2250*0b57cec5SDimitry Andric collectEntityInfo(TheCU, SP, Processed); 2251*0b57cec5SDimitry Andric 2252*0b57cec5SDimitry Andric // Add the range of this function to the list of ranges for the CU. 22535ffd83dbSDimitry Andric // With basic block sections, add ranges for all basic block sections. 22545ffd83dbSDimitry Andric for (const auto &R : Asm->MBBSectionRanges) 22555ffd83dbSDimitry Andric TheCU.addRange({R.second.BeginLabel, R.second.EndLabel}); 2256*0b57cec5SDimitry Andric 2257*0b57cec5SDimitry Andric // Under -gmlt, skip building the subprogram if there are no inlined 2258*0b57cec5SDimitry Andric // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram 2259*0b57cec5SDimitry Andric // is still needed as we need its source location. 2260*0b57cec5SDimitry Andric if (!TheCU.getCUNode()->getDebugInfoForProfiling() && 2261*0b57cec5SDimitry Andric TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly && 2262*0b57cec5SDimitry Andric LScopes.getAbstractScopesList().empty() && !IsDarwin) { 2263*0b57cec5SDimitry Andric assert(InfoHolder.getScopeVariables().empty()); 2264*0b57cec5SDimitry Andric PrevLabel = nullptr; 2265*0b57cec5SDimitry Andric CurFn = nullptr; 2266*0b57cec5SDimitry Andric return; 2267*0b57cec5SDimitry Andric } 2268*0b57cec5SDimitry Andric 2269*0b57cec5SDimitry Andric #ifndef NDEBUG 2270*0b57cec5SDimitry Andric size_t NumAbstractScopes = LScopes.getAbstractScopesList().size(); 2271*0b57cec5SDimitry Andric #endif 2272*0b57cec5SDimitry Andric // Construct abstract scopes. 2273*0b57cec5SDimitry Andric for (LexicalScope *AScope : LScopes.getAbstractScopesList()) { 2274fcaf7f86SDimitry Andric const auto *SP = cast<DISubprogram>(AScope->getScopeNode()); 2275*0b57cec5SDimitry Andric for (const DINode *DN : SP->getRetainedNodes()) { 2276*0b57cec5SDimitry Andric if (!Processed.insert(InlinedEntity(DN, nullptr)).second) 2277*0b57cec5SDimitry Andric continue; 2278*0b57cec5SDimitry Andric 2279*0b57cec5SDimitry Andric const MDNode *Scope = nullptr; 2280*0b57cec5SDimitry Andric if (auto *DV = dyn_cast<DILocalVariable>(DN)) 2281*0b57cec5SDimitry Andric Scope = DV->getScope(); 2282*0b57cec5SDimitry Andric else if (auto *DL = dyn_cast<DILabel>(DN)) 2283*0b57cec5SDimitry Andric Scope = DL->getScope(); 2284*0b57cec5SDimitry Andric else 2285*0b57cec5SDimitry Andric llvm_unreachable("Unexpected DI type!"); 2286*0b57cec5SDimitry Andric 2287*0b57cec5SDimitry Andric // Collect info for variables/labels that were optimized out. 2288*0b57cec5SDimitry Andric ensureAbstractEntityIsCreated(TheCU, DN, Scope); 2289*0b57cec5SDimitry Andric assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes 2290*0b57cec5SDimitry Andric && "ensureAbstractEntityIsCreated inserted abstract scopes"); 2291*0b57cec5SDimitry Andric } 2292*0b57cec5SDimitry Andric constructAbstractSubprogramScopeDIE(TheCU, AScope); 2293*0b57cec5SDimitry Andric } 2294*0b57cec5SDimitry Andric 2295*0b57cec5SDimitry Andric ProcessedSPNodes.insert(SP); 2296*0b57cec5SDimitry Andric DIE &ScopeDIE = TheCU.constructSubprogramScopeDIE(SP, FnScope); 2297*0b57cec5SDimitry Andric if (auto *SkelCU = TheCU.getSkeleton()) 2298*0b57cec5SDimitry Andric if (!LScopes.getAbstractScopesList().empty() && 2299*0b57cec5SDimitry Andric TheCU.getCUNode()->getSplitDebugInlining()) 2300*0b57cec5SDimitry Andric SkelCU->constructSubprogramScopeDIE(SP, FnScope); 2301*0b57cec5SDimitry Andric 2302*0b57cec5SDimitry Andric // Construct call site entries. 2303*0b57cec5SDimitry Andric constructCallSiteEntryDIEs(*SP, TheCU, ScopeDIE, *MF); 2304*0b57cec5SDimitry Andric 2305*0b57cec5SDimitry Andric // Clear debug info 2306*0b57cec5SDimitry Andric // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the 2307*0b57cec5SDimitry Andric // DbgVariables except those that are also in AbstractVariables (since they 2308*0b57cec5SDimitry Andric // can be used cross-function) 2309*0b57cec5SDimitry Andric InfoHolder.getScopeVariables().clear(); 2310*0b57cec5SDimitry Andric InfoHolder.getScopeLabels().clear(); 2311*0b57cec5SDimitry Andric PrevLabel = nullptr; 2312*0b57cec5SDimitry Andric CurFn = nullptr; 2313*0b57cec5SDimitry Andric } 2314*0b57cec5SDimitry Andric 2315*0b57cec5SDimitry Andric // Register a source line with debug info. Returns the unique label that was 2316*0b57cec5SDimitry Andric // emitted and which provides correspondence to the source line list. 2317*0b57cec5SDimitry Andric void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S, 2318*0b57cec5SDimitry Andric unsigned Flags) { 2319*0b57cec5SDimitry Andric ::recordSourceLine(*Asm, Line, Col, S, Flags, 2320*0b57cec5SDimitry Andric Asm->OutStreamer->getContext().getDwarfCompileUnitID(), 2321*0b57cec5SDimitry Andric getDwarfVersion(), getUnits()); 2322*0b57cec5SDimitry Andric } 2323*0b57cec5SDimitry Andric 2324*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 2325*0b57cec5SDimitry Andric // Emit Methods 2326*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 2327*0b57cec5SDimitry Andric 2328*0b57cec5SDimitry Andric // Emit the debug info section. 2329*0b57cec5SDimitry Andric void DwarfDebug::emitDebugInfo() { 2330*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 2331*0b57cec5SDimitry Andric Holder.emitUnits(/* UseOffsets */ false); 2332*0b57cec5SDimitry Andric } 2333*0b57cec5SDimitry Andric 2334*0b57cec5SDimitry Andric // Emit the abbreviation section. 2335*0b57cec5SDimitry Andric void DwarfDebug::emitAbbreviations() { 2336*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 2337*0b57cec5SDimitry Andric 2338*0b57cec5SDimitry Andric Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection()); 2339*0b57cec5SDimitry Andric } 2340*0b57cec5SDimitry Andric 2341*0b57cec5SDimitry Andric void DwarfDebug::emitStringOffsetsTableHeader() { 2342*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 2343*0b57cec5SDimitry Andric Holder.getStringPool().emitStringOffsetsTableHeader( 2344*0b57cec5SDimitry Andric *Asm, Asm->getObjFileLowering().getDwarfStrOffSection(), 2345*0b57cec5SDimitry Andric Holder.getStringOffsetsStartSym()); 2346*0b57cec5SDimitry Andric } 2347*0b57cec5SDimitry Andric 2348*0b57cec5SDimitry Andric template <typename AccelTableT> 2349*0b57cec5SDimitry Andric void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section, 2350*0b57cec5SDimitry Andric StringRef TableName) { 235181ad6265SDimitry Andric Asm->OutStreamer->switchSection(Section); 2352*0b57cec5SDimitry Andric 2353*0b57cec5SDimitry Andric // Emit the full data. 2354*0b57cec5SDimitry Andric emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol()); 2355*0b57cec5SDimitry Andric } 2356*0b57cec5SDimitry Andric 2357*0b57cec5SDimitry Andric void DwarfDebug::emitAccelDebugNames() { 2358*0b57cec5SDimitry Andric // Don't emit anything if we have no compilation units to index. 2359*0b57cec5SDimitry Andric if (getUnits().empty()) 2360*0b57cec5SDimitry Andric return; 2361*0b57cec5SDimitry Andric 2362*0b57cec5SDimitry Andric emitDWARF5AccelTable(Asm, AccelDebugNames, *this, getUnits()); 2363*0b57cec5SDimitry Andric } 2364*0b57cec5SDimitry Andric 2365*0b57cec5SDimitry Andric // Emit visible names into a hashed accelerator table section. 2366*0b57cec5SDimitry Andric void DwarfDebug::emitAccelNames() { 2367*0b57cec5SDimitry Andric emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(), 2368*0b57cec5SDimitry Andric "Names"); 2369*0b57cec5SDimitry Andric } 2370*0b57cec5SDimitry Andric 2371*0b57cec5SDimitry Andric // Emit objective C classes and categories into a hashed accelerator table 2372*0b57cec5SDimitry Andric // section. 2373*0b57cec5SDimitry Andric void DwarfDebug::emitAccelObjC() { 2374*0b57cec5SDimitry Andric emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(), 2375*0b57cec5SDimitry Andric "ObjC"); 2376*0b57cec5SDimitry Andric } 2377*0b57cec5SDimitry Andric 2378*0b57cec5SDimitry Andric // Emit namespace dies into a hashed accelerator table. 2379*0b57cec5SDimitry Andric void DwarfDebug::emitAccelNamespaces() { 2380*0b57cec5SDimitry Andric emitAccel(AccelNamespace, 2381*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfAccelNamespaceSection(), 2382*0b57cec5SDimitry Andric "namespac"); 2383*0b57cec5SDimitry Andric } 2384*0b57cec5SDimitry Andric 2385*0b57cec5SDimitry Andric // Emit type dies into a hashed accelerator table. 2386*0b57cec5SDimitry Andric void DwarfDebug::emitAccelTypes() { 2387*0b57cec5SDimitry Andric emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(), 2388*0b57cec5SDimitry Andric "types"); 2389*0b57cec5SDimitry Andric } 2390*0b57cec5SDimitry Andric 2391*0b57cec5SDimitry Andric // Public name handling. 2392*0b57cec5SDimitry Andric // The format for the various pubnames: 2393*0b57cec5SDimitry Andric // 2394*0b57cec5SDimitry Andric // dwarf pubnames - offset/name pairs where the offset is the offset into the CU 2395*0b57cec5SDimitry Andric // for the DIE that is named. 2396*0b57cec5SDimitry Andric // 2397*0b57cec5SDimitry Andric // gnu pubnames - offset/index value/name tuples where the offset is the offset 2398*0b57cec5SDimitry Andric // into the CU and the index value is computed according to the type of value 2399*0b57cec5SDimitry Andric // for the DIE that is named. 2400*0b57cec5SDimitry Andric // 2401*0b57cec5SDimitry Andric // For type units the offset is the offset of the skeleton DIE. For split dwarf 2402*0b57cec5SDimitry Andric // it's the offset within the debug_info/debug_types dwo section, however, the 2403*0b57cec5SDimitry Andric // reference in the pubname header doesn't change. 2404*0b57cec5SDimitry Andric 2405*0b57cec5SDimitry Andric /// computeIndexValue - Compute the gdb index value for the DIE and CU. 2406*0b57cec5SDimitry Andric static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU, 2407*0b57cec5SDimitry Andric const DIE *Die) { 2408*0b57cec5SDimitry Andric // Entities that ended up only in a Type Unit reference the CU instead (since 2409*0b57cec5SDimitry Andric // the pub entry has offsets within the CU there's no real offset that can be 2410*0b57cec5SDimitry Andric // provided anyway). As it happens all such entities (namespaces and types, 2411*0b57cec5SDimitry Andric // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out 2412*0b57cec5SDimitry Andric // not to be true it would be necessary to persist this information from the 2413*0b57cec5SDimitry Andric // point at which the entry is added to the index data structure - since by 2414*0b57cec5SDimitry Andric // the time the index is built from that, the original type/namespace DIE in a 2415*0b57cec5SDimitry Andric // type unit has already been destroyed so it can't be queried for properties 2416*0b57cec5SDimitry Andric // like tag, etc. 2417*0b57cec5SDimitry Andric if (Die->getTag() == dwarf::DW_TAG_compile_unit) 2418*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, 2419*0b57cec5SDimitry Andric dwarf::GIEL_EXTERNAL); 2420*0b57cec5SDimitry Andric dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC; 2421*0b57cec5SDimitry Andric 2422*0b57cec5SDimitry Andric // We could have a specification DIE that has our most of our knowledge, 2423*0b57cec5SDimitry Andric // look for that now. 2424*0b57cec5SDimitry Andric if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) { 2425*0b57cec5SDimitry Andric DIE &SpecDIE = SpecVal.getDIEEntry().getEntry(); 2426*0b57cec5SDimitry Andric if (SpecDIE.findAttribute(dwarf::DW_AT_external)) 2427*0b57cec5SDimitry Andric Linkage = dwarf::GIEL_EXTERNAL; 2428*0b57cec5SDimitry Andric } else if (Die->findAttribute(dwarf::DW_AT_external)) 2429*0b57cec5SDimitry Andric Linkage = dwarf::GIEL_EXTERNAL; 2430*0b57cec5SDimitry Andric 2431*0b57cec5SDimitry Andric switch (Die->getTag()) { 2432*0b57cec5SDimitry Andric case dwarf::DW_TAG_class_type: 2433*0b57cec5SDimitry Andric case dwarf::DW_TAG_structure_type: 2434*0b57cec5SDimitry Andric case dwarf::DW_TAG_union_type: 2435*0b57cec5SDimitry Andric case dwarf::DW_TAG_enumeration_type: 2436*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor( 24378bcb0991SDimitry Andric dwarf::GIEK_TYPE, 24388bcb0991SDimitry Andric dwarf::isCPlusPlus((dwarf::SourceLanguage)CU->getLanguage()) 24398bcb0991SDimitry Andric ? dwarf::GIEL_EXTERNAL 24408bcb0991SDimitry Andric : dwarf::GIEL_STATIC); 2441*0b57cec5SDimitry Andric case dwarf::DW_TAG_typedef: 2442*0b57cec5SDimitry Andric case dwarf::DW_TAG_base_type: 2443*0b57cec5SDimitry Andric case dwarf::DW_TAG_subrange_type: 2444*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC); 2445*0b57cec5SDimitry Andric case dwarf::DW_TAG_namespace: 2446*0b57cec5SDimitry Andric return dwarf::GIEK_TYPE; 2447*0b57cec5SDimitry Andric case dwarf::DW_TAG_subprogram: 2448*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage); 2449*0b57cec5SDimitry Andric case dwarf::DW_TAG_variable: 2450*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage); 2451*0b57cec5SDimitry Andric case dwarf::DW_TAG_enumerator: 2452*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, 2453*0b57cec5SDimitry Andric dwarf::GIEL_STATIC); 2454*0b57cec5SDimitry Andric default: 2455*0b57cec5SDimitry Andric return dwarf::GIEK_NONE; 2456*0b57cec5SDimitry Andric } 2457*0b57cec5SDimitry Andric } 2458*0b57cec5SDimitry Andric 2459*0b57cec5SDimitry Andric /// emitDebugPubSections - Emit visible names and types into debug pubnames and 2460*0b57cec5SDimitry Andric /// pubtypes sections. 2461*0b57cec5SDimitry Andric void DwarfDebug::emitDebugPubSections() { 2462*0b57cec5SDimitry Andric for (const auto &NU : CUMap) { 2463*0b57cec5SDimitry Andric DwarfCompileUnit *TheU = NU.second; 2464*0b57cec5SDimitry Andric if (!TheU->hasDwarfPubSections()) 2465*0b57cec5SDimitry Andric continue; 2466*0b57cec5SDimitry Andric 2467*0b57cec5SDimitry Andric bool GnuStyle = TheU->getCUNode()->getNameTableKind() == 2468*0b57cec5SDimitry Andric DICompileUnit::DebugNameTableKind::GNU; 2469*0b57cec5SDimitry Andric 247081ad6265SDimitry Andric Asm->OutStreamer->switchSection( 2471*0b57cec5SDimitry Andric GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection() 2472*0b57cec5SDimitry Andric : Asm->getObjFileLowering().getDwarfPubNamesSection()); 2473*0b57cec5SDimitry Andric emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames()); 2474*0b57cec5SDimitry Andric 247581ad6265SDimitry Andric Asm->OutStreamer->switchSection( 2476*0b57cec5SDimitry Andric GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection() 2477*0b57cec5SDimitry Andric : Asm->getObjFileLowering().getDwarfPubTypesSection()); 2478*0b57cec5SDimitry Andric emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes()); 2479*0b57cec5SDimitry Andric } 2480*0b57cec5SDimitry Andric } 2481*0b57cec5SDimitry Andric 2482*0b57cec5SDimitry Andric void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) { 2483*0b57cec5SDimitry Andric if (useSectionsAsReferences()) 24845ffd83dbSDimitry Andric Asm->emitDwarfOffset(CU.getSection()->getBeginSymbol(), 2485*0b57cec5SDimitry Andric CU.getDebugSectionOffset()); 2486*0b57cec5SDimitry Andric else 2487*0b57cec5SDimitry Andric Asm->emitDwarfSymbolReference(CU.getLabelBegin()); 2488*0b57cec5SDimitry Andric } 2489*0b57cec5SDimitry Andric 2490*0b57cec5SDimitry Andric void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name, 2491*0b57cec5SDimitry Andric DwarfCompileUnit *TheU, 2492*0b57cec5SDimitry Andric const StringMap<const DIE *> &Globals) { 2493*0b57cec5SDimitry Andric if (auto *Skeleton = TheU->getSkeleton()) 2494*0b57cec5SDimitry Andric TheU = Skeleton; 2495*0b57cec5SDimitry Andric 2496*0b57cec5SDimitry Andric // Emit the header. 2497fe6060f1SDimitry Andric MCSymbol *EndLabel = Asm->emitDwarfUnitLength( 2498fe6060f1SDimitry Andric "pub" + Name, "Length of Public " + Name + " Info"); 2499*0b57cec5SDimitry Andric 2500*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DWARF Version"); 2501*0b57cec5SDimitry Andric Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION); 2502*0b57cec5SDimitry Andric 2503*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Offset of Compilation Unit Info"); 2504*0b57cec5SDimitry Andric emitSectionReference(*TheU); 2505*0b57cec5SDimitry Andric 2506*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Compilation Unit Length"); 2507e8d8bef9SDimitry Andric Asm->emitDwarfLengthOrOffset(TheU->getLength()); 2508*0b57cec5SDimitry Andric 2509*0b57cec5SDimitry Andric // Emit the pubnames for this compilation unit. 2510*0b57cec5SDimitry Andric for (const auto &GI : Globals) { 2511*0b57cec5SDimitry Andric const char *Name = GI.getKeyData(); 2512*0b57cec5SDimitry Andric const DIE *Entity = GI.second; 2513*0b57cec5SDimitry Andric 2514*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DIE offset"); 2515e8d8bef9SDimitry Andric Asm->emitDwarfLengthOrOffset(Entity->getOffset()); 2516*0b57cec5SDimitry Andric 2517*0b57cec5SDimitry Andric if (GnuStyle) { 2518*0b57cec5SDimitry Andric dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity); 2519*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment( 2520*0b57cec5SDimitry Andric Twine("Attributes: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + 2521*0b57cec5SDimitry Andric ", " + dwarf::GDBIndexEntryLinkageString(Desc.Linkage)); 2522*0b57cec5SDimitry Andric Asm->emitInt8(Desc.toBits()); 2523*0b57cec5SDimitry Andric } 2524*0b57cec5SDimitry Andric 2525*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("External Name"); 25265ffd83dbSDimitry Andric Asm->OutStreamer->emitBytes(StringRef(Name, GI.getKeyLength() + 1)); 2527*0b57cec5SDimitry Andric } 2528*0b57cec5SDimitry Andric 2529*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("End Mark"); 2530e8d8bef9SDimitry Andric Asm->emitDwarfLengthOrOffset(0); 25315ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(EndLabel); 2532*0b57cec5SDimitry Andric } 2533*0b57cec5SDimitry Andric 2534*0b57cec5SDimitry Andric /// Emit null-terminated strings into a debug str section. 2535*0b57cec5SDimitry Andric void DwarfDebug::emitDebugStr() { 2536*0b57cec5SDimitry Andric MCSection *StringOffsetsSection = nullptr; 2537*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable()) { 2538*0b57cec5SDimitry Andric emitStringOffsetsTableHeader(); 2539*0b57cec5SDimitry Andric StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection(); 2540*0b57cec5SDimitry Andric } 2541*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 2542*0b57cec5SDimitry Andric Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection(), 2543*0b57cec5SDimitry Andric StringOffsetsSection, /* UseRelativeOffsets = */ true); 2544*0b57cec5SDimitry Andric } 2545*0b57cec5SDimitry Andric 2546*0b57cec5SDimitry Andric void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer, 2547*0b57cec5SDimitry Andric const DebugLocStream::Entry &Entry, 2548*0b57cec5SDimitry Andric const DwarfCompileUnit *CU) { 2549*0b57cec5SDimitry Andric auto &&Comments = DebugLocs.getComments(Entry); 2550*0b57cec5SDimitry Andric auto Comment = Comments.begin(); 2551*0b57cec5SDimitry Andric auto End = Comments.end(); 2552*0b57cec5SDimitry Andric 2553*0b57cec5SDimitry Andric // The expressions are inserted into a byte stream rather early (see 2554*0b57cec5SDimitry Andric // DwarfExpression::addExpression) so for those ops (e.g. DW_OP_convert) that 2555*0b57cec5SDimitry Andric // need to reference a base_type DIE the offset of that DIE is not yet known. 2556*0b57cec5SDimitry Andric // To deal with this we instead insert a placeholder early and then extract 2557*0b57cec5SDimitry Andric // it here and replace it with the real reference. 2558*0b57cec5SDimitry Andric unsigned PtrSize = Asm->MAI->getCodePointerSize(); 2559*0b57cec5SDimitry Andric DWARFDataExtractor Data(StringRef(DebugLocs.getBytes(Entry).data(), 2560*0b57cec5SDimitry Andric DebugLocs.getBytes(Entry).size()), 2561*0b57cec5SDimitry Andric Asm->getDataLayout().isLittleEndian(), PtrSize); 25625ffd83dbSDimitry Andric DWARFExpression Expr(Data, PtrSize, Asm->OutContext.getDwarfFormat()); 2563*0b57cec5SDimitry Andric 2564*0b57cec5SDimitry Andric using Encoding = DWARFExpression::Operation::Encoding; 25658bcb0991SDimitry Andric uint64_t Offset = 0; 2566fcaf7f86SDimitry Andric for (const auto &Op : Expr) { 2567*0b57cec5SDimitry Andric assert(Op.getCode() != dwarf::DW_OP_const_type && 2568*0b57cec5SDimitry Andric "3 operand ops not yet supported"); 2569e8d8bef9SDimitry Andric Streamer.emitInt8(Op.getCode(), Comment != End ? *(Comment++) : ""); 2570*0b57cec5SDimitry Andric Offset++; 2571*0b57cec5SDimitry Andric for (unsigned I = 0; I < 2; ++I) { 2572*0b57cec5SDimitry Andric if (Op.getDescription().Op[I] == Encoding::SizeNA) 2573*0b57cec5SDimitry Andric continue; 2574*0b57cec5SDimitry Andric if (Op.getDescription().Op[I] == Encoding::BaseTypeRef) { 257504eeddc0SDimitry Andric unsigned Length = 257604eeddc0SDimitry Andric Streamer.emitDIERef(*CU->ExprRefedBaseTypes[Op.getRawOperand(I)].Die); 2577*0b57cec5SDimitry Andric // Make sure comments stay aligned. 257804eeddc0SDimitry Andric for (unsigned J = 0; J < Length; ++J) 2579*0b57cec5SDimitry Andric if (Comment != End) 2580*0b57cec5SDimitry Andric Comment++; 2581*0b57cec5SDimitry Andric } else { 25828bcb0991SDimitry Andric for (uint64_t J = Offset; J < Op.getOperandEndOffset(I); ++J) 2583e8d8bef9SDimitry Andric Streamer.emitInt8(Data.getData()[J], Comment != End ? *(Comment++) : ""); 2584*0b57cec5SDimitry Andric } 2585*0b57cec5SDimitry Andric Offset = Op.getOperandEndOffset(I); 2586*0b57cec5SDimitry Andric } 2587*0b57cec5SDimitry Andric assert(Offset == Op.getEndOffset()); 2588*0b57cec5SDimitry Andric } 2589*0b57cec5SDimitry Andric } 2590*0b57cec5SDimitry Andric 2591*0b57cec5SDimitry Andric void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, 2592*0b57cec5SDimitry Andric const DbgValueLoc &Value, 2593*0b57cec5SDimitry Andric DwarfExpression &DwarfExpr) { 2594*0b57cec5SDimitry Andric auto *DIExpr = Value.getExpression(); 2595*0b57cec5SDimitry Andric DIExpressionCursor ExprCursor(DIExpr); 2596*0b57cec5SDimitry Andric DwarfExpr.addFragmentOffset(DIExpr); 2597fe6060f1SDimitry Andric 2598fe6060f1SDimitry Andric // If the DIExpr is is an Entry Value, we want to follow the same code path 2599fe6060f1SDimitry Andric // regardless of whether the DBG_VALUE is variadic or not. 2600fe6060f1SDimitry Andric if (DIExpr && DIExpr->isEntryValue()) { 2601fe6060f1SDimitry Andric // Entry values can only be a single register with no additional DIExpr, 2602fe6060f1SDimitry Andric // so just add it directly. 2603fe6060f1SDimitry Andric assert(Value.getLocEntries().size() == 1); 2604fe6060f1SDimitry Andric assert(Value.getLocEntries()[0].isLocation()); 2605fe6060f1SDimitry Andric MachineLocation Location = Value.getLocEntries()[0].getLoc(); 2606fe6060f1SDimitry Andric DwarfExpr.setLocation(Location, DIExpr); 2607fe6060f1SDimitry Andric 2608fe6060f1SDimitry Andric DwarfExpr.beginEntryValueExpression(ExprCursor); 2609fe6060f1SDimitry Andric 2610fe6060f1SDimitry Andric const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo(); 2611fe6060f1SDimitry Andric if (!DwarfExpr.addMachineRegExpression(TRI, ExprCursor, Location.getReg())) 2612fe6060f1SDimitry Andric return; 2613fe6060f1SDimitry Andric return DwarfExpr.addExpression(std::move(ExprCursor)); 2614fe6060f1SDimitry Andric } 2615fe6060f1SDimitry Andric 2616*0b57cec5SDimitry Andric // Regular entry. 2617fe6060f1SDimitry Andric auto EmitValueLocEntry = [&DwarfExpr, &BT, 2618fe6060f1SDimitry Andric &AP](const DbgValueLocEntry &Entry, 2619fe6060f1SDimitry Andric DIExpressionCursor &Cursor) -> bool { 2620fe6060f1SDimitry Andric if (Entry.isInt()) { 2621*0b57cec5SDimitry Andric if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed || 2622*0b57cec5SDimitry Andric BT->getEncoding() == dwarf::DW_ATE_signed_char)) 2623fe6060f1SDimitry Andric DwarfExpr.addSignedConstant(Entry.getInt()); 2624*0b57cec5SDimitry Andric else 2625fe6060f1SDimitry Andric DwarfExpr.addUnsignedConstant(Entry.getInt()); 2626fe6060f1SDimitry Andric } else if (Entry.isLocation()) { 2627fe6060f1SDimitry Andric MachineLocation Location = Entry.getLoc(); 2628fe6060f1SDimitry Andric if (Location.isIndirect()) 2629fe6060f1SDimitry Andric DwarfExpr.setMemoryLocationKind(); 2630*0b57cec5SDimitry Andric 2631*0b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo(); 2632*0b57cec5SDimitry Andric if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) 2633fe6060f1SDimitry Andric return false; 2634fe6060f1SDimitry Andric } else if (Entry.isTargetIndexLocation()) { 2635fe6060f1SDimitry Andric TargetIndexLocation Loc = Entry.getTargetIndexLocation(); 2636fe6060f1SDimitry Andric // TODO TargetIndexLocation is a target-independent. Currently only the 2637fe6060f1SDimitry Andric // WebAssembly-specific encoding is supported. 2638e8d8bef9SDimitry Andric assert(AP.TM.getTargetTriple().isWasm()); 26395ffd83dbSDimitry Andric DwarfExpr.addWasmLocation(Loc.Index, static_cast<uint64_t>(Loc.Offset)); 2640fe6060f1SDimitry Andric } else if (Entry.isConstantFP()) { 2641e8d8bef9SDimitry Andric if (AP.getDwarfVersion() >= 4 && !AP.getDwarfDebug()->tuneForSCE() && 2642fe6060f1SDimitry Andric !Cursor) { 2643fe6060f1SDimitry Andric DwarfExpr.addConstantFP(Entry.getConstantFP()->getValueAPF(), AP); 2644fe6060f1SDimitry Andric } else if (Entry.getConstantFP() 2645fe6060f1SDimitry Andric ->getValueAPF() 2646fe6060f1SDimitry Andric .bitcastToAPInt() 2647fe6060f1SDimitry Andric .getBitWidth() <= 64 /*bits*/) { 2648fe6060f1SDimitry Andric DwarfExpr.addUnsignedConstant( 2649fe6060f1SDimitry Andric Entry.getConstantFP()->getValueAPF().bitcastToAPInt()); 2650fe6060f1SDimitry Andric } else { 2651fe6060f1SDimitry Andric LLVM_DEBUG( 2652fe6060f1SDimitry Andric dbgs() << "Skipped DwarfExpression creation for ConstantFP of size" 2653fe6060f1SDimitry Andric << Entry.getConstantFP() 2654fe6060f1SDimitry Andric ->getValueAPF() 2655fe6060f1SDimitry Andric .bitcastToAPInt() 2656fe6060f1SDimitry Andric .getBitWidth() 2657fe6060f1SDimitry Andric << " bits\n"); 2658fe6060f1SDimitry Andric return false; 2659fe6060f1SDimitry Andric } 2660fe6060f1SDimitry Andric } 2661fe6060f1SDimitry Andric return true; 2662fe6060f1SDimitry Andric }; 2663fe6060f1SDimitry Andric 2664fe6060f1SDimitry Andric if (!Value.isVariadic()) { 2665fe6060f1SDimitry Andric if (!EmitValueLocEntry(Value.getLocEntries()[0], ExprCursor)) 2666fe6060f1SDimitry Andric return; 2667fe6060f1SDimitry Andric DwarfExpr.addExpression(std::move(ExprCursor)); 2668e8d8bef9SDimitry Andric return; 2669e8d8bef9SDimitry Andric } 2670fe6060f1SDimitry Andric 2671fe6060f1SDimitry Andric // If any of the location entries are registers with the value 0, then the 2672fe6060f1SDimitry Andric // location is undefined. 2673fe6060f1SDimitry Andric if (any_of(Value.getLocEntries(), [](const DbgValueLocEntry &Entry) { 2674fe6060f1SDimitry Andric return Entry.isLocation() && !Entry.getLoc().getReg(); 2675fe6060f1SDimitry Andric })) 2676fe6060f1SDimitry Andric return; 2677fe6060f1SDimitry Andric 2678fe6060f1SDimitry Andric DwarfExpr.addExpression( 2679fe6060f1SDimitry Andric std::move(ExprCursor), 2680fe6060f1SDimitry Andric [EmitValueLocEntry, &Value](unsigned Idx, 2681fe6060f1SDimitry Andric DIExpressionCursor &Cursor) -> bool { 2682fe6060f1SDimitry Andric return EmitValueLocEntry(Value.getLocEntries()[Idx], Cursor); 2683fe6060f1SDimitry Andric }); 2684*0b57cec5SDimitry Andric } 2685*0b57cec5SDimitry Andric 2686*0b57cec5SDimitry Andric void DebugLocEntry::finalize(const AsmPrinter &AP, 2687*0b57cec5SDimitry Andric DebugLocStream::ListBuilder &List, 2688*0b57cec5SDimitry Andric const DIBasicType *BT, 2689*0b57cec5SDimitry Andric DwarfCompileUnit &TheCU) { 2690*0b57cec5SDimitry Andric assert(!Values.empty() && 2691*0b57cec5SDimitry Andric "location list entries without values are redundant"); 2692*0b57cec5SDimitry Andric assert(Begin != End && "unexpected location list entry with empty range"); 2693*0b57cec5SDimitry Andric DebugLocStream::EntryBuilder Entry(List, Begin, End); 2694*0b57cec5SDimitry Andric BufferByteStreamer Streamer = Entry.getStreamer(); 2695*0b57cec5SDimitry Andric DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer, TheCU); 2696*0b57cec5SDimitry Andric const DbgValueLoc &Value = Values[0]; 2697*0b57cec5SDimitry Andric if (Value.isFragment()) { 2698*0b57cec5SDimitry Andric // Emit all fragments that belong to the same variable and range. 2699*0b57cec5SDimitry Andric assert(llvm::all_of(Values, [](DbgValueLoc P) { 2700*0b57cec5SDimitry Andric return P.isFragment(); 2701*0b57cec5SDimitry Andric }) && "all values are expected to be fragments"); 27025ffd83dbSDimitry Andric assert(llvm::is_sorted(Values) && "fragments are expected to be sorted"); 2703*0b57cec5SDimitry Andric 2704e8d8bef9SDimitry Andric for (const auto &Fragment : Values) 2705*0b57cec5SDimitry Andric DwarfDebug::emitDebugLocValue(AP, BT, Fragment, DwarfExpr); 2706*0b57cec5SDimitry Andric 2707*0b57cec5SDimitry Andric } else { 2708*0b57cec5SDimitry Andric assert(Values.size() == 1 && "only fragments may have >1 value"); 2709*0b57cec5SDimitry Andric DwarfDebug::emitDebugLocValue(AP, BT, Value, DwarfExpr); 2710*0b57cec5SDimitry Andric } 2711*0b57cec5SDimitry Andric DwarfExpr.finalize(); 2712480093f4SDimitry Andric if (DwarfExpr.TagOffset) 2713480093f4SDimitry Andric List.setTagOffset(*DwarfExpr.TagOffset); 2714*0b57cec5SDimitry Andric } 2715*0b57cec5SDimitry Andric 2716*0b57cec5SDimitry Andric void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry, 2717*0b57cec5SDimitry Andric const DwarfCompileUnit *CU) { 2718*0b57cec5SDimitry Andric // Emit the size. 2719*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Loc expr size"); 2720*0b57cec5SDimitry Andric if (getDwarfVersion() >= 5) 27215ffd83dbSDimitry Andric Asm->emitULEB128(DebugLocs.getBytes(Entry).size()); 2722*0b57cec5SDimitry Andric else if (DebugLocs.getBytes(Entry).size() <= std::numeric_limits<uint16_t>::max()) 2723*0b57cec5SDimitry Andric Asm->emitInt16(DebugLocs.getBytes(Entry).size()); 2724*0b57cec5SDimitry Andric else { 2725*0b57cec5SDimitry Andric // The entry is too big to fit into 16 bit, drop it as there is nothing we 2726*0b57cec5SDimitry Andric // can do. 2727*0b57cec5SDimitry Andric Asm->emitInt16(0); 2728*0b57cec5SDimitry Andric return; 2729*0b57cec5SDimitry Andric } 2730*0b57cec5SDimitry Andric // Emit the entry. 2731*0b57cec5SDimitry Andric APByteStreamer Streamer(*Asm); 2732*0b57cec5SDimitry Andric emitDebugLocEntry(Streamer, Entry, CU); 2733*0b57cec5SDimitry Andric } 2734*0b57cec5SDimitry Andric 2735*0b57cec5SDimitry Andric // Emit the header of a DWARF 5 range list table list table. Returns the symbol 2736*0b57cec5SDimitry Andric // that designates the end of the table for the caller to emit when the table is 2737*0b57cec5SDimitry Andric // complete. 2738*0b57cec5SDimitry Andric static MCSymbol *emitRnglistsTableHeader(AsmPrinter *Asm, 2739*0b57cec5SDimitry Andric const DwarfFile &Holder) { 27405ffd83dbSDimitry Andric MCSymbol *TableEnd = mcdwarf::emitListsTableHeaderStart(*Asm->OutStreamer); 2741*0b57cec5SDimitry Andric 2742*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Offset entry count"); 2743*0b57cec5SDimitry Andric Asm->emitInt32(Holder.getRangeLists().size()); 27445ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(Holder.getRnglistsTableBaseSym()); 2745*0b57cec5SDimitry Andric 2746*0b57cec5SDimitry Andric for (const RangeSpanList &List : Holder.getRangeLists()) 2747e8d8bef9SDimitry Andric Asm->emitLabelDifference(List.Label, Holder.getRnglistsTableBaseSym(), 2748e8d8bef9SDimitry Andric Asm->getDwarfOffsetByteSize()); 2749*0b57cec5SDimitry Andric 2750*0b57cec5SDimitry Andric return TableEnd; 2751*0b57cec5SDimitry Andric } 2752*0b57cec5SDimitry Andric 2753*0b57cec5SDimitry Andric // Emit the header of a DWARF 5 locations list table. Returns the symbol that 2754*0b57cec5SDimitry Andric // designates the end of the table for the caller to emit when the table is 2755*0b57cec5SDimitry Andric // complete. 2756*0b57cec5SDimitry Andric static MCSymbol *emitLoclistsTableHeader(AsmPrinter *Asm, 27578bcb0991SDimitry Andric const DwarfDebug &DD) { 27585ffd83dbSDimitry Andric MCSymbol *TableEnd = mcdwarf::emitListsTableHeaderStart(*Asm->OutStreamer); 27598bcb0991SDimitry Andric 27608bcb0991SDimitry Andric const auto &DebugLocs = DD.getDebugLocs(); 2761*0b57cec5SDimitry Andric 2762*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Offset entry count"); 2763480093f4SDimitry Andric Asm->emitInt32(DebugLocs.getLists().size()); 27645ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(DebugLocs.getSym()); 2765*0b57cec5SDimitry Andric 2766480093f4SDimitry Andric for (const auto &List : DebugLocs.getLists()) 2767e8d8bef9SDimitry Andric Asm->emitLabelDifference(List.Label, DebugLocs.getSym(), 2768e8d8bef9SDimitry Andric Asm->getDwarfOffsetByteSize()); 2769480093f4SDimitry Andric 2770*0b57cec5SDimitry Andric return TableEnd; 2771*0b57cec5SDimitry Andric } 2772*0b57cec5SDimitry Andric 27738bcb0991SDimitry Andric template <typename Ranges, typename PayloadEmitter> 27748bcb0991SDimitry Andric static void emitRangeList( 27758bcb0991SDimitry Andric DwarfDebug &DD, AsmPrinter *Asm, MCSymbol *Sym, const Ranges &R, 27768bcb0991SDimitry Andric const DwarfCompileUnit &CU, unsigned BaseAddressx, unsigned OffsetPair, 27778bcb0991SDimitry Andric unsigned StartxLength, unsigned EndOfList, 27788bcb0991SDimitry Andric StringRef (*StringifyEnum)(unsigned), 27798bcb0991SDimitry Andric bool ShouldUseBaseAddress, 27808bcb0991SDimitry Andric PayloadEmitter EmitPayload) { 27818bcb0991SDimitry Andric 27828bcb0991SDimitry Andric auto Size = Asm->MAI->getCodePointerSize(); 27838bcb0991SDimitry Andric bool UseDwarf5 = DD.getDwarfVersion() >= 5; 27848bcb0991SDimitry Andric 27858bcb0991SDimitry Andric // Emit our symbol so we can find the beginning of the range. 27865ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(Sym); 27878bcb0991SDimitry Andric 27888bcb0991SDimitry Andric // Gather all the ranges that apply to the same section so they can share 27898bcb0991SDimitry Andric // a base address entry. 27908bcb0991SDimitry Andric MapVector<const MCSection *, std::vector<decltype(&*R.begin())>> SectionRanges; 27918bcb0991SDimitry Andric 27928bcb0991SDimitry Andric for (const auto &Range : R) 27938bcb0991SDimitry Andric SectionRanges[&Range.Begin->getSection()].push_back(&Range); 27948bcb0991SDimitry Andric 27958bcb0991SDimitry Andric const MCSymbol *CUBase = CU.getBaseAddress(); 27968bcb0991SDimitry Andric bool BaseIsSet = false; 27978bcb0991SDimitry Andric for (const auto &P : SectionRanges) { 27988bcb0991SDimitry Andric auto *Base = CUBase; 27998bcb0991SDimitry Andric if (!Base && ShouldUseBaseAddress) { 28008bcb0991SDimitry Andric const MCSymbol *Begin = P.second.front()->Begin; 28018bcb0991SDimitry Andric const MCSymbol *NewBase = DD.getSectionLabel(&Begin->getSection()); 28028bcb0991SDimitry Andric if (!UseDwarf5) { 28038bcb0991SDimitry Andric Base = NewBase; 28048bcb0991SDimitry Andric BaseIsSet = true; 28055ffd83dbSDimitry Andric Asm->OutStreamer->emitIntValue(-1, Size); 28068bcb0991SDimitry Andric Asm->OutStreamer->AddComment(" base address"); 28075ffd83dbSDimitry Andric Asm->OutStreamer->emitSymbolValue(Base, Size); 28088bcb0991SDimitry Andric } else if (NewBase != Begin || P.second.size() > 1) { 28098bcb0991SDimitry Andric // Only use a base address if 28108bcb0991SDimitry Andric // * the existing pool address doesn't match (NewBase != Begin) 28118bcb0991SDimitry Andric // * or, there's more than one entry to share the base address 28128bcb0991SDimitry Andric Base = NewBase; 28138bcb0991SDimitry Andric BaseIsSet = true; 28148bcb0991SDimitry Andric Asm->OutStreamer->AddComment(StringifyEnum(BaseAddressx)); 28158bcb0991SDimitry Andric Asm->emitInt8(BaseAddressx); 28168bcb0991SDimitry Andric Asm->OutStreamer->AddComment(" base address index"); 28175ffd83dbSDimitry Andric Asm->emitULEB128(DD.getAddressPool().getIndex(Base)); 28188bcb0991SDimitry Andric } 28198bcb0991SDimitry Andric } else if (BaseIsSet && !UseDwarf5) { 28208bcb0991SDimitry Andric BaseIsSet = false; 28218bcb0991SDimitry Andric assert(!Base); 28225ffd83dbSDimitry Andric Asm->OutStreamer->emitIntValue(-1, Size); 28235ffd83dbSDimitry Andric Asm->OutStreamer->emitIntValue(0, Size); 28248bcb0991SDimitry Andric } 28258bcb0991SDimitry Andric 28268bcb0991SDimitry Andric for (const auto *RS : P.second) { 28278bcb0991SDimitry Andric const MCSymbol *Begin = RS->Begin; 28288bcb0991SDimitry Andric const MCSymbol *End = RS->End; 28298bcb0991SDimitry Andric assert(Begin && "Range without a begin symbol?"); 28308bcb0991SDimitry Andric assert(End && "Range without an end symbol?"); 28318bcb0991SDimitry Andric if (Base) { 28328bcb0991SDimitry Andric if (UseDwarf5) { 28338bcb0991SDimitry Andric // Emit offset_pair when we have a base. 28348bcb0991SDimitry Andric Asm->OutStreamer->AddComment(StringifyEnum(OffsetPair)); 28358bcb0991SDimitry Andric Asm->emitInt8(OffsetPair); 28368bcb0991SDimitry Andric Asm->OutStreamer->AddComment(" starting offset"); 28375ffd83dbSDimitry Andric Asm->emitLabelDifferenceAsULEB128(Begin, Base); 28388bcb0991SDimitry Andric Asm->OutStreamer->AddComment(" ending offset"); 28395ffd83dbSDimitry Andric Asm->emitLabelDifferenceAsULEB128(End, Base); 28408bcb0991SDimitry Andric } else { 28415ffd83dbSDimitry Andric Asm->emitLabelDifference(Begin, Base, Size); 28425ffd83dbSDimitry Andric Asm->emitLabelDifference(End, Base, Size); 28438bcb0991SDimitry Andric } 28448bcb0991SDimitry Andric } else if (UseDwarf5) { 28458bcb0991SDimitry Andric Asm->OutStreamer->AddComment(StringifyEnum(StartxLength)); 28468bcb0991SDimitry Andric Asm->emitInt8(StartxLength); 28478bcb0991SDimitry Andric Asm->OutStreamer->AddComment(" start index"); 28485ffd83dbSDimitry Andric Asm->emitULEB128(DD.getAddressPool().getIndex(Begin)); 28498bcb0991SDimitry Andric Asm->OutStreamer->AddComment(" length"); 28505ffd83dbSDimitry Andric Asm->emitLabelDifferenceAsULEB128(End, Begin); 28518bcb0991SDimitry Andric } else { 28525ffd83dbSDimitry Andric Asm->OutStreamer->emitSymbolValue(Begin, Size); 28535ffd83dbSDimitry Andric Asm->OutStreamer->emitSymbolValue(End, Size); 28548bcb0991SDimitry Andric } 28558bcb0991SDimitry Andric EmitPayload(*RS); 28568bcb0991SDimitry Andric } 28578bcb0991SDimitry Andric } 28588bcb0991SDimitry Andric 28598bcb0991SDimitry Andric if (UseDwarf5) { 28608bcb0991SDimitry Andric Asm->OutStreamer->AddComment(StringifyEnum(EndOfList)); 28618bcb0991SDimitry Andric Asm->emitInt8(EndOfList); 28628bcb0991SDimitry Andric } else { 28638bcb0991SDimitry Andric // Terminate the list with two 0 values. 28645ffd83dbSDimitry Andric Asm->OutStreamer->emitIntValue(0, Size); 28655ffd83dbSDimitry Andric Asm->OutStreamer->emitIntValue(0, Size); 28668bcb0991SDimitry Andric } 28678bcb0991SDimitry Andric } 28688bcb0991SDimitry Andric 2869480093f4SDimitry Andric // Handles emission of both debug_loclist / debug_loclist.dwo 28708bcb0991SDimitry Andric static void emitLocList(DwarfDebug &DD, AsmPrinter *Asm, const DebugLocStream::List &List) { 2871480093f4SDimitry Andric emitRangeList(DD, Asm, List.Label, DD.getDebugLocs().getEntries(List), 2872480093f4SDimitry Andric *List.CU, dwarf::DW_LLE_base_addressx, 2873480093f4SDimitry Andric dwarf::DW_LLE_offset_pair, dwarf::DW_LLE_startx_length, 2874480093f4SDimitry Andric dwarf::DW_LLE_end_of_list, llvm::dwarf::LocListEncodingString, 28758bcb0991SDimitry Andric /* ShouldUseBaseAddress */ true, 28768bcb0991SDimitry Andric [&](const DebugLocStream::Entry &E) { 28778bcb0991SDimitry Andric DD.emitDebugLocEntryLocation(E, List.CU); 28788bcb0991SDimitry Andric }); 28798bcb0991SDimitry Andric } 28808bcb0991SDimitry Andric 2881480093f4SDimitry Andric void DwarfDebug::emitDebugLocImpl(MCSection *Sec) { 2882*0b57cec5SDimitry Andric if (DebugLocs.getLists().empty()) 2883*0b57cec5SDimitry Andric return; 2884*0b57cec5SDimitry Andric 288581ad6265SDimitry Andric Asm->OutStreamer->switchSection(Sec); 2886480093f4SDimitry Andric 2887*0b57cec5SDimitry Andric MCSymbol *TableEnd = nullptr; 2888480093f4SDimitry Andric if (getDwarfVersion() >= 5) 28898bcb0991SDimitry Andric TableEnd = emitLoclistsTableHeader(Asm, *this); 2890*0b57cec5SDimitry Andric 28918bcb0991SDimitry Andric for (const auto &List : DebugLocs.getLists()) 28928bcb0991SDimitry Andric emitLocList(*this, Asm, List); 2893*0b57cec5SDimitry Andric 2894*0b57cec5SDimitry Andric if (TableEnd) 28955ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(TableEnd); 2896*0b57cec5SDimitry Andric } 2897*0b57cec5SDimitry Andric 2898480093f4SDimitry Andric // Emit locations into the .debug_loc/.debug_loclists section. 2899480093f4SDimitry Andric void DwarfDebug::emitDebugLoc() { 2900480093f4SDimitry Andric emitDebugLocImpl( 2901480093f4SDimitry Andric getDwarfVersion() >= 5 2902480093f4SDimitry Andric ? Asm->getObjFileLowering().getDwarfLoclistsSection() 2903480093f4SDimitry Andric : Asm->getObjFileLowering().getDwarfLocSection()); 2904480093f4SDimitry Andric } 2905480093f4SDimitry Andric 2906480093f4SDimitry Andric // Emit locations into the .debug_loc.dwo/.debug_loclists.dwo section. 2907*0b57cec5SDimitry Andric void DwarfDebug::emitDebugLocDWO() { 2908480093f4SDimitry Andric if (getDwarfVersion() >= 5) { 2909480093f4SDimitry Andric emitDebugLocImpl( 2910480093f4SDimitry Andric Asm->getObjFileLowering().getDwarfLoclistsDWOSection()); 2911480093f4SDimitry Andric 2912480093f4SDimitry Andric return; 2913480093f4SDimitry Andric } 2914480093f4SDimitry Andric 2915*0b57cec5SDimitry Andric for (const auto &List : DebugLocs.getLists()) { 291681ad6265SDimitry Andric Asm->OutStreamer->switchSection( 2917*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfLocDWOSection()); 29185ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(List.Label); 2919480093f4SDimitry Andric 2920*0b57cec5SDimitry Andric for (const auto &Entry : DebugLocs.getEntries(List)) { 2921*0b57cec5SDimitry Andric // GDB only supports startx_length in pre-standard split-DWARF. 2922*0b57cec5SDimitry Andric // (in v5 standard loclists, it currently* /only/ supports base_address + 2923*0b57cec5SDimitry Andric // offset_pair, so the implementations can't really share much since they 2924*0b57cec5SDimitry Andric // need to use different representations) 2925*0b57cec5SDimitry Andric // * as of October 2018, at least 29265ffd83dbSDimitry Andric // 29275ffd83dbSDimitry Andric // In v5 (see emitLocList), this uses SectionLabels to reuse existing 29285ffd83dbSDimitry Andric // addresses in the address pool to minimize object size/relocations. 2929*0b57cec5SDimitry Andric Asm->emitInt8(dwarf::DW_LLE_startx_length); 29308bcb0991SDimitry Andric unsigned idx = AddrPool.getIndex(Entry.Begin); 29315ffd83dbSDimitry Andric Asm->emitULEB128(idx); 2932480093f4SDimitry Andric // Also the pre-standard encoding is slightly different, emitting this as 2933480093f4SDimitry Andric // an address-length entry here, but its a ULEB128 in DWARFv5 loclists. 29345ffd83dbSDimitry Andric Asm->emitLabelDifference(Entry.End, Entry.Begin, 4); 2935*0b57cec5SDimitry Andric emitDebugLocEntryLocation(Entry, List.CU); 2936*0b57cec5SDimitry Andric } 2937*0b57cec5SDimitry Andric Asm->emitInt8(dwarf::DW_LLE_end_of_list); 2938*0b57cec5SDimitry Andric } 2939*0b57cec5SDimitry Andric } 2940*0b57cec5SDimitry Andric 2941*0b57cec5SDimitry Andric struct ArangeSpan { 2942*0b57cec5SDimitry Andric const MCSymbol *Start, *End; 2943*0b57cec5SDimitry Andric }; 2944*0b57cec5SDimitry Andric 2945*0b57cec5SDimitry Andric // Emit a debug aranges section, containing a CU lookup for any 2946*0b57cec5SDimitry Andric // address we can tie back to a CU. 2947*0b57cec5SDimitry Andric void DwarfDebug::emitDebugARanges() { 2948*0b57cec5SDimitry Andric // Provides a unique id per text section. 2949*0b57cec5SDimitry Andric MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap; 2950*0b57cec5SDimitry Andric 2951*0b57cec5SDimitry Andric // Filter labels by section. 2952*0b57cec5SDimitry Andric for (const SymbolCU &SCU : ArangeLabels) { 2953*0b57cec5SDimitry Andric if (SCU.Sym->isInSection()) { 2954*0b57cec5SDimitry Andric // Make a note of this symbol and it's section. 2955*0b57cec5SDimitry Andric MCSection *Section = &SCU.Sym->getSection(); 2956*0b57cec5SDimitry Andric if (!Section->getKind().isMetadata()) 2957*0b57cec5SDimitry Andric SectionMap[Section].push_back(SCU); 2958*0b57cec5SDimitry Andric } else { 2959*0b57cec5SDimitry Andric // Some symbols (e.g. common/bss on mach-o) can have no section but still 2960*0b57cec5SDimitry Andric // appear in the output. This sucks as we rely on sections to build 2961*0b57cec5SDimitry Andric // arange spans. We can do it without, but it's icky. 2962*0b57cec5SDimitry Andric SectionMap[nullptr].push_back(SCU); 2963*0b57cec5SDimitry Andric } 2964*0b57cec5SDimitry Andric } 2965*0b57cec5SDimitry Andric 2966*0b57cec5SDimitry Andric DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans; 2967*0b57cec5SDimitry Andric 2968*0b57cec5SDimitry Andric for (auto &I : SectionMap) { 2969*0b57cec5SDimitry Andric MCSection *Section = I.first; 2970*0b57cec5SDimitry Andric SmallVector<SymbolCU, 8> &List = I.second; 2971*0b57cec5SDimitry Andric if (List.size() < 1) 2972*0b57cec5SDimitry Andric continue; 2973*0b57cec5SDimitry Andric 2974*0b57cec5SDimitry Andric // If we have no section (e.g. common), just write out 2975*0b57cec5SDimitry Andric // individual spans for each symbol. 2976*0b57cec5SDimitry Andric if (!Section) { 2977*0b57cec5SDimitry Andric for (const SymbolCU &Cur : List) { 2978*0b57cec5SDimitry Andric ArangeSpan Span; 2979*0b57cec5SDimitry Andric Span.Start = Cur.Sym; 2980*0b57cec5SDimitry Andric Span.End = nullptr; 2981*0b57cec5SDimitry Andric assert(Cur.CU); 2982*0b57cec5SDimitry Andric Spans[Cur.CU].push_back(Span); 2983*0b57cec5SDimitry Andric } 2984*0b57cec5SDimitry Andric continue; 2985*0b57cec5SDimitry Andric } 2986*0b57cec5SDimitry Andric 2987*0b57cec5SDimitry Andric // Sort the symbols by offset within the section. 2988*0b57cec5SDimitry Andric llvm::stable_sort(List, [&](const SymbolCU &A, const SymbolCU &B) { 298981ad6265SDimitry Andric unsigned IA = A.Sym ? Asm->OutStreamer->getSymbolOrder(A.Sym) : 0; 299081ad6265SDimitry Andric unsigned IB = B.Sym ? Asm->OutStreamer->getSymbolOrder(B.Sym) : 0; 2991*0b57cec5SDimitry Andric 2992*0b57cec5SDimitry Andric // Symbols with no order assigned should be placed at the end. 2993*0b57cec5SDimitry Andric // (e.g. section end labels) 2994*0b57cec5SDimitry Andric if (IA == 0) 2995*0b57cec5SDimitry Andric return false; 2996*0b57cec5SDimitry Andric if (IB == 0) 2997*0b57cec5SDimitry Andric return true; 2998*0b57cec5SDimitry Andric return IA < IB; 2999*0b57cec5SDimitry Andric }); 3000*0b57cec5SDimitry Andric 3001*0b57cec5SDimitry Andric // Insert a final terminator. 3002*0b57cec5SDimitry Andric List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section))); 3003*0b57cec5SDimitry Andric 3004*0b57cec5SDimitry Andric // Build spans between each label. 3005*0b57cec5SDimitry Andric const MCSymbol *StartSym = List[0].Sym; 3006*0b57cec5SDimitry Andric for (size_t n = 1, e = List.size(); n < e; n++) { 3007*0b57cec5SDimitry Andric const SymbolCU &Prev = List[n - 1]; 3008*0b57cec5SDimitry Andric const SymbolCU &Cur = List[n]; 3009*0b57cec5SDimitry Andric 3010*0b57cec5SDimitry Andric // Try and build the longest span we can within the same CU. 3011*0b57cec5SDimitry Andric if (Cur.CU != Prev.CU) { 3012*0b57cec5SDimitry Andric ArangeSpan Span; 3013*0b57cec5SDimitry Andric Span.Start = StartSym; 3014*0b57cec5SDimitry Andric Span.End = Cur.Sym; 3015*0b57cec5SDimitry Andric assert(Prev.CU); 3016*0b57cec5SDimitry Andric Spans[Prev.CU].push_back(Span); 3017*0b57cec5SDimitry Andric StartSym = Cur.Sym; 3018*0b57cec5SDimitry Andric } 3019*0b57cec5SDimitry Andric } 3020*0b57cec5SDimitry Andric } 3021*0b57cec5SDimitry Andric 3022*0b57cec5SDimitry Andric // Start the dwarf aranges section. 302381ad6265SDimitry Andric Asm->OutStreamer->switchSection( 3024*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfARangesSection()); 3025*0b57cec5SDimitry Andric 3026*0b57cec5SDimitry Andric unsigned PtrSize = Asm->MAI->getCodePointerSize(); 3027*0b57cec5SDimitry Andric 3028*0b57cec5SDimitry Andric // Build a list of CUs used. 3029*0b57cec5SDimitry Andric std::vector<DwarfCompileUnit *> CUs; 3030*0b57cec5SDimitry Andric for (const auto &it : Spans) { 3031*0b57cec5SDimitry Andric DwarfCompileUnit *CU = it.first; 3032*0b57cec5SDimitry Andric CUs.push_back(CU); 3033*0b57cec5SDimitry Andric } 3034*0b57cec5SDimitry Andric 3035*0b57cec5SDimitry Andric // Sort the CU list (again, to ensure consistent output order). 3036*0b57cec5SDimitry Andric llvm::sort(CUs, [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) { 3037*0b57cec5SDimitry Andric return A->getUniqueID() < B->getUniqueID(); 3038*0b57cec5SDimitry Andric }); 3039*0b57cec5SDimitry Andric 3040*0b57cec5SDimitry Andric // Emit an arange table for each CU we used. 3041*0b57cec5SDimitry Andric for (DwarfCompileUnit *CU : CUs) { 3042*0b57cec5SDimitry Andric std::vector<ArangeSpan> &List = Spans[CU]; 3043*0b57cec5SDimitry Andric 3044*0b57cec5SDimitry Andric // Describe the skeleton CU's offset and length, not the dwo file's. 3045*0b57cec5SDimitry Andric if (auto *Skel = CU->getSkeleton()) 3046*0b57cec5SDimitry Andric CU = Skel; 3047*0b57cec5SDimitry Andric 3048*0b57cec5SDimitry Andric // Emit size of content not including length itself. 3049*0b57cec5SDimitry Andric unsigned ContentSize = 3050*0b57cec5SDimitry Andric sizeof(int16_t) + // DWARF ARange version number 3051e8d8bef9SDimitry Andric Asm->getDwarfOffsetByteSize() + // Offset of CU in the .debug_info 3052e8d8bef9SDimitry Andric // section 3053*0b57cec5SDimitry Andric sizeof(int8_t) + // Pointer Size (in bytes) 3054*0b57cec5SDimitry Andric sizeof(int8_t); // Segment Size (in bytes) 3055*0b57cec5SDimitry Andric 3056*0b57cec5SDimitry Andric unsigned TupleSize = PtrSize * 2; 3057*0b57cec5SDimitry Andric 3058*0b57cec5SDimitry Andric // 7.20 in the Dwarf specs requires the table to be aligned to a tuple. 3059e8d8bef9SDimitry Andric unsigned Padding = offsetToAlignment( 3060e8d8bef9SDimitry Andric Asm->getUnitLengthFieldByteSize() + ContentSize, Align(TupleSize)); 3061*0b57cec5SDimitry Andric 3062*0b57cec5SDimitry Andric ContentSize += Padding; 3063*0b57cec5SDimitry Andric ContentSize += (List.size() + 1) * TupleSize; 3064*0b57cec5SDimitry Andric 3065*0b57cec5SDimitry Andric // For each compile unit, write the list of spans it covers. 3066e8d8bef9SDimitry Andric Asm->emitDwarfUnitLength(ContentSize, "Length of ARange Set"); 3067*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DWARF Arange version number"); 3068*0b57cec5SDimitry Andric Asm->emitInt16(dwarf::DW_ARANGES_VERSION); 3069*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Offset Into Debug Info Section"); 3070*0b57cec5SDimitry Andric emitSectionReference(*CU); 3071*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Address Size (in bytes)"); 3072*0b57cec5SDimitry Andric Asm->emitInt8(PtrSize); 3073*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Segment Size (in bytes)"); 3074*0b57cec5SDimitry Andric Asm->emitInt8(0); 3075*0b57cec5SDimitry Andric 3076*0b57cec5SDimitry Andric Asm->OutStreamer->emitFill(Padding, 0xff); 3077*0b57cec5SDimitry Andric 3078*0b57cec5SDimitry Andric for (const ArangeSpan &Span : List) { 30795ffd83dbSDimitry Andric Asm->emitLabelReference(Span.Start, PtrSize); 3080*0b57cec5SDimitry Andric 308181ad6265SDimitry Andric // Calculate the size as being from the span start to its end. 308281ad6265SDimitry Andric // 308381ad6265SDimitry Andric // If the size is zero, then round it up to one byte. The DWARF 308481ad6265SDimitry Andric // specification requires that entries in this table have nonzero 308581ad6265SDimitry Andric // lengths. 308681ad6265SDimitry Andric auto SizeRef = SymSize.find(Span.Start); 308781ad6265SDimitry Andric if ((SizeRef == SymSize.end() || SizeRef->second != 0) && Span.End) { 30885ffd83dbSDimitry Andric Asm->emitLabelDifference(Span.End, Span.Start, PtrSize); 3089*0b57cec5SDimitry Andric } else { 3090*0b57cec5SDimitry Andric // For symbols without an end marker (e.g. common), we 3091*0b57cec5SDimitry Andric // write a single arange entry containing just that one symbol. 309281ad6265SDimitry Andric uint64_t Size; 309381ad6265SDimitry Andric if (SizeRef == SymSize.end() || SizeRef->second == 0) 3094*0b57cec5SDimitry Andric Size = 1; 309581ad6265SDimitry Andric else 309681ad6265SDimitry Andric Size = SizeRef->second; 3097*0b57cec5SDimitry Andric 30985ffd83dbSDimitry Andric Asm->OutStreamer->emitIntValue(Size, PtrSize); 3099*0b57cec5SDimitry Andric } 3100*0b57cec5SDimitry Andric } 3101*0b57cec5SDimitry Andric 3102*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("ARange terminator"); 31035ffd83dbSDimitry Andric Asm->OutStreamer->emitIntValue(0, PtrSize); 31045ffd83dbSDimitry Andric Asm->OutStreamer->emitIntValue(0, PtrSize); 3105*0b57cec5SDimitry Andric } 3106*0b57cec5SDimitry Andric } 3107*0b57cec5SDimitry Andric 3108*0b57cec5SDimitry Andric /// Emit a single range list. We handle both DWARF v5 and earlier. 3109*0b57cec5SDimitry Andric static void emitRangeList(DwarfDebug &DD, AsmPrinter *Asm, 3110*0b57cec5SDimitry Andric const RangeSpanList &List) { 3111480093f4SDimitry Andric emitRangeList(DD, Asm, List.Label, List.Ranges, *List.CU, 31128bcb0991SDimitry Andric dwarf::DW_RLE_base_addressx, dwarf::DW_RLE_offset_pair, 31138bcb0991SDimitry Andric dwarf::DW_RLE_startx_length, dwarf::DW_RLE_end_of_list, 31148bcb0991SDimitry Andric llvm::dwarf::RangeListEncodingString, 3115480093f4SDimitry Andric List.CU->getCUNode()->getRangesBaseAddress() || 31168bcb0991SDimitry Andric DD.getDwarfVersion() >= 5, 31178bcb0991SDimitry Andric [](auto) {}); 3118*0b57cec5SDimitry Andric } 3119*0b57cec5SDimitry Andric 3120480093f4SDimitry Andric void DwarfDebug::emitDebugRangesImpl(const DwarfFile &Holder, MCSection *Section) { 3121480093f4SDimitry Andric if (Holder.getRangeLists().empty()) 3122480093f4SDimitry Andric return; 3123480093f4SDimitry Andric 3124480093f4SDimitry Andric assert(useRangesSection()); 3125480093f4SDimitry Andric assert(!CUMap.empty()); 3126480093f4SDimitry Andric assert(llvm::any_of(CUMap, [](const decltype(CUMap)::value_type &Pair) { 3127480093f4SDimitry Andric return !Pair.second->getCUNode()->isDebugDirectivesOnly(); 3128480093f4SDimitry Andric })); 3129480093f4SDimitry Andric 313081ad6265SDimitry Andric Asm->OutStreamer->switchSection(Section); 3131480093f4SDimitry Andric 3132480093f4SDimitry Andric MCSymbol *TableEnd = nullptr; 3133480093f4SDimitry Andric if (getDwarfVersion() >= 5) 3134480093f4SDimitry Andric TableEnd = emitRnglistsTableHeader(Asm, Holder); 3135480093f4SDimitry Andric 3136*0b57cec5SDimitry Andric for (const RangeSpanList &List : Holder.getRangeLists()) 3137480093f4SDimitry Andric emitRangeList(*this, Asm, List); 3138*0b57cec5SDimitry Andric 3139*0b57cec5SDimitry Andric if (TableEnd) 31405ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(TableEnd); 3141*0b57cec5SDimitry Andric } 3142*0b57cec5SDimitry Andric 3143*0b57cec5SDimitry Andric /// Emit address ranges into the .debug_ranges section or into the DWARF v5 3144*0b57cec5SDimitry Andric /// .debug_rnglists section. 3145*0b57cec5SDimitry Andric void DwarfDebug::emitDebugRanges() { 3146*0b57cec5SDimitry Andric const auto &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 3147*0b57cec5SDimitry Andric 3148480093f4SDimitry Andric emitDebugRangesImpl(Holder, 3149480093f4SDimitry Andric getDwarfVersion() >= 5 3150480093f4SDimitry Andric ? Asm->getObjFileLowering().getDwarfRnglistsSection() 3151480093f4SDimitry Andric : Asm->getObjFileLowering().getDwarfRangesSection()); 3152*0b57cec5SDimitry Andric } 3153*0b57cec5SDimitry Andric 3154*0b57cec5SDimitry Andric void DwarfDebug::emitDebugRangesDWO() { 3155480093f4SDimitry Andric emitDebugRangesImpl(InfoHolder, 3156*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfRnglistsDWOSection()); 3157*0b57cec5SDimitry Andric } 3158*0b57cec5SDimitry Andric 3159e8d8bef9SDimitry Andric /// Emit the header of a DWARF 5 macro section, or the GNU extension for 3160e8d8bef9SDimitry Andric /// DWARF 4. 31615ffd83dbSDimitry Andric static void emitMacroHeader(AsmPrinter *Asm, const DwarfDebug &DD, 3162e8d8bef9SDimitry Andric const DwarfCompileUnit &CU, uint16_t DwarfVersion) { 31635ffd83dbSDimitry Andric enum HeaderFlagMask { 31645ffd83dbSDimitry Andric #define HANDLE_MACRO_FLAG(ID, NAME) MACRO_FLAG_##NAME = ID, 31655ffd83dbSDimitry Andric #include "llvm/BinaryFormat/Dwarf.def" 31665ffd83dbSDimitry Andric }; 31675ffd83dbSDimitry Andric Asm->OutStreamer->AddComment("Macro information version"); 3168e8d8bef9SDimitry Andric Asm->emitInt16(DwarfVersion >= 5 ? DwarfVersion : 4); 3169e8d8bef9SDimitry Andric // We emit the line offset flag unconditionally here, since line offset should 3170e8d8bef9SDimitry Andric // be mostly present. 3171e8d8bef9SDimitry Andric if (Asm->isDwarf64()) { 3172e8d8bef9SDimitry Andric Asm->OutStreamer->AddComment("Flags: 64 bit, debug_line_offset present"); 3173e8d8bef9SDimitry Andric Asm->emitInt8(MACRO_FLAG_OFFSET_SIZE | MACRO_FLAG_DEBUG_LINE_OFFSET); 3174e8d8bef9SDimitry Andric } else { 31755ffd83dbSDimitry Andric Asm->OutStreamer->AddComment("Flags: 32 bit, debug_line_offset present"); 3176e8d8bef9SDimitry Andric Asm->emitInt8(MACRO_FLAG_DEBUG_LINE_OFFSET); 3177e8d8bef9SDimitry Andric } 31785ffd83dbSDimitry Andric Asm->OutStreamer->AddComment("debug_line_offset"); 3179e8d8bef9SDimitry Andric if (DD.useSplitDwarf()) 3180e8d8bef9SDimitry Andric Asm->emitDwarfLengthOrOffset(0); 3181e8d8bef9SDimitry Andric else 3182e8d8bef9SDimitry Andric Asm->emitDwarfSymbolReference(CU.getLineTableStartSym()); 31835ffd83dbSDimitry Andric } 31845ffd83dbSDimitry Andric 3185*0b57cec5SDimitry Andric void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) { 3186*0b57cec5SDimitry Andric for (auto *MN : Nodes) { 3187*0b57cec5SDimitry Andric if (auto *M = dyn_cast<DIMacro>(MN)) 3188*0b57cec5SDimitry Andric emitMacro(*M); 3189*0b57cec5SDimitry Andric else if (auto *F = dyn_cast<DIMacroFile>(MN)) 3190*0b57cec5SDimitry Andric emitMacroFile(*F, U); 3191*0b57cec5SDimitry Andric else 3192*0b57cec5SDimitry Andric llvm_unreachable("Unexpected DI type!"); 3193*0b57cec5SDimitry Andric } 3194*0b57cec5SDimitry Andric } 3195*0b57cec5SDimitry Andric 3196*0b57cec5SDimitry Andric void DwarfDebug::emitMacro(DIMacro &M) { 3197*0b57cec5SDimitry Andric StringRef Name = M.getName(); 3198*0b57cec5SDimitry Andric StringRef Value = M.getValue(); 31995ffd83dbSDimitry Andric 3200e8d8bef9SDimitry Andric // There should be one space between the macro name and the macro value in 3201e8d8bef9SDimitry Andric // define entries. In undef entries, only the macro name is emitted. 3202e8d8bef9SDimitry Andric std::string Str = Value.empty() ? Name.str() : (Name + " " + Value).str(); 3203e8d8bef9SDimitry Andric 3204e8d8bef9SDimitry Andric if (UseDebugMacroSection) { 3205e8d8bef9SDimitry Andric if (getDwarfVersion() >= 5) { 32065ffd83dbSDimitry Andric unsigned Type = M.getMacinfoType() == dwarf::DW_MACINFO_define 32075ffd83dbSDimitry Andric ? dwarf::DW_MACRO_define_strx 32085ffd83dbSDimitry Andric : dwarf::DW_MACRO_undef_strx; 32095ffd83dbSDimitry Andric Asm->OutStreamer->AddComment(dwarf::MacroString(Type)); 32105ffd83dbSDimitry Andric Asm->emitULEB128(Type); 32115ffd83dbSDimitry Andric Asm->OutStreamer->AddComment("Line Number"); 32125ffd83dbSDimitry Andric Asm->emitULEB128(M.getLine()); 32135ffd83dbSDimitry Andric Asm->OutStreamer->AddComment("Macro String"); 3214e8d8bef9SDimitry Andric Asm->emitULEB128( 3215e8d8bef9SDimitry Andric InfoHolder.getStringPool().getIndexedEntry(*Asm, Str).getIndex()); 3216e8d8bef9SDimitry Andric } else { 3217e8d8bef9SDimitry Andric unsigned Type = M.getMacinfoType() == dwarf::DW_MACINFO_define 3218e8d8bef9SDimitry Andric ? dwarf::DW_MACRO_GNU_define_indirect 3219e8d8bef9SDimitry Andric : dwarf::DW_MACRO_GNU_undef_indirect; 3220e8d8bef9SDimitry Andric Asm->OutStreamer->AddComment(dwarf::GnuMacroString(Type)); 3221e8d8bef9SDimitry Andric Asm->emitULEB128(Type); 3222e8d8bef9SDimitry Andric Asm->OutStreamer->AddComment("Line Number"); 3223e8d8bef9SDimitry Andric Asm->emitULEB128(M.getLine()); 3224e8d8bef9SDimitry Andric Asm->OutStreamer->AddComment("Macro String"); 3225e8d8bef9SDimitry Andric Asm->emitDwarfSymbolReference( 3226e8d8bef9SDimitry Andric InfoHolder.getStringPool().getEntry(*Asm, Str).getSymbol()); 3227e8d8bef9SDimitry Andric } 32285ffd83dbSDimitry Andric } else { 32295ffd83dbSDimitry Andric Asm->OutStreamer->AddComment(dwarf::MacinfoString(M.getMacinfoType())); 32305ffd83dbSDimitry Andric Asm->emitULEB128(M.getMacinfoType()); 32315ffd83dbSDimitry Andric Asm->OutStreamer->AddComment("Line Number"); 32325ffd83dbSDimitry Andric Asm->emitULEB128(M.getLine()); 32335ffd83dbSDimitry Andric Asm->OutStreamer->AddComment("Macro String"); 3234e8d8bef9SDimitry Andric Asm->OutStreamer->emitBytes(Str); 3235*0b57cec5SDimitry Andric Asm->emitInt8('\0'); 3236*0b57cec5SDimitry Andric } 32375ffd83dbSDimitry Andric } 32385ffd83dbSDimitry Andric 32395ffd83dbSDimitry Andric void DwarfDebug::emitMacroFileImpl( 3240e8d8bef9SDimitry Andric DIMacroFile &MF, DwarfCompileUnit &U, unsigned StartFile, unsigned EndFile, 32415ffd83dbSDimitry Andric StringRef (*MacroFormToString)(unsigned Form)) { 32425ffd83dbSDimitry Andric 32435ffd83dbSDimitry Andric Asm->OutStreamer->AddComment(MacroFormToString(StartFile)); 32445ffd83dbSDimitry Andric Asm->emitULEB128(StartFile); 32455ffd83dbSDimitry Andric Asm->OutStreamer->AddComment("Line Number"); 3246e8d8bef9SDimitry Andric Asm->emitULEB128(MF.getLine()); 32475ffd83dbSDimitry Andric Asm->OutStreamer->AddComment("File Number"); 3248e8d8bef9SDimitry Andric DIFile &F = *MF.getFile(); 3249e8d8bef9SDimitry Andric if (useSplitDwarf()) 3250e8d8bef9SDimitry Andric Asm->emitULEB128(getDwoLineTable(U)->getFile( 3251e8d8bef9SDimitry Andric F.getDirectory(), F.getFilename(), getMD5AsBytes(&F), 3252e8d8bef9SDimitry Andric Asm->OutContext.getDwarfVersion(), F.getSource())); 3253e8d8bef9SDimitry Andric else 3254e8d8bef9SDimitry Andric Asm->emitULEB128(U.getOrCreateSourceID(&F)); 3255e8d8bef9SDimitry Andric handleMacroNodes(MF.getElements(), U); 32565ffd83dbSDimitry Andric Asm->OutStreamer->AddComment(MacroFormToString(EndFile)); 32575ffd83dbSDimitry Andric Asm->emitULEB128(EndFile); 32585ffd83dbSDimitry Andric } 3259*0b57cec5SDimitry Andric 3260*0b57cec5SDimitry Andric void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) { 32615ffd83dbSDimitry Andric // DWARFv5 macro and DWARFv4 macinfo share some common encodings, 32625ffd83dbSDimitry Andric // so for readibility/uniformity, We are explicitly emitting those. 3263*0b57cec5SDimitry Andric assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file); 3264e8d8bef9SDimitry Andric if (UseDebugMacroSection) 3265e8d8bef9SDimitry Andric emitMacroFileImpl( 3266e8d8bef9SDimitry Andric F, U, dwarf::DW_MACRO_start_file, dwarf::DW_MACRO_end_file, 3267e8d8bef9SDimitry Andric (getDwarfVersion() >= 5) ? dwarf::MacroString : dwarf::GnuMacroString); 32685ffd83dbSDimitry Andric else 32695ffd83dbSDimitry Andric emitMacroFileImpl(F, U, dwarf::DW_MACINFO_start_file, 32705ffd83dbSDimitry Andric dwarf::DW_MACINFO_end_file, dwarf::MacinfoString); 3271*0b57cec5SDimitry Andric } 3272*0b57cec5SDimitry Andric 3273480093f4SDimitry Andric void DwarfDebug::emitDebugMacinfoImpl(MCSection *Section) { 3274*0b57cec5SDimitry Andric for (const auto &P : CUMap) { 3275*0b57cec5SDimitry Andric auto &TheCU = *P.second; 3276*0b57cec5SDimitry Andric auto *SkCU = TheCU.getSkeleton(); 3277*0b57cec5SDimitry Andric DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; 3278*0b57cec5SDimitry Andric auto *CUNode = cast<DICompileUnit>(P.first); 3279*0b57cec5SDimitry Andric DIMacroNodeArray Macros = CUNode->getMacros(); 3280480093f4SDimitry Andric if (Macros.empty()) 3281480093f4SDimitry Andric continue; 328281ad6265SDimitry Andric Asm->OutStreamer->switchSection(Section); 32835ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(U.getMacroLabelBegin()); 3284e8d8bef9SDimitry Andric if (UseDebugMacroSection) 3285e8d8bef9SDimitry Andric emitMacroHeader(Asm, *this, U, getDwarfVersion()); 3286*0b57cec5SDimitry Andric handleMacroNodes(Macros, U); 3287*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("End Of Macro List Mark"); 3288*0b57cec5SDimitry Andric Asm->emitInt8(0); 3289*0b57cec5SDimitry Andric } 3290480093f4SDimitry Andric } 3291480093f4SDimitry Andric 32925ffd83dbSDimitry Andric /// Emit macros into a debug macinfo/macro section. 3293480093f4SDimitry Andric void DwarfDebug::emitDebugMacinfo() { 32945ffd83dbSDimitry Andric auto &ObjLower = Asm->getObjFileLowering(); 3295e8d8bef9SDimitry Andric emitDebugMacinfoImpl(UseDebugMacroSection 32965ffd83dbSDimitry Andric ? ObjLower.getDwarfMacroSection() 32975ffd83dbSDimitry Andric : ObjLower.getDwarfMacinfoSection()); 3298480093f4SDimitry Andric } 3299480093f4SDimitry Andric 3300480093f4SDimitry Andric void DwarfDebug::emitDebugMacinfoDWO() { 33015ffd83dbSDimitry Andric auto &ObjLower = Asm->getObjFileLowering(); 3302e8d8bef9SDimitry Andric emitDebugMacinfoImpl(UseDebugMacroSection 33035ffd83dbSDimitry Andric ? ObjLower.getDwarfMacroDWOSection() 33045ffd83dbSDimitry Andric : ObjLower.getDwarfMacinfoDWOSection()); 3305480093f4SDimitry Andric } 3306*0b57cec5SDimitry Andric 3307*0b57cec5SDimitry Andric // DWARF5 Experimental Separate Dwarf emitters. 3308*0b57cec5SDimitry Andric 3309*0b57cec5SDimitry Andric void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die, 3310*0b57cec5SDimitry Andric std::unique_ptr<DwarfCompileUnit> NewU) { 3311*0b57cec5SDimitry Andric 3312*0b57cec5SDimitry Andric if (!CompilationDir.empty()) 3313*0b57cec5SDimitry Andric NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); 3314*0b57cec5SDimitry Andric addGnuPubAttributes(*NewU, Die); 3315*0b57cec5SDimitry Andric 3316*0b57cec5SDimitry Andric SkeletonHolder.addUnit(std::move(NewU)); 3317*0b57cec5SDimitry Andric } 3318*0b57cec5SDimitry Andric 3319*0b57cec5SDimitry Andric DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) { 3320*0b57cec5SDimitry Andric 33218bcb0991SDimitry Andric auto OwnedUnit = std::make_unique<DwarfCompileUnit>( 3322480093f4SDimitry Andric CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder, 3323480093f4SDimitry Andric UnitKind::Skeleton); 3324*0b57cec5SDimitry Andric DwarfCompileUnit &NewCU = *OwnedUnit; 3325*0b57cec5SDimitry Andric NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); 3326*0b57cec5SDimitry Andric 3327*0b57cec5SDimitry Andric NewCU.initStmtList(); 3328*0b57cec5SDimitry Andric 3329*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable()) 3330*0b57cec5SDimitry Andric NewCU.addStringOffsetsStart(); 3331*0b57cec5SDimitry Andric 3332*0b57cec5SDimitry Andric initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit)); 3333*0b57cec5SDimitry Andric 3334*0b57cec5SDimitry Andric return NewCU; 3335*0b57cec5SDimitry Andric } 3336*0b57cec5SDimitry Andric 3337*0b57cec5SDimitry Andric // Emit the .debug_info.dwo section for separated dwarf. This contains the 3338*0b57cec5SDimitry Andric // compile units that would normally be in debug_info. 3339*0b57cec5SDimitry Andric void DwarfDebug::emitDebugInfoDWO() { 3340*0b57cec5SDimitry Andric assert(useSplitDwarf() && "No split dwarf debug info?"); 3341*0b57cec5SDimitry Andric // Don't emit relocations into the dwo file. 3342*0b57cec5SDimitry Andric InfoHolder.emitUnits(/* UseOffsets */ true); 3343*0b57cec5SDimitry Andric } 3344*0b57cec5SDimitry Andric 3345*0b57cec5SDimitry Andric // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the 3346*0b57cec5SDimitry Andric // abbreviations for the .debug_info.dwo section. 3347*0b57cec5SDimitry Andric void DwarfDebug::emitDebugAbbrevDWO() { 3348*0b57cec5SDimitry Andric assert(useSplitDwarf() && "No split dwarf?"); 3349*0b57cec5SDimitry Andric InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection()); 3350*0b57cec5SDimitry Andric } 3351*0b57cec5SDimitry Andric 3352*0b57cec5SDimitry Andric void DwarfDebug::emitDebugLineDWO() { 3353*0b57cec5SDimitry Andric assert(useSplitDwarf() && "No split dwarf?"); 3354*0b57cec5SDimitry Andric SplitTypeUnitFileTable.Emit( 3355*0b57cec5SDimitry Andric *Asm->OutStreamer, MCDwarfLineTableParams(), 3356*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfLineDWOSection()); 3357*0b57cec5SDimitry Andric } 3358*0b57cec5SDimitry Andric 3359*0b57cec5SDimitry Andric void DwarfDebug::emitStringOffsetsTableHeaderDWO() { 3360*0b57cec5SDimitry Andric assert(useSplitDwarf() && "No split dwarf?"); 3361*0b57cec5SDimitry Andric InfoHolder.getStringPool().emitStringOffsetsTableHeader( 3362*0b57cec5SDimitry Andric *Asm, Asm->getObjFileLowering().getDwarfStrOffDWOSection(), 3363*0b57cec5SDimitry Andric InfoHolder.getStringOffsetsStartSym()); 3364*0b57cec5SDimitry Andric } 3365*0b57cec5SDimitry Andric 3366*0b57cec5SDimitry Andric // Emit the .debug_str.dwo section for separated dwarf. This contains the 3367*0b57cec5SDimitry Andric // string section and is identical in format to traditional .debug_str 3368*0b57cec5SDimitry Andric // sections. 3369*0b57cec5SDimitry Andric void DwarfDebug::emitDebugStrDWO() { 3370*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable()) 3371*0b57cec5SDimitry Andric emitStringOffsetsTableHeaderDWO(); 3372*0b57cec5SDimitry Andric assert(useSplitDwarf() && "No split dwarf?"); 3373*0b57cec5SDimitry Andric MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection(); 3374*0b57cec5SDimitry Andric InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(), 3375*0b57cec5SDimitry Andric OffSec, /* UseRelativeOffsets = */ false); 3376*0b57cec5SDimitry Andric } 3377*0b57cec5SDimitry Andric 3378*0b57cec5SDimitry Andric // Emit address pool. 3379*0b57cec5SDimitry Andric void DwarfDebug::emitDebugAddr() { 3380*0b57cec5SDimitry Andric AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection()); 3381*0b57cec5SDimitry Andric } 3382*0b57cec5SDimitry Andric 3383*0b57cec5SDimitry Andric MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) { 3384*0b57cec5SDimitry Andric if (!useSplitDwarf()) 3385*0b57cec5SDimitry Andric return nullptr; 3386*0b57cec5SDimitry Andric const DICompileUnit *DIUnit = CU.getCUNode(); 3387*0b57cec5SDimitry Andric SplitTypeUnitFileTable.maybeSetRootFile( 3388*0b57cec5SDimitry Andric DIUnit->getDirectory(), DIUnit->getFilename(), 3389e8d8bef9SDimitry Andric getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource()); 3390*0b57cec5SDimitry Andric return &SplitTypeUnitFileTable; 3391*0b57cec5SDimitry Andric } 3392*0b57cec5SDimitry Andric 3393*0b57cec5SDimitry Andric uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) { 3394*0b57cec5SDimitry Andric MD5 Hash; 3395*0b57cec5SDimitry Andric Hash.update(Identifier); 3396*0b57cec5SDimitry Andric // ... take the least significant 8 bytes and return those. Our MD5 3397*0b57cec5SDimitry Andric // implementation always returns its results in little endian, so we actually 3398*0b57cec5SDimitry Andric // need the "high" word. 3399*0b57cec5SDimitry Andric MD5::MD5Result Result; 3400*0b57cec5SDimitry Andric Hash.final(Result); 3401*0b57cec5SDimitry Andric return Result.high(); 3402*0b57cec5SDimitry Andric } 3403*0b57cec5SDimitry Andric 3404*0b57cec5SDimitry Andric void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU, 3405*0b57cec5SDimitry Andric StringRef Identifier, DIE &RefDie, 3406*0b57cec5SDimitry Andric const DICompositeType *CTy) { 3407*0b57cec5SDimitry Andric // Fast path if we're building some type units and one has already used the 3408*0b57cec5SDimitry Andric // address pool we know we're going to throw away all this work anyway, so 3409*0b57cec5SDimitry Andric // don't bother building dependent types. 34101fd87a68SDimitry Andric if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed()) 3411*0b57cec5SDimitry Andric return; 3412*0b57cec5SDimitry Andric 3413*0b57cec5SDimitry Andric auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0)); 3414*0b57cec5SDimitry Andric if (!Ins.second) { 3415*0b57cec5SDimitry Andric CU.addDIETypeSignature(RefDie, Ins.first->second); 3416*0b57cec5SDimitry Andric return; 3417*0b57cec5SDimitry Andric } 3418*0b57cec5SDimitry Andric 3419*0b57cec5SDimitry Andric bool TopLevelType = TypeUnitsUnderConstruction.empty(); 3420*0b57cec5SDimitry Andric AddrPool.resetUsedFlag(); 3421*0b57cec5SDimitry Andric 34228bcb0991SDimitry Andric auto OwnedUnit = std::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder, 3423*0b57cec5SDimitry Andric getDwoLineTable(CU)); 3424*0b57cec5SDimitry Andric DwarfTypeUnit &NewTU = *OwnedUnit; 3425*0b57cec5SDimitry Andric DIE &UnitDie = NewTU.getUnitDie(); 3426*0b57cec5SDimitry Andric TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy); 3427*0b57cec5SDimitry Andric 3428*0b57cec5SDimitry Andric NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2, 3429*0b57cec5SDimitry Andric CU.getLanguage()); 3430*0b57cec5SDimitry Andric 3431*0b57cec5SDimitry Andric uint64_t Signature = makeTypeSignature(Identifier); 3432*0b57cec5SDimitry Andric NewTU.setTypeSignature(Signature); 3433*0b57cec5SDimitry Andric Ins.first->second = Signature; 3434*0b57cec5SDimitry Andric 3435*0b57cec5SDimitry Andric if (useSplitDwarf()) { 3436*0b57cec5SDimitry Andric MCSection *Section = 3437*0b57cec5SDimitry Andric getDwarfVersion() <= 4 3438*0b57cec5SDimitry Andric ? Asm->getObjFileLowering().getDwarfTypesDWOSection() 3439*0b57cec5SDimitry Andric : Asm->getObjFileLowering().getDwarfInfoDWOSection(); 3440*0b57cec5SDimitry Andric NewTU.setSection(Section); 3441*0b57cec5SDimitry Andric } else { 3442*0b57cec5SDimitry Andric MCSection *Section = 3443*0b57cec5SDimitry Andric getDwarfVersion() <= 4 3444*0b57cec5SDimitry Andric ? Asm->getObjFileLowering().getDwarfTypesSection(Signature) 3445*0b57cec5SDimitry Andric : Asm->getObjFileLowering().getDwarfInfoSection(Signature); 3446*0b57cec5SDimitry Andric NewTU.setSection(Section); 3447*0b57cec5SDimitry Andric // Non-split type units reuse the compile unit's line table. 3448*0b57cec5SDimitry Andric CU.applyStmtList(UnitDie); 3449*0b57cec5SDimitry Andric } 3450*0b57cec5SDimitry Andric 3451*0b57cec5SDimitry Andric // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type 3452*0b57cec5SDimitry Andric // units. 3453*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable() && !useSplitDwarf()) 3454*0b57cec5SDimitry Andric NewTU.addStringOffsetsStart(); 3455*0b57cec5SDimitry Andric 3456*0b57cec5SDimitry Andric NewTU.setType(NewTU.createTypeDIE(CTy)); 3457*0b57cec5SDimitry Andric 3458*0b57cec5SDimitry Andric if (TopLevelType) { 3459*0b57cec5SDimitry Andric auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction); 3460*0b57cec5SDimitry Andric TypeUnitsUnderConstruction.clear(); 3461*0b57cec5SDimitry Andric 3462*0b57cec5SDimitry Andric // Types referencing entries in the address table cannot be placed in type 3463*0b57cec5SDimitry Andric // units. 34641fd87a68SDimitry Andric if (AddrPool.hasBeenUsed()) { 3465*0b57cec5SDimitry Andric 3466*0b57cec5SDimitry Andric // Remove all the types built while building this type. 3467*0b57cec5SDimitry Andric // This is pessimistic as some of these types might not be dependent on 3468*0b57cec5SDimitry Andric // the type that used an address. 3469*0b57cec5SDimitry Andric for (const auto &TU : TypeUnitsToAdd) 3470*0b57cec5SDimitry Andric TypeSignatures.erase(TU.second); 3471*0b57cec5SDimitry Andric 3472*0b57cec5SDimitry Andric // Construct this type in the CU directly. 3473*0b57cec5SDimitry Andric // This is inefficient because all the dependent types will be rebuilt 3474*0b57cec5SDimitry Andric // from scratch, including building them in type units, discovering that 3475*0b57cec5SDimitry Andric // they depend on addresses, throwing them out and rebuilding them. 3476*0b57cec5SDimitry Andric CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy)); 3477*0b57cec5SDimitry Andric return; 3478*0b57cec5SDimitry Andric } 3479*0b57cec5SDimitry Andric 3480*0b57cec5SDimitry Andric // If the type wasn't dependent on fission addresses, finish adding the type 3481*0b57cec5SDimitry Andric // and all its dependent types. 3482*0b57cec5SDimitry Andric for (auto &TU : TypeUnitsToAdd) { 3483*0b57cec5SDimitry Andric InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get()); 3484*0b57cec5SDimitry Andric InfoHolder.emitUnit(TU.first.get(), useSplitDwarf()); 3485*0b57cec5SDimitry Andric } 3486*0b57cec5SDimitry Andric } 3487*0b57cec5SDimitry Andric CU.addDIETypeSignature(RefDie, Signature); 3488*0b57cec5SDimitry Andric } 3489*0b57cec5SDimitry Andric 3490*0b57cec5SDimitry Andric // Add the Name along with its companion DIE to the appropriate accelerator 3491*0b57cec5SDimitry Andric // table (for AccelTableKind::Dwarf it's always AccelDebugNames, for 3492*0b57cec5SDimitry Andric // AccelTableKind::Apple, we use the table we got as an argument). If 3493*0b57cec5SDimitry Andric // accelerator tables are disabled, this function does nothing. 3494*0b57cec5SDimitry Andric template <typename DataT> 3495*0b57cec5SDimitry Andric void DwarfDebug::addAccelNameImpl(const DICompileUnit &CU, 3496*0b57cec5SDimitry Andric AccelTable<DataT> &AppleAccel, StringRef Name, 3497*0b57cec5SDimitry Andric const DIE &Die) { 3498*0b57cec5SDimitry Andric if (getAccelTableKind() == AccelTableKind::None) 3499*0b57cec5SDimitry Andric return; 3500*0b57cec5SDimitry Andric 3501*0b57cec5SDimitry Andric if (getAccelTableKind() != AccelTableKind::Apple && 3502*0b57cec5SDimitry Andric CU.getNameTableKind() != DICompileUnit::DebugNameTableKind::Default) 3503*0b57cec5SDimitry Andric return; 3504*0b57cec5SDimitry Andric 3505*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 3506*0b57cec5SDimitry Andric DwarfStringPoolEntryRef Ref = Holder.getStringPool().getEntry(*Asm, Name); 3507*0b57cec5SDimitry Andric 3508*0b57cec5SDimitry Andric switch (getAccelTableKind()) { 3509*0b57cec5SDimitry Andric case AccelTableKind::Apple: 3510*0b57cec5SDimitry Andric AppleAccel.addName(Ref, Die); 3511*0b57cec5SDimitry Andric break; 3512*0b57cec5SDimitry Andric case AccelTableKind::Dwarf: 3513*0b57cec5SDimitry Andric AccelDebugNames.addName(Ref, Die); 3514*0b57cec5SDimitry Andric break; 3515*0b57cec5SDimitry Andric case AccelTableKind::Default: 3516*0b57cec5SDimitry Andric llvm_unreachable("Default should have already been resolved."); 3517*0b57cec5SDimitry Andric case AccelTableKind::None: 3518*0b57cec5SDimitry Andric llvm_unreachable("None handled above"); 3519*0b57cec5SDimitry Andric } 3520*0b57cec5SDimitry Andric } 3521*0b57cec5SDimitry Andric 3522*0b57cec5SDimitry Andric void DwarfDebug::addAccelName(const DICompileUnit &CU, StringRef Name, 3523*0b57cec5SDimitry Andric const DIE &Die) { 3524*0b57cec5SDimitry Andric addAccelNameImpl(CU, AccelNames, Name, Die); 3525*0b57cec5SDimitry Andric } 3526*0b57cec5SDimitry Andric 3527*0b57cec5SDimitry Andric void DwarfDebug::addAccelObjC(const DICompileUnit &CU, StringRef Name, 3528*0b57cec5SDimitry Andric const DIE &Die) { 3529*0b57cec5SDimitry Andric // ObjC names go only into the Apple accelerator tables. 3530*0b57cec5SDimitry Andric if (getAccelTableKind() == AccelTableKind::Apple) 3531*0b57cec5SDimitry Andric addAccelNameImpl(CU, AccelObjC, Name, Die); 3532*0b57cec5SDimitry Andric } 3533*0b57cec5SDimitry Andric 3534*0b57cec5SDimitry Andric void DwarfDebug::addAccelNamespace(const DICompileUnit &CU, StringRef Name, 3535*0b57cec5SDimitry Andric const DIE &Die) { 3536*0b57cec5SDimitry Andric addAccelNameImpl(CU, AccelNamespace, Name, Die); 3537*0b57cec5SDimitry Andric } 3538*0b57cec5SDimitry Andric 3539*0b57cec5SDimitry Andric void DwarfDebug::addAccelType(const DICompileUnit &CU, StringRef Name, 3540*0b57cec5SDimitry Andric const DIE &Die, char Flags) { 3541*0b57cec5SDimitry Andric addAccelNameImpl(CU, AccelTypes, Name, Die); 3542*0b57cec5SDimitry Andric } 3543*0b57cec5SDimitry Andric 3544*0b57cec5SDimitry Andric uint16_t DwarfDebug::getDwarfVersion() const { 3545*0b57cec5SDimitry Andric return Asm->OutStreamer->getContext().getDwarfVersion(); 3546*0b57cec5SDimitry Andric } 3547*0b57cec5SDimitry Andric 3548e8d8bef9SDimitry Andric dwarf::Form DwarfDebug::getDwarfSectionOffsetForm() const { 3549e8d8bef9SDimitry Andric if (Asm->getDwarfVersion() >= 4) 3550e8d8bef9SDimitry Andric return dwarf::Form::DW_FORM_sec_offset; 3551e8d8bef9SDimitry Andric assert((!Asm->isDwarf64() || (Asm->getDwarfVersion() == 3)) && 3552e8d8bef9SDimitry Andric "DWARF64 is not defined prior DWARFv3"); 3553e8d8bef9SDimitry Andric return Asm->isDwarf64() ? dwarf::Form::DW_FORM_data8 3554e8d8bef9SDimitry Andric : dwarf::Form::DW_FORM_data4; 3555e8d8bef9SDimitry Andric } 3556e8d8bef9SDimitry Andric 3557*0b57cec5SDimitry Andric const MCSymbol *DwarfDebug::getSectionLabel(const MCSection *S) { 3558fe6060f1SDimitry Andric auto I = SectionLabels.find(S); 3559fe6060f1SDimitry Andric if (I == SectionLabels.end()) 3560fe6060f1SDimitry Andric return nullptr; 3561fe6060f1SDimitry Andric return I->second; 3562*0b57cec5SDimitry Andric } 35635ffd83dbSDimitry Andric void DwarfDebug::insertSectionLabel(const MCSymbol *S) { 35645ffd83dbSDimitry Andric if (SectionLabels.insert(std::make_pair(&S->getSection(), S)).second) 35655ffd83dbSDimitry Andric if (useSplitDwarf() || getDwarfVersion() >= 5) 35665ffd83dbSDimitry Andric AddrPool.getIndex(S); 35675ffd83dbSDimitry Andric } 3568e8d8bef9SDimitry Andric 3569bdd1243dSDimitry Andric std::optional<MD5::MD5Result> 3570bdd1243dSDimitry Andric DwarfDebug::getMD5AsBytes(const DIFile *File) const { 3571e8d8bef9SDimitry Andric assert(File); 3572e8d8bef9SDimitry Andric if (getDwarfVersion() < 5) 3573bdd1243dSDimitry Andric return std::nullopt; 3574bdd1243dSDimitry Andric std::optional<DIFile::ChecksumInfo<StringRef>> Checksum = File->getChecksum(); 3575e8d8bef9SDimitry Andric if (!Checksum || Checksum->Kind != DIFile::CSK_MD5) 3576bdd1243dSDimitry Andric return std::nullopt; 3577e8d8bef9SDimitry Andric 3578e8d8bef9SDimitry Andric // Convert the string checksum to an MD5Result for the streamer. 3579e8d8bef9SDimitry Andric // The verifier validates the checksum so we assume it's okay. 3580e8d8bef9SDimitry Andric // An MD5 checksum is 16 bytes. 3581e8d8bef9SDimitry Andric std::string ChecksumString = fromHex(Checksum->Value); 3582e8d8bef9SDimitry Andric MD5::MD5Result CKMem; 358381ad6265SDimitry Andric std::copy(ChecksumString.begin(), ChecksumString.end(), CKMem.data()); 3584e8d8bef9SDimitry Andric return CKMem; 3585e8d8bef9SDimitry Andric } 3586