blob: 3b53e9ff49fe9891c66f8623b273438d63cbeeb0 [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"
Benjamin Kramer4bb87cb2012-04-18 10:37:32 +000016#include "llvm/ADT/DenseMapInfo.h"
Chris Lattner91f01582007-07-09 16:54:03 +000017#include "llvm/Support/MathExtras.h"
Benjamin Kramer2945a322012-03-06 20:40:02 +000018#include <algorithm>
Reid Spencer845b31d2007-07-17 02:16:12 +000019#include <cstdlib>
20
Chris Lattnerc95dc982007-01-27 07:10:46 +000021using namespace llvm;
22
Chris Lattner42e4bdf2007-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.
32 CurArray = (const void**)malloc(sizeof(void*) * (CurArraySize+1));
33 assert(CurArray && "Failed to allocate memory?");
34 memset(CurArray, -1, CurArraySize*sizeof(void*));
35
36 // The end pointer, always valid, is set to a valid element to help the
37 // iterator.
38 CurArray[CurArraySize] = 0;
39}
40
Chris Lattner373a7332007-11-06 22:12:43 +000041bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
Chris Lattnerc95dc982007-01-27 07:10:46 +000042 if (isSmall()) {
43 // Check to see if it is already in the set.
Owen Andersone992a562007-07-27 18:07:02 +000044 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattnerc95dc982007-01-27 07:10:46 +000045 APtr != E; ++APtr)
46 if (*APtr == Ptr)
47 return false;
48
49 // Nope, there isn't. If we stay small, just 'pushback' now.
50 if (NumElements < CurArraySize-1) {
51 SmallArray[NumElements++] = Ptr;
52 return true;
53 }
54 // Otherwise, hit the big set case, which will call grow.
55 }
56
Jakob Stoklund Olesene10fff62011-03-30 18:32:48 +000057 if (NumElements*4 >= CurArraySize*3) {
58 // If more than 3/4 of the array is full, grow.
59 Grow(CurArraySize < 64 ? 128 : CurArraySize*2);
60 } else if (CurArraySize-(NumElements+NumTombstones) < CurArraySize/8) {
61 // If fewer of 1/8 of the array is empty (meaning that many are filled with
62 // tombstones), rehash.
63 Grow(CurArraySize);
64 }
Chris Lattnerc95dc982007-01-27 07:10:46 +000065
66 // Okay, we know we have space. Find a hash bucket.
Dan Gohman430b8a22008-08-05 14:45:15 +000067 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
Chris Lattnerc95dc982007-01-27 07:10:46 +000068 if (*Bucket == Ptr) return false; // Already inserted, good.
69
70 // Otherwise, insert it!
Chris Lattnere237cf92007-02-07 01:11:25 +000071 if (*Bucket == getTombstoneMarker())
72 --NumTombstones;
Dan Gohman430b8a22008-08-05 14:45:15 +000073 *Bucket = Ptr;
Chris Lattnerc95dc982007-01-27 07:10:46 +000074 ++NumElements; // Track density.
75 return true;
76}
77
Chris Lattner373a7332007-11-06 22:12:43 +000078bool SmallPtrSetImpl::erase_imp(const void * Ptr) {
Chris Lattner0b930852007-01-27 07:59:10 +000079 if (isSmall()) {
80 // Check to see if it is in the set.
Owen Andersone992a562007-07-27 18:07:02 +000081 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattner0b930852007-01-27 07:59:10 +000082 APtr != E; ++APtr)
83 if (*APtr == Ptr) {
Chris Lattner61766ca2007-06-21 23:23:32 +000084 // If it is in the set, replace this element.
85 *APtr = E[-1];
Chris Lattner0b930852007-01-27 07:59:10 +000086 E[-1] = getEmptyMarker();
87 --NumElements;
Chris Lattner7ef856d2007-02-05 23:10:31 +000088 return true;
Chris Lattner0b930852007-01-27 07:59:10 +000089 }
90
91 return false;
92 }
93
94 // Okay, we know we have space. Find a hash bucket.
95 void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
96 if (*Bucket != Ptr) return false; // Not in the set?
97
98 // Set this as a tombstone.
99 *Bucket = getTombstoneMarker();
100 --NumElements;
Chris Lattnere237cf92007-02-07 01:11:25 +0000101 ++NumTombstones;
Chris Lattner0b930852007-01-27 07:59:10 +0000102 return true;
103}
104
Owen Andersone992a562007-07-27 18:07:02 +0000105const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
Benjamin Kramer4bb87cb2012-04-18 10:37:32 +0000106 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
Chris Lattnerc95dc982007-01-27 07:10:46 +0000107 unsigned ArraySize = CurArraySize;
108 unsigned ProbeAmt = 1;
Owen Andersone992a562007-07-27 18:07:02 +0000109 const void *const *Array = CurArray;
110 const void *const *Tombstone = 0;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000111 while (1) {
112 // Found Ptr's bucket?
113 if (Array[Bucket] == Ptr)
114 return Array+Bucket;
115
116 // If we found an empty bucket, the pointer doesn't exist in the set.
117 // Return a tombstone if we've seen one so far, or the empty bucket if
118 // not.
119 if (Array[Bucket] == getEmptyMarker())
120 return Tombstone ? Tombstone : Array+Bucket;
121
122 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
123 // prefer to return it than something that would require more probing.
124 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
125 Tombstone = Array+Bucket; // Remember the first tombstone found.
126
127 // It's a hash collision or a tombstone. Reprobe.
128 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
129 }
130}
131
132/// Grow - Allocate a larger backing store for the buckets and move it over.
133///
Jakob Stoklund Olesene10fff62011-03-30 18:32:48 +0000134void SmallPtrSetImpl::Grow(unsigned NewSize) {
Chris Lattnerc95dc982007-01-27 07:10:46 +0000135 // Allocate at twice as many buckets, but at least 128.
136 unsigned OldSize = CurArraySize;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000137
Owen Andersone992a562007-07-27 18:07:02 +0000138 const void **OldBuckets = CurArray;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000139 bool WasSmall = isSmall();
140
141 // Install the new array. Clear all the buckets to empty.
Owen Andersone992a562007-07-27 18:07:02 +0000142 CurArray = (const void**)malloc(sizeof(void*) * (NewSize+1));
Owen Anderson1629a1f2007-07-16 21:27:44 +0000143 assert(CurArray && "Failed to allocate memory?");
Chris Lattnerc95dc982007-01-27 07:10:46 +0000144 CurArraySize = NewSize;
145 memset(CurArray, -1, NewSize*sizeof(void*));
146
147 // The end pointer, always valid, is set to a valid element to help the
148 // iterator.
149 CurArray[NewSize] = 0;
150
151 // Copy over all the elements.
152 if (WasSmall) {
153 // Small sets store their elements in order.
Owen Andersone992a562007-07-27 18:07:02 +0000154 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000155 BucketPtr != E; ++BucketPtr) {
Owen Andersone992a562007-07-27 18:07:02 +0000156 const void *Elt = *BucketPtr;
157 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattnerc95dc982007-01-27 07:10:46 +0000158 }
159 } else {
160 // Copy over all valid entries.
Owen Andersone992a562007-07-27 18:07:02 +0000161 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000162 BucketPtr != E; ++BucketPtr) {
163 // Copy over the element if it is valid.
Owen Andersone992a562007-07-27 18:07:02 +0000164 const void *Elt = *BucketPtr;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000165 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
Owen Andersone992a562007-07-27 18:07:02 +0000166 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattnerc95dc982007-01-27 07:10:46 +0000167 }
168
Owen Anderson1629a1f2007-07-16 21:27:44 +0000169 free(OldBuckets);
Jeff Cohenac58a162007-04-14 21:50:21 +0000170 NumTombstones = 0;
171 }
172}
173
Duncan Sands2a8bf422010-06-30 15:02:37 +0000174SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage,
175 const SmallPtrSetImpl& that) {
176 SmallArray = SmallStorage;
177
Owen Andersonbf31b852007-07-24 21:31:23 +0000178 // If we're becoming small, prepare to insert into our stack space
Jeff Cohenac58a162007-04-14 21:50:21 +0000179 if (that.isSmall()) {
Duncan Sands2a8bf422010-06-30 15:02:37 +0000180 CurArray = SmallArray;
Owen Andersonbf31b852007-07-24 21:31:23 +0000181 // Otherwise, allocate new heap space (unless we were the same size)
Jeff Cohenac58a162007-04-14 21:50:21 +0000182 } else {
Owen Andersone992a562007-07-27 18:07:02 +0000183 CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1));
Owen Anderson1629a1f2007-07-16 21:27:44 +0000184 assert(CurArray && "Failed to allocate memory?");
Chris Lattnerc95dc982007-01-27 07:10:46 +0000185 }
Owen Andersonbf31b852007-07-24 21:31:23 +0000186
187 // Copy over the new array size
188 CurArraySize = that.CurArraySize;
189
190 // Copy over the contents from the other set
191 memcpy(CurArray, that.CurArray, sizeof(void*)*(CurArraySize+1));
192
193 NumElements = that.NumElements;
194 NumTombstones = that.NumTombstones;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000195}
Chris Lattner91f01582007-07-09 16:54:03 +0000196
197/// CopyFrom - implement operator= from a smallptrset that has the same pointer
198/// type, but may have a different small size.
199void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
Owen Anderson69b5d122007-07-09 22:27:20 +0000200 if (isSmall() && RHS.isSmall())
201 assert(CurArraySize == RHS.CurArraySize &&
202 "Cannot assign sets with different small sizes");
Owen Andersonb54b3152007-07-18 19:54:15 +0000203
Owen Anderson69b5d122007-07-09 22:27:20 +0000204 // If we're becoming small, prepare to insert into our stack space
Owen Anderson71a1e572007-07-19 06:45:33 +0000205 if (RHS.isSmall()) {
206 if (!isSmall())
207 free(CurArray);
Duncan Sands2a8bf422010-06-30 15:02:37 +0000208 CurArray = SmallArray;
Owen Anderson69b5d122007-07-09 22:27:20 +0000209 // Otherwise, allocate new heap space (unless we were the same size)
Owen Anderson71a1e572007-07-19 06:45:33 +0000210 } else if (CurArraySize != RHS.CurArraySize) {
Owen Andersonb54b3152007-07-18 19:54:15 +0000211 if (isSmall())
Owen Andersone992a562007-07-27 18:07:02 +0000212 CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
Owen Andersonb54b3152007-07-18 19:54:15 +0000213 else
Owen Andersone992a562007-07-27 18:07:02 +0000214 CurArray = (const void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1));
Owen Anderson1629a1f2007-07-16 21:27:44 +0000215 assert(CurArray && "Failed to allocate memory?");
216 }
Owen Anderson69b5d122007-07-09 22:27:20 +0000217
218 // Copy over the new array size
219 CurArraySize = RHS.CurArraySize;
220
221 // Copy over the contents from the other set
222 memcpy(CurArray, RHS.CurArray, sizeof(void*)*(CurArraySize+1));
Owen Andersonb54b3152007-07-18 19:54:15 +0000223
224 NumElements = RHS.NumElements;
225 NumTombstones = RHS.NumTombstones;
Chris Lattner91f01582007-07-09 16:54:03 +0000226}
Reid Spencer845b31d2007-07-17 02:16:12 +0000227
Benjamin Kramer2945a322012-03-06 20:40:02 +0000228void SmallPtrSetImpl::swap(SmallPtrSetImpl &RHS) {
229 if (this == &RHS) return;
230
231 // We can only avoid copying elements if neither set is small.
232 if (!this->isSmall() && !RHS.isSmall()) {
233 std::swap(this->CurArray, RHS.CurArray);
234 std::swap(this->CurArraySize, RHS.CurArraySize);
235 std::swap(this->NumElements, RHS.NumElements);
236 std::swap(this->NumTombstones, RHS.NumTombstones);
237 return;
238 }
239
240 // FIXME: From here on we assume that both sets have the same small size.
241
242 // If only RHS is small, copy the small elements into LHS and move the pointer
243 // from LHS to RHS.
244 if (!this->isSmall() && RHS.isSmall()) {
Benjamin Kramerf03e62a2012-03-07 22:48:42 +0000245 std::copy(RHS.SmallArray, RHS.SmallArray+RHS.CurArraySize,
246 this->SmallArray);
Benjamin Kramer2945a322012-03-06 20:40:02 +0000247 std::swap(this->NumElements, RHS.NumElements);
248 std::swap(this->CurArraySize, RHS.CurArraySize);
249 RHS.CurArray = this->CurArray;
250 RHS.NumTombstones = this->NumTombstones;
251 this->CurArray = this->SmallArray;
252 this->NumTombstones = 0;
253 return;
254 }
255
256 // If only LHS is small, copy the small elements into RHS and move the pointer
257 // from RHS to LHS.
258 if (this->isSmall() && !RHS.isSmall()) {
Benjamin Kramerf03e62a2012-03-07 22:48:42 +0000259 std::copy(this->SmallArray, this->SmallArray+this->CurArraySize,
260 RHS.SmallArray);
Benjamin Kramer2945a322012-03-06 20:40:02 +0000261 std::swap(RHS.NumElements, this->NumElements);
262 std::swap(RHS.CurArraySize, this->CurArraySize);
263 this->CurArray = RHS.CurArray;
264 this->NumTombstones = RHS.NumTombstones;
265 RHS.CurArray = RHS.SmallArray;
266 RHS.NumTombstones = 0;
267 return;
268 }
269
270 // Both a small, just swap the small elements.
271 assert(this->isSmall() && RHS.isSmall());
272 assert(this->CurArraySize == RHS.CurArraySize);
Benjamin Kramerf03e62a2012-03-07 22:48:42 +0000273 std::swap_ranges(this->SmallArray, this->SmallArray+this->CurArraySize,
Benjamin Kramer24e0e7c2012-03-07 22:33:21 +0000274 RHS.SmallArray);
Benjamin Kramer2945a322012-03-06 20:40:02 +0000275 std::swap(this->NumElements, RHS.NumElements);
276}
277
Reid Spencer845b31d2007-07-17 02:16:12 +0000278SmallPtrSetImpl::~SmallPtrSetImpl() {
279 if (!isSmall())
280 free(CurArray);
281}