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"
161a4cbbe4SBob Haarman #include "llvm/ADT/Twine.h"
17f9c275feSReid Kleckner #include "llvm/CodeGen/AsmPrinter.h"
18f9c275feSReid Kleckner #include "llvm/CodeGen/MachineFunction.h"
19f9c275feSReid Kleckner #include "llvm/CodeGen/MachineInstr.h"
20f9c275feSReid Kleckner #include "llvm/CodeGen/MachineModuleInfo.h"
21b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetSubtargetInfo.h"
2228865809SReid Kleckner #include "llvm/IR/DebugInfo.h"
23a5b1eef8SReid Kleckner #include "llvm/MC/MCStreamer.h"
24ce6de374SOCHyams #include "llvm/Support/CommandLine.h"
25f9c275feSReid Kleckner 
26f9c275feSReid Kleckner using namespace llvm;
27f9c275feSReid Kleckner 
287224c081SVedant Kumar #define DEBUG_TYPE "dwarfdebug"
297224c081SVedant Kumar 
30ce6de374SOCHyams /// If true, we drop variable location ranges which exist entirely outside the
31ce6de374SOCHyams /// variable's lexical scope instruction ranges.
32ce6de374SOCHyams static cl::opt<bool> TrimVarLocs("trim-var-locs", cl::Hidden, cl::init(true));
33ce6de374SOCHyams 
341a4cbbe4SBob Haarman Optional<DbgVariableLocation>
351a4cbbe4SBob Haarman DbgVariableLocation::extractFromMachineInstruction(
361a4cbbe4SBob Haarman     const MachineInstr &Instruction) {
371a4cbbe4SBob Haarman   DbgVariableLocation Location;
38223303c5SBob Haarman   if (!Instruction.isDebugValue())
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();
49223303c5SBob Haarman   while (Op != DIExpr->expr_op_end()) {
50223303c5SBob Haarman     switch (Op->getOp()) {
51223303c5SBob Haarman     case dwarf::DW_OP_constu: {
52223303c5SBob Haarman       int Value = Op->getArg(0);
53223303c5SBob Haarman       ++Op;
54223303c5SBob Haarman       if (Op != DIExpr->expr_op_end()) {
55223303c5SBob Haarman         switch (Op->getOp()) {
56223303c5SBob Haarman         case dwarf::DW_OP_minus:
57223303c5SBob Haarman           Offset -= Value;
58223303c5SBob Haarman           break;
59223303c5SBob Haarman         case dwarf::DW_OP_plus:
60223303c5SBob Haarman           Offset += Value;
6168e46019SBob Haarman           break;
62223303c5SBob Haarman         default:
63223303c5SBob Haarman           continue;
64223303c5SBob Haarman         }
65223303c5SBob Haarman       }
66223303c5SBob Haarman     } break;
67223303c5SBob Haarman     case dwarf::DW_OP_plus_uconst:
68223303c5SBob Haarman       Offset += Op->getArg(0);
69223303c5SBob Haarman       break;
70223303c5SBob Haarman     case dwarf::DW_OP_LLVM_fragment:
71223303c5SBob Haarman       Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
72223303c5SBob Haarman       break;
73223303c5SBob Haarman     case dwarf::DW_OP_deref:
7408f5fd51SReid Kleckner       Location.LoadChain.push_back(Offset);
7508f5fd51SReid Kleckner       Offset = 0;
76223303c5SBob Haarman       break;
77223303c5SBob Haarman     default:
781a4cbbe4SBob Haarman       return None;
79223303c5SBob Haarman     }
80223303c5SBob Haarman     ++Op;
81223303c5SBob Haarman   }
82223303c5SBob Haarman 
8308f5fd51SReid Kleckner   // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
8408f5fd51SReid Kleckner   // instruction.
8508f5fd51SReid Kleckner   // FIXME: Replace these with DIExpression.
8608f5fd51SReid Kleckner   if (Instruction.isIndirectDebugValue())
8708f5fd51SReid Kleckner     Location.LoadChain.push_back(Offset);
8808f5fd51SReid Kleckner 
891a4cbbe4SBob Haarman   return Location;
90223303c5SBob Haarman }
91223303c5SBob Haarman 
92f9c275feSReid Kleckner DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
93f9c275feSReid Kleckner 
94f9c275feSReid Kleckner // Each LexicalScope has first instruction and last instruction to mark
95f9c275feSReid Kleckner // beginning and end of a scope respectively. Create an inverse map that list
96f9c275feSReid Kleckner // scopes starts (and ends) with an instruction. One instruction may start (or
97f9c275feSReid Kleckner // end) multiple scopes. Ignore scopes that are not reachable.
98f9c275feSReid Kleckner void DebugHandlerBase::identifyScopeMarkers() {
99f9c275feSReid Kleckner   SmallVector<LexicalScope *, 4> WorkList;
100f9c275feSReid Kleckner   WorkList.push_back(LScopes.getCurrentFunctionScope());
101f9c275feSReid Kleckner   while (!WorkList.empty()) {
102f9c275feSReid Kleckner     LexicalScope *S = WorkList.pop_back_val();
103f9c275feSReid Kleckner 
104f9c275feSReid Kleckner     const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
105f9c275feSReid Kleckner     if (!Children.empty())
106f9c275feSReid Kleckner       WorkList.append(Children.begin(), Children.end());
107f9c275feSReid Kleckner 
108f9c275feSReid Kleckner     if (S->isAbstractScope())
109f9c275feSReid Kleckner       continue;
110f9c275feSReid Kleckner 
111f9c275feSReid Kleckner     for (const InsnRange &R : S->getRanges()) {
112f9c275feSReid Kleckner       assert(R.first && "InsnRange does not have first instruction!");
113f9c275feSReid Kleckner       assert(R.second && "InsnRange does not have second instruction!");
114f9c275feSReid Kleckner       requestLabelBeforeInsn(R.first);
115f9c275feSReid Kleckner       requestLabelAfterInsn(R.second);
116f9c275feSReid Kleckner     }
117f9c275feSReid Kleckner   }
118f9c275feSReid Kleckner }
119f9c275feSReid Kleckner 
120f9c275feSReid Kleckner // Return Label preceding the instruction.
121f9c275feSReid Kleckner MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
122f9c275feSReid Kleckner   MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
123f9c275feSReid Kleckner   assert(Label && "Didn't insert label before instruction");
124f9c275feSReid Kleckner   return Label;
125f9c275feSReid Kleckner }
126f9c275feSReid Kleckner 
127f9c275feSReid Kleckner // Return Label immediately following the instruction.
128f9c275feSReid Kleckner MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
129f9c275feSReid Kleckner   return LabelsAfterInsn.lookup(MI);
130f9c275feSReid Kleckner }
131f9c275feSReid Kleckner 
132acee5685SAmjad Aboud /// If this type is derived from a base type then return base type size.
133da82ce99SFangrui Song uint64_t DebugHandlerBase::getBaseTypeSize(const DIType *Ty) {
134acee5685SAmjad Aboud   assert(Ty);
135da82ce99SFangrui Song   const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
136acee5685SAmjad Aboud   if (!DDTy)
137acee5685SAmjad Aboud     return Ty->getSizeInBits();
138acee5685SAmjad Aboud 
139acee5685SAmjad Aboud   unsigned Tag = DDTy->getTag();
140acee5685SAmjad Aboud 
141acee5685SAmjad Aboud   if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
142acee5685SAmjad Aboud       Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
143e1156c2eSVictor Leschuk       Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
144acee5685SAmjad Aboud     return DDTy->getSizeInBits();
145acee5685SAmjad Aboud 
146da82ce99SFangrui Song   DIType *BaseType = DDTy->getBaseType();
147acee5685SAmjad Aboud 
14874bfafa1SAdrian McCarthy   if (!BaseType)
14974bfafa1SAdrian McCarthy     return 0;
150acee5685SAmjad Aboud 
151acee5685SAmjad Aboud   // If this is a derived type, go ahead and get the base type, unless it's a
152acee5685SAmjad Aboud   // reference then it's just the size of the field. Pointer types have no need
153acee5685SAmjad Aboud   // of this since they're a different type of qualification on the type.
154acee5685SAmjad Aboud   if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
155acee5685SAmjad Aboud       BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
156acee5685SAmjad Aboud     return Ty->getSizeInBits();
157acee5685SAmjad Aboud 
158acee5685SAmjad Aboud   return getBaseTypeSize(BaseType);
159acee5685SAmjad Aboud }
160acee5685SAmjad Aboud 
161*7669f3c0SAmy Huang bool DebugHandlerBase::isUnsignedDIType(const DIType *Ty) {
162*7669f3c0SAmy Huang   if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
163*7669f3c0SAmy Huang     // FIXME: Enums without a fixed underlying type have unknown signedness
164*7669f3c0SAmy Huang     // here, leading to incorrectly emitted constants.
165*7669f3c0SAmy Huang     if (CTy->getTag() == dwarf::DW_TAG_enumeration_type)
166*7669f3c0SAmy Huang       return false;
167*7669f3c0SAmy Huang 
168*7669f3c0SAmy Huang     // (Pieces of) aggregate types that get hacked apart by SROA may be
169*7669f3c0SAmy Huang     // represented by a constant. Encode them as unsigned bytes.
170*7669f3c0SAmy Huang     return true;
171*7669f3c0SAmy Huang   }
172*7669f3c0SAmy Huang 
173*7669f3c0SAmy Huang   if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
174*7669f3c0SAmy Huang     dwarf::Tag T = (dwarf::Tag)Ty->getTag();
175*7669f3c0SAmy Huang     // Encode pointer constants as unsigned bytes. This is used at least for
176*7669f3c0SAmy Huang     // null pointer constant emission.
177*7669f3c0SAmy Huang     // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
178*7669f3c0SAmy Huang     // here, but accept them for now due to a bug in SROA producing bogus
179*7669f3c0SAmy Huang     // dbg.values.
180*7669f3c0SAmy Huang     if (T == dwarf::DW_TAG_pointer_type ||
181*7669f3c0SAmy Huang         T == dwarf::DW_TAG_ptr_to_member_type ||
182*7669f3c0SAmy Huang         T == dwarf::DW_TAG_reference_type ||
183*7669f3c0SAmy Huang         T == dwarf::DW_TAG_rvalue_reference_type)
184*7669f3c0SAmy Huang       return true;
185*7669f3c0SAmy Huang     assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
186*7669f3c0SAmy Huang            T == dwarf::DW_TAG_volatile_type ||
187*7669f3c0SAmy Huang            T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type);
188*7669f3c0SAmy Huang     assert(DTy->getBaseType() && "Expected valid base type");
189*7669f3c0SAmy Huang     return isUnsignedDIType(DTy->getBaseType());
190*7669f3c0SAmy Huang   }
191*7669f3c0SAmy Huang 
192*7669f3c0SAmy Huang   auto *BTy = cast<DIBasicType>(Ty);
193*7669f3c0SAmy Huang   unsigned Encoding = BTy->getEncoding();
194*7669f3c0SAmy Huang   assert((Encoding == dwarf::DW_ATE_unsigned ||
195*7669f3c0SAmy Huang           Encoding == dwarf::DW_ATE_unsigned_char ||
196*7669f3c0SAmy Huang           Encoding == dwarf::DW_ATE_signed ||
197*7669f3c0SAmy Huang           Encoding == dwarf::DW_ATE_signed_char ||
198*7669f3c0SAmy Huang           Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
199*7669f3c0SAmy Huang           Encoding == dwarf::DW_ATE_boolean ||
200*7669f3c0SAmy Huang           (Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
201*7669f3c0SAmy Huang            Ty->getName() == "decltype(nullptr)")) &&
202*7669f3c0SAmy Huang          "Unsupported encoding");
203*7669f3c0SAmy Huang   return Encoding == dwarf::DW_ATE_unsigned ||
204*7669f3c0SAmy Huang          Encoding == dwarf::DW_ATE_unsigned_char ||
205*7669f3c0SAmy Huang          Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
206*7669f3c0SAmy Huang          Ty->getTag() == dwarf::DW_TAG_unspecified_type;
207*7669f3c0SAmy Huang }
208*7669f3c0SAmy Huang 
209debb3c35SBenjamin Kramer static bool hasDebugInfo(const MachineModuleInfo *MMI,
210debb3c35SBenjamin Kramer                          const MachineFunction *MF) {
211b2fbb4b2SDavid Blaikie   if (!MMI->hasDebugInfo())
212b2fbb4b2SDavid Blaikie     return false;
213f1caa283SMatthias Braun   auto *SP = MF->getFunction().getSubprogram();
214b2fbb4b2SDavid Blaikie   if (!SP)
215b2fbb4b2SDavid Blaikie     return false;
216b2fbb4b2SDavid Blaikie   assert(SP->getUnit());
217b2fbb4b2SDavid Blaikie   auto EK = SP->getUnit()->getEmissionKind();
218b2fbb4b2SDavid Blaikie   if (EK == DICompileUnit::NoDebug)
219b2fbb4b2SDavid Blaikie     return false;
220b2fbb4b2SDavid Blaikie   return true;
221b2fbb4b2SDavid Blaikie }
222b2fbb4b2SDavid Blaikie 
223f9c275feSReid Kleckner void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
2248f333795SAdrian Prantl   PrevInstBB = nullptr;
225b2fbb4b2SDavid Blaikie 
2265bc8543aSReid Kleckner   if (!Asm || !hasDebugInfo(MMI, MF)) {
227b2fbb4b2SDavid Blaikie     skippedNonDebugFunction();
228b2fbb4b2SDavid Blaikie     return;
229b2fbb4b2SDavid Blaikie   }
230b2fbb4b2SDavid Blaikie 
231f9c275feSReid Kleckner   // Grab the lexical scopes for the function, if we don't have any of those
232f9c275feSReid Kleckner   // then we're not going to be able to do anything.
233f9c275feSReid Kleckner   LScopes.initialize(*MF);
234b2fbb4b2SDavid Blaikie   if (LScopes.empty()) {
235b2fbb4b2SDavid Blaikie     beginFunctionImpl(MF);
236f9c275feSReid Kleckner     return;
237b2fbb4b2SDavid Blaikie   }
238f9c275feSReid Kleckner 
239f9c275feSReid Kleckner   // Make sure that each lexical scope will have a begin/end label.
240f9c275feSReid Kleckner   identifyScopeMarkers();
241f9c275feSReid Kleckner 
242f9c275feSReid Kleckner   // Calculate history for local variables.
243f9c275feSReid Kleckner   assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
2442532ac88SHsiangkai Wang   assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
2452532ac88SHsiangkai Wang   calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
2462532ac88SHsiangkai Wang                             DbgValues, DbgLabels);
2470b5a8050SOCHyams   InstOrdering.initialize(*MF);
248ce6de374SOCHyams   if (TrimVarLocs)
2490b5a8050SOCHyams     DbgValues.trimLocationRanges(*MF, LScopes, InstOrdering);
2507224c081SVedant Kumar   LLVM_DEBUG(DbgValues.dump());
251f9c275feSReid Kleckner 
252f9c275feSReid Kleckner   // Request labels for the full history.
253f9c275feSReid Kleckner   for (const auto &I : DbgValues) {
2546feef56dSDavid Stenberg     const auto &Entries = I.second;
2556feef56dSDavid Stenberg     if (Entries.empty())
256f9c275feSReid Kleckner       continue;
257f9c275feSReid Kleckner 
2589dbeca3dSDavid Stenberg     auto IsDescribedByReg = [](const MachineInstr *MI) {
259539381daSstozer       return MI->getDebugOperand(0).isReg() && MI->getDebugOperand(0).getReg();
2609dbeca3dSDavid Stenberg     };
2619dbeca3dSDavid Stenberg 
2629dbeca3dSDavid Stenberg     // The first mention of a function argument gets the CurrentFnBegin label,
2639dbeca3dSDavid Stenberg     // so arguments are visible when breaking at function entry.
2649dbeca3dSDavid Stenberg     //
2659dbeca3dSDavid Stenberg     // We do not change the label for values that are described by registers,
2669dbeca3dSDavid Stenberg     // as that could place them above their defining instructions. We should
2679dbeca3dSDavid Stenberg     // ideally not change the labels for constant debug values either, since
2689dbeca3dSDavid Stenberg     // doing that violates the ranges that are calculated in the history map.
2699dbeca3dSDavid Stenberg     // However, we currently do not emit debug values for constant arguments
2709dbeca3dSDavid Stenberg     // directly at the start of the function, so this code is still useful.
271e3928651SSriraman Tallam     // FIXME: If the first mention of an argument is in a unique section basic
272e3928651SSriraman Tallam     // block, we cannot always assign the CurrentFnBeginLabel as it lies in a
273e3928651SSriraman Tallam     // different section.  Temporarily, we disable generating loc list
274e3928651SSriraman Tallam     // information or DW_AT_const_value when the block is in a different
275e3928651SSriraman Tallam     // section.
2763739979cSDavid Stenberg     const DILocalVariable *DIVar =
2775ffec6deSDavid Stenberg         Entries.front().getInstr()->getDebugVariable();
278f9c275feSReid Kleckner     if (DIVar->isParameter() &&
279e3928651SSriraman Tallam         getDISubprogram(DIVar->getScope())->describes(&MF->getFunction()) &&
280e3928651SSriraman Tallam         Entries.front().getInstr()->getParent()->sameSection(&MF->front())) {
2815ffec6deSDavid Stenberg       if (!IsDescribedByReg(Entries.front().getInstr()))
2825ffec6deSDavid Stenberg         LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin();
2835ffec6deSDavid Stenberg       if (Entries.front().getInstr()->getDebugExpression()->isFragment()) {
284941fa758SAdrian Prantl         // Mark all non-overlapping initial fragments.
2856feef56dSDavid Stenberg         for (auto I = Entries.begin(); I != Entries.end(); ++I) {
2865ffec6deSDavid Stenberg           if (!I->isDbgValue())
2875ffec6deSDavid Stenberg             continue;
2885ffec6deSDavid Stenberg           const DIExpression *Fragment = I->getInstr()->getDebugExpression();
2896feef56dSDavid Stenberg           if (std::any_of(Entries.begin(), I,
2906feef56dSDavid Stenberg                           [&](DbgValueHistoryMap::Entry Pred) {
2915ffec6deSDavid Stenberg                             return Pred.isDbgValue() &&
2925ffec6deSDavid Stenberg                                    Fragment->fragmentsOverlap(
2935ffec6deSDavid Stenberg                                        Pred.getInstr()->getDebugExpression());
294f9c275feSReid Kleckner                           }))
295f9c275feSReid Kleckner             break;
2965d0e6b67SDavid Stenberg           // The code that generates location lists for DWARF assumes that the
2975d0e6b67SDavid Stenberg           // entries' start labels are monotonically increasing, and since we
2985d0e6b67SDavid Stenberg           // don't change the label for fragments that are described by
2995d0e6b67SDavid Stenberg           // registers, we must bail out when encountering such a fragment.
3005d0e6b67SDavid Stenberg           if (IsDescribedByReg(I->getInstr()))
3015d0e6b67SDavid Stenberg             break;
3025ffec6deSDavid Stenberg           LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin();
303f9c275feSReid Kleckner         }
304f9c275feSReid Kleckner       }
305f9c275feSReid Kleckner     }
306f9c275feSReid Kleckner 
3076feef56dSDavid Stenberg     for (const auto &Entry : Entries) {
3085ffec6deSDavid Stenberg       if (Entry.isDbgValue())
3095ffec6deSDavid Stenberg         requestLabelBeforeInsn(Entry.getInstr());
3105ffec6deSDavid Stenberg       else
3115ffec6deSDavid Stenberg         requestLabelAfterInsn(Entry.getInstr());
312f9c275feSReid Kleckner     }
313f9c275feSReid Kleckner   }
314f9c275feSReid Kleckner 
3152532ac88SHsiangkai Wang   // Ensure there is a symbol before DBG_LABEL.
3162532ac88SHsiangkai Wang   for (const auto &I : DbgLabels) {
3172532ac88SHsiangkai Wang     const MachineInstr *MI = I.second;
3182532ac88SHsiangkai Wang     requestLabelBeforeInsn(MI);
3192532ac88SHsiangkai Wang   }
3202532ac88SHsiangkai Wang 
321f9c275feSReid Kleckner   PrevInstLoc = DebugLoc();
322f9c275feSReid Kleckner   PrevLabel = Asm->getFunctionBegin();
323b2fbb4b2SDavid Blaikie   beginFunctionImpl(MF);
324f9c275feSReid Kleckner }
325f9c275feSReid Kleckner 
326f9c275feSReid Kleckner void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
3274242df14SJameson Nash   if (!MMI->hasDebugInfo())
328f9c275feSReid Kleckner     return;
329f9c275feSReid Kleckner 
330f9c275feSReid Kleckner   assert(CurMI == nullptr);
331f9c275feSReid Kleckner   CurMI = MI;
332f9c275feSReid Kleckner 
333f9c275feSReid Kleckner   // Insert labels where requested.
334f9c275feSReid Kleckner   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
335f9c275feSReid Kleckner       LabelsBeforeInsn.find(MI);
336f9c275feSReid Kleckner 
337f9c275feSReid Kleckner   // No label needed.
338f9c275feSReid Kleckner   if (I == LabelsBeforeInsn.end())
339f9c275feSReid Kleckner     return;
340f9c275feSReid Kleckner 
341f9c275feSReid Kleckner   // Label already assigned.
342f9c275feSReid Kleckner   if (I->second)
343f9c275feSReid Kleckner     return;
344f9c275feSReid Kleckner 
345f9c275feSReid Kleckner   if (!PrevLabel) {
346f9c275feSReid Kleckner     PrevLabel = MMI->getContext().createTempSymbol();
3476d2d589bSFangrui Song     Asm->OutStreamer->emitLabel(PrevLabel);
348f9c275feSReid Kleckner   }
349f9c275feSReid Kleckner   I->second = PrevLabel;
350f9c275feSReid Kleckner }
351f9c275feSReid Kleckner 
352f9c275feSReid Kleckner void DebugHandlerBase::endInstruction() {
3534242df14SJameson Nash   if (!MMI->hasDebugInfo())
354f9c275feSReid Kleckner     return;
355f9c275feSReid Kleckner 
356f9c275feSReid Kleckner   assert(CurMI != nullptr);
357fb31da13SAdrian Prantl   // Don't create a new label after DBG_VALUE and other instructions that don't
358fb31da13SAdrian Prantl   // generate code.
359fb31da13SAdrian Prantl   if (!CurMI->isMetaInstruction()) {
360f9c275feSReid Kleckner     PrevLabel = nullptr;
361ac7fe5e0SPaul Robinson     PrevInstBB = CurMI->getParent();
362ac7fe5e0SPaul Robinson   }
363f9c275feSReid Kleckner 
364f9c275feSReid Kleckner   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
365f9c275feSReid Kleckner       LabelsAfterInsn.find(CurMI);
366f9c275feSReid Kleckner   CurMI = nullptr;
367f9c275feSReid Kleckner 
368f9c275feSReid Kleckner   // No label needed.
369f9c275feSReid Kleckner   if (I == LabelsAfterInsn.end())
370f9c275feSReid Kleckner     return;
371f9c275feSReid Kleckner 
372f9c275feSReid Kleckner   // Label already assigned.
373f9c275feSReid Kleckner   if (I->second)
374f9c275feSReid Kleckner     return;
375f9c275feSReid Kleckner 
376f9c275feSReid Kleckner   // We need a label after this instruction.
377f9c275feSReid Kleckner   if (!PrevLabel) {
378f9c275feSReid Kleckner     PrevLabel = MMI->getContext().createTempSymbol();
3796d2d589bSFangrui Song     Asm->OutStreamer->emitLabel(PrevLabel);
380f9c275feSReid Kleckner   }
381f9c275feSReid Kleckner   I->second = PrevLabel;
382f9c275feSReid Kleckner }
383f9c275feSReid Kleckner 
384f9c275feSReid Kleckner void DebugHandlerBase::endFunction(const MachineFunction *MF) {
3854242df14SJameson Nash   if (hasDebugInfo(MMI, MF))
386b2fbb4b2SDavid Blaikie     endFunctionImpl(MF);
387f9c275feSReid Kleckner   DbgValues.clear();
3882532ac88SHsiangkai Wang   DbgLabels.clear();
389f9c275feSReid Kleckner   LabelsBeforeInsn.clear();
390f9c275feSReid Kleckner   LabelsAfterInsn.clear();
3910b5a8050SOCHyams   InstOrdering.clear();
392f9c275feSReid Kleckner }
393e4b3c138SKrzysztof Pszeniczny 
394e4b3c138SKrzysztof Pszeniczny void DebugHandlerBase::beginBasicBlock(const MachineBasicBlock &MBB) {
395e4b3c138SKrzysztof Pszeniczny   if (!MBB.isBeginSection())
396e4b3c138SKrzysztof Pszeniczny     return;
397e4b3c138SKrzysztof Pszeniczny 
398e4b3c138SKrzysztof Pszeniczny   PrevLabel = MBB.getSymbol();
399e4b3c138SKrzysztof Pszeniczny }
400e4b3c138SKrzysztof Pszeniczny 
401e4b3c138SKrzysztof Pszeniczny void DebugHandlerBase::endBasicBlock(const MachineBasicBlock &MBB) {
402e4b3c138SKrzysztof Pszeniczny   if (!MBB.isEndSection())
403e4b3c138SKrzysztof Pszeniczny     return;
404e4b3c138SKrzysztof Pszeniczny 
405e4b3c138SKrzysztof Pszeniczny   PrevLabel = nullptr;
406e4b3c138SKrzysztof Pszeniczny }
407