blob: 119bb871d4c0a41c00cfa5700cd4a270649e2ed5 [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"
Matthias Braunc20b3382017-07-20 01:30:39 +000018#include "llvm/Support/ErrorHandling.h"
Benjamin Kramere1c34e92012-03-06 20:40:02 +000019#include <algorithm>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000020#include <cassert>
Reid Spencer5060fd02007-07-17 02:16:12 +000021#include <cstdlib>
22
Chris Lattner74102df2007-01-27 07:10:46 +000023using namespace llvm;
24
Chandler Carruth173bd7e2014-02-03 11:24:18 +000025void SmallPtrSetImplBase::shrink_and_clear() {
Chris Lattner44f7d3a2007-08-05 07:32:14 +000026 assert(!isSmall() && "Can't shrink a small set!");
27 free(CurArray);
28
29 // Reduce the number of buckets.
Matthias Braunacd531c2016-02-15 21:38:42 +000030 unsigned Size = size();
31 CurArraySize = Size > 16 ? 1 << (Log2_32_Ceil(Size) + 1) : 32;
32 NumNonEmpty = NumTombstones = 0;
Chris Lattner44f7d3a2007-08-05 07:32:14 +000033
34 // Install the new array. Clear all the buckets to empty.
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +000035 CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
Matthias Braunc20b3382017-07-20 01:30:39 +000036 if (CurArray == nullptr)
37 report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
38
Chris Lattner44f7d3a2007-08-05 07:32:14 +000039 memset(CurArray, -1, CurArraySize*sizeof(void*));
Chris Lattner44f7d3a2007-08-05 07:32:14 +000040}
41
David Blaikie70573dc2014-11-19 07:49:26 +000042std::pair<const void *const *, bool>
Matthias Braun4962bd72016-01-27 04:20:24 +000043SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
Matthias Braunacd531c2016-02-15 21:38:42 +000044 if (LLVM_UNLIKELY(size() * 4 >= CurArraySize * 3)) {
Jakob Stoklund Olesenbdc1b012011-03-30 18:32:48 +000045 // If more than 3/4 of the array is full, grow.
Matthias Braunacd531c2016-02-15 21:38:42 +000046 Grow(CurArraySize < 64 ? 128 : CurArraySize * 2);
47 } else if (LLVM_UNLIKELY(CurArraySize - NumNonEmpty < CurArraySize / 8)) {
Jakob Stoklund Olesenbdc1b012011-03-30 18:32:48 +000048 // If fewer of 1/8 of the array is empty (meaning that many are filled with
49 // tombstones), rehash.
50 Grow(CurArraySize);
51 }
Matthias Braunf67e5c72016-01-28 04:49:07 +000052
Chris Lattner74102df2007-01-27 07:10:46 +000053 // Okay, we know we have space. Find a hash bucket.
Dan Gohmane955c482008-08-05 14:45:15 +000054 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
David Blaikie70573dc2014-11-19 07:49:26 +000055 if (*Bucket == Ptr)
56 return std::make_pair(Bucket, false); // Already inserted, good.
57
Chris Lattner74102df2007-01-27 07:10:46 +000058 // Otherwise, insert it!
Chris Lattnere3467672007-02-07 01:11:25 +000059 if (*Bucket == getTombstoneMarker())
60 --NumTombstones;
Matthias Braunacd531c2016-02-15 21:38:42 +000061 else
62 ++NumNonEmpty; // Track density.
Dan Gohmane955c482008-08-05 14:45:15 +000063 *Bucket = Ptr;
Benjamin Kramerd76ed062017-10-13 20:37:52 +000064 incrementEpoch();
David Blaikie70573dc2014-11-19 07:49:26 +000065 return std::make_pair(Bucket, true);
Chris Lattner74102df2007-01-27 07:10:46 +000066}
67
Chandler Carruth173bd7e2014-02-03 11:24:18 +000068const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
Benjamin Kramer94988cb2012-04-18 10:37:32 +000069 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
Chris Lattner74102df2007-01-27 07:10:46 +000070 unsigned ArraySize = CurArraySize;
71 unsigned ProbeAmt = 1;
Owen Anderson49f037a2007-07-27 18:07:02 +000072 const void *const *Array = CurArray;
Craig Topperc10719f2014-04-07 04:17:22 +000073 const void *const *Tombstone = nullptr;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000074 while (true) {
Chris Lattner74102df2007-01-27 07:10:46 +000075 // If we found an empty bucket, the pointer doesn't exist in the set.
76 // Return a tombstone if we've seen one so far, or the empty bucket if
77 // not.
Benjamin Kramer654a85e2015-02-23 16:41:36 +000078 if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker()))
Chris Lattner74102df2007-01-27 07:10:46 +000079 return Tombstone ? Tombstone : Array+Bucket;
Benjamin Kramer654a85e2015-02-23 16:41:36 +000080
81 // Found Ptr's bucket?
82 if (LLVM_LIKELY(Array[Bucket] == Ptr))
83 return Array+Bucket;
84
Chris Lattner74102df2007-01-27 07:10:46 +000085 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
86 // prefer to return it than something that would require more probing.
87 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
88 Tombstone = Array+Bucket; // Remember the first tombstone found.
Matthias Braunf67e5c72016-01-28 04:49:07 +000089
Chris Lattner74102df2007-01-27 07:10:46 +000090 // It's a hash collision or a tombstone. Reprobe.
91 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
92 }
93}
94
95/// Grow - Allocate a larger backing store for the buckets and move it over.
96///
Chandler Carruth173bd7e2014-02-03 11:24:18 +000097void SmallPtrSetImplBase::Grow(unsigned NewSize) {
Owen Anderson49f037a2007-07-27 18:07:02 +000098 const void **OldBuckets = CurArray;
Matthias Braunacd531c2016-02-15 21:38:42 +000099 const void **OldEnd = EndPointer();
Chris Lattner74102df2007-01-27 07:10:46 +0000100 bool WasSmall = isSmall();
Matthias Braunf67e5c72016-01-28 04:49:07 +0000101
Chris Lattner74102df2007-01-27 07:10:46 +0000102 // Install the new array. Clear all the buckets to empty.
Matthias Braunc20b3382017-07-20 01:30:39 +0000103 const void **NewBuckets = (const void**) malloc(sizeof(void*) * NewSize);
104 if (NewBuckets == nullptr)
105 report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
106
107 // Reset member only if memory was allocated successfully
108 CurArray = NewBuckets;
Chris Lattner74102df2007-01-27 07:10:46 +0000109 CurArraySize = NewSize;
110 memset(CurArray, -1, NewSize*sizeof(void*));
Matthias Braunf67e5c72016-01-28 04:49:07 +0000111
Matthias Braunacd531c2016-02-15 21:38:42 +0000112 // Copy over all valid entries.
113 for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
114 // Copy over the element if it is valid.
115 const void *Elt = *BucketPtr;
116 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
Owen Anderson49f037a2007-07-27 18:07:02 +0000117 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000118 }
Matthias Braunacd531c2016-02-15 21:38:42 +0000119
120 if (!WasSmall)
121 free(OldBuckets);
122 NumNonEmpty -= NumTombstones;
123 NumTombstones = 0;
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000124}
125
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000126SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
Matthias Braunf67e5c72016-01-28 04:49:07 +0000127 const SmallPtrSetImplBase &that) {
Duncan Sands7b909662010-06-30 15:02:37 +0000128 SmallArray = SmallStorage;
129
Owen Anderson4c540242007-07-24 21:31:23 +0000130 // If we're becoming small, prepare to insert into our stack space
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000131 if (that.isSmall()) {
Duncan Sands7b909662010-06-30 15:02:37 +0000132 CurArray = SmallArray;
Owen Anderson4c540242007-07-24 21:31:23 +0000133 // Otherwise, allocate new heap space (unless we were the same size)
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000134 } else {
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000135 CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
Matthias Braunc20b3382017-07-20 01:30:39 +0000136 if (CurArray == nullptr)
137 report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
Chris Lattner74102df2007-01-27 07:10:46 +0000138 }
Matthias Braunf67e5c72016-01-28 04:49:07 +0000139
Matthias Braun924f0802016-01-28 04:49:11 +0000140 // Copy over the that array.
141 CopyHelper(that);
Chris Lattner74102df2007-01-27 07:10:46 +0000142}
Chris Lattner85049a42007-07-09 16:54:03 +0000143
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000144SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
145 unsigned SmallSize,
146 SmallPtrSetImplBase &&that) {
Chandler Carruth55758e92013-11-20 11:14:33 +0000147 SmallArray = SmallStorage;
Matthias Braun924f0802016-01-28 04:49:11 +0000148 MoveHelper(SmallSize, std::move(that));
Chandler Carruth55758e92013-11-20 11:14:33 +0000149}
Chandler Carruth55758e92013-11-20 11:14:33 +0000150
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000151void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
Chandler Carruth480f5d22013-11-26 00:54:44 +0000152 assert(&RHS != this && "Self-copy should be handled by the caller.");
Chandler Carruth26643172013-11-26 00:44:36 +0000153
Owen Andersone33356b2007-07-09 22:27:20 +0000154 if (isSmall() && RHS.isSmall())
155 assert(CurArraySize == RHS.CurArraySize &&
156 "Cannot assign sets with different small sizes");
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000157
Owen Andersone33356b2007-07-09 22:27:20 +0000158 // If we're becoming small, prepare to insert into our stack space
Owen Andersonee990102007-07-19 06:45:33 +0000159 if (RHS.isSmall()) {
160 if (!isSmall())
161 free(CurArray);
Duncan Sands7b909662010-06-30 15:02:37 +0000162 CurArray = SmallArray;
Owen Andersone33356b2007-07-09 22:27:20 +0000163 // Otherwise, allocate new heap space (unless we were the same size)
Owen Andersonee990102007-07-19 06:45:33 +0000164 } else if (CurArraySize != RHS.CurArraySize) {
Owen Andersone21f2702007-07-18 19:54:15 +0000165 if (isSmall())
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000166 CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
Aaron Ballman5de23782013-11-18 17:33:32 +0000167 else {
168 const void **T = (const void**)realloc(CurArray,
169 sizeof(void*) * RHS.CurArraySize);
170 if (!T)
171 free(CurArray);
172 CurArray = T;
173 }
Matthias Braunc20b3382017-07-20 01:30:39 +0000174 if (CurArray == nullptr)
175 report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
Owen Anderson81990a32007-07-16 21:27:44 +0000176 }
Matthias Braunf67e5c72016-01-28 04:49:07 +0000177
Matthias Braun924f0802016-01-28 04:49:11 +0000178 CopyHelper(RHS);
179}
180
181void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) {
Owen Andersone33356b2007-07-09 22:27:20 +0000182 // Copy over the new array size
183 CurArraySize = RHS.CurArraySize;
184
185 // Copy over the contents from the other set
Matthias Braunacd531c2016-02-15 21:38:42 +0000186 std::copy(RHS.CurArray, RHS.EndPointer(), CurArray);
Matthias Braunf67e5c72016-01-28 04:49:07 +0000187
Matthias Braunacd531c2016-02-15 21:38:42 +0000188 NumNonEmpty = RHS.NumNonEmpty;
Owen Andersone21f2702007-07-18 19:54:15 +0000189 NumTombstones = RHS.NumTombstones;
Chris Lattner85049a42007-07-09 16:54:03 +0000190}
Reid Spencer5060fd02007-07-17 02:16:12 +0000191
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000192void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
193 SmallPtrSetImplBase &&RHS) {
Chandler Carruth55758e92013-11-20 11:14:33 +0000194 if (!isSmall())
195 free(CurArray);
Matthias Braun924f0802016-01-28 04:49:11 +0000196 MoveHelper(SmallSize, std::move(RHS));
197}
198
199void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize,
200 SmallPtrSetImplBase &&RHS) {
201 assert(&RHS != this && "Self-move should be handled by the caller.");
Chandler Carruth55758e92013-11-20 11:14:33 +0000202
203 if (RHS.isSmall()) {
204 // Copy a small RHS rather than moving.
205 CurArray = SmallArray;
Matthias Braunacd531c2016-02-15 21:38:42 +0000206 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, CurArray);
Chandler Carruth55758e92013-11-20 11:14:33 +0000207 } else {
208 CurArray = RHS.CurArray;
209 RHS.CurArray = RHS.SmallArray;
210 }
211
212 // Copy the rest of the trivial members.
213 CurArraySize = RHS.CurArraySize;
Matthias Braunacd531c2016-02-15 21:38:42 +0000214 NumNonEmpty = RHS.NumNonEmpty;
Chandler Carruth55758e92013-11-20 11:14:33 +0000215 NumTombstones = RHS.NumTombstones;
Chandler Carruthc74010d2013-11-20 18:29:56 +0000216
217 // Make the RHS small and empty.
218 RHS.CurArraySize = SmallSize;
219 assert(RHS.CurArray == RHS.SmallArray);
Matthias Braunacd531c2016-02-15 21:38:42 +0000220 RHS.NumNonEmpty = 0;
Chandler Carruthc74010d2013-11-20 18:29:56 +0000221 RHS.NumTombstones = 0;
Chandler Carruth55758e92013-11-20 11:14:33 +0000222}
Chandler Carruth55758e92013-11-20 11:14:33 +0000223
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000224void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000225 if (this == &RHS) return;
226
227 // We can only avoid copying elements if neither set is small.
228 if (!this->isSmall() && !RHS.isSmall()) {
229 std::swap(this->CurArray, RHS.CurArray);
230 std::swap(this->CurArraySize, RHS.CurArraySize);
Matthias Braunacd531c2016-02-15 21:38:42 +0000231 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000232 std::swap(this->NumTombstones, RHS.NumTombstones);
233 return;
234 }
235
236 // FIXME: From here on we assume that both sets have the same small size.
237
238 // If only RHS is small, copy the small elements into LHS and move the pointer
239 // from LHS to RHS.
240 if (!this->isSmall() && RHS.isSmall()) {
Matthias Braunacd531c2016-02-15 21:38:42 +0000241 assert(RHS.CurArray == RHS.SmallArray);
242 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, this->SmallArray);
243 std::swap(RHS.CurArraySize, this->CurArraySize);
244 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
245 std::swap(this->NumTombstones, RHS.NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000246 RHS.CurArray = this->CurArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000247 this->CurArray = this->SmallArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000248 return;
249 }
250
251 // If only LHS is small, copy the small elements into RHS and move the pointer
252 // from RHS to LHS.
253 if (this->isSmall() && !RHS.isSmall()) {
Matthias Braunacd531c2016-02-15 21:38:42 +0000254 assert(this->CurArray == this->SmallArray);
255 std::copy(this->CurArray, this->CurArray + this->NumNonEmpty,
Benjamin Kramer22842f82012-03-07 22:48:42 +0000256 RHS.SmallArray);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000257 std::swap(RHS.CurArraySize, this->CurArraySize);
Matthias Braunacd531c2016-02-15 21:38:42 +0000258 std::swap(RHS.NumNonEmpty, this->NumNonEmpty);
259 std::swap(RHS.NumTombstones, this->NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000260 this->CurArray = RHS.CurArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000261 RHS.CurArray = RHS.SmallArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000262 return;
263 }
264
265 // Both a small, just swap the small elements.
266 assert(this->isSmall() && RHS.isSmall());
Matthias Braunacd531c2016-02-15 21:38:42 +0000267 unsigned MinNonEmpty = std::min(this->NumNonEmpty, RHS.NumNonEmpty);
268 std::swap_ranges(this->SmallArray, this->SmallArray + MinNonEmpty,
Benjamin Kramer6e8d4b82012-03-07 22:33:21 +0000269 RHS.SmallArray);
Matthias Braunacd531c2016-02-15 21:38:42 +0000270 if (this->NumNonEmpty > MinNonEmpty) {
271 std::copy(this->SmallArray + MinNonEmpty,
272 this->SmallArray + this->NumNonEmpty,
273 RHS.SmallArray + MinNonEmpty);
274 } else {
275 std::copy(RHS.SmallArray + MinNonEmpty, RHS.SmallArray + RHS.NumNonEmpty,
276 this->SmallArray + MinNonEmpty);
277 }
278 assert(this->CurArraySize == RHS.CurArraySize);
279 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
280 std::swap(this->NumTombstones, RHS.NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000281}