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 // Okay, we know we have space. Find a hash bucket.
48 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
49 if (*Bucket == Ptr)
50 return {Bucket, false}; // Already inserted, good.
51
52 // Otherwise, insert it.
53 ++NumEntries;
54 *Bucket = Ptr;
56 return {Bucket, true};
57}
58
59const void *const *SmallPtrSetImplBase::doFind(const void *Ptr) const {
60 unsigned Mask = CurArraySize - 1;
61 unsigned BucketNo = DenseMapInfo<void *>::getHashValue(Ptr) & Mask;
62 while (true) {
63 const void *const *Bucket = CurArray + BucketNo;
64 if (LLVM_LIKELY(*Bucket == Ptr))
65 return Bucket;
66 if (LLVM_LIKELY(*Bucket == getEmptyMarker()))
67 return nullptr;
68 BucketNo = (BucketNo + 1) & Mask;
69 }
70}
71
72const void *const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
73 unsigned Mask = CurArraySize - 1;
74 unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & Mask;
75 const void *const *Array = CurArray;
76 while (true) {
77 if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker()))
78 return Array + Bucket;
79 if (LLVM_LIKELY(Array[Bucket] == Ptr))
80 return Array + Bucket;
81 Bucket = (Bucket + 1) & Mask;
82 }
83}
84
85void SmallPtrSetImplBase::eraseFromBucket(const void **Bucket) {
86 // Knuth TAOCP 6.4 Algorithm R: walk forward sliding each following entry
87 // whose probe path crosses the hole.
88 unsigned Mask = CurArraySize - 1;
89 unsigned I = Bucket - CurArray;
90 unsigned J = I;
91 const void *Empty = getEmptyMarker();
92 while ((J = (J + 1) & Mask), CurArray[J] != Empty) {
94 if (((I - Ideal) & Mask) < ((J - Ideal) & Mask)) {
95 CurArray[I] = CurArray[J];
96 I = J;
97 }
98 }
99 CurArray[I] = Empty;
100}
101
102/// Grow - Allocate a larger backing store for the buckets and move it over.
103///
104void SmallPtrSetImplBase::Grow(unsigned NewSize) {
105 auto OldBuckets = buckets();
106 bool WasSmall = isSmall();
107
108 // Install the new array. Clear all the buckets to empty.
109 const void **NewBuckets = (const void**) safe_malloc(sizeof(void*) * NewSize);
110
111 // Reset member only if memory was allocated successfully
112 CurArray = NewBuckets;
113 CurArraySize = NewSize;
114 memset(CurArray, -1, NewSize*sizeof(void*));
115
116 // Copy over all valid entries.
117 for (const void *&Bucket : OldBuckets) {
118 // Copy over the element if it is valid.
119 if (Bucket != getEmptyMarker())
120 *const_cast<void **>(FindBucketFor(Bucket)) = const_cast<void *>(Bucket);
121 }
122
123 if (!WasSmall)
124 free(OldBuckets.begin());
125 IsSmall = false;
126}
127
129 const SmallPtrSetImplBase &that) {
130 IsSmall = that.isSmall();
131 if (IsSmall) {
132 // If we're becoming small, prepare to insert into our stack space
133 CurArray = SmallStorage;
134 } else {
135 // Otherwise, allocate new heap space (unless we were the same size)
136 CurArray = (const void**)safe_malloc(sizeof(void*) * that.CurArraySize);
137 }
138
139 // Copy over the that array.
140 copyHelper(that);
141}
142
144 unsigned SmallSize,
145 const void **RHSSmallStorage,
146 SmallPtrSetImplBase &&that) {
147 moveHelper(SmallStorage, SmallSize, RHSSmallStorage, std::move(that));
148}
149
150void SmallPtrSetImplBase::copyFrom(const void **SmallStorage,
151 const SmallPtrSetImplBase &RHS) {
152 assert(&RHS != this && "Self-copy should be handled by the caller.");
153
154 if (isSmall() && RHS.isSmall())
155 assert(CurArraySize == RHS.CurArraySize &&
156 "Cannot assign sets with different small sizes");
157
158 // If we're becoming small, prepare to insert into our stack space
159 if (RHS.isSmall()) {
160 if (!isSmall())
161 free(CurArray);
162 CurArray = SmallStorage;
163 IsSmall = true;
164 // Otherwise, allocate new heap space (unless we were the same size)
165 } else if (CurArraySize != RHS.CurArraySize) {
166 if (isSmall())
167 CurArray = (const void**)safe_malloc(sizeof(void*) * RHS.CurArraySize);
168 else {
169 const void **T = (const void**)safe_realloc(CurArray,
170 sizeof(void*) * RHS.CurArraySize);
171 CurArray = T;
172 }
173 IsSmall = false;
174 }
175
176 copyHelper(RHS);
177}
178
179void SmallPtrSetImplBase::copyHelper(const SmallPtrSetImplBase &RHS) {
180 // Copy over the new array size
181 CurArraySize = RHS.CurArraySize;
182
183 // Copy over the contents from the other set
184 llvm::copy(RHS.buckets(), CurArray);
185
186 NumEntries = RHS.NumEntries;
187}
188
189void SmallPtrSetImplBase::moveFrom(const void **SmallStorage,
190 unsigned SmallSize,
191 const void **RHSSmallStorage,
192 SmallPtrSetImplBase &&RHS) {
193 if (!isSmall())
194 free(CurArray);
195 moveHelper(SmallStorage, SmallSize, RHSSmallStorage, std::move(RHS));
196}
197
198void SmallPtrSetImplBase::moveHelper(const void **SmallStorage,
199 unsigned SmallSize,
200 const void **RHSSmallStorage,
201 SmallPtrSetImplBase &&RHS) {
202 assert(&RHS != this && "Self-move should be handled by the caller.");
203
204 if (RHS.isSmall()) {
205 // Copy a small RHS rather than moving.
206 CurArray = SmallStorage;
207 llvm::copy(RHS.small_buckets(), CurArray);
208 } else {
209 CurArray = RHS.CurArray;
210 RHS.CurArray = RHSSmallStorage;
211 }
212
213 // Copy the rest of the trivial members.
214 CurArraySize = RHS.CurArraySize;
215 NumEntries = RHS.NumEntries;
216 IsSmall = RHS.IsSmall;
217
218 // Make the RHS small and empty.
219 RHS.CurArraySize = SmallSize;
220 RHS.NumEntries = 0;
221 RHS.IsSmall = true;
222}
223
224void SmallPtrSetImplBase::swap(const void **SmallStorage,
225 const void **RHSSmallStorage,
226 SmallPtrSetImplBase &RHS) {
227 if (this == &RHS) return;
228
229 // We can only avoid copying elements if neither set is small.
230 if (!this->isSmall() && !RHS.isSmall()) {
231 std::swap(this->CurArray, RHS.CurArray);
232 std::swap(this->CurArraySize, RHS.CurArraySize);
233 std::swap(this->NumEntries, RHS.NumEntries);
234 return;
235 }
236
237 // FIXME: From here on we assume that both sets have the same small size.
238
239 // Both a small, just swap the small elements.
240 if (this->isSmall() && RHS.isSmall()) {
241 unsigned MinEntries = std::min(this->NumEntries, RHS.NumEntries);
242 std::swap_ranges(this->CurArray, this->CurArray + MinEntries, RHS.CurArray);
243 if (this->NumEntries > MinEntries) {
244 std::copy(this->CurArray + MinEntries, this->CurArray + this->NumEntries,
245 RHS.CurArray + MinEntries);
246 } else {
247 std::copy(RHS.CurArray + MinEntries, RHS.CurArray + RHS.NumEntries,
248 this->CurArray + MinEntries);
249 }
250 assert(this->CurArraySize == RHS.CurArraySize);
251 std::swap(this->NumEntries, RHS.NumEntries);
252 return;
253 }
254
255 // If only one side is small, copy the small elements into the large side and
256 // move the pointer from the large side to the small side.
257 SmallPtrSetImplBase &SmallSide = this->isSmall() ? *this : RHS;
258 SmallPtrSetImplBase &LargeSide = this->isSmall() ? RHS : *this;
259 const void **LargeSideInlineStorage =
260 this->isSmall() ? RHSSmallStorage : SmallStorage;
261 llvm::copy(SmallSide.small_buckets(), LargeSideInlineStorage);
262 std::swap(LargeSide.CurArraySize, SmallSide.CurArraySize);
263 std::swap(LargeSide.NumEntries, SmallSide.NumEntries);
264 SmallSide.CurArray = LargeSide.CurArray;
265 SmallSide.IsSmall = false;
266 LargeSide.CurArray = LargeSideInlineStorage;
267 LargeSide.IsSmall = true;
268}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:336
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
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:1884
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:876
An information struct used to provide DenseMap with the various necessary components for a given valu...