LLVM  5.0.0
DwarfExpression.cpp
Go to the documentation of this file.
1 //===-- llvm/CodeGen/DwarfExpression.cpp - Dwarf Debug Framework ----------===//
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 // This file contains support for writing dwarf debug info into asm files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "DwarfExpression.h"
15 #include "DwarfDebug.h"
22 
23 using namespace llvm;
24 
25 void DwarfExpression::addReg(int DwarfReg, const char *Comment) {
26  assert(DwarfReg >= 0 && "invalid negative dwarf register number");
28  "location description already locked down");
30  if (DwarfReg < 32) {
31  emitOp(dwarf::DW_OP_reg0 + DwarfReg, Comment);
32  } else {
33  emitOp(dwarf::DW_OP_regx, Comment);
34  emitUnsigned(DwarfReg);
35  }
36 }
37 
38 void DwarfExpression::addBReg(int DwarfReg, int Offset) {
39  assert(DwarfReg >= 0 && "invalid negative dwarf register number");
40  assert(LocationKind != Register && "location description already locked down");
41  if (DwarfReg < 32) {
42  emitOp(dwarf::DW_OP_breg0 + DwarfReg);
43  } else {
44  emitOp(dwarf::DW_OP_bregx);
45  emitUnsigned(DwarfReg);
46  }
47  emitSigned(Offset);
48 }
49 
51  emitOp(dwarf::DW_OP_fbreg);
52  emitSigned(Offset);
53 }
54 
55 void DwarfExpression::addOpPiece(unsigned SizeInBits, unsigned OffsetInBits) {
56  if (!SizeInBits)
57  return;
58 
59  const unsigned SizeOfByte = 8;
60  if (OffsetInBits > 0 || SizeInBits % SizeOfByte) {
61  emitOp(dwarf::DW_OP_bit_piece);
62  emitUnsigned(SizeInBits);
63  emitUnsigned(OffsetInBits);
64  } else {
65  emitOp(dwarf::DW_OP_piece);
66  unsigned ByteSize = SizeInBits / SizeOfByte;
67  emitUnsigned(ByteSize);
68  }
69  this->OffsetInBits += SizeInBits;
70 }
71 
72 void DwarfExpression::addShr(unsigned ShiftBy) {
73  emitOp(dwarf::DW_OP_constu);
74  emitUnsigned(ShiftBy);
75  emitOp(dwarf::DW_OP_shr);
76 }
77 
78 void DwarfExpression::addAnd(unsigned Mask) {
79  emitOp(dwarf::DW_OP_constu);
80  emitUnsigned(Mask);
81  emitOp(dwarf::DW_OP_and);
82 }
83 
85  unsigned MachineReg, unsigned MaxSize) {
86  if (!TRI.isPhysicalRegister(MachineReg)) {
87  if (isFrameRegister(TRI, MachineReg)) {
88  DwarfRegs.push_back({-1, 0, nullptr});
89  return true;
90  }
91  return false;
92  }
93 
94  int Reg = TRI.getDwarfRegNum(MachineReg, false);
95 
96  // If this is a valid register number, emit it.
97  if (Reg >= 0) {
98  DwarfRegs.push_back({Reg, 0, nullptr});
99  return true;
100  }
101 
102  // Walk up the super-register chain until we find a valid number.
103  // For example, EAX on x86_64 is a 32-bit fragment of RAX with offset 0.
104  for (MCSuperRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
105  Reg = TRI.getDwarfRegNum(*SR, false);
106  if (Reg >= 0) {
107  unsigned Idx = TRI.getSubRegIndex(*SR, MachineReg);
108  unsigned Size = TRI.getSubRegIdxSize(Idx);
109  unsigned RegOffset = TRI.getSubRegIdxOffset(Idx);
110  DwarfRegs.push_back({Reg, 0, "super-register"});
111  // Use a DW_OP_bit_piece to describe the sub-register.
112  setSubRegisterPiece(Size, RegOffset);
113  return true;
114  }
115  }
116 
117  // Otherwise, attempt to find a covering set of sub-register numbers.
118  // For example, Q0 on ARM is a composition of D0+D1.
119  unsigned CurPos = 0;
120  // The size of the register in bits.
121  const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(MachineReg);
122  unsigned RegSize = TRI.getRegSizeInBits(*RC);
123  // Keep track of the bits in the register we already emitted, so we
124  // can avoid emitting redundant aliasing subregs.
125  SmallBitVector Coverage(RegSize, false);
126  for (MCSubRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
127  unsigned Idx = TRI.getSubRegIndex(MachineReg, *SR);
128  unsigned Size = TRI.getSubRegIdxSize(Idx);
129  unsigned Offset = TRI.getSubRegIdxOffset(Idx);
130  Reg = TRI.getDwarfRegNum(*SR, false);
131 
132  // Intersection between the bits we already emitted and the bits
133  // covered by this subregister.
134  SmallBitVector Intersection(RegSize, false);
135  Intersection.set(Offset, Offset + Size);
136  Intersection ^= Coverage;
137 
138  // If this sub-register has a DWARF number and we haven't covered
139  // its range, emit a DWARF piece for it.
140  if (Reg >= 0 && Intersection.any()) {
141  // Emit a piece for any gap in the coverage.
142  if (Offset > CurPos)
143  DwarfRegs.push_back({-1, Offset - CurPos, nullptr});
144  DwarfRegs.push_back(
145  {Reg, std::min<unsigned>(Size, MaxSize - Offset), "sub-register"});
146  if (Offset >= MaxSize)
147  break;
148 
149  // Mark it as emitted.
150  Coverage.set(Offset, Offset + Size);
151  CurPos = Offset + Size;
152  }
153  }
154 
155  return CurPos;
156 }
157 
159  if (DwarfVersion >= 4)
160  emitOp(dwarf::DW_OP_stack_value);
161 }
162 
166  emitOp(dwarf::DW_OP_consts);
167  emitSigned(Value);
168 }
169 
173  emitOp(dwarf::DW_OP_constu);
174  emitUnsigned(Value);
175 }
176 
180 
181  unsigned Size = Value.getBitWidth();
182  const uint64_t *Data = Value.getRawData();
183 
184  // Chop it up into 64-bit pieces, because that's the maximum that
185  // addUnsignedConstant takes.
186  unsigned Offset = 0;
187  while (Offset < Size) {
188  addUnsignedConstant(*Data++);
189  if (Offset == 0 && Size <= 64)
190  break;
191  addStackValue();
192  addOpPiece(std::min(Size - Offset, 64u), Offset);
193  Offset += 64;
194  }
195 }
196 
198  DIExpressionCursor &ExprCursor,
199  unsigned MachineReg,
200  unsigned FragmentOffsetInBits) {
201  auto Fragment = ExprCursor.getFragmentInfo();
202  if (!addMachineReg(TRI, MachineReg, Fragment ? Fragment->SizeInBits : ~1U)) {
204  return false;
205  }
206 
207  bool HasComplexExpression = false;
208  auto Op = ExprCursor.peek();
209  if (Op && Op->getOp() != dwarf::DW_OP_LLVM_fragment)
210  HasComplexExpression = true;
211 
212  // If the register can only be described by a complex expression (i.e.,
213  // multiple subregisters) it doesn't safely compose with another complex
214  // expression. For example, it is not possible to apply a DW_OP_deref
215  // operation to multiple DW_OP_pieces.
216  if (HasComplexExpression && DwarfRegs.size() > 1) {
217  DwarfRegs.clear();
219  return false;
220  }
221 
222  // Handle simple register locations.
223  if (LocationKind != Memory && !HasComplexExpression) {
224  for (auto &Reg : DwarfRegs) {
225  if (Reg.DwarfRegNo >= 0)
226  addReg(Reg.DwarfRegNo, Reg.Comment);
227  addOpPiece(Reg.Size);
228  }
229  DwarfRegs.clear();
230  return true;
231  }
232 
233  // Don't emit locations that cannot be expressed without DW_OP_stack_value.
234  if (DwarfVersion < 4)
235  if (std::any_of(ExprCursor.begin(), ExprCursor.end(),
236  [](DIExpression::ExprOperand Op) -> bool {
237  return Op.getOp() == dwarf::DW_OP_stack_value;
238  })) {
239  DwarfRegs.clear();
241  return false;
242  }
243 
244  assert(DwarfRegs.size() == 1);
245  auto Reg = DwarfRegs[0];
246  bool FBReg = isFrameRegister(TRI, MachineReg);
247  int SignedOffset = 0;
248  assert(Reg.Size == 0 && "subregister has same size as superregister");
249 
250  // Pattern-match combinations for which more efficient representations exist.
251  // [Reg, DW_OP_plus_uconst, Offset] --> [DW_OP_breg, Offset].
252  if (Op && (Op->getOp() == dwarf::DW_OP_plus_uconst)) {
253  SignedOffset = Op->getArg(0);
254  ExprCursor.take();
255  }
256 
257  // [Reg, DW_OP_constu, Offset, DW_OP_plus] --> [DW_OP_breg, Offset]
258  // [Reg, DW_OP_constu, Offset, DW_OP_minus] --> [DW_OP_breg,-Offset]
259  // If Reg is a subregister we need to mask it out before subtracting.
260  if (Op && Op->getOp() == dwarf::DW_OP_constu) {
261  auto N = ExprCursor.peekNext();
262  if (N && (N->getOp() == dwarf::DW_OP_plus ||
263  (N->getOp() == dwarf::DW_OP_minus && !SubRegisterSizeInBits))) {
264  int Offset = Op->getArg(0);
265  SignedOffset = (N->getOp() == dwarf::DW_OP_minus) ? -Offset : Offset;
266  ExprCursor.consume(2);
267  }
268  }
269 
270  if (FBReg)
271  addFBReg(SignedOffset);
272  else
273  addBReg(Reg.DwarfRegNo, SignedOffset);
274  DwarfRegs.clear();
275  return true;
276 }
277 
278 /// Assuming a well-formed expression, match "DW_OP_deref* DW_OP_LLVM_fragment?".
279 static bool isMemoryLocation(DIExpressionCursor ExprCursor) {
280  while (ExprCursor) {
281  auto Op = ExprCursor.take();
282  switch (Op->getOp()) {
283  case dwarf::DW_OP_deref:
285  break;
286  default:
287  return false;
288  }
289  }
290  return true;
291 }
292 
294  unsigned FragmentOffsetInBits) {
295  // If we need to mask out a subregister, do it now, unless the next
296  // operation would emit an OpPiece anyway.
297  auto N = ExprCursor.peek();
298  if (SubRegisterSizeInBits && N && (N->getOp() != dwarf::DW_OP_LLVM_fragment))
299  maskSubRegister();
300 
301  while (ExprCursor) {
302  auto Op = ExprCursor.take();
303  switch (Op->getOp()) {
305  unsigned SizeInBits = Op->getArg(1);
306  unsigned FragmentOffset = Op->getArg(0);
307  // The fragment offset must have already been adjusted by emitting an
308  // empty DW_OP_piece / DW_OP_bit_piece before we emitted the base
309  // location.
310  assert(OffsetInBits >= FragmentOffset && "fragment offset not added?");
311 
312  // If addMachineReg already emitted DW_OP_piece operations to represent
313  // a super-register by splicing together sub-registers, subtract the size
314  // of the pieces that was already emitted.
315  SizeInBits -= OffsetInBits - FragmentOffset;
316 
317  // If addMachineReg requested a DW_OP_bit_piece to stencil out a
318  // sub-register that is smaller than the current fragment's size, use it.
320  SizeInBits = std::min<unsigned>(SizeInBits, SubRegisterSizeInBits);
321 
322  // Emit a DW_OP_stack_value for implicit location descriptions.
323  if (LocationKind == Implicit)
324  addStackValue();
325 
326  // Emit the DW_OP_piece.
327  addOpPiece(SizeInBits, SubRegisterOffsetInBits);
328  setSubRegisterPiece(0, 0);
329  // Reset the location description kind.
331  return;
332  }
333  case dwarf::DW_OP_plus_uconst:
335  emitOp(dwarf::DW_OP_plus_uconst);
336  emitUnsigned(Op->getArg(0));
337  break;
338  case dwarf::DW_OP_plus:
339  case dwarf::DW_OP_minus:
340  emitOp(Op->getOp());
341  break;
342  case dwarf::DW_OP_deref: {
344  if (LocationKind != Memory && isMemoryLocation(ExprCursor))
345  // Turning this into a memory location description makes the deref
346  // implicit.
348  else
349  emitOp(dwarf::DW_OP_deref);
350  break;
351  }
352  case dwarf::DW_OP_constu:
354  emitOp(dwarf::DW_OP_constu);
355  emitUnsigned(Op->getArg(0));
356  break;
357  case dwarf::DW_OP_stack_value:
359  break;
360  case dwarf::DW_OP_swap:
362  emitOp(dwarf::DW_OP_swap);
363  break;
364  case dwarf::DW_OP_xderef:
366  emitOp(dwarf::DW_OP_xderef);
367  break;
368  default:
369  llvm_unreachable("unhandled opcode found in expression");
370  }
371  }
372 
373  if (LocationKind == Implicit)
374  // Turn this into an implicit location description.
375  addStackValue();
376 }
377 
378 /// add masking operations to stencil out a subregister.
380  assert(SubRegisterSizeInBits && "no subregister was registered");
381  if (SubRegisterOffsetInBits > 0)
383  uint64_t Mask = (1ULL << (uint64_t)SubRegisterSizeInBits) - 1ULL;
384  addAnd(Mask);
385 }
386 
387 
389  assert(DwarfRegs.size() == 0 && "dwarf registers not emitted");
390  // Emit any outstanding DW_OP_piece operations to mask out subregisters.
391  if (SubRegisterSizeInBits == 0)
392  return;
393  // Don't emit a DW_OP_piece for a subregister at offset 0.
394  if (SubRegisterOffsetInBits == 0)
395  return;
397 }
398 
400  if (!Expr || !Expr->isFragment())
401  return;
402 
403  uint64_t FragmentOffset = Expr->getFragmentInfo()->OffsetInBits;
404  assert(FragmentOffset >= OffsetInBits &&
405  "overlapping or duplicate fragments");
406  if (FragmentOffset > OffsetInBits)
407  addOpPiece(FragmentOffset - OffsetInBits);
408  OffsetInBits = FragmentOffset;
409 }
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
void addShr(unsigned ShiftBy)
Emit a shift-right dwarf operation.
DIExpression::expr_op_iterator begin() const
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
void addUnsignedConstant(uint64_t Value)
Emit an unsigned constant.
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
This class provides various memory handling functions that manipulate MemoryBlock instances...
Definition: Memory.h:46
Optional< DIExpression::ExprOperand > peek() const
Return the current operation.
unsigned getRegSizeInBits(const TargetRegisterClass &RC) const
Return the size in bits of a register from class RC.
unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const
For a given register pair, return the sub-register index if the second register is a sub-register of ...
MCSuperRegIterator enumerates all super-registers of Reg.
Reg
All possible values of the reg field in the ModR/M byte.
void addBReg(int DwarfReg, int Offset)
Emit a DW_OP_breg operation.
void addFBReg(int Offset)
Emit DW_OP_fbreg <Offset>.
Only used in LLVM metadata.
Definition: Dwarf.h:123
enum llvm::DwarfExpression::@277 LocationKind
The kind of location description being produced.
Holds a DIExpression and keeps track of how many operands have been consumed so far.
void addReg(int DwarfReg, const char *Comment=nullptr)
Emit a DW_OP_reg operation.
void setSubRegisterPiece(unsigned SizeInBits, unsigned OffsetInBits)
Push a DW_OP_piece / DW_OP_bit_piece for emitting later, if one is needed to represent a subregister...
static Optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
void maskSubRegister()
Add masking operations to stencil out a subregister.
bool isFragment() const
Return whether this is a piece of an aggregate variable.
virtual void emitUnsigned(uint64_t Value)=0
Emit a raw unsigned value.
Optional< DIExpression::ExprOperand > take()
Consume one operation.
Optional< DIExpression::FragmentInfo > getFragmentInfo() const
Retrieve the fragment information, if any.
virtual void emitOp(uint8_t Op, const char *Comment=nullptr)=0
Output a dwarf operand and an optional assembler comment.
Optional< DIExpression::ExprOperand > peekNext() const
Return the next operation.
A lightweight wrapper around an expression operand.
uint64_t OffsetInBits
Current Fragment Offset in Bits.
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:825
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1488
virtual void emitSigned(int64_t Value)=0
Emit a raw signed value.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
MCSubRegIterator enumerates all sub-registers of Reg.
SmallBitVector & set()
void addExpression(DIExpressionCursor &&Expr, unsigned FragmentOffsetInBits=0)
Emit all remaining operations in the DIExpressionCursor.
bool addMachineReg(const TargetRegisterInfo &TRI, unsigned MachineReg, unsigned MaxSize=~1U)
Emit a partial DWARF register operation.
void addStackValue()
Emit a DW_OP_stack_value, if supported.
const size_t N
DWARF expression.
Class for arbitrary precision integers.
Definition: APInt.h:69
void addAnd(unsigned Mask)
Emit a bitwise and dwarf operation.
const TargetRegisterClass * getMinimalPhysRegClass(unsigned Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
DIExpression::expr_op_iterator end() const
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
unsigned SubRegisterSizeInBits
Sometimes we need to add a DW_OP_bit_piece to describe a subregister.
void consume(unsigned N)
Consume N operations.
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:668
void finalize()
This needs to be called last to commit any pending changes.
unsigned getSubRegIdxOffset(unsigned Idx) const
Get the offset of the bit range covered by a sub-register index.
void addFragmentOffset(const DIExpression *Expr)
If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to the fragment described by Ex...
Holds information about all subregisters comprising a register location.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
bool any() const
Returns true if any bit is set.
void addOpPiece(unsigned SizeInBits, unsigned OffsetInBits=0)
Emit a DW_OP_piece or DW_OP_bit_piece operation for a variable fragment.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
static bool isMemoryLocation(DIExpressionCursor ExprCursor)
Assuming a well-formed expression, match "DW_OP_deref* DW_OP_LLVM_fragment?".
void addSignedConstant(int64_t Value)
Emit a signed constant.
SmallVector< Register, 2 > DwarfRegs
The register location, if any.
unsigned getSubRegIdxSize(unsigned Idx) const
Get the size of the bit range covered by a sub-register index.
bool addMachineRegExpression(const TargetRegisterInfo &TRI, DIExpressionCursor &Expr, unsigned MachineReg, unsigned FragmentOffsetInBits=0)
Emit a machine register location.
virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg)=0
Return whether the given machine register is the frame register in the current function.