2014-03-04 12:17:44 +01:00
|
|
|
//===- ValueHandle.h - Value Smart Pointer classes --------------*- C++ -*-===//
|
2009-04-01 00:11:05 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares the ValueHandle class and its sub-classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-04 12:17:44 +01:00
|
|
|
#ifndef LLVM_IR_VALUEHANDLE_H
|
|
|
|
#define LLVM_IR_VALUEHANDLE_H
|
2009-04-01 00:11:05 +02:00
|
|
|
|
2009-07-31 20:20:18 +02:00
|
|
|
#include "llvm/ADT/DenseMapInfo.h"
|
2009-04-01 00:11:05 +02:00
|
|
|
#include "llvm/ADT/PointerIntPair.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Value.h"
|
2017-05-11 01:41:30 +02:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include <cassert>
|
2009-04-01 00:11:05 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
2009-04-03 02:26:01 +02:00
|
|
|
|
2014-10-15 22:28:31 +02:00
|
|
|
/// \brief This is the common base class of value handles.
|
|
|
|
///
|
2009-04-01 00:11:05 +02:00
|
|
|
/// ValueHandle's are smart pointers to Value's that have special behavior when
|
|
|
|
/// the value is deleted or ReplaceAllUsesWith'd. See the specific handles
|
|
|
|
/// below for details.
|
|
|
|
class ValueHandleBase {
|
|
|
|
friend class Value;
|
2017-05-11 01:41:30 +02:00
|
|
|
|
2009-04-01 00:11:05 +02:00
|
|
|
protected:
|
2014-10-15 22:28:31 +02:00
|
|
|
/// \brief This indicates what sub class the handle actually is.
|
|
|
|
///
|
2009-04-01 00:11:05 +02:00
|
|
|
/// This is to avoid having a vtable for the light-weight handle pointers. The
|
2009-05-02 23:10:48 +02:00
|
|
|
/// fully general Callback version does have a vtable.
|
2017-05-01 19:07:54 +02:00
|
|
|
enum HandleBaseKind { Assert, Callback, Weak, WeakTracking };
|
2009-09-19 22:40:05 +02:00
|
|
|
|
2015-08-04 00:30:24 +02:00
|
|
|
ValueHandleBase(const ValueHandleBase &RHS)
|
|
|
|
: ValueHandleBase(RHS.PrevPair.getInt(), RHS) {}
|
|
|
|
|
|
|
|
ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
|
2017-05-11 01:41:30 +02:00
|
|
|
: PrevPair(nullptr, Kind), Val(RHS.getValPtr()) {
|
2017-04-27 08:02:18 +02:00
|
|
|
if (isValid(getValPtr()))
|
2015-08-04 00:30:24 +02:00
|
|
|
AddToExistingUseList(RHS.getPrevPtr());
|
|
|
|
}
|
|
|
|
|
2012-04-08 12:16:43 +02:00
|
|
|
private:
|
2017-05-01 19:36:12 +02:00
|
|
|
PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
|
2017-05-11 01:41:30 +02:00
|
|
|
ValueHandleBase *Next = nullptr;
|
|
|
|
Value *Val = nullptr;
|
2017-04-27 08:02:18 +02:00
|
|
|
|
|
|
|
void setValPtr(Value *V) { Val = V; }
|
2012-09-17 08:31:17 +02:00
|
|
|
|
2009-04-01 00:11:05 +02:00
|
|
|
public:
|
2009-05-05 23:23:20 +02:00
|
|
|
explicit ValueHandleBase(HandleBaseKind Kind)
|
2017-05-11 01:41:30 +02:00
|
|
|
: PrevPair(nullptr, Kind) {}
|
2009-04-01 00:11:05 +02:00
|
|
|
ValueHandleBase(HandleBaseKind Kind, Value *V)
|
2017-05-11 01:41:30 +02:00
|
|
|
: PrevPair(nullptr, Kind), Val(V) {
|
2017-04-27 08:02:18 +02:00
|
|
|
if (isValid(getValPtr()))
|
2009-04-01 00:11:05 +02:00
|
|
|
AddToUseList();
|
|
|
|
}
|
2015-08-04 00:30:24 +02:00
|
|
|
|
2009-04-01 00:11:05 +02:00
|
|
|
~ValueHandleBase() {
|
2017-04-27 08:02:18 +02:00
|
|
|
if (isValid(getValPtr()))
|
2009-09-19 22:40:05 +02:00
|
|
|
RemoveFromUseList();
|
2009-04-01 00:11:05 +02:00
|
|
|
}
|
2009-09-19 22:40:05 +02:00
|
|
|
|
2009-04-01 00:11:05 +02:00
|
|
|
Value *operator=(Value *RHS) {
|
2017-04-27 08:02:18 +02:00
|
|
|
if (getValPtr() == RHS)
|
|
|
|
return RHS;
|
|
|
|
if (isValid(getValPtr()))
|
|
|
|
RemoveFromUseList();
|
|
|
|
setValPtr(RHS);
|
|
|
|
if (isValid(getValPtr()))
|
|
|
|
AddToUseList();
|
2009-04-01 00:11:05 +02:00
|
|
|
return RHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *operator=(const ValueHandleBase &RHS) {
|
2017-04-27 08:02:18 +02:00
|
|
|
if (getValPtr() == RHS.getValPtr())
|
|
|
|
return RHS.getValPtr();
|
|
|
|
if (isValid(getValPtr()))
|
|
|
|
RemoveFromUseList();
|
|
|
|
setValPtr(RHS.getValPtr());
|
|
|
|
if (isValid(getValPtr()))
|
|
|
|
AddToExistingUseList(RHS.getPrevPtr());
|
|
|
|
return getValPtr();
|
2009-04-01 00:11:05 +02:00
|
|
|
}
|
2009-09-19 22:40:05 +02:00
|
|
|
|
2017-04-27 08:02:18 +02:00
|
|
|
Value *operator->() const { return getValPtr(); }
|
|
|
|
Value &operator*() const { return *getValPtr(); }
|
2009-04-27 22:32:07 +02:00
|
|
|
|
2009-04-01 00:11:05 +02:00
|
|
|
protected:
|
2017-04-27 08:02:18 +02:00
|
|
|
Value *getValPtr() const { return Val; }
|
2012-04-08 12:16:43 +02:00
|
|
|
|
2009-07-31 20:20:18 +02:00
|
|
|
static bool isValid(Value *V) {
|
|
|
|
return V &&
|
|
|
|
V != DenseMapInfo<Value *>::getEmptyKey() &&
|
|
|
|
V != DenseMapInfo<Value *>::getTombstoneKey();
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:34:47 +01:00
|
|
|
/// \brief Remove this ValueHandle from its current use list.
|
|
|
|
void RemoveFromUseList();
|
|
|
|
|
|
|
|
/// \brief Clear the underlying pointer without clearing the use list.
|
|
|
|
///
|
|
|
|
/// This should only be used if a derived class has manually removed the
|
|
|
|
/// handle from the use list.
|
2017-04-27 08:02:18 +02:00
|
|
|
void clearValPtr() { setValPtr(nullptr); }
|
2017-01-24 13:34:47 +01:00
|
|
|
|
2012-08-14 01:06:54 +02:00
|
|
|
public:
|
2009-04-01 00:11:05 +02:00
|
|
|
// Callbacks made from Value.
|
|
|
|
static void ValueIsDeleted(Value *V);
|
|
|
|
static void ValueIsRAUWd(Value *Old, Value *New);
|
2009-09-19 22:40:05 +02:00
|
|
|
|
2012-08-14 01:06:54 +02:00
|
|
|
private:
|
2009-04-01 00:11:05 +02:00
|
|
|
// Internal implementation details.
|
|
|
|
ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
|
|
|
|
HandleBaseKind getKind() const { return PrevPair.getInt(); }
|
|
|
|
void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
|
2009-09-19 22:40:05 +02:00
|
|
|
|
2015-01-09 01:48:47 +01:00
|
|
|
/// \brief Add this ValueHandle to the use list for V.
|
2014-10-15 22:28:31 +02:00
|
|
|
///
|
2009-10-12 19:43:32 +02:00
|
|
|
/// List is the address of either the head of the list or a Next node within
|
|
|
|
/// the existing use list.
|
2009-04-01 00:11:05 +02:00
|
|
|
void AddToExistingUseList(ValueHandleBase **List);
|
2009-09-19 22:40:05 +02:00
|
|
|
|
2014-10-15 22:28:31 +02:00
|
|
|
/// \brief Add this ValueHandle to the use list after Node.
|
2009-10-12 19:43:32 +02:00
|
|
|
void AddToExistingUseListAfter(ValueHandleBase *Node);
|
|
|
|
|
2015-01-09 01:48:47 +01:00
|
|
|
/// \brief Add this ValueHandle to the use list for V.
|
2009-04-01 00:11:05 +02:00
|
|
|
void AddToUseList();
|
|
|
|
};
|
2009-09-19 22:40:05 +02:00
|
|
|
|
2017-05-01 19:07:54 +02:00
|
|
|
/// \brief A nullable Value handle that is nullable.
|
|
|
|
///
|
|
|
|
/// This is a value handle that points to a value, and nulls itself
|
|
|
|
/// out if that value is deleted.
|
|
|
|
class WeakVH : public ValueHandleBase {
|
|
|
|
public:
|
|
|
|
WeakVH() : ValueHandleBase(Weak) {}
|
|
|
|
WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
|
|
|
|
WeakVH(const WeakVH &RHS)
|
|
|
|
: ValueHandleBase(Weak, RHS) {}
|
|
|
|
|
|
|
|
WeakVH &operator=(const WeakVH &RHS) = default;
|
|
|
|
|
|
|
|
Value *operator=(Value *RHS) {
|
|
|
|
return ValueHandleBase::operator=(RHS);
|
|
|
|
}
|
|
|
|
Value *operator=(const ValueHandleBase &RHS) {
|
|
|
|
return ValueHandleBase::operator=(RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator Value*() const {
|
|
|
|
return getValPtr();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Specialize simplify_type to allow WeakVH to participate in
|
|
|
|
// dyn_cast, isa, etc.
|
|
|
|
template <> struct simplify_type<WeakVH> {
|
2017-05-11 01:41:30 +02:00
|
|
|
using SimpleType = Value *;
|
|
|
|
|
2017-05-01 19:07:54 +02:00
|
|
|
static SimpleType getSimplifiedValue(WeakVH &WVH) { return WVH; }
|
|
|
|
};
|
|
|
|
template <> struct simplify_type<const WeakVH> {
|
2017-05-11 01:41:30 +02:00
|
|
|
using SimpleType = Value *;
|
|
|
|
|
2017-05-01 19:07:54 +02:00
|
|
|
static SimpleType getSimplifiedValue(const WeakVH &WVH) { return WVH; }
|
|
|
|
};
|
|
|
|
|
2017-04-26 18:37:05 +02:00
|
|
|
/// \brief Value handle that is nullable, but tries to track the Value.
|
2017-04-26 18:20:59 +02:00
|
|
|
///
|
2017-04-26 18:37:05 +02:00
|
|
|
/// This is a value handle that tries hard to point to a Value, even across
|
|
|
|
/// RAUW operations, but will null itself out if the value is destroyed. this
|
|
|
|
/// is useful for advisory sorts of information, but should not be used as the
|
|
|
|
/// key of a map (since the map would have to rearrange itself when the pointer
|
|
|
|
/// changes).
|
2017-05-01 19:07:49 +02:00
|
|
|
class WeakTrackingVH : public ValueHandleBase {
|
2017-04-26 18:20:59 +02:00
|
|
|
public:
|
2017-05-01 19:07:49 +02:00
|
|
|
WeakTrackingVH() : ValueHandleBase(WeakTracking) {}
|
|
|
|
WeakTrackingVH(Value *P) : ValueHandleBase(WeakTracking, P) {}
|
|
|
|
WeakTrackingVH(const WeakTrackingVH &RHS)
|
|
|
|
: ValueHandleBase(WeakTracking, RHS) {}
|
2017-04-26 18:20:59 +02:00
|
|
|
|
2017-05-01 19:07:49 +02:00
|
|
|
WeakTrackingVH &operator=(const WeakTrackingVH &RHS) = default;
|
2017-04-26 18:20:59 +02:00
|
|
|
|
|
|
|
Value *operator=(Value *RHS) {
|
|
|
|
return ValueHandleBase::operator=(RHS);
|
|
|
|
}
|
|
|
|
Value *operator=(const ValueHandleBase &RHS) {
|
|
|
|
return ValueHandleBase::operator=(RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator Value*() const {
|
|
|
|
return getValPtr();
|
|
|
|
}
|
2017-05-01 18:28:58 +02:00
|
|
|
|
|
|
|
bool pointsToAliveValue() const {
|
|
|
|
return ValueHandleBase::isValid(getValPtr());
|
|
|
|
}
|
2017-04-26 18:20:59 +02:00
|
|
|
};
|
|
|
|
|
2017-05-01 19:07:49 +02:00
|
|
|
// Specialize simplify_type to allow WeakTrackingVH to participate in
|
2017-04-26 18:20:59 +02:00
|
|
|
// dyn_cast, isa, etc.
|
2017-05-01 19:07:49 +02:00
|
|
|
template <> struct simplify_type<WeakTrackingVH> {
|
2017-05-11 01:41:30 +02:00
|
|
|
using SimpleType = Value *;
|
|
|
|
|
2017-05-01 19:07:49 +02:00
|
|
|
static SimpleType getSimplifiedValue(WeakTrackingVH &WVH) { return WVH; }
|
2017-04-26 18:20:59 +02:00
|
|
|
};
|
2017-05-01 19:07:49 +02:00
|
|
|
template <> struct simplify_type<const WeakTrackingVH> {
|
2017-05-11 01:41:30 +02:00
|
|
|
using SimpleType = Value *;
|
|
|
|
|
2017-05-01 19:07:49 +02:00
|
|
|
static SimpleType getSimplifiedValue(const WeakTrackingVH &WVH) {
|
|
|
|
return WVH;
|
|
|
|
}
|
2017-04-26 18:20:59 +02:00
|
|
|
};
|
|
|
|
|
2014-10-15 22:28:31 +02:00
|
|
|
/// \brief Value handle that asserts if the Value is deleted.
|
|
|
|
///
|
|
|
|
/// This is a Value Handle that points to a value and asserts out if the value
|
|
|
|
/// is destroyed while the handle is still live. This is very useful for
|
|
|
|
/// catching dangling pointer bugs and other things which can be non-obvious.
|
|
|
|
/// One particularly useful place to use this is as the Key of a map. Dangling
|
|
|
|
/// pointer bugs often lead to really subtle bugs that only occur if another
|
|
|
|
/// object happens to get allocated to the same address as the old one. Using
|
|
|
|
/// an AssertingVH ensures that an assert is triggered as soon as the bad
|
|
|
|
/// delete occurs.
|
2009-04-01 00:11:05 +02:00
|
|
|
///
|
|
|
|
/// Note that an AssertingVH handle does *not* follow values across RAUW
|
|
|
|
/// operations. This means that RAUW's need to explicitly update the
|
|
|
|
/// AssertingVH's as it moves. This is required because in non-assert mode this
|
2009-04-19 20:20:21 +02:00
|
|
|
/// class turns into a trivial wrapper around a pointer.
|
2009-04-03 01:53:03 +02:00
|
|
|
template <typename ValueTy>
|
2009-09-19 22:40:05 +02:00
|
|
|
class AssertingVH
|
2009-04-01 00:11:05 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
: public ValueHandleBase
|
|
|
|
#endif
|
|
|
|
{
|
2017-05-11 01:41:30 +02:00
|
|
|
friend struct DenseMapInfo<AssertingVH<ValueTy>>;
|
2009-04-01 00:11:05 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2015-01-10 00:17:25 +01:00
|
|
|
Value *getRawValPtr() const { return ValueHandleBase::getValPtr(); }
|
|
|
|
void setRawValPtr(Value *P) { ValueHandleBase::operator=(P); }
|
2009-04-01 00:11:05 +02:00
|
|
|
#else
|
2015-01-10 00:17:25 +01:00
|
|
|
Value *ThePtr;
|
|
|
|
Value *getRawValPtr() const { return ThePtr; }
|
|
|
|
void setRawValPtr(Value *P) { ThePtr = P; }
|
2009-04-01 00:11:05 +02:00
|
|
|
#endif
|
2015-01-10 00:17:25 +01:00
|
|
|
// Convert a ValueTy*, which may be const, to the raw Value*.
|
2009-08-07 21:54:29 +02:00
|
|
|
static Value *GetAsValue(Value *V) { return V; }
|
|
|
|
static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
|
|
|
|
|
2015-01-10 00:17:25 +01:00
|
|
|
ValueTy *getValPtr() const { return static_cast<ValueTy *>(getRawValPtr()); }
|
|
|
|
void setValPtr(ValueTy *P) { setRawValPtr(GetAsValue(P)); }
|
|
|
|
|
2009-04-01 00:11:05 +02:00
|
|
|
public:
|
|
|
|
#ifndef NDEBUG
|
|
|
|
AssertingVH() : ValueHandleBase(Assert) {}
|
2009-08-07 21:54:29 +02:00
|
|
|
AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
|
2009-04-01 00:11:05 +02:00
|
|
|
AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
|
|
|
|
#else
|
2014-04-24 08:44:33 +02:00
|
|
|
AssertingVH() : ThePtr(nullptr) {}
|
2015-01-10 00:17:25 +01:00
|
|
|
AssertingVH(ValueTy *P) : ThePtr(GetAsValue(P)) {}
|
2009-04-01 00:11:05 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
operator ValueTy*() const {
|
|
|
|
return getValPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueTy *operator=(ValueTy *RHS) {
|
|
|
|
setValPtr(RHS);
|
|
|
|
return getValPtr();
|
|
|
|
}
|
2009-09-19 22:39:50 +02:00
|
|
|
ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
|
2009-04-01 00:11:05 +02:00
|
|
|
setValPtr(RHS.getValPtr());
|
|
|
|
return getValPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueTy *operator->() const { return getValPtr(); }
|
2009-04-03 01:53:03 +02:00
|
|
|
ValueTy &operator*() const { return *getValPtr(); }
|
2009-04-01 00:11:05 +02:00
|
|
|
};
|
|
|
|
|
2009-10-19 20:49:59 +02:00
|
|
|
// Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
|
|
|
|
template<typename T>
|
2017-05-11 01:41:30 +02:00
|
|
|
struct DenseMapInfo<AssertingVH<T>> {
|
2009-10-19 20:49:59 +02:00
|
|
|
static inline AssertingVH<T> getEmptyKey() {
|
2015-01-10 00:17:25 +01:00
|
|
|
AssertingVH<T> Res;
|
|
|
|
Res.setRawValPtr(DenseMapInfo<Value *>::getEmptyKey());
|
|
|
|
return Res;
|
2009-10-19 20:49:59 +02:00
|
|
|
}
|
2017-05-11 01:41:30 +02:00
|
|
|
|
2015-01-10 00:17:25 +01:00
|
|
|
static inline AssertingVH<T> getTombstoneKey() {
|
|
|
|
AssertingVH<T> Res;
|
|
|
|
Res.setRawValPtr(DenseMapInfo<Value *>::getTombstoneKey());
|
|
|
|
return Res;
|
2009-10-19 20:49:59 +02:00
|
|
|
}
|
2017-05-11 01:41:30 +02:00
|
|
|
|
2009-10-19 20:49:59 +02:00
|
|
|
static unsigned getHashValue(const AssertingVH<T> &Val) {
|
2015-01-10 00:17:25 +01:00
|
|
|
return DenseMapInfo<Value *>::getHashValue(Val.getRawValPtr());
|
2009-10-19 20:49:59 +02:00
|
|
|
}
|
2017-05-11 01:41:30 +02:00
|
|
|
|
2014-09-03 20:11:48 +02:00
|
|
|
static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
|
2015-01-10 00:17:25 +01:00
|
|
|
return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
|
|
|
|
RHS.getRawValPtr());
|
2014-09-03 20:11:48 +02:00
|
|
|
}
|
2009-12-15 08:26:43 +01:00
|
|
|
};
|
2014-09-03 20:11:48 +02:00
|
|
|
|
2009-12-15 08:26:43 +01:00
|
|
|
template <typename T>
|
2017-05-11 01:41:30 +02:00
|
|
|
struct isPodLike<AssertingVH<T>> {
|
2009-10-19 20:49:59 +02:00
|
|
|
#ifdef NDEBUG
|
2009-12-15 08:26:43 +01:00
|
|
|
static const bool value = true;
|
2009-10-19 20:49:59 +02:00
|
|
|
#else
|
2009-12-15 08:26:43 +01:00
|
|
|
static const bool value = false;
|
2009-10-19 20:49:59 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2014-10-15 22:28:31 +02:00
|
|
|
/// \brief Value handle that tracks a Value across RAUW.
|
2009-09-22 04:02:33 +02:00
|
|
|
///
|
|
|
|
/// TrackingVH is designed for situations where a client needs to hold a handle
|
|
|
|
/// to a Value (or subclass) across some operations which may move that value,
|
|
|
|
/// but should never destroy it or replace it with some unacceptable type.
|
|
|
|
///
|
|
|
|
/// It is an error to attempt to replace a value with one of a type which is
|
|
|
|
/// incompatible with any of its outstanding TrackingVHs.
|
2017-05-01 18:28:58 +02:00
|
|
|
///
|
|
|
|
/// It is an error to read from a TrackingVH that does not point to a valid
|
|
|
|
/// value. A TrackingVH is said to not point to a valid value if either it
|
|
|
|
/// hasn't yet been assigned a value yet or because the value it was tracking
|
|
|
|
/// has since been deleted.
|
|
|
|
///
|
|
|
|
/// Assigning a value to a TrackingVH is always allowed, even if said TrackingVH
|
|
|
|
/// no longer points to a valid value.
|
|
|
|
template <typename ValueTy> class TrackingVH {
|
2017-05-01 19:07:49 +02:00
|
|
|
WeakTrackingVH InnerHandle;
|
2009-09-22 04:02:33 +02:00
|
|
|
|
2017-05-01 18:28:58 +02:00
|
|
|
public:
|
|
|
|
ValueTy *getValPtr() const {
|
|
|
|
assert(InnerHandle.pointsToAliveValue() &&
|
|
|
|
"TrackingVH must be non-null and valid on dereference!");
|
2009-09-22 04:02:33 +02:00
|
|
|
|
|
|
|
// Check that the value is a member of the correct subclass. We would like
|
|
|
|
// to check this property on assignment for better debugging, but we don't
|
|
|
|
// want to require a virtual interface on this VH. Instead we allow RAUW to
|
|
|
|
// replace this value with a value of an invalid type, and check it here.
|
2017-05-01 18:28:58 +02:00
|
|
|
assert(isa<ValueTy>(InnerHandle) &&
|
2009-09-22 04:02:33 +02:00
|
|
|
"Tracked Value was replaced by one with an invalid type!");
|
2017-05-01 18:28:58 +02:00
|
|
|
return cast<ValueTy>(InnerHandle);
|
2009-09-22 04:02:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void setValPtr(ValueTy *P) {
|
2017-05-01 18:28:58 +02:00
|
|
|
// Assigning to non-valid TrackingVH's are fine so we just unconditionally
|
|
|
|
// assign here.
|
|
|
|
InnerHandle = GetAsValue(P);
|
2009-09-22 04:02:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a ValueTy*, which may be const, to the type the base
|
|
|
|
// class expects.
|
|
|
|
static Value *GetAsValue(Value *V) { return V; }
|
|
|
|
static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
|
|
|
|
|
|
|
|
public:
|
2017-05-11 01:41:30 +02:00
|
|
|
TrackingVH() = default;
|
2017-05-01 18:28:58 +02:00
|
|
|
TrackingVH(ValueTy *P) { setValPtr(P); }
|
2009-09-22 04:02:33 +02:00
|
|
|
|
|
|
|
operator ValueTy*() const {
|
|
|
|
return getValPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueTy *operator=(ValueTy *RHS) {
|
|
|
|
setValPtr(RHS);
|
|
|
|
return getValPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueTy *operator->() const { return getValPtr(); }
|
|
|
|
ValueTy &operator*() const { return *getValPtr(); }
|
|
|
|
};
|
|
|
|
|
2014-10-15 22:28:31 +02:00
|
|
|
/// \brief Value handle with callbacks on RAUW and destruction.
|
|
|
|
///
|
|
|
|
/// This is a value handle that allows subclasses to define callbacks that run
|
|
|
|
/// when the underlying Value has RAUW called on it or is destroyed. This
|
|
|
|
/// class can be used as the key of a map, as long as the user takes it out of
|
|
|
|
/// the map before calling setValPtr() (since the map has to rearrange itself
|
2015-08-04 00:33:50 +02:00
|
|
|
/// when the pointer changes). Unlike ValueHandleBase, this class has a vtable.
|
2009-05-02 23:10:48 +02:00
|
|
|
class CallbackVH : public ValueHandleBase {
|
2013-11-19 01:57:56 +01:00
|
|
|
virtual void anchor();
|
2009-05-02 23:10:48 +02:00
|
|
|
protected:
|
2015-08-04 00:30:24 +02:00
|
|
|
~CallbackVH() = default;
|
|
|
|
CallbackVH(const CallbackVH &) = default;
|
|
|
|
CallbackVH &operator=(const CallbackVH &) = default;
|
2009-05-02 23:10:48 +02:00
|
|
|
|
|
|
|
void setValPtr(Value *P) {
|
|
|
|
ValueHandleBase::operator=(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
CallbackVH() : ValueHandleBase(Callback) {}
|
|
|
|
CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
|
|
|
|
|
|
|
|
operator Value*() const {
|
|
|
|
return getValPtr();
|
|
|
|
}
|
|
|
|
|
2014-10-15 22:28:31 +02:00
|
|
|
/// \brief Callback for Value destruction.
|
|
|
|
///
|
|
|
|
/// Called when this->getValPtr() is destroyed, inside ~Value(), so you
|
|
|
|
/// may call any non-virtual Value method on getValPtr(), but no subclass
|
2017-05-01 19:07:49 +02:00
|
|
|
/// methods. If WeakTrackingVH were implemented as a CallbackVH, it would use
|
|
|
|
/// this
|
2017-04-26 18:37:05 +02:00
|
|
|
/// method to call setValPtr(NULL). AssertingVH would use this method to
|
2014-10-15 22:28:31 +02:00
|
|
|
/// cause an assertion failure.
|
2009-05-02 23:10:48 +02:00
|
|
|
///
|
|
|
|
/// All implementations must remove the reference from this object to the
|
|
|
|
/// Value that's being destroyed.
|
2014-04-09 08:08:46 +02:00
|
|
|
virtual void deleted() { setValPtr(nullptr); }
|
2009-05-02 23:10:48 +02:00
|
|
|
|
2014-10-15 22:28:31 +02:00
|
|
|
/// \brief Callback for Value RAUW.
|
|
|
|
///
|
2009-05-02 23:10:48 +02:00
|
|
|
/// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
|
2017-05-01 19:07:49 +02:00
|
|
|
/// _before_ any of the uses have actually been replaced. If WeakTrackingVH
|
|
|
|
/// were
|
2017-04-26 18:37:05 +02:00
|
|
|
/// implemented as a CallbackVH, it would use this method to call
|
2009-05-02 23:10:48 +02:00
|
|
|
/// setValPtr(new_value). AssertingVH would do nothing in this method.
|
2013-11-19 01:57:56 +01:00
|
|
|
virtual void allUsesReplacedWith(Value *) {}
|
2009-05-02 23:10:48 +02:00
|
|
|
};
|
|
|
|
|
2017-01-24 13:34:47 +01:00
|
|
|
/// Value handle that poisons itself if the Value is deleted.
|
|
|
|
///
|
|
|
|
/// This is a Value Handle that points to a value and poisons itself if the
|
|
|
|
/// value is destroyed while the handle is still live. This is very useful for
|
|
|
|
/// catching dangling pointer bugs where an \c AssertingVH cannot be used
|
|
|
|
/// because the dangling handle needs to outlive the value without ever being
|
|
|
|
/// used.
|
|
|
|
///
|
|
|
|
/// One particularly useful place to use this is as the Key of a map. Dangling
|
|
|
|
/// pointer bugs often lead to really subtle bugs that only occur if another
|
|
|
|
/// object happens to get allocated to the same address as the old one. Using
|
|
|
|
/// a PoisoningVH ensures that an assert is triggered if looking up a new value
|
|
|
|
/// in the map finds a handle from the old value.
|
|
|
|
///
|
|
|
|
/// Note that a PoisoningVH handle does *not* follow values across RAUW
|
|
|
|
/// operations. This means that RAUW's need to explicitly update the
|
|
|
|
/// PoisoningVH's as it moves. This is required because in non-assert mode this
|
|
|
|
/// class turns into a trivial wrapper around a pointer.
|
|
|
|
template <typename ValueTy>
|
|
|
|
class PoisoningVH
|
|
|
|
#ifndef NDEBUG
|
|
|
|
final : public CallbackVH
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
friend struct DenseMapInfo<PoisoningVH<ValueTy>>;
|
|
|
|
|
|
|
|
// Convert a ValueTy*, which may be const, to the raw Value*.
|
|
|
|
static Value *GetAsValue(Value *V) { return V; }
|
|
|
|
static Value *GetAsValue(const Value *V) { return const_cast<Value *>(V); }
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
/// A flag tracking whether this value has been poisoned.
|
|
|
|
///
|
|
|
|
/// On delete and RAUW, we leave the value pointer alone so that as a raw
|
|
|
|
/// pointer it produces the same value (and we fit into the same key of
|
|
|
|
/// a hash table, etc), but we poison the handle so that any top-level usage
|
|
|
|
/// will fail.
|
|
|
|
bool Poisoned = false;
|
|
|
|
|
|
|
|
Value *getRawValPtr() const { return ValueHandleBase::getValPtr(); }
|
|
|
|
void setRawValPtr(Value *P) { ValueHandleBase::operator=(P); }
|
|
|
|
|
|
|
|
/// Handle deletion by poisoning the handle.
|
|
|
|
void deleted() override {
|
|
|
|
assert(!Poisoned && "Tried to delete an already poisoned handle!");
|
|
|
|
Poisoned = true;
|
|
|
|
RemoveFromUseList();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle RAUW by poisoning the handle.
|
|
|
|
void allUsesReplacedWith(Value *) override {
|
|
|
|
assert(!Poisoned && "Tried to RAUW an already poisoned handle!");
|
|
|
|
Poisoned = true;
|
|
|
|
RemoveFromUseList();
|
|
|
|
}
|
|
|
|
#else // NDEBUG
|
|
|
|
Value *ThePtr = nullptr;
|
|
|
|
|
|
|
|
Value *getRawValPtr() const { return ThePtr; }
|
|
|
|
void setRawValPtr(Value *P) { ThePtr = P; }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ValueTy *getValPtr() const {
|
|
|
|
assert(!Poisoned && "Accessed a poisoned value handle!");
|
|
|
|
return static_cast<ValueTy *>(getRawValPtr());
|
|
|
|
}
|
|
|
|
void setValPtr(ValueTy *P) { setRawValPtr(GetAsValue(P)); }
|
|
|
|
|
|
|
|
public:
|
|
|
|
PoisoningVH() = default;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
PoisoningVH(ValueTy *P) : CallbackVH(GetAsValue(P)) {}
|
|
|
|
PoisoningVH(const PoisoningVH &RHS)
|
|
|
|
: CallbackVH(RHS), Poisoned(RHS.Poisoned) {}
|
2017-05-11 01:41:30 +02:00
|
|
|
|
2017-01-24 13:34:47 +01:00
|
|
|
~PoisoningVH() {
|
|
|
|
if (Poisoned)
|
|
|
|
clearValPtr();
|
|
|
|
}
|
2017-05-11 01:41:30 +02:00
|
|
|
|
2017-01-24 13:34:47 +01:00
|
|
|
PoisoningVH &operator=(const PoisoningVH &RHS) {
|
|
|
|
if (Poisoned)
|
|
|
|
clearValPtr();
|
|
|
|
CallbackVH::operator=(RHS);
|
|
|
|
Poisoned = RHS.Poisoned;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
PoisoningVH(ValueTy *P) : ThePtr(GetAsValue(P)) {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
operator ValueTy *() const { return getValPtr(); }
|
|
|
|
|
|
|
|
ValueTy *operator->() const { return getValPtr(); }
|
|
|
|
ValueTy &operator*() const { return *getValPtr(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Specialize DenseMapInfo to allow PoisoningVH to participate in DenseMap.
|
|
|
|
template <typename T> struct DenseMapInfo<PoisoningVH<T>> {
|
|
|
|
static inline PoisoningVH<T> getEmptyKey() {
|
|
|
|
PoisoningVH<T> Res;
|
|
|
|
Res.setRawValPtr(DenseMapInfo<Value *>::getEmptyKey());
|
|
|
|
return Res;
|
|
|
|
}
|
2017-05-11 01:41:30 +02:00
|
|
|
|
2017-01-24 13:34:47 +01:00
|
|
|
static inline PoisoningVH<T> getTombstoneKey() {
|
|
|
|
PoisoningVH<T> Res;
|
|
|
|
Res.setRawValPtr(DenseMapInfo<Value *>::getTombstoneKey());
|
|
|
|
return Res;
|
|
|
|
}
|
2017-05-11 01:41:30 +02:00
|
|
|
|
2017-01-24 13:34:47 +01:00
|
|
|
static unsigned getHashValue(const PoisoningVH<T> &Val) {
|
|
|
|
return DenseMapInfo<Value *>::getHashValue(Val.getRawValPtr());
|
|
|
|
}
|
2017-05-11 01:41:30 +02:00
|
|
|
|
2017-01-24 13:34:47 +01:00
|
|
|
static bool isEqual(const PoisoningVH<T> &LHS, const PoisoningVH<T> &RHS) {
|
|
|
|
return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
|
|
|
|
RHS.getRawValPtr());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> struct isPodLike<PoisoningVH<T>> {
|
|
|
|
#ifdef NDEBUG
|
|
|
|
static const bool value = true;
|
|
|
|
#else
|
|
|
|
static const bool value = false;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2017-05-11 01:41:30 +02:00
|
|
|
} // end namespace llvm
|
2009-04-01 00:11:05 +02:00
|
|
|
|
2017-05-11 01:41:30 +02:00
|
|
|
#endif // LLVM_IR_VALUEHANDLE_H
|