Toggle sense of flag
[oota-llvm.git] / runtime / libtrace / tracelib.c
index 537917e4acba387e9d6aec2b892863719e458d0a..5b4c7f68319b2e1c25ae6d9302bda1ccd999f3b1 100644 (file)
@@ -1,32 +1,24 @@
-/*===-- Libraries/tracelib.c - Runtime routines for tracing -----*- C++ -*--===
+/*===-- tracelib.c - Runtime routines for tracing ---------------*- C++ -*-===*
  *
- * Runtime routines for supporting tracing of execution
- * for code generated by LLVM.
+ * Runtime routines for supporting tracing of execution for code generated by
+ * LLVM.
  *
- *===---------------------------------------------------------------------===*/
+ *===----------------------------------------------------------------------===*/
 
 #include "tracelib.h"
 #include <assert.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#ifndef sun
-#include <stdint.h>
-#endif
+#include "Support/DataTypes.h"
 
 /*===---------------------------------------------------------------------=====
  * HASH FUNCTIONS
  *===---------------------------------------------------------------------===*/
 
 /* use #defines until we have inlining */
-
-#ifndef sun
-typedef uint32_t Pointer;               /* int representation of a pointer */
-#else
-typedef uint64_t Pointer;               /* int representation of a pointer */
-#endif
-typedef uint32_t Index;                 /* type of index/size for hash table */
-typedef uint32_t Generic;               /* type of values stored in table */ 
+typedef uintptr_t Index;                 /* type of keys, size for hash table */
+typedef uint32_t  Generic;               /* type of values stored in table */ 
 
 /* Index IntegerHashFunc(const Generic value, const Index size) */
 #define IntegerHashFunc(value, size) \
@@ -44,7 +36,6 @@ typedef uint32_t Generic;               /* type of values stored in table */
 #define PointerRehashFunc(value, size) \
   IntegerRehashFunc((Index) value, size)
 
-
 /*===---------------------------------------------------------------------=====
  * POINTER-TO-GENERIC HASH TABLE.
  * These should be moved to a separate location: HashTable.[ch]
@@ -55,6 +46,16 @@ typedef char FULLEMPTY;
 const FULLEMPTY EMPTY = '\0';
 const FULLEMPTY FULL  = '\1';
 
+// List of primes closest to powers of 2 in [2^20 -- 2^30], obtained from
+// http://www.utm.edu/research/primes/lists/2small/0bit.html.
+// Use these as the successive sizes of the hash table.
+#define NUMPRIMES 11
+#define FIRSTENTRY 2
+const uint PRIMES[NUMPRIMES] = { (1<<20)-3,  (1<<21)-9,  (1<<22)-3, (1<<23)-15,
+                                 (1<<24)-3,  (1<<25)-39, (1<<26)-5, (1<<27)-39,
+                                 (1<<28)-57, (1<<29)-3,  (1<<30)-35 };
+uint CurrentSizeEntry = FIRSTENTRY;
+
 const uint MAX_NUM_PROBES = 4;
 
 typedef struct PtrValueHashEntry_struct {
@@ -70,13 +71,17 @@ typedef struct PtrValueHashTable_struct {
 } PtrValueHashTable;
 
 
-extern Generic LookupOrInsertPtr(PtrValueHashTable* ptrTable,
-                                 void* ptr, ACTION action);
+static Generic LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr,
+                                 ACTION action, Generic value);
 
-extern void Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value);
+static void Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value);
 
-extern void Delete(PtrValueHashTable* ptrTable, void* ptr);
+static void Delete(PtrValueHashTable* ptrTable, void* ptr);
 
+/* Returns 0 if the item is not found. */
+/* void* LookupPtr(PtrValueHashTable* ptrTable, void* ptr) */
+#define LookupPtr(ptrTable, ptr) \
+  LookupOrInsertPtr(ptrTable, ptr, FIND, (Generic) 0)
 
 void
 InitializeTable(PtrValueHashTable* ptrTable, Index newSize)
@@ -104,11 +109,9 @@ ReallocTable(PtrValueHashTable* ptrTable, Index newSize)
     return;
 
 #ifndef NDEBUG
-  printf("\n***\n*** WARNING: REALLOCATING SPACE FOR POINTER HASH TABLE.\n");
-  printf("*** oldSize = %d, oldCapacity = %d\n***\n\n",
-         ptrTable->size, ptrTable->capacity); 
-  printf("*** NEW SEQUENCE NUMBER FOR A POINTER WILL PROBABLY NOT MATCH ");
-  printf(" THE OLD ONE!\n***\n\n");
+  printf("\n***\n*** REALLOCATING SPACE FOR POINTER HASH TABLE.\n");
+  printf("*** oldSize = %ld, oldCapacity = %ld\n***\n\n",
+         (long) ptrTable->size, (long) ptrTable->capacity); 
 #endif
 
   unsigned int i;
@@ -119,20 +122,18 @@ ReallocTable(PtrValueHashTable* ptrTable, Index newSize)
   
   /* allocate the new storage and flags and re-insert the old entries */
   InitializeTable(ptrTable, newSize);
-  memcpy(ptrTable->table, oldTable,
-         oldCapacity * sizeof(PtrValueHashEntry));
-  memcpy(ptrTable->fullEmptyFlags, oldFlags,
-         oldCapacity * sizeof(FULLEMPTY));
-  ptrTable->size = oldSize;
+  for (i=0; i < oldCapacity; ++i)
+    if (oldFlags[i] == FULL)
+      Insert(ptrTable, oldTable[i].key, oldTable[i].value);
+
+  assert(ptrTable->size == oldSize && "Incorrect number of entries copied?");
 
 #ifndef NDEBUG
-  for (i=0; i < oldCapacity; ++i) {
-    assert(ptrTable->fullEmptyFlags[i] == oldFlags[i]);
-    assert(ptrTable->table[i].key == oldTable[i].key);
-    assert(ptrTable->table[i].value == oldTable[i].value);
-  }
+  for (i=0; i < oldCapacity; ++i)
+    if (oldFlags[i] == FULL)
+      assert(LookupPtr(ptrTable, oldTable[i].key) == oldTable[i].value);
 #endif
-      
+  
   free(oldTable);
   free(oldFlags);
 }
@@ -160,8 +161,8 @@ void
 DeleteAtIndex(PtrValueHashTable* ptrTable, Index index)
 {
   assert(ptrTable->fullEmptyFlags[index] == FULL && "Deleting empty slot!");
-  ptrTable->table[index].key = NULL
-  ptrTable->table[index].value = (Generic) NULL
+  ptrTable->table[index].key = 0
+  ptrTable->table[index].value = (Generic) 0
   ptrTable->fullEmptyFlags[index] = EMPTY;
   ptrTable->size--;
 }
@@ -194,7 +195,11 @@ FindIndex(PtrValueHashTable* ptrTable, void* ptr)
   
   if (numProbes == MAX_NUM_PROBES)
     { /* table is too full: reallocate and search again */
-      ReallocTable(ptrTable, 1 + 2*ptrTable->capacity);
+      if (CurrentSizeEntry >= NUMPRIMES-1) {
+        fprintf(stderr, "Out of PRIME Numbers!!!");
+        abort();
+      }
+      ReallocTable(ptrTable, PRIMES[++CurrentSizeEntry]);
       return FindIndex(ptrTable, ptr);
     }
   else
@@ -205,8 +210,13 @@ FindIndex(PtrValueHashTable* ptrTable, void* ptr)
     }
 }
 
+/* Look up hash table using 'ptr' as the key.  If an entry exists, return
+ * the value mapped to 'ptr'.  If not, and if action==ENTER is specified,
+ * create a new entry with value 'value', but return 0 in any case.
+ */
 Generic
-LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr, ACTION action)
+LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr, ACTION action,
+                  Generic value)
 {
   Index index = FindIndex(ptrTable, ptr);
   if (ptrTable->fullEmptyFlags[index] == FULL &&
@@ -214,18 +224,12 @@ LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr, ACTION action)
     return ptrTable->table[index].value;
   
   /* Lookup failed: item is not in the table */
-  if (action != ENTER)
-    return (Generic) NULL;
-  
-  /* Insert item into the table and return its index */
-  InsertAtIndex(ptrTable, ptr, (Generic) NULL, index);
-  return (Generic) NULL;
-}
+  /* If action is ENTER, insert item into the table.  Return 0 in any case. */ 
+  if (action == ENTER)
+    InsertAtIndex(ptrTable, ptr, value, index);
 
-/* Returns NULL if the item is not found. */
-/* void* LookupPtr(PtrValueHashTable* ptrTable, void* ptr) */
-#define LookupPtr(ptrTable, ptr) \
-  LookupOrInsertPtr(ptrTable, ptr, FIND)
+  return (Generic) 0;
+}
 
 void
 Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value)
@@ -252,7 +256,7 @@ Delete(PtrValueHashTable* ptrTable, void* ptr)
  *===---------------------------------------------------------------------===*/
 
 PtrValueHashTable* SequenceNumberTable = NULL;
-Index INITIAL_SIZE = 1 << 22;
+#define INITIAL_SIZE (PRIMES[FIRSTENTRY])
 
 #define MAX_NUM_SAVED 1024
 
@@ -273,12 +277,13 @@ HashPointerToSeqNum(char* ptr)
     assert(MAX_NUM_PROBES < INITIAL_SIZE+1 && "Initial size too small");
     SequenceNumberTable = CreateTable(INITIAL_SIZE);
   }
-  seqnum = (SequenceNumber) LookupPtr(SequenceNumberTable, ptr);
-  if (seqnum == 0)
-    {
-      Insert(SequenceNumberTable, ptr, ++count);
-      seqnum = count;
-    }
+  seqnum = (SequenceNumber)
+    LookupOrInsertPtr(SequenceNumberTable, ptr, ENTER, count+1);
+
+  if (seqnum == 0)    /* new entry was created with value count+1 */
+    seqnum = ++count;    /* so increment counter */
+
+  assert(seqnum <= count && "Invalid sequence number in table!");
   return seqnum;
 }