1 //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 // * Instructions are the largest class of Users.
15 // * Constants may be users of other constants (think arrays and stuff)
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_IR_USER_H
20 #define LLVM_IR_USER_H
22 #include "llvm/ADT/iterator.h"
23 #include "llvm/ADT/iterator_range.h"
24 #include "llvm/IR/Use.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/ErrorHandling.h"
36 template <typename T> class ArrayRef;
37 template <typename T> class MutableArrayRef;
39 /// \brief Compile-time customization of User operands.
41 /// Customizes operand-related allocators and accessors.
45 class User : public Value {
47 friend struct HungoffOperandTraits;
49 virtual void anchor();
51 LLVM_ATTRIBUTE_ALWAYS_INLINE inline static void *
52 allocateFixedOperandUser(size_t, unsigned, unsigned);
55 /// Allocate a User with an operand pointer co-allocated.
57 /// This is used for subclasses which need to allocate a variable number
58 /// of operands, ie, 'hung off uses'.
59 void *operator new(size_t Size);
61 /// Allocate a User with the operands co-allocated.
63 /// This is used for subclasses which have a fixed number of operands.
64 void *operator new(size_t Size, unsigned Us);
66 /// Allocate a User with the operands co-allocated. If DescBytes is non-zero
67 /// then allocate an additional DescBytes bytes before the operands. These
68 /// bytes can be accessed by calling getDescriptor.
70 /// DescBytes needs to be divisible by sizeof(void *). The allocated
71 /// descriptor, if any, is aligned to sizeof(void *) bytes.
73 /// This is used for subclasses which have a fixed number of operands.
74 void *operator new(size_t Size, unsigned Us, unsigned DescBytes);
76 User(Type *ty, unsigned vty, Use *, unsigned NumOps)
78 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
79 NumUserOperands = NumOps;
80 // If we have hung off uses, then the operand list should initially be
82 assert((!HasHungOffUses || !getOperandList()) &&
83 "Error in initializing hung off uses for User");
86 /// \brief Allocate the array of Uses, followed by a pointer
87 /// (with bottom bit set) to the User.
88 /// \param IsPhi identifies callers which are phi nodes and which need
89 /// N BasicBlock* allocated along with N
90 void allocHungoffUses(unsigned N, bool IsPhi = false);
92 /// \brief Grow the number of hung off uses. Note that allocHungoffUses
93 /// should be called if there are no uses.
94 void growHungoffUses(unsigned N, bool IsPhi = false);
97 User(const User &) = delete;
98 ~User() override = default;
100 /// \brief Free memory allocated for User and Use objects.
101 void operator delete(void *Usr);
102 /// \brief Placement delete - required by std, but never called.
103 void operator delete(void*, unsigned) {
104 llvm_unreachable("Constructor throws?");
106 /// \brief Placement delete - required by std, but never called.
107 void operator delete(void*, unsigned, bool) {
108 llvm_unreachable("Constructor throws?");
112 template <int Idx, typename U> static Use &OpFrom(const U *that) {
114 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
115 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
117 template <int Idx> Use &Op() {
118 return OpFrom<Idx>(this);
120 template <int Idx> const Use &Op() const {
121 return OpFrom<Idx>(this);
125 const Use *getHungOffOperands() const {
126 return *(reinterpret_cast<const Use *const *>(this) - 1);
129 Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
131 const Use *getIntrusiveOperands() const {
132 return reinterpret_cast<const Use *>(this) - NumUserOperands;
135 Use *getIntrusiveOperands() {
136 return reinterpret_cast<Use *>(this) - NumUserOperands;
139 void setOperandList(Use *NewList) {
140 assert(HasHungOffUses &&
141 "Setting operand list only required for hung off uses");
142 getHungOffOperands() = NewList;
146 const Use *getOperandList() const {
147 return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
149 Use *getOperandList() {
150 return const_cast<Use *>(static_cast<const User *>(this)->getOperandList());
153 Value *getOperand(unsigned i) const {
154 assert(i < NumUserOperands && "getOperand() out of range!");
155 return getOperandList()[i];
158 void setOperand(unsigned i, Value *Val) {
159 assert(i < NumUserOperands && "setOperand() out of range!");
160 assert((!isa<Constant>((const Value*)this) ||
161 isa<GlobalValue>((const Value*)this)) &&
162 "Cannot mutate a constant with setOperand!");
163 getOperandList()[i] = Val;
166 const Use &getOperandUse(unsigned i) const {
167 assert(i < NumUserOperands && "getOperandUse() out of range!");
168 return getOperandList()[i];
170 Use &getOperandUse(unsigned i) {
171 assert(i < NumUserOperands && "getOperandUse() out of range!");
172 return getOperandList()[i];
175 unsigned getNumOperands() const { return NumUserOperands; }
177 /// Returns the descriptor co-allocated with this User instance.
178 ArrayRef<const uint8_t> getDescriptor() const;
180 /// Returns the descriptor co-allocated with this User instance.
181 MutableArrayRef<uint8_t> getDescriptor();
183 /// Set the number of operands on a GlobalVariable.
185 /// GlobalVariable always allocates space for a single operands, but
186 /// doesn't always use it.
188 /// FIXME: As that the number of operands is used to find the start of
189 /// the allocated memory in operator delete, we need to always think we have
190 /// 1 operand before delete.
191 void setGlobalVariableNumOperands(unsigned NumOps) {
192 assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
193 NumUserOperands = NumOps;
196 /// \brief Subclasses with hung off uses need to manage the operand count
197 /// themselves. In these instances, the operand count isn't used to find the
198 /// OperandList, so there's no issue in having the operand count change.
199 void setNumHungOffUseOperands(unsigned NumOps) {
200 assert(HasHungOffUses && "Must have hung off uses to use this method");
201 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
202 NumUserOperands = NumOps;
205 // ---------------------------------------------------------------------------
206 // Operand Iterator interface...
208 typedef Use* op_iterator;
209 typedef const Use* const_op_iterator;
210 typedef iterator_range<op_iterator> op_range;
211 typedef iterator_range<const_op_iterator> const_op_range;
213 op_iterator op_begin() { return getOperandList(); }
214 const_op_iterator op_begin() const { return getOperandList(); }
215 op_iterator op_end() {
216 return getOperandList() + NumUserOperands;
218 const_op_iterator op_end() const {
219 return getOperandList() + NumUserOperands;
221 op_range operands() {
222 return op_range(op_begin(), op_end());
224 const_op_range operands() const {
225 return const_op_range(op_begin(), op_end());
228 /// \brief Iterator for directly iterating over the operand Values.
229 struct value_op_iterator
230 : iterator_adaptor_base<value_op_iterator, op_iterator,
231 std::random_access_iterator_tag, Value *,
232 ptrdiff_t, Value *, Value *> {
233 explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
235 Value *operator*() const { return *I; }
236 Value *operator->() const { return operator*(); }
239 value_op_iterator value_op_begin() {
240 return value_op_iterator(op_begin());
242 value_op_iterator value_op_end() {
243 return value_op_iterator(op_end());
245 iterator_range<value_op_iterator> operand_values() {
246 return make_range(value_op_begin(), value_op_end());
249 struct const_value_op_iterator
250 : iterator_adaptor_base<const_value_op_iterator, const_op_iterator,
251 std::random_access_iterator_tag, const Value *,
252 ptrdiff_t, const Value *, const Value *> {
253 explicit const_value_op_iterator(const Use *U = nullptr) :
254 iterator_adaptor_base(U) {}
255 const Value *operator*() const { return *I; }
256 const Value *operator->() const { return operator*(); }
259 const_value_op_iterator value_op_begin() const {
260 return const_value_op_iterator(op_begin());
262 const_value_op_iterator value_op_end() const {
263 return const_value_op_iterator(op_end());
265 iterator_range<const_value_op_iterator> operand_values() const {
266 return make_range(value_op_begin(), value_op_end());
269 /// \brief Drop all references to operands.
271 /// This function is in charge of "letting go" of all objects that this User
272 /// refers to. This allows one to 'delete' a whole class at a time, even
273 /// though there may be circular references... First all references are
274 /// dropped, and all use counts go to zero. Then everything is deleted for
275 /// real. Note that no operations are valid on an object that has "dropped
276 /// all references", except operator delete.
277 void dropAllReferences() {
278 for (Use &U : operands())
282 /// \brief Replace uses of one Value with another.
284 /// Replaces all references to the "From" definition with references to the
286 void replaceUsesOfWith(Value *From, Value *To);
288 // Methods for support type inquiry through isa, cast, and dyn_cast:
289 static inline bool classof(const Value *V) {
290 return isa<Instruction>(V) || isa<Constant>(V);
293 // Either Use objects, or a Use pointer can be prepended to User.
294 static_assert(alignof(Use) >= alignof(User),
295 "Alignment is insufficient after objects prepended to User");
296 static_assert(alignof(Use *) >= alignof(User),
297 "Alignment is insufficient after objects prepended to User");
299 template<> struct simplify_type<User::op_iterator> {
300 typedef Value* SimpleType;
301 static SimpleType getSimplifiedValue(User::op_iterator &Val) {
305 template<> struct simplify_type<User::const_op_iterator> {
306 typedef /*const*/ Value* SimpleType;
307 static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
312 } // end namespace llvm
314 #endif // LLVM_IR_USER_H