xref: /llvm-project/llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp (revision 4169338e75cdce73d34063532db598c95ee82ae4)
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/CodeGen/AsmPrinter.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/CodeGen/MachineModuleInfo.h"
19 #include "llvm/CodeGen/TargetSubtargetInfo.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/Support/CommandLine.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "dwarfdebug"
28 
29 /// If true, we drop variable location ranges which exist entirely outside the
30 /// variable's lexical scope instruction ranges.
31 static cl::opt<bool> TrimVarLocs("trim-var-locs", cl::Hidden, cl::init(true));
32 
33 std::optional<DbgVariableLocation>
34 DbgVariableLocation::extractFromMachineInstruction(
35     const MachineInstr &Instruction) {
36   DbgVariableLocation Location;
37   // Variables calculated from multiple locations can't be represented here.
38   if (Instruction.getNumDebugOperands() != 1)
39     return std::nullopt;
40   if (!Instruction.getDebugOperand(0).isReg())
41     return std::nullopt;
42   Location.Register = Instruction.getDebugOperand(0).getReg();
43   Location.FragmentInfo.reset();
44   // We only handle expressions generated by DIExpression::appendOffset,
45   // which doesn't require a full stack machine.
46   int64_t Offset = 0;
47   const DIExpression *DIExpr = Instruction.getDebugExpression();
48   auto Op = DIExpr->expr_op_begin();
49   // We can handle a DBG_VALUE_LIST iff it has exactly one location operand that
50   // appears exactly once at the start of the expression.
51   if (Instruction.isDebugValueList()) {
52     if (Instruction.getNumDebugOperands() == 1 &&
53         Op->getOp() == dwarf::DW_OP_LLVM_arg)
54       ++Op;
55     else
56       return std::nullopt;
57   }
58   while (Op != DIExpr->expr_op_end()) {
59     switch (Op->getOp()) {
60     case dwarf::DW_OP_constu: {
61       int Value = Op->getArg(0);
62       ++Op;
63       if (Op != DIExpr->expr_op_end()) {
64         switch (Op->getOp()) {
65         case dwarf::DW_OP_minus:
66           Offset -= Value;
67           break;
68         case dwarf::DW_OP_plus:
69           Offset += Value;
70           break;
71         default:
72           continue;
73         }
74       }
75     } break;
76     case dwarf::DW_OP_plus_uconst:
77       Offset += Op->getArg(0);
78       break;
79     case dwarf::DW_OP_LLVM_fragment:
80       Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
81       break;
82     case dwarf::DW_OP_deref:
83       Location.LoadChain.push_back(Offset);
84       Offset = 0;
85       break;
86     default:
87       return std::nullopt;
88     }
89     ++Op;
90   }
91 
92   // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
93   // instruction.
94   // FIXME: Replace these with DIExpression.
95   if (Instruction.isIndirectDebugValue())
96     Location.LoadChain.push_back(Offset);
97 
98   return Location;
99 }
100 
101 DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
102 
103 void DebugHandlerBase::beginModule(Module *M) {
104   if (M->debug_compile_units().empty())
105     Asm = nullptr;
106 }
107 
108 // Each LexicalScope has first instruction and last instruction to mark
109 // beginning and end of a scope respectively. Create an inverse map that list
110 // scopes starts (and ends) with an instruction. One instruction may start (or
111 // end) multiple scopes. Ignore scopes that are not reachable.
112 void DebugHandlerBase::identifyScopeMarkers() {
113   SmallVector<LexicalScope *, 4> WorkList;
114   WorkList.push_back(LScopes.getCurrentFunctionScope());
115   while (!WorkList.empty()) {
116     LexicalScope *S = WorkList.pop_back_val();
117 
118     const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
119     if (!Children.empty())
120       WorkList.append(Children.begin(), Children.end());
121 
122     if (S->isAbstractScope())
123       continue;
124 
125     for (const InsnRange &R : S->getRanges()) {
126       assert(R.first && "InsnRange does not have first instruction!");
127       assert(R.second && "InsnRange does not have second instruction!");
128       requestLabelBeforeInsn(R.first);
129       requestLabelAfterInsn(R.second);
130     }
131   }
132 }
133 
134 // Return Label preceding the instruction.
135 MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
136   MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
137   assert(Label && "Didn't insert label before instruction");
138   return Label;
139 }
140 
141 // Return Label immediately following the instruction.
142 MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
143   return LabelsAfterInsn.lookup(MI);
144 }
145 
146 /// If this type is derived from a base type then return base type size.
147 uint64_t DebugHandlerBase::getBaseTypeSize(const DIType *Ty) {
148   assert(Ty);
149   const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
150   if (!DDTy)
151     return Ty->getSizeInBits();
152 
153   unsigned Tag = DDTy->getTag();
154 
155   if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
156       Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
157       Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type &&
158       Tag != dwarf::DW_TAG_immutable_type &&
159       Tag != dwarf::DW_TAG_template_alias)
160     return DDTy->getSizeInBits();
161 
162   DIType *BaseType = DDTy->getBaseType();
163 
164   if (!BaseType)
165     return 0;
166 
167   // If this is a derived type, go ahead and get the base type, unless it's a
168   // reference then it's just the size of the field. Pointer types have no need
169   // of this since they're a different type of qualification on the type.
170   if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
171       BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
172     return Ty->getSizeInBits();
173 
174   return getBaseTypeSize(BaseType);
175 }
176 
177 bool DebugHandlerBase::isUnsignedDIType(const DIType *Ty) {
178   if (isa<DIStringType>(Ty)) {
179     // Some transformations (e.g. instcombine) may decide to turn a Fortran
180     // character object into an integer, and later ones (e.g. SROA) may
181     // further inject a constant integer in a llvm.dbg.value call to track
182     // the object's value. Here we trust the transformations are doing the
183     // right thing, and treat the constant as unsigned to preserve that value
184     // (i.e. avoid sign extension).
185     return true;
186   }
187 
188   if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
189     if (CTy->getTag() == dwarf::DW_TAG_enumeration_type) {
190       if (!(Ty = CTy->getBaseType()))
191         // FIXME: Enums without a fixed underlying type have unknown signedness
192         // here, leading to incorrectly emitted constants.
193         return false;
194     } else
195       // (Pieces of) aggregate types that get hacked apart by SROA may be
196       // represented by a constant. Encode them as unsigned bytes.
197       return true;
198   }
199 
200   if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
201     dwarf::Tag T = (dwarf::Tag)Ty->getTag();
202     // Encode pointer constants as unsigned bytes. This is used at least for
203     // null pointer constant emission.
204     // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
205     // here, but accept them for now due to a bug in SROA producing bogus
206     // dbg.values.
207     if (T == dwarf::DW_TAG_pointer_type ||
208         T == dwarf::DW_TAG_ptr_to_member_type ||
209         T == dwarf::DW_TAG_reference_type ||
210         T == dwarf::DW_TAG_rvalue_reference_type)
211       return true;
212     assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
213            T == dwarf::DW_TAG_volatile_type ||
214            T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type ||
215            T == dwarf::DW_TAG_immutable_type ||
216            T == dwarf::DW_TAG_template_alias);
217     assert(DTy->getBaseType() && "Expected valid base type");
218     return isUnsignedDIType(DTy->getBaseType());
219   }
220 
221   auto *BTy = cast<DIBasicType>(Ty);
222   unsigned Encoding = BTy->getEncoding();
223   assert((Encoding == dwarf::DW_ATE_unsigned ||
224           Encoding == dwarf::DW_ATE_unsigned_char ||
225           Encoding == dwarf::DW_ATE_signed ||
226           Encoding == dwarf::DW_ATE_signed_char ||
227           Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
228           Encoding == dwarf::DW_ATE_boolean ||
229           Encoding == dwarf::DW_ATE_complex_float ||
230           Encoding == dwarf::DW_ATE_signed_fixed ||
231           Encoding == dwarf::DW_ATE_unsigned_fixed ||
232           (Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
233            Ty->getName() == "decltype(nullptr)")) &&
234          "Unsupported encoding");
235   return Encoding == dwarf::DW_ATE_unsigned ||
236          Encoding == dwarf::DW_ATE_unsigned_char ||
237          Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
238          Encoding == llvm::dwarf::DW_ATE_unsigned_fixed ||
239          Ty->getTag() == dwarf::DW_TAG_unspecified_type;
240 }
241 
242 static bool hasDebugInfo(const MachineModuleInfo *MMI,
243                          const MachineFunction *MF) {
244   if (!MMI->hasDebugInfo())
245     return false;
246   auto *SP = MF->getFunction().getSubprogram();
247   if (!SP)
248     return false;
249   assert(SP->getUnit());
250   auto EK = SP->getUnit()->getEmissionKind();
251   if (EK == DICompileUnit::NoDebug)
252     return false;
253   return true;
254 }
255 
256 void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
257   PrevInstBB = nullptr;
258 
259   if (!Asm || !hasDebugInfo(MMI, MF)) {
260     skippedNonDebugFunction();
261     return;
262   }
263 
264   // Grab the lexical scopes for the function, if we don't have any of those
265   // then we're not going to be able to do anything.
266   LScopes.initialize(*MF);
267   if (LScopes.empty()) {
268     beginFunctionImpl(MF);
269     return;
270   }
271 
272   // Make sure that each lexical scope will have a begin/end label.
273   identifyScopeMarkers();
274 
275   // Calculate history for local variables.
276   assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
277   assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
278   calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
279                             DbgValues, DbgLabels);
280   InstOrdering.initialize(*MF);
281   if (TrimVarLocs)
282     DbgValues.trimLocationRanges(*MF, LScopes, InstOrdering);
283   LLVM_DEBUG(DbgValues.dump(MF->getName()));
284 
285   // Request labels for the full history.
286   for (const auto &I : DbgValues) {
287     const auto &Entries = I.second;
288     if (Entries.empty())
289       continue;
290 
291     auto IsDescribedByReg = [](const MachineInstr *MI) {
292       return any_of(MI->debug_operands(),
293                     [](auto &MO) { return MO.isReg() && MO.getReg(); });
294     };
295 
296     // The first mention of a function argument gets the CurrentFnBegin label,
297     // so arguments are visible when breaking at function entry.
298     //
299     // We do not change the label for values that are described by registers,
300     // as that could place them above their defining instructions. We should
301     // ideally not change the labels for constant debug values either, since
302     // doing that violates the ranges that are calculated in the history map.
303     // However, we currently do not emit debug values for constant arguments
304     // directly at the start of the function, so this code is still useful.
305     const DILocalVariable *DIVar =
306         Entries.front().getInstr()->getDebugVariable();
307     if (DIVar->isParameter() &&
308         getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
309       if (!IsDescribedByReg(Entries.front().getInstr()))
310         LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin();
311       if (Entries.front().getInstr()->getDebugExpression()->isFragment()) {
312         // Mark all non-overlapping initial fragments.
313         for (const auto *I = Entries.begin(); I != Entries.end(); ++I) {
314           if (!I->isDbgValue())
315             continue;
316           const DIExpression *Fragment = I->getInstr()->getDebugExpression();
317           if (std::any_of(Entries.begin(), I,
318                           [&](DbgValueHistoryMap::Entry Pred) {
319                             return Pred.isDbgValue() &&
320                                    Fragment->fragmentsOverlap(
321                                        Pred.getInstr()->getDebugExpression());
322                           }))
323             break;
324           // The code that generates location lists for DWARF assumes that the
325           // entries' start labels are monotonically increasing, and since we
326           // don't change the label for fragments that are described by
327           // registers, we must bail out when encountering such a fragment.
328           if (IsDescribedByReg(I->getInstr()))
329             break;
330           LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin();
331         }
332       }
333     }
334 
335     for (const auto &Entry : Entries) {
336       if (Entry.isDbgValue())
337         requestLabelBeforeInsn(Entry.getInstr());
338       else
339         requestLabelAfterInsn(Entry.getInstr());
340     }
341   }
342 
343   // Ensure there is a symbol before DBG_LABEL.
344   for (const auto &I : DbgLabels) {
345     const MachineInstr *MI = I.second;
346     requestLabelBeforeInsn(MI);
347   }
348 
349   PrevInstLoc = DebugLoc();
350   PrevLabel = Asm->getFunctionBegin();
351   beginFunctionImpl(MF);
352 }
353 
354 void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
355   if (!Asm || !MMI->hasDebugInfo())
356     return;
357 
358   assert(CurMI == nullptr);
359   CurMI = MI;
360 
361   // Insert labels where requested.
362   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
363       LabelsBeforeInsn.find(MI);
364 
365   // No label needed.
366   if (I == LabelsBeforeInsn.end())
367     return;
368 
369   // Label already assigned.
370   if (I->second)
371     return;
372 
373   if (!PrevLabel) {
374     PrevLabel = MMI->getContext().createTempSymbol();
375     Asm->OutStreamer->emitLabel(PrevLabel);
376   }
377   I->second = PrevLabel;
378 }
379 
380 void DebugHandlerBase::endInstruction() {
381   if (!Asm || !MMI->hasDebugInfo())
382     return;
383 
384   assert(CurMI != nullptr);
385   // Don't create a new label after DBG_VALUE and other instructions that don't
386   // generate code.
387   if (!CurMI->isMetaInstruction()) {
388     PrevLabel = nullptr;
389     PrevInstBB = CurMI->getParent();
390   }
391 
392   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
393       LabelsAfterInsn.find(CurMI);
394 
395   // No label needed or label already assigned.
396   if (I == LabelsAfterInsn.end() || I->second) {
397     CurMI = nullptr;
398     return;
399   }
400 
401   // We need a label after this instruction.  With basic block sections, just
402   // use the end symbol of the section if this is the last instruction of the
403   // section.  This reduces the need for an additional label and also helps
404   // merging ranges.
405   if (CurMI->getParent()->isEndSection() && CurMI->getNextNode() == nullptr) {
406     PrevLabel = CurMI->getParent()->getEndSymbol();
407   } else if (!PrevLabel) {
408     PrevLabel = MMI->getContext().createTempSymbol();
409     Asm->OutStreamer->emitLabel(PrevLabel);
410   }
411   I->second = PrevLabel;
412   CurMI = nullptr;
413 }
414 
415 void DebugHandlerBase::endFunction(const MachineFunction *MF) {
416   if (Asm && hasDebugInfo(MMI, MF))
417     endFunctionImpl(MF);
418   DbgValues.clear();
419   DbgLabels.clear();
420   LabelsBeforeInsn.clear();
421   LabelsAfterInsn.clear();
422   InstOrdering.clear();
423 }
424 
425 void DebugHandlerBase::beginBasicBlockSection(const MachineBasicBlock &MBB) {
426   EpilogBeginBlock = nullptr;
427   if (!MBB.isEntryBlock())
428     PrevLabel = MBB.getSymbol();
429 }
430 
431 void DebugHandlerBase::endBasicBlockSection(const MachineBasicBlock &MBB) {
432   PrevLabel = nullptr;
433 }
434