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