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 
281a4cbbe4SBob Haarman Optional<DbgVariableLocation>
291a4cbbe4SBob Haarman DbgVariableLocation::extractFromMachineInstruction(
301a4cbbe4SBob Haarman     const MachineInstr &Instruction) {
311a4cbbe4SBob Haarman   DbgVariableLocation Location;
32223303c5SBob Haarman   if (!Instruction.isDebugValue())
331a4cbbe4SBob Haarman     return None;
34223303c5SBob Haarman   if (!Instruction.getOperand(0).isReg())
351a4cbbe4SBob Haarman     return None;
36223303c5SBob Haarman   Location.Register = Instruction.getOperand(0).getReg();
37223303c5SBob Haarman   Location.FragmentInfo.reset();
38223303c5SBob Haarman   // We only handle expressions generated by DIExpression::appendOffset,
39223303c5SBob Haarman   // which doesn't require a full stack machine.
40223303c5SBob Haarman   int64_t Offset = 0;
41223303c5SBob Haarman   const DIExpression *DIExpr = Instruction.getDebugExpression();
42223303c5SBob Haarman   auto Op = DIExpr->expr_op_begin();
43223303c5SBob Haarman   while (Op != DIExpr->expr_op_end()) {
44223303c5SBob Haarman     switch (Op->getOp()) {
45223303c5SBob Haarman     case dwarf::DW_OP_constu: {
46223303c5SBob Haarman       int Value = Op->getArg(0);
47223303c5SBob Haarman       ++Op;
48223303c5SBob Haarman       if (Op != DIExpr->expr_op_end()) {
49223303c5SBob Haarman         switch (Op->getOp()) {
50223303c5SBob Haarman         case dwarf::DW_OP_minus:
51223303c5SBob Haarman           Offset -= Value;
52223303c5SBob Haarman           break;
53223303c5SBob Haarman         case dwarf::DW_OP_plus:
54223303c5SBob Haarman           Offset += Value;
5568e46019SBob Haarman           break;
56223303c5SBob Haarman         default:
57223303c5SBob Haarman           continue;
58223303c5SBob Haarman         }
59223303c5SBob Haarman       }
60223303c5SBob Haarman     } break;
61223303c5SBob Haarman     case dwarf::DW_OP_plus_uconst:
62223303c5SBob Haarman       Offset += Op->getArg(0);
63223303c5SBob Haarman       break;
64223303c5SBob Haarman     case dwarf::DW_OP_LLVM_fragment:
65223303c5SBob Haarman       Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
66223303c5SBob Haarman       break;
67223303c5SBob Haarman     case dwarf::DW_OP_deref:
6808f5fd51SReid Kleckner       Location.LoadChain.push_back(Offset);
6908f5fd51SReid Kleckner       Offset = 0;
70223303c5SBob Haarman       break;
71223303c5SBob Haarman     default:
721a4cbbe4SBob Haarman       return None;
73223303c5SBob Haarman     }
74223303c5SBob Haarman     ++Op;
75223303c5SBob Haarman   }
76223303c5SBob Haarman 
7708f5fd51SReid Kleckner   // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
7808f5fd51SReid Kleckner   // instruction.
7908f5fd51SReid Kleckner   // FIXME: Replace these with DIExpression.
8008f5fd51SReid Kleckner   if (Instruction.isIndirectDebugValue())
8108f5fd51SReid Kleckner     Location.LoadChain.push_back(Offset);
8208f5fd51SReid Kleckner 
831a4cbbe4SBob Haarman   return Location;
84223303c5SBob Haarman }
85223303c5SBob Haarman 
86f9c275feSReid Kleckner DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
87f9c275feSReid Kleckner 
88f9c275feSReid Kleckner // Each LexicalScope has first instruction and last instruction to mark
89f9c275feSReid Kleckner // beginning and end of a scope respectively. Create an inverse map that list
90f9c275feSReid Kleckner // scopes starts (and ends) with an instruction. One instruction may start (or
91f9c275feSReid Kleckner // end) multiple scopes. Ignore scopes that are not reachable.
92f9c275feSReid Kleckner void DebugHandlerBase::identifyScopeMarkers() {
93f9c275feSReid Kleckner   SmallVector<LexicalScope *, 4> WorkList;
94f9c275feSReid Kleckner   WorkList.push_back(LScopes.getCurrentFunctionScope());
95f9c275feSReid Kleckner   while (!WorkList.empty()) {
96f9c275feSReid Kleckner     LexicalScope *S = WorkList.pop_back_val();
97f9c275feSReid Kleckner 
98f9c275feSReid Kleckner     const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
99f9c275feSReid Kleckner     if (!Children.empty())
100f9c275feSReid Kleckner       WorkList.append(Children.begin(), Children.end());
101f9c275feSReid Kleckner 
102f9c275feSReid Kleckner     if (S->isAbstractScope())
103f9c275feSReid Kleckner       continue;
104f9c275feSReid Kleckner 
105f9c275feSReid Kleckner     for (const InsnRange &R : S->getRanges()) {
106f9c275feSReid Kleckner       assert(R.first && "InsnRange does not have first instruction!");
107f9c275feSReid Kleckner       assert(R.second && "InsnRange does not have second instruction!");
108f9c275feSReid Kleckner       requestLabelBeforeInsn(R.first);
109f9c275feSReid Kleckner       requestLabelAfterInsn(R.second);
110f9c275feSReid Kleckner     }
111f9c275feSReid Kleckner   }
112f9c275feSReid Kleckner }
113f9c275feSReid Kleckner 
114f9c275feSReid Kleckner // Return Label preceding the instruction.
115f9c275feSReid Kleckner MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
116f9c275feSReid Kleckner   MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
117f9c275feSReid Kleckner   assert(Label && "Didn't insert label before instruction");
118f9c275feSReid Kleckner   return Label;
119f9c275feSReid Kleckner }
120f9c275feSReid Kleckner 
121f9c275feSReid Kleckner // Return Label immediately following the instruction.
122f9c275feSReid Kleckner MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
123f9c275feSReid Kleckner   return LabelsAfterInsn.lookup(MI);
124f9c275feSReid Kleckner }
125f9c275feSReid Kleckner 
126acee5685SAmjad Aboud /// If this type is derived from a base type then return base type size.
127acee5685SAmjad Aboud uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
128acee5685SAmjad Aboud   DIType *Ty = TyRef.resolve();
129acee5685SAmjad Aboud   assert(Ty);
130acee5685SAmjad Aboud   DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
131acee5685SAmjad Aboud   if (!DDTy)
132acee5685SAmjad Aboud     return Ty->getSizeInBits();
133acee5685SAmjad Aboud 
134acee5685SAmjad Aboud   unsigned Tag = DDTy->getTag();
135acee5685SAmjad Aboud 
136acee5685SAmjad Aboud   if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
137acee5685SAmjad Aboud       Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
138e1156c2eSVictor Leschuk       Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
139acee5685SAmjad Aboud     return DDTy->getSizeInBits();
140acee5685SAmjad Aboud 
141acee5685SAmjad Aboud   DIType *BaseType = DDTy->getBaseType().resolve();
142acee5685SAmjad Aboud 
14374bfafa1SAdrian McCarthy   if (!BaseType)
14474bfafa1SAdrian McCarthy     return 0;
145acee5685SAmjad Aboud 
146acee5685SAmjad Aboud   // If this is a derived type, go ahead and get the base type, unless it's a
147acee5685SAmjad Aboud   // reference then it's just the size of the field. Pointer types have no need
148acee5685SAmjad Aboud   // of this since they're a different type of qualification on the type.
149acee5685SAmjad Aboud   if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
150acee5685SAmjad Aboud       BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
151acee5685SAmjad Aboud     return Ty->getSizeInBits();
152acee5685SAmjad Aboud 
153acee5685SAmjad Aboud   return getBaseTypeSize(BaseType);
154acee5685SAmjad Aboud }
155acee5685SAmjad Aboud 
156debb3c35SBenjamin Kramer static bool hasDebugInfo(const MachineModuleInfo *MMI,
157debb3c35SBenjamin Kramer                          const MachineFunction *MF) {
158b2fbb4b2SDavid Blaikie   if (!MMI->hasDebugInfo())
159b2fbb4b2SDavid Blaikie     return false;
160f1caa283SMatthias Braun   auto *SP = MF->getFunction().getSubprogram();
161b2fbb4b2SDavid Blaikie   if (!SP)
162b2fbb4b2SDavid Blaikie     return false;
163b2fbb4b2SDavid Blaikie   assert(SP->getUnit());
164b2fbb4b2SDavid Blaikie   auto EK = SP->getUnit()->getEmissionKind();
165b2fbb4b2SDavid Blaikie   if (EK == DICompileUnit::NoDebug)
166b2fbb4b2SDavid Blaikie     return false;
167b2fbb4b2SDavid Blaikie   return true;
168b2fbb4b2SDavid Blaikie }
169b2fbb4b2SDavid Blaikie 
170f9c275feSReid Kleckner void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
1718f333795SAdrian Prantl   PrevInstBB = nullptr;
172b2fbb4b2SDavid Blaikie 
1735bc8543aSReid Kleckner   if (!Asm || !hasDebugInfo(MMI, MF)) {
174b2fbb4b2SDavid Blaikie     skippedNonDebugFunction();
175b2fbb4b2SDavid Blaikie     return;
176b2fbb4b2SDavid Blaikie   }
177b2fbb4b2SDavid Blaikie 
178f9c275feSReid Kleckner   // Grab the lexical scopes for the function, if we don't have any of those
179f9c275feSReid Kleckner   // then we're not going to be able to do anything.
180f9c275feSReid Kleckner   LScopes.initialize(*MF);
181b2fbb4b2SDavid Blaikie   if (LScopes.empty()) {
182b2fbb4b2SDavid Blaikie     beginFunctionImpl(MF);
183f9c275feSReid Kleckner     return;
184b2fbb4b2SDavid Blaikie   }
185f9c275feSReid Kleckner 
186f9c275feSReid Kleckner   // Make sure that each lexical scope will have a begin/end label.
187f9c275feSReid Kleckner   identifyScopeMarkers();
188f9c275feSReid Kleckner 
189f9c275feSReid Kleckner   // Calculate history for local variables.
190f9c275feSReid Kleckner   assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
191f9c275feSReid Kleckner   calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
192f9c275feSReid Kleckner                            DbgValues);
193f9c275feSReid Kleckner 
194f9c275feSReid Kleckner   // Request labels for the full history.
195f9c275feSReid Kleckner   for (const auto &I : DbgValues) {
196f9c275feSReid Kleckner     const auto &Ranges = I.second;
197f9c275feSReid Kleckner     if (Ranges.empty())
198f9c275feSReid Kleckner       continue;
199f9c275feSReid Kleckner 
200f9c275feSReid Kleckner     // The first mention of a function argument gets the CurrentFnBegin
201f9c275feSReid Kleckner     // label, so arguments are visible when breaking at function entry.
202f9c275feSReid Kleckner     const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
203f9c275feSReid Kleckner     if (DIVar->isParameter() &&
204f1caa283SMatthias Braun         getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
205f9c275feSReid Kleckner       LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
206941fa758SAdrian Prantl       if (Ranges.front().first->getDebugExpression()->isFragment()) {
207941fa758SAdrian Prantl         // Mark all non-overlapping initial fragments.
208f9c275feSReid Kleckner         for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
209941fa758SAdrian Prantl           const DIExpression *Fragment = I->first->getDebugExpression();
210f9c275feSReid Kleckner           if (std::all_of(Ranges.begin(), I,
211f9c275feSReid Kleckner                           [&](DbgValueHistoryMap::InstrRange Pred) {
212*a223f815SBjorn Pettersson                             return !Fragment->fragmentsOverlap(
213*a223f815SBjorn Pettersson                                 Pred.first->getDebugExpression());
214f9c275feSReid Kleckner                           }))
215f9c275feSReid Kleckner             LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
216f9c275feSReid Kleckner           else
217f9c275feSReid Kleckner             break;
218f9c275feSReid Kleckner         }
219f9c275feSReid Kleckner       }
220f9c275feSReid Kleckner     }
221f9c275feSReid Kleckner 
222f9c275feSReid Kleckner     for (const auto &Range : Ranges) {
223f9c275feSReid Kleckner       requestLabelBeforeInsn(Range.first);
224f9c275feSReid Kleckner       if (Range.second)
225f9c275feSReid Kleckner         requestLabelAfterInsn(Range.second);
226f9c275feSReid Kleckner     }
227f9c275feSReid Kleckner   }
228f9c275feSReid Kleckner 
229f9c275feSReid Kleckner   PrevInstLoc = DebugLoc();
230f9c275feSReid Kleckner   PrevLabel = Asm->getFunctionBegin();
231b2fbb4b2SDavid Blaikie   beginFunctionImpl(MF);
232f9c275feSReid Kleckner }
233f9c275feSReid Kleckner 
234f9c275feSReid Kleckner void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
235f9c275feSReid Kleckner   if (!MMI->hasDebugInfo())
236f9c275feSReid Kleckner     return;
237f9c275feSReid Kleckner 
238f9c275feSReid Kleckner   assert(CurMI == nullptr);
239f9c275feSReid Kleckner   CurMI = MI;
240f9c275feSReid Kleckner 
241f9c275feSReid Kleckner   // Insert labels where requested.
242f9c275feSReid Kleckner   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
243f9c275feSReid Kleckner       LabelsBeforeInsn.find(MI);
244f9c275feSReid Kleckner 
245f9c275feSReid Kleckner   // No label needed.
246f9c275feSReid Kleckner   if (I == LabelsBeforeInsn.end())
247f9c275feSReid Kleckner     return;
248f9c275feSReid Kleckner 
249f9c275feSReid Kleckner   // Label already assigned.
250f9c275feSReid Kleckner   if (I->second)
251f9c275feSReid Kleckner     return;
252f9c275feSReid Kleckner 
253f9c275feSReid Kleckner   if (!PrevLabel) {
254f9c275feSReid Kleckner     PrevLabel = MMI->getContext().createTempSymbol();
255f9c275feSReid Kleckner     Asm->OutStreamer->EmitLabel(PrevLabel);
256f9c275feSReid Kleckner   }
257f9c275feSReid Kleckner   I->second = PrevLabel;
258f9c275feSReid Kleckner }
259f9c275feSReid Kleckner 
260f9c275feSReid Kleckner void DebugHandlerBase::endInstruction() {
261f9c275feSReid Kleckner   if (!MMI->hasDebugInfo())
262f9c275feSReid Kleckner     return;
263f9c275feSReid Kleckner 
264f9c275feSReid Kleckner   assert(CurMI != nullptr);
265fb31da13SAdrian Prantl   // Don't create a new label after DBG_VALUE and other instructions that don't
266fb31da13SAdrian Prantl   // generate code.
267fb31da13SAdrian Prantl   if (!CurMI->isMetaInstruction()) {
268f9c275feSReid Kleckner     PrevLabel = nullptr;
269ac7fe5e0SPaul Robinson     PrevInstBB = CurMI->getParent();
270ac7fe5e0SPaul Robinson   }
271f9c275feSReid Kleckner 
272f9c275feSReid Kleckner   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
273f9c275feSReid Kleckner       LabelsAfterInsn.find(CurMI);
274f9c275feSReid Kleckner   CurMI = nullptr;
275f9c275feSReid Kleckner 
276f9c275feSReid Kleckner   // No label needed.
277f9c275feSReid Kleckner   if (I == LabelsAfterInsn.end())
278f9c275feSReid Kleckner     return;
279f9c275feSReid Kleckner 
280f9c275feSReid Kleckner   // Label already assigned.
281f9c275feSReid Kleckner   if (I->second)
282f9c275feSReid Kleckner     return;
283f9c275feSReid Kleckner 
284f9c275feSReid Kleckner   // We need a label after this instruction.
285f9c275feSReid Kleckner   if (!PrevLabel) {
286f9c275feSReid Kleckner     PrevLabel = MMI->getContext().createTempSymbol();
287f9c275feSReid Kleckner     Asm->OutStreamer->EmitLabel(PrevLabel);
288f9c275feSReid Kleckner   }
289f9c275feSReid Kleckner   I->second = PrevLabel;
290f9c275feSReid Kleckner }
291f9c275feSReid Kleckner 
292f9c275feSReid Kleckner void DebugHandlerBase::endFunction(const MachineFunction *MF) {
293b2fbb4b2SDavid Blaikie   if (hasDebugInfo(MMI, MF))
294b2fbb4b2SDavid Blaikie     endFunctionImpl(MF);
295f9c275feSReid Kleckner   DbgValues.clear();
296f9c275feSReid Kleckner   LabelsBeforeInsn.clear();
297f9c275feSReid Kleckner   LabelsAfterInsn.clear();
298f9c275feSReid Kleckner }
299