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 'use's 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 User's.
15 // * Constants may be users of other constants (think arrays and stuff)
17 //===----------------------------------------------------------------------===//
22 #include "llvm/Value.h"
26 /// OperandTraits - Compile-time customization of
27 /// operand-related allocators and accessors
28 /// for use of the User class
34 /// OperandTraits<User> - specialization to User
36 struct OperandTraits<User> {
37 static inline Use *op_begin(User*);
38 static inline Use *op_end(User*);
39 static inline unsigned operands(const User*);
44 static inline void *allocate(unsigned);
47 class User : public Value {
48 User(const User &); // Do not implement
49 void *operator new(size_t); // Do not implement
51 friend struct HungoffOperandTraits;
53 /// OperandList - This is a pointer to the array of Uses for this User.
54 /// For nodes of fixed arity (e.g. a binary operator) this array will live
55 /// prefixed to some derived class instance. For nodes of resizable variable
56 /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically
57 /// allocated and should be destroyed by the classes' virtual dtor.
60 /// NumOperands - The number of values used by this User.
64 void *operator new(size_t s, unsigned Us);
65 User(const Type *ty, unsigned vty, Use *OpList, unsigned NumOps)
66 : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {}
67 Use *allocHungoffUses(unsigned) const;
68 void dropHungoffUses(Use *U) {
69 if (OperandList == U) {
73 Use::zap(U, U->getImpliedUser(), true);
77 Use::zap(OperandList, OperandList + NumOperands);
79 /// operator delete - free memory allocated for User and Use objects
80 void operator delete(void *Usr);
81 /// placement delete - required by std, but never called.
82 void operator delete(void*, unsigned) {
83 assert(0 && "Constructor throws?");
85 template <unsigned Idx> Use &Op() {
86 return OperandTraits<User>::op_begin(this)[Idx];
88 template <unsigned Idx> const Use &Op() const {
89 return OperandTraits<User>::op_begin(const_cast<User*>(this))[Idx];
91 Value *getOperand(unsigned i) const {
92 assert(i < NumOperands && "getOperand() out of range!");
93 return OperandList[i];
95 void setOperand(unsigned i, Value *Val) {
96 assert(i < NumOperands && "setOperand() out of range!");
97 assert((!isa<Constant>((const Value*)this) ||
98 isa<GlobalValue>((const Value*)this)) &&
99 "Cannot mutate a constant with setOperand!");
100 OperandList[i] = Val;
102 const Use &getOperandUse(unsigned i) const {
103 assert(i < NumOperands && "getOperand() out of range!");
104 return OperandList[i];
106 Use &getOperandUse(unsigned i) {
107 assert(i < NumOperands && "getOperand() out of range!");
108 return OperandList[i];
111 unsigned getNumOperands() const { return NumOperands; }
113 // ---------------------------------------------------------------------------
114 // Operand Iterator interface...
116 typedef Use* op_iterator;
117 typedef const Use* const_op_iterator;
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; }
124 // dropAllReferences() - This function is in charge of "letting go" of all
125 // objects that this User refers to. This allows one to
126 // 'delete' a whole class at a time, even though there may be circular
127 // references... First all references are dropped, and all use counts go to
128 // zero. Then everything is deleted for real. Note that no operations are
129 // valid on an object that has "dropped all references", except operator
132 void dropAllReferences() {
133 for (op_iterator i = op_begin(), e = op_end(); i != e; ++i)
137 /// replaceUsesOfWith - Replaces all references to the "From" definition with
138 /// references to the "To" definition.
140 void replaceUsesOfWith(Value *From, Value *To);
142 // Methods for support type inquiry through isa, cast, and dyn_cast:
143 static inline bool classof(const User *) { return true; }
144 static inline bool classof(const Value *V) {
145 return isa<Instruction>(V) || isa<Constant>(V);
149 inline Use *OperandTraits<User>::op_begin(User *U) {
150 return U->op_begin();
153 inline Use *OperandTraits<User>::op_end(User *U) {
157 inline unsigned OperandTraits<User>::operands(const User *U) {
158 return U->getNumOperands();
161 template<> struct simplify_type<User::op_iterator> {
162 typedef Value* SimpleType;
164 static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
165 return static_cast<SimpleType>(Val->get());
169 template<> struct simplify_type<const User::op_iterator>
170 : public simplify_type<User::op_iterator> {};
172 template<> struct simplify_type<User::const_op_iterator> {
173 typedef Value* SimpleType;
175 static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
176 return static_cast<SimpleType>(Val->get());
180 template<> struct simplify_type<const User::const_op_iterator>
181 : public simplify_type<User::const_op_iterator> {};
184 // value_use_iterator::getOperandNo - Requires the definition of the User class.
185 template<typename UserTy>
186 unsigned value_use_iterator<UserTy>::getOperandNo() const {
187 return U - U->getUser()->op_begin();
190 } // End llvm namespace