14 #ifndef LLVM_ADT_BITVECTOR_H 15 #define LLVM_ADT_BITVECTOR_H 33 const BitVectorT &Parent;
37 assert(Current != -1 &&
"Trying to advance past end.");
38 Current = Parent.find_next(Current);
43 : Parent(Parent), Current(Current) {}
62 assert(&Parent == &Other.Parent &&
63 "Comparing iterators from different BitVectors");
64 return Current == Other.Current;
68 assert(&Parent == &Other.Parent &&
69 "Comparing iterators from different BitVectors");
70 return Current != Other.Current;
75 typedef unsigned long BitWord;
77 enum { BITWORD_SIZE = (
unsigned)
sizeof(BitWord) * CHAR_BIT };
79 static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
80 "Unsupported word size");
96 WordRef = &b.Bits[Idx / BITWORD_SIZE];
97 BitPos = Idx % BITWORD_SIZE;
110 *WordRef |= BitWord(1) << BitPos;
112 *WordRef &= ~(BitWord(1) << BitPos);
117 return ((*WordRef) & (BitWord(1) << BitPos)) != 0;
125 return const_set_bits_iterator(*
this);
128 return const_set_bits_iterator(*
this, -1);
131 return make_range(set_bits_begin(), set_bits_end());
139 explicit BitVector(
unsigned s,
bool t =
false) : Size(s) {
140 size_t Capacity = NumBitWords(s);
141 Bits = allocate(Capacity);
154 size_t Capacity = NumBitWords(RHS.
size());
155 Bits = allocate(Capacity);
167 bool empty()
const {
return Size == 0; }
174 unsigned NumBits = 0;
175 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
182 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
190 for (
unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
195 if (
unsigned Remainder = Size % BITWORD_SIZE)
196 return Bits[Size / BITWORD_SIZE] == (1UL << Remainder) - 1;
209 assert(Begin <= End && End <= Size);
213 unsigned FirstWord = Begin / BITWORD_SIZE;
214 unsigned LastWord = (End - 1) / BITWORD_SIZE;
217 for (
unsigned i = FirstWord; i <= LastWord; ++i) {
218 BitWord Copy = Bits[i];
220 if (i == FirstWord) {
221 unsigned FirstBit = Begin % BITWORD_SIZE;
222 Copy &= maskTrailingZeros<BitWord>(FirstBit);
226 unsigned LastBit = (End - 1) % BITWORD_SIZE;
227 Copy &= maskTrailingOnes<BitWord>(LastBit + 1);
238 assert(Begin <= End && End <= Size);
242 unsigned LastWord = (End - 1) / BITWORD_SIZE;
243 unsigned FirstWord = Begin / BITWORD_SIZE;
245 for (
unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
246 unsigned CurrentWord = i - 1;
248 BitWord Copy = Bits[CurrentWord];
249 if (CurrentWord == LastWord) {
250 unsigned LastBit = (End - 1) % BITWORD_SIZE;
251 Copy &= maskTrailingOnes<BitWord>(LastBit + 1);
254 if (CurrentWord == FirstWord) {
255 unsigned FirstBit = Begin % BITWORD_SIZE;
256 Copy &= maskTrailingZeros<BitWord>(FirstBit);
269 assert(Begin <= End && End <= Size);
273 unsigned FirstWord = Begin / BITWORD_SIZE;
274 unsigned LastWord = (End - 1) / BITWORD_SIZE;
277 for (
unsigned i = FirstWord; i <= LastWord; ++i) {
278 BitWord Copy = Bits[i];
280 if (i == FirstWord) {
281 unsigned FirstBit = Begin % BITWORD_SIZE;
282 Copy |= maskTrailingOnes<BitWord>(FirstBit);
286 unsigned LastBit = (End - 1) % BITWORD_SIZE;
287 Copy |= maskTrailingZeros<BitWord>(LastBit + 1);
291 return Result <
size() ? Result : -1;
300 assert(Begin <= End && End <= Size);
304 unsigned LastWord = (End - 1) / BITWORD_SIZE;
305 unsigned FirstWord = Begin / BITWORD_SIZE;
307 for (
unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
308 unsigned CurrentWord = i - 1;
310 BitWord Copy = Bits[CurrentWord];
311 if (CurrentWord == LastWord) {
312 unsigned LastBit = (End - 1) % BITWORD_SIZE;
313 Copy |= maskTrailingZeros<BitWord>(LastBit + 1);
316 if (CurrentWord == FirstWord) {
317 unsigned FirstBit = Begin % BITWORD_SIZE;
318 Copy |= maskTrailingOnes<BitWord>(FirstBit);
324 return Result < Size ? Result : -1;
340 int find_next(
unsigned Prev)
const {
return find_first_in(Prev + 1, Size); }
344 int find_prev(
unsigned PriorTo)
const {
return find_last_in(0, PriorTo); }
353 return find_first_unset_in(Prev + 1, Size);
363 return find_last_unset_in(0, PriorTo);
373 if (N > getBitCapacity()) {
374 unsigned OldCapacity = Bits.
size();
386 unsigned OldSize =
Size;
388 if (t || N < OldSize)
393 if (N > getBitCapacity())
399 init_words(Bits,
true);
405 assert(Bits.
data() &&
"Bits never allocated");
406 Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
412 assert(I <=
E &&
"Attempted to set backwards range!");
413 assert(
E <=
size() &&
"Attempted to set out-of-bounds range!");
415 if (I ==
E)
return *
this;
417 if (I / BITWORD_SIZE ==
E / BITWORD_SIZE) {
418 BitWord EMask = 1UL << (
E % BITWORD_SIZE);
419 BitWord IMask = 1UL << (I % BITWORD_SIZE);
420 BitWord
Mask = EMask - IMask;
421 Bits[I / BITWORD_SIZE] |=
Mask;
425 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
426 Bits[I / BITWORD_SIZE] |= PrefixMask;
429 for (; I + BITWORD_SIZE <=
E; I += BITWORD_SIZE)
430 Bits[I / BITWORD_SIZE] = ~0UL;
432 BitWord PostfixMask = (1UL << (
E % BITWORD_SIZE)) - 1;
434 Bits[I / BITWORD_SIZE] |= PostfixMask;
440 init_words(Bits,
false);
445 Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
451 assert(I <= E &&
"Attempted to reset backwards range!");
452 assert(E <=
size() &&
"Attempted to reset out-of-bounds range!");
454 if (I == E)
return *
this;
456 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
457 BitWord EMask = 1UL << (E % BITWORD_SIZE);
458 BitWord IMask = 1UL << (I % BITWORD_SIZE);
459 BitWord
Mask = EMask - IMask;
460 Bits[I / BITWORD_SIZE] &= ~Mask;
464 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
465 Bits[I / BITWORD_SIZE] &= ~PrefixMask;
468 for (; I + BITWORD_SIZE <=
E; I += BITWORD_SIZE)
469 Bits[I / BITWORD_SIZE] = 0UL;
471 BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
473 Bits[I / BITWORD_SIZE] &= ~PostfixMask;
479 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
486 Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
492 assert (Idx < Size &&
"Out-of-bounds Bit access.");
497 assert (Idx < Size &&
"Out-of-bounds Bit access.");
498 BitWord
Mask = BitWord(1) << (Idx % BITWORD_SIZE);
499 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
502 bool test(
unsigned Idx)
const {
508 unsigned OldSize =
Size;
509 unsigned NewSize = Size + 1;
513 if (NewSize > getBitCapacity())
514 resize(NewSize,
false);
525 unsigned ThisWords = NumBitWords(
size());
526 unsigned RHSWords = NumBitWords(RHS.
size());
527 for (
unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
528 if (Bits[i] & RHS.Bits[i])
535 unsigned ThisWords = NumBitWords(
size());
536 unsigned RHSWords = NumBitWords(RHS.
size());
538 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
539 if (Bits[i] != RHS.Bits[i])
543 if (i != ThisWords) {
544 for (; i != ThisWords; ++i)
547 }
else if (i != RHSWords) {
548 for (; i != RHSWords; ++i)
556 return !(*
this == RHS);
561 unsigned ThisWords = NumBitWords(
size());
562 unsigned RHSWords = NumBitWords(RHS.
size());
564 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
565 Bits[i] &= RHS.Bits[i];
570 for (; i != ThisWords; ++i)
578 unsigned ThisWords = NumBitWords(
size());
579 unsigned RHSWords = NumBitWords(RHS.
size());
581 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
582 Bits[i] &= ~RHS.Bits[i];
589 unsigned ThisWords = NumBitWords(
size());
590 unsigned RHSWords = NumBitWords(RHS.
size());
592 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
593 if ((Bits[i] & ~RHS.Bits[i]) != 0)
596 for (; i != ThisWords ; ++i)
606 for (
size_t i = 0, e = NumBitWords(RHS.
size()); i != e; ++i)
607 Bits[i] |= RHS.Bits[i];
614 for (
size_t i = 0, e = NumBitWords(RHS.
size()); i != e; ++i)
615 Bits[i] ^= RHS.Bits[i];
624 unsigned NumWords = NumBitWords(Size);
627 wordShr(N / BITWORD_SIZE);
629 unsigned BitDistance = N % BITWORD_SIZE;
630 if (BitDistance == 0)
655 const BitWord
Mask = maskTrailingOnes<BitWord>(BitDistance);
656 const unsigned LSH = BITWORD_SIZE - BitDistance;
658 for (
unsigned I = 0; I < NumWords - 1; ++
I) {
659 Bits[
I] >>= BitDistance;
660 Bits[
I] |= (Bits[I + 1] &
Mask) << LSH;
663 Bits[NumWords - 1] >>= BitDistance;
673 unsigned NumWords = NumBitWords(Size);
676 wordShl(N / BITWORD_SIZE);
678 unsigned BitDistance = N % BITWORD_SIZE;
679 if (BitDistance == 0)
705 const BitWord
Mask = maskLeadingOnes<BitWord>(BitDistance);
706 const unsigned RSH = BITWORD_SIZE - BitDistance;
708 for (
int I = NumWords - 1; I > 0; --
I) {
709 Bits[
I] <<= BitDistance;
710 Bits[
I] |= (Bits[I - 1] &
Mask) >> RSH;
712 Bits[0] <<= BitDistance;
720 if (
this == &RHS)
return *
this;
723 unsigned RHSWords = NumBitWords(Size);
724 if (Size <= getBitCapacity()) {
732 unsigned NewCapacity = RHSWords;
733 assert(NewCapacity > 0 &&
"negative capacity?");
734 auto NewBits = allocate(NewCapacity);
735 std::memcpy(NewBits.data(), RHS.Bits.
data(), NewCapacity *
sizeof(BitWord));
738 std::free(Bits.
data());
745 if (
this == &RHS)
return *
this;
747 std::free(Bits.
data());
777 applyMask<true, false>(
Mask, MaskWords);
783 applyMask<false, false>(
Mask, MaskWords);
789 applyMask<true, true>(
Mask, MaskWords);
795 applyMask<false, true>(
Mask, MaskWords);
817 uint32_t NumWords = NumBitWords(Size);
825 std::memmove(Dest.begin(), Src.begin(), Dest.size() *
sizeof(BitWord));
837 uint32_t NumWords = NumBitWords(Size);
841 assert(Dest.size() == Src.size());
843 std::memmove(Dest.begin(), Src.begin(), Dest.size() *
sizeof(BitWord));
844 std::memset(Dest.end(), 0, Count *
sizeof(BitWord));
848 BitWord *RawBits =
static_cast<BitWord *
>(
853 int next_unset_in_word(
int WordIndex, BitWord
Word)
const {
855 return Result <
size() ? Result : -1;
858 unsigned NumBitWords(
unsigned S)
const {
859 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
863 void set_unused_bits(
bool t =
true) {
865 unsigned UsedWords = NumBitWords(Size);
866 if (Bits.
size() > UsedWords)
870 unsigned ExtraBits = Size % BITWORD_SIZE;
872 BitWord ExtraBitMask = ~0UL << ExtraBits;
874 Bits[UsedWords-1] |= ExtraBitMask;
876 Bits[UsedWords-1] &= ~ExtraBitMask;
881 void clear_unused_bits() {
882 set_unused_bits(
false);
885 void grow(
unsigned NewSize) {
886 size_t NewCapacity = std::max<size_t>(NumBitWords(NewSize), Bits.
size() * 2);
887 assert(NewCapacity > 0 &&
"realloc-ing zero space");
888 BitWord *NewBits =
static_cast<BitWord *
>(
899 template<
bool AddBits,
bool InvertMask>
900 void applyMask(
const uint32_t *
Mask,
unsigned MaskWords) {
901 static_assert(BITWORD_SIZE % 32 == 0,
"Unsupported BitWord size.");
902 MaskWords = std::min(MaskWords, (
size() + 31) / 32);
903 const unsigned Scale = BITWORD_SIZE / 32;
905 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
906 BitWord BW = Bits[i];
908 for (
unsigned b = 0; b != BITWORD_SIZE; b += 32) {
910 if (InvertMask) M = ~M;
911 if (AddBits) BW |= BitWord(M) << b;
912 else BW &= ~(BitWord(M) << b);
916 for (
unsigned b = 0; MaskWords; b += 32, --MaskWords) {
918 if (InvertMask) M = ~M;
919 if (AddBits) Bits[i] |= BitWord(M) << b;
920 else Bits[i] &= ~(BitWord(M) << b);
946 #endif // LLVM_ADT_BITVECTOR_H BitVector & reset(const BitVector &RHS)
reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
clearBitsInMask - Clear any bits in this vector that are set in Mask.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
int find_last_unset() const
find_last_unset - Returns the index of the last unset bit, -1 if all of the bits are set...
This class represents lattice values for constants.
bool empty() const
empty - Tests whether there are no bits in this bitvector.
const_set_bits_iterator set_bits_end() const
int find_last() const
find_last - Returns the index of the last set bit, -1 if none of the bits are set.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
bool all() const
all - Returns true if all bits are set.
bool test(unsigned Idx) const
const_set_bits_iterator_impl operator++(int)
BitVector(const BitVector &RHS)
BitVector copy ctor.
void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
bool operator[](unsigned Idx) const
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
int find_next_unset(unsigned Prev) const
find_next_unset - Returns the index of the next unset bit following the "Prev" bit.
BitVector & operator>>=(unsigned N)
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...
#define LLVM_UNLIKELY(EXPR)
BitVector & operator<<=(unsigned N)
void clear()
clear - Removes all bits from the bitvector. Does not change capacity.
size_t getMemorySize() const
Return the size (in bytes) of the bit vector.
int find_prev_unset(unsigned PriorTo)
find_prev_unset - Returns the index of the first unset bit that precedes the bit at PriorTo...
bool operator==(const const_set_bits_iterator_impl &Other) const
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.
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
size_t capacity_in_bytes(const BitVector &X)
support::ulittle32_t Word
void swap(BitVector &RHS)
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
reference(BitVector &b, unsigned Idx)
const_set_bits_iterator_impl(const BitVectorT &Parent, int Current)
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
BitVector & operator|=(const BitVector &RHS)
int find_first_unset() const
find_first_unset - Returns the index of the first unset bit, -1 if all of the bits are set...
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_realloc(void *Ptr, size_t Sz)
int find_first_unset_in(unsigned Begin, unsigned End) const
find_first_unset_in - Returns the index of the first unset bit in the range [Begin, End).
BitVector()
BitVector default ctor - Creates an empty bitvector.
BitVector & flip(unsigned Idx)
const_set_bits_iterator_impl & operator++()
void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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...
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
int find_prev(unsigned PriorTo) const
find_prev - Returns the index of the first set bit that precedes the the bit at PriorTo.
size_t getBitCapacity() const
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool any() const
any - Returns true if any bit is set.
BitVector & operator^=(const BitVector &RHS)
BitVector(unsigned s, bool t=false)
BitVector ctor - Creates a bitvector of specified number of bits.
const_set_bits_iterator set_iterator
const_set_bits_iterator set_bits_begin() const
ForwardIterator for the bits that are set.
BitVector(BitVector &&RHS)
bool operator==(const BitVector &RHS) const
BitVector & reset(unsigned I, unsigned E)
reset - Efficiently reset a range of bits in [I, E)
MutableArrayRef< T > drop_back(size_t N=1) const
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
const BitVector & operator=(const BitVector &RHS)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
unsigned countPopulation(T Value)
Count the number of set bits in a value.
const_set_bits_iterator_impl(const BitVectorT &Parent)
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
int find_last_in(unsigned Begin, unsigned End) const
find_last_in - Returns the index of the last set bit in the range [Begin, End).
size_type count() const
count - Returns the number of bits which are set.
reference operator[](unsigned Idx)
unsigned operator*() const
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
A range adaptor for a pair of iterators.
bool none() const
none - Returns true if none of the bits are set.
MutableArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
reference & operator=(bool t)
int find_first_in(unsigned Begin, unsigned End) const
find_first_in - Returns the index of the first set bit in the range [Begin, End). ...
reference & operator=(reference t)
size_type size() const
size - Returns the number of bits in this bitvector.
const_set_bits_iterator_impl< BitVector > const_set_bits_iterator
bool operator!=(const const_set_bits_iterator_impl &Other) const
BitVector & reset(unsigned Idx)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator_range< const_set_bits_iterator > set_bits() const
E & operator &=(E &LHS, E RHS)
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 test(const BitVector &RHS) const
test - Check if (This - RHS) is zero.
int find_last_unset_in(unsigned Begin, unsigned End) const
find_last_unset_in - Returns the index of the last unset bit in the range [Begin, End)...
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
bool operator!=(const BitVector &RHS) const
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.
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsInMask - Add '1' bits from Mask to this vector.
const BitVector & operator=(BitVector &&RHS)