1f9c275feSReid Kleckner //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
2f9c275feSReid Kleckner //
3f9c275feSReid Kleckner //                     The LLVM Compiler Infrastructure
4f9c275feSReid Kleckner //
5f9c275feSReid Kleckner // This file is distributed under the University of Illinois Open Source
6f9c275feSReid Kleckner // License. See LICENSE.TXT for details.
7f9c275feSReid Kleckner //
8f9c275feSReid Kleckner //===----------------------------------------------------------------------===//
9f9c275feSReid Kleckner //
10f9c275feSReid Kleckner // Common functionality for different debug information format backends.
11f9c275feSReid Kleckner // LLVM currently supports DWARF and CodeView.
12f9c275feSReid Kleckner //
13f9c275feSReid Kleckner //===----------------------------------------------------------------------===//
14f9c275feSReid Kleckner 
15f9c275feSReid Kleckner #include "DebugHandlerBase.h"
161a4cbbe4SBob Haarman #include "llvm/ADT/Optional.h"
171a4cbbe4SBob Haarman #include "llvm/ADT/Twine.h"
18f9c275feSReid Kleckner #include "llvm/CodeGen/AsmPrinter.h"
19f9c275feSReid Kleckner #include "llvm/CodeGen/MachineFunction.h"
20f9c275feSReid Kleckner #include "llvm/CodeGen/MachineInstr.h"
21f9c275feSReid Kleckner #include "llvm/CodeGen/MachineModuleInfo.h"
22b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetSubtargetInfo.h"
2328865809SReid Kleckner #include "llvm/IR/DebugInfo.h"
24a5b1eef8SReid Kleckner #include "llvm/MC/MCStreamer.h"
25f9c275feSReid Kleckner 
26f9c275feSReid Kleckner using namespace llvm;
27f9c275feSReid Kleckner 
287224c081SVedant Kumar #define DEBUG_TYPE "dwarfdebug"
297224c081SVedant Kumar 
301a4cbbe4SBob Haarman Optional<DbgVariableLocation>
311a4cbbe4SBob Haarman DbgVariableLocation::extractFromMachineInstruction(
321a4cbbe4SBob Haarman     const MachineInstr &Instruction) {
331a4cbbe4SBob Haarman   DbgVariableLocation Location;
34223303c5SBob Haarman   if (!Instruction.isDebugValue())
351a4cbbe4SBob Haarman     return None;
36223303c5SBob Haarman   if (!Instruction.getOperand(0).isReg())
371a4cbbe4SBob Haarman     return None;
38223303c5SBob Haarman   Location.Register = Instruction.getOperand(0).getReg();
39223303c5SBob Haarman   Location.FragmentInfo.reset();
40223303c5SBob Haarman   // We only handle expressions generated by DIExpression::appendOffset,
41223303c5SBob Haarman   // which doesn't require a full stack machine.
42223303c5SBob Haarman   int64_t Offset = 0;
43223303c5SBob Haarman   const DIExpression *DIExpr = Instruction.getDebugExpression();
44223303c5SBob Haarman   auto Op = DIExpr->expr_op_begin();
45223303c5SBob Haarman   while (Op != DIExpr->expr_op_end()) {
46223303c5SBob Haarman     switch (Op->getOp()) {
47223303c5SBob Haarman     case dwarf::DW_OP_constu: {
48223303c5SBob Haarman       int Value = Op->getArg(0);
49223303c5SBob Haarman       ++Op;
50223303c5SBob Haarman       if (Op != DIExpr->expr_op_end()) {
51223303c5SBob Haarman         switch (Op->getOp()) {
52223303c5SBob Haarman         case dwarf::DW_OP_minus:
53223303c5SBob Haarman           Offset -= Value;
54223303c5SBob Haarman           break;
55223303c5SBob Haarman         case dwarf::DW_OP_plus:
56223303c5SBob Haarman           Offset += Value;
5768e46019SBob Haarman           break;
58223303c5SBob Haarman         default:
59223303c5SBob Haarman           continue;
60223303c5SBob Haarman         }
61223303c5SBob Haarman       }
62223303c5SBob Haarman     } break;
63223303c5SBob Haarman     case dwarf::DW_OP_plus_uconst:
64223303c5SBob Haarman       Offset += Op->getArg(0);
65223303c5SBob Haarman       break;
66223303c5SBob Haarman     case dwarf::DW_OP_LLVM_fragment:
67223303c5SBob Haarman       Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
68223303c5SBob Haarman       break;
69223303c5SBob Haarman     case dwarf::DW_OP_deref:
7008f5fd51SReid Kleckner       Location.LoadChain.push_back(Offset);
7108f5fd51SReid Kleckner       Offset = 0;
72223303c5SBob Haarman       break;
73223303c5SBob Haarman     default:
741a4cbbe4SBob Haarman       return None;
75223303c5SBob Haarman     }
76223303c5SBob Haarman     ++Op;
77223303c5SBob Haarman   }
78223303c5SBob Haarman 
7908f5fd51SReid Kleckner   // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
8008f5fd51SReid Kleckner   // instruction.
8108f5fd51SReid Kleckner   // FIXME: Replace these with DIExpression.
8208f5fd51SReid Kleckner   if (Instruction.isIndirectDebugValue())
8308f5fd51SReid Kleckner     Location.LoadChain.push_back(Offset);
8408f5fd51SReid Kleckner 
851a4cbbe4SBob Haarman   return Location;
86223303c5SBob Haarman }
87223303c5SBob Haarman 
88f9c275feSReid Kleckner DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
89f9c275feSReid Kleckner 
90f9c275feSReid Kleckner // Each LexicalScope has first instruction and last instruction to mark
91f9c275feSReid Kleckner // beginning and end of a scope respectively. Create an inverse map that list
92f9c275feSReid Kleckner // scopes starts (and ends) with an instruction. One instruction may start (or
93f9c275feSReid Kleckner // end) multiple scopes. Ignore scopes that are not reachable.
94f9c275feSReid Kleckner void DebugHandlerBase::identifyScopeMarkers() {
95f9c275feSReid Kleckner   SmallVector<LexicalScope *, 4> WorkList;
96f9c275feSReid Kleckner   WorkList.push_back(LScopes.getCurrentFunctionScope());
97f9c275feSReid Kleckner   while (!WorkList.empty()) {
98f9c275feSReid Kleckner     LexicalScope *S = WorkList.pop_back_val();
99f9c275feSReid Kleckner 
100f9c275feSReid Kleckner     const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
101f9c275feSReid Kleckner     if (!Children.empty())
102f9c275feSReid Kleckner       WorkList.append(Children.begin(), Children.end());
103f9c275feSReid Kleckner 
104f9c275feSReid Kleckner     if (S->isAbstractScope())
105f9c275feSReid Kleckner       continue;
106f9c275feSReid Kleckner 
107f9c275feSReid Kleckner     for (const InsnRange &R : S->getRanges()) {
108f9c275feSReid Kleckner       assert(R.first && "InsnRange does not have first instruction!");
109f9c275feSReid Kleckner       assert(R.second && "InsnRange does not have second instruction!");
110f9c275feSReid Kleckner       requestLabelBeforeInsn(R.first);
111f9c275feSReid Kleckner       requestLabelAfterInsn(R.second);
112f9c275feSReid Kleckner     }
113f9c275feSReid Kleckner   }
114f9c275feSReid Kleckner }
115f9c275feSReid Kleckner 
116f9c275feSReid Kleckner // Return Label preceding the instruction.
117f9c275feSReid Kleckner MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
118f9c275feSReid Kleckner   MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
119f9c275feSReid Kleckner   assert(Label && "Didn't insert label before instruction");
120f9c275feSReid Kleckner   return Label;
121f9c275feSReid Kleckner }
122f9c275feSReid Kleckner 
123f9c275feSReid Kleckner // Return Label immediately following the instruction.
124f9c275feSReid Kleckner MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
125f9c275feSReid Kleckner   return LabelsAfterInsn.lookup(MI);
126f9c275feSReid Kleckner }
127f9c275feSReid Kleckner 
128acee5685SAmjad Aboud /// If this type is derived from a base type then return base type size.
129acee5685SAmjad Aboud uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
130acee5685SAmjad Aboud   DIType *Ty = TyRef.resolve();
131acee5685SAmjad Aboud   assert(Ty);
132acee5685SAmjad Aboud   DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
133acee5685SAmjad Aboud   if (!DDTy)
134acee5685SAmjad Aboud     return Ty->getSizeInBits();
135acee5685SAmjad Aboud 
136acee5685SAmjad Aboud   unsigned Tag = DDTy->getTag();
137acee5685SAmjad Aboud 
138acee5685SAmjad Aboud   if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
139acee5685SAmjad Aboud       Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
140e1156c2eSVictor Leschuk       Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
141acee5685SAmjad Aboud     return DDTy->getSizeInBits();
142acee5685SAmjad Aboud 
143acee5685SAmjad Aboud   DIType *BaseType = DDTy->getBaseType().resolve();
144acee5685SAmjad Aboud 
14574bfafa1SAdrian McCarthy   if (!BaseType)
14674bfafa1SAdrian McCarthy     return 0;
147acee5685SAmjad Aboud 
148acee5685SAmjad Aboud   // If this is a derived type, go ahead and get the base type, unless it's a
149acee5685SAmjad Aboud   // reference then it's just the size of the field. Pointer types have no need
150acee5685SAmjad Aboud   // of this since they're a different type of qualification on the type.
151acee5685SAmjad Aboud   if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
152acee5685SAmjad Aboud       BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
153acee5685SAmjad Aboud     return Ty->getSizeInBits();
154acee5685SAmjad Aboud 
155acee5685SAmjad Aboud   return getBaseTypeSize(BaseType);
156acee5685SAmjad Aboud }
157acee5685SAmjad Aboud 
158debb3c35SBenjamin Kramer static bool hasDebugInfo(const MachineModuleInfo *MMI,
159debb3c35SBenjamin Kramer                          const MachineFunction *MF) {
160b2fbb4b2SDavid Blaikie   if (!MMI->hasDebugInfo())
161b2fbb4b2SDavid Blaikie     return false;
162f1caa283SMatthias Braun   auto *SP = MF->getFunction().getSubprogram();
163b2fbb4b2SDavid Blaikie   if (!SP)
164b2fbb4b2SDavid Blaikie     return false;
165b2fbb4b2SDavid Blaikie   assert(SP->getUnit());
166b2fbb4b2SDavid Blaikie   auto EK = SP->getUnit()->getEmissionKind();
167b2fbb4b2SDavid Blaikie   if (EK == DICompileUnit::NoDebug)
168b2fbb4b2SDavid Blaikie     return false;
169b2fbb4b2SDavid Blaikie   return true;
170b2fbb4b2SDavid Blaikie }
171b2fbb4b2SDavid Blaikie 
172f9c275feSReid Kleckner void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
1738f333795SAdrian Prantl   PrevInstBB = nullptr;
174b2fbb4b2SDavid Blaikie 
1755bc8543aSReid Kleckner   if (!Asm || !hasDebugInfo(MMI, MF)) {
176b2fbb4b2SDavid Blaikie     skippedNonDebugFunction();
177b2fbb4b2SDavid Blaikie     return;
178b2fbb4b2SDavid Blaikie   }
179b2fbb4b2SDavid Blaikie 
180f9c275feSReid Kleckner   // Grab the lexical scopes for the function, if we don't have any of those
181f9c275feSReid Kleckner   // then we're not going to be able to do anything.
182f9c275feSReid Kleckner   LScopes.initialize(*MF);
183b2fbb4b2SDavid Blaikie   if (LScopes.empty()) {
184b2fbb4b2SDavid Blaikie     beginFunctionImpl(MF);
185f9c275feSReid Kleckner     return;
186b2fbb4b2SDavid Blaikie   }
187f9c275feSReid Kleckner 
188f9c275feSReid Kleckner   // Make sure that each lexical scope will have a begin/end label.
189f9c275feSReid Kleckner   identifyScopeMarkers();
190f9c275feSReid Kleckner 
191f9c275feSReid Kleckner   // Calculate history for local variables.
192f9c275feSReid Kleckner   assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
193*2532ac88SHsiangkai Wang   assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
194*2532ac88SHsiangkai Wang   calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
195*2532ac88SHsiangkai Wang                             DbgValues, DbgLabels);
1967224c081SVedant Kumar   LLVM_DEBUG(DbgValues.dump());
197f9c275feSReid Kleckner 
198f9c275feSReid Kleckner   // Request labels for the full history.
199f9c275feSReid Kleckner   for (const auto &I : DbgValues) {
200f9c275feSReid Kleckner     const auto &Ranges = I.second;
201f9c275feSReid Kleckner     if (Ranges.empty())
202f9c275feSReid Kleckner       continue;
203f9c275feSReid Kleckner 
204f9c275feSReid Kleckner     // The first mention of a function argument gets the CurrentFnBegin
205f9c275feSReid Kleckner     // label, so arguments are visible when breaking at function entry.
206f9c275feSReid Kleckner     const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
207f9c275feSReid Kleckner     if (DIVar->isParameter() &&
208f1caa283SMatthias Braun         getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
209f9c275feSReid Kleckner       LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
210941fa758SAdrian Prantl       if (Ranges.front().first->getDebugExpression()->isFragment()) {
211941fa758SAdrian Prantl         // Mark all non-overlapping initial fragments.
212f9c275feSReid Kleckner         for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
213941fa758SAdrian Prantl           const DIExpression *Fragment = I->first->getDebugExpression();
214f9c275feSReid Kleckner           if (std::all_of(Ranges.begin(), I,
215f9c275feSReid Kleckner                           [&](DbgValueHistoryMap::InstrRange Pred) {
216a223f815SBjorn Pettersson                             return !Fragment->fragmentsOverlap(
217a223f815SBjorn Pettersson                                 Pred.first->getDebugExpression());
218f9c275feSReid Kleckner                           }))
219f9c275feSReid Kleckner             LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
220f9c275feSReid Kleckner           else
221f9c275feSReid Kleckner             break;
222f9c275feSReid Kleckner         }
223f9c275feSReid Kleckner       }
224f9c275feSReid Kleckner     }
225f9c275feSReid Kleckner 
226f9c275feSReid Kleckner     for (const auto &Range : Ranges) {
227f9c275feSReid Kleckner       requestLabelBeforeInsn(Range.first);
228f9c275feSReid Kleckner       if (Range.second)
229f9c275feSReid Kleckner         requestLabelAfterInsn(Range.second);
230f9c275feSReid Kleckner     }
231f9c275feSReid Kleckner   }
232f9c275feSReid Kleckner 
233*2532ac88SHsiangkai Wang   // Ensure there is a symbol before DBG_LABEL.
234*2532ac88SHsiangkai Wang   for (const auto &I : DbgLabels) {
235*2532ac88SHsiangkai Wang     const MachineInstr *MI = I.second;
236*2532ac88SHsiangkai Wang     requestLabelBeforeInsn(MI);
237*2532ac88SHsiangkai Wang   }
238*2532ac88SHsiangkai Wang 
239f9c275feSReid Kleckner   PrevInstLoc = DebugLoc();
240f9c275feSReid Kleckner   PrevLabel = Asm->getFunctionBegin();
241b2fbb4b2SDavid Blaikie   beginFunctionImpl(MF);
242f9c275feSReid Kleckner }
243f9c275feSReid Kleckner 
244f9c275feSReid Kleckner void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
245f9c275feSReid Kleckner   if (!MMI->hasDebugInfo())
246f9c275feSReid Kleckner     return;
247f9c275feSReid Kleckner 
248f9c275feSReid Kleckner   assert(CurMI == nullptr);
249f9c275feSReid Kleckner   CurMI = MI;
250f9c275feSReid Kleckner 
251f9c275feSReid Kleckner   // Insert labels where requested.
252f9c275feSReid Kleckner   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
253f9c275feSReid Kleckner       LabelsBeforeInsn.find(MI);
254f9c275feSReid Kleckner 
255f9c275feSReid Kleckner   // No label needed.
256f9c275feSReid Kleckner   if (I == LabelsBeforeInsn.end())
257f9c275feSReid Kleckner     return;
258f9c275feSReid Kleckner 
259f9c275feSReid Kleckner   // Label already assigned.
260f9c275feSReid Kleckner   if (I->second)
261f9c275feSReid Kleckner     return;
262f9c275feSReid Kleckner 
263f9c275feSReid Kleckner   if (!PrevLabel) {
264f9c275feSReid Kleckner     PrevLabel = MMI->getContext().createTempSymbol();
265f9c275feSReid Kleckner     Asm->OutStreamer->EmitLabel(PrevLabel);
266f9c275feSReid Kleckner   }
267f9c275feSReid Kleckner   I->second = PrevLabel;
268f9c275feSReid Kleckner }
269f9c275feSReid Kleckner 
270f9c275feSReid Kleckner void DebugHandlerBase::endInstruction() {
271f9c275feSReid Kleckner   if (!MMI->hasDebugInfo())
272f9c275feSReid Kleckner     return;
273f9c275feSReid Kleckner 
274f9c275feSReid Kleckner   assert(CurMI != nullptr);
275fb31da13SAdrian Prantl   // Don't create a new label after DBG_VALUE and other instructions that don't
276fb31da13SAdrian Prantl   // generate code.
277fb31da13SAdrian Prantl   if (!CurMI->isMetaInstruction()) {
278f9c275feSReid Kleckner     PrevLabel = nullptr;
279ac7fe5e0SPaul Robinson     PrevInstBB = CurMI->getParent();
280ac7fe5e0SPaul Robinson   }
281f9c275feSReid Kleckner 
282f9c275feSReid Kleckner   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
283f9c275feSReid Kleckner       LabelsAfterInsn.find(CurMI);
284f9c275feSReid Kleckner   CurMI = nullptr;
285f9c275feSReid Kleckner 
286f9c275feSReid Kleckner   // No label needed.
287f9c275feSReid Kleckner   if (I == LabelsAfterInsn.end())
288f9c275feSReid Kleckner     return;
289f9c275feSReid Kleckner 
290f9c275feSReid Kleckner   // Label already assigned.
291f9c275feSReid Kleckner   if (I->second)
292f9c275feSReid Kleckner     return;
293f9c275feSReid Kleckner 
294f9c275feSReid Kleckner   // We need a label after this instruction.
295f9c275feSReid Kleckner   if (!PrevLabel) {
296f9c275feSReid Kleckner     PrevLabel = MMI->getContext().createTempSymbol();
297f9c275feSReid Kleckner     Asm->OutStreamer->EmitLabel(PrevLabel);
298f9c275feSReid Kleckner   }
299f9c275feSReid Kleckner   I->second = PrevLabel;
300f9c275feSReid Kleckner }
301f9c275feSReid Kleckner 
302f9c275feSReid Kleckner void DebugHandlerBase::endFunction(const MachineFunction *MF) {
303b2fbb4b2SDavid Blaikie   if (hasDebugInfo(MMI, MF))
304b2fbb4b2SDavid Blaikie     endFunctionImpl(MF);
305f9c275feSReid Kleckner   DbgValues.clear();
306*2532ac88SHsiangkai Wang   DbgLabels.clear();
307f9c275feSReid Kleckner   LabelsBeforeInsn.clear();
308f9c275feSReid Kleckner   LabelsAfterInsn.clear();
309f9c275feSReid Kleckner }
310