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