//===----------------------------------------------------------------------===//
#include "llvm/ADT/FoldingSet.h"
+#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include <cassert>
#include <cstring>
void FoldingSetNodeID::AddInteger(unsigned I) {
Bits.push_back(I);
}
-void FoldingSetNodeID::AddInteger(int64_t I) {
- AddInteger((uint64_t)I);
+void FoldingSetNodeID::AddInteger(long I) {
+ AddInteger((unsigned long)I);
}
-void FoldingSetNodeID::AddInteger(uint64_t I) {
- Bits.push_back(unsigned(I));
-
- // If the integer is small, encode it just as 32-bits.
- if ((uint64_t)(int)I != I)
- Bits.push_back(unsigned(I >> 32));
+void FoldingSetNodeID::AddInteger(unsigned long I) {
+ if (sizeof(long) == sizeof(int))
+ AddInteger(unsigned(I));
+ else if (sizeof(long) == sizeof(long long)) {
+ AddInteger((unsigned long long)I);
+ } else {
+ llvm_unreachable("unexpected sizeof(long)");
+ }
}
-void FoldingSetNodeID::AddFloat(float F) {
- Bits.push_back(FloatToBits(F));
+void FoldingSetNodeID::AddInteger(long long I) {
+ AddInteger((unsigned long long)I);
}
-void FoldingSetNodeID::AddDouble(double D) {
- AddInteger(DoubleToBits(D));
+void FoldingSetNodeID::AddInteger(unsigned long long I) {
+ AddInteger(unsigned(I));
+ if ((uint64_t)(int)I != I)
+ Bits.push_back(unsigned(I >> 32));
}
-void FoldingSetNodeID::AddString(const std::string &String) {
- unsigned Size = String.size();
+
+void FoldingSetNodeID::AddString(StringRef String) {
+ unsigned Size = String.size();
Bits.push_back(Size);
if (!Size) return;
unsigned Units = Size / 4;
unsigned Pos = 0;
- const unsigned *Base = (const unsigned *)String.data();
+ const unsigned *Base = (const unsigned*) String.data();
// If the string is aligned do a bulk transfer.
if (!((intptr_t)Base & 3)) {
Pos = (Units + 1) * 4;
} else {
// Otherwise do it the hard way.
- for ( Pos += 4; Pos <= Size; Pos += 4) {
+ for (Pos += 4; Pos <= Size; Pos += 4) {
unsigned V = ((unsigned char)String[Pos - 4] << 24) |
((unsigned char)String[Pos - 3] << 16) |
((unsigned char)String[Pos - 2] << 8) |
/// lookup the node in the FoldingSetImpl.
unsigned FoldingSetNodeID::ComputeHash() const {
// This is adapted from SuperFastHash by Paul Hsieh.
- unsigned Hash = Bits.size();
+ unsigned Hash = static_cast<unsigned>(Bits.size());
for (const unsigned *BP = &Bits[0], *E = BP+Bits.size(); BP != E; ++BP) {
unsigned Data = *BP;
Hash += Data & 0xFFFF;
//===----------------------------------------------------------------------===//
// FoldingSetImpl Implementation
-FoldingSetImpl::FoldingSetImpl(unsigned Log2InitSize) : NumNodes(0) {
+FoldingSetImpl::FoldingSetImpl(unsigned Log2InitSize) {
assert(5 < Log2InitSize && Log2InitSize < 32 &&
"Initial hash table size out of range");
NumBuckets = 1 << Log2InitSize;
Buckets = new void*[NumBuckets+1];
- memset(Buckets, 0, NumBuckets*sizeof(void*));
-
- // Set the very last bucket to be a non-null "pointer".
- Buckets[NumBuckets] = reinterpret_cast<void*>(-2);
+ clear();
}
FoldingSetImpl::~FoldingSetImpl() {
delete [] Buckets;
}
+void FoldingSetImpl::clear() {
+ // Set all but the last bucket to null pointers.
+ memset(Buckets, 0, NumBuckets*sizeof(void*));
+
+ // Set the very last bucket to be a non-null "pointer".
+ Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
+
+ // Reset the node count to zero.
+ NumNodes = 0;
+}
/// GrowHashTable - Double the size of the hash table and rehash everything.
///
unsigned OldNumBuckets = NumBuckets;
NumBuckets <<= 1;
- // Reset the node count to zero: we're going to reinsert everything.
- NumNodes = 0;
-
// Clear out new buckets.
Buckets = new void*[NumBuckets+1];
- memset(Buckets, 0, NumBuckets*sizeof(void*));
-
- // Set the very last bucket to be a non-null "pointer".
- Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
+ clear();
// Walk the old buckets, rehashing nodes into their new place.
+ FoldingSetNodeID ID;
for (unsigned i = 0; i != OldNumBuckets; ++i) {
void *Probe = OldBuckets[i];
if (!Probe) continue;
NodeInBucket->SetNextInBucket(0);
// Insert the node into the new bucket, after recomputing the hash.
- FoldingSetNodeID ID;
GetNodeProfile(ID, NodeInBucket);
InsertNode(NodeInBucket, GetBucketFor(ID, Buckets, NumBuckets));
+ ID.clear();
}
}
InsertPos = 0;
+ FoldingSetNodeID OtherID;
while (Node *NodeInBucket = GetNextPtr(Probe)) {
- FoldingSetNodeID OtherID;
GetNodeProfile(OtherID, NodeInBucket);
if (OtherID == ID)
return NodeInBucket;
Probe = NodeInBucket->getNextInBucket();
+ OtherID.clear();
}
// Didn't find the node, return null with the bucket as the InsertPos.