File indexing completed on 2026-05-10 08:43:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef LLVM_ADT_SMALLBITVECTOR_H
0015 #define LLVM_ADT_SMALLBITVECTOR_H
0016
0017 #include "llvm/ADT/BitVector.h"
0018 #include "llvm/ADT/iterator_range.h"
0019 #include "llvm/Support/MathExtras.h"
0020 #include <algorithm>
0021 #include <cassert>
0022 #include <climits>
0023 #include <cstddef>
0024 #include <cstdint>
0025 #include <limits>
0026 #include <utility>
0027
0028 namespace llvm {
0029
0030
0031
0032
0033
0034
0035 class SmallBitVector {
0036
0037
0038
0039 uintptr_t X = 1;
0040
0041 enum {
0042
0043 NumBaseBits = sizeof(uintptr_t) * CHAR_BIT,
0044
0045
0046
0047 SmallNumRawBits = NumBaseBits - 1,
0048
0049
0050
0051
0052 SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
0053 NumBaseBits == 64 ? 6 :
0054 SmallNumRawBits),
0055
0056
0057 SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
0058 };
0059
0060 static_assert(NumBaseBits == 64 || NumBaseBits == 32,
0061 "Unsupported word size");
0062
0063 public:
0064 using size_type = uintptr_t;
0065
0066
0067 class reference {
0068 SmallBitVector &TheVector;
0069 unsigned BitPos;
0070
0071 public:
0072 reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
0073
0074 reference(const reference&) = default;
0075
0076 reference& operator=(reference t) {
0077 *this = bool(t);
0078 return *this;
0079 }
0080
0081 reference& operator=(bool t) {
0082 if (t)
0083 TheVector.set(BitPos);
0084 else
0085 TheVector.reset(BitPos);
0086 return *this;
0087 }
0088
0089 operator bool() const {
0090 return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
0091 }
0092 };
0093
0094 private:
0095 BitVector *getPointer() const {
0096 assert(!isSmall());
0097 return reinterpret_cast<BitVector *>(X);
0098 }
0099
0100 void switchToSmall(uintptr_t NewSmallBits, size_type NewSize) {
0101 X = 1;
0102 setSmallSize(NewSize);
0103 setSmallBits(NewSmallBits);
0104 }
0105
0106 void switchToLarge(BitVector *BV) {
0107 X = reinterpret_cast<uintptr_t>(BV);
0108 assert(!isSmall() && "Tried to use an unaligned pointer");
0109 }
0110
0111
0112
0113 uintptr_t getSmallRawBits() const {
0114 assert(isSmall());
0115 return X >> 1;
0116 }
0117
0118 void setSmallRawBits(uintptr_t NewRawBits) {
0119 assert(isSmall());
0120 X = (NewRawBits << 1) | uintptr_t(1);
0121 }
0122
0123
0124 size_type getSmallSize() const {
0125 return getSmallRawBits() >> SmallNumDataBits;
0126 }
0127
0128 void setSmallSize(size_type Size) {
0129 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
0130 }
0131
0132
0133 uintptr_t getSmallBits() const {
0134 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
0135 }
0136
0137 void setSmallBits(uintptr_t NewBits) {
0138 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
0139 (getSmallSize() << SmallNumDataBits));
0140 }
0141
0142 public:
0143
0144 SmallBitVector() = default;
0145
0146
0147
0148 explicit SmallBitVector(unsigned s, bool t = false) {
0149 if (s <= SmallNumDataBits)
0150 switchToSmall(t ? ~uintptr_t(0) : 0, s);
0151 else
0152 switchToLarge(new BitVector(s, t));
0153 }
0154
0155
0156 SmallBitVector(const SmallBitVector &RHS) {
0157 if (RHS.isSmall())
0158 X = RHS.X;
0159 else
0160 switchToLarge(new BitVector(*RHS.getPointer()));
0161 }
0162
0163 SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
0164 RHS.X = 1;
0165 }
0166
0167 ~SmallBitVector() {
0168 if (!isSmall())
0169 delete getPointer();
0170 }
0171
0172 using const_set_bits_iterator = const_set_bits_iterator_impl<SmallBitVector>;
0173 using set_iterator = const_set_bits_iterator;
0174
0175 const_set_bits_iterator set_bits_begin() const {
0176 return const_set_bits_iterator(*this);
0177 }
0178
0179 const_set_bits_iterator set_bits_end() const {
0180 return const_set_bits_iterator(*this, -1);
0181 }
0182
0183 iterator_range<const_set_bits_iterator> set_bits() const {
0184 return make_range(set_bits_begin(), set_bits_end());
0185 }
0186
0187 bool isSmall() const { return X & uintptr_t(1); }
0188
0189
0190 bool empty() const {
0191 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
0192 }
0193
0194
0195 size_type size() const {
0196 return isSmall() ? getSmallSize() : getPointer()->size();
0197 }
0198
0199
0200 size_type count() const {
0201 if (isSmall()) {
0202 uintptr_t Bits = getSmallBits();
0203 return llvm::popcount(Bits);
0204 }
0205 return getPointer()->count();
0206 }
0207
0208
0209 bool any() const {
0210 if (isSmall())
0211 return getSmallBits() != 0;
0212 return getPointer()->any();
0213 }
0214
0215
0216 bool all() const {
0217 if (isSmall())
0218 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
0219 return getPointer()->all();
0220 }
0221
0222
0223 bool none() const {
0224 if (isSmall())
0225 return getSmallBits() == 0;
0226 return getPointer()->none();
0227 }
0228
0229
0230 int find_first() const {
0231 if (isSmall()) {
0232 uintptr_t Bits = getSmallBits();
0233 if (Bits == 0)
0234 return -1;
0235 return llvm::countr_zero(Bits);
0236 }
0237 return getPointer()->find_first();
0238 }
0239
0240 int find_last() const {
0241 if (isSmall()) {
0242 uintptr_t Bits = getSmallBits();
0243 if (Bits == 0)
0244 return -1;
0245 return NumBaseBits - llvm::countl_zero(Bits) - 1;
0246 }
0247 return getPointer()->find_last();
0248 }
0249
0250
0251 int find_first_unset() const {
0252 if (isSmall()) {
0253 if (count() == getSmallSize())
0254 return -1;
0255
0256 uintptr_t Bits = getSmallBits();
0257 return llvm::countr_one(Bits);
0258 }
0259 return getPointer()->find_first_unset();
0260 }
0261
0262 int find_last_unset() const {
0263 if (isSmall()) {
0264 if (count() == getSmallSize())
0265 return -1;
0266
0267 uintptr_t Bits = getSmallBits();
0268
0269 Bits |= ~uintptr_t(0) << getSmallSize();
0270 return NumBaseBits - llvm::countl_one(Bits) - 1;
0271 }
0272 return getPointer()->find_last_unset();
0273 }
0274
0275
0276
0277 int find_next(unsigned Prev) const {
0278 if (isSmall()) {
0279 uintptr_t Bits = getSmallBits();
0280
0281 Bits &= ~uintptr_t(0) << (Prev + 1);
0282 if (Bits == 0 || Prev + 1 >= getSmallSize())
0283 return -1;
0284 return llvm::countr_zero(Bits);
0285 }
0286 return getPointer()->find_next(Prev);
0287 }
0288
0289
0290
0291 int find_next_unset(unsigned Prev) const {
0292 if (isSmall()) {
0293 uintptr_t Bits = getSmallBits();
0294
0295 Bits |= (uintptr_t(1) << (Prev + 1)) - 1;
0296
0297 Bits |= ~uintptr_t(0) << getSmallSize();
0298
0299 if (Bits == ~uintptr_t(0) || Prev + 1 >= getSmallSize())
0300 return -1;
0301 return llvm::countr_one(Bits);
0302 }
0303 return getPointer()->find_next_unset(Prev);
0304 }
0305
0306
0307
0308 int find_prev(unsigned PriorTo) const {
0309 if (isSmall()) {
0310 if (PriorTo == 0)
0311 return -1;
0312
0313 --PriorTo;
0314 uintptr_t Bits = getSmallBits();
0315 Bits &= maskTrailingOnes<uintptr_t>(PriorTo + 1);
0316 if (Bits == 0)
0317 return -1;
0318
0319 return NumBaseBits - llvm::countl_zero(Bits) - 1;
0320 }
0321 return getPointer()->find_prev(PriorTo);
0322 }
0323
0324
0325 void clear() {
0326 if (!isSmall())
0327 delete getPointer();
0328 switchToSmall(0, 0);
0329 }
0330
0331
0332 void resize(unsigned N, bool t = false) {
0333 if (!isSmall()) {
0334 getPointer()->resize(N, t);
0335 } else if (SmallNumDataBits >= N) {
0336 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
0337 setSmallSize(N);
0338 setSmallBits(NewBits | getSmallBits());
0339 } else {
0340 BitVector *BV = new BitVector(N, t);
0341 uintptr_t OldBits = getSmallBits();
0342 for (size_type I = 0, E = getSmallSize(); I != E; ++I)
0343 (*BV)[I] = (OldBits >> I) & 1;
0344 switchToLarge(BV);
0345 }
0346 }
0347
0348 void reserve(unsigned N) {
0349 if (isSmall()) {
0350 if (N > SmallNumDataBits) {
0351 uintptr_t OldBits = getSmallRawBits();
0352 size_type SmallSize = getSmallSize();
0353 BitVector *BV = new BitVector(SmallSize);
0354 for (size_type I = 0; I < SmallSize; ++I)
0355 if ((OldBits >> I) & 1)
0356 BV->set(I);
0357 BV->reserve(N);
0358 switchToLarge(BV);
0359 }
0360 } else {
0361 getPointer()->reserve(N);
0362 }
0363 }
0364
0365
0366 SmallBitVector &set() {
0367 if (isSmall())
0368 setSmallBits(~uintptr_t(0));
0369 else
0370 getPointer()->set();
0371 return *this;
0372 }
0373
0374 SmallBitVector &set(unsigned Idx) {
0375 if (isSmall()) {
0376 assert(Idx <= static_cast<unsigned>(
0377 std::numeric_limits<uintptr_t>::digits) &&
0378 "undefined behavior");
0379 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
0380 }
0381 else
0382 getPointer()->set(Idx);
0383 return *this;
0384 }
0385
0386
0387 SmallBitVector &set(unsigned I, unsigned E) {
0388 assert(I <= E && "Attempted to set backwards range!");
0389 assert(E <= size() && "Attempted to set out-of-bounds range!");
0390 if (I == E) return *this;
0391 if (isSmall()) {
0392 uintptr_t EMask = ((uintptr_t)1) << E;
0393 uintptr_t IMask = ((uintptr_t)1) << I;
0394 uintptr_t Mask = EMask - IMask;
0395 setSmallBits(getSmallBits() | Mask);
0396 } else
0397 getPointer()->set(I, E);
0398 return *this;
0399 }
0400
0401 SmallBitVector &reset() {
0402 if (isSmall())
0403 setSmallBits(0);
0404 else
0405 getPointer()->reset();
0406 return *this;
0407 }
0408
0409 SmallBitVector &reset(unsigned Idx) {
0410 if (isSmall())
0411 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
0412 else
0413 getPointer()->reset(Idx);
0414 return *this;
0415 }
0416
0417
0418 SmallBitVector &reset(unsigned I, unsigned E) {
0419 assert(I <= E && "Attempted to reset backwards range!");
0420 assert(E <= size() && "Attempted to reset out-of-bounds range!");
0421 if (I == E) return *this;
0422 if (isSmall()) {
0423 uintptr_t EMask = ((uintptr_t)1) << E;
0424 uintptr_t IMask = ((uintptr_t)1) << I;
0425 uintptr_t Mask = EMask - IMask;
0426 setSmallBits(getSmallBits() & ~Mask);
0427 } else
0428 getPointer()->reset(I, E);
0429 return *this;
0430 }
0431
0432 SmallBitVector &flip() {
0433 if (isSmall())
0434 setSmallBits(~getSmallBits());
0435 else
0436 getPointer()->flip();
0437 return *this;
0438 }
0439
0440 SmallBitVector &flip(unsigned Idx) {
0441 if (isSmall())
0442 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
0443 else
0444 getPointer()->flip(Idx);
0445 return *this;
0446 }
0447
0448
0449 SmallBitVector operator~() const {
0450 return SmallBitVector(*this).flip();
0451 }
0452
0453
0454 reference operator[](unsigned Idx) {
0455 assert(Idx < size() && "Out-of-bounds Bit access.");
0456 return reference(*this, Idx);
0457 }
0458
0459 bool operator[](unsigned Idx) const {
0460 assert(Idx < size() && "Out-of-bounds Bit access.");
0461 if (isSmall())
0462 return ((getSmallBits() >> Idx) & 1) != 0;
0463 return getPointer()->operator[](Idx);
0464 }
0465
0466
0467 bool back() const {
0468 assert(!empty() && "Getting last element of empty vector.");
0469 return (*this)[size() - 1];
0470 }
0471
0472 bool test(unsigned Idx) const {
0473 return (*this)[Idx];
0474 }
0475
0476
0477 void push_back(bool Val) {
0478 resize(size() + 1, Val);
0479 }
0480
0481
0482 void pop_back() {
0483 assert(!empty() && "Empty vector has no element to pop.");
0484 resize(size() - 1);
0485 }
0486
0487
0488 bool anyCommon(const SmallBitVector &RHS) const {
0489 if (isSmall() && RHS.isSmall())
0490 return (getSmallBits() & RHS.getSmallBits()) != 0;
0491 if (!isSmall() && !RHS.isSmall())
0492 return getPointer()->anyCommon(*RHS.getPointer());
0493
0494 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
0495 if (test(i) && RHS.test(i))
0496 return true;
0497 return false;
0498 }
0499
0500
0501 bool operator==(const SmallBitVector &RHS) const {
0502 if (size() != RHS.size())
0503 return false;
0504 if (isSmall() && RHS.isSmall())
0505 return getSmallBits() == RHS.getSmallBits();
0506 else if (!isSmall() && !RHS.isSmall())
0507 return *getPointer() == *RHS.getPointer();
0508 else {
0509 for (size_type I = 0, E = size(); I != E; ++I) {
0510 if ((*this)[I] != RHS[I])
0511 return false;
0512 }
0513 return true;
0514 }
0515 }
0516
0517 bool operator!=(const SmallBitVector &RHS) const {
0518 return !(*this == RHS);
0519 }
0520
0521
0522
0523 SmallBitVector &operator&=(const SmallBitVector &RHS) {
0524 resize(std::max(size(), RHS.size()));
0525 if (isSmall() && RHS.isSmall())
0526 setSmallBits(getSmallBits() & RHS.getSmallBits());
0527 else if (!isSmall() && !RHS.isSmall())
0528 getPointer()->operator&=(*RHS.getPointer());
0529 else {
0530 size_type I, E;
0531 for (I = 0, E = std::min(size(), RHS.size()); I != E; ++I)
0532 (*this)[I] = test(I) && RHS.test(I);
0533 for (E = size(); I != E; ++I)
0534 reset(I);
0535 }
0536 return *this;
0537 }
0538
0539
0540 SmallBitVector &reset(const SmallBitVector &RHS) {
0541 if (isSmall() && RHS.isSmall())
0542 setSmallBits(getSmallBits() & ~RHS.getSmallBits());
0543 else if (!isSmall() && !RHS.isSmall())
0544 getPointer()->reset(*RHS.getPointer());
0545 else
0546 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
0547 if (RHS.test(i))
0548 reset(i);
0549
0550 return *this;
0551 }
0552
0553
0554 bool test(const SmallBitVector &RHS) const {
0555 if (isSmall() && RHS.isSmall())
0556 return (getSmallBits() & ~RHS.getSmallBits()) != 0;
0557 if (!isSmall() && !RHS.isSmall())
0558 return getPointer()->test(*RHS.getPointer());
0559
0560 unsigned i, e;
0561 for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
0562 if (test(i) && !RHS.test(i))
0563 return true;
0564
0565 for (e = size(); i != e; ++i)
0566 if (test(i))
0567 return true;
0568
0569 return false;
0570 }
0571
0572 SmallBitVector &operator|=(const SmallBitVector &RHS) {
0573 resize(std::max(size(), RHS.size()));
0574 if (isSmall() && RHS.isSmall())
0575 setSmallBits(getSmallBits() | RHS.getSmallBits());
0576 else if (!isSmall() && !RHS.isSmall())
0577 getPointer()->operator|=(*RHS.getPointer());
0578 else {
0579 for (size_type I = 0, E = RHS.size(); I != E; ++I)
0580 (*this)[I] = test(I) || RHS.test(I);
0581 }
0582 return *this;
0583 }
0584
0585 SmallBitVector &operator^=(const SmallBitVector &RHS) {
0586 resize(std::max(size(), RHS.size()));
0587 if (isSmall() && RHS.isSmall())
0588 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
0589 else if (!isSmall() && !RHS.isSmall())
0590 getPointer()->operator^=(*RHS.getPointer());
0591 else {
0592 for (size_type I = 0, E = RHS.size(); I != E; ++I)
0593 (*this)[I] = test(I) != RHS.test(I);
0594 }
0595 return *this;
0596 }
0597
0598 SmallBitVector &operator<<=(unsigned N) {
0599 if (isSmall())
0600 setSmallBits(getSmallBits() << N);
0601 else
0602 getPointer()->operator<<=(N);
0603 return *this;
0604 }
0605
0606 SmallBitVector &operator>>=(unsigned N) {
0607 if (isSmall())
0608 setSmallBits(getSmallBits() >> N);
0609 else
0610 getPointer()->operator>>=(N);
0611 return *this;
0612 }
0613
0614
0615 const SmallBitVector &operator=(const SmallBitVector &RHS) {
0616 if (isSmall()) {
0617 if (RHS.isSmall())
0618 X = RHS.X;
0619 else
0620 switchToLarge(new BitVector(*RHS.getPointer()));
0621 } else {
0622 if (!RHS.isSmall())
0623 *getPointer() = *RHS.getPointer();
0624 else {
0625 delete getPointer();
0626 X = RHS.X;
0627 }
0628 }
0629 return *this;
0630 }
0631
0632 const SmallBitVector &operator=(SmallBitVector &&RHS) {
0633 if (this != &RHS) {
0634 clear();
0635 swap(RHS);
0636 }
0637 return *this;
0638 }
0639
0640 void swap(SmallBitVector &RHS) {
0641 std::swap(X, RHS.X);
0642 }
0643
0644
0645
0646 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
0647 if (isSmall())
0648 applyMask<true, false>(Mask, MaskWords);
0649 else
0650 getPointer()->setBitsInMask(Mask, MaskWords);
0651 }
0652
0653
0654
0655 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
0656 if (isSmall())
0657 applyMask<false, false>(Mask, MaskWords);
0658 else
0659 getPointer()->clearBitsInMask(Mask, MaskWords);
0660 }
0661
0662
0663
0664 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
0665 if (isSmall())
0666 applyMask<true, true>(Mask, MaskWords);
0667 else
0668 getPointer()->setBitsNotInMask(Mask, MaskWords);
0669 }
0670
0671
0672
0673 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
0674 if (isSmall())
0675 applyMask<false, true>(Mask, MaskWords);
0676 else
0677 getPointer()->clearBitsNotInMask(Mask, MaskWords);
0678 }
0679
0680 void invalid() {
0681 assert(empty());
0682 X = (uintptr_t)-1;
0683 }
0684 bool isInvalid() const { return X == (uintptr_t)-1; }
0685
0686 ArrayRef<uintptr_t> getData(uintptr_t &Store) const {
0687 if (!isSmall())
0688 return getPointer()->getData();
0689 Store = getSmallBits();
0690 return Store;
0691 }
0692
0693 private:
0694 template <bool AddBits, bool InvertMask>
0695 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
0696 assert(MaskWords <= sizeof(uintptr_t) && "Mask is larger than base!");
0697 uintptr_t M = Mask[0];
0698 if (NumBaseBits == 64)
0699 M |= uint64_t(Mask[1]) << 32;
0700 if (InvertMask)
0701 M = ~M;
0702 if (AddBits)
0703 setSmallBits(getSmallBits() | M);
0704 else
0705 setSmallBits(getSmallBits() & ~M);
0706 }
0707 };
0708
0709 inline SmallBitVector
0710 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
0711 SmallBitVector Result(LHS);
0712 Result &= RHS;
0713 return Result;
0714 }
0715
0716 inline SmallBitVector
0717 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
0718 SmallBitVector Result(LHS);
0719 Result |= RHS;
0720 return Result;
0721 }
0722
0723 inline SmallBitVector
0724 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
0725 SmallBitVector Result(LHS);
0726 Result ^= RHS;
0727 return Result;
0728 }
0729
0730 template <> struct DenseMapInfo<SmallBitVector> {
0731 static inline SmallBitVector getEmptyKey() { return SmallBitVector(); }
0732 static inline SmallBitVector getTombstoneKey() {
0733 SmallBitVector V;
0734 V.invalid();
0735 return V;
0736 }
0737 static unsigned getHashValue(const SmallBitVector &V) {
0738 uintptr_t Store;
0739 return DenseMapInfo<
0740 std::pair<SmallBitVector::size_type, ArrayRef<uintptr_t>>>::
0741 getHashValue(std::make_pair(V.size(), V.getData(Store)));
0742 }
0743 static bool isEqual(const SmallBitVector &LHS, const SmallBitVector &RHS) {
0744 if (LHS.isInvalid() || RHS.isInvalid())
0745 return LHS.isInvalid() == RHS.isInvalid();
0746 return LHS == RHS;
0747 }
0748 };
0749 }
0750
0751 namespace std {
0752
0753
0754 inline void
0755 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
0756 LHS.swap(RHS);
0757 }
0758
0759 }
0760
0761 #endif