Thanks to visit codestin.com
Credit goes to llvm.org

LLVM 22.0.0git
BitVector.h
Go to the documentation of this file.
1//===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements the BitVector class.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_BITVECTOR_H
15#define LLVM_ADT_BITVECTOR_H
16
17#include "llvm/ADT/ArrayRef.h"
21#include <algorithm>
22#include <cassert>
23#include <climits>
24#include <cstdint>
25#include <cstdlib>
26#include <cstring>
27#include <iterator>
28#include <utility>
29
30namespace llvm {
31
32/// ForwardIterator for the bits that are set.
33/// Iterators get invalidated when resize / reserve is called.
34template <typename BitVectorT> class const_set_bits_iterator_impl {
35 const BitVectorT &Parent;
36 int Current = 0;
37
38 void advance() {
39 assert(Current != -1 && "Trying to advance past end.");
40 Current = Parent.find_next(Current);
41 }
42
43 void retreat() {
44 if (Current == -1) {
45 Current = Parent.find_last();
46 } else {
47 Current = Parent.find_prev(Current);
48 }
49 }
50
51public:
52 using iterator_category = std::bidirectional_iterator_tag;
53 using difference_type = std::ptrdiff_t;
55 using pointer = const value_type *;
57
58 const_set_bits_iterator_impl(const BitVectorT &Parent, int Current)
59 : Parent(Parent), Current(Current) {}
60 explicit const_set_bits_iterator_impl(const BitVectorT &Parent)
61 : const_set_bits_iterator_impl(Parent, Parent.find_first()) {}
63
65 auto Prev = *this;
66 advance();
67 return Prev;
68 }
69
71 advance();
72 return *this;
73 }
74
76 auto Prev = *this;
77 retreat();
78 return Prev;
79 }
80
82 retreat();
83 return *this;
84 }
85
86 unsigned operator*() const { return Current; }
87
89 assert(&Parent == &Other.Parent &&
90 "Comparing iterators from different BitVectors");
91 return Current == Other.Current;
92 }
93
95 assert(&Parent == &Other.Parent &&
96 "Comparing iterators from different BitVectors");
97 return Current != Other.Current;
98 }
99};
100
102 typedef uintptr_t BitWord;
103
104 enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
105
106 static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
107 "Unsupported word size");
108
109 using Storage = SmallVector<BitWord>;
110
111 Storage Bits; // Actual bits.
112 unsigned Size = 0; // Size of bitvector in bits.
113
114public:
116
117 // Encapsulation of a single bit.
118 class reference {
119
120 BitWord *WordRef;
121 unsigned BitPos;
122
123 public:
124 reference(BitVector &b, unsigned Idx) {
125 WordRef = &b.Bits[Idx / BITWORD_SIZE];
126 BitPos = Idx % BITWORD_SIZE;
127 }
128
129 reference() = delete;
130 reference(const reference&) = default;
131
133 *this = bool(t);
134 return *this;
135 }
136
138 if (t)
139 *WordRef |= BitWord(1) << BitPos;
140 else
141 *WordRef &= ~(BitWord(1) << BitPos);
142 return *this;
143 }
144
145 operator bool() const {
146 return ((*WordRef) & (BitWord(1) << BitPos)) != 0;
147 }
148 };
149
152
157 return const_set_bits_iterator(*this, -1);
158 }
162
163 /// BitVector default ctor - Creates an empty bitvector.
164 BitVector() = default;
165
166 /// BitVector ctor - Creates a bitvector of specified number of bits. All
167 /// bits are initialized to the specified value.
168 explicit BitVector(unsigned s, bool t = false)
169 : Bits(NumBitWords(s), 0 - (BitWord)t), Size(s) {
170 if (t)
171 clear_unused_bits();
172 }
173
174 /// empty - Tests whether there are no bits in this bitvector.
175 bool empty() const { return Size == 0; }
176
177 /// size - Returns the number of bits in this bitvector.
178 size_type size() const { return Size; }
179
180 /// count - Returns the number of bits which are set.
181 size_type count() const {
182 unsigned NumBits = 0;
183 for (auto Bit : Bits)
184 NumBits += llvm::popcount(Bit);
185 return NumBits;
186 }
187
188 /// any - Returns true if any bit is set.
189 bool any() const {
190 return any_of(Bits, [](BitWord Bit) { return Bit != 0; });
191 }
192
193 /// all - Returns true if all bits are set.
194 bool all() const {
195 for (unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
196 if (Bits[i] != ~BitWord(0))
197 return false;
198
199 // If bits remain check that they are ones. The unused bits are always zero.
200 if (unsigned Remainder = Size % BITWORD_SIZE)
201 return Bits[Size / BITWORD_SIZE] == (BitWord(1) << Remainder) - 1;
202
203 return true;
204 }
205
206 /// none - Returns true if none of the bits are set.
207 bool none() const {
208 return !any();
209 }
210
211 /// find_first_in - Returns the index of the first set / unset bit,
212 /// depending on \p Set, in the range [Begin, End).
213 /// Returns -1 if all bits in the range are unset / set.
214 int find_first_in(unsigned Begin, unsigned End, bool Set = true) const {
215 assert(Begin <= End && End <= Size);
216 if (Begin == End)
217 return -1;
218
219 unsigned FirstWord = Begin / BITWORD_SIZE;
220 unsigned LastWord = (End - 1) / BITWORD_SIZE;
221
222 // Check subsequent words.
223 // The code below is based on search for the first _set_ bit. If
224 // we're searching for the first _unset_, we just take the
225 // complement of each word before we use it and apply
226 // the same method.
227 for (unsigned i = FirstWord; i <= LastWord; ++i) {
228 BitWord Copy = Bits[i];
229 if (!Set)
230 Copy = ~Copy;
231
232 if (i == FirstWord) {
233 unsigned FirstBit = Begin % BITWORD_SIZE;
234 Copy &= maskTrailingZeros<BitWord>(FirstBit);
235 }
236
237 if (i == LastWord) {
238 unsigned LastBit = (End - 1) % BITWORD_SIZE;
239 Copy &= maskTrailingOnes<BitWord>(LastBit + 1);
240 }
241 if (Copy != 0)
242 return i * BITWORD_SIZE + llvm::countr_zero(Copy);
243 }
244 return -1;
245 }
246
247 /// find_last_in - Returns the index of the last set bit in the range
248 /// [Begin, End). Returns -1 if all bits in the range are unset.
249 int find_last_in(unsigned Begin, unsigned End) const {
250 assert(Begin <= End && End <= Size);
251 if (Begin == End)
252 return -1;
253
254 unsigned LastWord = (End - 1) / BITWORD_SIZE;
255 unsigned FirstWord = Begin / BITWORD_SIZE;
256
257 for (unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
258 unsigned CurrentWord = i - 1;
259
260 BitWord Copy = Bits[CurrentWord];
261 if (CurrentWord == LastWord) {
262 unsigned LastBit = (End - 1) % BITWORD_SIZE;
263 Copy &= maskTrailingOnes<BitWord>(LastBit + 1);
264 }
265
266 if (CurrentWord == FirstWord) {
267 unsigned FirstBit = Begin % BITWORD_SIZE;
268 Copy &= maskTrailingZeros<BitWord>(FirstBit);
269 }
270
271 if (Copy != 0)
272 return (CurrentWord + 1) * BITWORD_SIZE - llvm::countl_zero(Copy) - 1;
273 }
274
275 return -1;
276 }
277
278 /// find_first_unset_in - Returns the index of the first unset bit in the
279 /// range [Begin, End). Returns -1 if all bits in the range are set.
280 int find_first_unset_in(unsigned Begin, unsigned End) const {
281 return find_first_in(Begin, End, /* Set = */ false);
282 }
283
284 /// find_last_unset_in - Returns the index of the last unset bit in the
285 /// range [Begin, End). Returns -1 if all bits in the range are set.
286 int find_last_unset_in(unsigned Begin, unsigned End) const {
287 assert(Begin <= End && End <= Size);
288 if (Begin == End)
289 return -1;
290
291 unsigned LastWord = (End - 1) / BITWORD_SIZE;
292 unsigned FirstWord = Begin / BITWORD_SIZE;
293
294 for (unsigned i = LastWord + 1; i >= FirstWord + 1; --i) {
295 unsigned CurrentWord = i - 1;
296
297 BitWord Copy = Bits[CurrentWord];
298 if (CurrentWord == LastWord) {
299 unsigned LastBit = (End - 1) % BITWORD_SIZE;
300 Copy |= maskTrailingZeros<BitWord>(LastBit + 1);
301 }
302
303 if (CurrentWord == FirstWord) {
304 unsigned FirstBit = Begin % BITWORD_SIZE;
305 Copy |= maskTrailingOnes<BitWord>(FirstBit);
306 }
307
308 if (Copy != ~BitWord(0)) {
309 unsigned Result =
310 (CurrentWord + 1) * BITWORD_SIZE - llvm::countl_one(Copy) - 1;
311 return Result < Size ? Result : -1;
312 }
313 }
314 return -1;
315 }
316
317 /// find_first - Returns the index of the first set bit, -1 if none
318 /// of the bits are set.
319 int find_first() const { return find_first_in(0, Size); }
320
321 /// find_last - Returns the index of the last set bit, -1 if none of the bits
322 /// are set.
323 int find_last() const { return find_last_in(0, Size); }
324
325 /// find_next - Returns the index of the next set bit following the
326 /// "Prev" bit. Returns -1 if the next set bit is not found.
327 int find_next(unsigned Prev) const { return find_first_in(Prev + 1, Size); }
328
329 /// find_prev - Returns the index of the first set bit that precedes the
330 /// the bit at \p PriorTo. Returns -1 if all previous bits are unset.
331 int find_prev(unsigned PriorTo) const { return find_last_in(0, PriorTo); }
332
333 /// find_first_unset - Returns the index of the first unset bit, -1 if all
334 /// of the bits are set.
335 int find_first_unset() const { return find_first_unset_in(0, Size); }
336
337 /// find_next_unset - Returns the index of the next unset bit following the
338 /// "Prev" bit. Returns -1 if all remaining bits are set.
339 int find_next_unset(unsigned Prev) const {
340 return find_first_unset_in(Prev + 1, Size);
341 }
342
343 /// find_last_unset - Returns the index of the last unset bit, -1 if all of
344 /// the bits are set.
345 int find_last_unset() const { return find_last_unset_in(0, Size); }
346
347 /// find_prev_unset - Returns the index of the first unset bit that precedes
348 /// the bit at \p PriorTo. Returns -1 if all previous bits are set.
349 int find_prev_unset(unsigned PriorTo) const {
350 return find_last_unset_in(0, PriorTo);
351 }
352
353 /// clear - Removes all bits from the bitvector.
354 void clear() {
355 Size = 0;
356 Bits.clear();
357 }
358
359 /// resize - Grow or shrink the bitvector.
360 void resize(unsigned N, bool t = false) {
361 set_unused_bits(t);
362 Size = N;
363 Bits.resize(NumBitWords(N), 0 - BitWord(t));
364 clear_unused_bits();
365 }
366
367 void reserve(unsigned N) { Bits.reserve(NumBitWords(N)); }
368
369 // Set, reset, flip
371 init_words(true);
372 clear_unused_bits();
373 return *this;
374 }
375
376 BitVector &set(unsigned Idx) {
377 assert(Idx < Size && "access in bound");
378 Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
379 return *this;
380 }
381
382 /// set - Efficiently set a range of bits in [I, E)
383 BitVector &set(unsigned I, unsigned E) {
384 assert(I <= E && "Attempted to set backwards range!");
385 assert(E <= size() && "Attempted to set out-of-bounds range!");
386
387 if (I == E) return *this;
388
389 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
390 BitWord EMask = BitWord(1) << (E % BITWORD_SIZE);
391 BitWord IMask = BitWord(1) << (I % BITWORD_SIZE);
392 BitWord Mask = EMask - IMask;
393 Bits[I / BITWORD_SIZE] |= Mask;
394 return *this;
395 }
396
397 BitWord PrefixMask = ~BitWord(0) << (I % BITWORD_SIZE);
398 Bits[I / BITWORD_SIZE] |= PrefixMask;
399 I = alignTo(I, BITWORD_SIZE);
400
401 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
402 Bits[I / BITWORD_SIZE] = ~BitWord(0);
403
404 BitWord PostfixMask = (BitWord(1) << (E % BITWORD_SIZE)) - 1;
405 if (I < E)
406 Bits[I / BITWORD_SIZE] |= PostfixMask;
407
408 return *this;
409 }
410
412 init_words(false);
413 return *this;
414 }
415
416 BitVector &reset(unsigned Idx) {
417 Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
418 return *this;
419 }
420
421 /// reset - Efficiently reset a range of bits in [I, E)
422 BitVector &reset(unsigned I, unsigned E) {
423 assert(I <= E && "Attempted to reset backwards range!");
424 assert(E <= size() && "Attempted to reset out-of-bounds range!");
425
426 if (I == E) return *this;
427
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;
433 return *this;
434 }
435
436 BitWord PrefixMask = ~BitWord(0) << (I % BITWORD_SIZE);
437 Bits[I / BITWORD_SIZE] &= ~PrefixMask;
438 I = alignTo(I, BITWORD_SIZE);
439
440 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
441 Bits[I / BITWORD_SIZE] = BitWord(0);
442
443 BitWord PostfixMask = (BitWord(1) << (E % BITWORD_SIZE)) - 1;
444 if (I < E)
445 Bits[I / BITWORD_SIZE] &= ~PostfixMask;
446
447 return *this;
448 }
449
451 for (auto &Bit : Bits)
452 Bit = ~Bit;
453 clear_unused_bits();
454 return *this;
455 }
456
457 BitVector &flip(unsigned Idx) {
458 Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
459 return *this;
460 }
461
462 // Indexing.
463 reference operator[](unsigned Idx) {
464 assert (Idx < Size && "Out-of-bounds Bit access.");
465 return reference(*this, Idx);
466 }
467
468 bool operator[](unsigned Idx) const {
469 assert (Idx < Size && "Out-of-bounds Bit access.");
470 BitWord Mask = BitWord(1) << (Idx % BITWORD_SIZE);
471 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
472 }
473
474 /// Return the last element in the vector.
475 bool back() const {
476 assert(!empty() && "Getting last element of empty vector.");
477 return (*this)[size() - 1];
478 }
479
480 bool test(unsigned Idx) const {
481 return (*this)[Idx];
482 }
483
484 // Push single bit to end of vector.
485 void push_back(bool Val) {
486 unsigned OldSize = Size;
487 unsigned NewSize = Size + 1;
488
489 // Resize, which will insert zeros.
490 // If we already fit then the unused bits will be already zero.
491 if (NewSize > getBitCapacity())
492 resize(NewSize, false);
493 else
494 Size = NewSize;
495
496 // If true, set single bit.
497 if (Val)
498 set(OldSize);
499 }
500
501 /// Pop one bit from the end of the vector.
502 void pop_back() {
503 assert(!empty() && "Empty vector has no element to pop.");
504 resize(size() - 1);
505 }
506
507 /// Test if any common bits are set.
508 bool anyCommon(const BitVector &RHS) const {
509 unsigned ThisWords = Bits.size();
510 unsigned RHSWords = RHS.Bits.size();
511 for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
512 if (Bits[i] & RHS.Bits[i])
513 return true;
514 return false;
515 }
516
517 // Comparison operators.
518 bool operator==(const BitVector &RHS) const {
519 if (size() != RHS.size())
520 return false;
521 unsigned NumWords = Bits.size();
522 return std::equal(Bits.begin(), Bits.begin() + NumWords, RHS.Bits.begin());
523 }
524
525 bool operator!=(const BitVector &RHS) const { return !(*this == RHS); }
526
527 /// Intersection, union, disjoint union.
529 unsigned ThisWords = Bits.size();
530 unsigned RHSWords = RHS.Bits.size();
531 unsigned i;
532 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
533 Bits[i] &= RHS.Bits[i];
534
535 // Any bits that are just in this bitvector become zero, because they aren't
536 // in the RHS bit vector. Any words only in RHS are ignored because they
537 // are already zero in the LHS.
538 for (; i != ThisWords; ++i)
539 Bits[i] = 0;
540
541 return *this;
542 }
543
544 /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
546 unsigned ThisWords = Bits.size();
547 unsigned RHSWords = RHS.Bits.size();
548 for (unsigned i = 0; i != std::min(ThisWords, RHSWords); ++i)
549 Bits[i] &= ~RHS.Bits[i];
550 return *this;
551 }
552
553 /// test - Check if (This - RHS) is zero.
554 /// This is the same as reset(RHS) and any().
555 bool test(const BitVector &RHS) const {
556 unsigned ThisWords = Bits.size();
557 unsigned RHSWords = RHS.Bits.size();
558 unsigned i;
559 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
560 if ((Bits[i] & ~RHS.Bits[i]) != 0)
561 return true;
562
563 for (; i != ThisWords ; ++i)
564 if (Bits[i] != 0)
565 return true;
566
567 return false;
568 }
569
570 template <class F, class... ArgTys>
571 static BitVector &apply(F &&f, BitVector &Out, BitVector const &Arg,
572 ArgTys const &...Args) {
574 std::initializer_list<unsigned>{Args.size()...},
575 [&Arg](auto const &BV) { return Arg.size() == BV; }) &&
576 "consistent sizes");
577 Out.resize(Arg.size());
578 for (size_type I = 0, E = Arg.Bits.size(); I != E; ++I)
579 Out.Bits[I] = f(Arg.Bits[I], Args.Bits[I]...);
580 Out.clear_unused_bits();
581 return Out;
582 }
583
585 if (size() < RHS.size())
586 resize(RHS.size());
587 for (size_type I = 0, E = RHS.Bits.size(); I != E; ++I)
588 Bits[I] |= RHS.Bits[I];
589 return *this;
590 }
591
593 if (size() < RHS.size())
594 resize(RHS.size());
595 for (size_type I = 0, E = RHS.Bits.size(); I != E; ++I)
596 Bits[I] ^= RHS.Bits[I];
597 return *this;
598 }
599
601 assert(N <= Size);
602 if (LLVM_UNLIKELY(empty() || N == 0))
603 return *this;
604
605 unsigned NumWords = Bits.size();
606 assert(NumWords >= 1);
607
608 wordShr(N / BITWORD_SIZE);
609
610 unsigned BitDistance = N % BITWORD_SIZE;
611 if (BitDistance == 0)
612 return *this;
613
614 // When the shift size is not a multiple of the word size, then we have
615 // a tricky situation where each word in succession needs to extract some
616 // of the bits from the next word and or them into this word while
617 // shifting this word to make room for the new bits. This has to be done
618 // for every word in the array.
619
620 // Since we're shifting each word right, some bits will fall off the end
621 // of each word to the right, and empty space will be created on the left.
622 // The final word in the array will lose bits permanently, so starting at
623 // the beginning, work forwards shifting each word to the right, and
624 // OR'ing in the bits from the end of the next word to the beginning of
625 // the current word.
626
627 // Example:
628 // Starting with {0xAABBCCDD, 0xEEFF0011, 0x22334455} and shifting right
629 // by 4 bits.
630 // Step 1: Word[0] >>= 4 ; 0x0ABBCCDD
631 // Step 2: Word[0] |= 0x10000000 ; 0x1ABBCCDD
632 // Step 3: Word[1] >>= 4 ; 0x0EEFF001
633 // Step 4: Word[1] |= 0x50000000 ; 0x5EEFF001
634 // Step 5: Word[2] >>= 4 ; 0x02334455
635 // Result: { 0x1ABBCCDD, 0x5EEFF001, 0x02334455 }
636 const BitWord Mask = maskTrailingOnes<BitWord>(BitDistance);
637 const unsigned LSH = BITWORD_SIZE - BitDistance;
638
639 for (unsigned I = 0; I < NumWords - 1; ++I) {
640 Bits[I] >>= BitDistance;
641 Bits[I] |= (Bits[I + 1] & Mask) << LSH;
642 }
643
644 Bits[NumWords - 1] >>= BitDistance;
645
646 return *this;
647 }
648
650 assert(N <= Size);
651 if (LLVM_UNLIKELY(empty() || N == 0))
652 return *this;
653
654 unsigned NumWords = Bits.size();
655 assert(NumWords >= 1);
656
657 wordShl(N / BITWORD_SIZE);
658
659 unsigned BitDistance = N % BITWORD_SIZE;
660 if (BitDistance == 0)
661 return *this;
662
663 // When the shift size is not a multiple of the word size, then we have
664 // a tricky situation where each word in succession needs to extract some
665 // of the bits from the previous word and or them into this word while
666 // shifting this word to make room for the new bits. This has to be done
667 // for every word in the array. This is similar to the algorithm outlined
668 // in operator>>=, but backwards.
669
670 // Since we're shifting each word left, some bits will fall off the end
671 // of each word to the left, and empty space will be created on the right.
672 // The first word in the array will lose bits permanently, so starting at
673 // the end, work backwards shifting each word to the left, and OR'ing
674 // in the bits from the end of the next word to the beginning of the
675 // current word.
676
677 // Example:
678 // Starting with {0xAABBCCDD, 0xEEFF0011, 0x22334455} and shifting left
679 // by 4 bits.
680 // Step 1: Word[2] <<= 4 ; 0x23344550
681 // Step 2: Word[2] |= 0x0000000E ; 0x2334455E
682 // Step 3: Word[1] <<= 4 ; 0xEFF00110
683 // Step 4: Word[1] |= 0x0000000A ; 0xEFF0011A
684 // Step 5: Word[0] <<= 4 ; 0xABBCCDD0
685 // Result: { 0xABBCCDD0, 0xEFF0011A, 0x2334455E }
686 const BitWord Mask = maskLeadingOnes<BitWord>(BitDistance);
687 const unsigned RSH = BITWORD_SIZE - BitDistance;
688
689 for (int I = NumWords - 1; I > 0; --I) {
690 Bits[I] <<= BitDistance;
691 Bits[I] |= (Bits[I - 1] & Mask) >> RSH;
692 }
693 Bits[0] <<= BitDistance;
694 clear_unused_bits();
695
696 return *this;
697 }
698
700 std::swap(Bits, RHS.Bits);
701 std::swap(Size, RHS.Size);
702 }
703
704 void invalid() {
705 assert(!Size && Bits.empty());
706 Size = (unsigned)-1;
707 }
708 bool isInvalid() const { return Size == (unsigned)-1; }
709
710 ArrayRef<BitWord> getData() const { return {Bits.data(), Bits.size()}; }
711
712 //===--------------------------------------------------------------------===//
713 // Portable bit mask operations.
714 //===--------------------------------------------------------------------===//
715 //
716 // These methods all operate on arrays of uint32_t, each holding 32 bits. The
717 // fixed word size makes it easier to work with literal bit vector constants
718 // in portable code.
719 //
720 // The LSB in each word is the lowest numbered bit. The size of a portable
721 // bit mask is always a whole multiple of 32 bits. If no bit mask size is
722 // given, the bit mask is assumed to cover the entire BitVector.
723
724 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
725 /// This computes "*this |= Mask".
726 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
727 applyMask<true, false>(Mask, MaskWords);
728 }
729
730 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
731 /// Don't resize. This computes "*this &= ~Mask".
732 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
733 applyMask<false, false>(Mask, MaskWords);
734 }
735
736 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
737 /// Don't resize. This computes "*this |= ~Mask".
738 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
739 applyMask<true, true>(Mask, MaskWords);
740 }
741
742 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
743 /// Don't resize. This computes "*this &= Mask".
744 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
745 applyMask<false, true>(Mask, MaskWords);
746 }
747
748private:
749 /// Perform a logical left shift of \p Count words by moving everything
750 /// \p Count words to the right in memory.
751 ///
752 /// While confusing, words are stored from least significant at Bits[0] to
753 /// most significant at Bits[NumWords-1]. A logical shift left, however,
754 /// moves the current least significant bit to a higher logical index, and
755 /// fills the previous least significant bits with 0. Thus, we actually
756 /// need to move the bytes of the memory to the right, not to the left.
757 /// Example:
758 /// Words = [0xBBBBAAAA, 0xDDDDFFFF, 0x00000000, 0xDDDD0000]
759 /// represents a BitVector where 0xBBBBAAAA contain the least significant
760 /// bits. So if we want to shift the BitVector left by 2 words, we need
761 /// to turn this into 0x00000000 0x00000000 0xBBBBAAAA 0xDDDDFFFF by using a
762 /// memmove which moves right, not left.
763 void wordShl(uint32_t Count) {
764 if (Count == 0)
765 return;
766
767 uint32_t NumWords = Bits.size();
768
769 // Since we always move Word-sized chunks of data with src and dest both
770 // aligned to a word-boundary, we don't need to worry about endianness
771 // here.
772 std::copy(Bits.begin(), Bits.begin() + NumWords - Count,
773 Bits.begin() + Count);
774 std::fill(Bits.begin(), Bits.begin() + Count, 0);
775 clear_unused_bits();
776 }
777
778 /// Perform a logical right shift of \p Count words by moving those
779 /// words to the left in memory. See wordShl for more information.
780 ///
781 void wordShr(uint32_t Count) {
782 if (Count == 0)
783 return;
784
785 uint32_t NumWords = Bits.size();
786
787 std::copy(Bits.begin() + Count, Bits.begin() + NumWords, Bits.begin());
788 std::fill(Bits.begin() + NumWords - Count, Bits.begin() + NumWords, 0);
789 }
790
791 unsigned NumBitWords(unsigned S) const {
792 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
793 }
794
795 // Set the unused bits in the high words.
796 void set_unused_bits(bool t = true) {
797 // Then set any stray high bits of the last used word.
798 if (unsigned ExtraBits = Size % BITWORD_SIZE) {
799 BitWord ExtraBitMask = ~BitWord(0) << ExtraBits;
800 if (t)
801 Bits.back() |= ExtraBitMask;
802 else
803 Bits.back() &= ~ExtraBitMask;
804 }
805 }
806
807 // Clear the unused bits in the high words.
808 void clear_unused_bits() {
809 set_unused_bits(false);
810 }
811
812 void init_words(bool t) { llvm::fill(Bits, 0 - (BitWord)t); }
813
814 template<bool AddBits, bool InvertMask>
815 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
816 static_assert(BITWORD_SIZE % 32 == 0, "Unsupported BitWord size.");
817 MaskWords = std::min(MaskWords, (size() + 31) / 32);
818 const unsigned Scale = BITWORD_SIZE / 32;
819 unsigned i;
820 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
821 BitWord BW = Bits[i];
822 // This inner loop should unroll completely when BITWORD_SIZE > 32.
823 for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
824 uint32_t M = *Mask++;
825 if (InvertMask) M = ~M;
826 if (AddBits) BW |= BitWord(M) << b;
827 else BW &= ~(BitWord(M) << b);
828 }
829 Bits[i] = BW;
830 }
831 for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
832 uint32_t M = *Mask++;
833 if (InvertMask) M = ~M;
834 if (AddBits) Bits[i] |= BitWord(M) << b;
835 else Bits[i] &= ~(BitWord(M) << b);
836 }
837 if (AddBits)
838 clear_unused_bits();
839 }
840
841public:
842 /// Return the size (in bytes) of the bit vector.
843 size_type getMemorySize() const { return Bits.size() * sizeof(BitWord); }
844 size_type getBitCapacity() const { return Bits.size() * BITWORD_SIZE; }
845};
846
848 return X.getMemorySize();
849}
850
851template <> struct DenseMapInfo<BitVector> {
852 static inline BitVector getEmptyKey() { return {}; }
853 static inline BitVector getTombstoneKey() {
854 BitVector V;
855 V.invalid();
856 return V;
857 }
858 static unsigned getHashValue(const BitVector &V) {
860 getHashValue(std::make_pair(V.size(), V.getData()));
861 }
862 static bool isEqual(const BitVector &LHS, const BitVector &RHS) {
863 if (LHS.isInvalid() || RHS.isInvalid())
864 return LHS.isInvalid() == RHS.isInvalid();
865 return LHS == RHS;
866 }
867};
868} // end namespace llvm
869
870namespace std {
871 /// Implement std::swap in terms of BitVector swap.
873} // end namespace std
874
875#endif // LLVM_ADT_BITVECTOR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:336
This file defines DenseMapInfo traits for DenseMap.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
reference & operator=(bool t)
Definition BitVector.h:137
reference(BitVector &b, unsigned Idx)
Definition BitVector.h:124
reference & operator=(reference t)
Definition BitVector.h:132
reference(const reference &)=default
BitVector & operator>>=(unsigned N)
Definition BitVector.h:600
bool test(unsigned Idx) const
Definition BitVector.h:480
BitVector & reset()
Definition BitVector.h:411
void swap(BitVector &RHS)
Definition BitVector.h:699
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
Definition BitVector.h:319
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
Definition BitVector.h:508
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
bool test(const BitVector &RHS) const
test - Check if (This - RHS) is zero.
Definition BitVector.h:555
BitVector()=default
BitVector default ctor - Creates an empty bitvector.
bool back() const
Return the last element in the vector.
Definition BitVector.h:475
bool operator!=(const BitVector &RHS) const
Definition BitVector.h:525
void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
Definition BitVector.h:744
int find_last() const
find_last - Returns the index of the last set bit, -1 if none of the bits are set.
Definition BitVector.h:323
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,...
Definition BitVector.h:280
size_type count() const
count - Returns the number of bits which are set.
Definition BitVector.h:181
BitVector & operator<<=(unsigned N)
Definition BitVector.h:649
ArrayRef< BitWord > getData() const
Definition BitVector.h:710
const_set_bits_iterator set_bits_end() const
Definition BitVector.h:156
BitVector & reset(unsigned Idx)
Definition BitVector.h:416
const_set_bits_iterator set_iterator
Definition BitVector.h:151
BitVector & set()
Definition BitVector.h:370
int find_last_unset() const
find_last_unset - Returns the index of the last unset bit, -1 if all of the bits are set.
Definition BitVector.h:345
void reserve(unsigned N)
Definition BitVector.h:367
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsInMask - Add '1' bits from Mask to this vector.
Definition BitVector.h:726
bool any() const
any - Returns true if any bit is set.
Definition BitVector.h:189
bool all() const
all - Returns true if all bits are set.
Definition BitVector.h:194
void push_back(bool Val)
Definition BitVector.h:485
BitVector(unsigned s, bool t=false)
BitVector ctor - Creates a bitvector of specified number of bits.
Definition BitVector.h:168
BitVector & reset(const BitVector &RHS)
reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
Definition BitVector.h:545
BitVector & operator|=(const BitVector &RHS)
Definition BitVector.h:584
void pop_back()
Pop one bit from the end of the vector.
Definition BitVector.h:502
void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
clearBitsInMask - Clear any bits in this vector that are set in Mask.
Definition BitVector.h:732
int find_prev(unsigned PriorTo) const
find_prev - Returns the index of the first set bit that precedes the the bit at PriorTo.
Definition BitVector.h:331
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).
Definition BitVector.h:249
void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
Definition BitVector.h:738
BitVector & flip()
Definition BitVector.h:450
BitVector & reset(unsigned I, unsigned E)
reset - Efficiently reset a range of bits in [I, E)
Definition BitVector.h:422
bool operator==(const BitVector &RHS) const
Definition BitVector.h:518
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
Definition BitVector.h:327
const_set_bits_iterator_impl< BitVector > const_set_bits_iterator
Definition BitVector.h:150
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:207
const_set_bits_iterator set_bits_begin() const
Definition BitVector.h:153
iterator_range< const_set_bits_iterator > set_bits() const
Definition BitVector.h:159
BitVector & set(unsigned I, unsigned E)
set - Efficiently set a range of bits in [I, E)
Definition BitVector.h:383
size_type getBitCapacity() const
Definition BitVector.h:844
int find_first_in(unsigned Begin, unsigned End, bool Set=true) const
find_first_in - Returns the index of the first set / unset bit, depending on Set, in the range [Begin...
Definition BitVector.h:214
size_type size() const
size - Returns the number of bits in this bitvector.
Definition BitVector.h:178
BitVector & operator^=(const BitVector &RHS)
Definition BitVector.h:592
BitVector & flip(unsigned Idx)
Definition BitVector.h:457
size_type getMemorySize() const
Return the size (in bytes) of the bit vector.
Definition BitVector.h:843
static BitVector & apply(F &&f, BitVector &Out, BitVector const &Arg, ArgTys const &...Args)
Definition BitVector.h:571
unsigned size_type
Definition BitVector.h:115
bool empty() const
empty - Tests whether there are no bits in this bitvector.
Definition BitVector.h:175
int find_next_unset(unsigned Prev) const
find_next_unset - Returns the index of the next unset bit following the "Prev" bit.
Definition BitVector.h:339
BitVector & set(unsigned Idx)
Definition BitVector.h:376
int find_prev_unset(unsigned PriorTo) const
find_prev_unset - Returns the index of the first unset bit that precedes the bit at PriorTo.
Definition BitVector.h:349
BitVector & operator&=(const BitVector &RHS)
Intersection, union, disjoint union.
Definition BitVector.h:528
int find_first_unset() const
find_first_unset - Returns the index of the first unset bit, -1 if all of the bits are set.
Definition BitVector.h:335
bool isInvalid() const
Definition BitVector.h:708
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).
Definition BitVector.h:286
bool operator[](unsigned Idx) const
Definition BitVector.h:468
reference operator[](unsigned Idx)
Definition BitVector.h:463
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.
Definition BitVector.h:34
const_set_bits_iterator_impl operator--(int)
Definition BitVector.h:75
const_set_bits_iterator_impl(const BitVectorT &Parent)
Definition BitVector.h:60
std::bidirectional_iterator_tag iterator_category
Definition BitVector.h:52
bool operator==(const const_set_bits_iterator_impl &Other) const
Definition BitVector.h:88
const_set_bits_iterator_impl(const const_set_bits_iterator_impl &)=default
const_set_bits_iterator_impl & operator++()
Definition BitVector.h:70
const_set_bits_iterator_impl & operator--()
Definition BitVector.h:81
const_set_bits_iterator_impl operator++(int)
Definition BitVector.h:64
const_set_bits_iterator_impl(const BitVectorT &Parent, int Current)
Definition BitVector.h:58
bool operator!=(const const_set_bits_iterator_impl &Other) const
Definition BitVector.h:94
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.
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.
Definition STLExtras.h:1725
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1705
BitVector::size_type capacity_in_bytes(const BitVector &X)
Definition BitVector.h:847
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.
Definition MathExtras.h:97
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:186
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1712
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:222
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
int countl_one(T Value)
Count the number of ones from the most significant bit to the first zero bit.
Definition bit.h:266
@ Other
Any other memory.
Definition ModRef.h:68
constexpr T maskTrailingZeros(unsigned N)
Create a bitmask with the N right-most bits set to 0, and all other bits set to 1.
Definition MathExtras.h:103
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:86
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:154
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
static BitVector getEmptyKey()
Definition BitVector.h:852
static bool isEqual(const BitVector &LHS, const BitVector &RHS)
Definition BitVector.h:862
static unsigned getHashValue(const BitVector &V)
Definition BitVector.h:858
static BitVector getTombstoneKey()
Definition BitVector.h:853
An information struct used to provide DenseMap with the various necessary components for a given valu...