BitVector.h 17 KB

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