1 //===- llvm/Support/ValueHandle.h - Value Smart Pointer classes -*- 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 file declares the ValueHandle class and its sub-classes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_SUPPORT_VALUEHANDLE_H 15 #define LLVM_SUPPORT_VALUEHANDLE_H 16 17 #include "llvm/ADT/DenseMapInfo.h" 18 #include "llvm/ADT/PointerIntPair.h" 19 #include "llvm/Value.h" 20 21 namespace llvm { 22 class ValueHandleBase; 23 24 // ValueHandleBase** is only 4-byte aligned. 25 template<> 26 class PointerLikeTypeTraits<ValueHandleBase**> { 27 public: getAsVoidPointer(ValueHandleBase ** P)28 static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; } getFromVoidPointer(void * P)29 static inline ValueHandleBase **getFromVoidPointer(void *P) { 30 return static_cast<ValueHandleBase**>(P); 31 } 32 enum { NumLowBitsAvailable = 2 }; 33 }; 34 35 /// ValueHandleBase - This is the common base class of value handles. 36 /// ValueHandle's are smart pointers to Value's that have special behavior when 37 /// the value is deleted or ReplaceAllUsesWith'd. See the specific handles 38 /// below for details. 39 /// 40 class ValueHandleBase { 41 friend class Value; 42 protected: 43 /// HandleBaseKind - This indicates what sub class the handle actually is. 44 /// This is to avoid having a vtable for the light-weight handle pointers. The 45 /// fully general Callback version does have a vtable. 46 enum HandleBaseKind { 47 Assert, 48 Callback, 49 Tracking, 50 Weak 51 }; 52 private: 53 54 PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair; 55 ValueHandleBase *Next; 56 Value *VP; 57 58 explicit ValueHandleBase(const ValueHandleBase&); // DO NOT IMPLEMENT. 59 public: ValueHandleBase(HandleBaseKind Kind)60 explicit ValueHandleBase(HandleBaseKind Kind) 61 : PrevPair(0, Kind), Next(0), VP(0) {} ValueHandleBase(HandleBaseKind Kind,Value * V)62 ValueHandleBase(HandleBaseKind Kind, Value *V) 63 : PrevPair(0, Kind), Next(0), VP(V) { 64 if (isValid(VP)) 65 AddToUseList(); 66 } ValueHandleBase(HandleBaseKind Kind,const ValueHandleBase & RHS)67 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS) 68 : PrevPair(0, Kind), Next(0), VP(RHS.VP) { 69 if (isValid(VP)) 70 AddToExistingUseList(RHS.getPrevPtr()); 71 } ~ValueHandleBase()72 ~ValueHandleBase() { 73 if (isValid(VP)) 74 RemoveFromUseList(); 75 } 76 77 Value *operator=(Value *RHS) { 78 if (VP == RHS) return RHS; 79 if (isValid(VP)) RemoveFromUseList(); 80 VP = RHS; 81 if (isValid(VP)) AddToUseList(); 82 return RHS; 83 } 84 85 Value *operator=(const ValueHandleBase &RHS) { 86 if (VP == RHS.VP) return RHS.VP; 87 if (isValid(VP)) RemoveFromUseList(); 88 VP = RHS.VP; 89 if (isValid(VP)) AddToExistingUseList(RHS.getPrevPtr()); 90 return VP; 91 } 92 93 Value *operator->() const { return getValPtr(); } 94 Value &operator*() const { return *getValPtr(); } 95 96 protected: getValPtr()97 Value *getValPtr() const { return VP; } isValid(Value * V)98 static bool isValid(Value *V) { 99 return V && 100 V != DenseMapInfo<Value *>::getEmptyKey() && 101 V != DenseMapInfo<Value *>::getTombstoneKey(); 102 } 103 104 private: 105 // Callbacks made from Value. 106 static void ValueIsDeleted(Value *V); 107 static void ValueIsRAUWd(Value *Old, Value *New); 108 109 // Internal implementation details. getPrevPtr()110 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); } getKind()111 HandleBaseKind getKind() const { return PrevPair.getInt(); } setPrevPtr(ValueHandleBase ** Ptr)112 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); } 113 114 /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where 115 /// List is the address of either the head of the list or a Next node within 116 /// the existing use list. 117 void AddToExistingUseList(ValueHandleBase **List); 118 119 /// AddToExistingUseListAfter - Add this ValueHandle to the use list after 120 /// Node. 121 void AddToExistingUseListAfter(ValueHandleBase *Node); 122 123 /// AddToUseList - Add this ValueHandle to the use list for VP. 124 void AddToUseList(); 125 /// RemoveFromUseList - Remove this ValueHandle from its current use list. 126 void RemoveFromUseList(); 127 }; 128 129 /// WeakVH - This is a value handle that tries hard to point to a Value, even 130 /// across RAUW operations, but will null itself out if the value is destroyed. 131 /// this is useful for advisory sorts of information, but should not be used as 132 /// the key of a map (since the map would have to rearrange itself when the 133 /// pointer changes). 134 class WeakVH : public ValueHandleBase { 135 public: WeakVH()136 WeakVH() : ValueHandleBase(Weak) {} WeakVH(Value * P)137 WeakVH(Value *P) : ValueHandleBase(Weak, P) {} WeakVH(const WeakVH & RHS)138 WeakVH(const WeakVH &RHS) 139 : ValueHandleBase(Weak, RHS) {} 140 141 Value *operator=(Value *RHS) { 142 return ValueHandleBase::operator=(RHS); 143 } 144 Value *operator=(const ValueHandleBase &RHS) { 145 return ValueHandleBase::operator=(RHS); 146 } 147 148 operator Value*() const { 149 return getValPtr(); 150 } 151 }; 152 153 // Specialize simplify_type to allow WeakVH to participate in 154 // dyn_cast, isa, etc. 155 template<typename From> struct simplify_type; 156 template<> struct simplify_type<const WeakVH> { 157 typedef Value* SimpleType; 158 static SimpleType getSimplifiedValue(const WeakVH &WVH) { 159 return static_cast<Value *>(WVH); 160 } 161 }; 162 template<> struct simplify_type<WeakVH> : public simplify_type<const WeakVH> {}; 163 164 /// AssertingVH - This is a Value Handle that points to a value and asserts out 165 /// if the value is destroyed while the handle is still live. This is very 166 /// useful for catching dangling pointer bugs and other things which can be 167 /// non-obvious. One particularly useful place to use this is as the Key of a 168 /// map. Dangling pointer bugs often lead to really subtle bugs that only occur 169 /// if another object happens to get allocated to the same address as the old 170 /// one. Using an AssertingVH ensures that an assert is triggered as soon as 171 /// the bad delete occurs. 172 /// 173 /// Note that an AssertingVH handle does *not* follow values across RAUW 174 /// operations. This means that RAUW's need to explicitly update the 175 /// AssertingVH's as it moves. This is required because in non-assert mode this 176 /// class turns into a trivial wrapper around a pointer. 177 template <typename ValueTy> 178 class AssertingVH 179 #ifndef NDEBUG 180 : public ValueHandleBase 181 #endif 182 { 183 184 #ifndef NDEBUG 185 ValueTy *getValPtr() const { 186 return static_cast<ValueTy*>(ValueHandleBase::getValPtr()); 187 } 188 void setValPtr(ValueTy *P) { 189 ValueHandleBase::operator=(GetAsValue(P)); 190 } 191 #else 192 ValueTy *ThePtr; 193 ValueTy *getValPtr() const { return ThePtr; } 194 void setValPtr(ValueTy *P) { ThePtr = P; } 195 #endif 196 197 // Convert a ValueTy*, which may be const, to the type the base 198 // class expects. 199 static Value *GetAsValue(Value *V) { return V; } 200 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); } 201 202 public: 203 #ifndef NDEBUG 204 AssertingVH() : ValueHandleBase(Assert) {} 205 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {} 206 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {} 207 #else 208 AssertingVH() : ThePtr(0) {} 209 AssertingVH(ValueTy *P) : ThePtr(P) {} 210 #endif 211 212 operator ValueTy*() const { 213 return getValPtr(); 214 } 215 216 ValueTy *operator=(ValueTy *RHS) { 217 setValPtr(RHS); 218 return getValPtr(); 219 } 220 ValueTy *operator=(const AssertingVH<ValueTy> &RHS) { 221 setValPtr(RHS.getValPtr()); 222 return getValPtr(); 223 } 224 225 ValueTy *operator->() const { return getValPtr(); } 226 ValueTy &operator*() const { return *getValPtr(); } 227 }; 228 229 // Specialize simplify_type to allow AssertingVH to participate in 230 // dyn_cast, isa, etc. 231 template<typename From> struct simplify_type; 232 template<> struct simplify_type<const AssertingVH<Value> > { 233 typedef Value* SimpleType; 234 static SimpleType getSimplifiedValue(const AssertingVH<Value> &AVH) { 235 return static_cast<Value *>(AVH); 236 } 237 }; 238 template<> struct simplify_type<AssertingVH<Value> > 239 : public simplify_type<const AssertingVH<Value> > {}; 240 241 // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap. 242 template<typename T> 243 struct DenseMapInfo<AssertingVH<T> > { 244 typedef DenseMapInfo<T*> PointerInfo; 245 static inline AssertingVH<T> getEmptyKey() { 246 return AssertingVH<T>(PointerInfo::getEmptyKey()); 247 } 248 static inline T* getTombstoneKey() { 249 return AssertingVH<T>(PointerInfo::getTombstoneKey()); 250 } 251 static unsigned getHashValue(const AssertingVH<T> &Val) { 252 return PointerInfo::getHashValue(Val); 253 } 254 static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) { 255 return LHS == RHS; 256 } 257 }; 258 259 template <typename T> 260 struct isPodLike<AssertingVH<T> > { 261 #ifdef NDEBUG 262 static const bool value = true; 263 #else 264 static const bool value = false; 265 #endif 266 }; 267 268 269 /// TrackingVH - This is a value handle that tracks a Value (or Value subclass), 270 /// even across RAUW operations. 271 /// 272 /// TrackingVH is designed for situations where a client needs to hold a handle 273 /// to a Value (or subclass) across some operations which may move that value, 274 /// but should never destroy it or replace it with some unacceptable type. 275 /// 276 /// It is an error to do anything with a TrackingVH whose value has been 277 /// destroyed, except to destruct it. 278 /// 279 /// It is an error to attempt to replace a value with one of a type which is 280 /// incompatible with any of its outstanding TrackingVHs. 281 template<typename ValueTy> 282 class TrackingVH : public ValueHandleBase { 283 void CheckValidity() const { 284 Value *VP = ValueHandleBase::getValPtr(); 285 286 // Null is always ok. 287 if (!VP) return; 288 289 // Check that this value is valid (i.e., it hasn't been deleted). We 290 // explicitly delay this check until access to avoid requiring clients to be 291 // unnecessarily careful w.r.t. destruction. 292 assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!"); 293 294 // Check that the value is a member of the correct subclass. We would like 295 // to check this property on assignment for better debugging, but we don't 296 // want to require a virtual interface on this VH. Instead we allow RAUW to 297 // replace this value with a value of an invalid type, and check it here. 298 assert(isa<ValueTy>(VP) && 299 "Tracked Value was replaced by one with an invalid type!"); 300 } 301 302 ValueTy *getValPtr() const { 303 CheckValidity(); 304 return (ValueTy*)ValueHandleBase::getValPtr(); 305 } 306 void setValPtr(ValueTy *P) { 307 CheckValidity(); 308 ValueHandleBase::operator=(GetAsValue(P)); 309 } 310 311 // Convert a ValueTy*, which may be const, to the type the base 312 // class expects. 313 static Value *GetAsValue(Value *V) { return V; } 314 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); } 315 316 public: 317 TrackingVH() : ValueHandleBase(Tracking) {} 318 TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {} 319 TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {} 320 321 operator ValueTy*() const { 322 return getValPtr(); 323 } 324 325 ValueTy *operator=(ValueTy *RHS) { 326 setValPtr(RHS); 327 return getValPtr(); 328 } 329 ValueTy *operator=(const TrackingVH<ValueTy> &RHS) { 330 setValPtr(RHS.getValPtr()); 331 return getValPtr(); 332 } 333 334 ValueTy *operator->() const { return getValPtr(); } 335 ValueTy &operator*() const { return *getValPtr(); } 336 }; 337 338 // Specialize simplify_type to allow TrackingVH to participate in 339 // dyn_cast, isa, etc. 340 template<typename From> struct simplify_type; 341 template<> struct simplify_type<const TrackingVH<Value> > { 342 typedef Value* SimpleType; 343 static SimpleType getSimplifiedValue(const TrackingVH<Value> &AVH) { 344 return static_cast<Value *>(AVH); 345 } 346 }; 347 template<> struct simplify_type<TrackingVH<Value> > 348 : public simplify_type<const TrackingVH<Value> > {}; 349 350 /// CallbackVH - This is a value handle that allows subclasses to define 351 /// callbacks that run when the underlying Value has RAUW called on it or is 352 /// destroyed. This class can be used as the key of a map, as long as the user 353 /// takes it out of the map before calling setValPtr() (since the map has to 354 /// rearrange itself when the pointer changes). Unlike ValueHandleBase, this 355 /// class has a vtable and a virtual destructor. 356 class CallbackVH : public ValueHandleBase { 357 protected: 358 CallbackVH(const CallbackVH &RHS) 359 : ValueHandleBase(Callback, RHS) {} 360 361 virtual ~CallbackVH(); 362 363 void setValPtr(Value *P) { 364 ValueHandleBase::operator=(P); 365 } 366 367 public: 368 CallbackVH() : ValueHandleBase(Callback) {} 369 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {} 370 371 operator Value*() const { 372 return getValPtr(); 373 } 374 375 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may 376 /// call any non-virtual Value method on getValPtr(), but no subclass methods. 377 /// If WeakVH were implemented as a CallbackVH, it would use this method to 378 /// call setValPtr(NULL). AssertingVH would use this method to cause an 379 /// assertion failure. 380 /// 381 /// All implementations must remove the reference from this object to the 382 /// Value that's being destroyed. 383 virtual void deleted() { 384 setValPtr(NULL); 385 } 386 387 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called, 388 /// _before_ any of the uses have actually been replaced. If WeakVH were 389 /// implemented as a CallbackVH, it would use this method to call 390 /// setValPtr(new_value). AssertingVH would do nothing in this method. 391 virtual void allUsesReplacedWith(Value *) {} 392 }; 393 394 // Specialize simplify_type to allow CallbackVH to participate in 395 // dyn_cast, isa, etc. 396 template<typename From> struct simplify_type; 397 template<> struct simplify_type<const CallbackVH> { 398 typedef Value* SimpleType; 399 static SimpleType getSimplifiedValue(const CallbackVH &CVH) { 400 return static_cast<Value *>(CVH); 401 } 402 }; 403 template<> struct simplify_type<CallbackVH> 404 : public simplify_type<const CallbackVH> {}; 405 406 } // End llvm namespace 407 408 #endif 409