13 #ifndef LLVM_ADT_BITVECTOR_H 14 #define LLVM_ADT_BITVECTOR_H 32 const BitVectorT &Parent;
36 assert(Current != -1 &&
"Trying to advance past end.");
37 Current = Parent.find_next(Current);
42 : Parent(Parent), Current(Current) {}
61 assert(&Parent == &Other.Parent &&
62 "Comparing iterators from different BitVectors");
63 return Current == Other.Current;
67 assert(&Parent == &Other.Parent &&
68 "Comparing iterators from different BitVectors");
69 return Current != Other.Current;
74 typedef uintptr_t BitWord;
76 enum { BITWORD_SIZE = (
unsigned)
sizeof(BitWord) * CHAR_BIT };
78 static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
79 "Unsupported word size");
95 WordRef = &b.Bits[Idx / BITWORD_SIZE];
96 BitPos = Idx % BITWORD_SIZE;
109 *WordRef |= BitWord(1) << BitPos;
111 *WordRef &= ~(BitWord(1) << BitPos);
116 return ((*WordRef) & (BitWord(1) << BitPos)) != 0;
124 return const_set_bits_iterator(*
this);
127 return const_set_bits_iterator(*
this, -1);
130 return make_range(set_bits_begin(), set_bits_end());
138 explicit BitVector(
unsigned s,
bool t =
false) : Size(s) {
139 size_t Capacity = NumBitWords(s);
140 Bits = allocate(Capacity);
153 size_t Capacity = NumBitWords(RHS.
size());
154 Bits = allocate(Capacity);
155 std::memcpy(Bits.data(), RHS.Bits.
data(), Capacity *
sizeof(BitWord));
166 bool empty()
const {
return Size == 0; }
173 unsigned NumBits = 0;
174 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
181 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
189 for (
unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
190 if (Bits[i] != ~BitWord(0))
194 if (
unsigned Remainder = Size % BITWORD_SIZE)
195 return Bits[Size / BITWORD_SIZE] == (BitWord(1) << Remainder) - 1;
208 assert(Begin <= End && End <= Size);
212 unsigned FirstWord = Begin / BITWORD_SIZE;
213 unsigned LastWord = (End - 1) / BITWORD_SIZE;
216 for (
unsigned i = FirstWord; i <= LastWord; ++i) {
217 BitWord Copy = Bits[i];
219 if (i == FirstWord) {
220 unsigned FirstBit = Begin % BITWORD_SIZE;
221 Copy &= maskTrailingZeros<BitWord>(FirstBit);
225 unsigned LastBit = (End - 1) % BITWORD_SIZE;
226 Copy &= maskTrailingOnes<BitWord>(LastBit + 1);
237 assert(Begin <= End && End <= Size);
241 unsigned LastWord = (End - 1) / BITWORD_SIZE;
242 unsigned FirstWord = Begin / BITWORD_SIZE;
244 for (
unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
245 unsigned CurrentWord = i - 1;
247 BitWord Copy = Bits[CurrentWord];
248 if (CurrentWord == LastWord) {
249 unsigned LastBit = (End - 1) % BITWORD_SIZE;
250 Copy &= maskTrailingOnes<BitWord>(LastBit + 1);
253 if (CurrentWord == FirstWord) {
254 unsigned FirstBit = Begin % BITWORD_SIZE;
255 Copy &= maskTrailingZeros<BitWord>(FirstBit);
268 assert(Begin <= End && End <= Size);
272 unsigned FirstWord = Begin / BITWORD_SIZE;
273 unsigned LastWord = (End - 1) / BITWORD_SIZE;
276 for (
unsigned i = FirstWord; i <= LastWord; ++i) {
277 BitWord Copy = Bits[i];
279 if (i == FirstWord) {
280 unsigned FirstBit = Begin % BITWORD_SIZE;
281 Copy |= maskTrailingOnes<BitWord>(FirstBit);
285 unsigned LastBit = (End - 1) % BITWORD_SIZE;
286 Copy |= maskTrailingZeros<BitWord>(LastBit + 1);
288 if (Copy != ~BitWord(0)) {
290 return Result <
size() ? Result : -1;
299 assert(Begin <= End && End <= Size);
303 unsigned LastWord = (End - 1) / BITWORD_SIZE;
304 unsigned FirstWord = Begin / BITWORD_SIZE;
306 for (
unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
307 unsigned CurrentWord = i - 1;
309 BitWord Copy = Bits[CurrentWord];
310 if (CurrentWord == LastWord) {
311 unsigned LastBit = (End - 1) % BITWORD_SIZE;
312 Copy |= maskTrailingZeros<BitWord>(LastBit + 1);
315 if (CurrentWord == FirstWord) {
316 unsigned FirstBit = Begin % BITWORD_SIZE;
317 Copy |= maskTrailingOnes<BitWord>(FirstBit);
320 if (Copy != ~BitWord(0)) {
323 return Result < Size ? Result : -1;
339 int find_next(
unsigned Prev)
const {
return find_first_in(Prev + 1, Size); }
343 int find_prev(
unsigned PriorTo)
const {
return find_last_in(0, PriorTo); }
352 return find_first_unset_in(Prev + 1, Size);
362 return find_last_unset_in(0, PriorTo);
372 if (N > getBitCapacity()) {
373 unsigned OldCapacity = Bits.
size();
385 unsigned OldSize =
Size;
387 if (t || N < OldSize)
392 if (N > getBitCapacity())
398 init_words(Bits,
true);
404 assert(Bits.
data() &&
"Bits never allocated");
405 Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
411 assert(I <=
E &&
"Attempted to set backwards range!");
412 assert(
E <=
size() &&
"Attempted to set out-of-bounds range!");
414 if (I ==
E)
return *
this;
416 if (I / BITWORD_SIZE ==
E / BITWORD_SIZE) {
417 BitWord EMask = BitWord(1) << (
E % BITWORD_SIZE);
418 BitWord IMask = BitWord(1) << (I % BITWORD_SIZE);
419 BitWord
Mask = EMask - IMask;
420 Bits[I / BITWORD_SIZE] |=
Mask;
424 BitWord PrefixMask = ~BitWord(0) << (I % BITWORD_SIZE);
425 Bits[I / BITWORD_SIZE] |= PrefixMask;
428 for (; I + BITWORD_SIZE <=
E; I += BITWORD_SIZE)
429 Bits[I / BITWORD_SIZE] = ~BitWord(0);
431 BitWord PostfixMask = (BitWord(1) << (
E % BITWORD_SIZE)) - 1;
433 Bits[I / BITWORD_SIZE] |= PostfixMask;
439 init_words(Bits,
false);
444 Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
450 assert(I <= E &&
"Attempted to reset backwards range!");
451 assert(E <=
size() &&
"Attempted to reset out-of-bounds range!");
453 if (I == E)
return *
this;
455 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
456 BitWord EMask = BitWord(1) << (E % BITWORD_SIZE);
457 BitWord IMask = BitWord(1) << (I % BITWORD_SIZE);
458 BitWord
Mask = EMask - IMask;
459 Bits[I / BITWORD_SIZE] &= ~Mask;
463 BitWord PrefixMask = ~BitWord(0) << (I % BITWORD_SIZE);
464 Bits[I / BITWORD_SIZE] &= ~PrefixMask;
467 for (; I + BITWORD_SIZE <=
E; I += BITWORD_SIZE)
468 Bits[I / BITWORD_SIZE] = BitWord(0);
470 BitWord PostfixMask = (BitWord(1) << (E % BITWORD_SIZE)) - 1;
472 Bits[I / BITWORD_SIZE] &= ~PostfixMask;
478 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
485 Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
491 assert (Idx < Size &&
"Out-of-bounds Bit access.");
496 assert (Idx < Size &&
"Out-of-bounds Bit access.");
497 BitWord
Mask = BitWord(1) << (Idx % BITWORD_SIZE);
498 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
501 bool test(
unsigned Idx)
const {
507 unsigned OldSize =
Size;
508 unsigned NewSize = Size + 1;
512 if (NewSize > getBitCapacity())
513 resize(NewSize,
false);
524 unsigned ThisWords = NumBitWords(
size());
525 unsigned RHSWords = NumBitWords(RHS.
size());
526 for (
unsigned i = 0,
e = std::min(ThisWords, RHSWords); i !=
e; ++i)
527 if (Bits[i] & RHS.Bits[i])
534 unsigned ThisWords = NumBitWords(
size());
535 unsigned RHSWords = NumBitWords(RHS.
size());
537 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
538 if (Bits[i] != RHS.Bits[i])
542 if (i != ThisWords) {
543 for (; i != ThisWords; ++i)
546 }
else if (i != RHSWords) {
547 for (; i != RHSWords; ++i)
555 return !(*
this == RHS);
560 unsigned ThisWords = NumBitWords(
size());
561 unsigned RHSWords = NumBitWords(RHS.
size());
563 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
564 Bits[i] &= RHS.Bits[i];
569 for (; i != ThisWords; ++i)
577 unsigned ThisWords = NumBitWords(
size());
578 unsigned RHSWords = NumBitWords(RHS.
size());
580 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
581 Bits[i] &= ~RHS.Bits[i];
588 unsigned ThisWords = NumBitWords(
size());
589 unsigned RHSWords = NumBitWords(RHS.
size());
591 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
592 if ((Bits[i] & ~RHS.Bits[i]) != 0)
595 for (; i != ThisWords ; ++i)
605 for (
size_t i = 0,
e = NumBitWords(RHS.
size()); i !=
e; ++i)
606 Bits[i] |= RHS.Bits[i];
613 for (
size_t i = 0,
e = NumBitWords(RHS.
size()); i !=
e; ++i)
614 Bits[i] ^= RHS.Bits[i];
623 unsigned NumWords = NumBitWords(Size);
626 wordShr(N / BITWORD_SIZE);
628 unsigned BitDistance = N % BITWORD_SIZE;
629 if (BitDistance == 0)
654 const BitWord
Mask = maskTrailingOnes<BitWord>(BitDistance);
655 const unsigned LSH = BITWORD_SIZE - BitDistance;
657 for (
unsigned I = 0; I < NumWords - 1; ++
I) {
658 Bits[
I] >>= BitDistance;
659 Bits[
I] |= (Bits[I + 1] &
Mask) << LSH;
662 Bits[NumWords - 1] >>= BitDistance;
672 unsigned NumWords = NumBitWords(Size);
675 wordShl(N / BITWORD_SIZE);
677 unsigned BitDistance = N % BITWORD_SIZE;
678 if (BitDistance == 0)
704 const BitWord
Mask = maskLeadingOnes<BitWord>(BitDistance);
705 const unsigned RSH = BITWORD_SIZE - BitDistance;
707 for (
int I = NumWords - 1; I > 0; --
I) {
708 Bits[
I] <<= BitDistance;
709 Bits[
I] |= (Bits[I - 1] &
Mask) >> RSH;
711 Bits[0] <<= BitDistance;
719 if (
this == &RHS)
return *
this;
722 unsigned RHSWords = NumBitWords(Size);
723 if (Size <= getBitCapacity()) {
725 std::memcpy(Bits.
data(), RHS.Bits.
data(), RHSWords *
sizeof(BitWord));
731 unsigned NewCapacity = RHSWords;
732 assert(NewCapacity > 0 &&
"negative capacity?");
733 auto NewBits = allocate(NewCapacity);
734 std::memcpy(NewBits.data(), RHS.Bits.
data(), NewCapacity *
sizeof(BitWord));
737 std::free(Bits.
data());
744 if (
this == &RHS)
return *
this;
746 std::free(Bits.
data());
776 applyMask<true, false>(
Mask, MaskWords);
782 applyMask<false, false>(
Mask, MaskWords);
788 applyMask<true, true>(
Mask, MaskWords);
794 applyMask<false, true>(
Mask, MaskWords);
816 uint32_t NumWords = NumBitWords(Size);
824 std::memmove(Dest.begin(), Src.begin(), Dest.size() *
sizeof(BitWord));
825 std::memset(Bits.
data(), 0, Count *
sizeof(BitWord));
836 uint32_t NumWords = NumBitWords(Size);
840 assert(Dest.size() == Src.size());
842 std::memmove(Dest.begin(), Src.begin(), Dest.size() *
sizeof(BitWord));
843 std::memset(Dest.end(), 0, Count *
sizeof(BitWord));
847 BitWord *RawBits =
static_cast<BitWord *
>(
852 int next_unset_in_word(
int WordIndex, BitWord
Word)
const {
854 return Result <
size() ? Result : -1;
857 unsigned NumBitWords(
unsigned S)
const {
858 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
862 void set_unused_bits(
bool t =
true) {
864 unsigned UsedWords = NumBitWords(Size);
865 if (Bits.
size() > UsedWords)
869 unsigned ExtraBits = Size % BITWORD_SIZE;
871 BitWord ExtraBitMask = ~BitWord(0) << ExtraBits;
873 Bits[UsedWords-1] |= ExtraBitMask;
875 Bits[UsedWords-1] &= ~ExtraBitMask;
880 void clear_unused_bits() {
881 set_unused_bits(
false);
884 void grow(
unsigned NewSize) {
885 size_t NewCapacity = std::max<size_t>(NumBitWords(NewSize), Bits.
size() * 2);
886 assert(NewCapacity > 0 &&
"realloc-ing zero space");
887 BitWord *NewBits =
static_cast<BitWord *
>(
895 memset(B.
data(), 0 - (int)t, B.
size() *
sizeof(BitWord));
898 template<
bool AddBits,
bool InvertMask>
899 void applyMask(
const uint32_t *
Mask,
unsigned MaskWords) {
900 static_assert(BITWORD_SIZE % 32 == 0,
"Unsupported BitWord size.");
901 MaskWords = std::min(MaskWords, (
size() + 31) / 32);
902 const unsigned Scale = BITWORD_SIZE / 32;
904 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
905 BitWord BW = Bits[i];
907 for (
unsigned b = 0; b != BITWORD_SIZE; b += 32) {
909 if (InvertMask) M = ~M;
910 if (AddBits) BW |= BitWord(M) << b;
911 else BW &= ~(BitWord(M) << b);
915 for (
unsigned b = 0; MaskWords; b += 32, --MaskWords) {
917 if (InvertMask) M = ~M;
918 if (AddBits) Bits[i] |= BitWord(M) << b;
919 else Bits[i] &= ~(BitWord(M) << b);
945 #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
#define LLVM_UNLIKELY(EXPR)
int find_last() const
find_last - Returns the index of the last set bit, -1 if none of the bits are set.
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...
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
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)
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...
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
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")
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)
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...
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.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
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)
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 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
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsInMask - Add '1' bits from Mask to this vector.
const BitVector & operator=(BitVector &&RHS)