Simplify BitVector code

Instead of managing memory by hand, delegate it to std::vector. This makes the
code much simpler, and also avoids repeatedly computing the storage size.

According to valgrind --tool=callgrind, this also slightly decreases the
instruction count, but by a small margin.

Differential Revision: https://reviews.llvm.org/D100387
This commit is contained in:
serge-sans-paille 2021-04-13 16:14:32 +02:00
parent a33b647100
commit 82f0e3d3ea
1 changed files with 53 additions and 174 deletions

View File

@ -79,14 +79,16 @@ class BitVector {
static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32, static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
"Unsupported word size"); "Unsupported word size");
MutableArrayRef<BitWord> Bits; // Actual bits. using Storage = std::vector<BitWord>;
unsigned Size; // Size of bitvector in bits.
Storage Bits; // Actual bits.
unsigned Size; // Size of bitvector in bits.
public: public:
typedef unsigned size_type; typedef unsigned size_type;
// Encapsulation of a single bit. // Encapsulation of a single bit.
class reference { class reference {
friend class BitVector;
BitWord *WordRef; BitWord *WordRef;
unsigned BitPos; unsigned BitPos;
@ -136,33 +138,12 @@ public:
/// BitVector ctor - Creates a bitvector of specified number of bits. All /// BitVector ctor - Creates a bitvector of specified number of bits. All
/// bits are initialized to the specified value. /// bits are initialized to the specified value.
explicit BitVector(unsigned s, bool t = false) : Size(s) { explicit BitVector(unsigned s, bool t = false)
size_t Capacity = NumBitWords(s); : Bits(NumBitWords(s), 0 - (BitWord)t), Size(s) {
Bits = allocate(Capacity);
init_words(Bits, t);
if (t) if (t)
clear_unused_bits(); clear_unused_bits();
} }
/// BitVector copy ctor.
BitVector(const BitVector &RHS) : Size(RHS.size()) {
if (Size == 0) {
Bits = MutableArrayRef<BitWord>();
return;
}
size_t Capacity = NumBitWords(RHS.size());
Bits = allocate(Capacity);
std::memcpy(Bits.data(), RHS.Bits.data(), Capacity * sizeof(BitWord));
}
BitVector(BitVector &&RHS) : Bits(RHS.Bits), Size(RHS.Size) {
RHS.Bits = MutableArrayRef<BitWord>();
RHS.Size = 0;
}
~BitVector() { std::free(Bits.data()); }
/// empty - Tests whether there are no bits in this bitvector. /// empty - Tests whether there are no bits in this bitvector.
bool empty() const { return Size == 0; } bool empty() const { return Size == 0; }
@ -172,17 +153,14 @@ public:
/// count - Returns the number of bits which are set. /// count - Returns the number of bits which are set.
size_type count() const { size_type count() const {
unsigned NumBits = 0; unsigned NumBits = 0;
for (unsigned i = 0; i < NumBitWords(size()); ++i) for (auto Bit : Bits)
NumBits += countPopulation(Bits[i]); NumBits += countPopulation(Bit);
return NumBits; return NumBits;
} }
/// any - Returns true if any bit is set. /// any - Returns true if any bit is set.
bool any() const { bool any() const {
for (unsigned i = 0; i < NumBitWords(size()); ++i) return any_of(Bits, [](BitWord Bit) { return Bit != 0; });
if (Bits[i] != 0)
return true;
return false;
} }
/// all - Returns true if all bits are set. /// all - Returns true if all bits are set.
@ -348,43 +326,27 @@ public:
/// clear - Removes all bits from the bitvector. Does not change capacity. /// clear - Removes all bits from the bitvector. Does not change capacity.
void clear() { void clear() {
Size = 0; Size = 0;
Bits.clear();
} }
/// resize - Grow or shrink the bitvector. /// resize - Grow or shrink the bitvector.
void resize(unsigned N, bool t = false) { void resize(unsigned N, bool t = false) {
if (N > getBitCapacity()) { set_unused_bits(t);
unsigned OldCapacity = Bits.size();
grow(N);
init_words(Bits.drop_front(OldCapacity), t);
}
// Set any old unused bits that are now included in the BitVector. This
// may set bits that are not included in the new vector, but we will clear
// them back out below.
if (N > Size)
set_unused_bits(t);
// Update the size, and clear out any bits that are now unused
unsigned OldSize = Size;
Size = N; Size = N;
if (t || N < OldSize) Bits.resize(NumBitWords(N), 0 - BitWord(t));
clear_unused_bits(); clear_unused_bits();
} }
void reserve(unsigned N) { void reserve(unsigned N) { Bits.reserve(NumBitWords(N)); }
if (N > getBitCapacity())
grow(N);
}
// Set, reset, flip // Set, reset, flip
BitVector &set() { BitVector &set() {
init_words(Bits, true); init_words(true);
clear_unused_bits(); clear_unused_bits();
return *this; return *this;
} }
BitVector &set(unsigned Idx) { BitVector &set(unsigned Idx) {
assert(Bits.data() && "Bits never allocated");
Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE); Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
return *this; return *this;
} }
@ -419,7 +381,7 @@ public:
} }
BitVector &reset() { BitVector &reset() {
init_words(Bits, false); init_words(false);
return *this; return *this;
} }
@ -458,8 +420,8 @@ public:
} }
BitVector &flip() { BitVector &flip() {
for (unsigned i = 0; i < NumBitWords(size()); ++i) for (auto &Bit : Bits)
Bits[i] = ~Bits[i]; Bit = ~Bit;
clear_unused_bits(); clear_unused_bits();
return *this; return *this;
} }
@ -504,8 +466,8 @@ public:
/// Test if any common bits are set. /// Test if any common bits are set.
bool anyCommon(const BitVector &RHS) const { bool anyCommon(const BitVector &RHS) const {
unsigned ThisWords = NumBitWords(size()); unsigned ThisWords = Bits.size();
unsigned RHSWords = NumBitWords(RHS.size()); unsigned RHSWords = RHS.Bits.size();
for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i) for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
if (Bits[i] & RHS.Bits[i]) if (Bits[i] & RHS.Bits[i])
return true; return true;
@ -516,18 +478,16 @@ public:
bool operator==(const BitVector &RHS) const { bool operator==(const BitVector &RHS) const {
if (size() != RHS.size()) if (size() != RHS.size())
return false; return false;
unsigned NumWords = NumBitWords(size()); unsigned NumWords = Bits.size();
return Bits.take_front(NumWords) == RHS.Bits.take_front(NumWords); return std::equal(Bits.begin(), Bits.begin() + NumWords, RHS.Bits.begin());
} }
bool operator!=(const BitVector &RHS) const { bool operator!=(const BitVector &RHS) const { return !(*this == RHS); }
return !(*this == RHS);
}
/// Intersection, union, disjoint union. /// Intersection, union, disjoint union.
BitVector &operator&=(const BitVector &RHS) { BitVector &operator&=(const BitVector &RHS) {
unsigned ThisWords = NumBitWords(size()); unsigned ThisWords = Bits.size();
unsigned RHSWords = NumBitWords(RHS.size()); unsigned RHSWords = RHS.Bits.size();
unsigned i; unsigned i;
for (i = 0; i != std::min(ThisWords, RHSWords); ++i) for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
Bits[i] &= RHS.Bits[i]; Bits[i] &= RHS.Bits[i];
@ -543,10 +503,9 @@ public:
/// reset - Reset bits that are set in RHS. Same as *this &= ~RHS. /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
BitVector &reset(const BitVector &RHS) { BitVector &reset(const BitVector &RHS) {
unsigned ThisWords = NumBitWords(size()); unsigned ThisWords = Bits.size();
unsigned RHSWords = NumBitWords(RHS.size()); unsigned RHSWords = RHS.Bits.size();
unsigned i; for (unsigned i = 0; i != std::min(ThisWords, RHSWords); ++i)
for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
Bits[i] &= ~RHS.Bits[i]; Bits[i] &= ~RHS.Bits[i];
return *this; return *this;
} }
@ -554,8 +513,8 @@ public:
/// test - Check if (This - RHS) is zero. /// test - Check if (This - RHS) is zero.
/// This is the same as reset(RHS) and any(). /// This is the same as reset(RHS) and any().
bool test(const BitVector &RHS) const { bool test(const BitVector &RHS) const {
unsigned ThisWords = NumBitWords(size()); unsigned ThisWords = Bits.size();
unsigned RHSWords = NumBitWords(RHS.size()); unsigned RHSWords = RHS.Bits.size();
unsigned i; unsigned i;
for (i = 0; i != std::min(ThisWords, RHSWords); ++i) for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
if ((Bits[i] & ~RHS.Bits[i]) != 0) if ((Bits[i] & ~RHS.Bits[i]) != 0)
@ -576,7 +535,7 @@ public:
[&Arg](auto const &BV) { return Arg.size() == BV; }) && [&Arg](auto const &BV) { return Arg.size() == BV; }) &&
"consistent sizes"); "consistent sizes");
Out.resize(Arg.size()); Out.resize(Arg.size());
for (size_t i = 0, e = Out.NumBitWords(Arg.size()); i != e; ++i) for (size_t i = 0, e = Arg.Bits.size(); i != e; ++i)
Out.Bits[i] = f(Arg.Bits[i], Args.Bits[i]...); Out.Bits[i] = f(Arg.Bits[i], Args.Bits[i]...);
Out.clear_unused_bits(); Out.clear_unused_bits();
return Out; return Out;
@ -585,7 +544,7 @@ public:
BitVector &operator|=(const BitVector &RHS) { BitVector &operator|=(const BitVector &RHS) {
if (size() < RHS.size()) if (size() < RHS.size())
resize(RHS.size()); resize(RHS.size());
for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i) for (size_t i = 0, e = RHS.Bits.size(); i != e; ++i)
Bits[i] |= RHS.Bits[i]; Bits[i] |= RHS.Bits[i];
return *this; return *this;
} }
@ -593,7 +552,7 @@ public:
BitVector &operator^=(const BitVector &RHS) { BitVector &operator^=(const BitVector &RHS) {
if (size() < RHS.size()) if (size() < RHS.size())
resize(RHS.size()); resize(RHS.size());
for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i) for (size_t i = 0, e = RHS.Bits.size(); i != e; ++i)
Bits[i] ^= RHS.Bits[i]; Bits[i] ^= RHS.Bits[i];
return *this; return *this;
} }
@ -603,7 +562,7 @@ public:
if (LLVM_UNLIKELY(empty() || N == 0)) if (LLVM_UNLIKELY(empty() || N == 0))
return *this; return *this;
unsigned NumWords = NumBitWords(Size); unsigned NumWords = Bits.size();
assert(NumWords >= 1); assert(NumWords >= 1);
wordShr(N / BITWORD_SIZE); wordShr(N / BITWORD_SIZE);
@ -652,7 +611,7 @@ public:
if (LLVM_UNLIKELY(empty() || N == 0)) if (LLVM_UNLIKELY(empty() || N == 0))
return *this; return *this;
unsigned NumWords = NumBitWords(Size); unsigned NumWords = Bits.size();
assert(NumWords >= 1); assert(NumWords >= 1);
wordShl(N / BITWORD_SIZE); wordShl(N / BITWORD_SIZE);
@ -697,53 +656,6 @@ public:
return *this; return *this;
} }
// Assignment operator.
const BitVector &operator=(const BitVector &RHS) {
if (this == &RHS) return *this;
Size = RHS.size();
// Handle tombstone when the BitVector is a key of a DenseHash.
if (RHS.isInvalid()) {
std::free(Bits.data());
Bits = None;
return *this;
}
unsigned RHSWords = NumBitWords(Size);
if (Size <= getBitCapacity()) {
if (Size)
std::memcpy(Bits.data(), RHS.Bits.data(), RHSWords * sizeof(BitWord));
clear_unused_bits();
return *this;
}
// Grow the bitvector to have enough elements.
unsigned NewCapacity = RHSWords;
assert(NewCapacity > 0 && "negative capacity?");
auto NewBits = allocate(NewCapacity);
std::memcpy(NewBits.data(), RHS.Bits.data(), NewCapacity * sizeof(BitWord));
// Destroy the old bits.
std::free(Bits.data());
Bits = NewBits;
return *this;
}
const BitVector &operator=(BitVector &&RHS) {
if (this == &RHS) return *this;
std::free(Bits.data());
Bits = RHS.Bits;
Size = RHS.Size;
RHS.Bits = MutableArrayRef<BitWord>();
RHS.Size = 0;
return *this;
}
void swap(BitVector &RHS) { void swap(BitVector &RHS) {
std::swap(Bits, RHS.Bits); std::swap(Bits, RHS.Bits);
std::swap(Size, RHS.Size); std::swap(Size, RHS.Size);
@ -755,9 +667,7 @@ public:
} }
bool isInvalid() const { return Size == (unsigned)-1; } bool isInvalid() const { return Size == (unsigned)-1; }
ArrayRef<BitWord> getData() const { ArrayRef<BitWord> getData() const { return {&Bits[0], Bits.size()}; }
return Bits.take_front(NumBitWords(size()));
}
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
// Portable bit mask operations. // Portable bit mask operations.
@ -807,23 +717,21 @@ private:
/// Example: /// Example:
/// Words = [0xBBBBAAAA, 0xDDDDFFFF, 0x00000000, 0xDDDD0000] /// Words = [0xBBBBAAAA, 0xDDDDFFFF, 0x00000000, 0xDDDD0000]
/// represents a BitVector where 0xBBBBAAAA contain the least significant /// represents a BitVector where 0xBBBBAAAA contain the least significant
/// bits. So if we want to shift the BitVector left by 2 words, we need to /// bits. So if we want to shift the BitVector left by 2 words, we need
/// turn this into 0x00000000 0x00000000 0xBBBBAAAA 0xDDDDFFFF by using a /// to turn this into 0x00000000 0x00000000 0xBBBBAAAA 0xDDDDFFFF by using a
/// memmove which moves right, not left. /// memmove which moves right, not left.
void wordShl(uint32_t Count) { void wordShl(uint32_t Count) {
if (Count == 0) if (Count == 0)
return; return;
uint32_t NumWords = NumBitWords(Size); uint32_t NumWords = Bits.size();
auto Src = Bits.take_front(NumWords).drop_back(Count);
auto Dest = Bits.take_front(NumWords).drop_front(Count);
// Since we always move Word-sized chunks of data with src and dest both // Since we always move Word-sized chunks of data with src and dest both
// aligned to a word-boundary, we don't need to worry about endianness // aligned to a word-boundary, we don't need to worry about endianness
// here. // here.
std::memmove(Dest.begin(), Src.begin(), Dest.size() * sizeof(BitWord)); std::copy(Bits.begin(), Bits.begin() + NumWords - Count,
std::memset(Bits.data(), 0, Count * sizeof(BitWord)); Bits.begin() + Count);
std::fill(Bits.begin(), Bits.begin() + Count, 0);
clear_unused_bits(); clear_unused_bits();
} }
@ -834,20 +742,10 @@ private:
if (Count == 0) if (Count == 0)
return; return;
uint32_t NumWords = NumBitWords(Size); uint32_t NumWords = Bits.size();
auto Src = Bits.take_front(NumWords).drop_front(Count); std::copy(Bits.begin() + Count, Bits.begin() + NumWords, Bits.begin());
auto Dest = Bits.take_front(NumWords).drop_back(Count); std::fill(Bits.begin() + NumWords - Count, Bits.begin() + NumWords, 0);
assert(Dest.size() == Src.size());
std::memmove(Dest.begin(), Src.begin(), Dest.size() * sizeof(BitWord));
std::memset(Dest.end(), 0, Count * sizeof(BitWord));
}
MutableArrayRef<BitWord> allocate(size_t NumWords) {
BitWord *RawBits = static_cast<BitWord *>(
safe_malloc(NumWords * sizeof(BitWord)));
return MutableArrayRef<BitWord>(RawBits, NumWords);
} }
int next_unset_in_word(int WordIndex, BitWord Word) const { int next_unset_in_word(int WordIndex, BitWord Word) const {
@ -861,19 +759,13 @@ private:
// Set the unused bits in the high words. // Set the unused bits in the high words.
void set_unused_bits(bool t = true) { void set_unused_bits(bool t = true) {
// Set high words first.
unsigned UsedWords = NumBitWords(Size);
if (Bits.size() > UsedWords)
init_words(Bits.drop_front(UsedWords), t);
// Then set any stray high bits of the last used word. // Then set any stray high bits of the last used word.
unsigned ExtraBits = Size % BITWORD_SIZE; if (unsigned ExtraBits = Size % BITWORD_SIZE) {
if (ExtraBits) {
BitWord ExtraBitMask = ~BitWord(0) << ExtraBits; BitWord ExtraBitMask = ~BitWord(0) << ExtraBits;
if (t) if (t)
Bits[UsedWords-1] |= ExtraBitMask; Bits.back() |= ExtraBitMask;
else else
Bits[UsedWords-1] &= ~ExtraBitMask; Bits.back() &= ~ExtraBitMask;
} }
} }
@ -882,18 +774,8 @@ private:
set_unused_bits(false); set_unused_bits(false);
} }
void grow(unsigned NewSize) { void init_words(bool t) {
size_t NewCapacity = std::max<size_t>(NumBitWords(NewSize), Bits.size() * 2); std::fill(Bits.begin(), Bits.end(), 0 - (BitWord)t);
assert(NewCapacity > 0 && "realloc-ing zero space");
BitWord *NewBits = static_cast<BitWord *>(
safe_realloc(Bits.data(), NewCapacity * sizeof(BitWord)));
Bits = MutableArrayRef<BitWord>(NewBits, NewCapacity);
clear_unused_bits();
}
void init_words(MutableArrayRef<BitWord> B, bool t) {
if (B.size() > 0)
memset(B.data(), 0 - (int)t, B.size() * sizeof(BitWord));
} }
template<bool AddBits, bool InvertMask> template<bool AddBits, bool InvertMask>
@ -934,7 +816,7 @@ inline size_t capacity_in_bytes(const BitVector &X) {
} }
template <> struct DenseMapInfo<BitVector> { template <> struct DenseMapInfo<BitVector> {
static inline BitVector getEmptyKey() { return BitVector(); } static inline BitVector getEmptyKey() { return {}; }
static inline BitVector getTombstoneKey() { static inline BitVector getTombstoneKey() {
BitVector V; BitVector V;
V.invalid(); V.invalid();
@ -954,10 +836,7 @@ template <> struct DenseMapInfo<BitVector> {
namespace std { namespace std {
/// Implement std::swap in terms of BitVector swap. /// Implement std::swap in terms of BitVector swap.
inline void inline void swap(llvm::BitVector &LHS, llvm::BitVector &RHS) { LHS.swap(RHS); }
swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {
LHS.swap(RHS);
}
} // end namespace std } // end namespace std
#endif // LLVM_ADT_BITVECTOR_H #endif // LLVM_ADT_BITVECTOR_H