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 next set bit following the "Prev" bit.
220 /// Returns -1 if the next set bit is not found.
221 int find_next(unsigned Prev) const {
223 uintptr_t Bits = getSmallBits();
224 // Mask off previous bits.
225 Bits &= ~uintptr_t(0) << (Prev + 1);
226 if (Bits == 0 || Prev + 1 >= getSmallSize())
228 return countTrailingZeros(Bits);
230 return getPointer()->find_next(Prev);
240 /// Grow or shrink the bitvector.
241 void resize(unsigned N, bool t = false) {
243 getPointer()->resize(N, t);
244 } else if (SmallNumDataBits >= N) {
245 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
247 setSmallBits(NewBits | getSmallBits());
249 BitVector *BV = new BitVector(N, t);
250 uintptr_t OldBits = getSmallBits();
251 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
252 (*BV)[i] = (OldBits >> i) & 1;
257 void reserve(unsigned N) {
259 if (N > SmallNumDataBits) {
260 uintptr_t OldBits = getSmallRawBits();
261 size_t SmallSize = getSmallSize();
262 BitVector *BV = new BitVector(SmallSize);
263 for (size_t i = 0; i < SmallSize; ++i)
264 if ((OldBits >> i) & 1)
270 getPointer()->reserve(N);
275 SmallBitVector &set() {
277 setSmallBits(~uintptr_t(0));
283 SmallBitVector &set(unsigned Idx) {
285 assert(Idx <= static_cast<unsigned>(
286 std::numeric_limits<uintptr_t>::digits) &&
287 "undefined behavior");
288 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
291 getPointer()->set(Idx);
295 /// Efficiently set a range of bits in [I, E)
296 SmallBitVector &set(unsigned I, unsigned E) {
297 assert(I <= E && "Attempted to set backwards range!");
298 assert(E <= size() && "Attempted to set out-of-bounds range!");
299 if (I == E) return *this;
301 uintptr_t EMask = ((uintptr_t)1) << E;
302 uintptr_t IMask = ((uintptr_t)1) << I;
303 uintptr_t Mask = EMask - IMask;
304 setSmallBits(getSmallBits() | Mask);
306 getPointer()->set(I, E);
310 SmallBitVector &reset() {
314 getPointer()->reset();
318 SmallBitVector &reset(unsigned Idx) {
320 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
322 getPointer()->reset(Idx);
326 /// Efficiently reset a range of bits in [I, E)
327 SmallBitVector &reset(unsigned I, unsigned E) {
328 assert(I <= E && "Attempted to reset backwards range!");
329 assert(E <= size() && "Attempted to reset out-of-bounds range!");
330 if (I == E) return *this;
332 uintptr_t EMask = ((uintptr_t)1) << E;
333 uintptr_t IMask = ((uintptr_t)1) << I;
334 uintptr_t Mask = EMask - IMask;
335 setSmallBits(getSmallBits() & ~Mask);
337 getPointer()->reset(I, E);
341 SmallBitVector &flip() {
343 setSmallBits(~getSmallBits());
345 getPointer()->flip();
349 SmallBitVector &flip(unsigned Idx) {
351 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
353 getPointer()->flip(Idx);
358 SmallBitVector operator~() const {
359 return SmallBitVector(*this).flip();
363 reference operator[](unsigned Idx) {
364 assert(Idx < size() && "Out-of-bounds Bit access.");
365 return reference(*this, Idx);
368 bool operator[](unsigned Idx) const {
369 assert(Idx < size() && "Out-of-bounds Bit access.");
371 return ((getSmallBits() >> Idx) & 1) != 0;
372 return getPointer()->operator[](Idx);
375 bool test(unsigned Idx) const {
379 /// Test if any common bits are set.
380 bool anyCommon(const SmallBitVector &RHS) const {
381 if (isSmall() && RHS.isSmall())
382 return (getSmallBits() & RHS.getSmallBits()) != 0;
383 if (!isSmall() && !RHS.isSmall())
384 return getPointer()->anyCommon(*RHS.getPointer());
386 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
387 if (test(i) && RHS.test(i))
392 // Comparison operators.
393 bool operator==(const SmallBitVector &RHS) const {
394 if (size() != RHS.size())
397 return getSmallBits() == RHS.getSmallBits();
399 return *getPointer() == *RHS.getPointer();
402 bool operator!=(const SmallBitVector &RHS) const {
403 return !(*this == RHS);
406 // Intersection, union, disjoint union.
407 SmallBitVector &operator&=(const SmallBitVector &RHS) {
408 resize(std::max(size(), RHS.size()));
410 setSmallBits(getSmallBits() & RHS.getSmallBits());
411 else if (!RHS.isSmall())
412 getPointer()->operator&=(*RHS.getPointer());
414 SmallBitVector Copy = RHS;
416 getPointer()->operator&=(*Copy.getPointer());
421 /// Reset bits that are set in RHS. Same as *this &= ~RHS.
422 SmallBitVector &reset(const SmallBitVector &RHS) {
423 if (isSmall() && RHS.isSmall())
424 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
425 else if (!isSmall() && !RHS.isSmall())
426 getPointer()->reset(*RHS.getPointer());
428 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
435 /// Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
436 bool test(const SmallBitVector &RHS) const {
437 if (isSmall() && RHS.isSmall())
438 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
439 if (!isSmall() && !RHS.isSmall())
440 return getPointer()->test(*RHS.getPointer());
443 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
444 if (test(i) && !RHS.test(i))
447 for (e = size(); i != e; ++i)
454 SmallBitVector &operator|=(const SmallBitVector &RHS) {
455 resize(std::max(size(), RHS.size()));
457 setSmallBits(getSmallBits() | RHS.getSmallBits());
458 else if (!RHS.isSmall())
459 getPointer()->operator|=(*RHS.getPointer());
461 SmallBitVector Copy = RHS;
463 getPointer()->operator|=(*Copy.getPointer());
468 SmallBitVector &operator^=(const SmallBitVector &RHS) {
469 resize(std::max(size(), RHS.size()));
471 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
472 else if (!RHS.isSmall())
473 getPointer()->operator^=(*RHS.getPointer());
475 SmallBitVector Copy = RHS;
477 getPointer()->operator^=(*Copy.getPointer());
482 // Assignment operator.
483 const SmallBitVector &operator=(const SmallBitVector &RHS) {
488 switchToLarge(new BitVector(*RHS.getPointer()));
491 *getPointer() = *RHS.getPointer();
500 const SmallBitVector &operator=(SmallBitVector &&RHS) {
508 void swap(SmallBitVector &RHS) {
512 /// Add '1' bits from Mask to this vector. Don't resize.
513 /// This computes "*this |= Mask".
514 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
516 applyMask<true, false>(Mask, MaskWords);
518 getPointer()->setBitsInMask(Mask, MaskWords);
521 /// Clear any bits in this vector that are set in Mask. Don't resize.
522 /// This computes "*this &= ~Mask".
523 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
525 applyMask<false, false>(Mask, MaskWords);
527 getPointer()->clearBitsInMask(Mask, MaskWords);
530 /// Add a bit to this vector for every '0' bit in Mask. Don't resize.
531 /// This computes "*this |= ~Mask".
532 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
534 applyMask<true, true>(Mask, MaskWords);
536 getPointer()->setBitsNotInMask(Mask, MaskWords);
539 /// Clear a bit in this vector for every '0' bit in Mask. Don't resize.
540 /// This computes "*this &= Mask".
541 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
543 applyMask<false, true>(Mask, MaskWords);
545 getPointer()->clearBitsNotInMask(Mask, MaskWords);
549 template <bool AddBits, bool InvertMask>
550 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
551 assert(MaskWords <= sizeof(uintptr_t) && "Mask is larger than base!");
552 uintptr_t M = Mask[0];
553 if (NumBaseBits == 64)
554 M |= uint64_t(Mask[1]) << 32;
558 setSmallBits(getSmallBits() | M);
560 setSmallBits(getSmallBits() & ~M);
564 inline SmallBitVector
565 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
566 SmallBitVector Result(LHS);
571 inline SmallBitVector
572 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
573 SmallBitVector Result(LHS);
578 inline SmallBitVector
579 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
580 SmallBitVector Result(LHS);
585 } // End llvm namespace
588 /// Implement std::swap in terms of BitVector swap.
590 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {