1f9c275feSReid Kleckner //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
2f9c275feSReid Kleckner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f9c275feSReid Kleckner //
7f9c275feSReid Kleckner //===----------------------------------------------------------------------===//
8f9c275feSReid Kleckner //
9f9c275feSReid Kleckner // Common functionality for different debug information format backends.
10f4873346SYonghong Song // LLVM currently supports DWARF and CodeView.
11f9c275feSReid Kleckner //
12f9c275feSReid Kleckner //===----------------------------------------------------------------------===//
13f9c275feSReid Kleckner 
1461b189e0SYonghong Song #include "llvm/CodeGen/DebugHandlerBase.h"
151a4cbbe4SBob Haarman #include "llvm/ADT/Optional.h"
16f9c275feSReid Kleckner #include "llvm/CodeGen/AsmPrinter.h"
17f9c275feSReid Kleckner #include "llvm/CodeGen/MachineFunction.h"
18f9c275feSReid Kleckner #include "llvm/CodeGen/MachineInstr.h"
19f9c275feSReid Kleckner #include "llvm/CodeGen/MachineModuleInfo.h"
20b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetSubtargetInfo.h"
2128865809SReid Kleckner #include "llvm/IR/DebugInfo.h"
22a5b1eef8SReid Kleckner #include "llvm/MC/MCStreamer.h"
23ce6de374SOCHyams #include "llvm/Support/CommandLine.h"
24f9c275feSReid Kleckner 
25f9c275feSReid Kleckner using namespace llvm;
26f9c275feSReid Kleckner 
277224c081SVedant Kumar #define DEBUG_TYPE "dwarfdebug"
287224c081SVedant Kumar 
29ce6de374SOCHyams /// If true, we drop variable location ranges which exist entirely outside the
30ce6de374SOCHyams /// variable's lexical scope instruction ranges.
31ce6de374SOCHyams static cl::opt<bool> TrimVarLocs("trim-var-locs", cl::Hidden, cl::init(true));
32ce6de374SOCHyams 
331a4cbbe4SBob Haarman Optional<DbgVariableLocation>
extractFromMachineInstruction(const MachineInstr & Instruction)341a4cbbe4SBob Haarman DbgVariableLocation::extractFromMachineInstruction(
351a4cbbe4SBob Haarman     const MachineInstr &Instruction) {
361a4cbbe4SBob Haarman   DbgVariableLocation Location;
37e64f3cccSStephen Tozer   // Variables calculated from multiple locations can't be represented here.
38e64f3cccSStephen Tozer   if (Instruction.getNumDebugOperands() != 1)
391a4cbbe4SBob Haarman     return None;
40539381daSstozer   if (!Instruction.getDebugOperand(0).isReg())
411a4cbbe4SBob Haarman     return None;
42539381daSstozer   Location.Register = Instruction.getDebugOperand(0).getReg();
43223303c5SBob Haarman   Location.FragmentInfo.reset();
44223303c5SBob Haarman   // We only handle expressions generated by DIExpression::appendOffset,
45223303c5SBob Haarman   // which doesn't require a full stack machine.
46223303c5SBob Haarman   int64_t Offset = 0;
47223303c5SBob Haarman   const DIExpression *DIExpr = Instruction.getDebugExpression();
48223303c5SBob Haarman   auto Op = DIExpr->expr_op_begin();
49e64f3cccSStephen Tozer   // We can handle a DBG_VALUE_LIST iff it has exactly one location operand that
50e64f3cccSStephen Tozer   // appears exactly once at the start of the expression.
51e64f3cccSStephen Tozer   if (Instruction.isDebugValueList()) {
52e64f3cccSStephen Tozer     if (Instruction.getNumDebugOperands() == 1 &&
53e64f3cccSStephen Tozer         Op->getOp() == dwarf::DW_OP_LLVM_arg)
54e64f3cccSStephen Tozer       ++Op;
55e64f3cccSStephen Tozer     else
56e64f3cccSStephen Tozer       return None;
57e64f3cccSStephen Tozer   }
58223303c5SBob Haarman   while (Op != DIExpr->expr_op_end()) {
59223303c5SBob Haarman     switch (Op->getOp()) {
60223303c5SBob Haarman     case dwarf::DW_OP_constu: {
61223303c5SBob Haarman       int Value = Op->getArg(0);
62223303c5SBob Haarman       ++Op;
63223303c5SBob Haarman       if (Op != DIExpr->expr_op_end()) {
64223303c5SBob Haarman         switch (Op->getOp()) {
65223303c5SBob Haarman         case dwarf::DW_OP_minus:
66223303c5SBob Haarman           Offset -= Value;
67223303c5SBob Haarman           break;
68223303c5SBob Haarman         case dwarf::DW_OP_plus:
69223303c5SBob Haarman           Offset += Value;
7068e46019SBob Haarman           break;
71223303c5SBob Haarman         default:
72223303c5SBob Haarman           continue;
73223303c5SBob Haarman         }
74223303c5SBob Haarman       }
75223303c5SBob Haarman     } break;
76223303c5SBob Haarman     case dwarf::DW_OP_plus_uconst:
77223303c5SBob Haarman       Offset += Op->getArg(0);
78223303c5SBob Haarman       break;
79223303c5SBob Haarman     case dwarf::DW_OP_LLVM_fragment:
80223303c5SBob Haarman       Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
81223303c5SBob Haarman       break;
82223303c5SBob Haarman     case dwarf::DW_OP_deref:
8308f5fd51SReid Kleckner       Location.LoadChain.push_back(Offset);
8408f5fd51SReid Kleckner       Offset = 0;
85223303c5SBob Haarman       break;
86223303c5SBob Haarman     default:
871a4cbbe4SBob Haarman       return None;
88223303c5SBob Haarman     }
89223303c5SBob Haarman     ++Op;
90223303c5SBob Haarman   }
91223303c5SBob Haarman 
9208f5fd51SReid Kleckner   // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
9308f5fd51SReid Kleckner   // instruction.
9408f5fd51SReid Kleckner   // FIXME: Replace these with DIExpression.
9508f5fd51SReid Kleckner   if (Instruction.isIndirectDebugValue())
9608f5fd51SReid Kleckner     Location.LoadChain.push_back(Offset);
9708f5fd51SReid Kleckner 
981a4cbbe4SBob Haarman   return Location;
99223303c5SBob Haarman }
100223303c5SBob Haarman 
DebugHandlerBase(AsmPrinter * A)101f9c275feSReid Kleckner DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
102f9c275feSReid Kleckner 
beginModule(Module * M)103a0ad066cSJameson Nash void DebugHandlerBase::beginModule(Module *M) {
104a0ad066cSJameson Nash   if (M->debug_compile_units().empty())
105a0ad066cSJameson Nash     Asm = nullptr;
106a0ad066cSJameson Nash }
107a0ad066cSJameson Nash 
108f9c275feSReid Kleckner // Each LexicalScope has first instruction and last instruction to mark
109f9c275feSReid Kleckner // beginning and end of a scope respectively. Create an inverse map that list
110f9c275feSReid Kleckner // scopes starts (and ends) with an instruction. One instruction may start (or
111f9c275feSReid Kleckner // end) multiple scopes. Ignore scopes that are not reachable.
identifyScopeMarkers()112f9c275feSReid Kleckner void DebugHandlerBase::identifyScopeMarkers() {
113f9c275feSReid Kleckner   SmallVector<LexicalScope *, 4> WorkList;
114f9c275feSReid Kleckner   WorkList.push_back(LScopes.getCurrentFunctionScope());
115f9c275feSReid Kleckner   while (!WorkList.empty()) {
116f9c275feSReid Kleckner     LexicalScope *S = WorkList.pop_back_val();
117f9c275feSReid Kleckner 
118f9c275feSReid Kleckner     const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
119f9c275feSReid Kleckner     if (!Children.empty())
120f9c275feSReid Kleckner       WorkList.append(Children.begin(), Children.end());
121f9c275feSReid Kleckner 
122f9c275feSReid Kleckner     if (S->isAbstractScope())
123f9c275feSReid Kleckner       continue;
124f9c275feSReid Kleckner 
125f9c275feSReid Kleckner     for (const InsnRange &R : S->getRanges()) {
126f9c275feSReid Kleckner       assert(R.first && "InsnRange does not have first instruction!");
127f9c275feSReid Kleckner       assert(R.second && "InsnRange does not have second instruction!");
128f9c275feSReid Kleckner       requestLabelBeforeInsn(R.first);
129f9c275feSReid Kleckner       requestLabelAfterInsn(R.second);
130f9c275feSReid Kleckner     }
131f9c275feSReid Kleckner   }
132f9c275feSReid Kleckner }
133f9c275feSReid Kleckner 
134f9c275feSReid Kleckner // Return Label preceding the instruction.
getLabelBeforeInsn(const MachineInstr * MI)135f9c275feSReid Kleckner MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
136f9c275feSReid Kleckner   MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
137f9c275feSReid Kleckner   assert(Label && "Didn't insert label before instruction");
138f9c275feSReid Kleckner   return Label;
139f9c275feSReid Kleckner }
140f9c275feSReid Kleckner 
141f9c275feSReid Kleckner // Return Label immediately following the instruction.
getLabelAfterInsn(const MachineInstr * MI)142f9c275feSReid Kleckner MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
143f9c275feSReid Kleckner   return LabelsAfterInsn.lookup(MI);
144f9c275feSReid Kleckner }
145f9c275feSReid Kleckner 
146acee5685SAmjad Aboud /// If this type is derived from a base type then return base type size.
getBaseTypeSize(const DIType * Ty)147da82ce99SFangrui Song uint64_t DebugHandlerBase::getBaseTypeSize(const DIType *Ty) {
148acee5685SAmjad Aboud   assert(Ty);
149da82ce99SFangrui Song   const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
150acee5685SAmjad Aboud   if (!DDTy)
151acee5685SAmjad Aboud     return Ty->getSizeInBits();
152acee5685SAmjad Aboud 
153acee5685SAmjad Aboud   unsigned Tag = DDTy->getTag();
154acee5685SAmjad Aboud 
155acee5685SAmjad Aboud   if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
156acee5685SAmjad Aboud       Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
15734435fd1SLuís Ferreira       Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type &&
15834435fd1SLuís Ferreira       Tag != dwarf::DW_TAG_immutable_type)
159acee5685SAmjad Aboud     return DDTy->getSizeInBits();
160acee5685SAmjad Aboud 
161da82ce99SFangrui Song   DIType *BaseType = DDTy->getBaseType();
162acee5685SAmjad Aboud 
16374bfafa1SAdrian McCarthy   if (!BaseType)
16474bfafa1SAdrian McCarthy     return 0;
165acee5685SAmjad Aboud 
166acee5685SAmjad Aboud   // If this is a derived type, go ahead and get the base type, unless it's a
167acee5685SAmjad Aboud   // reference then it's just the size of the field. Pointer types have no need
168acee5685SAmjad Aboud   // of this since they're a different type of qualification on the type.
169acee5685SAmjad Aboud   if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
170acee5685SAmjad Aboud       BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
171acee5685SAmjad Aboud     return Ty->getSizeInBits();
172acee5685SAmjad Aboud 
173acee5685SAmjad Aboud   return getBaseTypeSize(BaseType);
174acee5685SAmjad Aboud }
175acee5685SAmjad Aboud 
isUnsignedDIType(const DIType * Ty)1767669f3c0SAmy Huang bool DebugHandlerBase::isUnsignedDIType(const DIType *Ty) {
177bb346146SChen Zheng   if (isa<DIStringType>(Ty)) {
178070090cfSChih-Ping Chen     // Some transformations (e.g. instcombine) may decide to turn a Fortran
179070090cfSChih-Ping Chen     // character object into an integer, and later ones (e.g. SROA) may
180070090cfSChih-Ping Chen     // further inject a constant integer in a llvm.dbg.value call to track
181070090cfSChih-Ping Chen     // the object's value. Here we trust the transformations are doing the
182070090cfSChih-Ping Chen     // right thing, and treat the constant as unsigned to preserve that value
183070090cfSChih-Ping Chen     // (i.e. avoid sign extension).
184bb346146SChen Zheng     return true;
185bb346146SChen Zheng   }
186070090cfSChih-Ping Chen 
1877669f3c0SAmy Huang   if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
1885cb21086SDavid Blaikie     if (CTy->getTag() == dwarf::DW_TAG_enumeration_type) {
1895cb21086SDavid Blaikie       if (!(Ty = CTy->getBaseType()))
1907669f3c0SAmy Huang         // FIXME: Enums without a fixed underlying type have unknown signedness
1917669f3c0SAmy Huang         // here, leading to incorrectly emitted constants.
1927669f3c0SAmy Huang         return false;
1935cb21086SDavid Blaikie     } else
1947669f3c0SAmy Huang       // (Pieces of) aggregate types that get hacked apart by SROA may be
1957669f3c0SAmy Huang       // represented by a constant. Encode them as unsigned bytes.
1967669f3c0SAmy Huang       return true;
1977669f3c0SAmy Huang   }
1987669f3c0SAmy Huang 
1997669f3c0SAmy Huang   if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
2007669f3c0SAmy Huang     dwarf::Tag T = (dwarf::Tag)Ty->getTag();
2017669f3c0SAmy Huang     // Encode pointer constants as unsigned bytes. This is used at least for
2027669f3c0SAmy Huang     // null pointer constant emission.
2037669f3c0SAmy Huang     // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
2047669f3c0SAmy Huang     // here, but accept them for now due to a bug in SROA producing bogus
2057669f3c0SAmy Huang     // dbg.values.
2067669f3c0SAmy Huang     if (T == dwarf::DW_TAG_pointer_type ||
2077669f3c0SAmy Huang         T == dwarf::DW_TAG_ptr_to_member_type ||
2087669f3c0SAmy Huang         T == dwarf::DW_TAG_reference_type ||
2097669f3c0SAmy Huang         T == dwarf::DW_TAG_rvalue_reference_type)
2107669f3c0SAmy Huang       return true;
2117669f3c0SAmy Huang     assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
2127669f3c0SAmy Huang            T == dwarf::DW_TAG_volatile_type ||
21334435fd1SLuís Ferreira            T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type ||
21434435fd1SLuís Ferreira            T == dwarf::DW_TAG_immutable_type);
2157669f3c0SAmy Huang     assert(DTy->getBaseType() && "Expected valid base type");
2167669f3c0SAmy Huang     return isUnsignedDIType(DTy->getBaseType());
2177669f3c0SAmy Huang   }
2187669f3c0SAmy Huang 
2197669f3c0SAmy Huang   auto *BTy = cast<DIBasicType>(Ty);
2207669f3c0SAmy Huang   unsigned Encoding = BTy->getEncoding();
2217669f3c0SAmy Huang   assert((Encoding == dwarf::DW_ATE_unsigned ||
2227669f3c0SAmy Huang           Encoding == dwarf::DW_ATE_unsigned_char ||
2237669f3c0SAmy Huang           Encoding == dwarf::DW_ATE_signed ||
2247669f3c0SAmy Huang           Encoding == dwarf::DW_ATE_signed_char ||
2257669f3c0SAmy Huang           Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
2267669f3c0SAmy Huang           Encoding == dwarf::DW_ATE_boolean ||
2277669f3c0SAmy Huang           (Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
2287669f3c0SAmy Huang            Ty->getName() == "decltype(nullptr)")) &&
2297669f3c0SAmy Huang          "Unsupported encoding");
2307669f3c0SAmy Huang   return Encoding == dwarf::DW_ATE_unsigned ||
2317669f3c0SAmy Huang          Encoding == dwarf::DW_ATE_unsigned_char ||
2327669f3c0SAmy Huang          Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
2337669f3c0SAmy Huang          Ty->getTag() == dwarf::DW_TAG_unspecified_type;
2347669f3c0SAmy Huang }
2357669f3c0SAmy Huang 
hasDebugInfo(const MachineModuleInfo * MMI,const MachineFunction * MF)236debb3c35SBenjamin Kramer static bool hasDebugInfo(const MachineModuleInfo *MMI,
237debb3c35SBenjamin Kramer                          const MachineFunction *MF) {
238b2fbb4b2SDavid Blaikie   if (!MMI->hasDebugInfo())
239b2fbb4b2SDavid Blaikie     return false;
240f1caa283SMatthias Braun   auto *SP = MF->getFunction().getSubprogram();
241b2fbb4b2SDavid Blaikie   if (!SP)
242b2fbb4b2SDavid Blaikie     return false;
243b2fbb4b2SDavid Blaikie   assert(SP->getUnit());
244b2fbb4b2SDavid Blaikie   auto EK = SP->getUnit()->getEmissionKind();
245b2fbb4b2SDavid Blaikie   if (EK == DICompileUnit::NoDebug)
246b2fbb4b2SDavid Blaikie     return false;
247b2fbb4b2SDavid Blaikie   return true;
248b2fbb4b2SDavid Blaikie }
249b2fbb4b2SDavid Blaikie 
beginFunction(const MachineFunction * MF)250f9c275feSReid Kleckner void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
2518f333795SAdrian Prantl   PrevInstBB = nullptr;
252b2fbb4b2SDavid Blaikie 
2535bc8543aSReid Kleckner   if (!Asm || !hasDebugInfo(MMI, MF)) {
254b2fbb4b2SDavid Blaikie     skippedNonDebugFunction();
255b2fbb4b2SDavid Blaikie     return;
256b2fbb4b2SDavid Blaikie   }
257b2fbb4b2SDavid Blaikie 
258f9c275feSReid Kleckner   // Grab the lexical scopes for the function, if we don't have any of those
259f9c275feSReid Kleckner   // then we're not going to be able to do anything.
260f9c275feSReid Kleckner   LScopes.initialize(*MF);
261b2fbb4b2SDavid Blaikie   if (LScopes.empty()) {
262b2fbb4b2SDavid Blaikie     beginFunctionImpl(MF);
263f9c275feSReid Kleckner     return;
264b2fbb4b2SDavid Blaikie   }
265f9c275feSReid Kleckner 
266f9c275feSReid Kleckner   // Make sure that each lexical scope will have a begin/end label.
267f9c275feSReid Kleckner   identifyScopeMarkers();
268f9c275feSReid Kleckner 
269f9c275feSReid Kleckner   // Calculate history for local variables.
270f9c275feSReid Kleckner   assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
2712532ac88SHsiangkai Wang   assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
2722532ac88SHsiangkai Wang   calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
2732532ac88SHsiangkai Wang                             DbgValues, DbgLabels);
2740b5a8050SOCHyams   InstOrdering.initialize(*MF);
275ce6de374SOCHyams   if (TrimVarLocs)
2760b5a8050SOCHyams     DbgValues.trimLocationRanges(*MF, LScopes, InstOrdering);
2777224c081SVedant Kumar   LLVM_DEBUG(DbgValues.dump());
278f9c275feSReid Kleckner 
279f9c275feSReid Kleckner   // Request labels for the full history.
280f9c275feSReid Kleckner   for (const auto &I : DbgValues) {
2816feef56dSDavid Stenberg     const auto &Entries = I.second;
2826feef56dSDavid Stenberg     if (Entries.empty())
283f9c275feSReid Kleckner       continue;
284f9c275feSReid Kleckner 
2859dbeca3dSDavid Stenberg     auto IsDescribedByReg = [](const MachineInstr *MI) {
286e64f3cccSStephen Tozer       return any_of(MI->debug_operands(),
287e64f3cccSStephen Tozer                     [](auto &MO) { return MO.isReg() && MO.getReg(); });
2889dbeca3dSDavid Stenberg     };
2899dbeca3dSDavid Stenberg 
2909dbeca3dSDavid Stenberg     // The first mention of a function argument gets the CurrentFnBegin label,
2919dbeca3dSDavid Stenberg     // so arguments are visible when breaking at function entry.
2929dbeca3dSDavid Stenberg     //
2939dbeca3dSDavid Stenberg     // We do not change the label for values that are described by registers,
2949dbeca3dSDavid Stenberg     // as that could place them above their defining instructions. We should
2959dbeca3dSDavid Stenberg     // ideally not change the labels for constant debug values either, since
2969dbeca3dSDavid Stenberg     // doing that violates the ranges that are calculated in the history map.
2979dbeca3dSDavid Stenberg     // However, we currently do not emit debug values for constant arguments
2989dbeca3dSDavid Stenberg     // directly at the start of the function, so this code is still useful.
2993739979cSDavid Stenberg     const DILocalVariable *DIVar =
3005ffec6deSDavid Stenberg         Entries.front().getInstr()->getDebugVariable();
301f9c275feSReid Kleckner     if (DIVar->isParameter() &&
302516e5bb2SSriraman Tallam         getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
3035ffec6deSDavid Stenberg       if (!IsDescribedByReg(Entries.front().getInstr()))
3045ffec6deSDavid Stenberg         LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin();
3055ffec6deSDavid Stenberg       if (Entries.front().getInstr()->getDebugExpression()->isFragment()) {
306941fa758SAdrian Prantl         // Mark all non-overlapping initial fragments.
307*9e6d1f4bSKazu Hirata         for (const auto *I = Entries.begin(); I != Entries.end(); ++I) {
3085ffec6deSDavid Stenberg           if (!I->isDbgValue())
3095ffec6deSDavid Stenberg             continue;
3105ffec6deSDavid Stenberg           const DIExpression *Fragment = I->getInstr()->getDebugExpression();
3116feef56dSDavid Stenberg           if (std::any_of(Entries.begin(), I,
3126feef56dSDavid Stenberg                           [&](DbgValueHistoryMap::Entry Pred) {
3135ffec6deSDavid Stenberg                             return Pred.isDbgValue() &&
3145ffec6deSDavid Stenberg                                    Fragment->fragmentsOverlap(
3155ffec6deSDavid Stenberg                                        Pred.getInstr()->getDebugExpression());
316f9c275feSReid Kleckner                           }))
317f9c275feSReid Kleckner             break;
3185d0e6b67SDavid Stenberg           // The code that generates location lists for DWARF assumes that the
3195d0e6b67SDavid Stenberg           // entries' start labels are monotonically increasing, and since we
3205d0e6b67SDavid Stenberg           // don't change the label for fragments that are described by
3215d0e6b67SDavid Stenberg           // registers, we must bail out when encountering such a fragment.
3225d0e6b67SDavid Stenberg           if (IsDescribedByReg(I->getInstr()))
3235d0e6b67SDavid Stenberg             break;
3245ffec6deSDavid Stenberg           LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin();
325f9c275feSReid Kleckner         }
326f9c275feSReid Kleckner       }
327f9c275feSReid Kleckner     }
328f9c275feSReid Kleckner 
3296feef56dSDavid Stenberg     for (const auto &Entry : Entries) {
3305ffec6deSDavid Stenberg       if (Entry.isDbgValue())
3315ffec6deSDavid Stenberg         requestLabelBeforeInsn(Entry.getInstr());
3325ffec6deSDavid Stenberg       else
3335ffec6deSDavid Stenberg         requestLabelAfterInsn(Entry.getInstr());
334f9c275feSReid Kleckner     }
335f9c275feSReid Kleckner   }
336f9c275feSReid Kleckner 
3372532ac88SHsiangkai Wang   // Ensure there is a symbol before DBG_LABEL.
3382532ac88SHsiangkai Wang   for (const auto &I : DbgLabels) {
3392532ac88SHsiangkai Wang     const MachineInstr *MI = I.second;
3402532ac88SHsiangkai Wang     requestLabelBeforeInsn(MI);
3412532ac88SHsiangkai Wang   }
3422532ac88SHsiangkai Wang 
343f9c275feSReid Kleckner   PrevInstLoc = DebugLoc();
344f9c275feSReid Kleckner   PrevLabel = Asm->getFunctionBegin();
345b2fbb4b2SDavid Blaikie   beginFunctionImpl(MF);
346f9c275feSReid Kleckner }
347f9c275feSReid Kleckner 
beginInstruction(const MachineInstr * MI)348f9c275feSReid Kleckner void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
349a0ad066cSJameson Nash   if (!Asm || !MMI->hasDebugInfo())
350f9c275feSReid Kleckner     return;
351f9c275feSReid Kleckner 
352f9c275feSReid Kleckner   assert(CurMI == nullptr);
353f9c275feSReid Kleckner   CurMI = MI;
354f9c275feSReid Kleckner 
355f9c275feSReid Kleckner   // Insert labels where requested.
356f9c275feSReid Kleckner   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
357f9c275feSReid Kleckner       LabelsBeforeInsn.find(MI);
358f9c275feSReid Kleckner 
359f9c275feSReid Kleckner   // No label needed.
360f9c275feSReid Kleckner   if (I == LabelsBeforeInsn.end())
361f9c275feSReid Kleckner     return;
362f9c275feSReid Kleckner 
363f9c275feSReid Kleckner   // Label already assigned.
364f9c275feSReid Kleckner   if (I->second)
365f9c275feSReid Kleckner     return;
366f9c275feSReid Kleckner 
367f9c275feSReid Kleckner   if (!PrevLabel) {
368f9c275feSReid Kleckner     PrevLabel = MMI->getContext().createTempSymbol();
3696d2d589bSFangrui Song     Asm->OutStreamer->emitLabel(PrevLabel);
370f9c275feSReid Kleckner   }
371f9c275feSReid Kleckner   I->second = PrevLabel;
372f9c275feSReid Kleckner }
373f9c275feSReid Kleckner 
endInstruction()374f9c275feSReid Kleckner void DebugHandlerBase::endInstruction() {
375a0ad066cSJameson Nash   if (!Asm || !MMI->hasDebugInfo())
376f9c275feSReid Kleckner     return;
377f9c275feSReid Kleckner 
378f9c275feSReid Kleckner   assert(CurMI != nullptr);
379fb31da13SAdrian Prantl   // Don't create a new label after DBG_VALUE and other instructions that don't
380fb31da13SAdrian Prantl   // generate code.
381fb31da13SAdrian Prantl   if (!CurMI->isMetaInstruction()) {
382f9c275feSReid Kleckner     PrevLabel = nullptr;
383ac7fe5e0SPaul Robinson     PrevInstBB = CurMI->getParent();
384ac7fe5e0SPaul Robinson   }
385f9c275feSReid Kleckner 
386f9c275feSReid Kleckner   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
387f9c275feSReid Kleckner       LabelsAfterInsn.find(CurMI);
388516e5bb2SSriraman Tallam 
389516e5bb2SSriraman Tallam   // No label needed or label already assigned.
390516e5bb2SSriraman Tallam   if (I == LabelsAfterInsn.end() || I->second) {
391f9c275feSReid Kleckner     CurMI = nullptr;
392192b4141SNico Weber     return;
393516e5bb2SSriraman Tallam   }
394192b4141SNico Weber 
395516e5bb2SSriraman Tallam   // We need a label after this instruction.  With basic block sections, just
396516e5bb2SSriraman Tallam   // use the end symbol of the section if this is the last instruction of the
397516e5bb2SSriraman Tallam   // section.  This reduces the need for an additional label and also helps
398516e5bb2SSriraman Tallam   // merging ranges.
399516e5bb2SSriraman Tallam   if (CurMI->getParent()->isEndSection() && CurMI->getNextNode() == nullptr) {
400516e5bb2SSriraman Tallam     PrevLabel = CurMI->getParent()->getEndSymbol();
401516e5bb2SSriraman Tallam   } else if (!PrevLabel) {
402f9c275feSReid Kleckner     PrevLabel = MMI->getContext().createTempSymbol();
4036d2d589bSFangrui Song     Asm->OutStreamer->emitLabel(PrevLabel);
404f9c275feSReid Kleckner   }
405f9c275feSReid Kleckner   I->second = PrevLabel;
406516e5bb2SSriraman Tallam   CurMI = nullptr;
407f9c275feSReid Kleckner }
408f9c275feSReid Kleckner 
endFunction(const MachineFunction * MF)409f9c275feSReid Kleckner void DebugHandlerBase::endFunction(const MachineFunction *MF) {
410a0ad066cSJameson Nash   if (Asm && hasDebugInfo(MMI, MF))
411b2fbb4b2SDavid Blaikie     endFunctionImpl(MF);
412f9c275feSReid Kleckner   DbgValues.clear();
4132532ac88SHsiangkai Wang   DbgLabels.clear();
414f9c275feSReid Kleckner   LabelsBeforeInsn.clear();
415f9c275feSReid Kleckner   LabelsAfterInsn.clear();
4160b5a8050SOCHyams   InstOrdering.clear();
417f9c275feSReid Kleckner }
418e4b3c138SKrzysztof Pszeniczny 
beginBasicBlock(const MachineBasicBlock & MBB)419e4b3c138SKrzysztof Pszeniczny void DebugHandlerBase::beginBasicBlock(const MachineBasicBlock &MBB) {
420e4b3c138SKrzysztof Pszeniczny   if (!MBB.isBeginSection())
421e4b3c138SKrzysztof Pszeniczny     return;
422e4b3c138SKrzysztof Pszeniczny 
423e4b3c138SKrzysztof Pszeniczny   PrevLabel = MBB.getSymbol();
424e4b3c138SKrzysztof Pszeniczny }
425e4b3c138SKrzysztof Pszeniczny 
endBasicBlock(const MachineBasicBlock & MBB)426e4b3c138SKrzysztof Pszeniczny void DebugHandlerBase::endBasicBlock(const MachineBasicBlock &MBB) {
427e4b3c138SKrzysztof Pszeniczny   if (!MBB.isEndSection())
428e4b3c138SKrzysztof Pszeniczny     return;
429e4b3c138SKrzysztof Pszeniczny 
430e4b3c138SKrzysztof Pszeniczny   PrevLabel = nullptr;
431e4b3c138SKrzysztof Pszeniczny }
432