[llvm] 5f565c0 - [IR] Support efficient AssertingVH/PoisoningVH lookup

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 14 13:04:56 PDT 2020


Author: Nikita Popov
Date: 2020-06-14T22:03:03+02:00
New Revision: 5f565c04194bedb6535ce002fb53aa2134d9ff25

URL: https://github.com/llvm/llvm-project/commit/5f565c04194bedb6535ce002fb53aa2134d9ff25
DIFF: https://github.com/llvm/llvm-project/commit/5f565c04194bedb6535ce002fb53aa2134d9ff25.diff

LOG: [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

Added: 
    

Modified: 
    llvm/include/llvm/IR/ValueHandle.h
    llvm/unittests/IR/ValueHandleTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/ValueHandle.h b/llvm/include/llvm/IR/ValueHandle.h
index 81438c6cdaca..badc1ca8d1f6 100644
--- a/llvm/include/llvm/IR/ValueHandle.h
+++ b/llvm/include/llvm/IR/ValueHandle.h
@@ -307,30 +307,10 @@ class AssertingVH
   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

diff  --git a/llvm/unittests/IR/ValueHandleTest.cpp b/llvm/unittests/IR/ValueHandleTest.cpp
index b799ff095806..1aed8e1a1ee7 100644
--- a/llvm/unittests/IR/ValueHandleTest.cpp
+++ b/llvm/unittests/IR/ValueHandleTest.cpp
@@ -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) {


        


More information about the llvm-commits mailing list