14 #ifndef LLVM_ADT_SMALLBITVECTOR_H 15 #define LLVM_ADT_SMALLBITVECTOR_H 43 NumBaseBits =
sizeof(uintptr_t) * CHAR_BIT,
47 SmallNumRawBits = NumBaseBits - 1,
52 SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
53 NumBaseBits == 64 ? 6 :
57 SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
60 static_assert(NumBaseBits == 64 || NumBaseBits == 32,
61 "Unsupported word size");
83 TheVector.
set(BitPos);
85 TheVector.
reset(BitPos);
90 return const_cast<const SmallBitVector &
>(TheVector).
operator[](BitPos);
100 void switchToSmall(uintptr_t NewSmallBits,
size_t NewSize) {
102 setSmallSize(NewSize);
103 setSmallBits(NewSmallBits);
107 X =
reinterpret_cast<uintptr_t
>(BV);
113 uintptr_t getSmallRawBits()
const {
118 void setSmallRawBits(uintptr_t NewRawBits) {
120 X = (NewRawBits << 1) | uintptr_t(1);
124 size_t getSmallSize()
const {
return getSmallRawBits() >> SmallNumDataBits; }
126 void setSmallSize(
size_t Size) {
127 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
131 uintptr_t getSmallBits()
const {
132 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
135 void setSmallBits(uintptr_t NewBits) {
136 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
137 (getSmallSize() << SmallNumDataBits));
147 if (s <= SmallNumDataBits)
148 switchToSmall(t ? ~uintptr_t(0) : 0, s);
158 switchToLarge(
new BitVector(*RHS.getPointer()));
185 bool isSmall()
const {
return X & uintptr_t(1); }
189 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
194 return isSmall() ? getSmallSize() : getPointer()->size();
200 uintptr_t
Bits = getSmallBits();
203 return getPointer()->count();
209 return getSmallBits() != 0;
210 return getPointer()->any();
216 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
217 return getPointer()->all();
223 return getSmallBits() == 0;
224 return getPointer()->none();
230 uintptr_t
Bits = getSmallBits();
235 return getPointer()->find_first();
240 uintptr_t
Bits = getSmallBits();
245 return getPointer()->find_last();
251 if (
count() == getSmallSize())
254 uintptr_t
Bits = getSmallBits();
257 return getPointer()->find_first_unset();
262 if (
count() == getSmallSize())
265 uintptr_t
Bits = getSmallBits();
267 Bits |= ~uintptr_t(0) << getSmallSize();
270 return getPointer()->find_last_unset();
277 uintptr_t
Bits = getSmallBits();
279 Bits &= ~uintptr_t(0) << (Prev + 1);
280 if (Bits == 0 || Prev + 1 >= getSmallSize())
284 return getPointer()->find_next(Prev);
292 uintptr_t
Bits = getSmallBits();
294 uintptr_t
Mask = (1 << Prev) - 1;
297 if (Bits == ~uintptr_t(0) || Prev + 1 >= getSmallSize())
301 return getPointer()->find_next_unset(Prev);
312 uintptr_t
Bits = getSmallBits();
313 Bits &= maskTrailingOnes<uintptr_t>(PriorTo + 1);
319 return getPointer()->find_prev(PriorTo);
332 getPointer()->resize(N, t);
333 }
else if (SmallNumDataBits >= N) {
334 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
336 setSmallBits(NewBits | getSmallBits());
339 uintptr_t OldBits = getSmallBits();
340 for (
size_t i = 0, e = getSmallSize(); i != e; ++i)
341 (*BV)[i] = (OldBits >> i) & 1;
348 if (N > SmallNumDataBits) {
349 uintptr_t OldBits = getSmallRawBits();
350 size_t SmallSize = getSmallSize();
352 for (
size_t i = 0; i < SmallSize; ++i)
353 if ((OldBits >> i) & 1)
359 getPointer()->reserve(N);
366 setSmallBits(~uintptr_t(0));
374 assert(Idx <= static_cast<unsigned>(
375 std::numeric_limits<uintptr_t>::digits) &&
376 "undefined behavior");
377 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
380 getPointer()->set(Idx);
386 assert(I <=
E &&
"Attempted to set backwards range!");
387 assert(
E <=
size() &&
"Attempted to set out-of-bounds range!");
388 if (I ==
E)
return *
this;
390 uintptr_t EMask = ((uintptr_t)1) <<
E;
391 uintptr_t IMask = ((uintptr_t)1) <<
I;
392 uintptr_t
Mask = EMask - IMask;
393 setSmallBits(getSmallBits() | Mask);
395 getPointer()->set(I,
E);
403 getPointer()->reset();
409 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
411 getPointer()->reset(Idx);
417 assert(I <= E &&
"Attempted to reset backwards range!");
418 assert(E <=
size() &&
"Attempted to reset out-of-bounds range!");
419 if (I == E)
return *
this;
421 uintptr_t EMask = ((uintptr_t)1) <<
E;
422 uintptr_t IMask = ((uintptr_t)1) <<
I;
423 uintptr_t
Mask = EMask - IMask;
424 setSmallBits(getSmallBits() & ~Mask);
426 getPointer()->reset(I, E);
432 setSmallBits(~getSmallBits());
434 getPointer()->flip();
440 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
442 getPointer()->flip(Idx);
453 assert(Idx <
size() &&
"Out-of-bounds Bit access.");
458 assert(Idx <
size() &&
"Out-of-bounds Bit access.");
460 return ((getSmallBits() >> Idx) & 1) != 0;
461 return getPointer()->operator[](Idx);
464 bool test(
unsigned Idx)
const {
476 return (getSmallBits() & RHS.getSmallBits()) != 0;
478 return getPointer()->anyCommon(*RHS.getPointer());
480 for (
unsigned i = 0, e = std::min(
size(), RHS.
size()); i != e; ++i)
491 return getSmallBits() == RHS.getSmallBits();
493 return *getPointer() == *RHS.getPointer();
495 for (
size_t i = 0, e =
size(); i != e; ++i) {
496 if ((*
this)[i] != RHS[i])
504 return !(*
this == RHS);
512 setSmallBits(getSmallBits() & RHS.getSmallBits());
514 getPointer()->operator&=(*RHS.getPointer());
517 for (i = 0, e = std::min(
size(), RHS.
size()); i != e; ++i)
518 (*
this)[i] =
test(i) && RHS.
test(i);
519 for (e =
size(); i != e; ++i)
528 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
530 getPointer()->
reset(*RHS.getPointer());
532 for (
unsigned i = 0, e = std::min(
size(), RHS.
size()); i != e; ++i)
542 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
544 return getPointer()->test(*RHS.getPointer());
547 for (i = 0, e = std::min(
size(), RHS.
size()); i != e; ++i)
551 for (e =
size(); i != e; ++i)
561 setSmallBits(getSmallBits() | RHS.getSmallBits());
563 getPointer()->operator|=(*RHS.getPointer());
565 for (
size_t i = 0, e = RHS.
size(); i != e; ++i)
566 (*
this)[i] =
test(i) || RHS.
test(i);
574 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
576 getPointer()->operator^=(*RHS.getPointer());
578 for (
size_t i = 0, e = RHS.
size(); i != e; ++i)
579 (*
this)[i] =
test(i) != RHS.
test(i);
586 setSmallBits(getSmallBits() << N);
588 getPointer()->operator<<=(
N);
594 setSmallBits(getSmallBits() >> N);
596 getPointer()->operator>>=(
N);
606 switchToLarge(
new BitVector(*RHS.getPointer()));
609 *getPointer() = *RHS.getPointer();
634 applyMask<true, false>(
Mask, MaskWords);
636 getPointer()->setBitsInMask(Mask, MaskWords);
643 applyMask<false, false>(
Mask, MaskWords);
645 getPointer()->clearBitsInMask(Mask, MaskWords);
652 applyMask<true, true>(
Mask, MaskWords);
654 getPointer()->setBitsNotInMask(Mask, MaskWords);
661 applyMask<false, true>(
Mask, MaskWords);
663 getPointer()->clearBitsNotInMask(Mask, MaskWords);
667 template <
bool AddBits,
bool InvertMask>
668 void applyMask(
const uint32_t *
Mask,
unsigned MaskWords) {
669 assert(MaskWords <=
sizeof(uintptr_t) &&
"Mask is larger than base!");
670 uintptr_t M = Mask[0];
671 if (NumBaseBits == 64)
672 M |= uint64_t(Mask[1]) << 32;
676 setSmallBits(getSmallBits() | M);
678 setSmallBits(getSmallBits() & ~M);
715 #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.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
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)
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.
std::size_t 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...
SmallBitVector & flip(unsigned Idx)
bool test(unsigned Idx) const
SmallBitVector(unsigned s, bool t=false)
Creates a bitvector of specified number of bits.
std::size_t countTrailingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the least significant bit to the first zero bit.
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)
std::size_t 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...
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.
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.
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)
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.
std::size_t countLeadingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the most significant bit to the first zero bit.
size_type count() const
Returns the number of bits which are set.
SmallBitVector & operator|=(const SmallBitVector &RHS)
SmallBitVector(SmallBitVector &&RHS)