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 {
121 return getSmallRawBits() >> SmallNumDataBits;
124 void setSmallSize(size_t Size) {
125 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
128 // Return the element bits.
129 uintptr_t getSmallBits() const {
130 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
133 void setSmallBits(uintptr_t NewBits) {
134 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
135 (getSmallSize() << SmallNumDataBits));
139 /// Creates an empty bitvector.
140 SmallBitVector() : X(1) {}
142 /// Creates a bitvector of specified number of bits. All bits are initialized
143 /// to the specified value.
144 explicit SmallBitVector(unsigned s, bool t = false) {
145 if (s <= SmallNumDataBits)
146 switchToSmall(t ? ~uintptr_t(0) : 0, s);
148 switchToLarge(new BitVector(s, t));
151 /// SmallBitVector copy ctor.
152 SmallBitVector(const SmallBitVector &RHS) {
156 switchToLarge(new BitVector(*RHS.getPointer()));
159 SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
168 /// Tests whether there are no bits in this bitvector.
170 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
173 /// Returns the number of bits in this bitvector.
174 size_t size() const {
175 return isSmall() ? getSmallSize() : getPointer()->size();
178 /// Returns the number of bits which are set.
179 size_type count() const {
181 uintptr_t Bits = getSmallBits();
182 return countPopulation(Bits);
184 return getPointer()->count();
187 /// Returns true if any bit is set.
190 return getSmallBits() != 0;
191 return getPointer()->any();
194 /// Returns true if all bits are set.
197 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
198 return getPointer()->all();
201 /// Returns true if none of the bits are set.
204 return getSmallBits() == 0;
205 return getPointer()->none();
208 /// Returns the index of the first set bit, -1 if none of the bits are set.
209 int find_first() const {
211 uintptr_t Bits = getSmallBits();
214 return countTrailingZeros(Bits);
216 return getPointer()->find_first();
219 /// Returns the index of the first unset bit, -1 if all of the bits are set.
220 int find_first_unset() const {
222 if (count() == getSmallSize())
225 uintptr_t Bits = getSmallBits();
226 return countTrailingOnes(Bits);
228 return getPointer()->find_first_unset();
231 /// Returns the index of the next set bit following the "Prev" bit.
232 /// Returns -1 if the next set bit is not found.
233 int find_next(unsigned Prev) const {
235 uintptr_t Bits = getSmallBits();
236 // Mask off previous bits.
237 Bits &= ~uintptr_t(0) << (Prev + 1);
238 if (Bits == 0 || Prev + 1 >= getSmallSize())
240 return countTrailingZeros(Bits);
242 return getPointer()->find_next(Prev);
245 /// Returns the index of the next unset bit following the "Prev" bit.
246 /// Returns -1 if the next unset bit is not found.
247 int find_next_unset(unsigned Prev) const {
250 uintptr_t Bits = getSmallBits();
251 // Mask in previous bits.
252 uintptr_t Mask = (1 << Prev) - 1;
255 if (Bits == ~uintptr_t(0) || Prev + 1 >= getSmallSize())
257 return countTrailingOnes(Bits);
259 return getPointer()->find_next_unset(Prev);
269 /// Grow or shrink the bitvector.
270 void resize(unsigned N, bool t = false) {
272 getPointer()->resize(N, t);
273 } else if (SmallNumDataBits >= N) {
274 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
276 setSmallBits(NewBits | getSmallBits());
278 BitVector *BV = new BitVector(N, t);
279 uintptr_t OldBits = getSmallBits();
280 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
281 (*BV)[i] = (OldBits >> i) & 1;
286 void reserve(unsigned N) {
288 if (N > SmallNumDataBits) {
289 uintptr_t OldBits = getSmallRawBits();
290 size_t SmallSize = getSmallSize();
291 BitVector *BV = new BitVector(SmallSize);
292 for (size_t i = 0; i < SmallSize; ++i)
293 if ((OldBits >> i) & 1)
299 getPointer()->reserve(N);
304 SmallBitVector &set() {
306 setSmallBits(~uintptr_t(0));
312 SmallBitVector &set(unsigned Idx) {
314 assert(Idx <= static_cast<unsigned>(
315 std::numeric_limits<uintptr_t>::digits) &&
316 "undefined behavior");
317 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
320 getPointer()->set(Idx);
324 /// Efficiently set a range of bits in [I, E)
325 SmallBitVector &set(unsigned I, unsigned E) {
326 assert(I <= E && "Attempted to set backwards range!");
327 assert(E <= size() && "Attempted to set out-of-bounds range!");
328 if (I == E) return *this;
330 uintptr_t EMask = ((uintptr_t)1) << E;
331 uintptr_t IMask = ((uintptr_t)1) << I;
332 uintptr_t Mask = EMask - IMask;
333 setSmallBits(getSmallBits() | Mask);
335 getPointer()->set(I, E);
339 SmallBitVector &reset() {
343 getPointer()->reset();
347 SmallBitVector &reset(unsigned Idx) {
349 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
351 getPointer()->reset(Idx);
355 /// Efficiently reset a range of bits in [I, E)
356 SmallBitVector &reset(unsigned I, unsigned E) {
357 assert(I <= E && "Attempted to reset backwards range!");
358 assert(E <= size() && "Attempted to reset out-of-bounds range!");
359 if (I == E) return *this;
361 uintptr_t EMask = ((uintptr_t)1) << E;
362 uintptr_t IMask = ((uintptr_t)1) << I;
363 uintptr_t Mask = EMask - IMask;
364 setSmallBits(getSmallBits() & ~Mask);
366 getPointer()->reset(I, E);
370 SmallBitVector &flip() {
372 setSmallBits(~getSmallBits());
374 getPointer()->flip();
378 SmallBitVector &flip(unsigned Idx) {
380 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
382 getPointer()->flip(Idx);
387 SmallBitVector operator~() const {
388 return SmallBitVector(*this).flip();
392 reference operator[](unsigned Idx) {
393 assert(Idx < size() && "Out-of-bounds Bit access.");
394 return reference(*this, Idx);
397 bool operator[](unsigned Idx) const {
398 assert(Idx < size() && "Out-of-bounds Bit access.");
400 return ((getSmallBits() >> Idx) & 1) != 0;
401 return getPointer()->operator[](Idx);
404 bool test(unsigned Idx) const {
408 /// Test if any common bits are set.
409 bool anyCommon(const SmallBitVector &RHS) const {
410 if (isSmall() && RHS.isSmall())
411 return (getSmallBits() & RHS.getSmallBits()) != 0;
412 if (!isSmall() && !RHS.isSmall())
413 return getPointer()->anyCommon(*RHS.getPointer());
415 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
416 if (test(i) && RHS.test(i))
421 // Comparison operators.
422 bool operator==(const SmallBitVector &RHS) const {
423 if (size() != RHS.size())
426 return getSmallBits() == RHS.getSmallBits();
428 return *getPointer() == *RHS.getPointer();
431 bool operator!=(const SmallBitVector &RHS) const {
432 return !(*this == RHS);
435 // Intersection, union, disjoint union.
436 SmallBitVector &operator&=(const SmallBitVector &RHS) {
437 resize(std::max(size(), RHS.size()));
439 setSmallBits(getSmallBits() & RHS.getSmallBits());
440 else if (!RHS.isSmall())
441 getPointer()->operator&=(*RHS.getPointer());
443 SmallBitVector Copy = RHS;
445 getPointer()->operator&=(*Copy.getPointer());
450 /// Reset bits that are set in RHS. Same as *this &= ~RHS.
451 SmallBitVector &reset(const SmallBitVector &RHS) {
452 if (isSmall() && RHS.isSmall())
453 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
454 else if (!isSmall() && !RHS.isSmall())
455 getPointer()->reset(*RHS.getPointer());
457 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
464 /// Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
465 bool test(const SmallBitVector &RHS) const {
466 if (isSmall() && RHS.isSmall())
467 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
468 if (!isSmall() && !RHS.isSmall())
469 return getPointer()->test(*RHS.getPointer());
472 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
473 if (test(i) && !RHS.test(i))
476 for (e = size(); i != e; ++i)
483 SmallBitVector &operator|=(const SmallBitVector &RHS) {
484 resize(std::max(size(), RHS.size()));
486 setSmallBits(getSmallBits() | RHS.getSmallBits());
487 else if (!RHS.isSmall())
488 getPointer()->operator|=(*RHS.getPointer());
490 SmallBitVector Copy = RHS;
492 getPointer()->operator|=(*Copy.getPointer());
497 SmallBitVector &operator^=(const SmallBitVector &RHS) {
498 resize(std::max(size(), RHS.size()));
500 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
501 else if (!RHS.isSmall())
502 getPointer()->operator^=(*RHS.getPointer());
504 SmallBitVector Copy = RHS;
506 getPointer()->operator^=(*Copy.getPointer());
511 // Assignment operator.
512 const SmallBitVector &operator=(const SmallBitVector &RHS) {
517 switchToLarge(new BitVector(*RHS.getPointer()));
520 *getPointer() = *RHS.getPointer();
529 const SmallBitVector &operator=(SmallBitVector &&RHS) {
537 void swap(SmallBitVector &RHS) {
541 /// Add '1' bits from Mask to this vector. Don't resize.
542 /// This computes "*this |= Mask".
543 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
545 applyMask<true, false>(Mask, MaskWords);
547 getPointer()->setBitsInMask(Mask, MaskWords);
550 /// Clear any bits in this vector that are set in Mask. Don't resize.
551 /// This computes "*this &= ~Mask".
552 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
554 applyMask<false, false>(Mask, MaskWords);
556 getPointer()->clearBitsInMask(Mask, MaskWords);
559 /// Add a bit to this vector for every '0' bit in Mask. Don't resize.
560 /// This computes "*this |= ~Mask".
561 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
563 applyMask<true, true>(Mask, MaskWords);
565 getPointer()->setBitsNotInMask(Mask, MaskWords);
568 /// Clear a bit in this vector for every '0' bit in Mask. Don't resize.
569 /// This computes "*this &= Mask".
570 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
572 applyMask<false, true>(Mask, MaskWords);
574 getPointer()->clearBitsNotInMask(Mask, MaskWords);
578 template <bool AddBits, bool InvertMask>
579 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
580 assert(MaskWords <= sizeof(uintptr_t) && "Mask is larger than base!");
581 uintptr_t M = Mask[0];
582 if (NumBaseBits == 64)
583 M |= uint64_t(Mask[1]) << 32;
587 setSmallBits(getSmallBits() | M);
589 setSmallBits(getSmallBits() & ~M);
593 inline SmallBitVector
594 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
595 SmallBitVector Result(LHS);
600 inline SmallBitVector
601 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
602 SmallBitVector Result(LHS);
607 inline SmallBitVector
608 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
609 SmallBitVector Result(LHS);
614 } // End llvm namespace
617 /// Implement std::swap in terms of BitVector swap.
619 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {