Convert assert(0) to llvm_unreachable
[oota-llvm.git] / include / llvm / ADT / SparseBitVector.h
1 //===- llvm/ADT/SparseBitVector.h - Efficient Sparse BitVector -*- 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 defines the SparseBitVector class.  See the doxygen comment for
11 // SparseBitVector for more details on the algorithm used.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ADT_SPARSEBITVECTOR_H
16 #define LLVM_ADT_SPARSEBITVECTOR_H
17
18 #include "llvm/ADT/ilist.h"
19 #include "llvm/ADT/ilist_node.h"
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <cassert>
25 #include <climits>
26
27 namespace llvm {
28
29 /// SparseBitVector is an implementation of a bitvector that is sparse by only
30 /// storing the elements that have non-zero bits set.  In order to make this
31 /// fast for the most common cases, SparseBitVector is implemented as a linked
32 /// list of SparseBitVectorElements.  We maintain a pointer to the last
33 /// SparseBitVectorElement accessed (in the form of a list iterator), in order
34 /// to make multiple in-order test/set constant time after the first one is
35 /// executed.  Note that using vectors to store SparseBitVectorElement's does
36 /// not work out very well because it causes insertion in the middle to take
37 /// enormous amounts of time with a large amount of bits.  Other structures that
38 /// have better worst cases for insertion in the middle (various balanced trees,
39 /// etc) do not perform as well in practice as a linked list with this iterator
40 /// kept up to date.  They are also significantly more memory intensive.
41
42
43 template <unsigned ElementSize = 128>
44 struct SparseBitVectorElement
45   : public ilist_node<SparseBitVectorElement<ElementSize> > {
46 public:
47   typedef unsigned long BitWord;
48   enum {
49     BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT,
50     BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE,
51     BITS_PER_ELEMENT = ElementSize
52   };
53
54 private:
55   // Index of Element in terms of where first bit starts.
56   unsigned ElementIndex;
57   BitWord Bits[BITWORDS_PER_ELEMENT];
58   // Needed for sentinels
59   friend struct ilist_sentinel_traits<SparseBitVectorElement>;
60   SparseBitVectorElement() {
61     ElementIndex = ~0U;
62     memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
63   }
64
65 public:
66   explicit SparseBitVectorElement(unsigned Idx) {
67     ElementIndex = Idx;
68     memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
69   }
70
71   // Comparison.
72   bool operator==(const SparseBitVectorElement &RHS) const {
73     if (ElementIndex != RHS.ElementIndex)
74       return false;
75     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
76       if (Bits[i] != RHS.Bits[i])
77         return false;
78     return true;
79   }
80
81   bool operator!=(const SparseBitVectorElement &RHS) const {
82     return !(*this == RHS);
83   }
84
85   // Return the bits that make up word Idx in our element.
86   BitWord word(unsigned Idx) const {
87     assert (Idx < BITWORDS_PER_ELEMENT);
88     return Bits[Idx];
89   }
90
91   unsigned index() const {
92     return ElementIndex;
93   }
94
95   bool empty() const {
96     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
97       if (Bits[i])
98         return false;
99     return true;
100   }
101
102   void set(unsigned Idx) {
103     Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
104   }
105
106   bool test_and_set (unsigned Idx) {
107     bool old = test(Idx);
108     if (!old) {
109       set(Idx);
110       return true;
111     }
112     return false;
113   }
114
115   void reset(unsigned Idx) {
116     Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
117   }
118
119   bool test(unsigned Idx) const {
120     return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
121   }
122
123   unsigned count() const {
124     unsigned NumBits = 0;
125     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
126       if (sizeof(BitWord) == 4)
127         NumBits += CountPopulation_32(Bits[i]);
128       else if (sizeof(BitWord) == 8)
129         NumBits += CountPopulation_64(Bits[i]);
130       else
131         llvm_unreachable("Unsupported!");
132     return NumBits;
133   }
134
135   /// find_first - Returns the index of the first set bit.
136   int find_first() const {
137     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
138       if (Bits[i] != 0) {
139         if (sizeof(BitWord) == 4)
140           return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
141         if (sizeof(BitWord) == 8)
142           return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
143         llvm_unreachable("Unsupported!");
144       }
145     llvm_unreachable("Illegal empty element");
146   }
147
148   /// find_next - Returns the index of the next set bit starting from the
149   /// "Curr" bit. Returns -1 if the next set bit is not found.
150   int find_next(unsigned Curr) const {
151     if (Curr >= BITS_PER_ELEMENT)
152       return -1;
153
154     unsigned WordPos = Curr / BITWORD_SIZE;
155     unsigned BitPos = Curr % BITWORD_SIZE;
156     BitWord Copy = Bits[WordPos];
157     assert (WordPos <= BITWORDS_PER_ELEMENT
158             && "Word Position outside of element");
159
160     // Mask off previous bits.
161     Copy &= ~0L << BitPos;
162
163     if (Copy != 0) {
164       if (sizeof(BitWord) == 4)
165         return WordPos * BITWORD_SIZE + CountTrailingZeros_32(Copy);
166       if (sizeof(BitWord) == 8)
167         return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
168       llvm_unreachable("Unsupported!");
169     }
170
171     // Check subsequent words.
172     for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
173       if (Bits[i] != 0) {
174         if (sizeof(BitWord) == 4)
175           return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
176         if (sizeof(BitWord) == 8)
177           return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
178         llvm_unreachable("Unsupported!");
179       }
180     return -1;
181   }
182
183   // Union this element with RHS and return true if this one changed.
184   bool unionWith(const SparseBitVectorElement &RHS) {
185     bool changed = false;
186     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
187       BitWord old = changed ? 0 : Bits[i];
188
189       Bits[i] |= RHS.Bits[i];
190       if (!changed && old != Bits[i])
191         changed = true;
192     }
193     return changed;
194   }
195
196   // Return true if we have any bits in common with RHS
197   bool intersects(const SparseBitVectorElement &RHS) const {
198     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
199       if (RHS.Bits[i] & Bits[i])
200         return true;
201     }
202     return false;
203   }
204
205   // Intersect this Element with RHS and return true if this one changed.
206   // BecameZero is set to true if this element became all-zero bits.
207   bool intersectWith(const SparseBitVectorElement &RHS,
208                      bool &BecameZero) {
209     bool changed = false;
210     bool allzero = true;
211
212     BecameZero = false;
213     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
214       BitWord old = changed ? 0 : Bits[i];
215
216       Bits[i] &= RHS.Bits[i];
217       if (Bits[i] != 0)
218         allzero = false;
219
220       if (!changed && old != Bits[i])
221         changed = true;
222     }
223     BecameZero = allzero;
224     return changed;
225   }
226   // Intersect this Element with the complement of RHS and return true if this
227   // one changed.  BecameZero is set to true if this element became all-zero
228   // bits.
229   bool intersectWithComplement(const SparseBitVectorElement &RHS,
230                                bool &BecameZero) {
231     bool changed = false;
232     bool allzero = true;
233
234     BecameZero = false;
235     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
236       BitWord old = changed ? 0 : Bits[i];
237
238       Bits[i] &= ~RHS.Bits[i];
239       if (Bits[i] != 0)
240         allzero = false;
241
242       if (!changed && old != Bits[i])
243         changed = true;
244     }
245     BecameZero = allzero;
246     return changed;
247   }
248   // Three argument version of intersectWithComplement that intersects
249   // RHS1 & ~RHS2 into this element
250   void intersectWithComplement(const SparseBitVectorElement &RHS1,
251                                const SparseBitVectorElement &RHS2,
252                                bool &BecameZero) {
253     bool allzero = true;
254
255     BecameZero = false;
256     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
257       Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
258       if (Bits[i] != 0)
259         allzero = false;
260     }
261     BecameZero = allzero;
262   }
263
264   // Get a hash value for this element;
265   uint64_t getHashValue() const {
266     uint64_t HashVal = 0;
267     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
268       HashVal ^= Bits[i];
269     }
270     return HashVal;
271   }
272 };
273
274 template <unsigned ElementSize = 128>
275 class SparseBitVector {
276   typedef ilist<SparseBitVectorElement<ElementSize> > ElementList;
277   typedef typename ElementList::iterator ElementListIter;
278   typedef typename ElementList::const_iterator ElementListConstIter;
279   enum {
280     BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
281   };
282
283   // Pointer to our current Element.
284   ElementListIter CurrElementIter;
285   ElementList Elements;
286
287   // This is like std::lower_bound, except we do linear searching from the
288   // current position.
289   ElementListIter FindLowerBound(unsigned ElementIndex) {
290
291     if (Elements.empty()) {
292       CurrElementIter = Elements.begin();
293       return Elements.begin();
294     }
295
296     // Make sure our current iterator is valid.
297     if (CurrElementIter == Elements.end())
298       --CurrElementIter;
299
300     // Search from our current iterator, either backwards or forwards,
301     // depending on what element we are looking for.
302     ElementListIter ElementIter = CurrElementIter;
303     if (CurrElementIter->index() == ElementIndex) {
304       return ElementIter;
305     } else if (CurrElementIter->index() > ElementIndex) {
306       while (ElementIter != Elements.begin()
307              && ElementIter->index() > ElementIndex)
308         --ElementIter;
309     } else {
310       while (ElementIter != Elements.end() &&
311              ElementIter->index() < ElementIndex)
312         ++ElementIter;
313     }
314     CurrElementIter = ElementIter;
315     return ElementIter;
316   }
317
318   // Iterator to walk set bits in the bitmap.  This iterator is a lot uglier
319   // than it would be, in order to be efficient.
320   class SparseBitVectorIterator {
321   private:
322     bool AtEnd;
323
324     const SparseBitVector<ElementSize> *BitVector;
325
326     // Current element inside of bitmap.
327     ElementListConstIter Iter;
328
329     // Current bit number inside of our bitmap.
330     unsigned BitNumber;
331
332     // Current word number inside of our element.
333     unsigned WordNumber;
334
335     // Current bits from the element.
336     typename SparseBitVectorElement<ElementSize>::BitWord Bits;
337
338     // Move our iterator to the first non-zero bit in the bitmap.
339     void AdvanceToFirstNonZero() {
340       if (AtEnd)
341         return;
342       if (BitVector->Elements.empty()) {
343         AtEnd = true;
344         return;
345       }
346       Iter = BitVector->Elements.begin();
347       BitNumber = Iter->index() * ElementSize;
348       unsigned BitPos = Iter->find_first();
349       BitNumber += BitPos;
350       WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
351       Bits = Iter->word(WordNumber);
352       Bits >>= BitPos % BITWORD_SIZE;
353     }
354
355     // Move our iterator to the next non-zero bit.
356     void AdvanceToNextNonZero() {
357       if (AtEnd)
358         return;
359
360       while (Bits && !(Bits & 1)) {
361         Bits >>= 1;
362         BitNumber += 1;
363       }
364
365       // See if we ran out of Bits in this word.
366       if (!Bits) {
367         int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
368         // If we ran out of set bits in this element, move to next element.
369         if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
370           ++Iter;
371           WordNumber = 0;
372
373           // We may run out of elements in the bitmap.
374           if (Iter == BitVector->Elements.end()) {
375             AtEnd = true;
376             return;
377           }
378           // Set up for next non zero word in bitmap.
379           BitNumber = Iter->index() * ElementSize;
380           NextSetBitNumber = Iter->find_first();
381           BitNumber += NextSetBitNumber;
382           WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
383           Bits = Iter->word(WordNumber);
384           Bits >>= NextSetBitNumber % BITWORD_SIZE;
385         } else {
386           WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
387           Bits = Iter->word(WordNumber);
388           Bits >>= NextSetBitNumber % BITWORD_SIZE;
389           BitNumber = Iter->index() * ElementSize;
390           BitNumber += NextSetBitNumber;
391         }
392       }
393     }
394   public:
395     // Preincrement.
396     inline SparseBitVectorIterator& operator++() {
397       ++BitNumber;
398       Bits >>= 1;
399       AdvanceToNextNonZero();
400       return *this;
401     }
402
403     // Postincrement.
404     inline SparseBitVectorIterator operator++(int) {
405       SparseBitVectorIterator tmp = *this;
406       ++*this;
407       return tmp;
408     }
409
410     // Return the current set bit number.
411     unsigned operator*() const {
412       return BitNumber;
413     }
414
415     bool operator==(const SparseBitVectorIterator &RHS) const {
416       // If they are both at the end, ignore the rest of the fields.
417       if (AtEnd && RHS.AtEnd)
418         return true;
419       // Otherwise they are the same if they have the same bit number and
420       // bitmap.
421       return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
422     }
423     bool operator!=(const SparseBitVectorIterator &RHS) const {
424       return !(*this == RHS);
425     }
426     SparseBitVectorIterator(): BitVector(NULL) {
427     }
428
429
430     SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
431                             bool end = false):BitVector(RHS) {
432       Iter = BitVector->Elements.begin();
433       BitNumber = 0;
434       Bits = 0;
435       WordNumber = ~0;
436       AtEnd = end;
437       AdvanceToFirstNonZero();
438     }
439   };
440 public:
441   typedef SparseBitVectorIterator iterator;
442
443   SparseBitVector () {
444     CurrElementIter = Elements.begin ();
445   }
446
447   ~SparseBitVector() {
448   }
449
450   // SparseBitVector copy ctor.
451   SparseBitVector(const SparseBitVector &RHS) {
452     ElementListConstIter ElementIter = RHS.Elements.begin();
453     while (ElementIter != RHS.Elements.end()) {
454       Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
455       ++ElementIter;
456     }
457
458     CurrElementIter = Elements.begin ();
459   }
460
461   // Clear.
462   void clear() {
463     Elements.clear();
464   }
465
466   // Assignment
467   SparseBitVector& operator=(const SparseBitVector& RHS) {
468     Elements.clear();
469
470     ElementListConstIter ElementIter = RHS.Elements.begin();
471     while (ElementIter != RHS.Elements.end()) {
472       Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
473       ++ElementIter;
474     }
475
476     CurrElementIter = Elements.begin ();
477
478     return *this;
479   }
480
481   // Test, Reset, and Set a bit in the bitmap.
482   bool test(unsigned Idx) {
483     if (Elements.empty())
484       return false;
485
486     unsigned ElementIndex = Idx / ElementSize;
487     ElementListIter ElementIter = FindLowerBound(ElementIndex);
488
489     // If we can't find an element that is supposed to contain this bit, there
490     // is nothing more to do.
491     if (ElementIter == Elements.end() ||
492         ElementIter->index() != ElementIndex)
493       return false;
494     return ElementIter->test(Idx % ElementSize);
495   }
496
497   void reset(unsigned Idx) {
498     if (Elements.empty())
499       return;
500
501     unsigned ElementIndex = Idx / ElementSize;
502     ElementListIter ElementIter = FindLowerBound(ElementIndex);
503
504     // If we can't find an element that is supposed to contain this bit, there
505     // is nothing more to do.
506     if (ElementIter == Elements.end() ||
507         ElementIter->index() != ElementIndex)
508       return;
509     ElementIter->reset(Idx % ElementSize);
510
511     // When the element is zeroed out, delete it.
512     if (ElementIter->empty()) {
513       ++CurrElementIter;
514       Elements.erase(ElementIter);
515     }
516   }
517
518   void set(unsigned Idx) {
519     unsigned ElementIndex = Idx / ElementSize;
520     SparseBitVectorElement<ElementSize> *Element;
521     ElementListIter ElementIter;
522     if (Elements.empty()) {
523       Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
524       ElementIter = Elements.insert(Elements.end(), Element);
525
526     } else {
527       ElementIter = FindLowerBound(ElementIndex);
528
529       if (ElementIter == Elements.end() ||
530           ElementIter->index() != ElementIndex) {
531         Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
532         // We may have hit the beginning of our SparseBitVector, in which case,
533         // we may need to insert right after this element, which requires moving
534         // the current iterator forward one, because insert does insert before.
535         if (ElementIter != Elements.end() &&
536             ElementIter->index() < ElementIndex)
537           ElementIter = Elements.insert(++ElementIter, Element);
538         else
539           ElementIter = Elements.insert(ElementIter, Element);
540       }
541     }
542     CurrElementIter = ElementIter;
543
544     ElementIter->set(Idx % ElementSize);
545   }
546
547   bool test_and_set (unsigned Idx) {
548     bool old = test(Idx);
549     if (!old) {
550       set(Idx);
551       return true;
552     }
553     return false;
554   }
555
556   bool operator!=(const SparseBitVector &RHS) const {
557     return !(*this == RHS);
558   }
559
560   bool operator==(const SparseBitVector &RHS) const {
561     ElementListConstIter Iter1 = Elements.begin();
562     ElementListConstIter Iter2 = RHS.Elements.begin();
563
564     for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
565          ++Iter1, ++Iter2) {
566       if (*Iter1 != *Iter2)
567         return false;
568     }
569     return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
570   }
571
572   // Union our bitmap with the RHS and return true if we changed.
573   bool operator|=(const SparseBitVector &RHS) {
574     bool changed = false;
575     ElementListIter Iter1 = Elements.begin();
576     ElementListConstIter Iter2 = RHS.Elements.begin();
577
578     // If RHS is empty, we are done
579     if (RHS.Elements.empty())
580       return false;
581
582     while (Iter2 != RHS.Elements.end()) {
583       if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
584         Elements.insert(Iter1,
585                         new SparseBitVectorElement<ElementSize>(*Iter2));
586         ++Iter2;
587         changed = true;
588       } else if (Iter1->index() == Iter2->index()) {
589         changed |= Iter1->unionWith(*Iter2);
590         ++Iter1;
591         ++Iter2;
592       } else {
593         ++Iter1;
594       }
595     }
596     CurrElementIter = Elements.begin();
597     return changed;
598   }
599
600   // Intersect our bitmap with the RHS and return true if ours changed.
601   bool operator&=(const SparseBitVector &RHS) {
602     bool changed = false;
603     ElementListIter Iter1 = Elements.begin();
604     ElementListConstIter Iter2 = RHS.Elements.begin();
605
606     // Check if both bitmaps are empty.
607     if (Elements.empty() && RHS.Elements.empty())
608       return false;
609
610     // Loop through, intersecting as we go, erasing elements when necessary.
611     while (Iter2 != RHS.Elements.end()) {
612       if (Iter1 == Elements.end()) {
613         CurrElementIter = Elements.begin();
614         return changed;
615       }
616
617       if (Iter1->index() > Iter2->index()) {
618         ++Iter2;
619       } else if (Iter1->index() == Iter2->index()) {
620         bool BecameZero;
621         changed |= Iter1->intersectWith(*Iter2, BecameZero);
622         if (BecameZero) {
623           ElementListIter IterTmp = Iter1;
624           ++Iter1;
625           Elements.erase(IterTmp);
626         } else {
627           ++Iter1;
628         }
629         ++Iter2;
630       } else {
631         ElementListIter IterTmp = Iter1;
632         ++Iter1;
633         Elements.erase(IterTmp);
634       }
635     }
636     Elements.erase(Iter1, Elements.end());
637     CurrElementIter = Elements.begin();
638     return changed;
639   }
640
641   // Intersect our bitmap with the complement of the RHS and return true
642   // if ours changed.
643   bool intersectWithComplement(const SparseBitVector &RHS) {
644     bool changed = false;
645     ElementListIter Iter1 = Elements.begin();
646     ElementListConstIter Iter2 = RHS.Elements.begin();
647
648     // If either our bitmap or RHS is empty, we are done
649     if (Elements.empty() || RHS.Elements.empty())
650       return false;
651
652     // Loop through, intersecting as we go, erasing elements when necessary.
653     while (Iter2 != RHS.Elements.end()) {
654       if (Iter1 == Elements.end()) {
655         CurrElementIter = Elements.begin();
656         return changed;
657       }
658
659       if (Iter1->index() > Iter2->index()) {
660         ++Iter2;
661       } else if (Iter1->index() == Iter2->index()) {
662         bool BecameZero;
663         changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
664         if (BecameZero) {
665           ElementListIter IterTmp = Iter1;
666           ++Iter1;
667           Elements.erase(IterTmp);
668         } else {
669           ++Iter1;
670         }
671         ++Iter2;
672       } else {
673         ++Iter1;
674       }
675     }
676     CurrElementIter = Elements.begin();
677     return changed;
678   }
679
680   bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const {
681     return intersectWithComplement(*RHS);
682   }
683
684
685   //  Three argument version of intersectWithComplement.
686   //  Result of RHS1 & ~RHS2 is stored into this bitmap.
687   void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
688                                const SparseBitVector<ElementSize> &RHS2)
689   {
690     Elements.clear();
691     CurrElementIter = Elements.begin();
692     ElementListConstIter Iter1 = RHS1.Elements.begin();
693     ElementListConstIter Iter2 = RHS2.Elements.begin();
694
695     // If RHS1 is empty, we are done
696     // If RHS2 is empty, we still have to copy RHS1
697     if (RHS1.Elements.empty())
698       return;
699
700     // Loop through, intersecting as we go, erasing elements when necessary.
701     while (Iter2 != RHS2.Elements.end()) {
702       if (Iter1 == RHS1.Elements.end())
703         return;
704
705       if (Iter1->index() > Iter2->index()) {
706         ++Iter2;
707       } else if (Iter1->index() == Iter2->index()) {
708         bool BecameZero = false;
709         SparseBitVectorElement<ElementSize> *NewElement =
710           new SparseBitVectorElement<ElementSize>(Iter1->index());
711         NewElement->intersectWithComplement(*Iter1, *Iter2, BecameZero);
712         if (!BecameZero) {
713           Elements.push_back(NewElement);
714         }
715         else
716           delete NewElement;
717         ++Iter1;
718         ++Iter2;
719       } else {
720         SparseBitVectorElement<ElementSize> *NewElement =
721           new SparseBitVectorElement<ElementSize>(*Iter1);
722         Elements.push_back(NewElement);
723         ++Iter1;
724       }
725     }
726
727     // copy the remaining elements
728     while (Iter1 != RHS1.Elements.end()) {
729         SparseBitVectorElement<ElementSize> *NewElement =
730           new SparseBitVectorElement<ElementSize>(*Iter1);
731         Elements.push_back(NewElement);
732         ++Iter1;
733       }
734
735     return;
736   }
737
738   void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
739                                const SparseBitVector<ElementSize> *RHS2) {
740     intersectWithComplement(*RHS1, *RHS2);
741   }
742
743   bool intersects(const SparseBitVector<ElementSize> *RHS) const {
744     return intersects(*RHS);
745   }
746
747   // Return true if we share any bits in common with RHS
748   bool intersects(const SparseBitVector<ElementSize> &RHS) const {
749     ElementListConstIter Iter1 = Elements.begin();
750     ElementListConstIter Iter2 = RHS.Elements.begin();
751
752     // Check if both bitmaps are empty.
753     if (Elements.empty() && RHS.Elements.empty())
754       return false;
755
756     // Loop through, intersecting stopping when we hit bits in common.
757     while (Iter2 != RHS.Elements.end()) {
758       if (Iter1 == Elements.end())
759         return false;
760
761       if (Iter1->index() > Iter2->index()) {
762         ++Iter2;
763       } else if (Iter1->index() == Iter2->index()) {
764         if (Iter1->intersects(*Iter2))
765           return true;
766         ++Iter1;
767         ++Iter2;
768       } else {
769         ++Iter1;
770       }
771     }
772     return false;
773   }
774
775   // Return true iff all bits set in this SparseBitVector are
776   // also set in RHS.
777   bool contains(const SparseBitVector<ElementSize> &RHS) const {
778     SparseBitVector<ElementSize> Result(*this);
779     Result &= RHS;
780     return (Result == RHS);
781   }
782
783   // Return the first set bit in the bitmap.  Return -1 if no bits are set.
784   int find_first() const {
785     if (Elements.empty())
786       return -1;
787     const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
788     return (First.index() * ElementSize) + First.find_first();
789   }
790
791   // Return true if the SparseBitVector is empty
792   bool empty() const {
793     return Elements.empty();
794   }
795
796   unsigned count() const {
797     unsigned BitCount = 0;
798     for (ElementListConstIter Iter = Elements.begin();
799          Iter != Elements.end();
800          ++Iter)
801       BitCount += Iter->count();
802
803     return BitCount;
804   }
805   iterator begin() const {
806     return iterator(this);
807   }
808
809   iterator end() const {
810     return iterator(this, true);
811   }
812
813   // Get a hash value for this bitmap.
814   uint64_t getHashValue() const {
815     uint64_t HashVal = 0;
816     for (ElementListConstIter Iter = Elements.begin();
817          Iter != Elements.end();
818          ++Iter) {
819       HashVal ^= Iter->index();
820       HashVal ^= Iter->getHashValue();
821     }
822     return HashVal;
823   }
824 };
825
826 // Convenience functions to allow Or and And without dereferencing in the user
827 // code.
828
829 template <unsigned ElementSize>
830 inline bool operator |=(SparseBitVector<ElementSize> &LHS,
831                         const SparseBitVector<ElementSize> *RHS) {
832   return LHS |= *RHS;
833 }
834
835 template <unsigned ElementSize>
836 inline bool operator |=(SparseBitVector<ElementSize> *LHS,
837                         const SparseBitVector<ElementSize> &RHS) {
838   return LHS->operator|=(RHS);
839 }
840
841 template <unsigned ElementSize>
842 inline bool operator &=(SparseBitVector<ElementSize> *LHS,
843                         const SparseBitVector<ElementSize> &RHS) {
844   return LHS->operator&=(RHS);
845 }
846
847 template <unsigned ElementSize>
848 inline bool operator &=(SparseBitVector<ElementSize> &LHS,
849                         const SparseBitVector<ElementSize> *RHS) {
850   return LHS &= *RHS;
851 }
852
853 // Convenience functions for infix union, intersection, difference operators.
854
855 template <unsigned ElementSize>
856 inline SparseBitVector<ElementSize>
857 operator|(const SparseBitVector<ElementSize> &LHS,
858           const SparseBitVector<ElementSize> &RHS) {
859   SparseBitVector<ElementSize> Result(LHS);
860   Result |= RHS;
861   return Result;
862 }
863
864 template <unsigned ElementSize>
865 inline SparseBitVector<ElementSize>
866 operator&(const SparseBitVector<ElementSize> &LHS,
867           const SparseBitVector<ElementSize> &RHS) {
868   SparseBitVector<ElementSize> Result(LHS);
869   Result &= RHS;
870   return Result;
871 }
872
873 template <unsigned ElementSize>
874 inline SparseBitVector<ElementSize>
875 operator-(const SparseBitVector<ElementSize> &LHS,
876           const SparseBitVector<ElementSize> &RHS) {
877   SparseBitVector<ElementSize> Result;
878   Result.intersectWithComplement(LHS, RHS);
879   return Result;
880 }
881
882
883
884
885 // Dump a SparseBitVector to a stream
886 template <unsigned ElementSize>
887 void dump(const SparseBitVector<ElementSize> &LHS, raw_ostream &out) {
888   out << "[";
889
890   typename SparseBitVector<ElementSize>::iterator bi = LHS.begin(),
891     be = LHS.end();
892   if (bi != be) {
893     out << *bi;
894     for (++bi; bi != be; ++bi) {
895       out << " " << *bi;
896     }
897   }
898   out << "]\n";
899 }
900 } // end namespace llvm
901
902 #endif