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