LLVM API Documentation

ValueHandle.h

Go to the documentation of this file.
00001 //===- llvm/Support/ValueHandle.h - Value Smart Pointer classes -*- 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 file declares the ValueHandle class and its sub-classes.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SUPPORT_VALUEHANDLE_H
00015 #define LLVM_SUPPORT_VALUEHANDLE_H
00016 
00017 #include "llvm/ADT/DenseMapInfo.h"
00018 #include "llvm/ADT/PointerIntPair.h"
00019 #include "llvm/Value.h"
00020 
00021 namespace llvm {
00022 class ValueHandleBase;
00023 
00024 // ValueHandleBase** is only 4-byte aligned.
00025 template<>
00026 class PointerLikeTypeTraits<ValueHandleBase**> {
00027 public:
00028   static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
00029   static inline ValueHandleBase **getFromVoidPointer(void *P) {
00030     return static_cast<ValueHandleBase**>(P);
00031   }
00032   enum { NumLowBitsAvailable = 2 };
00033 };
00034 
00035 /// ValueHandleBase - This is the common base class of value handles.
00036 /// ValueHandle's are smart pointers to Value's that have special behavior when
00037 /// the value is deleted or ReplaceAllUsesWith'd.  See the specific handles
00038 /// below for details.
00039 ///
00040 class ValueHandleBase {
00041   friend class Value;
00042 protected:
00043   /// HandleBaseKind - This indicates what sub class the handle actually is.
00044   /// This is to avoid having a vtable for the light-weight handle pointers. The
00045   /// fully general Callback version does have a vtable.
00046   enum HandleBaseKind {
00047     Assert,
00048     Weak,
00049     Callback
00050   };
00051 private:
00052   
00053   PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
00054   ValueHandleBase *Next;
00055   Value *VP;
00056 public:
00057   explicit ValueHandleBase(HandleBaseKind Kind)
00058     : PrevPair(0, Kind), Next(0), VP(0) {}
00059   ValueHandleBase(HandleBaseKind Kind, Value *V)
00060     : PrevPair(0, Kind), Next(0), VP(V) {
00061     if (isValid(VP))
00062       AddToUseList();
00063   }
00064   ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
00065     : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
00066     if (isValid(VP))
00067       AddToExistingUseList(RHS.getPrevPtr());
00068   }
00069   ~ValueHandleBase() {
00070     if (isValid(VP))
00071       RemoveFromUseList();   
00072   }
00073   
00074   Value *operator=(Value *RHS) {
00075     if (VP == RHS) return RHS;
00076     if (isValid(VP)) RemoveFromUseList();
00077     VP = RHS;
00078     if (isValid(VP)) AddToUseList();
00079     return RHS;
00080   }
00081 
00082   Value *operator=(const ValueHandleBase &RHS) {
00083     if (VP == RHS.VP) return RHS.VP;
00084     if (isValid(VP)) RemoveFromUseList();
00085     VP = RHS.VP;
00086     if (isValid(VP)) AddToExistingUseList(RHS.getPrevPtr());
00087     return VP;
00088   }
00089   
00090   Value *operator->() const { return getValPtr(); }
00091   Value &operator*() const { return *getValPtr(); }
00092 
00093 protected:
00094   Value *getValPtr() const { return VP; }
00095 private:
00096   static bool isValid(Value *V) {
00097     return V &&
00098            V != DenseMapInfo<Value *>::getEmptyKey() &&
00099            V != DenseMapInfo<Value *>::getTombstoneKey();
00100   }
00101 
00102   // Callbacks made from Value.
00103   static void ValueIsDeleted(Value *V);
00104   static void ValueIsRAUWd(Value *Old, Value *New);
00105   
00106   // Internal implementation details.
00107   ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
00108   HandleBaseKind getKind() const { return PrevPair.getInt(); }
00109   void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
00110   
00111   /// AddToExistingUseList - Add this ValueHandle to the use list for VP,
00112   /// where List is known to point into the existing use list.
00113   void AddToExistingUseList(ValueHandleBase **List);
00114   
00115   /// AddToUseList - Add this ValueHandle to the use list for VP.
00116   void AddToUseList();
00117   /// RemoveFromUseList - Remove this ValueHandle from its current use list.
00118   void RemoveFromUseList();
00119 };
00120   
00121 /// WeakVH - This is a value handle that tries hard to point to a Value, even
00122 /// across RAUW operations, but will null itself out if the value is destroyed.
00123 /// this is useful for advisory sorts of information, but should not be used as
00124 /// the key of a map (since the map would have to rearrange itself when the
00125 /// pointer changes).
00126 class WeakVH : public ValueHandleBase {
00127 public:
00128   WeakVH() : ValueHandleBase(Weak) {}
00129   WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
00130   WeakVH(const WeakVH &RHS)
00131     : ValueHandleBase(Weak, RHS) {}
00132 
00133   operator Value*() const {
00134     return getValPtr();
00135   }
00136 };
00137 
00138 // Specialize simplify_type to allow WeakVH to participate in
00139 // dyn_cast, isa, etc.
00140 template<typename From> struct simplify_type;
00141 template<> struct simplify_type<const WeakVH> {
00142   typedef Value* SimpleType;
00143   static SimpleType getSimplifiedValue(const WeakVH &WVH) {
00144     return static_cast<Value *>(WVH);
00145   }
00146 };
00147 template<> struct simplify_type<WeakVH> : public simplify_type<const WeakVH> {};
00148 
00149 /// AssertingVH - This is a Value Handle that points to a value and asserts out
00150 /// if the value is destroyed while the handle is still live.  This is very
00151 /// useful for catching dangling pointer bugs and other things which can be
00152 /// non-obvious.  One particularly useful place to use this is as the Key of a
00153 /// map.  Dangling pointer bugs often lead to really subtle bugs that only occur
00154 /// if another object happens to get allocated to the same address as the old
00155 /// one.  Using an AssertingVH ensures that an assert is triggered as soon as
00156 /// the bad delete occurs.
00157 ///
00158 /// Note that an AssertingVH handle does *not* follow values across RAUW
00159 /// operations.  This means that RAUW's need to explicitly update the
00160 /// AssertingVH's as it moves.  This is required because in non-assert mode this
00161 /// class turns into a trivial wrapper around a pointer.
00162 template <typename ValueTy>
00163 class AssertingVH 
00164 #ifndef NDEBUG
00165   : public ValueHandleBase
00166 #endif
00167   {
00168 
00169 #ifndef NDEBUG
00170   ValueTy *getValPtr() const {
00171     return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
00172   }
00173   void setValPtr(ValueTy *P) {
00174     ValueHandleBase::operator=(GetAsValue(P));
00175   }
00176 #else
00177   ValueTy *ThePtr;
00178   ValueTy *getValPtr() const { return ThePtr; }
00179   void setValPtr(ValueTy *P) { ThePtr = P; }
00180 #endif
00181 
00182   // Convert a ValueTy*, which may be const, to the type the base
00183   // class expects.
00184   static Value *GetAsValue(Value *V) { return V; }
00185   static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
00186 
00187 public:
00188 #ifndef NDEBUG
00189   AssertingVH() : ValueHandleBase(Assert) {}
00190   AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
00191   AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
00192 #else
00193   AssertingVH() : ThePtr(0) {}
00194   AssertingVH(ValueTy *P) : ThePtr(P) {}
00195 #endif
00196 
00197   operator ValueTy*() const {
00198     return getValPtr();
00199   }
00200 
00201   ValueTy *operator=(ValueTy *RHS) {
00202     setValPtr(RHS);
00203     return getValPtr();
00204   }
00205   ValueTy *operator=(AssertingVH<ValueTy> &RHS) {
00206     setValPtr(RHS.getValPtr());
00207     return getValPtr();
00208   }
00209 
00210   ValueTy *operator->() const { return getValPtr(); }
00211   ValueTy &operator*() const { return *getValPtr(); }
00212 };
00213 
00214 // Specialize simplify_type to allow AssertingVH to participate in
00215 // dyn_cast, isa, etc.
00216 template<typename From> struct simplify_type;
00217 template<> struct simplify_type<const AssertingVH<Value> > {
00218   typedef Value* SimpleType;
00219   static SimpleType getSimplifiedValue(const AssertingVH<Value> &AVH) {
00220     return static_cast<Value *>(AVH);
00221   }
00222 };
00223 template<> struct simplify_type<AssertingVH<Value> >
00224   : public simplify_type<const AssertingVH<Value> > {};
00225 
00226 /// CallbackVH - This is a value handle that allows subclasses to define
00227 /// callbacks that run when the underlying Value has RAUW called on it or is
00228 /// destroyed.  This class can be used as the key of a map, as long as the user
00229 /// takes it out of the map before calling setValPtr() (since the map has to
00230 /// rearrange itself when the pointer changes).  Unlike ValueHandleBase, this
00231 /// class has a vtable and a virtual destructor.
00232 class CallbackVH : public ValueHandleBase {
00233 protected:
00234   CallbackVH(const CallbackVH &RHS)
00235     : ValueHandleBase(Callback, RHS) {}
00236 
00237   virtual ~CallbackVH();
00238 
00239   void setValPtr(Value *P) {
00240     ValueHandleBase::operator=(P);
00241   }
00242 
00243 public:
00244   CallbackVH() : ValueHandleBase(Callback) {}
00245   CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
00246 
00247   operator Value*() const {
00248     return getValPtr();
00249   }
00250 
00251   /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
00252   /// call any non-virtual Value method on getValPtr(), but no subclass methods.
00253   /// If WeakVH were implemented as a CallbackVH, it would use this method to
00254   /// call setValPtr(NULL).  AssertingVH would use this method to cause an
00255   /// assertion failure.
00256   ///
00257   /// All implementations must remove the reference from this object to the
00258   /// Value that's being destroyed.
00259   virtual void deleted() {
00260     setValPtr(NULL);
00261   }
00262 
00263   /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
00264   /// _before_ any of the uses have actually been replaced.  If WeakVH were
00265   /// implemented as a CallbackVH, it would use this method to call
00266   /// setValPtr(new_value).  AssertingVH would do nothing in this method.
00267   virtual void allUsesReplacedWith(Value *new_value) {}
00268 };
00269 
00270 // Specialize simplify_type to allow CallbackVH to participate in
00271 // dyn_cast, isa, etc.
00272 template<typename From> struct simplify_type;
00273 template<> struct simplify_type<const CallbackVH> {
00274   typedef Value* SimpleType;
00275   static SimpleType getSimplifiedValue(const CallbackVH &CVH) {
00276     return static_cast<Value *>(CVH);
00277   }
00278 };
00279 template<> struct simplify_type<CallbackVH>
00280   : public simplify_type<const CallbackVH> {};
00281 
00282 } // End llvm namespace
00283 
00284 #endif