blob: aa12e85fa4c08156ac7162238b396ce6983b7b87 [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>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000019#include <cassert>
Reid Spencer5060fd02007-07-17 02:16:12 +000020#include <cstdlib>
21
Chris Lattner74102df2007-01-27 07:10:46 +000022using namespace llvm;
23
Chandler Carruth173bd7e2014-02-03 11:24:18 +000024void SmallPtrSetImplBase::shrink_and_clear() {
Chris Lattner44f7d3a2007-08-05 07:32:14 +000025 assert(!isSmall() && "Can't shrink a small set!");
26 free(CurArray);
27
28 // Reduce the number of buckets.
Matthias Braunacd531c2016-02-15 21:38:42 +000029 unsigned Size = size();
30 CurArraySize = Size > 16 ? 1 << (Log2_32_Ceil(Size) + 1) : 32;
31 NumNonEmpty = NumTombstones = 0;
Chris Lattner44f7d3a2007-08-05 07:32:14 +000032
33 // Install the new array. Clear all the buckets to empty.
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +000034 CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
Chris Lattner44f7d3a2007-08-05 07:32:14 +000035 assert(CurArray && "Failed to allocate memory?");
36 memset(CurArray, -1, CurArraySize*sizeof(void*));
Chris Lattner44f7d3a2007-08-05 07:32:14 +000037}
38
David Blaikie70573dc2014-11-19 07:49:26 +000039std::pair<const void *const *, bool>
Matthias Braun4962bd72016-01-27 04:20:24 +000040SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
Matthias Braunacd531c2016-02-15 21:38:42 +000041 if (LLVM_UNLIKELY(size() * 4 >= CurArraySize * 3)) {
Jakob Stoklund Olesenbdc1b012011-03-30 18:32:48 +000042 // If more than 3/4 of the array is full, grow.
Matthias Braunacd531c2016-02-15 21:38:42 +000043 Grow(CurArraySize < 64 ? 128 : CurArraySize * 2);
44 } else if (LLVM_UNLIKELY(CurArraySize - NumNonEmpty < CurArraySize / 8)) {
Jakob Stoklund Olesenbdc1b012011-03-30 18:32:48 +000045 // If fewer of 1/8 of the array is empty (meaning that many are filled with
46 // tombstones), rehash.
47 Grow(CurArraySize);
48 }
Matthias Braunf67e5c72016-01-28 04:49:07 +000049
Chris Lattner74102df2007-01-27 07:10:46 +000050 // Okay, we know we have space. Find a hash bucket.
Dan Gohmane955c482008-08-05 14:45:15 +000051 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
David Blaikie70573dc2014-11-19 07:49:26 +000052 if (*Bucket == Ptr)
53 return std::make_pair(Bucket, false); // Already inserted, good.
54
Chris Lattner74102df2007-01-27 07:10:46 +000055 // Otherwise, insert it!
Chris Lattnere3467672007-02-07 01:11:25 +000056 if (*Bucket == getTombstoneMarker())
57 --NumTombstones;
Matthias Braunacd531c2016-02-15 21:38:42 +000058 else
59 ++NumNonEmpty; // Track density.
Dan Gohmane955c482008-08-05 14:45:15 +000060 *Bucket = Ptr;
David Blaikie70573dc2014-11-19 07:49:26 +000061 return std::make_pair(Bucket, true);
Chris Lattner74102df2007-01-27 07:10:46 +000062}
63
Chandler Carruth173bd7e2014-02-03 11:24:18 +000064const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
Benjamin Kramer94988cb2012-04-18 10:37:32 +000065 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
Chris Lattner74102df2007-01-27 07:10:46 +000066 unsigned ArraySize = CurArraySize;
67 unsigned ProbeAmt = 1;
Owen Anderson49f037a2007-07-27 18:07:02 +000068 const void *const *Array = CurArray;
Craig Topperc10719f2014-04-07 04:17:22 +000069 const void *const *Tombstone = nullptr;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000070 while (true) {
Chris Lattner74102df2007-01-27 07:10:46 +000071 // If we found an empty bucket, the pointer doesn't exist in the set.
72 // Return a tombstone if we've seen one so far, or the empty bucket if
73 // not.
Benjamin Kramer654a85e2015-02-23 16:41:36 +000074 if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker()))
Chris Lattner74102df2007-01-27 07:10:46 +000075 return Tombstone ? Tombstone : Array+Bucket;
Benjamin Kramer654a85e2015-02-23 16:41:36 +000076
77 // Found Ptr's bucket?
78 if (LLVM_LIKELY(Array[Bucket] == Ptr))
79 return Array+Bucket;
80
Chris Lattner74102df2007-01-27 07:10:46 +000081 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
82 // prefer to return it than something that would require more probing.
83 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
84 Tombstone = Array+Bucket; // Remember the first tombstone found.
Matthias Braunf67e5c72016-01-28 04:49:07 +000085
Chris Lattner74102df2007-01-27 07:10:46 +000086 // It's a hash collision or a tombstone. Reprobe.
87 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
88 }
89}
90
91/// Grow - Allocate a larger backing store for the buckets and move it over.
92///
Chandler Carruth173bd7e2014-02-03 11:24:18 +000093void SmallPtrSetImplBase::Grow(unsigned NewSize) {
Owen Anderson49f037a2007-07-27 18:07:02 +000094 const void **OldBuckets = CurArray;
Matthias Braunacd531c2016-02-15 21:38:42 +000095 const void **OldEnd = EndPointer();
Chris Lattner74102df2007-01-27 07:10:46 +000096 bool WasSmall = isSmall();
Matthias Braunf67e5c72016-01-28 04:49:07 +000097
Chris Lattner74102df2007-01-27 07:10:46 +000098 // Install the new array. Clear all the buckets to empty.
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +000099 CurArray = (const void**)malloc(sizeof(void*) * NewSize);
Owen Anderson81990a32007-07-16 21:27:44 +0000100 assert(CurArray && "Failed to allocate memory?");
Chris Lattner74102df2007-01-27 07:10:46 +0000101 CurArraySize = NewSize;
102 memset(CurArray, -1, NewSize*sizeof(void*));
Matthias Braunf67e5c72016-01-28 04:49:07 +0000103
Matthias Braunacd531c2016-02-15 21:38:42 +0000104 // Copy over all valid entries.
105 for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
106 // Copy over the element if it is valid.
107 const void *Elt = *BucketPtr;
108 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
Owen Anderson49f037a2007-07-27 18:07:02 +0000109 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000110 }
Matthias Braunacd531c2016-02-15 21:38:42 +0000111
112 if (!WasSmall)
113 free(OldBuckets);
114 NumNonEmpty -= NumTombstones;
115 NumTombstones = 0;
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000116}
117
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000118SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
Matthias Braunf67e5c72016-01-28 04:49:07 +0000119 const SmallPtrSetImplBase &that) {
Duncan Sands7b909662010-06-30 15:02:37 +0000120 SmallArray = SmallStorage;
121
Owen Anderson4c540242007-07-24 21:31:23 +0000122 // If we're becoming small, prepare to insert into our stack space
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000123 if (that.isSmall()) {
Duncan Sands7b909662010-06-30 15:02:37 +0000124 CurArray = SmallArray;
Owen Anderson4c540242007-07-24 21:31:23 +0000125 // Otherwise, allocate new heap space (unless we were the same size)
Jeff Cohene7ce8f22007-04-14 21:50:21 +0000126 } else {
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000127 CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
Owen Anderson81990a32007-07-16 21:27:44 +0000128 assert(CurArray && "Failed to allocate memory?");
Chris Lattner74102df2007-01-27 07:10:46 +0000129 }
Matthias Braunf67e5c72016-01-28 04:49:07 +0000130
Matthias Braun924f0802016-01-28 04:49:11 +0000131 // Copy over the that array.
132 CopyHelper(that);
Chris Lattner74102df2007-01-27 07:10:46 +0000133}
Chris Lattner85049a42007-07-09 16:54:03 +0000134
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000135SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
136 unsigned SmallSize,
137 SmallPtrSetImplBase &&that) {
Chandler Carruth55758e92013-11-20 11:14:33 +0000138 SmallArray = SmallStorage;
Matthias Braun924f0802016-01-28 04:49:11 +0000139 MoveHelper(SmallSize, std::move(that));
Chandler Carruth55758e92013-11-20 11:14:33 +0000140}
Chandler Carruth55758e92013-11-20 11:14:33 +0000141
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000142void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
Chandler Carruth480f5d22013-11-26 00:54:44 +0000143 assert(&RHS != this && "Self-copy should be handled by the caller.");
Chandler Carruth26643172013-11-26 00:44:36 +0000144
Owen Andersone33356b2007-07-09 22:27:20 +0000145 if (isSmall() && RHS.isSmall())
146 assert(CurArraySize == RHS.CurArraySize &&
147 "Cannot assign sets with different small sizes");
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000148
Owen Andersone33356b2007-07-09 22:27:20 +0000149 // If we're becoming small, prepare to insert into our stack space
Owen Andersonee990102007-07-19 06:45:33 +0000150 if (RHS.isSmall()) {
151 if (!isSmall())
152 free(CurArray);
Duncan Sands7b909662010-06-30 15:02:37 +0000153 CurArray = SmallArray;
Owen Andersone33356b2007-07-09 22:27:20 +0000154 // Otherwise, allocate new heap space (unless we were the same size)
Owen Andersonee990102007-07-19 06:45:33 +0000155 } else if (CurArraySize != RHS.CurArraySize) {
Owen Andersone21f2702007-07-18 19:54:15 +0000156 if (isSmall())
Jean-Luc Duprat89fe2472013-03-29 22:07:12 +0000157 CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
Aaron Ballman5de23782013-11-18 17:33:32 +0000158 else {
159 const void **T = (const void**)realloc(CurArray,
160 sizeof(void*) * RHS.CurArraySize);
161 if (!T)
162 free(CurArray);
163 CurArray = T;
164 }
Owen Anderson81990a32007-07-16 21:27:44 +0000165 assert(CurArray && "Failed to allocate memory?");
166 }
Matthias Braunf67e5c72016-01-28 04:49:07 +0000167
Matthias Braun924f0802016-01-28 04:49:11 +0000168 CopyHelper(RHS);
169}
170
171void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) {
Owen Andersone33356b2007-07-09 22:27:20 +0000172 // Copy over the new array size
173 CurArraySize = RHS.CurArraySize;
174
175 // Copy over the contents from the other set
Matthias Braunacd531c2016-02-15 21:38:42 +0000176 std::copy(RHS.CurArray, RHS.EndPointer(), CurArray);
Matthias Braunf67e5c72016-01-28 04:49:07 +0000177
Matthias Braunacd531c2016-02-15 21:38:42 +0000178 NumNonEmpty = RHS.NumNonEmpty;
Owen Andersone21f2702007-07-18 19:54:15 +0000179 NumTombstones = RHS.NumTombstones;
Chris Lattner85049a42007-07-09 16:54:03 +0000180}
Reid Spencer5060fd02007-07-17 02:16:12 +0000181
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000182void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
183 SmallPtrSetImplBase &&RHS) {
Chandler Carruth55758e92013-11-20 11:14:33 +0000184 if (!isSmall())
185 free(CurArray);
Matthias Braun924f0802016-01-28 04:49:11 +0000186 MoveHelper(SmallSize, std::move(RHS));
187}
188
189void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize,
190 SmallPtrSetImplBase &&RHS) {
191 assert(&RHS != this && "Self-move should be handled by the caller.");
Chandler Carruth55758e92013-11-20 11:14:33 +0000192
193 if (RHS.isSmall()) {
194 // Copy a small RHS rather than moving.
195 CurArray = SmallArray;
Matthias Braunacd531c2016-02-15 21:38:42 +0000196 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, CurArray);
Chandler Carruth55758e92013-11-20 11:14:33 +0000197 } else {
198 CurArray = RHS.CurArray;
199 RHS.CurArray = RHS.SmallArray;
200 }
201
202 // Copy the rest of the trivial members.
203 CurArraySize = RHS.CurArraySize;
Matthias Braunacd531c2016-02-15 21:38:42 +0000204 NumNonEmpty = RHS.NumNonEmpty;
Chandler Carruth55758e92013-11-20 11:14:33 +0000205 NumTombstones = RHS.NumTombstones;
Chandler Carruthc74010d2013-11-20 18:29:56 +0000206
207 // Make the RHS small and empty.
208 RHS.CurArraySize = SmallSize;
209 assert(RHS.CurArray == RHS.SmallArray);
Matthias Braunacd531c2016-02-15 21:38:42 +0000210 RHS.NumNonEmpty = 0;
Chandler Carruthc74010d2013-11-20 18:29:56 +0000211 RHS.NumTombstones = 0;
Chandler Carruth55758e92013-11-20 11:14:33 +0000212}
Chandler Carruth55758e92013-11-20 11:14:33 +0000213
Chandler Carruth173bd7e2014-02-03 11:24:18 +0000214void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000215 if (this == &RHS) return;
216
217 // We can only avoid copying elements if neither set is small.
218 if (!this->isSmall() && !RHS.isSmall()) {
219 std::swap(this->CurArray, RHS.CurArray);
220 std::swap(this->CurArraySize, RHS.CurArraySize);
Matthias Braunacd531c2016-02-15 21:38:42 +0000221 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000222 std::swap(this->NumTombstones, RHS.NumTombstones);
223 return;
224 }
225
226 // FIXME: From here on we assume that both sets have the same small size.
227
228 // If only RHS is small, copy the small elements into LHS and move the pointer
229 // from LHS to RHS.
230 if (!this->isSmall() && RHS.isSmall()) {
Matthias Braunacd531c2016-02-15 21:38:42 +0000231 assert(RHS.CurArray == RHS.SmallArray);
232 std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, this->SmallArray);
233 std::swap(RHS.CurArraySize, this->CurArraySize);
234 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
235 std::swap(this->NumTombstones, RHS.NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000236 RHS.CurArray = this->CurArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000237 this->CurArray = this->SmallArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000238 return;
239 }
240
241 // If only LHS is small, copy the small elements into RHS and move the pointer
242 // from RHS to LHS.
243 if (this->isSmall() && !RHS.isSmall()) {
Matthias Braunacd531c2016-02-15 21:38:42 +0000244 assert(this->CurArray == this->SmallArray);
245 std::copy(this->CurArray, this->CurArray + this->NumNonEmpty,
Benjamin Kramer22842f82012-03-07 22:48:42 +0000246 RHS.SmallArray);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000247 std::swap(RHS.CurArraySize, this->CurArraySize);
Matthias Braunacd531c2016-02-15 21:38:42 +0000248 std::swap(RHS.NumNonEmpty, this->NumNonEmpty);
249 std::swap(RHS.NumTombstones, this->NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000250 this->CurArray = RHS.CurArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000251 RHS.CurArray = RHS.SmallArray;
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000252 return;
253 }
254
255 // Both a small, just swap the small elements.
256 assert(this->isSmall() && RHS.isSmall());
Matthias Braunacd531c2016-02-15 21:38:42 +0000257 unsigned MinNonEmpty = std::min(this->NumNonEmpty, RHS.NumNonEmpty);
258 std::swap_ranges(this->SmallArray, this->SmallArray + MinNonEmpty,
Benjamin Kramer6e8d4b82012-03-07 22:33:21 +0000259 RHS.SmallArray);
Matthias Braunacd531c2016-02-15 21:38:42 +0000260 if (this->NumNonEmpty > MinNonEmpty) {
261 std::copy(this->SmallArray + MinNonEmpty,
262 this->SmallArray + this->NumNonEmpty,
263 RHS.SmallArray + MinNonEmpty);
264 } else {
265 std::copy(RHS.SmallArray + MinNonEmpty, RHS.SmallArray + RHS.NumNonEmpty,
266 this->SmallArray + MinNonEmpty);
267 }
268 assert(this->CurArraySize == RHS.CurArraySize);
269 std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
270 std::swap(this->NumTombstones, RHS.NumTombstones);
Benjamin Kramere1c34e92012-03-06 20:40:02 +0000271}