DwarfExpression.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. //===- llvm/CodeGen/DwarfExpression.cpp - Dwarf Debug Framework -----------===//
  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. // This file contains support for writing dwarf debug info into asm files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "DwarfExpression.h"
  13. #include "DwarfCompileUnit.h"
  14. #include "llvm/ADT/APInt.h"
  15. #include "llvm/ADT/SmallBitVector.h"
  16. #include "llvm/BinaryFormat/Dwarf.h"
  17. #include "llvm/CodeGen/TargetRegisterInfo.h"
  18. #include "llvm/IR/DebugInfoMetadata.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include <algorithm>
  21. #include <cassert>
  22. #include <cstdint>
  23. using namespace llvm;
  24. void DwarfExpression::emitConstu(uint64_t Value) {
  25. if (Value < 32)
  26. emitOp(dwarf::DW_OP_lit0 + Value);
  27. else if (Value == std::numeric_limits<uint64_t>::max()) {
  28. // Only do this for 64-bit values as the DWARF expression stack uses
  29. // target-address-size values.
  30. emitOp(dwarf::DW_OP_lit0);
  31. emitOp(dwarf::DW_OP_not);
  32. } else {
  33. emitOp(dwarf::DW_OP_constu);
  34. emitUnsigned(Value);
  35. }
  36. }
  37. void DwarfExpression::addReg(int DwarfReg, const char *Comment) {
  38. assert(DwarfReg >= 0 && "invalid negative dwarf register number");
  39. assert((isUnknownLocation() || isRegisterLocation()) &&
  40. "location description already locked down");
  41. LocationKind = Register;
  42. if (DwarfReg < 32) {
  43. emitOp(dwarf::DW_OP_reg0 + DwarfReg, Comment);
  44. } else {
  45. emitOp(dwarf::DW_OP_regx, Comment);
  46. emitUnsigned(DwarfReg);
  47. }
  48. }
  49. void DwarfExpression::addBReg(int DwarfReg, int Offset) {
  50. assert(DwarfReg >= 0 && "invalid negative dwarf register number");
  51. assert(!isRegisterLocation() && "location description already locked down");
  52. if (DwarfReg < 32) {
  53. emitOp(dwarf::DW_OP_breg0 + DwarfReg);
  54. } else {
  55. emitOp(dwarf::DW_OP_bregx);
  56. emitUnsigned(DwarfReg);
  57. }
  58. emitSigned(Offset);
  59. }
  60. void DwarfExpression::addFBReg(int Offset) {
  61. emitOp(dwarf::DW_OP_fbreg);
  62. emitSigned(Offset);
  63. }
  64. void DwarfExpression::addOpPiece(unsigned SizeInBits, unsigned OffsetInBits) {
  65. if (!SizeInBits)
  66. return;
  67. const unsigned SizeOfByte = 8;
  68. if (OffsetInBits > 0 || SizeInBits % SizeOfByte) {
  69. emitOp(dwarf::DW_OP_bit_piece);
  70. emitUnsigned(SizeInBits);
  71. emitUnsigned(OffsetInBits);
  72. } else {
  73. emitOp(dwarf::DW_OP_piece);
  74. unsigned ByteSize = SizeInBits / SizeOfByte;
  75. emitUnsigned(ByteSize);
  76. }
  77. this->OffsetInBits += SizeInBits;
  78. }
  79. void DwarfExpression::addShr(unsigned ShiftBy) {
  80. emitConstu(ShiftBy);
  81. emitOp(dwarf::DW_OP_shr);
  82. }
  83. void DwarfExpression::addAnd(unsigned Mask) {
  84. emitConstu(Mask);
  85. emitOp(dwarf::DW_OP_and);
  86. }
  87. bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
  88. unsigned MachineReg, unsigned MaxSize) {
  89. if (!TRI.isPhysicalRegister(MachineReg)) {
  90. if (isFrameRegister(TRI, MachineReg)) {
  91. DwarfRegs.push_back({-1, 0, nullptr});
  92. return true;
  93. }
  94. return false;
  95. }
  96. int Reg = TRI.getDwarfRegNum(MachineReg, false);
  97. // If this is a valid register number, emit it.
  98. if (Reg >= 0) {
  99. DwarfRegs.push_back({Reg, 0, nullptr});
  100. return true;
  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. // Otherwise, attempt to find a covering set of sub-register numbers.
  117. // For example, Q0 on ARM is a composition of D0+D1.
  118. unsigned CurPos = 0;
  119. // The size of the register in bits.
  120. const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(MachineReg);
  121. unsigned RegSize = TRI.getRegSizeInBits(*RC);
  122. // Keep track of the bits in the register we already emitted, so we
  123. // can avoid emitting redundant aliasing subregs. Because this is
  124. // just doing a greedy scan of all subregisters, it is possible that
  125. // this doesn't find a combination of subregisters that fully cover
  126. // the register (even though one may exist).
  127. SmallBitVector Coverage(RegSize, false);
  128. for (MCSubRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
  129. unsigned Idx = TRI.getSubRegIndex(MachineReg, *SR);
  130. unsigned Size = TRI.getSubRegIdxSize(Idx);
  131. unsigned Offset = TRI.getSubRegIdxOffset(Idx);
  132. Reg = TRI.getDwarfRegNum(*SR, false);
  133. if (Reg < 0)
  134. continue;
  135. // Intersection between the bits we already emitted and the bits
  136. // covered by this subregister.
  137. SmallBitVector CurSubReg(RegSize, false);
  138. CurSubReg.set(Offset, Offset + Size);
  139. // If this sub-register has a DWARF number and we haven't covered
  140. // its range, emit a DWARF piece for it.
  141. if (CurSubReg.test(Coverage)) {
  142. // Emit a piece for any gap in the coverage.
  143. if (Offset > CurPos)
  144. DwarfRegs.push_back({-1, Offset - CurPos, "no DWARF register encoding"});
  145. DwarfRegs.push_back(
  146. {Reg, std::min<unsigned>(Size, MaxSize - Offset), "sub-register"});
  147. if (Offset >= MaxSize)
  148. break;
  149. // Mark it as emitted.
  150. Coverage.set(Offset, Offset + Size);
  151. CurPos = Offset + Size;
  152. }
  153. }
  154. // Failed to find any DWARF encoding.
  155. if (CurPos == 0)
  156. return false;
  157. // Found a partial or complete DWARF encoding.
  158. if (CurPos < RegSize)
  159. DwarfRegs.push_back({-1, RegSize - CurPos, "no DWARF register encoding"});
  160. return true;
  161. }
  162. void DwarfExpression::addStackValue() {
  163. if (DwarfVersion >= 4)
  164. emitOp(dwarf::DW_OP_stack_value);
  165. }
  166. void DwarfExpression::addSignedConstant(int64_t Value) {
  167. assert(isImplicitLocation() || isUnknownLocation());
  168. LocationKind = Implicit;
  169. emitOp(dwarf::DW_OP_consts);
  170. emitSigned(Value);
  171. }
  172. void DwarfExpression::addUnsignedConstant(uint64_t Value) {
  173. assert(isImplicitLocation() || isUnknownLocation());
  174. LocationKind = Implicit;
  175. emitConstu(Value);
  176. }
  177. void DwarfExpression::addUnsignedConstant(const APInt &Value) {
  178. assert(isImplicitLocation() || isUnknownLocation());
  179. LocationKind = Implicit;
  180. unsigned Size = Value.getBitWidth();
  181. const uint64_t *Data = Value.getRawData();
  182. // Chop it up into 64-bit pieces, because that's the maximum that
  183. // addUnsignedConstant takes.
  184. unsigned Offset = 0;
  185. while (Offset < Size) {
  186. addUnsignedConstant(*Data++);
  187. if (Offset == 0 && Size <= 64)
  188. break;
  189. addStackValue();
  190. addOpPiece(std::min(Size - Offset, 64u), Offset);
  191. Offset += 64;
  192. }
  193. }
  194. bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI,
  195. DIExpressionCursor &ExprCursor,
  196. unsigned MachineReg,
  197. unsigned FragmentOffsetInBits) {
  198. auto Fragment = ExprCursor.getFragmentInfo();
  199. if (!addMachineReg(TRI, MachineReg, Fragment ? Fragment->SizeInBits : ~1U)) {
  200. LocationKind = Unknown;
  201. return false;
  202. }
  203. bool HasComplexExpression = false;
  204. auto Op = ExprCursor.peek();
  205. if (Op && Op->getOp() != dwarf::DW_OP_LLVM_fragment)
  206. HasComplexExpression = true;
  207. // If the register can only be described by a complex expression (i.e.,
  208. // multiple subregisters) it doesn't safely compose with another complex
  209. // expression. For example, it is not possible to apply a DW_OP_deref
  210. // operation to multiple DW_OP_pieces.
  211. if (HasComplexExpression && DwarfRegs.size() > 1) {
  212. DwarfRegs.clear();
  213. LocationKind = Unknown;
  214. return false;
  215. }
  216. // Handle simple register locations.
  217. if (!isMemoryLocation() && !HasComplexExpression) {
  218. for (auto &Reg : DwarfRegs) {
  219. if (Reg.DwarfRegNo >= 0)
  220. addReg(Reg.DwarfRegNo, Reg.Comment);
  221. addOpPiece(Reg.Size);
  222. }
  223. if (isEntryValue() && DwarfVersion >= 4)
  224. emitOp(dwarf::DW_OP_stack_value);
  225. DwarfRegs.clear();
  226. return true;
  227. }
  228. // Don't emit locations that cannot be expressed without DW_OP_stack_value.
  229. if (DwarfVersion < 4)
  230. if (any_of(ExprCursor, [](DIExpression::ExprOperand Op) -> bool {
  231. return Op.getOp() == dwarf::DW_OP_stack_value;
  232. })) {
  233. DwarfRegs.clear();
  234. LocationKind = Unknown;
  235. return false;
  236. }
  237. assert(DwarfRegs.size() == 1);
  238. auto Reg = DwarfRegs[0];
  239. bool FBReg = isFrameRegister(TRI, MachineReg);
  240. int SignedOffset = 0;
  241. assert(Reg.Size == 0 && "subregister has same size as superregister");
  242. // Pattern-match combinations for which more efficient representations exist.
  243. // [Reg, DW_OP_plus_uconst, Offset] --> [DW_OP_breg, Offset].
  244. if (Op && (Op->getOp() == dwarf::DW_OP_plus_uconst)) {
  245. SignedOffset = Op->getArg(0);
  246. ExprCursor.take();
  247. }
  248. // [Reg, DW_OP_constu, Offset, DW_OP_plus] --> [DW_OP_breg, Offset]
  249. // [Reg, DW_OP_constu, Offset, DW_OP_minus] --> [DW_OP_breg,-Offset]
  250. // If Reg is a subregister we need to mask it out before subtracting.
  251. if (Op && Op->getOp() == dwarf::DW_OP_constu) {
  252. auto N = ExprCursor.peekNext();
  253. if (N && (N->getOp() == dwarf::DW_OP_plus ||
  254. (N->getOp() == dwarf::DW_OP_minus && !SubRegisterSizeInBits))) {
  255. int Offset = Op->getArg(0);
  256. SignedOffset = (N->getOp() == dwarf::DW_OP_minus) ? -Offset : Offset;
  257. ExprCursor.consume(2);
  258. }
  259. }
  260. if (FBReg)
  261. addFBReg(SignedOffset);
  262. else
  263. addBReg(Reg.DwarfRegNo, SignedOffset);
  264. DwarfRegs.clear();
  265. return true;
  266. }
  267. void DwarfExpression::addEntryValueExpression(DIExpressionCursor &ExprCursor) {
  268. auto Op = ExprCursor.take();
  269. assert(Op && Op->getOp() == dwarf::DW_OP_entry_value);
  270. assert(!isMemoryLocation() &&
  271. "We don't support entry values of memory locations yet");
  272. if (DwarfVersion >= 5)
  273. emitOp(dwarf::DW_OP_entry_value);
  274. else
  275. emitOp(dwarf::DW_OP_GNU_entry_value);
  276. emitUnsigned(Op->getArg(0));
  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:
  284. case dwarf::DW_OP_LLVM_fragment:
  285. break;
  286. default:
  287. return false;
  288. }
  289. }
  290. return true;
  291. }
  292. void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor,
  293. unsigned FragmentOffsetInBits) {
  294. // If we need to mask out a subregister, do it now, unless the next
  295. // operation would emit an OpPiece anyway.
  296. auto N = ExprCursor.peek();
  297. if (SubRegisterSizeInBits && N && (N->getOp() != dwarf::DW_OP_LLVM_fragment))
  298. maskSubRegister();
  299. Optional<DIExpression::ExprOperand> PrevConvertOp = None;
  300. while (ExprCursor) {
  301. auto Op = ExprCursor.take();
  302. switch (Op->getOp()) {
  303. case dwarf::DW_OP_LLVM_fragment: {
  304. unsigned SizeInBits = Op->getArg(1);
  305. unsigned FragmentOffset = Op->getArg(0);
  306. // The fragment offset must have already been adjusted by emitting an
  307. // empty DW_OP_piece / DW_OP_bit_piece before we emitted the base
  308. // location.
  309. assert(OffsetInBits >= FragmentOffset && "fragment offset not added?");
  310. // If addMachineReg already emitted DW_OP_piece operations to represent
  311. // a super-register by splicing together sub-registers, subtract the size
  312. // of the pieces that was already emitted.
  313. SizeInBits -= OffsetInBits - FragmentOffset;
  314. // If addMachineReg requested a DW_OP_bit_piece to stencil out a
  315. // sub-register that is smaller than the current fragment's size, use it.
  316. if (SubRegisterSizeInBits)
  317. SizeInBits = std::min<unsigned>(SizeInBits, SubRegisterSizeInBits);
  318. // Emit a DW_OP_stack_value for implicit location descriptions.
  319. if (isImplicitLocation())
  320. addStackValue();
  321. // Emit the DW_OP_piece.
  322. addOpPiece(SizeInBits, SubRegisterOffsetInBits);
  323. setSubRegisterPiece(0, 0);
  324. // Reset the location description kind.
  325. LocationKind = Unknown;
  326. return;
  327. }
  328. case dwarf::DW_OP_plus_uconst:
  329. assert(!isRegisterLocation());
  330. emitOp(dwarf::DW_OP_plus_uconst);
  331. emitUnsigned(Op->getArg(0));
  332. break;
  333. case dwarf::DW_OP_plus:
  334. case dwarf::DW_OP_minus:
  335. case dwarf::DW_OP_mul:
  336. case dwarf::DW_OP_div:
  337. case dwarf::DW_OP_mod:
  338. case dwarf::DW_OP_or:
  339. case dwarf::DW_OP_and:
  340. case dwarf::DW_OP_xor:
  341. case dwarf::DW_OP_shl:
  342. case dwarf::DW_OP_shr:
  343. case dwarf::DW_OP_shra:
  344. case dwarf::DW_OP_lit0:
  345. case dwarf::DW_OP_not:
  346. case dwarf::DW_OP_dup:
  347. emitOp(Op->getOp());
  348. break;
  349. case dwarf::DW_OP_deref:
  350. assert(!isRegisterLocation());
  351. if (!isMemoryLocation() && ::isMemoryLocation(ExprCursor))
  352. // Turning this into a memory location description makes the deref
  353. // implicit.
  354. LocationKind = Memory;
  355. else
  356. emitOp(dwarf::DW_OP_deref);
  357. break;
  358. case dwarf::DW_OP_constu:
  359. assert(!isRegisterLocation());
  360. emitConstu(Op->getArg(0));
  361. break;
  362. case dwarf::DW_OP_LLVM_convert: {
  363. unsigned BitSize = Op->getArg(0);
  364. dwarf::TypeKind Encoding = static_cast<dwarf::TypeKind>(Op->getArg(1));
  365. if (DwarfVersion >= 5) {
  366. emitOp(dwarf::DW_OP_convert);
  367. // Reuse the base_type if we already have one in this CU otherwise we
  368. // create a new one.
  369. unsigned I = 0, E = CU.ExprRefedBaseTypes.size();
  370. for (; I != E; ++I)
  371. if (CU.ExprRefedBaseTypes[I].BitSize == BitSize &&
  372. CU.ExprRefedBaseTypes[I].Encoding == Encoding)
  373. break;
  374. if (I == E)
  375. CU.ExprRefedBaseTypes.emplace_back(BitSize, Encoding);
  376. // If targeting a location-list; simply emit the index into the raw
  377. // byte stream as ULEB128, DwarfDebug::emitDebugLocEntry has been
  378. // fitted with means to extract it later.
  379. // If targeting a inlined DW_AT_location; insert a DIEBaseTypeRef
  380. // (containing the index and a resolve mechanism during emit) into the
  381. // DIE value list.
  382. emitBaseTypeRef(I);
  383. } else {
  384. if (PrevConvertOp && PrevConvertOp->getArg(0) < BitSize) {
  385. if (Encoding == dwarf::DW_ATE_signed)
  386. emitLegacySExt(PrevConvertOp->getArg(0));
  387. else if (Encoding == dwarf::DW_ATE_unsigned)
  388. emitLegacyZExt(PrevConvertOp->getArg(0));
  389. PrevConvertOp = None;
  390. } else {
  391. PrevConvertOp = Op;
  392. }
  393. }
  394. break;
  395. }
  396. case dwarf::DW_OP_stack_value:
  397. LocationKind = Implicit;
  398. break;
  399. case dwarf::DW_OP_swap:
  400. assert(!isRegisterLocation());
  401. emitOp(dwarf::DW_OP_swap);
  402. break;
  403. case dwarf::DW_OP_xderef:
  404. assert(!isRegisterLocation());
  405. emitOp(dwarf::DW_OP_xderef);
  406. break;
  407. case dwarf::DW_OP_deref_size:
  408. emitOp(dwarf::DW_OP_deref_size);
  409. emitData1(Op->getArg(0));
  410. break;
  411. case dwarf::DW_OP_LLVM_tag_offset:
  412. TagOffset = Op->getArg(0);
  413. break;
  414. default:
  415. llvm_unreachable("unhandled opcode found in expression");
  416. }
  417. }
  418. if (isImplicitLocation())
  419. // Turn this into an implicit location description.
  420. addStackValue();
  421. }
  422. /// add masking operations to stencil out a subregister.
  423. void DwarfExpression::maskSubRegister() {
  424. assert(SubRegisterSizeInBits && "no subregister was registered");
  425. if (SubRegisterOffsetInBits > 0)
  426. addShr(SubRegisterOffsetInBits);
  427. uint64_t Mask = (1ULL << (uint64_t)SubRegisterSizeInBits) - 1ULL;
  428. addAnd(Mask);
  429. }
  430. void DwarfExpression::finalize() {
  431. assert(DwarfRegs.size() == 0 && "dwarf registers not emitted");
  432. // Emit any outstanding DW_OP_piece operations to mask out subregisters.
  433. if (SubRegisterSizeInBits == 0)
  434. return;
  435. // Don't emit a DW_OP_piece for a subregister at offset 0.
  436. if (SubRegisterOffsetInBits == 0)
  437. return;
  438. addOpPiece(SubRegisterSizeInBits, SubRegisterOffsetInBits);
  439. }
  440. void DwarfExpression::addFragmentOffset(const DIExpression *Expr) {
  441. if (!Expr || !Expr->isFragment())
  442. return;
  443. uint64_t FragmentOffset = Expr->getFragmentInfo()->OffsetInBits;
  444. assert(FragmentOffset >= OffsetInBits &&
  445. "overlapping or duplicate fragments");
  446. if (FragmentOffset > OffsetInBits)
  447. addOpPiece(FragmentOffset - OffsetInBits);
  448. OffsetInBits = FragmentOffset;
  449. }
  450. void DwarfExpression::emitLegacySExt(unsigned FromBits) {
  451. // (((X >> (FromBits - 1)) * (~0)) << FromBits) | X
  452. emitOp(dwarf::DW_OP_dup);
  453. emitOp(dwarf::DW_OP_constu);
  454. emitUnsigned(FromBits - 1);
  455. emitOp(dwarf::DW_OP_shr);
  456. emitOp(dwarf::DW_OP_lit0);
  457. emitOp(dwarf::DW_OP_not);
  458. emitOp(dwarf::DW_OP_mul);
  459. emitOp(dwarf::DW_OP_constu);
  460. emitUnsigned(FromBits);
  461. emitOp(dwarf::DW_OP_shl);
  462. emitOp(dwarf::DW_OP_or);
  463. }
  464. void DwarfExpression::emitLegacyZExt(unsigned FromBits) {
  465. // (X & (1 << FromBits - 1))
  466. emitOp(dwarf::DW_OP_constu);
  467. emitUnsigned((1ULL << FromBits) - 1);
  468. emitOp(dwarf::DW_OP_and);
  469. }