User.h 12 KB

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