LLVM API Documentation
00001 //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This class defines the interface that one who 'use's a Value must implement. 00011 // Each instance of the Value class keeps track of what User's have handles 00012 // to it. 00013 // 00014 // * Instructions are the largest class of User's. 00015 // * Constants may be users of other constants (think arrays and stuff) 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #ifndef LLVM_USER_H 00020 #define LLVM_USER_H 00021 00022 #include "llvm/Value.h" 00023 00024 namespace llvm { 00025 00026 /// OperandTraits - Compile-time customization of 00027 /// operand-related allocators and accessors 00028 /// for use of the User class 00029 template <class> 00030 struct OperandTraits; 00031 00032 class User; 00033 00034 /// OperandTraits<User> - specialization to User 00035 template <> 00036 struct OperandTraits<User> { 00037 static inline Use *op_begin(User*); 00038 static inline Use *op_end(User*); 00039 static inline unsigned operands(const User*); 00040 template <class U> 00041 struct Layout { 00042 typedef U overlay; 00043 }; 00044 }; 00045 00046 class User : public Value { 00047 User(const User &); // Do not implement 00048 void *operator new(size_t); // Do not implement 00049 template <unsigned> 00050 friend struct HungoffOperandTraits; 00051 protected: 00052 /// OperandList - This is a pointer to the array of Uses for this User. 00053 /// For nodes of fixed arity (e.g. a binary operator) this array will live 00054 /// prefixed to some derived class instance. For nodes of resizable variable 00055 /// arity (e.g. PHINodes, SwitchInst etc.), this memory will be dynamically 00056 /// allocated and should be destroyed by the classes' virtual dtor. 00057 Use *OperandList; 00058 00059 /// NumOperands - The number of values used by this User. 00060 /// 00061 unsigned NumOperands; 00062 00063 void *operator new(size_t s, unsigned Us); 00064 void *operator new(size_t s, unsigned Us, bool Prefix); 00065 User(const Type *ty, unsigned vty, Use *OpList, unsigned NumOps) 00066 : Value(ty, vty), OperandList(OpList), NumOperands(NumOps) {} 00067 Use *allocHungoffUses(unsigned) const; 00068 void dropHungoffUses(Use *U) { 00069 if (OperandList == U) { 00070 OperandList = 0; 00071 NumOperands = 0; 00072 } 00073 Use::zap(U, U->getImpliedUser(), true); 00074 } 00075 public: 00076 ~User() { 00077 if ((intptr_t(OperandList) & 1) == 0) 00078 Use::zap(OperandList, OperandList + NumOperands); 00079 } 00080 /// operator delete - free memory allocated for User and Use objects 00081 void operator delete(void *Usr); 00082 /// placement delete - required by std, but never called. 00083 void operator delete(void*, unsigned) { 00084 assert(0 && "Constructor throws?"); 00085 } 00086 /// placement delete - required by std, but never called. 00087 void operator delete(void*, unsigned, bool) { 00088 assert(0 && "Constructor throws?"); 00089 } 00090 protected: 00091 template <int Idx, typename U> static Use &OpFrom(const U *that) { 00092 return Idx < 0 00093 ? OperandTraits<U>::op_end(const_cast<U*>(that))[Idx] 00094 : OperandTraits<U>::op_begin(const_cast<U*>(that))[Idx]; 00095 } 00096 template <int Idx> Use &Op() { 00097 return OpFrom<Idx>(this); 00098 } 00099 template <int Idx> const Use &Op() const { 00100 return OpFrom<Idx>(this); 00101 } 00102 public: 00103 Value *getOperand(unsigned i) const { 00104 assert(i < NumOperands && "getOperand() out of range!"); 00105 return OperandList[i]; 00106 } 00107 void setOperand(unsigned i, Value *Val) { 00108 assert(i < NumOperands && "setOperand() out of range!"); 00109 assert((!isa<Constant>((const Value*)this) || 00110 isa<GlobalValue>((const Value*)this)) && 00111 "Cannot mutate a constant with setOperand!"); 00112 OperandList[i] = Val; 00113 } 00114 const Use &getOperandUse(unsigned i) const { 00115 assert(i < NumOperands && "getOperand() out of range!"); 00116 return OperandList[i]; 00117 } 00118 Use &getOperandUse(unsigned i) { 00119 assert(i < NumOperands && "getOperand() out of range!"); 00120 return OperandList[i]; 00121 } 00122 00123 unsigned getNumOperands() const { return NumOperands; } 00124 00125 // --------------------------------------------------------------------------- 00126 // Operand Iterator interface... 00127 // 00128 typedef Use* op_iterator; 00129 typedef const Use* const_op_iterator; 00130 00131 inline op_iterator op_begin() { return OperandList; } 00132 inline const_op_iterator op_begin() const { return OperandList; } 00133 inline op_iterator op_end() { return OperandList+NumOperands; } 00134 inline const_op_iterator op_end() const { return OperandList+NumOperands; } 00135 00136 // dropAllReferences() - This function is in charge of "letting go" of all 00137 // objects that this User refers to. This allows one to 00138 // 'delete' a whole class at a time, even though there may be circular 00139 // references... First all references are dropped, and all use counts go to 00140 // zero. Then everything is deleted for real. Note that no operations are 00141 // valid on an object that has "dropped all references", except operator 00142 // delete. 00143 // 00144 void dropAllReferences() { 00145 for (op_iterator i = op_begin(), e = op_end(); i != e; ++i) 00146 i->set(0); 00147 } 00148 00149 /// replaceUsesOfWith - Replaces all references to the "From" definition with 00150 /// references to the "To" definition. 00151 /// 00152 void replaceUsesOfWith(Value *From, Value *To); 00153 00154 // Methods for support type inquiry through isa, cast, and dyn_cast: 00155 static inline bool classof(const User *) { return true; } 00156 static inline bool classof(const Value *V) { 00157 return isa<Instruction>(V) || isa<Constant>(V); 00158 } 00159 }; 00160 00161 inline Use *OperandTraits<User>::op_begin(User *U) { 00162 return U->op_begin(); 00163 } 00164 00165 inline Use *OperandTraits<User>::op_end(User *U) { 00166 return U->op_end(); 00167 } 00168 00169 inline unsigned OperandTraits<User>::operands(const User *U) { 00170 return U->getNumOperands(); 00171 } 00172 00173 template<> struct simplify_type<User::op_iterator> { 00174 typedef Value* SimpleType; 00175 00176 static SimpleType getSimplifiedValue(const User::op_iterator &Val) { 00177 return static_cast<SimpleType>(Val->get()); 00178 } 00179 }; 00180 00181 template<> struct simplify_type<const User::op_iterator> 00182 : public simplify_type<User::op_iterator> {}; 00183 00184 template<> struct simplify_type<User::const_op_iterator> { 00185 typedef Value* SimpleType; 00186 00187 static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) { 00188 return static_cast<SimpleType>(Val->get()); 00189 } 00190 }; 00191 00192 template<> struct simplify_type<const User::const_op_iterator> 00193 : public simplify_type<User::const_op_iterator> {}; 00194 00195 00196 // value_use_iterator::getOperandNo - Requires the definition of the User class. 00197 template<typename UserTy> 00198 unsigned value_use_iterator<UserTy>::getOperandNo() const { 00199 return U - U->getUser()->op_begin(); 00200 } 00201 00202 } // End llvm namespace 00203 00204 #endif