[PATCH] D81793: [IR] Support efficient lookup of AssertingVH/PoisoningVH

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 14 07:29:11 PDT 2020


nikic updated this revision to Diff 270619.
nikic marked an inline comment as done.
nikic added a comment.

Remove unnecessary public, add some basic tests. These essentially just check that AssertingVH/PoisoningVH inside a DenseMap work, because I don't know how I could test that using find_as() does not construct a temporary value handle.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81793/new/

https://reviews.llvm.org/D81793

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


Index: llvm/unittests/IR/ValueHandleTest.cpp
===================================================================
--- llvm/unittests/IR/ValueHandleTest.cpp
+++ llvm/unittests/IR/ValueHandleTest.cpp
@@ -491,6 +491,30 @@
   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) {
Index: llvm/include/llvm/IR/ValueHandle.h
===================================================================
--- llvm/include/llvm/IR/ValueHandle.h
+++ llvm/include/llvm/IR/ValueHandle.h
@@ -307,30 +307,10 @@
   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 @@
     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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81793.270619.patch
Type: text/x-patch
Size: 3235 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200614/5abaf9b0/attachment.bin>


More information about the llvm-commits mailing list