blob: 358c8e8abbe91ffbe7c64145346ee959ce9a11ee [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
Chandler Carruth173bd7e2014-02-03 11:24:18 +000023void SmallPtrSetImplBase::shrink_and_clear() {
Chris Lattner44f7d3a2007-08-05 07:32:14 +000024 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
David Blaikie70573dc2014-11-19 07:49:26 +000037std::pair<const void *const *, bool>
38SmallPtrSetImplBase::insert_imp(const void *Ptr) {
Chris Lattner74102df2007-01-27 07:10:46 +000039 if (isSmall()) {
40 // Check to see if it is already in the set.
Owen Anderson49f037a2007-07-27 18:07:02 +000041 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattner74102df2007-01-27 07:10:46 +000042 APtr != E; ++APtr)
43 if (*APtr == Ptr)
David Blaikie70573dc2014-11-19 07:49:26 +000044 return std::make_pair(APtr, false);
45
Chris Lattner74102df2007-01-27 07:10:46 +000046 // Nope, there isn't. If we stay small, just 'pushback' now.
Craig Topper298f6382014-08-20 04:41:36 +000047 if (NumElements < CurArraySize) {
Chris Lattner74102df2007-01-27 07:10:46 +000048 SmallArray[NumElements++] = Ptr;
David Blaikie70573dc2014-11-19 07:49:26 +000049 return std::make_pair(SmallArray + (NumElements - 1), true);
Chris Lattner74102df2007-01-27 07:10:46 +000050 }
51 // Otherwise, hit the big set case, which will call grow.
52 }
Benjamin Kramer654a85e2015-02-23 16:41:36 +000053
54 if (LLVM_UNLIKELY(NumElements * 4 >= CurArraySize * 3)) {
Jakob Stoklund Olesenbdc1b012011-03-30 18:32:48 +000055 // If more than 3/4 of the array is full, grow.
56 Grow(CurArraySize < 64 ? 128 : CurArraySize*2);
Benjamin Kramer654a85e2015-02-23 16:41:36 +000057 } else if (LLVM_UNLIKELY(CurArraySize - (NumElements + NumTombstones) <
58 CurArraySize / 8)) {
Jakob Stoklund Olesenbdc1b012011-03-30 18:32:48 +000059 // If fewer of 1/8 of the array is empty (meaning that many are filled with
60 // tombstones), rehash.
61 Grow(CurArraySize);
62 }
Chris Lattner74102df2007-01-27 07:10:46 +000063
64 // Okay, we know we have space. Find a hash bucket.
Dan Gohmane955c482008-08-05 14:45:15 +000065 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
David Blaikie70573dc2014-11-19 07:49:26 +000066 if (*Bucket == Ptr)
67 return std::make_pair(Bucket, false); // Already inserted, good.
68
Chris Lattner74102df2007-01-27 07:10:46 +000069 // Otherwise, insert it!
Chris Lattnere3467672007-02-07 01:11:25 +000070 if (*Bucket == getTombstoneMarker())
71 --NumTombstones;
Dan Gohmane955c482008-08-05 14:45:15 +000072 *Bucket = Ptr;
Chris Lattner74102df2007-01-27 07:10:46 +000073 ++NumElements; // Track density.
David Blaikie70573dc2014-11-19 07:49:26 +000074 return std::make_pair(Bucket, true);
Chris Lattner74102df2007-01-27 07:10:46 +000075}
76
Chandler Carruth173bd7e2014-02-03 11:24:18 +000077bool SmallPtrSetImplBase::erase_imp(const void * Ptr) {
Chris Lattner39ab70c2007-01-27 07:59:10 +000078 if (isSmall()) {
79 // Check to see if it is in the set.
Owen Anderson49f037a2007-07-27 18:07:02 +000080 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattner39ab70c2007-01-27 07:59:10 +000081 APtr != E; ++APtr)
82 if (*APtr == Ptr) {
Chris Lattner836e8f32007-06-21 23:23:32 +000083 // If it is in the set, replace this element.
84 *APtr = E[-1];
Chris Lattner39ab70c2007-01-27 07:59:10 +000085 E[-1] = getEmptyMarker();
86 --NumElements;
Chris Lattner92c5d112007-02-05 23:10:31 +000087 return true;
Chris Lattner39ab70c2007-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 Lattnere3467672007-02-07 01:11:25 +0000100 ++NumTombstones;
Chris Lattner39ab70c2007-01-27 07:59:10 +0000101 return true;
102}
103
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000104const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
Benjamin Kramer94988cb2012-04-18 10:37:32 +0000105 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
Chris Lattner74102df2007-01-27 07:10:46 +0000106 unsigned ArraySize = CurArraySize;
107 unsigned ProbeAmt = 1;
Owen Anderson49f037a2007-07-27 18:07:02 +0000108 const void *const *Array = CurArray;
Craig Topperc10719f2014-04-07 04:17:22 +0000109 const void *const *Tombstone = nullptr;
Chris Lattner74102df2007-01-27 07:10:46 +0000110 while (1) {
Chris Lattner74102df2007-01-27 07:10:46 +0000111 // If we found an empty bucket, the pointer doesn't exist in the set.
112 // Return a tombstone if we've seen one so far, or the empty bucket if
113 // not.
Benjamin Kramer654a85e2015-02-23 16:41:36 +0000114 if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker()))
Chris Lattner74102df2007-01-27 07:10:46 +0000115 return Tombstone ? Tombstone : Array+Bucket;
Benjamin Kramer654a85e2015-02-23 16:41:36 +0000116
117 // Found Ptr's bucket?
118 if (LLVM_LIKELY(Array[Bucket] == Ptr))
119 return Array+Bucket;
120
Chris Lattner74102df2007-01-27 07:10:46 +0000121 // 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///
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000133void SmallPtrSetImplBase::Grow(unsigned NewSize) {
Chris Lattner74102df2007-01-27 07:10:46 +0000134 // Allocate at twice as many buckets, but at least 128.
135 unsigned OldSize = CurArraySize;
Chris Lattner74102df2007-01-27 07:10:46 +0000136
Owen Anderson49f037a2007-07-27 18:07:02 +0000137 const void **OldBuckets = CurArray;
Chris Lattner74102df2007-01-27 07:10:46 +0000138 bool WasSmall = isSmall();
139
140 // Install the new array. Clear all the buckets to empty.
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000141 CurArray = (const void**)malloc(sizeof(void*) * NewSize);
Owen Anderson81990a32007-07-16 21:27:44 +0000142 assert(CurArray && "Failed to allocate memory?");
Chris Lattner74102df2007-01-27 07:10:46 +0000143 CurArraySize = NewSize;
144 memset(CurArray, -1, NewSize*sizeof(void*));
145
Chris Lattner74102df2007-01-27 07:10:46 +0000146 // Copy over all the elements.
147 if (WasSmall) {
148 // Small sets store their elements in order.
Owen Anderson49f037a2007-07-27 18:07:02 +0000149 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
Chris Lattner74102df2007-01-27 07:10:46 +0000150 BucketPtr != E; ++BucketPtr) {
Owen Anderson49f037a2007-07-27 18:07:02 +0000151 const void *Elt = *BucketPtr;
152 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattner74102df2007-01-27 07:10:46 +0000153 }
154 } else {
155 // Copy over all valid entries.
Owen Anderson49f037a2007-07-27 18:07:02 +0000156 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
Chris Lattner74102df2007-01-27 07:10:46 +0000157 BucketPtr != E; ++BucketPtr) {
158 // Copy over the element if it is valid.
Owen Anderson49f037a2007-07-27 18:07:02 +0000159 const void *Elt = *BucketPtr;
Chris Lattner74102df2007-01-27 07:10:46 +0000160 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
Owen Anderson49f037a2007-07-27 18:07:02 +0000161 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattner74102df2007-01-27 07:10:46 +0000162 }
163
Owen Anderson81990a32007-07-16 21:27:44 +0000164 free(OldBuckets);
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000165 NumTombstones = 0;
166 }
167}
168
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000169SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
170 const SmallPtrSetImplBase& that) {
Duncan Sands7b909662010-06-30 15:02:37 +0000171 SmallArray = SmallStorage;
172
Owen Anderson4c540242007-07-24 21:31:23 +0000173 // If we're becoming small, prepare to insert into our stack space
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000174 if (that.isSmall()) {
Duncan Sands7b909662010-06-30 15:02:37 +0000175 CurArray = SmallArray;
Owen Anderson4c540242007-07-24 21:31:23 +0000176 // Otherwise, allocate new heap space (unless we were the same size)
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000177 } else {
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000178 CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
Owen Anderson81990a32007-07-16 21:27:44 +0000179 assert(CurArray && "Failed to allocate memory?");
Chris Lattner74102df2007-01-27 07:10:46 +0000180 }
Owen Anderson4c540242007-07-24 21:31:23 +0000181
182 // Copy over the new array size
183 CurArraySize = that.CurArraySize;
184
185 // Copy over the contents from the other set
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000186 memcpy(CurArray, that.CurArray, sizeof(void*)*CurArraySize);
Owen Anderson4c540242007-07-24 21:31:23 +0000187
188 NumElements = that.NumElements;
189 NumTombstones = that.NumTombstones;
Chris Lattner74102df2007-01-27 07:10:46 +0000190}
Chris Lattner85049a42007-07-09 16:54:03 +0000191
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000192SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
193 unsigned SmallSize,
194 SmallPtrSetImplBase &&that) {
Chandler Carruth55758e92013-11-20 11:14:33 +0000195 SmallArray = SmallStorage;
196
197 // Copy over the basic members.
198 CurArraySize = that.CurArraySize;
199 NumElements = that.NumElements;
200 NumTombstones = that.NumTombstones;
201
202 // When small, just copy into our small buffer.
203 if (that.isSmall()) {
204 CurArray = SmallArray;
205 memcpy(CurArray, that.CurArray, sizeof(void *) * CurArraySize);
Craig Topper298f6382014-08-20 04:41:36 +0000206 } else {
207 // Otherwise, we steal the large memory allocation and no copy is needed.
208 CurArray = that.CurArray;
209 that.CurArray = that.SmallArray;
Chandler Carruth55758e92013-11-20 11:14:33 +0000210 }
211
Chandler Carruthc74010d2013-11-20 18:29:56 +0000212 // Make the "that" object small and empty.
213 that.CurArraySize = SmallSize;
214 assert(that.CurArray == that.SmallArray);
215 that.NumElements = 0;
216 that.NumTombstones = 0;
Chandler Carruth55758e92013-11-20 11:14:33 +0000217}
Chandler Carruth55758e92013-11-20 11:14:33 +0000218
Chris Lattner85049a42007-07-09 16:54:03 +0000219/// CopyFrom - implement operator= from a smallptrset that has the same pointer
220/// type, but may have a different small size.
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000221void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
Chandler Carruth480f5d22013-11-26 00:54:44 +0000222 assert(&RHS != this && "Self-copy should be handled by the caller.");
Chandler Carruth26643172013-11-26 00:44:36 +0000223
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 Carruth173bd7e2014-02-03 11:24:18 +0000257void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
258 SmallPtrSetImplBase &&RHS) {
Chandler Carruth480f5d22013-11-26 00:54:44 +0000259 assert(&RHS != this && "Self-move should be handled by the caller.");
260
Chandler Carruth55758e92013-11-20 11:14:33 +0000261 if (!isSmall())
262 free(CurArray);
263
264 if (RHS.isSmall()) {
265 // Copy a small RHS rather than moving.
266 CurArray = SmallArray;
267 memcpy(CurArray, RHS.CurArray, sizeof(void*)*RHS.CurArraySize);
268 } else {
269 CurArray = RHS.CurArray;
270 RHS.CurArray = RHS.SmallArray;
271 }
272
273 // Copy the rest of the trivial members.
274 CurArraySize = RHS.CurArraySize;
275 NumElements = RHS.NumElements;
276 NumTombstones = RHS.NumTombstones;
Chandler Carruthc74010d2013-11-20 18:29:56 +0000277
278 // Make the RHS small and empty.
279 RHS.CurArraySize = SmallSize;
280 assert(RHS.CurArray == RHS.SmallArray);
281 RHS.NumElements = 0;
282 RHS.NumTombstones = 0;
Chandler Carruth55758e92013-11-20 11:14:33 +0000283}
Chandler Carruth55758e92013-11-20 11:14:33 +0000284
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000285void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000286 if (this == &RHS) return;
287
288 // We can only avoid copying elements if neither set is small.
289 if (!this->isSmall() && !RHS.isSmall()) {
290 std::swap(this->CurArray, RHS.CurArray);
291 std::swap(this->CurArraySize, RHS.CurArraySize);
292 std::swap(this->NumElements, RHS.NumElements);
293 std::swap(this->NumTombstones, RHS.NumTombstones);
294 return;
295 }
296
297 // FIXME: From here on we assume that both sets have the same small size.
298
299 // If only RHS is small, copy the small elements into LHS and move the pointer
300 // from LHS to RHS.
301 if (!this->isSmall() && RHS.isSmall()) {
Benjamin Kramer22842f82012-03-07 22:48:42 +0000302 std::copy(RHS.SmallArray, RHS.SmallArray+RHS.CurArraySize,
303 this->SmallArray);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000304 std::swap(this->NumElements, RHS.NumElements);
305 std::swap(this->CurArraySize, RHS.CurArraySize);
306 RHS.CurArray = this->CurArray;
307 RHS.NumTombstones = this->NumTombstones;
308 this->CurArray = this->SmallArray;
309 this->NumTombstones = 0;
310 return;
311 }
312
313 // If only LHS is small, copy the small elements into RHS and move the pointer
314 // from RHS to LHS.
315 if (this->isSmall() && !RHS.isSmall()) {
Benjamin Kramer22842f82012-03-07 22:48:42 +0000316 std::copy(this->SmallArray, this->SmallArray+this->CurArraySize,
317 RHS.SmallArray);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000318 std::swap(RHS.NumElements, this->NumElements);
319 std::swap(RHS.CurArraySize, this->CurArraySize);
320 this->CurArray = RHS.CurArray;
321 this->NumTombstones = RHS.NumTombstones;
322 RHS.CurArray = RHS.SmallArray;
323 RHS.NumTombstones = 0;
324 return;
325 }
326
327 // Both a small, just swap the small elements.
328 assert(this->isSmall() && RHS.isSmall());
329 assert(this->CurArraySize == RHS.CurArraySize);
Benjamin Kramer22842f82012-03-07 22:48:42 +0000330 std::swap_ranges(this->SmallArray, this->SmallArray+this->CurArraySize,
Benjamin Kramer6e8d4b82012-03-07 22:33:21 +0000331 RHS.SmallArray);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000332 std::swap(this->NumElements, RHS.NumElements);
333}
334
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000335SmallPtrSetImplBase::~SmallPtrSetImplBase() {
Reid Spencer5060fd02007-07-17 02:16:12 +0000336 if (!isSmall())
337 free(CurArray);
338}