[llvm] [ADT] Use range-based for loops in SmallPtrSet.h (NFC) (PR #152821)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 8 21:14:28 PDT 2025
https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/152821
>From d3d7fdef9b8c056ed7c534c986c98241fe2b927c Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 8 Aug 2025 16:42:24 -0700
Subject: [PATCH 1/2] [ADT] Use range-based for loops in SmallPtrSet.h (NFC)
This patch defines a couple of helper functions so that we can convert
four loops to range-based for loops.
---
llvm/include/llvm/ADT/SmallPtrSet.h | 38 +++++++++++++++--------------
1 file changed, 20 insertions(+), 18 deletions(-)
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index d49ef1d4fbc57..7f9b781513420 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -18,6 +18,7 @@
#include "llvm/ADT/ADL.h"
#include "llvm/ADT/EpochTracker.h"
#include "llvm/ADT/STLForwardCompat.h"
+#include "llvm/ADT/iterator_range.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/ReverseIteration.h"
@@ -147,17 +148,23 @@ class SmallPtrSetImplBase : public DebugEpochBase {
return isSmall() ? CurArray + NumNonEmpty : CurArray + CurArraySize;
}
+ iterator_range<const void **> smallBuckets() {
+ return make_range(CurArray, CurArray + NumNonEmpty);
+ }
+
+ iterator_range<const void *const *> smallBuckets() const {
+ return {CurArray, CurArray + NumNonEmpty};
+ }
+
/// insert_imp - This returns true if the pointer was new to the set, false if
/// it was already in the set. This is hidden from the client so that the
/// derived class can check that the right type of pointer is passed in.
std::pair<const void *const *, bool> insert_imp(const void *Ptr) {
if (isSmall()) {
// Check to see if it is already in the set.
- for (const void **APtr = CurArray, **E = CurArray + NumNonEmpty;
- APtr != E; ++APtr) {
- const void *Value = *APtr;
- if (Value == Ptr)
- return std::make_pair(APtr, false);
+ for (const void *&Bucket : smallBuckets()) {
+ if (Bucket == Ptr)
+ return std::make_pair(&Bucket, false);
}
// Nope, there isn't. If we stay small, just 'pushback' now.
@@ -177,10 +184,9 @@ class SmallPtrSetImplBase : public DebugEpochBase {
/// in.
bool erase_imp(const void * Ptr) {
if (isSmall()) {
- for (const void **APtr = CurArray, **E = CurArray + NumNonEmpty;
- APtr != E; ++APtr) {
- if (*APtr == Ptr) {
- *APtr = CurArray[--NumNonEmpty];
+ for (const void *&Bucket : smallBuckets()) {
+ if (Bucket == Ptr) {
+ Bucket = CurArray[--NumNonEmpty];
incrementEpoch();
return true;
}
@@ -206,11 +212,9 @@ class SmallPtrSetImplBase : public DebugEpochBase {
const void *const * find_imp(const void * Ptr) const {
if (isSmall()) {
// Linear search for the item.
- for (const void *const *APtr = CurArray, *const *E =
- CurArray + NumNonEmpty;
- APtr != E; ++APtr)
- if (*APtr == Ptr)
- return APtr;
+ for (const void *const &Bucket : smallBuckets())
+ if (Bucket == Ptr)
+ return &Bucket;
return EndPointer();
}
@@ -223,10 +227,8 @@ class SmallPtrSetImplBase : public DebugEpochBase {
bool contains_imp(const void *Ptr) const {
if (isSmall()) {
// Linear search for the item.
- const void *const *APtr = CurArray;
- const void *const *E = CurArray + NumNonEmpty;
- for (; APtr != E; ++APtr)
- if (*APtr == Ptr)
+ for (const void *const &Bucket : smallBuckets())
+ if (Bucket == Ptr)
return true;
return false;
}
>From 649afb67b8e2f0dde680f11671a44a06b58c956c Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 8 Aug 2025 20:58:51 -0700
Subject: [PATCH 2/2] Address a comment.
---
llvm/include/llvm/ADT/SmallPtrSet.h | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 7f9b781513420..88aedf26dcaac 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -148,11 +148,11 @@ class SmallPtrSetImplBase : public DebugEpochBase {
return isSmall() ? CurArray + NumNonEmpty : CurArray + CurArraySize;
}
- iterator_range<const void **> smallBuckets() {
+ iterator_range<const void **> small_buckets() {
return make_range(CurArray, CurArray + NumNonEmpty);
}
- iterator_range<const void *const *> smallBuckets() const {
+ iterator_range<const void *const *> small_buckets() const {
return {CurArray, CurArray + NumNonEmpty};
}
@@ -162,7 +162,7 @@ class SmallPtrSetImplBase : public DebugEpochBase {
std::pair<const void *const *, bool> insert_imp(const void *Ptr) {
if (isSmall()) {
// Check to see if it is already in the set.
- for (const void *&Bucket : smallBuckets()) {
+ for (const void *&Bucket : small_buckets()) {
if (Bucket == Ptr)
return std::make_pair(&Bucket, false);
}
@@ -184,7 +184,7 @@ class SmallPtrSetImplBase : public DebugEpochBase {
/// in.
bool erase_imp(const void * Ptr) {
if (isSmall()) {
- for (const void *&Bucket : smallBuckets()) {
+ for (const void *&Bucket : small_buckets()) {
if (Bucket == Ptr) {
Bucket = CurArray[--NumNonEmpty];
incrementEpoch();
@@ -212,7 +212,7 @@ class SmallPtrSetImplBase : public DebugEpochBase {
const void *const * find_imp(const void * Ptr) const {
if (isSmall()) {
// Linear search for the item.
- for (const void *const &Bucket : smallBuckets())
+ for (const void *const &Bucket : small_buckets())
if (Bucket == Ptr)
return &Bucket;
return EndPointer();
@@ -227,7 +227,7 @@ class SmallPtrSetImplBase : public DebugEpochBase {
bool contains_imp(const void *Ptr) const {
if (isSmall()) {
// Linear search for the item.
- for (const void *const &Bucket : smallBuckets())
+ for (const void *const &Bucket : small_buckets())
if (Bucket == Ptr)
return true;
return false;
More information about the llvm-commits
mailing list