User.h 12 KB

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