User.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===//
  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 class defines the interface that one who uses a Value must implement.
  11. // Each instance of the Value class keeps track of what User's have handles
  12. // to it.
  13. //
  14. // * Instructions are the largest class of Users.
  15. // * Constants may be users of other constants (think arrays and stuff)
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_IR_USER_H
  19. #define LLVM_IR_USER_H
  20. #include "llvm/ADT/iterator.h"
  21. #include "llvm/ADT/iterator_range.h"
  22. #include "llvm/IR/Value.h"
  23. #include "llvm/Support/AlignOf.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. namespace llvm {
  26. /// \brief Compile-time customization of User operands.
  27. ///
  28. /// Customizes operand-related allocators and accessors.
  29. template <class>
  30. struct OperandTraits;
  31. class User : public Value {
  32. User(const User &) = delete;
  33. template <unsigned>
  34. friend struct HungoffOperandTraits;
  35. virtual void anchor();
  36. protected:
  37. /// Allocate a User with an operand pointer co-allocated.
  38. ///
  39. /// This is used for subclasses which need to allocate a variable number
  40. /// of operands, ie, 'hung off uses'.
  41. void *operator new(size_t Size);
  42. /// Allocate a User with the operands co-allocated.
  43. ///
  44. /// This is used for subclasses which have a fixed number of operands.
  45. void *operator new(size_t Size, unsigned Us);
  46. User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
  47. : Value(ty, vty) {
  48. assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
  49. NumUserOperands = NumOps;
  50. // If we have hung off uses, then the operand list should initially be
  51. // null.
  52. assert((!HasHungOffUses || !getOperandList()) &&
  53. "Error in initializing hung off uses for User");
  54. }
  55. /// \brief Allocate the array of Uses, followed by a pointer
  56. /// (with bottom bit set) to the User.
  57. /// \param IsPhi identifies callers which are phi nodes and which need
  58. /// N BasicBlock* allocated along with N
  59. void allocHungoffUses(unsigned N, bool IsPhi = false);
  60. /// \brief Grow the number of hung off uses. Note that allocHungoffUses
  61. /// should be called if there are no uses.
  62. void growHungoffUses(unsigned N, bool IsPhi = false);
  63. public:
  64. ~User() override {
  65. }
  66. /// \brief Free memory allocated for User and Use objects.
  67. void operator delete(void *Usr);
  68. /// \brief Placement delete - required by std, but never called.
  69. void operator delete(void*, unsigned) {
  70. llvm_unreachable("Constructor throws?");
  71. }
  72. /// \brief Placement delete - required by std, but never called.
  73. void operator delete(void*, unsigned, bool) {
  74. llvm_unreachable("Constructor throws?");
  75. }
  76. protected:
  77. template <int Idx, typename U> static Use &OpFrom(const U *that) {
  78. return Idx < 0
  79. ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
  80. : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
  81. }
  82. template <int Idx> Use &Op() {
  83. return OpFrom<Idx>(this);
  84. }
  85. template <int Idx> const Use &Op() const {
  86. return OpFrom<Idx>(this);
  87. }
  88. private:
  89. Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
  90. Use *getIntrusiveOperands() {
  91. return reinterpret_cast<Use *>(this) - NumUserOperands;
  92. }
  93. void setOperandList(Use *NewList) {
  94. assert(HasHungOffUses &&
  95. "Setting operand list only required for hung off uses");
  96. getHungOffOperands() = NewList;
  97. }
  98. public:
  99. Use *getOperandList() {
  100. return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
  101. }
  102. const Use *getOperandList() const {
  103. return const_cast<User *>(this)->getOperandList();
  104. }
  105. Value *getOperand(unsigned i) const {
  106. assert(i < NumUserOperands && "getOperand() out of range!");
  107. return getOperandList()[i];
  108. }
  109. void setOperand(unsigned i, Value *Val) {
  110. assert(i < NumUserOperands && "setOperand() out of range!");
  111. assert((!isa<Constant>((const Value*)this) ||
  112. isa<GlobalValue>((const Value*)this)) &&
  113. "Cannot mutate a constant with setOperand!");
  114. getOperandList()[i] = Val;
  115. }
  116. const Use &getOperandUse(unsigned i) const {
  117. assert(i < NumUserOperands && "getOperandUse() out of range!");
  118. return getOperandList()[i];
  119. }
  120. Use &getOperandUse(unsigned i) {
  121. assert(i < NumUserOperands && "getOperandUse() out of range!");
  122. return getOperandList()[i];
  123. }
  124. unsigned getNumOperands() const { return NumUserOperands; }
  125. /// Set the number of operands on a GlobalVariable.
  126. ///
  127. /// GlobalVariable always allocates space for a single operands, but
  128. /// doesn't always use it.
  129. ///
  130. /// FIXME: As that the number of operands is used to find the start of
  131. /// the allocated memory in operator delete, we need to always think we have
  132. /// 1 operand before delete.
  133. void setGlobalVariableNumOperands(unsigned NumOps) {
  134. assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
  135. NumUserOperands = NumOps;
  136. }
  137. /// Set the number of operands on a Function.
  138. ///
  139. /// Function always allocates space for a single operands, but
  140. /// doesn't always use it.
  141. ///
  142. /// FIXME: As that the number of operands is used to find the start of
  143. /// the allocated memory in operator delete, we need to always think we have
  144. /// 1 operand before delete.
  145. void setFunctionNumOperands(unsigned NumOps) {
  146. assert(NumOps <= 1 && "Function can only have 0 or 1 operands");
  147. NumUserOperands = NumOps;
  148. }
  149. /// \brief Subclasses with hung off uses need to manage the operand count
  150. /// themselves. In these instances, the operand count isn't used to find the
  151. /// OperandList, so there's no issue in having the operand count change.
  152. void setNumHungOffUseOperands(unsigned NumOps) {
  153. assert(HasHungOffUses && "Must have hung off uses to use this method");
  154. assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
  155. NumUserOperands = NumOps;
  156. }
  157. // ---------------------------------------------------------------------------
  158. // Operand Iterator interface...
  159. //
  160. typedef Use* op_iterator;
  161. typedef const Use* const_op_iterator;
  162. typedef iterator_range<op_iterator> op_range;
  163. typedef iterator_range<const_op_iterator> const_op_range;
  164. op_iterator op_begin() { return getOperandList(); }
  165. const_op_iterator op_begin() const { return getOperandList(); }
  166. op_iterator op_end() {
  167. return getOperandList() + NumUserOperands;
  168. }
  169. const_op_iterator op_end() const {
  170. return getOperandList() + NumUserOperands;
  171. }
  172. op_range operands() {
  173. return op_range(op_begin(), op_end());
  174. }
  175. const_op_range operands() const {
  176. return const_op_range(op_begin(), op_end());
  177. }
  178. /// \brief Iterator for directly iterating over the operand Values.
  179. struct value_op_iterator
  180. : iterator_adaptor_base<value_op_iterator, op_iterator,
  181. std::random_access_iterator_tag, Value *,
  182. ptrdiff_t, Value *, Value *> {
  183. explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
  184. Value *operator*() const { return *I; }
  185. Value *operator->() const { return operator*(); }
  186. };
  187. value_op_iterator value_op_begin() {
  188. return value_op_iterator(op_begin());
  189. }
  190. value_op_iterator value_op_end() {
  191. return value_op_iterator(op_end());
  192. }
  193. iterator_range<value_op_iterator> operand_values() {
  194. return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
  195. }
  196. /// \brief Drop all references to operands.
  197. ///
  198. /// This function is in charge of "letting go" of all objects that this User
  199. /// refers to. This allows one to 'delete' a whole class at a time, even
  200. /// though there may be circular references... First all references are
  201. /// dropped, and all use counts go to zero. Then everything is deleted for
  202. /// real. Note that no operations are valid on an object that has "dropped
  203. /// all references", except operator delete.
  204. void dropAllReferences() {
  205. for (Use &U : operands())
  206. U.set(nullptr);
  207. }
  208. /// \brief Replace uses of one Value with another.
  209. ///
  210. /// Replaces all references to the "From" definition with references to the
  211. /// "To" definition.
  212. void replaceUsesOfWith(Value *From, Value *To);
  213. // Methods for support type inquiry through isa, cast, and dyn_cast:
  214. static inline bool classof(const Value *V) {
  215. return isa<Instruction>(V) || isa<Constant>(V);
  216. }
  217. };
  218. // Either Use objects, or a Use pointer can be prepended to User.
  219. // HLSL Change Starts - comment out static asserts, as they are causing errors
  220. //static_assert(AlignOf<Use>::Alignment >= AlignOf<User>::Alignment,
  221. // "Alignment is insufficient after objects prepended to User");
  222. //static_assert(AlignOf<Use *>::Alignment >= AlignOf<User>::Alignment,
  223. // "Alignment is insufficient after objects prepended to User");
  224. // HLSL Change Ends
  225. template<> struct simplify_type<User::op_iterator> {
  226. typedef Value* SimpleType;
  227. static SimpleType getSimplifiedValue(User::op_iterator &Val) {
  228. return Val->get();
  229. }
  230. };
  231. template<> struct simplify_type<User::const_op_iterator> {
  232. typedef /*const*/ Value* SimpleType;
  233. static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
  234. return Val->get();
  235. }
  236. };
  237. } // End llvm namespace
  238. #endif