LLVM  6.0.0svn
DebugHandlerBase.cpp
Go to the documentation of this file.
1 //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Common functionality for different debug information format backends.
11 // LLVM currently supports DWARF and CodeView.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "DebugHandlerBase.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/Twine.h"
23 #include "llvm/IR/DebugInfo.h"
24 #include "llvm/MC/MCStreamer.h"
25 
26 using namespace llvm;
27 
30  const MachineInstr &Instruction) {
31  DbgVariableLocation Location;
32  if (!Instruction.isDebugValue())
33  return None;
34  if (!Instruction.getOperand(0).isReg())
35  return None;
36  Location.Register = Instruction.getOperand(0).getReg();
37  Location.FragmentInfo.reset();
38  // We only handle expressions generated by DIExpression::appendOffset,
39  // which doesn't require a full stack machine.
40  int64_t Offset = 0;
41  const DIExpression *DIExpr = Instruction.getDebugExpression();
42  auto Op = DIExpr->expr_op_begin();
43  while (Op != DIExpr->expr_op_end()) {
44  switch (Op->getOp()) {
45  case dwarf::DW_OP_constu: {
46  int Value = Op->getArg(0);
47  ++Op;
48  if (Op != DIExpr->expr_op_end()) {
49  switch (Op->getOp()) {
50  case dwarf::DW_OP_minus:
51  Offset -= Value;
52  break;
53  case dwarf::DW_OP_plus:
54  Offset += Value;
55  break;
56  default:
57  continue;
58  }
59  }
60  } break;
61  case dwarf::DW_OP_plus_uconst:
62  Offset += Op->getArg(0);
63  break;
65  Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
66  break;
67  case dwarf::DW_OP_deref:
68  Location.LoadChain.push_back(Offset);
69  Offset = 0;
70  break;
71  default:
72  return None;
73  }
74  ++Op;
75  }
76 
77  // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
78  // instruction.
79  // FIXME: Replace these with DIExpression.
80  if (Instruction.isIndirectDebugValue())
81  Location.LoadChain.push_back(Offset);
82 
83  return Location;
84 }
85 
87 
88 // Each LexicalScope has first instruction and last instruction to mark
89 // beginning and end of a scope respectively. Create an inverse map that list
90 // scopes starts (and ends) with an instruction. One instruction may start (or
91 // end) multiple scopes. Ignore scopes that are not reachable.
95  while (!WorkList.empty()) {
96  LexicalScope *S = WorkList.pop_back_val();
97 
98  const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
99  if (!Children.empty())
100  WorkList.append(Children.begin(), Children.end());
101 
102  if (S->isAbstractScope())
103  continue;
104 
105  for (const InsnRange &R : S->getRanges()) {
106  assert(R.first && "InsnRange does not have first instruction!");
107  assert(R.second && "InsnRange does not have second instruction!");
108  requestLabelBeforeInsn(R.first);
109  requestLabelAfterInsn(R.second);
110  }
111  }
112 }
113 
114 // Return Label preceding the instruction.
116  MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
117  assert(Label && "Didn't insert label before instruction");
118  return Label;
119 }
120 
121 // Return Label immediately following the instruction.
123  return LabelsAfterInsn.lookup(MI);
124 }
125 
127  const DIExpression *P2) {
128  auto Fragment1 = *P1->getFragmentInfo();
129  auto Fragment2 = *P2->getFragmentInfo();
130  unsigned l1 = Fragment1.OffsetInBits;
131  unsigned l2 = Fragment2.OffsetInBits;
132  unsigned r1 = l1 + Fragment1.SizeInBits;
133  unsigned r2 = l2 + Fragment2.SizeInBits;
134  if (r1 <= l2)
135  return -1;
136  else if (r2 <= l1)
137  return 1;
138  else
139  return 0;
140 }
141 
143  const DIExpression *P2) {
144  if (!P1->isFragment() || !P2->isFragment())
145  return true;
146  return fragmentCmp(P1, P2) == 0;
147 }
148 
149 /// If this type is derived from a base type then return base type size.
151  DIType *Ty = TyRef.resolve();
152  assert(Ty);
153  DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
154  if (!DDTy)
155  return Ty->getSizeInBits();
156 
157  unsigned Tag = DDTy->getTag();
158 
159  if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
160  Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
161  Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
162  return DDTy->getSizeInBits();
163 
164  DIType *BaseType = DDTy->getBaseType().resolve();
165 
166  assert(BaseType && "Unexpected invalid base type");
167 
168  // If this is a derived type, go ahead and get the base type, unless it's a
169  // reference then it's just the size of the field. Pointer types have no need
170  // of this since they're a different type of qualification on the type.
171  if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
172  BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
173  return Ty->getSizeInBits();
174 
175  return getBaseTypeSize(BaseType);
176 }
177 
178 static bool hasDebugInfo(const MachineModuleInfo *MMI,
179  const MachineFunction *MF) {
180  if (!MMI->hasDebugInfo())
181  return false;
182  auto *SP = MF->getFunction()->getSubprogram();
183  if (!SP)
184  return false;
185  assert(SP->getUnit());
186  auto EK = SP->getUnit()->getEmissionKind();
187  if (EK == DICompileUnit::NoDebug)
188  return false;
189  return true;
190 }
191 
193  PrevInstBB = nullptr;
194 
195  if (!Asm || !hasDebugInfo(MMI, MF)) {
197  return;
198  }
199 
200  // Grab the lexical scopes for the function, if we don't have any of those
201  // then we're not going to be able to do anything.
202  LScopes.initialize(*MF);
203  if (LScopes.empty()) {
204  beginFunctionImpl(MF);
205  return;
206  }
207 
208  // Make sure that each lexical scope will have a begin/end label.
210 
211  // Calculate history for local variables.
212  assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
214  DbgValues);
215 
216  // Request labels for the full history.
217  for (const auto &I : DbgValues) {
218  const auto &Ranges = I.second;
219  if (Ranges.empty())
220  continue;
221 
222  // The first mention of a function argument gets the CurrentFnBegin
223  // label, so arguments are visible when breaking at function entry.
224  const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
225  if (DIVar->isParameter() &&
226  getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) {
227  LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
228  if (Ranges.front().first->getDebugExpression()->isFragment()) {
229  // Mark all non-overlapping initial fragments.
230  for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
231  const DIExpression *Fragment = I->first->getDebugExpression();
232  if (std::all_of(Ranges.begin(), I,
234  return !fragmentsOverlap(
235  Fragment, Pred.first->getDebugExpression());
236  }))
237  LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
238  else
239  break;
240  }
241  }
242  }
243 
244  for (const auto &Range : Ranges) {
245  requestLabelBeforeInsn(Range.first);
246  if (Range.second)
247  requestLabelAfterInsn(Range.second);
248  }
249  }
250 
251  PrevInstLoc = DebugLoc();
253  beginFunctionImpl(MF);
254 }
255 
257  if (!MMI->hasDebugInfo())
258  return;
259 
260  assert(CurMI == nullptr);
261  CurMI = MI;
262 
263  // Insert labels where requested.
265  LabelsBeforeInsn.find(MI);
266 
267  // No label needed.
268  if (I == LabelsBeforeInsn.end())
269  return;
270 
271  // Label already assigned.
272  if (I->second)
273  return;
274 
275  if (!PrevLabel) {
277  Asm->OutStreamer->EmitLabel(PrevLabel);
278  }
279  I->second = PrevLabel;
280 }
281 
283  if (!MMI->hasDebugInfo())
284  return;
285 
286  assert(CurMI != nullptr);
287  // Don't create a new label after DBG_VALUE and other instructions that don't
288  // generate code.
289  if (!CurMI->isMetaInstruction()) {
290  PrevLabel = nullptr;
292  }
293 
295  LabelsAfterInsn.find(CurMI);
296  CurMI = nullptr;
297 
298  // No label needed.
299  if (I == LabelsAfterInsn.end())
300  return;
301 
302  // Label already assigned.
303  if (I->second)
304  return;
305 
306  // We need a label after this instruction.
307  if (!PrevLabel) {
309  Asm->OutStreamer->EmitLabel(PrevLabel);
310  }
311  I->second = PrevLabel;
312 }
313 
315  if (hasDebugInfo(MMI, MF))
316  endFunctionImpl(MF);
317  DbgValues.clear();
318  LabelsBeforeInsn.clear();
319  LabelsAfterInsn.clear();
320 }
void push_back(const T &Elt)
Definition: SmallVector.h:212
void calculateDbgValueHistory(const MachineFunction *MF, const TargetRegisterInfo *TRI, DbgValueHistoryMap &Result)
static void r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition: SHA1.cpp:55
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:92
bool hasDebugInfo() const
Returns true if valid debug info is present.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
bool isAbstractScope() const
Definition: LexicalScopes.h:65
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
unsigned getReg() const
getReg - Returns the register number.
const MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:95
SmallVectorImpl< InsnRange > & getRanges()
Definition: LexicalScopes.h:67
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:813
A debug info location.
Definition: DebugLoc.h:34
bool isMetaInstruction() const
Return true if this instruction doesn&#39;t produce any output in the form of executable instructions...
Definition: MachineInstr.h:883
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:45
const MachineInstr * CurMI
If nonnull, stores the current machine instruction we&#39;re processing.
DebugLoc PrevInstLoc
Previous instruction&#39;s location information.
SmallVectorImpl< LexicalScope * > & getChildren()
Definition: LexicalScopes.h:66
DbgValueHistoryMap DbgValues
History of DBG_VALUE and clobber instructions for each user variable.
unsigned getTag() const
expr_op_iterator expr_op_begin() const
Visit the elements via ExprOperand wrappers.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
void beginFunction(const MachineFunction *MF) override
Gather pre-function debug information.
Only used in LLVM metadata.
Definition: Dwarf.h:125
uint64_t getSizeInBits() const
Holds a subclass of DINode.
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:45
DebugHandlerBase(AsmPrinter *A)
expr_op_iterator expr_op_end() const
MCSymbol * getFunctionBegin() const
Definition: AsmPrinter.h:194
void identifyScopeMarkers()
Indentify instructions that are marking the beginning of or ending of a scope.
void initialize(const MachineFunction &)
initialize - Scan machine function and constuct lexical scope nest, resets the instance if necessary...
const MCContext & getContext() const
static Optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
virtual void endFunctionImpl(const MachineFunction *MF)=0
DenseMap< const MachineInstr *, MCSymbol * > LabelsAfterInsn
Maps instruction with label emitted after instruction.
void requestLabelBeforeInsn(const MachineInstr *MI)
Ensure that a label will be emitted before MI.
void resolve()
Resolve a unique, unresolved node.
Definition: Metadata.cpp:568
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:215
static bool fragmentsOverlap(const DIExpression *P1, const DIExpression *P2)
Determine whether two variable fragments overlap.
const MachineBasicBlock * PrevInstBB
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1497
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:116
AsmPrinter * Asm
Target of debug info emission.
llvm::Optional< llvm::DIExpression::FragmentInfo > FragmentInfo
Present if the location is part of a larger variable.
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:77
DenseMap< const MachineInstr *, MCSymbol * > LabelsBeforeInsn
Maps instruction with label emitted before instruction.
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
void endFunction(const MachineFunction *MF) override
Gather post-function debug information.
static void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition: SHA1.cpp:49
MCSymbol * getLabelAfterInsn(const MachineInstr *MI)
Return Label immediately following the instruction.
const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
virtual void skippedNonDebugFunction()
Represents the location at which a variable is stored.
virtual void beginFunctionImpl(const MachineFunction *MF)=0
static Optional< DbgVariableLocation > extractFromMachineInstruction(const MachineInstr &Instruction)
Extract a VariableLocation from a MachineInstr.
Base class for types.
static bool hasDebugInfo(const MachineModuleInfo *MMI, const MachineFunction *MF)
bool isDebugValue() const
Definition: MachineInstr.h:816
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:385
DWARF expression.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:398
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:139
void requestLabelAfterInsn(const MachineInstr *MI)
Ensure that a label will be emitted after MI.
Representation of each machine instruction.
Definition: MachineInstr.h:59
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:120
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:61
#define I(x, y, z)
Definition: MD5.cpp:58
std::pair< const MachineInstr *, const MachineInstr * > InstrRange
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
void endInstruction() override
Process end of an instruction.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
void reset()
Definition: Optional.h:112
MCSymbol * getLabelBeforeInsn(const MachineInstr *MI)
Return Label preceding the instruction.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallVector< int64_t, 1 > LoadChain
Chain of offsetted loads necessary to load the value if it lives in memory.
static int fragmentCmp(const DIExpression *P1, const DIExpression *P2)
Determine the relative position of the fragments described by P1 and P2.
LLVM Value Representation.
Definition: Value.h:73
MachineModuleInfo * MMI
Collected machine module information.
static uint64_t getBaseTypeSize(const DITypeRef TyRef)
If this type is derived from a base type then return base type size.
IRTranslator LLVM IR MI
unsigned Register
Base register.
DILocalScope * getScope() const
Get the local scope for this variable.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:295
bool isFragment() const
Return whether this is a piece of an aggregate variable.
bool isIndirectDebugValue() const
A DBG_VALUE is indirect iff the first operand is a register and the second operand is an immediate...
Definition: MachineInstr.h:820
std::pair< const MachineInstr *, const MachineInstr * > InsnRange
InsnRange - This is used to track range of instructions with identical lexical scope.
Definition: LexicalScopes.h:40
bool empty()
empty - Return true if there is any lexical scope information available.
LexicalScope * getCurrentFunctionScope() const
getCurrentFunctionScope - Return lexical scope for the current function.
This class contains meta information specific to a module.