1f9c275feSReid Kleckner //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===// 2f9c275feSReid Kleckner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6f9c275feSReid Kleckner // 7f9c275feSReid Kleckner //===----------------------------------------------------------------------===// 8f9c275feSReid Kleckner // 9f9c275feSReid Kleckner // Common functionality for different debug information format backends. 10f4873346SYonghong Song // LLVM currently supports DWARF and CodeView. 11f9c275feSReid Kleckner // 12f9c275feSReid Kleckner //===----------------------------------------------------------------------===// 13f9c275feSReid Kleckner 1461b189e0SYonghong Song #include "llvm/CodeGen/DebugHandlerBase.h" 151a4cbbe4SBob Haarman #include "llvm/ADT/Optional.h" 161a4cbbe4SBob Haarman #include "llvm/ADT/Twine.h" 17f9c275feSReid Kleckner #include "llvm/CodeGen/AsmPrinter.h" 18f9c275feSReid Kleckner #include "llvm/CodeGen/MachineFunction.h" 19f9c275feSReid Kleckner #include "llvm/CodeGen/MachineInstr.h" 20f9c275feSReid Kleckner #include "llvm/CodeGen/MachineModuleInfo.h" 21b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetSubtargetInfo.h" 2228865809SReid Kleckner #include "llvm/IR/DebugInfo.h" 23a5b1eef8SReid Kleckner #include "llvm/MC/MCStreamer.h" 24f9c275feSReid Kleckner 25f9c275feSReid Kleckner using namespace llvm; 26f9c275feSReid Kleckner 277224c081SVedant Kumar #define DEBUG_TYPE "dwarfdebug" 287224c081SVedant Kumar 291a4cbbe4SBob Haarman Optional<DbgVariableLocation> 301a4cbbe4SBob Haarman DbgVariableLocation::extractFromMachineInstruction( 311a4cbbe4SBob Haarman const MachineInstr &Instruction) { 321a4cbbe4SBob Haarman DbgVariableLocation Location; 33223303c5SBob Haarman if (!Instruction.isDebugValue()) 341a4cbbe4SBob Haarman return None; 35223303c5SBob Haarman if (!Instruction.getOperand(0).isReg()) 361a4cbbe4SBob Haarman return None; 37223303c5SBob Haarman Location.Register = Instruction.getOperand(0).getReg(); 38223303c5SBob Haarman Location.FragmentInfo.reset(); 39223303c5SBob Haarman // We only handle expressions generated by DIExpression::appendOffset, 40223303c5SBob Haarman // which doesn't require a full stack machine. 41223303c5SBob Haarman int64_t Offset = 0; 42223303c5SBob Haarman const DIExpression *DIExpr = Instruction.getDebugExpression(); 43223303c5SBob Haarman auto Op = DIExpr->expr_op_begin(); 44223303c5SBob Haarman while (Op != DIExpr->expr_op_end()) { 45223303c5SBob Haarman switch (Op->getOp()) { 46223303c5SBob Haarman case dwarf::DW_OP_constu: { 47223303c5SBob Haarman int Value = Op->getArg(0); 48223303c5SBob Haarman ++Op; 49223303c5SBob Haarman if (Op != DIExpr->expr_op_end()) { 50223303c5SBob Haarman switch (Op->getOp()) { 51223303c5SBob Haarman case dwarf::DW_OP_minus: 52223303c5SBob Haarman Offset -= Value; 53223303c5SBob Haarman break; 54223303c5SBob Haarman case dwarf::DW_OP_plus: 55223303c5SBob Haarman Offset += Value; 5668e46019SBob Haarman break; 57223303c5SBob Haarman default: 58223303c5SBob Haarman continue; 59223303c5SBob Haarman } 60223303c5SBob Haarman } 61223303c5SBob Haarman } break; 62223303c5SBob Haarman case dwarf::DW_OP_plus_uconst: 63223303c5SBob Haarman Offset += Op->getArg(0); 64223303c5SBob Haarman break; 65223303c5SBob Haarman case dwarf::DW_OP_LLVM_fragment: 66223303c5SBob Haarman Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)}; 67223303c5SBob Haarman break; 68223303c5SBob Haarman case dwarf::DW_OP_deref: 6908f5fd51SReid Kleckner Location.LoadChain.push_back(Offset); 7008f5fd51SReid Kleckner Offset = 0; 71223303c5SBob Haarman break; 72223303c5SBob Haarman default: 731a4cbbe4SBob Haarman return None; 74223303c5SBob Haarman } 75223303c5SBob Haarman ++Op; 76223303c5SBob Haarman } 77223303c5SBob Haarman 7808f5fd51SReid Kleckner // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE 7908f5fd51SReid Kleckner // instruction. 8008f5fd51SReid Kleckner // FIXME: Replace these with DIExpression. 8108f5fd51SReid Kleckner if (Instruction.isIndirectDebugValue()) 8208f5fd51SReid Kleckner Location.LoadChain.push_back(Offset); 8308f5fd51SReid Kleckner 841a4cbbe4SBob Haarman return Location; 85223303c5SBob Haarman } 86223303c5SBob Haarman 87f9c275feSReid Kleckner DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} 88f9c275feSReid Kleckner 89f9c275feSReid Kleckner // Each LexicalScope has first instruction and last instruction to mark 90f9c275feSReid Kleckner // beginning and end of a scope respectively. Create an inverse map that list 91f9c275feSReid Kleckner // scopes starts (and ends) with an instruction. One instruction may start (or 92f9c275feSReid Kleckner // end) multiple scopes. Ignore scopes that are not reachable. 93f9c275feSReid Kleckner void DebugHandlerBase::identifyScopeMarkers() { 94f9c275feSReid Kleckner SmallVector<LexicalScope *, 4> WorkList; 95f9c275feSReid Kleckner WorkList.push_back(LScopes.getCurrentFunctionScope()); 96f9c275feSReid Kleckner while (!WorkList.empty()) { 97f9c275feSReid Kleckner LexicalScope *S = WorkList.pop_back_val(); 98f9c275feSReid Kleckner 99f9c275feSReid Kleckner const SmallVectorImpl<LexicalScope *> &Children = S->getChildren(); 100f9c275feSReid Kleckner if (!Children.empty()) 101f9c275feSReid Kleckner WorkList.append(Children.begin(), Children.end()); 102f9c275feSReid Kleckner 103f9c275feSReid Kleckner if (S->isAbstractScope()) 104f9c275feSReid Kleckner continue; 105f9c275feSReid Kleckner 106f9c275feSReid Kleckner for (const InsnRange &R : S->getRanges()) { 107f9c275feSReid Kleckner assert(R.first && "InsnRange does not have first instruction!"); 108f9c275feSReid Kleckner assert(R.second && "InsnRange does not have second instruction!"); 109f9c275feSReid Kleckner requestLabelBeforeInsn(R.first); 110f9c275feSReid Kleckner requestLabelAfterInsn(R.second); 111f9c275feSReid Kleckner } 112f9c275feSReid Kleckner } 113f9c275feSReid Kleckner } 114f9c275feSReid Kleckner 115f9c275feSReid Kleckner // Return Label preceding the instruction. 116f9c275feSReid Kleckner MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) { 117f9c275feSReid Kleckner MCSymbol *Label = LabelsBeforeInsn.lookup(MI); 118f9c275feSReid Kleckner assert(Label && "Didn't insert label before instruction"); 119f9c275feSReid Kleckner return Label; 120f9c275feSReid Kleckner } 121f9c275feSReid Kleckner 122f9c275feSReid Kleckner // Return Label immediately following the instruction. 123f9c275feSReid Kleckner MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) { 124f9c275feSReid Kleckner return LabelsAfterInsn.lookup(MI); 125f9c275feSReid Kleckner } 126f9c275feSReid Kleckner 12774533bd3SVedant Kumar // Return the function-local offset of an instruction. 12874533bd3SVedant Kumar const MCExpr * 12974533bd3SVedant Kumar DebugHandlerBase::getFunctionLocalOffsetAfterInsn(const MachineInstr *MI) { 13074533bd3SVedant Kumar MCContext &MC = Asm->OutContext; 13174533bd3SVedant Kumar 13274533bd3SVedant Kumar MCSymbol *Start = Asm->getFunctionBegin(); 13374533bd3SVedant Kumar const auto *StartRef = MCSymbolRefExpr::create(Start, MC); 13474533bd3SVedant Kumar 13574533bd3SVedant Kumar MCSymbol *AfterInsn = getLabelAfterInsn(MI); 13674533bd3SVedant Kumar assert(AfterInsn && "Expected label after instruction"); 13774533bd3SVedant Kumar const auto *AfterRef = MCSymbolRefExpr::create(AfterInsn, MC); 13874533bd3SVedant Kumar 13974533bd3SVedant Kumar return MCBinaryExpr::createSub(AfterRef, StartRef, MC); 14074533bd3SVedant Kumar } 14174533bd3SVedant Kumar 142acee5685SAmjad Aboud /// If this type is derived from a base type then return base type size. 143acee5685SAmjad Aboud uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) { 144acee5685SAmjad Aboud DIType *Ty = TyRef.resolve(); 145acee5685SAmjad Aboud assert(Ty); 146acee5685SAmjad Aboud DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty); 147acee5685SAmjad Aboud if (!DDTy) 148acee5685SAmjad Aboud return Ty->getSizeInBits(); 149acee5685SAmjad Aboud 150acee5685SAmjad Aboud unsigned Tag = DDTy->getTag(); 151acee5685SAmjad Aboud 152acee5685SAmjad Aboud if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && 153acee5685SAmjad Aboud Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && 154e1156c2eSVictor Leschuk Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type) 155acee5685SAmjad Aboud return DDTy->getSizeInBits(); 156acee5685SAmjad Aboud 157acee5685SAmjad Aboud DIType *BaseType = DDTy->getBaseType().resolve(); 158acee5685SAmjad Aboud 15974bfafa1SAdrian McCarthy if (!BaseType) 16074bfafa1SAdrian McCarthy return 0; 161acee5685SAmjad Aboud 162acee5685SAmjad Aboud // If this is a derived type, go ahead and get the base type, unless it's a 163acee5685SAmjad Aboud // reference then it's just the size of the field. Pointer types have no need 164acee5685SAmjad Aboud // of this since they're a different type of qualification on the type. 165acee5685SAmjad Aboud if (BaseType->getTag() == dwarf::DW_TAG_reference_type || 166acee5685SAmjad Aboud BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type) 167acee5685SAmjad Aboud return Ty->getSizeInBits(); 168acee5685SAmjad Aboud 169acee5685SAmjad Aboud return getBaseTypeSize(BaseType); 170acee5685SAmjad Aboud } 171acee5685SAmjad Aboud 172debb3c35SBenjamin Kramer static bool hasDebugInfo(const MachineModuleInfo *MMI, 173debb3c35SBenjamin Kramer const MachineFunction *MF) { 174b2fbb4b2SDavid Blaikie if (!MMI->hasDebugInfo()) 175b2fbb4b2SDavid Blaikie return false; 176f1caa283SMatthias Braun auto *SP = MF->getFunction().getSubprogram(); 177b2fbb4b2SDavid Blaikie if (!SP) 178b2fbb4b2SDavid Blaikie return false; 179b2fbb4b2SDavid Blaikie assert(SP->getUnit()); 180b2fbb4b2SDavid Blaikie auto EK = SP->getUnit()->getEmissionKind(); 181b2fbb4b2SDavid Blaikie if (EK == DICompileUnit::NoDebug) 182b2fbb4b2SDavid Blaikie return false; 183b2fbb4b2SDavid Blaikie return true; 184b2fbb4b2SDavid Blaikie } 185b2fbb4b2SDavid Blaikie 186f9c275feSReid Kleckner void DebugHandlerBase::beginFunction(const MachineFunction *MF) { 1878f333795SAdrian Prantl PrevInstBB = nullptr; 188b2fbb4b2SDavid Blaikie 1895bc8543aSReid Kleckner if (!Asm || !hasDebugInfo(MMI, MF)) { 190b2fbb4b2SDavid Blaikie skippedNonDebugFunction(); 191b2fbb4b2SDavid Blaikie return; 192b2fbb4b2SDavid Blaikie } 193b2fbb4b2SDavid Blaikie 194f9c275feSReid Kleckner // Grab the lexical scopes for the function, if we don't have any of those 195f9c275feSReid Kleckner // then we're not going to be able to do anything. 196f9c275feSReid Kleckner LScopes.initialize(*MF); 197b2fbb4b2SDavid Blaikie if (LScopes.empty()) { 198b2fbb4b2SDavid Blaikie beginFunctionImpl(MF); 199f9c275feSReid Kleckner return; 200b2fbb4b2SDavid Blaikie } 201f9c275feSReid Kleckner 202f9c275feSReid Kleckner // Make sure that each lexical scope will have a begin/end label. 203f9c275feSReid Kleckner identifyScopeMarkers(); 204f9c275feSReid Kleckner 205f9c275feSReid Kleckner // Calculate history for local variables. 206f9c275feSReid Kleckner assert(DbgValues.empty() && "DbgValues map wasn't cleaned!"); 2072532ac88SHsiangkai Wang assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!"); 2082532ac88SHsiangkai Wang calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(), 2092532ac88SHsiangkai Wang DbgValues, DbgLabels); 2107224c081SVedant Kumar LLVM_DEBUG(DbgValues.dump()); 211f9c275feSReid Kleckner 212f9c275feSReid Kleckner // Request labels for the full history. 213f9c275feSReid Kleckner for (const auto &I : DbgValues) { 214*6feef56dSDavid Stenberg const auto &Entries = I.second; 215*6feef56dSDavid Stenberg if (Entries.empty()) 216f9c275feSReid Kleckner continue; 217f9c275feSReid Kleckner 2189dbeca3dSDavid Stenberg auto IsDescribedByReg = [](const MachineInstr *MI) { 2199dbeca3dSDavid Stenberg return MI->getOperand(0).isReg() && MI->getOperand(0).getReg(); 2209dbeca3dSDavid Stenberg }; 2219dbeca3dSDavid Stenberg 2229dbeca3dSDavid Stenberg // The first mention of a function argument gets the CurrentFnBegin label, 2239dbeca3dSDavid Stenberg // so arguments are visible when breaking at function entry. 2249dbeca3dSDavid Stenberg // 2259dbeca3dSDavid Stenberg // We do not change the label for values that are described by registers, 2269dbeca3dSDavid Stenberg // as that could place them above their defining instructions. We should 2279dbeca3dSDavid Stenberg // ideally not change the labels for constant debug values either, since 2289dbeca3dSDavid Stenberg // doing that violates the ranges that are calculated in the history map. 2299dbeca3dSDavid Stenberg // However, we currently do not emit debug values for constant arguments 2309dbeca3dSDavid Stenberg // directly at the start of the function, so this code is still useful. 2313739979cSDavid Stenberg const DILocalVariable *DIVar = 232*6feef56dSDavid Stenberg Entries.front().getBegin()->getDebugVariable(); 233f9c275feSReid Kleckner if (DIVar->isParameter() && 234f1caa283SMatthias Braun getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) { 235*6feef56dSDavid Stenberg if (!IsDescribedByReg(Entries.front().getBegin())) 236*6feef56dSDavid Stenberg LabelsBeforeInsn[Entries.front().getBegin()] = Asm->getFunctionBegin(); 237*6feef56dSDavid Stenberg if (Entries.front().getBegin()->getDebugExpression()->isFragment()) { 238941fa758SAdrian Prantl // Mark all non-overlapping initial fragments. 239*6feef56dSDavid Stenberg for (auto I = Entries.begin(); I != Entries.end(); ++I) { 2403739979cSDavid Stenberg const DIExpression *Fragment = I->getBegin()->getDebugExpression(); 241*6feef56dSDavid Stenberg if (std::any_of(Entries.begin(), I, 242*6feef56dSDavid Stenberg [&](DbgValueHistoryMap::Entry Pred) { 2439dbeca3dSDavid Stenberg return Fragment->fragmentsOverlap( 2443739979cSDavid Stenberg Pred.getBegin()->getDebugExpression()); 245f9c275feSReid Kleckner })) 246f9c275feSReid Kleckner break; 2473739979cSDavid Stenberg if (!IsDescribedByReg(I->getBegin())) 2483739979cSDavid Stenberg LabelsBeforeInsn[I->getBegin()] = Asm->getFunctionBegin(); 249f9c275feSReid Kleckner } 250f9c275feSReid Kleckner } 251f9c275feSReid Kleckner } 252f9c275feSReid Kleckner 253*6feef56dSDavid Stenberg for (const auto &Entry : Entries) { 254*6feef56dSDavid Stenberg requestLabelBeforeInsn(Entry.getBegin()); 255*6feef56dSDavid Stenberg if (Entry.getEnd()) 256*6feef56dSDavid Stenberg requestLabelAfterInsn(Entry.getEnd()); 257f9c275feSReid Kleckner } 258f9c275feSReid Kleckner } 259f9c275feSReid Kleckner 2602532ac88SHsiangkai Wang // Ensure there is a symbol before DBG_LABEL. 2612532ac88SHsiangkai Wang for (const auto &I : DbgLabels) { 2622532ac88SHsiangkai Wang const MachineInstr *MI = I.second; 2632532ac88SHsiangkai Wang requestLabelBeforeInsn(MI); 2642532ac88SHsiangkai Wang } 2652532ac88SHsiangkai Wang 266f9c275feSReid Kleckner PrevInstLoc = DebugLoc(); 267f9c275feSReid Kleckner PrevLabel = Asm->getFunctionBegin(); 268b2fbb4b2SDavid Blaikie beginFunctionImpl(MF); 269f9c275feSReid Kleckner } 270f9c275feSReid Kleckner 271f9c275feSReid Kleckner void DebugHandlerBase::beginInstruction(const MachineInstr *MI) { 272f9c275feSReid Kleckner if (!MMI->hasDebugInfo()) 273f9c275feSReid Kleckner return; 274f9c275feSReid Kleckner 275f9c275feSReid Kleckner assert(CurMI == nullptr); 276f9c275feSReid Kleckner CurMI = MI; 277f9c275feSReid Kleckner 278f9c275feSReid Kleckner // Insert labels where requested. 279f9c275feSReid Kleckner DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 280f9c275feSReid Kleckner LabelsBeforeInsn.find(MI); 281f9c275feSReid Kleckner 282f9c275feSReid Kleckner // No label needed. 283f9c275feSReid Kleckner if (I == LabelsBeforeInsn.end()) 284f9c275feSReid Kleckner return; 285f9c275feSReid Kleckner 286f9c275feSReid Kleckner // Label already assigned. 287f9c275feSReid Kleckner if (I->second) 288f9c275feSReid Kleckner return; 289f9c275feSReid Kleckner 290f9c275feSReid Kleckner if (!PrevLabel) { 291f9c275feSReid Kleckner PrevLabel = MMI->getContext().createTempSymbol(); 292f9c275feSReid Kleckner Asm->OutStreamer->EmitLabel(PrevLabel); 293f9c275feSReid Kleckner } 294f9c275feSReid Kleckner I->second = PrevLabel; 295f9c275feSReid Kleckner } 296f9c275feSReid Kleckner 297f9c275feSReid Kleckner void DebugHandlerBase::endInstruction() { 298f9c275feSReid Kleckner if (!MMI->hasDebugInfo()) 299f9c275feSReid Kleckner return; 300f9c275feSReid Kleckner 301f9c275feSReid Kleckner assert(CurMI != nullptr); 302fb31da13SAdrian Prantl // Don't create a new label after DBG_VALUE and other instructions that don't 303fb31da13SAdrian Prantl // generate code. 304fb31da13SAdrian Prantl if (!CurMI->isMetaInstruction()) { 305f9c275feSReid Kleckner PrevLabel = nullptr; 306ac7fe5e0SPaul Robinson PrevInstBB = CurMI->getParent(); 307ac7fe5e0SPaul Robinson } 308f9c275feSReid Kleckner 309f9c275feSReid Kleckner DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 310f9c275feSReid Kleckner LabelsAfterInsn.find(CurMI); 311f9c275feSReid Kleckner CurMI = nullptr; 312f9c275feSReid Kleckner 313f9c275feSReid Kleckner // No label needed. 314f9c275feSReid Kleckner if (I == LabelsAfterInsn.end()) 315f9c275feSReid Kleckner return; 316f9c275feSReid Kleckner 317f9c275feSReid Kleckner // Label already assigned. 318f9c275feSReid Kleckner if (I->second) 319f9c275feSReid Kleckner return; 320f9c275feSReid Kleckner 321f9c275feSReid Kleckner // We need a label after this instruction. 322f9c275feSReid Kleckner if (!PrevLabel) { 323f9c275feSReid Kleckner PrevLabel = MMI->getContext().createTempSymbol(); 324f9c275feSReid Kleckner Asm->OutStreamer->EmitLabel(PrevLabel); 325f9c275feSReid Kleckner } 326f9c275feSReid Kleckner I->second = PrevLabel; 327f9c275feSReid Kleckner } 328f9c275feSReid Kleckner 329f9c275feSReid Kleckner void DebugHandlerBase::endFunction(const MachineFunction *MF) { 330b2fbb4b2SDavid Blaikie if (hasDebugInfo(MMI, MF)) 331b2fbb4b2SDavid Blaikie endFunctionImpl(MF); 332f9c275feSReid Kleckner DbgValues.clear(); 3332532ac88SHsiangkai Wang DbgLabels.clear(); 334f9c275feSReid Kleckner LabelsBeforeInsn.clear(); 335f9c275feSReid Kleckner LabelsAfterInsn.clear(); 336f9c275feSReid Kleckner } 337