Properly emit range comparisons for switch cases, where neighbour cases
[oota-llvm.git] / include / llvm / ADT / BitVector.h
1 //===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Evan Cheng and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the BitVector class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_BITVECTOR_H
15 #define LLVM_ADT_BITVECTOR_H
16
17 #include "llvm/Support/MathExtras.h"
18 #include <algorithm>
19 #include <cstdlib>
20
21 namespace llvm {
22
23 class BitVector {
24   typedef unsigned long BitWord;
25
26   enum { BITS_PER_WORD = sizeof(BitWord) * 8 };
27
28   BitWord  *Bits;        // Actual bits. 
29   unsigned Size;         // Size of bitvector in bits.
30   unsigned Capacity;     // Size of allocated memory in BitWord.
31
32 public:
33   // Encapsulation of a single bit.
34   class reference {
35     friend class BitVector;
36
37     BitWord *WordRef;
38     unsigned BitPos;
39
40     reference();  // Undefined
41
42   public:
43     reference(BitVector &b, unsigned Idx) {
44       WordRef = &b.Bits[Idx / BITS_PER_WORD];
45       BitPos = Idx % BITS_PER_WORD;
46     }
47
48     ~reference() {}
49
50     reference& operator=(bool t) {
51       if (t)
52         *WordRef |= 1L << BitPos;
53       else
54         *WordRef &= ~(1L << BitPos);
55       return *this;
56     }
57
58     operator bool() const {
59       return (*WordRef) & (1L << BitPos);
60     }
61   };
62
63
64   /// BitVector default ctor - Creates an empty bitvector.
65   BitVector() : Size(0), Capacity(0) {
66     Bits = NULL;
67   }
68
69   /// BitVector ctor - Creates a bitvector of specified number of bits. All
70   /// bits are initialized to the specified value.
71   explicit BitVector(unsigned s, bool t = false) : Size(s) {
72     Capacity = NumBitWords(s);
73     Bits = new BitWord[Capacity];
74     init_words(Bits, Capacity, t);
75     if (t)
76       clear_unused_bits();
77   }
78
79   /// BitVector copy ctor.
80   BitVector(const BitVector &RHS) : Size(RHS.size()) {
81     if (Size == 0) {
82       Bits = NULL;
83       Capacity = 0;
84       return;
85     }
86
87     Capacity = NumBitWords(RHS.size());
88     Bits = new BitWord[Capacity];
89     std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
90   }
91   
92   ~BitVector() {
93     delete[] Bits;
94   }
95
96   /// size - Returns the number of bits in this bitvector.
97   unsigned size() const { return Size; }
98
99   /// count - Returns the number of bits which are set.
100   unsigned count() const {
101     unsigned NumBits = 0;
102     for (unsigned i = 0; i < NumBitWords(size()); ++i)
103       if (sizeof(BitWord) == 4)
104         NumBits += CountPopulation_32(Bits[i]);
105       else if (sizeof(BitWord) == 8)
106         NumBits += CountPopulation_64(Bits[i]);
107       else
108         assert(0 && "Unsupported!");
109     return NumBits;
110   }
111
112   /// any - Returns true if any bit is set.
113   bool any() const {
114     for (unsigned i = 0; i < NumBitWords(size()); ++i)
115       if (Bits[i] != 0)
116         return true;
117     return false;
118   }
119
120   /// none - Returns true if none of the bits are set.
121   bool none() const {
122     return !any();
123   }
124
125   /// find_first - Returns the index of the first set bit, -1 if none
126   /// of the bits are set.
127   int find_first() const {
128     for (unsigned i = 0; i < NumBitWords(size()); ++i)
129       if (Bits[i] != 0) {
130         if (sizeof(BitWord) == 4)
131           return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
132         else if (sizeof(BitWord) == 8)
133           return i * BITS_PER_WORD + CountTrailingZeros_64(Bits[i]);
134         else
135           assert(0 && "Unsupported!");
136       }
137     return -1;
138   }
139
140   /// find_next - Returns the index of the next set bit following the
141   /// "Prev" bit. Returns -1 if the next set bit is not found.
142   int find_next(unsigned Prev) const {
143     ++Prev;
144     if (Prev >= Size)
145       return -1;
146
147     unsigned WordPos = Prev / BITS_PER_WORD;
148     unsigned BitPos = Prev % BITS_PER_WORD;
149     BitWord Copy = Bits[WordPos];
150     // Mask off previous bits.
151     Copy &= ~0L << BitPos;
152
153     if (Copy != 0) {
154       if (sizeof(BitWord) == 4)
155         return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
156       else if (sizeof(BitWord) == 8)
157         return WordPos * BITS_PER_WORD + CountTrailingZeros_64(Copy);
158       else
159         assert(0 && "Unsupported!");
160     }
161
162     // Check subsequent words.
163     for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
164       if (Bits[i] != 0) {
165         if (sizeof(BitWord) == 4)
166           return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
167         else if (sizeof(BitWord) == 8)
168           return i * BITS_PER_WORD + CountTrailingZeros_64(Bits[i]);
169         else
170           assert(0 && "Unsupported!");
171       }
172     return -1;
173   }
174
175   /// clear - Clear all bits.
176   void clear() {
177     Size = 0;
178   }
179
180   /// resize - Grow or shrink the bitvector.
181   void resize(unsigned N, bool t = false) {
182     if (N > Capacity * BITS_PER_WORD) {
183       unsigned OldCapacity = Capacity;
184       grow(N);
185       init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
186     }
187     Size = N;
188     clear_unused_bits();
189   }
190
191   void reserve(unsigned N) {
192     if (N > Capacity * BITS_PER_WORD)
193       grow(N);
194   }
195
196   // Set, reset, flip
197   BitVector &set() {
198     init_words(Bits, Capacity, true);
199     clear_unused_bits();
200     return *this;
201   }
202
203   BitVector &set(unsigned Idx) {
204     Bits[Idx / BITS_PER_WORD] |= 1L << (Idx % BITS_PER_WORD);
205     return *this;
206   }
207
208   BitVector &reset() {
209     init_words(Bits, Capacity, false);
210     return *this;
211   }
212
213   BitVector &reset(unsigned Idx) {
214     Bits[Idx / BITS_PER_WORD] &= ~(1L << (Idx % BITS_PER_WORD));
215     return *this;
216   }
217
218   BitVector &flip() {
219     for (unsigned i = 0; i < NumBitWords(size()); ++i)
220       Bits[i] = ~Bits[i];
221     clear_unused_bits();
222     return *this;
223   }
224
225   BitVector &flip(unsigned Idx) {
226     Bits[Idx / BITS_PER_WORD] ^= 1L << (Idx % BITS_PER_WORD);
227     return *this;
228   }
229
230   // No argument flip.
231   BitVector operator~() const {
232     return BitVector(*this).flip();
233   }
234
235   // Indexing.
236   reference operator[](unsigned Idx) {
237     return reference(*this, Idx);
238   }
239
240   bool operator[](unsigned Idx) const {
241     BitWord Mask = 1L << (Idx % BITS_PER_WORD);
242     return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
243   }
244
245   bool test(unsigned Idx) const {
246     return (*this)[Idx];
247   }
248
249   // Comparison operators.
250   bool operator==(const BitVector &RHS) const {
251     if (Size != RHS.Size)
252       return false;
253
254     for (unsigned i = 0; i < NumBitWords(size()); ++i)
255       if (Bits[i] != RHS.Bits[i])
256         return false;
257     return true;
258   }
259
260   bool operator!=(const BitVector &RHS) const {
261     return !(*this == RHS);
262   }
263
264   // Intersection, union, disjoint union.
265   BitVector operator&=(const BitVector &RHS) {
266     assert(Size == RHS.Size && "Illegal operation!");
267     for (unsigned i = 0; i < NumBitWords(size()); ++i)
268       Bits[i] &= RHS.Bits[i];
269     return *this;
270   }
271
272   BitVector operator|=(const BitVector &RHS) {
273     assert(Size == RHS.Size && "Illegal operation!");
274     for (unsigned i = 0; i < NumBitWords(size()); ++i)
275       Bits[i] |= RHS.Bits[i];
276     return *this;
277   }
278
279   BitVector operator^=(const BitVector &RHS) {
280     assert(Size == RHS.Size && "Illegal operation!");
281     for (unsigned i = 0; i < NumBitWords(size()); ++i)
282       Bits[i] ^= RHS.Bits[i];
283     return *this;
284   }
285   
286   // Assignment operator.
287   const BitVector &operator=(const BitVector &RHS) {
288     if (this == &RHS) return *this;
289
290     Size = RHS.size();
291     unsigned RHSWords = NumBitWords(Size);
292     if (Size <= Capacity * BITS_PER_WORD) {
293       std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
294       clear_unused_bits();
295       return *this;
296     }
297   
298     // Grow the bitvector to have enough elements.
299     Capacity = NumBitWords(Size);
300     BitWord *NewBits = new BitWord[Capacity];
301     std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
302
303     // Destroy the old bits.
304     delete[] Bits;
305     Bits = NewBits;
306
307     return *this;
308   }
309
310 private:
311   unsigned NumBitWords(unsigned S) const {
312     return (S + BITS_PER_WORD-1) / BITS_PER_WORD;
313   }
314
315   // Clear the unused top bits in the high word.
316   void clear_unused_bits() {
317     unsigned ExtraBits = Size % BITS_PER_WORD;
318     if (ExtraBits) {
319       unsigned index = Size / BITS_PER_WORD;
320       Bits[index] &= ~(~0L << ExtraBits);
321     }
322   }
323
324   void grow(unsigned NewSize) {
325     unsigned OldCapacity = Capacity;
326     Capacity = NumBitWords(NewSize);
327     BitWord *NewBits = new BitWord[Capacity];
328
329     // Copy the old bits over.
330     if (OldCapacity != 0)
331       std::copy(Bits, &Bits[OldCapacity], NewBits);
332
333     // Destroy the old bits.
334     delete[] Bits;
335     Bits = NewBits;
336   }
337
338   void init_words(BitWord *B, unsigned NumWords, bool t) {
339     memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
340   } 
341 };
342
343 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
344   BitVector Result(LHS);
345   Result &= RHS;
346   return Result;
347 }
348
349 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
350   BitVector Result(LHS);
351   Result |= RHS;
352   return Result;
353 }
354
355 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
356   BitVector Result(LHS);
357   Result ^= RHS;
358   return Result;
359 }
360  
361 } // End llvm namespace
362 #endif