ValueHandle.h revision 263508
174462Salfred//===- llvm/Support/ValueHandle.h - Value Smart Pointer classes -*- C++ -*-===//
274462Salfred//
374462Salfred//                     The LLVM Compiler Infrastructure
474462Salfred//
574462Salfred// This file is distributed under the University of Illinois Open Source
674462Salfred// License. See LICENSE.TXT for details.
774462Salfred//
874462Salfred//===----------------------------------------------------------------------===//
974462Salfred//
1074462Salfred// This file declares the ValueHandle class and its sub-classes.
1174462Salfred//
1274462Salfred//===----------------------------------------------------------------------===//
1374462Salfred
1474462Salfred#ifndef LLVM_SUPPORT_VALUEHANDLE_H
1574462Salfred#define LLVM_SUPPORT_VALUEHANDLE_H
1674462Salfred
1774462Salfred#include "llvm/ADT/DenseMapInfo.h"
1874462Salfred#include "llvm/ADT/PointerIntPair.h"
1974462Salfred#include "llvm/IR/Value.h"
2074462Salfred
2174462Salfrednamespace llvm {
2274462Salfredclass ValueHandleBase;
2374462Salfredtemplate<typename From> struct simplify_type;
2474462Salfred
2574462Salfred// ValueHandleBase** is only 4-byte aligned.
2674462Salfredtemplate<>
2774462Salfredclass PointerLikeTypeTraits<ValueHandleBase**> {
2874462Salfredpublic:
2974462Salfred  static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
3074462Salfred  static inline ValueHandleBase **getFromVoidPointer(void *P) {
3174462Salfred    return static_cast<ValueHandleBase**>(P);
3274462Salfred  }
3374462Salfred  enum { NumLowBitsAvailable = 2 };
3474462Salfred};
3574462Salfred
3674462Salfred/// ValueHandleBase - This is the common base class of value handles.
3774462Salfred/// ValueHandle's are smart pointers to Value's that have special behavior when
3874462Salfred/// the value is deleted or ReplaceAllUsesWith'd.  See the specific handles
3974462Salfred/// below for details.
4074462Salfred///
4174462Salfredclass ValueHandleBase {
4274462Salfred  friend class Value;
4374462Salfredprotected:
4474462Salfred  /// HandleBaseKind - This indicates what sub class the handle actually is.
4574462Salfred  /// This is to avoid having a vtable for the light-weight handle pointers. The
4674462Salfred  /// fully general Callback version does have a vtable.
4774462Salfred  enum HandleBaseKind {
4874462Salfred    Assert,
4974462Salfred    Callback,
5074462Salfred    Tracking,
5174462Salfred    Weak
5274462Salfred  };
5374462Salfred
5474462Salfredprivate:
5574462Salfred  PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
5674462Salfred  ValueHandleBase *Next;
57224001Sdelphij
5874462Salfred  // A subclass may want to store some information along with the value
5974462Salfred  // pointer. Allow them to do this by making the value pointer a pointer-int
6074462Salfred  // pair. The 'setValPtrInt' and 'getValPtrInt' methods below give them this
6174462Salfred  // access.
6274462Salfred  PointerIntPair<Value*, 2> VP;
6374462Salfred
6474462Salfred  ValueHandleBase(const ValueHandleBase&) LLVM_DELETED_FUNCTION;
6574462Salfredpublic:
6674462Salfred  explicit ValueHandleBase(HandleBaseKind Kind)
6774462Salfred    : PrevPair(0, Kind), Next(0), VP(0, 0) {}
6874462Salfred  ValueHandleBase(HandleBaseKind Kind, Value *V)
6974462Salfred    : PrevPair(0, Kind), Next(0), VP(V, 0) {
7074462Salfred    if (isValid(VP.getPointer()))
7174462Salfred      AddToUseList();
7274462Salfred  }
7374462Salfred  ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
7474462Salfred    : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
7574462Salfred    if (isValid(VP.getPointer()))
7674462Salfred      AddToExistingUseList(RHS.getPrevPtr());
7774462Salfred  }
7874462Salfred  ~ValueHandleBase() {
7974462Salfred    if (isValid(VP.getPointer()))
8074462Salfred      RemoveFromUseList();
8174462Salfred  }
8274462Salfred
8374462Salfred  Value *operator=(Value *RHS) {
8474462Salfred    if (VP.getPointer() == RHS) return RHS;
8574462Salfred    if (isValid(VP.getPointer())) RemoveFromUseList();
8674462Salfred    VP.setPointer(RHS);
8774462Salfred    if (isValid(VP.getPointer())) AddToUseList();
8874462Salfred    return RHS;
8974462Salfred  }
9074462Salfred
9174462Salfred  Value *operator=(const ValueHandleBase &RHS) {
9274462Salfred    if (VP.getPointer() == RHS.VP.getPointer()) return RHS.VP.getPointer();
9374462Salfred    if (isValid(VP.getPointer())) RemoveFromUseList();
9474462Salfred    VP.setPointer(RHS.VP.getPointer());
9574462Salfred    if (isValid(VP.getPointer())) AddToExistingUseList(RHS.getPrevPtr());
9674462Salfred    return VP.getPointer();
9774462Salfred  }
9874462Salfred
9974462Salfred  Value *operator->() const { return getValPtr(); }
10074462Salfred  Value &operator*() const { return *getValPtr(); }
10174462Salfred
10274462Salfredprotected:
10374462Salfred  Value *getValPtr() const { return VP.getPointer(); }
10474462Salfred
10574462Salfred  void setValPtrInt(unsigned K) { VP.setInt(K); }
10674462Salfred  unsigned getValPtrInt() const { return VP.getInt(); }
10774462Salfred
10874462Salfred  static bool isValid(Value *V) {
10974462Salfred    return V &&
11074462Salfred           V != DenseMapInfo<Value *>::getEmptyKey() &&
11174462Salfred           V != DenseMapInfo<Value *>::getTombstoneKey();
11274462Salfred  }
11374462Salfred
11474462Salfredpublic:
11574462Salfred  // Callbacks made from Value.
11674462Salfred  static void ValueIsDeleted(Value *V);
11774462Salfred  static void ValueIsRAUWd(Value *Old, Value *New);
11874462Salfred
119121656Smbrprivate:
12074462Salfred  // Internal implementation details.
12174462Salfred  ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
12274462Salfred  HandleBaseKind getKind() const { return PrevPair.getInt(); }
12374462Salfred  void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
12474462Salfred
12574462Salfred  /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
12674462Salfred  /// List is the address of either the head of the list or a Next node within
12774462Salfred  /// the existing use list.
12874462Salfred  void AddToExistingUseList(ValueHandleBase **List);
12974462Salfred
13074462Salfred  /// AddToExistingUseListAfter - Add this ValueHandle to the use list after
13174462Salfred  /// Node.
13274462Salfred  void AddToExistingUseListAfter(ValueHandleBase *Node);
13374462Salfred
13474462Salfred  /// AddToUseList - Add this ValueHandle to the use list for VP.
13574462Salfred  void AddToUseList();
13674462Salfred  /// RemoveFromUseList - Remove this ValueHandle from its current use list.
137121656Smbr  void RemoveFromUseList();
13874462Salfred};
13974462Salfred
14074462Salfred/// WeakVH - This is a value handle that tries hard to point to a Value, even
14174462Salfred/// across RAUW operations, but will null itself out if the value is destroyed.
14274462Salfred/// this is useful for advisory sorts of information, but should not be used as
14374462Salfred/// the key of a map (since the map would have to rearrange itself when the
14474462Salfred/// pointer changes).
14574462Salfredclass WeakVH : public ValueHandleBase {
14674462Salfredpublic:
14774462Salfred  WeakVH() : ValueHandleBase(Weak) {}
14874462Salfred  WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
14974462Salfred  WeakVH(const WeakVH &RHS)
15074462Salfred    : ValueHandleBase(Weak, RHS) {}
15174462Salfred
15274462Salfred  Value *operator=(Value *RHS) {
15374462Salfred    return ValueHandleBase::operator=(RHS);
15474462Salfred  }
155302453Sngie  Value *operator=(const ValueHandleBase &RHS) {
15674462Salfred    return ValueHandleBase::operator=(RHS);
15774462Salfred  }
15874462Salfred
15974462Salfred  operator Value*() const {
16074462Salfred    return getValPtr();
16174462Salfred  }
16274462Salfred};
16374462Salfred
16474462Salfred// Specialize simplify_type to allow WeakVH to participate in
16574462Salfred// dyn_cast, isa, etc.
16674462Salfredtemplate<> struct simplify_type<WeakVH> {
16774462Salfred  typedef Value* SimpleType;
16874462Salfred  static SimpleType getSimplifiedValue(WeakVH &WVH) {
16974462Salfred    return WVH;
17074462Salfred  }
17174462Salfred};
17274462Salfred
17374462Salfred/// AssertingVH - This is a Value Handle that points to a value and asserts out
17474462Salfred/// if the value is destroyed while the handle is still live.  This is very
17574462Salfred/// useful for catching dangling pointer bugs and other things which can be
17674462Salfred/// non-obvious.  One particularly useful place to use this is as the Key of a
17774462Salfred/// map.  Dangling pointer bugs often lead to really subtle bugs that only occur
17874462Salfred/// if another object happens to get allocated to the same address as the old
17974462Salfred/// one.  Using an AssertingVH ensures that an assert is triggered as soon as
18074462Salfred/// the bad delete occurs.
18174462Salfred///
18274462Salfred/// Note that an AssertingVH handle does *not* follow values across RAUW
18374462Salfred/// operations.  This means that RAUW's need to explicitly update the
18474462Salfred/// AssertingVH's as it moves.  This is required because in non-assert mode this
18574462Salfred/// class turns into a trivial wrapper around a pointer.
18674462Salfredtemplate <typename ValueTy>
18774462Salfredclass AssertingVH
18874462Salfred#ifndef NDEBUG
18974462Salfred  : public ValueHandleBase
19074462Salfred#endif
19174462Salfred  {
19274462Salfred
19374462Salfred#ifndef NDEBUG
19474462Salfred  ValueTy *getValPtr() const {
19574462Salfred    return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
19674462Salfred  }
19774462Salfred  void setValPtr(ValueTy *P) {
19874462Salfred    ValueHandleBase::operator=(GetAsValue(P));
19974462Salfred  }
20074462Salfred#else
20174462Salfred  ValueTy *ThePtr;
202104592Salfred  ValueTy *getValPtr() const { return ThePtr; }
203104592Salfred  void setValPtr(ValueTy *P) { ThePtr = P; }
20474462Salfred#endif
20574462Salfred
20674462Salfred  // Convert a ValueTy*, which may be const, to the type the base
207  // class expects.
208  static Value *GetAsValue(Value *V) { return V; }
209  static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
210
211public:
212#ifndef NDEBUG
213  AssertingVH() : ValueHandleBase(Assert) {}
214  AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
215  AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
216#else
217  AssertingVH() : ThePtr(0) {}
218  AssertingVH(ValueTy *P) : ThePtr(P) {}
219#endif
220
221  operator ValueTy*() const {
222    return getValPtr();
223  }
224
225  ValueTy *operator=(ValueTy *RHS) {
226    setValPtr(RHS);
227    return getValPtr();
228  }
229  ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
230    setValPtr(RHS.getValPtr());
231    return getValPtr();
232  }
233
234  ValueTy *operator->() const { return getValPtr(); }
235  ValueTy &operator*() const { return *getValPtr(); }
236};
237
238// Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
239template<typename T>
240struct DenseMapInfo<AssertingVH<T> > {
241  typedef DenseMapInfo<T*> PointerInfo;
242  static inline AssertingVH<T> getEmptyKey() {
243    return AssertingVH<T>(PointerInfo::getEmptyKey());
244  }
245  static inline T* getTombstoneKey() {
246    return AssertingVH<T>(PointerInfo::getTombstoneKey());
247  }
248  static unsigned getHashValue(const AssertingVH<T> &Val) {
249    return PointerInfo::getHashValue(Val);
250  }
251  static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
252    return LHS == RHS;
253  }
254};
255
256template <typename T>
257struct isPodLike<AssertingVH<T> > {
258#ifdef NDEBUG
259  static const bool value = true;
260#else
261  static const bool value = false;
262#endif
263};
264
265
266/// TrackingVH - This is a value handle that tracks a Value (or Value subclass),
267/// even across RAUW operations.
268///
269/// TrackingVH is designed for situations where a client needs to hold a handle
270/// to a Value (or subclass) across some operations which may move that value,
271/// but should never destroy it or replace it with some unacceptable type.
272///
273/// It is an error to do anything with a TrackingVH whose value has been
274/// destroyed, except to destruct it.
275///
276/// It is an error to attempt to replace a value with one of a type which is
277/// incompatible with any of its outstanding TrackingVHs.
278template<typename ValueTy>
279class TrackingVH : public ValueHandleBase {
280  void CheckValidity() const {
281    Value *VP = ValueHandleBase::getValPtr();
282
283    // Null is always ok.
284    if (!VP) return;
285
286    // Check that this value is valid (i.e., it hasn't been deleted). We
287    // explicitly delay this check until access to avoid requiring clients to be
288    // unnecessarily careful w.r.t. destruction.
289    assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
290
291    // Check that the value is a member of the correct subclass. We would like
292    // to check this property on assignment for better debugging, but we don't
293    // want to require a virtual interface on this VH. Instead we allow RAUW to
294    // replace this value with a value of an invalid type, and check it here.
295    assert(isa<ValueTy>(VP) &&
296           "Tracked Value was replaced by one with an invalid type!");
297  }
298
299  ValueTy *getValPtr() const {
300    CheckValidity();
301    return (ValueTy*)ValueHandleBase::getValPtr();
302  }
303  void setValPtr(ValueTy *P) {
304    CheckValidity();
305    ValueHandleBase::operator=(GetAsValue(P));
306  }
307
308  // Convert a ValueTy*, which may be const, to the type the base
309  // class expects.
310  static Value *GetAsValue(Value *V) { return V; }
311  static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
312
313public:
314  TrackingVH() : ValueHandleBase(Tracking) {}
315  TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
316  TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
317
318  operator ValueTy*() const {
319    return getValPtr();
320  }
321
322  ValueTy *operator=(ValueTy *RHS) {
323    setValPtr(RHS);
324    return getValPtr();
325  }
326  ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
327    setValPtr(RHS.getValPtr());
328    return getValPtr();
329  }
330
331  ValueTy *operator->() const { return getValPtr(); }
332  ValueTy &operator*() const { return *getValPtr(); }
333};
334
335/// CallbackVH - This is a value handle that allows subclasses to define
336/// callbacks that run when the underlying Value has RAUW called on it or is
337/// destroyed.  This class can be used as the key of a map, as long as the user
338/// takes it out of the map before calling setValPtr() (since the map has to
339/// rearrange itself when the pointer changes).  Unlike ValueHandleBase, this
340/// class has a vtable and a virtual destructor.
341class CallbackVH : public ValueHandleBase {
342  virtual void anchor();
343protected:
344  CallbackVH(const CallbackVH &RHS)
345    : ValueHandleBase(Callback, RHS) {}
346
347  virtual ~CallbackVH() {}
348
349  void setValPtr(Value *P) {
350    ValueHandleBase::operator=(P);
351  }
352
353public:
354  CallbackVH() : ValueHandleBase(Callback) {}
355  CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
356
357  operator Value*() const {
358    return getValPtr();
359  }
360
361  /// Called when this->getValPtr() is destroyed, inside ~Value(), so you may
362  /// call any non-virtual Value method on getValPtr(), but no subclass methods.
363  /// If WeakVH were implemented as a CallbackVH, it would use this method to
364  /// call setValPtr(NULL).  AssertingVH would use this method to cause an
365  /// assertion failure.
366  ///
367  /// All implementations must remove the reference from this object to the
368  /// Value that's being destroyed.
369  virtual void deleted() { setValPtr(NULL); }
370
371  /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
372  /// _before_ any of the uses have actually been replaced.  If WeakVH were
373  /// implemented as a CallbackVH, it would use this method to call
374  /// setValPtr(new_value).  AssertingVH would do nothing in this method.
375  virtual void allUsesReplacedWith(Value *) {}
376};
377
378} // End llvm namespace
379
380#endif
381