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/Value.h"
25 #include "llvm/Support/AlignOf.h"
26 #include "llvm/Support/ErrorHandling.h"
30 template <typename T> class ArrayRef;
31 template <typename T> class MutableArrayRef;
33 /// \brief Compile-time customization of User operands.
35 /// Customizes operand-related allocators and accessors.
39 class User : public Value {
40 User(const User &) = delete;
42 friend struct HungoffOperandTraits;
43 virtual void anchor();
45 LLVM_ATTRIBUTE_ALWAYS_INLINE inline static void *
46 allocateFixedOperandUser(size_t, unsigned, unsigned);
49 /// Allocate a User with an operand pointer co-allocated.
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);
55 /// Allocate a User with the operands co-allocated.
57 /// This is used for subclasses which have a fixed number of operands.
58 void *operator new(size_t Size, unsigned Us);
60 /// Allocate a User with the operands co-allocated. If DescBytes is non-zero
61 /// then allocate an additional DescBytes bytes before the operands. These
62 /// bytes can be accessed by calling getDescriptor.
64 /// DescBytes needs to be divisible by sizeof(void *). The allocated
65 /// descriptor, if any, is aligned to sizeof(void *) bytes.
67 /// This is used for subclasses which have a fixed number of operands.
68 void *operator new(size_t Size, unsigned Us, unsigned DescBytes);
70 User(Type *ty, unsigned vty, Use *, unsigned NumOps)
72 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
73 NumUserOperands = NumOps;
74 // If we have hung off uses, then the operand list should initially be
76 assert((!HasHungOffUses || !getOperandList()) &&
77 "Error in initializing hung off uses for User");
80 /// \brief Allocate the array of Uses, followed by a pointer
81 /// (with bottom bit set) to the User.
82 /// \param IsPhi identifies callers which are phi nodes and which need
83 /// N BasicBlock* allocated along with N
84 void allocHungoffUses(unsigned N, bool IsPhi = false);
86 /// \brief Grow the number of hung off uses. Note that allocHungoffUses
87 /// should be called if there are no uses.
88 void growHungoffUses(unsigned N, bool IsPhi = false);
93 /// \brief Free memory allocated for User and Use objects.
94 void operator delete(void *Usr);
95 /// \brief Placement delete - required by std, but never called.
96 void operator delete(void*, unsigned) {
97 llvm_unreachable("Constructor throws?");
99 /// \brief Placement delete - required by std, but never called.
100 void operator delete(void*, unsigned, bool) {
101 llvm_unreachable("Constructor throws?");
104 template <int Idx, typename U> static Use &OpFrom(const U *that) {
106 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
107 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
109 template <int Idx> Use &Op() {
110 return OpFrom<Idx>(this);
112 template <int Idx> const Use &Op() const {
113 return OpFrom<Idx>(this);
116 Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
118 Use *getIntrusiveOperands() {
119 return reinterpret_cast<Use *>(this) - NumUserOperands;
122 void setOperandList(Use *NewList) {
123 assert(HasHungOffUses &&
124 "Setting operand list only required for hung off uses");
125 getHungOffOperands() = NewList;
128 Use *getOperandList() {
129 return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
131 const Use *getOperandList() const {
132 return const_cast<User *>(this)->getOperandList();
134 Value *getOperand(unsigned i) const {
135 assert(i < NumUserOperands && "getOperand() out of range!");
136 return getOperandList()[i];
138 void setOperand(unsigned i, Value *Val) {
139 assert(i < NumUserOperands && "setOperand() out of range!");
140 assert((!isa<Constant>((const Value*)this) ||
141 isa<GlobalValue>((const Value*)this)) &&
142 "Cannot mutate a constant with setOperand!");
143 getOperandList()[i] = Val;
145 const Use &getOperandUse(unsigned i) const {
146 assert(i < NumUserOperands && "getOperandUse() out of range!");
147 return getOperandList()[i];
149 Use &getOperandUse(unsigned i) {
150 assert(i < NumUserOperands && "getOperandUse() out of range!");
151 return getOperandList()[i];
154 unsigned getNumOperands() const { return NumUserOperands; }
156 /// Returns the descriptor co-allocated with this User instance.
157 ArrayRef<const uint8_t> getDescriptor() const;
159 /// Returns the descriptor co-allocated with this User instance.
160 MutableArrayRef<uint8_t> getDescriptor();
162 /// Set the number of operands on a GlobalVariable.
164 /// GlobalVariable always allocates space for a single operands, but
165 /// doesn't always use it.
167 /// FIXME: As that the number of operands is used to find the start of
168 /// the allocated memory in operator delete, we need to always think we have
169 /// 1 operand before delete.
170 void setGlobalVariableNumOperands(unsigned NumOps) {
171 assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
172 NumUserOperands = NumOps;
175 /// \brief Subclasses with hung off uses need to manage the operand count
176 /// themselves. In these instances, the operand count isn't used to find the
177 /// OperandList, so there's no issue in having the operand count change.
178 void setNumHungOffUseOperands(unsigned NumOps) {
179 assert(HasHungOffUses && "Must have hung off uses to use this method");
180 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
181 NumUserOperands = NumOps;
184 // ---------------------------------------------------------------------------
185 // Operand Iterator interface...
187 typedef Use* op_iterator;
188 typedef const Use* const_op_iterator;
189 typedef iterator_range<op_iterator> op_range;
190 typedef iterator_range<const_op_iterator> const_op_range;
192 op_iterator op_begin() { return getOperandList(); }
193 const_op_iterator op_begin() const { return getOperandList(); }
194 op_iterator op_end() {
195 return getOperandList() + NumUserOperands;
197 const_op_iterator op_end() const {
198 return getOperandList() + NumUserOperands;
200 op_range operands() {
201 return op_range(op_begin(), op_end());
203 const_op_range operands() const {
204 return const_op_range(op_begin(), op_end());
207 /// \brief Iterator for directly iterating over the operand Values.
208 struct value_op_iterator
209 : iterator_adaptor_base<value_op_iterator, op_iterator,
210 std::random_access_iterator_tag, Value *,
211 ptrdiff_t, Value *, Value *> {
212 explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
214 Value *operator*() const { return *I; }
215 Value *operator->() const { return operator*(); }
218 value_op_iterator value_op_begin() {
219 return value_op_iterator(op_begin());
221 value_op_iterator value_op_end() {
222 return value_op_iterator(op_end());
224 iterator_range<value_op_iterator> operand_values() {
225 return make_range(value_op_begin(), value_op_end());
228 /// \brief Drop all references to operands.
230 /// This function is in charge of "letting go" of all objects that this User
231 /// refers to. This allows one to 'delete' a whole class at a time, even
232 /// though there may be circular references... First all references are
233 /// dropped, and all use counts go to zero. Then everything is deleted for
234 /// real. Note that no operations are valid on an object that has "dropped
235 /// all references", except operator delete.
236 void dropAllReferences() {
237 for (Use &U : operands())
241 /// \brief Replace uses of one Value with another.
243 /// Replaces all references to the "From" definition with references to the
245 void replaceUsesOfWith(Value *From, Value *To);
247 // Methods for support type inquiry through isa, cast, and dyn_cast:
248 static inline bool classof(const Value *V) {
249 return isa<Instruction>(V) || isa<Constant>(V);
252 // Either Use objects, or a Use pointer can be prepended to User.
253 static_assert(AlignOf<Use>::Alignment >= AlignOf<User>::Alignment,
254 "Alignment is insufficient after objects prepended to User");
255 static_assert(AlignOf<Use *>::Alignment >= AlignOf<User>::Alignment,
256 "Alignment is insufficient after objects prepended to User");
258 template<> struct simplify_type<User::op_iterator> {
259 typedef Value* SimpleType;
260 static SimpleType getSimplifiedValue(User::op_iterator &Val) {
264 template<> struct simplify_type<User::const_op_iterator> {
265 typedef /*const*/ Value* SimpleType;
266 static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
271 } // End llvm namespace