User.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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?"); - HLSL Change: it does on OOM
  71. /// \brief Placement delete - required by std, but never called.
  72. void operator delete(void*, unsigned, bool) {
  73. llvm_unreachable("Constructor throws?");
  74. }
  75. protected:
  76. template <int Idx, typename U> static Use &OpFrom(const U *that) {
  77. return Idx < 0
  78. ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
  79. : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
  80. }
  81. template <int Idx> Use &Op() {
  82. return OpFrom<Idx>(this);
  83. }
  84. template <int Idx> const Use &Op() const {
  85. return OpFrom<Idx>(this);
  86. }
  87. private:
  88. Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
  89. Use *getIntrusiveOperands() {
  90. return reinterpret_cast<Use *>(this) - NumUserOperands;
  91. }
  92. void setOperandList(Use *NewList) {
  93. assert(HasHungOffUses &&
  94. "Setting operand list only required for hung off uses");
  95. getHungOffOperands() = NewList;
  96. }
  97. public:
  98. Use *getOperandList() {
  99. return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
  100. }
  101. const Use *getOperandList() const {
  102. return const_cast<User *>(this)->getOperandList();
  103. }
  104. Value *getOperand(unsigned i) const {
  105. assert(i < NumUserOperands && "getOperand() out of range!");
  106. return getOperandList()[i];
  107. }
  108. void setOperand(unsigned i, Value *Val) {
  109. assert(i < NumUserOperands && "setOperand() out of range!");
  110. assert((!isa<Constant>((const Value*)this) ||
  111. isa<GlobalValue>((const Value*)this)) &&
  112. "Cannot mutate a constant with setOperand!");
  113. getOperandList()[i] = Val;
  114. }
  115. const Use &getOperandUse(unsigned i) const {
  116. assert(i < NumUserOperands && "getOperandUse() out of range!");
  117. return getOperandList()[i];
  118. }
  119. Use &getOperandUse(unsigned i) {
  120. assert(i < NumUserOperands && "getOperandUse() out of range!");
  121. return getOperandList()[i];
  122. }
  123. unsigned getNumOperands() const { return NumUserOperands; }
  124. /// Set the number of operands on a GlobalVariable.
  125. ///
  126. /// GlobalVariable always allocates space for a single operands, but
  127. /// doesn't always use it.
  128. ///
  129. /// FIXME: As that the number of operands is used to find the start of
  130. /// the allocated memory in operator delete, we need to always think we have
  131. /// 1 operand before delete.
  132. void setGlobalVariableNumOperands(unsigned NumOps) {
  133. assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
  134. NumUserOperands = NumOps;
  135. }
  136. /// Set the number of operands on a Function.
  137. ///
  138. /// Function always allocates space for a single operands, but
  139. /// doesn't always use it.
  140. ///
  141. /// FIXME: As that the number of operands is used to find the start of
  142. /// the allocated memory in operator delete, we need to always think we have
  143. /// 1 operand before delete.
  144. void setFunctionNumOperands(unsigned NumOps) {
  145. assert(NumOps <= 1 && "Function can only have 0 or 1 operands");
  146. NumUserOperands = NumOps;
  147. }
  148. /// \brief Subclasses with hung off uses need to manage the operand count
  149. /// themselves. In these instances, the operand count isn't used to find the
  150. /// OperandList, so there's no issue in having the operand count change.
  151. void setNumHungOffUseOperands(unsigned NumOps) {
  152. assert(HasHungOffUses && "Must have hung off uses to use this method");
  153. assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
  154. NumUserOperands = NumOps;
  155. }
  156. // ---------------------------------------------------------------------------
  157. // Operand Iterator interface...
  158. //
  159. typedef Use* op_iterator;
  160. typedef const Use* const_op_iterator;
  161. typedef iterator_range<op_iterator> op_range;
  162. typedef iterator_range<const_op_iterator> const_op_range;
  163. op_iterator op_begin() { return getOperandList(); }
  164. const_op_iterator op_begin() const { return getOperandList(); }
  165. op_iterator op_end() {
  166. return getOperandList() + NumUserOperands;
  167. }
  168. const_op_iterator op_end() const {
  169. return getOperandList() + NumUserOperands;
  170. }
  171. op_range operands() {
  172. return op_range(op_begin(), op_end());
  173. }
  174. const_op_range operands() const {
  175. return const_op_range(op_begin(), op_end());
  176. }
  177. /// \brief Iterator for directly iterating over the operand Values.
  178. struct value_op_iterator
  179. : iterator_adaptor_base<value_op_iterator, op_iterator,
  180. std::random_access_iterator_tag, Value *,
  181. ptrdiff_t, Value *, Value *> {
  182. explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
  183. Value *operator*() const { return *I; }
  184. Value *operator->() const { return operator*(); }
  185. };
  186. value_op_iterator value_op_begin() {
  187. return value_op_iterator(op_begin());
  188. }
  189. value_op_iterator value_op_end() {
  190. return value_op_iterator(op_end());
  191. }
  192. iterator_range<value_op_iterator> operand_values() {
  193. return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
  194. }
  195. /// \brief Drop all references to operands.
  196. ///
  197. /// This function is in charge of "letting go" of all objects that this User
  198. /// refers to. This allows one to 'delete' a whole class at a time, even
  199. /// though there may be circular references... First all references are
  200. /// dropped, and all use counts go to zero. Then everything is deleted for
  201. /// real. Note that no operations are valid on an object that has "dropped
  202. /// all references", except operator delete.
  203. void dropAllReferences() {
  204. for (Use &U : operands())
  205. U.set(nullptr);
  206. }
  207. /// \brief Replace uses of one Value with another.
  208. ///
  209. /// Replaces all references to the "From" definition with references to the
  210. /// "To" definition.
  211. void replaceUsesOfWith(Value *From, Value *To);
  212. // Methods for support type inquiry through isa, cast, and dyn_cast:
  213. static inline bool classof(const Value *V) {
  214. return isa<Instruction>(V) || isa<Constant>(V);
  215. }
  216. };
  217. // Either Use objects, or a Use pointer can be prepended to User.
  218. // HLSL Change Starts - comment out static asserts, as they are causing errors
  219. //static_assert(AlignOf<Use>::Alignment >= AlignOf<User>::Alignment,
  220. // "Alignment is insufficient after objects prepended to User");
  221. //static_assert(AlignOf<Use *>::Alignment >= AlignOf<User>::Alignment,
  222. // "Alignment is insufficient after objects prepended to User");
  223. // HLSL Change Ends
  224. template<> struct simplify_type<User::op_iterator> {
  225. typedef Value* SimpleType;
  226. static SimpleType getSimplifiedValue(User::op_iterator &Val) {
  227. return Val->get();
  228. }
  229. };
  230. template<> struct simplify_type<User::const_op_iterator> {
  231. typedef /*const*/ Value* SimpleType;
  232. static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
  233. return Val->get();
  234. }
  235. };
  236. } // End llvm namespace
  237. #endif