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 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& operator=(reference t) {
71 reference& operator=(bool t) {
73 TheVector.set(BitPos);
75 TheVector.reset(BitPos);
79 operator bool() const {
80 return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
85 bool isSmall() const {
86 return X & uintptr_t(1);
89 BitVector *getPointer() const {
91 return reinterpret_cast<BitVector *>(X);
94 void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
96 setSmallSize(NewSize);
97 setSmallBits(NewSmallBits);
100 void switchToLarge(BitVector *BV) {
101 X = reinterpret_cast<uintptr_t>(BV);
102 assert(!isSmall() && "Tried to use an unaligned pointer");
105 // Return all the bits used for the "small" representation; this includes
106 // bits for the size as well as the element bits.
107 uintptr_t getSmallRawBits() const {
112 void setSmallRawBits(uintptr_t NewRawBits) {
114 X = (NewRawBits << 1) | uintptr_t(1);
118 size_t getSmallSize() const {
119 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 /// SmallBitVector default ctor - Creates an empty bitvector.
138 SmallBitVector() : X(1) {}
140 /// SmallBitVector ctor - Creates a bitvector of specified number of bits. All
141 /// bits are initialized 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 /// empty - Tests whether there are no bits in this bitvector.
168 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
171 /// size - Returns the number of bits in this bitvector.
172 size_t size() const {
173 return isSmall() ? getSmallSize() : getPointer()->size();
176 /// count - Returns the number of bits which are set.
177 size_type count() const {
179 uintptr_t Bits = getSmallBits();
180 if (NumBaseBits == 32)
181 return CountPopulation_32(Bits);
182 if (NumBaseBits == 64)
183 return CountPopulation_64(Bits);
184 llvm_unreachable("Unsupported!");
186 return getPointer()->count();
189 /// any - Returns true if any bit is set.
192 return getSmallBits() != 0;
193 return getPointer()->any();
196 /// all - Returns true if all bits are set.
199 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
200 return getPointer()->all();
203 /// none - Returns true if none of the bits are set.
206 return getSmallBits() == 0;
207 return getPointer()->none();
210 /// find_first - Returns the index of the first set bit, -1 if none
211 /// of the bits are set.
212 int find_first() const {
214 uintptr_t Bits = getSmallBits();
217 if (NumBaseBits == 32)
218 return countTrailingZeros(Bits);
219 if (NumBaseBits == 64)
220 return countTrailingZeros(Bits);
221 llvm_unreachable("Unsupported!");
223 return getPointer()->find_first();
226 /// find_next - Returns the index of the next set bit following the
227 /// "Prev" bit. Returns -1 if the next set bit is not found.
228 int find_next(unsigned Prev) const {
230 uintptr_t Bits = getSmallBits();
231 // Mask off previous bits.
232 Bits &= ~uintptr_t(0) << (Prev + 1);
233 if (Bits == 0 || Prev + 1 >= getSmallSize())
235 if (NumBaseBits == 32)
236 return countTrailingZeros(Bits);
237 if (NumBaseBits == 64)
238 return countTrailingZeros(Bits);
239 llvm_unreachable("Unsupported!");
241 return getPointer()->find_next(Prev);
244 /// clear - Clear all bits.
251 /// resize - Grow or shrink the bitvector.
252 void resize(unsigned N, bool t = false) {
254 getPointer()->resize(N, t);
255 } else if (SmallNumDataBits >= N) {
256 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
258 setSmallBits(NewBits | getSmallBits());
260 BitVector *BV = new BitVector(N, t);
261 uintptr_t OldBits = getSmallBits();
262 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
263 (*BV)[i] = (OldBits >> i) & 1;
268 void reserve(unsigned N) {
270 if (N > SmallNumDataBits) {
271 uintptr_t OldBits = getSmallRawBits();
272 size_t SmallSize = getSmallSize();
273 BitVector *BV = new BitVector(SmallSize);
274 for (size_t i = 0; i < SmallSize; ++i)
275 if ((OldBits >> i) & 1)
281 getPointer()->reserve(N);
286 SmallBitVector &set() {
288 setSmallBits(~uintptr_t(0));
294 SmallBitVector &set(unsigned Idx) {
296 assert(Idx <= static_cast<unsigned>(
297 std::numeric_limits<uintptr_t>::digits) &&
298 "undefined behavior");
299 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
302 getPointer()->set(Idx);
306 /// set - Efficiently set a range of bits in [I, E)
307 SmallBitVector &set(unsigned I, unsigned E) {
308 assert(I <= E && "Attempted to set backwards range!");
309 assert(E <= size() && "Attempted to set out-of-bounds range!");
310 if (I == E) return *this;
312 uintptr_t EMask = ((uintptr_t)1) << E;
313 uintptr_t IMask = ((uintptr_t)1) << I;
314 uintptr_t Mask = EMask - IMask;
315 setSmallBits(getSmallBits() | Mask);
317 getPointer()->set(I, E);
321 SmallBitVector &reset() {
325 getPointer()->reset();
329 SmallBitVector &reset(unsigned Idx) {
331 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
333 getPointer()->reset(Idx);
337 /// reset - Efficiently reset a range of bits in [I, E)
338 SmallBitVector &reset(unsigned I, unsigned E) {
339 assert(I <= E && "Attempted to reset backwards range!");
340 assert(E <= size() && "Attempted to reset out-of-bounds range!");
341 if (I == E) return *this;
343 uintptr_t EMask = ((uintptr_t)1) << E;
344 uintptr_t IMask = ((uintptr_t)1) << I;
345 uintptr_t Mask = EMask - IMask;
346 setSmallBits(getSmallBits() & ~Mask);
348 getPointer()->reset(I, E);
352 SmallBitVector &flip() {
354 setSmallBits(~getSmallBits());
356 getPointer()->flip();
360 SmallBitVector &flip(unsigned Idx) {
362 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
364 getPointer()->flip(Idx);
369 SmallBitVector operator~() const {
370 return SmallBitVector(*this).flip();
374 reference operator[](unsigned Idx) {
375 assert(Idx < size() && "Out-of-bounds Bit access.");
376 return reference(*this, Idx);
379 bool operator[](unsigned Idx) const {
380 assert(Idx < size() && "Out-of-bounds Bit access.");
382 return ((getSmallBits() >> Idx) & 1) != 0;
383 return getPointer()->operator[](Idx);
386 bool test(unsigned Idx) const {
390 /// Test if any common bits are set.
391 bool anyCommon(const SmallBitVector &RHS) const {
392 if (isSmall() && RHS.isSmall())
393 return (getSmallBits() & RHS.getSmallBits()) != 0;
394 if (!isSmall() && !RHS.isSmall())
395 return getPointer()->anyCommon(*RHS.getPointer());
397 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
398 if (test(i) && RHS.test(i))
403 // Comparison operators.
404 bool operator==(const SmallBitVector &RHS) const {
405 if (size() != RHS.size())
408 return getSmallBits() == RHS.getSmallBits();
410 return *getPointer() == *RHS.getPointer();
413 bool operator!=(const SmallBitVector &RHS) const {
414 return !(*this == RHS);
417 // Intersection, union, disjoint union.
418 SmallBitVector &operator&=(const SmallBitVector &RHS) {
419 resize(std::max(size(), RHS.size()));
421 setSmallBits(getSmallBits() & RHS.getSmallBits());
422 else if (!RHS.isSmall())
423 getPointer()->operator&=(*RHS.getPointer());
425 SmallBitVector Copy = RHS;
427 getPointer()->operator&=(*Copy.getPointer());
432 /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
433 SmallBitVector &reset(const SmallBitVector &RHS) {
434 if (isSmall() && RHS.isSmall())
435 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
436 else if (!isSmall() && !RHS.isSmall())
437 getPointer()->reset(*RHS.getPointer());
439 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
446 /// test - Check if (This - RHS) is zero.
447 /// This is the same as reset(RHS) and any().
448 bool test(const SmallBitVector &RHS) const {
449 if (isSmall() && RHS.isSmall())
450 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
451 if (!isSmall() && !RHS.isSmall())
452 return getPointer()->test(*RHS.getPointer());
455 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
456 if (test(i) && !RHS.test(i))
459 for (e = size(); i != e; ++i)
466 SmallBitVector &operator|=(const SmallBitVector &RHS) {
467 resize(std::max(size(), RHS.size()));
469 setSmallBits(getSmallBits() | RHS.getSmallBits());
470 else if (!RHS.isSmall())
471 getPointer()->operator|=(*RHS.getPointer());
473 SmallBitVector Copy = RHS;
475 getPointer()->operator|=(*Copy.getPointer());
480 SmallBitVector &operator^=(const SmallBitVector &RHS) {
481 resize(std::max(size(), RHS.size()));
483 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
484 else if (!RHS.isSmall())
485 getPointer()->operator^=(*RHS.getPointer());
487 SmallBitVector Copy = RHS;
489 getPointer()->operator^=(*Copy.getPointer());
494 // Assignment operator.
495 const SmallBitVector &operator=(const SmallBitVector &RHS) {
500 switchToLarge(new BitVector(*RHS.getPointer()));
503 *getPointer() = *RHS.getPointer();
512 const SmallBitVector &operator=(SmallBitVector &&RHS) {
520 void swap(SmallBitVector &RHS) {
524 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
525 /// This computes "*this |= Mask".
526 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
528 applyMask<true, false>(Mask, MaskWords);
530 getPointer()->setBitsInMask(Mask, MaskWords);
533 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
534 /// Don't resize. This computes "*this &= ~Mask".
535 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
537 applyMask<false, false>(Mask, MaskWords);
539 getPointer()->clearBitsInMask(Mask, MaskWords);
542 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
543 /// Don't resize. This computes "*this |= ~Mask".
544 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
546 applyMask<true, true>(Mask, MaskWords);
548 getPointer()->setBitsNotInMask(Mask, MaskWords);
551 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
552 /// Don't resize. This computes "*this &= Mask".
553 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
555 applyMask<false, true>(Mask, MaskWords);
557 getPointer()->clearBitsNotInMask(Mask, MaskWords);
561 template<bool AddBits, bool InvertMask>
562 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
563 assert((NumBaseBits == 64 || NumBaseBits == 32) && "Unsupported word size");
564 if (NumBaseBits == 64 && MaskWords >= 2) {
565 uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
566 if (InvertMask) M = ~M;
567 if (AddBits) setSmallBits(getSmallBits() | M);
568 else setSmallBits(getSmallBits() & ~M);
570 uint32_t M = Mask[0];
571 if (InvertMask) M = ~M;
572 if (AddBits) setSmallBits(getSmallBits() | M);
573 else setSmallBits(getSmallBits() & ~M);
578 inline SmallBitVector
579 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
580 SmallBitVector Result(LHS);
585 inline SmallBitVector
586 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
587 SmallBitVector Result(LHS);
592 inline SmallBitVector
593 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
594 SmallBitVector Result(LHS);
599 } // End llvm namespace
602 /// Implement std::swap in terms of BitVector swap.
604 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {