DwarfExpression.cpp 8.8 KB

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