X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=include%2Fllvm%2FADT%2FSparseBitVector.h;h=d5bde2963fbdff43a9fe10dfc0f7927559f143c4;hp=b4b53805c4345ced7618efe12e61d2e1d1986088;hb=af628cc0b84d3f6acfb2a8dab696b93945e73e88;hpb=d68a07650cdb2e18f18f362ba533459aa10e01b6 diff --git a/include/llvm/ADT/SparseBitVector.h b/include/llvm/ADT/SparseBitVector.h index b4b53805c43..d5bde2963fb 100644 --- a/include/llvm/ADT/SparseBitVector.h +++ b/include/llvm/ADT/SparseBitVector.h @@ -15,12 +15,14 @@ #ifndef LLVM_ADT_SPARSEBITVECTOR_H #define LLVM_ADT_SPARSEBITVECTOR_H -#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.h" +#include "llvm/Support/raw_ostream.h" +#include +#include namespace llvm { @@ -40,11 +42,12 @@ namespace llvm { template struct SparseBitVectorElement - : ilist_node > { + : public ilist_node > { public: typedef unsigned long BitWord; + typedef unsigned size_type; 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 }; @@ -54,7 +57,7 @@ private: unsigned ElementIndex; BitWord Bits[BITWORDS_PER_ELEMENT]; // Needed for sentinels - friend class ilist_sentinel_traits; + friend struct ilist_sentinel_traits; SparseBitVectorElement() { ElementIndex = ~0U; memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT); @@ -118,7 +121,7 @@ public: return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE)); } - unsigned count() const { + size_type count() const { unsigned NumBits = 0; for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) if (sizeof(BitWord) == 4) @@ -126,7 +129,7 @@ public: else if (sizeof(BitWord) == 8) NumBits += CountPopulation_64(Bits[i]); else - assert(0 && "Unsupported!"); + llvm_unreachable("Unsupported!"); return NumBits; } @@ -135,14 +138,12 @@ public: for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) if (Bits[i] != 0) { if (sizeof(BitWord) == 4) - return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]); - else if (sizeof(BitWord) == 8) - return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]); - else - assert(0 && "Unsupported!"); + return i * BITWORD_SIZE + countTrailingZeros(Bits[i]); + if (sizeof(BitWord) == 8) + return i * BITWORD_SIZE + countTrailingZeros(Bits[i]); + llvm_unreachable("Unsupported!"); } - assert(0 && "Illegal empty element"); - return 0; // Not reached + llvm_unreachable("Illegal empty element"); } /// find_next - Returns the index of the next set bit starting from the @@ -158,26 +159,24 @@ public: && "Word Position outside of element"); // Mask off previous bits. - Copy &= ~0L << BitPos; + Copy &= ~0UL << BitPos; if (Copy != 0) { if (sizeof(BitWord) == 4) - return WordPos * BITWORD_SIZE + CountTrailingZeros_32(Copy); - else if (sizeof(BitWord) == 8) - return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy); - else - assert(0 && "Unsupported!"); + return WordPos * BITWORD_SIZE + countTrailingZeros(Copy); + if (sizeof(BitWord) == 8) + return WordPos * BITWORD_SIZE + countTrailingZeros(Copy); + llvm_unreachable("Unsupported!"); } // Check subsequent words. for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i) if (Bits[i] != 0) { if (sizeof(BitWord) == 4) - return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]); - else if (sizeof(BitWord) == 8) - return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]); - else - assert(0 && "Unsupported!"); + return i * BITWORD_SIZE + countTrailingZeros(Bits[i]); + if (sizeof(BitWord) == 8) + return i * BITWORD_SIZE + countTrailingZeros(Bits[i]); + llvm_unreachable("Unsupported!"); } return -1; } @@ -262,15 +261,22 @@ public: } BecameZero = allzero; } +}; - // Get a hash value for this element; - uint64_t getHashValue() const { - uint64_t HashVal = 0; - for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) { - HashVal ^= Bits[i]; - } - return HashVal; - } +template +struct ilist_traits > + : public ilist_default_traits > { + typedef SparseBitVectorElement Element; + + Element *createSentinel() const { return static_cast(&Sentinel); } + static void destroySentinel(Element *) {} + + Element *provideInitialHead() const { return createSentinel(); } + Element *ensureHead(Element *) const { return createSentinel(); } + static void noteHead(Element *, Element *) {} + +private: + mutable ilist_half_node Sentinel; }; template @@ -377,7 +383,7 @@ class SparseBitVector { AtEnd = true; return; } - // Set up for next non zero word in bitmap. + // Set up for next non-zero word in bitmap. BitNumber = Iter->index() * ElementSize; NextSetBitNumber = Iter->find_first(); BitNumber += NextSetBitNumber; @@ -459,11 +465,16 @@ 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)); @@ -471,7 +482,7 @@ public: } CurrElementIter = Elements.begin (); - + return *this; } @@ -635,8 +646,8 @@ 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(); @@ -679,8 +690,8 @@ 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) { @@ -769,6 +780,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()) @@ -798,18 +817,6 @@ public: iterator end() const { return iterator(this, true); } - - // Get a hash value for this bitmap. - uint64_t getHashValue() const { - uint64_t HashVal = 0; - for (ElementListConstIter Iter = Elements.begin(); - Iter != Elements.end(); - ++Iter) { - HashVal ^= Iter->index(); - HashVal ^= Iter->getHashValue(); - } - return HashVal; - } }; // Convenience functions to allow Or and And without dereferencing in the user @@ -836,23 +843,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. -// Dump a SparseBitVector to a stream template -void dump(const SparseBitVector &LHS, llvm::OStream &out) { - out << "[ "; +inline SparseBitVector +operator|(const SparseBitVector &LHS, + const SparseBitVector &RHS) { + SparseBitVector Result(LHS); + Result |= RHS; + return Result; +} - typename SparseBitVector::iterator bi; - for (bi = LHS.begin(); bi != LHS.end(); ++bi) { - out << *bi << " "; - } - out << " ]\n"; +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