1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-18 18:42:46 +02: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:
Nikita Popov 2020-06-13 21:10:22 +02:00
parent 400a0511f5
commit 4e890ca658
2 changed files with 38 additions and 23 deletions

View File

@ -307,30 +307,10 @@ public:
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>
struct DenseMapInfo<AssertingVH<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());
}
};
struct DenseMapInfo<AssertingVH<T>> : DenseMapInfo<T *> {};
/// 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(),
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

View File

@ -491,6 +491,30 @@ TEST_F(ValueHandle, PoisoningVH_DoesNotFollowRAUW) {
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
TEST_F(ValueHandle, PoisoningVH_ReducesToPointer) {