DwarfExpression.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #include "DwarfExpression.h"
  14. #include "DwarfDebug.h"
  15. #include "llvm/ADT/SmallBitVector.h"
  16. #include "llvm/CodeGen/AsmPrinter.h"
  17. #include "llvm/Support/Dwarf.h"
  18. #include "llvm/Target/TargetMachine.h"
  19. #include "llvm/Target/TargetRegisterInfo.h"
  20. #include "llvm/Target/TargetSubtargetInfo.h"
  21. using namespace llvm;
  22. const TargetRegisterInfo *DwarfExpression::getTRI() const {
  23. return AP.TM.getSubtargetImpl()->getRegisterInfo();
  24. }
  25. unsigned DwarfExpression::getDwarfVersion() const {
  26. return AP.getDwarfDebug()->getDwarfVersion();
  27. }
  28. void DwarfExpression::AddReg(int DwarfReg, const char *Comment) {
  29. assert(DwarfReg >= 0 && "invalid negative dwarf register number");
  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. void DwarfExpression::AddRegIndirect(int DwarfReg, int Offset, bool Deref) {
  38. assert(DwarfReg >= 0 && "invalid negative dwarf register number");
  39. if (DwarfReg < 32) {
  40. EmitOp(dwarf::DW_OP_breg0 + DwarfReg);
  41. } else {
  42. EmitOp(dwarf::DW_OP_bregx);
  43. EmitUnsigned(DwarfReg);
  44. }
  45. EmitSigned(Offset);
  46. if (Deref)
  47. EmitOp(dwarf::DW_OP_deref);
  48. }
  49. void DwarfExpression::AddOpPiece(unsigned SizeInBits, unsigned OffsetInBits) {
  50. assert(SizeInBits > 0 && "piece has size zero");
  51. const unsigned SizeOfByte = 8;
  52. if (OffsetInBits > 0 || SizeInBits % SizeOfByte) {
  53. EmitOp(dwarf::DW_OP_bit_piece);
  54. EmitUnsigned(SizeInBits);
  55. EmitUnsigned(OffsetInBits);
  56. } else {
  57. EmitOp(dwarf::DW_OP_piece);
  58. unsigned ByteSize = SizeInBits / SizeOfByte;
  59. EmitUnsigned(ByteSize);
  60. }
  61. }
  62. void DwarfExpression::AddShr(unsigned ShiftBy) {
  63. EmitOp(dwarf::DW_OP_constu);
  64. EmitUnsigned(ShiftBy);
  65. EmitOp(dwarf::DW_OP_shr);
  66. }
  67. bool DwarfExpression::AddMachineRegIndirect(unsigned MachineReg, int Offset) {
  68. int DwarfReg = getTRI()->getDwarfRegNum(MachineReg, false);
  69. if (DwarfReg < 0)
  70. return false;
  71. if (isFrameRegister(MachineReg)) {
  72. // If variable offset is based in frame register then use fbreg.
  73. EmitOp(dwarf::DW_OP_fbreg);
  74. EmitSigned(Offset);
  75. } else {
  76. AddRegIndirect(DwarfReg, Offset);
  77. }
  78. return true;
  79. }
  80. bool DwarfExpression::AddMachineRegPiece(unsigned MachineReg,
  81. unsigned PieceSizeInBits,
  82. unsigned PieceOffsetInBits) {
  83. const TargetRegisterInfo *TRI = getTRI();
  84. int Reg = TRI->getDwarfRegNum(MachineReg, false);
  85. // If this is a valid register number, emit it.
  86. if (Reg >= 0) {
  87. AddReg(Reg);
  88. if (PieceSizeInBits)
  89. AddOpPiece(PieceSizeInBits, PieceOffsetInBits);
  90. return true;
  91. }
  92. // Walk up the super-register chain until we find a valid number.
  93. // For example, EAX on x86_64 is a 32-bit piece of RAX with offset 0.
  94. for (MCSuperRegIterator SR(MachineReg, TRI); SR.isValid(); ++SR) {
  95. Reg = TRI->getDwarfRegNum(*SR, false);
  96. if (Reg >= 0) {
  97. unsigned Idx = TRI->getSubRegIndex(*SR, MachineReg);
  98. unsigned Size = TRI->getSubRegIdxSize(Idx);
  99. unsigned RegOffset = TRI->getSubRegIdxOffset(Idx);
  100. AddReg(Reg, "super-register");
  101. if (PieceOffsetInBits == RegOffset) {
  102. AddOpPiece(Size, RegOffset);
  103. } else {
  104. // If this is part of a variable in a sub-register at a
  105. // non-zero offset, we need to manually shift the value into
  106. // place, since the DW_OP_piece describes the part of the
  107. // variable, not the position of the subregister.
  108. if (RegOffset)
  109. AddShr(RegOffset);
  110. AddOpPiece(Size, PieceOffsetInBits);
  111. }
  112. return true;
  113. }
  114. }
  115. // Otherwise, attempt to find a covering set of sub-register numbers.
  116. // For example, Q0 on ARM is a composition of D0+D1.
  117. //
  118. // Keep track of the current position so we can emit the more
  119. // efficient DW_OP_piece.
  120. unsigned CurPos = PieceOffsetInBits;
  121. // The size of the register in bits, assuming 8 bits per byte.
  122. unsigned RegSize = TRI->getMinimalPhysRegClass(MachineReg)->getSize() * 8;
  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. // Intersection between the bits we already emitted and the bits
  132. // covered by this subregister.
  133. SmallBitVector Intersection(RegSize, false);
  134. Intersection.set(Offset, Offset + Size);
  135. Intersection ^= Coverage;
  136. // If this sub-register has a DWARF number and we haven't covered
  137. // its range, emit a DWARF piece for it.
  138. if (Reg >= 0 && Intersection.any()) {
  139. AddReg(Reg, "sub-register");
  140. AddOpPiece(Size, Offset == CurPos ? 0 : Offset);
  141. CurPos = Offset + Size;
  142. // Mark it as emitted.
  143. Coverage.set(Offset, Offset + Size);
  144. }
  145. }
  146. return CurPos > PieceOffsetInBits;
  147. }
  148. void DwarfExpression::AddSignedConstant(int Value) {
  149. EmitOp(dwarf::DW_OP_consts);
  150. EmitSigned(Value);
  151. // The proper way to describe a constant value is
  152. // DW_OP_constu <const>, DW_OP_stack_value.
  153. // Unfortunately, DW_OP_stack_value was not available until DWARF-4,
  154. // so we will continue to generate DW_OP_constu <const> for DWARF-2
  155. // and DWARF-3. Technically, this is incorrect since DW_OP_const <const>
  156. // actually describes a value at a constant addess, not a constant value.
  157. // However, in the past there was no better way to describe a constant
  158. // value, so the producers and consumers started to rely on heuristics
  159. // to disambiguate the value vs. location status of the expression.
  160. // See PR21176 for more details.
  161. if (getDwarfVersion() >= 4)
  162. EmitOp(dwarf::DW_OP_stack_value);
  163. }
  164. void DwarfExpression::AddUnsignedConstant(unsigned Value) {
  165. EmitOp(dwarf::DW_OP_constu);
  166. EmitUnsigned(Value);
  167. // cf. comment in DwarfExpression::AddSignedConstant().
  168. if (getDwarfVersion() >= 4)
  169. EmitOp(dwarf::DW_OP_stack_value);
  170. }
  171. static unsigned getOffsetOrZero(unsigned OffsetInBits,
  172. unsigned PieceOffsetInBits) {
  173. if (OffsetInBits == PieceOffsetInBits)
  174. return 0;
  175. assert(OffsetInBits >= PieceOffsetInBits && "overlapping pieces");
  176. return OffsetInBits;
  177. }
  178. bool DwarfExpression::AddMachineRegExpression(DIExpression Expr,
  179. unsigned MachineReg,
  180. unsigned PieceOffsetInBits) {
  181. unsigned N = Expr.getNumElements();
  182. unsigned I = 0;
  183. bool ValidReg = false;
  184. // Pattern-match combinations for which more efficient representations exist
  185. // first.
  186. if (N >= 3 && Expr.getElement(0) == dwarf::DW_OP_piece) {
  187. unsigned SizeOfByte = 8;
  188. unsigned OffsetInBits = Expr.getElement(1) * SizeOfByte;
  189. unsigned SizeInBits = Expr.getElement(2) * SizeOfByte;
  190. ValidReg =
  191. AddMachineRegPiece(MachineReg, SizeInBits,
  192. getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
  193. I = 3;
  194. } else if (N >= 3 && Expr.getElement(0) == dwarf::DW_OP_plus &&
  195. Expr.getElement(2) == dwarf::DW_OP_deref) {
  196. // [DW_OP_reg,Offset,DW_OP_plus,DW_OP_deref] --> [DW_OP_breg,Offset].
  197. unsigned Offset = Expr.getElement(1);
  198. ValidReg = AddMachineRegIndirect(MachineReg, Offset);
  199. I = 3;
  200. } else if (N >= 1 && Expr.getElement(0) == dwarf::DW_OP_deref) {
  201. // [DW_OP_reg,DW_OP_deref] --> [DW_OP_breg].
  202. ValidReg = AddMachineRegIndirect(MachineReg);
  203. I = 1;
  204. } else
  205. ValidReg = AddMachineRegPiece(MachineReg);
  206. if (!ValidReg)
  207. return false;
  208. // Emit remaining elements of the expression.
  209. AddExpression(Expr, I);
  210. return true;
  211. }
  212. void DwarfExpression::AddExpression(DIExpression Expr, unsigned I,
  213. unsigned PieceOffsetInBits) {
  214. unsigned N = Expr.getNumElements();
  215. for (; I < N; ++I) {
  216. switch (Expr.getElement(I)) {
  217. case dwarf::DW_OP_piece: {
  218. unsigned SizeOfByte = 8;
  219. unsigned OffsetInBits = Expr.getElement(++I) * SizeOfByte;
  220. unsigned SizeInBits = Expr.getElement(++I) * SizeOfByte;
  221. AddOpPiece(SizeInBits, getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
  222. break;
  223. }
  224. case dwarf::DW_OP_plus:
  225. EmitOp(dwarf::DW_OP_plus_uconst);
  226. EmitUnsigned(Expr.getElement(++I));
  227. break;
  228. case dwarf::DW_OP_deref:
  229. EmitOp(dwarf::DW_OP_deref);
  230. break;
  231. default:
  232. llvm_unreachable("unhandled opcode found in DIExpression");
  233. }
  234. }
  235. }