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/ArrayRef.h"
23 #include "llvm/ADT/iterator.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/Support/AlignOf.h"
27 #include "llvm/Support/ErrorHandling.h"
31 /// \brief Compile-time customization of User operands.
33 /// Customizes operand-related allocators and accessors.
37 class User : public Value {
38 User(const User &) = delete;
40 friend struct HungoffOperandTraits;
41 virtual void anchor();
43 LLVM_ATTRIBUTE_ALWAYS_INLINE inline static void *
44 allocateFixedOperandUser(size_t, unsigned, unsigned);
47 /// Allocate a User with an operand pointer co-allocated.
49 /// This is used for subclasses which need to allocate a variable number
50 /// of operands, ie, 'hung off uses'.
51 void *operator new(size_t Size);
53 /// Allocate a User with the operands co-allocated.
55 /// This is used for subclasses which have a fixed number of operands.
56 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.
62 /// DescBytes needs to be divisible by sizeof(void *). The allocated
63 /// descriptor, if any, is aligned to sizeof(void *) bytes.
65 /// This is used for subclasses which have a fixed number of operands.
66 void *operator new(size_t Size, unsigned Us, unsigned DescBytes);
68 User(Type *ty, unsigned vty, Use *, unsigned NumOps)
70 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
71 NumUserOperands = NumOps;
72 // If we have hung off uses, then the operand list should initially be
74 assert((!HasHungOffUses || !getOperandList()) &&
75 "Error in initializing hung off uses for User");
78 /// \brief Allocate the array of Uses, followed by a pointer
79 /// (with bottom bit set) to the User.
80 /// \param IsPhi identifies callers which are phi nodes and which need
81 /// N BasicBlock* allocated along with N
82 void allocHungoffUses(unsigned N, bool IsPhi = false);
84 /// \brief Grow the number of hung off uses. Note that allocHungoffUses
85 /// should be called if there are no uses.
86 void growHungoffUses(unsigned N, bool IsPhi = false);
91 /// \brief Free memory allocated for User and Use objects.
92 void operator delete(void *Usr);
93 /// \brief Placement delete - required by std, but never called.
94 void operator delete(void*, unsigned) {
95 llvm_unreachable("Constructor throws?");
97 /// \brief Placement delete - required by std, but never called.
98 void operator delete(void*, unsigned, bool) {
99 llvm_unreachable("Constructor throws?");
102 template <int Idx, typename U> static Use &OpFrom(const U *that) {
104 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
105 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
107 template <int Idx> Use &Op() {
108 return OpFrom<Idx>(this);
110 template <int Idx> const Use &Op() const {
111 return OpFrom<Idx>(this);
114 Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
116 Use *getIntrusiveOperands() {
117 return reinterpret_cast<Use *>(this) - NumUserOperands;
120 void setOperandList(Use *NewList) {
121 assert(HasHungOffUses &&
122 "Setting operand list only required for hung off uses");
123 getHungOffOperands() = NewList;
126 Use *getOperandList() {
127 return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
129 const Use *getOperandList() const {
130 return const_cast<User *>(this)->getOperandList();
132 Value *getOperand(unsigned i) const {
133 assert(i < NumUserOperands && "getOperand() out of range!");
134 return getOperandList()[i];
136 void setOperand(unsigned i, Value *Val) {
137 assert(i < NumUserOperands && "setOperand() out of range!");
138 assert((!isa<Constant>((const Value*)this) ||
139 isa<GlobalValue>((const Value*)this)) &&
140 "Cannot mutate a constant with setOperand!");
141 getOperandList()[i] = Val;
143 const Use &getOperandUse(unsigned i) const {
144 assert(i < NumUserOperands && "getOperandUse() out of range!");
145 return getOperandList()[i];
147 Use &getOperandUse(unsigned i) {
148 assert(i < NumUserOperands && "getOperandUse() out of range!");
149 return getOperandList()[i];
152 unsigned getNumOperands() const { return NumUserOperands; }
154 /// Returns the descriptor co-allocated with this User instance.
155 ArrayRef<const uint8_t> getDescriptor() const;
157 /// Returns the descriptor co-allocated with this User instance.
158 MutableArrayRef<uint8_t> getDescriptor();
160 /// Set the number of operands on a GlobalVariable.
162 /// GlobalVariable always allocates space for a single operands, but
163 /// doesn't always use it.
165 /// FIXME: As that the number of operands is used to find the start of
166 /// the allocated memory in operator delete, we need to always think we have
167 /// 1 operand before delete.
168 void setGlobalVariableNumOperands(unsigned NumOps) {
169 assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
170 NumUserOperands = NumOps;
173 /// \brief Subclasses with hung off uses need to manage the operand count
174 /// themselves. In these instances, the operand count isn't used to find the
175 /// OperandList, so there's no issue in having the operand count change.
176 void setNumHungOffUseOperands(unsigned NumOps) {
177 assert(HasHungOffUses && "Must have hung off uses to use this method");
178 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
179 NumUserOperands = NumOps;
182 // ---------------------------------------------------------------------------
183 // Operand Iterator interface...
185 typedef Use* op_iterator;
186 typedef const Use* const_op_iterator;
187 typedef iterator_range<op_iterator> op_range;
188 typedef iterator_range<const_op_iterator> const_op_range;
190 op_iterator op_begin() { return getOperandList(); }
191 const_op_iterator op_begin() const { return getOperandList(); }
192 op_iterator op_end() {
193 return getOperandList() + NumUserOperands;
195 const_op_iterator op_end() const {
196 return getOperandList() + NumUserOperands;
198 op_range operands() {
199 return op_range(op_begin(), op_end());
201 const_op_range operands() const {
202 return const_op_range(op_begin(), op_end());
205 /// \brief Iterator for directly iterating over the operand Values.
206 struct value_op_iterator
207 : iterator_adaptor_base<value_op_iterator, op_iterator,
208 std::random_access_iterator_tag, Value *,
209 ptrdiff_t, Value *, Value *> {
210 explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
212 Value *operator*() const { return *I; }
213 Value *operator->() const { return operator*(); }
216 value_op_iterator value_op_begin() {
217 return value_op_iterator(op_begin());
219 value_op_iterator value_op_end() {
220 return value_op_iterator(op_end());
222 iterator_range<value_op_iterator> operand_values() {
223 return make_range(value_op_begin(), value_op_end());
226 /// \brief Drop all references to operands.
228 /// This function is in charge of "letting go" of all objects that this User
229 /// refers to. This allows one to 'delete' a whole class at a time, even
230 /// though there may be circular references... First all references are
231 /// dropped, and all use counts go to zero. Then everything is deleted for
232 /// real. Note that no operations are valid on an object that has "dropped
233 /// all references", except operator delete.
234 void dropAllReferences() {
235 for (Use &U : operands())
239 /// \brief Replace uses of one Value with another.
241 /// Replaces all references to the "From" definition with references to the
243 void replaceUsesOfWith(Value *From, Value *To);
245 // Methods for support type inquiry through isa, cast, and dyn_cast:
246 static inline bool classof(const Value *V) {
247 return isa<Instruction>(V) || isa<Constant>(V);
250 // Either Use objects, or a Use pointer can be prepended to User.
251 static_assert(AlignOf<Use>::Alignment >= AlignOf<User>::Alignment,
252 "Alignment is insufficient after objects prepended to User");
253 static_assert(AlignOf<Use *>::Alignment >= AlignOf<User>::Alignment,
254 "Alignment is insufficient after objects prepended to User");
256 template<> struct simplify_type<User::op_iterator> {
257 typedef Value* SimpleType;
258 static SimpleType getSimplifiedValue(User::op_iterator &Val) {
262 template<> struct simplify_type<User::const_op_iterator> {
263 typedef /*const*/ Value* SimpleType;
264 static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
269 } // End llvm namespace