SDNodeDbgValue.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //===-- llvm/CodeGen/SDNodeDbgValue.h - SelectionDAG dbg_value --*- C++ -*-===//
  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 declares the SDDbgValue class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SDNODEDBGVALUE_H
  13. #define LLVM_LIB_CODEGEN_SELECTIONDAG_SDNODEDBGVALUE_H
  14. #include "llvm/IR/DebugLoc.h"
  15. #include "llvm/Support/Allocator.h"
  16. #include "llvm/Support/DataTypes.h"
  17. #include <utility>
  18. namespace llvm {
  19. class DIVariable;
  20. class DIExpression;
  21. class SDNode;
  22. class Value;
  23. class raw_ostream;
  24. /// Holds the information for a single machine location through SDISel; either
  25. /// an SDNode, a constant, a stack location, or a virtual register.
  26. class SDDbgOperand {
  27. public:
  28. enum Kind {
  29. SDNODE = 0, ///< Value is the result of an expression.
  30. CONST = 1, ///< Value is a constant.
  31. FRAMEIX = 2, ///< Value is contents of a stack location.
  32. VREG = 3 ///< Value is a virtual register.
  33. };
  34. Kind getKind() const { return kind; }
  35. /// Returns the SDNode* for a register ref
  36. SDNode *getSDNode() const {
  37. assert(kind == SDNODE);
  38. return u.s.Node;
  39. }
  40. /// Returns the ResNo for a register ref
  41. unsigned getResNo() const {
  42. assert(kind == SDNODE);
  43. return u.s.ResNo;
  44. }
  45. /// Returns the Value* for a constant
  46. const Value *getConst() const {
  47. assert(kind == CONST);
  48. return u.Const;
  49. }
  50. /// Returns the FrameIx for a stack object
  51. unsigned getFrameIx() const {
  52. assert(kind == FRAMEIX);
  53. return u.FrameIx;
  54. }
  55. /// Returns the Virtual Register for a VReg
  56. unsigned getVReg() const {
  57. assert(kind == VREG);
  58. return u.VReg;
  59. }
  60. static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo) {
  61. return SDDbgOperand(Node, ResNo);
  62. }
  63. static SDDbgOperand fromFrameIdx(unsigned FrameIdx) {
  64. return SDDbgOperand(FrameIdx, FRAMEIX);
  65. }
  66. static SDDbgOperand fromVReg(unsigned VReg) {
  67. return SDDbgOperand(VReg, VREG);
  68. }
  69. static SDDbgOperand fromConst(const Value *Const) {
  70. return SDDbgOperand(Const);
  71. }
  72. bool operator!=(const SDDbgOperand &Other) const { return !(*this == Other); }
  73. bool operator==(const SDDbgOperand &Other) const {
  74. if (kind != Other.kind)
  75. return false;
  76. switch (kind) {
  77. case SDNODE:
  78. return getSDNode() == Other.getSDNode() && getResNo() == Other.getResNo();
  79. case CONST:
  80. return getConst() == Other.getConst();
  81. case VREG:
  82. return getVReg() == Other.getVReg();
  83. case FRAMEIX:
  84. return getFrameIx() == Other.getFrameIx();
  85. }
  86. return false;
  87. }
  88. private:
  89. Kind kind;
  90. union {
  91. struct {
  92. SDNode *Node; ///< Valid for expressions.
  93. unsigned ResNo; ///< Valid for expressions.
  94. } s;
  95. const Value *Const; ///< Valid for constants.
  96. unsigned FrameIx; ///< Valid for stack objects.
  97. unsigned VReg; ///< Valid for registers.
  98. } u;
  99. /// Constructor for non-constants.
  100. SDDbgOperand(SDNode *N, unsigned R) : kind(SDNODE) {
  101. u.s.Node = N;
  102. u.s.ResNo = R;
  103. }
  104. /// Constructor for constants.
  105. SDDbgOperand(const Value *C) : kind(CONST) { u.Const = C; }
  106. /// Constructor for virtual registers and frame indices.
  107. SDDbgOperand(unsigned VRegOrFrameIdx, Kind Kind) : kind(Kind) {
  108. assert((Kind == VREG || Kind == FRAMEIX) &&
  109. "Invalid SDDbgValue constructor");
  110. if (kind == VREG)
  111. u.VReg = VRegOrFrameIdx;
  112. else
  113. u.FrameIx = VRegOrFrameIdx;
  114. }
  115. };
  116. /// Holds the information from a dbg_value node through SDISel.
  117. /// We do not use SDValue here to avoid including its header.
  118. class SDDbgValue {
  119. public:
  120. private:
  121. // SDDbgValues are allocated by a BumpPtrAllocator, which means the destructor
  122. // may not be called; therefore all member arrays must also be allocated by
  123. // that BumpPtrAllocator, to ensure that they are correctly freed.
  124. size_t NumLocationOps;
  125. SDDbgOperand *LocationOps;
  126. // SDNode dependencies will be calculated as SDNodes that appear in
  127. // LocationOps plus these AdditionalDependencies.
  128. size_t NumAdditionalDependencies;
  129. SDNode **AdditionalDependencies;
  130. DIVariable *Var;
  131. DIExpression *Expr;
  132. DebugLoc DL;
  133. unsigned Order;
  134. bool IsIndirect;
  135. bool IsVariadic;
  136. bool Invalid = false;
  137. bool Emitted = false;
  138. public:
  139. SDDbgValue(BumpPtrAllocator &Alloc, DIVariable *Var, DIExpression *Expr,
  140. ArrayRef<SDDbgOperand> L, ArrayRef<SDNode *> Dependencies,
  141. bool IsIndirect, DebugLoc DL, unsigned O, bool IsVariadic)
  142. : NumLocationOps(L.size()),
  143. LocationOps(Alloc.Allocate<SDDbgOperand>(L.size())),
  144. NumAdditionalDependencies(Dependencies.size()),
  145. AdditionalDependencies(Alloc.Allocate<SDNode *>(Dependencies.size())),
  146. Var(Var), Expr(Expr), DL(DL), Order(O), IsIndirect(IsIndirect),
  147. IsVariadic(IsVariadic) {
  148. assert(IsVariadic || L.size() == 1);
  149. assert(!(IsVariadic && IsIndirect));
  150. std::copy(L.begin(), L.end(), LocationOps);
  151. std::copy(Dependencies.begin(), Dependencies.end(), AdditionalDependencies);
  152. }
  153. // We allocate arrays with the BumpPtrAllocator and never free or copy them,
  154. // for LocationOps and AdditionalDependencies, as we never expect to copy or
  155. // destroy an SDDbgValue. If we ever start copying or destroying instances, we
  156. // should manage the allocated memory appropriately.
  157. SDDbgValue(const SDDbgValue &Other) = delete;
  158. SDDbgValue &operator=(const SDDbgValue &Other) = delete;
  159. ~SDDbgValue() = delete;
  160. /// Returns the DIVariable pointer for the variable.
  161. DIVariable *getVariable() const { return Var; }
  162. /// Returns the DIExpression pointer for the expression.
  163. DIExpression *getExpression() const { return Expr; }
  164. ArrayRef<SDDbgOperand> getLocationOps() const {
  165. return ArrayRef<SDDbgOperand>(LocationOps, NumLocationOps);
  166. }
  167. SmallVector<SDDbgOperand> copyLocationOps() const {
  168. return SmallVector<SDDbgOperand>(LocationOps, LocationOps + NumLocationOps);
  169. }
  170. // Returns the SDNodes which this SDDbgValue depends on.
  171. SmallVector<SDNode *> getSDNodes() const {
  172. SmallVector<SDNode *> Dependencies;
  173. for (const SDDbgOperand &DbgOp : getLocationOps())
  174. if (DbgOp.getKind() == SDDbgOperand::SDNODE)
  175. Dependencies.push_back(DbgOp.getSDNode());
  176. for (SDNode *Node : getAdditionalDependencies())
  177. Dependencies.push_back(Node);
  178. return Dependencies;
  179. }
  180. ArrayRef<SDNode *> getAdditionalDependencies() const {
  181. return ArrayRef<SDNode *>(AdditionalDependencies,
  182. NumAdditionalDependencies);
  183. }
  184. /// Returns whether this is an indirect value.
  185. bool isIndirect() const { return IsIndirect; }
  186. bool isVariadic() const { return IsVariadic; }
  187. /// Returns the DebugLoc.
  188. const DebugLoc &getDebugLoc() const { return DL; }
  189. /// Returns the SDNodeOrder. This is the order of the preceding node in the
  190. /// input.
  191. unsigned getOrder() const { return Order; }
  192. /// setIsInvalidated / isInvalidated - Setter / getter of the "Invalidated"
  193. /// property. A SDDbgValue is invalid if the SDNode that produces the value is
  194. /// deleted.
  195. void setIsInvalidated() { Invalid = true; }
  196. bool isInvalidated() const { return Invalid; }
  197. /// setIsEmitted / isEmitted - Getter/Setter for flag indicating that this
  198. /// SDDbgValue has been emitted to an MBB.
  199. void setIsEmitted() { Emitted = true; }
  200. bool isEmitted() const { return Emitted; }
  201. /// clearIsEmitted - Reset Emitted flag, for certain special cases where
  202. /// dbg.addr is emitted twice.
  203. void clearIsEmitted() { Emitted = false; }
  204. LLVM_DUMP_METHOD void dump() const;
  205. LLVM_DUMP_METHOD void print(raw_ostream &OS) const;
  206. };
  207. /// Holds the information from a dbg_label node through SDISel.
  208. /// We do not use SDValue here to avoid including its header.
  209. class SDDbgLabel {
  210. MDNode *Label;
  211. DebugLoc DL;
  212. unsigned Order;
  213. public:
  214. SDDbgLabel(MDNode *Label, DebugLoc dl, unsigned O)
  215. : Label(Label), DL(std::move(dl)), Order(O) {}
  216. /// Returns the MDNode pointer for the label.
  217. MDNode *getLabel() const { return Label; }
  218. /// Returns the DebugLoc.
  219. const DebugLoc &getDebugLoc() const { return DL; }
  220. /// Returns the SDNodeOrder. This is the order of the preceding node in the
  221. /// input.
  222. unsigned getOrder() const { return Order; }
  223. };
  224. } // end llvm namespace
  225. #endif