[llvm] d2de309 - [ADT] Swap the two variants of DenseMap::doFind (NFC) (#155203)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 25 07:33:29 PDT 2025


Author: Kazu Hirata
Date: 2025-08-25T07:33:26-07:00
New Revision: d2de3096b0f1fd4f0f470e34135523c65ad9c6b0

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

LOG: [ADT] Swap the two variants of DenseMap::doFind (NFC) (#155203)

doFind itself makes no modification, so we can implement it as a const
function.  The only problem is that the non-const version of find
needs to return a modifiable bucket.

This patch "swaps" the constness of doFind.  Specifically, the primary
implementation becomes const, preventing accidental modifications.
Then the non-const variant is derived off of the const variant.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/DenseMap.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index ab1bc6356dcb9..0fa911a229c92 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -629,8 +629,9 @@ class DenseMapBase : public DebugEpochBase {
     return TheBucket;
   }
 
-  template <typename LookupKeyT> BucketT *doFind(const LookupKeyT &Val) {
-    BucketT *BucketsPtr = getBuckets();
+  template <typename LookupKeyT>
+  const BucketT *doFind(const LookupKeyT &Val) const {
+    const BucketT *BucketsPtr = getBuckets();
     const unsigned NumBuckets = getNumBuckets();
     if (NumBuckets == 0)
       return nullptr;
@@ -639,7 +640,7 @@ class DenseMapBase : public DebugEpochBase {
     unsigned BucketNo = getHashValue(Val) & (NumBuckets - 1);
     unsigned ProbeAmt = 1;
     while (true) {
-      BucketT *Bucket = BucketsPtr + BucketNo;
+      const BucketT *Bucket = BucketsPtr + BucketNo;
       if (LLVM_LIKELY(KeyInfoT::isEqual(Val, Bucket->getFirst())))
         return Bucket;
       if (LLVM_LIKELY(KeyInfoT::isEqual(Bucket->getFirst(), EmptyKey)))
@@ -652,9 +653,9 @@ class DenseMapBase : public DebugEpochBase {
     }
   }
 
-  template <typename LookupKeyT>
-  const BucketT *doFind(const LookupKeyT &Val) const {
-    return const_cast<DenseMapBase *>(this)->doFind(Val); // NOLINT
+  template <typename LookupKeyT> BucketT *doFind(const LookupKeyT &Val) {
+    return const_cast<BucketT *>(
+        static_cast<const DenseMapBase *>(this)->doFind(Val));
   }
 
   /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in


        


More information about the llvm-commits mailing list