1//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the SmallPtrSet class. See SmallPtrSet.h for an
10// overview of the algorithm.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/SmallPtrSet.h"
15#include "llvm/ADT/DenseMapInfo.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/Support/MathExtras.h"
18#include "llvm/Support/MemAlloc.h"
19#include <algorithm>
20#include <cassert>
21#include <cstdlib>
22
23using namespace llvm;
24
25void SmallPtrSetImplBase::shrink_and_clear() {
26 assert(!isSmall() && "Can't shrink a small set!");
27 free(ptr: CurArray);
28
29 // Reduce the number of buckets.
30 unsigned Size = size();
31 CurArraySize = Size > 16 ? 1 << (Log2_32_Ceil(Value: Size) + 1) : 32;
32 NumEntries = 0;
33
34 // Install the new array. Clear all the buckets to empty.
35 CurArray = (const void**)safe_malloc(Sz: sizeof(void*) * CurArraySize);
36
37 memset(s: CurArray, c: -1, n: CurArraySize*sizeof(void*));
38}
39
40std::pair<const void *const *, bool>
41SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
42 if (LLVM_UNLIKELY(size() * 3 >= CurArraySize * 2)) {
43 // If more than 2/3 of the array is full, grow.
44 Grow(NewSize: CurArraySize < 64 ? 128 : CurArraySize * 2);
45 }
46
47 // Find the first empty bucket or Ptr itself on the probe chain.
48 unsigned Mask = CurArraySize - 1;
49 unsigned I = DenseMapInfo<void *>::getHashValue(PtrVal: Ptr) & Mask;
50 const void **Array = CurArray;
51 while (Array[I] != getEmptyMarker()) {
52 if (Array[I] == Ptr)
53 return {Array + I, false};
54 I = (I + 1) & Mask;
55 }
56
57 // Insert into the empty bucket.
58 ++NumEntries;
59 Array[I] = Ptr;
60 incrementEpoch();
61 return {Array + I, true};
62}
63
64const void *const *SmallPtrSetImplBase::doFind(const void *Ptr) const {
65 unsigned Mask = CurArraySize - 1;
66 unsigned BucketNo = DenseMapInfo<void *>::getHashValue(PtrVal: Ptr) & Mask;
67 while (true) {
68 const void *const *Bucket = CurArray + BucketNo;
69 if (LLVM_LIKELY(*Bucket == Ptr))
70 return Bucket;
71 if (LLVM_LIKELY(*Bucket == getEmptyMarker()))
72 return nullptr;
73 BucketNo = (BucketNo + 1) & Mask;
74 }
75}
76
77void SmallPtrSetImplBase::eraseFromBucket(const void **Bucket) {
78 // Knuth TAOCP 6.4 Algorithm R: walk forward sliding each following entry
79 // whose probe path crosses the hole.
80 unsigned Mask = CurArraySize - 1;
81 unsigned I = Bucket - CurArray;
82 unsigned J = I;
83 const void *Empty = getEmptyMarker();
84 while ((J = (J + 1) & Mask), CurArray[J] != Empty) {
85 auto Ideal = DenseMapInfo<void *>::getHashValue(PtrVal: CurArray[J]);
86 if (((I - Ideal) & Mask) < ((J - Ideal) & Mask)) {
87 CurArray[I] = CurArray[J];
88 I = J;
89 }
90 }
91 CurArray[I] = Empty;
92}
93
94/// Grow - Allocate a larger backing store for the buckets and move it over.
95///
96void SmallPtrSetImplBase::Grow(unsigned NewSize) {
97 auto OldBuckets = buckets();
98 bool WasSmall = isSmall();
99
100 // Install the new array. Clear all the buckets to empty.
101 const void **NewBuckets = (const void**) safe_malloc(Sz: sizeof(void*) * NewSize);
102
103 // Reset member only if memory was allocated successfully
104 CurArray = NewBuckets;
105 CurArraySize = NewSize;
106 memset(s: CurArray, c: -1, n: NewSize*sizeof(void*));
107
108 // Copy over all valid entries.
109 unsigned Mask = CurArraySize - 1;
110 for (const void *Ptr : OldBuckets) {
111 if (Ptr == getEmptyMarker())
112 continue;
113 // Find the first empty bucket on this key's probe chain; there is no equal
114 // key, so nothing to compare against.
115 unsigned I = DenseMapInfo<void *>::getHashValue(PtrVal: Ptr) & Mask;
116 while (NewBuckets[I] != getEmptyMarker())
117 I = (I + 1) & Mask;
118 NewBuckets[I] = Ptr;
119 }
120
121 if (!WasSmall)
122 free(ptr: OldBuckets.begin());
123 IsSmall = false;
124}
125
126SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
127 const SmallPtrSetImplBase &that) {
128 IsSmall = that.isSmall();
129 if (IsSmall) {
130 // If we're becoming small, prepare to insert into our stack space
131 CurArray = SmallStorage;
132 } else {
133 // Otherwise, allocate new heap space (unless we were the same size)
134 CurArray = (const void**)safe_malloc(Sz: sizeof(void*) * that.CurArraySize);
135 }
136
137 // Copy over the that array.
138 copyHelper(RHS: that);
139}
140
141SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
142 unsigned SmallSize,
143 const void **RHSSmallStorage,
144 SmallPtrSetImplBase &&that) {
145 moveHelper(SmallStorage, SmallSize, RHSSmallStorage, RHS: std::move(that));
146}
147
148void SmallPtrSetImplBase::copyFrom(const void **SmallStorage,
149 const SmallPtrSetImplBase &RHS) {
150 assert(&RHS != this && "Self-copy should be handled by the caller.");
151
152 if (isSmall() && RHS.isSmall())
153 assert(CurArraySize == RHS.CurArraySize &&
154 "Cannot assign sets with different small sizes");
155
156 // If we're becoming small, prepare to insert into our stack space
157 if (RHS.isSmall()) {
158 if (!isSmall())
159 free(ptr: CurArray);
160 CurArray = SmallStorage;
161 IsSmall = true;
162 // Otherwise, allocate new heap space (unless we were the same size)
163 } else if (CurArraySize != RHS.CurArraySize) {
164 if (isSmall())
165 CurArray = (const void**)safe_malloc(Sz: sizeof(void*) * RHS.CurArraySize);
166 else {
167 const void **T = (const void**)safe_realloc(Ptr: CurArray,
168 Sz: sizeof(void*) * RHS.CurArraySize);
169 CurArray = T;
170 }
171 IsSmall = false;
172 }
173
174 copyHelper(RHS);
175}
176
177void SmallPtrSetImplBase::copyHelper(const SmallPtrSetImplBase &RHS) {
178 // Copy over the new array size
179 CurArraySize = RHS.CurArraySize;
180
181 // Copy over the contents from the other set
182 llvm::copy(Range: RHS.buckets(), Out: CurArray);
183
184 NumEntries = RHS.NumEntries;
185}
186
187void SmallPtrSetImplBase::moveFrom(const void **SmallStorage,
188 unsigned SmallSize,
189 const void **RHSSmallStorage,
190 SmallPtrSetImplBase &&RHS) {
191 if (!isSmall())
192 free(ptr: CurArray);
193 moveHelper(SmallStorage, SmallSize, RHSSmallStorage, RHS: std::move(RHS));
194}
195
196void SmallPtrSetImplBase::moveHelper(const void **SmallStorage,
197 unsigned SmallSize,
198 const void **RHSSmallStorage,
199 SmallPtrSetImplBase &&RHS) {
200 assert(&RHS != this && "Self-move should be handled by the caller.");
201
202 if (RHS.isSmall()) {
203 // Copy a small RHS rather than moving.
204 CurArray = SmallStorage;
205 llvm::copy(Range: RHS.small_buckets(), Out: CurArray);
206 } else {
207 CurArray = RHS.CurArray;
208 RHS.CurArray = RHSSmallStorage;
209 }
210
211 // Copy the rest of the trivial members.
212 CurArraySize = RHS.CurArraySize;
213 NumEntries = RHS.NumEntries;
214 IsSmall = RHS.IsSmall;
215
216 // Make the RHS small and empty.
217 RHS.CurArraySize = SmallSize;
218 RHS.NumEntries = 0;
219 RHS.IsSmall = true;
220}
221
222void SmallPtrSetImplBase::swap(const void **SmallStorage,
223 const void **RHSSmallStorage,
224 SmallPtrSetImplBase &RHS) {
225 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(a&: this->CurArray, b&: RHS.CurArray);
230 std::swap(a&: this->CurArraySize, b&: RHS.CurArraySize);
231 std::swap(a&: this->NumEntries, b&: RHS.NumEntries);
232 return;
233 }
234
235 // FIXME: From here on we assume that both sets have the same small size.
236
237 // Both a small, just swap the small elements.
238 if (this->isSmall() && RHS.isSmall()) {
239 unsigned MinEntries = std::min(a: this->NumEntries, b: RHS.NumEntries);
240 std::swap_ranges(first1: this->CurArray, last1: this->CurArray + MinEntries, first2: RHS.CurArray);
241 if (this->NumEntries > MinEntries) {
242 std::copy(first: this->CurArray + MinEntries, last: this->CurArray + this->NumEntries,
243 result: RHS.CurArray + MinEntries);
244 } else {
245 std::copy(first: RHS.CurArray + MinEntries, last: RHS.CurArray + RHS.NumEntries,
246 result: this->CurArray + MinEntries);
247 }
248 assert(this->CurArraySize == RHS.CurArraySize);
249 std::swap(a&: this->NumEntries, b&: RHS.NumEntries);
250 return;
251 }
252
253 // If only one side is small, copy the small elements into the large side and
254 // move the pointer from the large side to the small side.
255 SmallPtrSetImplBase &SmallSide = this->isSmall() ? *this : RHS;
256 SmallPtrSetImplBase &LargeSide = this->isSmall() ? RHS : *this;
257 const void **LargeSideInlineStorage =
258 this->isSmall() ? RHSSmallStorage : SmallStorage;
259 llvm::copy(Range: SmallSide.small_buckets(), Out: LargeSideInlineStorage);
260 std::swap(a&: LargeSide.CurArraySize, b&: SmallSide.CurArraySize);
261 std::swap(a&: LargeSide.NumEntries, b&: SmallSide.NumEntries);
262 SmallSide.CurArray = LargeSide.CurArray;
263 SmallSide.IsSmall = false;
264 LargeSide.CurArray = LargeSideInlineStorage;
265 LargeSide.IsSmall = true;
266}
267