Movatterモバイル変換


[0]ホーム

URL:


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

Generated on Thu Jul 17 2025 03:52:47 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp