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