SDNodeDbgValue.h 8.3 KB

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