]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/ADT/SmallBitVector.h
Merge ^/head r317216 through r317280.
[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 {
121     return getSmallRawBits() >> SmallNumDataBits;
122   }
123
124   void setSmallSize(size_t Size) {
125     setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
126   }
127
128   // Return the element bits.
129   uintptr_t getSmallBits() const {
130     return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
131   }
132
133   void setSmallBits(uintptr_t NewBits) {
134     setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
135                     (getSmallSize() << SmallNumDataBits));
136   }
137
138 public:
139   /// Creates an empty bitvector.
140   SmallBitVector() : X(1) {}
141
142   /// Creates a bitvector of specified number of bits. All bits are initialized
143   /// to the specified value.
144   explicit SmallBitVector(unsigned s, bool t = false) {
145     if (s <= SmallNumDataBits)
146       switchToSmall(t ? ~uintptr_t(0) : 0, s);
147     else
148       switchToLarge(new BitVector(s, t));
149   }
150
151   /// SmallBitVector copy ctor.
152   SmallBitVector(const SmallBitVector &RHS) {
153     if (RHS.isSmall())
154       X = RHS.X;
155     else
156       switchToLarge(new BitVector(*RHS.getPointer()));
157   }
158
159   SmallBitVector(SmallBitVector &&RHS) : X(RHS.X) {
160     RHS.X = 1;
161   }
162
163   ~SmallBitVector() {
164     if (!isSmall())
165       delete getPointer();
166   }
167
168   /// Tests whether there are no bits in this bitvector.
169   bool empty() const {
170     return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
171   }
172
173   /// Returns the number of bits in this bitvector.
174   size_t size() const {
175     return isSmall() ? getSmallSize() : getPointer()->size();
176   }
177
178   /// Returns the number of bits which are set.
179   size_type count() const {
180     if (isSmall()) {
181       uintptr_t Bits = getSmallBits();
182       return countPopulation(Bits);
183     }
184     return getPointer()->count();
185   }
186
187   /// Returns true if any bit is set.
188   bool any() const {
189     if (isSmall())
190       return getSmallBits() != 0;
191     return getPointer()->any();
192   }
193
194   /// Returns true if all bits are set.
195   bool all() const {
196     if (isSmall())
197       return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
198     return getPointer()->all();
199   }
200
201   /// Returns true if none of the bits are set.
202   bool none() const {
203     if (isSmall())
204       return getSmallBits() == 0;
205     return getPointer()->none();
206   }
207
208   /// Returns the index of the first set bit, -1 if none of the bits are set.
209   int find_first() const {
210     if (isSmall()) {
211       uintptr_t Bits = getSmallBits();
212       if (Bits == 0)
213         return -1;
214       return countTrailingZeros(Bits);
215     }
216     return getPointer()->find_first();
217   }
218
219   /// Returns the index of the first unset bit, -1 if all of the bits are set.
220   int find_first_unset() const {
221     if (isSmall()) {
222       if (count() == getSmallSize())
223         return -1;
224
225       uintptr_t Bits = getSmallBits();
226       return countTrailingOnes(Bits);
227     }
228     return getPointer()->find_first_unset();
229   }
230
231   /// Returns the index of the next set bit following the "Prev" bit.
232   /// Returns -1 if the next set bit is not found.
233   int find_next(unsigned Prev) const {
234     if (isSmall()) {
235       uintptr_t Bits = getSmallBits();
236       // Mask off previous bits.
237       Bits &= ~uintptr_t(0) << (Prev + 1);
238       if (Bits == 0 || Prev + 1 >= getSmallSize())
239         return -1;
240       return countTrailingZeros(Bits);
241     }
242     return getPointer()->find_next(Prev);
243   }
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       ++Prev;
250       uintptr_t Bits = getSmallBits();
251       // Mask in previous bits.
252       uintptr_t Mask = (1 << Prev) - 1;
253       Bits |= Mask;
254
255       if (Bits == ~uintptr_t(0) || Prev + 1 >= getSmallSize())
256         return -1;
257       return countTrailingOnes(Bits);
258     }
259     return getPointer()->find_next_unset(Prev);
260   }
261
262   /// Clear all bits.
263   void clear() {
264     if (!isSmall())
265       delete getPointer();
266     switchToSmall(0, 0);
267   }
268
269   /// Grow or shrink the bitvector.
270   void resize(unsigned N, bool t = false) {
271     if (!isSmall()) {
272       getPointer()->resize(N, t);
273     } else if (SmallNumDataBits >= N) {
274       uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
275       setSmallSize(N);
276       setSmallBits(NewBits | getSmallBits());
277     } else {
278       BitVector *BV = new BitVector(N, t);
279       uintptr_t OldBits = getSmallBits();
280       for (size_t i = 0, e = getSmallSize(); i != e; ++i)
281         (*BV)[i] = (OldBits >> i) & 1;
282       switchToLarge(BV);
283     }
284   }
285
286   void reserve(unsigned N) {
287     if (isSmall()) {
288       if (N > SmallNumDataBits) {
289         uintptr_t OldBits = getSmallRawBits();
290         size_t SmallSize = getSmallSize();
291         BitVector *BV = new BitVector(SmallSize);
292         for (size_t i = 0; i < SmallSize; ++i)
293           if ((OldBits >> i) & 1)
294             BV->set(i);
295         BV->reserve(N);
296         switchToLarge(BV);
297       }
298     } else {
299       getPointer()->reserve(N);
300     }
301   }
302
303   // Set, reset, flip
304   SmallBitVector &set() {
305     if (isSmall())
306       setSmallBits(~uintptr_t(0));
307     else
308       getPointer()->set();
309     return *this;
310   }
311
312   SmallBitVector &set(unsigned Idx) {
313     if (isSmall()) {
314       assert(Idx <= static_cast<unsigned>(
315                         std::numeric_limits<uintptr_t>::digits) &&
316              "undefined behavior");
317       setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
318     }
319     else
320       getPointer()->set(Idx);
321     return *this;
322   }
323
324   /// Efficiently set a range of bits in [I, E)
325   SmallBitVector &set(unsigned I, unsigned E) {
326     assert(I <= E && "Attempted to set backwards range!");
327     assert(E <= size() && "Attempted to set out-of-bounds range!");
328     if (I == E) return *this;
329     if (isSmall()) {
330       uintptr_t EMask = ((uintptr_t)1) << E;
331       uintptr_t IMask = ((uintptr_t)1) << I;
332       uintptr_t Mask = EMask - IMask;
333       setSmallBits(getSmallBits() | Mask);
334     } else
335       getPointer()->set(I, E);
336     return *this;
337   }
338
339   SmallBitVector &reset() {
340     if (isSmall())
341       setSmallBits(0);
342     else
343       getPointer()->reset();
344     return *this;
345   }
346
347   SmallBitVector &reset(unsigned Idx) {
348     if (isSmall())
349       setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
350     else
351       getPointer()->reset(Idx);
352     return *this;
353   }
354
355   /// Efficiently reset a range of bits in [I, E)
356   SmallBitVector &reset(unsigned I, unsigned E) {
357     assert(I <= E && "Attempted to reset backwards range!");
358     assert(E <= size() && "Attempted to reset out-of-bounds range!");
359     if (I == E) return *this;
360     if (isSmall()) {
361       uintptr_t EMask = ((uintptr_t)1) << E;
362       uintptr_t IMask = ((uintptr_t)1) << I;
363       uintptr_t Mask = EMask - IMask;
364       setSmallBits(getSmallBits() & ~Mask);
365     } else
366       getPointer()->reset(I, E);
367     return *this;
368   }
369
370   SmallBitVector &flip() {
371     if (isSmall())
372       setSmallBits(~getSmallBits());
373     else
374       getPointer()->flip();
375     return *this;
376   }
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
386   // No argument flip.
387   SmallBitVector operator~() const {
388     return SmallBitVector(*this).flip();
389   }
390
391   // Indexing.
392   reference operator[](unsigned Idx) {
393     assert(Idx < size() && "Out-of-bounds Bit access.");
394     return reference(*this, Idx);
395   }
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
404   bool test(unsigned Idx) const {
405     return (*this)[Idx];
406   }
407
408   /// Test if any common bits are set.
409   bool anyCommon(const SmallBitVector &RHS) const {
410     if (isSmall() && RHS.isSmall())
411       return (getSmallBits() & RHS.getSmallBits()) != 0;
412     if (!isSmall() && !RHS.isSmall())
413       return getPointer()->anyCommon(*RHS.getPointer());
414
415     for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
416       if (test(i) && RHS.test(i))
417         return true;
418     return false;
419   }
420
421   // Comparison operators.
422   bool operator==(const SmallBitVector &RHS) const {
423     if (size() != RHS.size())
424       return false;
425     if (isSmall())
426       return getSmallBits() == RHS.getSmallBits();
427     else
428       return *getPointer() == *RHS.getPointer();
429   }
430
431   bool operator!=(const SmallBitVector &RHS) const {
432     return !(*this == RHS);
433   }
434
435   // Intersection, union, disjoint union.
436   SmallBitVector &operator&=(const SmallBitVector &RHS) {
437     resize(std::max(size(), RHS.size()));
438     if (isSmall())
439       setSmallBits(getSmallBits() & RHS.getSmallBits());
440     else if (!RHS.isSmall())
441       getPointer()->operator&=(*RHS.getPointer());
442     else {
443       SmallBitVector Copy = RHS;
444       Copy.resize(size());
445       getPointer()->operator&=(*Copy.getPointer());
446     }
447     return *this;
448   }
449
450   /// Reset bits that are set in RHS. Same as *this &= ~RHS.
451   SmallBitVector &reset(const SmallBitVector &RHS) {
452     if (isSmall() && RHS.isSmall())
453       setSmallBits(getSmallBits() & ~RHS.getSmallBits());
454     else if (!isSmall() && !RHS.isSmall())
455       getPointer()->reset(*RHS.getPointer());
456     else
457       for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
458         if (RHS.test(i))
459           reset(i);
460
461     return *this;
462   }
463
464   /// Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
465   bool test(const SmallBitVector &RHS) const {
466     if (isSmall() && RHS.isSmall())
467       return (getSmallBits() & ~RHS.getSmallBits()) != 0;
468     if (!isSmall() && !RHS.isSmall())
469       return getPointer()->test(*RHS.getPointer());
470
471     unsigned i, e;
472     for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
473       if (test(i) && !RHS.test(i))
474         return true;
475
476     for (e = size(); i != e; ++i)
477       if (test(i))
478         return true;
479
480     return false;
481   }
482
483   SmallBitVector &operator|=(const SmallBitVector &RHS) {
484     resize(std::max(size(), RHS.size()));
485     if (isSmall())
486       setSmallBits(getSmallBits() | RHS.getSmallBits());
487     else if (!RHS.isSmall())
488       getPointer()->operator|=(*RHS.getPointer());
489     else {
490       SmallBitVector Copy = RHS;
491       Copy.resize(size());
492       getPointer()->operator|=(*Copy.getPointer());
493     }
494     return *this;
495   }
496
497   SmallBitVector &operator^=(const SmallBitVector &RHS) {
498     resize(std::max(size(), RHS.size()));
499     if (isSmall())
500       setSmallBits(getSmallBits() ^ RHS.getSmallBits());
501     else if (!RHS.isSmall())
502       getPointer()->operator^=(*RHS.getPointer());
503     else {
504       SmallBitVector Copy = RHS;
505       Copy.resize(size());
506       getPointer()->operator^=(*Copy.getPointer());
507     }
508     return *this;
509   }
510
511   SmallBitVector &operator<<=(unsigned N) {
512     if (isSmall())
513       setSmallBits(getSmallBits() << N);
514     else
515       getPointer()->operator<<=(N);
516     return *this;
517   }
518
519   SmallBitVector &operator>>=(unsigned N) {
520     if (isSmall())
521       setSmallBits(getSmallBits() >> N);
522     else
523       getPointer()->operator>>=(N);
524     return *this;
525   }
526
527   // Assignment operator.
528   const SmallBitVector &operator=(const SmallBitVector &RHS) {
529     if (isSmall()) {
530       if (RHS.isSmall())
531         X = RHS.X;
532       else
533         switchToLarge(new BitVector(*RHS.getPointer()));
534     } else {
535       if (!RHS.isSmall())
536         *getPointer() = *RHS.getPointer();
537       else {
538         delete getPointer();
539         X = RHS.X;
540       }
541     }
542     return *this;
543   }
544
545   const SmallBitVector &operator=(SmallBitVector &&RHS) {
546     if (this != &RHS) {
547       clear();
548       swap(RHS);
549     }
550     return *this;
551   }
552
553   void swap(SmallBitVector &RHS) {
554     std::swap(X, RHS.X);
555   }
556
557   /// Add '1' bits from Mask to this vector. Don't resize.
558   /// This computes "*this |= Mask".
559   void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
560     if (isSmall())
561       applyMask<true, false>(Mask, MaskWords);
562     else
563       getPointer()->setBitsInMask(Mask, MaskWords);
564   }
565
566   /// Clear any bits in this vector that are set in Mask. Don't resize.
567   /// This computes "*this &= ~Mask".
568   void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
569     if (isSmall())
570       applyMask<false, false>(Mask, MaskWords);
571     else
572       getPointer()->clearBitsInMask(Mask, MaskWords);
573   }
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
584   /// Clear a bit in this vector for every '0' bit in Mask. Don't resize.
585   /// This computes "*this &= Mask".
586   void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
587     if (isSmall())
588       applyMask<false, true>(Mask, MaskWords);
589     else
590       getPointer()->clearBitsNotInMask(Mask, MaskWords);
591   }
592
593 private:
594   template <bool AddBits, bool InvertMask>
595   void applyMask(const uint32_t *Mask, unsigned MaskWords) {
596     assert(MaskWords <= sizeof(uintptr_t) && "Mask is larger than base!");
597     uintptr_t M = Mask[0];
598     if (NumBaseBits == 64)
599       M |= uint64_t(Mask[1]) << 32;
600     if (InvertMask)
601       M = ~M;
602     if (AddBits)
603       setSmallBits(getSmallBits() | M);
604     else
605       setSmallBits(getSmallBits() & ~M);
606   }
607 };
608
609 inline SmallBitVector
610 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
611   SmallBitVector Result(LHS);
612   Result &= RHS;
613   return Result;
614 }
615
616 inline SmallBitVector
617 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
618   SmallBitVector Result(LHS);
619   Result |= RHS;
620   return Result;
621 }
622
623 inline SmallBitVector
624 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
625   SmallBitVector Result(LHS);
626   Result ^= RHS;
627   return Result;
628 }
629
630 } // End llvm namespace
631
632 namespace std {
633   /// Implement std::swap in terms of BitVector swap.
634   inline void
635   swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
636     LHS.swap(RHS);
637   }
638 }
639
640 #endif