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

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 13 12:50:26 PDT 2020


nikic created this revision.
nikic added reviewers: chandlerc, MaskRay, jdoerfert.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81793

Files:
  llvm/include/llvm/IR/ValueHandle.h


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_by()
+// 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>> : public 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_by(), 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.270582.patch
Type: text/x-patch
Size: 1880 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200613/303b9179/attachment.bin>


More information about the llvm-commits mailing list