blob: 92ffa49833dc91cf6a2f9eab219af50c199d5751 [file] [log] [blame]
Chris Lattnerc95dc982007-01-27 07:10:46 +00001//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc95dc982007-01-27 07:10:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner24757de2007-01-27 07:18:32 +000010// This file implements the SmallPtrSet class. See SmallPtrSet.h for an
11// overview of the algorithm.
Chris Lattnerc95dc982007-01-27 07:10:46 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner91f01582007-07-09 16:54:03 +000016#include "llvm/Support/MathExtras.h"
Benjamin Kramer2945a322012-03-06 20:40:02 +000017#include <algorithm>
Reid Spencer845b31d2007-07-17 02:16:12 +000018#include <cstdlib>
19
Chris Lattnerc95dc982007-01-27 07:10:46 +000020using namespace llvm;
21
Chris Lattner42e4bdf2007-08-05 07:32:14 +000022void SmallPtrSetImpl::shrink_and_clear() {
23 assert(!isSmall() && "Can't shrink a small set!");
24 free(CurArray);
25
26 // Reduce the number of buckets.
27 CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32;
28 NumElements = NumTombstones = 0;
29
30 // Install the new array. Clear all the buckets to empty.
31 CurArray = (const void**)malloc(sizeof(void*) * (CurArraySize+1));
32 assert(CurArray && "Failed to allocate memory?");
33 memset(CurArray, -1, CurArraySize*sizeof(void*));
34
35 // The end pointer, always valid, is set to a valid element to help the
36 // iterator.
37 CurArray[CurArraySize] = 0;
38}
39
Chris Lattner373a7332007-11-06 22:12:43 +000040bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
Chris Lattnerc95dc982007-01-27 07:10:46 +000041 if (isSmall()) {
42 // Check to see if it is already in the set.
Owen Andersone992a562007-07-27 18:07:02 +000043 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattnerc95dc982007-01-27 07:10:46 +000044 APtr != E; ++APtr)
45 if (*APtr == Ptr)
46 return false;
47
48 // Nope, there isn't. If we stay small, just 'pushback' now.
49 if (NumElements < CurArraySize-1) {
50 SmallArray[NumElements++] = Ptr;
51 return true;
52 }
53 // Otherwise, hit the big set case, which will call grow.
54 }
55
Jakob Stoklund Olesene10fff62011-03-30 18:32:48 +000056 if (NumElements*4 >= CurArraySize*3) {
57 // If more than 3/4 of the array is full, grow.
58 Grow(CurArraySize < 64 ? 128 : CurArraySize*2);
59 } else if (CurArraySize-(NumElements+NumTombstones) < CurArraySize/8) {
60 // If fewer of 1/8 of the array is empty (meaning that many are filled with
61 // tombstones), rehash.
62 Grow(CurArraySize);
63 }
Chris Lattnerc95dc982007-01-27 07:10:46 +000064
65 // Okay, we know we have space. Find a hash bucket.
Dan Gohman430b8a22008-08-05 14:45:15 +000066 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
Chris Lattnerc95dc982007-01-27 07:10:46 +000067 if (*Bucket == Ptr) return false; // Already inserted, good.
68
69 // Otherwise, insert it!
Chris Lattnere237cf92007-02-07 01:11:25 +000070 if (*Bucket == getTombstoneMarker())
71 --NumTombstones;
Dan Gohman430b8a22008-08-05 14:45:15 +000072 *Bucket = Ptr;
Chris Lattnerc95dc982007-01-27 07:10:46 +000073 ++NumElements; // Track density.
74 return true;
75}
76
Chris Lattner373a7332007-11-06 22:12:43 +000077bool SmallPtrSetImpl::erase_imp(const void * Ptr) {
Chris Lattner0b930852007-01-27 07:59:10 +000078 if (isSmall()) {
79 // Check to see if it is in the set.
Owen Andersone992a562007-07-27 18:07:02 +000080 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattner0b930852007-01-27 07:59:10 +000081 APtr != E; ++APtr)
82 if (*APtr == Ptr) {
Chris Lattner61766ca2007-06-21 23:23:32 +000083 // If it is in the set, replace this element.
84 *APtr = E[-1];
Chris Lattner0b930852007-01-27 07:59:10 +000085 E[-1] = getEmptyMarker();
86 --NumElements;
Chris Lattner7ef856d2007-02-05 23:10:31 +000087 return true;
Chris Lattner0b930852007-01-27 07:59:10 +000088 }
89
90 return false;
91 }
92
93 // Okay, we know we have space. Find a hash bucket.
94 void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
95 if (*Bucket != Ptr) return false; // Not in the set?
96
97 // Set this as a tombstone.
98 *Bucket = getTombstoneMarker();
99 --NumElements;
Chris Lattnere237cf92007-02-07 01:11:25 +0000100 ++NumTombstones;
Chris Lattner0b930852007-01-27 07:59:10 +0000101 return true;
102}
103
Owen Andersone992a562007-07-27 18:07:02 +0000104const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
Chris Lattnerc95dc982007-01-27 07:10:46 +0000105 unsigned Bucket = Hash(Ptr);
106 unsigned ArraySize = CurArraySize;
107 unsigned ProbeAmt = 1;
Owen Andersone992a562007-07-27 18:07:02 +0000108 const void *const *Array = CurArray;
109 const void *const *Tombstone = 0;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000110 while (1) {
111 // Found Ptr's bucket?
112 if (Array[Bucket] == Ptr)
113 return Array+Bucket;
114
115 // If we found an empty bucket, the pointer doesn't exist in the set.
116 // Return a tombstone if we've seen one so far, or the empty bucket if
117 // not.
118 if (Array[Bucket] == getEmptyMarker())
119 return Tombstone ? Tombstone : Array+Bucket;
120
121 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
122 // prefer to return it than something that would require more probing.
123 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
124 Tombstone = Array+Bucket; // Remember the first tombstone found.
125
126 // It's a hash collision or a tombstone. Reprobe.
127 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
128 }
129}
130
131/// Grow - Allocate a larger backing store for the buckets and move it over.
132///
Jakob Stoklund Olesene10fff62011-03-30 18:32:48 +0000133void SmallPtrSetImpl::Grow(unsigned NewSize) {
Chris Lattnerc95dc982007-01-27 07:10:46 +0000134 // Allocate at twice as many buckets, but at least 128.
135 unsigned OldSize = CurArraySize;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000136
Owen Andersone992a562007-07-27 18:07:02 +0000137 const void **OldBuckets = CurArray;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000138 bool WasSmall = isSmall();
139
140 // Install the new array. Clear all the buckets to empty.
Owen Andersone992a562007-07-27 18:07:02 +0000141 CurArray = (const void**)malloc(sizeof(void*) * (NewSize+1));
Owen Anderson1629a1f2007-07-16 21:27:44 +0000142 assert(CurArray && "Failed to allocate memory?");
Chris Lattnerc95dc982007-01-27 07:10:46 +0000143 CurArraySize = NewSize;
144 memset(CurArray, -1, NewSize*sizeof(void*));
145
146 // The end pointer, always valid, is set to a valid element to help the
147 // iterator.
148 CurArray[NewSize] = 0;
149
150 // Copy over all the elements.
151 if (WasSmall) {
152 // Small sets store their elements in order.
Owen Andersone992a562007-07-27 18:07:02 +0000153 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000154 BucketPtr != E; ++BucketPtr) {
Owen Andersone992a562007-07-27 18:07:02 +0000155 const void *Elt = *BucketPtr;
156 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattnerc95dc982007-01-27 07:10:46 +0000157 }
158 } else {
159 // Copy over all valid entries.
Owen Andersone992a562007-07-27 18:07:02 +0000160 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000161 BucketPtr != E; ++BucketPtr) {
162 // Copy over the element if it is valid.
Owen Andersone992a562007-07-27 18:07:02 +0000163 const void *Elt = *BucketPtr;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000164 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
Owen Andersone992a562007-07-27 18:07:02 +0000165 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattnerc95dc982007-01-27 07:10:46 +0000166 }
167
Owen Anderson1629a1f2007-07-16 21:27:44 +0000168 free(OldBuckets);
Jeff Cohenac58a162007-04-14 21:50:21 +0000169 NumTombstones = 0;
170 }
171}
172
Duncan Sands2a8bf422010-06-30 15:02:37 +0000173SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage,
174 const SmallPtrSetImpl& that) {
175 SmallArray = SmallStorage;
176
Owen Andersonbf31b852007-07-24 21:31:23 +0000177 // If we're becoming small, prepare to insert into our stack space
Jeff Cohenac58a162007-04-14 21:50:21 +0000178 if (that.isSmall()) {
Duncan Sands2a8bf422010-06-30 15:02:37 +0000179 CurArray = SmallArray;
Owen Andersonbf31b852007-07-24 21:31:23 +0000180 // Otherwise, allocate new heap space (unless we were the same size)
Jeff Cohenac58a162007-04-14 21:50:21 +0000181 } else {
Owen Andersone992a562007-07-27 18:07:02 +0000182 CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1));
Owen Anderson1629a1f2007-07-16 21:27:44 +0000183 assert(CurArray && "Failed to allocate memory?");
Chris Lattnerc95dc982007-01-27 07:10:46 +0000184 }
Owen Andersonbf31b852007-07-24 21:31:23 +0000185
186 // Copy over the new array size
187 CurArraySize = that.CurArraySize;
188
189 // Copy over the contents from the other set
190 memcpy(CurArray, that.CurArray, sizeof(void*)*(CurArraySize+1));
191
192 NumElements = that.NumElements;
193 NumTombstones = that.NumTombstones;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000194}
Chris Lattner91f01582007-07-09 16:54:03 +0000195
196/// CopyFrom - implement operator= from a smallptrset that has the same pointer
197/// type, but may have a different small size.
198void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
Owen Anderson69b5d122007-07-09 22:27:20 +0000199 if (isSmall() && RHS.isSmall())
200 assert(CurArraySize == RHS.CurArraySize &&
201 "Cannot assign sets with different small sizes");
Owen Andersonb54b3152007-07-18 19:54:15 +0000202
Owen Anderson69b5d122007-07-09 22:27:20 +0000203 // If we're becoming small, prepare to insert into our stack space
Owen Anderson71a1e572007-07-19 06:45:33 +0000204 if (RHS.isSmall()) {
205 if (!isSmall())
206 free(CurArray);
Duncan Sands2a8bf422010-06-30 15:02:37 +0000207 CurArray = SmallArray;
Owen Anderson69b5d122007-07-09 22:27:20 +0000208 // Otherwise, allocate new heap space (unless we were the same size)
Owen Anderson71a1e572007-07-19 06:45:33 +0000209 } else if (CurArraySize != RHS.CurArraySize) {
Owen Andersonb54b3152007-07-18 19:54:15 +0000210 if (isSmall())
Owen Andersone992a562007-07-27 18:07:02 +0000211 CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
Owen Andersonb54b3152007-07-18 19:54:15 +0000212 else
Owen Andersone992a562007-07-27 18:07:02 +0000213 CurArray = (const void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1));
Owen Anderson1629a1f2007-07-16 21:27:44 +0000214 assert(CurArray && "Failed to allocate memory?");
215 }
Owen Anderson69b5d122007-07-09 22:27:20 +0000216
217 // Copy over the new array size
218 CurArraySize = RHS.CurArraySize;
219
220 // Copy over the contents from the other set
221 memcpy(CurArray, RHS.CurArray, sizeof(void*)*(CurArraySize+1));
Owen Andersonb54b3152007-07-18 19:54:15 +0000222
223 NumElements = RHS.NumElements;
224 NumTombstones = RHS.NumTombstones;
Chris Lattner91f01582007-07-09 16:54:03 +0000225}
Reid Spencer845b31d2007-07-17 02:16:12 +0000226
Benjamin Kramer2945a322012-03-06 20:40:02 +0000227void SmallPtrSetImpl::swap(SmallPtrSetImpl &RHS) {
228 if (this == &RHS) return;
229
230 // We can only avoid copying elements if neither set is small.
231 if (!this->isSmall() && !RHS.isSmall()) {
232 std::swap(this->CurArray, RHS.CurArray);
233 std::swap(this->CurArraySize, RHS.CurArraySize);
234 std::swap(this->NumElements, RHS.NumElements);
235 std::swap(this->NumTombstones, RHS.NumTombstones);
236 return;
237 }
238
239 // FIXME: From here on we assume that both sets have the same small size.
240
241 // If only RHS is small, copy the small elements into LHS and move the pointer
242 // from LHS to RHS.
243 if (!this->isSmall() && RHS.isSmall()) {
Benjamin Kramer24e0e7c2012-03-07 22:33:21 +0000244 std::copy(RHS.SmallArray, RHS.SmallArray+CurArraySize, this->SmallArray);
Benjamin Kramer2945a322012-03-06 20:40:02 +0000245 std::swap(this->NumElements, RHS.NumElements);
246 std::swap(this->CurArraySize, RHS.CurArraySize);
247 RHS.CurArray = this->CurArray;
248 RHS.NumTombstones = this->NumTombstones;
249 this->CurArray = this->SmallArray;
250 this->NumTombstones = 0;
251 return;
252 }
253
254 // If only LHS is small, copy the small elements into RHS and move the pointer
255 // from RHS to LHS.
256 if (this->isSmall() && !RHS.isSmall()) {
Benjamin Kramer24e0e7c2012-03-07 22:33:21 +0000257 std::copy(this->SmallArray, this->SmallArray+CurArraySize, RHS.SmallArray);
Benjamin Kramer2945a322012-03-06 20:40:02 +0000258 std::swap(RHS.NumElements, this->NumElements);
259 std::swap(RHS.CurArraySize, this->CurArraySize);
260 this->CurArray = RHS.CurArray;
261 this->NumTombstones = RHS.NumTombstones;
262 RHS.CurArray = RHS.SmallArray;
263 RHS.NumTombstones = 0;
264 return;
265 }
266
267 // Both a small, just swap the small elements.
268 assert(this->isSmall() && RHS.isSmall());
269 assert(this->CurArraySize == RHS.CurArraySize);
Benjamin Kramer24e0e7c2012-03-07 22:33:21 +0000270 std::swap_ranges(this->SmallArray, this->SmallArray+CurArraySize,
271 RHS.SmallArray);
Benjamin Kramer2945a322012-03-06 20:40:02 +0000272 std::swap(this->NumElements, RHS.NumElements);
273}
274
Reid Spencer845b31d2007-07-17 02:16:12 +0000275SmallPtrSetImpl::~SmallPtrSetImpl() {
276 if (!isSmall())
277 free(CurArray);
278}