Fix a ValueTracking rule: RHS means operand 1, not 0. Add a simple
[oota-llvm.git] / lib / Support / StringMap.cpp
index 95ff2bbe94a2ca0071a0c8d2af3fe4da202edaa5..0c61732a61b307996080b910da932eadb9f4867a 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by Chris Lattner 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.
 //
 //===----------------------------------------------------------------------===//
 //
 using namespace llvm;
 
 StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
+  ItemSize = itemSize;
+  
+  // If a size is specified, initialize the table with that many buckets.
+  if (InitSize) {
+    init(InitSize);
+    return;
+  }
+  
+  // Otherwise, initialize it with zero buckets to avoid the allocation.
+  TheTable = 0;
+  NumBuckets = 0;
+  NumItems = 0;
+  NumTombstones = 0;
+}
+
+void StringMapImpl::init(unsigned InitSize) {
   assert((InitSize & (InitSize-1)) == 0 &&
          "Init Size must be a power of 2 or zero!");
-  NumBuckets = InitSize ? InitSize : 512;
-  ItemSize = itemSize;
+  NumBuckets = InitSize ? InitSize : 16;
   NumItems = 0;
   NumTombstones = 0;
   
-  TheTable = new ItemBucket[NumBuckets+1]();
-  memset(TheTable, 0, NumBuckets*sizeof(ItemBucket));
+  TheTable = (ItemBucket*)calloc(NumBuckets+1, sizeof(ItemBucket));
   
   // Allocate one extra bucket, set it to look filled so the iterators stop at
   // end.
@@ -52,8 +66,12 @@ static unsigned HashString(const char *Start, const char *End) {
 /// case, the FullHashValue field of the bucket will be set to the hash value
 /// of the string.
 unsigned StringMapImpl::LookupBucketFor(const char *NameStart,
-                                         const char *NameEnd) {
+                                        const char *NameEnd) {
   unsigned HTSize = NumBuckets;
+  if (HTSize == 0) {  // Hash table unallocated so far?
+    init(16);
+    HTSize = NumBuckets;
+  }
   unsigned FullHashValue = HashString(NameStart, NameEnd);
   unsigned BucketNo = FullHashValue & (HTSize-1);
   
@@ -110,6 +128,7 @@ unsigned StringMapImpl::LookupBucketFor(const char *NameStart,
 /// This does not modify the map.
 int StringMapImpl::FindKey(const char *KeyStart, const char *KeyEnd) const {
   unsigned HTSize = NumBuckets;
+  if (HTSize == 0) return -1;  // Really empty table?
   unsigned FullHashValue = HashString(KeyStart, KeyEnd);
   unsigned BucketNo = FullHashValue & (HTSize-1);
   
@@ -180,8 +199,7 @@ void StringMapImpl::RehashTable() {
   unsigned NewSize = NumBuckets*2;
   // Allocate one extra bucket which will always be non-empty.  This allows the
   // iterators to stop at end.
-  ItemBucket *NewTableArray = new ItemBucket[NewSize+1]();
-  memset(NewTableArray, 0, NewSize*sizeof(ItemBucket));
+  ItemBucket *NewTableArray =(ItemBucket*)calloc(NewSize+1, sizeof(ItemBucket));
   NewTableArray[NewSize].Item = (StringMapEntryBase*)2;
   
   // Rehash all the items into their new buckets.  Luckily :) we already have
@@ -209,7 +227,7 @@ void StringMapImpl::RehashTable() {
     }
   }
   
-  delete[] TheTable;
+  free(TheTable);
   
   TheTable = NewTableArray;
   NumBuckets = NewSize;