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   // Encapsulation of a single bit.
     58   class reference {
     59     SmallBitVector &TheVector;
     60     unsigned BitPos;
     61 
     62   public:
     63     reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
     64 
     65     reference& operator=(reference t) {
     66       *this = bool(t);
     67       return *this;
     68     }
     69 
     70     reference& operator=(bool t) {
     71       if (t)
     72         TheVector.set(BitPos);
     73       else
     74         TheVector.reset(BitPos);
     75       return *this;
     76     }
     77 
     78     operator bool() const {
     79       return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
     80     }
     81   };
     82 
     83 private:
     84   bool isSmall() const {
     85     return X & uintptr_t(1);
     86   }
     87 
     88   BitVector *getPointer() const {
     89     assert(!isSmall());
     90     return reinterpret_cast<BitVector *>(X);
     91   }
     92 
     93   void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
     94     X = 1;
     95     setSmallSize(NewSize);
     96     setSmallBits(NewSmallBits);
     97   }
     98 
     99   void switchToLarge(BitVector *BV) {
    100     X = reinterpret_cast<uintptr_t>(BV);
    101     assert(!isSmall() && "Tried to use an unaligned pointer");
    102   }
    103 
    104   // Return all the bits used for the "small" representation; this includes
    105   // bits for the size as well as the element bits.
    106   uintptr_t getSmallRawBits() const {
    107     assert(isSmall());
    108     return X >> 1;
    109   }
    110 
    111   void setSmallRawBits(uintptr_t NewRawBits) {
    112     assert(isSmall());
    113     X = (NewRawBits << 1) | uintptr_t(1);
    114   }
    115 
    116   // Return the size.
    117   size_t getSmallSize() const {
    118     return getSmallRawBits() >> SmallNumDataBits;
    119   }
    120 
    121   void setSmallSize(size_t Size) {
    122     setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
    123   }
    124 
    125   // Return the element bits.
    126   uintptr_t getSmallBits() const {
    127     return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
    128   }
    129 
    130   void setSmallBits(uintptr_t NewBits) {
    131     setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
    132                     (getSmallSize() << SmallNumDataBits));
    133   }
    134 
    135 public:
    136   /// SmallBitVector default ctor - Creates an empty bitvector.
    137   SmallBitVector() : X(1) {}
    138 
    139   /// SmallBitVector ctor - Creates a bitvector of specified number of bits. All
    140   /// bits are initialized to the specified value.
    141   explicit SmallBitVector(unsigned s, bool t = false) {
    142     if (s <= SmallNumDataBits)
    143       switchToSmall(t ? ~uintptr_t(0) : 0, s);
    144     else
    145       switchToLarge(new BitVector(s, t));
    146   }
    147 
    148   /// SmallBitVector copy ctor.
    149   SmallBitVector(const SmallBitVector &RHS) {
    150     if (RHS.isSmall())
    151       X = RHS.X;
    152     else
    153       switchToLarge(new BitVector(*RHS.getPointer()));
    154   }
    155 
    156 #if LLVM_USE_RVALUE_REFERENCES
    157   SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
    158     RHS.X = 1;
    159   }
    160 #endif
    161 
    162   ~SmallBitVector() {
    163     if (!isSmall())
    164       delete getPointer();
    165   }
    166 
    167   /// empty - Tests whether there are no bits in this bitvector.
    168   bool empty() const {
    169     return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
    170   }
    171 
    172   /// size - Returns the number of bits in this bitvector.
    173   size_t size() const {
    174     return isSmall() ? getSmallSize() : getPointer()->size();
    175   }
    176 
    177   /// count - Returns the number of bits which are set.
    178   unsigned count() const {
    179     if (isSmall()) {
    180       uintptr_t Bits = getSmallBits();
    181       if (sizeof(uintptr_t) * CHAR_BIT == 32)
    182         return CountPopulation_32(Bits);
    183       if (sizeof(uintptr_t) * CHAR_BIT == 64)
    184         return CountPopulation_64(Bits);
    185       llvm_unreachable("Unsupported!");
    186     }
    187     return getPointer()->count();
    188   }
    189 
    190   /// any - Returns true if any bit is set.
    191   bool any() const {
    192     if (isSmall())
    193       return getSmallBits() != 0;
    194     return getPointer()->any();
    195   }
    196 
    197   /// all - Returns true if all bits are set.
    198   bool all() const {
    199     if (isSmall())
    200       return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
    201     return getPointer()->all();
    202   }
    203 
    204   /// none - Returns true if none of the bits are set.
    205   bool none() const {
    206     if (isSmall())
    207       return getSmallBits() == 0;
    208     return getPointer()->none();
    209   }
    210 
    211   /// find_first - Returns the index of the first set bit, -1 if none
    212   /// of the bits are set.
    213   int find_first() const {
    214     if (isSmall()) {
    215       uintptr_t Bits = getSmallBits();
    216       if (Bits == 0)
    217         return -1;
    218       if (sizeof(uintptr_t) * CHAR_BIT == 32)
    219         return CountTrailingZeros_32(Bits);
    220       if (sizeof(uintptr_t) * CHAR_BIT == 64)
    221         return CountTrailingZeros_64(Bits);
    222       llvm_unreachable("Unsupported!");
    223     }
    224     return getPointer()->find_first();
    225   }
    226 
    227   /// find_next - Returns the index of the next set bit following the
    228   /// "Prev" bit. Returns -1 if the next set bit is not found.
    229   int find_next(unsigned Prev) const {
    230     if (isSmall()) {
    231       uintptr_t Bits = getSmallBits();
    232       // Mask off previous bits.
    233       Bits &= ~uintptr_t(0) << (Prev + 1);
    234       if (Bits == 0 || Prev + 1 >= getSmallSize())
    235         return -1;
    236       if (sizeof(uintptr_t) * CHAR_BIT == 32)
    237         return CountTrailingZeros_32(Bits);
    238       if (sizeof(uintptr_t) * CHAR_BIT == 64)
    239         return CountTrailingZeros_64(Bits);
    240       llvm_unreachable("Unsupported!");
    241     }
    242     return getPointer()->find_next(Prev);
    243   }
    244 
    245   /// clear - Clear all bits.
    246   void clear() {
    247     if (!isSmall())
    248       delete getPointer();
    249     switchToSmall(0, 0);
    250   }
    251 
    252   /// resize - Grow or shrink the bitvector.
    253   void resize(unsigned N, bool t = false) {
    254     if (!isSmall()) {
    255       getPointer()->resize(N, t);
    256     } else if (SmallNumDataBits >= N) {
    257       uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
    258       setSmallSize(N);
    259       setSmallBits(NewBits | getSmallBits());
    260     } else {
    261       BitVector *BV = new BitVector(N, t);
    262       uintptr_t OldBits = getSmallBits();
    263       for (size_t i = 0, e = getSmallSize(); i != e; ++i)
    264         (*BV)[i] = (OldBits >> i) & 1;
    265       switchToLarge(BV);
    266     }
    267   }
    268 
    269   void reserve(unsigned N) {
    270     if (isSmall()) {
    271       if (N > SmallNumDataBits) {
    272         uintptr_t OldBits = getSmallRawBits();
    273         size_t SmallSize = getSmallSize();
    274         BitVector *BV = new BitVector(SmallSize);
    275         for (size_t i = 0; i < SmallSize; ++i)
    276           if ((OldBits >> i) & 1)
    277             BV->set(i);
    278         BV->reserve(N);
    279         switchToLarge(BV);
    280       }
    281     } else {
    282       getPointer()->reserve(N);
    283     }
    284   }
    285 
    286   // Set, reset, flip
    287   SmallBitVector &set() {
    288     if (isSmall())
    289       setSmallBits(~uintptr_t(0));
    290     else
    291       getPointer()->set();
    292     return *this;
    293   }
    294 
    295   SmallBitVector &set(unsigned Idx) {
    296     if (isSmall())
    297       setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
    298     else
    299       getPointer()->set(Idx);
    300     return *this;
    301   }
    302 
    303   SmallBitVector &reset() {
    304     if (isSmall())
    305       setSmallBits(0);
    306     else
    307       getPointer()->reset();
    308     return *this;
    309   }
    310 
    311   SmallBitVector &reset(unsigned Idx) {
    312     if (isSmall())
    313       setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
    314     else
    315       getPointer()->reset(Idx);
    316     return *this;
    317   }
    318 
    319   SmallBitVector &flip() {
    320     if (isSmall())
    321       setSmallBits(~getSmallBits());
    322     else
    323       getPointer()->flip();
    324     return *this;
    325   }
    326 
    327   SmallBitVector &flip(unsigned Idx) {
    328     if (isSmall())
    329       setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
    330     else
    331       getPointer()->flip(Idx);
    332     return *this;
    333   }
    334 
    335   // No argument flip.
    336   SmallBitVector operator~() const {
    337     return SmallBitVector(*this).flip();
    338   }
    339 
    340   // Indexing.
    341   reference operator[](unsigned Idx) {
    342     assert(Idx < size() && "Out-of-bounds Bit access.");
    343     return reference(*this, Idx);
    344   }
    345 
    346   bool operator[](unsigned Idx) const {
    347     assert(Idx < size() && "Out-of-bounds Bit access.");
    348     if (isSmall())
    349       return ((getSmallBits() >> Idx) & 1) != 0;
    350     return getPointer()->operator[](Idx);
    351   }
    352 
    353   bool test(unsigned Idx) const {
    354     return (*this)[Idx];
    355   }
    356 
    357   /// Test if any common bits are set.
    358   bool anyCommon(const SmallBitVector &RHS) const {
    359     if (isSmall() && RHS.isSmall())
    360       return (getSmallBits() & RHS.getSmallBits()) != 0;
    361     if (!isSmall() && !RHS.isSmall())
    362       return getPointer()->anyCommon(*RHS.getPointer());
    363 
    364     for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
    365       if (test(i) && RHS.test(i))
    366         return true;
    367     return false;
    368   }
    369 
    370   // Comparison operators.
    371   bool operator==(const SmallBitVector &RHS) const {
    372     if (size() != RHS.size())
    373       return false;
    374     if (isSmall())
    375       return getSmallBits() == RHS.getSmallBits();
    376     else
    377       return *getPointer() == *RHS.getPointer();
    378   }
    379 
    380   bool operator!=(const SmallBitVector &RHS) const {
    381     return !(*this == RHS);
    382   }
    383 
    384   // Intersection, union, disjoint union.
    385   SmallBitVector &operator&=(const SmallBitVector &RHS) {
    386     resize(std::max(size(), RHS.size()));
    387     if (isSmall())
    388       setSmallBits(getSmallBits() & RHS.getSmallBits());
    389     else if (!RHS.isSmall())
    390       getPointer()->operator&=(*RHS.getPointer());
    391     else {
    392       SmallBitVector Copy = RHS;
    393       Copy.resize(size());
    394       getPointer()->operator&=(*Copy.getPointer());
    395     }
    396     return *this;
    397   }
    398 
    399   SmallBitVector &operator|=(const SmallBitVector &RHS) {
    400     resize(std::max(size(), RHS.size()));
    401     if (isSmall())
    402       setSmallBits(getSmallBits() | RHS.getSmallBits());
    403     else if (!RHS.isSmall())
    404       getPointer()->operator|=(*RHS.getPointer());
    405     else {
    406       SmallBitVector Copy = RHS;
    407       Copy.resize(size());
    408       getPointer()->operator|=(*Copy.getPointer());
    409     }
    410     return *this;
    411   }
    412 
    413   SmallBitVector &operator^=(const SmallBitVector &RHS) {
    414     resize(std::max(size(), RHS.size()));
    415     if (isSmall())
    416       setSmallBits(getSmallBits() ^ RHS.getSmallBits());
    417     else if (!RHS.isSmall())
    418       getPointer()->operator^=(*RHS.getPointer());
    419     else {
    420       SmallBitVector Copy = RHS;
    421       Copy.resize(size());
    422       getPointer()->operator^=(*Copy.getPointer());
    423     }
    424     return *this;
    425   }
    426 
    427   // Assignment operator.
    428   const SmallBitVector &operator=(const SmallBitVector &RHS) {
    429     if (isSmall()) {
    430       if (RHS.isSmall())
    431         X = RHS.X;
    432       else
    433         switchToLarge(new BitVector(*RHS.getPointer()));
    434     } else {
    435       if (!RHS.isSmall())
    436         *getPointer() = *RHS.getPointer();
    437       else {
    438         delete getPointer();
    439         X = RHS.X;
    440       }
    441     }
    442     return *this;
    443   }
    444 
    445 #if LLVM_USE_RVALUE_REFERENCES
    446   const SmallBitVector &operator=(SmallBitVector &&RHS) {
    447     if (this != &RHS) {
    448       clear();
    449       swap(RHS);
    450     }
    451     return *this;
    452   }
    453 #endif
    454 
    455   void swap(SmallBitVector &RHS) {
    456     std::swap(X, RHS.X);
    457   }
    458 
    459   /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
    460   /// This computes "*this |= Mask".
    461   void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
    462     if (isSmall())
    463       applyMask<true, false>(Mask, MaskWords);
    464     else
    465       getPointer()->setBitsInMask(Mask, MaskWords);
    466   }
    467 
    468   /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
    469   /// Don't resize. This computes "*this &= ~Mask".
    470   void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
    471     if (isSmall())
    472       applyMask<false, false>(Mask, MaskWords);
    473     else
    474       getPointer()->clearBitsInMask(Mask, MaskWords);
    475   }
    476 
    477   /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
    478   /// Don't resize.  This computes "*this |= ~Mask".
    479   void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
    480     if (isSmall())
    481       applyMask<true, true>(Mask, MaskWords);
    482     else
    483       getPointer()->setBitsNotInMask(Mask, MaskWords);
    484   }
    485 
    486   /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
    487   /// Don't resize.  This computes "*this &= Mask".
    488   void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
    489     if (isSmall())
    490       applyMask<false, true>(Mask, MaskWords);
    491     else
    492       getPointer()->clearBitsNotInMask(Mask, MaskWords);
    493   }
    494 
    495 private:
    496   template<bool AddBits, bool InvertMask>
    497   void applyMask(const uint32_t *Mask, unsigned MaskWords) {
    498     assert((NumBaseBits == 64 || NumBaseBits == 32) && "Unsupported word size");
    499     if (NumBaseBits == 64 && MaskWords >= 2) {
    500       uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
    501       if (InvertMask) M = ~M;
    502       if (AddBits) setSmallBits(getSmallBits() | M);
    503       else         setSmallBits(getSmallBits() & ~M);
    504     } else {
    505       uint32_t M = Mask[0];
    506       if (InvertMask) M = ~M;
    507       if (AddBits) setSmallBits(getSmallBits() | M);
    508       else         setSmallBits(getSmallBits() & ~M);
    509     }
    510   }
    511 };
    512 
    513 inline SmallBitVector
    514 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
    515   SmallBitVector Result(LHS);
    516   Result &= RHS;
    517   return Result;
    518 }
    519 
    520 inline SmallBitVector
    521 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
    522   SmallBitVector Result(LHS);
    523   Result |= RHS;
    524   return Result;
    525 }
    526 
    527 inline SmallBitVector
    528 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
    529   SmallBitVector Result(LHS);
    530   Result ^= RHS;
    531   return Result;
    532 }
    533 
    534 } // End llvm namespace
    535 
    536 namespace std {
    537   /// Implement std::swap in terms of BitVector swap.
    538   inline void
    539   swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
    540     LHS.swap(RHS);
    541   }
    542 }
    543 
    544 #endif
    545