14#ifndef LLVM_ADT_BITVECTOR_H
15#define LLVM_ADT_BITVECTOR_H
36 const BitVectorT &Parent;
40 assert(Current != -1 &&
"Trying to advance past end.");
41 Current = Parent.find_next(Current);
46 Current = Parent.find_last();
48 Current = Parent.find_prev(Current);
60 : Parent(Parent), Current(Current) {}
91 "Comparing iterators from different BitVectors");
92 return Current ==
Other.Current;
97 "Comparing iterators from different BitVectors");
98 return Current !=
Other.Current;
105 enum { BITWORD_SIZE = (
unsigned)
sizeof(BitWord) * CHAR_BIT };
107 static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
108 "Unsupported word size");
126 WordRef = &b.Bits[Idx / BITWORD_SIZE];
127 BitPos = Idx % BITWORD_SIZE;
140 *WordRef |= BitWord(1) << BitPos;
142 *WordRef &= ~(BitWord(1) << BitPos);
147 return ((*WordRef) & (BitWord(1) << BitPos)) != 0;
170 : Bits(NumBitWords(s), 0 - (BitWord)t), Size(s) {
176 bool empty()
const {
return Size == 0; }
183 unsigned NumBits = 0;
184 for (
auto Bit : Bits)
191 return any_of(Bits, [](BitWord Bit) {
return Bit != 0; });
196 for (
unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
197 if (Bits[i] != ~BitWord(0))
201 if (
unsigned Remainder = Size % BITWORD_SIZE)
202 return Bits[Size / BITWORD_SIZE] == (BitWord(1) << Remainder) - 1;
219 unsigned FirstWord = Begin / BITWORD_SIZE;
220 unsigned LastWord = (
End - 1) / BITWORD_SIZE;
227 for (
unsigned i = FirstWord; i <= LastWord; ++i) {
228 BitWord Copy = Bits[i];
232 if (i == FirstWord) {
233 unsigned FirstBit = Begin % BITWORD_SIZE;
238 unsigned LastBit = (
End - 1) % BITWORD_SIZE;
254 unsigned LastWord = (
End - 1) / BITWORD_SIZE;
255 unsigned FirstWord = Begin / BITWORD_SIZE;
257 for (
unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
258 unsigned CurrentWord = i - 1;
260 BitWord Copy = Bits[CurrentWord];
261 if (CurrentWord == LastWord) {
262 unsigned LastBit = (
End - 1) % BITWORD_SIZE;
266 if (CurrentWord == FirstWord) {
267 unsigned FirstBit = Begin % BITWORD_SIZE;
291 unsigned LastWord = (
End - 1) / BITWORD_SIZE;
292 unsigned FirstWord = Begin / BITWORD_SIZE;
294 for (
unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
295 unsigned CurrentWord = i - 1;
297 BitWord Copy = Bits[CurrentWord];
298 if (CurrentWord == LastWord) {
299 unsigned LastBit = (
End - 1) % BITWORD_SIZE;
303 if (CurrentWord == FirstWord) {
304 unsigned FirstBit = Begin % BITWORD_SIZE;
308 if (Copy != ~BitWord(0)) {
311 return Result < Size ? Result : -1;
359 Bits.resize(NumBitWords(
N), 0 - BitWord(t));
364 void reserve(
unsigned N) { Bits.reserve(NumBitWords(
N)); }
375 assert(Idx < Size &&
"access in bound");
376 Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
382 assert(
I <=
E &&
"Attempted to set backwards range!");
383 assert(
E <=
size() &&
"Attempted to set out-of-bounds range!");
385 if (
I ==
E)
return *
this;
387 if (
I / BITWORD_SIZE ==
E / BITWORD_SIZE) {
388 BitWord EMask = BitWord(1) << (
E % BITWORD_SIZE);
389 BitWord IMask = BitWord(1) << (
I % BITWORD_SIZE);
390 BitWord Mask = EMask - IMask;
391 Bits[
I / BITWORD_SIZE] |= Mask;
395 BitWord PrefixMask = ~BitWord(0) << (
I % BITWORD_SIZE);
396 Bits[
I / BITWORD_SIZE] |= PrefixMask;
399 for (;
I + BITWORD_SIZE <=
E;
I += BITWORD_SIZE)
400 Bits[
I / BITWORD_SIZE] = ~BitWord(0);
402 BitWord PostfixMask = (BitWord(1) << (
E % BITWORD_SIZE)) - 1;
404 Bits[
I / BITWORD_SIZE] |= PostfixMask;
417 Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
423 assert(
I <=
E &&
"Attempted to reset backwards range!");
424 assert(
E <=
size() &&
"Attempted to reset out-of-bounds range!");
426 if (
I ==
E)
return *
this;
428 if (
I / BITWORD_SIZE ==
E / BITWORD_SIZE) {
429 BitWord EMask = BitWord(1) << (
E % BITWORD_SIZE);
430 BitWord IMask = BitWord(1) << (
I % BITWORD_SIZE);
431 BitWord Mask = EMask - IMask;
432 Bits[
I / BITWORD_SIZE] &= ~Mask;
436 BitWord PrefixMask = ~BitWord(0) << (
I % BITWORD_SIZE);
437 Bits[
I / BITWORD_SIZE] &= ~PrefixMask;
440 for (;
I + BITWORD_SIZE <=
E;
I += BITWORD_SIZE)
441 Bits[
I / BITWORD_SIZE] = BitWord(0);
443 BitWord PostfixMask = (BitWord(1) << (
E % BITWORD_SIZE)) - 1;
445 Bits[
I / BITWORD_SIZE] &= ~PostfixMask;
452 for (
auto &Bit : Bits)
460 Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
466 assert (Idx < Size &&
"Out-of-bounds Bit access.");
471 assert (Idx < Size &&
"Out-of-bounds Bit access.");
472 BitWord Mask = BitWord(1) << (Idx % BITWORD_SIZE);
473 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
478 assert(!
empty() &&
"Getting last element of empty vector.");
479 return (*
this)[
size() - 1];
483 bool test(
unsigned Idx)
const {
489 for (
unsigned i = Begin; i <
End; ++i) {
498 for (
unsigned i = Begin; i <
End; ++i) {
507 unsigned OldSize = Size;
508 unsigned NewSize = Size + 1;
524 assert(!
empty() &&
"Empty vector has no element to pop.");
530 unsigned ThisWords = Bits.size();
531 unsigned RHSWords =
RHS.Bits.size();
532 for (
unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
533 if (Bits[i] &
RHS.Bits[i])
542 unsigned NumWords = Bits.size();
543 return std::equal(Bits.begin(), Bits.begin() + NumWords,
RHS.Bits.begin());
550 unsigned ThisWords = Bits.
size();
551 unsigned RHSWords =
RHS.Bits.size();
553 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
554 Bits[i] &=
RHS.Bits[i];
559 for (; i != ThisWords; ++i)
567 unsigned ThisWords = Bits.
size();
568 unsigned RHSWords =
RHS.Bits.size();
569 for (
unsigned i = 0; i != std::min(ThisWords, RHSWords); ++i)
570 Bits[i] &= ~
RHS.Bits[i];
577 unsigned ThisWords = Bits.size();
578 unsigned RHSWords =
RHS.Bits.size();
580 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
581 if ((Bits[i] & ~
RHS.Bits[i]) != 0)
584 for (; i != ThisWords ; ++i)
594 template <
class F,
class... ArgTys>
596 ArgTys
const &...Args) {
597 assert(((Arg.
size() == Args.size()) && ...) &&
"consistent sizes");
598 Out.resize(Arg.
size());
600 Out.Bits[
I] = f(Arg.Bits[
I], Args.Bits[
I]...);
601 Out.clear_unused_bits();
610 Bits[
I] |=
RHS.Bits[
I];
619 Bits[
I] ^=
RHS.Bits[
I];
628 unsigned NumWords = Bits.size();
631 wordShr(
N / BITWORD_SIZE);
633 unsigned BitDistance =
N % BITWORD_SIZE;
634 if (BitDistance == 0)
660 const unsigned LSH = BITWORD_SIZE - BitDistance;
662 for (
unsigned I = 0;
I < NumWords - 1; ++
I) {
663 Bits[
I] >>= BitDistance;
664 Bits[
I] |= (Bits[
I + 1] & Mask) << LSH;
667 Bits[NumWords - 1] >>= BitDistance;
677 unsigned NumWords = Bits.size();
680 wordShl(
N / BITWORD_SIZE);
682 unsigned BitDistance =
N % BITWORD_SIZE;
683 if (BitDistance == 0)
710 const unsigned RSH = BITWORD_SIZE - BitDistance;
712 for (
int I = NumWords - 1;
I > 0; --
I) {
713 Bits[
I] <<= BitDistance;
714 Bits[
I] |= (Bits[
I - 1] & Mask) >> RSH;
716 Bits[0] <<= BitDistance;
744 applyMask<true, false>(Mask, MaskWords);
750 applyMask<false, false>(Mask, MaskWords);
756 applyMask<true, true>(Mask, MaskWords);
762 applyMask<false, true>(Mask, MaskWords);
789 std::copy(Bits.begin(), Bits.begin() + NumWords -
Count,
790 Bits.begin() +
Count);
791 std::fill(Bits.begin(), Bits.begin() +
Count, 0);
804 std::copy(Bits.begin() + Count, Bits.begin() + NumWords, Bits.begin());
805 std::fill(Bits.begin() + NumWords - Count, Bits.begin() + NumWords, 0);
808 unsigned NumBitWords(
unsigned S)
const {
809 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
813 void set_unused_bits(
bool t =
true) {
815 if (
unsigned ExtraBits = Size % BITWORD_SIZE) {
816 BitWord ExtraBitMask = ~BitWord(0) << ExtraBits;
818 Bits.back() |= ExtraBitMask;
820 Bits.back() &= ~ExtraBitMask;
825 void clear_unused_bits() {
826 set_unused_bits(
false);
829 void init_words(
bool t) {
llvm::fill(Bits, 0 - (BitWord)t); }
831 template<
bool AddBits,
bool InvertMask>
832 void applyMask(
const uint32_t *Mask,
unsigned MaskWords) {
833 static_assert(BITWORD_SIZE % 32 == 0,
"Unsupported BitWord size.");
834 MaskWords = std::min(MaskWords, (
size() + 31) / 32);
835 const unsigned Scale = BITWORD_SIZE / 32;
837 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
838 BitWord BW =
Bits[i];
840 for (
unsigned b = 0;
b != BITWORD_SIZE;
b += 32) {
841 uint32_t
M = *
Mask++;
842 if (InvertMask)
M = ~M;
843 if (AddBits) BW |= BitWord(M) <<
b;
844 else BW &= ~(BitWord(M) <<
b);
848 for (
unsigned b = 0; MaskWords;
b += 32, --MaskWords) {
849 uint32_t
M = *
Mask++;
850 if (InvertMask)
M = ~M;
851 if (AddBits)
Bits[i] |= BitWord(M) <<
b;
852 else Bits[i] &= ~(BitWord(M) <<
b);
865 return X.getMemorySize();
871 getHashValue(std::make_pair(
V.size(),
V.getData()));
static int alignTo(int Num, int PowOf2)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_UNLIKELY(EXPR)
#define LLVM_ATTRIBUTE_WARN_UNUSED
This file defines DenseMapInfo traits for DenseMap.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
reference & operator=(bool t)
reference(BitVector &b, unsigned Idx)
reference & operator=(reference t)
reference(const reference &)=default
BitVector & operator>>=(unsigned N)
bool test(unsigned Idx) const
Returns true if bit Idx is set.
BitVector & reset()
Reset all bits in the bitvector.
void swap(BitVector &RHS)
int find_first() const
Returns the index of the first set bit, -1 if none of the bits are set.
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
void clear()
Removes all bits from the bitvector.
bool test(const BitVector &RHS) const
Check if (This - RHS) is non-zero.
BitVector()=default
BitVector default ctor - Creates an empty bitvector.
bool back() const
Return the last element in the bitvector.
bool operator!=(const BitVector &RHS) const
void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
Clear a bit in this vector for every '0' bit in Mask.
int find_last() const
Returns the index of the last set bit, -1 if none of the bits are set.
int find_first_unset_in(unsigned Begin, unsigned End) const
Returns the index of the first unset bit in the range [Begin, End).
size_type count() const
Returns the number of bits which are set.
BitVector & operator<<=(unsigned N)
ArrayRef< BitWord > getData() const
const_set_bits_iterator set_bits_end() const
BitVector & reset(unsigned Idx)
Reset bit Idx in the bitvector.
BitVector & set()
Set all bits in the bitvector.
int find_last_unset() const
Returns the index of the last unset bit, -1 if all of the bits are set.
void reserve(unsigned N)
Reserve space for atleast N bits in the bitvector.
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
Add '1' bits from Mask to this vector.
const_set_bits_iterator set_iterator
bool any() const
Returns true if any bit is set.
bool all() const
Returns true if all bits are set.
BitVector(unsigned s, bool t=false)
BitVector ctor - Creates a bitvector of specified number of bits.
BitVector & reset(const BitVector &RHS)
Reset bits that are set in RHS. Same as *this &= ~RHS.
BitVector & operator|=(const BitVector &RHS)
Union of this bitvector with RHS.
void pop_back()
Pop one bit from the end of the vector.
void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
Clear any bits in this vector that are set in Mask.
int find_prev(unsigned PriorTo) const
Returns the index of the first set bit that precedes the bit at PriorTo.
const_set_bits_iterator_impl< BitVector > const_set_bits_iterator
int find_last_in(unsigned Begin, unsigned End) const
Returns the index of the last set bit in the range [Begin, End).
void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
Add a bit to this vector for every '0' bit in Mask.
BitVector & flip()
Flip all bits in the bitvector.
BitVector & reset(unsigned I, unsigned E)
Efficiently reset a range of bits in [I, E)
bool operator==(const BitVector &RHS) const
int find_next(unsigned Prev) const
Returns the index of the next set bit following the "Prev" bit.
bool none() const
Returns true if none of the bits are set.
const_set_bits_iterator set_bits_begin() const
iterator_range< const_set_bits_iterator > set_bits() const
BitVector & set(unsigned I, unsigned E)
Efficiently set a range of bits in [I, E)
bool test_any(unsigned Begin, unsigned End) const
Returns true if any of the bits in the range [Begin, End) are set.
size_type getBitCapacity() const
int find_first_in(unsigned Begin, unsigned End, bool Set=true) const
Returns the index of the first set/unset bit, depending on Set, in the range [Begin,...
size_type size() const
Returns the number of bits in this bitvector.
BitVector & operator^=(const BitVector &RHS)
Disjoint union of this bitvector with RHS.
BitVector & flip(unsigned Idx)
Flip bit Idx in the bitvector.
bool test_all(unsigned Begin, unsigned End) const
Returns true if all bits in the range [Begin, End) are set.
size_type getMemorySize() const
Return the size (in bytes) of the bit vector.
static BitVector & apply(F &&f, BitVector &Out, BitVector const &Arg, ArgTys const &...Args)
bool empty() const
Returns whether there are no bits in this bitvector.
int find_next_unset(unsigned Prev) const
Returns the index of the next unset bit following the Prev bit.
BitVector & set(unsigned Idx)
int find_prev_unset(unsigned PriorTo) const
Returns the index of the first unset bit that precedes the bit at PriorTo.
BitVector & operator&=(const BitVector &RHS)
Intersection of this bitvector with RHS.
int find_first_unset() const
Returns the index of the first unset bit, -1 if all of the bits are set.
bool subsetOf(const BitVector &RHS) const
Check if This is a subset of RHS.
int find_last_unset_in(unsigned Begin, unsigned End) const
Returns the index of the last unset bit in the range [Begin, End).
bool operator[](unsigned Idx) const
reference operator[](unsigned Idx)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
ForwardIterator for the bits that are set.
const_set_bits_iterator_impl operator--(int)
const_set_bits_iterator_impl(const BitVectorT &Parent)
std::bidirectional_iterator_tag iterator_category
bool operator==(const const_set_bits_iterator_impl &Other) const
const_set_bits_iterator_impl(const const_set_bits_iterator_impl &)=default
const_set_bits_iterator_impl & operator++()
const_set_bits_iterator_impl & operator--()
const_set_bits_iterator_impl operator++(int)
std::ptrdiff_t difference_type
const_set_bits_iterator_impl(const BitVectorT &Parent, int Current)
bool operator!=(const const_set_bits_iterator_impl &Other) const
const value_type * pointer
unsigned operator*() const
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
constexpr uint32_t applyMask(uint32_t Mask, uint32_t Data)
Spread data bits into instruction word according to mask.
This is an optimization pass for GlobalISel generic memory operations.
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
BitVector::size_type capacity_in_bytes(const BitVector &X)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
constexpr T maskLeadingOnes(unsigned N)
Create a bitmask with the N left-most bits set to 1, and all other bits set to 0.
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
int countl_one(T Value)
Count the number of ones from the most significant bit to the first zero bit.
constexpr T maskTrailingZeros(unsigned N)
Create a bitmask with the N right-most bits set to 0, and all other bits set to 1.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
@ End
Run the machine verifier once, at the end of the pipeline.
Implement std::hash so that hash_code can be used in STL containers.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
static bool isEqual(const BitVector &LHS, const BitVector &RHS)
static unsigned getHashValue(const BitVector &V)
An information struct used to provide DenseMap with the various necessary components for a given valu...