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 SmallPtrSetImpl::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 bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
39 // Check to see if it is already in the set.
40 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
45 // Nope, there isn't. If we stay small, just 'pushback' now.
46 if (NumElements < CurArraySize-1) {
47 SmallArray[NumElements++] = Ptr;
50 // Otherwise, hit the big set case, which will call grow.
53 if (NumElements*4 >= CurArraySize*3) {
54 // If more than 3/4 of the array is full, grow.
55 Grow(CurArraySize < 64 ? 128 : CurArraySize*2);
56 } else if (CurArraySize-(NumElements+NumTombstones) < CurArraySize/8) {
57 // If fewer of 1/8 of the array is empty (meaning that many are filled with
58 // tombstones), rehash.
62 // Okay, we know we have space. Find a hash bucket.
63 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
64 if (*Bucket == Ptr) return false; // Already inserted, good.
66 // Otherwise, insert it!
67 if (*Bucket == getTombstoneMarker())
70 ++NumElements; // Track density.
74 bool SmallPtrSetImpl::erase_imp(const void * Ptr) {
76 // Check to see if it is in the set.
77 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
80 // If it is in the set, replace this element.
82 E[-1] = getEmptyMarker();
90 // Okay, we know we have space. Find a hash bucket.
91 void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
92 if (*Bucket != Ptr) return false; // Not in the set?
94 // Set this as a tombstone.
95 *Bucket = getTombstoneMarker();
101 const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
102 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
103 unsigned ArraySize = CurArraySize;
104 unsigned ProbeAmt = 1;
105 const void *const *Array = CurArray;
106 const void *const *Tombstone = 0;
108 // Found Ptr's bucket?
109 if (Array[Bucket] == Ptr)
112 // If we found an empty bucket, the pointer doesn't exist in the set.
113 // Return a tombstone if we've seen one so far, or the empty bucket if
115 if (Array[Bucket] == getEmptyMarker())
116 return Tombstone ? Tombstone : Array+Bucket;
118 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
119 // prefer to return it than something that would require more probing.
120 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
121 Tombstone = Array+Bucket; // Remember the first tombstone found.
123 // It's a hash collision or a tombstone. Reprobe.
124 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
128 /// Grow - Allocate a larger backing store for the buckets and move it over.
130 void SmallPtrSetImpl::Grow(unsigned NewSize) {
131 // Allocate at twice as many buckets, but at least 128.
132 unsigned OldSize = CurArraySize;
134 const void **OldBuckets = CurArray;
135 bool WasSmall = isSmall();
137 // Install the new array. Clear all the buckets to empty.
138 CurArray = (const void**)malloc(sizeof(void*) * NewSize);
139 assert(CurArray && "Failed to allocate memory?");
140 CurArraySize = NewSize;
141 memset(CurArray, -1, NewSize*sizeof(void*));
143 // Copy over all the elements.
145 // Small sets store their elements in order.
146 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
147 BucketPtr != E; ++BucketPtr) {
148 const void *Elt = *BucketPtr;
149 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
152 // Copy over all valid entries.
153 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
154 BucketPtr != E; ++BucketPtr) {
155 // Copy over the element if it is valid.
156 const void *Elt = *BucketPtr;
157 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
158 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
166 SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage,
167 const SmallPtrSetImpl& that) {
168 SmallArray = SmallStorage;
170 // If we're becoming small, prepare to insert into our stack space
171 if (that.isSmall()) {
172 CurArray = SmallArray;
173 // Otherwise, allocate new heap space (unless we were the same size)
175 CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
176 assert(CurArray && "Failed to allocate memory?");
179 // Copy over the new array size
180 CurArraySize = that.CurArraySize;
182 // Copy over the contents from the other set
183 memcpy(CurArray, that.CurArray, sizeof(void*)*CurArraySize);
185 NumElements = that.NumElements;
186 NumTombstones = that.NumTombstones;
189 #if LLVM_HAS_RVALUE_REFERENCES
190 SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize,
191 SmallPtrSetImpl &&that) {
192 SmallArray = SmallStorage;
194 // Copy over the basic members.
195 CurArraySize = that.CurArraySize;
196 NumElements = that.NumElements;
197 NumTombstones = that.NumTombstones;
199 // When small, just copy into our small buffer.
200 if (that.isSmall()) {
201 CurArray = SmallArray;
202 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;
210 // Make the "that" object small and empty.
211 that.CurArraySize = SmallSize;
212 assert(that.CurArray == that.SmallArray);
213 that.NumElements = 0;
214 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 SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
221 if (isSmall() && RHS.isSmall())
222 assert(CurArraySize == RHS.CurArraySize &&
223 "Cannot assign sets with different small sizes");
225 // If we're becoming small, prepare to insert into our stack space
229 CurArray = SmallArray;
230 // Otherwise, allocate new heap space (unless we were the same size)
231 } else if (CurArraySize != RHS.CurArraySize) {
233 CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
235 const void **T = (const void**)realloc(CurArray,
236 sizeof(void*) * RHS.CurArraySize);
241 assert(CurArray && "Failed to allocate memory?");
244 // Copy over the new array size
245 CurArraySize = RHS.CurArraySize;
247 // Copy over the contents from the other set
248 memcpy(CurArray, RHS.CurArray, sizeof(void*)*CurArraySize);
250 NumElements = RHS.NumElements;
251 NumTombstones = RHS.NumTombstones;
254 #if LLVM_HAS_RVALUE_REFERENCES
255 void SmallPtrSetImpl::MoveFrom(unsigned SmallSize, SmallPtrSetImpl &&RHS) {
260 // Copy a small RHS rather than moving.
261 CurArray = SmallArray;
262 memcpy(CurArray, RHS.CurArray, sizeof(void*)*RHS.CurArraySize);
264 CurArray = RHS.CurArray;
265 RHS.CurArray = RHS.SmallArray;
268 // Copy the rest of the trivial members.
269 CurArraySize = RHS.CurArraySize;
270 NumElements = RHS.NumElements;
271 NumTombstones = RHS.NumTombstones;
273 // Make the RHS small and empty.
274 RHS.CurArraySize = SmallSize;
275 assert(RHS.CurArray == RHS.SmallArray);
277 RHS.NumTombstones = 0;
281 void SmallPtrSetImpl::swap(SmallPtrSetImpl &RHS) {
282 if (this == &RHS) return;
284 // We can only avoid copying elements if neither set is small.
285 if (!this->isSmall() && !RHS.isSmall()) {
286 std::swap(this->CurArray, RHS.CurArray);
287 std::swap(this->CurArraySize, RHS.CurArraySize);
288 std::swap(this->NumElements, RHS.NumElements);
289 std::swap(this->NumTombstones, RHS.NumTombstones);
293 // FIXME: From here on we assume that both sets have the same small size.
295 // If only RHS is small, copy the small elements into LHS and move the pointer
297 if (!this->isSmall() && RHS.isSmall()) {
298 std::copy(RHS.SmallArray, RHS.SmallArray+RHS.CurArraySize,
300 std::swap(this->NumElements, RHS.NumElements);
301 std::swap(this->CurArraySize, RHS.CurArraySize);
302 RHS.CurArray = this->CurArray;
303 RHS.NumTombstones = this->NumTombstones;
304 this->CurArray = this->SmallArray;
305 this->NumTombstones = 0;
309 // If only LHS is small, copy the small elements into RHS and move the pointer
311 if (this->isSmall() && !RHS.isSmall()) {
312 std::copy(this->SmallArray, this->SmallArray+this->CurArraySize,
314 std::swap(RHS.NumElements, this->NumElements);
315 std::swap(RHS.CurArraySize, this->CurArraySize);
316 this->CurArray = RHS.CurArray;
317 this->NumTombstones = RHS.NumTombstones;
318 RHS.CurArray = RHS.SmallArray;
319 RHS.NumTombstones = 0;
323 // Both a small, just swap the small elements.
324 assert(this->isSmall() && RHS.isSmall());
325 assert(this->CurArraySize == RHS.CurArraySize);
326 std::swap_ranges(this->SmallArray, this->SmallArray+this->CurArraySize,
328 std::swap(this->NumElements, RHS.NumElements);
331 SmallPtrSetImpl::~SmallPtrSetImpl() {