14 #ifndef LLVM_ADT_BITVECTOR_H
15 #define LLVM_ADT_BITVECTOR_H
29 typedef unsigned long BitWord;
31 enum { BITWORD_SIZE = (
unsigned)
sizeof(BitWord) * CHAR_BIT };
33 static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
34 "Unsupported word size");
51 WordRef = &b.Bits[Idx / BITWORD_SIZE];
52 BitPos = Idx % BITWORD_SIZE;
65 *WordRef |= BitWord(1) << BitPos;
67 *WordRef &= ~(BitWord(1) << BitPos);
72 return ((*WordRef) & (BitWord(1) << BitPos)) != 0;
84 explicit BitVector(
unsigned s,
bool t =
false) : Size(s) {
85 Capacity = NumBitWords(s);
86 Bits = (BitWord *)std::malloc(Capacity *
sizeof(BitWord));
87 init_words(
Bits, Capacity,
t);
100 Capacity = NumBitWords(RHS.
size());
101 Bits = (BitWord *)std::malloc(Capacity *
sizeof(BitWord));
102 std::memcpy(Bits, RHS.Bits, Capacity *
sizeof(BitWord));
106 :
Bits(RHS.
Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
108 RHS.Size = RHS.Capacity = 0;
116 bool empty()
const {
return Size == 0; }
123 unsigned NumBits = 0;
124 for (
unsigned i = 0;
i < NumBitWords(
size()); ++
i)
131 for (
unsigned i = 0;
i < NumBitWords(
size()); ++
i)
139 for (
unsigned i = 0;
i < Size / BITWORD_SIZE; ++
i)
144 if (
unsigned Remainder = Size % BITWORD_SIZE)
145 return Bits[Size / BITWORD_SIZE] == (1UL << Remainder) - 1;
158 for (
unsigned i = 0;
i < NumBitWords(
size()); ++
i)
171 unsigned WordPos = Prev / BITWORD_SIZE;
172 unsigned BitPos = Prev % BITWORD_SIZE;
173 BitWord Copy =
Bits[WordPos];
175 Copy &= ~0UL << BitPos;
181 for (
unsigned i = WordPos+1;
i < NumBitWords(
size()); ++
i)
194 if (N > Capacity * BITWORD_SIZE) {
195 unsigned OldCapacity = Capacity;
197 init_words(&
Bits[OldCapacity], (Capacity-OldCapacity),
t);
207 unsigned OldSize = Size;
209 if (
t || N < OldSize)
214 if (N > Capacity * BITWORD_SIZE)
220 init_words(
Bits, Capacity,
true);
227 Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
233 assert(I <= E &&
"Attempted to set backwards range!");
234 assert(E <=
size() &&
"Attempted to set out-of-bounds range!");
236 if (I == E)
return *
this;
238 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
239 BitWord EMask = 1UL << (E % BITWORD_SIZE);
240 BitWord IMask = 1UL << (I % BITWORD_SIZE);
241 BitWord
Mask = EMask - IMask;
246 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
247 Bits[I / BITWORD_SIZE] |= PrefixMask;
250 for (; I + BITWORD_SIZE <=
E; I += BITWORD_SIZE)
251 Bits[I / BITWORD_SIZE] = ~0UL;
253 BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
255 Bits[I / BITWORD_SIZE] |= PostfixMask;
261 init_words(
Bits, Capacity,
false);
266 Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
272 assert(I <= E &&
"Attempted to reset backwards range!");
273 assert(E <=
size() &&
"Attempted to reset out-of-bounds range!");
275 if (I == E)
return *
this;
277 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
278 BitWord EMask = 1UL << (E % BITWORD_SIZE);
279 BitWord IMask = 1UL << (I % BITWORD_SIZE);
280 BitWord
Mask = EMask - IMask;
281 Bits[I / BITWORD_SIZE] &= ~Mask;
285 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
286 Bits[I / BITWORD_SIZE] &= ~PrefixMask;
289 for (; I + BITWORD_SIZE <=
E; I += BITWORD_SIZE)
290 Bits[I / BITWORD_SIZE] = 0UL;
292 BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
294 Bits[I / BITWORD_SIZE] &= ~PostfixMask;
300 for (
unsigned i = 0;
i < NumBitWords(
size()); ++
i)
307 Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
313 assert (Idx < Size &&
"Out-of-bounds Bit access.");
318 assert (Idx < Size &&
"Out-of-bounds Bit access.");
319 BitWord
Mask = BitWord(1) << (Idx % BITWORD_SIZE);
320 return (
Bits[Idx / BITWORD_SIZE] & Mask) != 0;
323 bool test(
unsigned Idx)
const {
329 unsigned ThisWords = NumBitWords(
size());
330 unsigned RHSWords = NumBitWords(RHS.
size());
331 for (
unsigned i = 0, e =
std::min(ThisWords, RHSWords);
i != e; ++
i)
332 if (
Bits[
i] & RHS.Bits[
i])
339 unsigned ThisWords = NumBitWords(
size());
340 unsigned RHSWords = NumBitWords(RHS.
size());
342 for (i = 0; i !=
std::min(ThisWords, RHSWords); ++
i)
343 if (
Bits[i] != RHS.Bits[i])
347 if (i != ThisWords) {
348 for (; i != ThisWords; ++
i)
351 }
else if (i != RHSWords) {
352 for (; i != RHSWords; ++
i)
360 return !(*
this == RHS);
365 unsigned ThisWords = NumBitWords(
size());
366 unsigned RHSWords = NumBitWords(RHS.
size());
368 for (i = 0; i !=
std::min(ThisWords, RHSWords); ++
i)
369 Bits[i] &= RHS.Bits[i];
374 for (; i != ThisWords; ++
i)
382 unsigned ThisWords = NumBitWords(
size());
383 unsigned RHSWords = NumBitWords(RHS.
size());
385 for (i = 0; i !=
std::min(ThisWords, RHSWords); ++
i)
386 Bits[i] &= ~RHS.Bits[i];
393 unsigned ThisWords = NumBitWords(
size());
394 unsigned RHSWords = NumBitWords(RHS.
size());
396 for (i = 0; i !=
std::min(ThisWords, RHSWords); ++
i)
397 if ((
Bits[i] & ~RHS.Bits[i]) != 0)
400 for (; i != ThisWords ; ++
i)
410 for (
size_t i = 0, e = NumBitWords(RHS.
size());
i != e; ++
i)
418 for (
size_t i = 0, e = NumBitWords(RHS.
size());
i != e; ++
i)
425 if (
this == &RHS)
return *
this;
428 unsigned RHSWords = NumBitWords(Size);
429 if (Size <= Capacity * BITWORD_SIZE) {
431 std::memcpy(
Bits, RHS.Bits, RHSWords *
sizeof(BitWord));
438 assert(Capacity > 0 &&
"negative capacity?");
439 BitWord *NewBits = (BitWord *)std::malloc(Capacity *
sizeof(BitWord));
440 std::memcpy(NewBits, RHS.Bits, Capacity *
sizeof(BitWord));
450 if (
this == &RHS)
return *
this;
455 Capacity = RHS.Capacity;
458 RHS.Size = RHS.Capacity = 0;
484 applyMask<true, false>(
Mask, MaskWords);
490 applyMask<false, false>(
Mask, MaskWords);
496 applyMask<true, true>(
Mask, MaskWords);
502 applyMask<false, true>(
Mask, MaskWords);
506 unsigned NumBitWords(
unsigned S)
const {
507 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
511 void set_unused_bits(
bool t =
true) {
513 unsigned UsedWords = NumBitWords(Size);
514 if (Capacity > UsedWords)
515 init_words(&
Bits[UsedWords], (Capacity-UsedWords),
t);
518 unsigned ExtraBits = Size % BITWORD_SIZE;
520 BitWord ExtraBitMask = ~0UL << ExtraBits;
522 Bits[UsedWords-1] |= ExtraBitMask;
524 Bits[UsedWords-1] &= ~ExtraBitMask;
529 void clear_unused_bits() {
530 set_unused_bits(
false);
533 void grow(
unsigned NewSize) {
534 Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
535 assert(Capacity > 0 &&
"realloc-ing zero space");
536 Bits = (BitWord *)std::realloc(
Bits, Capacity *
sizeof(BitWord));
541 void init_words(BitWord *
B,
unsigned NumWords,
bool t) {
542 memset(B, 0 - (
int)t, NumWords*
sizeof(BitWord));
545 template<
bool AddBits,
bool InvertMask>
546 void applyMask(
const uint32_t *
Mask,
unsigned MaskWords) {
547 static_assert(BITWORD_SIZE % 32 == 0,
"Unsupported BitWord size.");
549 const unsigned Scale = BITWORD_SIZE / 32;
551 for (i = 0; MaskWords >= Scale; ++
i, MaskWords -= Scale) {
552 BitWord BW =
Bits[
i];
554 for (
unsigned b = 0; b != BITWORD_SIZE; b += 32) {
556 if (InvertMask) M = ~M;
557 if (AddBits) BW |= BitWord(M) << b;
558 else BW &= ~(BitWord(M) << b);
562 for (
unsigned b = 0; MaskWords; b += 32, --MaskWords) {
564 if (InvertMask) M = ~M;
565 if (AddBits)
Bits[
i] |= BitWord(M) << b;
566 else Bits[
i] &= ~(BitWord(M) << b);
591 #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.
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.
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.
BitVector & set(unsigned Idx)
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...
void clear()
clear - Clear all bits.
void swap(BitVector &RHS)
BitVector & operator&=(const BitVector &RHS)
Intersection, union, disjoint union.
reference(BitVector &b, unsigned Idx)
BitVector & operator|=(const BitVector &RHS)
bool all() const
all - Returns true if all bits are set.
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
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 etc Experimental Use value profile to guide fuzzing Number of simultaneous worker processes to run the jobs If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
BitVector()
BitVector default ctor - Creates an empty bitvector.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
bool operator[](unsigned Idx) const
BitVector & flip(unsigned Idx)
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")
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
BitVector(BitVector &&RHS)
BitVector & reset(unsigned I, unsigned E)
reset - Efficiently reset a range of bits in [I, E)
const BitVector & operator=(const BitVector &RHS)
unsigned countPopulation(T Value)
Count the number of set bits in a 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.
bool test(const BitVector &RHS) const
test - Check if (This - RHS) is zero.
reference & operator=(bool t)
reference & operator=(reference t)
BitVector & reset(unsigned Idx)
bool operator!=(const BitVector &RHS) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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 operator==(const BitVector &RHS) const
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)