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 // Encapsulation of a single bit.
39 friend class BitVector;
44 reference(); // Undefined
47 reference(BitVector &b, unsigned Idx) {
48 WordRef = &b.Bits[Idx / BITWORD_SIZE];
49 BitPos = Idx % BITWORD_SIZE;
54 reference &operator=(reference t) {
59 reference& operator=(bool t) {
61 *WordRef |= 1L << BitPos;
63 *WordRef &= ~(1L << BitPos);
67 operator bool() const {
68 return ((*WordRef) & (1L << BitPos)) ? true : false;
73 /// BitVector default ctor - Creates an empty bitvector.
74 BitVector() : Size(0), Capacity(0) {
78 /// BitVector ctor - Creates a bitvector of specified number of bits. All
79 /// bits are initialized to the specified value.
80 explicit BitVector(unsigned s, bool t = false) : Size(s) {
81 Capacity = NumBitWords(s);
82 Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
83 init_words(Bits, Capacity, t);
88 /// BitVector copy ctor.
89 BitVector(const BitVector &RHS) : Size(RHS.size()) {
96 Capacity = NumBitWords(RHS.size());
97 Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
98 std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
101 #if LLVM_USE_RVALUE_REFERENCES
102 BitVector(BitVector &&RHS)
103 : Bits(RHS.Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
112 /// empty - Tests whether there are no bits in this bitvector.
113 bool empty() const { return Size == 0; }
115 /// size - Returns the number of bits in this bitvector.
116 unsigned size() const { return Size; }
118 /// count - Returns the number of bits which are set.
119 unsigned count() const {
120 unsigned NumBits = 0;
121 for (unsigned i = 0; i < NumBitWords(size()); ++i)
122 if (sizeof(BitWord) == 4)
123 NumBits += CountPopulation_32((uint32_t)Bits[i]);
124 else if (sizeof(BitWord) == 8)
125 NumBits += CountPopulation_64(Bits[i]);
127 llvm_unreachable("Unsupported!");
131 /// any - Returns true if any bit is set.
133 for (unsigned i = 0; i < NumBitWords(size()); ++i)
139 /// all - Returns true if all bits are set.
141 // TODO: Optimize this.
142 return count() == size();
145 /// none - Returns true if none of the bits are set.
150 /// find_first - Returns the index of the first set bit, -1 if none
151 /// of the bits are set.
152 int find_first() const {
153 for (unsigned i = 0; i < NumBitWords(size()); ++i)
155 if (sizeof(BitWord) == 4)
156 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
157 if (sizeof(BitWord) == 8)
158 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
159 llvm_unreachable("Unsupported!");
164 /// find_next - Returns the index of the next set bit following the
165 /// "Prev" bit. Returns -1 if the next set bit is not found.
166 int find_next(unsigned Prev) const {
171 unsigned WordPos = Prev / BITWORD_SIZE;
172 unsigned BitPos = Prev % BITWORD_SIZE;
173 BitWord Copy = Bits[WordPos];
174 // Mask off previous bits.
175 Copy &= ~0UL << BitPos;
178 if (sizeof(BitWord) == 4)
179 return WordPos * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Copy);
180 if (sizeof(BitWord) == 8)
181 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
182 llvm_unreachable("Unsupported!");
185 // Check subsequent words.
186 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
188 if (sizeof(BitWord) == 4)
189 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
190 if (sizeof(BitWord) == 8)
191 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
192 llvm_unreachable("Unsupported!");
197 /// clear - Clear all bits.
202 /// resize - Grow or shrink the bitvector.
203 void resize(unsigned N, bool t = false) {
204 if (N > Capacity * BITWORD_SIZE) {
205 unsigned OldCapacity = Capacity;
207 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
210 // Set any old unused bits that are now included in the BitVector. This
211 // may set bits that are not included in the new vector, but we will clear
212 // them back out below.
216 // Update the size, and clear out any bits that are now unused
217 unsigned OldSize = Size;
219 if (t || N < OldSize)
223 void reserve(unsigned N) {
224 if (N > Capacity * BITWORD_SIZE)
230 init_words(Bits, Capacity, true);
235 BitVector &set(unsigned Idx) {
236 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
241 init_words(Bits, Capacity, false);
245 BitVector &reset(unsigned Idx) {
246 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
251 for (unsigned i = 0; i < NumBitWords(size()); ++i)
257 BitVector &flip(unsigned Idx) {
258 Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
263 reference operator[](unsigned Idx) {
264 assert (Idx < Size && "Out-of-bounds Bit access.");
265 return reference(*this, Idx);
268 bool operator[](unsigned Idx) const {
269 assert (Idx < Size && "Out-of-bounds Bit access.");
270 BitWord Mask = 1L << (Idx % BITWORD_SIZE);
271 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
274 bool test(unsigned Idx) const {
278 /// Test if any common bits are set.
279 bool anyCommon(const BitVector &RHS) const {
280 unsigned ThisWords = NumBitWords(size());
281 unsigned RHSWords = NumBitWords(RHS.size());
282 for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
283 if (Bits[i] & RHS.Bits[i])
288 // Comparison operators.
289 bool operator==(const BitVector &RHS) const {
290 unsigned ThisWords = NumBitWords(size());
291 unsigned RHSWords = NumBitWords(RHS.size());
293 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
294 if (Bits[i] != RHS.Bits[i])
297 // Verify that any extra words are all zeros.
298 if (i != ThisWords) {
299 for (; i != ThisWords; ++i)
302 } else if (i != RHSWords) {
303 for (; i != RHSWords; ++i)
310 bool operator!=(const BitVector &RHS) const {
311 return !(*this == RHS);
314 /// Intersection, union, disjoint union.
315 BitVector &operator&=(const BitVector &RHS) {
316 unsigned ThisWords = NumBitWords(size());
317 unsigned RHSWords = NumBitWords(RHS.size());
319 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
320 Bits[i] &= RHS.Bits[i];
322 // Any bits that are just in this bitvector become zero, because they aren't
323 // in the RHS bit vector. Any words only in RHS are ignored because they
324 // are already zero in the LHS.
325 for (; i != ThisWords; ++i)
331 /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
332 BitVector &reset(const BitVector &RHS) {
333 unsigned ThisWords = NumBitWords(size());
334 unsigned RHSWords = NumBitWords(RHS.size());
336 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
337 Bits[i] &= ~RHS.Bits[i];
341 /// test - Check if (This - RHS) is zero.
342 /// This is the same as reset(RHS) and any().
343 bool test(const BitVector &RHS) const {
344 unsigned ThisWords = NumBitWords(size());
345 unsigned RHSWords = NumBitWords(RHS.size());
347 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
348 if ((Bits[i] & ~RHS.Bits[i]) != 0)
351 for (; i != ThisWords ; ++i)
358 BitVector &operator|=(const BitVector &RHS) {
359 if (size() < RHS.size())
361 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
362 Bits[i] |= RHS.Bits[i];
366 BitVector &operator^=(const BitVector &RHS) {
367 if (size() < RHS.size())
369 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
370 Bits[i] ^= RHS.Bits[i];
374 // Assignment operator.
375 const BitVector &operator=(const BitVector &RHS) {
376 if (this == &RHS) return *this;
379 unsigned RHSWords = NumBitWords(Size);
380 if (Size <= Capacity * BITWORD_SIZE) {
382 std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
387 // Grow the bitvector to have enough elements.
389 BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
390 std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
392 // Destroy the old bits.
399 #if LLVM_USE_RVALUE_REFERENCES
400 const BitVector &operator=(BitVector &&RHS) {
401 if (this == &RHS) return *this;
406 Capacity = RHS.Capacity;
414 void swap(BitVector &RHS) {
415 std::swap(Bits, RHS.Bits);
416 std::swap(Size, RHS.Size);
417 std::swap(Capacity, RHS.Capacity);
420 //===--------------------------------------------------------------------===//
421 // Portable bit mask operations.
422 //===--------------------------------------------------------------------===//
424 // These methods all operate on arrays of uint32_t, each holding 32 bits. The
425 // fixed word size makes it easier to work with literal bit vector constants
428 // The LSB in each word is the lowest numbered bit. The size of a portable
429 // bit mask is always a whole multiple of 32 bits. If no bit mask size is
430 // given, the bit mask is assumed to cover the entire BitVector.
432 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
433 /// This computes "*this |= Mask".
434 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
435 applyMask<true, false>(Mask, MaskWords);
438 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
439 /// Don't resize. This computes "*this &= ~Mask".
440 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
441 applyMask<false, false>(Mask, MaskWords);
444 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
445 /// Don't resize. This computes "*this |= ~Mask".
446 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
447 applyMask<true, true>(Mask, MaskWords);
450 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
451 /// Don't resize. This computes "*this &= Mask".
452 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
453 applyMask<false, true>(Mask, MaskWords);
457 unsigned NumBitWords(unsigned S) const {
458 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
461 // Set the unused bits in the high words.
462 void set_unused_bits(bool t = true) {
463 // Set high words first.
464 unsigned UsedWords = NumBitWords(Size);
465 if (Capacity > UsedWords)
466 init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
468 // Then set any stray high bits of the last used word.
469 unsigned ExtraBits = Size % BITWORD_SIZE;
471 BitWord ExtraBitMask = ~0UL << ExtraBits;
473 Bits[UsedWords-1] |= ExtraBitMask;
475 Bits[UsedWords-1] &= ~ExtraBitMask;
479 // Clear the unused bits in the high words.
480 void clear_unused_bits() {
481 set_unused_bits(false);
484 void grow(unsigned NewSize) {
485 Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
486 Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
491 void init_words(BitWord *B, unsigned NumWords, bool t) {
492 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
495 template<bool AddBits, bool InvertMask>
496 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
497 assert(BITWORD_SIZE % 32 == 0 && "Unsupported BitWord size.");
498 MaskWords = std::min(MaskWords, (size() + 31) / 32);
499 const unsigned Scale = BITWORD_SIZE / 32;
501 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
502 BitWord BW = Bits[i];
503 // This inner loop should unroll completely when BITWORD_SIZE > 32.
504 for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
505 uint32_t M = *Mask++;
506 if (InvertMask) M = ~M;
507 if (AddBits) BW |= BitWord(M) << b;
508 else BW &= ~(BitWord(M) << b);
512 for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
513 uint32_t M = *Mask++;
514 if (InvertMask) M = ~M;
515 if (AddBits) Bits[i] |= BitWord(M) << b;
516 else Bits[i] &= ~(BitWord(M) << b);
523 } // End llvm namespace
526 /// Implement std::swap in terms of BitVector swap.
528 swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {