1 //===- llvm/Support/ValueHandle.h - Value Smart Pointer classes -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the ValueHandle class and its sub-classes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_VALUEHANDLE_H
15 #define LLVM_SUPPORT_VALUEHANDLE_H
17 #include "llvm/ADT/PointerIntPair.h"
18 #include "llvm/Value.h"
21 class ValueHandleBase;
23 // ValueHandleBase** is only 4-byte aligned.
25 class PointerLikeTypeTraits<ValueHandleBase**> {
27 static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
28 static inline ValueHandleBase **getFromVoidPointer(void *P) {
29 return static_cast<ValueHandleBase**>(P);
31 enum { NumLowBitsAvailable = 2 };
34 /// ValueHandleBase - This is the common base class of value handles.
35 /// ValueHandle's are smart pointers to Value's that have special behavior when
36 /// the value is deleted or ReplaceAllUsesWith'd. See the specific handles
37 /// below for details.
39 class ValueHandleBase {
42 /// HandleBaseKind - This indicates what sub class the handle actually is.
43 /// This is to avoid having a vtable for the light-weight handle pointers. The
44 /// fully generally Callback version does have a vtable.
52 PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
53 ValueHandleBase *Next;
56 ValueHandleBase(HandleBaseKind Kind) : PrevPair(0, Kind), Next(0), VP(0) {}
57 ValueHandleBase(HandleBaseKind Kind, Value *V)
58 : PrevPair(0, Kind), Next(0), VP(V) {
62 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
63 : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
65 AddToExistingUseList(RHS.getPrevPtr());
72 Value *operator=(Value *RHS) {
73 if (VP == RHS) return RHS;
74 if (VP) RemoveFromUseList();
76 if (VP) AddToUseList();
80 Value *operator=(const ValueHandleBase &RHS) {
81 if (VP == RHS.VP) return RHS.VP;
82 if (VP) RemoveFromUseList();
84 if (VP) AddToExistingUseList(RHS.getPrevPtr());
88 Value *operator->() const { return getValPtr(); }
89 Value &operator*() const { return *getValPtr(); }
92 Value *getValPtr() const { return VP; }
94 // Callbacks made from Value.
95 static void ValueIsDeleted(Value *V);
96 static void ValueIsRAUWd(Value *Old, Value *New);
98 // Internal implementation details.
99 ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
100 HandleBaseKind getKind() const { return PrevPair.getInt(); }
101 void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
103 /// AddToUseList - Add this ValueHandle to the use list for VP, where List is
104 /// known to point into the existing use list.
105 void AddToExistingUseList(ValueHandleBase **List);
107 /// AddToUseList - Add this ValueHandle to the use list for VP.
109 /// RemoveFromUseList - Remove this ValueHandle from its current use list.
110 void RemoveFromUseList();
113 /// WeakVH - This is a value handle that tries hard to point to a Value, even
114 /// across RAUW operations, but will null itself out if the value is destroyed.
115 /// this is useful for advisory sorts of information, but should not be used as
116 /// the key of a map (since the map would have to rearrange itself when the
117 /// pointer changes).
118 class WeakVH : public ValueHandleBase {
120 WeakVH() : ValueHandleBase(Weak) {}
121 WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
122 WeakVH(const WeakVH &RHS)
123 : ValueHandleBase(Weak, RHS) {}
125 operator Value*() const {
130 /// AssertingVH - This is a Value Handle that points to a value and asserts out
131 /// if the value is destroyed while the handle is still live. This is very
132 /// useful for catching dangling pointer bugs and other things which can be
133 /// non-obvious. One particularly useful place to use this is as the Key of a
134 /// map. Dangling pointer bugs often lead to really subtle bugs that only occur
135 /// if another object happens to get allocated to the same address as the old
136 /// one. Using an AssertingVH ensures that an assert is triggered as soon as
137 /// the bad delete occurs.
139 /// Note that an AssertingVH handle does *not* follow values across RAUW
140 /// operations. This means that RAUW's need to explicitly update the
141 /// AssertingVH's as it moves. This is required because in non-assert mode this
142 /// class turns into a trivial wrapper around a pointer.
143 template <typename ValueTy>
146 : public ValueHandleBase
151 ValueTy *getValPtr() const {
152 return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
154 void setValPtr(ValueTy *P) {
155 ValueHandleBase::operator=(P);
159 ValueTy *getValPtr() const { return ThePtr; }
160 void setValPtr(ValueTy *P) { ThePtr = P; }
165 AssertingVH() : ValueHandleBase(Assert) {}
166 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, P) {}
167 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
169 AssertingVH() : ThePtr(0) {}
170 AssertingVH(ValueTy *P) : ThePtr(P) {}
173 operator ValueTy*() const {
177 ValueTy *operator=(ValueTy *RHS) {
181 ValueTy *operator=(AssertingVH<ValueTy> &RHS) {
182 setValPtr(RHS.getValPtr());
186 ValueTy *operator->() const { return getValPtr(); }
187 ValueTy &operator*() const { return *getValPtr(); }
190 } // End llvm namespace