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_range.h"
23 #include "llvm/IR/Value.h"
24 #include "llvm/Support/ErrorHandling.h"
28 /// OperandTraits - Compile-time customization of
29 /// operand-related allocators and accessors
30 /// for use of the User class
34 class User : public Value {
35 User(const User &) LLVM_DELETED_FUNCTION;
36 void *operator new(size_t) LLVM_DELETED_FUNCTION;
38 friend struct HungoffOperandTraits;
39 virtual void anchor();
41 /// OperandList - This is a pointer to the array of Uses for this User.
42 /// For nodes of fixed arity (e.g. a binary operator) this array will live
43 /// prefixed to some derived class instance. For nodes of resizable variable
44 /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
45 /// allocated and should be destroyed by the classes' virtual dtor.
48 /// NumOperands - The number of values used by this User.
52 void *operator new(size_t s, unsigned Us);
53 User(Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
54 : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {}
55 Use *allocHungoffUses(unsigned) const;
56 void dropHungoffUses() {
57 Use::zap(OperandList, OperandList + NumOperands, true);
58 OperandList = nullptr;
59 // Reset NumOperands so User::operator delete() does the right thing.
64 Use::zap(OperandList, OperandList + NumOperands);
66 /// operator delete - free memory allocated for User and Use objects
67 void operator delete(void *Usr);
68 /// placement delete - required by std, but never called.
69 void operator delete(void*, unsigned) {
70 llvm_unreachable("Constructor throws?");
72 /// placement delete - required by std, but never called.
73 void operator delete(void*, unsigned, bool) {
74 llvm_unreachable("Constructor throws?");
77 template <int Idx, typename U> static Use &OpFrom(const U *that) {
79 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx]
80 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx];
82 template <int Idx> Use &Op() {
83 return OpFrom<Idx>(this);
85 template <int Idx> const Use &Op() const {
86 return OpFrom<Idx>(this);
89 Value *getOperand(unsigned i) const {
90 assert(i < NumOperands && "getOperand() out of range!");
91 return OperandList[i];
93 void setOperand(unsigned i, Value *Val) {
94 assert(i < NumOperands && "setOperand() out of range!");
95 assert((!isa<Constant>((const Value*)this) ||
96 isa<GlobalValue>((const Value*)this)) &&
97 "Cannot mutate a constant with setOperand!");
100 const Use &getOperandUse(unsigned i) const {
101 assert(i < NumOperands && "getOperandUse() out of range!");
102 return OperandList[i];
104 Use &getOperandUse(unsigned i) {
105 assert(i < NumOperands && "getOperandUse() out of range!");
106 return OperandList[i];
109 unsigned getNumOperands() const { return NumOperands; }
111 // ---------------------------------------------------------------------------
112 // Operand Iterator interface...
114 typedef Use* op_iterator;
115 typedef const Use* const_op_iterator;
116 typedef iterator_range<op_iterator> op_range;
117 typedef iterator_range<const_op_iterator> const_op_range;
119 inline op_iterator op_begin() { return OperandList; }
120 inline const_op_iterator op_begin() const { return OperandList; }
121 inline op_iterator op_end() { return OperandList+NumOperands; }
122 inline const_op_iterator op_end() const { return OperandList+NumOperands; }
123 inline op_range operands() {
124 return op_range(op_begin(), op_end());
126 inline const_op_range operands() const {
127 return const_op_range(op_begin(), op_end());
130 /// Convenience iterator for directly iterating over the Values in the
132 class value_op_iterator
133 : public std::iterator<std::random_access_iterator_tag, Value *,
134 ptrdiff_t, Value *, Value *> {
137 explicit value_op_iterator(Use *U = nullptr) : OI(U) {}
139 bool operator==(const value_op_iterator &x) const {
142 bool operator!=(const value_op_iterator &x) const {
143 return !operator==(x);
146 value_op_iterator &operator+=(ptrdiff_t n) {
150 value_op_iterator &operator-=(ptrdiff_t n) {
154 value_op_iterator operator+(ptrdiff_t n) const {
155 return value_op_iterator(OI + n);
157 friend value_op_iterator operator+(ptrdiff_t n,
158 const value_op_iterator &i) {
161 value_op_iterator operator-(ptrdiff_t n) const {
162 return value_op_iterator(OI - n);
164 ptrdiff_t operator-(const value_op_iterator &RHS) const {
167 bool operator<(const value_op_iterator &RHS) const { return OI < RHS.OI; }
168 bool operator>(const value_op_iterator &RHS) const { return OI > RHS.OI; }
169 bool operator<=(const value_op_iterator &RHS) const { return OI <= RHS.OI; }
170 bool operator>=(const value_op_iterator &RHS) const { return OI >= RHS.OI; }
171 value_op_iterator &operator++() { return *this += 1; }
172 value_op_iterator &operator--() { return *this -= 1; }
173 value_op_iterator operator++(int) {
174 value_op_iterator tmp = *this;
178 value_op_iterator operator--(int) {
179 value_op_iterator tmp = *this;
184 Value *operator*() const { return *OI; }
185 Value *operator->() const { return operator*(); }
186 Value *operator[](ptrdiff_t n) const { return *(*this + n); }
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 // dropAllReferences() - This function is in charge of "letting go" of all
200 // objects that this User refers to. This allows one to
201 // 'delete' a whole class at a time, even though there may be circular
202 // references... First all references are dropped, and all use counts go to
203 // zero. Then everything is deleted for real. Note that no operations are
204 // valid on an object that has "dropped all references", except operator
207 void dropAllReferences() {
208 for (Use &U : operands())
212 /// replaceUsesOfWith - Replaces all references to the "From" definition with
213 /// references to the "To" definition.
215 void replaceUsesOfWith(Value *From, Value *To);
217 // Methods for support type inquiry through isa, cast, and dyn_cast:
218 static inline bool classof(const Value *V) {
219 return isa<Instruction>(V) || isa<Constant>(V);
223 template<> struct simplify_type<User::op_iterator> {
224 typedef Value* SimpleType;
225 static SimpleType getSimplifiedValue(User::op_iterator &Val) {
229 template<> struct simplify_type<User::const_op_iterator> {
230 typedef /*const*/ Value* SimpleType;
231 static SimpleType getSimplifiedValue(User::const_op_iterator &Val) {
236 } // End llvm namespace