SmallBitVector.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/SmallBitVector.h - 'Normally small' bit vectors -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file implements the SmallBitVector class.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ADT_SMALLBITVECTOR_H
  19. #define LLVM_ADT_SMALLBITVECTOR_H
  20. #include "llvm/ADT/BitVector.h"
  21. #include "llvm/ADT/iterator_range.h"
  22. #include "llvm/Support/MathExtras.h"
  23. #include <algorithm>
  24. #include <cassert>
  25. #include <climits>
  26. #include <cstddef>
  27. #include <cstdint>
  28. #include <limits>
  29. #include <utility>
  30. namespace llvm {
  31. /// This is a 'bitvector' (really, a variable-sized bit array), optimized for
  32. /// the case when the array is small. It contains one pointer-sized field, which
  33. /// is directly used as a plain collection of bits when possible, or as a
  34. /// pointer to a larger heap-allocated array when necessary. This allows normal
  35. /// "small" cases to be fast without losing generality for large inputs.
  36. class SmallBitVector {
  37. // TODO: In "large" mode, a pointer to a BitVector is used, leading to an
  38. // unnecessary level of indirection. It would be more efficient to use a
  39. // pointer to memory containing size, allocation size, and the array of bits.
  40. uintptr_t X = 1;
  41. enum {
  42. // The number of bits in this class.
  43. NumBaseBits = sizeof(uintptr_t) * CHAR_BIT,
  44. // One bit is used to discriminate between small and large mode. The
  45. // remaining bits are used for the small-mode representation.
  46. SmallNumRawBits = NumBaseBits - 1,
  47. // A few more bits are used to store the size of the bit set in small mode.
  48. // Theoretically this is a ceil-log2. These bits are encoded in the most
  49. // significant bits of the raw bits.
  50. SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
  51. NumBaseBits == 64 ? 6 :
  52. SmallNumRawBits),
  53. // The remaining bits are used to store the actual set in small mode.
  54. SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
  55. };
  56. static_assert(NumBaseBits == 64 || NumBaseBits == 32,
  57. "Unsupported word size");
  58. public:
  59. using size_type = uintptr_t;
  60. // Encapsulation of a single bit.
  61. class reference {
  62. SmallBitVector &TheVector;
  63. unsigned BitPos;
  64. public:
  65. reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
  66. reference(const reference&) = default;
  67. reference& operator=(reference t) {
  68. *this = bool(t);
  69. return *this;
  70. }
  71. reference& operator=(bool t) {
  72. if (t)
  73. TheVector.set(BitPos);
  74. else
  75. TheVector.reset(BitPos);
  76. return *this;
  77. }
  78. operator bool() const {
  79. return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
  80. }
  81. };
  82. private:
  83. BitVector *getPointer() const {
  84. assert(!isSmall());
  85. return reinterpret_cast<BitVector *>(X);
  86. }
  87. void switchToSmall(uintptr_t NewSmallBits, size_type NewSize) {
  88. X = 1;
  89. setSmallSize(NewSize);
  90. setSmallBits(NewSmallBits);
  91. }
  92. void switchToLarge(BitVector *BV) {
  93. X = reinterpret_cast<uintptr_t>(BV);
  94. assert(!isSmall() && "Tried to use an unaligned pointer");
  95. }
  96. // Return all the bits used for the "small" representation; this includes
  97. // bits for the size as well as the element bits.
  98. uintptr_t getSmallRawBits() const {
  99. assert(isSmall());
  100. return X >> 1;
  101. }
  102. void setSmallRawBits(uintptr_t NewRawBits) {
  103. assert(isSmall());
  104. X = (NewRawBits << 1) | uintptr_t(1);
  105. }
  106. // Return the size.
  107. size_type getSmallSize() const {
  108. return getSmallRawBits() >> SmallNumDataBits;
  109. }
  110. void setSmallSize(size_type Size) {
  111. setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
  112. }
  113. // Return the element bits.
  114. uintptr_t getSmallBits() const {
  115. return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
  116. }
  117. void setSmallBits(uintptr_t NewBits) {
  118. setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
  119. (getSmallSize() << SmallNumDataBits));
  120. }
  121. public:
  122. /// Creates an empty bitvector.
  123. SmallBitVector() = default;
  124. /// Creates a bitvector of specified number of bits. All bits are initialized
  125. /// to the specified value.
  126. explicit SmallBitVector(unsigned s, bool t = false) {
  127. if (s <= SmallNumDataBits)
  128. switchToSmall(t ? ~uintptr_t(0) : 0, s);
  129. else
  130. switchToLarge(new BitVector(s, t));
  131. }
  132. /// SmallBitVector copy ctor.
  133. SmallBitVector(const SmallBitVector &RHS) {
  134. if (RHS.isSmall())
  135. X = RHS.X;
  136. else
  137. switchToLarge(new BitVector(*RHS.getPointer()));
  138. }
  139. SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
  140. RHS.X = 1;
  141. }
  142. ~SmallBitVector() {
  143. if (!isSmall())
  144. delete getPointer();
  145. }
  146. using const_set_bits_iterator = const_set_bits_iterator_impl<SmallBitVector>;
  147. using set_iterator = const_set_bits_iterator;
  148. const_set_bits_iterator set_bits_begin() const {
  149. return const_set_bits_iterator(*this);
  150. }
  151. const_set_bits_iterator set_bits_end() const {
  152. return const_set_bits_iterator(*this, -1);
  153. }
  154. iterator_range<const_set_bits_iterator> set_bits() const {
  155. return make_range(set_bits_begin(), set_bits_end());
  156. }
  157. bool isSmall() const { return X & uintptr_t(1); }
  158. /// Tests whether there are no bits in this bitvector.
  159. bool empty() const {
  160. return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
  161. }
  162. /// Returns the number of bits in this bitvector.
  163. size_type size() const {
  164. return isSmall() ? getSmallSize() : getPointer()->size();
  165. }
  166. /// Returns the number of bits which are set.
  167. size_type count() const {
  168. if (isSmall()) {
  169. uintptr_t Bits = getSmallBits();
  170. return llvm::popcount(Bits);
  171. }
  172. return getPointer()->count();
  173. }
  174. /// Returns true if any bit is set.
  175. bool any() const {
  176. if (isSmall())
  177. return getSmallBits() != 0;
  178. return getPointer()->any();
  179. }
  180. /// Returns true if all bits are set.
  181. bool all() const {
  182. if (isSmall())
  183. return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
  184. return getPointer()->all();
  185. }
  186. /// Returns true if none of the bits are set.
  187. bool none() const {
  188. if (isSmall())
  189. return getSmallBits() == 0;
  190. return getPointer()->none();
  191. }
  192. /// Returns the index of the first set bit, -1 if none of the bits are set.
  193. int find_first() const {
  194. if (isSmall()) {
  195. uintptr_t Bits = getSmallBits();
  196. if (Bits == 0)
  197. return -1;
  198. return countTrailingZeros(Bits);
  199. }
  200. return getPointer()->find_first();
  201. }
  202. int find_last() const {
  203. if (isSmall()) {
  204. uintptr_t Bits = getSmallBits();
  205. if (Bits == 0)
  206. return -1;
  207. return NumBaseBits - countLeadingZeros(Bits) - 1;
  208. }
  209. return getPointer()->find_last();
  210. }
  211. /// Returns the index of the first unset bit, -1 if all of the bits are set.
  212. int find_first_unset() const {
  213. if (isSmall()) {
  214. if (count() == getSmallSize())
  215. return -1;
  216. uintptr_t Bits = getSmallBits();
  217. return countTrailingOnes(Bits);
  218. }
  219. return getPointer()->find_first_unset();
  220. }
  221. int find_last_unset() const {
  222. if (isSmall()) {
  223. if (count() == getSmallSize())
  224. return -1;
  225. uintptr_t Bits = getSmallBits();
  226. // Set unused bits.
  227. Bits |= ~uintptr_t(0) << getSmallSize();
  228. return NumBaseBits - countLeadingOnes(Bits) - 1;
  229. }
  230. return getPointer()->find_last_unset();
  231. }
  232. /// Returns the index of the next set bit following the "Prev" bit.
  233. /// Returns -1 if the next set bit is not found.
  234. int find_next(unsigned Prev) const {
  235. if (isSmall()) {
  236. uintptr_t Bits = getSmallBits();
  237. // Mask off previous bits.
  238. Bits &= ~uintptr_t(0) << (Prev + 1);
  239. if (Bits == 0 || Prev + 1 >= getSmallSize())
  240. return -1;
  241. return countTrailingZeros(Bits);
  242. }
  243. return getPointer()->find_next(Prev);
  244. }
  245. /// Returns the index of the next unset bit following the "Prev" bit.
  246. /// Returns -1 if the next unset bit is not found.
  247. int find_next_unset(unsigned Prev) const {
  248. if (isSmall()) {
  249. uintptr_t Bits = getSmallBits();
  250. // Mask in previous bits.
  251. Bits |= (uintptr_t(1) << (Prev + 1)) - 1;
  252. // Mask in unused bits.
  253. Bits |= ~uintptr_t(0) << getSmallSize();
  254. if (Bits == ~uintptr_t(0) || Prev + 1 >= getSmallSize())
  255. return -1;
  256. return countTrailingOnes(Bits);
  257. }
  258. return getPointer()->find_next_unset(Prev);
  259. }
  260. /// find_prev - Returns the index of the first set bit that precedes the
  261. /// the bit at \p PriorTo. Returns -1 if all previous bits are unset.
  262. int find_prev(unsigned PriorTo) const {
  263. if (isSmall()) {
  264. if (PriorTo == 0)
  265. return -1;
  266. --PriorTo;
  267. uintptr_t Bits = getSmallBits();
  268. Bits &= maskTrailingOnes<uintptr_t>(PriorTo + 1);
  269. if (Bits == 0)
  270. return -1;
  271. return NumBaseBits - countLeadingZeros(Bits) - 1;
  272. }
  273. return getPointer()->find_prev(PriorTo);
  274. }
  275. /// Clear all bits.
  276. void clear() {
  277. if (!isSmall())
  278. delete getPointer();
  279. switchToSmall(0, 0);
  280. }
  281. /// Grow or shrink the bitvector.
  282. void resize(unsigned N, bool t = false) {
  283. if (!isSmall()) {
  284. getPointer()->resize(N, t);
  285. } else if (SmallNumDataBits >= N) {
  286. uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
  287. setSmallSize(N);
  288. setSmallBits(NewBits | getSmallBits());
  289. } else {
  290. BitVector *BV = new BitVector(N, t);
  291. uintptr_t OldBits = getSmallBits();
  292. for (size_type I = 0, E = getSmallSize(); I != E; ++I)
  293. (*BV)[I] = (OldBits >> I) & 1;
  294. switchToLarge(BV);
  295. }
  296. }
  297. void reserve(unsigned N) {
  298. if (isSmall()) {
  299. if (N > SmallNumDataBits) {
  300. uintptr_t OldBits = getSmallRawBits();
  301. size_type SmallSize = getSmallSize();
  302. BitVector *BV = new BitVector(SmallSize);
  303. for (size_type I = 0; I < SmallSize; ++I)
  304. if ((OldBits >> I) & 1)
  305. BV->set(I);
  306. BV->reserve(N);
  307. switchToLarge(BV);
  308. }
  309. } else {
  310. getPointer()->reserve(N);
  311. }
  312. }
  313. // Set, reset, flip
  314. SmallBitVector &set() {
  315. if (isSmall())
  316. setSmallBits(~uintptr_t(0));
  317. else
  318. getPointer()->set();
  319. return *this;
  320. }
  321. SmallBitVector &set(unsigned Idx) {
  322. if (isSmall()) {
  323. assert(Idx <= static_cast<unsigned>(
  324. std::numeric_limits<uintptr_t>::digits) &&
  325. "undefined behavior");
  326. setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
  327. }
  328. else
  329. getPointer()->set(Idx);
  330. return *this;
  331. }
  332. /// Efficiently set a range of bits in [I, E)
  333. SmallBitVector &set(unsigned I, unsigned E) {
  334. assert(I <= E && "Attempted to set backwards range!");
  335. assert(E <= size() && "Attempted to set out-of-bounds range!");
  336. if (I == E) return *this;
  337. if (isSmall()) {
  338. uintptr_t EMask = ((uintptr_t)1) << E;
  339. uintptr_t IMask = ((uintptr_t)1) << I;
  340. uintptr_t Mask = EMask - IMask;
  341. setSmallBits(getSmallBits() | Mask);
  342. } else
  343. getPointer()->set(I, E);
  344. return *this;
  345. }
  346. SmallBitVector &reset() {
  347. if (isSmall())
  348. setSmallBits(0);
  349. else
  350. getPointer()->reset();
  351. return *this;
  352. }
  353. SmallBitVector &reset(unsigned Idx) {
  354. if (isSmall())
  355. setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
  356. else
  357. getPointer()->reset(Idx);
  358. return *this;
  359. }
  360. /// Efficiently reset a range of bits in [I, E)
  361. SmallBitVector &reset(unsigned I, unsigned E) {
  362. assert(I <= E && "Attempted to reset backwards range!");
  363. assert(E <= size() && "Attempted to reset out-of-bounds range!");
  364. if (I == E) return *this;
  365. if (isSmall()) {
  366. uintptr_t EMask = ((uintptr_t)1) << E;
  367. uintptr_t IMask = ((uintptr_t)1) << I;
  368. uintptr_t Mask = EMask - IMask;
  369. setSmallBits(getSmallBits() & ~Mask);
  370. } else
  371. getPointer()->reset(I, E);
  372. return *this;
  373. }
  374. SmallBitVector &flip() {
  375. if (isSmall())
  376. setSmallBits(~getSmallBits());
  377. else
  378. getPointer()->flip();
  379. return *this;
  380. }
  381. SmallBitVector &flip(unsigned Idx) {
  382. if (isSmall())
  383. setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
  384. else
  385. getPointer()->flip(Idx);
  386. return *this;
  387. }
  388. // No argument flip.
  389. SmallBitVector operator~() const {
  390. return SmallBitVector(*this).flip();
  391. }
  392. // Indexing.
  393. reference operator[](unsigned Idx) {
  394. assert(Idx < size() && "Out-of-bounds Bit access.");
  395. return reference(*this, Idx);
  396. }
  397. bool operator[](unsigned Idx) const {
  398. assert(Idx < size() && "Out-of-bounds Bit access.");
  399. if (isSmall())
  400. return ((getSmallBits() >> Idx) & 1) != 0;
  401. return getPointer()->operator[](Idx);
  402. }
  403. /// Return the last element in the vector.
  404. bool back() const {
  405. assert(!empty() && "Getting last element of empty vector.");
  406. return (*this)[size() - 1];
  407. }
  408. bool test(unsigned Idx) const {
  409. return (*this)[Idx];
  410. }
  411. // Push single bit to end of vector.
  412. void push_back(bool Val) {
  413. resize(size() + 1, Val);
  414. }
  415. /// Pop one bit from the end of the vector.
  416. void pop_back() {
  417. assert(!empty() && "Empty vector has no element to pop.");
  418. resize(size() - 1);
  419. }
  420. /// Test if any common bits are set.
  421. bool anyCommon(const SmallBitVector &RHS) const {
  422. if (isSmall() && RHS.isSmall())
  423. return (getSmallBits() & RHS.getSmallBits()) != 0;
  424. if (!isSmall() && !RHS.isSmall())
  425. return getPointer()->anyCommon(*RHS.getPointer());
  426. for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
  427. if (test(i) && RHS.test(i))
  428. return true;
  429. return false;
  430. }
  431. // Comparison operators.
  432. bool operator==(const SmallBitVector &RHS) const {
  433. if (size() != RHS.size())
  434. return false;
  435. if (isSmall() && RHS.isSmall())
  436. return getSmallBits() == RHS.getSmallBits();
  437. else if (!isSmall() && !RHS.isSmall())
  438. return *getPointer() == *RHS.getPointer();
  439. else {
  440. for (size_type I = 0, E = size(); I != E; ++I) {
  441. if ((*this)[I] != RHS[I])
  442. return false;
  443. }
  444. return true;
  445. }
  446. }
  447. bool operator!=(const SmallBitVector &RHS) const {
  448. return !(*this == RHS);
  449. }
  450. // Intersection, union, disjoint union.
  451. // FIXME BitVector::operator&= does not resize the LHS but this does
  452. SmallBitVector &operator&=(const SmallBitVector &RHS) {
  453. resize(std::max(size(), RHS.size()));
  454. if (isSmall() && RHS.isSmall())
  455. setSmallBits(getSmallBits() & RHS.getSmallBits());
  456. else if (!isSmall() && !RHS.isSmall())
  457. getPointer()->operator&=(*RHS.getPointer());
  458. else {
  459. size_type I, E;
  460. for (I = 0, E = std::min(size(), RHS.size()); I != E; ++I)
  461. (*this)[I] = test(I) && RHS.test(I);
  462. for (E = size(); I != E; ++I)
  463. reset(I);
  464. }
  465. return *this;
  466. }
  467. /// Reset bits that are set in RHS. Same as *this &= ~RHS.
  468. SmallBitVector &reset(const SmallBitVector &RHS) {
  469. if (isSmall() && RHS.isSmall())
  470. setSmallBits(getSmallBits() & ~RHS.getSmallBits());
  471. else if (!isSmall() && !RHS.isSmall())
  472. getPointer()->reset(*RHS.getPointer());
  473. else
  474. for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
  475. if (RHS.test(i))
  476. reset(i);
  477. return *this;
  478. }
  479. /// Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
  480. bool test(const SmallBitVector &RHS) const {
  481. if (isSmall() && RHS.isSmall())
  482. return (getSmallBits() & ~RHS.getSmallBits()) != 0;
  483. if (!isSmall() && !RHS.isSmall())
  484. return getPointer()->test(*RHS.getPointer());
  485. unsigned i, e;
  486. for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
  487. if (test(i) && !RHS.test(i))
  488. return true;
  489. for (e = size(); i != e; ++i)
  490. if (test(i))
  491. return true;
  492. return false;
  493. }
  494. SmallBitVector &operator|=(const SmallBitVector &RHS) {
  495. resize(std::max(size(), RHS.size()));
  496. if (isSmall() && RHS.isSmall())
  497. setSmallBits(getSmallBits() | RHS.getSmallBits());
  498. else if (!isSmall() && !RHS.isSmall())
  499. getPointer()->operator|=(*RHS.getPointer());
  500. else {
  501. for (size_type I = 0, E = RHS.size(); I != E; ++I)
  502. (*this)[I] = test(I) || RHS.test(I);
  503. }
  504. return *this;
  505. }
  506. SmallBitVector &operator^=(const SmallBitVector &RHS) {
  507. resize(std::max(size(), RHS.size()));
  508. if (isSmall() && RHS.isSmall())
  509. setSmallBits(getSmallBits() ^ RHS.getSmallBits());
  510. else if (!isSmall() && !RHS.isSmall())
  511. getPointer()->operator^=(*RHS.getPointer());
  512. else {
  513. for (size_type I = 0, E = RHS.size(); I != E; ++I)
  514. (*this)[I] = test(I) != RHS.test(I);
  515. }
  516. return *this;
  517. }
  518. SmallBitVector &operator<<=(unsigned N) {
  519. if (isSmall())
  520. setSmallBits(getSmallBits() << N);
  521. else
  522. getPointer()->operator<<=(N);
  523. return *this;
  524. }
  525. SmallBitVector &operator>>=(unsigned N) {
  526. if (isSmall())
  527. setSmallBits(getSmallBits() >> N);
  528. else
  529. getPointer()->operator>>=(N);
  530. return *this;
  531. }
  532. // Assignment operator.
  533. const SmallBitVector &operator=(const SmallBitVector &RHS) {
  534. if (isSmall()) {
  535. if (RHS.isSmall())
  536. X = RHS.X;
  537. else
  538. switchToLarge(new BitVector(*RHS.getPointer()));
  539. } else {
  540. if (!RHS.isSmall())
  541. *getPointer() = *RHS.getPointer();
  542. else {
  543. delete getPointer();
  544. X = RHS.X;
  545. }
  546. }
  547. return *this;
  548. }
  549. const SmallBitVector &operator=(SmallBitVector &&RHS) {
  550. if (this != &RHS) {
  551. clear();
  552. swap(RHS);
  553. }
  554. return *this;
  555. }
  556. void swap(SmallBitVector &RHS) {
  557. std::swap(X, RHS.X);
  558. }
  559. /// Add '1' bits from Mask to this vector. Don't resize.
  560. /// This computes "*this |= Mask".
  561. void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  562. if (isSmall())
  563. applyMask<true, false>(Mask, MaskWords);
  564. else
  565. getPointer()->setBitsInMask(Mask, MaskWords);
  566. }
  567. /// Clear any bits in this vector that are set in Mask. Don't resize.
  568. /// This computes "*this &= ~Mask".
  569. void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  570. if (isSmall())
  571. applyMask<false, false>(Mask, MaskWords);
  572. else
  573. getPointer()->clearBitsInMask(Mask, MaskWords);
  574. }
  575. /// Add a bit to this vector for every '0' bit in Mask. Don't resize.
  576. /// This computes "*this |= ~Mask".
  577. void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  578. if (isSmall())
  579. applyMask<true, true>(Mask, MaskWords);
  580. else
  581. getPointer()->setBitsNotInMask(Mask, MaskWords);
  582. }
  583. /// Clear a bit in this vector for every '0' bit in Mask. Don't resize.
  584. /// This computes "*this &= Mask".
  585. void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  586. if (isSmall())
  587. applyMask<false, true>(Mask, MaskWords);
  588. else
  589. getPointer()->clearBitsNotInMask(Mask, MaskWords);
  590. }
  591. void invalid() {
  592. assert(empty());
  593. X = (uintptr_t)-1;
  594. }
  595. bool isInvalid() const { return X == (uintptr_t)-1; }
  596. ArrayRef<uintptr_t> getData(uintptr_t &Store) const {
  597. if (!isSmall())
  598. return getPointer()->getData();
  599. Store = getSmallBits();
  600. return Store;
  601. }
  602. private:
  603. template <bool AddBits, bool InvertMask>
  604. void applyMask(const uint32_t *Mask, unsigned MaskWords) {
  605. assert(MaskWords <= sizeof(uintptr_t) && "Mask is larger than base!");
  606. uintptr_t M = Mask[0];
  607. if (NumBaseBits == 64)
  608. M |= uint64_t(Mask[1]) << 32;
  609. if (InvertMask)
  610. M = ~M;
  611. if (AddBits)
  612. setSmallBits(getSmallBits() | M);
  613. else
  614. setSmallBits(getSmallBits() & ~M);
  615. }
  616. };
  617. inline SmallBitVector
  618. operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
  619. SmallBitVector Result(LHS);
  620. Result &= RHS;
  621. return Result;
  622. }
  623. inline SmallBitVector
  624. operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
  625. SmallBitVector Result(LHS);
  626. Result |= RHS;
  627. return Result;
  628. }
  629. inline SmallBitVector
  630. operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
  631. SmallBitVector Result(LHS);
  632. Result ^= RHS;
  633. return Result;
  634. }
  635. template <> struct DenseMapInfo<SmallBitVector> {
  636. static inline SmallBitVector getEmptyKey() { return SmallBitVector(); }
  637. static inline SmallBitVector getTombstoneKey() {
  638. SmallBitVector V;
  639. V.invalid();
  640. return V;
  641. }
  642. static unsigned getHashValue(const SmallBitVector &V) {
  643. uintptr_t Store;
  644. return DenseMapInfo<
  645. std::pair<SmallBitVector::size_type, ArrayRef<uintptr_t>>>::
  646. getHashValue(std::make_pair(V.size(), V.getData(Store)));
  647. }
  648. static bool isEqual(const SmallBitVector &LHS, const SmallBitVector &RHS) {
  649. if (LHS.isInvalid() || RHS.isInvalid())
  650. return LHS.isInvalid() == RHS.isInvalid();
  651. return LHS == RHS;
  652. }
  653. };
  654. } // end namespace llvm
  655. namespace std {
  656. /// Implement std::swap in terms of BitVector swap.
  657. inline void
  658. swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
  659. LHS.swap(RHS);
  660. }
  661. } // end namespace std
  662. #endif // LLVM_ADT_SMALLBITVECTOR_H
  663. #ifdef __GNUC__
  664. #pragma GCC diagnostic pop
  665. #endif