[NFC] Cherry-picking some changes to prepare for ReverseIterator support (#5329)

There are a number of changes required to get ReverseIterator support
into DXC. Rather than attempting to do it all as one large PR, it makes
more sense to break it up into smaller PRs of 2-3 changes.

These first two are just some small refactoring in the SmallPtrSet
classes and should not change any behavior.

There was one small change in the DXC codebase that was necessary to
bring forward which deviates from the original cherry-pick slightly
(will mark it in a comment).

---------

Co-authored-by: Matthias Braun <matze@braunis.de>
This commit is contained in:
Brian Favela 2023-06-27 13:51:11 -04:00 коммит произвёл GitHub
Родитель 8e0e56fe9d
Коммит 3b4ce9d338
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 40 добавлений и 51 удалений

Просмотреть файл

@ -101,7 +101,23 @@ protected:
/// insert_imp - This returns true if the pointer was new to the set, false if
/// it was already in the set. This is hidden from the client so that the
/// derived class can check that the right type of pointer is passed in.
std::pair<const void *const *, bool> insert_imp(const void *Ptr);
std::pair<const void *const *, bool> insert_imp(const void *Ptr) {
if (isSmall()) {
// Check to see if it is already in the set.
for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
APtr != E; ++APtr)
if (*APtr == Ptr)
return std::make_pair(APtr, false);
// Nope, there isn't. If we stay small, just 'pushback' now.
if (NumElements < CurArraySize) {
SmallArray[NumElements++] = Ptr;
return std::make_pair(SmallArray + (NumElements - 1), true);
}
// Otherwise, hit the big set case, which will call grow.
}
return insert_imp_big(Ptr);
}
/// erase_imp - If the set contains the specified pointer, remove it and
/// return true, otherwise return false. This is hidden from the client so
@ -126,6 +142,8 @@ protected:
private:
bool isSmall() const { return CurArray == SmallArray; }
std::pair<const void *const *, bool> insert_imp_big(const void *Ptr);
const void * const *FindBucketFor(const void *Ptr) const;
void shrink_and_clear();
@ -140,6 +158,12 @@ protected:
void CopyFrom(const SmallPtrSetImplBase &RHS);
void MoveFrom(unsigned SmallSize, SmallPtrSetImplBase &&RHS);
private:
/// Code shared by MoveFrom() and move constructor.
void MoveHelper(unsigned SmallSize, SmallPtrSetImplBase &&RHS);
/// Code shared by CopyFrom() and copy constructor.
void CopyHelper(const SmallPtrSetImplBase &RHS);
};
/// SmallPtrSetIteratorImpl - This is the common base class shared between all

Просмотреть файл

@ -35,22 +35,7 @@ void SmallPtrSetImplBase::shrink_and_clear() {
}
std::pair<const void *const *, bool>
SmallPtrSetImplBase::insert_imp(const void *Ptr) {
if (isSmall()) {
// Check to see if it is already in the set.
for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
APtr != E; ++APtr)
if (*APtr == Ptr)
return std::make_pair(APtr, false);
// Nope, there isn't. If we stay small, just 'pushback' now.
if (NumElements < CurArraySize) {
SmallArray[NumElements++] = Ptr;
return std::make_pair(SmallArray + (NumElements - 1), true);
}
// Otherwise, hit the big set case, which will call grow.
}
SmallPtrSetImplBase::insert_imp_big(const void *Ptr) {
if (LLVM_UNLIKELY(NumElements * 4 >= CurArraySize * 3)) {
// If more than 3/4 of the array is full, grow.
Grow(CurArraySize < 64 ? 128 : CurArraySize*2);
@ -178,46 +163,18 @@ SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
CurArray = new const void*[that.CurArraySize]; // HLSL Change: Use overridable operator new
assert(CurArray && "Failed to allocate memory?");
}
// Copy over the new array size
CurArraySize = that.CurArraySize;
// Copy over the contents from the other set
memcpy(CurArray, that.CurArray, sizeof(void*)*CurArraySize);
NumElements = that.NumElements;
NumTombstones = that.NumTombstones;
// Copy over the that array.
CopyHelper(that);
}
SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
unsigned SmallSize,
SmallPtrSetImplBase &&that) {
SmallArray = SmallStorage;
// Copy over the basic members.
CurArraySize = that.CurArraySize;
NumElements = that.NumElements;
NumTombstones = that.NumTombstones;
// When small, just copy into our small buffer.
if (that.isSmall()) {
CurArray = SmallArray;
memcpy(CurArray, that.CurArray, sizeof(void *) * CurArraySize);
} else {
// Otherwise, we steal the large memory allocation and no copy is needed.
CurArray = that.CurArray;
that.CurArray = that.SmallArray;
}
// Make the "that" object small and empty.
that.CurArraySize = SmallSize;
assert(that.CurArray == that.SmallArray);
that.NumElements = 0;
that.NumTombstones = 0;
MoveHelper(SmallSize, std::move(that));
}
/// CopyFrom - implement operator= from a smallptrset that has the same pointer
/// type, but may have a different small size.
void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
assert(&RHS != this && "Self-copy should be handled by the caller.");
@ -244,7 +201,11 @@ void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
}
assert(CurArray && "Failed to allocate memory?");
}
CopyHelper(RHS);
}
void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) {
// Copy over the new array size
CurArraySize = RHS.CurArraySize;
@ -257,10 +218,14 @@ void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize,
SmallPtrSetImplBase &&RHS) {
assert(&RHS != this && "Self-move should be handled by the caller.");
if (!isSmall())
delete[] CurArray; // HLSL Change: Use overridable operator delete
MoveHelper(SmallSize, std::move(RHS));
}
void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize,
SmallPtrSetImplBase &&RHS) {
assert(&RHS != this && "Self-move should be handled by the caller.");
if (RHS.isSmall()) {
// Copy a small RHS rather than moving.