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