LLVM 23.0.0git
SmallPtrSet.cpp
Go to the documentation of this file.
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
16#include "llvm/ADT/STLExtras.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(CurArray);
28
29 // Reduce the number of buckets.
30 unsigned Size = size();
31 CurArraySize = Size > 16 ? 1 << (Log2_32_Ceil(Size) + 1) : 32;
32 NumEntries = 0;
33
34 // Install the new array. Clear all the buckets to empty.
35 CurArray = (const void**)safe_malloc(sizeof(void*) * CurArraySize);
36
37 memset(CurArray, -1, 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(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(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;
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(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 }
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) {
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(sizeof(void*) * NewSize);
102
103 // Reset member only if memory was allocated successfully
104 CurArray = NewBuckets;
105 CurArraySize = NewSize;
106 memset(CurArray, -1, 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(Ptr) & Mask;
116 while (NewBuckets[I] != getEmptyMarker())
117 I = (I + 1) & Mask;
118 NewBuckets[I] = Ptr;
119 }
120
121 if (!WasSmall)
122 free(OldBuckets.begin());
123 IsSmall = false;
124}
125
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(sizeof(void*) * that.CurArraySize);
135 }
136
137 // Copy over the that array.
138 copyHelper(that);
139}
140
142 unsigned SmallSize,
143 const void **RHSSmallStorage,
144 SmallPtrSetImplBase &&that) {
145 moveHelper(SmallStorage, SmallSize, RHSSmallStorage, 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(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(sizeof(void*) * RHS.CurArraySize);
166 else {
167 const void **T = (const void**)safe_realloc(CurArray,
168 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(RHS.buckets(), 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(CurArray);
193 moveHelper(SmallStorage, SmallSize, RHSSmallStorage, 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(RHS.small_buckets(), 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(this->CurArray, RHS.CurArray);
230 std::swap(this->CurArraySize, RHS.CurArraySize);
231 std::swap(this->NumEntries, 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(this->NumEntries, RHS.NumEntries);
240 std::swap_ranges(this->CurArray, this->CurArray + MinEntries, RHS.CurArray);
241 if (this->NumEntries > MinEntries) {
242 std::copy(this->CurArray + MinEntries, this->CurArray + this->NumEntries,
243 RHS.CurArray + MinEntries);
244 } else {
245 std::copy(RHS.CurArray + MinEntries, RHS.CurArray + RHS.NumEntries,
246 this->CurArray + MinEntries);
247 }
248 assert(this->CurArraySize == RHS.CurArraySize);
249 std::swap(this->NumEntries, 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(SmallSide.small_buckets(), LargeSideInlineStorage);
260 std::swap(LargeSide.CurArraySize, SmallSide.CurArraySize);
261 std::swap(LargeSide.NumEntries, SmallSide.NumEntries);
262 SmallSide.CurArray = LargeSide.CurArray;
263 SmallSide.IsSmall = false;
264 LargeSide.CurArray = LargeSideInlineStorage;
265 LargeSide.IsSmall = true;
266}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:338
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:337
This file defines DenseMapInfo traits for DenseMap.
#define I(x, y, z)
Definition MD5.cpp:57
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
#define T
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallPtrSet class.
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
Value * RHS
SmallPtrSetImplBase - This is the common code shared among all the SmallPtrSet<>'s,...
Definition SmallPtrSet.h:58
size_type size() const
Definition SmallPtrSet.h:99
iterator_range< const void ** > buckets()
LLVM_ABI SmallPtrSetImplBase(const void **SmallStorage, const SmallPtrSetImplBase &that)
unsigned NumEntries
Number of elements in CurArray that contain a value.
Definition SmallPtrSet.h:70
const void ** CurArray
The current set of buckets, in either small or big representation.
Definition SmallPtrSet.h:63
bool IsSmall
Whether the set is in small representation.
Definition SmallPtrSet.h:72
LLVM_ABI void copyFrom(const void **SmallStorage, const SmallPtrSetImplBase &RHS)
LLVM_ABI void moveFrom(const void **SmallStorage, unsigned SmallSize, const void **RHSSmallStorage, SmallPtrSetImplBase &&RHS)
iterator_range< const void ** > small_buckets()
unsigned CurArraySize
CurArraySize - The allocated size of CurArray, always a power of two.
Definition SmallPtrSet.h:65
LLVM_ABI void eraseFromBucket(const void **Bucket)
Erase the entry at Bucket and close the resulting hole via Knuth TAOCP 6.4 Algorithm R.
static void * getEmptyMarker()
LLVM_ABI void Grow(unsigned NewSize)
Allocate a larger backing store for the buckets and move it over.
LLVM_ABI void swap(const void **SmallStorage, const void **RHSSmallStorage, SmallPtrSetImplBase &RHS)
swap - Swaps the elements of two sets.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
This is an optimization pass for GlobalISel generic memory operations.
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition MemAlloc.h:25
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_realloc(void *Ptr, size_t Sz)
Definition MemAlloc.h:52
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1885
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:862
An information struct used to provide DenseMap with the various necessary components for a given valu...