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;
37 void *operator new(size_t) = delete;
39 friend struct HungoffOperandTraits;
40 virtual void anchor();
42 /// \brief This is a pointer to the array of Uses for this User.
44 /// For nodes of fixed arity (e.g. a binary operator) this array will live
45 /// prefixed to some derived class instance. For nodes of resizable variable
46 /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
47 /// allocated and should be destroyed by the classes' virtual dtor.
48 Use *LegacyOperandList;
51 void *operator new(size_t s, unsigned Us);
53 User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
55 setOperandList(OpList);
56 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
57 NumUserOperands = NumOps;
60 /// \brief Allocate the array of Uses, followed by a pointer
61 /// (with bottom bit set) to the User.
62 /// \param IsPhi identifies callers which are phi nodes and which need
63 /// N BasicBlock* allocated along with N
64 void allocHungoffUses(unsigned N, bool IsPhi = false);
66 /// \brief Grow the number of hung off uses. Note that allocHungoffUses
67 /// should be called if there are no uses.
68 void growHungoffUses(unsigned N, bool IsPhi = false);
72 // drop the hung off uses.
73 Use::zap(getOperandList(), getOperandList() + NumUserOperands,
76 setOperandList(nullptr);
77 // Reset NumOperands so User::operator delete() does the right thing.
81 /// \brief Free memory allocated for User and Use objects.
82 void operator delete(void *Usr);
83 /// \brief Placement delete - required by std, but never called.
84 void operator delete(void*, unsigned) {
85 llvm_unreachable("Constructor throws?");
87 /// \brief Placement delete - required by std, but never called.
88 void operator delete(void*, unsigned, bool) {
89 llvm_unreachable("Constructor throws?");
92 template <int Idx, typename U> static Use &OpFrom(const U *that) {
94 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
95 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
97 template <int Idx> Use &Op() {
98 return OpFrom<Idx>(this);
100 template <int Idx> const Use &Op() const {
101 return OpFrom<Idx>(this);
104 void setOperandList(Use *NewList) {
105 LegacyOperandList = NewList;
108 Use *getOperandList() const {
109 return LegacyOperandList;
111 Value *getOperand(unsigned i) const {
112 assert(i < NumUserOperands && "getOperand() out of range!");
113 return getOperandList()[i];
115 void setOperand(unsigned i, Value *Val) {
116 assert(i < NumUserOperands && "setOperand() out of range!");
117 assert((!isa<Constant>((const Value*)this) ||
118 isa<GlobalValue>((const Value*)this)) &&
119 "Cannot mutate a constant with setOperand!");
120 getOperandList()[i] = Val;
122 const Use &getOperandUse(unsigned i) const {
123 assert(i < NumUserOperands && "getOperandUse() out of range!");
124 return getOperandList()[i];
126 Use &getOperandUse(unsigned i) {
127 assert(i < NumUserOperands && "getOperandUse() out of range!");
128 return getOperandList()[i];
131 unsigned getNumOperands() const { return NumUserOperands; }
133 /// Set the number of operands on a GlobalVariable.
135 /// GlobalVariable always allocates space for a single operands, but
136 /// doesn't always use it.
138 /// FIXME: As that the number of operands is used to find the start of
139 /// the allocated memory in operator delete, we need to always think we have
140 /// 1 operand before delete.
141 void setGlobalVariableNumOperands(unsigned NumOps) {
142 assert(NumOps <= 1 && "GlobalVariable can only have 0 or 1 operands");
143 NumUserOperands = NumOps;
146 /// \brief Subclasses with hung off uses need to manage the operand count
147 /// themselves. In these instances, the operand count isn't used to find the
148 /// OperandList, so there's no issue in having the operand count change.
149 void setNumHungOffUseOperands(unsigned NumOps) {
150 assert(HasHungOffUses && "Must have hung off uses to use this method");
151 assert(NumOps < (1u << NumUserOperandsBits) && "Too many operands");
152 NumUserOperands = NumOps;
155 // ---------------------------------------------------------------------------
156 // Operand Iterator interface...
158 typedef Use* op_iterator;
159 typedef const Use* const_op_iterator;
160 typedef iterator_range<op_iterator> op_range;
161 typedef iterator_range<const_op_iterator> const_op_range;
163 inline op_iterator op_begin() { return getOperandList(); }
164 inline const_op_iterator op_begin() const { return getOperandList(); }
165 inline op_iterator op_end() {
166 return getOperandList() + NumUserOperands;
168 inline const_op_iterator op_end() const {
169 return getOperandList() + NumUserOperands;
171 inline op_range operands() {
172 return op_range(op_begin(), op_end());
174 inline const_op_range operands() const {
175 return const_op_range(op_begin(), op_end());
178 /// \brief Iterator for directly iterating over the operand Values.
179 struct value_op_iterator
180 : iterator_adaptor_base<value_op_iterator, op_iterator,
181 std::random_access_iterator_tag, Value *,
182 ptrdiff_t, Value *, Value *> {
183 explicit value_op_iterator(Use *U = nullptr) : iterator_adaptor_base(U) {}
185 Value *operator*() const { return *I; }
186 Value *operator->() const { return operator*(); }
189 inline value_op_iterator value_op_begin() {
190 return value_op_iterator(op_begin());
192 inline value_op_iterator value_op_end() {
193 return value_op_iterator(op_end());
195 inline iterator_range<value_op_iterator> operand_values() {
196 return iterator_range<value_op_iterator>(value_op_begin(), value_op_end());
199 /// \brief Drop all references to operands.
201 /// This function is in charge of "letting go" of all objects that this User
202 /// refers to. This allows one to 'delete' a whole class at a time, even
203 /// though there may be circular references... First all references are
204 /// dropped, and all use counts go to zero. Then everything is deleted for
205 /// real. Note that no operations are valid on an object that has "dropped
206 /// all references", except operator delete.
207 void dropAllReferences() {
208 for (Use &U : operands())
212 /// \brief Replace uses of one Value with another.
214 /// Replaces all references to the "From" definition with references to the
216 void replaceUsesOfWith(Value *From, Value *To);
218 // Methods for support type inquiry through isa, cast, and dyn_cast:
219 static inline bool classof(const Value *V) {
220 return isa<Instruction>(V) || isa<Constant>(V);
224 template<> struct simplify_type<User::op_iterator> {
225 typedef Value* SimpleType;
226 static SimpleType getSimplifiedValue(User::op_iterator &Val) {
230 template<> struct simplify_type<User::const_op_iterator> {
231 typedef /*const*/ Value* SimpleType;
232 static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
237 } // End llvm namespace