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"
24 void SmallPtrSetImplBase::shrink_and_clear() {
25 assert(!isSmall() && "Can't shrink a small set!");
28 // Reduce the number of buckets.
29 unsigned Size = size();
30 CurArraySize = Size > 16 ? 1 << (Log2_32_Ceil(Size) + 1) : 32;
31 NumNonEmpty = NumTombstones = 0;
33 // Install the new array. Clear all the buckets to empty.
34 CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
35 assert(CurArray && "Failed to allocate memory?");
36 memset(CurArray, -1, CurArraySize*sizeof(void*));
39 std::pair<const void *const *, bool>
40 SmallPtrSetImplBase::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);
44 } else if (LLVM_UNLIKELY(CurArraySize - NumNonEmpty < CurArraySize / 8)) {
45 // If fewer of 1/8 of the array is empty (meaning that many are filled with
46 // tombstones), rehash.
50 // Okay, we know we have space. Find a hash bucket.
51 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
53 return std::make_pair(Bucket, false); // Already inserted, good.
55 // Otherwise, insert it!
56 if (*Bucket == getTombstoneMarker())
59 ++NumNonEmpty; // Track density.
61 return std::make_pair(Bucket, true);
64 const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
65 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
66 unsigned ArraySize = CurArraySize;
67 unsigned ProbeAmt = 1;
68 const void *const *Array = CurArray;
69 const void *const *Tombstone = nullptr;
71 // If we found an empty bucket, the pointer doesn't exist in the set.
72 // Return a tombstone if we've seen one so far, or the empty bucket if
74 if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker()))
75 return Tombstone ? Tombstone : Array+Bucket;
77 // Found Ptr's bucket?
78 if (LLVM_LIKELY(Array[Bucket] == Ptr))
81 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
82 // prefer to return it than something that would require more probing.
83 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
84 Tombstone = Array+Bucket; // Remember the first tombstone found.
86 // It's a hash collision or a tombstone. Reprobe.
87 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
91 /// Grow - Allocate a larger backing store for the buckets and move it over.
93 void SmallPtrSetImplBase::Grow(unsigned NewSize) {
94 const void **OldBuckets = CurArray;
95 const void **OldEnd = EndPointer();
96 bool WasSmall = isSmall();
98 // Install the new array. Clear all the buckets to empty.
99 CurArray = (const void**)malloc(sizeof(void*) * NewSize);
100 assert(CurArray && "Failed to allocate memory?");
101 CurArraySize = NewSize;
102 memset(CurArray, -1, NewSize*sizeof(void*));
104 // Copy over all valid entries.
105 for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
106 // Copy over the element if it is valid.
107 const void *Elt = *BucketPtr;
108 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
109 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
114 NumNonEmpty -= NumTombstones;
118 SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
119 const SmallPtrSetImplBase &that) {
120 SmallArray = SmallStorage;
122 // If we're becoming small, prepare to insert into our stack space
123 if (that.isSmall()) {
124 CurArray = SmallArray;
125 // Otherwise, allocate new heap space (unless we were the same size)
127 CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
128 assert(CurArray && "Failed to allocate memory?");
131 // Copy over the that array.
135 SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
137 SmallPtrSetImplBase &&that) {
138 SmallArray = SmallStorage;
139 MoveHelper(SmallSize, std::move(that));
142 void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
143 assert(&RHS != this && "Self-copy should be handled by the caller.");
145 if (isSmall() && RHS.isSmall())
146 assert(CurArraySize == RHS.CurArraySize &&
147 "Cannot assign sets with different small sizes");
149 // If we're becoming small, prepare to insert into our stack space
153 CurArray = SmallArray;
154 // Otherwise, allocate new heap space (unless we were the same size)
155 } else if (CurArraySize != RHS.CurArraySize) {
157 CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
159 const void **T = (const void**)realloc(CurArray,
160 sizeof(void*) * RHS.CurArraySize);
165 assert(CurArray && "Failed to allocate memory?");
171 void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) {
172 // Copy over the new array size
173 CurArraySize = RHS.CurArraySize;
175 // Copy over the contents from the other set
176 std::copy(RHS.CurArray, RHS.EndPointer(), CurArray);
178 NumNonEmpty = RHS.NumNonEmpty;
179 NumTombstones = RHS.NumTombstones;
182 void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
183 SmallPtrSetImplBase &&RHS) {
186 MoveHelper(SmallSize, std::move(RHS));
189 void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize,
190 SmallPtrSetImplBase &&RHS) {
191 assert(&RHS != this && "Self-move should be handled by the caller.");
194 // Copy a small RHS rather than moving.
195 CurArray = SmallArray;
196 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, CurArray);
198 CurArray = RHS.CurArray;
199 RHS.CurArray = RHS.SmallArray;
202 // Copy the rest of the trivial members.
203 CurArraySize = RHS.CurArraySize;
204 NumNonEmpty = RHS.NumNonEmpty;
205 NumTombstones = RHS.NumTombstones;
207 // Make the RHS small and empty.
208 RHS.CurArraySize = SmallSize;
209 assert(RHS.CurArray == RHS.SmallArray);
211 RHS.NumTombstones = 0;
214 void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
215 if (this == &RHS) return;
217 // We can only avoid copying elements if neither set is small.
218 if (!this->isSmall() && !RHS.isSmall()) {
219 std::swap(this->CurArray, RHS.CurArray);
220 std::swap(this->CurArraySize, RHS.CurArraySize);
221 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
222 std::swap(this->NumTombstones, RHS.NumTombstones);
226 // FIXME: From here on we assume that both sets have the same small size.
228 // If only RHS is small, copy the small elements into LHS and move the pointer
230 if (!this->isSmall() && RHS.isSmall()) {
231 assert(RHS.CurArray == RHS.SmallArray);
232 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, this->SmallArray);
233 std::swap(RHS.CurArraySize, this->CurArraySize);
234 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
235 std::swap(this->NumTombstones, RHS.NumTombstones);
236 RHS.CurArray = this->CurArray;
237 this->CurArray = this->SmallArray;
241 // If only LHS is small, copy the small elements into RHS and move the pointer
243 if (this->isSmall() && !RHS.isSmall()) {
244 assert(this->CurArray == this->SmallArray);
245 std::copy(this->CurArray, this->CurArray + this->NumNonEmpty,
247 std::swap(RHS.CurArraySize, this->CurArraySize);
248 std::swap(RHS.NumNonEmpty, this->NumNonEmpty);
249 std::swap(RHS.NumTombstones, this->NumTombstones);
250 this->CurArray = RHS.CurArray;
251 RHS.CurArray = RHS.SmallArray;
255 // Both a small, just swap the small elements.
256 assert(this->isSmall() && RHS.isSmall());
257 unsigned MinNonEmpty = std::min(this->NumNonEmpty, RHS.NumNonEmpty);
258 std::swap_ranges(this->SmallArray, this->SmallArray + MinNonEmpty,
260 if (this->NumNonEmpty > MinNonEmpty) {
261 std::copy(this->SmallArray + MinNonEmpty,
262 this->SmallArray + this->NumNonEmpty,
263 RHS.SmallArray + MinNonEmpty);
265 std::copy(RHS.SmallArray + MinNonEmpty, RHS.SmallArray + RHS.NumNonEmpty,
266 this->SmallArray + MinNonEmpty);
268 assert(this->CurArraySize == RHS.CurArraySize);
269 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
270 std::swap(this->NumTombstones, RHS.NumTombstones);