LLVM API Documentation

ValueHandle.h
Go to the documentation of this file.
00001 //===- 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_IR_VALUEHANDLE_H
00015 #define LLVM_IR_VALUEHANDLE_H
00016 
00017 #include "llvm/ADT/DenseMapInfo.h"
00018 #include "llvm/ADT/PointerIntPair.h"
00019 #include "llvm/IR/Value.h"
00020 
00021 namespace llvm {
00022 class ValueHandleBase;
00023 template<typename From> struct simplify_type;
00024 
00025 // ValueHandleBase** is only 4-byte aligned.
00026 template<>
00027 class PointerLikeTypeTraits<ValueHandleBase**> {
00028 public:
00029   static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
00030   static inline ValueHandleBase **getFromVoidPointer(void *P) {
00031     return static_cast<ValueHandleBase**>(P);
00032   }
00033   enum { NumLowBitsAvailable = 2 };
00034 };
00035 
00036 /// ValueHandleBase - This is the common base class of value handles.
00037 /// ValueHandle's are smart pointers to Value's that have special behavior when
00038 /// the value is deleted or ReplaceAllUsesWith'd.  See the specific handles
00039 /// below for details.
00040 ///
00041 class ValueHandleBase {
00042   friend class Value;
00043 protected:
00044   /// HandleBaseKind - This indicates what sub class the handle actually is.
00045   /// This is to avoid having a vtable for the light-weight handle pointers. The
00046   /// fully general Callback version does have a vtable.
00047   enum HandleBaseKind {
00048     Assert,
00049     Callback,
00050     Tracking,
00051     Weak
00052   };
00053 
00054 private:
00055   PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
00056   ValueHandleBase *Next;
00057 
00058   // A subclass may want to store some information along with the value
00059   // pointer. Allow them to do this by making the value pointer a pointer-int
00060   // pair. The 'setValPtrInt' and 'getValPtrInt' methods below give them this
00061   // access.
00062   PointerIntPair<Value*, 2> VP;
00063 
00064   ValueHandleBase(const ValueHandleBase&) LLVM_DELETED_FUNCTION;
00065 public:
00066   explicit ValueHandleBase(HandleBaseKind Kind)
00067     : PrevPair(nullptr, Kind), Next(nullptr), VP(nullptr, 0) {}
00068   ValueHandleBase(HandleBaseKind Kind, Value *V)
00069     : PrevPair(nullptr, Kind), Next(nullptr), VP(V, 0) {
00070     if (isValid(VP.getPointer()))
00071       AddToUseList();
00072   }
00073   ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
00074     : PrevPair(nullptr, Kind), Next(nullptr), VP(RHS.VP) {
00075     if (isValid(VP.getPointer()))
00076       AddToExistingUseList(RHS.getPrevPtr());
00077   }
00078   ~ValueHandleBase() {
00079     if (isValid(VP.getPointer()))
00080       RemoveFromUseList();
00081   }
00082 
00083   Value *operator=(Value *RHS) {
00084     if (VP.getPointer() == RHS) return RHS;
00085     if (isValid(VP.getPointer())) RemoveFromUseList();
00086     VP.setPointer(RHS);
00087     if (isValid(VP.getPointer())) AddToUseList();
00088     return RHS;
00089   }
00090 
00091   Value *operator=(const ValueHandleBase &RHS) {
00092     if (VP.getPointer() == RHS.VP.getPointer()) return RHS.VP.getPointer();
00093     if (isValid(VP.getPointer())) RemoveFromUseList();
00094     VP.setPointer(RHS.VP.getPointer());
00095     if (isValid(VP.getPointer())) AddToExistingUseList(RHS.getPrevPtr());
00096     return VP.getPointer();
00097   }
00098 
00099   Value *operator->() const { return getValPtr(); }
00100   Value &operator*() const { return *getValPtr(); }
00101 
00102 protected:
00103   Value *getValPtr() const { return VP.getPointer(); }
00104 
00105   void setValPtrInt(unsigned K) { VP.setInt(K); }
00106   unsigned getValPtrInt() const { return VP.getInt(); }
00107 
00108   static bool isValid(Value *V) {
00109     return V &&
00110            V != DenseMapInfo<Value *>::getEmptyKey() &&
00111            V != DenseMapInfo<Value *>::getTombstoneKey();
00112   }
00113 
00114 public:
00115   // Callbacks made from Value.
00116   static void ValueIsDeleted(Value *V);
00117   static void ValueIsRAUWd(Value *Old, Value *New);
00118 
00119 private:
00120   // Internal implementation details.
00121   ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
00122   HandleBaseKind getKind() const { return PrevPair.getInt(); }
00123   void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
00124 
00125   /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
00126   /// List is the address of either the head of the list or a Next node within
00127   /// the existing use list.
00128   void AddToExistingUseList(ValueHandleBase **List);
00129 
00130   /// AddToExistingUseListAfter - Add this ValueHandle to the use list after
00131   /// Node.
00132   void AddToExistingUseListAfter(ValueHandleBase *Node);
00133 
00134   /// AddToUseList - Add this ValueHandle to the use list for VP.
00135   void AddToUseList();
00136   /// RemoveFromUseList - Remove this ValueHandle from its current use list.
00137   void RemoveFromUseList();
00138 };
00139 
00140 /// WeakVH - This is a value handle that tries hard to point to a Value, even
00141 /// across RAUW operations, but will null itself out if the value is destroyed.
00142 /// this is useful for advisory sorts of information, but should not be used as
00143 /// the key of a map (since the map would have to rearrange itself when the
00144 /// pointer changes).
00145 class WeakVH : public ValueHandleBase {
00146 public:
00147   WeakVH() : ValueHandleBase(Weak) {}
00148   WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
00149   WeakVH(const WeakVH &RHS)
00150     : ValueHandleBase(Weak, RHS) {}
00151 
00152   Value *operator=(Value *RHS) {
00153     return ValueHandleBase::operator=(RHS);
00154   }
00155   Value *operator=(const ValueHandleBase &RHS) {
00156     return ValueHandleBase::operator=(RHS);
00157   }
00158 
00159   operator Value*() const {
00160     return getValPtr();
00161   }
00162 };
00163 
00164 // Specialize simplify_type to allow WeakVH to participate in
00165 // dyn_cast, isa, etc.
00166 template<> struct simplify_type<WeakVH> {
00167   typedef Value* SimpleType;
00168   static SimpleType getSimplifiedValue(WeakVH &WVH) {
00169     return WVH;
00170   }
00171 };
00172 
00173 /// AssertingVH - This is a Value Handle that points to a value and asserts out
00174 /// if the value is destroyed while the handle is still live.  This is very
00175 /// useful for catching dangling pointer bugs and other things which can be
00176 /// non-obvious.  One particularly useful place to use this is as the Key of a
00177 /// map.  Dangling pointer bugs often lead to really subtle bugs that only occur
00178 /// if another object happens to get allocated to the same address as the old
00179 /// one.  Using an AssertingVH ensures that an assert is triggered as soon as
00180 /// the bad delete occurs.
00181 ///
00182 /// Note that an AssertingVH handle does *not* follow values across RAUW
00183 /// operations.  This means that RAUW's need to explicitly update the
00184 /// AssertingVH's as it moves.  This is required because in non-assert mode this
00185 /// class turns into a trivial wrapper around a pointer.
00186 template <typename ValueTy>
00187 class AssertingVH
00188 #ifndef NDEBUG
00189   : public ValueHandleBase
00190 #endif
00191   {
00192   friend struct DenseMapInfo<AssertingVH<ValueTy> >;
00193 
00194 #ifndef NDEBUG
00195   ValueTy *getValPtr() const {
00196     return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
00197   }
00198   void setValPtr(ValueTy *P) {
00199     ValueHandleBase::operator=(GetAsValue(P));
00200   }
00201 #else
00202   ValueTy *ThePtr;
00203   ValueTy *getValPtr() const { return ThePtr; }
00204   void setValPtr(ValueTy *P) { ThePtr = P; }
00205 #endif
00206 
00207   // Convert a ValueTy*, which may be const, to the type the base
00208   // class expects.
00209   static Value *GetAsValue(Value *V) { return V; }
00210   static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
00211 
00212 public:
00213 #ifndef NDEBUG
00214   AssertingVH() : ValueHandleBase(Assert) {}
00215   AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
00216   AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
00217 #else
00218   AssertingVH() : ThePtr(nullptr) {}
00219   AssertingVH(ValueTy *P) : ThePtr(P) {}
00220 #endif
00221 
00222   operator ValueTy*() const {
00223     return getValPtr();
00224   }
00225 
00226   ValueTy *operator=(ValueTy *RHS) {
00227     setValPtr(RHS);
00228     return getValPtr();
00229   }
00230   ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
00231     setValPtr(RHS.getValPtr());
00232     return getValPtr();
00233   }
00234 
00235   ValueTy *operator->() const { return getValPtr(); }
00236   ValueTy &operator*() const { return *getValPtr(); }
00237 };
00238 
00239 // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
00240 template<typename T>
00241 struct DenseMapInfo<AssertingVH<T> > {
00242   typedef DenseMapInfo<T*> PointerInfo;
00243   static inline AssertingVH<T> getEmptyKey() {
00244     return AssertingVH<T>(PointerInfo::getEmptyKey());
00245   }
00246   static inline T* getTombstoneKey() {
00247     return AssertingVH<T>(PointerInfo::getTombstoneKey());
00248   }
00249   static unsigned getHashValue(const AssertingVH<T> &Val) {
00250     return PointerInfo::getHashValue(Val);
00251   }
00252 #ifndef NDEBUG
00253   static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
00254     // Avoid downcasting AssertingVH<T> to T*, as empty/tombstone keys may not
00255     // be properly aligned pointers to T*.
00256     return LHS.ValueHandleBase::getValPtr() == RHS.ValueHandleBase::getValPtr();
00257   }
00258 #else
00259   static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
00260     return LHS == RHS;
00261   }
00262 #endif
00263 };
00264 
00265 template <typename T>
00266 struct isPodLike<AssertingVH<T> > {
00267 #ifdef NDEBUG
00268   static const bool value = true;
00269 #else
00270   static const bool value = false;
00271 #endif
00272 };
00273 
00274 
00275 /// TrackingVH - This is a value handle that tracks a Value (or Value subclass),
00276 /// even across RAUW operations.
00277 ///
00278 /// TrackingVH is designed for situations where a client needs to hold a handle
00279 /// to a Value (or subclass) across some operations which may move that value,
00280 /// but should never destroy it or replace it with some unacceptable type.
00281 ///
00282 /// It is an error to do anything with a TrackingVH whose value has been
00283 /// destroyed, except to destruct it.
00284 ///
00285 /// It is an error to attempt to replace a value with one of a type which is
00286 /// incompatible with any of its outstanding TrackingVHs.
00287 template<typename ValueTy>
00288 class TrackingVH : public ValueHandleBase {
00289   void CheckValidity() const {
00290     Value *VP = ValueHandleBase::getValPtr();
00291 
00292     // Null is always ok.
00293     if (!VP) return;
00294 
00295     // Check that this value is valid (i.e., it hasn't been deleted). We
00296     // explicitly delay this check until access to avoid requiring clients to be
00297     // unnecessarily careful w.r.t. destruction.
00298     assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
00299 
00300     // Check that the value is a member of the correct subclass. We would like
00301     // to check this property on assignment for better debugging, but we don't
00302     // want to require a virtual interface on this VH. Instead we allow RAUW to
00303     // replace this value with a value of an invalid type, and check it here.
00304     assert(isa<ValueTy>(VP) &&
00305            "Tracked Value was replaced by one with an invalid type!");
00306   }
00307 
00308   ValueTy *getValPtr() const {
00309     CheckValidity();
00310     return (ValueTy*)ValueHandleBase::getValPtr();
00311   }
00312   void setValPtr(ValueTy *P) {
00313     CheckValidity();
00314     ValueHandleBase::operator=(GetAsValue(P));
00315   }
00316 
00317   // Convert a ValueTy*, which may be const, to the type the base
00318   // class expects.
00319   static Value *GetAsValue(Value *V) { return V; }
00320   static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
00321 
00322 public:
00323   TrackingVH() : ValueHandleBase(Tracking) {}
00324   TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
00325   TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
00326 
00327   operator ValueTy*() const {
00328     return getValPtr();
00329   }
00330 
00331   ValueTy *operator=(ValueTy *RHS) {
00332     setValPtr(RHS);
00333     return getValPtr();
00334   }
00335   ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
00336     setValPtr(RHS.getValPtr());
00337     return getValPtr();
00338   }
00339 
00340   ValueTy *operator->() const { return getValPtr(); }
00341   ValueTy &operator*() const { return *getValPtr(); }
00342 };
00343 
00344 /// CallbackVH - This is a value handle that allows subclasses to define
00345 /// callbacks that run when the underlying Value has RAUW called on it or is
00346 /// destroyed.  This class can be used as the key of a map, as long as the user
00347 /// takes it out of the map before calling setValPtr() (since the map has to
00348 /// rearrange itself when the pointer changes).  Unlike ValueHandleBase, this
00349 /// class has a vtable and a virtual destructor.
00350 class CallbackVH : public ValueHandleBase {
00351   virtual void anchor();
00352 protected:
00353   CallbackVH(const CallbackVH &RHS)
00354     : ValueHandleBase(Callback, RHS) {}
00355 
00356   virtual ~CallbackVH() {}
00357 
00358   void setValPtr(Value *P) {
00359     ValueHandleBase::operator=(P);
00360   }
00361 
00362 public:
00363   CallbackVH() : ValueHandleBase(Callback) {}
00364   CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
00365 
00366   operator Value*() const {
00367     return getValPtr();
00368   }
00369 
00370   /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
00371   /// call any non-virtual Value method on getValPtr(), but no subclass methods.
00372   /// If WeakVH were implemented as a CallbackVH, it would use this method to
00373   /// call setValPtr(NULL).  AssertingVH would use this method to cause an
00374   /// assertion failure.
00375   ///
00376   /// All implementations must remove the reference from this object to the
00377   /// Value that's being destroyed.
00378   virtual void deleted() { setValPtr(nullptr); }
00379 
00380   /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
00381   /// _before_ any of the uses have actually been replaced.  If WeakVH were
00382   /// implemented as a CallbackVH, it would use this method to call
00383   /// setValPtr(new_value).  AssertingVH would do nothing in this method.
00384   virtual void allUsesReplacedWith(Value *) {}
00385 };
00386 
00387 } // End llvm namespace
00388 
00389 #endif