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