14 #ifndef LLVM_ADT_BITVECTOR_H
15 #define LLVM_ADT_BITVECTOR_H
28 typedef unsigned long BitWord;
30 enum { BITWORD_SIZE = (
unsigned)
sizeof(BitWord) * CHAR_BIT };
48 WordRef = &b.Bits[Idx / BITWORD_SIZE];
49 BitPos = Idx % BITWORD_SIZE;
61 *WordRef |= 1L << BitPos;
63 *WordRef &= ~(1L << BitPos);
68 return ((*WordRef) & (1L << BitPos)) ?
true :
false;
80 explicit BitVector(
unsigned s,
bool t =
false) : Size(s) {
81 Capacity = NumBitWords(s);
83 init_words(
Bits, Capacity, t);
96 Capacity = NumBitWords(RHS.
size());
98 std::memcpy(Bits, RHS.Bits, Capacity *
sizeof(BitWord));
101 #if LLVM_HAS_RVALUE_REFERENCES
103 :
Bits(RHS.
Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
113 bool empty()
const {
return Size == 0; }
116 unsigned size()
const {
return Size; }
120 unsigned NumBits = 0;
121 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
122 if (
sizeof(BitWord) == 4)
124 else if (
sizeof(BitWord) == 8)
133 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
141 for (
unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
146 if (
unsigned Remainder = Size % BITWORD_SIZE)
147 return Bits[Size / BITWORD_SIZE] == (1UL << Remainder) - 1;
160 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
162 if (
sizeof(BitWord) == 4)
164 if (
sizeof(BitWord) == 8)
178 unsigned WordPos = Prev / BITWORD_SIZE;
179 unsigned BitPos = Prev % BITWORD_SIZE;
180 BitWord Copy =
Bits[WordPos];
182 Copy &= ~0UL << BitPos;
185 if (
sizeof(BitWord) == 4)
187 if (
sizeof(BitWord) == 8)
193 for (
unsigned i = WordPos+1; i < NumBitWords(
size()); ++i)
195 if (
sizeof(BitWord) == 4)
197 if (
sizeof(BitWord) == 8)
211 if (N > Capacity * BITWORD_SIZE) {
212 unsigned OldCapacity = Capacity;
214 init_words(&
Bits[OldCapacity], (Capacity-OldCapacity), t);
224 unsigned OldSize = Size;
226 if (t || N < OldSize)
231 if (N > Capacity * BITWORD_SIZE)
237 init_words(
Bits, Capacity,
true);
243 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
249 assert(I <= E &&
"Attempted to set backwards range!");
250 assert(E <=
size() &&
"Attempted to set out-of-bounds range!");
252 if (I == E)
return *
this;
254 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
255 BitWord EMask = 1UL << (E % BITWORD_SIZE);
256 BitWord IMask = 1UL << (I % BITWORD_SIZE);
257 BitWord Mask = EMask - IMask;
258 Bits[I / BITWORD_SIZE] |= Mask;
262 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
263 Bits[I / BITWORD_SIZE] |= PrefixMask;
266 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
267 Bits[I / BITWORD_SIZE] = ~0UL;
269 BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
270 Bits[I / BITWORD_SIZE] |= PostfixMask;
276 init_words(
Bits, Capacity,
false);
281 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
287 assert(I <= E &&
"Attempted to reset backwards range!");
288 assert(E <=
size() &&
"Attempted to reset out-of-bounds range!");
290 if (I == E)
return *
this;
292 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
293 BitWord EMask = 1UL << (E % BITWORD_SIZE);
294 BitWord IMask = 1UL << (I % BITWORD_SIZE);
295 BitWord Mask = EMask - IMask;
296 Bits[I / BITWORD_SIZE] &= ~Mask;
300 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
301 Bits[I / BITWORD_SIZE] &= ~PrefixMask;
304 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
305 Bits[I / BITWORD_SIZE] = 0UL;
307 BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
308 Bits[I / BITWORD_SIZE] &= ~PostfixMask;
314 for (
unsigned i = 0; i < NumBitWords(
size()); ++i)
321 Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
327 assert (Idx < Size &&
"Out-of-bounds Bit access.");
332 assert (Idx < Size &&
"Out-of-bounds Bit access.");
333 BitWord Mask = 1L << (Idx % BITWORD_SIZE);
334 return (
Bits[Idx / BITWORD_SIZE] & Mask) != 0;
337 bool test(
unsigned Idx)
const {
343 unsigned ThisWords = NumBitWords(
size());
344 unsigned RHSWords = NumBitWords(RHS.
size());
345 for (
unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
346 if (
Bits[i] & RHS.Bits[i])
353 unsigned ThisWords = NumBitWords(
size());
354 unsigned RHSWords = NumBitWords(RHS.
size());
356 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
357 if (
Bits[i] != RHS.Bits[i])
361 if (i != ThisWords) {
362 for (; i != ThisWords; ++i)
365 }
else if (i != RHSWords) {
366 for (; i != RHSWords; ++i)
374 return !(*
this == RHS);
379 unsigned ThisWords = NumBitWords(
size());
380 unsigned RHSWords = NumBitWords(RHS.
size());
382 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
383 Bits[i] &= RHS.Bits[i];
388 for (; i != ThisWords; ++i)
396 unsigned ThisWords = NumBitWords(
size());
397 unsigned RHSWords = NumBitWords(RHS.
size());
399 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
400 Bits[i] &= ~RHS.Bits[i];
407 unsigned ThisWords = NumBitWords(
size());
408 unsigned RHSWords = NumBitWords(RHS.
size());
410 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
411 if ((
Bits[i] & ~RHS.Bits[i]) != 0)
414 for (; i != ThisWords ; ++i)
424 for (
size_t i = 0, e = NumBitWords(RHS.
size()); i != e; ++i)
425 Bits[i] |= RHS.Bits[i];
432 for (
size_t i = 0, e = NumBitWords(RHS.
size()); i != e; ++i)
433 Bits[i] ^= RHS.Bits[i];
439 if (
this == &RHS)
return *
this;
442 unsigned RHSWords = NumBitWords(Size);
443 if (Size <= Capacity * BITWORD_SIZE) {
452 BitWord *NewBits = (BitWord *)
std::malloc(Capacity *
sizeof(BitWord));
453 std::memcpy(NewBits, RHS.Bits, Capacity *
sizeof(BitWord));
462 #if LLVM_HAS_RVALUE_REFERENCES
464 if (
this == &RHS)
return *
this;
469 Capacity = RHS.Capacity;
498 applyMask<true, false>(Mask, MaskWords);
504 applyMask<false, false>(Mask, MaskWords);
510 applyMask<true, true>(Mask, MaskWords);
516 applyMask<false, true>(Mask, MaskWords);
520 unsigned NumBitWords(
unsigned S)
const {
521 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
525 void set_unused_bits(
bool t =
true) {
527 unsigned UsedWords = NumBitWords(Size);
528 if (Capacity > UsedWords)
529 init_words(&
Bits[UsedWords], (Capacity-UsedWords), t);
532 unsigned ExtraBits = Size % BITWORD_SIZE;
534 BitWord ExtraBitMask = ~0UL << ExtraBits;
536 Bits[UsedWords-1] |= ExtraBitMask;
538 Bits[UsedWords-1] &= ~ExtraBitMask;
543 void clear_unused_bits() {
544 set_unused_bits(
false);
547 void grow(
unsigned NewSize) {
548 Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
554 void init_words(BitWord *B,
unsigned NumWords,
bool t) {
555 memset(B, 0 - (
int)t, NumWords*
sizeof(BitWord));
558 template<
bool AddBits,
bool InvertMask>
559 void applyMask(
const uint32_t *Mask,
unsigned MaskWords) {
560 assert(BITWORD_SIZE % 32 == 0 &&
"Unsupported BitWord size.");
561 MaskWords = std::min(MaskWords, (
size() + 31) / 32);
562 const unsigned Scale = BITWORD_SIZE / 32;
564 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
565 BitWord BW =
Bits[i];
567 for (
unsigned b = 0; b != BITWORD_SIZE; b += 32) {
568 uint32_t M = *Mask++;
569 if (InvertMask) M = ~M;
570 if (AddBits) BW |= BitWord(M) << b;
571 else BW &= ~(BitWord(M) << b);
575 for (
unsigned b = 0; MaskWords; b += 32, --MaskWords) {
576 uint32_t M = *Mask++;
577 if (InvertMask) M = ~M;
578 if (AddBits)
Bits[i] |= BitWord(M) << b;
579 else Bits[i] &= ~(BitWord(M) << b);
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)
bool none() const
none - Returns true if none of the bits are set.
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
int find_next(unsigned Prev) const
BitVector & set(unsigned Idx)
BitVector(const BitVector &RHS)
BitVector copy ctor.
void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
bool any() const
any - Returns true if any bit is set.
void clear()
clear - Clear all bits.
#define llvm_unreachable(msg)
void swap(BitVector &RHS)
BitVector & operator&=(const BitVector &RHS)
Intersection, union, disjoint union.
reference(BitVector &b, unsigned Idx)
BitVector & operator|=(const BitVector &RHS)
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, std::size_t >::type 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 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)
unsigned count() const
count - Returns the number of bits which are set.
void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
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)
void *realloc(void *ptr, size_t size);
BitVector(unsigned s, bool t=false)
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
unsigned CountPopulation_64(uint64_t Value)
BitVector & reset(unsigned I, unsigned E)
reset - Efficiently reset a range of bits in [I, E)
const BitVector & operator=(const BitVector &RHS)
unsigned CountPopulation_32(uint32_t Value)
reference operator[](unsigned Idx)
bool test(unsigned Idx) const
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
void *malloc(size_t size);
bool test(const BitVector &RHS) const
reference & operator=(bool t)
reference & operator=(reference t)
BitVector & reset(unsigned Idx)
bool operator!=(const BitVector &RHS) const
bool operator==(const BitVector &RHS) const
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
unsigned size() const
size - Returns the number of bits in this bitvector.