From ddd4dcdcde7b655fbebd17b1e684b171e72025be Mon Sep 17 00:00:00 2001 From: Julian Seward Date: Tue, 17 Aug 2004 13:31:55 +0000 Subject: [PATCH] Make a start on the intermediate-representation optimiser. git-svn-id: svn://svn.valgrind.org/vex/trunk@171 --- VEX/Makefile | 5 ++ VEX/priv/ir/iropt.c | 118 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 VEX/priv/ir/iropt.c diff --git a/VEX/Makefile b/VEX/Makefile index 179740d53..9b6beb054 100644 --- a/VEX/Makefile +++ b/VEX/Makefile @@ -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 diff --git a/VEX/priv/ir/iropt.c b/VEX/priv/ir/iropt.c new file mode 100644 index 000000000..e1825158e --- /dev/null +++ b/VEX/priv/ir/iropt.c @@ -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 ---*/ +/*---------------------------------------------------------------*/