Make a start on the intermediate-representation optimiser.

git-svn-id: svn://svn.valgrind.org/vex/trunk@171
This commit is contained in:
Julian Seward
2004-08-17 13:31:55 +00:00
parent 401af9b34a
commit ddd4dcdcde
2 changed files with 123 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ PRIV_HEADERS = priv/host-x86/hdefs.h \
priv/guest-x86/gdefs.h
LIB_OBJS = priv/ir/irdefs.o \
priv/ir/iropt.o \
priv/main/vex_main.o \
priv/main/vex_globals.o \
priv/main/vex_util.o \
@@ -72,6 +73,10 @@ priv/ir/irdefs.o: $(ALL_HEADERS) priv/ir/irdefs.c
$(CC) $(CCFLAGS) $(ALL_INCLUDES) -o priv/ir/irdefs.o \
-c priv/ir/irdefs.c
priv/ir/iropt.o: $(ALL_HEADERS) priv/ir/iropt.c
$(CC) $(CCFLAGS) $(ALL_INCLUDES) -o priv/ir/iropt.o \
-c priv/ir/iropt.c
priv/main/vex_main.o: $(ALL_HEADERS) priv/main/vex_main.c
$(CC) $(CCFLAGS) $(ALL_INCLUDES) -o priv/main/vex_main.o \
-c priv/main/vex_main.c

118
VEX/priv/ir/iropt.c Normal file
View File

@@ -0,0 +1,118 @@
/*---------------------------------------------------------------*/
/*--- ---*/
/*--- This file (ir/iropt.c) is ---*/
/*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/
/*--- ---*/
/*---------------------------------------------------------------*/
#include "libvex_basictypes.h"
#include "libvex.h"
#include "main/vex_util.h"
/*---------------------------------------------------------------*/
/*--- Finite mappery, of a sort ---*/
/*---------------------------------------------------------------*/
/* General map from 64-bit thing 64-bit thing. Could be done faster
by hashing. */
typedef
struct {
Bool* inuse;
ULong* key;
ULong* val;
Int size;
Int used;
}
Hash64;
static Hash64* newH64 ( void )
{
Hash64* h = LibVEX_Alloc(sizeof(Hash64));
h->size = 4;
h->used = 0;
h->inuse = LibVEX_Alloc(h->size * sizeof(Bool));
h->key = LibVEX_Alloc(h->size * sizeof(ULong));
h->val = LibVEX_Alloc(h->size * sizeof(ULong));
return h;
}
/* Lookup key in the map. */
static Bool lookupH64 ( /*OUT*/ULong* val, Hash64* h, ULong key )
{
Int i;
for (i = 0; i < h->used; i++) {
if (h->inuse[i] && h->key[i] == key) {
*val = h->val[i];
return True;
}
}
return False;
}
/* Delete any binding for key in h. */
static void delFromH64 ( Hash64* h, ULong key )
{
Int i;
for (i = 0; i < h->used; i++) {
if (h->inuse[i] && h->key[i] == key) {
h->inuse[i] = False;
return;
}
}
}
/* Add key->val to the map. Replaces any existing binding for key. */
static void addToH64 ( Hash64* h, ULong key, ULong val )
{
Int i, j;
/* Find and replace existing binding, if any. */
for (i = 0; i < h->used; i++) {
if (h->inuse[i] && h->key[i] == key) {
h->val[i] = val;
return;
}
}
/* Ensure a space is available. */
if (h->used == h->size) {
/* Copy into arrays twice the size. */
Bool* inuse2 = LibVEX_Alloc(2 * h->size * sizeof(Bool));
ULong* key2 = LibVEX_Alloc(2 * h->size * sizeof(ULong));
ULong* val2 = LibVEX_Alloc(2 * h->size * sizeof(ULong));
for (i = j = 0; i < h->size; i++) {
if (!h->inuse[i]) continue;
inuse2[j] = True;
key2[j] = h->key[i];
val2[j] = h->val[i];
j++;
}
h->used = j;
h->size *= 2;
h->inuse = inuse2;
h->key = key2;
h->val = val2;
}
/* Finally, add it. */
vassert(h->used < h->size);
h->inuse[h->used] = True;
h->key[h->used] = key;
h->key[h->used] = val;
h->used++;
}
/*---------------------------------------------------------------*/
/*--- end ir/iropt.c ---*/
/*---------------------------------------------------------------*/