X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FADT%2FSparseBitVector.h;h=89774c3f562833997812f1b0d747e289a3cdbbc7;hb=ac24e251014de60a16558fc0a1f2340c334d2aa8;hp=3a2fb1f234f789d64e67cc72e4b59466ccbfd806;hpb=6320260e060ce4fc5d199d757b15a0f43be66029;p=oota-llvm.git diff --git a/include/llvm/ADT/SparseBitVector.h b/include/llvm/ADT/SparseBitVector.h index 3a2fb1f234f..89774c3f562 100644 --- a/include/llvm/ADT/SparseBitVector.h +++ b/include/llvm/ADT/SparseBitVector.h @@ -2,8 +2,8 @@ // // The LLVM Compiler Infrastructure // -// This file was developed by Daniel Berlin and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // @@ -15,13 +15,15 @@ #ifndef LLVM_ADT_SPARSEBITVECTOR_H #define LLVM_ADT_SPARSEBITVECTOR_H -#include -#include -#include +#include "llvm/ADT/ilist.h" +#include "llvm/ADT/ilist_node.h" #include "llvm/Support/DataTypes.h" -#include "llvm/ADT/STLExtras.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" -#include "llvm/ADT/ilist" +#include "llvm/Support/raw_ostream.h" +#include +#include + namespace llvm { /// SparseBitVector is an implementation of a bitvector that is sparse by only @@ -39,58 +41,33 @@ namespace llvm { template -struct SparseBitVectorElement { +struct SparseBitVectorElement + : public ilist_node > { public: typedef unsigned long BitWord; enum { - BITWORD_SIZE = sizeof(BitWord) * 8, + BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT, BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE, BITS_PER_ELEMENT = ElementSize }; - SparseBitVectorElement *getNext() const { - return Next; - } - SparseBitVectorElement *getPrev() const { - return Prev; - } - - void setNext(SparseBitVectorElement *RHS) { - Next = RHS; - } - void setPrev(SparseBitVectorElement *RHS) { - Prev = RHS; - } - private: - SparseBitVectorElement *Next; - SparseBitVectorElement *Prev; // Index of Element in terms of where first bit starts. unsigned ElementIndex; BitWord Bits[BITWORDS_PER_ELEMENT]; // Needed for sentinels + friend struct ilist_sentinel_traits; SparseBitVectorElement() { - ElementIndex = ~0UL; + ElementIndex = ~0U; memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT); } - friend struct ilist_traits >; - public: explicit SparseBitVectorElement(unsigned Idx) { ElementIndex = Idx; memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT); } - ~SparseBitVectorElement() { - } - - // Copy ctor. - SparseBitVectorElement(const SparseBitVectorElement &RHS) { - ElementIndex = RHS.ElementIndex; - std::copy(&RHS.Bits[0], &RHS.Bits[BITWORDS_PER_ELEMENT], Bits); - } - // Comparison. bool operator==(const SparseBitVectorElement &RHS) const { if (ElementIndex != RHS.ElementIndex) @@ -128,9 +105,11 @@ public: bool test_and_set (unsigned Idx) { bool old = test(Idx); - if (!old) + if (!old) { set(Idx); - return !old; + return true; + } + return false; } void reset(unsigned Idx) { @@ -149,7 +128,7 @@ public: else if (sizeof(BitWord) == 8) NumBits += CountPopulation_64(Bits[i]); else - assert(0 && "Unsupported!"); + llvm_unreachable("Unsupported!"); return NumBits; } @@ -159,23 +138,21 @@ public: if (Bits[i] != 0) { if (sizeof(BitWord) == 4) return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]); - else if (sizeof(BitWord) == 8) + if (sizeof(BitWord) == 8) return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]); - else - assert(0 && "Unsupported!"); + llvm_unreachable("Unsupported!"); } - assert(0 && "Illegal empty element"); + llvm_unreachable("Illegal empty element"); } - /// find_next - Returns the index of the next set bit following the - /// "Prev" bit. Returns -1 if the next set bit is not found. - int find_next(unsigned Prev) const { - ++Prev; - if (Prev >= BITS_PER_ELEMENT) + /// find_next - Returns the index of the next set bit starting from the + /// "Curr" bit. Returns -1 if the next set bit is not found. + int find_next(unsigned Curr) const { + if (Curr >= BITS_PER_ELEMENT) return -1; - unsigned WordPos = Prev / BITWORD_SIZE; - unsigned BitPos = Prev % BITWORD_SIZE; + unsigned WordPos = Curr / BITWORD_SIZE; + unsigned BitPos = Curr % BITWORD_SIZE; BitWord Copy = Bits[WordPos]; assert (WordPos <= BITWORDS_PER_ELEMENT && "Word Position outside of element"); @@ -186,10 +163,9 @@ public: if (Copy != 0) { if (sizeof(BitWord) == 4) return WordPos * BITWORD_SIZE + CountTrailingZeros_32(Copy); - else if (sizeof(BitWord) == 8) + if (sizeof(BitWord) == 8) return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy); - else - assert(0 && "Unsupported!"); + llvm_unreachable("Unsupported!"); } // Check subsequent words. @@ -197,10 +173,9 @@ public: if (Bits[i] != 0) { if (sizeof(BitWord) == 4) return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]); - else if (sizeof(BitWord) == 8) + if (sizeof(BitWord) == 8) return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]); - else - assert(0 && "Unsupported!"); + llvm_unreachable("Unsupported!"); } return -1; } @@ -212,7 +187,7 @@ public: BitWord old = changed ? 0 : Bits[i]; Bits[i] |= RHS.Bits[i]; - if (old != Bits[i]) + if (!changed && old != Bits[i]) changed = true; } return changed; @@ -242,17 +217,17 @@ public: if (Bits[i] != 0) allzero = false; - if (old != Bits[i]) + if (!changed && old != Bits[i]) changed = true; } - BecameZero = !allzero; + BecameZero = allzero; return changed; } // Intersect this Element with the complement of RHS and return true if this // one changed. BecameZero is set to true if this element became all-zero // bits. bool intersectWithComplement(const SparseBitVectorElement &RHS, - bool &BecameZero) { + bool &BecameZero) { bool changed = false; bool allzero = true; @@ -264,10 +239,10 @@ public: if (Bits[i] != 0) allzero = false; - if (old != Bits[i]) + if (!changed && old != Bits[i]) changed = true; } - BecameZero = !allzero; + BecameZero = allzero; return changed; } // Three argument version of intersectWithComplement that intersects @@ -283,7 +258,7 @@ public: if (Bits[i] != 0) allzero = false; } - BecameZero = !allzero; + BecameZero = allzero; } }; @@ -324,9 +299,8 @@ class SparseBitVector { --ElementIter; } else { while (ElementIter != Elements.end() && - ElementIter->index() <= ElementIndex) + ElementIter->index() < ElementIndex) ++ElementIter; - --ElementIter; } CurrElementIter = ElementIter; return ElementIter; @@ -403,6 +377,8 @@ class SparseBitVector { WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE; Bits = Iter->word(WordNumber); Bits >>= NextSetBitNumber % BITWORD_SIZE; + BitNumber = Iter->index() * ElementSize; + BitNumber += NextSetBitNumber; } } } @@ -429,7 +405,7 @@ class SparseBitVector { bool operator==(const SparseBitVectorIterator &RHS) const { // If they are both at the end, ignore the rest of the fields. - if (AtEnd == RHS.AtEnd) + if (AtEnd && RHS.AtEnd) return true; // Otherwise they are the same if they have the same bit number and // bitmap. @@ -473,6 +449,26 @@ public: CurrElementIter = Elements.begin (); } + // Clear. + void clear() { + Elements.clear(); + } + + // Assignment + SparseBitVector& operator=(const SparseBitVector& RHS) { + Elements.clear(); + + ElementListConstIter ElementIter = RHS.Elements.begin(); + while (ElementIter != RHS.Elements.end()) { + Elements.push_back(SparseBitVectorElement(*ElementIter)); + ++ElementIter; + } + + CurrElementIter = Elements.begin (); + + return *this; + } + // Test, Reset, and Set a bit in the bitmap. bool test(unsigned Idx) { if (Elements.empty()) @@ -524,18 +520,44 @@ public: if (ElementIter == Elements.end() || ElementIter->index() != ElementIndex) { Element = new SparseBitVectorElement(ElementIndex); - // Insert does insert before, and lower bound gives the one before. - ElementIter = Elements.insert(++ElementIter, Element); + // We may have hit the beginning of our SparseBitVector, in which case, + // we may need to insert right after this element, which requires moving + // the current iterator forward one, because insert does insert before. + if (ElementIter != Elements.end() && + ElementIter->index() < ElementIndex) + ElementIter = Elements.insert(++ElementIter, Element); + else + ElementIter = Elements.insert(ElementIter, Element); } } + CurrElementIter = ElementIter; + ElementIter->set(Idx % ElementSize); } bool test_and_set (unsigned Idx) { bool old = test(Idx); - if (!old) + if (!old) { set(Idx); - return !old; + return true; + } + return false; + } + + bool operator!=(const SparseBitVector &RHS) const { + return !(*this == RHS); + } + + bool operator==(const SparseBitVector &RHS) const { + ElementListConstIter Iter1 = Elements.begin(); + ElementListConstIter Iter2 = RHS.Elements.begin(); + + for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end(); + ++Iter1, ++Iter2) { + if (*Iter1 != *Iter2) + return false; + } + return Iter1 == Elements.end() && Iter2 == RHS.Elements.end(); } // Union our bitmap with the RHS and return true if we changed. @@ -544,16 +566,10 @@ public: ElementListIter Iter1 = Elements.begin(); ElementListConstIter Iter2 = RHS.Elements.begin(); - // Check if both bitmaps are empty - if (Elements.empty() && RHS.Elements.empty()) + // If RHS is empty, we are done + if (RHS.Elements.empty()) return false; - // See if the first bitmap element is the same in both. This is only - // possible if they are the same bitmap. - if (Iter1 != Elements.end() && Iter2 != RHS.Elements.end()) - if (*Iter1 == *Iter2) - return false; - while (Iter2 != RHS.Elements.end()) { if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) { Elements.insert(Iter1, @@ -582,16 +598,12 @@ public: if (Elements.empty() && RHS.Elements.empty()) return false; - // See if the first bitmap element is the same in both. This is only - // possible if they are the same bitmap. - if (Iter1 != Elements.end() && Iter2 != RHS.Elements.end()) - if (*Iter1 == *Iter2) - return false; - // Loop through, intersecting as we go, erasing elements when necessary. while (Iter2 != RHS.Elements.end()) { - if (Iter1 == Elements.end()) + if (Iter1 == Elements.end()) { + CurrElementIter = Elements.begin(); return changed; + } if (Iter1->index() > Iter2->index()) { ++Iter2; @@ -600,9 +612,11 @@ public: changed |= Iter1->intersectWith(*Iter2, BecameZero); if (BecameZero) { ElementListIter IterTmp = Iter1; + ++Iter1; Elements.erase(IterTmp); + } else { + ++Iter1; } - ++Iter1; ++Iter2; } else { ElementListIter IterTmp = Iter1; @@ -615,29 +629,23 @@ public: return changed; } - // Intersect our bitmap with the complement of the RHS and return true if ours - // changed. + // Intersect our bitmap with the complement of the RHS and return true + // if ours changed. bool intersectWithComplement(const SparseBitVector &RHS) { bool changed = false; ElementListIter Iter1 = Elements.begin(); ElementListConstIter Iter2 = RHS.Elements.begin(); - // Check if they are both empty - if (Elements.empty() && RHS.Elements.empty()) + // If either our bitmap or RHS is empty, we are done + if (Elements.empty() || RHS.Elements.empty()) return false; - // See if the first bitmap element is the same in both. This is only - // possible if they are the same bitmap. - if (Iter1 != Elements.end() && Iter2 != RHS.Elements.end()) - if (*Iter1 == *Iter2) { - Elements.clear(); - return true; - } - // Loop through, intersecting as we go, erasing elements when necessary. while (Iter2 != RHS.Elements.end()) { - if (Iter1 == Elements.end()) + if (Iter1 == Elements.end()) { + CurrElementIter = Elements.begin(); return changed; + } if (Iter1->index() > Iter2->index()) { ++Iter2; @@ -646,14 +654,14 @@ public: changed |= Iter1->intersectWithComplement(*Iter2, BecameZero); if (BecameZero) { ElementListIter IterTmp = Iter1; + ++Iter1; Elements.erase(IterTmp); + } else { + ++Iter1; } - ++Iter1; ++Iter2; } else { - ElementListIter IterTmp = Iter1; ++Iter1; - Elements.erase(IterTmp); } } CurrElementIter = Elements.begin(); @@ -665,26 +673,21 @@ public: } - // Three argument version of intersectWithComplement. Result of RHS1 & ~RHS2 - // is stored into this bitmap. + // Three argument version of intersectWithComplement. + // Result of RHS1 & ~RHS2 is stored into this bitmap. void intersectWithComplement(const SparseBitVector &RHS1, const SparseBitVector &RHS2) { Elements.clear(); + CurrElementIter = Elements.begin(); ElementListConstIter Iter1 = RHS1.Elements.begin(); ElementListConstIter Iter2 = RHS2.Elements.begin(); - // Check if they are both empty. - if (RHS1.empty() && RHS2.empty()) + // If RHS1 is empty, we are done + // If RHS2 is empty, we still have to copy RHS1 + if (RHS1.Elements.empty()) return; - // See if the first bitmap element is the same in both. This is only - // possible if they are the same bitmap. - if (Iter1 != RHS1.Elements.end() && Iter2 != RHS2.Elements.end()) - if (*Iter1 == *Iter2) { - return; - } - // Loop through, intersecting as we go, erasing elements when necessary. while (Iter2 != RHS2.Elements.end()) { if (Iter1 == RHS1.Elements.end()) @@ -702,10 +705,12 @@ public: } else delete NewElement; - ++Iter1; ++Iter2; } else { + SparseBitVectorElement *NewElement = + new SparseBitVectorElement(*Iter1); + Elements.push_back(NewElement); ++Iter1; } } @@ -718,7 +723,6 @@ public: ++Iter1; } - CurrElementIter = Elements.begin(); return; } @@ -740,13 +744,6 @@ public: if (Elements.empty() && RHS.Elements.empty()) return false; - // See if the first bitmap element is the same in both. This is only - // possible if they are the same bitmap. - if (Iter1 != Elements.end() && Iter2 != RHS.Elements.end()) - if (*Iter1 == *Iter2) { - return true; - } - // Loop through, intersecting stopping when we hit bits in common. while (Iter2 != RHS.Elements.end()) { if (Iter1 == Elements.end()) @@ -766,6 +763,14 @@ public: return false; } + // Return true iff all bits set in this SparseBitVector are + // also set in RHS. + bool contains(const SparseBitVector &RHS) const { + SparseBitVector Result(*this); + Result &= RHS; + return (Result == RHS); + } + // Return the first set bit in the bitmap. Return -1 if no bits are set. int find_first() const { if (Elements.empty()) @@ -793,18 +798,7 @@ public: } iterator end() const { - return iterator(this, ~0); - } - - // Dump our bits to stderr - void dump(llvm::OStream &out) const { - out << "[ "; - for (iterator bi = begin(); - bi != end(); - ++bi) { - out << *bi << " "; - } - out << std::endl; + return iterator(this, true); } }; @@ -832,9 +826,56 @@ inline bool operator &=(SparseBitVector *LHS, template inline bool operator &=(SparseBitVector &LHS, const SparseBitVector *RHS) { - return LHS &= (*RHS); + return LHS &= *RHS; +} + +// Convenience functions for infix union, intersection, difference operators. + +template +inline SparseBitVector +operator|(const SparseBitVector &LHS, + const SparseBitVector &RHS) { + SparseBitVector Result(LHS); + Result |= RHS; + return Result; +} + +template +inline SparseBitVector +operator&(const SparseBitVector &LHS, + const SparseBitVector &RHS) { + SparseBitVector Result(LHS); + Result &= RHS; + return Result; +} + +template +inline SparseBitVector +operator-(const SparseBitVector &LHS, + const SparseBitVector &RHS) { + SparseBitVector Result; + Result.intersectWithComplement(LHS, RHS); + return Result; } + + + +// Dump a SparseBitVector to a stream +template +void dump(const SparseBitVector &LHS, raw_ostream &out) { + out << "["; + + typename SparseBitVector::iterator bi = LHS.begin(), + be = LHS.end(); + if (bi != be) { + out << *bi; + for (++bi; bi != be; ++bi) { + out << " " << *bi; + } + } + out << "]\n"; } +} // end namespace llvm #endif