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