blob: 47e960e243cf581b09749a9c8ea44e8b4d49c080 [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;
David Blaikie70573dc2014-11-19 07:49:26 +000064 return std::make_pair(Bucket, true);
Chris Lattner74102df2007-01-27 07:10:46 +000065}
66
Chandler Carruth173bd7e2014-02-03 11:24:18 +000067const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
Benjamin Kramer94988cb2012-04-18 10:37:32 +000068 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
Chris Lattner74102df2007-01-27 07:10:46 +000069 unsigned ArraySize = CurArraySize;
70 unsigned ProbeAmt = 1;
Owen Anderson49f037a2007-07-27 18:07:02 +000071 const void *const *Array = CurArray;
Craig Topperc10719f2014-04-07 04:17:22 +000072 const void *const *Tombstone = nullptr;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000073 while (true) {
Chris Lattner74102df2007-01-27 07:10:46 +000074 // If we found an empty bucket, the pointer doesn't exist in the set.
75 // Return a tombstone if we've seen one so far, or the empty bucket if
76 // not.
Benjamin Kramer654a85e2015-02-23 16:41:36 +000077 if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker()))
Chris Lattner74102df2007-01-27 07:10:46 +000078 return Tombstone ? Tombstone : Array+Bucket;
Benjamin Kramer654a85e2015-02-23 16:41:36 +000079
80 // Found Ptr's bucket?
81 if (LLVM_LIKELY(Array[Bucket] == Ptr))
82 return Array+Bucket;
83
Chris Lattner74102df2007-01-27 07:10:46 +000084 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
85 // prefer to return it than something that would require more probing.
86 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
87 Tombstone = Array+Bucket; // Remember the first tombstone found.
Matthias Braunf67e5c72016-01-28 04:49:07 +000088
Chris Lattner74102df2007-01-27 07:10:46 +000089 // It's a hash collision or a tombstone. Reprobe.
90 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
91 }
92}
93
94/// Grow - Allocate a larger backing store for the buckets and move it over.
95///
Chandler Carruth173bd7e2014-02-03 11:24:18 +000096void SmallPtrSetImplBase::Grow(unsigned NewSize) {
Owen Anderson49f037a2007-07-27 18:07:02 +000097 const void **OldBuckets = CurArray;
Matthias Braunacd531c2016-02-15 21:38:42 +000098 const void **OldEnd = EndPointer();
Chris Lattner74102df2007-01-27 07:10:46 +000099 bool WasSmall = isSmall();
Matthias Braunf67e5c72016-01-28 04:49:07 +0000100
Chris Lattner74102df2007-01-27 07:10:46 +0000101 // Install the new array. Clear all the buckets to empty.
Matthias Braunc20b3382017-07-20 01:30:39 +0000102 const void **NewBuckets = (const void**) malloc(sizeof(void*) * NewSize);
103 if (NewBuckets == nullptr)
104 report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
105
106 // Reset member only if memory was allocated successfully
107 CurArray = NewBuckets;
Chris Lattner74102df2007-01-27 07:10:46 +0000108 CurArraySize = NewSize;
109 memset(CurArray, -1, NewSize*sizeof(void*));
Matthias Braunf67e5c72016-01-28 04:49:07 +0000110
Matthias Braunacd531c2016-02-15 21:38:42 +0000111 // Copy over all valid entries.
112 for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
113 // Copy over the element if it is valid.
114 const void *Elt = *BucketPtr;
115 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
Owen Anderson49f037a2007-07-27 18:07:02 +0000116 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000117 }
Matthias Braunacd531c2016-02-15 21:38:42 +0000118
119 if (!WasSmall)
120 free(OldBuckets);
121 NumNonEmpty -= NumTombstones;
122 NumTombstones = 0;
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000123}
124
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000125SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
Matthias Braunf67e5c72016-01-28 04:49:07 +0000126 const SmallPtrSetImplBase &that) {
Duncan Sands7b909662010-06-30 15:02:37 +0000127 SmallArray = SmallStorage;
128
Owen Anderson4c540242007-07-24 21:31:23 +0000129 // If we're becoming small, prepare to insert into our stack space
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000130 if (that.isSmall()) {
Duncan Sands7b909662010-06-30 15:02:37 +0000131 CurArray = SmallArray;
Owen Anderson4c540242007-07-24 21:31:23 +0000132 // Otherwise, allocate new heap space (unless we were the same size)
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000133 } else {
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000134 CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
Matthias Braunc20b3382017-07-20 01:30:39 +0000135 if (CurArray == nullptr)
136 report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
Chris Lattner74102df2007-01-27 07:10:46 +0000137 }
Matthias Braunf67e5c72016-01-28 04:49:07 +0000138
Matthias Braun924f0802016-01-28 04:49:11 +0000139 // Copy over the that array.
140 CopyHelper(that);
Chris Lattner74102df2007-01-27 07:10:46 +0000141}
Chris Lattner85049a42007-07-09 16:54:03 +0000142
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000143SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
144 unsigned SmallSize,
145 SmallPtrSetImplBase &&that) {
Chandler Carruth55758e92013-11-20 11:14:33 +0000146 SmallArray = SmallStorage;
Matthias Braun924f0802016-01-28 04:49:11 +0000147 MoveHelper(SmallSize, std::move(that));
Chandler Carruth55758e92013-11-20 11:14:33 +0000148}
Chandler Carruth55758e92013-11-20 11:14:33 +0000149
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000150void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
Chandler Carruth480f5d22013-11-26 00:54:44 +0000151 assert(&RHS != this && "Self-copy should be handled by the caller.");
Chandler Carruth26643172013-11-26 00:44:36 +0000152
Owen Andersone33356b2007-07-09 22:27:20 +0000153 if (isSmall() && RHS.isSmall())
154 assert(CurArraySize == RHS.CurArraySize &&
155 "Cannot assign sets with different small sizes");
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000156
Owen Andersone33356b2007-07-09 22:27:20 +0000157 // If we're becoming small, prepare to insert into our stack space
Owen Andersonee990102007-07-19 06:45:33 +0000158 if (RHS.isSmall()) {
159 if (!isSmall())
160 free(CurArray);
Duncan Sands7b909662010-06-30 15:02:37 +0000161 CurArray = SmallArray;
Owen Andersone33356b2007-07-09 22:27:20 +0000162 // Otherwise, allocate new heap space (unless we were the same size)
Owen Andersonee990102007-07-19 06:45:33 +0000163 } else if (CurArraySize != RHS.CurArraySize) {
Owen Andersone21f2702007-07-18 19:54:15 +0000164 if (isSmall())
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000165 CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
Aaron Ballman5de23782013-11-18 17:33:32 +0000166 else {
167 const void **T = (const void**)realloc(CurArray,
168 sizeof(void*) * RHS.CurArraySize);
169 if (!T)
170 free(CurArray);
171 CurArray = T;
172 }
Matthias Braunc20b3382017-07-20 01:30:39 +0000173 if (CurArray == nullptr)
174 report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
Owen Anderson81990a32007-07-16 21:27:44 +0000175 }
Matthias Braunf67e5c72016-01-28 04:49:07 +0000176
Matthias Braun924f0802016-01-28 04:49:11 +0000177 CopyHelper(RHS);
178}
179
180void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) {
Owen Andersone33356b2007-07-09 22:27:20 +0000181 // Copy over the new array size
182 CurArraySize = RHS.CurArraySize;
183
184 // Copy over the contents from the other set
Matthias Braunacd531c2016-02-15 21:38:42 +0000185 std::copy(RHS.CurArray, RHS.EndPointer(), CurArray);
Matthias Braunf67e5c72016-01-28 04:49:07 +0000186
Matthias Braunacd531c2016-02-15 21:38:42 +0000187 NumNonEmpty = RHS.NumNonEmpty;
Owen Andersone21f2702007-07-18 19:54:15 +0000188 NumTombstones = RHS.NumTombstones;
Chris Lattner85049a42007-07-09 16:54:03 +0000189}
Reid Spencer5060fd02007-07-17 02:16:12 +0000190
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000191void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
192 SmallPtrSetImplBase &&RHS) {
Chandler Carruth55758e92013-11-20 11:14:33 +0000193 if (!isSmall())
194 free(CurArray);
Matthias Braun924f0802016-01-28 04:49:11 +0000195 MoveHelper(SmallSize, std::move(RHS));
196}
197
198void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize,
199 SmallPtrSetImplBase &&RHS) {
200 assert(&RHS != this && "Self-move should be handled by the caller.");
Chandler Carruth55758e92013-11-20 11:14:33 +0000201
202 if (RHS.isSmall()) {
203 // Copy a small RHS rather than moving.
204 CurArray = SmallArray;
Matthias Braunacd531c2016-02-15 21:38:42 +0000205 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, CurArray);
Chandler Carruth55758e92013-11-20 11:14:33 +0000206 } else {
207 CurArray = RHS.CurArray;
208 RHS.CurArray = RHS.SmallArray;
209 }
210
211 // Copy the rest of the trivial members.
212 CurArraySize = RHS.CurArraySize;
Matthias Braunacd531c2016-02-15 21:38:42 +0000213 NumNonEmpty = RHS.NumNonEmpty;
Chandler Carruth55758e92013-11-20 11:14:33 +0000214 NumTombstones = RHS.NumTombstones;
Chandler Carruthc74010d2013-11-20 18:29:56 +0000215
216 // Make the RHS small and empty.
217 RHS.CurArraySize = SmallSize;
218 assert(RHS.CurArray == RHS.SmallArray);
Matthias Braunacd531c2016-02-15 21:38:42 +0000219 RHS.NumNonEmpty = 0;
Chandler Carruthc74010d2013-11-20 18:29:56 +0000220 RHS.NumTombstones = 0;
Chandler Carruth55758e92013-11-20 11:14:33 +0000221}
Chandler Carruth55758e92013-11-20 11:14:33 +0000222
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000223void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000224 if (this == &RHS) return;
225
226 // We can only avoid copying elements if neither set is small.
227 if (!this->isSmall() && !RHS.isSmall()) {
228 std::swap(this->CurArray, RHS.CurArray);
229 std::swap(this->CurArraySize, RHS.CurArraySize);
Matthias Braunacd531c2016-02-15 21:38:42 +0000230 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000231 std::swap(this->NumTombstones, RHS.NumTombstones);
232 return;
233 }
234
235 // FIXME: From here on we assume that both sets have the same small size.
236
237 // If only RHS is small, copy the small elements into LHS and move the pointer
238 // from LHS to RHS.
239 if (!this->isSmall() && RHS.isSmall()) {
Matthias Braunacd531c2016-02-15 21:38:42 +0000240 assert(RHS.CurArray == RHS.SmallArray);
241 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, this->SmallArray);
242 std::swap(RHS.CurArraySize, this->CurArraySize);
243 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
244 std::swap(this->NumTombstones, RHS.NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000245 RHS.CurArray = this->CurArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000246 this->CurArray = this->SmallArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000247 return;
248 }
249
250 // If only LHS is small, copy the small elements into RHS and move the pointer
251 // from RHS to LHS.
252 if (this->isSmall() && !RHS.isSmall()) {
Matthias Braunacd531c2016-02-15 21:38:42 +0000253 assert(this->CurArray == this->SmallArray);
254 std::copy(this->CurArray, this->CurArray + this->NumNonEmpty,
Benjamin Kramer22842f82012-03-07 22:48:42 +0000255 RHS.SmallArray);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000256 std::swap(RHS.CurArraySize, this->CurArraySize);
Matthias Braunacd531c2016-02-15 21:38:42 +0000257 std::swap(RHS.NumNonEmpty, this->NumNonEmpty);
258 std::swap(RHS.NumTombstones, this->NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000259 this->CurArray = RHS.CurArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000260 RHS.CurArray = RHS.SmallArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000261 return;
262 }
263
264 // Both a small, just swap the small elements.
265 assert(this->isSmall() && RHS.isSmall());
Matthias Braunacd531c2016-02-15 21:38:42 +0000266 unsigned MinNonEmpty = std::min(this->NumNonEmpty, RHS.NumNonEmpty);
267 std::swap_ranges(this->SmallArray, this->SmallArray + MinNonEmpty,
Benjamin Kramer6e8d4b82012-03-07 22:33:21 +0000268 RHS.SmallArray);
Matthias Braunacd531c2016-02-15 21:38:42 +0000269 if (this->NumNonEmpty > MinNonEmpty) {
270 std::copy(this->SmallArray + MinNonEmpty,
271 this->SmallArray + this->NumNonEmpty,
272 RHS.SmallArray + MinNonEmpty);
273 } else {
274 std::copy(RHS.SmallArray + MinNonEmpty, RHS.SmallArray + RHS.NumNonEmpty,
275 this->SmallArray + MinNonEmpty);
276 }
277 assert(this->CurArraySize == RHS.CurArraySize);
278 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
279 std::swap(this->NumTombstones, RHS.NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000280}