mirror of
https://github.com/RPCSX/llvm.git
synced 2024-11-27 21:50:40 +00:00
Add a new WeakVH value handle; NFC
This relands r301425. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@301813 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
399b4d037d
commit
41673c62ec
@ -34,7 +34,7 @@ protected:
|
|||||||
///
|
///
|
||||||
/// This is to avoid having a vtable for the light-weight handle pointers. The
|
/// This is to avoid having a vtable for the light-weight handle pointers. The
|
||||||
/// fully general Callback version does have a vtable.
|
/// fully general Callback version does have a vtable.
|
||||||
enum HandleBaseKind { Assert, Callback, WeakTracking };
|
enum HandleBaseKind { Assert, Callback, Weak, WeakTracking };
|
||||||
|
|
||||||
ValueHandleBase(const ValueHandleBase &RHS)
|
ValueHandleBase(const ValueHandleBase &RHS)
|
||||||
: ValueHandleBase(RHS.PrevPair.getInt(), RHS) {}
|
: ValueHandleBase(RHS.PrevPair.getInt(), RHS) {}
|
||||||
@ -46,7 +46,7 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
|
PointerIntPair<ValueHandleBase**, 3, HandleBaseKind> PrevPair;
|
||||||
ValueHandleBase *Next;
|
ValueHandleBase *Next;
|
||||||
|
|
||||||
Value *Val;
|
Value *Val;
|
||||||
@ -134,6 +134,42 @@ private:
|
|||||||
void AddToUseList();
|
void AddToUseList();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// \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> {
|
||||||
|
typedef Value *SimpleType;
|
||||||
|
static SimpleType getSimplifiedValue(WeakVH &WVH) { return WVH; }
|
||||||
|
};
|
||||||
|
template <> struct simplify_type<const WeakVH> {
|
||||||
|
typedef Value *SimpleType;
|
||||||
|
static SimpleType getSimplifiedValue(const WeakVH &WVH) { return WVH; }
|
||||||
|
};
|
||||||
|
|
||||||
/// \brief Value handle that is nullable, but tries to track the Value.
|
/// \brief Value handle that is nullable, but tries to track the Value.
|
||||||
///
|
///
|
||||||
/// This is a value handle that tries hard to point to a Value, even across
|
/// This is a value handle that tries hard to point to a Value, even across
|
||||||
|
@ -820,8 +820,10 @@ void ValueHandleBase::ValueIsDeleted(Value *V) {
|
|||||||
switch (Entry->getKind()) {
|
switch (Entry->getKind()) {
|
||||||
case Assert:
|
case Assert:
|
||||||
break;
|
break;
|
||||||
|
case Weak:
|
||||||
case WeakTracking:
|
case WeakTracking:
|
||||||
// WeakTracking just goes to null, which will unlink it from the list.
|
// WeakTracking and Weak just go to null, which unlinks them
|
||||||
|
// from the list.
|
||||||
Entry->operator=(nullptr);
|
Entry->operator=(nullptr);
|
||||||
break;
|
break;
|
||||||
case Callback:
|
case Callback:
|
||||||
@ -869,7 +871,8 @@ void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
|
|||||||
|
|
||||||
switch (Entry->getKind()) {
|
switch (Entry->getKind()) {
|
||||||
case Assert:
|
case Assert:
|
||||||
// Asserting handle does not follow RAUW implicitly.
|
case Weak:
|
||||||
|
// Asserting and Weak handles do not follow RAUW implicitly.
|
||||||
break;
|
break;
|
||||||
case WeakTracking:
|
case WeakTracking:
|
||||||
// Weak goes to the new value, which will unlink it from Old's list.
|
// Weak goes to the new value, which will unlink it from Old's list.
|
||||||
|
@ -34,6 +34,24 @@ public:
|
|||||||
ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
|
ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, WeakVH_BasicOperation) {
|
||||||
|
WeakVH WVH(BitcastV.get());
|
||||||
|
EXPECT_EQ(BitcastV.get(), WVH);
|
||||||
|
WVH = ConstantV;
|
||||||
|
EXPECT_EQ(ConstantV, WVH);
|
||||||
|
|
||||||
|
// Make sure I can call a method on the underlying Value. It
|
||||||
|
// doesn't matter which method.
|
||||||
|
EXPECT_EQ(Type::getInt32Ty(Context), WVH->getType());
|
||||||
|
EXPECT_EQ(Type::getInt32Ty(Context), (*WVH).getType());
|
||||||
|
|
||||||
|
WVH = BitcastV.get();
|
||||||
|
BitcastV->replaceAllUsesWith(ConstantV);
|
||||||
|
EXPECT_EQ(WVH, BitcastV.get());
|
||||||
|
BitcastV.reset();
|
||||||
|
EXPECT_EQ(WVH, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(ValueHandle, WeakTrackingVH_BasicOperation) {
|
TEST_F(ValueHandle, WeakTrackingVH_BasicOperation) {
|
||||||
WeakTrackingVH WVH(BitcastV.get());
|
WeakTrackingVH WVH(BitcastV.get());
|
||||||
EXPECT_EQ(BitcastV.get(), WVH);
|
EXPECT_EQ(BitcastV.get(), WVH);
|
||||||
|
Loading…
Reference in New Issue
Block a user