xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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/ADT/Optional.h"
160b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
220b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h"
230b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
24e8d8bef9SDimitry Andric #include "llvm/Support/CommandLine.h"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric #define DEBUG_TYPE "dwarfdebug"
290b57cec5SDimitry Andric 
30e8d8bef9SDimitry Andric /// If true, we drop variable location ranges which exist entirely outside the
31e8d8bef9SDimitry Andric /// variable's lexical scope instruction ranges.
32e8d8bef9SDimitry Andric static cl::opt<bool> TrimVarLocs("trim-var-locs", cl::Hidden, cl::init(true));
33e8d8bef9SDimitry Andric 
340b57cec5SDimitry Andric Optional<DbgVariableLocation>
350b57cec5SDimitry Andric DbgVariableLocation::extractFromMachineInstruction(
360b57cec5SDimitry Andric     const MachineInstr &Instruction) {
370b57cec5SDimitry Andric   DbgVariableLocation Location;
38*fe6060f1SDimitry Andric   // Variables calculated from multiple locations can't be represented here.
39*fe6060f1SDimitry Andric   if (Instruction.getNumDebugOperands() != 1)
400b57cec5SDimitry Andric     return None;
415ffd83dbSDimitry Andric   if (!Instruction.getDebugOperand(0).isReg())
420b57cec5SDimitry Andric     return None;
435ffd83dbSDimitry Andric   Location.Register = Instruction.getDebugOperand(0).getReg();
440b57cec5SDimitry Andric   Location.FragmentInfo.reset();
450b57cec5SDimitry Andric   // We only handle expressions generated by DIExpression::appendOffset,
460b57cec5SDimitry Andric   // which doesn't require a full stack machine.
470b57cec5SDimitry Andric   int64_t Offset = 0;
480b57cec5SDimitry Andric   const DIExpression *DIExpr = Instruction.getDebugExpression();
490b57cec5SDimitry Andric   auto Op = DIExpr->expr_op_begin();
50*fe6060f1SDimitry Andric   // We can handle a DBG_VALUE_LIST iff it has exactly one location operand that
51*fe6060f1SDimitry Andric   // appears exactly once at the start of the expression.
52*fe6060f1SDimitry Andric   if (Instruction.isDebugValueList()) {
53*fe6060f1SDimitry Andric     if (Instruction.getNumDebugOperands() == 1 &&
54*fe6060f1SDimitry Andric         Op->getOp() == dwarf::DW_OP_LLVM_arg)
55*fe6060f1SDimitry Andric       ++Op;
56*fe6060f1SDimitry Andric     else
57*fe6060f1SDimitry Andric       return None;
58*fe6060f1SDimitry Andric   }
590b57cec5SDimitry Andric   while (Op != DIExpr->expr_op_end()) {
600b57cec5SDimitry Andric     switch (Op->getOp()) {
610b57cec5SDimitry Andric     case dwarf::DW_OP_constu: {
620b57cec5SDimitry Andric       int Value = Op->getArg(0);
630b57cec5SDimitry Andric       ++Op;
640b57cec5SDimitry Andric       if (Op != DIExpr->expr_op_end()) {
650b57cec5SDimitry Andric         switch (Op->getOp()) {
660b57cec5SDimitry Andric         case dwarf::DW_OP_minus:
670b57cec5SDimitry Andric           Offset -= Value;
680b57cec5SDimitry Andric           break;
690b57cec5SDimitry Andric         case dwarf::DW_OP_plus:
700b57cec5SDimitry Andric           Offset += Value;
710b57cec5SDimitry Andric           break;
720b57cec5SDimitry Andric         default:
730b57cec5SDimitry Andric           continue;
740b57cec5SDimitry Andric         }
750b57cec5SDimitry Andric       }
760b57cec5SDimitry Andric     } break;
770b57cec5SDimitry Andric     case dwarf::DW_OP_plus_uconst:
780b57cec5SDimitry Andric       Offset += Op->getArg(0);
790b57cec5SDimitry Andric       break;
800b57cec5SDimitry Andric     case dwarf::DW_OP_LLVM_fragment:
810b57cec5SDimitry Andric       Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
820b57cec5SDimitry Andric       break;
830b57cec5SDimitry Andric     case dwarf::DW_OP_deref:
840b57cec5SDimitry Andric       Location.LoadChain.push_back(Offset);
850b57cec5SDimitry Andric       Offset = 0;
860b57cec5SDimitry Andric       break;
870b57cec5SDimitry Andric     default:
880b57cec5SDimitry Andric       return None;
890b57cec5SDimitry Andric     }
900b57cec5SDimitry Andric     ++Op;
910b57cec5SDimitry Andric   }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
940b57cec5SDimitry Andric   // instruction.
950b57cec5SDimitry Andric   // FIXME: Replace these with DIExpression.
960b57cec5SDimitry Andric   if (Instruction.isIndirectDebugValue())
970b57cec5SDimitry Andric     Location.LoadChain.push_back(Offset);
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   return Location;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
1030b57cec5SDimitry Andric 
104e8d8bef9SDimitry Andric void DebugHandlerBase::beginModule(Module *M) {
105e8d8bef9SDimitry Andric   if (M->debug_compile_units().empty())
106e8d8bef9SDimitry Andric     Asm = nullptr;
107e8d8bef9SDimitry Andric }
108e8d8bef9SDimitry Andric 
1090b57cec5SDimitry Andric // Each LexicalScope has first instruction and last instruction to mark
1100b57cec5SDimitry Andric // beginning and end of a scope respectively. Create an inverse map that list
1110b57cec5SDimitry Andric // scopes starts (and ends) with an instruction. One instruction may start (or
1120b57cec5SDimitry Andric // end) multiple scopes. Ignore scopes that are not reachable.
1130b57cec5SDimitry Andric void DebugHandlerBase::identifyScopeMarkers() {
1140b57cec5SDimitry Andric   SmallVector<LexicalScope *, 4> WorkList;
1150b57cec5SDimitry Andric   WorkList.push_back(LScopes.getCurrentFunctionScope());
1160b57cec5SDimitry Andric   while (!WorkList.empty()) {
1170b57cec5SDimitry Andric     LexicalScope *S = WorkList.pop_back_val();
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric     const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
1200b57cec5SDimitry Andric     if (!Children.empty())
1210b57cec5SDimitry Andric       WorkList.append(Children.begin(), Children.end());
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric     if (S->isAbstractScope())
1240b57cec5SDimitry Andric       continue;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric     for (const InsnRange &R : S->getRanges()) {
1270b57cec5SDimitry Andric       assert(R.first && "InsnRange does not have first instruction!");
1280b57cec5SDimitry Andric       assert(R.second && "InsnRange does not have second instruction!");
1290b57cec5SDimitry Andric       requestLabelBeforeInsn(R.first);
1300b57cec5SDimitry Andric       requestLabelAfterInsn(R.second);
1310b57cec5SDimitry Andric     }
1320b57cec5SDimitry Andric   }
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric // Return Label preceding the instruction.
1360b57cec5SDimitry Andric MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
1370b57cec5SDimitry Andric   MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1380b57cec5SDimitry Andric   assert(Label && "Didn't insert label before instruction");
1390b57cec5SDimitry Andric   return Label;
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric // Return Label immediately following the instruction.
1430b57cec5SDimitry Andric MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
1440b57cec5SDimitry Andric   return LabelsAfterInsn.lookup(MI);
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric /// If this type is derived from a base type then return base type size.
1480b57cec5SDimitry Andric uint64_t DebugHandlerBase::getBaseTypeSize(const DIType *Ty) {
1490b57cec5SDimitry Andric   assert(Ty);
1500b57cec5SDimitry Andric   const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
1510b57cec5SDimitry Andric   if (!DDTy)
1520b57cec5SDimitry Andric     return Ty->getSizeInBits();
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   unsigned Tag = DDTy->getTag();
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric   if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
1570b57cec5SDimitry Andric       Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
1580b57cec5SDimitry Andric       Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
1590b57cec5SDimitry Andric     return DDTy->getSizeInBits();
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   DIType *BaseType = DDTy->getBaseType();
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   if (!BaseType)
1640b57cec5SDimitry Andric     return 0;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   // If this is a derived type, go ahead and get the base type, unless it's a
1670b57cec5SDimitry Andric   // reference then it's just the size of the field. Pointer types have no need
1680b57cec5SDimitry Andric   // of this since they're a different type of qualification on the type.
1690b57cec5SDimitry Andric   if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
1700b57cec5SDimitry Andric       BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
1710b57cec5SDimitry Andric     return Ty->getSizeInBits();
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   return getBaseTypeSize(BaseType);
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
176e8d8bef9SDimitry Andric bool DebugHandlerBase::isUnsignedDIType(const DIType *Ty) {
177*fe6060f1SDimitry Andric   // SROA may generate dbg value intrinsics to assign an unsigned value to a
178*fe6060f1SDimitry Andric   // Fortran CHARACTER(1) type variables. Make them as unsigned.
179*fe6060f1SDimitry Andric   if (isa<DIStringType>(Ty)) {
180*fe6060f1SDimitry Andric     assert((Ty->getSizeInBits()) == 8 && "Not a valid unsigned type!");
181*fe6060f1SDimitry Andric     return true;
182*fe6060f1SDimitry Andric   }
183e8d8bef9SDimitry Andric   if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
184e8d8bef9SDimitry Andric     // FIXME: Enums without a fixed underlying type have unknown signedness
185e8d8bef9SDimitry Andric     // here, leading to incorrectly emitted constants.
186e8d8bef9SDimitry Andric     if (CTy->getTag() == dwarf::DW_TAG_enumeration_type)
187e8d8bef9SDimitry Andric       return false;
188e8d8bef9SDimitry Andric 
189e8d8bef9SDimitry Andric     // (Pieces of) aggregate types that get hacked apart by SROA may be
190e8d8bef9SDimitry Andric     // represented by a constant. Encode them as unsigned bytes.
191e8d8bef9SDimitry Andric     return true;
192e8d8bef9SDimitry Andric   }
193e8d8bef9SDimitry Andric 
194e8d8bef9SDimitry Andric   if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
195e8d8bef9SDimitry Andric     dwarf::Tag T = (dwarf::Tag)Ty->getTag();
196e8d8bef9SDimitry Andric     // Encode pointer constants as unsigned bytes. This is used at least for
197e8d8bef9SDimitry Andric     // null pointer constant emission.
198e8d8bef9SDimitry Andric     // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
199e8d8bef9SDimitry Andric     // here, but accept them for now due to a bug in SROA producing bogus
200e8d8bef9SDimitry Andric     // dbg.values.
201e8d8bef9SDimitry Andric     if (T == dwarf::DW_TAG_pointer_type ||
202e8d8bef9SDimitry Andric         T == dwarf::DW_TAG_ptr_to_member_type ||
203e8d8bef9SDimitry Andric         T == dwarf::DW_TAG_reference_type ||
204e8d8bef9SDimitry Andric         T == dwarf::DW_TAG_rvalue_reference_type)
205e8d8bef9SDimitry Andric       return true;
206e8d8bef9SDimitry Andric     assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
207e8d8bef9SDimitry Andric            T == dwarf::DW_TAG_volatile_type ||
208e8d8bef9SDimitry Andric            T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type);
209e8d8bef9SDimitry Andric     assert(DTy->getBaseType() && "Expected valid base type");
210e8d8bef9SDimitry Andric     return isUnsignedDIType(DTy->getBaseType());
211e8d8bef9SDimitry Andric   }
212e8d8bef9SDimitry Andric 
213e8d8bef9SDimitry Andric   auto *BTy = cast<DIBasicType>(Ty);
214e8d8bef9SDimitry Andric   unsigned Encoding = BTy->getEncoding();
215e8d8bef9SDimitry Andric   assert((Encoding == dwarf::DW_ATE_unsigned ||
216e8d8bef9SDimitry Andric           Encoding == dwarf::DW_ATE_unsigned_char ||
217e8d8bef9SDimitry Andric           Encoding == dwarf::DW_ATE_signed ||
218e8d8bef9SDimitry Andric           Encoding == dwarf::DW_ATE_signed_char ||
219e8d8bef9SDimitry Andric           Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
220e8d8bef9SDimitry Andric           Encoding == dwarf::DW_ATE_boolean ||
221e8d8bef9SDimitry Andric           (Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
222e8d8bef9SDimitry Andric            Ty->getName() == "decltype(nullptr)")) &&
223e8d8bef9SDimitry Andric          "Unsupported encoding");
224e8d8bef9SDimitry Andric   return Encoding == dwarf::DW_ATE_unsigned ||
225e8d8bef9SDimitry Andric          Encoding == dwarf::DW_ATE_unsigned_char ||
226e8d8bef9SDimitry Andric          Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
227e8d8bef9SDimitry Andric          Ty->getTag() == dwarf::DW_TAG_unspecified_type;
228e8d8bef9SDimitry Andric }
229e8d8bef9SDimitry Andric 
2300b57cec5SDimitry Andric static bool hasDebugInfo(const MachineModuleInfo *MMI,
2310b57cec5SDimitry Andric                          const MachineFunction *MF) {
2320b57cec5SDimitry Andric   if (!MMI->hasDebugInfo())
2330b57cec5SDimitry Andric     return false;
2340b57cec5SDimitry Andric   auto *SP = MF->getFunction().getSubprogram();
2350b57cec5SDimitry Andric   if (!SP)
2360b57cec5SDimitry Andric     return false;
2370b57cec5SDimitry Andric   assert(SP->getUnit());
2380b57cec5SDimitry Andric   auto EK = SP->getUnit()->getEmissionKind();
2390b57cec5SDimitry Andric   if (EK == DICompileUnit::NoDebug)
2400b57cec5SDimitry Andric     return false;
2410b57cec5SDimitry Andric   return true;
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
2450b57cec5SDimitry Andric   PrevInstBB = nullptr;
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   if (!Asm || !hasDebugInfo(MMI, MF)) {
2480b57cec5SDimitry Andric     skippedNonDebugFunction();
2490b57cec5SDimitry Andric     return;
2500b57cec5SDimitry Andric   }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   // Grab the lexical scopes for the function, if we don't have any of those
2530b57cec5SDimitry Andric   // then we're not going to be able to do anything.
2540b57cec5SDimitry Andric   LScopes.initialize(*MF);
2550b57cec5SDimitry Andric   if (LScopes.empty()) {
2560b57cec5SDimitry Andric     beginFunctionImpl(MF);
2570b57cec5SDimitry Andric     return;
2580b57cec5SDimitry Andric   }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric   // Make sure that each lexical scope will have a begin/end label.
2610b57cec5SDimitry Andric   identifyScopeMarkers();
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   // Calculate history for local variables.
2640b57cec5SDimitry Andric   assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
2650b57cec5SDimitry Andric   assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
2660b57cec5SDimitry Andric   calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
2670b57cec5SDimitry Andric                             DbgValues, DbgLabels);
268e8d8bef9SDimitry Andric   InstOrdering.initialize(*MF);
269e8d8bef9SDimitry Andric   if (TrimVarLocs)
270e8d8bef9SDimitry Andric     DbgValues.trimLocationRanges(*MF, LScopes, InstOrdering);
2710b57cec5SDimitry Andric   LLVM_DEBUG(DbgValues.dump());
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric   // Request labels for the full history.
2740b57cec5SDimitry Andric   for (const auto &I : DbgValues) {
2750b57cec5SDimitry Andric     const auto &Entries = I.second;
2760b57cec5SDimitry Andric     if (Entries.empty())
2770b57cec5SDimitry Andric       continue;
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric     auto IsDescribedByReg = [](const MachineInstr *MI) {
280*fe6060f1SDimitry Andric       return any_of(MI->debug_operands(),
281*fe6060f1SDimitry Andric                     [](auto &MO) { return MO.isReg() && MO.getReg(); });
2820b57cec5SDimitry Andric     };
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric     // The first mention of a function argument gets the CurrentFnBegin label,
2850b57cec5SDimitry Andric     // so arguments are visible when breaking at function entry.
2860b57cec5SDimitry Andric     //
2870b57cec5SDimitry Andric     // We do not change the label for values that are described by registers,
2880b57cec5SDimitry Andric     // as that could place them above their defining instructions. We should
2890b57cec5SDimitry Andric     // ideally not change the labels for constant debug values either, since
2900b57cec5SDimitry Andric     // doing that violates the ranges that are calculated in the history map.
2910b57cec5SDimitry Andric     // However, we currently do not emit debug values for constant arguments
2920b57cec5SDimitry Andric     // directly at the start of the function, so this code is still useful.
2930b57cec5SDimitry Andric     const DILocalVariable *DIVar =
2940b57cec5SDimitry Andric         Entries.front().getInstr()->getDebugVariable();
2950b57cec5SDimitry Andric     if (DIVar->isParameter() &&
296*fe6060f1SDimitry Andric         getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
2970b57cec5SDimitry Andric       if (!IsDescribedByReg(Entries.front().getInstr()))
2980b57cec5SDimitry Andric         LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin();
2990b57cec5SDimitry Andric       if (Entries.front().getInstr()->getDebugExpression()->isFragment()) {
3000b57cec5SDimitry Andric         // Mark all non-overlapping initial fragments.
3010b57cec5SDimitry Andric         for (auto I = Entries.begin(); I != Entries.end(); ++I) {
3020b57cec5SDimitry Andric           if (!I->isDbgValue())
3030b57cec5SDimitry Andric             continue;
3040b57cec5SDimitry Andric           const DIExpression *Fragment = I->getInstr()->getDebugExpression();
3050b57cec5SDimitry Andric           if (std::any_of(Entries.begin(), I,
3060b57cec5SDimitry Andric                           [&](DbgValueHistoryMap::Entry Pred) {
3070b57cec5SDimitry Andric                             return Pred.isDbgValue() &&
3080b57cec5SDimitry Andric                                    Fragment->fragmentsOverlap(
3090b57cec5SDimitry Andric                                        Pred.getInstr()->getDebugExpression());
3100b57cec5SDimitry Andric                           }))
3110b57cec5SDimitry Andric             break;
3120b57cec5SDimitry Andric           // The code that generates location lists for DWARF assumes that the
3130b57cec5SDimitry Andric           // entries' start labels are monotonically increasing, and since we
3140b57cec5SDimitry Andric           // don't change the label for fragments that are described by
3150b57cec5SDimitry Andric           // registers, we must bail out when encountering such a fragment.
3160b57cec5SDimitry Andric           if (IsDescribedByReg(I->getInstr()))
3170b57cec5SDimitry Andric             break;
3180b57cec5SDimitry Andric           LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin();
3190b57cec5SDimitry Andric         }
3200b57cec5SDimitry Andric       }
3210b57cec5SDimitry Andric     }
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric     for (const auto &Entry : Entries) {
3240b57cec5SDimitry Andric       if (Entry.isDbgValue())
3250b57cec5SDimitry Andric         requestLabelBeforeInsn(Entry.getInstr());
3260b57cec5SDimitry Andric       else
3270b57cec5SDimitry Andric         requestLabelAfterInsn(Entry.getInstr());
3280b57cec5SDimitry Andric     }
3290b57cec5SDimitry Andric   }
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric   // Ensure there is a symbol before DBG_LABEL.
3320b57cec5SDimitry Andric   for (const auto &I : DbgLabels) {
3330b57cec5SDimitry Andric     const MachineInstr *MI = I.second;
3340b57cec5SDimitry Andric     requestLabelBeforeInsn(MI);
3350b57cec5SDimitry Andric   }
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   PrevInstLoc = DebugLoc();
3380b57cec5SDimitry Andric   PrevLabel = Asm->getFunctionBegin();
3390b57cec5SDimitry Andric   beginFunctionImpl(MF);
3400b57cec5SDimitry Andric }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
343e8d8bef9SDimitry Andric   if (!Asm || !MMI->hasDebugInfo())
3440b57cec5SDimitry Andric     return;
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric   assert(CurMI == nullptr);
3470b57cec5SDimitry Andric   CurMI = MI;
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric   // Insert labels where requested.
3500b57cec5SDimitry Andric   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
3510b57cec5SDimitry Andric       LabelsBeforeInsn.find(MI);
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric   // No label needed.
3540b57cec5SDimitry Andric   if (I == LabelsBeforeInsn.end())
3550b57cec5SDimitry Andric     return;
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   // Label already assigned.
3580b57cec5SDimitry Andric   if (I->second)
3590b57cec5SDimitry Andric     return;
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   if (!PrevLabel) {
3620b57cec5SDimitry Andric     PrevLabel = MMI->getContext().createTempSymbol();
3635ffd83dbSDimitry Andric     Asm->OutStreamer->emitLabel(PrevLabel);
3640b57cec5SDimitry Andric   }
3650b57cec5SDimitry Andric   I->second = PrevLabel;
3660b57cec5SDimitry Andric }
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric void DebugHandlerBase::endInstruction() {
369e8d8bef9SDimitry Andric   if (!Asm || !MMI->hasDebugInfo())
3700b57cec5SDimitry Andric     return;
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   assert(CurMI != nullptr);
3730b57cec5SDimitry Andric   // Don't create a new label after DBG_VALUE and other instructions that don't
3740b57cec5SDimitry Andric   // generate code.
3750b57cec5SDimitry Andric   if (!CurMI->isMetaInstruction()) {
3760b57cec5SDimitry Andric     PrevLabel = nullptr;
3770b57cec5SDimitry Andric     PrevInstBB = CurMI->getParent();
3780b57cec5SDimitry Andric   }
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
3810b57cec5SDimitry Andric       LabelsAfterInsn.find(CurMI);
382*fe6060f1SDimitry Andric 
383*fe6060f1SDimitry Andric   // No label needed or label already assigned.
384*fe6060f1SDimitry Andric   if (I == LabelsAfterInsn.end() || I->second) {
3850b57cec5SDimitry Andric     CurMI = nullptr;
3860b57cec5SDimitry Andric     return;
387*fe6060f1SDimitry Andric   }
3880b57cec5SDimitry Andric 
389*fe6060f1SDimitry Andric   // We need a label after this instruction.  With basic block sections, just
390*fe6060f1SDimitry Andric   // use the end symbol of the section if this is the last instruction of the
391*fe6060f1SDimitry Andric   // section.  This reduces the need for an additional label and also helps
392*fe6060f1SDimitry Andric   // merging ranges.
393*fe6060f1SDimitry Andric   if (CurMI->getParent()->isEndSection() && CurMI->getNextNode() == nullptr) {
394*fe6060f1SDimitry Andric     PrevLabel = CurMI->getParent()->getEndSymbol();
395*fe6060f1SDimitry Andric   } else if (!PrevLabel) {
3960b57cec5SDimitry Andric     PrevLabel = MMI->getContext().createTempSymbol();
3975ffd83dbSDimitry Andric     Asm->OutStreamer->emitLabel(PrevLabel);
3980b57cec5SDimitry Andric   }
3990b57cec5SDimitry Andric   I->second = PrevLabel;
400*fe6060f1SDimitry Andric   CurMI = nullptr;
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric void DebugHandlerBase::endFunction(const MachineFunction *MF) {
404e8d8bef9SDimitry Andric   if (Asm && hasDebugInfo(MMI, MF))
4050b57cec5SDimitry Andric     endFunctionImpl(MF);
4060b57cec5SDimitry Andric   DbgValues.clear();
4070b57cec5SDimitry Andric   DbgLabels.clear();
4080b57cec5SDimitry Andric   LabelsBeforeInsn.clear();
4090b57cec5SDimitry Andric   LabelsAfterInsn.clear();
410e8d8bef9SDimitry Andric   InstOrdering.clear();
4110b57cec5SDimitry Andric }
4125ffd83dbSDimitry Andric 
4135ffd83dbSDimitry Andric void DebugHandlerBase::beginBasicBlock(const MachineBasicBlock &MBB) {
4145ffd83dbSDimitry Andric   if (!MBB.isBeginSection())
4155ffd83dbSDimitry Andric     return;
4165ffd83dbSDimitry Andric 
4175ffd83dbSDimitry Andric   PrevLabel = MBB.getSymbol();
4185ffd83dbSDimitry Andric }
4195ffd83dbSDimitry Andric 
4205ffd83dbSDimitry Andric void DebugHandlerBase::endBasicBlock(const MachineBasicBlock &MBB) {
4215ffd83dbSDimitry Andric   if (!MBB.isEndSection())
4225ffd83dbSDimitry Andric     return;
4235ffd83dbSDimitry Andric 
4245ffd83dbSDimitry Andric   PrevLabel = nullptr;
4255ffd83dbSDimitry Andric }
426