Home | History | Annotate | Download | only in ADT
      1 //===- llvm/ADT/SmallBitVector.h - 'Normally small' bit vectors -*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements the SmallBitVector class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_ADT_SMALLBITVECTOR_H
     15 #define LLVM_ADT_SMALLBITVECTOR_H
     16 
     17 #include "llvm/ADT/BitVector.h"
     18 #include "llvm/Support/Compiler.h"
     19 #include "llvm/Support/MathExtras.h"
     20 #include <cassert>
     21 
     22 namespace llvm {
     23 
     24 /// SmallBitVector - This is a 'bitvector' (really, a variable-sized bit array),
     25 /// optimized for the case when the array is small.  It contains one
     26 /// pointer-sized field, which is directly used as a plain collection of bits
     27 /// when possible, or as a pointer to a larger heap-allocated array when
     28 /// necessary.  This allows normal "small" cases to be fast without losing
     29 /// generality for large inputs.
     30 ///
     31 class SmallBitVector {
     32   // TODO: In "large" mode, a pointer to a BitVector is used, leading to an
     33   // unnecessary level of indirection. It would be more efficient to use a
     34   // pointer to memory containing size, allocation size, and the array of bits.
     35   uintptr_t X;
     36 
     37   enum {
     38     // The number of bits in this class.
     39     NumBaseBits = sizeof(uintptr_t) * CHAR_BIT,
     40 
     41     // One bit is used to discriminate between small and large mode. The
     42     // remaining bits are used for the small-mode representation.
     43     SmallNumRawBits = NumBaseBits - 1,
     44 
     45     // A few more bits are used to store the size of the bit set in small mode.
     46     // Theoretically this is a ceil-log2. These bits are encoded in the most
     47     // significant bits of the raw bits.
     48     SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
     49                         NumBaseBits == 64 ? 6 :
     50                         SmallNumRawBits),
     51 
     52     // The remaining bits are used to store the actual set in small mode.
     53     SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
     54   };
     55 
     56 public:
     57   typedef unsigned size_type;
     58   // Encapsulation of a single bit.
     59   class reference {
     60     SmallBitVector &TheVector;
     61     unsigned BitPos;
     62 
     63   public:
     64     reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
     65 
     66     reference& operator=(reference t) {
     67       *this = bool(t);
     68       return *this;
     69     }
     70 
     71     reference& operator=(bool t) {
     72       if (t)
     73         TheVector.set(BitPos);
     74       else
     75         TheVector.reset(BitPos);
     76       return *this;
     77     }
     78 
     79     operator bool() const {
     80       return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
     81     }
     82   };
     83 
     84 private:
     85   bool isSmall() const {
     86     return X & uintptr_t(1);
     87   }
     88 
     89   BitVector *getPointer() const {
     90     assert(!isSmall());
     91     return reinterpret_cast<BitVector *>(X);
     92   }
     93 
     94   void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
     95     X = 1;
     96     setSmallSize(NewSize);
     97     setSmallBits(NewSmallBits);
     98   }
     99 
    100   void switchToLarge(BitVector *BV) {
    101     X = reinterpret_cast<uintptr_t>(BV);
    102     assert(!isSmall() && "Tried to use an unaligned pointer");
    103   }
    104 
    105   // Return all the bits used for the "small" representation; this includes
    106   // bits for the size as well as the element bits.
    107   uintptr_t getSmallRawBits() const {
    108     assert(isSmall());
    109     return X >> 1;
    110   }
    111 
    112   void setSmallRawBits(uintptr_t NewRawBits) {
    113     assert(isSmall());
    114     X = (NewRawBits << 1) | uintptr_t(1);
    115   }
    116 
    117   // Return the size.
    118   size_t getSmallSize() const {
    119     return getSmallRawBits() >> SmallNumDataBits;
    120   }
    121 
    122   void setSmallSize(size_t Size) {
    123     setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
    124   }
    125 
    126   // Return the element bits.
    127   uintptr_t getSmallBits() const {
    128     return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
    129   }
    130 
    131   void setSmallBits(uintptr_t NewBits) {
    132     setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
    133                     (getSmallSize() << SmallNumDataBits));
    134   }
    135 
    136 public:
    137   /// SmallBitVector default ctor - Creates an empty bitvector.
    138   SmallBitVector() : X(1) {}
    139 
    140   /// SmallBitVector ctor - Creates a bitvector of specified number of bits. All
    141   /// bits are initialized to the specified value.
    142   explicit SmallBitVector(unsigned s, bool t = false) {
    143     if (s <= SmallNumDataBits)
    144       switchToSmall(t ? ~uintptr_t(0) : 0, s);
    145     else
    146       switchToLarge(new BitVector(s, t));
    147   }
    148 
    149   /// SmallBitVector copy ctor.
    150   SmallBitVector(const SmallBitVector &RHS) {
    151     if (RHS.isSmall())
    152       X = RHS.X;
    153     else
    154       switchToLarge(new BitVector(*RHS.getPointer()));
    155   }
    156 
    157   SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
    158     RHS.X = 1;
    159   }
    160 
    161   ~SmallBitVector() {
    162     if (!isSmall())
    163       delete getPointer();
    164   }
    165 
    166   /// empty - Tests whether there are no bits in this bitvector.
    167   bool empty() const {
    168     return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
    169   }
    170 
    171   /// size - Returns the number of bits in this bitvector.
    172   size_t size() const {
    173     return isSmall() ? getSmallSize() : getPointer()->size();
    174   }
    175 
    176   /// count - Returns the number of bits which are set.
    177   size_type count() const {
    178     if (isSmall()) {
    179       uintptr_t Bits = getSmallBits();
    180       if (NumBaseBits == 32)
    181         return CountPopulation_32(Bits);
    182       if (NumBaseBits == 64)
    183         return CountPopulation_64(Bits);
    184       llvm_unreachable("Unsupported!");
    185     }
    186     return getPointer()->count();
    187   }
    188 
    189   /// any - Returns true if any bit is set.
    190   bool any() const {
    191     if (isSmall())
    192       return getSmallBits() != 0;
    193     return getPointer()->any();
    194   }
    195 
    196   /// all - Returns true if all bits are set.
    197   bool all() const {
    198     if (isSmall())
    199       return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
    200     return getPointer()->all();
    201   }
    202 
    203   /// none - Returns true if none of the bits are set.
    204   bool none() const {
    205     if (isSmall())
    206       return getSmallBits() == 0;
    207     return getPointer()->none();
    208   }
    209 
    210   /// find_first - Returns the index of the first set bit, -1 if none
    211   /// of the bits are set.
    212   int find_first() const {
    213     if (isSmall()) {
    214       uintptr_t Bits = getSmallBits();
    215       if (Bits == 0)
    216         return -1;
    217       if (NumBaseBits == 32)
    218         return countTrailingZeros(Bits);
    219       if (NumBaseBits == 64)
    220         return countTrailingZeros(Bits);
    221       llvm_unreachable("Unsupported!");
    222     }
    223     return getPointer()->find_first();
    224   }
    225 
    226   /// find_next - Returns the index of the next set bit following the
    227   /// "Prev" bit. Returns -1 if the next set bit is not found.
    228   int find_next(unsigned Prev) const {
    229     if (isSmall()) {
    230       uintptr_t Bits = getSmallBits();
    231       // Mask off previous bits.
    232       Bits &= ~uintptr_t(0) << (Prev + 1);
    233       if (Bits == 0 || Prev + 1 >= getSmallSize())
    234         return -1;
    235       if (NumBaseBits == 32)
    236         return countTrailingZeros(Bits);
    237       if (NumBaseBits == 64)
    238         return countTrailingZeros(Bits);
    239       llvm_unreachable("Unsupported!");
    240     }
    241     return getPointer()->find_next(Prev);
    242   }
    243 
    244   /// clear - Clear all bits.
    245   void clear() {
    246     if (!isSmall())
    247       delete getPointer();
    248     switchToSmall(0, 0);
    249   }
    250 
    251   /// resize - Grow or shrink the bitvector.
    252   void resize(unsigned N, bool t = false) {
    253     if (!isSmall()) {
    254       getPointer()->resize(N, t);
    255     } else if (SmallNumDataBits >= N) {
    256       uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
    257       setSmallSize(N);
    258       setSmallBits(NewBits | getSmallBits());
    259     } else {
    260       BitVector *BV = new BitVector(N, t);
    261       uintptr_t OldBits = getSmallBits();
    262       for (size_t i = 0, e = getSmallSize(); i != e; ++i)
    263         (*BV)[i] = (OldBits >> i) & 1;
    264       switchToLarge(BV);
    265     }
    266   }
    267 
    268   void reserve(unsigned N) {
    269     if (isSmall()) {
    270       if (N > SmallNumDataBits) {
    271         uintptr_t OldBits = getSmallRawBits();
    272         size_t SmallSize = getSmallSize();
    273         BitVector *BV = new BitVector(SmallSize);
    274         for (size_t i = 0; i < SmallSize; ++i)
    275           if ((OldBits >> i) & 1)
    276             BV->set(i);
    277         BV->reserve(N);
    278         switchToLarge(BV);
    279       }
    280     } else {
    281       getPointer()->reserve(N);
    282     }
    283   }
    284 
    285   // Set, reset, flip
    286   SmallBitVector &set() {
    287     if (isSmall())
    288       setSmallBits(~uintptr_t(0));
    289     else
    290       getPointer()->set();
    291     return *this;
    292   }
    293 
    294   SmallBitVector &set(unsigned Idx) {
    295     if (isSmall())
    296       setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
    297     else
    298       getPointer()->set(Idx);
    299     return *this;
    300   }
    301 
    302   /// set - Efficiently set a range of bits in [I, E)
    303   SmallBitVector &set(unsigned I, unsigned E) {
    304     assert(I <= E && "Attempted to set backwards range!");
    305     assert(E <= size() && "Attempted to set out-of-bounds range!");
    306     if (I == E) return *this;
    307     if (isSmall()) {
    308       uintptr_t EMask = ((uintptr_t)1) << E;
    309       uintptr_t IMask = ((uintptr_t)1) << I;
    310       uintptr_t Mask = EMask - IMask;
    311       setSmallBits(getSmallBits() | Mask);
    312     } else
    313       getPointer()->set(I, E);
    314     return *this;
    315   }
    316 
    317   SmallBitVector &reset() {
    318     if (isSmall())
    319       setSmallBits(0);
    320     else
    321       getPointer()->reset();
    322     return *this;
    323   }
    324 
    325   SmallBitVector &reset(unsigned Idx) {
    326     if (isSmall())
    327       setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
    328     else
    329       getPointer()->reset(Idx);
    330     return *this;
    331   }
    332 
    333   /// reset - Efficiently reset a range of bits in [I, E)
    334   SmallBitVector &reset(unsigned I, unsigned E) {
    335     assert(I <= E && "Attempted to reset backwards range!");
    336     assert(E <= size() && "Attempted to reset out-of-bounds range!");
    337     if (I == E) return *this;
    338     if (isSmall()) {
    339       uintptr_t EMask = ((uintptr_t)1) << E;
    340       uintptr_t IMask = ((uintptr_t)1) << I;
    341       uintptr_t Mask = EMask - IMask;
    342       setSmallBits(getSmallBits() & ~Mask);
    343     } else
    344       getPointer()->reset(I, E);
    345     return *this;
    346   }
    347 
    348   SmallBitVector &flip() {
    349     if (isSmall())
    350       setSmallBits(~getSmallBits());
    351     else
    352       getPointer()->flip();
    353     return *this;
    354   }
    355 
    356   SmallBitVector &flip(unsigned Idx) {
    357     if (isSmall())
    358       setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
    359     else
    360       getPointer()->flip(Idx);
    361     return *this;
    362   }
    363 
    364   // No argument flip.
    365   SmallBitVector operator~() const {
    366     return SmallBitVector(*this).flip();
    367   }
    368 
    369   // Indexing.
    370   reference operator[](unsigned Idx) {
    371     assert(Idx < size() && "Out-of-bounds Bit access.");
    372     return reference(*this, Idx);
    373   }
    374 
    375   bool operator[](unsigned Idx) const {
    376     assert(Idx < size() && "Out-of-bounds Bit access.");
    377     if (isSmall())
    378       return ((getSmallBits() >> Idx) & 1) != 0;
    379     return getPointer()->operator[](Idx);
    380   }
    381 
    382   bool test(unsigned Idx) const {
    383     return (*this)[Idx];
    384   }
    385 
    386   /// Test if any common bits are set.
    387   bool anyCommon(const SmallBitVector &RHS) const {
    388     if (isSmall() && RHS.isSmall())
    389       return (getSmallBits() & RHS.getSmallBits()) != 0;
    390     if (!isSmall() && !RHS.isSmall())
    391       return getPointer()->anyCommon(*RHS.getPointer());
    392 
    393     for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
    394       if (test(i) && RHS.test(i))
    395         return true;
    396     return false;
    397   }
    398 
    399   // Comparison operators.
    400   bool operator==(const SmallBitVector &RHS) const {
    401     if (size() != RHS.size())
    402       return false;
    403     if (isSmall())
    404       return getSmallBits() == RHS.getSmallBits();
    405     else
    406       return *getPointer() == *RHS.getPointer();
    407   }
    408 
    409   bool operator!=(const SmallBitVector &RHS) const {
    410     return !(*this == RHS);
    411   }
    412 
    413   // Intersection, union, disjoint union.
    414   SmallBitVector &operator&=(const SmallBitVector &RHS) {
    415     resize(std::max(size(), RHS.size()));
    416     if (isSmall())
    417       setSmallBits(getSmallBits() & RHS.getSmallBits());
    418     else if (!RHS.isSmall())
    419       getPointer()->operator&=(*RHS.getPointer());
    420     else {
    421       SmallBitVector Copy = RHS;
    422       Copy.resize(size());
    423       getPointer()->operator&=(*Copy.getPointer());
    424     }
    425     return *this;
    426   }
    427 
    428   /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
    429   SmallBitVector &reset(const SmallBitVector &RHS) {
    430     if (isSmall() && RHS.isSmall())
    431       setSmallBits(getSmallBits() & ~RHS.getSmallBits());
    432     else if (!isSmall() && !RHS.isSmall())
    433       getPointer()->reset(*RHS.getPointer());
    434     else
    435       for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
    436         if (RHS.test(i))
    437           reset(i);
    438 
    439     return *this;
    440   }
    441 
    442   /// test - Check if (This - RHS) is zero.
    443   /// This is the same as reset(RHS) and any().
    444   bool test(const SmallBitVector &RHS) const {
    445     if (isSmall() && RHS.isSmall())
    446       return (getSmallBits() & ~RHS.getSmallBits()) != 0;
    447     if (!isSmall() && !RHS.isSmall())
    448       return getPointer()->test(*RHS.getPointer());
    449 
    450     unsigned i, e;
    451     for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
    452       if (test(i) && !RHS.test(i))
    453         return true;
    454 
    455     for (e = size(); i != e; ++i)
    456       if (test(i))
    457         return true;
    458 
    459     return false;
    460   }
    461 
    462   SmallBitVector &operator|=(const SmallBitVector &RHS) {
    463     resize(std::max(size(), RHS.size()));
    464     if (isSmall())
    465       setSmallBits(getSmallBits() | RHS.getSmallBits());
    466     else if (!RHS.isSmall())
    467       getPointer()->operator|=(*RHS.getPointer());
    468     else {
    469       SmallBitVector Copy = RHS;
    470       Copy.resize(size());
    471       getPointer()->operator|=(*Copy.getPointer());
    472     }
    473     return *this;
    474   }
    475 
    476   SmallBitVector &operator^=(const SmallBitVector &RHS) {
    477     resize(std::max(size(), RHS.size()));
    478     if (isSmall())
    479       setSmallBits(getSmallBits() ^ RHS.getSmallBits());
    480     else if (!RHS.isSmall())
    481       getPointer()->operator^=(*RHS.getPointer());
    482     else {
    483       SmallBitVector Copy = RHS;
    484       Copy.resize(size());
    485       getPointer()->operator^=(*Copy.getPointer());
    486     }
    487     return *this;
    488   }
    489 
    490   // Assignment operator.
    491   const SmallBitVector &operator=(const SmallBitVector &RHS) {
    492     if (isSmall()) {
    493       if (RHS.isSmall())
    494         X = RHS.X;
    495       else
    496         switchToLarge(new BitVector(*RHS.getPointer()));
    497     } else {
    498       if (!RHS.isSmall())
    499         *getPointer() = *RHS.getPointer();
    500       else {
    501         delete getPointer();
    502         X = RHS.X;
    503       }
    504     }
    505     return *this;
    506   }
    507 
    508   const SmallBitVector &operator=(SmallBitVector &&RHS) {
    509     if (this != &RHS) {
    510       clear();
    511       swap(RHS);
    512     }
    513     return *this;
    514   }
    515 
    516   void swap(SmallBitVector &RHS) {
    517     std::swap(X, RHS.X);
    518   }
    519 
    520   /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
    521   /// This computes "*this |= Mask".
    522   void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
    523     if (isSmall())
    524       applyMask<true, false>(Mask, MaskWords);
    525     else
    526       getPointer()->setBitsInMask(Mask, MaskWords);
    527   }
    528 
    529   /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
    530   /// Don't resize. This computes "*this &= ~Mask".
    531   void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
    532     if (isSmall())
    533       applyMask<false, false>(Mask, MaskWords);
    534     else
    535       getPointer()->clearBitsInMask(Mask, MaskWords);
    536   }
    537 
    538   /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
    539   /// Don't resize.  This computes "*this |= ~Mask".
    540   void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
    541     if (isSmall())
    542       applyMask<true, true>(Mask, MaskWords);
    543     else
    544       getPointer()->setBitsNotInMask(Mask, MaskWords);
    545   }
    546 
    547   /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
    548   /// Don't resize.  This computes "*this &= Mask".
    549   void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
    550     if (isSmall())
    551       applyMask<false, true>(Mask, MaskWords);
    552     else
    553       getPointer()->clearBitsNotInMask(Mask, MaskWords);
    554   }
    555 
    556 private:
    557   template<bool AddBits, bool InvertMask>
    558   void applyMask(const uint32_t *Mask, unsigned MaskWords) {
    559     assert((NumBaseBits == 64 || NumBaseBits == 32) && "Unsupported word size");
    560     if (NumBaseBits == 64 && MaskWords >= 2) {
    561       uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
    562       if (InvertMask) M = ~M;
    563       if (AddBits) setSmallBits(getSmallBits() | M);
    564       else         setSmallBits(getSmallBits() & ~M);
    565     } else {
    566       uint32_t M = Mask[0];
    567       if (InvertMask) M = ~M;
    568       if (AddBits) setSmallBits(getSmallBits() | M);
    569       else         setSmallBits(getSmallBits() & ~M);
    570     }
    571   }
    572 };
    573 
    574 inline SmallBitVector
    575 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
    576   SmallBitVector Result(LHS);
    577   Result &= RHS;
    578   return Result;
    579 }
    580 
    581 inline SmallBitVector
    582 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
    583   SmallBitVector Result(LHS);
    584   Result |= RHS;
    585   return Result;
    586 }
    587 
    588 inline SmallBitVector
    589 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
    590   SmallBitVector Result(LHS);
    591   Result ^= RHS;
    592   return Result;
    593 }
    594 
    595 } // End llvm namespace
    596 
    597 namespace std {
    598   /// Implement std::swap in terms of BitVector swap.
    599   inline void
    600   swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
    601     LHS.swap(RHS);
    602   }
    603 }
    604 
    605 #endif
    606