blob: f0fed7792ce60ab1536e16c815a3aa9358ac3f54 [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.
Jean-Luc Duprat61733092013-03-29 05:45:22 +000032 CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
Chris Lattner42e4bdf2007-08-05 07:32:14 +000033 assert(CurArray && "Failed to allocate memory?");
34 memset(CurArray, -1, CurArraySize*sizeof(void*));
Chris Lattner42e4bdf2007-08-05 07:32:14 +000035}
36
Chris Lattner373a7332007-11-06 22:12:43 +000037bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
Chris Lattnerc95dc982007-01-27 07:10:46 +000038 if (isSmall()) {
39 // Check to see if it is already in the set.
Owen Andersone992a562007-07-27 18:07:02 +000040 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattnerc95dc982007-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 Olesene10fff62011-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 Lattnerc95dc982007-01-27 07:10:46 +000061
62 // Okay, we know we have space. Find a hash bucket.
Dan Gohman430b8a22008-08-05 14:45:15 +000063 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
Chris Lattnerc95dc982007-01-27 07:10:46 +000064 if (*Bucket == Ptr) return false; // Already inserted, good.
65
66 // Otherwise, insert it!
Chris Lattnere237cf92007-02-07 01:11:25 +000067 if (*Bucket == getTombstoneMarker())
68 --NumTombstones;
Dan Gohman430b8a22008-08-05 14:45:15 +000069 *Bucket = Ptr;
Chris Lattnerc95dc982007-01-27 07:10:46 +000070 ++NumElements; // Track density.
71 return true;
72}
73
Chris Lattner373a7332007-11-06 22:12:43 +000074bool SmallPtrSetImpl::erase_imp(const void * Ptr) {
Chris Lattner0b930852007-01-27 07:59:10 +000075 if (isSmall()) {
76 // Check to see if it is in the set.
Owen Andersone992a562007-07-27 18:07:02 +000077 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
Chris Lattner0b930852007-01-27 07:59:10 +000078 APtr != E; ++APtr)
79 if (*APtr == Ptr) {
Chris Lattner61766ca2007-06-21 23:23:32 +000080 // If it is in the set, replace this element.
81 *APtr = E[-1];
Chris Lattner0b930852007-01-27 07:59:10 +000082 E[-1] = getEmptyMarker();
83 --NumElements;
Chris Lattner7ef856d2007-02-05 23:10:31 +000084 return true;
Chris Lattner0b930852007-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 Lattnere237cf92007-02-07 01:11:25 +000097 ++NumTombstones;
Chris Lattner0b930852007-01-27 07:59:10 +000098 return true;
99}
100
Owen Andersone992a562007-07-27 18:07:02 +0000101const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
Benjamin Kramer4bb87cb2012-04-18 10:37:32 +0000102 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
Chris Lattnerc95dc982007-01-27 07:10:46 +0000103 unsigned ArraySize = CurArraySize;
104 unsigned ProbeAmt = 1;
Owen Andersone992a562007-07-27 18:07:02 +0000105 const void *const *Array = CurArray;
106 const void *const *Tombstone = 0;
Chris Lattnerc95dc982007-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 Olesene10fff62011-03-30 18:32:48 +0000130void SmallPtrSetImpl::Grow(unsigned NewSize) {
Chris Lattnerc95dc982007-01-27 07:10:46 +0000131 // Allocate at twice as many buckets, but at least 128.
132 unsigned OldSize = CurArraySize;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000133
Owen Andersone992a562007-07-27 18:07:02 +0000134 const void **OldBuckets = CurArray;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000135 bool WasSmall = isSmall();
136
137 // Install the new array. Clear all the buckets to empty.
Jean-Luc Duprat61733092013-03-29 05:45:22 +0000138 CurArray = (const void**)malloc(sizeof(void*) * NewSize);
Owen Anderson1629a1f2007-07-16 21:27:44 +0000139 assert(CurArray && "Failed to allocate memory?");
Chris Lattnerc95dc982007-01-27 07:10:46 +0000140 CurArraySize = NewSize;
141 memset(CurArray, -1, NewSize*sizeof(void*));
142
Chris Lattnerc95dc982007-01-27 07:10:46 +0000143 // Copy over all the elements.
144 if (WasSmall) {
145 // Small sets store their elements in order.
Owen Andersone992a562007-07-27 18:07:02 +0000146 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000147 BucketPtr != E; ++BucketPtr) {
Owen Andersone992a562007-07-27 18:07:02 +0000148 const void *Elt = *BucketPtr;
149 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattnerc95dc982007-01-27 07:10:46 +0000150 }
151 } else {
152 // Copy over all valid entries.
Owen Andersone992a562007-07-27 18:07:02 +0000153 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000154 BucketPtr != E; ++BucketPtr) {
155 // Copy over the element if it is valid.
Owen Andersone992a562007-07-27 18:07:02 +0000156 const void *Elt = *BucketPtr;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000157 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
Owen Andersone992a562007-07-27 18:07:02 +0000158 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Chris Lattnerc95dc982007-01-27 07:10:46 +0000159 }
160
Owen Anderson1629a1f2007-07-16 21:27:44 +0000161 free(OldBuckets);
Jeff Cohenac58a162007-04-14 21:50:21 +0000162 NumTombstones = 0;
163 }
164}
165
Duncan Sands2a8bf422010-06-30 15:02:37 +0000166SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage,
167 const SmallPtrSetImpl& that) {
168 SmallArray = SmallStorage;
169
Owen Andersonbf31b852007-07-24 21:31:23 +0000170 // If we're becoming small, prepare to insert into our stack space
Jeff Cohenac58a162007-04-14 21:50:21 +0000171 if (that.isSmall()) {
Duncan Sands2a8bf422010-06-30 15:02:37 +0000172 CurArray = SmallArray;
Owen Andersonbf31b852007-07-24 21:31:23 +0000173 // Otherwise, allocate new heap space (unless we were the same size)
Jeff Cohenac58a162007-04-14 21:50:21 +0000174 } else {
Jean-Luc Duprat61733092013-03-29 05:45:22 +0000175 CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
Owen Anderson1629a1f2007-07-16 21:27:44 +0000176 assert(CurArray && "Failed to allocate memory?");
Chris Lattnerc95dc982007-01-27 07:10:46 +0000177 }
Owen Andersonbf31b852007-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 Duprat61733092013-03-29 05:45:22 +0000183 memcpy(CurArray, that.CurArray, sizeof(void*)*CurArraySize);
Owen Andersonbf31b852007-07-24 21:31:23 +0000184
185 NumElements = that.NumElements;
186 NumTombstones = that.NumTombstones;
Chris Lattnerc95dc982007-01-27 07:10:46 +0000187}
Chris Lattner91f01582007-07-09 16:54:03 +0000188
189/// CopyFrom - implement operator= from a smallptrset that has the same pointer
190/// type, but may have a different small size.
191void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
Owen Anderson69b5d122007-07-09 22:27:20 +0000192 if (isSmall() && RHS.isSmall())
193 assert(CurArraySize == RHS.CurArraySize &&
194 "Cannot assign sets with different small sizes");
Jean-Luc Duprat61733092013-03-29 05:45:22 +0000195
Owen Anderson69b5d122007-07-09 22:27:20 +0000196 // If we're becoming small, prepare to insert into our stack space
Owen Anderson71a1e572007-07-19 06:45:33 +0000197 if (RHS.isSmall()) {
198 if (!isSmall())
199 free(CurArray);
Duncan Sands2a8bf422010-06-30 15:02:37 +0000200 CurArray = SmallArray;
Owen Anderson69b5d122007-07-09 22:27:20 +0000201 // Otherwise, allocate new heap space (unless we were the same size)
Owen Anderson71a1e572007-07-19 06:45:33 +0000202 } else if (CurArraySize != RHS.CurArraySize) {
Owen Andersonb54b3152007-07-18 19:54:15 +0000203 if (isSmall())
Jean-Luc Duprat61733092013-03-29 05:45:22 +0000204 CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
Owen Andersonb54b3152007-07-18 19:54:15 +0000205 else
Jean-Luc Duprat61733092013-03-29 05:45:22 +0000206 CurArray = (const void**)realloc(CurArray, sizeof(void*)*RHS.CurArraySize);
Owen Anderson1629a1f2007-07-16 21:27:44 +0000207 assert(CurArray && "Failed to allocate memory?");
208 }
Owen Anderson69b5d122007-07-09 22:27:20 +0000209
210 // Copy over the new array size
211 CurArraySize = RHS.CurArraySize;
212
213 // Copy over the contents from the other set
Jean-Luc Duprat61733092013-03-29 05:45:22 +0000214 memcpy(CurArray, RHS.CurArray, sizeof(void*)*CurArraySize);
Owen Andersonb54b3152007-07-18 19:54:15 +0000215
216 NumElements = RHS.NumElements;
217 NumTombstones = RHS.NumTombstones;
Chris Lattner91f01582007-07-09 16:54:03 +0000218}
Reid Spencer845b31d2007-07-17 02:16:12 +0000219
Benjamin Kramer2945a322012-03-06 20:40:02 +0000220void SmallPtrSetImpl::swap(SmallPtrSetImpl &RHS) {
221 if (this == &RHS) return;
222
223 // We can only avoid copying elements if neither set is small.
224 if (!this->isSmall() && !RHS.isSmall()) {
225 std::swap(this->CurArray, RHS.CurArray);
226 std::swap(this->CurArraySize, RHS.CurArraySize);
227 std::swap(this->NumElements, RHS.NumElements);
228 std::swap(this->NumTombstones, RHS.NumTombstones);
229 return;
230 }
231
232 // FIXME: From here on we assume that both sets have the same small size.
233
234 // If only RHS is small, copy the small elements into LHS and move the pointer
235 // from LHS to RHS.
236 if (!this->isSmall() && RHS.isSmall()) {
Benjamin Kramerf03e62a2012-03-07 22:48:42 +0000237 std::copy(RHS.SmallArray, RHS.SmallArray+RHS.CurArraySize,
238 this->SmallArray);
Benjamin Kramer2945a322012-03-06 20:40:02 +0000239 std::swap(this->NumElements, RHS.NumElements);
240 std::swap(this->CurArraySize, RHS.CurArraySize);
241 RHS.CurArray = this->CurArray;
242 RHS.NumTombstones = this->NumTombstones;
243 this->CurArray = this->SmallArray;
244 this->NumTombstones = 0;
245 return;
246 }
247
248 // If only LHS is small, copy the small elements into RHS and move the pointer
249 // from RHS to LHS.
250 if (this->isSmall() && !RHS.isSmall()) {
Benjamin Kramerf03e62a2012-03-07 22:48:42 +0000251 std::copy(this->SmallArray, this->SmallArray+this->CurArraySize,
252 RHS.SmallArray);
Benjamin Kramer2945a322012-03-06 20:40:02 +0000253 std::swap(RHS.NumElements, this->NumElements);
254 std::swap(RHS.CurArraySize, this->CurArraySize);
255 this->CurArray = RHS.CurArray;
256 this->NumTombstones = RHS.NumTombstones;
257 RHS.CurArray = RHS.SmallArray;
258 RHS.NumTombstones = 0;
259 return;
260 }
261
262 // Both a small, just swap the small elements.
263 assert(this->isSmall() && RHS.isSmall());
264 assert(this->CurArraySize == RHS.CurArraySize);
Benjamin Kramerf03e62a2012-03-07 22:48:42 +0000265 std::swap_ranges(this->SmallArray, this->SmallArray+this->CurArraySize,
Benjamin Kramer24e0e7c2012-03-07 22:33:21 +0000266 RHS.SmallArray);
Benjamin Kramer2945a322012-03-06 20:40:02 +0000267 std::swap(this->NumElements, RHS.NumElements);
268}
269
Reid Spencer845b31d2007-07-17 02:16:12 +0000270SmallPtrSetImpl::~SmallPtrSetImpl() {
271 if (!isSmall())
272 free(CurArray);
273}