mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-01-31 20:51:52 +01:00
[IR] Support efficient AssertingVH/PoisoningVH lookup
Currently, there doesn't seem to be any way to look up a Value* in a map/set indexed by AssertingVH/PoisoningVH, without creating a value handle -- which is fairly expensive, because it involves adding the value handle to the use list and immediately removing it again. Using find_as(Value *) does not work (and is in fact worse than just using find(Value *)), because it will end up creating multiple value handles during the lookup itself. For AssertingVH, address this by simply using DenseMapInfo<T *> instead of manually implementing something. The AssertingVH<T> will now get coerced to T*, rather than the other way around. For PoisoningVH, add extra overloads of getHashValue() and isEqual() that accept a T* argument. This allows using find_as(Value *) to perform efficient lookups in assertion-enabled builds. Differential Revision: https://reviews.llvm.org/D81793
This commit is contained in:
parent
400a0511f5
commit
4e890ca658
@ -307,30 +307,10 @@ public:
|
|||||||
ValueTy &operator*() const { return *getValPtr(); }
|
ValueTy &operator*() const { return *getValPtr(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
|
// Treat AssertingVH<T> like T* inside maps. This also allows using find_as()
|
||||||
|
// to look up a value without constructing a value handle.
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct DenseMapInfo<AssertingVH<T>> {
|
struct DenseMapInfo<AssertingVH<T>> : DenseMapInfo<T *> {};
|
||||||
static inline AssertingVH<T> getEmptyKey() {
|
|
||||||
AssertingVH<T> Res;
|
|
||||||
Res.setRawValPtr(DenseMapInfo<Value *>::getEmptyKey());
|
|
||||||
return Res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline AssertingVH<T> getTombstoneKey() {
|
|
||||||
AssertingVH<T> Res;
|
|
||||||
Res.setRawValPtr(DenseMapInfo<Value *>::getTombstoneKey());
|
|
||||||
return Res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned getHashValue(const AssertingVH<T> &Val) {
|
|
||||||
return DenseMapInfo<Value *>::getHashValue(Val.getRawValPtr());
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
|
|
||||||
return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
|
|
||||||
RHS.getRawValPtr());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Value handle that tracks a Value across RAUW.
|
/// Value handle that tracks a Value across RAUW.
|
||||||
///
|
///
|
||||||
@ -562,6 +542,17 @@ template <typename T> struct DenseMapInfo<PoisoningVH<T>> {
|
|||||||
return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
|
return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
|
||||||
RHS.getRawValPtr());
|
RHS.getRawValPtr());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Allow lookup by T* via find_as(), without constructing a temporary
|
||||||
|
// value handle.
|
||||||
|
|
||||||
|
static unsigned getHashValue(const T *Val) {
|
||||||
|
return DenseMapInfo<Value *>::getHashValue(Val);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool isEqual(const T *LHS, const PoisoningVH<T> &RHS) {
|
||||||
|
return DenseMapInfo<Value *>::isEqual(LHS, RHS.getRawValPtr());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // end namespace llvm
|
} // end namespace llvm
|
||||||
|
@ -491,6 +491,30 @@ TEST_F(ValueHandle, PoisoningVH_DoesNotFollowRAUW) {
|
|||||||
EXPECT_TRUE(DenseMapInfo<PoisoningVH<Value>>::isEqual(VH, BitcastV.get()));
|
EXPECT_TRUE(DenseMapInfo<PoisoningVH<Value>>::isEqual(VH, BitcastV.get()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, AssertingVH_DenseMap) {
|
||||||
|
DenseMap<AssertingVH<Value>, int> Map;
|
||||||
|
Map.insert({BitcastV.get(), 1});
|
||||||
|
Map.insert({ConstantV, 2});
|
||||||
|
// These will create a temporary AssertingVH during lookup.
|
||||||
|
EXPECT_TRUE(Map.find(BitcastV.get()) != Map.end());
|
||||||
|
EXPECT_TRUE(Map.find(ConstantV) != Map.end());
|
||||||
|
// These will not create a temporary AssertingVH.
|
||||||
|
EXPECT_TRUE(Map.find_as(BitcastV.get()) != Map.end());
|
||||||
|
EXPECT_TRUE(Map.find_as(ConstantV) != Map.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ValueHandle, PoisoningVH_DenseMap) {
|
||||||
|
DenseMap<PoisoningVH<Value>, int> Map;
|
||||||
|
Map.insert({BitcastV.get(), 1});
|
||||||
|
Map.insert({ConstantV, 2});
|
||||||
|
// These will create a temporary PoisoningVH during lookup.
|
||||||
|
EXPECT_TRUE(Map.find(BitcastV.get()) != Map.end());
|
||||||
|
EXPECT_TRUE(Map.find(ConstantV) != Map.end());
|
||||||
|
// These will not create a temporary PoisoningVH.
|
||||||
|
EXPECT_TRUE(Map.find_as(BitcastV.get()) != Map.end());
|
||||||
|
EXPECT_TRUE(Map.find_as(ConstantV) != Map.end());
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef NDEBUG
|
#ifdef NDEBUG
|
||||||
|
|
||||||
TEST_F(ValueHandle, PoisoningVH_ReducesToPointer) {
|
TEST_F(ValueHandle, PoisoningVH_ReducesToPointer) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user