1f9c275feSReid Kleckner //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===// 2f9c275feSReid Kleckner // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler 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) { 214f9c275feSReid Kleckner const auto &Ranges = I.second; 215f9c275feSReid Kleckner if (Ranges.empty()) 216f9c275feSReid Kleckner continue; 217f9c275feSReid Kleckner 218f9c275feSReid Kleckner // The first mention of a function argument gets the CurrentFnBegin 219f9c275feSReid Kleckner // label, so arguments are visible when breaking at function entry. 220f9c275feSReid Kleckner const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable(); 221f9c275feSReid Kleckner if (DIVar->isParameter() && 222f1caa283SMatthias Braun getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) { 223f9c275feSReid Kleckner LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin(); 224941fa758SAdrian Prantl if (Ranges.front().first->getDebugExpression()->isFragment()) { 225941fa758SAdrian Prantl // Mark all non-overlapping initial fragments. 226f9c275feSReid Kleckner for (auto I = Ranges.begin(); I != Ranges.end(); ++I) { 227941fa758SAdrian Prantl const DIExpression *Fragment = I->first->getDebugExpression(); 228f9c275feSReid Kleckner if (std::all_of(Ranges.begin(), I, 229f9c275feSReid Kleckner [&](DbgValueHistoryMap::InstrRange Pred) { 230a223f815SBjorn Pettersson return !Fragment->fragmentsOverlap( 231a223f815SBjorn Pettersson Pred.first->getDebugExpression()); 232f9c275feSReid Kleckner })) 233f9c275feSReid Kleckner LabelsBeforeInsn[I->first] = Asm->getFunctionBegin(); 234f9c275feSReid Kleckner else 235f9c275feSReid Kleckner break; 236f9c275feSReid Kleckner } 237f9c275feSReid Kleckner } 238f9c275feSReid Kleckner } 239f9c275feSReid Kleckner 240f9c275feSReid Kleckner for (const auto &Range : Ranges) { 241f9c275feSReid Kleckner requestLabelBeforeInsn(Range.first); 242f9c275feSReid Kleckner if (Range.second) 243f9c275feSReid Kleckner requestLabelAfterInsn(Range.second); 244f9c275feSReid Kleckner } 245f9c275feSReid Kleckner } 246f9c275feSReid Kleckner 2472532ac88SHsiangkai Wang // Ensure there is a symbol before DBG_LABEL. 2482532ac88SHsiangkai Wang for (const auto &I : DbgLabels) { 2492532ac88SHsiangkai Wang const MachineInstr *MI = I.second; 2502532ac88SHsiangkai Wang requestLabelBeforeInsn(MI); 2512532ac88SHsiangkai Wang } 2522532ac88SHsiangkai Wang 253f9c275feSReid Kleckner PrevInstLoc = DebugLoc(); 254f9c275feSReid Kleckner PrevLabel = Asm->getFunctionBegin(); 255b2fbb4b2SDavid Blaikie beginFunctionImpl(MF); 256f9c275feSReid Kleckner } 257f9c275feSReid Kleckner 258f9c275feSReid Kleckner void DebugHandlerBase::beginInstruction(const MachineInstr *MI) { 259f9c275feSReid Kleckner if (!MMI->hasDebugInfo()) 260f9c275feSReid Kleckner return; 261f9c275feSReid Kleckner 262f9c275feSReid Kleckner assert(CurMI == nullptr); 263f9c275feSReid Kleckner CurMI = MI; 264f9c275feSReid Kleckner 265f9c275feSReid Kleckner // Insert labels where requested. 266f9c275feSReid Kleckner DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 267f9c275feSReid Kleckner LabelsBeforeInsn.find(MI); 268f9c275feSReid Kleckner 269f9c275feSReid Kleckner // No label needed. 270f9c275feSReid Kleckner if (I == LabelsBeforeInsn.end()) 271f9c275feSReid Kleckner return; 272f9c275feSReid Kleckner 273f9c275feSReid Kleckner // Label already assigned. 274f9c275feSReid Kleckner if (I->second) 275f9c275feSReid Kleckner return; 276f9c275feSReid Kleckner 277f9c275feSReid Kleckner if (!PrevLabel) { 278f9c275feSReid Kleckner PrevLabel = MMI->getContext().createTempSymbol(); 279f9c275feSReid Kleckner Asm->OutStreamer->EmitLabel(PrevLabel); 280f9c275feSReid Kleckner } 281f9c275feSReid Kleckner I->second = PrevLabel; 282f9c275feSReid Kleckner } 283f9c275feSReid Kleckner 284f9c275feSReid Kleckner void DebugHandlerBase::endInstruction() { 285f9c275feSReid Kleckner if (!MMI->hasDebugInfo()) 286f9c275feSReid Kleckner return; 287f9c275feSReid Kleckner 288f9c275feSReid Kleckner assert(CurMI != nullptr); 289fb31da13SAdrian Prantl // Don't create a new label after DBG_VALUE and other instructions that don't 290fb31da13SAdrian Prantl // generate code. 291fb31da13SAdrian Prantl if (!CurMI->isMetaInstruction()) { 292f9c275feSReid Kleckner PrevLabel = nullptr; 293ac7fe5e0SPaul Robinson PrevInstBB = CurMI->getParent(); 294ac7fe5e0SPaul Robinson } 295f9c275feSReid Kleckner 296f9c275feSReid Kleckner DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 297f9c275feSReid Kleckner LabelsAfterInsn.find(CurMI); 298f9c275feSReid Kleckner CurMI = nullptr; 299f9c275feSReid Kleckner 300f9c275feSReid Kleckner // No label needed. 301f9c275feSReid Kleckner if (I == LabelsAfterInsn.end()) 302f9c275feSReid Kleckner return; 303f9c275feSReid Kleckner 304f9c275feSReid Kleckner // Label already assigned. 305f9c275feSReid Kleckner if (I->second) 306f9c275feSReid Kleckner return; 307f9c275feSReid Kleckner 308f9c275feSReid Kleckner // We need a label after this instruction. 309f9c275feSReid Kleckner if (!PrevLabel) { 310f9c275feSReid Kleckner PrevLabel = MMI->getContext().createTempSymbol(); 311f9c275feSReid Kleckner Asm->OutStreamer->EmitLabel(PrevLabel); 312f9c275feSReid Kleckner } 313f9c275feSReid Kleckner I->second = PrevLabel; 314f9c275feSReid Kleckner } 315f9c275feSReid Kleckner 316f9c275feSReid Kleckner void DebugHandlerBase::endFunction(const MachineFunction *MF) { 317b2fbb4b2SDavid Blaikie if (hasDebugInfo(MMI, MF)) 318b2fbb4b2SDavid Blaikie endFunctionImpl(MF); 319f9c275feSReid Kleckner DbgValues.clear(); 3202532ac88SHsiangkai Wang DbgLabels.clear(); 321f9c275feSReid Kleckner LabelsBeforeInsn.clear(); 322f9c275feSReid Kleckner LabelsAfterInsn.clear(); 323f9c275feSReid Kleckner } 324