]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/ADT/SmallBitVector.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, 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   /// Clear all bits.
282   void clear() {
283     if (!isSmall())
284       delete getPointer();
285     switchToSmall(0, 0);
286   }
287
288   /// Grow or shrink the bitvector.
289   void resize(unsigned N, bool t = false) {
290     if (!isSmall()) {
291       getPointer()->resize(N, t);
292     } else if (SmallNumDataBits >= N) {
293       uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
294       setSmallSize(N);
295       setSmallBits(NewBits | getSmallBits());
296     } else {
297       BitVector *BV = new BitVector(N, t);
298       uintptr_t OldBits = getSmallBits();
299       for (size_t i = 0, e = getSmallSize(); i != e; ++i)
300         (*BV)[i] = (OldBits >> i) & 1;
301       switchToLarge(BV);
302     }
303   }
304
305   void reserve(unsigned N) {
306     if (isSmall()) {
307       if (N > SmallNumDataBits) {
308         uintptr_t OldBits = getSmallRawBits();
309         size_t SmallSize = getSmallSize();
310         BitVector *BV = new BitVector(SmallSize);
311         for (size_t i = 0; i < SmallSize; ++i)
312           if ((OldBits >> i) & 1)
313             BV->set(i);
314         BV->reserve(N);
315         switchToLarge(BV);
316       }
317     } else {
318       getPointer()->reserve(N);
319     }
320   }
321
322   // Set, reset, flip
323   SmallBitVector &set() {
324     if (isSmall())
325       setSmallBits(~uintptr_t(0));
326     else
327       getPointer()->set();
328     return *this;
329   }
330
331   SmallBitVector &set(unsigned Idx) {
332     if (isSmall()) {
333       assert(Idx <= static_cast<unsigned>(
334                         std::numeric_limits<uintptr_t>::digits) &&
335              "undefined behavior");
336       setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
337     }
338     else
339       getPointer()->set(Idx);
340     return *this;
341   }
342
343   /// Efficiently set a range of bits in [I, E)
344   SmallBitVector &set(unsigned I, unsigned E) {
345     assert(I <= E && "Attempted to set backwards range!");
346     assert(E <= size() && "Attempted to set out-of-bounds range!");
347     if (I == E) return *this;
348     if (isSmall()) {
349       uintptr_t EMask = ((uintptr_t)1) << E;
350       uintptr_t IMask = ((uintptr_t)1) << I;
351       uintptr_t Mask = EMask - IMask;
352       setSmallBits(getSmallBits() | Mask);
353     } else
354       getPointer()->set(I, E);
355     return *this;
356   }
357
358   SmallBitVector &reset() {
359     if (isSmall())
360       setSmallBits(0);
361     else
362       getPointer()->reset();
363     return *this;
364   }
365
366   SmallBitVector &reset(unsigned Idx) {
367     if (isSmall())
368       setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
369     else
370       getPointer()->reset(Idx);
371     return *this;
372   }
373
374   /// Efficiently reset a range of bits in [I, E)
375   SmallBitVector &reset(unsigned I, unsigned E) {
376     assert(I <= E && "Attempted to reset backwards range!");
377     assert(E <= size() && "Attempted to reset 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()->reset(I, E);
386     return *this;
387   }
388
389   SmallBitVector &flip() {
390     if (isSmall())
391       setSmallBits(~getSmallBits());
392     else
393       getPointer()->flip();
394     return *this;
395   }
396
397   SmallBitVector &flip(unsigned Idx) {
398     if (isSmall())
399       setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
400     else
401       getPointer()->flip(Idx);
402     return *this;
403   }
404
405   // No argument flip.
406   SmallBitVector operator~() const {
407     return SmallBitVector(*this).flip();
408   }
409
410   // Indexing.
411   reference operator[](unsigned Idx) {
412     assert(Idx < size() && "Out-of-bounds Bit access.");
413     return reference(*this, Idx);
414   }
415
416   bool operator[](unsigned Idx) const {
417     assert(Idx < size() && "Out-of-bounds Bit access.");
418     if (isSmall())
419       return ((getSmallBits() >> Idx) & 1) != 0;
420     return getPointer()->operator[](Idx);
421   }
422
423   bool test(unsigned Idx) const {
424     return (*this)[Idx];
425   }
426
427   /// Test if any common bits are set.
428   bool anyCommon(const SmallBitVector &RHS) const {
429     if (isSmall() && RHS.isSmall())
430       return (getSmallBits() & RHS.getSmallBits()) != 0;
431     if (!isSmall() && !RHS.isSmall())
432       return getPointer()->anyCommon(*RHS.getPointer());
433
434     for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
435       if (test(i) && RHS.test(i))
436         return true;
437     return false;
438   }
439
440   // Comparison operators.
441   bool operator==(const SmallBitVector &RHS) const {
442     if (size() != RHS.size())
443       return false;
444     if (isSmall())
445       return getSmallBits() == RHS.getSmallBits();
446     else
447       return *getPointer() == *RHS.getPointer();
448   }
449
450   bool operator!=(const SmallBitVector &RHS) const {
451     return !(*this == RHS);
452   }
453
454   // Intersection, union, disjoint union.
455   SmallBitVector &operator&=(const SmallBitVector &RHS) {
456     resize(std::max(size(), RHS.size()));
457     if (isSmall())
458       setSmallBits(getSmallBits() & RHS.getSmallBits());
459     else if (!RHS.isSmall())
460       getPointer()->operator&=(*RHS.getPointer());
461     else {
462       SmallBitVector Copy = RHS;
463       Copy.resize(size());
464       getPointer()->operator&=(*Copy.getPointer());
465     }
466     return *this;
467   }
468
469   /// Reset bits that are set in RHS. Same as *this &= ~RHS.
470   SmallBitVector &reset(const SmallBitVector &RHS) {
471     if (isSmall() && RHS.isSmall())
472       setSmallBits(getSmallBits() & ~RHS.getSmallBits());
473     else if (!isSmall() && !RHS.isSmall())
474       getPointer()->reset(*RHS.getPointer());
475     else
476       for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
477         if (RHS.test(i))
478           reset(i);
479
480     return *this;
481   }
482
483   /// Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
484   bool test(const SmallBitVector &RHS) const {
485     if (isSmall() && RHS.isSmall())
486       return (getSmallBits() & ~RHS.getSmallBits()) != 0;
487     if (!isSmall() && !RHS.isSmall())
488       return getPointer()->test(*RHS.getPointer());
489
490     unsigned i, e;
491     for (i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
492       if (test(i) && !RHS.test(i))
493         return true;
494
495     for (e = size(); i != e; ++i)
496       if (test(i))
497         return true;
498
499     return false;
500   }
501
502   SmallBitVector &operator|=(const SmallBitVector &RHS) {
503     resize(std::max(size(), RHS.size()));
504     if (isSmall())
505       setSmallBits(getSmallBits() | RHS.getSmallBits());
506     else if (!RHS.isSmall())
507       getPointer()->operator|=(*RHS.getPointer());
508     else {
509       SmallBitVector Copy = RHS;
510       Copy.resize(size());
511       getPointer()->operator|=(*Copy.getPointer());
512     }
513     return *this;
514   }
515
516   SmallBitVector &operator^=(const SmallBitVector &RHS) {
517     resize(std::max(size(), RHS.size()));
518     if (isSmall())
519       setSmallBits(getSmallBits() ^ RHS.getSmallBits());
520     else if (!RHS.isSmall())
521       getPointer()->operator^=(*RHS.getPointer());
522     else {
523       SmallBitVector Copy = RHS;
524       Copy.resize(size());
525       getPointer()->operator^=(*Copy.getPointer());
526     }
527     return *this;
528   }
529
530   SmallBitVector &operator<<=(unsigned N) {
531     if (isSmall())
532       setSmallBits(getSmallBits() << N);
533     else
534       getPointer()->operator<<=(N);
535     return *this;
536   }
537
538   SmallBitVector &operator>>=(unsigned N) {
539     if (isSmall())
540       setSmallBits(getSmallBits() >> N);
541     else
542       getPointer()->operator>>=(N);
543     return *this;
544   }
545
546   // Assignment operator.
547   const SmallBitVector &operator=(const SmallBitVector &RHS) {
548     if (isSmall()) {
549       if (RHS.isSmall())
550         X = RHS.X;
551       else
552         switchToLarge(new BitVector(*RHS.getPointer()));
553     } else {
554       if (!RHS.isSmall())
555         *getPointer() = *RHS.getPointer();
556       else {
557         delete getPointer();
558         X = RHS.X;
559       }
560     }
561     return *this;
562   }
563
564   const SmallBitVector &operator=(SmallBitVector &&RHS) {
565     if (this != &RHS) {
566       clear();
567       swap(RHS);
568     }
569     return *this;
570   }
571
572   void swap(SmallBitVector &RHS) {
573     std::swap(X, RHS.X);
574   }
575
576   /// Add '1' bits from Mask to this vector. Don't resize.
577   /// This computes "*this |= Mask".
578   void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
579     if (isSmall())
580       applyMask<true, false>(Mask, MaskWords);
581     else
582       getPointer()->setBitsInMask(Mask, MaskWords);
583   }
584
585   /// Clear any bits in this vector that are set in Mask. Don't resize.
586   /// This computes "*this &= ~Mask".
587   void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
588     if (isSmall())
589       applyMask<false, false>(Mask, MaskWords);
590     else
591       getPointer()->clearBitsInMask(Mask, MaskWords);
592   }
593
594   /// Add a bit to this vector for every '0' bit in Mask. Don't resize.
595   /// This computes "*this |= ~Mask".
596   void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
597     if (isSmall())
598       applyMask<true, true>(Mask, MaskWords);
599     else
600       getPointer()->setBitsNotInMask(Mask, MaskWords);
601   }
602
603   /// Clear a bit in this vector for every '0' bit in Mask. Don't resize.
604   /// This computes "*this &= Mask".
605   void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
606     if (isSmall())
607       applyMask<false, true>(Mask, MaskWords);
608     else
609       getPointer()->clearBitsNotInMask(Mask, MaskWords);
610   }
611
612 private:
613   template <bool AddBits, bool InvertMask>
614   void applyMask(const uint32_t *Mask, unsigned MaskWords) {
615     assert(MaskWords <= sizeof(uintptr_t) && "Mask is larger than base!");
616     uintptr_t M = Mask[0];
617     if (NumBaseBits == 64)
618       M |= uint64_t(Mask[1]) << 32;
619     if (InvertMask)
620       M = ~M;
621     if (AddBits)
622       setSmallBits(getSmallBits() | M);
623     else
624       setSmallBits(getSmallBits() & ~M);
625   }
626 };
627
628 inline SmallBitVector
629 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
630   SmallBitVector Result(LHS);
631   Result &= RHS;
632   return Result;
633 }
634
635 inline SmallBitVector
636 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
637   SmallBitVector Result(LHS);
638   Result |= RHS;
639   return Result;
640 }
641
642 inline SmallBitVector
643 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
644   SmallBitVector Result(LHS);
645   Result ^= RHS;
646   return Result;
647 }
648
649 } // End llvm namespace
650
651 namespace std {
652   /// Implement std::swap in terms of BitVector swap.
653   inline void
654   swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
655     LHS.swap(RHS);
656   }
657 }
658
659 #endif