LLVM 20.0.0git
SmallPtrSet.cpp
Go to the documentation of this file.
1//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the SmallPtrSet class. See SmallPtrSet.h for an
10// overview of the algorithm.
11//
12//===----------------------------------------------------------------------===//
13
18#include <algorithm>
19#include <cassert>
20#include <cstdlib>
21
22using namespace llvm;
23
24void SmallPtrSetImplBase::shrink_and_clear() {
25 assert(!isSmall() && "Can't shrink a small set!");
26 free(CurArray);
27
28 // Reduce the number of buckets.
29 unsigned Size = size();
30 CurArraySize = Size > 16 ? 1 << (Log2_32_Ceil(Size) + 1) : 32;
32
33 // Install the new array. Clear all the buckets to empty.
34 CurArray = (const void**)safe_malloc(sizeof(void*) * CurArraySize);
35
36 memset(CurArray, -1, CurArraySize*sizeof(void*));
37}
38
39std::pair<const void *const *, bool>
40SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
41 if (LLVM_UNLIKELY(size() * 4 >= CurArraySize * 3)) {
42 // If more than 3/4 of the array is full, grow.
43 Grow(CurArraySize < 64 ? 128 : CurArraySize * 2);
45 // If fewer of 1/8 of the array is empty (meaning that many are filled with
46 // tombstones), rehash.
47 Grow(CurArraySize);
48 }
49
50 // Okay, we know we have space. Find a hash bucket.
51 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
52 if (*Bucket == Ptr)
53 return std::make_pair(Bucket, false); // Already inserted, good.
54
55 // Otherwise, insert it!
56 if (*Bucket == getTombstoneMarker())
58 else
59 ++NumNonEmpty; // Track density.
60 *Bucket = Ptr;
62 return std::make_pair(Bucket, true);
63}
64
65const void *const *SmallPtrSetImplBase::doFind(const void *Ptr) const {
66 unsigned BucketNo =
68 unsigned ProbeAmt = 1;
69 while (true) {
70 const void *const *Bucket = CurArray + BucketNo;
71 if (LLVM_LIKELY(*Bucket == Ptr))
72 return Bucket;
73 if (LLVM_LIKELY(*Bucket == getEmptyMarker()))
74 return nullptr;
75
76 // Otherwise, it's a hash collision or a tombstone, continue quadratic
77 // probing.
78 BucketNo += ProbeAmt++;
79 BucketNo &= CurArraySize - 1;
80 }
81}
82
83const void *const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
85 unsigned ArraySize = CurArraySize;
86 unsigned ProbeAmt = 1;
87 const void *const *Array = CurArray;
88 const void *const *Tombstone = nullptr;
89 while (true) {
90 // If we found an empty bucket, the pointer doesn't exist in the set.
91 // Return a tombstone if we've seen one so far, or the empty bucket if
92 // not.
93 if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker()))
94 return Tombstone ? Tombstone : Array+Bucket;
95
96 // Found Ptr's bucket?
97 if (LLVM_LIKELY(Array[Bucket] == Ptr))
98 return Array+Bucket;
99
100 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
101 // prefer to return it than something that would require more probing.
102 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
103 Tombstone = Array+Bucket; // Remember the first tombstone found.
104
105 // It's a hash collision or a tombstone. Reprobe.
106 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
107 }
108}
109
110/// Grow - Allocate a larger backing store for the buckets and move it over.
111///
112void SmallPtrSetImplBase::Grow(unsigned NewSize) {
113 const void **OldBuckets = CurArray;
114 const void **OldEnd = EndPointer();
115 bool WasSmall = isSmall();
116
117 // Install the new array. Clear all the buckets to empty.
118 const void **NewBuckets = (const void**) safe_malloc(sizeof(void*) * NewSize);
119
120 // Reset member only if memory was allocated successfully
121 CurArray = NewBuckets;
122 CurArraySize = NewSize;
123 memset(CurArray, -1, NewSize*sizeof(void*));
124
125 // Copy over all valid entries.
126 for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
127 // Copy over the element if it is valid.
128 const void *Elt = *BucketPtr;
129 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
130 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
131 }
132
133 if (!WasSmall)
134 free(OldBuckets);
136 NumTombstones = 0;
137}
138
140 const SmallPtrSetImplBase &that) {
141 SmallArray = SmallStorage;
142
143 // If we're becoming small, prepare to insert into our stack space
144 if (that.isSmall()) {
146 // Otherwise, allocate new heap space (unless we were the same size)
147 } else {
148 CurArray = (const void**)safe_malloc(sizeof(void*) * that.CurArraySize);
149 }
150
151 // Copy over the that array.
152 CopyHelper(that);
153}
154
156 unsigned SmallSize,
157 SmallPtrSetImplBase &&that) {
158 SmallArray = SmallStorage;
159 MoveHelper(SmallSize, std::move(that));
160}
161
163 assert(&RHS != this && "Self-copy should be handled by the caller.");
164
165 if (isSmall() && RHS.isSmall())
166 assert(CurArraySize == RHS.CurArraySize &&
167 "Cannot assign sets with different small sizes");
168
169 // If we're becoming small, prepare to insert into our stack space
170 if (RHS.isSmall()) {
171 if (!isSmall())
172 free(CurArray);
174 // Otherwise, allocate new heap space (unless we were the same size)
175 } else if (CurArraySize != RHS.CurArraySize) {
176 if (isSmall())
177 CurArray = (const void**)safe_malloc(sizeof(void*) * RHS.CurArraySize);
178 else {
179 const void **T = (const void**)safe_realloc(CurArray,
180 sizeof(void*) * RHS.CurArraySize);
181 CurArray = T;
182 }
183 }
184
185 CopyHelper(RHS);
186}
187
188void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) {
189 // Copy over the new array size
190 CurArraySize = RHS.CurArraySize;
191
192 // Copy over the contents from the other set
193 std::copy(RHS.CurArray, RHS.EndPointer(), CurArray);
194
195 NumNonEmpty = RHS.NumNonEmpty;
196 NumTombstones = RHS.NumTombstones;
197}
198
199void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
200 SmallPtrSetImplBase &&RHS) {
201 if (!isSmall())
202 free(CurArray);
203 MoveHelper(SmallSize, std::move(RHS));
204}
205
206void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize,
207 SmallPtrSetImplBase &&RHS) {
208 assert(&RHS != this && "Self-move should be handled by the caller.");
209
210 if (RHS.isSmall()) {
211 // Copy a small RHS rather than moving.
213 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, CurArray);
214 } else {
215 CurArray = RHS.CurArray;
216 RHS.CurArray = RHS.SmallArray;
217 }
218
219 // Copy the rest of the trivial members.
220 CurArraySize = RHS.CurArraySize;
221 NumNonEmpty = RHS.NumNonEmpty;
222 NumTombstones = RHS.NumTombstones;
223
224 // Make the RHS small and empty.
225 RHS.CurArraySize = SmallSize;
226 assert(RHS.CurArray == RHS.SmallArray);
227 RHS.NumNonEmpty = 0;
228 RHS.NumTombstones = 0;
229}
230
232 if (this == &RHS) return;
233
234 // We can only avoid copying elements if neither set is small.
235 if (!this->isSmall() && !RHS.isSmall()) {
236 std::swap(this->CurArray, RHS.CurArray);
238 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
240 return;
241 }
242
243 // FIXME: From here on we assume that both sets have the same small size.
244
245 // If only RHS is small, copy the small elements into LHS and move the pointer
246 // from LHS to RHS.
247 if (!this->isSmall() && RHS.isSmall()) {
248 assert(RHS.CurArray == RHS.SmallArray);
249 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, this->SmallArray);
250 std::swap(RHS.CurArraySize, this->CurArraySize);
251 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
253 RHS.CurArray = this->CurArray;
254 this->CurArray = this->SmallArray;
255 return;
256 }
257
258 // If only LHS is small, copy the small elements into RHS and move the pointer
259 // from RHS to LHS.
260 if (this->isSmall() && !RHS.isSmall()) {
261 assert(this->CurArray == this->SmallArray);
262 std::copy(this->CurArray, this->CurArray + this->NumNonEmpty,
263 RHS.SmallArray);
264 std::swap(RHS.CurArraySize, this->CurArraySize);
265 std::swap(RHS.NumNonEmpty, this->NumNonEmpty);
266 std::swap(RHS.NumTombstones, this->NumTombstones);
267 this->CurArray = RHS.CurArray;
268 RHS.CurArray = RHS.SmallArray;
269 return;
270 }
271
272 // Both a small, just swap the small elements.
273 assert(this->isSmall() && RHS.isSmall());
274 unsigned MinNonEmpty = std::min(this->NumNonEmpty, RHS.NumNonEmpty);
275 std::swap_ranges(this->SmallArray, this->SmallArray + MinNonEmpty,
276 RHS.SmallArray);
277 if (this->NumNonEmpty > MinNonEmpty) {
278 std::copy(this->SmallArray + MinNonEmpty,
279 this->SmallArray + this->NumNonEmpty,
280 RHS.SmallArray + MinNonEmpty);
281 } else {
282 std::copy(RHS.SmallArray + MinNonEmpty, RHS.SmallArray + RHS.NumNonEmpty,
283 this->SmallArray + MinNonEmpty);
284 }
285 assert(this->CurArraySize == RHS.CurArraySize);
286 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
288}
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:237
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:236
This file defines DenseMapInfo traits for DenseMap.
uint64_t Size
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
const MachineOperand & RHS
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet class.
SmallPtrSetImplBase - This is the common code shared among all the SmallPtrSet<>'s,...
Definition: SmallPtrSet.h:52
size_type size() const
Definition: SmallPtrSet.h:95
unsigned NumTombstones
Number of tombstones in CurArray.
Definition: SmallPtrSet.h:69
void MoveFrom(unsigned SmallSize, SmallPtrSetImplBase &&RHS)
SmallPtrSetImplBase(const void **SmallStorage, const SmallPtrSetImplBase &that)
const void ** CurArray
CurArray - This is the current set of buckets.
Definition: SmallPtrSet.h:60
unsigned NumNonEmpty
Number of elements in CurArray that contain a value or are a tombstone.
Definition: SmallPtrSet.h:67
const void ** SmallArray
SmallArray - Points to a fixed size set of buckets, used in 'small mode'.
Definition: SmallPtrSet.h:57
void CopyFrom(const SmallPtrSetImplBase &RHS)
unsigned CurArraySize
CurArraySize - The allocated size of CurArray, always a power of two.
Definition: SmallPtrSet.h:62
const void ** EndPointer() const
Definition: SmallPtrSet.h:143
static void * getEmptyMarker()
Definition: SmallPtrSet.h:137
static void * getTombstoneMarker()
Definition: SmallPtrSet.h:135
void swap(SmallPtrSetImplBase &RHS)
swap - Swaps the elements of two sets.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition: MathExtras.h:353
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition: MemAlloc.h:25
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_realloc(void *Ptr, size_t Sz)
Definition: MemAlloc.h:52
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:52