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
25using namespace llvm;
26
27#define DEBUG_TYPE "dwarfdebug"
28
29Optional<DbgVariableLocation>
30DbgVariableLocation::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
87DebugHandlerBase::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.
93void 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.
116MCSymbol *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.
123MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
124  return LabelsAfterInsn.lookup(MI);
125}
126
127// Return the function-local offset of an instruction.
128const MCExpr *
129DebugHandlerBase::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.
143uint64_t DebugHandlerBase::getBaseTypeSize(const DIType *Ty) {
144  assert(Ty);
145  const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
146  if (!DDTy)
147    return Ty->getSizeInBits();
148
149  unsigned Tag = DDTy->getTag();
150
151  if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
152      Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
153      Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
154    return DDTy->getSizeInBits();
155
156  DIType *BaseType = DDTy->getBaseType();
157
158  if (!BaseType)
159    return 0;
160
161  // If this is a derived type, go ahead and get the base type, unless it's a
162  // reference then it's just the size of the field. Pointer types have no need
163  // of this since they're a different type of qualification on the type.
164  if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
165      BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
166    return Ty->getSizeInBits();
167
168  return getBaseTypeSize(BaseType);
169}
170
171static bool hasDebugInfo(const MachineModuleInfo *MMI,
172                         const MachineFunction *MF) {
173  if (!MMI->hasDebugInfo())
174    return false;
175  auto *SP = MF->getFunction().getSubprogram();
176  if (!SP)
177    return false;
178  assert(SP->getUnit());
179  auto EK = SP->getUnit()->getEmissionKind();
180  if (EK == DICompileUnit::NoDebug)
181    return false;
182  return true;
183}
184
185void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
186  PrevInstBB = nullptr;
187
188  if (!Asm || !hasDebugInfo(MMI, MF)) {
189    skippedNonDebugFunction();
190    return;
191  }
192
193  // Grab the lexical scopes for the function, if we don't have any of those
194  // then we're not going to be able to do anything.
195  LScopes.initialize(*MF);
196  if (LScopes.empty()) {
197    beginFunctionImpl(MF);
198    return;
199  }
200
201  // Make sure that each lexical scope will have a begin/end label.
202  identifyScopeMarkers();
203
204  // Calculate history for local variables.
205  assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
206  assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
207  calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
208                            DbgValues, DbgLabels);
209  LLVM_DEBUG(DbgValues.dump());
210
211  // Request labels for the full history.
212  for (const auto &I : DbgValues) {
213    const auto &Entries = I.second;
214    if (Entries.empty())
215      continue;
216
217    auto IsDescribedByReg = [](const MachineInstr *MI) {
218      return MI->getOperand(0).isReg() && MI->getOperand(0).getReg();
219    };
220
221    // The first mention of a function argument gets the CurrentFnBegin label,
222    // so arguments are visible when breaking at function entry.
223    //
224    // We do not change the label for values that are described by registers,
225    // as that could place them above their defining instructions. We should
226    // ideally not change the labels for constant debug values either, since
227    // doing that violates the ranges that are calculated in the history map.
228    // However, we currently do not emit debug values for constant arguments
229    // directly at the start of the function, so this code is still useful.
230    const DILocalVariable *DIVar =
231        Entries.front().getInstr()->getDebugVariable();
232    if (DIVar->isParameter() &&
233        getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
234      if (!IsDescribedByReg(Entries.front().getInstr()))
235        LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin();
236      if (Entries.front().getInstr()->getDebugExpression()->isFragment()) {
237        // Mark all non-overlapping initial fragments.
238        for (auto I = Entries.begin(); I != Entries.end(); ++I) {
239          if (!I->isDbgValue())
240            continue;
241          const DIExpression *Fragment = I->getInstr()->getDebugExpression();
242          if (std::any_of(Entries.begin(), I,
243                          [&](DbgValueHistoryMap::Entry Pred) {
244                            return Pred.isDbgValue() &&
245                                   Fragment->fragmentsOverlap(
246                                       Pred.getInstr()->getDebugExpression());
247                          }))
248            break;
249          // The code that generates location lists for DWARF assumes that the
250          // entries' start labels are monotonically increasing, and since we
251          // don't change the label for fragments that are described by
252          // registers, we must bail out when encountering such a fragment.
253          if (IsDescribedByReg(I->getInstr()))
254            break;
255          LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin();
256        }
257      }
258    }
259
260    for (const auto &Entry : Entries) {
261      if (Entry.isDbgValue())
262        requestLabelBeforeInsn(Entry.getInstr());
263      else
264        requestLabelAfterInsn(Entry.getInstr());
265    }
266  }
267
268  // Ensure there is a symbol before DBG_LABEL.
269  for (const auto &I : DbgLabels) {
270    const MachineInstr *MI = I.second;
271    requestLabelBeforeInsn(MI);
272  }
273
274  PrevInstLoc = DebugLoc();
275  PrevLabel = Asm->getFunctionBegin();
276  beginFunctionImpl(MF);
277}
278
279void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
280  if (!MMI->hasDebugInfo())
281    return;
282
283  assert(CurMI == nullptr);
284  CurMI = MI;
285
286  // Insert labels where requested.
287  DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
288      LabelsBeforeInsn.find(MI);
289
290  // No label needed.
291  if (I == LabelsBeforeInsn.end())
292    return;
293
294  // Label already assigned.
295  if (I->second)
296    return;
297
298  if (!PrevLabel) {
299    PrevLabel = MMI->getContext().createTempSymbol();
300    Asm->OutStreamer->EmitLabel(PrevLabel);
301  }
302  I->second = PrevLabel;
303}
304
305void DebugHandlerBase::endInstruction() {
306  if (!MMI->hasDebugInfo())
307    return;
308
309  assert(CurMI != nullptr);
310  // Don't create a new label after DBG_VALUE and other instructions that don't
311  // generate code.
312  if (!CurMI->isMetaInstruction()) {
313    PrevLabel = nullptr;
314    PrevInstBB = CurMI->getParent();
315  }
316
317  DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
318      LabelsAfterInsn.find(CurMI);
319  CurMI = nullptr;
320
321  // No label needed.
322  if (I == LabelsAfterInsn.end())
323    return;
324
325  // Label already assigned.
326  if (I->second)
327    return;
328
329  // We need a label after this instruction.
330  if (!PrevLabel) {
331    PrevLabel = MMI->getContext().createTempSymbol();
332    Asm->OutStreamer->EmitLabel(PrevLabel);
333  }
334  I->second = PrevLabel;
335}
336
337void DebugHandlerBase::endFunction(const MachineFunction *MF) {
338  if (hasDebugInfo(MMI, MF))
339    endFunctionImpl(MF);
340  DbgValues.clear();
341  DbgLabels.clear();
342  LabelsBeforeInsn.clear();
343  LabelsAfterInsn.clear();
344}
345