14 #ifndef LLVM_ADT_SMALLBITVECTOR_H
15 #define LLVM_ADT_SMALLBITVECTOR_H
39 NumBaseBits =
sizeof(uintptr_t) * CHAR_BIT,
43 SmallNumRawBits = NumBaseBits - 1,
48 SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
49 NumBaseBits == 64 ? 6 :
53 SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
72 TheVector.
set(BitPos);
74 TheVector.
reset(BitPos);
79 return const_cast<const SmallBitVector &
>(TheVector).
operator[](BitPos);
84 bool isSmall()
const {
85 return X & uintptr_t(1);
88 BitVector *getPointer()
const {
90 return reinterpret_cast<BitVector *
>(X);
93 void switchToSmall(uintptr_t NewSmallBits,
size_t NewSize) {
95 setSmallSize(NewSize);
96 setSmallBits(NewSmallBits);
99 void switchToLarge(BitVector *BV) {
100 X =
reinterpret_cast<uintptr_t
>(BV);
101 assert(!isSmall() &&
"Tried to use an unaligned pointer");
106 uintptr_t getSmallRawBits()
const {
111 void setSmallRawBits(uintptr_t NewRawBits) {
113 X = (NewRawBits << 1) | uintptr_t(1);
117 size_t getSmallSize()
const {
118 return getSmallRawBits() >> SmallNumDataBits;
121 void setSmallSize(
size_t Size) {
122 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
126 uintptr_t getSmallBits()
const {
127 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
130 void setSmallBits(uintptr_t NewBits) {
131 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
132 (getSmallSize() << SmallNumDataBits));
142 if (s <= SmallNumDataBits)
143 switchToSmall(t ? ~uintptr_t(0) : 0, s);
153 switchToLarge(
new BitVector(*RHS.getPointer()));
156 #if LLVM_HAS_RVALUE_REFERENCES
169 return isSmall() ? getSmallSize() == 0 : getPointer()->
empty();
174 return isSmall() ? getSmallSize() : getPointer()->
size();
180 uintptr_t
Bits = getSmallBits();
181 if (NumBaseBits == 32)
183 if (NumBaseBits == 64)
187 return getPointer()->
count();
193 return getSmallBits() != 0;
194 return getPointer()->
any();
200 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
201 return getPointer()->
all();
207 return getSmallBits() == 0;
208 return getPointer()->
none();
215 uintptr_t
Bits = getSmallBits();
218 if (NumBaseBits == 32)
220 if (NumBaseBits == 64)
231 uintptr_t
Bits = getSmallBits();
233 Bits &= ~uintptr_t(0) << (Prev + 1);
234 if (Bits == 0 || Prev + 1 >= getSmallSize())
236 if (NumBaseBits == 32)
238 if (NumBaseBits == 64)
255 getPointer()->
resize(N, t);
256 }
else if (SmallNumDataBits >= N) {
257 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
259 setSmallBits(NewBits | getSmallBits());
262 uintptr_t OldBits = getSmallBits();
263 for (
size_t i = 0, e = getSmallSize(); i != e; ++i)
264 (*BV)[i] = (OldBits >> i) & 1;
271 if (N > SmallNumDataBits) {
272 uintptr_t OldBits = getSmallRawBits();
273 size_t SmallSize = getSmallSize();
275 for (
size_t i = 0; i < SmallSize; ++i)
276 if ((OldBits >> i) & 1)
289 setSmallBits(~uintptr_t(0));
297 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
299 getPointer()->
set(Idx);
305 assert(I <= E &&
"Attempted to set backwards range!");
306 assert(E <=
size() &&
"Attempted to set out-of-bounds range!");
307 if (I == E)
return *
this;
309 uintptr_t EMask = ((uintptr_t)1) << E;
310 uintptr_t IMask = ((uintptr_t)1) <<
I;
311 uintptr_t Mask = EMask - IMask;
312 setSmallBits(getSmallBits() | Mask);
314 getPointer()->
set(I, E);
322 getPointer()->
reset();
328 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
330 getPointer()->
reset(Idx);
336 assert(I <= E &&
"Attempted to reset backwards range!");
337 assert(E <=
size() &&
"Attempted to reset out-of-bounds range!");
338 if (I == E)
return *
this;
340 uintptr_t EMask = ((uintptr_t)1) << E;
341 uintptr_t IMask = ((uintptr_t)1) <<
I;
342 uintptr_t Mask = EMask - IMask;
343 setSmallBits(getSmallBits() & ~Mask);
345 getPointer()->
reset(I, E);
351 setSmallBits(~getSmallBits());
353 getPointer()->
flip();
359 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
361 getPointer()->
flip(Idx);
372 assert(Idx <
size() &&
"Out-of-bounds Bit access.");
377 assert(Idx <
size() &&
"Out-of-bounds Bit access.");
379 return ((getSmallBits() >> Idx) & 1) != 0;
380 return getPointer()->operator[](Idx);
383 bool test(
unsigned Idx)
const {
389 if (isSmall() && RHS.isSmall())
390 return (getSmallBits() & RHS.getSmallBits()) != 0;
391 if (!isSmall() && !RHS.isSmall())
392 return getPointer()->
anyCommon(*RHS.getPointer());
394 for (
unsigned i = 0, e = std::min(
size(), RHS.
size()); i != e; ++i)
405 return getSmallBits() == RHS.getSmallBits();
407 return *getPointer() == *RHS.getPointer();
411 return !(*
this == RHS);
418 setSmallBits(getSmallBits() & RHS.getSmallBits());
419 else if (!RHS.isSmall())
420 getPointer()->operator&=(*RHS.getPointer());
424 getPointer()->operator&=(*Copy.getPointer());
431 if (isSmall() && RHS.isSmall())
432 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
433 else if (!isSmall() && !RHS.isSmall())
434 getPointer()->
reset(*RHS.getPointer());
436 for (
unsigned i = 0, e = std::min(
size(), RHS.
size()); i != e; ++i)
446 if (isSmall() && RHS.isSmall())
447 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
448 if (!isSmall() && !RHS.isSmall())
449 return getPointer()->
test(*RHS.getPointer());
452 for (i = 0, e = std::min(
size(), RHS.
size()); i != e; ++i)
456 for (e =
size(); i != e; ++i)
466 setSmallBits(getSmallBits() | RHS.getSmallBits());
467 else if (!RHS.isSmall())
468 getPointer()->operator|=(*RHS.getPointer());
472 getPointer()->operator|=(*Copy.getPointer());
480 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
481 else if (!RHS.isSmall())
482 getPointer()->operator^=(*RHS.getPointer());
486 getPointer()->operator^=(*Copy.getPointer());
497 switchToLarge(
new BitVector(*RHS.getPointer()));
500 *getPointer() = *RHS.getPointer();
509 #if LLVM_HAS_RVALUE_REFERENCES
527 applyMask<true, false>(Mask, MaskWords);
536 applyMask<false, false>(Mask, MaskWords);
545 applyMask<true, true>(Mask, MaskWords);
554 applyMask<false, true>(Mask, MaskWords);
560 template<
bool AddBits,
bool InvertMask>
561 void applyMask(
const uint32_t *Mask,
unsigned MaskWords) {
562 assert((NumBaseBits == 64 || NumBaseBits == 32) &&
"Unsupported word size");
563 if (NumBaseBits == 64 && MaskWords >= 2) {
564 uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
565 if (InvertMask) M = ~M;
566 if (AddBits) setSmallBits(getSmallBits() | M);
567 else setSmallBits(getSmallBits() & ~M);
569 uint32_t M = Mask[0];
570 if (InvertMask) M = ~M;
571 if (AddBits) setSmallBits(getSmallBits() | M);
572 else setSmallBits(getSmallBits() & ~M);
577 inline SmallBitVector
584 inline SmallBitVector
591 inline SmallBitVector
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
bool operator!=(const SmallBitVector &RHS) const
bool none() const
none - Returns true if none of the bits are set.
reference operator[](unsigned Idx)
SmallBitVector & reset(unsigned I, unsigned E)
reset - Efficiently reset a range of bits in [I, E)
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
int find_next(unsigned Prev) const
bool all() const
all - Returns true if all bits are set.
SmallBitVector operator&(const SmallBitVector &LHS, const SmallBitVector &RHS)
const SmallBitVector & operator=(const SmallBitVector &RHS)
void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
bool any() const
any - Returns true if any bit is set.
SmallBitVector & flip(unsigned Idx)
SmallBitVector & operator&=(const SmallBitVector &RHS)
SmallBitVector(unsigned s, bool t=false)
#define llvm_unreachable(msg)
bool anyCommon(const SmallBitVector &RHS) const
Test if any common bits are set.
SmallBitVector operator|(const SmallBitVector &LHS, const SmallBitVector &RHS)
reference & operator=(reference t)
SmallBitVector & reset(unsigned Idx)
unsigned count() const
count - Returns the number of bits which are set.
SmallBitVector & reset(const SmallBitVector &RHS)
reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
bool test(const SmallBitVector &RHS) const
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, std::size_t >::type countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
bool all() const
all - Returns true if all bits are set.
reference & operator=(bool t)
unsigned count() const
count - Returns the number of bits which are set.
void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
reference(SmallBitVector &b, unsigned Idx)
void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
bool empty() const
empty - Tests whether there are no bits in this bitvector.
int find_next(unsigned Prev) const
unsigned CountPopulation_64(uint64_t Value)
SmallBitVector()
SmallBitVector default ctor - Creates an empty bitvector.
void clear()
clear - Clear all bits.
SmallBitVector & operator^=(const SmallBitVector &RHS)
unsigned CountPopulation_32(uint32_t Value)
SmallBitVector & set(unsigned I, unsigned E)
set - Efficiently set a range of bits in [I, E)
SmallBitVector(const SmallBitVector &RHS)
SmallBitVector copy ctor.
bool test(unsigned Idx) const
bool none() const
none - Returns true if none of the bits are set.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
size_t size() const
size - Returns the number of bits in this bitvector.
bool test(unsigned Idx) const
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
void swap(SmallBitVector &RHS)
SmallBitVector operator~() const
SmallBitVector & set(unsigned Idx)
bool operator==(const SmallBitVector &RHS) const
SmallBitVector operator^(const SmallBitVector &LHS, const SmallBitVector &RHS)
bool any() const
any - Returns true if any bit is set.
bool operator[](unsigned Idx) const
bool empty() const
empty - Tests whether there are no bits in this bitvector.
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
SmallBitVector & operator|=(const SmallBitVector &RHS)
unsigned size() const
size - Returns the number of bits in this bitvector.