LLVM API Documentation

BitVector.h
Go to the documentation of this file.
00001 //===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the BitVector class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ADT_BITVECTOR_H
00015 #define LLVM_ADT_BITVECTOR_H
00016 
00017 #include "llvm/Support/Compiler.h"
00018 #include "llvm/Support/ErrorHandling.h"
00019 #include "llvm/Support/MathExtras.h"
00020 #include <algorithm>
00021 #include <cassert>
00022 #include <climits>
00023 #include <cstdlib>
00024 
00025 namespace llvm {
00026 
00027 class BitVector {
00028   typedef unsigned long BitWord;
00029 
00030   enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
00031 
00032   BitWord  *Bits;        // Actual bits.
00033   unsigned Size;         // Size of bitvector in bits.
00034   unsigned Capacity;     // Size of allocated memory in BitWord.
00035 
00036 public:
00037   // Encapsulation of a single bit.
00038   class reference {
00039     friend class BitVector;
00040 
00041     BitWord *WordRef;
00042     unsigned BitPos;
00043 
00044     reference();  // Undefined
00045 
00046   public:
00047     reference(BitVector &b, unsigned Idx) {
00048       WordRef = &b.Bits[Idx / BITWORD_SIZE];
00049       BitPos = Idx % BITWORD_SIZE;
00050     }
00051 
00052     ~reference() {}
00053 
00054     reference &operator=(reference t) {
00055       *this = bool(t);
00056       return *this;
00057     }
00058 
00059     reference& operator=(bool t) {
00060       if (t)
00061         *WordRef |= 1L << BitPos;
00062       else
00063         *WordRef &= ~(1L << BitPos);
00064       return *this;
00065     }
00066 
00067     operator bool() const {
00068       return ((*WordRef) & (1L << BitPos)) ? true : false;
00069     }
00070   };
00071 
00072 
00073   /// BitVector default ctor - Creates an empty bitvector.
00074   BitVector() : Size(0), Capacity(0) {
00075     Bits = 0;
00076   }
00077 
00078   /// BitVector ctor - Creates a bitvector of specified number of bits. All
00079   /// bits are initialized to the specified value.
00080   explicit BitVector(unsigned s, bool t = false) : Size(s) {
00081     Capacity = NumBitWords(s);
00082     Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
00083     init_words(Bits, Capacity, t);
00084     if (t)
00085       clear_unused_bits();
00086   }
00087 
00088   /// BitVector copy ctor.
00089   BitVector(const BitVector &RHS) : Size(RHS.size()) {
00090     if (Size == 0) {
00091       Bits = 0;
00092       Capacity = 0;
00093       return;
00094     }
00095 
00096     Capacity = NumBitWords(RHS.size());
00097     Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
00098     std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
00099   }
00100 
00101 #if LLVM_HAS_RVALUE_REFERENCES
00102   BitVector(BitVector &&RHS)
00103     : Bits(RHS.Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
00104     RHS.Bits = 0;
00105   }
00106 #endif
00107 
00108   ~BitVector() {
00109     std::free(Bits);
00110   }
00111 
00112   /// empty - Tests whether there are no bits in this bitvector.
00113   bool empty() const { return Size == 0; }
00114 
00115   /// size - Returns the number of bits in this bitvector.
00116   unsigned size() const { return Size; }
00117 
00118   /// count - Returns the number of bits which are set.
00119   unsigned count() const {
00120     unsigned NumBits = 0;
00121     for (unsigned i = 0; i < NumBitWords(size()); ++i)
00122       if (sizeof(BitWord) == 4)
00123         NumBits += CountPopulation_32((uint32_t)Bits[i]);
00124       else if (sizeof(BitWord) == 8)
00125         NumBits += CountPopulation_64(Bits[i]);
00126       else
00127         llvm_unreachable("Unsupported!");
00128     return NumBits;
00129   }
00130 
00131   /// any - Returns true if any bit is set.
00132   bool any() const {
00133     for (unsigned i = 0; i < NumBitWords(size()); ++i)
00134       if (Bits[i] != 0)
00135         return true;
00136     return false;
00137   }
00138 
00139   /// all - Returns true if all bits are set.
00140   bool all() const {
00141     // TODO: Optimize this.
00142     return count() == size();
00143   }
00144 
00145   /// none - Returns true if none of the bits are set.
00146   bool none() const {
00147     return !any();
00148   }
00149 
00150   /// find_first - Returns the index of the first set bit, -1 if none
00151   /// of the bits are set.
00152   int find_first() const {
00153     for (unsigned i = 0; i < NumBitWords(size()); ++i)
00154       if (Bits[i] != 0) {
00155         if (sizeof(BitWord) == 4)
00156           return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
00157         if (sizeof(BitWord) == 8)
00158           return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
00159         llvm_unreachable("Unsupported!");
00160       }
00161     return -1;
00162   }
00163 
00164   /// find_next - Returns the index of the next set bit following the
00165   /// "Prev" bit. Returns -1 if the next set bit is not found.
00166   int find_next(unsigned Prev) const {
00167     ++Prev;
00168     if (Prev >= Size)
00169       return -1;
00170 
00171     unsigned WordPos = Prev / BITWORD_SIZE;
00172     unsigned BitPos = Prev % BITWORD_SIZE;
00173     BitWord Copy = Bits[WordPos];
00174     // Mask off previous bits.
00175     Copy &= ~0UL << BitPos;
00176 
00177     if (Copy != 0) {
00178       if (sizeof(BitWord) == 4)
00179         return WordPos * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Copy);
00180       if (sizeof(BitWord) == 8)
00181         return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
00182       llvm_unreachable("Unsupported!");
00183     }
00184 
00185     // Check subsequent words.
00186     for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
00187       if (Bits[i] != 0) {
00188         if (sizeof(BitWord) == 4)
00189           return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
00190         if (sizeof(BitWord) == 8)
00191           return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
00192         llvm_unreachable("Unsupported!");
00193       }
00194     return -1;
00195   }
00196 
00197   /// clear - Clear all bits.
00198   void clear() {
00199     Size = 0;
00200   }
00201 
00202   /// resize - Grow or shrink the bitvector.
00203   void resize(unsigned N, bool t = false) {
00204     if (N > Capacity * BITWORD_SIZE) {
00205       unsigned OldCapacity = Capacity;
00206       grow(N);
00207       init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
00208     }
00209 
00210     // Set any old unused bits that are now included in the BitVector. This
00211     // may set bits that are not included in the new vector, but we will clear
00212     // them back out below.
00213     if (N > Size)
00214       set_unused_bits(t);
00215 
00216     // Update the size, and clear out any bits that are now unused
00217     unsigned OldSize = Size;
00218     Size = N;
00219     if (t || N < OldSize)
00220       clear_unused_bits();
00221   }
00222 
00223   void reserve(unsigned N) {
00224     if (N > Capacity * BITWORD_SIZE)
00225       grow(N);
00226   }
00227 
00228   // Set, reset, flip
00229   BitVector &set() {
00230     init_words(Bits, Capacity, true);
00231     clear_unused_bits();
00232     return *this;
00233   }
00234 
00235   BitVector &set(unsigned Idx) {
00236     Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
00237     return *this;
00238   }
00239 
00240   /// set - Efficiently set a range of bits in [I, E)
00241   BitVector &set(unsigned I, unsigned E) {
00242     assert(I <= E && "Attempted to set backwards range!");
00243     assert(E <= size() && "Attempted to set out-of-bounds range!");
00244 
00245     if (I == E) return *this;
00246 
00247     if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
00248       BitWord EMask = 1UL << (E % BITWORD_SIZE);
00249       BitWord IMask = 1UL << (I % BITWORD_SIZE);
00250       BitWord Mask = EMask - IMask;
00251       Bits[I / BITWORD_SIZE] |= Mask;
00252       return *this;
00253     }
00254 
00255     BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
00256     Bits[I / BITWORD_SIZE] |= PrefixMask;
00257     I = RoundUpToAlignment(I, BITWORD_SIZE);
00258 
00259     for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
00260       Bits[I / BITWORD_SIZE] = ~0UL;
00261 
00262     BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
00263     Bits[I / BITWORD_SIZE] |= PostfixMask;
00264 
00265     return *this;
00266   }
00267 
00268   BitVector &reset() {
00269     init_words(Bits, Capacity, false);
00270     return *this;
00271   }
00272 
00273   BitVector &reset(unsigned Idx) {
00274     Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
00275     return *this;
00276   }
00277 
00278   /// reset - Efficiently reset a range of bits in [I, E)
00279   BitVector &reset(unsigned I, unsigned E) {
00280     assert(I <= E && "Attempted to reset backwards range!");
00281     assert(E <= size() && "Attempted to reset out-of-bounds range!");
00282 
00283     if (I == E) return *this;
00284 
00285     if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
00286       BitWord EMask = 1UL << (E % BITWORD_SIZE);
00287       BitWord IMask = 1UL << (I % BITWORD_SIZE);
00288       BitWord Mask = EMask - IMask;
00289       Bits[I / BITWORD_SIZE] &= ~Mask;
00290       return *this;
00291     }
00292 
00293     BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
00294     Bits[I / BITWORD_SIZE] &= ~PrefixMask;
00295     I = RoundUpToAlignment(I, BITWORD_SIZE);
00296 
00297     for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
00298       Bits[I / BITWORD_SIZE] = 0UL;
00299 
00300     BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
00301     Bits[I / BITWORD_SIZE] &= ~PostfixMask;
00302 
00303     return *this;
00304   }
00305 
00306   BitVector &flip() {
00307     for (unsigned i = 0; i < NumBitWords(size()); ++i)
00308       Bits[i] = ~Bits[i];
00309     clear_unused_bits();
00310     return *this;
00311   }
00312 
00313   BitVector &flip(unsigned Idx) {
00314     Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
00315     return *this;
00316   }
00317 
00318   // Indexing.
00319   reference operator[](unsigned Idx) {
00320     assert (Idx < Size && "Out-of-bounds Bit access.");
00321     return reference(*this, Idx);
00322   }
00323 
00324   bool operator[](unsigned Idx) const {
00325     assert (Idx < Size && "Out-of-bounds Bit access.");
00326     BitWord Mask = 1L << (Idx % BITWORD_SIZE);
00327     return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
00328   }
00329 
00330   bool test(unsigned Idx) const {
00331     return (*this)[Idx];
00332   }
00333 
00334   /// Test if any common bits are set.
00335   bool anyCommon(const BitVector &RHS) const {
00336     unsigned ThisWords = NumBitWords(size());
00337     unsigned RHSWords  = NumBitWords(RHS.size());
00338     for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
00339       if (Bits[i] & RHS.Bits[i])
00340         return true;
00341     return false;
00342   }
00343 
00344   // Comparison operators.
00345   bool operator==(const BitVector &RHS) const {
00346     unsigned ThisWords = NumBitWords(size());
00347     unsigned RHSWords  = NumBitWords(RHS.size());
00348     unsigned i;
00349     for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
00350       if (Bits[i] != RHS.Bits[i])
00351         return false;
00352 
00353     // Verify that any extra words are all zeros.
00354     if (i != ThisWords) {
00355       for (; i != ThisWords; ++i)
00356         if (Bits[i])
00357           return false;
00358     } else if (i != RHSWords) {
00359       for (; i != RHSWords; ++i)
00360         if (RHS.Bits[i])
00361           return false;
00362     }
00363     return true;
00364   }
00365 
00366   bool operator!=(const BitVector &RHS) const {
00367     return !(*this == RHS);
00368   }
00369 
00370   /// Intersection, union, disjoint union.
00371   BitVector &operator&=(const BitVector &RHS) {
00372     unsigned ThisWords = NumBitWords(size());
00373     unsigned RHSWords  = NumBitWords(RHS.size());
00374     unsigned i;
00375     for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
00376       Bits[i] &= RHS.Bits[i];
00377 
00378     // Any bits that are just in this bitvector become zero, because they aren't
00379     // in the RHS bit vector.  Any words only in RHS are ignored because they
00380     // are already zero in the LHS.
00381     for (; i != ThisWords; ++i)
00382       Bits[i] = 0;
00383 
00384     return *this;
00385   }
00386 
00387   /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
00388   BitVector &reset(const BitVector &RHS) {
00389     unsigned ThisWords = NumBitWords(size());
00390     unsigned RHSWords  = NumBitWords(RHS.size());
00391     unsigned i;
00392     for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
00393       Bits[i] &= ~RHS.Bits[i];
00394     return *this;
00395   }
00396 
00397   /// test - Check if (This - RHS) is zero.
00398   /// This is the same as reset(RHS) and any().
00399   bool test(const BitVector &RHS) const {
00400     unsigned ThisWords = NumBitWords(size());
00401     unsigned RHSWords  = NumBitWords(RHS.size());
00402     unsigned i;
00403     for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
00404       if ((Bits[i] & ~RHS.Bits[i]) != 0)
00405         return true;
00406 
00407     for (; i != ThisWords ; ++i)
00408       if (Bits[i] != 0)
00409         return true;
00410 
00411     return false;
00412   }
00413 
00414   BitVector &operator|=(const BitVector &RHS) {
00415     if (size() < RHS.size())
00416       resize(RHS.size());
00417     for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
00418       Bits[i] |= RHS.Bits[i];
00419     return *this;
00420   }
00421 
00422   BitVector &operator^=(const BitVector &RHS) {
00423     if (size() < RHS.size())
00424       resize(RHS.size());
00425     for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
00426       Bits[i] ^= RHS.Bits[i];
00427     return *this;
00428   }
00429 
00430   // Assignment operator.
00431   const BitVector &operator=(const BitVector &RHS) {
00432     if (this == &RHS) return *this;
00433 
00434     Size = RHS.size();
00435     unsigned RHSWords = NumBitWords(Size);
00436     if (Size <= Capacity * BITWORD_SIZE) {
00437       if (Size)
00438         std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
00439       clear_unused_bits();
00440       return *this;
00441     }
00442 
00443     // Grow the bitvector to have enough elements.
00444     Capacity = RHSWords;
00445     BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
00446     std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
00447 
00448     // Destroy the old bits.
00449     std::free(Bits);
00450     Bits = NewBits;
00451 
00452     return *this;
00453   }
00454 
00455 #if LLVM_HAS_RVALUE_REFERENCES
00456   const BitVector &operator=(BitVector &&RHS) {
00457     if (this == &RHS) return *this;
00458 
00459     std::free(Bits);
00460     Bits = RHS.Bits;
00461     Size = RHS.Size;
00462     Capacity = RHS.Capacity;
00463 
00464     RHS.Bits = 0;
00465 
00466     return *this;
00467   }
00468 #endif
00469 
00470   void swap(BitVector &RHS) {
00471     std::swap(Bits, RHS.Bits);
00472     std::swap(Size, RHS.Size);
00473     std::swap(Capacity, RHS.Capacity);
00474   }
00475 
00476   //===--------------------------------------------------------------------===//
00477   // Portable bit mask operations.
00478   //===--------------------------------------------------------------------===//
00479   //
00480   // These methods all operate on arrays of uint32_t, each holding 32 bits. The
00481   // fixed word size makes it easier to work with literal bit vector constants
00482   // in portable code.
00483   //
00484   // The LSB in each word is the lowest numbered bit.  The size of a portable
00485   // bit mask is always a whole multiple of 32 bits.  If no bit mask size is
00486   // given, the bit mask is assumed to cover the entire BitVector.
00487 
00488   /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
00489   /// This computes "*this |= Mask".
00490   void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
00491     applyMask<true, false>(Mask, MaskWords);
00492   }
00493 
00494   /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
00495   /// Don't resize. This computes "*this &= ~Mask".
00496   void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
00497     applyMask<false, false>(Mask, MaskWords);
00498   }
00499 
00500   /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
00501   /// Don't resize.  This computes "*this |= ~Mask".
00502   void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
00503     applyMask<true, true>(Mask, MaskWords);
00504   }
00505 
00506   /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
00507   /// Don't resize.  This computes "*this &= Mask".
00508   void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
00509     applyMask<false, true>(Mask, MaskWords);
00510   }
00511 
00512 private:
00513   unsigned NumBitWords(unsigned S) const {
00514     return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
00515   }
00516 
00517   // Set the unused bits in the high words.
00518   void set_unused_bits(bool t = true) {
00519     //  Set high words first.
00520     unsigned UsedWords = NumBitWords(Size);
00521     if (Capacity > UsedWords)
00522       init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
00523 
00524     //  Then set any stray high bits of the last used word.
00525     unsigned ExtraBits = Size % BITWORD_SIZE;
00526     if (ExtraBits) {
00527       BitWord ExtraBitMask = ~0UL << ExtraBits;
00528       if (t)
00529         Bits[UsedWords-1] |= ExtraBitMask;
00530       else
00531         Bits[UsedWords-1] &= ~ExtraBitMask;
00532     }
00533   }
00534 
00535   // Clear the unused bits in the high words.
00536   void clear_unused_bits() {
00537     set_unused_bits(false);
00538   }
00539 
00540   void grow(unsigned NewSize) {
00541     Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
00542     Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
00543 
00544     clear_unused_bits();
00545   }
00546 
00547   void init_words(BitWord *B, unsigned NumWords, bool t) {
00548     memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
00549   }
00550 
00551   template<bool AddBits, bool InvertMask>
00552   void applyMask(const uint32_t *Mask, unsigned MaskWords) {
00553     assert(BITWORD_SIZE % 32 == 0 && "Unsupported BitWord size.");
00554     MaskWords = std::min(MaskWords, (size() + 31) / 32);
00555     const unsigned Scale = BITWORD_SIZE / 32;
00556     unsigned i;
00557     for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
00558       BitWord BW = Bits[i];
00559       // This inner loop should unroll completely when BITWORD_SIZE > 32.
00560       for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
00561         uint32_t M = *Mask++;
00562         if (InvertMask) M = ~M;
00563         if (AddBits) BW |=   BitWord(M) << b;
00564         else         BW &= ~(BitWord(M) << b);
00565       }
00566       Bits[i] = BW;
00567     }
00568     for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
00569       uint32_t M = *Mask++;
00570       if (InvertMask) M = ~M;
00571       if (AddBits) Bits[i] |=   BitWord(M) << b;
00572       else         Bits[i] &= ~(BitWord(M) << b);
00573     }
00574     if (AddBits)
00575       clear_unused_bits();
00576   }
00577 };
00578 
00579 } // End llvm namespace
00580 
00581 namespace std {
00582   /// Implement std::swap in terms of BitVector swap.
00583   inline void
00584   swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {
00585     LHS.swap(RHS);
00586   }
00587 }
00588 
00589 #endif