1 //===- llvm/ADT/BitVector.h - 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 BitVector class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_BITVECTOR_H
15 #define LLVM_ADT_BITVECTOR_H
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/MathExtras.h"
28 typedef unsigned long BitWord;
30 enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
32 BitWord *Bits; // Actual bits.
33 unsigned Size; // Size of bitvector in bits.
34 unsigned Capacity; // Size of allocated memory in BitWord.
37 typedef unsigned size_type;
38 // Encapsulation of a single bit.
40 friend class BitVector;
45 reference(); // Undefined
48 reference(BitVector &b, unsigned Idx) {
49 WordRef = &b.Bits[Idx / BITWORD_SIZE];
50 BitPos = Idx % BITWORD_SIZE;
55 reference &operator=(reference t) {
60 reference& operator=(bool t) {
62 *WordRef |= BitWord(1) << BitPos;
64 *WordRef &= ~(BitWord(1) << BitPos);
68 operator bool() const {
69 return ((*WordRef) & (BitWord(1) << BitPos)) ? true : false;
74 /// BitVector default ctor - Creates an empty bitvector.
75 BitVector() : Size(0), Capacity(0) {
79 /// BitVector ctor - Creates a bitvector of specified number of bits. All
80 /// bits are initialized to the specified value.
81 explicit BitVector(unsigned s, bool t = false) : Size(s) {
82 Capacity = NumBitWords(s);
83 Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
84 init_words(Bits, Capacity, t);
89 /// BitVector copy ctor.
90 BitVector(const BitVector &RHS) : Size(RHS.size()) {
97 Capacity = NumBitWords(RHS.size());
98 Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
99 std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
102 BitVector(BitVector &&RHS)
103 : Bits(RHS.Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
111 /// empty - Tests whether there are no bits in this bitvector.
112 bool empty() const { return Size == 0; }
114 /// size - Returns the number of bits in this bitvector.
115 size_type size() const { return Size; }
117 /// count - Returns the number of bits which are set.
118 size_type count() const {
119 unsigned NumBits = 0;
120 for (unsigned i = 0; i < NumBitWords(size()); ++i)
121 if (sizeof(BitWord) == 4)
122 NumBits += CountPopulation_32((uint32_t)Bits[i]);
123 else if (sizeof(BitWord) == 8)
124 NumBits += CountPopulation_64(Bits[i]);
126 llvm_unreachable("Unsupported!");
130 /// any - Returns true if any bit is set.
132 for (unsigned i = 0; i < NumBitWords(size()); ++i)
138 /// all - Returns true if all bits are set.
140 for (unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
144 // If bits remain check that they are ones. The unused bits are always zero.
145 if (unsigned Remainder = Size % BITWORD_SIZE)
146 return Bits[Size / BITWORD_SIZE] == (1UL << Remainder) - 1;
151 /// none - Returns true if none of the bits are set.
156 /// find_first - Returns the index of the first set bit, -1 if none
157 /// of the bits are set.
158 int find_first() const {
159 for (unsigned i = 0; i < NumBitWords(size()); ++i)
161 if (sizeof(BitWord) == 4)
162 return i * BITWORD_SIZE + countTrailingZeros((uint32_t)Bits[i]);
163 if (sizeof(BitWord) == 8)
164 return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
165 llvm_unreachable("Unsupported!");
170 /// find_next - Returns the index of the next set bit following the
171 /// "Prev" bit. Returns -1 if the next set bit is not found.
172 int find_next(unsigned Prev) const {
177 unsigned WordPos = Prev / BITWORD_SIZE;
178 unsigned BitPos = Prev % BITWORD_SIZE;
179 BitWord Copy = Bits[WordPos];
180 // Mask off previous bits.
181 Copy &= ~0UL << BitPos;
184 if (sizeof(BitWord) == 4)
185 return WordPos * BITWORD_SIZE + countTrailingZeros((uint32_t)Copy);
186 if (sizeof(BitWord) == 8)
187 return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
188 llvm_unreachable("Unsupported!");
191 // Check subsequent words.
192 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
194 if (sizeof(BitWord) == 4)
195 return i * BITWORD_SIZE + countTrailingZeros((uint32_t)Bits[i]);
196 if (sizeof(BitWord) == 8)
197 return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
198 llvm_unreachable("Unsupported!");
203 /// clear - Clear all bits.
208 /// resize - Grow or shrink the bitvector.
209 void resize(unsigned N, bool t = false) {
210 if (N > Capacity * BITWORD_SIZE) {
211 unsigned OldCapacity = Capacity;
213 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
216 // Set any old unused bits that are now included in the BitVector. This
217 // may set bits that are not included in the new vector, but we will clear
218 // them back out below.
222 // Update the size, and clear out any bits that are now unused
223 unsigned OldSize = Size;
225 if (t || N < OldSize)
229 void reserve(unsigned N) {
230 if (N > Capacity * BITWORD_SIZE)
236 init_words(Bits, Capacity, true);
241 BitVector &set(unsigned Idx) {
242 assert(Bits && "Bits never allocated");
243 Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
247 /// set - Efficiently set a range of bits in [I, E)
248 BitVector &set(unsigned I, unsigned E) {
249 assert(I <= E && "Attempted to set backwards range!");
250 assert(E <= size() && "Attempted to set out-of-bounds range!");
252 if (I == E) return *this;
254 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
255 BitWord EMask = 1UL << (E % BITWORD_SIZE);
256 BitWord IMask = 1UL << (I % BITWORD_SIZE);
257 BitWord Mask = EMask - IMask;
258 Bits[I / BITWORD_SIZE] |= Mask;
262 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
263 Bits[I / BITWORD_SIZE] |= PrefixMask;
264 I = RoundUpToAlignment(I, BITWORD_SIZE);
266 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
267 Bits[I / BITWORD_SIZE] = ~0UL;
269 BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
271 Bits[I / BITWORD_SIZE] |= PostfixMask;
277 init_words(Bits, Capacity, false);
281 BitVector &reset(unsigned Idx) {
282 Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
286 /// reset - Efficiently reset a range of bits in [I, E)
287 BitVector &reset(unsigned I, unsigned E) {
288 assert(I <= E && "Attempted to reset backwards range!");
289 assert(E <= size() && "Attempted to reset out-of-bounds range!");
291 if (I == E) return *this;
293 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
294 BitWord EMask = 1UL << (E % BITWORD_SIZE);
295 BitWord IMask = 1UL << (I % BITWORD_SIZE);
296 BitWord Mask = EMask - IMask;
297 Bits[I / BITWORD_SIZE] &= ~Mask;
301 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
302 Bits[I / BITWORD_SIZE] &= ~PrefixMask;
303 I = RoundUpToAlignment(I, BITWORD_SIZE);
305 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
306 Bits[I / BITWORD_SIZE] = 0UL;
308 BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
310 Bits[I / BITWORD_SIZE] &= ~PostfixMask;
316 for (unsigned i = 0; i < NumBitWords(size()); ++i)
322 BitVector &flip(unsigned Idx) {
323 Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
328 reference operator[](unsigned Idx) {
329 assert (Idx < Size && "Out-of-bounds Bit access.");
330 return reference(*this, Idx);
333 bool operator[](unsigned Idx) const {
334 assert (Idx < Size && "Out-of-bounds Bit access.");
335 BitWord Mask = BitWord(1) << (Idx % BITWORD_SIZE);
336 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
339 bool test(unsigned Idx) const {
343 /// Test if any common bits are set.
344 bool anyCommon(const BitVector &RHS) const {
345 unsigned ThisWords = NumBitWords(size());
346 unsigned RHSWords = NumBitWords(RHS.size());
347 for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
348 if (Bits[i] & RHS.Bits[i])
353 // Comparison operators.
354 bool operator==(const BitVector &RHS) const {
355 unsigned ThisWords = NumBitWords(size());
356 unsigned RHSWords = NumBitWords(RHS.size());
358 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
359 if (Bits[i] != RHS.Bits[i])
362 // Verify that any extra words are all zeros.
363 if (i != ThisWords) {
364 for (; i != ThisWords; ++i)
367 } else if (i != RHSWords) {
368 for (; i != RHSWords; ++i)
375 bool operator!=(const BitVector &RHS) const {
376 return !(*this == RHS);
379 /// Intersection, union, disjoint union.
380 BitVector &operator&=(const BitVector &RHS) {
381 unsigned ThisWords = NumBitWords(size());
382 unsigned RHSWords = NumBitWords(RHS.size());
384 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
385 Bits[i] &= RHS.Bits[i];
387 // Any bits that are just in this bitvector become zero, because they aren't
388 // in the RHS bit vector. Any words only in RHS are ignored because they
389 // are already zero in the LHS.
390 for (; i != ThisWords; ++i)
396 /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
397 BitVector &reset(const BitVector &RHS) {
398 unsigned ThisWords = NumBitWords(size());
399 unsigned RHSWords = NumBitWords(RHS.size());
401 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
402 Bits[i] &= ~RHS.Bits[i];
406 /// test - Check if (This - RHS) is zero.
407 /// This is the same as reset(RHS) and any().
408 bool test(const BitVector &RHS) const {
409 unsigned ThisWords = NumBitWords(size());
410 unsigned RHSWords = NumBitWords(RHS.size());
412 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
413 if ((Bits[i] & ~RHS.Bits[i]) != 0)
416 for (; i != ThisWords ; ++i)
423 BitVector &operator|=(const BitVector &RHS) {
424 if (size() < RHS.size())
426 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
427 Bits[i] |= RHS.Bits[i];
431 BitVector &operator^=(const BitVector &RHS) {
432 if (size() < RHS.size())
434 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
435 Bits[i] ^= RHS.Bits[i];
439 // Assignment operator.
440 const BitVector &operator=(const BitVector &RHS) {
441 if (this == &RHS) return *this;
444 unsigned RHSWords = NumBitWords(Size);
445 if (Size <= Capacity * BITWORD_SIZE) {
447 std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
452 // Grow the bitvector to have enough elements.
454 assert(Capacity > 0 && "negative capacity?");
455 BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
456 std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
458 // Destroy the old bits.
465 const BitVector &operator=(BitVector &&RHS) {
466 if (this == &RHS) return *this;
471 Capacity = RHS.Capacity;
478 void swap(BitVector &RHS) {
479 std::swap(Bits, RHS.Bits);
480 std::swap(Size, RHS.Size);
481 std::swap(Capacity, RHS.Capacity);
484 //===--------------------------------------------------------------------===//
485 // Portable bit mask operations.
486 //===--------------------------------------------------------------------===//
488 // These methods all operate on arrays of uint32_t, each holding 32 bits. The
489 // fixed word size makes it easier to work with literal bit vector constants
492 // The LSB in each word is the lowest numbered bit. The size of a portable
493 // bit mask is always a whole multiple of 32 bits. If no bit mask size is
494 // given, the bit mask is assumed to cover the entire BitVector.
496 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
497 /// This computes "*this |= Mask".
498 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
499 applyMask<true, false>(Mask, MaskWords);
502 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
503 /// Don't resize. This computes "*this &= ~Mask".
504 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
505 applyMask<false, false>(Mask, MaskWords);
508 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
509 /// Don't resize. This computes "*this |= ~Mask".
510 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
511 applyMask<true, true>(Mask, MaskWords);
514 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
515 /// Don't resize. This computes "*this &= Mask".
516 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
517 applyMask<false, true>(Mask, MaskWords);
521 unsigned NumBitWords(unsigned S) const {
522 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
525 // Set the unused bits in the high words.
526 void set_unused_bits(bool t = true) {
527 // Set high words first.
528 unsigned UsedWords = NumBitWords(Size);
529 if (Capacity > UsedWords)
530 init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
532 // Then set any stray high bits of the last used word.
533 unsigned ExtraBits = Size % BITWORD_SIZE;
535 BitWord ExtraBitMask = ~0UL << ExtraBits;
537 Bits[UsedWords-1] |= ExtraBitMask;
539 Bits[UsedWords-1] &= ~ExtraBitMask;
543 // Clear the unused bits in the high words.
544 void clear_unused_bits() {
545 set_unused_bits(false);
548 void grow(unsigned NewSize) {
549 Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
550 assert(Capacity > 0 && "realloc-ing zero space");
551 Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
556 void init_words(BitWord *B, unsigned NumWords, bool t) {
557 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
560 template<bool AddBits, bool InvertMask>
561 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
562 assert(BITWORD_SIZE % 32 == 0 && "Unsupported BitWord size.");
563 MaskWords = std::min(MaskWords, (size() + 31) / 32);
564 const unsigned Scale = BITWORD_SIZE / 32;
566 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
567 BitWord BW = Bits[i];
568 // This inner loop should unroll completely when BITWORD_SIZE > 32.
569 for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
570 uint32_t M = *Mask++;
571 if (InvertMask) M = ~M;
572 if (AddBits) BW |= BitWord(M) << b;
573 else BW &= ~(BitWord(M) << b);
577 for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
578 uint32_t M = *Mask++;
579 if (InvertMask) M = ~M;
580 if (AddBits) Bits[i] |= BitWord(M) << b;
581 else Bits[i] &= ~(BitWord(M) << b);
588 } // End llvm namespace
591 /// Implement std::swap in terms of BitVector swap.
593 swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {