added a few benchmarks/testapps from http://shootout.alioth.debian.org
[libfirm] / ir / be / beprofile.c
index ea1a475..829c766 100644 (file)
@@ -44,6 +44,7 @@
 #include "benode_t.h"
 #include "beutil.h"
 #include "ircons.h"
+#include "irhooks.h"
 
 #include "bechordal_t.h"
 
@@ -76,7 +77,6 @@ block_counter(ir_node * bb, void * data)
 {
        unsigned int  *count = data;
        *count = *count + 1;
-
 }
 
 static unsigned int
@@ -90,6 +90,7 @@ count_blocks(ir_graph * irg)
 
 /* keep the execcounts here because they are only read once per compiler run */
 static set * profile = NULL;
+static hook_entry_t hook;
 
 /**
  * Instrument a block with code needed for profiling
@@ -98,27 +99,68 @@ static void
 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
 {
        ir_graph *irg = get_irn_irg(bb);
-       ir_node *start_block = get_irg_start_block(irg);
-       ir_node  *load, *store, *offset, *add, *projm, *proji;
-       ir_node *cnst;
+       ir_node  *start_block = get_irg_start_block(irg);
+       ir_node  *load, *store, *offset, *add, *projm, *proji, *unknown;
+       ir_node  *cnst;
 
        if(bb == start_block || bb == get_irg_end_block(irg))
                return;
 
-       cnst   = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
-       offset = new_r_Add(irg, bb, address, cnst, mode_P);
-       load   = new_r_Load(irg, bb, new_NoMem(), offset, mode_Iu);
-       projm  = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
-       proji  = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
-       cnst   = new_r_Const_long(irg, start_block, mode_Iu, 1);
-       add    = new_r_Add(irg, bb, proji, cnst, mode_Iu);
-       store  = new_r_Store(irg, bb, projm, offset, add);
-       projm  = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
-       keep_alive(projm);
+       unknown = new_r_Unknown(irg, mode_M);
+       cnst    = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
+       offset  = new_r_Add(irg, bb, address, cnst, mode_P);
+       load    = new_r_Load(irg, bb, unknown, offset, mode_Iu);
+       projm   = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
+       proji   = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
+       cnst    = new_r_Const_long(irg, start_block, mode_Iu, 1);
+       add     = new_r_Add(irg, bb, proji, cnst, mode_Iu);
+       store   = new_r_Store(irg, bb, projm, offset, add);
+       projm   = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
+       set_irn_link(bb, projm);
+       set_irn_link(projm, load);
 }
 
+typedef struct fix_env {
+       ir_node *start_block;
+       ir_node *end_block;
+} fix_env;
+
+/**
+ * SSA Construction for instrumentation code memory
+ */
+static void
+fix_ssa(ir_node * bb, void * data)
+{
+       fix_env *env = data;
+       ir_node *mem;
+       int     arity = get_Block_n_cfgpreds(bb);
+
+       /* start and end block are not instrumented, skip! */
+       if (bb == env->start_block || bb == env->end_block)
+               return;
+
+       if (arity == 1) {
+               mem = get_irn_link(get_Block_cfgpred_block(bb, 0));
+       } else {
+               int n;
+               ir_node **ins;
+               ir_graph *irg = current_ir_graph;
+
+               NEW_ARR_A(ir_node*, ins, arity);
+               for (n = arity - 1; n >= 0; --n) {
+                       ins[n] = get_irn_link(get_Block_cfgpred_block(bb, n));
+               }
+               mem = new_r_Phi(irg, bb, arity, ins, mode_M);
+       }
+       set_Load_mem(get_irn_link(get_irn_link(bb)), mem);
+}
+
+
 /**
  * Generates a new irg which calls the initializer
+ *
+ * Pseudocode:
+ *      void __firmprof_initializer(void) { __init_firmprof(ent_filename, bblock_id, bblock_counts, n_blocks); }
  */
 static ir_graph *
 gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_counts, int n_blocks)
@@ -153,7 +195,6 @@ gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_c
        set_entity_ld_ident(init_ent, init_name);
 
        irg = new_ir_graph(ent, 0);
-       set_current_ir_graph(irg);
        empty_frame_type = get_irg_frame_type(irg);
        set_type_size_bytes(empty_frame_type, 0);
 
@@ -172,22 +213,8 @@ gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_c
        ins[2] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
        ins[3] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
 
-       call = new_r_Call( irg,
-                       bb,                                                     //ir_node *     block,
-                       get_irg_initial_mem(irg),       //ir_node *     store,
-                       symconst,                                       //ir_node *     callee,
-                       4,                                                      //int   arity,
-                       ins,                                            //ir_node **    in,
-                       init_type                                       //ir_type *     tp
-                       );
-
-       ret = new_r_Return ( irg,
-                       bb,                                                                             //ir_node *     block,
-                       new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular),   //ir_node *     store,
-                       0,                                                                              //int   arity,
-                       NULL                                                                    //ir_node **    in
-                       );
-
+       call = new_r_Call(irg, bb, get_irg_initial_mem(irg), symconst, 4, ins, init_type);
+       ret = new_r_Return(irg, bb, new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular), 0, NULL);
        mature_immBlock(bb);
 
        add_immBlock_pred(get_irg_end_block(irg), ret);
@@ -209,16 +236,13 @@ block_id_walker(ir_node * bb, void * data)
 }
 
 ir_graph *
-be_profile_instrument(void)
+be_profile_instrument(char * filename)
 {
-       ir_graph      *const_irg = get_const_code_irg();
-       ir_node       *const_block = get_irg_start_block(const_irg);
        int            n, i;
        unsigned int   n_blocks = 0;
        entity        *bblock_id, *bblock_counts, *ent_filename;
        ir_type       *array_type, *integer_type, *string_type, *character_type;
-       tarval       **tarval_array, **tarval_string;
-       char          *filename = "test.c"; //FIXME
+       tarval       **tarval_array, **tarval_string, *tv;
        int            filename_len = strlen(filename)+1;
        ident         *cur_ident;
 
@@ -230,23 +254,22 @@ be_profile_instrument(void)
        set_array_bounds_int(array_type, 0, 0, n_blocks);
 
        character_type = new_type_primitive(new_id_from_str("__char"), mode_Bs);
-       string_type    = new_type_array(new_id_from_str("__function_name"), 1, character_type);
+       string_type    = new_type_array(new_id_from_str("__filename"), 1, character_type);
        set_array_bounds_int(string_type, 0, 0, filename_len);
 
-       cur_ident      = new_id_from_str("__BLOCK_IDS");
+       cur_ident      = new_id_from_str("__FIRMPROF__BLOCK_IDS");
        bblock_id      = new_entity(get_glob_type(), cur_ident, array_type);
        set_entity_ld_ident(bblock_id, cur_ident);
        set_entity_variability(bblock_id, variability_initialized);
 
-       cur_ident      = new_id_from_str("__BLOCK_COUNTS");
+       cur_ident      = new_id_from_str("__FIRMPROF__BLOCK_COUNTS");
        bblock_counts  = new_entity(get_glob_type(), cur_ident, array_type);
        set_entity_ld_ident(bblock_counts, cur_ident);
        set_entity_variability(bblock_counts, variability_initialized);
 
-       cur_ident      = new_id_from_str("__FUNCTION_NAME");
+       cur_ident      = new_id_from_str("__FIRMPROF__FILE_NAME");
        ent_filename   = new_entity(get_glob_type(), cur_ident, string_type);
        set_entity_ld_ident(ent_filename, cur_ident);
-       set_entity_variability(ent_filename, variability_initialized);
 
        for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
                ir_graph *irg = get_irp_irg(n);
@@ -256,8 +279,9 @@ be_profile_instrument(void)
 
        /* initialize count array */
        tarval_array = alloca(sizeof(*tarval_array) * n_blocks);
+       tv = get_tarval_null(mode_Iu);
        for (i = 0; i < n_blocks; ++i) {
-               tarval_array[i] = get_tarval_null(mode_Iu);
+               tarval_array[i] = tv;
        }
        set_array_entity_values(bblock_counts, tarval_array, n_blocks);
 
@@ -266,26 +290,81 @@ be_profile_instrument(void)
        for (i = 0; i < filename_len; ++i) {
                tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
        }
+       set_entity_variability(ent_filename, variability_constant);
        set_array_entity_values(ent_filename, tarval_string, filename_len);
 
-
        /* initialize block id array and instrument blocks */
        wd.array = tarval_array;
        wd.id    = 0;
        for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
-               ir_graph *irg = get_irp_irg(n);
+               ir_graph      *irg = get_irp_irg(n);
+               int            i;
+               ir_node       *endbb = get_irg_end_block(irg);
+               fix_env       env;
+
+               set_current_ir_graph(irg);
 
                /* generate a symbolic constant pointing to the count array */
                sym.entity_p = bblock_counts;
                wd.symconst  = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
 
                irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
+               env.start_block = get_irg_start_block(irg);
+               env.end_block   = get_irg_end_block(irg);
+               set_irn_link(env.start_block, get_irg_no_mem(irg));
+               irg_block_walk_graph(irg, fix_ssa, NULL, &env);
+               for (i = get_Block_n_cfgpreds(endbb) - 1; i >= 0; --i) {
+                       ir_node *node = skip_Proj(get_Block_cfgpred(endbb, i));
+                       ir_node *bb   = get_Block_cfgpred_block(endbb, i);
+                       ir_node *sync;
+                       ir_node *ins[2];
+
+                       switch (get_irn_opcode(node)) {
+                       case iro_Return:
+                               ins[0] = get_irn_link(bb);
+                               ins[1] = get_Return_mem(node);
+                               sync   = new_r_Sync(irg, bb, 2, ins);
+                               set_Return_mem(node, sync);
+                               break;
+                       case iro_Raise:
+                               ins[0] = get_irn_link(bb);
+                               ins[1] = get_Raise_mem(node);
+                               sync   = new_r_Sync(irg, bb, 2, ins);
+                               set_Raise_mem(node, sync);
+                               break;
+                       default:
+                               /* a fragile's op exception. There should be another path to End,
+                                  so ignore it */
+                               assert(is_fragile_op(node) && "unexpected End control flow predecessor");
+                       }
+               }
        }
        set_array_entity_values(bblock_id, tarval_array, n_blocks);
 
        return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
 }
 
+static void
+profile_node_info(void *ctx, FILE *f, const ir_node *irn)
+{
+       if(is_Block(irn)) {
+               fprintf(f, "profiled execution count: %u\n", be_profile_get_block_execcount(irn));
+       }
+}
+
+static void
+register_vcg_hook(void)
+{
+       memset(&hook, 0, sizeof(hook));
+       hook.hook._hook_node_info = profile_node_info;
+       register_hook(hook_node_info, &hook);
+}
+
+static void
+unregister_vcg_hook(void)
+{
+       unregister_hook(hook_node_info, &hook);
+}
 
 /**
  * Reads the corresponding profile info file if it exists and returns a
@@ -300,9 +379,9 @@ be_profile_read(char * filename)
 
        f = fopen(filename, "r");
        if(f == NULL) {
-               perror("opening of profile data failed");
                return;
        }
+       printf("found profile data.\n");
 
        /* check magic */
        ret = fread(buf, 8, 1, f);
@@ -323,6 +402,7 @@ be_profile_read(char * filename)
        } while(1);
 
        fclose(f);
+       register_vcg_hook();
 }
 
 /**
@@ -331,12 +411,14 @@ be_profile_read(char * filename)
 void
 be_profile_free(void)
 {
-       if(profile)
+       if(profile) {
+               unregister_vcg_hook();
                del_set(profile);
+       }
 }
 
 /**
- * Tells whether profile module has aquired data
+ * Tells whether profile module has acquired data
  */
 int
 be_profile_has_data(void)