blob: fa8d91545e71064bde1cf0b7b7d22f27182d2b39 [file] [log] [blame]
Chris Lattner74102df2007-01-27 07:10:46 +00001//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattner74102df2007-01-27 07:10:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerab5d0ba2007-01-27 07:18:32 +000010// This file implements the SmallPtrSet class. See SmallPtrSet.h for an
11// overview of the algorithm.
Chris Lattner74102df2007-01-27 07:10:46 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer94988cb2012-04-18 10:37:32 +000016#include "llvm/ADT/DenseMapInfo.h"
Chris Lattner85049a42007-07-09 16:54:03 +000017#include "llvm/Support/MathExtras.h"
Benjamin Kramere1c34e92012-03-06 20:40:02 +000018#include <algorithm>
Reid Spencer5060fd02007-07-17 02:16:12 +000019#include <cstdlib>
20
Chris Lattner74102df2007-01-27 07:10:46 +000021using namespace llvm;
22
Chris Lattner44f7d3a2007-08-05 07:32:14 +000023void SmallPtrSetImpl::shrink_and_clear() {
24 assert(!isSmall() && "Can't shrink a small set!");
25 free(CurArray);
26
27 // Reduce the number of buckets.
28 CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32;
29 NumElements = NumTombstones = 0;
30
31 // Install the new array. Clear all the buckets to empty.
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +000032 CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
Chris Lattner44f7d3a2007-08-05 07:32:14 +000033 assert(CurArray && "Failed to allocate memory?");
34 memset(CurArray, -1, CurArraySize*sizeof(void*));
Chris Lattner44f7d3a2007-08-05 07:32:14 +000035}
36
Chris Lattner581f0062007-11-06 22:12:43 +000037bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
Chris Lattner74102df2007-01-27 07:10:46 +000038 if (isSmall()) {
39 // Check to see if it is already in the set.
Owen Anderson49f037a2007-07-27 18:07:02 +000040 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattner74102df2007-01-27 07:10:46 +000041 APtr != E; ++APtr)
42 if (*APtr == Ptr)
43 return false;
44
45 // Nope, there isn't. If we stay small, just 'pushback' now.
46 if (NumElements < CurArraySize-1) {
47 SmallArray[NumElements++] = Ptr;
48 return true;
49 }
50 // Otherwise, hit the big set case, which will call grow.
51 }
52
Jakob Stoklund Olesenbdc1b012011-03-30 18:32:48 +000053 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.
59 Grow(CurArraySize);
60 }
Chris Lattner74102df2007-01-27 07:10:46 +000061
62 // Okay, we know we have space. Find a hash bucket.
Dan Gohmane955c482008-08-05 14:45:15 +000063 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
Chris Lattner74102df2007-01-27 07:10:46 +000064 if (*Bucket == Ptr) return false; // Already inserted, good.
65
66 // Otherwise, insert it!
Chris Lattnere3467672007-02-07 01:11:25 +000067 if (*Bucket == getTombstoneMarker())
68 --NumTombstones;
Dan Gohmane955c482008-08-05 14:45:15 +000069 *Bucket = Ptr;
Chris Lattner74102df2007-01-27 07:10:46 +000070 ++NumElements; // Track density.
71 return true;
72}
73
Chris Lattner581f0062007-11-06 22:12:43 +000074bool SmallPtrSetImpl::erase_imp(const void * Ptr) {
Chris Lattner39ab70c2007-01-27 07:59:10 +000075 if (isSmall()) {
76 // Check to see if it is in the set.
Owen Anderson49f037a2007-07-27 18:07:02 +000077 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattner39ab70c2007-01-27 07:59:10 +000078 APtr != E; ++APtr)
79 if (*APtr == Ptr) {
Chris Lattner836e8f32007-06-21 23:23:32 +000080 // If it is in the set, replace this element.
81 *APtr = E[-1];
Chris Lattner39ab70c2007-01-27 07:59:10 +000082 E[-1] = getEmptyMarker();
83 --NumElements;
Chris Lattner92c5d112007-02-05 23:10:31 +000084 return true;
Chris Lattner39ab70c2007-01-27 07:59:10 +000085 }
86
87 return false;
88 }
89
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?
93
94 // Set this as a tombstone.
95 *Bucket = getTombstoneMarker();
96 --NumElements;
Chris Lattnere3467672007-02-07 01:11:25 +000097 ++NumTombstones;
Chris Lattner39ab70c2007-01-27 07:59:10 +000098 return true;
99}
100
Owen Anderson49f037a2007-07-27 18:07:02 +0000101const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
Benjamin Kramer94988cb2012-04-18 10:37:32 +0000102 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
Chris Lattner74102df2007-01-27 07:10:46 +0000103 unsigned ArraySize = CurArraySize;
104 unsigned ProbeAmt = 1;
Owen Anderson49f037a2007-07-27 18:07:02 +0000105 const void *const *Array = CurArray;
106 const void *const *Tombstone = 0;
Chris Lattner74102df2007-01-27 07:10:46 +0000107 while (1) {
108 // Found Ptr's bucket?
109 if (Array[Bucket] == Ptr)
110 return Array+Bucket;
111
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
114 // not.
115 if (Array[Bucket] == getEmptyMarker())
116 return Tombstone ? Tombstone : Array+Bucket;
117
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.
122
123 // It's a hash collision or a tombstone. Reprobe.
124 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
125 }
126}
127
128/// Grow - Allocate a larger backing store for the buckets and move it over.
129///
Jakob Stoklund Olesenbdc1b012011-03-30 18:32:48 +0000130void SmallPtrSetImpl::Grow(unsigned NewSize) {
Chris Lattner74102df2007-01-27 07:10:46 +0000131 // Allocate at twice as many buckets, but at least 128.
132 unsigned OldSize = CurArraySize;
Chris Lattner74102df2007-01-27 07:10:46 +0000133
Owen Anderson49f037a2007-07-27 18:07:02 +0000134 const void **OldBuckets = CurArray;
Chris Lattner74102df2007-01-27 07:10:46 +0000135 bool WasSmall = isSmall();
136
137 // Install the new array. Clear all the buckets to empty.
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000138 CurArray = (const void**)malloc(sizeof(void*) * NewSize);
Owen Anderson81990a32007-07-16 21:27:44 +0000139 assert(CurArray && "Failed to allocate memory?");
Chris Lattner74102df2007-01-27 07:10:46 +0000140 CurArraySize = NewSize;
141 memset(CurArray, -1, NewSize*sizeof(void*));
142
Chris Lattner74102df2007-01-27 07:10:46 +0000143 // Copy over all the elements.
144 if (WasSmall) {
145 // Small sets store their elements in order.
Owen Anderson49f037a2007-07-27 18:07:02 +0000146 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
Chris Lattner74102df2007-01-27 07:10:46 +0000147 BucketPtr != E; ++BucketPtr) {
Owen Anderson49f037a2007-07-27 18:07:02 +0000148 const void *Elt = *BucketPtr;
149 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattner74102df2007-01-27 07:10:46 +0000150 }
151 } else {
152 // Copy over all valid entries.
Owen Anderson49f037a2007-07-27 18:07:02 +0000153 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
Chris Lattner74102df2007-01-27 07:10:46 +0000154 BucketPtr != E; ++BucketPtr) {
155 // Copy over the element if it is valid.
Owen Anderson49f037a2007-07-27 18:07:02 +0000156 const void *Elt = *BucketPtr;
Chris Lattner74102df2007-01-27 07:10:46 +0000157 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
Owen Anderson49f037a2007-07-27 18:07:02 +0000158 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattner74102df2007-01-27 07:10:46 +0000159 }
160
Owen Anderson81990a32007-07-16 21:27:44 +0000161 free(OldBuckets);
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000162 NumTombstones = 0;
163 }
164}
165
Duncan Sands7b909662010-06-30 15:02:37 +0000166SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage,
167 const SmallPtrSetImpl& that) {
168 SmallArray = SmallStorage;
169
Owen Anderson4c540242007-07-24 21:31:23 +0000170 // If we're becoming small, prepare to insert into our stack space
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000171 if (that.isSmall()) {
Duncan Sands7b909662010-06-30 15:02:37 +0000172 CurArray = SmallArray;
Owen Anderson4c540242007-07-24 21:31:23 +0000173 // Otherwise, allocate new heap space (unless we were the same size)
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000174 } else {
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000175 CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
Owen Anderson81990a32007-07-16 21:27:44 +0000176 assert(CurArray && "Failed to allocate memory?");
Chris Lattner74102df2007-01-27 07:10:46 +0000177 }
Owen Anderson4c540242007-07-24 21:31:23 +0000178
179 // Copy over the new array size
180 CurArraySize = that.CurArraySize;
181
182 // Copy over the contents from the other set
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000183 memcpy(CurArray, that.CurArray, sizeof(void*)*CurArraySize);
Owen Anderson4c540242007-07-24 21:31:23 +0000184
185 NumElements = that.NumElements;
186 NumTombstones = that.NumTombstones;
Chris Lattner74102df2007-01-27 07:10:46 +0000187}
Chris Lattner85049a42007-07-09 16:54:03 +0000188
Chandler Carruth55758e92013-11-20 11:14:33 +0000189#if LLVM_HAS_RVALUE_REFERENCES
190SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize,
191 SmallPtrSetImpl &&that) {
192 SmallArray = SmallStorage;
193
194 // Copy over the basic members.
195 CurArraySize = that.CurArraySize;
196 NumElements = that.NumElements;
197 NumTombstones = that.NumTombstones;
198
199 // When small, just copy into our small buffer.
200 if (that.isSmall()) {
201 CurArray = SmallArray;
202 memcpy(CurArray, that.CurArray, sizeof(void *) * CurArraySize);
203 return;
204 }
205
206 // Otherwise, we steal the large memory allocation and no copy is needed.
207 CurArray = that.CurArray;
208 that.CurArray = that.SmallArray;
Chandler Carruthc74010d2013-11-20 18:29:56 +0000209
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;
Chandler Carruth55758e92013-11-20 11:14:33 +0000215}
216#endif
217
Chris Lattner85049a42007-07-09 16:54:03 +0000218/// CopyFrom - implement operator= from a smallptrset that has the same pointer
219/// type, but may have a different small size.
220void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
Chandler Carruth26643172013-11-26 00:44:36 +0000221 if (&RHS == this)
222 return;
223
Owen Andersone33356b2007-07-09 22:27:20 +0000224 if (isSmall() && RHS.isSmall())
225 assert(CurArraySize == RHS.CurArraySize &&
226 "Cannot assign sets with different small sizes");
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000227
Owen Andersone33356b2007-07-09 22:27:20 +0000228 // If we're becoming small, prepare to insert into our stack space
Owen Andersonee990102007-07-19 06:45:33 +0000229 if (RHS.isSmall()) {
230 if (!isSmall())
231 free(CurArray);
Duncan Sands7b909662010-06-30 15:02:37 +0000232 CurArray = SmallArray;
Owen Andersone33356b2007-07-09 22:27:20 +0000233 // Otherwise, allocate new heap space (unless we were the same size)
Owen Andersonee990102007-07-19 06:45:33 +0000234 } else if (CurArraySize != RHS.CurArraySize) {
Owen Andersone21f2702007-07-18 19:54:15 +0000235 if (isSmall())
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000236 CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
Aaron Ballman5de23782013-11-18 17:33:32 +0000237 else {
238 const void **T = (const void**)realloc(CurArray,
239 sizeof(void*) * RHS.CurArraySize);
240 if (!T)
241 free(CurArray);
242 CurArray = T;
243 }
Owen Anderson81990a32007-07-16 21:27:44 +0000244 assert(CurArray && "Failed to allocate memory?");
245 }
Owen Andersone33356b2007-07-09 22:27:20 +0000246
247 // Copy over the new array size
248 CurArraySize = RHS.CurArraySize;
249
250 // Copy over the contents from the other set
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000251 memcpy(CurArray, RHS.CurArray, sizeof(void*)*CurArraySize);
Owen Andersone21f2702007-07-18 19:54:15 +0000252
253 NumElements = RHS.NumElements;
254 NumTombstones = RHS.NumTombstones;
Chris Lattner85049a42007-07-09 16:54:03 +0000255}
Reid Spencer5060fd02007-07-17 02:16:12 +0000256
Chandler Carruth55758e92013-11-20 11:14:33 +0000257#if LLVM_HAS_RVALUE_REFERENCES
Chandler Carruthc74010d2013-11-20 18:29:56 +0000258void SmallPtrSetImpl::MoveFrom(unsigned SmallSize, SmallPtrSetImpl &&RHS) {
Chandler Carruth55758e92013-11-20 11:14:33 +0000259 if (!isSmall())
260 free(CurArray);
261
262 if (RHS.isSmall()) {
263 // Copy a small RHS rather than moving.
264 CurArray = SmallArray;
265 memcpy(CurArray, RHS.CurArray, sizeof(void*)*RHS.CurArraySize);
266 } else {
267 CurArray = RHS.CurArray;
268 RHS.CurArray = RHS.SmallArray;
269 }
270
271 // Copy the rest of the trivial members.
272 CurArraySize = RHS.CurArraySize;
273 NumElements = RHS.NumElements;
274 NumTombstones = RHS.NumTombstones;
Chandler Carruthc74010d2013-11-20 18:29:56 +0000275
276 // Make the RHS small and empty.
277 RHS.CurArraySize = SmallSize;
278 assert(RHS.CurArray == RHS.SmallArray);
279 RHS.NumElements = 0;
280 RHS.NumTombstones = 0;
Chandler Carruth55758e92013-11-20 11:14:33 +0000281}
282#endif
283
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000284void SmallPtrSetImpl::swap(SmallPtrSetImpl &RHS) {
285 if (this == &RHS) return;
286
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);
293 return;
294 }
295
296 // FIXME: From here on we assume that both sets have the same small size.
297
298 // If only RHS is small, copy the small elements into LHS and move the pointer
299 // from LHS to RHS.
300 if (!this->isSmall() && RHS.isSmall()) {
Benjamin Kramer22842f82012-03-07 22:48:42 +0000301 std::copy(RHS.SmallArray, RHS.SmallArray+RHS.CurArraySize,
302 this->SmallArray);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000303 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;
309 return;
310 }
311
312 // If only LHS is small, copy the small elements into RHS and move the pointer
313 // from RHS to LHS.
314 if (this->isSmall() && !RHS.isSmall()) {
Benjamin Kramer22842f82012-03-07 22:48:42 +0000315 std::copy(this->SmallArray, this->SmallArray+this->CurArraySize,
316 RHS.SmallArray);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000317 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;
323 return;
324 }
325
326 // Both a small, just swap the small elements.
327 assert(this->isSmall() && RHS.isSmall());
328 assert(this->CurArraySize == RHS.CurArraySize);
Benjamin Kramer22842f82012-03-07 22:48:42 +0000329 std::swap_ranges(this->SmallArray, this->SmallArray+this->CurArraySize,
Benjamin Kramer6e8d4b82012-03-07 22:33:21 +0000330 RHS.SmallArray);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000331 std::swap(this->NumElements, RHS.NumElements);
332}
333
Reid Spencer5060fd02007-07-17 02:16:12 +0000334SmallPtrSetImpl::~SmallPtrSetImpl() {
335 if (!isSmall())
336 free(CurArray);
337}