[llvm] fddb8fe - [ADT] Simplify SmallPtrSetIterator with std::reverse_iterator (NFC) (#160643)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 25 08:51:43 PDT 2025
Author: Kazu Hirata
Date: 2025-09-25T08:51:39-07:00
New Revision: fddb8fe5f2be4f31c643ee6ff6f017b01465b21d
URL: https://github.com/llvm/llvm-project/commit/fddb8fe5f2be4f31c643ee6ff6f017b01465b21d
DIFF: https://github.com/llvm/llvm-project/commit/fddb8fe5f2be4f31c643ee6ff6f017b01465b21d.diff
LOG: [ADT] Simplify SmallPtrSetIterator with std::reverse_iterator (NFC) (#160643)
SmallPtrSetIterator has two tasks:
- iterate the buckets in the requested direction
- skip the empty and tombstone buckets
These tasks are intertwined in the current implementation. This patch
separates them.
A new private iterator type, BucketItTy, now handles the iteration
direction. This is an alias for a raw pointer for forward iteration
and std::reverse_iterator<pointer> for reverse iteration.
The user-facing iterator now focuses solely on advancing BucketItTy
while skipping invalid (empty or tombstone) buckets. AdvanceIfNotValid
now works transparently for both directions. operator++ on BucketItTy
does the right thing whether it's a raw pointer or a
std::reverse_iterator.
This simplification removes RetreatIfNotValid and the
reverse-iteration logic in operator*() and operator++().
Added:
Modified:
llvm/include/llvm/ADT/SmallPtrSet.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index dca9f7995926d..d5332379fc542 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -281,16 +281,17 @@ class SmallPtrSetImplBase : public DebugEpochBase {
/// instances of SmallPtrSetIterator.
class SmallPtrSetIteratorImpl {
protected:
- const void *const *Bucket;
- const void *const *End;
+ using BucketItTy =
+ std::conditional_t<shouldReverseIterate(),
+ std::reverse_iterator<const void *const *>,
+ const void *const *>;
+
+ BucketItTy Bucket;
+ BucketItTy End;
public:
explicit SmallPtrSetIteratorImpl(const void *const *BP, const void *const *E)
: Bucket(BP), End(E) {
- if (shouldReverseIterate()) {
- RetreatIfNotValid();
- return;
- }
AdvanceIfNotValid();
}
@@ -312,14 +313,6 @@ class SmallPtrSetIteratorImpl {
*Bucket == SmallPtrSetImplBase::getTombstoneMarker()))
++Bucket;
}
- void RetreatIfNotValid() {
- assert(Bucket >= End);
- while (Bucket != End &&
- (Bucket[-1] == SmallPtrSetImplBase::getEmptyMarker() ||
- Bucket[-1] == SmallPtrSetImplBase::getTombstoneMarker())) {
- --Bucket;
- }
- }
};
/// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
@@ -344,21 +337,12 @@ class LLVM_DEBUGEPOCHBASE_HANDLEBASE_EMPTYBASE SmallPtrSetIterator
const PtrTy operator*() const {
assert(isHandleInSync() && "invalid iterator access!");
- if (shouldReverseIterate()) {
- assert(Bucket > End);
- return PtrTraits::getFromVoidPointer(const_cast<void *>(Bucket[-1]));
- }
assert(Bucket < End);
return PtrTraits::getFromVoidPointer(const_cast<void *>(*Bucket));
}
inline SmallPtrSetIterator &operator++() { // Preincrement
assert(isHandleInSync() && "invalid iterator access!");
- if (shouldReverseIterate()) {
- --Bucket;
- RetreatIfNotValid();
- return *this;
- }
++Bucket;
AdvanceIfNotValid();
return *this;
More information about the llvm-commits
mailing list