2007-01-27 15:10:46 +08:00
|
|
|
//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-01-27 15:10:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-01-27 15:18:32 +08:00
|
|
|
// This file implements the SmallPtrSet class. See SmallPtrSet.h for an
|
|
|
|
// overview of the algorithm.
|
2007-01-27 15:10:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-04-18 18:37:32 +08:00
|
|
|
#include "llvm/ADT/DenseMapInfo.h"
|
2007-07-10 00:54:03 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2017-07-20 09:30:39 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-03-07 04:40:02 +08:00
|
|
|
#include <algorithm>
|
2016-08-24 01:14:32 +08:00
|
|
|
#include <cassert>
|
2007-07-17 10:16:12 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2007-01-27 15:10:46 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
void SmallPtrSetImplBase::shrink_and_clear() {
|
2007-08-05 15:32:14 +08:00
|
|
|
assert(!isSmall() && "Can't shrink a small set!");
|
|
|
|
free(CurArray);
|
|
|
|
|
|
|
|
// Reduce the number of buckets.
|
2016-02-16 05:38:42 +08:00
|
|
|
unsigned Size = size();
|
|
|
|
CurArraySize = Size > 16 ? 1 << (Log2_32_Ceil(Size) + 1) : 32;
|
|
|
|
NumNonEmpty = NumTombstones = 0;
|
2007-08-05 15:32:14 +08:00
|
|
|
|
|
|
|
// Install the new array. Clear all the buckets to empty.
|
2013-03-30 06:07:12 +08:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
|
2017-07-20 09:30:39 +08:00
|
|
|
if (CurArray == nullptr)
|
|
|
|
report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
|
|
|
|
|
2007-08-05 15:32:14 +08:00
|
|
|
memset(CurArray, -1, CurArraySize*sizeof(void*));
|
|
|
|
}
|
|
|
|
|
2014-11-19 15:49:26 +08:00
|
|
|
std::pair<const void *const *, bool>
|
2016-01-27 12:20:24 +08:00
|
|
|
SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
|
2016-02-16 05:38:42 +08:00
|
|
|
if (LLVM_UNLIKELY(size() * 4 >= CurArraySize * 3)) {
|
2011-03-31 02:32:48 +08:00
|
|
|
// If more than 3/4 of the array is full, grow.
|
2016-02-16 05:38:42 +08:00
|
|
|
Grow(CurArraySize < 64 ? 128 : CurArraySize * 2);
|
|
|
|
} else if (LLVM_UNLIKELY(CurArraySize - NumNonEmpty < CurArraySize / 8)) {
|
2011-03-31 02:32:48 +08:00
|
|
|
// If fewer of 1/8 of the array is empty (meaning that many are filled with
|
|
|
|
// tombstones), rehash.
|
|
|
|
Grow(CurArraySize);
|
|
|
|
}
|
2016-01-28 12:49:07 +08:00
|
|
|
|
2007-01-27 15:10:46 +08:00
|
|
|
// Okay, we know we have space. Find a hash bucket.
|
2008-08-05 22:45:15 +08:00
|
|
|
const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
|
2014-11-19 15:49:26 +08:00
|
|
|
if (*Bucket == Ptr)
|
|
|
|
return std::make_pair(Bucket, false); // Already inserted, good.
|
|
|
|
|
2007-01-27 15:10:46 +08:00
|
|
|
// Otherwise, insert it!
|
2007-02-07 09:11:25 +08:00
|
|
|
if (*Bucket == getTombstoneMarker())
|
|
|
|
--NumTombstones;
|
2016-02-16 05:38:42 +08:00
|
|
|
else
|
|
|
|
++NumNonEmpty; // Track density.
|
2008-08-05 22:45:15 +08:00
|
|
|
*Bucket = Ptr;
|
2014-11-19 15:49:26 +08:00
|
|
|
return std::make_pair(Bucket, true);
|
2007-01-27 15:10:46 +08:00
|
|
|
}
|
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
|
2012-04-18 18:37:32 +08:00
|
|
|
unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1);
|
2007-01-27 15:10:46 +08:00
|
|
|
unsigned ArraySize = CurArraySize;
|
|
|
|
unsigned ProbeAmt = 1;
|
2007-07-28 02:07:02 +08:00
|
|
|
const void *const *Array = CurArray;
|
2014-04-07 12:17:22 +08:00
|
|
|
const void *const *Tombstone = nullptr;
|
2016-08-24 01:14:32 +08:00
|
|
|
while (true) {
|
2007-01-27 15:10:46 +08:00
|
|
|
// If we found an empty bucket, the pointer doesn't exist in the set.
|
|
|
|
// Return a tombstone if we've seen one so far, or the empty bucket if
|
|
|
|
// not.
|
2015-02-24 00:41:36 +08:00
|
|
|
if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker()))
|
2007-01-27 15:10:46 +08:00
|
|
|
return Tombstone ? Tombstone : Array+Bucket;
|
2015-02-24 00:41:36 +08:00
|
|
|
|
|
|
|
// Found Ptr's bucket?
|
|
|
|
if (LLVM_LIKELY(Array[Bucket] == Ptr))
|
|
|
|
return Array+Bucket;
|
|
|
|
|
2007-01-27 15:10:46 +08:00
|
|
|
// If this is a tombstone, remember it. If Ptr ends up not in the set, we
|
|
|
|
// prefer to return it than something that would require more probing.
|
|
|
|
if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
|
|
|
|
Tombstone = Array+Bucket; // Remember the first tombstone found.
|
2016-01-28 12:49:07 +08:00
|
|
|
|
2007-01-27 15:10:46 +08:00
|
|
|
// It's a hash collision or a tombstone. Reprobe.
|
|
|
|
Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Grow - Allocate a larger backing store for the buckets and move it over.
|
|
|
|
///
|
2014-02-03 19:24:18 +08:00
|
|
|
void SmallPtrSetImplBase::Grow(unsigned NewSize) {
|
2007-07-28 02:07:02 +08:00
|
|
|
const void **OldBuckets = CurArray;
|
2016-02-16 05:38:42 +08:00
|
|
|
const void **OldEnd = EndPointer();
|
2007-01-27 15:10:46 +08:00
|
|
|
bool WasSmall = isSmall();
|
2016-01-28 12:49:07 +08:00
|
|
|
|
2007-01-27 15:10:46 +08:00
|
|
|
// Install the new array. Clear all the buckets to empty.
|
2017-07-20 09:30:39 +08:00
|
|
|
const void **NewBuckets = (const void**) malloc(sizeof(void*) * NewSize);
|
|
|
|
if (NewBuckets == nullptr)
|
|
|
|
report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
|
|
|
|
|
|
|
|
// Reset member only if memory was allocated successfully
|
|
|
|
CurArray = NewBuckets;
|
2007-01-27 15:10:46 +08:00
|
|
|
CurArraySize = NewSize;
|
|
|
|
memset(CurArray, -1, NewSize*sizeof(void*));
|
2016-01-28 12:49:07 +08:00
|
|
|
|
2016-02-16 05:38:42 +08:00
|
|
|
// Copy over all valid entries.
|
|
|
|
for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
|
|
|
|
// Copy over the element if it is valid.
|
|
|
|
const void *Elt = *BucketPtr;
|
|
|
|
if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
|
2007-07-28 02:07:02 +08:00
|
|
|
*const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
|
2016-02-16 05:38:42 +08:00
|
|
|
}
|
2016-01-28 12:49:07 +08:00
|
|
|
|
2016-02-16 05:38:42 +08:00
|
|
|
if (!WasSmall)
|
2007-07-17 05:27:44 +08:00
|
|
|
free(OldBuckets);
|
2016-02-16 05:38:42 +08:00
|
|
|
NumNonEmpty -= NumTombstones;
|
|
|
|
NumTombstones = 0;
|
2007-04-15 05:50:21 +08:00
|
|
|
}
|
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
|
2016-01-28 12:49:07 +08:00
|
|
|
const SmallPtrSetImplBase &that) {
|
2010-06-30 23:02:37 +08:00
|
|
|
SmallArray = SmallStorage;
|
|
|
|
|
2007-07-25 05:31:23 +08:00
|
|
|
// If we're becoming small, prepare to insert into our stack space
|
2007-04-15 05:50:21 +08:00
|
|
|
if (that.isSmall()) {
|
2010-06-30 23:02:37 +08:00
|
|
|
CurArray = SmallArray;
|
2007-07-25 05:31:23 +08:00
|
|
|
// Otherwise, allocate new heap space (unless we were the same size)
|
2007-04-15 05:50:21 +08:00
|
|
|
} else {
|
2013-03-30 06:07:12 +08:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
|
2017-07-20 09:30:39 +08:00
|
|
|
if (CurArray == nullptr)
|
|
|
|
report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
|
2007-01-27 15:10:46 +08:00
|
|
|
}
|
2016-01-28 12:49:07 +08:00
|
|
|
|
2016-01-28 12:49:11 +08:00
|
|
|
// Copy over the that array.
|
|
|
|
CopyHelper(that);
|
2007-01-27 15:10:46 +08:00
|
|
|
}
|
2007-07-10 00:54:03 +08:00
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
|
|
|
|
unsigned SmallSize,
|
|
|
|
SmallPtrSetImplBase &&that) {
|
2013-11-20 19:14:33 +08:00
|
|
|
SmallArray = SmallStorage;
|
2016-01-28 12:49:11 +08:00
|
|
|
MoveHelper(SmallSize, std::move(that));
|
2013-11-20 19:14:33 +08:00
|
|
|
}
|
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
|
2013-11-26 08:54:44 +08:00
|
|
|
assert(&RHS != this && "Self-copy should be handled by the caller.");
|
2013-11-26 08:44:36 +08:00
|
|
|
|
2007-07-10 06:27:20 +08:00
|
|
|
if (isSmall() && RHS.isSmall())
|
|
|
|
assert(CurArraySize == RHS.CurArraySize &&
|
|
|
|
"Cannot assign sets with different small sizes");
|
2013-03-30 06:07:12 +08:00
|
|
|
|
2007-07-10 06:27:20 +08:00
|
|
|
// If we're becoming small, prepare to insert into our stack space
|
2007-07-19 14:45:33 +08:00
|
|
|
if (RHS.isSmall()) {
|
|
|
|
if (!isSmall())
|
|
|
|
free(CurArray);
|
2010-06-30 23:02:37 +08:00
|
|
|
CurArray = SmallArray;
|
2007-07-10 06:27:20 +08:00
|
|
|
// Otherwise, allocate new heap space (unless we were the same size)
|
2007-07-19 14:45:33 +08:00
|
|
|
} else if (CurArraySize != RHS.CurArraySize) {
|
2007-07-19 03:54:15 +08:00
|
|
|
if (isSmall())
|
2013-03-30 06:07:12 +08:00
|
|
|
CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
|
2013-11-19 01:33:32 +08:00
|
|
|
else {
|
|
|
|
const void **T = (const void**)realloc(CurArray,
|
|
|
|
sizeof(void*) * RHS.CurArraySize);
|
|
|
|
if (!T)
|
|
|
|
free(CurArray);
|
|
|
|
CurArray = T;
|
|
|
|
}
|
2017-07-20 09:30:39 +08:00
|
|
|
if (CurArray == nullptr)
|
|
|
|
report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
|
2007-07-17 05:27:44 +08:00
|
|
|
}
|
2016-01-28 12:49:07 +08:00
|
|
|
|
2016-01-28 12:49:11 +08:00
|
|
|
CopyHelper(RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) {
|
2007-07-10 06:27:20 +08:00
|
|
|
// Copy over the new array size
|
|
|
|
CurArraySize = RHS.CurArraySize;
|
|
|
|
|
|
|
|
// Copy over the contents from the other set
|
2016-02-16 05:38:42 +08:00
|
|
|
std::copy(RHS.CurArray, RHS.EndPointer(), CurArray);
|
2016-01-28 12:49:07 +08:00
|
|
|
|
2016-02-16 05:38:42 +08:00
|
|
|
NumNonEmpty = RHS.NumNonEmpty;
|
2007-07-19 03:54:15 +08:00
|
|
|
NumTombstones = RHS.NumTombstones;
|
2007-07-10 00:54:03 +08:00
|
|
|
}
|
2007-07-17 10:16:12 +08:00
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
|
|
|
|
SmallPtrSetImplBase &&RHS) {
|
2013-11-20 19:14:33 +08:00
|
|
|
if (!isSmall())
|
|
|
|
free(CurArray);
|
2016-01-28 12:49:11 +08:00
|
|
|
MoveHelper(SmallSize, std::move(RHS));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize,
|
|
|
|
SmallPtrSetImplBase &&RHS) {
|
|
|
|
assert(&RHS != this && "Self-move should be handled by the caller.");
|
2013-11-20 19:14:33 +08:00
|
|
|
|
|
|
|
if (RHS.isSmall()) {
|
|
|
|
// Copy a small RHS rather than moving.
|
|
|
|
CurArray = SmallArray;
|
2016-02-16 05:38:42 +08:00
|
|
|
std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, CurArray);
|
2013-11-20 19:14:33 +08:00
|
|
|
} else {
|
|
|
|
CurArray = RHS.CurArray;
|
|
|
|
RHS.CurArray = RHS.SmallArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the rest of the trivial members.
|
|
|
|
CurArraySize = RHS.CurArraySize;
|
2016-02-16 05:38:42 +08:00
|
|
|
NumNonEmpty = RHS.NumNonEmpty;
|
2013-11-20 19:14:33 +08:00
|
|
|
NumTombstones = RHS.NumTombstones;
|
2013-11-21 02:29:56 +08:00
|
|
|
|
|
|
|
// Make the RHS small and empty.
|
|
|
|
RHS.CurArraySize = SmallSize;
|
|
|
|
assert(RHS.CurArray == RHS.SmallArray);
|
2016-02-16 05:38:42 +08:00
|
|
|
RHS.NumNonEmpty = 0;
|
2013-11-21 02:29:56 +08:00
|
|
|
RHS.NumTombstones = 0;
|
2013-11-20 19:14:33 +08:00
|
|
|
}
|
|
|
|
|
2014-02-03 19:24:18 +08:00
|
|
|
void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
|
2012-03-07 04:40:02 +08:00
|
|
|
if (this == &RHS) return;
|
|
|
|
|
|
|
|
// We can only avoid copying elements if neither set is small.
|
|
|
|
if (!this->isSmall() && !RHS.isSmall()) {
|
|
|
|
std::swap(this->CurArray, RHS.CurArray);
|
|
|
|
std::swap(this->CurArraySize, RHS.CurArraySize);
|
2016-02-16 05:38:42 +08:00
|
|
|
std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
|
2012-03-07 04:40:02 +08:00
|
|
|
std::swap(this->NumTombstones, RHS.NumTombstones);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: From here on we assume that both sets have the same small size.
|
|
|
|
|
|
|
|
// If only RHS is small, copy the small elements into LHS and move the pointer
|
|
|
|
// from LHS to RHS.
|
|
|
|
if (!this->isSmall() && RHS.isSmall()) {
|
2016-02-16 05:38:42 +08:00
|
|
|
assert(RHS.CurArray == RHS.SmallArray);
|
|
|
|
std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, this->SmallArray);
|
|
|
|
std::swap(RHS.CurArraySize, this->CurArraySize);
|
|
|
|
std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
|
|
|
|
std::swap(this->NumTombstones, RHS.NumTombstones);
|
2012-03-07 04:40:02 +08:00
|
|
|
RHS.CurArray = this->CurArray;
|
|
|
|
this->CurArray = this->SmallArray;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If only LHS is small, copy the small elements into RHS and move the pointer
|
|
|
|
// from RHS to LHS.
|
|
|
|
if (this->isSmall() && !RHS.isSmall()) {
|
2016-02-16 05:38:42 +08:00
|
|
|
assert(this->CurArray == this->SmallArray);
|
|
|
|
std::copy(this->CurArray, this->CurArray + this->NumNonEmpty,
|
2012-03-08 06:48:42 +08:00
|
|
|
RHS.SmallArray);
|
2012-03-07 04:40:02 +08:00
|
|
|
std::swap(RHS.CurArraySize, this->CurArraySize);
|
2016-02-16 05:38:42 +08:00
|
|
|
std::swap(RHS.NumNonEmpty, this->NumNonEmpty);
|
|
|
|
std::swap(RHS.NumTombstones, this->NumTombstones);
|
2012-03-07 04:40:02 +08:00
|
|
|
this->CurArray = RHS.CurArray;
|
|
|
|
RHS.CurArray = RHS.SmallArray;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both a small, just swap the small elements.
|
|
|
|
assert(this->isSmall() && RHS.isSmall());
|
2016-02-16 05:38:42 +08:00
|
|
|
unsigned MinNonEmpty = std::min(this->NumNonEmpty, RHS.NumNonEmpty);
|
|
|
|
std::swap_ranges(this->SmallArray, this->SmallArray + MinNonEmpty,
|
2012-03-08 06:33:21 +08:00
|
|
|
RHS.SmallArray);
|
2016-02-16 05:38:42 +08:00
|
|
|
if (this->NumNonEmpty > MinNonEmpty) {
|
|
|
|
std::copy(this->SmallArray + MinNonEmpty,
|
|
|
|
this->SmallArray + this->NumNonEmpty,
|
|
|
|
RHS.SmallArray + MinNonEmpty);
|
|
|
|
} else {
|
|
|
|
std::copy(RHS.SmallArray + MinNonEmpty, RHS.SmallArray + RHS.NumNonEmpty,
|
|
|
|
this->SmallArray + MinNonEmpty);
|
|
|
|
}
|
|
|
|
assert(this->CurArraySize == RHS.CurArraySize);
|
|
|
|
std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
|
|
|
|
std::swap(this->NumTombstones, RHS.NumTombstones);
|
2012-03-07 04:40:02 +08:00
|
|
|
}
|