10b57cec5SDimitry Andric //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // Common functionality for different debug information format backends. 100b57cec5SDimitry Andric // LLVM currently supports DWARF and CodeView. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "llvm/CodeGen/DebugHandlerBase.h" 150b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h" 160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 200b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h" 210b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 22e8d8bef9SDimitry Andric #include "llvm/Support/CommandLine.h" 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric using namespace llvm; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric #define DEBUG_TYPE "dwarfdebug" 270b57cec5SDimitry Andric 28e8d8bef9SDimitry Andric /// If true, we drop variable location ranges which exist entirely outside the 29e8d8bef9SDimitry Andric /// variable's lexical scope instruction ranges. 30e8d8bef9SDimitry Andric static cl::opt<bool> TrimVarLocs("trim-var-locs", cl::Hidden, cl::init(true)); 31e8d8bef9SDimitry Andric 32*bdd1243dSDimitry Andric std::optional<DbgVariableLocation> 330b57cec5SDimitry Andric DbgVariableLocation::extractFromMachineInstruction( 340b57cec5SDimitry Andric const MachineInstr &Instruction) { 350b57cec5SDimitry Andric DbgVariableLocation Location; 36fe6060f1SDimitry Andric // Variables calculated from multiple locations can't be represented here. 37fe6060f1SDimitry Andric if (Instruction.getNumDebugOperands() != 1) 38*bdd1243dSDimitry Andric return std::nullopt; 395ffd83dbSDimitry Andric if (!Instruction.getDebugOperand(0).isReg()) 40*bdd1243dSDimitry Andric return std::nullopt; 415ffd83dbSDimitry Andric Location.Register = Instruction.getDebugOperand(0).getReg(); 420b57cec5SDimitry Andric Location.FragmentInfo.reset(); 430b57cec5SDimitry Andric // We only handle expressions generated by DIExpression::appendOffset, 440b57cec5SDimitry Andric // which doesn't require a full stack machine. 450b57cec5SDimitry Andric int64_t Offset = 0; 460b57cec5SDimitry Andric const DIExpression *DIExpr = Instruction.getDebugExpression(); 470b57cec5SDimitry Andric auto Op = DIExpr->expr_op_begin(); 48fe6060f1SDimitry Andric // We can handle a DBG_VALUE_LIST iff it has exactly one location operand that 49fe6060f1SDimitry Andric // appears exactly once at the start of the expression. 50fe6060f1SDimitry Andric if (Instruction.isDebugValueList()) { 51fe6060f1SDimitry Andric if (Instruction.getNumDebugOperands() == 1 && 52fe6060f1SDimitry Andric Op->getOp() == dwarf::DW_OP_LLVM_arg) 53fe6060f1SDimitry Andric ++Op; 54fe6060f1SDimitry Andric else 55*bdd1243dSDimitry Andric return std::nullopt; 56fe6060f1SDimitry Andric } 570b57cec5SDimitry Andric while (Op != DIExpr->expr_op_end()) { 580b57cec5SDimitry Andric switch (Op->getOp()) { 590b57cec5SDimitry Andric case dwarf::DW_OP_constu: { 600b57cec5SDimitry Andric int Value = Op->getArg(0); 610b57cec5SDimitry Andric ++Op; 620b57cec5SDimitry Andric if (Op != DIExpr->expr_op_end()) { 630b57cec5SDimitry Andric switch (Op->getOp()) { 640b57cec5SDimitry Andric case dwarf::DW_OP_minus: 650b57cec5SDimitry Andric Offset -= Value; 660b57cec5SDimitry Andric break; 670b57cec5SDimitry Andric case dwarf::DW_OP_plus: 680b57cec5SDimitry Andric Offset += Value; 690b57cec5SDimitry Andric break; 700b57cec5SDimitry Andric default: 710b57cec5SDimitry Andric continue; 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric } break; 750b57cec5SDimitry Andric case dwarf::DW_OP_plus_uconst: 760b57cec5SDimitry Andric Offset += Op->getArg(0); 770b57cec5SDimitry Andric break; 780b57cec5SDimitry Andric case dwarf::DW_OP_LLVM_fragment: 790b57cec5SDimitry Andric Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)}; 800b57cec5SDimitry Andric break; 810b57cec5SDimitry Andric case dwarf::DW_OP_deref: 820b57cec5SDimitry Andric Location.LoadChain.push_back(Offset); 830b57cec5SDimitry Andric Offset = 0; 840b57cec5SDimitry Andric break; 850b57cec5SDimitry Andric default: 86*bdd1243dSDimitry Andric return std::nullopt; 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric ++Op; 890b57cec5SDimitry Andric } 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE 920b57cec5SDimitry Andric // instruction. 930b57cec5SDimitry Andric // FIXME: Replace these with DIExpression. 940b57cec5SDimitry Andric if (Instruction.isIndirectDebugValue()) 950b57cec5SDimitry Andric Location.LoadChain.push_back(Offset); 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric return Location; 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} 1010b57cec5SDimitry Andric 102e8d8bef9SDimitry Andric void DebugHandlerBase::beginModule(Module *M) { 103e8d8bef9SDimitry Andric if (M->debug_compile_units().empty()) 104e8d8bef9SDimitry Andric Asm = nullptr; 105e8d8bef9SDimitry Andric } 106e8d8bef9SDimitry Andric 1070b57cec5SDimitry Andric // Each LexicalScope has first instruction and last instruction to mark 1080b57cec5SDimitry Andric // beginning and end of a scope respectively. Create an inverse map that list 1090b57cec5SDimitry Andric // scopes starts (and ends) with an instruction. One instruction may start (or 1100b57cec5SDimitry Andric // end) multiple scopes. Ignore scopes that are not reachable. 1110b57cec5SDimitry Andric void DebugHandlerBase::identifyScopeMarkers() { 1120b57cec5SDimitry Andric SmallVector<LexicalScope *, 4> WorkList; 1130b57cec5SDimitry Andric WorkList.push_back(LScopes.getCurrentFunctionScope()); 1140b57cec5SDimitry Andric while (!WorkList.empty()) { 1150b57cec5SDimitry Andric LexicalScope *S = WorkList.pop_back_val(); 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric const SmallVectorImpl<LexicalScope *> &Children = S->getChildren(); 1180b57cec5SDimitry Andric if (!Children.empty()) 1190b57cec5SDimitry Andric WorkList.append(Children.begin(), Children.end()); 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric if (S->isAbstractScope()) 1220b57cec5SDimitry Andric continue; 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric for (const InsnRange &R : S->getRanges()) { 1250b57cec5SDimitry Andric assert(R.first && "InsnRange does not have first instruction!"); 1260b57cec5SDimitry Andric assert(R.second && "InsnRange does not have second instruction!"); 1270b57cec5SDimitry Andric requestLabelBeforeInsn(R.first); 1280b57cec5SDimitry Andric requestLabelAfterInsn(R.second); 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric } 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric // Return Label preceding the instruction. 1340b57cec5SDimitry Andric MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) { 1350b57cec5SDimitry Andric MCSymbol *Label = LabelsBeforeInsn.lookup(MI); 1360b57cec5SDimitry Andric assert(Label && "Didn't insert label before instruction"); 1370b57cec5SDimitry Andric return Label; 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric // Return Label immediately following the instruction. 1410b57cec5SDimitry Andric MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) { 1420b57cec5SDimitry Andric return LabelsAfterInsn.lookup(MI); 1430b57cec5SDimitry Andric } 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric /// If this type is derived from a base type then return base type size. 1460b57cec5SDimitry Andric uint64_t DebugHandlerBase::getBaseTypeSize(const DIType *Ty) { 1470b57cec5SDimitry Andric assert(Ty); 1480b57cec5SDimitry Andric const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty); 1490b57cec5SDimitry Andric if (!DDTy) 1500b57cec5SDimitry Andric return Ty->getSizeInBits(); 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric unsigned Tag = DDTy->getTag(); 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && 1550b57cec5SDimitry Andric Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && 15604eeddc0SDimitry Andric Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type && 15704eeddc0SDimitry Andric Tag != dwarf::DW_TAG_immutable_type) 1580b57cec5SDimitry Andric return DDTy->getSizeInBits(); 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric DIType *BaseType = DDTy->getBaseType(); 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric if (!BaseType) 1630b57cec5SDimitry Andric return 0; 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric // If this is a derived type, go ahead and get the base type, unless it's a 1660b57cec5SDimitry Andric // reference then it's just the size of the field. Pointer types have no need 1670b57cec5SDimitry Andric // of this since they're a different type of qualification on the type. 1680b57cec5SDimitry Andric if (BaseType->getTag() == dwarf::DW_TAG_reference_type || 1690b57cec5SDimitry Andric BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type) 1700b57cec5SDimitry Andric return Ty->getSizeInBits(); 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric return getBaseTypeSize(BaseType); 1730b57cec5SDimitry Andric } 1740b57cec5SDimitry Andric 175e8d8bef9SDimitry Andric bool DebugHandlerBase::isUnsignedDIType(const DIType *Ty) { 176fe6060f1SDimitry Andric if (isa<DIStringType>(Ty)) { 177349cc55cSDimitry Andric // Some transformations (e.g. instcombine) may decide to turn a Fortran 178349cc55cSDimitry Andric // character object into an integer, and later ones (e.g. SROA) may 179349cc55cSDimitry Andric // further inject a constant integer in a llvm.dbg.value call to track 180349cc55cSDimitry Andric // the object's value. Here we trust the transformations are doing the 181349cc55cSDimitry Andric // right thing, and treat the constant as unsigned to preserve that value 182349cc55cSDimitry Andric // (i.e. avoid sign extension). 183fe6060f1SDimitry Andric return true; 184fe6060f1SDimitry Andric } 185349cc55cSDimitry Andric 186e8d8bef9SDimitry Andric if (auto *CTy = dyn_cast<DICompositeType>(Ty)) { 187349cc55cSDimitry Andric if (CTy->getTag() == dwarf::DW_TAG_enumeration_type) { 188349cc55cSDimitry Andric if (!(Ty = CTy->getBaseType())) 189e8d8bef9SDimitry Andric // FIXME: Enums without a fixed underlying type have unknown signedness 190e8d8bef9SDimitry Andric // here, leading to incorrectly emitted constants. 191e8d8bef9SDimitry Andric return false; 192349cc55cSDimitry Andric } else 193e8d8bef9SDimitry Andric // (Pieces of) aggregate types that get hacked apart by SROA may be 194e8d8bef9SDimitry Andric // represented by a constant. Encode them as unsigned bytes. 195e8d8bef9SDimitry Andric return true; 196e8d8bef9SDimitry Andric } 197e8d8bef9SDimitry Andric 198e8d8bef9SDimitry Andric if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) { 199e8d8bef9SDimitry Andric dwarf::Tag T = (dwarf::Tag)Ty->getTag(); 200e8d8bef9SDimitry Andric // Encode pointer constants as unsigned bytes. This is used at least for 201e8d8bef9SDimitry Andric // null pointer constant emission. 202e8d8bef9SDimitry Andric // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed 203e8d8bef9SDimitry Andric // here, but accept them for now due to a bug in SROA producing bogus 204e8d8bef9SDimitry Andric // dbg.values. 205e8d8bef9SDimitry Andric if (T == dwarf::DW_TAG_pointer_type || 206e8d8bef9SDimitry Andric T == dwarf::DW_TAG_ptr_to_member_type || 207e8d8bef9SDimitry Andric T == dwarf::DW_TAG_reference_type || 208e8d8bef9SDimitry Andric T == dwarf::DW_TAG_rvalue_reference_type) 209e8d8bef9SDimitry Andric return true; 210e8d8bef9SDimitry Andric assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type || 211e8d8bef9SDimitry Andric T == dwarf::DW_TAG_volatile_type || 21204eeddc0SDimitry Andric T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type || 21304eeddc0SDimitry Andric T == dwarf::DW_TAG_immutable_type); 214e8d8bef9SDimitry Andric assert(DTy->getBaseType() && "Expected valid base type"); 215e8d8bef9SDimitry Andric return isUnsignedDIType(DTy->getBaseType()); 216e8d8bef9SDimitry Andric } 217e8d8bef9SDimitry Andric 218e8d8bef9SDimitry Andric auto *BTy = cast<DIBasicType>(Ty); 219e8d8bef9SDimitry Andric unsigned Encoding = BTy->getEncoding(); 220e8d8bef9SDimitry Andric assert((Encoding == dwarf::DW_ATE_unsigned || 221e8d8bef9SDimitry Andric Encoding == dwarf::DW_ATE_unsigned_char || 222e8d8bef9SDimitry Andric Encoding == dwarf::DW_ATE_signed || 223e8d8bef9SDimitry Andric Encoding == dwarf::DW_ATE_signed_char || 224e8d8bef9SDimitry Andric Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF || 225e8d8bef9SDimitry Andric Encoding == dwarf::DW_ATE_boolean || 226e8d8bef9SDimitry Andric (Ty->getTag() == dwarf::DW_TAG_unspecified_type && 227e8d8bef9SDimitry Andric Ty->getName() == "decltype(nullptr)")) && 228e8d8bef9SDimitry Andric "Unsupported encoding"); 229e8d8bef9SDimitry Andric return Encoding == dwarf::DW_ATE_unsigned || 230e8d8bef9SDimitry Andric Encoding == dwarf::DW_ATE_unsigned_char || 231e8d8bef9SDimitry Andric Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean || 232e8d8bef9SDimitry Andric Ty->getTag() == dwarf::DW_TAG_unspecified_type; 233e8d8bef9SDimitry Andric } 234e8d8bef9SDimitry Andric 2350b57cec5SDimitry Andric static bool hasDebugInfo(const MachineModuleInfo *MMI, 2360b57cec5SDimitry Andric const MachineFunction *MF) { 2370b57cec5SDimitry Andric if (!MMI->hasDebugInfo()) 2380b57cec5SDimitry Andric return false; 2390b57cec5SDimitry Andric auto *SP = MF->getFunction().getSubprogram(); 2400b57cec5SDimitry Andric if (!SP) 2410b57cec5SDimitry Andric return false; 2420b57cec5SDimitry Andric assert(SP->getUnit()); 2430b57cec5SDimitry Andric auto EK = SP->getUnit()->getEmissionKind(); 2440b57cec5SDimitry Andric if (EK == DICompileUnit::NoDebug) 2450b57cec5SDimitry Andric return false; 2460b57cec5SDimitry Andric return true; 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric void DebugHandlerBase::beginFunction(const MachineFunction *MF) { 2500b57cec5SDimitry Andric PrevInstBB = nullptr; 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric if (!Asm || !hasDebugInfo(MMI, MF)) { 2530b57cec5SDimitry Andric skippedNonDebugFunction(); 2540b57cec5SDimitry Andric return; 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric // Grab the lexical scopes for the function, if we don't have any of those 2580b57cec5SDimitry Andric // then we're not going to be able to do anything. 2590b57cec5SDimitry Andric LScopes.initialize(*MF); 2600b57cec5SDimitry Andric if (LScopes.empty()) { 2610b57cec5SDimitry Andric beginFunctionImpl(MF); 2620b57cec5SDimitry Andric return; 2630b57cec5SDimitry Andric } 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric // Make sure that each lexical scope will have a begin/end label. 2660b57cec5SDimitry Andric identifyScopeMarkers(); 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric // Calculate history for local variables. 2690b57cec5SDimitry Andric assert(DbgValues.empty() && "DbgValues map wasn't cleaned!"); 2700b57cec5SDimitry Andric assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!"); 2710b57cec5SDimitry Andric calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(), 2720b57cec5SDimitry Andric DbgValues, DbgLabels); 273e8d8bef9SDimitry Andric InstOrdering.initialize(*MF); 274e8d8bef9SDimitry Andric if (TrimVarLocs) 275e8d8bef9SDimitry Andric DbgValues.trimLocationRanges(*MF, LScopes, InstOrdering); 2760b57cec5SDimitry Andric LLVM_DEBUG(DbgValues.dump()); 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric // Request labels for the full history. 2790b57cec5SDimitry Andric for (const auto &I : DbgValues) { 2800b57cec5SDimitry Andric const auto &Entries = I.second; 2810b57cec5SDimitry Andric if (Entries.empty()) 2820b57cec5SDimitry Andric continue; 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric auto IsDescribedByReg = [](const MachineInstr *MI) { 285fe6060f1SDimitry Andric return any_of(MI->debug_operands(), 286fe6060f1SDimitry Andric [](auto &MO) { return MO.isReg() && MO.getReg(); }); 2870b57cec5SDimitry Andric }; 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric // The first mention of a function argument gets the CurrentFnBegin label, 2900b57cec5SDimitry Andric // so arguments are visible when breaking at function entry. 2910b57cec5SDimitry Andric // 2920b57cec5SDimitry Andric // We do not change the label for values that are described by registers, 2930b57cec5SDimitry Andric // as that could place them above their defining instructions. We should 2940b57cec5SDimitry Andric // ideally not change the labels for constant debug values either, since 2950b57cec5SDimitry Andric // doing that violates the ranges that are calculated in the history map. 2960b57cec5SDimitry Andric // However, we currently do not emit debug values for constant arguments 2970b57cec5SDimitry Andric // directly at the start of the function, so this code is still useful. 2980b57cec5SDimitry Andric const DILocalVariable *DIVar = 2990b57cec5SDimitry Andric Entries.front().getInstr()->getDebugVariable(); 3000b57cec5SDimitry Andric if (DIVar->isParameter() && 301fe6060f1SDimitry Andric getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) { 3020b57cec5SDimitry Andric if (!IsDescribedByReg(Entries.front().getInstr())) 3030b57cec5SDimitry Andric LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin(); 3040b57cec5SDimitry Andric if (Entries.front().getInstr()->getDebugExpression()->isFragment()) { 3050b57cec5SDimitry Andric // Mark all non-overlapping initial fragments. 306fcaf7f86SDimitry Andric for (const auto *I = Entries.begin(); I != Entries.end(); ++I) { 3070b57cec5SDimitry Andric if (!I->isDbgValue()) 3080b57cec5SDimitry Andric continue; 3090b57cec5SDimitry Andric const DIExpression *Fragment = I->getInstr()->getDebugExpression(); 3100b57cec5SDimitry Andric if (std::any_of(Entries.begin(), I, 3110b57cec5SDimitry Andric [&](DbgValueHistoryMap::Entry Pred) { 3120b57cec5SDimitry Andric return Pred.isDbgValue() && 3130b57cec5SDimitry Andric Fragment->fragmentsOverlap( 3140b57cec5SDimitry Andric Pred.getInstr()->getDebugExpression()); 3150b57cec5SDimitry Andric })) 3160b57cec5SDimitry Andric break; 3170b57cec5SDimitry Andric // The code that generates location lists for DWARF assumes that the 3180b57cec5SDimitry Andric // entries' start labels are monotonically increasing, and since we 3190b57cec5SDimitry Andric // don't change the label for fragments that are described by 3200b57cec5SDimitry Andric // registers, we must bail out when encountering such a fragment. 3210b57cec5SDimitry Andric if (IsDescribedByReg(I->getInstr())) 3220b57cec5SDimitry Andric break; 3230b57cec5SDimitry Andric LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin(); 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric for (const auto &Entry : Entries) { 3290b57cec5SDimitry Andric if (Entry.isDbgValue()) 3300b57cec5SDimitry Andric requestLabelBeforeInsn(Entry.getInstr()); 3310b57cec5SDimitry Andric else 3320b57cec5SDimitry Andric requestLabelAfterInsn(Entry.getInstr()); 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric } 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric // Ensure there is a symbol before DBG_LABEL. 3370b57cec5SDimitry Andric for (const auto &I : DbgLabels) { 3380b57cec5SDimitry Andric const MachineInstr *MI = I.second; 3390b57cec5SDimitry Andric requestLabelBeforeInsn(MI); 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric PrevInstLoc = DebugLoc(); 3430b57cec5SDimitry Andric PrevLabel = Asm->getFunctionBegin(); 3440b57cec5SDimitry Andric beginFunctionImpl(MF); 3450b57cec5SDimitry Andric } 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric void DebugHandlerBase::beginInstruction(const MachineInstr *MI) { 348e8d8bef9SDimitry Andric if (!Asm || !MMI->hasDebugInfo()) 3490b57cec5SDimitry Andric return; 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric assert(CurMI == nullptr); 3520b57cec5SDimitry Andric CurMI = MI; 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric // Insert labels where requested. 3550b57cec5SDimitry Andric DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 3560b57cec5SDimitry Andric LabelsBeforeInsn.find(MI); 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric // No label needed. 3590b57cec5SDimitry Andric if (I == LabelsBeforeInsn.end()) 3600b57cec5SDimitry Andric return; 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric // Label already assigned. 3630b57cec5SDimitry Andric if (I->second) 3640b57cec5SDimitry Andric return; 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric if (!PrevLabel) { 3670b57cec5SDimitry Andric PrevLabel = MMI->getContext().createTempSymbol(); 3685ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(PrevLabel); 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric I->second = PrevLabel; 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric void DebugHandlerBase::endInstruction() { 374e8d8bef9SDimitry Andric if (!Asm || !MMI->hasDebugInfo()) 3750b57cec5SDimitry Andric return; 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric assert(CurMI != nullptr); 3780b57cec5SDimitry Andric // Don't create a new label after DBG_VALUE and other instructions that don't 3790b57cec5SDimitry Andric // generate code. 3800b57cec5SDimitry Andric if (!CurMI->isMetaInstruction()) { 3810b57cec5SDimitry Andric PrevLabel = nullptr; 3820b57cec5SDimitry Andric PrevInstBB = CurMI->getParent(); 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 3860b57cec5SDimitry Andric LabelsAfterInsn.find(CurMI); 387fe6060f1SDimitry Andric 388fe6060f1SDimitry Andric // No label needed or label already assigned. 389fe6060f1SDimitry Andric if (I == LabelsAfterInsn.end() || I->second) { 3900b57cec5SDimitry Andric CurMI = nullptr; 3910b57cec5SDimitry Andric return; 392fe6060f1SDimitry Andric } 3930b57cec5SDimitry Andric 394fe6060f1SDimitry Andric // We need a label after this instruction. With basic block sections, just 395fe6060f1SDimitry Andric // use the end symbol of the section if this is the last instruction of the 396fe6060f1SDimitry Andric // section. This reduces the need for an additional label and also helps 397fe6060f1SDimitry Andric // merging ranges. 398fe6060f1SDimitry Andric if (CurMI->getParent()->isEndSection() && CurMI->getNextNode() == nullptr) { 399fe6060f1SDimitry Andric PrevLabel = CurMI->getParent()->getEndSymbol(); 400fe6060f1SDimitry Andric } else if (!PrevLabel) { 4010b57cec5SDimitry Andric PrevLabel = MMI->getContext().createTempSymbol(); 4025ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(PrevLabel); 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric I->second = PrevLabel; 405fe6060f1SDimitry Andric CurMI = nullptr; 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric void DebugHandlerBase::endFunction(const MachineFunction *MF) { 409e8d8bef9SDimitry Andric if (Asm && hasDebugInfo(MMI, MF)) 4100b57cec5SDimitry Andric endFunctionImpl(MF); 4110b57cec5SDimitry Andric DbgValues.clear(); 4120b57cec5SDimitry Andric DbgLabels.clear(); 4130b57cec5SDimitry Andric LabelsBeforeInsn.clear(); 4140b57cec5SDimitry Andric LabelsAfterInsn.clear(); 415e8d8bef9SDimitry Andric InstOrdering.clear(); 4160b57cec5SDimitry Andric } 4175ffd83dbSDimitry Andric 418*bdd1243dSDimitry Andric void DebugHandlerBase::beginBasicBlockSection(const MachineBasicBlock &MBB) { 419*bdd1243dSDimitry Andric EpilogBeginBlock = nullptr; 420*bdd1243dSDimitry Andric if (!MBB.isEntryBlock()) 4215ffd83dbSDimitry Andric PrevLabel = MBB.getSymbol(); 4225ffd83dbSDimitry Andric } 4235ffd83dbSDimitry Andric 424*bdd1243dSDimitry Andric void DebugHandlerBase::endBasicBlockSection(const MachineBasicBlock &MBB) { 4255ffd83dbSDimitry Andric PrevLabel = nullptr; 4265ffd83dbSDimitry Andric } 427