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
56 static_assert(NumBaseBits == 64 || NumBaseBits == 32,
57 "Unsupported word size");
60 typedef unsigned size_type;
61 // Encapsulation of a single bit.
63 SmallBitVector &TheVector;
67 reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
69 reference& operator=(reference t) {
74 reference& operator=(bool t) {
76 TheVector.set(BitPos);
78 TheVector.reset(BitPos);
82 operator bool() const {
83 return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
88 bool isSmall() const {
89 return X & uintptr_t(1);
92 BitVector *getPointer() const {
94 return reinterpret_cast<BitVector *>(X);
97 void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
99 setSmallSize(NewSize);
100 setSmallBits(NewSmallBits);
103 void switchToLarge(BitVector *BV) {
104 X = reinterpret_cast<uintptr_t>(BV);
105 assert(!isSmall() && "Tried to use an unaligned pointer");
108 // Return all the bits used for the "small" representation; this includes
109 // bits for the size as well as the element bits.
110 uintptr_t getSmallRawBits() const {
115 void setSmallRawBits(uintptr_t NewRawBits) {
117 X = (NewRawBits << 1) | uintptr_t(1);
121 size_t getSmallSize() const {
122 return getSmallRawBits() >> SmallNumDataBits;
125 void setSmallSize(size_t Size) {
126 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
129 // Return the element bits.
130 uintptr_t getSmallBits() const {
131 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
134 void setSmallBits(uintptr_t NewBits) {
135 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
136 (getSmallSize() << SmallNumDataBits));
140 /// SmallBitVector default ctor - Creates an empty bitvector.
141 SmallBitVector() : X(1) {}
143 /// SmallBitVector ctor - Creates a bitvector of specified number of bits. All
144 /// bits are initialized to the specified value.
145 explicit SmallBitVector(unsigned s, bool t = false) {
146 if (s <= SmallNumDataBits)
147 switchToSmall(t ? ~uintptr_t(0) : 0, s);
149 switchToLarge(new BitVector(s, t));
152 /// SmallBitVector copy ctor.
153 SmallBitVector(const SmallBitVector &RHS) {
157 switchToLarge(new BitVector(*RHS.getPointer()));
160 SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
169 /// empty - Tests whether there are no bits in this bitvector.
171 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
174 /// size - Returns the number of bits in this bitvector.
175 size_t size() const {
176 return isSmall() ? getSmallSize() : getPointer()->size();
179 /// count - Returns the number of bits which are set.
180 size_type count() const {
182 uintptr_t Bits = getSmallBits();
183 if (NumBaseBits == 32)
184 return CountPopulation_32(Bits);
185 if (NumBaseBits == 64)
186 return CountPopulation_64(Bits);
187 llvm_unreachable("Unsupported!");
189 return getPointer()->count();
192 /// any - Returns true if any bit is set.
195 return getSmallBits() != 0;
196 return getPointer()->any();
199 /// all - Returns true if all bits are set.
202 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
203 return getPointer()->all();
206 /// none - Returns true if none of the bits are set.
209 return getSmallBits() == 0;
210 return getPointer()->none();
213 /// find_first - Returns the index of the first set bit, -1 if none
214 /// of the bits are set.
215 int find_first() const {
217 uintptr_t Bits = getSmallBits();
220 return countTrailingZeros(Bits);
222 return getPointer()->find_first();
225 /// find_next - Returns the index of the next set bit following the
226 /// "Prev" bit. Returns -1 if the next set bit is not found.
227 int find_next(unsigned Prev) const {
229 uintptr_t Bits = getSmallBits();
230 // Mask off previous bits.
231 Bits &= ~uintptr_t(0) << (Prev + 1);
232 if (Bits == 0 || Prev + 1 >= getSmallSize())
234 return countTrailingZeros(Bits);
236 return getPointer()->find_next(Prev);
239 /// clear - Clear all bits.
246 /// resize - Grow or shrink the bitvector.
247 void resize(unsigned N, bool t = false) {
249 getPointer()->resize(N, t);
250 } else if (SmallNumDataBits >= N) {
251 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
253 setSmallBits(NewBits | getSmallBits());
255 BitVector *BV = new BitVector(N, t);
256 uintptr_t OldBits = getSmallBits();
257 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
258 (*BV)[i] = (OldBits >> i) & 1;
263 void reserve(unsigned N) {
265 if (N > SmallNumDataBits) {
266 uintptr_t OldBits = getSmallRawBits();
267 size_t SmallSize = getSmallSize();
268 BitVector *BV = new BitVector(SmallSize);
269 for (size_t i = 0; i < SmallSize; ++i)
270 if ((OldBits >> i) & 1)
276 getPointer()->reserve(N);
281 SmallBitVector &set() {
283 setSmallBits(~uintptr_t(0));
289 SmallBitVector &set(unsigned Idx) {
291 assert(Idx <= static_cast<unsigned>(
292 std::numeric_limits<uintptr_t>::digits) &&
293 "undefined behavior");
294 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
297 getPointer()->set(Idx);
301 /// set - Efficiently set a range of bits in [I, E)
302 SmallBitVector &set(unsigned I, unsigned E) {
303 assert(I <= E && "Attempted to set backwards range!");
304 assert(E <= size() && "Attempted to set out-of-bounds range!");
305 if (I == E) return *this;
307 uintptr_t EMask = ((uintptr_t)1) << E;
308 uintptr_t IMask = ((uintptr_t)1) << I;
309 uintptr_t Mask = EMask - IMask;
310 setSmallBits(getSmallBits() | Mask);
312 getPointer()->set(I, E);
316 SmallBitVector &reset() {
320 getPointer()->reset();
324 SmallBitVector &reset(unsigned Idx) {
326 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
328 getPointer()->reset(Idx);
332 /// reset - Efficiently reset a range of bits in [I, E)
333 SmallBitVector &reset(unsigned I, unsigned E) {
334 assert(I <= E && "Attempted to reset backwards range!");
335 assert(E <= size() && "Attempted to reset out-of-bounds range!");
336 if (I == E) return *this;
338 uintptr_t EMask = ((uintptr_t)1) << E;
339 uintptr_t IMask = ((uintptr_t)1) << I;
340 uintptr_t Mask = EMask - IMask;
341 setSmallBits(getSmallBits() & ~Mask);
343 getPointer()->reset(I, E);
347 SmallBitVector &flip() {
349 setSmallBits(~getSmallBits());
351 getPointer()->flip();
355 SmallBitVector &flip(unsigned Idx) {
357 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
359 getPointer()->flip(Idx);
364 SmallBitVector operator~() const {
365 return SmallBitVector(*this).flip();
369 reference operator[](unsigned Idx) {
370 assert(Idx < size() && "Out-of-bounds Bit access.");
371 return reference(*this, Idx);
374 bool operator[](unsigned Idx) const {
375 assert(Idx < size() && "Out-of-bounds Bit access.");
377 return ((getSmallBits() >> Idx) & 1) != 0;
378 return getPointer()->operator[](Idx);
381 bool test(unsigned Idx) const {
385 /// Test if any common bits are set.
386 bool anyCommon(const SmallBitVector &RHS) const {
387 if (isSmall() && RHS.isSmall())
388 return (getSmallBits() & RHS.getSmallBits()) != 0;
389 if (!isSmall() && !RHS.isSmall())
390 return getPointer()->anyCommon(*RHS.getPointer());
392 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
393 if (test(i) && RHS.test(i))
398 // Comparison operators.
399 bool operator==(const SmallBitVector &RHS) const {
400 if (size() != RHS.size())
403 return getSmallBits() == RHS.getSmallBits();
405 return *getPointer() == *RHS.getPointer();
408 bool operator!=(const SmallBitVector &RHS) const {
409 return !(*this == RHS);
412 // Intersection, union, disjoint union.
413 SmallBitVector &operator&=(const SmallBitVector &RHS) {
414 resize(std::max(size(), RHS.size()));
416 setSmallBits(getSmallBits() & RHS.getSmallBits());
417 else if (!RHS.isSmall())
418 getPointer()->operator&=(*RHS.getPointer());
420 SmallBitVector Copy = RHS;
422 getPointer()->operator&=(*Copy.getPointer());
427 /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
428 SmallBitVector &reset(const SmallBitVector &RHS) {
429 if (isSmall() && RHS.isSmall())
430 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
431 else if (!isSmall() && !RHS.isSmall())
432 getPointer()->reset(*RHS.getPointer());
434 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
441 /// test - Check if (This - RHS) is zero.
442 /// This is the same as reset(RHS) and any().
443 bool test(const SmallBitVector &RHS) const {
444 if (isSmall() && RHS.isSmall())
445 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
446 if (!isSmall() && !RHS.isSmall())
447 return getPointer()->test(*RHS.getPointer());
450 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
451 if (test(i) && !RHS.test(i))
454 for (e = size(); i != e; ++i)
461 SmallBitVector &operator|=(const SmallBitVector &RHS) {
462 resize(std::max(size(), RHS.size()));
464 setSmallBits(getSmallBits() | RHS.getSmallBits());
465 else if (!RHS.isSmall())
466 getPointer()->operator|=(*RHS.getPointer());
468 SmallBitVector Copy = RHS;
470 getPointer()->operator|=(*Copy.getPointer());
475 SmallBitVector &operator^=(const SmallBitVector &RHS) {
476 resize(std::max(size(), RHS.size()));
478 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
479 else if (!RHS.isSmall())
480 getPointer()->operator^=(*RHS.getPointer());
482 SmallBitVector Copy = RHS;
484 getPointer()->operator^=(*Copy.getPointer());
489 // Assignment operator.
490 const SmallBitVector &operator=(const SmallBitVector &RHS) {
495 switchToLarge(new BitVector(*RHS.getPointer()));
498 *getPointer() = *RHS.getPointer();
507 const SmallBitVector &operator=(SmallBitVector &&RHS) {
515 void swap(SmallBitVector &RHS) {
519 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
520 /// This computes "*this |= Mask".
521 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
523 applyMask<true, false>(Mask, MaskWords);
525 getPointer()->setBitsInMask(Mask, MaskWords);
528 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
529 /// Don't resize. This computes "*this &= ~Mask".
530 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
532 applyMask<false, false>(Mask, MaskWords);
534 getPointer()->clearBitsInMask(Mask, MaskWords);
537 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
538 /// Don't resize. This computes "*this |= ~Mask".
539 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
541 applyMask<true, true>(Mask, MaskWords);
543 getPointer()->setBitsNotInMask(Mask, MaskWords);
546 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
547 /// Don't resize. This computes "*this &= Mask".
548 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
550 applyMask<false, true>(Mask, MaskWords);
552 getPointer()->clearBitsNotInMask(Mask, MaskWords);
556 template<bool AddBits, bool InvertMask>
557 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
558 if (NumBaseBits == 64 && MaskWords >= 2) {
559 uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
560 if (InvertMask) M = ~M;
561 if (AddBits) setSmallBits(getSmallBits() | M);
562 else setSmallBits(getSmallBits() & ~M);
564 uint32_t M = Mask[0];
565 if (InvertMask) M = ~M;
566 if (AddBits) setSmallBits(getSmallBits() | M);
567 else setSmallBits(getSmallBits() & ~M);
572 inline SmallBitVector
573 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
574 SmallBitVector Result(LHS);
579 inline SmallBitVector
580 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
581 SmallBitVector Result(LHS);
586 inline SmallBitVector
587 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
588 SmallBitVector Result(LHS);
593 } // End llvm namespace
596 /// Implement std::swap in terms of BitVector swap.
598 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {