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 /// Creates an empty bitvector.
138 SmallBitVector() : X(1) {}
140 /// Creates a bitvector of specified number of bits. All bits are initialized
141 /// to the specified value.
142 explicit SmallBitVector(unsigned s, bool t = false) {
143 if (s <= SmallNumDataBits)
144 switchToSmall(t ? ~uintptr_t(0) : 0, s);
146 switchToLarge(new BitVector(s, t));
149 /// SmallBitVector copy ctor.
150 SmallBitVector(const SmallBitVector &RHS) {
154 switchToLarge(new BitVector(*RHS.getPointer()));
157 SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
166 /// Tests whether there are no bits in this bitvector.
168 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
171 /// Returns the number of bits in this bitvector.
172 size_t size() const {
173 return isSmall() ? getSmallSize() : getPointer()->size();
176 /// Returns the number of bits which are set.
177 size_type count() const {
179 uintptr_t Bits = getSmallBits();
180 return countPopulation(Bits);
182 return getPointer()->count();
185 /// Returns true if any bit is set.
188 return getSmallBits() != 0;
189 return getPointer()->any();
192 /// Returns true if all bits are set.
195 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
196 return getPointer()->all();
199 /// Returns true if none of the bits are set.
202 return getSmallBits() == 0;
203 return getPointer()->none();
206 /// Returns the index of the first set bit, -1 if none of the bits are set.
207 int find_first() const {
209 uintptr_t Bits = getSmallBits();
212 return countTrailingZeros(Bits);
214 return getPointer()->find_first();
217 int find_last() const {
219 uintptr_t Bits = getSmallBits();
222 return NumBaseBits - countLeadingZeros(Bits);
224 return getPointer()->find_last();
227 /// Returns the index of the first unset bit, -1 if all of the bits are set.
228 int find_first_unset() const {
230 if (count() == getSmallSize())
233 uintptr_t Bits = getSmallBits();
234 return countTrailingOnes(Bits);
236 return getPointer()->find_first_unset();
239 int find_last_unset() const {
241 if (count() == getSmallSize())
244 uintptr_t Bits = getSmallBits();
245 return NumBaseBits - countLeadingOnes(Bits);
247 return getPointer()->find_last_unset();
250 /// Returns the index of the next set bit following the "Prev" bit.
251 /// Returns -1 if the next set bit is not found.
252 int find_next(unsigned Prev) const {
254 uintptr_t Bits = getSmallBits();
255 // Mask off previous bits.
256 Bits &= ~uintptr_t(0) << (Prev + 1);
257 if (Bits == 0 || Prev + 1 >= getSmallSize())
259 return countTrailingZeros(Bits);
261 return getPointer()->find_next(Prev);
264 /// Returns the index of the next unset bit following the "Prev" bit.
265 /// Returns -1 if the next unset bit is not found.
266 int find_next_unset(unsigned Prev) const {
269 uintptr_t Bits = getSmallBits();
270 // Mask in previous bits.
271 uintptr_t Mask = (1 << Prev) - 1;
274 if (Bits == ~uintptr_t(0) || Prev + 1 >= getSmallSize())
276 return countTrailingOnes(Bits);
278 return getPointer()->find_next_unset(Prev);
288 /// Grow or shrink the bitvector.
289 void resize(unsigned N, bool t = false) {
291 getPointer()->resize(N, t);
292 } else if (SmallNumDataBits >= N) {
293 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
295 setSmallBits(NewBits | getSmallBits());
297 BitVector *BV = new BitVector(N, t);
298 uintptr_t OldBits = getSmallBits();
299 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
300 (*BV)[i] = (OldBits >> i) & 1;
305 void reserve(unsigned N) {
307 if (N > SmallNumDataBits) {
308 uintptr_t OldBits = getSmallRawBits();
309 size_t SmallSize = getSmallSize();
310 BitVector *BV = new BitVector(SmallSize);
311 for (size_t i = 0; i < SmallSize; ++i)
312 if ((OldBits >> i) & 1)
318 getPointer()->reserve(N);
323 SmallBitVector &set() {
325 setSmallBits(~uintptr_t(0));
331 SmallBitVector &set(unsigned Idx) {
333 assert(Idx <= static_cast<unsigned>(
334 std::numeric_limits<uintptr_t>::digits) &&
335 "undefined behavior");
336 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
339 getPointer()->set(Idx);
343 /// Efficiently set a range of bits in [I, E)
344 SmallBitVector &set(unsigned I, unsigned E) {
345 assert(I <= E && "Attempted to set backwards range!");
346 assert(E <= size() && "Attempted to set out-of-bounds range!");
347 if (I == E) return *this;
349 uintptr_t EMask = ((uintptr_t)1) << E;
350 uintptr_t IMask = ((uintptr_t)1) << I;
351 uintptr_t Mask = EMask - IMask;
352 setSmallBits(getSmallBits() | Mask);
354 getPointer()->set(I, E);
358 SmallBitVector &reset() {
362 getPointer()->reset();
366 SmallBitVector &reset(unsigned Idx) {
368 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
370 getPointer()->reset(Idx);
374 /// Efficiently reset a range of bits in [I, E)
375 SmallBitVector &reset(unsigned I, unsigned E) {
376 assert(I <= E && "Attempted to reset backwards range!");
377 assert(E <= size() && "Attempted to reset 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()->reset(I, E);
389 SmallBitVector &flip() {
391 setSmallBits(~getSmallBits());
393 getPointer()->flip();
397 SmallBitVector &flip(unsigned Idx) {
399 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
401 getPointer()->flip(Idx);
406 SmallBitVector operator~() const {
407 return SmallBitVector(*this).flip();
411 reference operator[](unsigned Idx) {
412 assert(Idx < size() && "Out-of-bounds Bit access.");
413 return reference(*this, Idx);
416 bool operator[](unsigned Idx) const {
417 assert(Idx < size() && "Out-of-bounds Bit access.");
419 return ((getSmallBits() >> Idx) & 1) != 0;
420 return getPointer()->operator[](Idx);
423 bool test(unsigned Idx) const {
427 /// Test if any common bits are set.
428 bool anyCommon(const SmallBitVector &RHS) const {
429 if (isSmall() && RHS.isSmall())
430 return (getSmallBits() & RHS.getSmallBits()) != 0;
431 if (!isSmall() && !RHS.isSmall())
432 return getPointer()->anyCommon(*RHS.getPointer());
434 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
435 if (test(i) && RHS.test(i))
440 // Comparison operators.
441 bool operator==(const SmallBitVector &RHS) const {
442 if (size() != RHS.size())
445 return getSmallBits() == RHS.getSmallBits();
447 return *getPointer() == *RHS.getPointer();
450 bool operator!=(const SmallBitVector &RHS) const {
451 return !(*this == RHS);
454 // Intersection, union, disjoint union.
455 SmallBitVector &operator&=(const SmallBitVector &RHS) {
456 resize(std::max(size(), RHS.size()));
458 setSmallBits(getSmallBits() & RHS.getSmallBits());
459 else if (!RHS.isSmall())
460 getPointer()->operator&=(*RHS.getPointer());
462 SmallBitVector Copy = RHS;
464 getPointer()->operator&=(*Copy.getPointer());
469 /// Reset bits that are set in RHS. Same as *this &= ~RHS.
470 SmallBitVector &reset(const SmallBitVector &RHS) {
471 if (isSmall() && RHS.isSmall())
472 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
473 else if (!isSmall() && !RHS.isSmall())
474 getPointer()->reset(*RHS.getPointer());
476 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
483 /// Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
484 bool test(const SmallBitVector &RHS) const {
485 if (isSmall() && RHS.isSmall())
486 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
487 if (!isSmall() && !RHS.isSmall())
488 return getPointer()->test(*RHS.getPointer());
491 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
492 if (test(i) && !RHS.test(i))
495 for (e = size(); i != e; ++i)
502 SmallBitVector &operator|=(const SmallBitVector &RHS) {
503 resize(std::max(size(), RHS.size()));
505 setSmallBits(getSmallBits() | RHS.getSmallBits());
506 else if (!RHS.isSmall())
507 getPointer()->operator|=(*RHS.getPointer());
509 SmallBitVector Copy = RHS;
511 getPointer()->operator|=(*Copy.getPointer());
516 SmallBitVector &operator^=(const SmallBitVector &RHS) {
517 resize(std::max(size(), RHS.size()));
519 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
520 else if (!RHS.isSmall())
521 getPointer()->operator^=(*RHS.getPointer());
523 SmallBitVector Copy = RHS;
525 getPointer()->operator^=(*Copy.getPointer());
530 SmallBitVector &operator<<=(unsigned N) {
532 setSmallBits(getSmallBits() << N);
534 getPointer()->operator<<=(N);
538 SmallBitVector &operator>>=(unsigned N) {
540 setSmallBits(getSmallBits() >> N);
542 getPointer()->operator>>=(N);
546 // Assignment operator.
547 const SmallBitVector &operator=(const SmallBitVector &RHS) {
552 switchToLarge(new BitVector(*RHS.getPointer()));
555 *getPointer() = *RHS.getPointer();
564 const SmallBitVector &operator=(SmallBitVector &&RHS) {
572 void swap(SmallBitVector &RHS) {
576 /// Add '1' bits from Mask to this vector. Don't resize.
577 /// This computes "*this |= Mask".
578 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
580 applyMask<true, false>(Mask, MaskWords);
582 getPointer()->setBitsInMask(Mask, MaskWords);
585 /// Clear any bits in this vector that are set in Mask. Don't resize.
586 /// This computes "*this &= ~Mask".
587 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
589 applyMask<false, false>(Mask, MaskWords);
591 getPointer()->clearBitsInMask(Mask, MaskWords);
594 /// Add a bit to this vector for every '0' bit in Mask. Don't resize.
595 /// This computes "*this |= ~Mask".
596 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
598 applyMask<true, true>(Mask, MaskWords);
600 getPointer()->setBitsNotInMask(Mask, MaskWords);
603 /// Clear a bit in this vector for every '0' bit in Mask. Don't resize.
604 /// This computes "*this &= Mask".
605 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
607 applyMask<false, true>(Mask, MaskWords);
609 getPointer()->clearBitsNotInMask(Mask, MaskWords);
613 template <bool AddBits, bool InvertMask>
614 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
615 assert(MaskWords <= sizeof(uintptr_t) && "Mask is larger than base!");
616 uintptr_t M = Mask[0];
617 if (NumBaseBits == 64)
618 M |= uint64_t(Mask[1]) << 32;
622 setSmallBits(getSmallBits() | M);
624 setSmallBits(getSmallBits() & ~M);
628 inline SmallBitVector
629 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
630 SmallBitVector Result(LHS);
635 inline SmallBitVector
636 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
637 SmallBitVector Result(LHS);
642 inline SmallBitVector
643 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
644 SmallBitVector Result(LHS);
649 } // End llvm namespace
652 /// Implement std::swap in terms of BitVector swap.
654 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {