[llvm] [ADT] Use range-based for loops in SparseBitVector.h (NFC) (PR #158408)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 13 09:29:46 PDT 2025


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/158408

>From cd87122d6b11c5e35d4bc52c9cb8dae3b8c82c05 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 3 Sep 2025 23:50:37 -0700
Subject: [PATCH 1/2] [ADT] Use range-based for loops in SparseBitVector.h
 (NFC)

---
 llvm/include/llvm/ADT/SparseBitVector.h | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/ADT/SparseBitVector.h b/llvm/include/llvm/ADT/SparseBitVector.h
index 7151af6146e6e..35b3c25db5077 100644
--- a/llvm/include/llvm/ADT/SparseBitVector.h
+++ b/llvm/include/llvm/ADT/SparseBitVector.h
@@ -119,8 +119,8 @@ template <unsigned ElementSize = 128> struct SparseBitVectorElement {
 
   size_type count() const {
     unsigned NumBits = 0;
-    for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
-      NumBits += llvm::popcount(Bits[i]);
+    for (BitWord Bit : Bits)
+      NumBits += llvm::popcount(Bit);
     return NumBits;
   }
 
@@ -799,11 +799,8 @@ class SparseBitVector {
 
   unsigned count() const {
     unsigned BitCount = 0;
-    for (ElementListConstIter Iter = Elements.begin();
-         Iter != Elements.end();
-         ++Iter)
-      BitCount += Iter->count();
-
+    for (const auto &Elem : Elements)
+      BitCount += Elem.count();
     return BitCount;
   }
 

>From 3a16a9abdb807270dd2cdd8bac11302d4b9bb939 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 13 Sep 2025 09:17:15 -0700
Subject: [PATCH 2/2] Address a comment.

---
 llvm/include/llvm/ADT/SparseBitVector.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/SparseBitVector.h b/llvm/include/llvm/ADT/SparseBitVector.h
index 35b3c25db5077..90e2336f9f488 100644
--- a/llvm/include/llvm/ADT/SparseBitVector.h
+++ b/llvm/include/llvm/ADT/SparseBitVector.h
@@ -799,7 +799,7 @@ class SparseBitVector {
 
   unsigned count() const {
     unsigned BitCount = 0;
-    for (const auto &Elem : Elements)
+    for (const SparseBitVectorElement<ElementSize> &Elem : Elements)
       BitCount += Elem.count();
     return BitCount;
   }



More information about the llvm-commits mailing list