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;
140 size_t Capacity = NumBitWords(s);
141 Bits = allocate(Capacity);
154 size_t Capacity = NumBitWords(RHS.
size());
155 Bits = allocate(Capacity);
156 std::memcpy(Bits.data(), RHS.Bits.
data(), Capacity *
sizeof(BitWord));
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;
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);
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);
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;
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;
374 unsigned OldCapacity =
Bits.size();
376 init_words(
Bits.drop_front(OldCapacity),
t);
386 unsigned OldSize =
Size;
388 if (
t || N < OldSize)
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;
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 ThisWords = NumBitWords(
size());
509 unsigned RHSWords = NumBitWords(RHS.
size());
510 for (
unsigned i = 0, e =
std::min(ThisWords, RHSWords); i != e; ++i)
511 if (
Bits[i] & RHS.Bits[i])
518 unsigned ThisWords = NumBitWords(
size());
519 unsigned RHSWords = NumBitWords(RHS.
size());
521 for (i = 0; i !=
std::min(ThisWords, RHSWords); ++i)
522 if (
Bits[i] != RHS.Bits[i])
526 if (i != ThisWords) {
527 for (; i != ThisWords; ++i)
530 }
else if (i != RHSWords) {
531 for (; i != RHSWords; ++i)
539 return !(*
this == RHS);
544 unsigned ThisWords = NumBitWords(
size());
545 unsigned RHSWords = NumBitWords(RHS.
size());
547 for (i = 0; i !=
std::min(ThisWords, RHSWords); ++i)
548 Bits[i] &= RHS.Bits[i];
553 for (; i != ThisWords; ++i)
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];
572 unsigned ThisWords = NumBitWords(
size());
573 unsigned RHSWords = NumBitWords(RHS.
size());
575 for (i = 0; i !=
std::min(ThisWords, RHSWords); ++i)
576 if ((
Bits[i] & ~RHS.Bits[i]) != 0)
579 for (; i != ThisWords ; ++i)
589 for (
size_t i = 0, e = NumBitWords(RHS.
size()); i != e; ++i)
590 Bits[i] |= RHS.Bits[i];
597 for (
size_t i = 0, e = NumBitWords(RHS.
size()); i != e; ++i)
598 Bits[i] ^= RHS.Bits[i];
607 unsigned NumWords = NumBitWords(
Size);
610 wordShr(N / BITWORD_SIZE);
612 unsigned BitDistance = N % BITWORD_SIZE;
613 if (BitDistance == 0)
638 const BitWord
Mask = maskTrailingOnes<BitWord>(BitDistance);
639 const unsigned LSH = BITWORD_SIZE - BitDistance;
641 for (
unsigned I = 0;
I < NumWords - 1; ++
I) {
642 Bits[
I] >>= BitDistance;
646 Bits[NumWords - 1] >>= BitDistance;
656 unsigned NumWords = NumBitWords(
Size);
659 wordShl(N / BITWORD_SIZE);
661 unsigned BitDistance = N % BITWORD_SIZE;
662 if (BitDistance == 0)
688 const BitWord
Mask = maskLeadingOnes<BitWord>(BitDistance);
689 const unsigned RSH = BITWORD_SIZE - BitDistance;
691 for (
int I = NumWords - 1;
I > 0; --
I) {
692 Bits[
I] <<= BitDistance;
695 Bits[0] <<= BitDistance;
703 if (
this == &RHS)
return *
this;
706 unsigned RHSWords = NumBitWords(
Size);
709 std::memcpy(
Bits.data(), RHS.Bits.
data(), RHSWords *
sizeof(BitWord));
715 unsigned NewCapacity = RHSWords;
716 assert(NewCapacity > 0 &&
"negative capacity?");
717 auto NewBits = allocate(NewCapacity);
718 std::memcpy(NewBits.data(), RHS.Bits.
data(), NewCapacity *
sizeof(BitWord));
721 std::free(
Bits.data());
728 if (
this == &RHS)
return *
this;
730 std::free(
Bits.data());
760 applyMask<true, false>(
Mask, MaskWords);
766 applyMask<false, false>(
Mask, MaskWords);
772 applyMask<true, true>(
Mask, MaskWords);
778 applyMask<false, true>(
Mask, MaskWords);
802 auto Src =
Bits.take_front(NumWords).drop_back(Count);
803 auto Dest =
Bits.take_front(NumWords).drop_front(Count);
808 std::memmove(Dest.begin(), Src.begin(), Dest.size() *
sizeof(BitWord));
809 std::memset(
Bits.data(), 0, Count *
sizeof(BitWord));
822 auto Src =
Bits.take_front(NumWords).drop_front(Count);
823 auto Dest =
Bits.take_front(NumWords).drop_back(Count);
824 assert(Dest.size() == Src.size());
826 std::memmove(Dest.begin(), Src.begin(), Dest.size() *
sizeof(BitWord));
827 std::memset(Dest.end(), 0, Count *
sizeof(BitWord));
830 MutableArrayRef<BitWord> allocate(
size_t NumWords) {
831 BitWord *RawBits = (BitWord *)std::malloc(NumWords *
sizeof(BitWord));
832 return MutableArrayRef<BitWord>(RawBits, NumWords);
835 int next_unset_in_word(
int WordIndex, BitWord
Word)
const {
837 return Result <
size() ? Result : -1;
840 unsigned NumBitWords(
unsigned S)
const {
841 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
845 void set_unused_bits(
bool t =
true) {
847 unsigned UsedWords = NumBitWords(
Size);
848 if (
Bits.size() > UsedWords)
849 init_words(
Bits.drop_front(UsedWords),
t);
852 unsigned ExtraBits =
Size % BITWORD_SIZE;
854 BitWord ExtraBitMask = ~0UL << ExtraBits;
856 Bits[UsedWords-1] |= ExtraBitMask;
858 Bits[UsedWords-1] &= ~ExtraBitMask;
863 void clear_unused_bits() {
864 set_unused_bits(
false);
867 void grow(
unsigned NewSize) {
868 size_t NewCapacity = std::max<size_t>(NumBitWords(NewSize),
Bits.size() * 2);
869 assert(NewCapacity > 0 &&
"realloc-ing zero space");
871 (BitWord *)std::realloc(
Bits.data(), NewCapacity *
sizeof(BitWord));
872 Bits = MutableArrayRef<BitWord>(NewBits, NewCapacity);
876 void init_words(MutableArrayRef<BitWord>
B,
bool t) {
878 memset(B.data(), 0 - (int)t, B.size() *
sizeof(BitWord));
881 template<
bool AddBits,
bool InvertMask>
882 void applyMask(
const uint32_t *
Mask,
unsigned MaskWords) {
883 static_assert(BITWORD_SIZE % 32 == 0,
"Unsupported BitWord size.");
885 const unsigned Scale = BITWORD_SIZE / 32;
887 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
888 BitWord BW =
Bits[i];
890 for (
unsigned b = 0; b != BITWORD_SIZE; b += 32) {
892 if (InvertMask) M = ~M;
893 if (AddBits) BW |= BitWord(M) << b;
894 else BW &= ~(BitWord(M) << b);
898 for (
unsigned b = 0; MaskWords; b += 32, --MaskWords) {
900 if (InvertMask) M = ~M;
901 if (AddBits)
Bits[i] |= BitWord(M) << b;
902 else Bits[i] &= ~(BitWord(M) << b);
928 #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.
const_set_bits_iterator set_bits_end() const
void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
clearBitsInMask - Clear any bits in this vector that are set in Mask.
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).
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
size_type size() const
size - Returns the number of bits in this bitvector.
bool none() const
none - Returns true if none of the bits are set.
#define LLVM_UNLIKELY(EXPR)
bool operator==(const const_set_bits_iterator_impl &Other) const
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
iterator_range< const_set_bits_iterator > set_bits() const
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
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). ...
BitVector & set(unsigned Idx)
const_set_bits_iterator_impl operator++(int)
static size_t capacity_in_bytes(const BitVector &X)
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 any() const
any - Returns true if any bit is set.
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_last() const
find_last - Returns the index of the last set bit, -1 if none of the bits are set.
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...
BitVector & operator<<=(unsigned N)
void clear()
clear - Removes all bits from the bitvector. Does not change capacity.
int find_prev_unset(unsigned PriorTo)
find_prev_unset - Returns the index of the first unset bit that precedes the bit at PriorTo...
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.
support::ulittle32_t Word
void swap(BitVector &RHS)
BitVector & operator&=(const BitVector &RHS)
Intersection, union, disjoint union.
reference(BitVector &b, unsigned Idx)
const_set_bits_iterator_impl(const BitVectorT &Parent, int Current)
unsigned operator*() const
int find_prev(unsigned PriorTo) const
find_prev - Returns the index of the first set bit that precedes the the bit at PriorTo.
BitVector & operator|=(const BitVector &RHS)
bool all() const
all - Returns true if all bits are set.
BitVector()
BitVector default ctor - Creates an empty bitvector.
bool operator[](unsigned Idx) const
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.
size_type count() const
count - Returns the number of bits which are set.
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...
BitVector & set(unsigned I, unsigned E)
set - Efficiently set a range of bits in [I, E)
bool empty() const
empty - Tests whether there are no bits in this bitvector.
BitVector & operator^=(const BitVector &RHS)
BitVector(unsigned s, bool t=false)
BitVector ctor - Creates a bitvector of specified number of bits.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
const_set_bits_iterator set_bits_begin() const
static const unsigned End
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
const_set_bits_iterator set_iterator
ForwardIterator for the bits that are set.
BitVector(BitVector &&RHS)
BitVector & reset(unsigned I, unsigned E)
reset - Efficiently reset a range of bits in [I, E)
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).
const BitVector & operator=(const BitVector &RHS)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
unsigned countPopulation(T Value)
Count the number of set bits in a value.
const_set_bits_iterator_impl(const BitVectorT &Parent)
reference operator[](unsigned Idx)
bool test(unsigned Idx) const
int find_first_unset() const
find_first_unset - Returns the index of the first unset bit, -1 if all of the bits are set...
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
A range adaptor for a pair of iterators.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
int find_last_unset() const
find_last_unset - Returns the index of the last unset bit, -1 if all of the bits are set...
bool operator!=(const const_set_bits_iterator_impl &Other) const
bool test(const BitVector &RHS) const
test - Check if (This - RHS) is zero.
reference & operator=(bool t)
reference & operator=(reference t)
const_set_bits_iterator_impl< BitVector > const_set_bits_iterator
BitVector & reset(unsigned Idx)
bool operator!=(const BitVector &RHS) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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)...
int find_next_unset(unsigned Prev) const
find_next_unset - Returns the index of the next unset bit following the "Prev" 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.
size_t getBitCapacity() const
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.
size_t getMemorySize() const
Return the size (in bytes) of the bit vector.
const BitVector & operator=(BitVector &&RHS)