blob: 5b4c7f68319b2e1c25ae6d9302bda1ccd999f3b1 [file] [log] [blame]
Vikram S. Adveb3b04142003-07-29 20:01:01 +00001/*===-- tracelib.c - Runtime routines for tracing ---------------*- C++ -*-===*
2 *
3 * Runtime routines for supporting tracing of execution for code generated by
4 * LLVM.
5 *
6 *===----------------------------------------------------------------------===*/
Vikram S. Adve2df1f742002-05-19 15:49:58 +00007
8#include "tracelib.h"
9#include <assert.h>
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
Vikram S. Adveb3b04142003-07-29 20:01:01 +000013#include "Support/DataTypes.h"
Vikram S. Adve2df1f742002-05-19 15:49:58 +000014
15/*===---------------------------------------------------------------------=====
16 * HASH FUNCTIONS
17 *===---------------------------------------------------------------------===*/
18
19/* use #defines until we have inlining */
Vikram S. Adveb3b04142003-07-29 20:01:01 +000020typedef uintptr_t Index; /* type of keys, size for hash table */
21typedef uint32_t Generic; /* type of values stored in table */
Vikram S. Adve2df1f742002-05-19 15:49:58 +000022
23/* Index IntegerHashFunc(const Generic value, const Index size) */
24#define IntegerHashFunc(value, size) \
Vikram S. Advee04a2e02003-07-11 22:02:28 +000025 ( ((((Index) value) << 3) ^ (((Index) value) >> 3)) % size )
Vikram S. Adve2df1f742002-05-19 15:49:58 +000026
27/* Index IntegerRehashFunc(const Generic oldHashValue, const Index size) */
28#define IntegerRehashFunc(oldHashValue, size) \
Vikram S. Advee04a2e02003-07-11 22:02:28 +000029 ((Index) ((oldHashValue+16) % size)) /* 16 is relatively prime to a Mersenne prime! */
Vikram S. Adve2df1f742002-05-19 15:49:58 +000030
31/* Index PointerHashFunc(const void* value, const Index size) */
32#define PointerHashFunc(value, size) \
Vikram S. Advee04a2e02003-07-11 22:02:28 +000033 IntegerHashFunc((Index) value, size)
Vikram S. Adve2df1f742002-05-19 15:49:58 +000034
35/* Index PointerRehashFunc(const void* value, const Index size) */
36#define PointerRehashFunc(value, size) \
Vikram S. Advee04a2e02003-07-11 22:02:28 +000037 IntegerRehashFunc((Index) value, size)
Vikram S. Adve2df1f742002-05-19 15:49:58 +000038
Vikram S. Adve2df1f742002-05-19 15:49:58 +000039/*===---------------------------------------------------------------------=====
40 * POINTER-TO-GENERIC HASH TABLE.
41 * These should be moved to a separate location: HashTable.[ch]
42 *===---------------------------------------------------------------------===*/
43
44typedef enum { FIND, ENTER } ACTION;
45typedef char FULLEMPTY;
46const FULLEMPTY EMPTY = '\0';
47const FULLEMPTY FULL = '\1';
48
Vikram S. Adve6235ea22003-07-30 12:49:25 +000049// List of primes closest to powers of 2 in [2^20 -- 2^30], obtained from
50// http://www.utm.edu/research/primes/lists/2small/0bit.html.
51// Use these as the successive sizes of the hash table.
52#define NUMPRIMES 11
53#define FIRSTENTRY 2
54const uint PRIMES[NUMPRIMES] = { (1<<20)-3, (1<<21)-9, (1<<22)-3, (1<<23)-15,
55 (1<<24)-3, (1<<25)-39, (1<<26)-5, (1<<27)-39,
56 (1<<28)-57, (1<<29)-3, (1<<30)-35 };
57uint CurrentSizeEntry = FIRSTENTRY;
58
Vikram S. Adve2df1f742002-05-19 15:49:58 +000059const uint MAX_NUM_PROBES = 4;
60
61typedef struct PtrValueHashEntry_struct {
62 void* key;
63 Generic value;
64} PtrValueHashEntry;
65
66typedef struct PtrValueHashTable_struct {
67 PtrValueHashEntry* table;
68 FULLEMPTY* fullEmptyFlags;
69 Index capacity;
70 Index size;
71} PtrValueHashTable;
72
73
Vikram S. Adve6235ea22003-07-30 12:49:25 +000074static Generic LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr,
75 ACTION action, Generic value);
Vikram S. Adve2df1f742002-05-19 15:49:58 +000076
Vikram S. Adve6235ea22003-07-30 12:49:25 +000077static void Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value);
Vikram S. Adve2df1f742002-05-19 15:49:58 +000078
Vikram S. Adve6235ea22003-07-30 12:49:25 +000079static void Delete(PtrValueHashTable* ptrTable, void* ptr);
Vikram S. Adve2df1f742002-05-19 15:49:58 +000080
Vikram S. Adve6235ea22003-07-30 12:49:25 +000081/* Returns 0 if the item is not found. */
Vikram S. Adveb3b04142003-07-29 20:01:01 +000082/* void* LookupPtr(PtrValueHashTable* ptrTable, void* ptr) */
83#define LookupPtr(ptrTable, ptr) \
Vikram S. Adve6235ea22003-07-30 12:49:25 +000084 LookupOrInsertPtr(ptrTable, ptr, FIND, (Generic) 0)
Vikram S. Adve2df1f742002-05-19 15:49:58 +000085
86void
87InitializeTable(PtrValueHashTable* ptrTable, Index newSize)
88{
Vikram S. Adve0df8adc2003-07-08 18:42:44 +000089 ptrTable->table = (PtrValueHashEntry*) calloc(newSize,
Vikram S. Adve2df1f742002-05-19 15:49:58 +000090 sizeof(PtrValueHashEntry));
Vikram S. Adve0df8adc2003-07-08 18:42:44 +000091 ptrTable->fullEmptyFlags = (FULLEMPTY*) calloc(newSize, sizeof(FULLEMPTY));
Vikram S. Adve2df1f742002-05-19 15:49:58 +000092 ptrTable->capacity = newSize;
93 ptrTable->size = 0;
94}
95
96PtrValueHashTable*
97CreateTable(Index initialSize)
98{
99 PtrValueHashTable* ptrTable =
100 (PtrValueHashTable*) malloc(sizeof(PtrValueHashTable));
101 InitializeTable(ptrTable, initialSize);
102 return ptrTable;
103}
104
105void
106ReallocTable(PtrValueHashTable* ptrTable, Index newSize)
107{
Vikram S. Adve0df8adc2003-07-08 18:42:44 +0000108 if (newSize <= ptrTable->capacity)
109 return;
110
111#ifndef NDEBUG
Vikram S. Adveb3b04142003-07-29 20:01:01 +0000112 printf("\n***\n*** REALLOCATING SPACE FOR POINTER HASH TABLE.\n");
113 printf("*** oldSize = %ld, oldCapacity = %ld\n***\n\n",
114 (long) ptrTable->size, (long) ptrTable->capacity);
Vikram S. Adve0df8adc2003-07-08 18:42:44 +0000115#endif
116
117 unsigned int i;
118 PtrValueHashEntry* oldTable = ptrTable->table;
119 FULLEMPTY* oldFlags = ptrTable->fullEmptyFlags;
120 Index oldSize = ptrTable->size;
121 Index oldCapacity = ptrTable->capacity;
122
123 /* allocate the new storage and flags and re-insert the old entries */
124 InitializeTable(ptrTable, newSize);
Vikram S. Adveb3b04142003-07-29 20:01:01 +0000125 for (i=0; i < oldCapacity; ++i)
126 if (oldFlags[i] == FULL)
127 Insert(ptrTable, oldTable[i].key, oldTable[i].value);
128
129 assert(ptrTable->size == oldSize && "Incorrect number of entries copied?");
Vikram S. Adve0df8adc2003-07-08 18:42:44 +0000130
131#ifndef NDEBUG
Vikram S. Adveb3b04142003-07-29 20:01:01 +0000132 for (i=0; i < oldCapacity; ++i)
Vikram S. Adve6235ea22003-07-30 12:49:25 +0000133 if (oldFlags[i] == FULL)
Vikram S. Adveb3b04142003-07-29 20:01:01 +0000134 assert(LookupPtr(ptrTable, oldTable[i].key) == oldTable[i].value);
Vikram S. Adve0df8adc2003-07-08 18:42:44 +0000135#endif
Vikram S. Adveb3b04142003-07-29 20:01:01 +0000136
Vikram S. Adve0df8adc2003-07-08 18:42:44 +0000137 free(oldTable);
138 free(oldFlags);
Vikram S. Adve2df1f742002-05-19 15:49:58 +0000139}
140
141void
142DeleteTable(PtrValueHashTable* ptrTable)
143{
144 free(ptrTable->table);
145 free(ptrTable->fullEmptyFlags);
146 memset(ptrTable, '\0', sizeof(PtrValueHashTable));
147 free(ptrTable);
148}
149
150void
151InsertAtIndex(PtrValueHashTable* ptrTable, void* ptr, Generic value, Index index)
152{
153 assert(ptrTable->fullEmptyFlags[index] == EMPTY && "Slot is in use!");
154 ptrTable->table[index].key = ptr;
155 ptrTable->table[index].value = value;
156 ptrTable->fullEmptyFlags[index] = FULL;
157 ptrTable->size++;
158}
159
160void
161DeleteAtIndex(PtrValueHashTable* ptrTable, Index index)
162{
163 assert(ptrTable->fullEmptyFlags[index] == FULL && "Deleting empty slot!");
Vikram S. Adve6235ea22003-07-30 12:49:25 +0000164 ptrTable->table[index].key = 0;
165 ptrTable->table[index].value = (Generic) 0;
Vikram S. Adve2df1f742002-05-19 15:49:58 +0000166 ptrTable->fullEmptyFlags[index] = EMPTY;
167 ptrTable->size--;
168}
169
170Index
171FindIndex(PtrValueHashTable* ptrTable, void* ptr)
172{
173 uint numProbes = 1;
174 Index index = PointerHashFunc(ptr, ptrTable->capacity);
175 if (ptrTable->fullEmptyFlags[index] == FULL)
176 {
177 if (ptrTable->table[index].key == ptr)
178 return index;
179
180 /* First lookup failed on non-empty slot: probe further */
181 while (numProbes < MAX_NUM_PROBES)
182 {
183 index = PointerRehashFunc(index, ptrTable->capacity);
184 if (ptrTable->fullEmptyFlags[index] == EMPTY)
185 break;
186 else if (ptrTable->table[index].key == ptr)
187 return index;
188 ++numProbes;
189 }
190 }
191
192 /* Lookup failed: item is not in the table. */
193 /* If last slot is empty, use that slot. */
194 /* Otherwise, table must have been reallocated, so search again. */
195
196 if (numProbes == MAX_NUM_PROBES)
197 { /* table is too full: reallocate and search again */
Vikram S. Adve6235ea22003-07-30 12:49:25 +0000198 if (CurrentSizeEntry >= NUMPRIMES-1) {
199 fprintf(stderr, "Out of PRIME Numbers!!!");
200 abort();
201 }
202 ReallocTable(ptrTable, PRIMES[++CurrentSizeEntry]);
Vikram S. Adve2df1f742002-05-19 15:49:58 +0000203 return FindIndex(ptrTable, ptr);
204 }
205 else
206 {
207 assert(ptrTable->fullEmptyFlags[index] == EMPTY &&
208 "Stopped before finding an empty slot and before MAX probes!");
209 return index;
210 }
211}
212
Vikram S. Adve6235ea22003-07-30 12:49:25 +0000213/* Look up hash table using 'ptr' as the key. If an entry exists, return
214 * the value mapped to 'ptr'. If not, and if action==ENTER is specified,
215 * create a new entry with value 'value', but return 0 in any case.
216 */
Vikram S. Adve2df1f742002-05-19 15:49:58 +0000217Generic
Vikram S. Adve6235ea22003-07-30 12:49:25 +0000218LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr, ACTION action,
219 Generic value)
Vikram S. Adve2df1f742002-05-19 15:49:58 +0000220{
221 Index index = FindIndex(ptrTable, ptr);
222 if (ptrTable->fullEmptyFlags[index] == FULL &&
223 ptrTable->table[index].key == ptr)
224 return ptrTable->table[index].value;
225
226 /* Lookup failed: item is not in the table */
Vikram S. Adve6235ea22003-07-30 12:49:25 +0000227 /* If action is ENTER, insert item into the table. Return 0 in any case. */
228 if (action == ENTER)
229 InsertAtIndex(ptrTable, ptr, value, index);
230
231 return (Generic) 0;
Vikram S. Adve2df1f742002-05-19 15:49:58 +0000232}
233
Vikram S. Adve2df1f742002-05-19 15:49:58 +0000234void
235Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value)
236{
237 Index index = FindIndex(ptrTable, ptr);
238 assert(ptrTable->fullEmptyFlags[index] == EMPTY &&
239 "ptr is already in the table: delete it first!");
240 InsertAtIndex(ptrTable, ptr, value, index);
241}
242
243void
244Delete(PtrValueHashTable* ptrTable, void* ptr)
245{
246 Index index = FindIndex(ptrTable, ptr);
247 if (ptrTable->fullEmptyFlags[index] == FULL &&
248 ptrTable->table[index].key == ptr)
249 {
250 DeleteAtIndex(ptrTable, index);
251 }
252}
253
254/*===---------------------------------------------------------------------=====
255 * RUNTIME ROUTINES TO MAP POINTERS TO SEQUENCE NUMBERS
256 *===---------------------------------------------------------------------===*/
257
258PtrValueHashTable* SequenceNumberTable = NULL;
Vikram S. Adve6235ea22003-07-30 12:49:25 +0000259#define INITIAL_SIZE (PRIMES[FIRSTENTRY])
Vikram S. Adve2df1f742002-05-19 15:49:58 +0000260
261#define MAX_NUM_SAVED 1024
262
263typedef struct PointerSet_struct {
264 char* savedPointers[MAX_NUM_SAVED]; /* 1024 alloca'd ptrs shd suffice */
265 unsigned int numSaved;
266 struct PointerSet_struct* nextOnStack; /* implement a cheap stack */
267} PointerSet;
268
269PointerSet* topOfStack = NULL;
270
271SequenceNumber
272HashPointerToSeqNum(char* ptr)
273{
274 static SequenceNumber count = 0;
275 SequenceNumber seqnum;
276 if (SequenceNumberTable == NULL) {
277 assert(MAX_NUM_PROBES < INITIAL_SIZE+1 && "Initial size too small");
278 SequenceNumberTable = CreateTable(INITIAL_SIZE);
279 }
Vikram S. Adve6235ea22003-07-30 12:49:25 +0000280 seqnum = (SequenceNumber)
281 LookupOrInsertPtr(SequenceNumberTable, ptr, ENTER, count+1);
282
283 if (seqnum == 0) /* new entry was created with value count+1 */
284 seqnum = ++count; /* so increment counter */
285
286 assert(seqnum <= count && "Invalid sequence number in table!");
Vikram S. Adve2df1f742002-05-19 15:49:58 +0000287 return seqnum;
288}
289
290void
291ReleasePointerSeqNum(char* ptr)
292{ /* if a sequence number was assigned to this ptr, release it */
Vikram S. Adve2df1f742002-05-19 15:49:58 +0000293 if (SequenceNumberTable != NULL)
294 Delete(SequenceNumberTable, ptr);
295}
296
297void
298PushPointerSet()
299{
300 PointerSet* newSet = (PointerSet*) malloc(sizeof(PointerSet));
301 newSet->numSaved = 0;
302 newSet->nextOnStack = topOfStack;
303 topOfStack = newSet;
304}
305
306void
307PopPointerSet()
308{
309 PointerSet* oldSet;
310 assert(topOfStack != NULL && "popping from empty stack!");
311 oldSet = topOfStack;
312 topOfStack = oldSet->nextOnStack;
313 assert(oldSet->numSaved == 0);
314 free(oldSet);
315}
316
317/* free the pointers! */
318static void
319ReleaseRecordedPointers(char* savedPointers[MAX_NUM_SAVED],
320 unsigned int numSaved)
321{
322 unsigned int i;
323 for (i=0; i < topOfStack->numSaved; ++i)
324 ReleasePointerSeqNum(topOfStack->savedPointers[i]);
325}
326
327void
328ReleasePointersPopSet()
329{
330 ReleaseRecordedPointers(topOfStack->savedPointers, topOfStack->numSaved);
331 topOfStack->numSaved = 0;
332 PopPointerSet();
333}
334
335void
336RecordPointer(char* ptr)
337{ /* record pointers for release later */
338 if (topOfStack->numSaved == MAX_NUM_SAVED) {
339 printf("***\n*** WARNING: OUT OF ROOM FOR SAVED POINTERS."
340 " ALL POINTERS ARE BEING FREED.\n"
341 "*** THE SEQUENCE NUMBERS OF SAVED POINTERS WILL CHANGE!\n*** \n");
342 ReleaseRecordedPointers(topOfStack->savedPointers, topOfStack->numSaved);
343 topOfStack->numSaved = 0;
344 }
345 topOfStack->savedPointers[topOfStack->numSaved++] = ptr;
346}
347
348/*===---------------------------------------------------------------------=====
349 * TEST DRIVER FOR INSTRUMENTATION LIBRARY
350 *===---------------------------------------------------------------------===*/
351
352#ifndef TEST_INSTRLIB
353#undef TEST_INSTRLIB /* #define this to turn on by default */
354#endif
355
356#ifdef TEST_INSTRLIB
357int
358main(int argc, char** argv)
359{
360 int i, j;
361 int doRelease = 0;
362
363 INITIAL_SIZE = 5; /* start with small table to test realloc's*/
364
365 if (argc > 1 && ! strcmp(argv[1], "-r"))
366 {
367 PushPointerSet();
368 doRelease = 1;
369 }
370
371 for (i=0; i < argc; ++i)
372 for (j=0; argv[i][j]; ++j)
373 {
374 printf("Sequence number for argc[%d][%d] (%c) = Hash(%p) = %d\n",
375 i, j, argv[i][j], argv[i]+j, HashPointerToSeqNum(argv[i]+j));
376
377 if (doRelease)
378 RecordPointer(argv[i]+j);
379 }
380
381 if (doRelease)
382 ReleasePointersPopSet();
383
384 /* print sequence numbers out again to compare with (-r) and w/o release */
385 for (i=argc-1; i >= 0; --i)
386 for (j=0; argv[i][j]; ++j)
Vikram S. Advee04a2e02003-07-11 22:02:28 +0000387 printf("Sequence number for argc[%d][%d] (%c) = %d\n",
Vikram S. Adve2df1f742002-05-19 15:49:58 +0000388 i, j, argv[i][j], argv[i]+j, HashPointerToSeqNum(argv[i]+j));
389
390 return 0;
391}
392#endif