- Fix a few more problems when creating and emitting constants
[libfirm] / ir / be / beprofile.c
index 03bb669..39eec3e 100644 (file)
@@ -44,6 +44,8 @@
 #include "benode_t.h"
 #include "beutil.h"
 #include "ircons.h"
+#include "irhooks.h"
+#include "iredges.h"
 
 #include "bechordal_t.h"
 
 #include <libcore/lc_opts_enum.h>
 #endif /* WITH_LIBCORE */
 
-typedef struct _block_id_walker_data {
+#include "beprofile.h"
+
+typedef struct _block_id_walker_data_t {
        tarval        **array;
        unsigned int    id;
        ir_node *symconst;
-} block_id_walker_data;
+} block_id_walker_data_t;
+
+typedef struct _execcount_t {
+       unsigned int block;
+       unsigned int count;
+} execcount_t;
+
+static int
+cmp_execcount(const void * a, const void * b, size_t size)
+{
+       return ((execcount_t*)a)->block != ((execcount_t*)b)->block;
+}
 
 static void
 block_counter(ir_node * bb, void * data)
@@ -74,6 +89,10 @@ count_blocks(ir_graph * irg)
        return count;
 }
 
+/* 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
  */
@@ -81,56 +100,108 @@ 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;
+
+       /**
+        * We can't instrument the start and end block as there are no real
+        * instructions in these blocks
+        */
        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 * bblock_id, entity * bblock_counts, int n_blocks)
+gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_counts, int n_blocks)
 {
        ir_node *start_block;
 
-       ir_node   *ins[3];
+       ir_node   *ins[4];
        ident     *name = new_id_from_str("__firmprof_initializer");
-       entity    *ent = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
+       entity    *ent  = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
        ir_node   *ret, *call, *symconst;
        symconst_symbol sym;
 
        ident     *init_name = new_id_from_str("__init_firmprof");
-       ir_type   *init_type = new_type_method(init_name, 3, 0);
-       ir_type   *uint, *uintptr;
+       ir_type   *init_type = new_type_method(init_name, 4, 0);
+       ir_type   *uint, *uintptr, *string;
        entity    *init_ent;
        ir_graph  *irg;
        ir_node   *bb;
+       ir_type   *empty_frame_type;
+
+       set_entity_ld_ident(ent, name);
 
        uint    = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
        uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
+       string  = new_type_pointer(new_id_from_str("__charptr"), new_type_primitive(new_id_from_str("__char"), mode_Bs), mode_P);
 
-       set_method_param_type(init_type, 0, uintptr);
+       set_method_param_type(init_type, 0, string);
        set_method_param_type(init_type, 1, uintptr);
-       set_method_param_type(init_type, 2, uint);
+       set_method_param_type(init_type, 2, uintptr);
+       set_method_param_type(init_type, 3, uint);
        init_ent = new_entity(get_glob_type(), init_name, init_type);
+       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);
 
        bb = get_cur_block();
 
@@ -139,28 +210,16 @@ gen_initializer_irg(entity * bblock_id, entity * bblock_counts, int n_blocks)
        sym.entity_p = init_ent;
        symconst     = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
 
-       sym.entity_p = bblock_id;
+       sym.entity_p = ent_filename;
        ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
-       sym.entity_p = bblock_counts;
+       sym.entity_p = bblock_id;
        ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
-       ins[2] = 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,
-                       3,                                                      //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
-                       );
+       sym.entity_p = bblock_counts;
+       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, 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);
@@ -174,7 +233,7 @@ gen_initializer_irg(entity * bblock_id, entity * bblock_counts, int n_blocks)
 static void
 block_id_walker(ir_node * bb, void * data)
 {
-       block_id_walker_data *wd = data;
+       block_id_walker_data_t *wd = data;
 
        wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
        instrument_block(bb, wd->symconst, wd->id);
@@ -182,66 +241,277 @@ block_id_walker(ir_node * bb, void * data)
 }
 
 ir_graph *
-be_profile_instrument(void)
+be_profile_instrument(const 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, *bblock_count;
-       ir_type       *array_type, *integer_type;
-       tarval       **tarval_array;
+       entity        *bblock_id, *bblock_counts, *ent_filename;
+       ir_type       *array_type, *integer_type, *string_type, *character_type;
+       tarval       **tarval_array, **tarval_string, *tv;
+       int            filename_len = strlen(filename)+1;
+       ident         *cur_ident;
 
-       block_id_walker_data  wd;
+       block_id_walker_data_t  wd;
        symconst_symbol sym;
 
-       integer_type = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
-       array_type = new_type_array(new_id_from_str("__block_info_array"), 1, integer_type);
+       integer_type   = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
+       array_type     = new_type_array(new_id_from_str("__block_info_array"), 1, integer_type);
        set_array_bounds_int(array_type, 0, 0, n_blocks);
-       bblock_id = new_entity(get_glob_type(), new_id_from_str("__BLOCK_IDS"), array_type);
+
+       character_type = new_type_primitive(new_id_from_str("__char"), mode_Bs);
+       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("__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);
-       bblock_counts = new_entity(get_glob_type(), new_id_from_str("__BLOCK_COUNTS"), array_type);
+
+       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);
-       bblock_count = new_entity(get_glob_type(), new_id_from_str("__N_BLOCKS"), integer_type);
-       set_entity_variability(bblock_count, variability_initialized);
 
-       for (n = get_irp_n_irgs()-1; n>=0; --n) {
-               ir_graph      *irg = get_irp_irg(n);
+       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);
+
+       for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
+               ir_graph *irg = get_irp_irg(n);
 
                n_blocks += count_blocks(irg);
        }
 
        /* initialize count array */
-       tarval_array = alloca(sizeof(tarval_array[0]) * n_blocks);
-       for(i = 0; i < n_blocks; ++i) {
-               tarval_array[i] = get_tarval_null(mode_Iu);
+       tarval_array = alloca(sizeof(*tarval_array) * n_blocks);
+       tv = get_tarval_null(mode_Iu);
+       for (i = 0; i < n_blocks; ++i) {
+               tarval_array[i] = tv;
        }
        set_array_entity_values(bblock_counts, tarval_array, n_blocks);
 
-       /* initialize the block count entity */
-       set_atomic_ent_value(bblock_count, new_r_Const_long(const_irg, const_block, mode_Iu, n_blocks));
-
-       /* generate a symbolic constant pointing to the count array */
-       sym.entity_p = bblock_count;
+       /* initialize function name string constant */
+       tarval_string = alloca(sizeof(*tarval_string) * (filename_len));
+       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) {
+       wd.id    = 0;
+       for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
                ir_graph      *irg = get_irp_irg(n);
+               int            i;
+               ir_node       *endbb = get_irg_end_block(irg);
+               fix_env       env;
 
-               wd.symconst = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
+               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(bblock_id, bblock_counts, 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
+ * profile info struct
+ */
 void
-be_profile_read(void)
+be_profile_read(const char *filename)
 {
+       FILE   *f;
+       char    buf[8];
+       size_t  ret;
+
+       f = fopen(filename, "r");
+       if(f == NULL) {
+               return;
+       }
+       printf("found profile data '%s'.\n", filename);
+
+       /* check magic */
+       ret = fread(buf, 8, 1, f);
+       if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
+               return;
+       }
+
+       if(profile) be_profile_free();
+       profile = new_set(cmp_execcount, 16);
+
+       do {
+               execcount_t  query;
+               ret = fread(&query, sizeof(unsigned int), 2, f);
+
+               if(ret != 2) break;
+
+               set_insert(profile, &query, sizeof(query), query.block);
+       } while(1);
+
+       fclose(f);
+       register_vcg_hook();
+}
+
+/**
+ * Frees the profile info
+ */
+void
+be_profile_free(void)
+{
+       if(profile) {
+               unregister_vcg_hook();
+               del_set(profile);
+       }
+}
+
+/**
+ * Tells whether profile module has acquired data
+ */
+int
+be_profile_has_data(void)
+{
+       return (profile != NULL);
+}
+
+/**
+ * Get block execution count as determined be profiling
+ */
+unsigned int
+be_profile_get_block_execcount(const ir_node *block)
+{
+       execcount_t *ec, query;
+
+       if(!profile)
+               return 1;
+
+       query.block = get_irn_node_nr(block);
+       ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));
+
+       if(ec != NULL) {
+               return ec->count;
+       } else {
+               ir_fprintf(stderr, "Warning: Profile contains no data for %+F\n",
+                          block);
+               return 1;
+       }
+}
+
+typedef struct _intialize_execfreq_env_t {
+       ir_graph *irg;
+       exec_freq_t *execfreqs;
+       double freq_factor;
+} initialize_execfreq_env_t;
+
+// minimal execution frequency (an execfreq of 0 confuses algos)
+static const double MIN_EXECFREQ = 0.00001;
+
+static void initialize_execfreq(ir_node *block, void *data) {
+       initialize_execfreq_env_t *env = data;
+       double freq;
+
+       if(block == get_irg_start_block(env->irg)
+          || block == get_irg_end_block(env->irg)) {
+               freq = 1.0;
+       } else {
+               freq = be_profile_get_block_execcount(block);
+               freq *= env->freq_factor;
+               if(freq < MIN_EXECFREQ)
+                       freq = MIN_EXECFREQ;
+       }
+
+       set_execfreq(env->execfreqs, block, freq);
+}
+
+exec_freq_t *be_create_execfreqs_from_profile(ir_graph *irg)
+{
+       ir_node *block2 = NULL;
+       ir_node *start_block;
+       const ir_edge_t *edge;
+       initialize_execfreq_env_t env;
+       unsigned count;
+
+       env.irg = irg;
+       env.execfreqs = create_execfreq(irg);
+
+       // find the successor to the start block
+       start_block = get_irg_start_block(irg);
+       foreach_block_succ(start_block, edge) {
+               ir_node *succ = get_edge_src_irn(edge);
+               if(succ != start_block) {
+                       block2 = succ;
+                       break;
+               }
+       }
+       assert(block2 != NULL);
+
+       count = be_profile_get_block_execcount(block2);
+       if(count == 0) {
+               // the function was never executed, so fallback to estimated freqs
+               free_execfreq(env.execfreqs);
+
+               return compute_execfreq(irg, 10);
+       }
+
+       env.freq_factor = 1.0 / count;
+       irg_block_walk_graph(irg, initialize_execfreq, NULL, &env);
 
+       return env.execfreqs;
 }