1 //===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the BitVector class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_ADT_BITVECTOR_H
14 #define LLVM_ADT_BITVECTOR_H
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/Support/MathExtras.h"
30 /// ForwardIterator for the bits that are set.
31 /// Iterators get invalidated when resize / reserve is called.
32 template <typename BitVectorT> class const_set_bits_iterator_impl {
33 const BitVectorT &Parent;
37 assert(Current != -1 && "Trying to advance past end.");
38 Current = Parent.find_next(Current);
42 const_set_bits_iterator_impl(const BitVectorT &Parent, int Current)
43 : Parent(Parent), Current(Current) {}
44 explicit const_set_bits_iterator_impl(const BitVectorT &Parent)
45 : const_set_bits_iterator_impl(Parent, Parent.find_first()) {}
46 const_set_bits_iterator_impl(const const_set_bits_iterator_impl &) = default;
48 const_set_bits_iterator_impl operator++(int) {
54 const_set_bits_iterator_impl &operator++() {
59 unsigned operator*() const { return Current; }
61 bool operator==(const const_set_bits_iterator_impl &Other) const {
62 assert(&Parent == &Other.Parent &&
63 "Comparing iterators from different BitVectors");
64 return Current == Other.Current;
67 bool operator!=(const const_set_bits_iterator_impl &Other) const {
68 assert(&Parent == &Other.Parent &&
69 "Comparing iterators from different BitVectors");
70 return Current != Other.Current;
75 typedef uintptr_t BitWord;
77 enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
79 static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
80 "Unsupported word size");
82 MutableArrayRef<BitWord> Bits; // Actual bits.
83 unsigned Size; // Size of bitvector in bits.
86 typedef unsigned size_type;
87 // Encapsulation of a single bit.
89 friend class BitVector;
95 reference(BitVector &b, unsigned Idx) {
96 WordRef = &b.Bits[Idx / BITWORD_SIZE];
97 BitPos = Idx % BITWORD_SIZE;
100 reference() = delete;
101 reference(const reference&) = default;
103 reference &operator=(reference t) {
108 reference& operator=(bool t) {
110 *WordRef |= BitWord(1) << BitPos;
112 *WordRef &= ~(BitWord(1) << BitPos);
116 operator bool() const {
117 return ((*WordRef) & (BitWord(1) << BitPos)) != 0;
121 typedef const_set_bits_iterator_impl<BitVector> const_set_bits_iterator;
122 typedef const_set_bits_iterator set_iterator;
124 const_set_bits_iterator set_bits_begin() const {
125 return const_set_bits_iterator(*this);
127 const_set_bits_iterator set_bits_end() const {
128 return const_set_bits_iterator(*this, -1);
130 iterator_range<const_set_bits_iterator> set_bits() const {
131 return make_range(set_bits_begin(), set_bits_end());
134 /// BitVector default ctor - Creates an empty bitvector.
135 BitVector() : Size(0) {}
137 /// BitVector ctor - Creates a bitvector of specified number of bits. All
138 /// bits are initialized to the specified value.
139 explicit BitVector(unsigned s, bool t = false) : Size(s) {
140 size_t Capacity = NumBitWords(s);
141 Bits = allocate(Capacity);
147 /// BitVector copy ctor.
148 BitVector(const BitVector &RHS) : Size(RHS.size()) {
150 Bits = MutableArrayRef<BitWord>();
154 size_t Capacity = NumBitWords(RHS.size());
155 Bits = allocate(Capacity);
156 std::memcpy(Bits.data(), RHS.Bits.data(), Capacity * sizeof(BitWord));
159 BitVector(BitVector &&RHS) : Bits(RHS.Bits), Size(RHS.Size) {
160 RHS.Bits = MutableArrayRef<BitWord>();
164 ~BitVector() { std::free(Bits.data()); }
166 /// empty - Tests whether there are no bits in this bitvector.
167 bool empty() const { return Size == 0; }
169 /// size - Returns the number of bits in this bitvector.
170 size_type size() const { return Size; }
172 /// count - Returns the number of bits which are set.
173 size_type count() const {
174 unsigned NumBits = 0;
175 for (unsigned i = 0; i < NumBitWords(size()); ++i)
176 NumBits += countPopulation(Bits[i]);
180 /// any - Returns true if any bit is set.
182 for (unsigned i = 0; i < NumBitWords(size()); ++i)
188 /// all - Returns true if all bits are set.
190 for (unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
191 if (Bits[i] != ~BitWord(0))
194 // If bits remain check that they are ones. The unused bits are always zero.
195 if (unsigned Remainder = Size % BITWORD_SIZE)
196 return Bits[Size / BITWORD_SIZE] == (BitWord(1) << Remainder) - 1;
201 /// none - Returns true if none of the bits are set.
206 /// find_first_in - Returns the index of the first set bit in the range
207 /// [Begin, End). Returns -1 if all bits in the range are unset.
208 int find_first_in(unsigned Begin, unsigned End) const {
209 assert(Begin <= End && End <= Size);
213 unsigned FirstWord = Begin / BITWORD_SIZE;
214 unsigned LastWord = (End - 1) / BITWORD_SIZE;
216 // Check subsequent words.
217 for (unsigned i = FirstWord; i <= LastWord; ++i) {
218 BitWord Copy = Bits[i];
220 if (i == FirstWord) {
221 unsigned FirstBit = Begin % BITWORD_SIZE;
222 Copy &= maskTrailingZeros<BitWord>(FirstBit);
226 unsigned LastBit = (End - 1) % BITWORD_SIZE;
227 Copy &= maskTrailingOnes<BitWord>(LastBit + 1);
230 return i * BITWORD_SIZE + countTrailingZeros(Copy);
235 /// find_last_in - Returns the index of the last set bit in the range
236 /// [Begin, End). Returns -1 if all bits in the range are unset.
237 int find_last_in(unsigned Begin, unsigned End) const {
238 assert(Begin <= End && End <= Size);
242 unsigned LastWord = (End - 1) / BITWORD_SIZE;
243 unsigned FirstWord = Begin / BITWORD_SIZE;
245 for (unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
246 unsigned CurrentWord = i - 1;
248 BitWord Copy = Bits[CurrentWord];
249 if (CurrentWord == LastWord) {
250 unsigned LastBit = (End - 1) % BITWORD_SIZE;
251 Copy &= maskTrailingOnes<BitWord>(LastBit + 1);
254 if (CurrentWord == FirstWord) {
255 unsigned FirstBit = Begin % BITWORD_SIZE;
256 Copy &= maskTrailingZeros<BitWord>(FirstBit);
260 return (CurrentWord + 1) * BITWORD_SIZE - countLeadingZeros(Copy) - 1;
266 /// find_first_unset_in - Returns the index of the first unset bit in the
267 /// range [Begin, End). Returns -1 if all bits in the range are set.
268 int find_first_unset_in(unsigned Begin, unsigned End) const {
269 assert(Begin <= End && End <= Size);
273 unsigned FirstWord = Begin / BITWORD_SIZE;
274 unsigned LastWord = (End - 1) / BITWORD_SIZE;
276 // Check subsequent words.
277 for (unsigned i = FirstWord; i <= LastWord; ++i) {
278 BitWord Copy = Bits[i];
280 if (i == FirstWord) {
281 unsigned FirstBit = Begin % BITWORD_SIZE;
282 Copy |= maskTrailingOnes<BitWord>(FirstBit);
286 unsigned LastBit = (End - 1) % BITWORD_SIZE;
287 Copy |= maskTrailingZeros<BitWord>(LastBit + 1);
289 if (Copy != ~BitWord(0)) {
290 unsigned Result = i * BITWORD_SIZE + countTrailingOnes(Copy);
291 return Result < size() ? Result : -1;
297 /// find_last_unset_in - Returns the index of the last unset bit in the
298 /// range [Begin, End). Returns -1 if all bits in the range are set.
299 int find_last_unset_in(unsigned Begin, unsigned End) const {
300 assert(Begin <= End && End <= Size);
304 unsigned LastWord = (End - 1) / BITWORD_SIZE;
305 unsigned FirstWord = Begin / BITWORD_SIZE;
307 for (unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
308 unsigned CurrentWord = i - 1;
310 BitWord Copy = Bits[CurrentWord];
311 if (CurrentWord == LastWord) {
312 unsigned LastBit = (End - 1) % BITWORD_SIZE;
313 Copy |= maskTrailingZeros<BitWord>(LastBit + 1);
316 if (CurrentWord == FirstWord) {
317 unsigned FirstBit = Begin % BITWORD_SIZE;
318 Copy |= maskTrailingOnes<BitWord>(FirstBit);
321 if (Copy != ~BitWord(0)) {
323 (CurrentWord + 1) * BITWORD_SIZE - countLeadingOnes(Copy) - 1;
324 return Result < Size ? Result : -1;
330 /// find_first - Returns the index of the first set bit, -1 if none
331 /// of the bits are set.
332 int find_first() const { return find_first_in(0, Size); }
334 /// find_last - Returns the index of the last set bit, -1 if none of the bits
336 int find_last() const { return find_last_in(0, Size); }
338 /// find_next - Returns the index of the next set bit following the
339 /// "Prev" bit. Returns -1 if the next set bit is not found.
340 int find_next(unsigned Prev) const { return find_first_in(Prev + 1, Size); }
342 /// find_prev - Returns the index of the first set bit that precedes the
343 /// the bit at \p PriorTo. Returns -1 if all previous bits are unset.
344 int find_prev(unsigned PriorTo) const { return find_last_in(0, PriorTo); }
346 /// find_first_unset - Returns the index of the first unset bit, -1 if all
347 /// of the bits are set.
348 int find_first_unset() const { return find_first_unset_in(0, Size); }
350 /// find_next_unset - Returns the index of the next unset bit following the
351 /// "Prev" bit. Returns -1 if all remaining bits are set.
352 int find_next_unset(unsigned Prev) const {
353 return find_first_unset_in(Prev + 1, Size);
356 /// find_last_unset - Returns the index of the last unset bit, -1 if all of
357 /// the bits are set.
358 int find_last_unset() const { return find_last_unset_in(0, Size); }
360 /// find_prev_unset - Returns the index of the first unset bit that precedes
361 /// the bit at \p PriorTo. Returns -1 if all previous bits are set.
362 int find_prev_unset(unsigned PriorTo) {
363 return find_last_unset_in(0, PriorTo);
366 /// clear - Removes all bits from the bitvector. Does not change capacity.
371 /// resize - Grow or shrink the bitvector.
372 void resize(unsigned N, bool t = false) {
373 if (N > getBitCapacity()) {
374 unsigned OldCapacity = Bits.size();
376 init_words(Bits.drop_front(OldCapacity), t);
379 // Set any old unused bits that are now included in the BitVector. This
380 // may set bits that are not included in the new vector, but we will clear
381 // them back out below.
385 // Update the size, and clear out any bits that are now unused
386 unsigned OldSize = Size;
388 if (t || N < OldSize)
392 void reserve(unsigned N) {
393 if (N > getBitCapacity())
399 init_words(Bits, true);
404 BitVector &set(unsigned Idx) {
405 assert(Bits.data() && "Bits never allocated");
406 Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
410 /// set - Efficiently set a range of bits in [I, E)
411 BitVector &set(unsigned I, unsigned E) {
412 assert(I <= E && "Attempted to set backwards range!");
413 assert(E <= size() && "Attempted to set out-of-bounds range!");
415 if (I == E) return *this;
417 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
418 BitWord EMask = BitWord(1) << (E % BITWORD_SIZE);
419 BitWord IMask = BitWord(1) << (I % BITWORD_SIZE);
420 BitWord Mask = EMask - IMask;
421 Bits[I / BITWORD_SIZE] |= Mask;
425 BitWord PrefixMask = ~BitWord(0) << (I % BITWORD_SIZE);
426 Bits[I / BITWORD_SIZE] |= PrefixMask;
427 I = alignTo(I, BITWORD_SIZE);
429 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
430 Bits[I / BITWORD_SIZE] = ~BitWord(0);
432 BitWord PostfixMask = (BitWord(1) << (E % BITWORD_SIZE)) - 1;
434 Bits[I / BITWORD_SIZE] |= PostfixMask;
440 init_words(Bits, false);
444 BitVector &reset(unsigned Idx) {
445 Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
449 /// reset - Efficiently reset a range of bits in [I, E)
450 BitVector &reset(unsigned I, unsigned E) {
451 assert(I <= E && "Attempted to reset backwards range!");
452 assert(E <= size() && "Attempted to reset out-of-bounds range!");
454 if (I == E) return *this;
456 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
457 BitWord EMask = BitWord(1) << (E % BITWORD_SIZE);
458 BitWord IMask = BitWord(1) << (I % BITWORD_SIZE);
459 BitWord Mask = EMask - IMask;
460 Bits[I / BITWORD_SIZE] &= ~Mask;
464 BitWord PrefixMask = ~BitWord(0) << (I % BITWORD_SIZE);
465 Bits[I / BITWORD_SIZE] &= ~PrefixMask;
466 I = alignTo(I, BITWORD_SIZE);
468 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
469 Bits[I / BITWORD_SIZE] = BitWord(0);
471 BitWord PostfixMask = (BitWord(1) << (E % BITWORD_SIZE)) - 1;
473 Bits[I / BITWORD_SIZE] &= ~PostfixMask;
479 for (unsigned i = 0; i < NumBitWords(size()); ++i)
485 BitVector &flip(unsigned Idx) {
486 Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
491 reference operator[](unsigned Idx) {
492 assert (Idx < Size && "Out-of-bounds Bit access.");
493 return reference(*this, Idx);
496 bool operator[](unsigned Idx) const {
497 assert (Idx < Size && "Out-of-bounds Bit access.");
498 BitWord Mask = BitWord(1) << (Idx % BITWORD_SIZE);
499 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
502 bool test(unsigned Idx) const {
506 // Push single bit to end of vector.
507 void push_back(bool Val) {
508 unsigned OldSize = Size;
509 unsigned NewSize = Size + 1;
511 // Resize, which will insert zeros.
512 // If we already fit then the unused bits will be already zero.
513 if (NewSize > getBitCapacity())
514 resize(NewSize, false);
518 // If true, set single bit.
523 /// Test if any common bits are set.
524 bool anyCommon(const BitVector &RHS) const {
525 unsigned ThisWords = NumBitWords(size());
526 unsigned RHSWords = NumBitWords(RHS.size());
527 for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
528 if (Bits[i] & RHS.Bits[i])
533 // Comparison operators.
534 bool operator==(const BitVector &RHS) const {
535 if (size() != RHS.size())
537 unsigned NumWords = NumBitWords(size());
538 return Bits.take_front(NumWords) == RHS.Bits.take_front(NumWords);
541 bool operator!=(const BitVector &RHS) const {
542 return !(*this == RHS);
545 /// Intersection, union, disjoint union.
546 BitVector &operator&=(const BitVector &RHS) {
547 unsigned ThisWords = NumBitWords(size());
548 unsigned RHSWords = NumBitWords(RHS.size());
550 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
551 Bits[i] &= RHS.Bits[i];
553 // Any bits that are just in this bitvector become zero, because they aren't
554 // in the RHS bit vector. Any words only in RHS are ignored because they
555 // are already zero in the LHS.
556 for (; i != ThisWords; ++i)
562 /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
563 BitVector &reset(const BitVector &RHS) {
564 unsigned ThisWords = NumBitWords(size());
565 unsigned RHSWords = NumBitWords(RHS.size());
567 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
568 Bits[i] &= ~RHS.Bits[i];
572 /// test - Check if (This - RHS) is zero.
573 /// This is the same as reset(RHS) and any().
574 bool test(const BitVector &RHS) const {
575 unsigned ThisWords = NumBitWords(size());
576 unsigned RHSWords = NumBitWords(RHS.size());
578 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
579 if ((Bits[i] & ~RHS.Bits[i]) != 0)
582 for (; i != ThisWords ; ++i)
589 BitVector &operator|=(const BitVector &RHS) {
590 if (size() < RHS.size())
592 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
593 Bits[i] |= RHS.Bits[i];
597 BitVector &operator^=(const BitVector &RHS) {
598 if (size() < RHS.size())
600 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
601 Bits[i] ^= RHS.Bits[i];
605 BitVector &operator>>=(unsigned N) {
607 if (LLVM_UNLIKELY(empty() || N == 0))
610 unsigned NumWords = NumBitWords(Size);
611 assert(NumWords >= 1);
613 wordShr(N / BITWORD_SIZE);
615 unsigned BitDistance = N % BITWORD_SIZE;
616 if (BitDistance == 0)
619 // When the shift size is not a multiple of the word size, then we have
620 // a tricky situation where each word in succession needs to extract some
621 // of the bits from the next word and or them into this word while
622 // shifting this word to make room for the new bits. This has to be done
623 // for every word in the array.
625 // Since we're shifting each word right, some bits will fall off the end
626 // of each word to the right, and empty space will be created on the left.
627 // The final word in the array will lose bits permanently, so starting at
628 // the beginning, work forwards shifting each word to the right, and
629 // OR'ing in the bits from the end of the next word to the beginning of
633 // Starting with {0xAABBCCDD, 0xEEFF0011, 0x22334455} and shifting right
635 // Step 1: Word[0] >>= 4 ; 0x0ABBCCDD
636 // Step 2: Word[0] |= 0x10000000 ; 0x1ABBCCDD
637 // Step 3: Word[1] >>= 4 ; 0x0EEFF001
638 // Step 4: Word[1] |= 0x50000000 ; 0x5EEFF001
639 // Step 5: Word[2] >>= 4 ; 0x02334455
640 // Result: { 0x1ABBCCDD, 0x5EEFF001, 0x02334455 }
641 const BitWord Mask = maskTrailingOnes<BitWord>(BitDistance);
642 const unsigned LSH = BITWORD_SIZE - BitDistance;
644 for (unsigned I = 0; I < NumWords - 1; ++I) {
645 Bits[I] >>= BitDistance;
646 Bits[I] |= (Bits[I + 1] & Mask) << LSH;
649 Bits[NumWords - 1] >>= BitDistance;
654 BitVector &operator<<=(unsigned N) {
656 if (LLVM_UNLIKELY(empty() || N == 0))
659 unsigned NumWords = NumBitWords(Size);
660 assert(NumWords >= 1);
662 wordShl(N / BITWORD_SIZE);
664 unsigned BitDistance = N % BITWORD_SIZE;
665 if (BitDistance == 0)
668 // When the shift size is not a multiple of the word size, then we have
669 // a tricky situation where each word in succession needs to extract some
670 // of the bits from the previous word and or them into this word while
671 // shifting this word to make room for the new bits. This has to be done
672 // for every word in the array. This is similar to the algorithm outlined
673 // in operator>>=, but backwards.
675 // Since we're shifting each word left, some bits will fall off the end
676 // of each word to the left, and empty space will be created on the right.
677 // The first word in the array will lose bits permanently, so starting at
678 // the end, work backwards shifting each word to the left, and OR'ing
679 // in the bits from the end of the next word to the beginning of the
683 // Starting with {0xAABBCCDD, 0xEEFF0011, 0x22334455} and shifting left
685 // Step 1: Word[2] <<= 4 ; 0x23344550
686 // Step 2: Word[2] |= 0x0000000E ; 0x2334455E
687 // Step 3: Word[1] <<= 4 ; 0xEFF00110
688 // Step 4: Word[1] |= 0x0000000A ; 0xEFF0011A
689 // Step 5: Word[0] <<= 4 ; 0xABBCCDD0
690 // Result: { 0xABBCCDD0, 0xEFF0011A, 0x2334455E }
691 const BitWord Mask = maskLeadingOnes<BitWord>(BitDistance);
692 const unsigned RSH = BITWORD_SIZE - BitDistance;
694 for (int I = NumWords - 1; I > 0; --I) {
695 Bits[I] <<= BitDistance;
696 Bits[I] |= (Bits[I - 1] & Mask) >> RSH;
698 Bits[0] <<= BitDistance;
704 // Assignment operator.
705 const BitVector &operator=(const BitVector &RHS) {
706 if (this == &RHS) return *this;
710 // Handle tombstone when the BitVector is a key of a DenseHash.
711 if (RHS.isInvalid()) {
712 std::free(Bits.data());
717 unsigned RHSWords = NumBitWords(Size);
718 if (Size <= getBitCapacity()) {
720 std::memcpy(Bits.data(), RHS.Bits.data(), RHSWords * sizeof(BitWord));
725 // Grow the bitvector to have enough elements.
726 unsigned NewCapacity = RHSWords;
727 assert(NewCapacity > 0 && "negative capacity?");
728 auto NewBits = allocate(NewCapacity);
729 std::memcpy(NewBits.data(), RHS.Bits.data(), NewCapacity * sizeof(BitWord));
731 // Destroy the old bits.
732 std::free(Bits.data());
738 const BitVector &operator=(BitVector &&RHS) {
739 if (this == &RHS) return *this;
741 std::free(Bits.data());
745 RHS.Bits = MutableArrayRef<BitWord>();
751 void swap(BitVector &RHS) {
752 std::swap(Bits, RHS.Bits);
753 std::swap(Size, RHS.Size);
757 assert(!Size && Bits.empty());
760 bool isInvalid() const { return Size == (unsigned)-1; }
762 ArrayRef<BitWord> getData() const {
763 return Bits.take_front(NumBitWords(size()));
766 //===--------------------------------------------------------------------===//
767 // Portable bit mask operations.
768 //===--------------------------------------------------------------------===//
770 // These methods all operate on arrays of uint32_t, each holding 32 bits. The
771 // fixed word size makes it easier to work with literal bit vector constants
774 // The LSB in each word is the lowest numbered bit. The size of a portable
775 // bit mask is always a whole multiple of 32 bits. If no bit mask size is
776 // given, the bit mask is assumed to cover the entire BitVector.
778 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
779 /// This computes "*this |= Mask".
780 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
781 applyMask<true, false>(Mask, MaskWords);
784 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
785 /// Don't resize. This computes "*this &= ~Mask".
786 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
787 applyMask<false, false>(Mask, MaskWords);
790 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
791 /// Don't resize. This computes "*this |= ~Mask".
792 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
793 applyMask<true, true>(Mask, MaskWords);
796 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
797 /// Don't resize. This computes "*this &= Mask".
798 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
799 applyMask<false, true>(Mask, MaskWords);
803 /// Perform a logical left shift of \p Count words by moving everything
804 /// \p Count words to the right in memory.
806 /// While confusing, words are stored from least significant at Bits[0] to
807 /// most significant at Bits[NumWords-1]. A logical shift left, however,
808 /// moves the current least significant bit to a higher logical index, and
809 /// fills the previous least significant bits with 0. Thus, we actually
810 /// need to move the bytes of the memory to the right, not to the left.
812 /// Words = [0xBBBBAAAA, 0xDDDDFFFF, 0x00000000, 0xDDDD0000]
813 /// represents a BitVector where 0xBBBBAAAA contain the least significant
814 /// bits. So if we want to shift the BitVector left by 2 words, we need to
815 /// turn this into 0x00000000 0x00000000 0xBBBBAAAA 0xDDDDFFFF by using a
816 /// memmove which moves right, not left.
817 void wordShl(uint32_t Count) {
821 uint32_t NumWords = NumBitWords(Size);
823 auto Src = Bits.take_front(NumWords).drop_back(Count);
824 auto Dest = Bits.take_front(NumWords).drop_front(Count);
826 // Since we always move Word-sized chunks of data with src and dest both
827 // aligned to a word-boundary, we don't need to worry about endianness
829 std::memmove(Dest.begin(), Src.begin(), Dest.size() * sizeof(BitWord));
830 std::memset(Bits.data(), 0, Count * sizeof(BitWord));
834 /// Perform a logical right shift of \p Count words by moving those
835 /// words to the left in memory. See wordShl for more information.
837 void wordShr(uint32_t Count) {
841 uint32_t NumWords = NumBitWords(Size);
843 auto Src = Bits.take_front(NumWords).drop_front(Count);
844 auto Dest = Bits.take_front(NumWords).drop_back(Count);
845 assert(Dest.size() == Src.size());
847 std::memmove(Dest.begin(), Src.begin(), Dest.size() * sizeof(BitWord));
848 std::memset(Dest.end(), 0, Count * sizeof(BitWord));
851 MutableArrayRef<BitWord> allocate(size_t NumWords) {
852 BitWord *RawBits = static_cast<BitWord *>(
853 safe_malloc(NumWords * sizeof(BitWord)));
854 return MutableArrayRef<BitWord>(RawBits, NumWords);
857 int next_unset_in_word(int WordIndex, BitWord Word) const {
858 unsigned Result = WordIndex * BITWORD_SIZE + countTrailingOnes(Word);
859 return Result < size() ? Result : -1;
862 unsigned NumBitWords(unsigned S) const {
863 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
866 // Set the unused bits in the high words.
867 void set_unused_bits(bool t = true) {
868 // Set high words first.
869 unsigned UsedWords = NumBitWords(Size);
870 if (Bits.size() > UsedWords)
871 init_words(Bits.drop_front(UsedWords), t);
873 // Then set any stray high bits of the last used word.
874 unsigned ExtraBits = Size % BITWORD_SIZE;
876 BitWord ExtraBitMask = ~BitWord(0) << ExtraBits;
878 Bits[UsedWords-1] |= ExtraBitMask;
880 Bits[UsedWords-1] &= ~ExtraBitMask;
884 // Clear the unused bits in the high words.
885 void clear_unused_bits() {
886 set_unused_bits(false);
889 void grow(unsigned NewSize) {
890 size_t NewCapacity = std::max<size_t>(NumBitWords(NewSize), Bits.size() * 2);
891 assert(NewCapacity > 0 && "realloc-ing zero space");
892 BitWord *NewBits = static_cast<BitWord *>(
893 safe_realloc(Bits.data(), NewCapacity * sizeof(BitWord)));
894 Bits = MutableArrayRef<BitWord>(NewBits, NewCapacity);
898 void init_words(MutableArrayRef<BitWord> B, bool t) {
900 memset(B.data(), 0 - (int)t, B.size() * sizeof(BitWord));
903 template<bool AddBits, bool InvertMask>
904 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
905 static_assert(BITWORD_SIZE % 32 == 0, "Unsupported BitWord size.");
906 MaskWords = std::min(MaskWords, (size() + 31) / 32);
907 const unsigned Scale = BITWORD_SIZE / 32;
909 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
910 BitWord BW = Bits[i];
911 // This inner loop should unroll completely when BITWORD_SIZE > 32.
912 for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
913 uint32_t M = *Mask++;
914 if (InvertMask) M = ~M;
915 if (AddBits) BW |= BitWord(M) << b;
916 else BW &= ~(BitWord(M) << b);
920 for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
921 uint32_t M = *Mask++;
922 if (InvertMask) M = ~M;
923 if (AddBits) Bits[i] |= BitWord(M) << b;
924 else Bits[i] &= ~(BitWord(M) << b);
931 /// Return the size (in bytes) of the bit vector.
932 size_t getMemorySize() const { return Bits.size() * sizeof(BitWord); }
933 size_t getBitCapacity() const { return Bits.size() * BITWORD_SIZE; }
936 inline size_t capacity_in_bytes(const BitVector &X) {
937 return X.getMemorySize();
940 template <> struct DenseMapInfo<BitVector> {
941 static inline BitVector getEmptyKey() { return BitVector(); }
942 static inline BitVector getTombstoneKey() {
947 static unsigned getHashValue(const BitVector &V) {
948 return DenseMapInfo<std::pair<unsigned, ArrayRef<uintptr_t>>>::getHashValue(
949 std::make_pair(V.size(), V.getData()));
951 static bool isEqual(const BitVector &LHS, const BitVector &RHS) {
952 if (LHS.isInvalid() || RHS.isInvalid())
953 return LHS.isInvalid() == RHS.isInvalid();
957 } // end namespace llvm
960 /// Implement std::swap in terms of BitVector swap.
962 swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {
965 } // end namespace std
967 #endif // LLVM_ADT_BITVECTOR_H