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/Compiler.h"
19 #include "llvm/Support/MathExtras.h"
24 /// SmallBitVector - This is a 'bitvector' (really, a variable-sized bit array),
25 /// optimized for the case when the array is small. It contains one
26 /// pointer-sized field, which is directly used as a plain collection of bits
27 /// when possible, or as a pointer to a larger heap-allocated array when
28 /// necessary. This allows normal "small" cases to be fast without losing
29 /// generality for large inputs.
31 class SmallBitVector {
32 // TODO: In "large" mode, a pointer to a BitVector is used, leading to an
33 // unnecessary level of indirection. It would be more efficient to use a
34 // pointer to memory containing size, allocation size, and the array of bits.
38 // The number of bits in this class.
39 NumBaseBits = sizeof(uintptr_t) * CHAR_BIT,
41 // One bit is used to discriminate between small and large mode. The
42 // remaining bits are used for the small-mode representation.
43 SmallNumRawBits = NumBaseBits - 1,
45 // A few more bits are used to store the size of the bit set in small mode.
46 // Theoretically this is a ceil-log2. These bits are encoded in the most
47 // significant bits of the raw bits.
48 SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
49 NumBaseBits == 64 ? 6 :
52 // The remaining bits are used to store the actual set in small mode.
53 SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
57 // Encapsulation of a single bit.
59 SmallBitVector &TheVector;
63 reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
65 reference& operator=(reference t) {
70 reference& operator=(bool t) {
72 TheVector.set(BitPos);
74 TheVector.reset(BitPos);
78 operator bool() const {
79 return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
84 bool isSmall() const {
85 return X & uintptr_t(1);
88 BitVector *getPointer() const {
90 return reinterpret_cast<BitVector *>(X);
93 void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
95 setSmallSize(NewSize);
96 setSmallBits(NewSmallBits);
99 void switchToLarge(BitVector *BV) {
100 X = reinterpret_cast<uintptr_t>(BV);
101 assert(!isSmall() && "Tried to use an unaligned pointer");
104 // Return all the bits used for the "small" representation; this includes
105 // bits for the size as well as the element bits.
106 uintptr_t getSmallRawBits() const {
111 void setSmallRawBits(uintptr_t NewRawBits) {
113 X = (NewRawBits << 1) | uintptr_t(1);
117 size_t getSmallSize() const {
118 return getSmallRawBits() >> SmallNumDataBits;
121 void setSmallSize(size_t Size) {
122 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
125 // Return the element bits.
126 uintptr_t getSmallBits() const {
127 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
130 void setSmallBits(uintptr_t NewBits) {
131 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
132 (getSmallSize() << SmallNumDataBits));
136 /// SmallBitVector default ctor - Creates an empty bitvector.
137 SmallBitVector() : X(1) {}
139 /// SmallBitVector ctor - Creates a bitvector of specified number of bits. All
140 /// bits are initialized to the specified value.
141 explicit SmallBitVector(unsigned s, bool t = false) {
142 if (s <= SmallNumDataBits)
143 switchToSmall(t ? ~uintptr_t(0) : 0, s);
145 switchToLarge(new BitVector(s, t));
148 /// SmallBitVector copy ctor.
149 SmallBitVector(const SmallBitVector &RHS) {
153 switchToLarge(new BitVector(*RHS.getPointer()));
156 #if LLVM_HAS_RVALUE_REFERENCES
157 SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
167 /// empty - Tests whether there are no bits in this bitvector.
169 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
172 /// size - Returns the number of bits in this bitvector.
173 size_t size() const {
174 return isSmall() ? getSmallSize() : getPointer()->size();
177 /// count - Returns the number of bits which are set.
178 unsigned count() const {
180 uintptr_t Bits = getSmallBits();
181 if (sizeof(uintptr_t) * CHAR_BIT == 32)
182 return CountPopulation_32(Bits);
183 if (sizeof(uintptr_t) * CHAR_BIT == 64)
184 return CountPopulation_64(Bits);
185 llvm_unreachable("Unsupported!");
187 return getPointer()->count();
190 /// any - Returns true if any bit is set.
193 return getSmallBits() != 0;
194 return getPointer()->any();
197 /// all - Returns true if all bits are set.
200 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
201 return getPointer()->all();
204 /// none - Returns true if none of the bits are set.
207 return getSmallBits() == 0;
208 return getPointer()->none();
211 /// find_first - Returns the index of the first set bit, -1 if none
212 /// of the bits are set.
213 int find_first() const {
215 uintptr_t Bits = getSmallBits();
218 if (sizeof(uintptr_t) * CHAR_BIT == 32)
219 return CountTrailingZeros_32(Bits);
220 if (sizeof(uintptr_t) * CHAR_BIT == 64)
221 return CountTrailingZeros_64(Bits);
222 llvm_unreachable("Unsupported!");
224 return getPointer()->find_first();
227 /// find_next - Returns the index of the next set bit following the
228 /// "Prev" bit. Returns -1 if the next set bit is not found.
229 int find_next(unsigned Prev) const {
231 uintptr_t Bits = getSmallBits();
232 // Mask off previous bits.
233 Bits &= ~uintptr_t(0) << (Prev + 1);
234 if (Bits == 0 || Prev + 1 >= getSmallSize())
236 if (sizeof(uintptr_t) * CHAR_BIT == 32)
237 return CountTrailingZeros_32(Bits);
238 if (sizeof(uintptr_t) * CHAR_BIT == 64)
239 return CountTrailingZeros_64(Bits);
240 llvm_unreachable("Unsupported!");
242 return getPointer()->find_next(Prev);
245 /// clear - Clear all bits.
252 /// resize - Grow or shrink the bitvector.
253 void resize(unsigned N, bool t = false) {
255 getPointer()->resize(N, t);
256 } else if (SmallNumDataBits >= N) {
257 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
259 setSmallBits(NewBits | getSmallBits());
261 BitVector *BV = new BitVector(N, t);
262 uintptr_t OldBits = getSmallBits();
263 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
264 (*BV)[i] = (OldBits >> i) & 1;
269 void reserve(unsigned N) {
271 if (N > SmallNumDataBits) {
272 uintptr_t OldBits = getSmallRawBits();
273 size_t SmallSize = getSmallSize();
274 BitVector *BV = new BitVector(SmallSize);
275 for (size_t i = 0; i < SmallSize; ++i)
276 if ((OldBits >> i) & 1)
282 getPointer()->reserve(N);
287 SmallBitVector &set() {
289 setSmallBits(~uintptr_t(0));
295 SmallBitVector &set(unsigned Idx) {
297 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
299 getPointer()->set(Idx);
303 /// set - Efficiently set a range of bits in [I, E)
304 SmallBitVector &set(unsigned I, unsigned E) {
305 assert(I <= E && "Attempted to set backwards range!");
306 assert(E <= size() && "Attempted to set out-of-bounds range!");
307 if (I == E) return *this;
309 uintptr_t EMask = ((uintptr_t)1) << E;
310 uintptr_t IMask = ((uintptr_t)1) << I;
311 uintptr_t Mask = EMask - IMask;
312 setSmallBits(getSmallBits() | Mask);
314 getPointer()->set(I, E);
318 SmallBitVector &reset() {
322 getPointer()->reset();
326 SmallBitVector &reset(unsigned Idx) {
328 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
330 getPointer()->reset(Idx);
334 /// reset - Efficiently reset a range of bits in [I, E)
335 SmallBitVector &reset(unsigned I, unsigned E) {
336 assert(I <= E && "Attempted to reset backwards range!");
337 assert(E <= size() && "Attempted to reset out-of-bounds range!");
338 if (I == E) return *this;
340 uintptr_t EMask = ((uintptr_t)1) << E;
341 uintptr_t IMask = ((uintptr_t)1) << I;
342 uintptr_t Mask = EMask - IMask;
343 setSmallBits(getSmallBits() & ~Mask);
345 getPointer()->reset(I, E);
349 SmallBitVector &flip() {
351 setSmallBits(~getSmallBits());
353 getPointer()->flip();
357 SmallBitVector &flip(unsigned Idx) {
359 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
361 getPointer()->flip(Idx);
366 SmallBitVector operator~() const {
367 return SmallBitVector(*this).flip();
371 reference operator[](unsigned Idx) {
372 assert(Idx < size() && "Out-of-bounds Bit access.");
373 return reference(*this, Idx);
376 bool operator[](unsigned Idx) const {
377 assert(Idx < size() && "Out-of-bounds Bit access.");
379 return ((getSmallBits() >> Idx) & 1) != 0;
380 return getPointer()->operator[](Idx);
383 bool test(unsigned Idx) const {
387 /// Test if any common bits are set.
388 bool anyCommon(const SmallBitVector &RHS) const {
389 if (isSmall() && RHS.isSmall())
390 return (getSmallBits() & RHS.getSmallBits()) != 0;
391 if (!isSmall() && !RHS.isSmall())
392 return getPointer()->anyCommon(*RHS.getPointer());
394 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
395 if (test(i) && RHS.test(i))
400 // Comparison operators.
401 bool operator==(const SmallBitVector &RHS) const {
402 if (size() != RHS.size())
405 return getSmallBits() == RHS.getSmallBits();
407 return *getPointer() == *RHS.getPointer();
410 bool operator!=(const SmallBitVector &RHS) const {
411 return !(*this == RHS);
414 // Intersection, union, disjoint union.
415 SmallBitVector &operator&=(const SmallBitVector &RHS) {
416 resize(std::max(size(), RHS.size()));
418 setSmallBits(getSmallBits() & RHS.getSmallBits());
419 else if (!RHS.isSmall())
420 getPointer()->operator&=(*RHS.getPointer());
422 SmallBitVector Copy = RHS;
424 getPointer()->operator&=(*Copy.getPointer());
429 SmallBitVector &operator|=(const SmallBitVector &RHS) {
430 resize(std::max(size(), RHS.size()));
432 setSmallBits(getSmallBits() | RHS.getSmallBits());
433 else if (!RHS.isSmall())
434 getPointer()->operator|=(*RHS.getPointer());
436 SmallBitVector Copy = RHS;
438 getPointer()->operator|=(*Copy.getPointer());
443 SmallBitVector &operator^=(const SmallBitVector &RHS) {
444 resize(std::max(size(), RHS.size()));
446 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
447 else if (!RHS.isSmall())
448 getPointer()->operator^=(*RHS.getPointer());
450 SmallBitVector Copy = RHS;
452 getPointer()->operator^=(*Copy.getPointer());
457 // Assignment operator.
458 const SmallBitVector &operator=(const SmallBitVector &RHS) {
463 switchToLarge(new BitVector(*RHS.getPointer()));
466 *getPointer() = *RHS.getPointer();
475 #if LLVM_HAS_RVALUE_REFERENCES
476 const SmallBitVector &operator=(SmallBitVector &&RHS) {
485 void swap(SmallBitVector &RHS) {
489 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
490 /// This computes "*this |= Mask".
491 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
493 applyMask<true, false>(Mask, MaskWords);
495 getPointer()->setBitsInMask(Mask, MaskWords);
498 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
499 /// Don't resize. This computes "*this &= ~Mask".
500 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
502 applyMask<false, false>(Mask, MaskWords);
504 getPointer()->clearBitsInMask(Mask, MaskWords);
507 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
508 /// Don't resize. This computes "*this |= ~Mask".
509 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
511 applyMask<true, true>(Mask, MaskWords);
513 getPointer()->setBitsNotInMask(Mask, MaskWords);
516 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
517 /// Don't resize. This computes "*this &= Mask".
518 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
520 applyMask<false, true>(Mask, MaskWords);
522 getPointer()->clearBitsNotInMask(Mask, MaskWords);
526 template<bool AddBits, bool InvertMask>
527 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
528 assert((NumBaseBits == 64 || NumBaseBits == 32) && "Unsupported word size");
529 if (NumBaseBits == 64 && MaskWords >= 2) {
530 uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
531 if (InvertMask) M = ~M;
532 if (AddBits) setSmallBits(getSmallBits() | M);
533 else setSmallBits(getSmallBits() & ~M);
535 uint32_t M = Mask[0];
536 if (InvertMask) M = ~M;
537 if (AddBits) setSmallBits(getSmallBits() | M);
538 else setSmallBits(getSmallBits() & ~M);
543 inline SmallBitVector
544 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
545 SmallBitVector Result(LHS);
550 inline SmallBitVector
551 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
552 SmallBitVector Result(LHS);
557 inline SmallBitVector
558 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
559 SmallBitVector Result(LHS);
564 } // End llvm namespace
567 /// Implement std::swap in terms of BitVector swap.
569 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {