LLVM API Documentation
00001 //===-- llvm/Use.h - Definition of the Use class ----------------*- 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 defines the Use class. The Use class represents the operand of an 00011 // instruction or some other User instance which refers to a Value. The Use 00012 // class keeps the "use list" of the referenced value up to date. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef LLVM_USE_H 00017 #define LLVM_USE_H 00018 00019 #include "llvm/Support/Casting.h" 00020 #include "llvm/ADT/iterator.h" 00021 #include "llvm/ADT/PointerIntPair.h" 00022 00023 namespace llvm { 00024 00025 class Value; 00026 class User; 00027 00028 00029 /// Tag - generic tag type for (at least 32 bit) pointers 00030 enum Tag { noTag, tagOne, tagTwo, tagThree }; 00031 00032 00033 //===----------------------------------------------------------------------===// 00034 // Use Class 00035 //===----------------------------------------------------------------------===// 00036 00037 /// Use is here to make keeping the "use" list of a Value up-to-date really 00038 /// easy. 00039 class Use { 00040 public: 00041 /// swap - provide a fast substitute to std::swap<Use> 00042 /// that also works with less standard-compliant compilers 00043 void swap(Use &RHS); 00044 00045 private: 00046 /// Copy ctor - do not implement 00047 Use(const Use &U); 00048 00049 /// Destructor - Only for zap() 00050 inline ~Use() { 00051 if (Val) removeFromList(); 00052 } 00053 00054 /// Default ctor - This leaves the Use completely uninitialized. The only 00055 /// thing that is valid to do with this use is to call the "init" method. 00056 inline Use() {} 00057 enum PrevPtrTag { zeroDigitTag = noTag 00058 , oneDigitTag = tagOne 00059 , stopTag = tagTwo 00060 , fullStopTag = tagThree }; 00061 00062 public: 00063 /// Normally Use will just implicitly convert to a Value* that it holds. 00064 operator Value*() const { return Val; } 00065 00066 /// If implicit conversion to Value* doesn't work, the get() method returns 00067 /// the Value*. 00068 Value *get() const { return Val; } 00069 00070 /// getUser - This returns the User that contains this Use. For an 00071 /// instruction operand, for example, this will return the instruction. 00072 User *getUser() const; 00073 00074 inline void set(Value *Val); 00075 00076 Value *operator=(Value *RHS) { 00077 set(RHS); 00078 return RHS; 00079 } 00080 const Use &operator=(const Use &RHS) { 00081 set(RHS.Val); 00082 return *this; 00083 } 00084 00085 Value *operator->() { return Val; } 00086 const Value *operator->() const { return Val; } 00087 00088 Use *getNext() const { return Next; } 00089 00090 00091 /// zap - This is used to destroy Use operands when the number of operands of 00092 /// a User changes. 00093 static void zap(Use *Start, const Use *Stop, bool del = false); 00094 00095 private: 00096 const Use* getImpliedUser() const; 00097 static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0); 00098 00099 Value *Val; 00100 Use *Next; 00101 PointerIntPair<Use**, 2, PrevPtrTag> Prev; 00102 00103 void setPrev(Use **NewPrev) { 00104 Prev.setPointer(NewPrev); 00105 } 00106 void addToList(Use **List) { 00107 Next = *List; 00108 if (Next) Next->setPrev(&Next); 00109 setPrev(List); 00110 *List = this; 00111 } 00112 void removeFromList() { 00113 Use **StrippedPrev = Prev.getPointer(); 00114 *StrippedPrev = Next; 00115 if (Next) Next->setPrev(StrippedPrev); 00116 } 00117 00118 friend class Value; 00119 friend class User; 00120 }; 00121 00122 // simplify_type - Allow clients to treat uses just like values when using 00123 // casting operators. 00124 template<> struct simplify_type<Use> { 00125 typedef Value* SimpleType; 00126 static SimpleType getSimplifiedValue(const Use &Val) { 00127 return static_cast<SimpleType>(Val.get()); 00128 } 00129 }; 00130 template<> struct simplify_type<const Use> { 00131 typedef Value* SimpleType; 00132 static SimpleType getSimplifiedValue(const Use &Val) { 00133 return static_cast<SimpleType>(Val.get()); 00134 } 00135 }; 00136 00137 00138 00139 template<typename UserTy> // UserTy == 'User' or 'const User' 00140 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> { 00141 typedef forward_iterator<UserTy*, ptrdiff_t> super; 00142 typedef value_use_iterator<UserTy> _Self; 00143 00144 Use *U; 00145 explicit value_use_iterator(Use *u) : U(u) {} 00146 friend class Value; 00147 public: 00148 typedef typename super::reference reference; 00149 typedef typename super::pointer pointer; 00150 00151 value_use_iterator(const _Self &I) : U(I.U) {} 00152 value_use_iterator() {} 00153 00154 bool operator==(const _Self &x) const { 00155 return U == x.U; 00156 } 00157 bool operator!=(const _Self &x) const { 00158 return !operator==(x); 00159 } 00160 00161 /// atEnd - return true if this iterator is equal to use_end() on the value. 00162 bool atEnd() const { return U == 0; } 00163 00164 // Iterator traversal: forward iteration only 00165 _Self &operator++() { // Preincrement 00166 assert(U && "Cannot increment end iterator!"); 00167 U = U->getNext(); 00168 return *this; 00169 } 00170 _Self operator++(int) { // Postincrement 00171 _Self tmp = *this; ++*this; return tmp; 00172 } 00173 00174 // Retrieve a pointer to the current User. 00175 UserTy *operator*() const { 00176 assert(U && "Cannot dereference end iterator!"); 00177 return U->getUser(); 00178 } 00179 00180 UserTy *operator->() const { return operator*(); } 00181 00182 Use &getUse() const { return *U; } 00183 00184 /// getOperandNo - Return the operand # of this use in its User. Defined in 00185 /// User.h 00186 /// 00187 unsigned getOperandNo() const; 00188 }; 00189 00190 00191 template<> struct simplify_type<value_use_iterator<User> > { 00192 typedef User* SimpleType; 00193 00194 static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) { 00195 return *Val; 00196 } 00197 }; 00198 00199 template<> struct simplify_type<const value_use_iterator<User> > 00200 : public simplify_type<value_use_iterator<User> > {}; 00201 00202 template<> struct simplify_type<value_use_iterator<const User> > { 00203 typedef const User* SimpleType; 00204 00205 static SimpleType getSimplifiedValue(const 00206 value_use_iterator<const User> &Val) { 00207 return *Val; 00208 } 00209 }; 00210 00211 template<> struct simplify_type<const value_use_iterator<const User> > 00212 : public simplify_type<value_use_iterator<const User> > {}; 00213 00214 } // End llvm namespace 00215 00216 #endif
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.