#ifndef LLVM_SUPPORT_VALUEHANDLE_H
#define LLVM_SUPPORT_VALUEHANDLE_H
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/Value.h"
namespace llvm {
class ValueHandleBase;
template<>
class PointerLikeTypeTraits<ValueHandleBase**> {
public:
static inline void *getAsVoidPointer(ValueHandleBase** P) { return P; }
static inline ValueHandleBase **getFromVoidPointer(void *P) {
return static_cast<ValueHandleBase**>(P);
}
enum { NumLowBitsAvailable = 2 };
};
class ValueHandleBase {
friend class Value;
protected:
enum HandleBaseKind {
Assert,
Callback,
Tracking,
Weak
};
private:
PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
ValueHandleBase *Next;
Value *VP;
explicit ValueHandleBase(const ValueHandleBase&); public:
explicit ValueHandleBase(HandleBaseKind Kind)
: PrevPair(0, Kind), Next(0), VP(0) {}
ValueHandleBase(HandleBaseKind Kind, Value *V)
: PrevPair(0, Kind), Next(0), VP(V) {
if (isValid(VP))
AddToUseList();
}
ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
: PrevPair(0, Kind), Next(0), VP(RHS.VP) {
if (isValid(VP))
AddToExistingUseList(RHS.getPrevPtr());
}
~ValueHandleBase() {
if (isValid(VP))
RemoveFromUseList();
}
Value *operator=(Value *RHS) {
if (VP == RHS) return RHS;
if (isValid(VP)) RemoveFromUseList();
VP = RHS;
if (isValid(VP)) AddToUseList();
return RHS;
}
Value *operator=(const ValueHandleBase &RHS) {
if (VP == RHS.VP) return RHS.VP;
if (isValid(VP)) RemoveFromUseList();
VP = RHS.VP;
if (isValid(VP)) AddToExistingUseList(RHS.getPrevPtr());
return VP;
}
Value *operator->() const { return getValPtr(); }
Value &operator*() const { return *getValPtr(); }
protected:
Value *getValPtr() const { return VP; }
static bool isValid(Value *V) {
return V &&
V != DenseMapInfo<Value *>::getEmptyKey() &&
V != DenseMapInfo<Value *>::getTombstoneKey();
}
private:
static void ValueIsDeleted(Value *V);
static void ValueIsRAUWd(Value *Old, Value *New);
ValueHandleBase **getPrevPtr() const { return PrevPair.getPointer(); }
HandleBaseKind getKind() const { return PrevPair.getInt(); }
void setPrevPtr(ValueHandleBase **Ptr) { PrevPair.setPointer(Ptr); }
void AddToExistingUseList(ValueHandleBase **List);
void AddToExistingUseListAfter(ValueHandleBase *Node);
void AddToUseList();
void RemoveFromUseList();
};
class WeakVH : public ValueHandleBase {
public:
WeakVH() : ValueHandleBase(Weak) {}
WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
WeakVH(const WeakVH &RHS)
: ValueHandleBase(Weak, RHS) {}
Value *operator=(Value *RHS) {
return ValueHandleBase::operator=(RHS);
}
Value *operator=(const ValueHandleBase &RHS) {
return ValueHandleBase::operator=(RHS);
}
operator Value*() const {
return getValPtr();
}
};
template<typename From> struct simplify_type;
template<> struct simplify_type<const WeakVH> {
typedef Value* SimpleType;
static SimpleType getSimplifiedValue(const WeakVH &WVH) {
return static_cast<Value *>(WVH);
}
};
template<> struct simplify_type<WeakVH> : public simplify_type<const WeakVH> {};
template <typename ValueTy>
class AssertingVH
#ifndef NDEBUG
: public ValueHandleBase
#endif
{
#ifndef NDEBUG
ValueTy *getValPtr() const {
return static_cast<ValueTy*>(ValueHandleBase::getValPtr());
}
void setValPtr(ValueTy *P) {
ValueHandleBase::operator=(GetAsValue(P));
}
#else
ValueTy *ThePtr;
ValueTy *getValPtr() const { return ThePtr; }
void setValPtr(ValueTy *P) { ThePtr = P; }
#endif
static Value *GetAsValue(Value *V) { return V; }
static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
public:
#ifndef NDEBUG
AssertingVH() : ValueHandleBase(Assert) {}
AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
#else
AssertingVH() : ThePtr(0) {}
AssertingVH(ValueTy *P) : ThePtr(P) {}
#endif
operator ValueTy*() const {
return getValPtr();
}
ValueTy *operator=(ValueTy *RHS) {
setValPtr(RHS);
return getValPtr();
}
ValueTy *operator=(const AssertingVH<ValueTy> &RHS) {
setValPtr(RHS.getValPtr());
return getValPtr();
}
ValueTy *operator->() const { return getValPtr(); }
ValueTy &operator*() const { return *getValPtr(); }
};
template<typename From> struct simplify_type;
template<> struct simplify_type<const AssertingVH<Value> > {
typedef Value* SimpleType;
static SimpleType getSimplifiedValue(const AssertingVH<Value> &AVH) {
return static_cast<Value *>(AVH);
}
};
template<> struct simplify_type<AssertingVH<Value> >
: public simplify_type<const AssertingVH<Value> > {};
template<typename T>
struct DenseMapInfo<AssertingVH<T> > {
typedef DenseMapInfo<T*> PointerInfo;
static inline AssertingVH<T> getEmptyKey() {
return AssertingVH<T>(PointerInfo::getEmptyKey());
}
static inline T* getTombstoneKey() {
return AssertingVH<T>(PointerInfo::getTombstoneKey());
}
static unsigned getHashValue(const AssertingVH<T> &Val) {
return PointerInfo::getHashValue(Val);
}
static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
return LHS == RHS;
}
};
template <typename T>
struct isPodLike<AssertingVH<T> > {
#ifdef NDEBUG
static const bool value = true;
#else
static const bool value = false;
#endif
};
template<typename ValueTy>
class TrackingVH : public ValueHandleBase {
void CheckValidity() const {
Value *VP = ValueHandleBase::getValPtr();
if (!VP) return;
assert(ValueHandleBase::isValid(VP) && "Tracked Value was deleted!");
assert(isa<ValueTy>(VP) &&
"Tracked Value was replaced by one with an invalid type!");
}
ValueTy *getValPtr() const {
CheckValidity();
return (ValueTy*)ValueHandleBase::getValPtr();
}
void setValPtr(ValueTy *P) {
CheckValidity();
ValueHandleBase::operator=(GetAsValue(P));
}
static Value *GetAsValue(Value *V) { return V; }
static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
public:
TrackingVH() : ValueHandleBase(Tracking) {}
TrackingVH(ValueTy *P) : ValueHandleBase(Tracking, GetAsValue(P)) {}
TrackingVH(const TrackingVH &RHS) : ValueHandleBase(Tracking, RHS) {}
operator ValueTy*() const {
return getValPtr();
}
ValueTy *operator=(ValueTy *RHS) {
setValPtr(RHS);
return getValPtr();
}
ValueTy *operator=(const TrackingVH<ValueTy> &RHS) {
setValPtr(RHS.getValPtr());
return getValPtr();
}
ValueTy *operator->() const { return getValPtr(); }
ValueTy &operator*() const { return *getValPtr(); }
};
template<typename From> struct simplify_type;
template<> struct simplify_type<const TrackingVH<Value> > {
typedef Value* SimpleType;
static SimpleType getSimplifiedValue(const TrackingVH<Value> &AVH) {
return static_cast<Value *>(AVH);
}
};
template<> struct simplify_type<TrackingVH<Value> >
: public simplify_type<const TrackingVH<Value> > {};
class CallbackVH : public ValueHandleBase {
protected:
CallbackVH(const CallbackVH &RHS)
: ValueHandleBase(Callback, RHS) {}
virtual ~CallbackVH();
void setValPtr(Value *P) {
ValueHandleBase::operator=(P);
}
public:
CallbackVH() : ValueHandleBase(Callback) {}
CallbackVH(Value *P) : ValueHandleBase(Callback, P) {}
operator Value*() const {
return getValPtr();
}
virtual void deleted() {
setValPtr(NULL);
}
virtual void allUsesReplacedWith(Value *) {}
};
template<typename From> struct simplify_type;
template<> struct simplify_type<const CallbackVH> {
typedef Value* SimpleType;
static SimpleType getSimplifiedValue(const CallbackVH &CVH) {
return static_cast<Value *>(CVH);
}
};
template<> struct simplify_type<CallbackVH>
: public simplify_type<const CallbackVH> {};
}
#endif