Various plumbing to allow the optimiser (really, just a subset) to be
run post-instrumentation.



git-svn-id: svn://svn.valgrind.org/vex/trunk@492 8f6e269a-dfd6-0310-a8e1-e2731360e62c
diff --git a/head20041019/coregrind/vg_translate.c b/head20041019/coregrind/vg_translate.c
index 321a6cb..509890e 100644
--- a/head20041019/coregrind/vg_translate.c
+++ b/head20041019/coregrind/vg_translate.c
@@ -1727,6 +1727,7 @@
              VG_(need_to_handle_esp_assignment)()
                 ? vg_SP_update_pass
                 : NULL,
+             True, /* cleanup after instrumentation */
              NULL,
              VG_(clo_trace_codegen)
           );
diff --git a/priv/ir/iropt.h b/priv/ir/iropt.h
index d5a3086..24e5bde 100644
--- a/priv/ir/iropt.h
+++ b/priv/ir/iropt.h
@@ -18,6 +18,10 @@
                     Bool (*preciseMemExnsFn)(Int,Int),
                     Addr64 guest_addr );
 
+/* Do a constant folding/propagation pass. */
+extern
+IRBB* cprop_BB ( IRBB* );
+
 /* Do a dead-code removal pass, which is generally needed to avoid
    crashing the tree-builder. bb is destructively modified. */
 extern
diff --git a/priv/main/vex_main.c b/priv/main/vex_main.c
index e0b0983..2ba4e47 100644
--- a/priv/main/vex_main.c
+++ b/priv/main/vex_main.c
@@ -128,6 +128,7 @@
    /* IN: optionally, two instrumentation functions. */
    IRBB* (*instrument1) ( IRBB*, VexGuestLayout*, IRType hWordTy ),
    IRBB* (*instrument2) ( IRBB*, VexGuestLayout*, IRType hWordTy ),
+   Bool  cleanup_after_instrumentation,
    /* IN: optionally, an access check function for guest code. */
    Bool (*byte_accessible) ( Addr64 ),
    /* IN: debug: trace vex activity at various points */
@@ -281,6 +282,22 @@
    if (instrument1 || instrument2)
       sanityCheckIRBB(irbb, guest_word_type);
 
+   /* Do a post-instrumentation cleanup pass. */
+   if (cleanup_after_instrumentation) {
+      do_deadcode_BB( irbb );
+      irbb = cprop_BB( irbb );
+      do_deadcode_BB( irbb );
+      sanityCheckIRBB(irbb, guest_word_type);
+   }
+
+   if (vex_traceflags & VEX_TRACE_OPT2) {
+      vex_printf("\n------------------------" 
+                   " After post-instr IR optimisation "
+                   "------------------------\n\n");
+      ppIRBB ( irbb );
+      vex_printf("\n");
+   }
+
    /* Turn it into virtual-registerised code. */
    do_deadcode_BB( irbb );
    do_treebuild_BB( irbb );
diff --git a/pub/libvex.h b/pub/libvex.h
index 757ae9f..be34d91 100644
--- a/pub/libvex.h
+++ b/pub/libvex.h
@@ -150,6 +150,7 @@
    /* IN: optionally, two instrumentation functions. */
    IRBB* (*instrument1) ( IRBB*, VexGuestLayout*, IRType hWordTy ),
    IRBB* (*instrument2) ( IRBB*, VexGuestLayout*, IRType hWordTy ),
+   Bool  cleanup_after_instrumentation,
    /* IN: optionally, an access check function for guest code. */
    Bool (*byte_accessible) ( Addr64 ),
    /* IN: debug: trace vex activity at various points */
diff --git a/test_main.c b/test_main.c
index 7ca579d..5c2d300 100644
--- a/test_main.c
+++ b/test_main.c
@@ -48,7 +48,7 @@
 static Bool verbose = True;
 
 /* Forwards */
-static IRBB* ac_instrument ( IRBB*, VexGuestLayout* );
+static IRBB* ac_instrument ( IRBB*, VexGuestLayout*, IRType );
 
 
 int main ( int argc, char** argv )
@@ -123,6 +123,7 @@
                  transbuf, N_TRANSBUF, &trans_used,
                  ac_instrument, //NULL, /* instrument1 */
                  NULL, /* instrument2 */
+		 False, /* cleanup after instrument */
                  NULL, /* access checker */
                  TEST_FLAGS 
               );
@@ -155,7 +156,7 @@
 }
 
 static
-IRBB* ac_instrument (IRBB* bb_in, VexGuestLayout* layout)
+IRBB* ac_instrument (IRBB* bb_in, VexGuestLayout* layout, IRType hWordTy )
 {
 /* Use this rather than eg. -1 because it's a UInt. */
 #define INVALID_DATA_SIZE   999999