llvm.org GIT mirror llvm / 5c4a96d
the old trace values pass has been removed, remove its runtime library. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32999 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
5 changed file(s) with 1 addition(s) and 453 deletion(s). Raw diff Collapse all Expand all
1717 $(Echo) "Warning: with llvm-gcc version 4 and beyond"
1818 else
1919 ifneq ($(wildcard $(LLVMGCC)),)
20 PARALLEL_DIRS := GCCLibraries libdummy libprofile libtrace GC
20 PARALLEL_DIRS := GCCLibraries libdummy libprofile GC
2121 else
2222 PARALLEL_DIRS :=
2323 install all ::
+0
-18
runtime/libtrace/Makefile less more
None ##===- runtime/libtrace/Makefile ---------------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file was developed by the LLVM research group and is distributed under
5 # the University of Illinois Open Source License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL = ../..
10 BYTECODE_LIBRARY = 1
11 LIBRARYNAME = trace
12 BYTECODE_DESTINATION = $(CFERuntimeLibDir)
13
14 include $(LEVEL)/Makefile.common
15
16 CompileCommonOpts := $(filter-out -pedantic,$(CompileCommonOpts))
17 CompileCommonOpts := $(filter-out -Wno-long-long,$(CompileCommonOpts))
+0
-2
runtime/libtrace/README.txt less more
None These routines form the support library for the LLVM -trace and -tracem
1 instrumentation passes.
+0
-392
runtime/libtrace/tracelib.c less more
None /*===-- tracelib.c - Runtime routines for tracing ---------------*- C++ -*-===*
1 *
2 * Runtime routines for supporting tracing of execution for code generated by
3 * LLVM.
4 *
5 *===----------------------------------------------------------------------===*/
6
7 #include "tracelib.h"
8 #include
9 #include
10 #include
11 #include
12 #include "llvm/Support/DataTypes.h"
13
14 /*===---------------------------------------------------------------------=====
15 * HASH FUNCTIONS
16 *===---------------------------------------------------------------------===*/
17
18 /* use #defines until we have inlining */
19 typedef uintptr_t Index; /* type of keys, size for hash table */
20 typedef uint32_t Generic; /* type of values stored in table */
21
22 /* Index IntegerHashFunc(const Generic value, const Index size) */
23 #define IntegerHashFunc(value, size) \
24 ( ((((Index) value) << 3) ^ (((Index) value) >> 3)) % size )
25
26 /* Index IntegerRehashFunc(const Generic oldHashValue, const Index size) */
27 #define IntegerRehashFunc(oldHashValue, size) \
28 ((Index) ((oldHashValue+16) % size)) /* 16 is relatively prime to a Mersenne prime! */
29
30 /* Index PointerHashFunc(const void* value, const Index size) */
31 #define PointerHashFunc(value, size) \
32 IntegerHashFunc((Index) value, size)
33
34 /* Index PointerRehashFunc(const void* value, const Index size) */
35 #define PointerRehashFunc(value, size) \
36 IntegerRehashFunc((Index) value, size)
37
38 /*===---------------------------------------------------------------------=====
39 * POINTER-TO-GENERIC HASH TABLE.
40 * These should be moved to a separate location: HashTable.[ch]
41 *===---------------------------------------------------------------------===*/
42
43 typedef enum { FIND, ENTER } ACTION;
44 typedef char FULLEMPTY;
45 const FULLEMPTY EMPTY = '\0';
46 const FULLEMPTY FULL = '\1';
47
48 // List of primes closest to powers of 2 in [2^20 -- 2^30], obtained from
49 // http://www.utm.edu/research/primes/lists/2small/0bit.html.
50 // Use these as the successive sizes of the hash table.
51 #define NUMPRIMES 11
52 #define FIRSTENTRY 2
53 const unsigned PRIMES[NUMPRIMES] = { (1<<20)-3, (1<<21)-9, (1<<22)-3, (1<<23)-15,
54 (1<<24)-3, (1<<25)-39, (1<<26)-5, (1<<27)-39,
55 (1<<28)-57, (1<<29)-3, (1<<30)-35 };
56 unsigned CurrentSizeEntry = FIRSTENTRY;
57
58 const unsigned MAX_NUM_PROBES = 4;
59
60 typedef struct PtrValueHashEntry_struct {
61 void* key;
62 Generic value;
63 } PtrValueHashEntry;
64
65 typedef struct PtrValueHashTable_struct {
66 PtrValueHashEntry* table;
67 FULLEMPTY* fullEmptyFlags;
68 Index capacity;
69 Index size;
70 } PtrValueHashTable;
71
72
73 static Generic LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr,
74 ACTION action, Generic value);
75
76 static void Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value);
77
78 static void Delete(PtrValueHashTable* ptrTable, void* ptr);
79
80 /* Returns 0 if the item is not found. */
81 /* void* LookupPtr(PtrValueHashTable* ptrTable, void* ptr) */
82 #define LookupPtr(ptrTable, ptr) \
83 LookupOrInsertPtr(ptrTable, ptr, FIND, (Generic) 0)
84
85 void
86 InitializeTable(PtrValueHashTable* ptrTable, Index newSize)
87 {
88 ptrTable->table = (PtrValueHashEntry*) calloc(newSize,
89 sizeof(PtrValueHashEntry));
90 ptrTable->fullEmptyFlags = (FULLEMPTY*) calloc(newSize, sizeof(FULLEMPTY));
91 ptrTable->capacity = newSize;
92 ptrTable->size = 0;
93 }
94
95 PtrValueHashTable*
96 CreateTable(Index initialSize)
97 {
98 PtrValueHashTable* ptrTable =
99 (PtrValueHashTable*) malloc(sizeof(PtrValueHashTable));
100 InitializeTable(ptrTable, initialSize);
101 return ptrTable;
102 }
103
104 void
105 ReallocTable(PtrValueHashTable* ptrTable, Index newSize)
106 {
107 if (newSize <= ptrTable->capacity)
108 return;
109
110 #ifndef NDEBUG
111 printf("\n***\n*** REALLOCATING SPACE FOR POINTER HASH TABLE.\n");
112 printf("*** oldSize = %ld, oldCapacity = %ld\n***\n\n",
113 (long) ptrTable->size, (long) ptrTable->capacity);
114 #endif
115
116 unsigned int i;
117 PtrValueHashEntry* oldTable = ptrTable->table;
118 FULLEMPTY* oldFlags = ptrTable->fullEmptyFlags;
119 Index oldSize = ptrTable->size;
120 Index oldCapacity = ptrTable->capacity;
121
122 /* allocate the new storage and flags and re-insert the old entries */
123 InitializeTable(ptrTable, newSize);
124 for (i=0; i < oldCapacity; ++i)
125 if (oldFlags[i] == FULL)
126 Insert(ptrTable, oldTable[i].key, oldTable[i].value);
127
128 assert(ptrTable->size == oldSize && "Incorrect number of entries copied?");
129
130 #ifndef NDEBUG
131 for (i=0; i < oldCapacity; ++i)
132 if (oldFlags[i] == FULL)
133 assert(LookupPtr(ptrTable, oldTable[i].key) == oldTable[i].value);
134 #endif
135
136 free(oldTable);
137 free(oldFlags);
138 }
139
140 void
141 DeleteTable(PtrValueHashTable* ptrTable)
142 {
143 free(ptrTable->table);
144 free(ptrTable->fullEmptyFlags);
145 memset(ptrTable, '\0', sizeof(PtrValueHashTable));
146 free(ptrTable);
147 }
148
149 void
150 InsertAtIndex(PtrValueHashTable* ptrTable, void* ptr, Generic value, Index index)
151 {
152 assert(ptrTable->fullEmptyFlags[index] == EMPTY && "Slot is in use!");
153 ptrTable->table[index].key = ptr;
154 ptrTable->table[index].value = value;
155 ptrTable->fullEmptyFlags[index] = FULL;
156 ptrTable->size++;
157 }
158
159 void
160 DeleteAtIndex(PtrValueHashTable* ptrTable, Index index)
161 {
162 assert(ptrTable->fullEmptyFlags[index] == FULL && "Deleting empty slot!");
163 ptrTable->table[index].key = 0;
164 ptrTable->table[index].value = (Generic) 0;
165 ptrTable->fullEmptyFlags[index] = EMPTY;
166 ptrTable->size--;
167 }
168
169 Index
170 FindIndex(PtrValueHashTable* ptrTable, void* ptr)
171 {
172 unsigned numProbes = 1;
173 Index index = PointerHashFunc(ptr, ptrTable->capacity);
174 if (ptrTable->fullEmptyFlags[index] == FULL)
175 {
176 if (ptrTable->table[index].key == ptr)
177 return index;
178
179 /* First lookup failed on non-empty slot: probe further */
180 while (numProbes < MAX_NUM_PROBES)
181 {
182 index = PointerRehashFunc(index, ptrTable->capacity);
183 if (ptrTable->fullEmptyFlags[index] == EMPTY)
184 break;
185 else if (ptrTable->table[index].key == ptr)
186 return index;
187 ++numProbes;
188 }
189 }
190
191 /* Lookup failed: item is not in the table. */
192 /* If last slot is empty, use that slot. */
193 /* Otherwise, table must have been reallocated, so search again. */
194
195 if (numProbes == MAX_NUM_PROBES)
196 { /* table is too full: reallocate and search again */
197 if (CurrentSizeEntry >= NUMPRIMES-1) {
198 fprintf(stderr, "Out of PRIME Numbers!!!");
199 abort();
200 }
201 ReallocTable(ptrTable, PRIMES[++CurrentSizeEntry]);
202 return FindIndex(ptrTable, ptr);
203 }
204 else
205 {
206 assert(ptrTable->fullEmptyFlags[index] == EMPTY &&
207 "Stopped before finding an empty slot and before MAX probes!");
208 return index;
209 }
210 }
211
212 /* Look up hash table using 'ptr' as the key. If an entry exists, return
213 * the value mapped to 'ptr'. If not, and if action==ENTER is specified,
214 * create a new entry with value 'value', but return 0 in any case.
215 */
216 Generic
217 LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr, ACTION action,
218 Generic value)
219 {
220 Index index = FindIndex(ptrTable, ptr);
221 if (ptrTable->fullEmptyFlags[index] == FULL &&
222 ptrTable->table[index].key == ptr)
223 return ptrTable->table[index].value;
224
225 /* Lookup failed: item is not in the table */
226 /* If action is ENTER, insert item into the table. Return 0 in any case. */
227 if (action == ENTER)
228 InsertAtIndex(ptrTable, ptr, value, index);
229
230 return (Generic) 0;
231 }
232
233 void
234 Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value)
235 {
236 Index index = FindIndex(ptrTable, ptr);
237 assert(ptrTable->fullEmptyFlags[index] == EMPTY &&
238 "ptr is already in the table: delete it first!");
239 InsertAtIndex(ptrTable, ptr, value, index);
240 }
241
242 void
243 Delete(PtrValueHashTable* ptrTable, void* ptr)
244 {
245 Index index = FindIndex(ptrTable, ptr);
246 if (ptrTable->fullEmptyFlags[index] == FULL &&
247 ptrTable->table[index].key == ptr)
248 {
249 DeleteAtIndex(ptrTable, index);
250 }
251 }
252
253 /*===---------------------------------------------------------------------=====
254 * RUNTIME ROUTINES TO MAP POINTERS TO SEQUENCE NUMBERS
255 *===---------------------------------------------------------------------===*/
256
257 PtrValueHashTable* SequenceNumberTable = NULL;
258 #define INITIAL_SIZE (PRIMES[FIRSTENTRY])
259
260 #define MAX_NUM_SAVED 1024
261
262 typedef struct PointerSet_struct {
263 char* savedPointers[MAX_NUM_SAVED]; /* 1024 alloca'd ptrs shd suffice */
264 unsigned int numSaved;
265 struct PointerSet_struct* nextOnStack; /* implement a cheap stack */
266 } PointerSet;
267
268 PointerSet* topOfStack = NULL;
269
270 SequenceNumber
271 HashPointerToSeqNum(char* ptr)
272 {
273 static SequenceNumber count = 0;
274 SequenceNumber seqnum;
275 if (SequenceNumberTable == NULL) {
276 assert(MAX_NUM_PROBES < INITIAL_SIZE+1 && "Initial size too small");
277 SequenceNumberTable = CreateTable(INITIAL_SIZE);
278 }
279 seqnum = (SequenceNumber)
280 LookupOrInsertPtr(SequenceNumberTable, ptr, ENTER, count+1);
281
282 if (seqnum == 0) /* new entry was created with value count+1 */
283 seqnum = ++count; /* so increment counter */
284
285 assert(seqnum <= count && "Invalid sequence number in table!");
286 return seqnum;
287 }
288
289 void
290 ReleasePointerSeqNum(char* ptr)
291 { /* if a sequence number was assigned to this ptr, release it */
292 if (SequenceNumberTable != NULL)
293 Delete(SequenceNumberTable, ptr);
294 }
295
296 void
297 PushPointerSet()
298 {
299 PointerSet* newSet = (PointerSet*) malloc(sizeof(PointerSet));
300 newSet->numSaved = 0;
301 newSet->nextOnStack = topOfStack;
302 topOfStack = newSet;
303 }
304
305 void
306 PopPointerSet()
307 {
308 PointerSet* oldSet;
309 assert(topOfStack != NULL && "popping from empty stack!");
310 oldSet = topOfStack;
311 topOfStack = oldSet->nextOnStack;
312 assert(oldSet->numSaved == 0);
313 free(oldSet);
314 }
315
316 /* free the pointers! */
317 static void
318 ReleaseRecordedPointers(char* savedPointers[MAX_NUM_SAVED],
319 unsigned int numSaved)
320 {
321 unsigned int i;
322 for (i=0; i < topOfStack->numSaved; ++i)
323 ReleasePointerSeqNum(topOfStack->savedPointers[i]);
324 }
325
326 void
327 ReleasePointersPopSet()
328 {
329 ReleaseRecordedPointers(topOfStack->savedPointers, topOfStack->numSaved);
330 topOfStack->numSaved = 0;
331 PopPointerSet();
332 }
333
334 void
335 RecordPointer(char* ptr)
336 { /* record pointers for release later */
337 if (topOfStack->numSaved == MAX_NUM_SAVED) {
338 printf("***\n*** WARNING: OUT OF ROOM FOR SAVED POINTERS."
339 " ALL POINTERS ARE BEING FREED.\n"
340 "*** THE SEQUENCE NUMBERS OF SAVED POINTERS WILL CHANGE!\n*** \n");
341 ReleaseRecordedPointers(topOfStack->savedPointers, topOfStack->numSaved);
342 topOfStack->numSaved = 0;
343 }
344 topOfStack->savedPointers[topOfStack->numSaved++] = ptr;
345 }
346
347 /*===---------------------------------------------------------------------=====
348 * TEST DRIVER FOR INSTRUMENTATION LIBRARY
349 *===---------------------------------------------------------------------===*/
350
351 #ifndef TEST_INSTRLIB
352 #undef TEST_INSTRLIB /* #define this to turn on by default */
353 #endif
354
355 #ifdef TEST_INSTRLIB
356 int
357 main(int argc, char** argv)
358 {
359 int i, j;
360 int doRelease = 0;
361
362 INITIAL_SIZE = 5; /* start with small table to test realloc's*/
363
364 if (argc > 1 && ! strcmp(argv[1], "-r"))
365 {
366 PushPointerSet();
367 doRelease = 1;
368 }
369
370 for (i=0; i < argc; ++i)
371 for (j=0; argv[i][j]; ++j)
372 {
373 printf("Sequence number for argc[%d][%d] (%c) = Hash(%p) = %d\n",
374 i, j, argv[i][j], argv[i]+j, HashPointerToSeqNum(argv[i]+j));
375
376 if (doRelease)
377 RecordPointer(argv[i]+j);
378 }
379
380 if (doRelease)
381 ReleasePointersPopSet();
382
383 /* print sequence numbers out again to compare with (-r) and w/o release */
384 for (i=argc-1; i >= 0; --i)
385 for (j=0; argv[i][j]; ++j)
386 printf("Sequence number for argc[%d][%d] (%c) = %d\n",
387 i, j, argv[i][j], argv[i]+j, HashPointerToSeqNum(argv[i]+j));
388
389 return 0;
390 }
391 #endif
+0
-40
runtime/libtrace/tracelib.h less more
None /*===-- Libraries/tracelib.h - Runtime routines for tracing -----*- C++ -*--===
1 *
2 * Runtime routines for supporting tracing of execution
3 * for code generated by LLVM.
4 *
5 *===---------------------------------------------------------------------===*/
6
7 #ifndef _TEST_LIBRARIES_LIBINSTR_TRACELIB_
8 #define _TEST_LIBRARIES_LIBINSTR_TRACELIB_
9
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 #include
15
16 /*===---------------------------------------------------------------------=====
17 * Support for tracing pointers
18 *===---------------------------------------------------------------------===*/
19
20 typedef unsigned int SequenceNumber;
21
22 extern SequenceNumber HashPointerToSeqNum( char* ptr);
23
24 extern void ReleasePointerSeqNum(char* ptr);
25
26 extern void RecordPointer(char* ptr);
27
28 extern void PushPointerSet();
29
30 extern void ReleasePointersPopSet();
31
32
33 #ifdef __cplusplus
34 }
35 #endif
36
37 /*===---------------------------------------------------------------------===*/
38
39 #endif