Bugfix for the CommaSeparated option. The original code was adding the whole
[oota-llvm.git] / lib / Support / FoldingSet.cpp
index b2d34834d220b5651916d40e194d83519eb1c678..954dc77dff1e954d7f7e40ee97c3453730b4c9c0 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/FoldingSet.h"
-#include "llvm/ADT/APFloat.h"
-#include "llvm/ADT/APInt.h"
+#include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"
 #include <cassert>
+#include <cstring>
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -42,39 +42,35 @@ void FoldingSetNodeID::AddInteger(signed I) {
 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::AddFloat(float F) {
-  Bits.push_back(FloatToBits(F));
-}
-void FoldingSetNodeID::AddDouble(double D) {
- AddInteger(DoubleToBits(D));
+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::AddAPFloat(const APFloat& apf) {
-  APInt api = apf.convertToAPInt();
-  AddAPInt(api);
+void FoldingSetNodeID::AddInteger(long long I) {
+  AddInteger((unsigned long long)I);
 }
-void FoldingSetNodeID::AddAPInt(const APInt& api) {
-  const uint64_t *p = api.getRawData();
-  for (unsigned i=0; i<api.getNumWords(); i++)
-    AddInteger(*p++);
+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)) {
@@ -82,7 +78,7 @@ void FoldingSetNodeID::AddString(const std::string &String) {
     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) |
@@ -108,7 +104,7 @@ void FoldingSetNodeID::AddString(const std::string &String) {
 /// 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;
@@ -172,19 +168,26 @@ static void **GetBucketFor(const FoldingSetNodeID &ID,
 //===----------------------------------------------------------------------===//
 // 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.
 ///
@@ -193,17 +196,12 @@ void FoldingSetImpl::GrowHashTable() {
   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;
@@ -213,9 +211,9 @@ void FoldingSetImpl::GrowHashTable() {
       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();
     }
   }
   
@@ -234,13 +232,14 @@ FoldingSetImpl::Node
   
   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.
@@ -337,7 +336,8 @@ FoldingSetImpl::Node *FoldingSetImpl::GetOrInsertNode(FoldingSetImpl::Node *N) {
 
 FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) {
   // Skip to the first non-null non-self-cycle bucket.
-  while (*Bucket == 0 || GetNextPtr(*Bucket) == 0)
+  while (*Bucket != reinterpret_cast<void*>(-1) &&
+         (*Bucket == 0 || GetNextPtr(*Bucket) == 0))
     ++Bucket;
   
   NodePtr = static_cast<FoldingSetNode*>(*Bucket);
@@ -356,7 +356,8 @@ void FoldingSetIteratorImpl::advance() {
     // Skip to the next non-null non-self-cycle bucket.
     do {
       ++Bucket;
-    } while (*Bucket == 0 || GetNextPtr(*Bucket) == 0);
+    } while (*Bucket != reinterpret_cast<void*>(-1) &&
+             (*Bucket == 0 || GetNextPtr(*Bucket) == 0));
     
     NodePtr = static_cast<FoldingSetNode*>(*Bucket);
   }