1 //===- ValueHandle.h - Value Smart Pointer classes --------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file declares the ValueHandle class and its sub-classes. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_IR_VALUEHANDLE_H 14 #define LLVM_IR_VALUEHANDLE_H 15 16 #include "llvm/ADT/DenseMapInfo.h" 17 #include "llvm/ADT/PointerIntPair.h" 18 #include "llvm/IR/Value.h" 19 #include "llvm/Support/Casting.h" 20 #include "llvm/Support/Compiler.h" 21 #include <cassert> 22 23 namespace llvm { 24 25 /// This is the common base class of value handles. 26 /// 27 /// ValueHandle's are smart pointers to Value's that have special behavior when 28 /// the value is deleted or ReplaceAllUsesWith'd. See the specific handles 29 /// below for details. 30 class ValueHandleBase { 31 friend class Value; 32 33 protected: 34 /// This indicates what sub class the handle actually is. 35 /// 36 /// This is to avoid having a vtable for the light-weight handle pointers. The 37 /// fully general Callback version does have a vtable. 38 enum HandleBaseKind { Assert, Callback, Weak, WeakTracking }; 39 ValueHandleBase(const ValueHandleBase & RHS)40 ValueHandleBase(const ValueHandleBase &RHS) 41 : ValueHandleBase(RHS.PrevPair.getInt(), RHS) {} 42 ValueHandleBase(HandleBaseKind Kind,const ValueHandleBase & RHS)43 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS) 44 : PrevPair(nullptr, Kind), Val(RHS.getValPtr()) { 45 if (isValid(getValPtr())) 46 AddToExistingUseList(RHS.getPrevPtr()); 47 } 48 49 private: 50 PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair; 51 ValueHandleBase *Next = nullptr; 52 Value *Val = nullptr; 53 setValPtr(Value * V)54 void setValPtr(Value *V) { Val = V; } 55 56 public: ValueHandleBase(HandleBaseKind Kind)57 explicit ValueHandleBase(HandleBaseKind Kind) 58 : PrevPair(nullptr, Kind) {} ValueHandleBase(HandleBaseKind Kind,Value * V)59 ValueHandleBase(HandleBaseKind Kind, Value *V) 60 : PrevPair(nullptr, Kind), Val(V) { 61 if (isValid(getValPtr())) 62 AddToUseList(); 63 } 64 ~ValueHandleBase()65 ~ValueHandleBase() { 66 if (isValid(getValPtr())) 67 RemoveFromUseList(); 68 } 69 70 Value *operator=(Value *RHS) { 71 if (getValPtr() == RHS) 72 return RHS; 73 if (isValid(getValPtr())) 74 RemoveFromUseList(); 75 setValPtr(RHS); 76 if (isValid(getValPtr())) 77 AddToUseList(); 78 return RHS; 79 } 80 81 Value *operator=(const ValueHandleBase &RHS) { 82 if (getValPtr() == RHS.getValPtr()) 83 return RHS.getValPtr(); 84 if (isValid(getValPtr())) 85 RemoveFromUseList(); 86 setValPtr(RHS.getValPtr()); 87 if (isValid(getValPtr())) 88 AddToExistingUseList(RHS.getPrevPtr()); 89 return getValPtr(); 90 } 91 92 Value *operator->() const { return getValPtr(); } 93 Value &operator*() const { 94 Value *V = getValPtr(); 95 assert(V && "Dereferencing deleted ValueHandle"); 96 return *V; 97 } 98 99 protected: getValPtr()100 Value *getValPtr() const { return Val; } 101 isValid(Value * V)102 static bool isValid(Value *V) { 103 return V && 104 V != DenseMapInfo<Value *>::getEmptyKey() && 105 V != DenseMapInfo<Value *>::getTombstoneKey(); 106 } 107 108 /// Remove this ValueHandle from its current use list. 109 LLVM_ABI void RemoveFromUseList(); 110 111 /// Clear the underlying pointer without clearing the use list. 112 /// 113 /// This should only be used if a derived class has manually removed the 114 /// handle from the use list. clearValPtr()115 void clearValPtr() { setValPtr(nullptr); } 116 117 public: 118 // Callbacks made from Value. 119 LLVM_ABI static void ValueIsDeleted(Value *V); 120 LLVM_ABI static void ValueIsRAUWd(Value *Old, Value *New); 121 122 private: 123 // Internal implementation details. getPrevPtr()124 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); } getKind()125 HandleBaseKind getKind() const { return PrevPair.getInt(); } setPrevPtr(ValueHandleBase ** Ptr)126 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); } 127 128 /// Add this ValueHandle to the use list for V. 129 /// 130 /// List is the address of either the head of the list or a Next node within 131 /// the existing use list. 132 LLVM_ABI void AddToExistingUseList(ValueHandleBase **List); 133 134 /// Add this ValueHandle to the use list after Node. 135 void AddToExistingUseListAfter(ValueHandleBase *Node); 136 137 /// Add this ValueHandle to the use list for V. 138 LLVM_ABI void AddToUseList(); 139 }; 140 141 /// A nullable Value handle that is nullable. 142 /// 143 /// This is a value handle that points to a value, and nulls itself 144 /// out if that value is deleted. 145 class WeakVH : public ValueHandleBase { 146 public: WeakVH()147 WeakVH() : ValueHandleBase(Weak) {} WeakVH(Value * P)148 WeakVH(Value *P) : ValueHandleBase(Weak, P) {} WeakVH(const WeakVH & RHS)149 WeakVH(const WeakVH &RHS) 150 : ValueHandleBase(Weak, RHS) {} 151 152 WeakVH &operator=(const WeakVH &RHS) = default; 153 154 Value *operator=(Value *RHS) { 155 return ValueHandleBase::operator=(RHS); 156 } 157 Value *operator=(const ValueHandleBase &RHS) { 158 return ValueHandleBase::operator=(RHS); 159 } 160 161 operator Value*() const { 162 return getValPtr(); 163 } 164 }; 165 166 // Specialize simplify_type to allow WeakVH to participate in 167 // dyn_cast, isa, etc. 168 template <> struct simplify_type<WeakVH> { 169 using SimpleType = Value *; 170 171 static SimpleType getSimplifiedValue(WeakVH &WVH) { return WVH; } 172 }; 173 template <> struct simplify_type<const WeakVH> { 174 using SimpleType = Value *; 175 176 static SimpleType getSimplifiedValue(const WeakVH &WVH) { return WVH; } 177 }; 178 179 // Specialize DenseMapInfo to allow WeakVH to participate in DenseMap. 180 template <> struct DenseMapInfo<WeakVH> { 181 static inline WeakVH getEmptyKey() { 182 return WeakVH(DenseMapInfo<Value *>::getEmptyKey()); 183 } 184 185 static inline WeakVH getTombstoneKey() { 186 return WeakVH(DenseMapInfo<Value *>::getTombstoneKey()); 187 } 188 189 static unsigned getHashValue(const WeakVH &Val) { 190 return DenseMapInfo<Value *>::getHashValue(Val); 191 } 192 193 static bool isEqual(const WeakVH &LHS, const WeakVH &RHS) { 194 return DenseMapInfo<Value *>::isEqual(LHS, RHS); 195 } 196 }; 197 198 /// Value handle that is nullable, but tries to track the Value. 199 /// 200 /// This is a value handle that tries hard to point to a Value, even across 201 /// RAUW operations, but will null itself out if the value is destroyed. this 202 /// is useful for advisory sorts of information, but should not be used as the 203 /// key of a map (since the map would have to rearrange itself when the pointer 204 /// changes). 205 class WeakTrackingVH : public ValueHandleBase { 206 public: 207 WeakTrackingVH() : ValueHandleBase(WeakTracking) {} 208 WeakTrackingVH(Value *P) : ValueHandleBase(WeakTracking, P) {} 209 WeakTrackingVH(const WeakTrackingVH &RHS) 210 : ValueHandleBase(WeakTracking, RHS) {} 211 212 WeakTrackingVH &operator=(const WeakTrackingVH &RHS) = default; 213 214 Value *operator=(Value *RHS) { 215 return ValueHandleBase::operator=(RHS); 216 } 217 Value *operator=(const ValueHandleBase &RHS) { 218 return ValueHandleBase::operator=(RHS); 219 } 220 221 operator Value*() const { 222 return getValPtr(); 223 } 224 225 bool pointsToAliveValue() const { 226 return ValueHandleBase::isValid(getValPtr()); 227 } 228 }; 229 230 // Specialize simplify_type to allow WeakTrackingVH to participate in 231 // dyn_cast, isa, etc. 232 template <> struct simplify_type<WeakTrackingVH> { 233 using SimpleType = Value *; 234 235 static SimpleType getSimplifiedValue(WeakTrackingVH &WVH) { return WVH; } 236 }; 237 template <> struct simplify_type<const WeakTrackingVH> { 238 using SimpleType = Value *; 239 240 static SimpleType getSimplifiedValue(const WeakTrackingVH &WVH) { 241 return WVH; 242 } 243 }; 244 245 /// Value handle that asserts if the Value is deleted. 246 /// 247 /// This is a Value Handle that points to a value and asserts out if the value 248 /// is destroyed while the handle is still live. This is very useful for 249 /// catching dangling pointer bugs and other things which can be non-obvious. 250 /// One particularly useful place to use this is as the Key of a map. Dangling 251 /// pointer bugs often lead to really subtle bugs that only occur if another 252 /// object happens to get allocated to the same address as the old one. Using 253 /// an AssertingVH ensures that an assert is triggered as soon as the bad 254 /// delete occurs. 255 /// 256 /// Note that an AssertingVH handle does *not* follow values across RAUW 257 /// operations. This means that RAUW's need to explicitly update the 258 /// AssertingVH's as it moves. This is required because in non-assert mode this 259 /// class turns into a trivial wrapper around a pointer. 260 template <typename ValueTy> 261 class AssertingVH 262 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 263 : public ValueHandleBase 264 #endif 265 { 266 friend struct DenseMapInfo<AssertingVH<ValueTy>>; 267 268 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 269 Value *getRawValPtr() const { return ValueHandleBase::getValPtr(); } 270 void setRawValPtr(Value *P) { ValueHandleBase::operator=(P); } 271 #else 272 Value *ThePtr; 273 Value *getRawValPtr() const { return ThePtr; } 274 void setRawValPtr(Value *P) { ThePtr = P; } 275 #endif 276 // Convert a ValueTy*, which may be const, to the raw Value*. 277 static Value *GetAsValue(Value *V) { return V; } 278 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); } 279 280 ValueTy *getValPtr() const { return static_cast<ValueTy *>(getRawValPtr()); } 281 void setValPtr(ValueTy *P) { setRawValPtr(GetAsValue(P)); } 282 283 public: 284 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 285 AssertingVH() : ValueHandleBase(Assert) {} 286 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {} 287 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {} 288 #else 289 AssertingVH() : ThePtr(nullptr) {} 290 AssertingVH(ValueTy *P) : ThePtr(GetAsValue(P)) {} 291 AssertingVH(const AssertingVH &) = default; 292 #endif 293 294 operator ValueTy*() const { 295 return getValPtr(); 296 } 297 298 ValueTy *operator=(ValueTy *RHS) { 299 setValPtr(RHS); 300 return getValPtr(); 301 } 302 ValueTy *operator=(const AssertingVH<ValueTy> &RHS) { 303 setValPtr(RHS.getValPtr()); 304 return getValPtr(); 305 } 306 307 ValueTy *operator->() const { return getValPtr(); } 308 ValueTy &operator*() const { return *getValPtr(); } 309 }; 310 311 // Treat AssertingVH<T> like T* inside maps. This also allows using find_as() 312 // to look up a value without constructing a value handle. 313 template<typename T> 314 struct DenseMapInfo<AssertingVH<T>> : DenseMapInfo<T *> {}; 315 316 /// Value handle that tracks a Value across RAUW. 317 /// 318 /// TrackingVH is designed for situations where a client needs to hold a handle 319 /// to a Value (or subclass) across some operations which may move that value, 320 /// but should never destroy it or replace it with some unacceptable type. 321 /// 322 /// It is an error to attempt to replace a value with one of a type which is 323 /// incompatible with any of its outstanding TrackingVHs. 324 /// 325 /// It is an error to read from a TrackingVH that does not point to a valid 326 /// value. A TrackingVH is said to not point to a valid value if either it 327 /// hasn't yet been assigned a value yet or because the value it was tracking 328 /// has since been deleted. 329 /// 330 /// Assigning a value to a TrackingVH is always allowed, even if said TrackingVH 331 /// no longer points to a valid value. 332 template <typename ValueTy> class TrackingVH { 333 WeakTrackingVH InnerHandle; 334 335 public: 336 ValueTy *getValPtr() const { 337 assert(InnerHandle.pointsToAliveValue() && 338 "TrackingVH must be non-null and valid on dereference!"); 339 340 // Check that the value is a member of the correct subclass. We would like 341 // to check this property on assignment for better debugging, but we don't 342 // want to require a virtual interface on this VH. Instead we allow RAUW to 343 // replace this value with a value of an invalid type, and check it here. 344 assert(isa<ValueTy>(InnerHandle) && 345 "Tracked Value was replaced by one with an invalid type!"); 346 return cast<ValueTy>(InnerHandle); 347 } 348 349 void setValPtr(ValueTy *P) { 350 // Assigning to non-valid TrackingVH's are fine so we just unconditionally 351 // assign here. 352 InnerHandle = GetAsValue(P); 353 } 354 355 // Convert a ValueTy*, which may be const, to the type the base 356 // class expects. 357 static Value *GetAsValue(Value *V) { return V; } 358 static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); } 359 360 public: 361 TrackingVH() = default; 362 TrackingVH(ValueTy *P) { setValPtr(P); } 363 364 operator ValueTy*() const { 365 return getValPtr(); 366 } 367 368 ValueTy *operator=(ValueTy *RHS) { 369 setValPtr(RHS); 370 return getValPtr(); 371 } 372 373 ValueTy *operator->() const { return getValPtr(); } 374 ValueTy &operator*() const { return *getValPtr(); } 375 }; 376 377 /// Value handle with callbacks on RAUW and destruction. 378 /// 379 /// This is a value handle that allows subclasses to define callbacks that run 380 /// when the underlying Value has RAUW called on it or is destroyed. This 381 /// class can be used as the key of a map, as long as the user takes it out of 382 /// the map before calling setValPtr() (since the map has to rearrange itself 383 /// when the pointer changes). Unlike ValueHandleBase, this class has a vtable. 384 class LLVM_ABI CallbackVH : public ValueHandleBase { 385 virtual void anchor(); 386 protected: 387 ~CallbackVH() = default; 388 CallbackVH(const CallbackVH &) = default; 389 CallbackVH &operator=(const CallbackVH &) = default; 390 391 void setValPtr(Value *P) { 392 ValueHandleBase::operator=(P); 393 } 394 395 public: 396 CallbackVH() : ValueHandleBase(Callback) {} 397 CallbackVH(Value *P) : ValueHandleBase(Callback, P) {} 398 CallbackVH(const Value *P) : CallbackVH(const_cast<Value *>(P)) {} 399 400 operator Value*() const { 401 return getValPtr(); 402 } 403 404 /// Callback for Value destruction. 405 /// 406 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you 407 /// may call any non-virtual Value method on getValPtr(), but no subclass 408 /// methods. If WeakTrackingVH were implemented as a CallbackVH, it would use 409 /// this 410 /// method to call setValPtr(NULL). AssertingVH would use this method to 411 /// cause an assertion failure. 412 /// 413 /// All implementations must remove the reference from this object to the 414 /// Value that's being destroyed. 415 virtual void deleted() { setValPtr(nullptr); } 416 417 /// Callback for Value RAUW. 418 /// 419 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called, 420 /// _before_ any of the uses have actually been replaced. If WeakTrackingVH 421 /// were 422 /// implemented as a CallbackVH, it would use this method to call 423 /// setValPtr(new_value). AssertingVH would do nothing in this method. 424 virtual void allUsesReplacedWith(Value *) {} 425 }; 426 427 /// Value handle that poisons itself if the Value is deleted. 428 /// 429 /// This is a Value Handle that points to a value and poisons itself if the 430 /// value is destroyed while the handle is still live. This is very useful for 431 /// catching dangling pointer bugs where an \c AssertingVH cannot be used 432 /// because the dangling handle needs to outlive the value without ever being 433 /// used. 434 /// 435 /// One particularly useful place to use this is as the Key of a map. Dangling 436 /// pointer bugs often lead to really subtle bugs that only occur if another 437 /// object happens to get allocated to the same address as the old one. Using 438 /// a PoisoningVH ensures that an assert is triggered if looking up a new value 439 /// in the map finds a handle from the old value. 440 /// 441 /// Note that a PoisoningVH handle does *not* follow values across RAUW 442 /// operations. This means that RAUW's need to explicitly update the 443 /// PoisoningVH's as it moves. This is required because in non-assert mode this 444 /// class turns into a trivial wrapper around a pointer. 445 template <typename ValueTy> 446 class PoisoningVH final 447 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 448 : public CallbackVH 449 #endif 450 { 451 friend struct DenseMapInfo<PoisoningVH<ValueTy>>; 452 453 // Convert a ValueTy*, which may be const, to the raw Value*. 454 static Value *GetAsValue(Value *V) { return V; } 455 static Value *GetAsValue(const Value *V) { return const_cast<Value *>(V); } 456 457 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 458 /// A flag tracking whether this value has been poisoned. 459 /// 460 /// On delete and RAUW, we leave the value pointer alone so that as a raw 461 /// pointer it produces the same value (and we fit into the same key of 462 /// a hash table, etc), but we poison the handle so that any top-level usage 463 /// will fail. 464 bool Poisoned = false; 465 466 Value *getRawValPtr() const { return ValueHandleBase::getValPtr(); } 467 void setRawValPtr(Value *P) { ValueHandleBase::operator=(P); } 468 469 /// Handle deletion by poisoning the handle. 470 void deleted() override { 471 assert(!Poisoned && "Tried to delete an already poisoned handle!"); 472 Poisoned = true; 473 RemoveFromUseList(); 474 } 475 476 /// Handle RAUW by poisoning the handle. 477 void allUsesReplacedWith(Value *) override { 478 assert(!Poisoned && "Tried to RAUW an already poisoned handle!"); 479 Poisoned = true; 480 RemoveFromUseList(); 481 } 482 #else // LLVM_ENABLE_ABI_BREAKING_CHECKS 483 Value *ThePtr = nullptr; 484 485 Value *getRawValPtr() const { return ThePtr; } 486 void setRawValPtr(Value *P) { ThePtr = P; } 487 #endif 488 489 ValueTy *getValPtr() const { 490 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 491 assert(!Poisoned && "Accessed a poisoned value handle!"); 492 #endif 493 return static_cast<ValueTy *>(getRawValPtr()); 494 } 495 void setValPtr(ValueTy *P) { setRawValPtr(GetAsValue(P)); } 496 497 public: 498 PoisoningVH() = default; 499 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 500 PoisoningVH(ValueTy *P) : CallbackVH(GetAsValue(P)) {} 501 PoisoningVH(const PoisoningVH &RHS) 502 : CallbackVH(RHS), Poisoned(RHS.Poisoned) {} 503 504 ~PoisoningVH() { 505 if (Poisoned) 506 clearValPtr(); 507 } 508 509 PoisoningVH &operator=(const PoisoningVH &RHS) { 510 if (Poisoned) 511 clearValPtr(); 512 CallbackVH::operator=(RHS); 513 Poisoned = RHS.Poisoned; 514 return *this; 515 } 516 #else 517 PoisoningVH(ValueTy *P) : ThePtr(GetAsValue(P)) {} 518 #endif 519 520 operator ValueTy *() const { return getValPtr(); } 521 522 ValueTy *operator->() const { return getValPtr(); } 523 ValueTy &operator*() const { return *getValPtr(); } 524 }; 525 526 // Specialize DenseMapInfo to allow PoisoningVH to participate in DenseMap. 527 template <typename T> struct DenseMapInfo<PoisoningVH<T>> { 528 static inline PoisoningVH<T> getEmptyKey() { 529 PoisoningVH<T> Res; 530 Res.setRawValPtr(DenseMapInfo<Value *>::getEmptyKey()); 531 return Res; 532 } 533 534 static inline PoisoningVH<T> getTombstoneKey() { 535 PoisoningVH<T> Res; 536 Res.setRawValPtr(DenseMapInfo<Value *>::getTombstoneKey()); 537 return Res; 538 } 539 540 static unsigned getHashValue(const PoisoningVH<T> &Val) { 541 return DenseMapInfo<Value *>::getHashValue(Val.getRawValPtr()); 542 } 543 544 static bool isEqual(const PoisoningVH<T> &LHS, const PoisoningVH<T> &RHS) { 545 return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(), 546 RHS.getRawValPtr()); 547 } 548 549 // Allow lookup by T* via find_as(), without constructing a temporary 550 // value handle. 551 552 static unsigned getHashValue(const T *Val) { 553 return DenseMapInfo<Value *>::getHashValue(Val); 554 } 555 556 static bool isEqual(const T *LHS, const PoisoningVH<T> &RHS) { 557 return DenseMapInfo<Value *>::isEqual(LHS, RHS.getRawValPtr()); 558 } 559 }; 560 561 } // end namespace llvm 562 563 #endif // LLVM_IR_VALUEHANDLE_H 564