1//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===// 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 7//===----------------------------------------------------------------------===// 9// This file implements the SmallPtrSet class. See SmallPtrSet.h for an 10// overview of the algorithm. 12//===----------------------------------------------------------------------===// 24void SmallPtrSetImplBase::shrink_and_clear() {
28// Reduce the number of buckets. 33// Install the new array. Clear all the buckets to empty. 39std::pair<const void *const *, bool>
40SmallPtrSetImplBase::insert_imp_big(
constvoid *
Ptr) {
42// If more than 3/4 of the array is full, grow. 45// If fewer of 1/8 of the array is empty (meaning that many are filled with 46// tombstones), rehash. 50// Okay, we know we have space. Find a hash bucket. 51constvoid **Bucket =
const_cast<constvoid**
>(FindBucketFor(
Ptr));
53return std::make_pair(Bucket,
false);
// Already inserted, good. 55// Otherwise, insert it! 62return std::make_pair(Bucket,
true);
65constvoid *
const *SmallPtrSetImplBase::doFind(
constvoid *
Ptr)
const{
70constvoid *
const *Bucket =
CurArray + BucketNo;
76// Otherwise, it's a hash collision or a tombstone, continue quadratic 78 BucketNo += ProbeAmt++;
83constvoid *
const *SmallPtrSetImplBase::FindBucketFor(
constvoid *
Ptr)
const{
88constvoid *
const *Tombstone =
nullptr;
90// If we found an empty bucket, the pointer doesn't exist in the set. 91// Return a tombstone if we've seen one so far, or the empty bucket if 94return Tombstone ? Tombstone : Array+Bucket;
100// If this is a tombstone, remember it. If Ptr ends up not in the set, we 101// prefer to return it than something that would require more probing. 103 Tombstone = Array+Bucket;
// Remember the first tombstone found. 105// It's a hash collision or a tombstone. Reprobe. 106 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
110/// Grow - Allocate a larger backing store for the buckets and move it over. 112void SmallPtrSetImplBase::Grow(
unsigned NewSize) {
117// Install the new array. Clear all the buckets to empty. 118constvoid **NewBuckets = (
constvoid**)
safe_malloc(
sizeof(
void*) * NewSize);
120// Reset member only if memory was allocated successfully 123 memset(
CurArray, -1, NewSize*
sizeof(
void*));
125// Copy over all valid entries. 126for (
constvoid **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
127// Copy over the element if it is valid. 128constvoid *Elt = *BucketPtr;
130 *
const_cast<void**
>(FindBucketFor(Elt)) =
const_cast<void*
>(Elt);
144// If we're becoming small, prepare to insert into our stack space 147// Otherwise, allocate new heap space (unless we were the same size) 151// Copy over the that array. 157constvoid **RHSSmallStorage,
159 moveHelper(SmallStorage, SmallSize, RHSSmallStorage, std::move(that));
164assert(&
RHS !=
this &&
"Self-copy should be handled by the caller.");
168"Cannot assign sets with different small sizes");
170// If we're becoming small, prepare to insert into our stack space 176// Otherwise, allocate new heap space (unless we were the same size) 182sizeof(
void*) *
RHS.CurArraySize);
192// Copy over the new array size 195// Copy over the contents from the other set 204constvoid **RHSSmallStorage,
208 moveHelper(SmallStorage, SmallSize, RHSSmallStorage, std::move(
RHS));
211void SmallPtrSetImplBase::moveHelper(
constvoid **SmallStorage,
213constvoid **RHSSmallStorage,
215assert(&RHS !=
this &&
"Self-move should be handled by the caller.");
218// Copy a small RHS rather than moving. 223RHS.CurArray = RHSSmallStorage;
226// Copy the rest of the trivial members. 232// Make the RHS small and empty. 233RHS.CurArraySize = SmallSize;
235RHS.NumTombstones = 0;
240constvoid **RHSSmallStorage,
242if (
this == &
RHS)
return;
244// We can only avoid copying elements if neither set is small. 253// FIXME: From here on we assume that both sets have the same small size. 255// If only RHS is small, copy the small elements into LHS and move the pointer 258 std::copy(
RHS.CurArray,
RHS.CurArray +
RHS.NumNonEmpty, SmallStorage);
269// If only LHS is small, copy the small elements into RHS and move the pointer 284// Both a small, just swap the small elements. 286unsigned MinNonEmpty = std::min(this->NumNonEmpty,
RHS.NumNonEmpty);
288if (this->NumNonEmpty > MinNonEmpty) {
290RHS.CurArray + MinNonEmpty);
292 std::copy(
RHS.CurArray + MinNonEmpty,
RHS.CurArray +
RHS.NumNonEmpty,
293 this->CurArray + MinNonEmpty);
#define LLVM_UNLIKELY(EXPR)
#define LLVM_LIKELY(EXPR)
This file defines DenseMapInfo traits for DenseMap.
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet class.
SmallPtrSetImplBase - This is the common code shared among all the SmallPtrSet<>'s,...
unsigned NumTombstones
Number of tombstones in CurArray.
SmallPtrSetImplBase(const void **SmallStorage, const SmallPtrSetImplBase &that)
const void ** CurArray
The current set of buckets, in either small or big representation.
bool IsSmall
Whether the set is in small representation.
void copyFrom(const void **SmallStorage, const SmallPtrSetImplBase &RHS)
unsigned NumNonEmpty
Number of elements in CurArray that contain a value or are a tombstone.
void moveFrom(const void **SmallStorage, unsigned SmallSize, const void **RHSSmallStorage, SmallPtrSetImplBase &&RHS)
unsigned CurArraySize
CurArraySize - The allocated size of CurArray, always a power of two.
const void ** EndPointer() const
static void * getEmptyMarker()
static void * getTombstoneMarker()
void swap(const void **SmallStorage, const void **RHSSmallStorage, SmallPtrSetImplBase &RHS)
swap - Swaps the elements of two sets.
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.
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_realloc(void *Ptr, size_t Sz)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
An information struct used to provide DenseMap with the various necessary components for a given valu...