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
32bdd1243dSDimitry Andric std::optional<DbgVariableLocation>
extractFromMachineInstruction(const MachineInstr & Instruction)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)
38bdd1243dSDimitry Andric return std::nullopt;
395ffd83dbSDimitry Andric if (!Instruction.getDebugOperand(0).isReg())
40bdd1243dSDimitry 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
55bdd1243dSDimitry 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:
86bdd1243dSDimitry 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
DebugHandlerBase(AsmPrinter * A)1000b57cec5SDimitry Andric DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
1010b57cec5SDimitry Andric
beginModule(Module * M)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.
identifyScopeMarkers()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.
getLabelBeforeInsn(const MachineInstr * MI)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.
getLabelAfterInsn(const MachineInstr * MI)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.
getBaseTypeSize(const DIType * Ty)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
isUnsignedDIType(const DIType * Ty)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 ||
226*fe013be4SDimitry Andric Encoding == dwarf::DW_ATE_complex_float ||
227e8d8bef9SDimitry Andric (Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
228e8d8bef9SDimitry Andric Ty->getName() == "decltype(nullptr)")) &&
229e8d8bef9SDimitry Andric "Unsupported encoding");
230e8d8bef9SDimitry Andric return Encoding == dwarf::DW_ATE_unsigned ||
231e8d8bef9SDimitry Andric Encoding == dwarf::DW_ATE_unsigned_char ||
232e8d8bef9SDimitry Andric Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
233e8d8bef9SDimitry Andric Ty->getTag() == dwarf::DW_TAG_unspecified_type;
234e8d8bef9SDimitry Andric }
235e8d8bef9SDimitry Andric
hasDebugInfo(const MachineModuleInfo * MMI,const MachineFunction * MF)2360b57cec5SDimitry Andric static bool hasDebugInfo(const MachineModuleInfo *MMI,
2370b57cec5SDimitry Andric const MachineFunction *MF) {
2380b57cec5SDimitry Andric if (!MMI->hasDebugInfo())
2390b57cec5SDimitry Andric return false;
2400b57cec5SDimitry Andric auto *SP = MF->getFunction().getSubprogram();
2410b57cec5SDimitry Andric if (!SP)
2420b57cec5SDimitry Andric return false;
2430b57cec5SDimitry Andric assert(SP->getUnit());
2440b57cec5SDimitry Andric auto EK = SP->getUnit()->getEmissionKind();
2450b57cec5SDimitry Andric if (EK == DICompileUnit::NoDebug)
2460b57cec5SDimitry Andric return false;
2470b57cec5SDimitry Andric return true;
2480b57cec5SDimitry Andric }
2490b57cec5SDimitry Andric
beginFunction(const MachineFunction * MF)2500b57cec5SDimitry Andric void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
2510b57cec5SDimitry Andric PrevInstBB = nullptr;
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andric if (!Asm || !hasDebugInfo(MMI, MF)) {
2540b57cec5SDimitry Andric skippedNonDebugFunction();
2550b57cec5SDimitry Andric return;
2560b57cec5SDimitry Andric }
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andric // Grab the lexical scopes for the function, if we don't have any of those
2590b57cec5SDimitry Andric // then we're not going to be able to do anything.
2600b57cec5SDimitry Andric LScopes.initialize(*MF);
2610b57cec5SDimitry Andric if (LScopes.empty()) {
2620b57cec5SDimitry Andric beginFunctionImpl(MF);
2630b57cec5SDimitry Andric return;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric
2660b57cec5SDimitry Andric // Make sure that each lexical scope will have a begin/end label.
2670b57cec5SDimitry Andric identifyScopeMarkers();
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andric // Calculate history for local variables.
2700b57cec5SDimitry Andric assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
2710b57cec5SDimitry Andric assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
2720b57cec5SDimitry Andric calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
2730b57cec5SDimitry Andric DbgValues, DbgLabels);
274e8d8bef9SDimitry Andric InstOrdering.initialize(*MF);
275e8d8bef9SDimitry Andric if (TrimVarLocs)
276e8d8bef9SDimitry Andric DbgValues.trimLocationRanges(*MF, LScopes, InstOrdering);
277*fe013be4SDimitry Andric LLVM_DEBUG(DbgValues.dump(MF->getName()));
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andric // Request labels for the full history.
2800b57cec5SDimitry Andric for (const auto &I : DbgValues) {
2810b57cec5SDimitry Andric const auto &Entries = I.second;
2820b57cec5SDimitry Andric if (Entries.empty())
2830b57cec5SDimitry Andric continue;
2840b57cec5SDimitry Andric
2850b57cec5SDimitry Andric auto IsDescribedByReg = [](const MachineInstr *MI) {
286fe6060f1SDimitry Andric return any_of(MI->debug_operands(),
287fe6060f1SDimitry Andric [](auto &MO) { return MO.isReg() && MO.getReg(); });
2880b57cec5SDimitry Andric };
2890b57cec5SDimitry Andric
2900b57cec5SDimitry Andric // The first mention of a function argument gets the CurrentFnBegin label,
2910b57cec5SDimitry Andric // so arguments are visible when breaking at function entry.
2920b57cec5SDimitry Andric //
2930b57cec5SDimitry Andric // We do not change the label for values that are described by registers,
2940b57cec5SDimitry Andric // as that could place them above their defining instructions. We should
2950b57cec5SDimitry Andric // ideally not change the labels for constant debug values either, since
2960b57cec5SDimitry Andric // doing that violates the ranges that are calculated in the history map.
2970b57cec5SDimitry Andric // However, we currently do not emit debug values for constant arguments
2980b57cec5SDimitry Andric // directly at the start of the function, so this code is still useful.
2990b57cec5SDimitry Andric const DILocalVariable *DIVar =
3000b57cec5SDimitry Andric Entries.front().getInstr()->getDebugVariable();
3010b57cec5SDimitry Andric if (DIVar->isParameter() &&
302fe6060f1SDimitry Andric getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
3030b57cec5SDimitry Andric if (!IsDescribedByReg(Entries.front().getInstr()))
3040b57cec5SDimitry Andric LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin();
3050b57cec5SDimitry Andric if (Entries.front().getInstr()->getDebugExpression()->isFragment()) {
3060b57cec5SDimitry Andric // Mark all non-overlapping initial fragments.
307fcaf7f86SDimitry Andric for (const auto *I = Entries.begin(); I != Entries.end(); ++I) {
3080b57cec5SDimitry Andric if (!I->isDbgValue())
3090b57cec5SDimitry Andric continue;
3100b57cec5SDimitry Andric const DIExpression *Fragment = I->getInstr()->getDebugExpression();
3110b57cec5SDimitry Andric if (std::any_of(Entries.begin(), I,
3120b57cec5SDimitry Andric [&](DbgValueHistoryMap::Entry Pred) {
3130b57cec5SDimitry Andric return Pred.isDbgValue() &&
3140b57cec5SDimitry Andric Fragment->fragmentsOverlap(
3150b57cec5SDimitry Andric Pred.getInstr()->getDebugExpression());
3160b57cec5SDimitry Andric }))
3170b57cec5SDimitry Andric break;
3180b57cec5SDimitry Andric // The code that generates location lists for DWARF assumes that the
3190b57cec5SDimitry Andric // entries' start labels are monotonically increasing, and since we
3200b57cec5SDimitry Andric // don't change the label for fragments that are described by
3210b57cec5SDimitry Andric // registers, we must bail out when encountering such a fragment.
3220b57cec5SDimitry Andric if (IsDescribedByReg(I->getInstr()))
3230b57cec5SDimitry Andric break;
3240b57cec5SDimitry Andric LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin();
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric
3290b57cec5SDimitry Andric for (const auto &Entry : Entries) {
3300b57cec5SDimitry Andric if (Entry.isDbgValue())
3310b57cec5SDimitry Andric requestLabelBeforeInsn(Entry.getInstr());
3320b57cec5SDimitry Andric else
3330b57cec5SDimitry Andric requestLabelAfterInsn(Entry.getInstr());
3340b57cec5SDimitry Andric }
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric
3370b57cec5SDimitry Andric // Ensure there is a symbol before DBG_LABEL.
3380b57cec5SDimitry Andric for (const auto &I : DbgLabels) {
3390b57cec5SDimitry Andric const MachineInstr *MI = I.second;
3400b57cec5SDimitry Andric requestLabelBeforeInsn(MI);
3410b57cec5SDimitry Andric }
3420b57cec5SDimitry Andric
3430b57cec5SDimitry Andric PrevInstLoc = DebugLoc();
3440b57cec5SDimitry Andric PrevLabel = Asm->getFunctionBegin();
3450b57cec5SDimitry Andric beginFunctionImpl(MF);
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric
beginInstruction(const MachineInstr * MI)3480b57cec5SDimitry Andric void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
349e8d8bef9SDimitry Andric if (!Asm || !MMI->hasDebugInfo())
3500b57cec5SDimitry Andric return;
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andric assert(CurMI == nullptr);
3530b57cec5SDimitry Andric CurMI = MI;
3540b57cec5SDimitry Andric
3550b57cec5SDimitry Andric // Insert labels where requested.
3560b57cec5SDimitry Andric DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
3570b57cec5SDimitry Andric LabelsBeforeInsn.find(MI);
3580b57cec5SDimitry Andric
3590b57cec5SDimitry Andric // No label needed.
3600b57cec5SDimitry Andric if (I == LabelsBeforeInsn.end())
3610b57cec5SDimitry Andric return;
3620b57cec5SDimitry Andric
3630b57cec5SDimitry Andric // Label already assigned.
3640b57cec5SDimitry Andric if (I->second)
3650b57cec5SDimitry Andric return;
3660b57cec5SDimitry Andric
3670b57cec5SDimitry Andric if (!PrevLabel) {
3680b57cec5SDimitry Andric PrevLabel = MMI->getContext().createTempSymbol();
3695ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(PrevLabel);
3700b57cec5SDimitry Andric }
3710b57cec5SDimitry Andric I->second = PrevLabel;
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric
endInstruction()3740b57cec5SDimitry Andric void DebugHandlerBase::endInstruction() {
375e8d8bef9SDimitry Andric if (!Asm || !MMI->hasDebugInfo())
3760b57cec5SDimitry Andric return;
3770b57cec5SDimitry Andric
3780b57cec5SDimitry Andric assert(CurMI != nullptr);
3790b57cec5SDimitry Andric // Don't create a new label after DBG_VALUE and other instructions that don't
3800b57cec5SDimitry Andric // generate code.
3810b57cec5SDimitry Andric if (!CurMI->isMetaInstruction()) {
3820b57cec5SDimitry Andric PrevLabel = nullptr;
3830b57cec5SDimitry Andric PrevInstBB = CurMI->getParent();
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric
3860b57cec5SDimitry Andric DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
3870b57cec5SDimitry Andric LabelsAfterInsn.find(CurMI);
388fe6060f1SDimitry Andric
389fe6060f1SDimitry Andric // No label needed or label already assigned.
390fe6060f1SDimitry Andric if (I == LabelsAfterInsn.end() || I->second) {
3910b57cec5SDimitry Andric CurMI = nullptr;
3920b57cec5SDimitry Andric return;
393fe6060f1SDimitry Andric }
3940b57cec5SDimitry Andric
395fe6060f1SDimitry Andric // We need a label after this instruction. With basic block sections, just
396fe6060f1SDimitry Andric // use the end symbol of the section if this is the last instruction of the
397fe6060f1SDimitry Andric // section. This reduces the need for an additional label and also helps
398fe6060f1SDimitry Andric // merging ranges.
399fe6060f1SDimitry Andric if (CurMI->getParent()->isEndSection() && CurMI->getNextNode() == nullptr) {
400fe6060f1SDimitry Andric PrevLabel = CurMI->getParent()->getEndSymbol();
401fe6060f1SDimitry Andric } else if (!PrevLabel) {
4020b57cec5SDimitry Andric PrevLabel = MMI->getContext().createTempSymbol();
4035ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(PrevLabel);
4040b57cec5SDimitry Andric }
4050b57cec5SDimitry Andric I->second = PrevLabel;
406fe6060f1SDimitry Andric CurMI = nullptr;
4070b57cec5SDimitry Andric }
4080b57cec5SDimitry Andric
endFunction(const MachineFunction * MF)4090b57cec5SDimitry Andric void DebugHandlerBase::endFunction(const MachineFunction *MF) {
410e8d8bef9SDimitry Andric if (Asm && hasDebugInfo(MMI, MF))
4110b57cec5SDimitry Andric endFunctionImpl(MF);
4120b57cec5SDimitry Andric DbgValues.clear();
4130b57cec5SDimitry Andric DbgLabels.clear();
4140b57cec5SDimitry Andric LabelsBeforeInsn.clear();
4150b57cec5SDimitry Andric LabelsAfterInsn.clear();
416e8d8bef9SDimitry Andric InstOrdering.clear();
4170b57cec5SDimitry Andric }
4185ffd83dbSDimitry Andric
beginBasicBlockSection(const MachineBasicBlock & MBB)419bdd1243dSDimitry Andric void DebugHandlerBase::beginBasicBlockSection(const MachineBasicBlock &MBB) {
420bdd1243dSDimitry Andric EpilogBeginBlock = nullptr;
421bdd1243dSDimitry Andric if (!MBB.isEntryBlock())
4225ffd83dbSDimitry Andric PrevLabel = MBB.getSymbol();
4235ffd83dbSDimitry Andric }
4245ffd83dbSDimitry Andric
endBasicBlockSection(const MachineBasicBlock & MBB)425bdd1243dSDimitry Andric void DebugHandlerBase::endBasicBlockSection(const MachineBasicBlock &MBB) {
4265ffd83dbSDimitry Andric PrevLabel = nullptr;
4275ffd83dbSDimitry Andric }
428