BitVector.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. //===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the BitVector class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_BITVECTOR_H
  14. #define LLVM_ADT_BITVECTOR_H
  15. #include "llvm/Support/Compiler.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/MathExtras.h"
  18. #include <algorithm>
  19. #include <cassert>
  20. #include <climits>
  21. #include <cstdlib>
  22. namespace llvm {
  23. class BitVector {
  24. typedef unsigned long BitWord;
  25. enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
  26. static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
  27. "Unsupported word size");
  28. BitWord *Bits; // Actual bits.
  29. unsigned Size; // Size of bitvector in bits.
  30. unsigned Capacity; // Size of allocated memory in BitWord.
  31. public:
  32. typedef unsigned size_type;
  33. // Encapsulation of a single bit.
  34. class reference {
  35. friend class BitVector;
  36. BitWord *WordRef;
  37. unsigned BitPos;
  38. reference(); // Undefined
  39. public:
  40. reference(BitVector &b, unsigned Idx) {
  41. WordRef = &b.Bits[Idx / BITWORD_SIZE];
  42. BitPos = Idx % BITWORD_SIZE;
  43. }
  44. reference(const reference&) = default;
  45. reference &operator=(reference t) {
  46. *this = bool(t);
  47. return *this;
  48. }
  49. reference& operator=(bool t) {
  50. if (t)
  51. *WordRef |= BitWord(1) << BitPos;
  52. else
  53. *WordRef &= ~(BitWord(1) << BitPos);
  54. return *this;
  55. }
  56. operator bool() const {
  57. return ((*WordRef) & (BitWord(1) << BitPos)) ? true : false;
  58. }
  59. };
  60. /// BitVector default ctor - Creates an empty bitvector.
  61. BitVector() : Size(0), Capacity(0) {
  62. Bits = nullptr;
  63. }
  64. /// BitVector ctor - Creates a bitvector of specified number of bits. All
  65. /// bits are initialized to the specified value.
  66. explicit BitVector(unsigned s, bool t = false) : Size(s) {
  67. Capacity = NumBitWords(s);
  68. Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
  69. init_words(Bits, Capacity, t);
  70. if (t)
  71. clear_unused_bits();
  72. }
  73. /// BitVector copy ctor.
  74. BitVector(const BitVector &RHS) : Size(RHS.size()) {
  75. if (Size == 0) {
  76. Bits = nullptr;
  77. Capacity = 0;
  78. return;
  79. }
  80. Capacity = NumBitWords(RHS.size());
  81. Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
  82. if (Bits == nullptr) throw std::bad_alloc(); // HLSL Change
  83. std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
  84. }
  85. BitVector(BitVector &&RHS)
  86. : Bits(RHS.Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
  87. RHS.Bits = nullptr;
  88. }
  89. ~BitVector() {
  90. std::free(Bits);
  91. }
  92. /// empty - Tests whether there are no bits in this bitvector.
  93. bool empty() const { return Size == 0; }
  94. /// size - Returns the number of bits in this bitvector.
  95. size_type size() const { return Size; }
  96. /// count - Returns the number of bits which are set.
  97. size_type count() const {
  98. unsigned NumBits = 0;
  99. for (unsigned i = 0; i < NumBitWords(size()); ++i)
  100. NumBits += countPopulation(Bits[i]);
  101. return NumBits;
  102. }
  103. /// any - Returns true if any bit is set.
  104. bool any() const {
  105. for (unsigned i = 0; i < NumBitWords(size()); ++i)
  106. if (Bits[i] != 0)
  107. return true;
  108. return false;
  109. }
  110. /// all - Returns true if all bits are set.
  111. bool all() const {
  112. for (unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
  113. if (Bits[i] != ~0UL)
  114. return false;
  115. // If bits remain check that they are ones. The unused bits are always zero.
  116. if (unsigned Remainder = Size % BITWORD_SIZE)
  117. return Bits[Size / BITWORD_SIZE] == (1UL << Remainder) - 1;
  118. return true;
  119. }
  120. /// none - Returns true if none of the bits are set.
  121. bool none() const {
  122. return !any();
  123. }
  124. /// find_first - Returns the index of the first set bit, -1 if none
  125. /// of the bits are set.
  126. int find_first() const {
  127. for (unsigned i = 0; i < NumBitWords(size()); ++i)
  128. if (Bits[i] != 0)
  129. return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
  130. return -1;
  131. }
  132. /// find_next - Returns the index of the next set bit following the
  133. /// "Prev" bit. Returns -1 if the next set bit is not found.
  134. int find_next(unsigned Prev) const {
  135. ++Prev;
  136. if (Prev >= Size)
  137. return -1;
  138. unsigned WordPos = Prev / BITWORD_SIZE;
  139. unsigned BitPos = Prev % BITWORD_SIZE;
  140. BitWord Copy = Bits[WordPos];
  141. // Mask off previous bits.
  142. Copy &= ~0UL << BitPos;
  143. if (Copy != 0)
  144. return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
  145. // Check subsequent words.
  146. for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
  147. if (Bits[i] != 0)
  148. return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
  149. return -1;
  150. }
  151. /// clear - Clear all bits.
  152. void clear() {
  153. Size = 0;
  154. }
  155. /// resize - Grow or shrink the bitvector.
  156. void resize(unsigned N, bool t = false) {
  157. if (N > Capacity * BITWORD_SIZE) {
  158. unsigned OldCapacity = Capacity;
  159. grow(N);
  160. init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
  161. }
  162. // Set any old unused bits that are now included in the BitVector. This
  163. // may set bits that are not included in the new vector, but we will clear
  164. // them back out below.
  165. if (N > Size)
  166. set_unused_bits(t);
  167. // Update the size, and clear out any bits that are now unused
  168. unsigned OldSize = Size;
  169. Size = N;
  170. if (t || N < OldSize)
  171. clear_unused_bits();
  172. }
  173. void reserve(unsigned N) {
  174. if (N > Capacity * BITWORD_SIZE)
  175. grow(N);
  176. }
  177. // Set, reset, flip
  178. BitVector &set() {
  179. init_words(Bits, Capacity, true);
  180. clear_unused_bits();
  181. return *this;
  182. }
  183. BitVector &set(unsigned Idx) {
  184. assert(Bits && "Bits never allocated");
  185. Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
  186. return *this;
  187. }
  188. /// set - Efficiently set a range of bits in [I, E)
  189. BitVector &set(unsigned I, unsigned E) {
  190. assert(I <= E && "Attempted to set backwards range!");
  191. assert(E <= size() && "Attempted to set out-of-bounds range!");
  192. if (I == E) return *this;
  193. if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
  194. BitWord EMask = 1UL << (E % BITWORD_SIZE);
  195. BitWord IMask = 1UL << (I % BITWORD_SIZE);
  196. BitWord Mask = EMask - IMask;
  197. Bits[I / BITWORD_SIZE] |= Mask;
  198. return *this;
  199. }
  200. BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
  201. Bits[I / BITWORD_SIZE] |= PrefixMask;
  202. I = RoundUpToAlignment(I, BITWORD_SIZE);
  203. for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
  204. Bits[I / BITWORD_SIZE] = ~0UL;
  205. BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
  206. if (I < E)
  207. Bits[I / BITWORD_SIZE] |= PostfixMask;
  208. return *this;
  209. }
  210. BitVector &reset() {
  211. init_words(Bits, Capacity, false);
  212. return *this;
  213. }
  214. BitVector &reset(unsigned Idx) {
  215. Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
  216. return *this;
  217. }
  218. /// reset - Efficiently reset a range of bits in [I, E)
  219. BitVector &reset(unsigned I, unsigned E) {
  220. assert(I <= E && "Attempted to reset backwards range!");
  221. assert(E <= size() && "Attempted to reset out-of-bounds range!");
  222. if (I == E) return *this;
  223. if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
  224. BitWord EMask = 1UL << (E % BITWORD_SIZE);
  225. BitWord IMask = 1UL << (I % BITWORD_SIZE);
  226. BitWord Mask = EMask - IMask;
  227. Bits[I / BITWORD_SIZE] &= ~Mask;
  228. return *this;
  229. }
  230. BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
  231. Bits[I / BITWORD_SIZE] &= ~PrefixMask;
  232. I = RoundUpToAlignment(I, BITWORD_SIZE);
  233. for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
  234. Bits[I / BITWORD_SIZE] = 0UL;
  235. BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
  236. if (I < E)
  237. Bits[I / BITWORD_SIZE] &= ~PostfixMask;
  238. return *this;
  239. }
  240. BitVector &flip() {
  241. for (unsigned i = 0; i < NumBitWords(size()); ++i)
  242. Bits[i] = ~Bits[i];
  243. clear_unused_bits();
  244. return *this;
  245. }
  246. BitVector &flip(unsigned Idx) {
  247. Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
  248. return *this;
  249. }
  250. // Indexing.
  251. reference operator[](unsigned Idx) {
  252. assert (Idx < Size && "Out-of-bounds Bit access.");
  253. return reference(*this, Idx);
  254. }
  255. bool operator[](unsigned Idx) const {
  256. assert (Idx < Size && "Out-of-bounds Bit access.");
  257. BitWord Mask = BitWord(1) << (Idx % BITWORD_SIZE);
  258. return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
  259. }
  260. bool test(unsigned Idx) const {
  261. return (*this)[Idx];
  262. }
  263. /// Test if any common bits are set.
  264. bool anyCommon(const BitVector &RHS) const {
  265. unsigned ThisWords = NumBitWords(size());
  266. unsigned RHSWords = NumBitWords(RHS.size());
  267. for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
  268. if (Bits[i] & RHS.Bits[i])
  269. return true;
  270. return false;
  271. }
  272. // Comparison operators.
  273. bool operator==(const BitVector &RHS) const {
  274. unsigned ThisWords = NumBitWords(size());
  275. unsigned RHSWords = NumBitWords(RHS.size());
  276. unsigned i;
  277. for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
  278. if (Bits[i] != RHS.Bits[i])
  279. return false;
  280. // Verify that any extra words are all zeros.
  281. if (i != ThisWords) {
  282. for (; i != ThisWords; ++i)
  283. if (Bits[i])
  284. return false;
  285. } else if (i != RHSWords) {
  286. for (; i != RHSWords; ++i)
  287. if (RHS.Bits[i])
  288. return false;
  289. }
  290. return true;
  291. }
  292. bool operator!=(const BitVector &RHS) const {
  293. return !(*this == RHS);
  294. }
  295. /// Intersection, union, disjoint union.
  296. BitVector &operator&=(const BitVector &RHS) {
  297. unsigned ThisWords = NumBitWords(size());
  298. unsigned RHSWords = NumBitWords(RHS.size());
  299. unsigned i;
  300. for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
  301. Bits[i] &= RHS.Bits[i];
  302. // Any bits that are just in this bitvector become zero, because they aren't
  303. // in the RHS bit vector. Any words only in RHS are ignored because they
  304. // are already zero in the LHS.
  305. for (; i != ThisWords; ++i)
  306. Bits[i] = 0;
  307. return *this;
  308. }
  309. /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
  310. BitVector &reset(const BitVector &RHS) {
  311. unsigned ThisWords = NumBitWords(size());
  312. unsigned RHSWords = NumBitWords(RHS.size());
  313. unsigned i;
  314. for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
  315. Bits[i] &= ~RHS.Bits[i];
  316. return *this;
  317. }
  318. /// test - Check if (This - RHS) is zero.
  319. /// This is the same as reset(RHS) and any().
  320. bool test(const BitVector &RHS) const {
  321. unsigned ThisWords = NumBitWords(size());
  322. unsigned RHSWords = NumBitWords(RHS.size());
  323. unsigned i;
  324. for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
  325. if ((Bits[i] & ~RHS.Bits[i]) != 0)
  326. return true;
  327. for (; i != ThisWords ; ++i)
  328. if (Bits[i] != 0)
  329. return true;
  330. return false;
  331. }
  332. BitVector &operator|=(const BitVector &RHS) {
  333. if (size() < RHS.size())
  334. resize(RHS.size());
  335. for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
  336. Bits[i] |= RHS.Bits[i];
  337. return *this;
  338. }
  339. BitVector &operator^=(const BitVector &RHS) {
  340. if (size() < RHS.size())
  341. resize(RHS.size());
  342. for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
  343. Bits[i] ^= RHS.Bits[i];
  344. return *this;
  345. }
  346. // Assignment operator.
  347. const BitVector &operator=(const BitVector &RHS) {
  348. if (this == &RHS) return *this;
  349. Size = RHS.size();
  350. unsigned RHSWords = NumBitWords(Size);
  351. if (Size <= Capacity * BITWORD_SIZE) {
  352. if (Size)
  353. std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
  354. clear_unused_bits();
  355. return *this;
  356. }
  357. // Grow the bitvector to have enough elements.
  358. Capacity = RHSWords;
  359. assert(Capacity > 0 && "negative capacity?");
  360. BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
  361. if (NewBits == nullptr) throw std::bad_alloc(); // HLSL Change
  362. std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
  363. // Destroy the old bits.
  364. std::free(Bits);
  365. Bits = NewBits;
  366. return *this;
  367. }
  368. const BitVector &operator=(BitVector &&RHS) {
  369. if (this == &RHS) return *this;
  370. std::free(Bits);
  371. Bits = RHS.Bits;
  372. Size = RHS.Size;
  373. Capacity = RHS.Capacity;
  374. RHS.Bits = nullptr;
  375. return *this;
  376. }
  377. void swap(BitVector &RHS) {
  378. std::swap(Bits, RHS.Bits);
  379. std::swap(Size, RHS.Size);
  380. std::swap(Capacity, RHS.Capacity);
  381. }
  382. //===--------------------------------------------------------------------===//
  383. // Portable bit mask operations.
  384. //===--------------------------------------------------------------------===//
  385. //
  386. // These methods all operate on arrays of uint32_t, each holding 32 bits. The
  387. // fixed word size makes it easier to work with literal bit vector constants
  388. // in portable code.
  389. //
  390. // The LSB in each word is the lowest numbered bit. The size of a portable
  391. // bit mask is always a whole multiple of 32 bits. If no bit mask size is
  392. // given, the bit mask is assumed to cover the entire BitVector.
  393. /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
  394. /// This computes "*this |= Mask".
  395. void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  396. applyMask<true, false>(Mask, MaskWords);
  397. }
  398. /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
  399. /// Don't resize. This computes "*this &= ~Mask".
  400. void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  401. applyMask<false, false>(Mask, MaskWords);
  402. }
  403. /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
  404. /// Don't resize. This computes "*this |= ~Mask".
  405. void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  406. applyMask<true, true>(Mask, MaskWords);
  407. }
  408. /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
  409. /// Don't resize. This computes "*this &= Mask".
  410. void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
  411. applyMask<false, true>(Mask, MaskWords);
  412. }
  413. private:
  414. unsigned NumBitWords(unsigned S) const {
  415. return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
  416. }
  417. // Set the unused bits in the high words.
  418. void set_unused_bits(bool t = true) {
  419. // Set high words first.
  420. unsigned UsedWords = NumBitWords(Size);
  421. if (Capacity > UsedWords)
  422. init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
  423. // Then set any stray high bits of the last used word.
  424. unsigned ExtraBits = Size % BITWORD_SIZE;
  425. if (ExtraBits) {
  426. BitWord ExtraBitMask = ~0UL << ExtraBits;
  427. if (t)
  428. Bits[UsedWords-1] |= ExtraBitMask;
  429. else
  430. Bits[UsedWords-1] &= ~ExtraBitMask;
  431. }
  432. }
  433. // Clear the unused bits in the high words.
  434. void clear_unused_bits() {
  435. set_unused_bits(false);
  436. }
  437. void grow(unsigned NewSize) {
  438. Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
  439. assert(Capacity > 0 && "realloc-ing zero space");
  440. // HLSL Change Starts: don't lose old buffer while reallocating
  441. // Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
  442. BitWord *newBits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
  443. if (newBits == nullptr)
  444. throw std::bad_alloc();
  445. Bits = newBits;
  446. // HLSL Change Ends
  447. clear_unused_bits();
  448. }
  449. void init_words(BitWord *B, unsigned NumWords, bool t) {
  450. memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
  451. }
  452. template<bool AddBits, bool InvertMask>
  453. void applyMask(const uint32_t *Mask, unsigned MaskWords) {
  454. static_assert(BITWORD_SIZE % 32 == 0, "Unsupported BitWord size.");
  455. MaskWords = std::min(MaskWords, (size() + 31) / 32);
  456. const unsigned Scale = BITWORD_SIZE / 32;
  457. unsigned i;
  458. for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
  459. BitWord BW = Bits[i];
  460. // This inner loop should unroll completely when BITWORD_SIZE > 32.
  461. for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
  462. uint32_t M = *Mask++;
  463. if (InvertMask) M = ~M;
  464. if (AddBits) BW |= BitWord(M) << b;
  465. else BW &= ~(BitWord(M) << b);
  466. }
  467. Bits[i] = BW;
  468. }
  469. for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
  470. uint32_t M = *Mask++;
  471. if (InvertMask) M = ~M;
  472. if (AddBits) Bits[i] |= BitWord(M) << b;
  473. else Bits[i] &= ~(BitWord(M) << b);
  474. }
  475. if (AddBits)
  476. clear_unused_bits();
  477. }
  478. };
  479. } // End llvm namespace
  480. namespace std {
  481. /// Implement std::swap in terms of BitVector swap.
  482. inline void
  483. swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {
  484. LHS.swap(RHS);
  485. }
  486. }
  487. #endif