blob: 539b4eb34da15cbffddac886b10d3a0050ba4bb1 [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.
Matthias Braunacd531c2016-02-15 21:38:42 +000028 unsigned Size = size();
29 CurArraySize = Size > 16 ? 1 << (Log2_32_Ceil(Size) + 1) : 32;
30 NumNonEmpty = NumTombstones = 0;
Chris Lattner44f7d3a2007-08-05 07:32:14 +000031
32 // Install the new array. Clear all the buckets to empty.
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +000033 CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
Chris Lattner44f7d3a2007-08-05 07:32:14 +000034 assert(CurArray && "Failed to allocate memory?");
35 memset(CurArray, -1, CurArraySize*sizeof(void*));
Chris Lattner44f7d3a2007-08-05 07:32:14 +000036}
37
David Blaikie70573dc2014-11-19 07:49:26 +000038std::pair<const void *const *, bool>
Matthias Braun4962bd72016-01-27 04:20:24 +000039SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
Matthias Braunacd531c2016-02-15 21:38:42 +000040 if (LLVM_UNLIKELY(size() * 4 >= CurArraySize * 3)) {
Jakob Stoklund Olesenbdc1b012011-03-30 18:32:48 +000041 // If more than 3/4 of the array is full, grow.
Matthias Braunacd531c2016-02-15 21:38:42 +000042 Grow(CurArraySize < 64 ? 128 : CurArraySize * 2);
43 } else if (LLVM_UNLIKELY(CurArraySize - NumNonEmpty < CurArraySize / 8)) {
Jakob Stoklund Olesenbdc1b012011-03-30 18:32:48 +000044 // If fewer of 1/8 of the array is empty (meaning that many are filled with
45 // tombstones), rehash.
46 Grow(CurArraySize);
47 }
Matthias Braunf67e5c72016-01-28 04:49:07 +000048
Chris Lattner74102df2007-01-27 07:10:46 +000049 // Okay, we know we have space. Find a hash bucket.
Dan Gohmane955c482008-08-05 14:45:15 +000050 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
David Blaikie70573dc2014-11-19 07:49:26 +000051 if (*Bucket == Ptr)
52 return std::make_pair(Bucket, false); // Already inserted, good.
53
Chris Lattner74102df2007-01-27 07:10:46 +000054 // Otherwise, insert it!
Chris Lattnere3467672007-02-07 01:11:25 +000055 if (*Bucket == getTombstoneMarker())
56 --NumTombstones;
Matthias Braunacd531c2016-02-15 21:38:42 +000057 else
58 ++NumNonEmpty; // Track density.
Dan Gohmane955c482008-08-05 14:45:15 +000059 *Bucket = Ptr;
David Blaikie70573dc2014-11-19 07:49:26 +000060 return std::make_pair(Bucket, true);
Chris Lattner74102df2007-01-27 07:10:46 +000061}
62
Chandler Carruth173bd7e2014-02-03 11:24:18 +000063bool SmallPtrSetImplBase::erase_imp(const void * Ptr) {
Chris Lattner39ab70c2007-01-27 07:59:10 +000064 if (isSmall()) {
65 // Check to see if it is in the set.
Matthias Braunacd531c2016-02-15 21:38:42 +000066 for (const void **APtr = CurArray, **E = CurArray + NumNonEmpty; APtr != E;
67 ++APtr)
Chris Lattner39ab70c2007-01-27 07:59:10 +000068 if (*APtr == Ptr) {
Chris Lattner836e8f32007-06-21 23:23:32 +000069 // If it is in the set, replace this element.
Matthias Braunacd531c2016-02-15 21:38:42 +000070 *APtr = getTombstoneMarker();
71 ++NumTombstones;
Chris Lattner92c5d112007-02-05 23:10:31 +000072 return true;
Chris Lattner39ab70c2007-01-27 07:59:10 +000073 }
Matthias Braunf67e5c72016-01-28 04:49:07 +000074
Chris Lattner39ab70c2007-01-27 07:59:10 +000075 return false;
76 }
Matthias Braunf67e5c72016-01-28 04:49:07 +000077
Chris Lattner39ab70c2007-01-27 07:59:10 +000078 // Okay, we know we have space. Find a hash bucket.
79 void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
80 if (*Bucket != Ptr) return false; // Not in the set?
81
82 // Set this as a tombstone.
83 *Bucket = getTombstoneMarker();
Chris Lattnere3467672007-02-07 01:11:25 +000084 ++NumTombstones;
Chris Lattner39ab70c2007-01-27 07:59:10 +000085 return true;
86}
87
Chandler Carruth173bd7e2014-02-03 11:24:18 +000088const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
Benjamin Kramer94988cb2012-04-18 10:37:32 +000089 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
Chris Lattner74102df2007-01-27 07:10:46 +000090 unsigned ArraySize = CurArraySize;
91 unsigned ProbeAmt = 1;
Owen Anderson49f037a2007-07-27 18:07:02 +000092 const void *const *Array = CurArray;
Craig Topperc10719f2014-04-07 04:17:22 +000093 const void *const *Tombstone = nullptr;
Chris Lattner74102df2007-01-27 07:10:46 +000094 while (1) {
Chris Lattner74102df2007-01-27 07:10:46 +000095 // If we found an empty bucket, the pointer doesn't exist in the set.
96 // Return a tombstone if we've seen one so far, or the empty bucket if
97 // not.
Benjamin Kramer654a85e2015-02-23 16:41:36 +000098 if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker()))
Chris Lattner74102df2007-01-27 07:10:46 +000099 return Tombstone ? Tombstone : Array+Bucket;
Benjamin Kramer654a85e2015-02-23 16:41:36 +0000100
101 // Found Ptr's bucket?
102 if (LLVM_LIKELY(Array[Bucket] == Ptr))
103 return Array+Bucket;
104
Chris Lattner74102df2007-01-27 07:10:46 +0000105 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
106 // prefer to return it than something that would require more probing.
107 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
108 Tombstone = Array+Bucket; // Remember the first tombstone found.
Matthias Braunf67e5c72016-01-28 04:49:07 +0000109
Chris Lattner74102df2007-01-27 07:10:46 +0000110 // It's a hash collision or a tombstone. Reprobe.
111 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
112 }
113}
114
115/// Grow - Allocate a larger backing store for the buckets and move it over.
116///
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000117void SmallPtrSetImplBase::Grow(unsigned NewSize) {
Owen Anderson49f037a2007-07-27 18:07:02 +0000118 const void **OldBuckets = CurArray;
Matthias Braunacd531c2016-02-15 21:38:42 +0000119 const void **OldEnd = EndPointer();
Chris Lattner74102df2007-01-27 07:10:46 +0000120 bool WasSmall = isSmall();
Matthias Braunf67e5c72016-01-28 04:49:07 +0000121
Chris Lattner74102df2007-01-27 07:10:46 +0000122 // Install the new array. Clear all the buckets to empty.
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000123 CurArray = (const void**)malloc(sizeof(void*) * NewSize);
Owen Anderson81990a32007-07-16 21:27:44 +0000124 assert(CurArray && "Failed to allocate memory?");
Chris Lattner74102df2007-01-27 07:10:46 +0000125 CurArraySize = NewSize;
126 memset(CurArray, -1, NewSize*sizeof(void*));
Matthias Braunf67e5c72016-01-28 04:49:07 +0000127
Matthias Braunacd531c2016-02-15 21:38:42 +0000128 // Copy over all valid entries.
129 for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
130 // Copy over the element if it is valid.
131 const void *Elt = *BucketPtr;
132 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
Owen Anderson49f037a2007-07-27 18:07:02 +0000133 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000134 }
Matthias Braunacd531c2016-02-15 21:38:42 +0000135
136 if (!WasSmall)
137 free(OldBuckets);
138 NumNonEmpty -= NumTombstones;
139 NumTombstones = 0;
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000140}
141
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000142SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
Matthias Braunf67e5c72016-01-28 04:49:07 +0000143 const SmallPtrSetImplBase &that) {
Duncan Sands7b909662010-06-30 15:02:37 +0000144 SmallArray = SmallStorage;
145
Owen Anderson4c540242007-07-24 21:31:23 +0000146 // If we're becoming small, prepare to insert into our stack space
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000147 if (that.isSmall()) {
Duncan Sands7b909662010-06-30 15:02:37 +0000148 CurArray = SmallArray;
Owen Anderson4c540242007-07-24 21:31:23 +0000149 // Otherwise, allocate new heap space (unless we were the same size)
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000150 } else {
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000151 CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
Owen Anderson81990a32007-07-16 21:27:44 +0000152 assert(CurArray && "Failed to allocate memory?");
Chris Lattner74102df2007-01-27 07:10:46 +0000153 }
Matthias Braunf67e5c72016-01-28 04:49:07 +0000154
Matthias Braun924f0802016-01-28 04:49:11 +0000155 // Copy over the that array.
156 CopyHelper(that);
Chris Lattner74102df2007-01-27 07:10:46 +0000157}
Chris Lattner85049a42007-07-09 16:54:03 +0000158
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000159SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
160 unsigned SmallSize,
161 SmallPtrSetImplBase &&that) {
Chandler Carruth55758e92013-11-20 11:14:33 +0000162 SmallArray = SmallStorage;
Matthias Braun924f0802016-01-28 04:49:11 +0000163 MoveHelper(SmallSize, std::move(that));
Chandler Carruth55758e92013-11-20 11:14:33 +0000164}
Chandler Carruth55758e92013-11-20 11:14:33 +0000165
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000166void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
Chandler Carruth480f5d22013-11-26 00:54:44 +0000167 assert(&RHS != this && "Self-copy should be handled by the caller.");
Chandler Carruth26643172013-11-26 00:44:36 +0000168
Owen Andersone33356b2007-07-09 22:27:20 +0000169 if (isSmall() && RHS.isSmall())
170 assert(CurArraySize == RHS.CurArraySize &&
171 "Cannot assign sets with different small sizes");
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000172
Owen Andersone33356b2007-07-09 22:27:20 +0000173 // If we're becoming small, prepare to insert into our stack space
Owen Andersonee990102007-07-19 06:45:33 +0000174 if (RHS.isSmall()) {
175 if (!isSmall())
176 free(CurArray);
Duncan Sands7b909662010-06-30 15:02:37 +0000177 CurArray = SmallArray;
Owen Andersone33356b2007-07-09 22:27:20 +0000178 // Otherwise, allocate new heap space (unless we were the same size)
Owen Andersonee990102007-07-19 06:45:33 +0000179 } else if (CurArraySize != RHS.CurArraySize) {
Owen Andersone21f2702007-07-18 19:54:15 +0000180 if (isSmall())
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000181 CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
Aaron Ballman5de23782013-11-18 17:33:32 +0000182 else {
183 const void **T = (const void**)realloc(CurArray,
184 sizeof(void*) * RHS.CurArraySize);
185 if (!T)
186 free(CurArray);
187 CurArray = T;
188 }
Owen Anderson81990a32007-07-16 21:27:44 +0000189 assert(CurArray && "Failed to allocate memory?");
190 }
Matthias Braunf67e5c72016-01-28 04:49:07 +0000191
Matthias Braun924f0802016-01-28 04:49:11 +0000192 CopyHelper(RHS);
193}
194
195void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) {
Owen Andersone33356b2007-07-09 22:27:20 +0000196 // Copy over the new array size
197 CurArraySize = RHS.CurArraySize;
198
199 // Copy over the contents from the other set
Matthias Braunacd531c2016-02-15 21:38:42 +0000200 std::copy(RHS.CurArray, RHS.EndPointer(), CurArray);
Matthias Braunf67e5c72016-01-28 04:49:07 +0000201
Matthias Braunacd531c2016-02-15 21:38:42 +0000202 NumNonEmpty = RHS.NumNonEmpty;
Owen Andersone21f2702007-07-18 19:54:15 +0000203 NumTombstones = RHS.NumTombstones;
Chris Lattner85049a42007-07-09 16:54:03 +0000204}
Reid Spencer5060fd02007-07-17 02:16:12 +0000205
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000206void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
207 SmallPtrSetImplBase &&RHS) {
Chandler Carruth55758e92013-11-20 11:14:33 +0000208 if (!isSmall())
209 free(CurArray);
Matthias Braun924f0802016-01-28 04:49:11 +0000210 MoveHelper(SmallSize, std::move(RHS));
211}
212
213void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize,
214 SmallPtrSetImplBase &&RHS) {
215 assert(&RHS != this && "Self-move should be handled by the caller.");
Chandler Carruth55758e92013-11-20 11:14:33 +0000216
217 if (RHS.isSmall()) {
218 // Copy a small RHS rather than moving.
219 CurArray = SmallArray;
Matthias Braunacd531c2016-02-15 21:38:42 +0000220 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, CurArray);
Chandler Carruth55758e92013-11-20 11:14:33 +0000221 } else {
222 CurArray = RHS.CurArray;
223 RHS.CurArray = RHS.SmallArray;
224 }
225
226 // Copy the rest of the trivial members.
227 CurArraySize = RHS.CurArraySize;
Matthias Braunacd531c2016-02-15 21:38:42 +0000228 NumNonEmpty = RHS.NumNonEmpty;
Chandler Carruth55758e92013-11-20 11:14:33 +0000229 NumTombstones = RHS.NumTombstones;
Chandler Carruthc74010d2013-11-20 18:29:56 +0000230
231 // Make the RHS small and empty.
232 RHS.CurArraySize = SmallSize;
233 assert(RHS.CurArray == RHS.SmallArray);
Matthias Braunacd531c2016-02-15 21:38:42 +0000234 RHS.NumNonEmpty = 0;
Chandler Carruthc74010d2013-11-20 18:29:56 +0000235 RHS.NumTombstones = 0;
Chandler Carruth55758e92013-11-20 11:14:33 +0000236}
Chandler Carruth55758e92013-11-20 11:14:33 +0000237
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000238void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000239 if (this == &RHS) return;
240
241 // We can only avoid copying elements if neither set is small.
242 if (!this->isSmall() && !RHS.isSmall()) {
243 std::swap(this->CurArray, RHS.CurArray);
244 std::swap(this->CurArraySize, RHS.CurArraySize);
Matthias Braunacd531c2016-02-15 21:38:42 +0000245 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000246 std::swap(this->NumTombstones, RHS.NumTombstones);
247 return;
248 }
249
250 // FIXME: From here on we assume that both sets have the same small size.
251
252 // If only RHS is small, copy the small elements into LHS and move the pointer
253 // from LHS to RHS.
254 if (!this->isSmall() && RHS.isSmall()) {
Matthias Braunacd531c2016-02-15 21:38:42 +0000255 assert(RHS.CurArray == RHS.SmallArray);
256 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, this->SmallArray);
257 std::swap(RHS.CurArraySize, this->CurArraySize);
258 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
259 std::swap(this->NumTombstones, RHS.NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000260 RHS.CurArray = this->CurArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000261 this->CurArray = this->SmallArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000262 return;
263 }
264
265 // If only LHS is small, copy the small elements into RHS and move the pointer
266 // from RHS to LHS.
267 if (this->isSmall() && !RHS.isSmall()) {
Matthias Braunacd531c2016-02-15 21:38:42 +0000268 assert(this->CurArray == this->SmallArray);
269 std::copy(this->CurArray, this->CurArray + this->NumNonEmpty,
Benjamin Kramer22842f82012-03-07 22:48:42 +0000270 RHS.SmallArray);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000271 std::swap(RHS.CurArraySize, this->CurArraySize);
Matthias Braunacd531c2016-02-15 21:38:42 +0000272 std::swap(RHS.NumNonEmpty, this->NumNonEmpty);
273 std::swap(RHS.NumTombstones, this->NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000274 this->CurArray = RHS.CurArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000275 RHS.CurArray = RHS.SmallArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000276 return;
277 }
278
279 // Both a small, just swap the small elements.
280 assert(this->isSmall() && RHS.isSmall());
Matthias Braunacd531c2016-02-15 21:38:42 +0000281 unsigned MinNonEmpty = std::min(this->NumNonEmpty, RHS.NumNonEmpty);
282 std::swap_ranges(this->SmallArray, this->SmallArray + MinNonEmpty,
Benjamin Kramer6e8d4b82012-03-07 22:33:21 +0000283 RHS.SmallArray);
Matthias Braunacd531c2016-02-15 21:38:42 +0000284 if (this->NumNonEmpty > MinNonEmpty) {
285 std::copy(this->SmallArray + MinNonEmpty,
286 this->SmallArray + this->NumNonEmpty,
287 RHS.SmallArray + MinNonEmpty);
288 } else {
289 std::copy(RHS.SmallArray + MinNonEmpty, RHS.SmallArray + RHS.NumNonEmpty,
290 this->SmallArray + MinNonEmpty);
291 }
292 assert(this->CurArraySize == RHS.CurArraySize);
293 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
294 std::swap(this->NumTombstones, RHS.NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000295}