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 /// \brief Compile-time customization of User operands.
32 /// Customizes operand-related allocators and accessors.
36 class User : public Value {
37 User(const User &) = delete;
39 friend struct HungoffOperandTraits;
40 virtual void anchor();
43 /// Allocate a User with an operand pointer co-allocated.
45 /// This is used for subclasses which need to allocate a variable number
46 /// of operands, ie, 'hung off uses'.
47 void *operator new(size_t Size);
49 /// Allocate a User with the operands co-allocated.
51 /// This is used for subclasses which have a fixed number of operands.
52 void *operator new(size_t Size, unsigned Us);
54 User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
56 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
57 NumUserOperands = NumOps;
58 // If we have hung off uses, then the operand list should initially be
60 assert((!HasHungOffUses || !getOperandList()) &&
61 "Error in initializing hung off uses for User");
64 /// \brief Allocate the array of Uses, followed by a pointer
65 /// (with bottom bit set) to the User.
66 /// \param IsPhi identifies callers which are phi nodes and which need
67 /// N BasicBlock* allocated along with N
68 void allocHungoffUses(unsigned N, bool IsPhi = false);
70 /// \brief Grow the number of hung off uses. Note that allocHungoffUses
71 /// should be called if there are no uses.
72 void growHungoffUses(unsigned N, bool IsPhi = false);
76 /// \brief Free memory allocated for User and Use objects.
77 void operator delete(void *Usr);
78 /// \brief Placement delete - required by std, but never called.
79 void operator delete(void*, unsigned) {
80 llvm_unreachable("Constructor throws?");
82 /// \brief Placement delete - required by std, but never called.
83 void operator delete(void*, unsigned, bool) {
84 llvm_unreachable("Constructor throws?");
87 template <int Idx, typename U> static Use &OpFrom(const U *that) {
89 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
90 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
92 template <int Idx> Use &Op() {
93 return OpFrom<Idx>(this);
95 template <int Idx> const Use &Op() const {
96 return OpFrom<Idx>(this);
99 Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
101 Use *getIntrusiveOperands() {
102 return reinterpret_cast<Use *>(this) - NumUserOperands;
105 void setOperandList(Use *NewList) {
106 assert(HasHungOffUses &&
107 "Setting operand list only required for hung off uses");
108 getHungOffOperands() = NewList;
111 Use *getOperandList() {
112 return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
114 const Use *getOperandList() const {
115 return const_cast<User *>(this)->getOperandList();
117 Value *getOperand(unsigned i) const {
118 assert(i < NumUserOperands && "getOperand() out of range!");
119 return getOperandList()[i];
121 void setOperand(unsigned i, Value *Val) {
122 assert(i < NumUserOperands && "setOperand() out of range!");
123 assert((!isa<Constant>((const Value*)this) ||
124 isa<GlobalValue>((const Value*)this)) &&
125 "Cannot mutate a constant with setOperand!");
126 getOperandList()[i] = Val;
128 const Use &getOperandUse(unsigned i) const {
129 assert(i < NumUserOperands && "getOperandUse() out of range!");
130 return getOperandList()[i];
132 Use &getOperandUse(unsigned i) {
133 assert(i < NumUserOperands && "getOperandUse() out of range!");
134 return getOperandList()[i];
137 unsigned getNumOperands() const { return NumUserOperands; }
139 /// Set the number of operands on a GlobalVariable.
141 /// GlobalVariable always allocates space for a single operands, but
142 /// doesn't always use it.
144 /// FIXME: As that the number of operands is used to find the start of
145 /// the allocated memory in operator delete, we need to always think we have
146 /// 1 operand before delete.
147 void setGlobalVariableNumOperands(unsigned NumOps) {
148 assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
149 NumUserOperands = NumOps;
152 /// Set the number of operands on a Function.
154 /// Function always allocates space for a single operands, but
155 /// doesn't always use it.
157 /// FIXME: As that the number of operands is used to find the start of
158 /// the allocated memory in operator delete, we need to always think we have
159 /// 1 operand before delete.
160 void setFunctionNumOperands(unsigned NumOps) {
161 assert(NumOps <= 1 && "Function can only have 0 or 1 operands");
162 NumUserOperands = NumOps;
165 /// \brief Subclasses with hung off uses need to manage the operand count
166 /// themselves. In these instances, the operand count isn't used to find the
167 /// OperandList, so there's no issue in having the operand count change.
168 void setNumHungOffUseOperands(unsigned NumOps) {
169 assert(HasHungOffUses && "Must have hung off uses to use this method");
170 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
171 NumUserOperands = NumOps;
174 // ---------------------------------------------------------------------------
175 // Operand Iterator interface...
177 typedef Use* op_iterator;
178 typedef const Use* const_op_iterator;
179 typedef iterator_range<op_iterator> op_range;
180 typedef iterator_range<const_op_iterator> const_op_range;
182 op_iterator op_begin() { return getOperandList(); }
183 const_op_iterator op_begin() const { return getOperandList(); }
184 op_iterator op_end() {
185 return getOperandList() + NumUserOperands;
187 const_op_iterator op_end() const {
188 return getOperandList() + NumUserOperands;
190 op_range operands() {
191 return op_range(op_begin(), op_end());
193 const_op_range operands() const {
194 return const_op_range(op_begin(), op_end());
197 /// \brief Iterator for directly iterating over the operand Values.
198 struct value_op_iterator
199 : iterator_adaptor_base<value_op_iterator, op_iterator,
200 std::random_access_iterator_tag, Value *,
201 ptrdiff_t, Value *, Value *> {
202 explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
204 Value *operator*() const { return *I; }
205 Value *operator->() const { return operator*(); }
208 value_op_iterator value_op_begin() {
209 return value_op_iterator(op_begin());
211 value_op_iterator value_op_end() {
212 return value_op_iterator(op_end());
214 iterator_range<value_op_iterator> operand_values() {
215 return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
218 /// \brief Drop all references to operands.
220 /// This function is in charge of "letting go" of all objects that this User
221 /// refers to. This allows one to 'delete' a whole class at a time, even
222 /// though there may be circular references... First all references are
223 /// dropped, and all use counts go to zero. Then everything is deleted for
224 /// real. Note that no operations are valid on an object that has "dropped
225 /// all references", except operator delete.
226 void dropAllReferences() {
227 for (Use &U : operands())
231 /// \brief Replace uses of one Value with another.
233 /// Replaces all references to the "From" definition with references to the
235 void replaceUsesOfWith(Value *From, Value *To);
237 // Methods for support type inquiry through isa, cast, and dyn_cast:
238 static inline bool classof(const Value *V) {
239 return isa<Instruction>(V) || isa<Constant>(V);
242 // Either Use objects, or a Use pointer can be prepended to User.
243 static_assert(AlignOf<Use>::Alignment >= AlignOf<User>::Alignment,
244 "Alignment is insufficient after objects prepended to User");
245 static_assert(AlignOf<Use *>::Alignment >= AlignOf<User>::Alignment,
246 "Alignment is insufficient after objects prepended to User");
248 template<> struct simplify_type<User::op_iterator> {
249 typedef Value* SimpleType;
250 static SimpleType getSimplifiedValue(User::op_iterator &Val) {
254 template<> struct simplify_type<User::const_op_iterator> {
255 typedef /*const*/ Value* SimpleType;
256 static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
261 } // End llvm namespace