[llvm] [ADT] Use a range-based for loop in DenseMap.h (NFC) (PR #152084)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 4 22:56:26 PDT 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/152084

This patch introduces inlineBuckets to convert a loop into a
range-based for loop.


>From 0405c01fcacb666d9ce2c49742f4589888fd9d21 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 4 Aug 2025 09:45:14 -0700
Subject: [PATCH] [ADT] Use a range-based for loop in DenseMap.h (NFC)

This patch introduces inlineBuckets to convert a loop into a
range-based for loop.
---
 llvm/include/llvm/ADT/DenseMap.h | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 9557c9d8e34a1..ea5eac4cc63f0 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -1109,17 +1109,17 @@ class SmallDenseMap
       // temporary storage. Have the loop move the TmpEnd forward as it goes.
       const KeyT EmptyKey = this->getEmptyKey();
       const KeyT TombstoneKey = this->getTombstoneKey();
-      for (BucketT *P = getBuckets(), *E = P + InlineBuckets; P != E; ++P) {
-        if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
-            !KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
+      for (BucketT &B : inlineBuckets()) {
+        if (!KeyInfoT::isEqual(B.getFirst(), EmptyKey) &&
+            !KeyInfoT::isEqual(B.getFirst(), TombstoneKey)) {
           assert(size_t(TmpEnd - TmpBegin) < InlineBuckets &&
                  "Too many inline buckets!");
-          ::new (&TmpEnd->getFirst()) KeyT(std::move(P->getFirst()));
-          ::new (&TmpEnd->getSecond()) ValueT(std::move(P->getSecond()));
+          ::new (&TmpEnd->getFirst()) KeyT(std::move(B.getFirst()));
+          ::new (&TmpEnd->getSecond()) ValueT(std::move(B.getSecond()));
           ++TmpEnd;
-          P->getSecond().~ValueT();
+          B.getSecond().~ValueT();
         }
-        P->getFirst().~KeyT();
+        B.getFirst().~KeyT();
       }
 
       // AtLeast == InlineBuckets can happen if there are many tombstones,
@@ -1220,6 +1220,11 @@ class SmallDenseMap
     return Small ? InlineBuckets : getLargeRep()->NumBuckets;
   }
 
+  iterator_range<BucketT *> inlineBuckets() {
+    BucketT *Begin = getInlineBuckets();
+    return llvm::make_range(Begin, Begin + InlineBuckets);
+  }
+
   void deallocateBuckets() {
     if (Small)
       return;



More information about the llvm-commits mailing list