1 //===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SmallPtrSet class. See SmallPtrSet.h for an
11 // overview of the algorithm.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/DenseMapInfo.h"
17 #include "llvm/Support/MathExtras.h"
23 void SmallPtrSetImplBase::shrink_and_clear() {
24 assert(!isSmall() && "Can't shrink a small set!");
27 // Reduce the number of buckets.
28 CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32;
29 NumElements = NumTombstones = 0;
31 // Install the new array. Clear all the buckets to empty.
32 CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
33 assert(CurArray && "Failed to allocate memory?");
34 memset(CurArray, -1, CurArraySize*sizeof(void*));
37 std::pair<const void *const *, bool>
38 SmallPtrSetImplBase::insert_imp(const void *Ptr) {
40 // Check to see if it is already in the set.
41 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
44 return std::make_pair(APtr, false);
46 // Nope, there isn't. If we stay small, just 'pushback' now.
47 if (NumElements < CurArraySize) {
48 SmallArray[NumElements++] = Ptr;
49 return std::make_pair(SmallArray + (NumElements - 1), true);
51 // Otherwise, hit the big set case, which will call grow.
54 if (NumElements*4 >= CurArraySize*3) {
55 // If more than 3/4 of the array is full, grow.
56 Grow(CurArraySize < 64 ? 128 : CurArraySize*2);
57 } else if (CurArraySize-(NumElements+NumTombstones) < CurArraySize/8) {
58 // If fewer of 1/8 of the array is empty (meaning that many are filled with
59 // tombstones), rehash.
63 // Okay, we know we have space. Find a hash bucket.
64 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
66 return std::make_pair(Bucket, false); // Already inserted, good.
68 // Otherwise, insert it!
69 if (*Bucket == getTombstoneMarker())
72 ++NumElements; // Track density.
73 return std::make_pair(Bucket, true);
76 bool SmallPtrSetImplBase::erase_imp(const void * Ptr) {
78 // Check to see if it is in the set.
79 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
82 // If it is in the set, replace this element.
84 E[-1] = getEmptyMarker();
92 // Okay, we know we have space. Find a hash bucket.
93 void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
94 if (*Bucket != Ptr) return false; // Not in the set?
96 // Set this as a tombstone.
97 *Bucket = getTombstoneMarker();
103 const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
104 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
105 unsigned ArraySize = CurArraySize;
106 unsigned ProbeAmt = 1;
107 const void *const *Array = CurArray;
108 const void *const *Tombstone = nullptr;
110 // Found Ptr's bucket?
111 if (Array[Bucket] == Ptr)
114 // If we found an empty bucket, the pointer doesn't exist in the set.
115 // Return a tombstone if we've seen one so far, or the empty bucket if
117 if (Array[Bucket] == getEmptyMarker())
118 return Tombstone ? Tombstone : Array+Bucket;
120 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
121 // prefer to return it than something that would require more probing.
122 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
123 Tombstone = Array+Bucket; // Remember the first tombstone found.
125 // It's a hash collision or a tombstone. Reprobe.
126 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
130 /// Grow - Allocate a larger backing store for the buckets and move it over.
132 void SmallPtrSetImplBase::Grow(unsigned NewSize) {
133 // Allocate at twice as many buckets, but at least 128.
134 unsigned OldSize = CurArraySize;
136 const void **OldBuckets = CurArray;
137 bool WasSmall = isSmall();
139 // Install the new array. Clear all the buckets to empty.
140 CurArray = (const void**)malloc(sizeof(void*) * NewSize);
141 assert(CurArray && "Failed to allocate memory?");
142 CurArraySize = NewSize;
143 memset(CurArray, -1, NewSize*sizeof(void*));
145 // Copy over all the elements.
147 // Small sets store their elements in order.
148 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
149 BucketPtr != E; ++BucketPtr) {
150 const void *Elt = *BucketPtr;
151 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
154 // Copy over all valid entries.
155 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
156 BucketPtr != E; ++BucketPtr) {
157 // Copy over the element if it is valid.
158 const void *Elt = *BucketPtr;
159 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
160 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
168 SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
169 const SmallPtrSetImplBase& that) {
170 SmallArray = SmallStorage;
172 // If we're becoming small, prepare to insert into our stack space
173 if (that.isSmall()) {
174 CurArray = SmallArray;
175 // Otherwise, allocate new heap space (unless we were the same size)
177 CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
178 assert(CurArray && "Failed to allocate memory?");
181 // Copy over the new array size
182 CurArraySize = that.CurArraySize;
184 // Copy over the contents from the other set
185 memcpy(CurArray, that.CurArray, sizeof(void*)*CurArraySize);
187 NumElements = that.NumElements;
188 NumTombstones = that.NumTombstones;
191 SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
193 SmallPtrSetImplBase &&that) {
194 SmallArray = SmallStorage;
196 // Copy over the basic members.
197 CurArraySize = that.CurArraySize;
198 NumElements = that.NumElements;
199 NumTombstones = that.NumTombstones;
201 // When small, just copy into our small buffer.
202 if (that.isSmall()) {
203 CurArray = SmallArray;
204 memcpy(CurArray, that.CurArray, sizeof(void *) * CurArraySize);
206 // Otherwise, we steal the large memory allocation and no copy is needed.
207 CurArray = that.CurArray;
208 that.CurArray = that.SmallArray;
211 // Make the "that" object small and empty.
212 that.CurArraySize = SmallSize;
213 assert(that.CurArray == that.SmallArray);
214 that.NumElements = 0;
215 that.NumTombstones = 0;
218 /// CopyFrom - implement operator= from a smallptrset that has the same pointer
219 /// type, but may have a different small size.
220 void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
221 assert(&RHS != this && "Self-copy should be handled by the caller.");
223 if (isSmall() && RHS.isSmall())
224 assert(CurArraySize == RHS.CurArraySize &&
225 "Cannot assign sets with different small sizes");
227 // If we're becoming small, prepare to insert into our stack space
231 CurArray = SmallArray;
232 // Otherwise, allocate new heap space (unless we were the same size)
233 } else if (CurArraySize != RHS.CurArraySize) {
235 CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
237 const void **T = (const void**)realloc(CurArray,
238 sizeof(void*) * RHS.CurArraySize);
243 assert(CurArray && "Failed to allocate memory?");
246 // Copy over the new array size
247 CurArraySize = RHS.CurArraySize;
249 // Copy over the contents from the other set
250 memcpy(CurArray, RHS.CurArray, sizeof(void*)*CurArraySize);
252 NumElements = RHS.NumElements;
253 NumTombstones = RHS.NumTombstones;
256 void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
257 SmallPtrSetImplBase &&RHS) {
258 assert(&RHS != this && "Self-move should be handled by the caller.");
264 // Copy a small RHS rather than moving.
265 CurArray = SmallArray;
266 memcpy(CurArray, RHS.CurArray, sizeof(void*)*RHS.CurArraySize);
268 CurArray = RHS.CurArray;
269 RHS.CurArray = RHS.SmallArray;
272 // Copy the rest of the trivial members.
273 CurArraySize = RHS.CurArraySize;
274 NumElements = RHS.NumElements;
275 NumTombstones = RHS.NumTombstones;
277 // Make the RHS small and empty.
278 RHS.CurArraySize = SmallSize;
279 assert(RHS.CurArray == RHS.SmallArray);
281 RHS.NumTombstones = 0;
284 void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
285 if (this == &RHS) return;
287 // We can only avoid copying elements if neither set is small.
288 if (!this->isSmall() && !RHS.isSmall()) {
289 std::swap(this->CurArray, RHS.CurArray);
290 std::swap(this->CurArraySize, RHS.CurArraySize);
291 std::swap(this->NumElements, RHS.NumElements);
292 std::swap(this->NumTombstones, RHS.NumTombstones);
296 // FIXME: From here on we assume that both sets have the same small size.
298 // If only RHS is small, copy the small elements into LHS and move the pointer
300 if (!this->isSmall() && RHS.isSmall()) {
301 std::copy(RHS.SmallArray, RHS.SmallArray+RHS.CurArraySize,
303 std::swap(this->NumElements, RHS.NumElements);
304 std::swap(this->CurArraySize, RHS.CurArraySize);
305 RHS.CurArray = this->CurArray;
306 RHS.NumTombstones = this->NumTombstones;
307 this->CurArray = this->SmallArray;
308 this->NumTombstones = 0;
312 // If only LHS is small, copy the small elements into RHS and move the pointer
314 if (this->isSmall() && !RHS.isSmall()) {
315 std::copy(this->SmallArray, this->SmallArray+this->CurArraySize,
317 std::swap(RHS.NumElements, this->NumElements);
318 std::swap(RHS.CurArraySize, this->CurArraySize);
319 this->CurArray = RHS.CurArray;
320 this->NumTombstones = RHS.NumTombstones;
321 RHS.CurArray = RHS.SmallArray;
322 RHS.NumTombstones = 0;
326 // Both a small, just swap the small elements.
327 assert(this->isSmall() && RHS.isSmall());
328 assert(this->CurArraySize == RHS.CurArraySize);
329 std::swap_ranges(this->SmallArray, this->SmallArray+this->CurArraySize,
331 std::swap(this->NumElements, RHS.NumElements);
334 SmallPtrSetImplBase::~SmallPtrSetImplBase() {