xref: /llvm-project/llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
1 //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Common functionality for different debug information format backends.
10 // LLVM currently supports DWARF and CodeView.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/DebugHandlerBase.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/CodeGen/TargetSubtargetInfo.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/MC/MCStreamer.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "dwarfdebug"
28 
29 Optional<DbgVariableLocation>
30 DbgVariableLocation::extractFromMachineInstruction(
31     const MachineInstr &Instruction) {
32   DbgVariableLocation Location;
33   if (!Instruction.isDebugValue())
34     return None;
35   if (!Instruction.getOperand(0).isReg())
36     return None;
37   Location.Register = Instruction.getOperand(0).getReg();
38   Location.FragmentInfo.reset();
39   // We only handle expressions generated by DIExpression::appendOffset,
40   // which doesn't require a full stack machine.
41   int64_t Offset = 0;
42   const DIExpression *DIExpr = Instruction.getDebugExpression();
43   auto Op = DIExpr->expr_op_begin();
44   while (Op != DIExpr->expr_op_end()) {
45     switch (Op->getOp()) {
46     case dwarf::DW_OP_constu: {
47       int Value = Op->getArg(0);
48       ++Op;
49       if (Op != DIExpr->expr_op_end()) {
50         switch (Op->getOp()) {
51         case dwarf::DW_OP_minus:
52           Offset -= Value;
53           break;
54         case dwarf::DW_OP_plus:
55           Offset += Value;
56           break;
57         default:
58           continue;
59         }
60       }
61     } break;
62     case dwarf::DW_OP_plus_uconst:
63       Offset += Op->getArg(0);
64       break;
65     case dwarf::DW_OP_LLVM_fragment:
66       Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
67       break;
68     case dwarf::DW_OP_deref:
69       Location.LoadChain.push_back(Offset);
70       Offset = 0;
71       break;
72     default:
73       return None;
74     }
75     ++Op;
76   }
77 
78   // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
79   // instruction.
80   // FIXME: Replace these with DIExpression.
81   if (Instruction.isIndirectDebugValue())
82     Location.LoadChain.push_back(Offset);
83 
84   return Location;
85 }
86 
87 DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
88 
89 // Each LexicalScope has first instruction and last instruction to mark
90 // beginning and end of a scope respectively. Create an inverse map that list
91 // scopes starts (and ends) with an instruction. One instruction may start (or
92 // end) multiple scopes. Ignore scopes that are not reachable.
93 void DebugHandlerBase::identifyScopeMarkers() {
94   SmallVector<LexicalScope *, 4> WorkList;
95   WorkList.push_back(LScopes.getCurrentFunctionScope());
96   while (!WorkList.empty()) {
97     LexicalScope *S = WorkList.pop_back_val();
98 
99     const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
100     if (!Children.empty())
101       WorkList.append(Children.begin(), Children.end());
102 
103     if (S->isAbstractScope())
104       continue;
105 
106     for (const InsnRange &R : S->getRanges()) {
107       assert(R.first && "InsnRange does not have first instruction!");
108       assert(R.second && "InsnRange does not have second instruction!");
109       requestLabelBeforeInsn(R.first);
110       requestLabelAfterInsn(R.second);
111     }
112   }
113 }
114 
115 // Return Label preceding the instruction.
116 MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
117   MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
118   assert(Label && "Didn't insert label before instruction");
119   return Label;
120 }
121 
122 // Return Label immediately following the instruction.
123 MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
124   return LabelsAfterInsn.lookup(MI);
125 }
126 
127 // Return the function-local offset of an instruction.
128 const MCExpr *
129 DebugHandlerBase::getFunctionLocalOffsetAfterInsn(const MachineInstr *MI) {
130   MCContext &MC = Asm->OutContext;
131 
132   MCSymbol *Start = Asm->getFunctionBegin();
133   const auto *StartRef = MCSymbolRefExpr::create(Start, MC);
134 
135   MCSymbol *AfterInsn = getLabelAfterInsn(MI);
136   assert(AfterInsn && "Expected label after instruction");
137   const auto *AfterRef = MCSymbolRefExpr::create(AfterInsn, MC);
138 
139   return MCBinaryExpr::createSub(AfterRef, StartRef, MC);
140 }
141 
142 /// If this type is derived from a base type then return base type size.
143 uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
144   DIType *Ty = TyRef.resolve();
145   assert(Ty);
146   DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
147   if (!DDTy)
148     return Ty->getSizeInBits();
149 
150   unsigned Tag = DDTy->getTag();
151 
152   if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
153       Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
154       Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
155     return DDTy->getSizeInBits();
156 
157   DIType *BaseType = DDTy->getBaseType().resolve();
158 
159   if (!BaseType)
160     return 0;
161 
162   // If this is a derived type, go ahead and get the base type, unless it's a
163   // reference then it's just the size of the field. Pointer types have no need
164   // of this since they're a different type of qualification on the type.
165   if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
166       BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
167     return Ty->getSizeInBits();
168 
169   return getBaseTypeSize(BaseType);
170 }
171 
172 static bool hasDebugInfo(const MachineModuleInfo *MMI,
173                          const MachineFunction *MF) {
174   if (!MMI->hasDebugInfo())
175     return false;
176   auto *SP = MF->getFunction().getSubprogram();
177   if (!SP)
178     return false;
179   assert(SP->getUnit());
180   auto EK = SP->getUnit()->getEmissionKind();
181   if (EK == DICompileUnit::NoDebug)
182     return false;
183   return true;
184 }
185 
186 void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
187   PrevInstBB = nullptr;
188 
189   if (!Asm || !hasDebugInfo(MMI, MF)) {
190     skippedNonDebugFunction();
191     return;
192   }
193 
194   // Grab the lexical scopes for the function, if we don't have any of those
195   // then we're not going to be able to do anything.
196   LScopes.initialize(*MF);
197   if (LScopes.empty()) {
198     beginFunctionImpl(MF);
199     return;
200   }
201 
202   // Make sure that each lexical scope will have a begin/end label.
203   identifyScopeMarkers();
204 
205   // Calculate history for local variables.
206   assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
207   assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
208   calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
209                             DbgValues, DbgLabels);
210   LLVM_DEBUG(DbgValues.dump());
211 
212   // Request labels for the full history.
213   for (const auto &I : DbgValues) {
214     const auto &Ranges = I.second;
215     if (Ranges.empty())
216       continue;
217 
218     // The first mention of a function argument gets the CurrentFnBegin
219     // label, so arguments are visible when breaking at function entry.
220     const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
221     if (DIVar->isParameter() &&
222         getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
223       LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
224       if (Ranges.front().first->getDebugExpression()->isFragment()) {
225         // Mark all non-overlapping initial fragments.
226         for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
227           const DIExpression *Fragment = I->first->getDebugExpression();
228           if (std::all_of(Ranges.begin(), I,
229                           [&](DbgValueHistoryMap::InstrRange Pred) {
230                             return !Fragment->fragmentsOverlap(
231                                 Pred.first->getDebugExpression());
232                           }))
233             LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
234           else
235             break;
236         }
237       }
238     }
239 
240     for (const auto &Range : Ranges) {
241       requestLabelBeforeInsn(Range.first);
242       if (Range.second)
243         requestLabelAfterInsn(Range.second);
244     }
245   }
246 
247   // Ensure there is a symbol before DBG_LABEL.
248   for (const auto &I : DbgLabels) {
249     const MachineInstr *MI = I.second;
250     requestLabelBeforeInsn(MI);
251   }
252 
253   PrevInstLoc = DebugLoc();
254   PrevLabel = Asm->getFunctionBegin();
255   beginFunctionImpl(MF);
256 }
257 
258 void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
259   if (!MMI->hasDebugInfo())
260     return;
261 
262   assert(CurMI == nullptr);
263   CurMI = MI;
264 
265   // Insert labels where requested.
266   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
267       LabelsBeforeInsn.find(MI);
268 
269   // No label needed.
270   if (I == LabelsBeforeInsn.end())
271     return;
272 
273   // Label already assigned.
274   if (I->second)
275     return;
276 
277   if (!PrevLabel) {
278     PrevLabel = MMI->getContext().createTempSymbol();
279     Asm->OutStreamer->EmitLabel(PrevLabel);
280   }
281   I->second = PrevLabel;
282 }
283 
284 void DebugHandlerBase::endInstruction() {
285   if (!MMI->hasDebugInfo())
286     return;
287 
288   assert(CurMI != nullptr);
289   // Don't create a new label after DBG_VALUE and other instructions that don't
290   // generate code.
291   if (!CurMI->isMetaInstruction()) {
292     PrevLabel = nullptr;
293     PrevInstBB = CurMI->getParent();
294   }
295 
296   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
297       LabelsAfterInsn.find(CurMI);
298   CurMI = nullptr;
299 
300   // No label needed.
301   if (I == LabelsAfterInsn.end())
302     return;
303 
304   // Label already assigned.
305   if (I->second)
306     return;
307 
308   // We need a label after this instruction.
309   if (!PrevLabel) {
310     PrevLabel = MMI->getContext().createTempSymbol();
311     Asm->OutStreamer->EmitLabel(PrevLabel);
312   }
313   I->second = PrevLabel;
314 }
315 
316 void DebugHandlerBase::endFunction(const MachineFunction *MF) {
317   if (hasDebugInfo(MMI, MF))
318     endFunctionImpl(MF);
319   DbgValues.clear();
320   DbgLabels.clear();
321   LabelsBeforeInsn.clear();
322   LabelsAfterInsn.clear();
323 }
324