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/Support/MathExtras.h"
23 /// This is a 'bitvector' (really, a variable-sized bit array), optimized for
24 /// the case when the array is small. It contains one pointer-sized field, which
25 /// is directly used as a plain collection of bits when possible, or as a
26 /// pointer to a larger heap-allocated array when necessary. This allows normal
27 /// "small" cases to be fast without losing generality for large inputs.
28 class SmallBitVector {
29 // TODO: In "large" mode, a pointer to a BitVector is used, leading to an
30 // unnecessary level of indirection. It would be more efficient to use a
31 // pointer to memory containing size, allocation size, and the array of bits.
35 // The number of bits in this class.
36 NumBaseBits = sizeof(uintptr_t) * CHAR_BIT,
38 // One bit is used to discriminate between small and large mode. The
39 // remaining bits are used for the small-mode representation.
40 SmallNumRawBits = NumBaseBits - 1,
42 // A few more bits are used to store the size of the bit set in small mode.
43 // Theoretically this is a ceil-log2. These bits are encoded in the most
44 // significant bits of the raw bits.
45 SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
46 NumBaseBits == 64 ? 6 :
49 // The remaining bits are used to store the actual set in small mode.
50 SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
53 static_assert(NumBaseBits == 64 || NumBaseBits == 32,
54 "Unsupported word size");
57 typedef unsigned size_type;
58 // Encapsulation of a single bit.
60 SmallBitVector &TheVector;
64 reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
66 reference(const reference&) = default;
68 reference& operator=(reference t) {
73 reference& operator=(bool t) {
75 TheVector.set(BitPos);
77 TheVector.reset(BitPos);
81 operator bool() const {
82 return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
87 bool isSmall() const {
88 return X & uintptr_t(1);
91 BitVector *getPointer() const {
93 return reinterpret_cast<BitVector *>(X);
96 void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
98 setSmallSize(NewSize);
99 setSmallBits(NewSmallBits);
102 void switchToLarge(BitVector *BV) {
103 X = reinterpret_cast<uintptr_t>(BV);
104 assert(!isSmall() && "Tried to use an unaligned pointer");
107 // Return all the bits used for the "small" representation; this includes
108 // bits for the size as well as the element bits.
109 uintptr_t getSmallRawBits() const {
114 void setSmallRawBits(uintptr_t NewRawBits) {
116 X = (NewRawBits << 1) | uintptr_t(1);
120 size_t getSmallSize() const { return getSmallRawBits() >> SmallNumDataBits; }
122 void setSmallSize(size_t Size) {
123 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
126 // Return the element bits.
127 uintptr_t getSmallBits() const {
128 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
131 void setSmallBits(uintptr_t NewBits) {
132 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
133 (getSmallSize() << SmallNumDataBits));
137 typedef const_set_bits_iterator_impl<SmallBitVector> const_set_bits_iterator;
138 typedef const_set_bits_iterator set_iterator;
140 const_set_bits_iterator set_bits_begin() const {
141 return const_set_bits_iterator(*this);
143 const_set_bits_iterator set_bits_end() const {
144 return const_set_bits_iterator(*this, -1);
146 iterator_range<const_set_bits_iterator> set_bits() const {
147 return make_range(set_bits_begin(), set_bits_end());
150 /// Creates an empty bitvector.
151 SmallBitVector() : X(1) {}
153 /// Creates a bitvector of specified number of bits. All bits are initialized
154 /// to the specified value.
155 explicit SmallBitVector(unsigned s, bool t = false) {
156 if (s <= SmallNumDataBits)
157 switchToSmall(t ? ~uintptr_t(0) : 0, s);
159 switchToLarge(new BitVector(s, t));
162 /// SmallBitVector copy ctor.
163 SmallBitVector(const SmallBitVector &RHS) {
167 switchToLarge(new BitVector(*RHS.getPointer()));
170 SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
179 /// Tests whether there are no bits in this bitvector.
181 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
184 /// Returns the number of bits in this bitvector.
185 size_t size() const {
186 return isSmall() ? getSmallSize() : getPointer()->size();
189 /// Returns the number of bits which are set.
190 size_type count() const {
192 uintptr_t Bits = getSmallBits();
193 return countPopulation(Bits);
195 return getPointer()->count();
198 /// Returns true if any bit is set.
201 return getSmallBits() != 0;
202 return getPointer()->any();
205 /// Returns true if all bits are set.
208 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
209 return getPointer()->all();
212 /// Returns true if none of the bits are set.
215 return getSmallBits() == 0;
216 return getPointer()->none();
219 /// Returns the index of the first set bit, -1 if none of the bits are set.
220 int find_first() const {
222 uintptr_t Bits = getSmallBits();
225 return countTrailingZeros(Bits);
227 return getPointer()->find_first();
230 int find_last() const {
232 uintptr_t Bits = getSmallBits();
235 return NumBaseBits - countLeadingZeros(Bits);
237 return getPointer()->find_last();
240 /// Returns the index of the first unset bit, -1 if all of the bits are set.
241 int find_first_unset() const {
243 if (count() == getSmallSize())
246 uintptr_t Bits = getSmallBits();
247 return countTrailingOnes(Bits);
249 return getPointer()->find_first_unset();
252 int find_last_unset() const {
254 if (count() == getSmallSize())
257 uintptr_t Bits = getSmallBits();
258 return NumBaseBits - countLeadingOnes(Bits);
260 return getPointer()->find_last_unset();
263 /// Returns the index of the next set bit following the "Prev" bit.
264 /// Returns -1 if the next set bit is not found.
265 int find_next(unsigned Prev) const {
267 uintptr_t Bits = getSmallBits();
268 // Mask off previous bits.
269 Bits &= ~uintptr_t(0) << (Prev + 1);
270 if (Bits == 0 || Prev + 1 >= getSmallSize())
272 return countTrailingZeros(Bits);
274 return getPointer()->find_next(Prev);
277 /// Returns the index of the next unset bit following the "Prev" bit.
278 /// Returns -1 if the next unset bit is not found.
279 int find_next_unset(unsigned Prev) const {
282 uintptr_t Bits = getSmallBits();
283 // Mask in previous bits.
284 uintptr_t Mask = (1 << Prev) - 1;
287 if (Bits == ~uintptr_t(0) || Prev + 1 >= getSmallSize())
289 return countTrailingOnes(Bits);
291 return getPointer()->find_next_unset(Prev);
294 /// find_prev - Returns the index of the first set bit that precedes the
295 /// the bit at \p PriorTo. Returns -1 if all previous bits are unset.
296 int find_prev(unsigned PriorTo) const {
302 uintptr_t Bits = getSmallBits();
303 Bits &= maskTrailingOnes<uintptr_t>(PriorTo + 1);
307 return NumBaseBits - countLeadingZeros(Bits) - 1;
309 return getPointer()->find_prev(PriorTo);
319 /// Grow or shrink the bitvector.
320 void resize(unsigned N, bool t = false) {
322 getPointer()->resize(N, t);
323 } else if (SmallNumDataBits >= N) {
324 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
326 setSmallBits(NewBits | getSmallBits());
328 BitVector *BV = new BitVector(N, t);
329 uintptr_t OldBits = getSmallBits();
330 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
331 (*BV)[i] = (OldBits >> i) & 1;
336 void reserve(unsigned N) {
338 if (N > SmallNumDataBits) {
339 uintptr_t OldBits = getSmallRawBits();
340 size_t SmallSize = getSmallSize();
341 BitVector *BV = new BitVector(SmallSize);
342 for (size_t i = 0; i < SmallSize; ++i)
343 if ((OldBits >> i) & 1)
349 getPointer()->reserve(N);
354 SmallBitVector &set() {
356 setSmallBits(~uintptr_t(0));
362 SmallBitVector &set(unsigned Idx) {
364 assert(Idx <= static_cast<unsigned>(
365 std::numeric_limits<uintptr_t>::digits) &&
366 "undefined behavior");
367 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
370 getPointer()->set(Idx);
374 /// Efficiently set a range of bits in [I, E)
375 SmallBitVector &set(unsigned I, unsigned E) {
376 assert(I <= E && "Attempted to set backwards range!");
377 assert(E <= size() && "Attempted to set out-of-bounds range!");
378 if (I == E) return *this;
380 uintptr_t EMask = ((uintptr_t)1) << E;
381 uintptr_t IMask = ((uintptr_t)1) << I;
382 uintptr_t Mask = EMask - IMask;
383 setSmallBits(getSmallBits() | Mask);
385 getPointer()->set(I, E);
389 SmallBitVector &reset() {
393 getPointer()->reset();
397 SmallBitVector &reset(unsigned Idx) {
399 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
401 getPointer()->reset(Idx);
405 /// Efficiently reset a range of bits in [I, E)
406 SmallBitVector &reset(unsigned I, unsigned E) {
407 assert(I <= E && "Attempted to reset backwards range!");
408 assert(E <= size() && "Attempted to reset out-of-bounds range!");
409 if (I == E) return *this;
411 uintptr_t EMask = ((uintptr_t)1) << E;
412 uintptr_t IMask = ((uintptr_t)1) << I;
413 uintptr_t Mask = EMask - IMask;
414 setSmallBits(getSmallBits() & ~Mask);
416 getPointer()->reset(I, E);
420 SmallBitVector &flip() {
422 setSmallBits(~getSmallBits());
424 getPointer()->flip();
428 SmallBitVector &flip(unsigned Idx) {
430 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
432 getPointer()->flip(Idx);
437 SmallBitVector operator~() const {
438 return SmallBitVector(*this).flip();
442 reference operator[](unsigned Idx) {
443 assert(Idx < size() && "Out-of-bounds Bit access.");
444 return reference(*this, Idx);
447 bool operator[](unsigned Idx) const {
448 assert(Idx < size() && "Out-of-bounds Bit access.");
450 return ((getSmallBits() >> Idx) & 1) != 0;
451 return getPointer()->operator[](Idx);
454 bool test(unsigned Idx) const {
458 /// Test if any common bits are set.
459 bool anyCommon(const SmallBitVector &RHS) const {
460 if (isSmall() && RHS.isSmall())
461 return (getSmallBits() & RHS.getSmallBits()) != 0;
462 if (!isSmall() && !RHS.isSmall())
463 return getPointer()->anyCommon(*RHS.getPointer());
465 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
466 if (test(i) && RHS.test(i))
471 // Comparison operators.
472 bool operator==(const SmallBitVector &RHS) const {
473 if (size() != RHS.size())
476 return getSmallBits() == RHS.getSmallBits();
478 return *getPointer() == *RHS.getPointer();
481 bool operator!=(const SmallBitVector &RHS) const {
482 return !(*this == RHS);
485 // Intersection, union, disjoint union.
486 SmallBitVector &operator&=(const SmallBitVector &RHS) {
487 resize(std::max(size(), RHS.size()));
489 setSmallBits(getSmallBits() & RHS.getSmallBits());
490 else if (!RHS.isSmall())
491 getPointer()->operator&=(*RHS.getPointer());
493 SmallBitVector Copy = RHS;
495 getPointer()->operator&=(*Copy.getPointer());
500 /// Reset bits that are set in RHS. Same as *this &= ~RHS.
501 SmallBitVector &reset(const SmallBitVector &RHS) {
502 if (isSmall() && RHS.isSmall())
503 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
504 else if (!isSmall() && !RHS.isSmall())
505 getPointer()->reset(*RHS.getPointer());
507 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
514 /// Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
515 bool test(const SmallBitVector &RHS) const {
516 if (isSmall() && RHS.isSmall())
517 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
518 if (!isSmall() && !RHS.isSmall())
519 return getPointer()->test(*RHS.getPointer());
522 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
523 if (test(i) && !RHS.test(i))
526 for (e = size(); i != e; ++i)
533 SmallBitVector &operator|=(const SmallBitVector &RHS) {
534 resize(std::max(size(), RHS.size()));
536 setSmallBits(getSmallBits() | RHS.getSmallBits());
537 else if (!RHS.isSmall())
538 getPointer()->operator|=(*RHS.getPointer());
540 SmallBitVector Copy = RHS;
542 getPointer()->operator|=(*Copy.getPointer());
547 SmallBitVector &operator^=(const SmallBitVector &RHS) {
548 resize(std::max(size(), RHS.size()));
550 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
551 else if (!RHS.isSmall())
552 getPointer()->operator^=(*RHS.getPointer());
554 SmallBitVector Copy = RHS;
556 getPointer()->operator^=(*Copy.getPointer());
561 SmallBitVector &operator<<=(unsigned N) {
563 setSmallBits(getSmallBits() << N);
565 getPointer()->operator<<=(N);
569 SmallBitVector &operator>>=(unsigned N) {
571 setSmallBits(getSmallBits() >> N);
573 getPointer()->operator>>=(N);
577 // Assignment operator.
578 const SmallBitVector &operator=(const SmallBitVector &RHS) {
583 switchToLarge(new BitVector(*RHS.getPointer()));
586 *getPointer() = *RHS.getPointer();
595 const SmallBitVector &operator=(SmallBitVector &&RHS) {
603 void swap(SmallBitVector &RHS) {
607 /// Add '1' bits from Mask to this vector. Don't resize.
608 /// This computes "*this |= Mask".
609 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
611 applyMask<true, false>(Mask, MaskWords);
613 getPointer()->setBitsInMask(Mask, MaskWords);
616 /// Clear any bits in this vector that are set in Mask. Don't resize.
617 /// This computes "*this &= ~Mask".
618 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
620 applyMask<false, false>(Mask, MaskWords);
622 getPointer()->clearBitsInMask(Mask, MaskWords);
625 /// Add a bit to this vector for every '0' bit in Mask. Don't resize.
626 /// This computes "*this |= ~Mask".
627 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
629 applyMask<true, true>(Mask, MaskWords);
631 getPointer()->setBitsNotInMask(Mask, MaskWords);
634 /// Clear a bit in this vector for every '0' bit in Mask. Don't resize.
635 /// This computes "*this &= Mask".
636 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
638 applyMask<false, true>(Mask, MaskWords);
640 getPointer()->clearBitsNotInMask(Mask, MaskWords);
644 template <bool AddBits, bool InvertMask>
645 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
646 assert(MaskWords <= sizeof(uintptr_t) && "Mask is larger than base!");
647 uintptr_t M = Mask[0];
648 if (NumBaseBits == 64)
649 M |= uint64_t(Mask[1]) << 32;
653 setSmallBits(getSmallBits() | M);
655 setSmallBits(getSmallBits() & ~M);
659 inline SmallBitVector
660 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
661 SmallBitVector Result(LHS);
666 inline SmallBitVector
667 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
668 SmallBitVector Result(LHS);
673 inline SmallBitVector
674 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
675 SmallBitVector Result(LHS);
680 } // End llvm namespace
683 /// Implement std::swap in terms of BitVector swap.
685 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {