1//===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This defines the Use class. The Use class represents the operand of an 11// instruction or some other User instance which refers to a Value. The Use 12// class keeps the "use list" of the referenced value up to date. 13// 14// Pointer tagging is used to efficiently find the User corresponding 15// to a Use without having to store a User pointer in every Use. A 16// User is preceded in memory by all the Uses corresponding to its 17// operands, and the low bits of one of the fields (Prev) of the Use 18// class are used to encode offsets to be able to find that User given 19// a pointer to any Use. For details, see: 20// 21// http://www.llvm.org/docs/ProgrammersManual.html#UserLayout 22// 23//===----------------------------------------------------------------------===// 24 25#ifndef LLVM_USE_H 26#define LLVM_USE_H 27 28#include "llvm/ADT/PointerIntPair.h" 29#include "llvm/Support/Compiler.h" 30#include <cstddef> 31#include <iterator> 32 33namespace llvm { 34 35class Value; 36class User; 37class Use; 38template<typename> 39struct simplify_type; 40 41// Use** is only 4-byte aligned. 42template<> 43class PointerLikeTypeTraits<Use**> { 44public: 45 static inline void *getAsVoidPointer(Use** P) { return P; } 46 static inline Use **getFromVoidPointer(void *P) { 47 return static_cast<Use**>(P); 48 } 49 enum { NumLowBitsAvailable = 2 }; 50}; 51 52//===----------------------------------------------------------------------===// 53// Use Class 54//===----------------------------------------------------------------------===// 55 56/// Use is here to make keeping the "use" list of a Value up-to-date really 57/// easy. 58class Use { 59public: 60 /// swap - provide a fast substitute to std::swap<Use> 61 /// that also works with less standard-compliant compilers 62 void swap(Use &RHS); 63 64 // A type for the word following an array of hung-off Uses in memory, which is 65 // a pointer back to their User with the bottom bit set. 66 typedef PointerIntPair<User*, 1, unsigned> UserRef; 67 68private: 69 /// Copy ctor - do not implement 70 Use(const Use &U) LLVM_DELETED_FUNCTION; 71 72 /// Destructor - Only for zap() 73 ~Use() { 74 if (Val) removeFromList(); 75 } 76 77 enum PrevPtrTag { zeroDigitTag 78 , oneDigitTag 79 , stopTag 80 , fullStopTag }; 81 82 /// Constructor 83 Use(PrevPtrTag tag) : Val(0) { 84 Prev.setInt(tag); 85 } 86 87public: 88 /// Normally Use will just implicitly convert to a Value* that it holds. 89 operator Value*() const { return Val; } 90 91 /// If implicit conversion to Value* doesn't work, the get() method returns 92 /// the Value*. 93 Value *get() const { return Val; } 94 95 /// getUser - This returns the User that contains this Use. For an 96 /// instruction operand, for example, this will return the instruction. 97 User *getUser() const; 98 99 inline void set(Value *Val); 100 101 Value *operator=(Value *RHS) { 102 set(RHS); 103 return RHS; 104 } 105 const Use &operator=(const Use &RHS) { 106 set(RHS.Val); 107 return *this; 108 } 109 110 Value *operator->() { return Val; } 111 const Value *operator->() const { return Val; } 112 113 Use *getNext() const { return Next; } 114 115 116 /// initTags - initialize the waymarking tags on an array of Uses, so that 117 /// getUser() can find the User from any of those Uses. 118 static Use *initTags(Use *Start, Use *Stop); 119 120 /// zap - This is used to destroy Use operands when the number of operands of 121 /// a User changes. 122 static void zap(Use *Start, const Use *Stop, bool del = false); 123 124private: 125 const Use* getImpliedUser() const; 126 127 Value *Val; 128 Use *Next; 129 PointerIntPair<Use**, 2, PrevPtrTag> Prev; 130 131 void setPrev(Use **NewPrev) { 132 Prev.setPointer(NewPrev); 133 } 134 void addToList(Use **List) { 135 Next = *List; 136 if (Next) Next->setPrev(&Next); 137 setPrev(List); 138 *List = this; 139 } 140 void removeFromList() { 141 Use **StrippedPrev = Prev.getPointer(); 142 *StrippedPrev = Next; 143 if (Next) Next->setPrev(StrippedPrev); 144 } 145 146 friend class Value; 147}; 148 149// simplify_type - Allow clients to treat uses just like values when using 150// casting operators. 151template<> struct simplify_type<Use> { 152 typedef Value* SimpleType; 153 static SimpleType getSimplifiedValue(const Use &Val) { 154 return static_cast<SimpleType>(Val.get()); 155 } 156}; 157template<> struct simplify_type<const Use> { 158 typedef Value* SimpleType; 159 static SimpleType getSimplifiedValue(const Use &Val) { 160 return static_cast<SimpleType>(Val.get()); 161 } 162}; 163 164 165 166template<typename UserTy> // UserTy == 'User' or 'const User' 167class value_use_iterator : public std::iterator<std::forward_iterator_tag, 168 UserTy*, ptrdiff_t> { 169 typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super; 170 typedef value_use_iterator<UserTy> _Self; 171 172 Use *U; 173 explicit value_use_iterator(Use *u) : U(u) {} 174 friend class Value; 175public: 176 typedef typename super::reference reference; 177 typedef typename super::pointer pointer; 178 179 value_use_iterator(const _Self &I) : U(I.U) {} 180 value_use_iterator() {} 181 182 bool operator==(const _Self &x) const { 183 return U == x.U; 184 } 185 bool operator!=(const _Self &x) const { 186 return !operator==(x); 187 } 188 189 /// atEnd - return true if this iterator is equal to use_end() on the value. 190 bool atEnd() const { return U == 0; } 191 192 // Iterator traversal: forward iteration only 193 _Self &operator++() { // Preincrement 194 assert(U && "Cannot increment end iterator!"); 195 U = U->getNext(); 196 return *this; 197 } 198 _Self operator++(int) { // Postincrement 199 _Self tmp = *this; ++*this; return tmp; 200 } 201 202 // Retrieve a pointer to the current User. 203 UserTy *operator*() const { 204 assert(U && "Cannot dereference end iterator!"); 205 return U->getUser(); 206 } 207 208 UserTy *operator->() const { return operator*(); } 209 210 Use &getUse() const { return *U; } 211 212 /// getOperandNo - Return the operand # of this use in its User. Defined in 213 /// User.h 214 /// 215 unsigned getOperandNo() const; 216}; 217 218} // End llvm namespace 219 220#endif 221