7563e81e1cef5e46c01234f08bca20f1f16b4d42
[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/MathExtras.h"
19 #include <cassert>
20
21 namespace llvm {
22
23 /// SmallBitVector - This is a 'bitvector' (really, a variable-sized bit array),
24 /// optimized for the case when the array is small.  It contains one
25 /// pointer-sized field, which is directly used as a plain collection of bits
26 /// when possible, or as a pointer to a larger heap-allocated array when
27 /// necessary.  This allows normal "small" cases to be fast without losing
28 /// generality for large inputs.
29 ///
30 class SmallBitVector {
31   // TODO: In "large" mode, a pointer to a BitVector is used, leading to an
32   // unnecessary level of indirection. It would be more efficient to use a
33   // pointer to memory containing size, allocation size, and the array of bits.
34   uintptr_t X;
35
36   enum {
37     // The number of bits in this class.
38     NumBaseBits = sizeof(uintptr_t) * CHAR_BIT,
39
40     // One bit is used to discriminate between small and large mode. The
41     // remaining bits are used for the small-mode representation.
42     SmallNumRawBits = NumBaseBits - 1,
43
44     // A few more bits are used to store the size of the bit set in small mode.
45     // Theoretically this is a ceil-log2. These bits are encoded in the most
46     // significant bits of the raw bits.
47     SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
48                         NumBaseBits == 64 ? 6 :
49                         SmallNumRawBits),
50
51     // The remaining bits are used to store the actual set in small mode.
52     SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
53   };
54
55   bool isSmall() const {
56     return X & uintptr_t(1);
57   }
58
59   BitVector *getPointer() const {
60     assert(!isSmall());
61     return reinterpret_cast<BitVector *>(X);
62   }
63
64   void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
65     X = 1;
66     setSmallSize(NewSize);
67     setSmallBits(NewSmallBits);
68   }
69
70   void switchToLarge(BitVector *BV) {
71     X = reinterpret_cast<uintptr_t>(BV);
72     assert(!isSmall() && "Tried to use an unaligned pointer");
73   }
74
75   // Return all the bits used for the "small" representation; this includes
76   // bits for the size as well as the element bits.
77   uintptr_t getSmallRawBits() const {
78     assert(isSmall());
79     return X >> 1;
80   }
81
82   void setSmallRawBits(uintptr_t NewRawBits) {
83     assert(isSmall());
84     X = NewRawBits << 1 | uintptr_t(1);
85   }
86
87   // Return the size.
88   size_t getSmallSize() const {
89     return getSmallRawBits() >> SmallNumDataBits;
90   }
91
92   void setSmallSize(size_t Size) {
93     setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
94   }
95
96   // Return the element bits.
97   uintptr_t getSmallBits() const {
98     return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
99   }
100
101   void setSmallBits(uintptr_t NewBits) {
102     setSmallRawBits(NewBits & ~(~uintptr_t(0) << getSmallSize()) |
103                     (getSmallSize() << SmallNumDataBits));
104   }
105
106 public:
107   /// SmallBitVector default ctor - Creates an empty bitvector.
108   SmallBitVector() : X(1) {}
109
110   /// SmallBitVector ctor - Creates a bitvector of specified number of bits. All
111   /// bits are initialized to the specified value.
112   explicit SmallBitVector(unsigned s, bool t = false) {
113     if (s <= SmallNumDataBits)
114       switchToSmall(t ? ~uintptr_t(0) : 0, s);
115     else
116       switchToLarge(new BitVector(s, t));
117   }
118
119   /// SmallBitVector copy ctor.
120   SmallBitVector(const SmallBitVector &RHS) {
121     if (RHS.isSmall())
122       X = RHS.X;
123     else
124       switchToLarge(new BitVector(*RHS.getPointer()));
125   }
126
127   ~SmallBitVector() {
128     if (!isSmall())
129       delete getPointer();
130   }
131
132   /// empty - Tests whether there are no bits in this bitvector.
133   bool empty() const {
134     return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
135   }
136
137   /// size - Returns the number of bits in this bitvector.
138   size_t size() const {
139     return isSmall() ? getSmallSize() : getPointer()->size();
140   }
141
142   /// count - Returns the number of bits which are set.
143   unsigned count() const {
144     if (isSmall()) {
145       uintptr_t Bits = getSmallBits();
146       if (sizeof(uintptr_t) * CHAR_BIT == 32)
147         return CountPopulation_32(Bits);
148       if (sizeof(uintptr_t) * CHAR_BIT == 64)
149         return CountPopulation_64(Bits);
150       assert(0 && "Unsupported!");
151     }
152     return getPointer()->count();
153   }
154
155   /// any - Returns true if any bit is set.
156   bool any() const {
157     if (isSmall())
158       return getSmallBits() != 0;
159     return getPointer()->any();
160   }
161
162   /// none - Returns true if none of the bits are set.
163   bool none() const {
164     if (isSmall())
165       return getSmallBits() == 0;
166     return getPointer()->none();
167   }
168
169   /// find_first - Returns the index of the first set bit, -1 if none
170   /// of the bits are set.
171   int find_first() const {
172     if (isSmall()) {
173       uintptr_t Bits = getSmallBits();
174       if (sizeof(uintptr_t) * CHAR_BIT == 32) {
175         size_t FirstBit = CountTrailingZeros_32(Bits);
176         return FirstBit == 32 ? -1 : FirstBit;
177       } else if (sizeof(uintptr_t) * CHAR_BIT == 64) {
178         size_t FirstBit = CountTrailingZeros_64(Bits);
179         return FirstBit == 64 ? -1 : FirstBit;
180       }
181       assert(0 && "Unsupported!");
182     }
183     return getPointer()->find_first();
184   }
185
186   /// find_next - Returns the index of the next set bit following the
187   /// "Prev" bit. Returns -1 if the next set bit is not found.
188   int find_next(unsigned Prev) const {
189     if (isSmall()) {
190       uintptr_t Bits = getSmallBits();
191       // Mask off previous bits.
192       Bits &= ~uintptr_t(0) << (Prev + 1);
193       if (sizeof(uintptr_t) * CHAR_BIT == 32) {
194         size_t FirstBit = CountTrailingZeros_32(Bits);
195         return FirstBit == 32 ? -1 : FirstBit;
196       } else if (sizeof(uintptr_t) * CHAR_BIT == 64) {
197         size_t FirstBit = CountTrailingZeros_64(Bits);
198         return FirstBit == 64 ? -1 : FirstBit;
199       }
200       assert(0 && "Unsupported!");
201     }
202     return getPointer()->find_next(Prev);
203   }
204
205   /// clear - Clear all bits.
206   void clear() {
207     if (!isSmall())
208       delete getPointer();
209     switchToSmall(0, 0);
210   }
211
212   /// resize - Grow or shrink the bitvector.
213   void resize(unsigned N, bool t = false) {
214     if (!isSmall()) {
215       getPointer()->resize(N, t);
216     } else if (SmallNumDataBits >= N) {
217       uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
218       setSmallSize(N);
219       setSmallBits(NewBits | getSmallBits());
220     } else {
221       BitVector *BV = new BitVector(N, t);
222       uintptr_t OldBits = getSmallBits();
223       for (size_t i = 0, e = getSmallSize(); i != e; ++i)
224         (*BV)[i] = (OldBits >> i) & 1;
225       switchToLarge(BV);
226     }
227   }
228
229   void reserve(unsigned N) {
230     if (isSmall()) {
231       if (N > SmallNumDataBits) {
232         uintptr_t OldBits = getSmallRawBits();
233         size_t SmallSize = getSmallSize();
234         BitVector *BV = new BitVector(SmallSize);
235         for (size_t i = 0; i < SmallSize; ++i)
236           if ((OldBits >> i) & 1)
237             BV->set(i);
238         BV->reserve(N);
239         switchToLarge(BV);
240       }
241     } else {
242       getPointer()->reserve(N);
243     }
244   }
245
246   // Set, reset, flip
247   SmallBitVector &set() {
248     if (isSmall())
249       setSmallBits(~uintptr_t(0));
250     else
251       getPointer()->set();
252     return *this;
253   }
254
255   SmallBitVector &set(unsigned Idx) {
256     if (isSmall())
257       setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
258     else
259       getPointer()->set(Idx);
260     return *this;
261   }
262
263   SmallBitVector &reset() {
264     if (isSmall())
265       setSmallBits(0);
266     else
267       getPointer()->reset();
268     return *this;
269   }
270
271   SmallBitVector &reset(unsigned Idx) {
272     if (isSmall())
273       setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
274     else
275       getPointer()->reset(Idx);
276     return *this;
277   }
278
279   SmallBitVector &flip() {
280     if (isSmall())
281       setSmallBits(~getSmallBits());
282     else
283       getPointer()->flip();
284     return *this;
285   }
286
287   SmallBitVector &flip(unsigned Idx) {
288     if (isSmall())
289       setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
290     else
291       getPointer()->flip(Idx);
292     return *this;
293   }
294
295   // No argument flip.
296   SmallBitVector operator~() const {
297     return SmallBitVector(*this).flip();
298   }
299
300   // Indexing.
301   // TODO: Add an index operator which returns a "reference" (proxy class).
302   bool operator[](unsigned Idx) const {
303     assert(Idx < size() && "Out-of-bounds Bit access.");
304     if (isSmall())
305       return ((getSmallBits() >> Idx) & 1) != 0;
306     return getPointer()->operator[](Idx);
307   }
308
309   bool test(unsigned Idx) const {
310     return (*this)[Idx];
311   }
312
313   // Comparison operators.
314   bool operator==(const SmallBitVector &RHS) const {
315     if (size() != RHS.size())
316       return false;
317     if (isSmall())
318       return getSmallBits() == RHS.getSmallBits();
319     else
320       return *getPointer() == *RHS.getPointer();
321   }
322
323   bool operator!=(const SmallBitVector &RHS) const {
324     return !(*this == RHS);
325   }
326
327   // Intersection, union, disjoint union.
328   SmallBitVector &operator&=(const SmallBitVector &RHS) {
329     resize(std::max(size(), RHS.size()));
330     if (isSmall())
331       setSmallBits(getSmallBits() & RHS.getSmallBits());
332     else if (!RHS.isSmall())
333       getPointer()->operator&=(*RHS.getPointer());
334     else {
335       SmallBitVector Copy = RHS;
336       Copy.resize(size());
337       getPointer()->operator&=(*Copy.getPointer());
338     }
339     return *this;
340   }
341
342   SmallBitVector &operator|=(const SmallBitVector &RHS) {
343     resize(std::max(size(), RHS.size()));
344     if (isSmall())
345       setSmallBits(getSmallBits() | RHS.getSmallBits());
346     else if (!RHS.isSmall())
347       getPointer()->operator|=(*RHS.getPointer());
348     else {
349       SmallBitVector Copy = RHS;
350       Copy.resize(size());
351       getPointer()->operator|=(*Copy.getPointer());
352     }
353     return *this;
354   }
355
356   SmallBitVector &operator^=(const SmallBitVector &RHS) {
357     resize(std::max(size(), RHS.size()));
358     if (isSmall())
359       setSmallBits(getSmallBits() ^ RHS.getSmallBits());
360     else if (!RHS.isSmall())
361       getPointer()->operator^=(*RHS.getPointer());
362     else {
363       SmallBitVector Copy = RHS;
364       Copy.resize(size());
365       getPointer()->operator^=(*Copy.getPointer());
366     }
367     return *this;
368   }
369
370   // Assignment operator.
371   const SmallBitVector &operator=(const SmallBitVector &RHS) {
372     if (isSmall()) {
373       if (RHS.isSmall())
374         X = RHS.X;
375       else
376         switchToLarge(new BitVector(*RHS.getPointer()));
377     } else {
378       if (!RHS.isSmall())
379         *getPointer() = *RHS.getPointer();
380       else {
381         delete getPointer();
382         X = RHS.X;
383       }
384     }
385     return *this;
386   }
387
388   void swap(SmallBitVector &RHS) {
389     std::swap(X, RHS.X);
390   }
391 };
392
393 inline SmallBitVector
394 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
395   SmallBitVector Result(LHS);
396   Result &= RHS;
397   return Result;
398 }
399
400 inline SmallBitVector
401 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
402   SmallBitVector Result(LHS);
403   Result |= RHS;
404   return Result;
405 }
406
407 inline SmallBitVector
408 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
409   SmallBitVector Result(LHS);
410   Result ^= RHS;
411   return Result;
412 }
413
414 } // End llvm namespace
415
416 namespace std {
417   /// Implement std::swap in terms of BitVector swap.
418   inline void
419   swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
420     LHS.swap(RHS);
421   }
422 }
423
424 #endif