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(const reference&) = default;
71 reference& operator=(reference t) {
76 reference& operator=(bool t) {
78 TheVector.set(BitPos);
80 TheVector.reset(BitPos);
84 operator bool() const {
85 return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
90 bool isSmall() const {
91 return X & uintptr_t(1);
94 BitVector *getPointer() const {
96 return reinterpret_cast<BitVector *>(X);
99 void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
101 setSmallSize(NewSize);
102 setSmallBits(NewSmallBits);
105 void switchToLarge(BitVector *BV) {
106 X = reinterpret_cast<uintptr_t>(BV);
107 assert(!isSmall() && "Tried to use an unaligned pointer");
110 // Return all the bits used for the "small" representation; this includes
111 // bits for the size as well as the element bits.
112 uintptr_t getSmallRawBits() const {
117 void setSmallRawBits(uintptr_t NewRawBits) {
119 X = (NewRawBits << 1) | uintptr_t(1);
123 size_t getSmallSize() const {
124 return getSmallRawBits() >> SmallNumDataBits;
127 void setSmallSize(size_t Size) {
128 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
131 // Return the element bits.
132 uintptr_t getSmallBits() const {
133 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
136 void setSmallBits(uintptr_t NewBits) {
137 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
138 (getSmallSize() << SmallNumDataBits));
142 /// SmallBitVector default ctor - Creates an empty bitvector.
143 SmallBitVector() : X(1) {}
145 /// SmallBitVector ctor - Creates a bitvector of specified number of bits. All
146 /// bits are initialized to the specified value.
147 explicit SmallBitVector(unsigned s, bool t = false) {
148 if (s <= SmallNumDataBits)
149 switchToSmall(t ? ~uintptr_t(0) : 0, s);
151 switchToLarge(new BitVector(s, t));
154 /// SmallBitVector copy ctor.
155 SmallBitVector(const SmallBitVector &RHS) {
159 switchToLarge(new BitVector(*RHS.getPointer()));
162 SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
171 /// empty - Tests whether there are no bits in this bitvector.
173 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
176 /// size - Returns the number of bits in this bitvector.
177 size_t size() const {
178 return isSmall() ? getSmallSize() : getPointer()->size();
181 /// count - Returns the number of bits which are set.
182 size_type count() const {
184 uintptr_t Bits = getSmallBits();
185 return countPopulation(Bits);
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 return countTrailingZeros(Bits);
220 return getPointer()->find_first();
223 /// find_next - Returns the index of the next set bit following the
224 /// "Prev" bit. Returns -1 if the next set bit is not found.
225 int find_next(unsigned Prev) const {
227 uintptr_t Bits = getSmallBits();
228 // Mask off previous bits.
229 Bits &= ~uintptr_t(0) << (Prev + 1);
230 if (Bits == 0 || Prev + 1 >= getSmallSize())
232 return countTrailingZeros(Bits);
234 return getPointer()->find_next(Prev);
237 /// clear - Clear all bits.
244 /// resize - Grow or shrink the bitvector.
245 void resize(unsigned N, bool t = false) {
247 getPointer()->resize(N, t);
248 } else if (SmallNumDataBits >= N) {
249 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
251 setSmallBits(NewBits | getSmallBits());
253 BitVector *BV = new BitVector(N, t);
254 uintptr_t OldBits = getSmallBits();
255 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
256 (*BV)[i] = (OldBits >> i) & 1;
261 void reserve(unsigned N) {
263 if (N > SmallNumDataBits) {
264 uintptr_t OldBits = getSmallRawBits();
265 size_t SmallSize = getSmallSize();
266 BitVector *BV = new BitVector(SmallSize);
267 for (size_t i = 0; i < SmallSize; ++i)
268 if ((OldBits >> i) & 1)
274 getPointer()->reserve(N);
279 SmallBitVector &set() {
281 setSmallBits(~uintptr_t(0));
287 SmallBitVector &set(unsigned Idx) {
289 assert(Idx <= static_cast<unsigned>(
290 std::numeric_limits<uintptr_t>::digits) &&
291 "undefined behavior");
292 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
295 getPointer()->set(Idx);
299 /// set - Efficiently set a range of bits in [I, E)
300 SmallBitVector &set(unsigned I, unsigned E) {
301 assert(I <= E && "Attempted to set backwards range!");
302 assert(E <= size() && "Attempted to set out-of-bounds range!");
303 if (I == E) return *this;
305 uintptr_t EMask = ((uintptr_t)1) << E;
306 uintptr_t IMask = ((uintptr_t)1) << I;
307 uintptr_t Mask = EMask - IMask;
308 setSmallBits(getSmallBits() | Mask);
310 getPointer()->set(I, E);
314 SmallBitVector &reset() {
318 getPointer()->reset();
322 SmallBitVector &reset(unsigned Idx) {
324 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
326 getPointer()->reset(Idx);
330 /// reset - Efficiently reset a range of bits in [I, E)
331 SmallBitVector &reset(unsigned I, unsigned E) {
332 assert(I <= E && "Attempted to reset backwards range!");
333 assert(E <= size() && "Attempted to reset out-of-bounds range!");
334 if (I == E) return *this;
336 uintptr_t EMask = ((uintptr_t)1) << E;
337 uintptr_t IMask = ((uintptr_t)1) << I;
338 uintptr_t Mask = EMask - IMask;
339 setSmallBits(getSmallBits() & ~Mask);
341 getPointer()->reset(I, E);
345 SmallBitVector &flip() {
347 setSmallBits(~getSmallBits());
349 getPointer()->flip();
353 SmallBitVector &flip(unsigned Idx) {
355 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
357 getPointer()->flip(Idx);
362 SmallBitVector operator~() const {
363 return SmallBitVector(*this).flip();
367 reference operator[](unsigned Idx) {
368 assert(Idx < size() && "Out-of-bounds Bit access.");
369 return reference(*this, Idx);
372 bool operator[](unsigned Idx) const {
373 assert(Idx < size() && "Out-of-bounds Bit access.");
375 return ((getSmallBits() >> Idx) & 1) != 0;
376 return getPointer()->operator[](Idx);
379 bool test(unsigned Idx) const {
383 /// Test if any common bits are set.
384 bool anyCommon(const SmallBitVector &RHS) const {
385 if (isSmall() && RHS.isSmall())
386 return (getSmallBits() & RHS.getSmallBits()) != 0;
387 if (!isSmall() && !RHS.isSmall())
388 return getPointer()->anyCommon(*RHS.getPointer());
390 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
391 if (test(i) && RHS.test(i))
396 // Comparison operators.
397 bool operator==(const SmallBitVector &RHS) const {
398 if (size() != RHS.size())
401 return getSmallBits() == RHS.getSmallBits();
403 return *getPointer() == *RHS.getPointer();
406 bool operator!=(const SmallBitVector &RHS) const {
407 return !(*this == RHS);
410 // Intersection, union, disjoint union.
411 SmallBitVector &operator&=(const SmallBitVector &RHS) {
412 resize(std::max(size(), RHS.size()));
414 setSmallBits(getSmallBits() & RHS.getSmallBits());
415 else if (!RHS.isSmall())
416 getPointer()->operator&=(*RHS.getPointer());
418 SmallBitVector Copy = RHS;
420 getPointer()->operator&=(*Copy.getPointer());
425 /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
426 SmallBitVector &reset(const SmallBitVector &RHS) {
427 if (isSmall() && RHS.isSmall())
428 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
429 else if (!isSmall() && !RHS.isSmall())
430 getPointer()->reset(*RHS.getPointer());
432 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
439 /// test - Check if (This - RHS) is zero.
440 /// This is the same as reset(RHS) and any().
441 bool test(const SmallBitVector &RHS) const {
442 if (isSmall() && RHS.isSmall())
443 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
444 if (!isSmall() && !RHS.isSmall())
445 return getPointer()->test(*RHS.getPointer());
448 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
449 if (test(i) && !RHS.test(i))
452 for (e = size(); i != e; ++i)
459 SmallBitVector &operator|=(const SmallBitVector &RHS) {
460 resize(std::max(size(), RHS.size()));
462 setSmallBits(getSmallBits() | RHS.getSmallBits());
463 else if (!RHS.isSmall())
464 getPointer()->operator|=(*RHS.getPointer());
466 SmallBitVector Copy = RHS;
468 getPointer()->operator|=(*Copy.getPointer());
473 SmallBitVector &operator^=(const SmallBitVector &RHS) {
474 resize(std::max(size(), RHS.size()));
476 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
477 else if (!RHS.isSmall())
478 getPointer()->operator^=(*RHS.getPointer());
480 SmallBitVector Copy = RHS;
482 getPointer()->operator^=(*Copy.getPointer());
487 // Assignment operator.
488 const SmallBitVector &operator=(const SmallBitVector &RHS) {
493 switchToLarge(new BitVector(*RHS.getPointer()));
496 *getPointer() = *RHS.getPointer();
505 const SmallBitVector &operator=(SmallBitVector &&RHS) {
513 void swap(SmallBitVector &RHS) {
517 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
518 /// This computes "*this |= Mask".
519 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
521 applyMask<true, false>(Mask, MaskWords);
523 getPointer()->setBitsInMask(Mask, MaskWords);
526 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
527 /// Don't resize. This computes "*this &= ~Mask".
528 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
530 applyMask<false, false>(Mask, MaskWords);
532 getPointer()->clearBitsInMask(Mask, MaskWords);
535 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
536 /// Don't resize. This computes "*this |= ~Mask".
537 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
539 applyMask<true, true>(Mask, MaskWords);
541 getPointer()->setBitsNotInMask(Mask, MaskWords);
544 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
545 /// Don't resize. This computes "*this &= Mask".
546 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
548 applyMask<false, true>(Mask, MaskWords);
550 getPointer()->clearBitsNotInMask(Mask, MaskWords);
554 template<bool AddBits, bool InvertMask>
555 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
556 if (NumBaseBits == 64 && MaskWords >= 2) {
557 uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
558 if (InvertMask) M = ~M;
559 if (AddBits) setSmallBits(getSmallBits() | M);
560 else setSmallBits(getSmallBits() & ~M);
562 uint32_t M = Mask[0];
563 if (InvertMask) M = ~M;
564 if (AddBits) setSmallBits(getSmallBits() | M);
565 else setSmallBits(getSmallBits() & ~M);
570 inline SmallBitVector
571 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
572 SmallBitVector Result(LHS);
577 inline SmallBitVector
578 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
579 SmallBitVector Result(LHS);
584 inline SmallBitVector
585 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
586 SmallBitVector Result(LHS);
591 } // End llvm namespace
594 /// Implement std::swap in terms of BitVector swap.
596 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {