1 //===- llvm/ADT/SmallBitVector.h - 'Normally small' bit vectors -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SmallBitVector class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_SMALLBITVECTOR_H
15 #define LLVM_ADT_SMALLBITVECTOR_H
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/Support/MathExtras.h"
30 /// This is a 'bitvector' (really, a variable-sized bit array), optimized for
31 /// the case when the array is small. It contains one pointer-sized field, which
32 /// is directly used as a plain collection of bits when possible, or as a
33 /// pointer to a larger heap-allocated array when necessary. This allows normal
34 /// "small" cases to be fast without losing generality for large inputs.
35 class SmallBitVector {
36 // TODO: In "large" mode, a pointer to a BitVector is used, leading to an
37 // unnecessary level of indirection. It would be more efficient to use a
38 // pointer to memory containing size, allocation size, and the array of bits.
42 // The number of bits in this class.
43 NumBaseBits = sizeof(uintptr_t) * CHAR_BIT,
45 // One bit is used to discriminate between small and large mode. The
46 // remaining bits are used for the small-mode representation.
47 SmallNumRawBits = NumBaseBits - 1,
49 // A few more bits are used to store the size of the bit set in small mode.
50 // Theoretically this is a ceil-log2. These bits are encoded in the most
51 // significant bits of the raw bits.
52 SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
53 NumBaseBits == 64 ? 6 :
56 // The remaining bits are used to store the actual set in small mode.
57 SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
60 static_assert(NumBaseBits == 64 || NumBaseBits == 32,
61 "Unsupported word size");
64 using size_type = unsigned;
66 // Encapsulation of a single bit.
68 SmallBitVector &TheVector;
72 reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
74 reference(const reference&) = default;
76 reference& operator=(reference t) {
81 reference& operator=(bool t) {
83 TheVector.set(BitPos);
85 TheVector.reset(BitPos);
89 operator bool() const {
90 return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
95 BitVector *getPointer() const {
97 return reinterpret_cast<BitVector *>(X);
100 void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
102 setSmallSize(NewSize);
103 setSmallBits(NewSmallBits);
106 void switchToLarge(BitVector *BV) {
107 X = reinterpret_cast<uintptr_t>(BV);
108 assert(!isSmall() && "Tried to use an unaligned pointer");
111 // Return all the bits used for the "small" representation; this includes
112 // bits for the size as well as the element bits.
113 uintptr_t getSmallRawBits() const {
118 void setSmallRawBits(uintptr_t NewRawBits) {
120 X = (NewRawBits << 1) | uintptr_t(1);
124 size_t getSmallSize() const { return getSmallRawBits() >> SmallNumDataBits; }
126 void setSmallSize(size_t Size) {
127 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
130 // Return the element bits.
131 uintptr_t getSmallBits() const {
132 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
135 void setSmallBits(uintptr_t NewBits) {
136 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
137 (getSmallSize() << SmallNumDataBits));
141 /// Creates an empty bitvector.
142 SmallBitVector() = default;
144 /// Creates a bitvector of specified number of bits. All bits are initialized
145 /// to the specified value.
146 explicit SmallBitVector(unsigned s, bool t = false) {
147 if (s <= SmallNumDataBits)
148 switchToSmall(t ? ~uintptr_t(0) : 0, s);
150 switchToLarge(new BitVector(s, t));
153 /// SmallBitVector copy ctor.
154 SmallBitVector(const SmallBitVector &RHS) {
158 switchToLarge(new BitVector(*RHS.getPointer()));
161 SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
170 using const_set_bits_iterator = const_set_bits_iterator_impl<SmallBitVector>;
171 using set_iterator = const_set_bits_iterator;
173 const_set_bits_iterator set_bits_begin() const {
174 return const_set_bits_iterator(*this);
177 const_set_bits_iterator set_bits_end() const {
178 return const_set_bits_iterator(*this, -1);
181 iterator_range<const_set_bits_iterator> set_bits() const {
182 return make_range(set_bits_begin(), set_bits_end());
185 bool isSmall() const { return X & uintptr_t(1); }
187 /// Tests whether there are no bits in this bitvector.
189 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
192 /// Returns the number of bits in this bitvector.
193 size_t size() const {
194 return isSmall() ? getSmallSize() : getPointer()->size();
197 /// Returns the number of bits which are set.
198 size_type count() const {
200 uintptr_t Bits = getSmallBits();
201 return countPopulation(Bits);
203 return getPointer()->count();
206 /// Returns true if any bit is set.
209 return getSmallBits() != 0;
210 return getPointer()->any();
213 /// Returns true if all bits are set.
216 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
217 return getPointer()->all();
220 /// Returns true if none of the bits are set.
223 return getSmallBits() == 0;
224 return getPointer()->none();
227 /// Returns the index of the first set bit, -1 if none of the bits are set.
228 int find_first() const {
230 uintptr_t Bits = getSmallBits();
233 return countTrailingZeros(Bits);
235 return getPointer()->find_first();
238 int find_last() const {
240 uintptr_t Bits = getSmallBits();
243 return NumBaseBits - countLeadingZeros(Bits) - 1;
245 return getPointer()->find_last();
248 /// Returns the index of the first unset bit, -1 if all of the bits are set.
249 int find_first_unset() const {
251 if (count() == getSmallSize())
254 uintptr_t Bits = getSmallBits();
255 return countTrailingOnes(Bits);
257 return getPointer()->find_first_unset();
260 int find_last_unset() const {
262 if (count() == getSmallSize())
265 uintptr_t Bits = getSmallBits();
267 Bits |= ~uintptr_t(0) << getSmallSize();
268 return NumBaseBits - countLeadingOnes(Bits) - 1;
270 return getPointer()->find_last_unset();
273 /// Returns the index of the next set bit following the "Prev" bit.
274 /// Returns -1 if the next set bit is not found.
275 int find_next(unsigned Prev) const {
277 uintptr_t Bits = getSmallBits();
278 // Mask off previous bits.
279 Bits &= ~uintptr_t(0) << (Prev + 1);
280 if (Bits == 0 || Prev + 1 >= getSmallSize())
282 return countTrailingZeros(Bits);
284 return getPointer()->find_next(Prev);
287 /// Returns the index of the next unset bit following the "Prev" bit.
288 /// Returns -1 if the next unset bit is not found.
289 int find_next_unset(unsigned Prev) const {
292 uintptr_t Bits = getSmallBits();
293 // Mask in previous bits.
294 uintptr_t Mask = (1 << Prev) - 1;
297 if (Bits == ~uintptr_t(0) || Prev + 1 >= getSmallSize())
299 return countTrailingOnes(Bits);
301 return getPointer()->find_next_unset(Prev);
304 /// find_prev - Returns the index of the first set bit that precedes the
305 /// the bit at \p PriorTo. Returns -1 if all previous bits are unset.
306 int find_prev(unsigned PriorTo) const {
312 uintptr_t Bits = getSmallBits();
313 Bits &= maskTrailingOnes<uintptr_t>(PriorTo + 1);
317 return NumBaseBits - countLeadingZeros(Bits) - 1;
319 return getPointer()->find_prev(PriorTo);
329 /// Grow or shrink the bitvector.
330 void resize(unsigned N, bool t = false) {
332 getPointer()->resize(N, t);
333 } else if (SmallNumDataBits >= N) {
334 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
336 setSmallBits(NewBits | getSmallBits());
338 BitVector *BV = new BitVector(N, t);
339 uintptr_t OldBits = getSmallBits();
340 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
341 (*BV)[i] = (OldBits >> i) & 1;
346 void reserve(unsigned N) {
348 if (N > SmallNumDataBits) {
349 uintptr_t OldBits = getSmallRawBits();
350 size_t SmallSize = getSmallSize();
351 BitVector *BV = new BitVector(SmallSize);
352 for (size_t i = 0; i < SmallSize; ++i)
353 if ((OldBits >> i) & 1)
359 getPointer()->reserve(N);
364 SmallBitVector &set() {
366 setSmallBits(~uintptr_t(0));
372 SmallBitVector &set(unsigned Idx) {
374 assert(Idx <= static_cast<unsigned>(
375 std::numeric_limits<uintptr_t>::digits) &&
376 "undefined behavior");
377 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
380 getPointer()->set(Idx);
384 /// Efficiently set a range of bits in [I, E)
385 SmallBitVector &set(unsigned I, unsigned E) {
386 assert(I <= E && "Attempted to set backwards range!");
387 assert(E <= size() && "Attempted to set out-of-bounds range!");
388 if (I == E) return *this;
390 uintptr_t EMask = ((uintptr_t)1) << E;
391 uintptr_t IMask = ((uintptr_t)1) << I;
392 uintptr_t Mask = EMask - IMask;
393 setSmallBits(getSmallBits() | Mask);
395 getPointer()->set(I, E);
399 SmallBitVector &reset() {
403 getPointer()->reset();
407 SmallBitVector &reset(unsigned Idx) {
409 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
411 getPointer()->reset(Idx);
415 /// Efficiently reset a range of bits in [I, E)
416 SmallBitVector &reset(unsigned I, unsigned E) {
417 assert(I <= E && "Attempted to reset backwards range!");
418 assert(E <= size() && "Attempted to reset out-of-bounds range!");
419 if (I == E) return *this;
421 uintptr_t EMask = ((uintptr_t)1) << E;
422 uintptr_t IMask = ((uintptr_t)1) << I;
423 uintptr_t Mask = EMask - IMask;
424 setSmallBits(getSmallBits() & ~Mask);
426 getPointer()->reset(I, E);
430 SmallBitVector &flip() {
432 setSmallBits(~getSmallBits());
434 getPointer()->flip();
438 SmallBitVector &flip(unsigned Idx) {
440 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
442 getPointer()->flip(Idx);
447 SmallBitVector operator~() const {
448 return SmallBitVector(*this).flip();
452 reference operator[](unsigned Idx) {
453 assert(Idx < size() && "Out-of-bounds Bit access.");
454 return reference(*this, Idx);
457 bool operator[](unsigned Idx) const {
458 assert(Idx < size() && "Out-of-bounds Bit access.");
460 return ((getSmallBits() >> Idx) & 1) != 0;
461 return getPointer()->operator[](Idx);
464 bool test(unsigned Idx) const {
468 // Push single bit to end of vector.
469 void push_back(bool Val) {
470 resize(size() + 1, Val);
473 /// Test if any common bits are set.
474 bool anyCommon(const SmallBitVector &RHS) const {
475 if (isSmall() && RHS.isSmall())
476 return (getSmallBits() & RHS.getSmallBits()) != 0;
477 if (!isSmall() && !RHS.isSmall())
478 return getPointer()->anyCommon(*RHS.getPointer());
480 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
481 if (test(i) && RHS.test(i))
486 // Comparison operators.
487 bool operator==(const SmallBitVector &RHS) const {
488 if (size() != RHS.size())
490 if (isSmall() && RHS.isSmall())
491 return getSmallBits() == RHS.getSmallBits();
492 else if (!isSmall() && !RHS.isSmall())
493 return *getPointer() == *RHS.getPointer();
495 for (size_t i = 0, e = size(); i != e; ++i) {
496 if ((*this)[i] != RHS[i])
503 bool operator!=(const SmallBitVector &RHS) const {
504 return !(*this == RHS);
507 // Intersection, union, disjoint union.
508 // FIXME BitVector::operator&= does not resize the LHS but this does
509 SmallBitVector &operator&=(const SmallBitVector &RHS) {
510 resize(std::max(size(), RHS.size()));
511 if (isSmall() && RHS.isSmall())
512 setSmallBits(getSmallBits() & RHS.getSmallBits());
513 else if (!isSmall() && !RHS.isSmall())
514 getPointer()->operator&=(*RHS.getPointer());
517 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
518 (*this)[i] = test(i) && RHS.test(i);
519 for (e = size(); i != e; ++i)
525 /// Reset bits that are set in RHS. Same as *this &= ~RHS.
526 SmallBitVector &reset(const SmallBitVector &RHS) {
527 if (isSmall() && RHS.isSmall())
528 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
529 else if (!isSmall() && !RHS.isSmall())
530 getPointer()->reset(*RHS.getPointer());
532 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
539 /// Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
540 bool test(const SmallBitVector &RHS) const {
541 if (isSmall() && RHS.isSmall())
542 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
543 if (!isSmall() && !RHS.isSmall())
544 return getPointer()->test(*RHS.getPointer());
547 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
548 if (test(i) && !RHS.test(i))
551 for (e = size(); i != e; ++i)
558 SmallBitVector &operator|=(const SmallBitVector &RHS) {
559 resize(std::max(size(), RHS.size()));
560 if (isSmall() && RHS.isSmall())
561 setSmallBits(getSmallBits() | RHS.getSmallBits());
562 else if (!isSmall() && !RHS.isSmall())
563 getPointer()->operator|=(*RHS.getPointer());
565 for (size_t i = 0, e = RHS.size(); i != e; ++i)
566 (*this)[i] = test(i) || RHS.test(i);
571 SmallBitVector &operator^=(const SmallBitVector &RHS) {
572 resize(std::max(size(), RHS.size()));
573 if (isSmall() && RHS.isSmall())
574 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
575 else if (!isSmall() && !RHS.isSmall())
576 getPointer()->operator^=(*RHS.getPointer());
578 for (size_t i = 0, e = RHS.size(); i != e; ++i)
579 (*this)[i] = test(i) != RHS.test(i);
584 SmallBitVector &operator<<=(unsigned N) {
586 setSmallBits(getSmallBits() << N);
588 getPointer()->operator<<=(N);
592 SmallBitVector &operator>>=(unsigned N) {
594 setSmallBits(getSmallBits() >> N);
596 getPointer()->operator>>=(N);
600 // Assignment operator.
601 const SmallBitVector &operator=(const SmallBitVector &RHS) {
606 switchToLarge(new BitVector(*RHS.getPointer()));
609 *getPointer() = *RHS.getPointer();
618 const SmallBitVector &operator=(SmallBitVector &&RHS) {
626 void swap(SmallBitVector &RHS) {
630 /// Add '1' bits from Mask to this vector. Don't resize.
631 /// This computes "*this |= Mask".
632 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
634 applyMask<true, false>(Mask, MaskWords);
636 getPointer()->setBitsInMask(Mask, MaskWords);
639 /// Clear any bits in this vector that are set in Mask. Don't resize.
640 /// This computes "*this &= ~Mask".
641 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
643 applyMask<false, false>(Mask, MaskWords);
645 getPointer()->clearBitsInMask(Mask, MaskWords);
648 /// Add a bit to this vector for every '0' bit in Mask. Don't resize.
649 /// This computes "*this |= ~Mask".
650 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
652 applyMask<true, true>(Mask, MaskWords);
654 getPointer()->setBitsNotInMask(Mask, MaskWords);
657 /// Clear a bit in this vector for every '0' bit in Mask. Don't resize.
658 /// This computes "*this &= Mask".
659 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
661 applyMask<false, true>(Mask, MaskWords);
663 getPointer()->clearBitsNotInMask(Mask, MaskWords);
667 template <bool AddBits, bool InvertMask>
668 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
669 assert(MaskWords <= sizeof(uintptr_t) && "Mask is larger than base!");
670 uintptr_t M = Mask[0];
671 if (NumBaseBits == 64)
672 M |= uint64_t(Mask[1]) << 32;
676 setSmallBits(getSmallBits() | M);
678 setSmallBits(getSmallBits() & ~M);
682 inline SmallBitVector
683 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
684 SmallBitVector Result(LHS);
689 inline SmallBitVector
690 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
691 SmallBitVector Result(LHS);
696 inline SmallBitVector
697 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
698 SmallBitVector Result(LHS);
703 } // end namespace llvm
707 /// Implement std::swap in terms of BitVector swap.
709 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
713 } // end namespace std
715 #endif // LLVM_ADT_SMALLBITVECTOR_H