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.
11*f4873346SYonghong Song // 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 
12874533bd3SVedant Kumar // Return the function-local offset of an instruction.
12974533bd3SVedant Kumar const MCExpr *
13074533bd3SVedant Kumar DebugHandlerBase::getFunctionLocalOffsetAfterInsn(const MachineInstr *MI) {
13174533bd3SVedant Kumar   MCContext &MC = Asm->OutContext;
13274533bd3SVedant Kumar 
13374533bd3SVedant Kumar   MCSymbol *Start = Asm->getFunctionBegin();
13474533bd3SVedant Kumar   const auto *StartRef = MCSymbolRefExpr::create(Start, MC);
13574533bd3SVedant Kumar 
13674533bd3SVedant Kumar   MCSymbol *AfterInsn = getLabelAfterInsn(MI);
13774533bd3SVedant Kumar   assert(AfterInsn && "Expected label after instruction");
13874533bd3SVedant Kumar   const auto *AfterRef = MCSymbolRefExpr::create(AfterInsn, MC);
13974533bd3SVedant Kumar 
14074533bd3SVedant Kumar   return MCBinaryExpr::createSub(AfterRef, StartRef, MC);
14174533bd3SVedant Kumar }
14274533bd3SVedant Kumar 
143acee5685SAmjad Aboud /// If this type is derived from a base type then return base type size.
144acee5685SAmjad Aboud uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
145acee5685SAmjad Aboud   DIType *Ty = TyRef.resolve();
146acee5685SAmjad Aboud   assert(Ty);
147acee5685SAmjad Aboud   DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
148acee5685SAmjad Aboud   if (!DDTy)
149acee5685SAmjad Aboud     return Ty->getSizeInBits();
150acee5685SAmjad Aboud 
151acee5685SAmjad Aboud   unsigned Tag = DDTy->getTag();
152acee5685SAmjad Aboud 
153acee5685SAmjad Aboud   if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
154acee5685SAmjad Aboud       Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
155e1156c2eSVictor Leschuk       Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
156acee5685SAmjad Aboud     return DDTy->getSizeInBits();
157acee5685SAmjad Aboud 
158acee5685SAmjad Aboud   DIType *BaseType = DDTy->getBaseType().resolve();
159acee5685SAmjad Aboud 
16074bfafa1SAdrian McCarthy   if (!BaseType)
16174bfafa1SAdrian McCarthy     return 0;
162acee5685SAmjad Aboud 
163acee5685SAmjad Aboud   // If this is a derived type, go ahead and get the base type, unless it's a
164acee5685SAmjad Aboud   // reference then it's just the size of the field. Pointer types have no need
165acee5685SAmjad Aboud   // of this since they're a different type of qualification on the type.
166acee5685SAmjad Aboud   if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
167acee5685SAmjad Aboud       BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
168acee5685SAmjad Aboud     return Ty->getSizeInBits();
169acee5685SAmjad Aboud 
170acee5685SAmjad Aboud   return getBaseTypeSize(BaseType);
171acee5685SAmjad Aboud }
172acee5685SAmjad Aboud 
173debb3c35SBenjamin Kramer static bool hasDebugInfo(const MachineModuleInfo *MMI,
174debb3c35SBenjamin Kramer                          const MachineFunction *MF) {
175b2fbb4b2SDavid Blaikie   if (!MMI->hasDebugInfo())
176b2fbb4b2SDavid Blaikie     return false;
177f1caa283SMatthias Braun   auto *SP = MF->getFunction().getSubprogram();
178b2fbb4b2SDavid Blaikie   if (!SP)
179b2fbb4b2SDavid Blaikie     return false;
180b2fbb4b2SDavid Blaikie   assert(SP->getUnit());
181b2fbb4b2SDavid Blaikie   auto EK = SP->getUnit()->getEmissionKind();
182b2fbb4b2SDavid Blaikie   if (EK == DICompileUnit::NoDebug)
183b2fbb4b2SDavid Blaikie     return false;
184b2fbb4b2SDavid Blaikie   return true;
185b2fbb4b2SDavid Blaikie }
186b2fbb4b2SDavid Blaikie 
187f9c275feSReid Kleckner void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
1888f333795SAdrian Prantl   PrevInstBB = nullptr;
189b2fbb4b2SDavid Blaikie 
1905bc8543aSReid Kleckner   if (!Asm || !hasDebugInfo(MMI, MF)) {
191b2fbb4b2SDavid Blaikie     skippedNonDebugFunction();
192b2fbb4b2SDavid Blaikie     return;
193b2fbb4b2SDavid Blaikie   }
194b2fbb4b2SDavid Blaikie 
195f9c275feSReid Kleckner   // Grab the lexical scopes for the function, if we don't have any of those
196f9c275feSReid Kleckner   // then we're not going to be able to do anything.
197f9c275feSReid Kleckner   LScopes.initialize(*MF);
198b2fbb4b2SDavid Blaikie   if (LScopes.empty()) {
199b2fbb4b2SDavid Blaikie     beginFunctionImpl(MF);
200f9c275feSReid Kleckner     return;
201b2fbb4b2SDavid Blaikie   }
202f9c275feSReid Kleckner 
203f9c275feSReid Kleckner   // Make sure that each lexical scope will have a begin/end label.
204f9c275feSReid Kleckner   identifyScopeMarkers();
205f9c275feSReid Kleckner 
206f9c275feSReid Kleckner   // Calculate history for local variables.
207f9c275feSReid Kleckner   assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
2082532ac88SHsiangkai Wang   assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
2092532ac88SHsiangkai Wang   calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
2102532ac88SHsiangkai Wang                             DbgValues, DbgLabels);
2117224c081SVedant Kumar   LLVM_DEBUG(DbgValues.dump());
212f9c275feSReid Kleckner 
213f9c275feSReid Kleckner   // Request labels for the full history.
214f9c275feSReid Kleckner   for (const auto &I : DbgValues) {
215f9c275feSReid Kleckner     const auto &Ranges = I.second;
216f9c275feSReid Kleckner     if (Ranges.empty())
217f9c275feSReid Kleckner       continue;
218f9c275feSReid Kleckner 
219f9c275feSReid Kleckner     // The first mention of a function argument gets the CurrentFnBegin
220f9c275feSReid Kleckner     // label, so arguments are visible when breaking at function entry.
221f9c275feSReid Kleckner     const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
222f9c275feSReid Kleckner     if (DIVar->isParameter() &&
223f1caa283SMatthias Braun         getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
224f9c275feSReid Kleckner       LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
225941fa758SAdrian Prantl       if (Ranges.front().first->getDebugExpression()->isFragment()) {
226941fa758SAdrian Prantl         // Mark all non-overlapping initial fragments.
227f9c275feSReid Kleckner         for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
228941fa758SAdrian Prantl           const DIExpression *Fragment = I->first->getDebugExpression();
229f9c275feSReid Kleckner           if (std::all_of(Ranges.begin(), I,
230f9c275feSReid Kleckner                           [&](DbgValueHistoryMap::InstrRange Pred) {
231a223f815SBjorn Pettersson                             return !Fragment->fragmentsOverlap(
232a223f815SBjorn Pettersson                                 Pred.first->getDebugExpression());
233f9c275feSReid Kleckner                           }))
234f9c275feSReid Kleckner             LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
235f9c275feSReid Kleckner           else
236f9c275feSReid Kleckner             break;
237f9c275feSReid Kleckner         }
238f9c275feSReid Kleckner       }
239f9c275feSReid Kleckner     }
240f9c275feSReid Kleckner 
241f9c275feSReid Kleckner     for (const auto &Range : Ranges) {
242f9c275feSReid Kleckner       requestLabelBeforeInsn(Range.first);
243f9c275feSReid Kleckner       if (Range.second)
244f9c275feSReid Kleckner         requestLabelAfterInsn(Range.second);
245f9c275feSReid Kleckner     }
246f9c275feSReid Kleckner   }
247f9c275feSReid Kleckner 
2482532ac88SHsiangkai Wang   // Ensure there is a symbol before DBG_LABEL.
2492532ac88SHsiangkai Wang   for (const auto &I : DbgLabels) {
2502532ac88SHsiangkai Wang     const MachineInstr *MI = I.second;
2512532ac88SHsiangkai Wang     requestLabelBeforeInsn(MI);
2522532ac88SHsiangkai Wang   }
2532532ac88SHsiangkai Wang 
254f9c275feSReid Kleckner   PrevInstLoc = DebugLoc();
255f9c275feSReid Kleckner   PrevLabel = Asm->getFunctionBegin();
256b2fbb4b2SDavid Blaikie   beginFunctionImpl(MF);
257f9c275feSReid Kleckner }
258f9c275feSReid Kleckner 
259f9c275feSReid Kleckner void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
260f9c275feSReid Kleckner   if (!MMI->hasDebugInfo())
261f9c275feSReid Kleckner     return;
262f9c275feSReid Kleckner 
263f9c275feSReid Kleckner   assert(CurMI == nullptr);
264f9c275feSReid Kleckner   CurMI = MI;
265f9c275feSReid Kleckner 
266f9c275feSReid Kleckner   // Insert labels where requested.
267f9c275feSReid Kleckner   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
268f9c275feSReid Kleckner       LabelsBeforeInsn.find(MI);
269f9c275feSReid Kleckner 
270f9c275feSReid Kleckner   // No label needed.
271f9c275feSReid Kleckner   if (I == LabelsBeforeInsn.end())
272f9c275feSReid Kleckner     return;
273f9c275feSReid Kleckner 
274f9c275feSReid Kleckner   // Label already assigned.
275f9c275feSReid Kleckner   if (I->second)
276f9c275feSReid Kleckner     return;
277f9c275feSReid Kleckner 
278f9c275feSReid Kleckner   if (!PrevLabel) {
279f9c275feSReid Kleckner     PrevLabel = MMI->getContext().createTempSymbol();
280f9c275feSReid Kleckner     Asm->OutStreamer->EmitLabel(PrevLabel);
281f9c275feSReid Kleckner   }
282f9c275feSReid Kleckner   I->second = PrevLabel;
283f9c275feSReid Kleckner }
284f9c275feSReid Kleckner 
285f9c275feSReid Kleckner void DebugHandlerBase::endInstruction() {
286f9c275feSReid Kleckner   if (!MMI->hasDebugInfo())
287f9c275feSReid Kleckner     return;
288f9c275feSReid Kleckner 
289f9c275feSReid Kleckner   assert(CurMI != nullptr);
290fb31da13SAdrian Prantl   // Don't create a new label after DBG_VALUE and other instructions that don't
291fb31da13SAdrian Prantl   // generate code.
292fb31da13SAdrian Prantl   if (!CurMI->isMetaInstruction()) {
293f9c275feSReid Kleckner     PrevLabel = nullptr;
294ac7fe5e0SPaul Robinson     PrevInstBB = CurMI->getParent();
295ac7fe5e0SPaul Robinson   }
296f9c275feSReid Kleckner 
297f9c275feSReid Kleckner   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
298f9c275feSReid Kleckner       LabelsAfterInsn.find(CurMI);
299f9c275feSReid Kleckner   CurMI = nullptr;
300f9c275feSReid Kleckner 
301f9c275feSReid Kleckner   // No label needed.
302f9c275feSReid Kleckner   if (I == LabelsAfterInsn.end())
303f9c275feSReid Kleckner     return;
304f9c275feSReid Kleckner 
305f9c275feSReid Kleckner   // Label already assigned.
306f9c275feSReid Kleckner   if (I->second)
307f9c275feSReid Kleckner     return;
308f9c275feSReid Kleckner 
309f9c275feSReid Kleckner   // We need a label after this instruction.
310f9c275feSReid Kleckner   if (!PrevLabel) {
311f9c275feSReid Kleckner     PrevLabel = MMI->getContext().createTempSymbol();
312f9c275feSReid Kleckner     Asm->OutStreamer->EmitLabel(PrevLabel);
313f9c275feSReid Kleckner   }
314f9c275feSReid Kleckner   I->second = PrevLabel;
315f9c275feSReid Kleckner }
316f9c275feSReid Kleckner 
317f9c275feSReid Kleckner void DebugHandlerBase::endFunction(const MachineFunction *MF) {
318b2fbb4b2SDavid Blaikie   if (hasDebugInfo(MMI, MF))
319b2fbb4b2SDavid Blaikie     endFunctionImpl(MF);
320f9c275feSReid Kleckner   DbgValues.clear();
3212532ac88SHsiangkai Wang   DbgLabels.clear();
322f9c275feSReid Kleckner   LabelsBeforeInsn.clear();
323f9c275feSReid Kleckner   LabelsAfterInsn.clear();
324f9c275feSReid Kleckner }
325