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/ErrorHandling.h"
29 /// \brief Compile-time customization of User operands.
31 /// Customizes operand-related allocators and accessors.
35 class User : public Value {
36 User(const User &) = delete;
38 friend struct HungoffOperandTraits;
39 virtual void anchor();
41 /// \brief This is a pointer to the array of Uses for this User.
43 /// For nodes of fixed arity (e.g. a binary operator) this array will live
44 /// prefixed to some derived class instance. For nodes of resizable variable
45 /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
46 /// allocated and should be destroyed by the classes' virtual dtor.
47 Use *LegacyOperandList;
50 /// Allocate a User with an operand pointer co-allocated.
52 /// This is used for subclasses which need to allocate a variable number
53 /// of operands, ie, 'hung off uses'.
54 void *operator new(size_t Size);
56 /// Allocate a User with the operands co-allocated.
58 /// This is used for subclasses which have a fixed number of operands.
59 void *operator new(size_t Size, unsigned Us);
61 User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
63 setOperandList(OpList);
64 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
65 NumUserOperands = NumOps;
68 /// \brief Allocate the array of Uses, followed by a pointer
69 /// (with bottom bit set) to the User.
70 /// \param IsPhi identifies callers which are phi nodes and which need
71 /// N BasicBlock* allocated along with N
72 void allocHungoffUses(unsigned N, bool IsPhi = false);
74 /// \brief Grow the number of hung off uses. Note that allocHungoffUses
75 /// should be called if there are no uses.
76 void growHungoffUses(unsigned N, bool IsPhi = false);
80 // drop the hung off uses.
81 Use::zap(getOperandList(), getOperandList() + NumUserOperands,
84 setOperandList(nullptr);
85 // Reset NumOperands so User::operator delete() does the right thing.
89 /// \brief Free memory allocated for User and Use objects.
90 void operator delete(void *Usr);
91 /// \brief Placement delete - required by std, but never called.
92 void operator delete(void*, unsigned) {
93 llvm_unreachable("Constructor throws?");
95 /// \brief Placement delete - required by std, but never called.
96 void operator delete(void*, unsigned, bool) {
97 llvm_unreachable("Constructor throws?");
100 template <int Idx, typename U> static Use &OpFrom(const U *that) {
102 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
103 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
105 template <int Idx> Use &Op() {
106 return OpFrom<Idx>(this);
108 template <int Idx> const Use &Op() const {
109 return OpFrom<Idx>(this);
112 void setOperandList(Use *NewList) {
113 LegacyOperandList = NewList;
116 Use *getOperandList() const {
117 return LegacyOperandList;
119 Value *getOperand(unsigned i) const {
120 assert(i < NumUserOperands && "getOperand() out of range!");
121 return getOperandList()[i];
123 void setOperand(unsigned i, Value *Val) {
124 assert(i < NumUserOperands && "setOperand() out of range!");
125 assert((!isa<Constant>((const Value*)this) ||
126 isa<GlobalValue>((const Value*)this)) &&
127 "Cannot mutate a constant with setOperand!");
128 getOperandList()[i] = Val;
130 const Use &getOperandUse(unsigned i) const {
131 assert(i < NumUserOperands && "getOperandUse() out of range!");
132 return getOperandList()[i];
134 Use &getOperandUse(unsigned i) {
135 assert(i < NumUserOperands && "getOperandUse() out of range!");
136 return getOperandList()[i];
139 unsigned getNumOperands() const { return NumUserOperands; }
141 /// Set the number of operands on a GlobalVariable.
143 /// GlobalVariable always allocates space for a single operands, but
144 /// doesn't always use it.
146 /// FIXME: As that the number of operands is used to find the start of
147 /// the allocated memory in operator delete, we need to always think we have
148 /// 1 operand before delete.
149 void setGlobalVariableNumOperands(unsigned NumOps) {
150 assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
151 NumUserOperands = NumOps;
154 /// \brief Subclasses with hung off uses need to manage the operand count
155 /// themselves. In these instances, the operand count isn't used to find the
156 /// OperandList, so there's no issue in having the operand count change.
157 void setNumHungOffUseOperands(unsigned NumOps) {
158 assert(HasHungOffUses && "Must have hung off uses to use this method");
159 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
160 NumUserOperands = NumOps;
163 // ---------------------------------------------------------------------------
164 // Operand Iterator interface...
166 typedef Use* op_iterator;
167 typedef const Use* const_op_iterator;
168 typedef iterator_range<op_iterator> op_range;
169 typedef iterator_range<const_op_iterator> const_op_range;
171 inline op_iterator op_begin() { return getOperandList(); }
172 inline const_op_iterator op_begin() const { return getOperandList(); }
173 inline op_iterator op_end() {
174 return getOperandList() + NumUserOperands;
176 inline const_op_iterator op_end() const {
177 return getOperandList() + NumUserOperands;
179 inline op_range operands() {
180 return op_range(op_begin(), op_end());
182 inline const_op_range operands() const {
183 return const_op_range(op_begin(), op_end());
186 /// \brief Iterator for directly iterating over the operand Values.
187 struct value_op_iterator
188 : iterator_adaptor_base<value_op_iterator, op_iterator,
189 std::random_access_iterator_tag, Value *,
190 ptrdiff_t, Value *, Value *> {
191 explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
193 Value *operator*() const { return *I; }
194 Value *operator->() const { return operator*(); }
197 inline value_op_iterator value_op_begin() {
198 return value_op_iterator(op_begin());
200 inline value_op_iterator value_op_end() {
201 return value_op_iterator(op_end());
203 inline iterator_range<value_op_iterator> operand_values() {
204 return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
207 /// \brief Drop all references to operands.
209 /// This function is in charge of "letting go" of all objects that this User
210 /// refers to. This allows one to 'delete' a whole class at a time, even
211 /// though there may be circular references... First all references are
212 /// dropped, and all use counts go to zero. Then everything is deleted for
213 /// real. Note that no operations are valid on an object that has "dropped
214 /// all references", except operator delete.
215 void dropAllReferences() {
216 for (Use &U : operands())
220 /// \brief Replace uses of one Value with another.
222 /// Replaces all references to the "From" definition with references to the
224 void replaceUsesOfWith(Value *From, Value *To);
226 // Methods for support type inquiry through isa, cast, and dyn_cast:
227 static inline bool classof(const Value *V) {
228 return isa<Instruction>(V) || isa<Constant>(V);
232 template<> struct simplify_type<User::op_iterator> {
233 typedef Value* SimpleType;
234 static SimpleType getSimplifiedValue(User::op_iterator &Val) {
238 template<> struct simplify_type<User::const_op_iterator> {
239 typedef /*const*/ Value* SimpleType;
240 static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
245 } // End llvm namespace