SmallBitVector.h 21 KB

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