13 #ifndef LLVM_ADT_SMALLBITVECTOR_H 14 #define LLVM_ADT_SMALLBITVECTOR_H 42 NumBaseBits =
sizeof(uintptr_t) * CHAR_BIT,
46 SmallNumRawBits = NumBaseBits - 1,
51 SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
52 NumBaseBits == 64 ? 6 :
56 SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
59 static_assert(NumBaseBits == 64 || NumBaseBits == 32,
60 "Unsupported word size");
82 TheVector.
set(BitPos);
84 TheVector.
reset(BitPos);
89 return const_cast<const SmallBitVector &
>(TheVector).
operator[](BitPos);
99 void switchToSmall(uintptr_t NewSmallBits,
size_t NewSize) {
101 setSmallSize(NewSize);
102 setSmallBits(NewSmallBits);
106 X =
reinterpret_cast<uintptr_t
>(BV);
112 uintptr_t getSmallRawBits()
const {
117 void setSmallRawBits(uintptr_t NewRawBits) {
119 X = (NewRawBits << 1) | uintptr_t(1);
123 size_t getSmallSize()
const {
return getSmallRawBits() >> SmallNumDataBits; }
125 void setSmallSize(
size_t Size) {
126 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
130 uintptr_t getSmallBits()
const {
131 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
134 void setSmallBits(uintptr_t NewBits) {
135 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
136 (getSmallSize() << SmallNumDataBits));
146 if (s <= SmallNumDataBits)
147 switchToSmall(t ? ~uintptr_t(0) : 0, s);
157 switchToLarge(
new BitVector(*RHS.getPointer()));
184 bool isSmall()
const {
return X & uintptr_t(1); }
188 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
193 return isSmall() ? getSmallSize() : getPointer()->size();
199 uintptr_t
Bits = getSmallBits();
202 return getPointer()->count();
208 return getSmallBits() != 0;
209 return getPointer()->any();
215 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
216 return getPointer()->all();
222 return getSmallBits() == 0;
223 return getPointer()->none();
229 uintptr_t
Bits = getSmallBits();
234 return getPointer()->find_first();
239 uintptr_t
Bits = getSmallBits();
244 return getPointer()->find_last();
250 if (
count() == getSmallSize())
253 uintptr_t
Bits = getSmallBits();
256 return getPointer()->find_first_unset();
261 if (
count() == getSmallSize())
264 uintptr_t
Bits = getSmallBits();
266 Bits |= ~uintptr_t(0) << getSmallSize();
269 return getPointer()->find_last_unset();
276 uintptr_t
Bits = getSmallBits();
278 Bits &= ~uintptr_t(0) << (Prev + 1);
279 if (Bits == 0 || Prev + 1 >= getSmallSize())
283 return getPointer()->find_next(Prev);
291 uintptr_t
Bits = getSmallBits();
293 uintptr_t
Mask = (uintptr_t(1) << Prev) - 1;
296 if (Bits == ~uintptr_t(0) || Prev + 1 >= getSmallSize())
300 return getPointer()->find_next_unset(Prev);
311 uintptr_t
Bits = getSmallBits();
312 Bits &= maskTrailingOnes<uintptr_t>(PriorTo + 1);
318 return getPointer()->find_prev(PriorTo);
331 getPointer()->resize(N, t);
332 }
else if (SmallNumDataBits >= N) {
333 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
335 setSmallBits(NewBits | getSmallBits());
338 uintptr_t OldBits = getSmallBits();
339 for (
size_t i = 0,
e = getSmallSize(); i !=
e; ++i)
340 (*BV)[i] = (OldBits >> i) & 1;
347 if (N > SmallNumDataBits) {
348 uintptr_t OldBits = getSmallRawBits();
349 size_t SmallSize = getSmallSize();
351 for (
size_t i = 0; i < SmallSize; ++i)
352 if ((OldBits >> i) & 1)
358 getPointer()->reserve(N);
365 setSmallBits(~uintptr_t(0));
373 assert(Idx <= static_cast<unsigned>(
374 std::numeric_limits<uintptr_t>::digits) &&
375 "undefined behavior");
376 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
379 getPointer()->set(Idx);
385 assert(I <=
E &&
"Attempted to set backwards range!");
386 assert(
E <=
size() &&
"Attempted to set out-of-bounds range!");
387 if (I ==
E)
return *
this;
389 uintptr_t EMask = ((uintptr_t)1) <<
E;
390 uintptr_t IMask = ((uintptr_t)1) <<
I;
391 uintptr_t
Mask = EMask - IMask;
392 setSmallBits(getSmallBits() | Mask);
394 getPointer()->set(I,
E);
402 getPointer()->reset();
408 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
410 getPointer()->reset(Idx);
416 assert(I <= E &&
"Attempted to reset backwards range!");
417 assert(E <=
size() &&
"Attempted to reset out-of-bounds range!");
418 if (I == E)
return *
this;
420 uintptr_t EMask = ((uintptr_t)1) <<
E;
421 uintptr_t IMask = ((uintptr_t)1) <<
I;
422 uintptr_t
Mask = EMask - IMask;
423 setSmallBits(getSmallBits() & ~Mask);
425 getPointer()->reset(I, E);
431 setSmallBits(~getSmallBits());
433 getPointer()->flip();
439 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
441 getPointer()->flip(Idx);
452 assert(Idx <
size() &&
"Out-of-bounds Bit access.");
457 assert(Idx <
size() &&
"Out-of-bounds Bit access.");
459 return ((getSmallBits() >> Idx) & 1) != 0;
460 return getPointer()->operator[](Idx);
463 bool test(
unsigned Idx)
const {
475 return (getSmallBits() & RHS.getSmallBits()) != 0;
477 return getPointer()->anyCommon(*RHS.getPointer());
479 for (
unsigned i = 0,
e = std::min(
size(), RHS.
size()); i !=
e; ++i)
490 return getSmallBits() == RHS.getSmallBits();
492 return *getPointer() == *RHS.getPointer();
494 for (
size_t i = 0,
e =
size(); i !=
e; ++i) {
495 if ((*
this)[i] != RHS[i])
503 return !(*
this == RHS);
511 setSmallBits(getSmallBits() & RHS.getSmallBits());
513 getPointer()->operator&=(*RHS.getPointer());
516 for (i = 0, e = std::min(
size(), RHS.
size()); i !=
e; ++i)
517 (*
this)[i] =
test(i) && RHS.
test(i);
518 for (e =
size(); i !=
e; ++i)
527 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
529 getPointer()->
reset(*RHS.getPointer());
531 for (
unsigned i = 0,
e = std::min(
size(), RHS.
size()); i !=
e; ++i)
541 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
543 return getPointer()->test(*RHS.getPointer());
546 for (i = 0, e = std::min(
size(), RHS.
size()); i !=
e; ++i)
550 for (e =
size(); i !=
e; ++i)
560 setSmallBits(getSmallBits() | RHS.getSmallBits());
562 getPointer()->operator|=(*RHS.getPointer());
564 for (
size_t i = 0,
e = RHS.
size(); i !=
e; ++i)
565 (*
this)[i] =
test(i) || RHS.
test(i);
573 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
575 getPointer()->operator^=(*RHS.getPointer());
577 for (
size_t i = 0,
e = RHS.
size(); i !=
e; ++i)
578 (*
this)[i] =
test(i) != RHS.
test(i);
585 setSmallBits(getSmallBits() << N);
587 getPointer()->operator<<=(
N);
593 setSmallBits(getSmallBits() >> N);
595 getPointer()->operator>>=(
N);
605 switchToLarge(
new BitVector(*RHS.getPointer()));
608 *getPointer() = *RHS.getPointer();
633 applyMask<true, false>(
Mask, MaskWords);
635 getPointer()->setBitsInMask(Mask, MaskWords);
642 applyMask<false, false>(
Mask, MaskWords);
644 getPointer()->clearBitsInMask(Mask, MaskWords);
651 applyMask<true, true>(
Mask, MaskWords);
653 getPointer()->setBitsNotInMask(Mask, MaskWords);
660 applyMask<false, true>(
Mask, MaskWords);
662 getPointer()->clearBitsNotInMask(Mask, MaskWords);
666 template <
bool AddBits,
bool InvertMask>
667 void applyMask(
const uint32_t *
Mask,
unsigned MaskWords) {
668 assert(MaskWords <=
sizeof(uintptr_t) &&
"Mask is larger than base!");
669 uintptr_t M = Mask[0];
670 if (NumBaseBits == 64)
671 M |= uint64_t(Mask[1]) << 32;
675 setSmallBits(getSmallBits() | M);
677 setSmallBits(getSmallBits() & ~M);
714 #endif // LLVM_ADT_SMALLBITVECTOR_H
bool none() const
Returns true if none of the bits are set.
void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
Clear any bits in this vector that are set in Mask.
int find_first_unset() const
Returns the index of the first unset bit, -1 if all of the bits are set.
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
This class represents lattice values for constants.
const SmallBitVector & operator=(SmallBitVector &&RHS)
reference operator[](unsigned Idx)
SmallBitVector & reset(unsigned I, unsigned E)
Efficiently reset a range of bits in [I, E)
unsigned countLeadingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the most significant bit to the first zero bit.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
const SmallBitVector & operator=(const SmallBitVector &RHS)
APInt operator^(APInt a, const APInt &b)
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
APInt operator &(APInt a, const APInt &b)
bool operator!=(const SmallBitVector &RHS) const
int find_prev(unsigned PriorTo) const
find_prev - Returns the index of the first set bit that precedes the the bit at PriorTo.
SmallBitVector & flip(unsigned Idx)
bool test(unsigned Idx) const
unsigned 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...
SmallBitVector(unsigned s, bool t=false)
Creates a bitvector of specified number of bits.
reference & operator=(reference t)
SmallBitVector & reset(unsigned Idx)
bool operator==(const SmallBitVector &RHS) const
SmallBitVector & reset(const SmallBitVector &RHS)
Reset bits that are set in RHS. Same as *this &= ~RHS.
iterator_range< const_set_bits_iterator > set_bits() const
SmallBitVector operator~() const
reference & operator=(bool t)
SmallBitVector()=default
Creates an empty bitvector.
reference(SmallBitVector &b, unsigned Idx)
void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
Clear a bit in this vector for every '0' bit in Mask.
void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
Add a bit to this vector for every '0' bit in Mask.
const_set_bits_iterator set_bits_begin() const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const_set_bits_iterator_impl< SmallBitVector > const_set_bits_iterator
const_set_bits_iterator set_bits_end() const
int find_next_unset(unsigned Prev) const
Returns the index of the next unset bit following the "Prev" bit.
bool anyCommon(const SmallBitVector &RHS) const
Test if any common bits are set.
ForwardIterator for the bits that are set.
unsigned countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the most significant bit to the least stopping at the first 1...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void clear()
Clear all bits.
SmallBitVector & operator^=(const SmallBitVector &RHS)
unsigned countPopulation(T Value)
Count the number of set bits in a value.
Align max(MaybeAlign Lhs, Align Rhs)
SmallBitVector(const SmallBitVector &RHS)
SmallBitVector copy ctor.
bool test(const SmallBitVector &RHS) const
Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
SmallBitVector & operator &=(const SmallBitVector &RHS)
size_t size() const
Returns the number of bits in this bitvector.
int find_next(unsigned Prev) const
Returns the index of the next set bit following the "Prev" bit.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
A range adaptor for a pair of iterators.
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
Add '1' bits from Mask to this vector.
SmallBitVector & operator<<=(unsigned N)
void swap(SmallBitVector &RHS)
bool all() const
Returns true if all bits are set.
int find_last_unset() const
int find_first() const
Returns the index of the first set bit, -1 if none of the bits are set.
bool operator[](unsigned Idx) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallBitVector & operator>>=(unsigned N)
unsigned countTrailingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the least significant bit to the first zero bit.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
bool any() const
Returns true if any bit is set.
APInt operator|(APInt a, const APInt &b)
bool empty() const
Tests whether there are no bits in this bitvector.
size_type count() const
Returns the number of bits which are set.
SmallBitVector & operator|=(const SmallBitVector &RHS)
SmallBitVector(SmallBitVector &&RHS)