| 1 | //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- 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 | // Common functionality for different debug information format backends. |
| 10 | // LLVM currently supports DWARF and CodeView. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/DebugHandlerBase.h" |
| 15 | #include "llvm/CodeGen/AsmPrinter.h" |
| 16 | #include "llvm/CodeGen/MachineFunction.h" |
| 17 | #include "llvm/CodeGen/MachineInstr.h" |
| 18 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 19 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 20 | #include "llvm/IR/DebugInfo.h" |
| 21 | #include "llvm/IR/Module.h" |
| 22 | #include "llvm/MC/MCStreamer.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | #define DEBUG_TYPE "dwarfdebug" |
| 28 | |
| 29 | /// If true, we drop variable location ranges which exist entirely outside the |
| 30 | /// variable's lexical scope instruction ranges. |
| 31 | static cl::opt<bool> TrimVarLocs("trim-var-locs" , cl::Hidden, cl::init(Val: true)); |
| 32 | |
| 33 | std::optional<DbgVariableLocation> |
| 34 | DbgVariableLocation::( |
| 35 | const MachineInstr &Instruction) { |
| 36 | DbgVariableLocation Location; |
| 37 | // Variables calculated from multiple locations can't be represented here. |
| 38 | if (Instruction.getNumDebugOperands() != 1) |
| 39 | return std::nullopt; |
| 40 | if (!Instruction.getDebugOperand(Index: 0).isReg()) |
| 41 | return std::nullopt; |
| 42 | Location.Register = Instruction.getDebugOperand(Index: 0).getReg().asMCReg(); |
| 43 | Location.FragmentInfo.reset(); |
| 44 | // We only handle expressions generated by DIExpression::appendOffset, |
| 45 | // which doesn't require a full stack machine. |
| 46 | int64_t Offset = 0; |
| 47 | const DIExpression *DIExpr = Instruction.getDebugExpression(); |
| 48 | auto Op = DIExpr->expr_op_begin(); |
| 49 | // We can handle a DBG_VALUE_LIST iff it has exactly one location operand that |
| 50 | // appears exactly once at the start of the expression. |
| 51 | if (Instruction.isDebugValueList()) { |
| 52 | if (Instruction.getNumDebugOperands() == 1 && |
| 53 | Op->getOp() == dwarf::DW_OP_LLVM_arg) |
| 54 | ++Op; |
| 55 | else |
| 56 | return std::nullopt; |
| 57 | } |
| 58 | while (Op != DIExpr->expr_op_end()) { |
| 59 | switch (Op->getOp()) { |
| 60 | case dwarf::DW_OP_constu: { |
| 61 | int Value = Op->getArg(I: 0); |
| 62 | ++Op; |
| 63 | if (Op != DIExpr->expr_op_end()) { |
| 64 | switch (Op->getOp()) { |
| 65 | case dwarf::DW_OP_minus: |
| 66 | Offset -= Value; |
| 67 | break; |
| 68 | case dwarf::DW_OP_plus: |
| 69 | Offset += Value; |
| 70 | break; |
| 71 | default: |
| 72 | continue; |
| 73 | } |
| 74 | } |
| 75 | } break; |
| 76 | case dwarf::DW_OP_plus_uconst: |
| 77 | Offset += Op->getArg(I: 0); |
| 78 | break; |
| 79 | case dwarf::DW_OP_LLVM_fragment: |
| 80 | Location.FragmentInfo = {Op->getArg(I: 1), Op->getArg(I: 0)}; |
| 81 | break; |
| 82 | case dwarf::DW_OP_deref: |
| 83 | Location.LoadChain.push_back(Elt: Offset); |
| 84 | Offset = 0; |
| 85 | break; |
| 86 | default: |
| 87 | return std::nullopt; |
| 88 | } |
| 89 | ++Op; |
| 90 | } |
| 91 | |
| 92 | // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE |
| 93 | // instruction. |
| 94 | // FIXME: Replace these with DIExpression. |
| 95 | if (Instruction.isIndirectDebugValue()) |
| 96 | Location.LoadChain.push_back(Elt: Offset); |
| 97 | |
| 98 | return Location; |
| 99 | } |
| 100 | |
| 101 | DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} |
| 102 | |
| 103 | DebugHandlerBase::~DebugHandlerBase() = default; |
| 104 | |
| 105 | void DebugHandlerBase::beginModule(Module *M) { |
| 106 | if (M->debug_compile_units().empty()) |
| 107 | Asm = nullptr; |
| 108 | } |
| 109 | |
| 110 | // Each LexicalScope has first instruction and last instruction to mark |
| 111 | // beginning and end of a scope respectively. Create an inverse map that list |
| 112 | // scopes starts (and ends) with an instruction. One instruction may start (or |
| 113 | // end) multiple scopes. Ignore scopes that are not reachable. |
| 114 | void DebugHandlerBase::identifyScopeMarkers() { |
| 115 | SmallVector<LexicalScope *, 4> WorkList; |
| 116 | WorkList.push_back(Elt: LScopes.getCurrentFunctionScope()); |
| 117 | while (!WorkList.empty()) { |
| 118 | LexicalScope *S = WorkList.pop_back_val(); |
| 119 | |
| 120 | const SmallVectorImpl<LexicalScope *> &Children = S->getChildren(); |
| 121 | if (!Children.empty()) |
| 122 | WorkList.append(in_start: Children.begin(), in_end: Children.end()); |
| 123 | |
| 124 | if (S->isAbstractScope()) |
| 125 | continue; |
| 126 | |
| 127 | for (const InsnRange &R : S->getRanges()) { |
| 128 | assert(R.first && "InsnRange does not have first instruction!" ); |
| 129 | assert(R.second && "InsnRange does not have second instruction!" ); |
| 130 | requestLabelBeforeInsn(MI: R.first); |
| 131 | requestLabelAfterInsn(MI: R.second); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // Return Label preceding the instruction. |
| 137 | MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) { |
| 138 | MCSymbol *Label = LabelsBeforeInsn.lookup(Val: MI); |
| 139 | assert(Label && "Didn't insert label before instruction" ); |
| 140 | return Label; |
| 141 | } |
| 142 | |
| 143 | // Return Label immediately following the instruction. |
| 144 | MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) { |
| 145 | return LabelsAfterInsn.lookup(Val: MI); |
| 146 | } |
| 147 | |
| 148 | /// If this type is derived from a base type then return base type size. |
| 149 | uint64_t DebugHandlerBase::getBaseTypeSize(const DIType *Ty) { |
| 150 | assert(Ty); |
| 151 | |
| 152 | unsigned Tag = Ty->getTag(); |
| 153 | |
| 154 | if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && |
| 155 | Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && |
| 156 | Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type && |
| 157 | Tag != dwarf::DW_TAG_immutable_type && |
| 158 | Tag != dwarf::DW_TAG_template_alias) |
| 159 | return Ty->getSizeInBits(); |
| 160 | |
| 161 | DIType *BaseType = nullptr; |
| 162 | if (const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Val: Ty)) |
| 163 | BaseType = DDTy->getBaseType(); |
| 164 | else if (const DISubrangeType *SRTy = dyn_cast<DISubrangeType>(Val: Ty)) |
| 165 | BaseType = SRTy->getBaseType(); |
| 166 | |
| 167 | if (!BaseType) |
| 168 | return 0; |
| 169 | |
| 170 | // If this is a derived type, go ahead and get the base type, unless it's a |
| 171 | // reference then it's just the size of the field. Pointer types have no need |
| 172 | // of this since they're a different type of qualification on the type. |
| 173 | if (BaseType->getTag() == dwarf::DW_TAG_reference_type || |
| 174 | BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type) |
| 175 | return Ty->getSizeInBits(); |
| 176 | |
| 177 | return getBaseTypeSize(Ty: BaseType); |
| 178 | } |
| 179 | |
| 180 | bool DebugHandlerBase::isUnsignedDIType(const DIType *Ty) { |
| 181 | if (isa<DIStringType>(Val: Ty)) { |
| 182 | // Some transformations (e.g. instcombine) may decide to turn a Fortran |
| 183 | // character object into an integer, and later ones (e.g. SROA) may |
| 184 | // further inject a constant integer in a llvm.dbg.value call to track |
| 185 | // the object's value. Here we trust the transformations are doing the |
| 186 | // right thing, and treat the constant as unsigned to preserve that value |
| 187 | // (i.e. avoid sign extension). |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | if (auto *SRTy = dyn_cast<DISubrangeType>(Val: Ty)) { |
| 192 | Ty = SRTy->getBaseType(); |
| 193 | if (!Ty) |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | if (auto *CTy = dyn_cast<DICompositeType>(Val: Ty)) { |
| 198 | if (CTy->getTag() == dwarf::DW_TAG_enumeration_type) { |
| 199 | if (!(Ty = CTy->getBaseType())) |
| 200 | // FIXME: Enums without a fixed underlying type have unknown signedness |
| 201 | // here, leading to incorrectly emitted constants. |
| 202 | return false; |
| 203 | } else |
| 204 | // (Pieces of) aggregate types that get hacked apart by SROA may be |
| 205 | // represented by a constant. Encode them as unsigned bytes. |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | if (auto *DTy = dyn_cast<DIDerivedType>(Val: Ty)) { |
| 210 | dwarf::Tag T = (dwarf::Tag)Ty->getTag(); |
| 211 | // Encode pointer constants as unsigned bytes. This is used at least for |
| 212 | // null pointer constant emission. |
| 213 | // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed |
| 214 | // here, but accept them for now due to a bug in SROA producing bogus |
| 215 | // dbg.values. |
| 216 | if (T == dwarf::DW_TAG_pointer_type || |
| 217 | T == dwarf::DW_TAG_ptr_to_member_type || |
| 218 | T == dwarf::DW_TAG_reference_type || |
| 219 | T == dwarf::DW_TAG_rvalue_reference_type) |
| 220 | return true; |
| 221 | assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type || |
| 222 | T == dwarf::DW_TAG_volatile_type || |
| 223 | T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type || |
| 224 | T == dwarf::DW_TAG_immutable_type || |
| 225 | T == dwarf::DW_TAG_template_alias); |
| 226 | assert(DTy->getBaseType() && "Expected valid base type" ); |
| 227 | return isUnsignedDIType(Ty: DTy->getBaseType()); |
| 228 | } |
| 229 | |
| 230 | auto *BTy = cast<DIBasicType>(Val: Ty); |
| 231 | unsigned Encoding = BTy->getEncoding(); |
| 232 | assert((Encoding == dwarf::DW_ATE_unsigned || |
| 233 | Encoding == dwarf::DW_ATE_unsigned_char || |
| 234 | Encoding == dwarf::DW_ATE_signed || |
| 235 | Encoding == dwarf::DW_ATE_signed_char || |
| 236 | Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF || |
| 237 | Encoding == dwarf::DW_ATE_boolean || |
| 238 | Encoding == dwarf::DW_ATE_complex_float || |
| 239 | Encoding == dwarf::DW_ATE_signed_fixed || |
| 240 | Encoding == dwarf::DW_ATE_unsigned_fixed || |
| 241 | (Ty->getTag() == dwarf::DW_TAG_unspecified_type && |
| 242 | Ty->getName() == "decltype(nullptr)" )) && |
| 243 | "Unsupported encoding" ); |
| 244 | return Encoding == dwarf::DW_ATE_unsigned || |
| 245 | Encoding == dwarf::DW_ATE_unsigned_char || |
| 246 | Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean || |
| 247 | Encoding == llvm::dwarf::DW_ATE_unsigned_fixed || |
| 248 | Ty->getTag() == dwarf::DW_TAG_unspecified_type; |
| 249 | } |
| 250 | |
| 251 | static bool hasDebugInfo(const MachineFunction *MF) { |
| 252 | auto *SP = MF->getFunction().getSubprogram(); |
| 253 | if (!SP) |
| 254 | return false; |
| 255 | assert(SP->getUnit()); |
| 256 | auto EK = SP->getUnit()->getEmissionKind(); |
| 257 | if (EK == DICompileUnit::NoDebug) |
| 258 | return false; |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | void DebugHandlerBase::beginFunction(const MachineFunction *MF) { |
| 263 | PrevInstBB = nullptr; |
| 264 | |
| 265 | if (!Asm || !hasDebugInfo(MF)) { |
| 266 | skippedNonDebugFunction(); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | // Grab the lexical scopes for the function, if we don't have any of those |
| 271 | // then we're not going to be able to do anything. |
| 272 | LScopes.initialize(*MF); |
| 273 | if (LScopes.empty()) { |
| 274 | beginFunctionImpl(MF); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | // Make sure that each lexical scope will have a begin/end label. |
| 279 | identifyScopeMarkers(); |
| 280 | |
| 281 | // Calculate history for local variables. |
| 282 | assert(DbgValues.empty() && "DbgValues map wasn't cleaned!" ); |
| 283 | assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!" ); |
| 284 | calculateDbgEntityHistory(MF, TRI: Asm->MF->getSubtarget().getRegisterInfo(), |
| 285 | DbgValues, DbgLabels); |
| 286 | InstOrdering.initialize(MF: *MF); |
| 287 | if (TrimVarLocs) |
| 288 | DbgValues.trimLocationRanges(MF: *MF, LScopes, Ordering: InstOrdering); |
| 289 | LLVM_DEBUG(DbgValues.dump(MF->getName())); |
| 290 | |
| 291 | // Request labels for the full history. |
| 292 | for (const auto &I : DbgValues) { |
| 293 | const auto &Entries = I.second; |
| 294 | if (Entries.empty()) |
| 295 | continue; |
| 296 | |
| 297 | auto IsDescribedByReg = [](const MachineInstr *MI) { |
| 298 | return any_of(Range: MI->debug_operands(), |
| 299 | P: [](auto &MO) { return MO.isReg() && MO.getReg(); }); |
| 300 | }; |
| 301 | |
| 302 | // The first mention of a function argument gets the CurrentFnBegin label, |
| 303 | // so arguments are visible when breaking at function entry. |
| 304 | // |
| 305 | // We do not change the label for values that are described by registers, |
| 306 | // as that could place them above their defining instructions. We should |
| 307 | // ideally not change the labels for constant debug values either, since |
| 308 | // doing that violates the ranges that are calculated in the history map. |
| 309 | // However, we currently do not emit debug values for constant arguments |
| 310 | // directly at the start of the function, so this code is still useful. |
| 311 | const DILocalVariable *DIVar = |
| 312 | Entries.front().getInstr()->getDebugVariable(); |
| 313 | if (DIVar->isParameter() && |
| 314 | getDISubprogram(Scope: DIVar->getScope())->describes(F: &MF->getFunction())) { |
| 315 | if (!IsDescribedByReg(Entries.front().getInstr())) |
| 316 | LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin(); |
| 317 | if (Entries.front().getInstr()->getDebugExpression()->isFragment()) { |
| 318 | // Mark all non-overlapping initial fragments. |
| 319 | for (const auto *I = Entries.begin(); I != Entries.end(); ++I) { |
| 320 | if (!I->isDbgValue()) |
| 321 | continue; |
| 322 | const DIExpression *Fragment = I->getInstr()->getDebugExpression(); |
| 323 | if (std::any_of(first: Entries.begin(), last: I, |
| 324 | pred: [&](DbgValueHistoryMap::Entry Pred) { |
| 325 | return Pred.isDbgValue() && |
| 326 | Fragment->fragmentsOverlap( |
| 327 | Other: Pred.getInstr()->getDebugExpression()); |
| 328 | })) |
| 329 | break; |
| 330 | // The code that generates location lists for DWARF assumes that the |
| 331 | // entries' start labels are monotonically increasing, and since we |
| 332 | // don't change the label for fragments that are described by |
| 333 | // registers, we must bail out when encountering such a fragment. |
| 334 | if (IsDescribedByReg(I->getInstr())) |
| 335 | break; |
| 336 | LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin(); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | for (const auto &Entry : Entries) { |
| 342 | if (Entry.isDbgValue()) |
| 343 | requestLabelBeforeInsn(MI: Entry.getInstr()); |
| 344 | else |
| 345 | requestLabelAfterInsn(MI: Entry.getInstr()); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // Ensure there is a symbol before DBG_LABEL. |
| 350 | for (const auto &I : DbgLabels) { |
| 351 | const MachineInstr *MI = I.second; |
| 352 | requestLabelBeforeInsn(MI); |
| 353 | } |
| 354 | |
| 355 | PrevInstLoc = DebugLoc(); |
| 356 | PrevLabel = Asm->getFunctionBegin(); |
| 357 | beginFunctionImpl(MF); |
| 358 | } |
| 359 | |
| 360 | void DebugHandlerBase::beginInstruction(const MachineInstr *MI) { |
| 361 | if (!Asm || !Asm->hasDebugInfo()) |
| 362 | return; |
| 363 | |
| 364 | assert(CurMI == nullptr); |
| 365 | CurMI = MI; |
| 366 | |
| 367 | // Insert labels where requested. |
| 368 | DenseMap<const MachineInstr *, MCSymbol *>::iterator I = |
| 369 | LabelsBeforeInsn.find(Val: MI); |
| 370 | |
| 371 | // No label needed. |
| 372 | if (I == LabelsBeforeInsn.end()) |
| 373 | return; |
| 374 | |
| 375 | // Label already assigned. |
| 376 | if (I->second) |
| 377 | return; |
| 378 | |
| 379 | if (!PrevLabel) { |
| 380 | PrevLabel = MMI->getContext().createTempSymbol(); |
| 381 | Asm->OutStreamer->emitLabel(Symbol: PrevLabel); |
| 382 | } |
| 383 | I->second = PrevLabel; |
| 384 | } |
| 385 | |
| 386 | void DebugHandlerBase::endInstruction() { |
| 387 | if (!Asm || !Asm->hasDebugInfo()) |
| 388 | return; |
| 389 | |
| 390 | assert(CurMI != nullptr); |
| 391 | // Don't create a new label after DBG_VALUE and other instructions that don't |
| 392 | // generate code. |
| 393 | if (!CurMI->isMetaInstruction()) { |
| 394 | PrevLabel = nullptr; |
| 395 | PrevInstBB = CurMI->getParent(); |
| 396 | } |
| 397 | |
| 398 | DenseMap<const MachineInstr *, MCSymbol *>::iterator I = |
| 399 | LabelsAfterInsn.find(Val: CurMI); |
| 400 | |
| 401 | // No label needed or label already assigned. |
| 402 | if (I == LabelsAfterInsn.end() || I->second) { |
| 403 | CurMI = nullptr; |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | // We need a label after this instruction. With basic block sections, just |
| 408 | // use the end symbol of the section if this is the last instruction of the |
| 409 | // section. This reduces the need for an additional label and also helps |
| 410 | // merging ranges. |
| 411 | if (CurMI->getParent()->isEndSection() && CurMI->getNextNode() == nullptr) { |
| 412 | PrevLabel = CurMI->getParent()->getEndSymbol(); |
| 413 | } else if (!PrevLabel) { |
| 414 | PrevLabel = MMI->getContext().createTempSymbol(); |
| 415 | Asm->OutStreamer->emitLabel(Symbol: PrevLabel); |
| 416 | } |
| 417 | I->second = PrevLabel; |
| 418 | CurMI = nullptr; |
| 419 | } |
| 420 | |
| 421 | void DebugHandlerBase::endFunction(const MachineFunction *MF) { |
| 422 | if (Asm && hasDebugInfo(MF)) |
| 423 | endFunctionImpl(MF); |
| 424 | DbgValues.clear(); |
| 425 | DbgLabels.clear(); |
| 426 | LabelsBeforeInsn.clear(); |
| 427 | LabelsAfterInsn.clear(); |
| 428 | InstOrdering.clear(); |
| 429 | } |
| 430 | |
| 431 | void DebugHandlerBase::beginBasicBlockSection(const MachineBasicBlock &MBB) { |
| 432 | EpilogBeginBlock = nullptr; |
| 433 | if (!MBB.isEntryBlock()) |
| 434 | PrevLabel = MBB.getSymbol(); |
| 435 | } |
| 436 | |
| 437 | void DebugHandlerBase::endBasicBlockSection(const MachineBasicBlock &MBB) { |
| 438 | PrevLabel = nullptr; |
| 439 | } |
| 440 | |