From 8f9477b067b0e059ece95bae3e2bd84f95702666 Mon Sep 17 00:00:00 2001 From: Sterling Augustine Date: Wed, 14 Apr 2021 18:03:58 -0700 Subject: [PATCH] Revert "Simplify BitVector code" This reverts commit 82f0e3d3ea6bf927e3397b2fb423abbc5821a30f. The change breaks the asan buildbots. https://lab.llvm.org/buildbot/#/builders/99/builds/2835 --- llvm/include/llvm/ADT/BitVector.h | 227 +++++++++++++++++++++++------- 1 file changed, 174 insertions(+), 53 deletions(-) diff --git a/llvm/include/llvm/ADT/BitVector.h b/llvm/include/llvm/ADT/BitVector.h index 1083510e3d69..080f9611678f 100644 --- a/llvm/include/llvm/ADT/BitVector.h +++ b/llvm/include/llvm/ADT/BitVector.h @@ -79,16 +79,14 @@ class BitVector { static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32, "Unsupported word size"); - using Storage = std::vector; - - Storage Bits; // Actual bits. - unsigned Size; // Size of bitvector in bits. + MutableArrayRef Bits; // Actual bits. + unsigned Size; // Size of bitvector in bits. public: typedef unsigned size_type; - // Encapsulation of a single bit. class reference { + friend class BitVector; BitWord *WordRef; unsigned BitPos; @@ -138,12 +136,33 @@ public: /// BitVector ctor - Creates a bitvector of specified number of bits. All /// bits are initialized to the specified value. - explicit BitVector(unsigned s, bool t = false) - : Bits(NumBitWords(s), 0 - (BitWord)t), Size(s) { + explicit BitVector(unsigned s, bool t = false) : Size(s) { + size_t Capacity = NumBitWords(s); + Bits = allocate(Capacity); + init_words(Bits, t); if (t) clear_unused_bits(); } + /// BitVector copy ctor. + BitVector(const BitVector &RHS) : Size(RHS.size()) { + if (Size == 0) { + Bits = MutableArrayRef(); + 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(); + RHS.Size = 0; + } + + ~BitVector() { std::free(Bits.data()); } + /// empty - Tests whether there are no bits in this bitvector. bool empty() const { return Size == 0; } @@ -153,14 +172,17 @@ public: /// count - Returns the number of bits which are set. size_type count() const { unsigned NumBits = 0; - for (auto Bit : Bits) - NumBits += countPopulation(Bit); + for (unsigned i = 0; i < NumBitWords(size()); ++i) + NumBits += countPopulation(Bits[i]); return NumBits; } /// any - Returns true if any bit is set. bool any() const { - return any_of(Bits, [](BitWord Bit) { return Bit != 0; }); + for (unsigned i = 0; i < NumBitWords(size()); ++i) + if (Bits[i] != 0) + return true; + return false; } /// all - Returns true if all bits are set. @@ -326,27 +348,43 @@ public: /// clear - Removes all bits from the bitvector. Does not change capacity. void clear() { Size = 0; - Bits.clear(); } /// resize - Grow or shrink the bitvector. void resize(unsigned N, bool t = false) { - set_unused_bits(t); + if (N > getBitCapacity()) { + 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; - Bits.resize(NumBitWords(N), 0 - BitWord(t)); - clear_unused_bits(); + if (t || N < OldSize) + clear_unused_bits(); } - void reserve(unsigned N) { Bits.reserve(NumBitWords(N)); } + void reserve(unsigned N) { + if (N > getBitCapacity()) + grow(N); + } // Set, reset, flip BitVector &set() { - init_words(true); + init_words(Bits, true); clear_unused_bits(); return *this; } BitVector &set(unsigned Idx) { + assert(Bits.data() && "Bits never allocated"); Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE); return *this; } @@ -381,7 +419,7 @@ public: } BitVector &reset() { - init_words(false); + init_words(Bits, false); return *this; } @@ -420,8 +458,8 @@ public: } BitVector &flip() { - for (auto &Bit : Bits) - Bit = ~Bit; + for (unsigned i = 0; i < NumBitWords(size()); ++i) + Bits[i] = ~Bits[i]; clear_unused_bits(); return *this; } @@ -466,8 +504,8 @@ public: /// Test if any common bits are set. bool anyCommon(const BitVector &RHS) const { - unsigned ThisWords = Bits.size(); - unsigned RHSWords = RHS.Bits.size(); + unsigned ThisWords = NumBitWords(size()); + unsigned RHSWords = NumBitWords(RHS.size()); for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i) if (Bits[i] & RHS.Bits[i]) return true; @@ -478,16 +516,18 @@ public: bool operator==(const BitVector &RHS) const { if (size() != RHS.size()) return false; - unsigned NumWords = Bits.size(); - return std::equal(Bits.begin(), Bits.begin() + NumWords, RHS.Bits.begin()); + unsigned NumWords = NumBitWords(size()); + return Bits.take_front(NumWords) == RHS.Bits.take_front(NumWords); } - bool operator!=(const BitVector &RHS) const { return !(*this == RHS); } + bool operator!=(const BitVector &RHS) const { + return !(*this == RHS); + } /// Intersection, union, disjoint union. BitVector &operator&=(const BitVector &RHS) { - unsigned ThisWords = Bits.size(); - unsigned RHSWords = RHS.Bits.size(); + unsigned ThisWords = NumBitWords(size()); + unsigned RHSWords = NumBitWords(RHS.size()); unsigned i; for (i = 0; i != std::min(ThisWords, RHSWords); ++i) Bits[i] &= RHS.Bits[i]; @@ -503,9 +543,10 @@ public: /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS. BitVector &reset(const BitVector &RHS) { - unsigned ThisWords = Bits.size(); - unsigned RHSWords = RHS.Bits.size(); - for (unsigned i = 0; i != std::min(ThisWords, RHSWords); ++i) + unsigned ThisWords = NumBitWords(size()); + unsigned RHSWords = NumBitWords(RHS.size()); + unsigned i; + for (i = 0; i != std::min(ThisWords, RHSWords); ++i) Bits[i] &= ~RHS.Bits[i]; return *this; } @@ -513,8 +554,8 @@ public: /// test - Check if (This - RHS) is zero. /// This is the same as reset(RHS) and any(). bool test(const BitVector &RHS) const { - unsigned ThisWords = Bits.size(); - unsigned RHSWords = RHS.Bits.size(); + unsigned ThisWords = NumBitWords(size()); + unsigned RHSWords = NumBitWords(RHS.size()); unsigned i; for (i = 0; i != std::min(ThisWords, RHSWords); ++i) if ((Bits[i] & ~RHS.Bits[i]) != 0) @@ -535,7 +576,7 @@ public: [&Arg](auto const &BV) { return Arg.size() == BV; }) && "consistent sizes"); Out.resize(Arg.size()); - for (size_t i = 0, e = Arg.Bits.size(); i != e; ++i) + for (size_t i = 0, e = Out.NumBitWords(Arg.size()); i != e; ++i) Out.Bits[i] = f(Arg.Bits[i], Args.Bits[i]...); Out.clear_unused_bits(); return Out; @@ -544,7 +585,7 @@ public: BitVector &operator|=(const BitVector &RHS) { if (size() < RHS.size()) resize(RHS.size()); - for (size_t i = 0, e = RHS.Bits.size(); i != e; ++i) + for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i) Bits[i] |= RHS.Bits[i]; return *this; } @@ -552,7 +593,7 @@ public: BitVector &operator^=(const BitVector &RHS) { if (size() < RHS.size()) resize(RHS.size()); - for (size_t i = 0, e = RHS.Bits.size(); i != e; ++i) + for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i) Bits[i] ^= RHS.Bits[i]; return *this; } @@ -562,7 +603,7 @@ public: if (LLVM_UNLIKELY(empty() || N == 0)) return *this; - unsigned NumWords = Bits.size(); + unsigned NumWords = NumBitWords(Size); assert(NumWords >= 1); wordShr(N / BITWORD_SIZE); @@ -611,7 +652,7 @@ public: if (LLVM_UNLIKELY(empty() || N == 0)) return *this; - unsigned NumWords = Bits.size(); + unsigned NumWords = NumBitWords(Size); assert(NumWords >= 1); wordShl(N / BITWORD_SIZE); @@ -656,6 +697,53 @@ public: 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(); + RHS.Size = 0; + + return *this; + } + void swap(BitVector &RHS) { std::swap(Bits, RHS.Bits); std::swap(Size, RHS.Size); @@ -667,7 +755,9 @@ public: } bool isInvalid() const { return Size == (unsigned)-1; } - ArrayRef getData() const { return {&Bits[0], Bits.size()}; } + ArrayRef getData() const { + return Bits.take_front(NumBitWords(size())); + } //===--------------------------------------------------------------------===// // Portable bit mask operations. @@ -717,21 +807,23 @@ private: /// Example: /// Words = [0xBBBBAAAA, 0xDDDDFFFF, 0x00000000, 0xDDDD0000] /// 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 turn this into 0x00000000 0x00000000 0xBBBBAAAA 0xDDDDFFFF by using a + /// bits. So if we want to shift the BitVector left by 2 words, we need to + /// turn this into 0x00000000 0x00000000 0xBBBBAAAA 0xDDDDFFFF by using a /// memmove which moves right, not left. void wordShl(uint32_t Count) { if (Count == 0) return; - uint32_t NumWords = Bits.size(); + uint32_t NumWords = NumBitWords(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 // aligned to a word-boundary, we don't need to worry about endianness // here. - std::copy(Bits.begin(), Bits.begin() + NumWords - Count, - Bits.begin() + Count); - std::fill(Bits.begin(), Bits.begin() + Count, 0); + std::memmove(Dest.begin(), Src.begin(), Dest.size() * sizeof(BitWord)); + std::memset(Bits.data(), 0, Count * sizeof(BitWord)); clear_unused_bits(); } @@ -742,10 +834,20 @@ private: if (Count == 0) return; - uint32_t NumWords = Bits.size(); + uint32_t NumWords = NumBitWords(Size); - std::copy(Bits.begin() + Count, Bits.begin() + NumWords, Bits.begin()); - std::fill(Bits.begin() + NumWords - Count, Bits.begin() + NumWords, 0); + auto Src = Bits.take_front(NumWords).drop_front(Count); + auto Dest = Bits.take_front(NumWords).drop_back(Count); + assert(Dest.size() == Src.size()); + + std::memmove(Dest.begin(), Src.begin(), Dest.size() * sizeof(BitWord)); + std::memset(Dest.end(), 0, Count * sizeof(BitWord)); + } + + MutableArrayRef allocate(size_t NumWords) { + BitWord *RawBits = static_cast( + safe_malloc(NumWords * sizeof(BitWord))); + return MutableArrayRef(RawBits, NumWords); } int next_unset_in_word(int WordIndex, BitWord Word) const { @@ -759,13 +861,19 @@ private: // Set the unused bits in the high words. 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. - if (unsigned ExtraBits = Size % BITWORD_SIZE) { + unsigned ExtraBits = Size % BITWORD_SIZE; + if (ExtraBits) { BitWord ExtraBitMask = ~BitWord(0) << ExtraBits; if (t) - Bits.back() |= ExtraBitMask; + Bits[UsedWords-1] |= ExtraBitMask; else - Bits.back() &= ~ExtraBitMask; + Bits[UsedWords-1] &= ~ExtraBitMask; } } @@ -774,8 +882,18 @@ private: set_unused_bits(false); } - void init_words(bool t) { - std::fill(Bits.begin(), Bits.end(), 0 - (BitWord)t); + void grow(unsigned NewSize) { + size_t NewCapacity = std::max(NumBitWords(NewSize), Bits.size() * 2); + assert(NewCapacity > 0 && "realloc-ing zero space"); + BitWord *NewBits = static_cast( + safe_realloc(Bits.data(), NewCapacity * sizeof(BitWord))); + Bits = MutableArrayRef(NewBits, NewCapacity); + clear_unused_bits(); + } + + void init_words(MutableArrayRef B, bool t) { + if (B.size() > 0) + memset(B.data(), 0 - (int)t, B.size() * sizeof(BitWord)); } template @@ -816,7 +934,7 @@ inline size_t capacity_in_bytes(const BitVector &X) { } template <> struct DenseMapInfo { - static inline BitVector getEmptyKey() { return {}; } + static inline BitVector getEmptyKey() { return BitVector(); } static inline BitVector getTombstoneKey() { BitVector V; V.invalid(); @@ -836,7 +954,10 @@ template <> struct DenseMapInfo { namespace std { /// Implement std::swap in terms of BitVector swap. -inline void swap(llvm::BitVector &LHS, llvm::BitVector &RHS) { LHS.swap(RHS); } + inline void + swap(llvm::BitVector &LHS, llvm::BitVector &RHS) { + LHS.swap(RHS); + } } // end namespace std #endif // LLVM_ADT_BITVECTOR_H