fix bugs in execfreq rework commit
[libfirm] / ir / ana / execfreq.c
index 2e7a136..519d90c 100644 (file)
@@ -22,7 +22,6 @@
  * @brief       Compute an estimate of basic block executions.
  * @author      Adam M. Szalkowski
  * @date        28.05.2006
- * @version     $Id$
  */
 #include "config.h"
 
 #include "iredges.h"
 #include "irouts.h"
 #include "irprintf.h"
-#include "irtools.h"
+#include "util.h"
 #include "irhooks.h"
+#include "irnodehashmap.h"
 
-#include "execfreq.h"
+#include "execfreq_t.h"
 
-/* enable to also solve the equations with Gauss-Jordan */
-#undef COMPARE_AGAINST_GAUSSJORDAN
-
-#ifdef COMPARE_AGAINST_GAUSSJORDAN
-#include "gaussjordan.h"
-#endif
-
-
-#define EPSILON                     1e-5
+#define EPSILON          1e-5
 #define UNDEF(x)         (fabs(x) < EPSILON)
 #define SEIDEL_TOLERANCE 1e-7
 
 #define MAX_INT_FREQ 1000000
 
-#define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
-
-typedef struct _freq_t {
-       const ir_node    *irn;
-       int               idx;
-       double            freq;
+typedef struct freq_t {
+       double freq;
 } freq_t;
 
-struct ir_exec_freq {
-       set *set;
-       hook_entry_t hook;
-       double max;
-       double min_non_zero;
-       double m, b;
-       unsigned infeasible : 1;
-};
-
-static int
-cmp_freq(const void *a, const void *b, size_t size)
-{
-       const freq_t *p = a;
-       const freq_t *q = b;
-       (void) size;
+static ir_nodehashmap_t freq_map;
+static struct obstack   obst;
+static hook_entry_t     hook;
 
-       return !(p->irn == q->irn);
+double get_block_execfreq(const ir_node *block)
+{
+       const freq_t *freq = ir_nodehashmap_get(freq_t, &freq_map, block);
+       if (freq == NULL)
+               return 0.0;
+       return freq->freq;
 }
 
-static freq_t *
-set_find_freq(set * set, const ir_node * irn)
+void set_block_execfreq(ir_node *block, double newfreq)
 {
-       freq_t     query;
-
-       query.irn = irn;
-       return set_find(set, &query, sizeof(query), HASH_PTR(irn));
+       freq_t *freq = ir_nodehashmap_get(freq_t, &freq_map, block);
+       if (freq == NULL) {
+               freq = OALLOC(&obst, freq_t);
+               ir_nodehashmap_insert(&freq_map, block, freq);
+       }
+       freq->freq = newfreq;
 }
 
-static freq_t *
-set_insert_freq(set * set, const ir_node * irn)
+static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
 {
-       freq_t query;
-
-       query.irn = irn;
-       query.freq = 0.0;
-       query.idx  = -1;
-       return set_insert(set, &query, sizeof(query), HASH_PTR(irn));
+       (void)ctx;
+       if (!is_Block(irn))
+               return;
+       const freq_t *freq = ir_nodehashmap_get(freq_t, &freq_map, irn);
+       if (freq != NULL)
+               fprintf(f, "execution frequency: %g\n", get_block_execfreq(irn));
 }
 
-double
-get_block_execfreq(const ir_exec_freq *ef, const ir_node * irn)
+void init_execfreq(void)
 {
-       if(!ef->infeasible) {
-               set *freqs = ef->set;
-               freq_t *freq;
-               assert(is_Block(irn));
-               freq = set_find_freq(freqs, irn);
-               assert(freq);
-
-               assert(freq->freq >= 0);
-               return freq->freq;
-       }
+       ir_nodehashmap_init(&freq_map);
+       obstack_init(&obst);
 
-       return 1.0;
+       memset(&hook, 0, sizeof(hook));
+       hook.hook._hook_node_info = exec_freq_node_info;
+       register_hook(hook_node_info, &hook);
 }
 
-unsigned long
-get_block_execfreq_ulong(const ir_exec_freq *ef, const ir_node *bb)
+void exit_execfreq(void)
 {
-       double f       = get_block_execfreq(ef, bb);
-       int res        = (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
-       return res;
+       unregister_hook(hook_node_info, &hook);
+
+       obstack_free(&obst, NULL);
+       ir_nodehashmap_destroy(&freq_map);
 }
 
-static double *
-solve_lgs(gs_matrix_t *mat, double *x, int size)
+
+static double *solve_lgs(gs_matrix_t *mat, double *x, int size)
 {
        double init = 1.0 / size;
        double dev;
@@ -155,45 +129,27 @@ solve_lgs(gs_matrix_t *mat, double *x, int size)
        do {
                ++iter;
                dev = gs_matrix_gauss_seidel(mat, x, size);
-       } while(fabs(dev) > SEIDEL_TOLERANCE);
+       } while (fabs(dev) > SEIDEL_TOLERANCE);
        stat_ev_tim_pop("execfreq_seidel_time");
        stat_ev_dbl("execfreq_seidel_iter", iter);
 
-#ifdef COMPARE_AGAINST_GAUSSJORDAN
-       {
-               double *nw = XMALLOCN(double, size * size);
-               double *nx = XMALLOCNZ(double, size);
-
-               gs_matrix_export(mat, nw, size);
-
-               stat_ev_tim_push();
-               firm_gaussjordansolve(nw, nx, size);
-               stat_ev_tim_pop("execfreq_jordan_time");
-
-               xfree(nw);
-               xfree(nx);
-       }
-#endif
-
        return x;
 }
 
 /*
  * Determine probability that predecessor pos takes this cf edge.
  */
-static double
-get_cf_probability(ir_node *bb, int pos, double loop_weight)
+static double get_cf_probability(const ir_node *bb, int pos, double loop_weight)
 {
-       double           sum = 0.0;
-       double           cur = 1.0;
-       double           inv_loop_weight = 1./loop_weight;
-       const ir_node   *pred = get_Block_cfgpred_block(bb, pos);
-       const ir_loop   *pred_loop;
-       int              pred_depth;
-       const ir_edge_t *edge;
-       const ir_loop   *loop;
-       int              depth;
-       int              d;
+       double         sum = 0.0;
+       double         cur = 1.0;
+       double         inv_loop_weight = 1./loop_weight;
+       const ir_node *pred = get_Block_cfgpred_block(bb, pos);
+       const ir_loop *pred_loop;
+       int            pred_depth;
+       const ir_loop *loop;
+       int            depth;
+       int            d;
 
        if (is_Bad(pred))
                return 0;
@@ -222,115 +178,125 @@ get_cf_probability(ir_node *bb, int pos, double loop_weight)
        return cur/sum;
 }
 
-static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
+static double *freqs;
+static double  min_non_zero;
+static double  max_freq;
+
+static void collect_freqs(ir_node *node, void *data)
 {
-       if(is_Block(irn)) {
-               ir_exec_freq *ef = ctx;
-               fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
-       }
+       (void) data;
+       double freq = get_block_execfreq(node);
+       if (freq > max_freq)
+               max_freq = freq;
+       if (freq > 0.0 && freq < min_non_zero)
+               min_non_zero = freq;
+       ARR_APP1(double, freqs, freq);
 }
 
-ir_exec_freq *create_execfreq(ir_graph *irg)
+void ir_calculate_execfreq_int_factors(ir_execfreq_int_factors *factors,
+                                       ir_graph *irg)
 {
-       ir_exec_freq *execfreq = XMALLOCZ(ir_exec_freq);
-       execfreq->set = new_set(cmp_freq, 32);
+       /* compute m and b of the transformation used to convert the doubles into
+        * scaled ints */
+       freqs = NEW_ARR_F(double, 0);
+       min_non_zero = HUGE_VAL;
+       max_freq     = 0.0;
+       irg_block_walk_graph(irg, collect_freqs, NULL, NULL);
+
+       /*
+        * find the smallest difference of the execution frequencies
+        * we try to ressolve it with 1 integer.
+        */
+       size_t n_freqs       = ARR_LEN(freqs);
+       double smallest_diff = 1.0;
+       for (size_t i = 0; i < n_freqs; ++i) {
+               if (freqs[i] <= 0.0)
+                       continue;
 
-       memset(&execfreq->hook, 0, sizeof(execfreq->hook));
+               for (size_t j = i + 1; j < n_freqs; ++j) {
+                       double diff = fabs(freqs[i] - freqs[j]);
 
-       // set reasonable values to convert double execfreq to ulong execfreq
-       execfreq->m = 1.0;
+                       if (!UNDEF(diff))
+                               smallest_diff = MIN(diff, smallest_diff);
+               }
+       }
 
-       execfreq->hook.context = execfreq;
-       execfreq->hook.hook._hook_node_info = exec_freq_node_info;
-       register_hook(hook_node_info, &execfreq->hook);
-       (void) irg;
+       double l2 = min_non_zero;
+       double h2 = max_freq;
+       double l1 = 1.0;
+       double h1 = MAX_INT_FREQ;
 
-       return execfreq;
-}
+       /* according to that the slope of the translation function is
+        * 1.0 / smallest_diff */
+       factors->m = 1.0 / smallest_diff;
 
-void set_execfreq(ir_exec_freq *execfreq, const ir_node *block, double freq)
-{
-       freq_t *f = set_insert_freq(execfreq->set, block);
-       f->freq = freq;
+       /* the abscissa is then given by */
+       factors->b = l1 - factors->m * l2;
+
+       /*
+        * if the slope is so high that the largest integer would be larger than
+        * MAX_INT_FREQ set the largest int freq to that upper limit and recompute
+        * the translation function
+        */
+       if (factors->m * h2 + factors->b > MAX_INT_FREQ) {
+               factors->m = (h1 - l1) / (h2 - l2);
+               factors->b = l1 - factors->m * l2;
+       }
+
+       DEL_ARR_F(freqs);
 }
 
-static void collect_blocks(ir_node *bl, void *data)
+int get_block_execfreq_int(const ir_execfreq_int_factors *factors,
+                           const ir_node *block)
 {
-       set *freqs = data;
-       set_insert_freq(freqs, bl);
+       double f   = get_block_execfreq(block);
+       int    res = (int) (f > factors->min_non_zero ? factors->m * f + factors->b : 1.0);
+       return res;
 }
 
-ir_exec_freq *
-compute_execfreq(ir_graph * irg, double loop_weight)
+void ir_estimate_execfreq(ir_graph *irg)
 {
-       gs_matrix_t  *mat;
-       int           size;
-       int           n_keepalives;
-       int           idx;
-       freq_t       *freq, *s, *e;
-       ir_exec_freq *ef;
-       ir_node      *end = get_irg_end(irg);
-       set          *freqs;
-       dfs_t        *dfs;
-       double       *x;
-       double        norm;
+       double loop_weight = 10.0;
 
-       /*
-        * compute a DFS.
+       assure_irg_properties(irg,
+               IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
+               | IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
+
+       /* compute a DFS.
         * using a toposort on the CFG (without back edges) will propagate
         * the values better for the gauss/seidel iteration.
         * => they can "flow" from start to end.
         */
-       dfs = dfs_new(&absgraph_irg_cfg_succ, irg);
-       ef = XMALLOCZ(ir_exec_freq);
-       ef->min_non_zero = HUGE_VAL; /* initialize with a reasonable large number. */
-       freqs = ef->set = new_set(cmp_freq, dfs_get_n_nodes(dfs));
-
-       /*
-        * Populate the exec freq set.
-        * The DFS cannot be used alone, since the CFG might not be connected
-        * due to unreachable code.
-        */
-       irg_block_walk_graph(irg, collect_blocks, NULL, freqs);
-
-       construct_cf_backedges(irg);
-       edges_assure(irg);
+       dfs_t *dfs = dfs_new(&absgraph_irg_cfg_succ, irg);
 
-       size = dfs_get_n_nodes(dfs);
-       mat  = gs_new_matrix(size, size);
-       x    = XMALLOCN(double, size);
+       int          size = dfs_get_n_nodes(dfs);
+       gs_matrix_t *mat  = gs_new_matrix(size, size);
 
-       for (idx = dfs_get_n_nodes(dfs) - 1; idx >= 0; --idx) {
-               ir_node *bb = (ir_node *) dfs_get_post_num_node(dfs, size - idx - 1);
-               freq_t *freq;
-               int i;
+       ir_node *end_block = get_irg_end_block(irg);
 
-               freq = set_insert_freq(freqs, bb);
-               freq->idx = idx;
+       for (int idx = size - 1; idx >= 0; --idx) {
+               const ir_node *bb = (ir_node*)dfs_get_post_num_node(dfs, size-idx-1);
 
                /* Sum of (execution frequency of predecessor * probability of cf edge) ... */
-               for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
-                       ir_node *pred = get_Block_cfgpred_block(bb, i);
-                       int pred_idx  = size - dfs_get_post_num(dfs, pred) - 1;
-
-                       gs_matrix_set(mat, idx, pred_idx, get_cf_probability(bb, i, loop_weight));
+               for (int i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
+                       const ir_node *pred           = get_Block_cfgpred_block(bb, i);
+                       int            pred_idx       = size - dfs_get_post_num(dfs, pred)-1;
+                       double         cf_probability = get_cf_probability(bb, i, loop_weight);
+                       gs_matrix_set(mat, idx, pred_idx, cf_probability);
                }
                /* ... equals my execution frequency */
                gs_matrix_set(mat, idx, idx, -1.0);
-       }
-
-       dfs_free(dfs);
-
-       /*
-        * Add an edge from end to start.
-        * The problem is then an eigenvalue problem:
-        * Solve A*x = 1*x => (A-I)x = 0
-        */
-       s = set_find_freq(freqs, get_irg_start_block(irg));
 
-       e = set_find_freq(freqs, get_irg_end_block(irg));
-       if (e->idx >= 0)
-               gs_matrix_set(mat, s->idx, e->idx, 1.0);
+               /* Add an edge from end to start.
+                * The problem is then an eigenvalue problem:
+                * Solve A*x = 1*x => (A-I)x = 0
+                */
+               if (bb == end_block) {
+                       const ir_node *start_block = get_irg_start_block(irg);
+                       int            s_idx = size - dfs_get_post_num(dfs, start_block)-1;
+                       gs_matrix_set(mat, s_idx, idx, 1.0);
+               }
+       }
 
        /*
         * Also add an edge for each kept block to start.
@@ -338,105 +304,43 @@ compute_execfreq(ir_graph * irg, double loop_weight)
         * This avoid strange results for e.g. an irg containing a exit()-call
         * which block has no cfg successor.
         */
-       n_keepalives = get_End_n_keepalives(end);
-       for (idx = n_keepalives - 1; idx >= 0; --idx) {
+       ir_node       *start_block  = get_irg_start_block(irg);
+       int            s_idx        = size - dfs_get_post_num(dfs, start_block)-1;
+       const ir_node *end          = get_irg_end(irg);
+       int            n_keepalives = get_End_n_keepalives(end);
+       for (int idx = n_keepalives - 1; idx >= 0; --idx) {
                ir_node *keep = get_End_keepalive(end, idx);
+               if (!is_Block(keep) || get_irn_n_edges_kind(keep, EDGE_KIND_BLOCK) > 0)
+                       continue;
 
-               if (is_Block(keep) && get_Block_n_cfg_outs(keep) == 0) {
-                       freq_t *k = set_find_freq(freqs, keep);
-                       if (k->idx >= 0)
-                               gs_matrix_set(mat, s->idx, k->idx, 1.0);
-               }
+               int k_idx = size-dfs_get_post_num(dfs, keep)-1;
+               if (k_idx > 0)
+                       gs_matrix_set(mat, s_idx, k_idx, 1.0);
        }
 
        /* solve the system and delete the matrix */
+       double *x = XMALLOCN(double, size);
        solve_lgs(mat, x, size);
        gs_delete_matrix(mat);
 
-       /*
-        * compute the normalization factor.
+       /* compute the normalization factor.
         * 1.0 / exec freq of start block.
+        * (note: start_idx is != 0 in strange cases involving endless loops,
+        *  probably a misfeature/bug)
         */
-       norm = x[s->idx] != 0.0 ? 1.0 / x[s->idx] : 1.0;
+       int    start_idx  = size-dfs_get_post_num(dfs, get_irg_start_block(irg))-1;
+       double start_freq = x[start_idx];
+       double norm       = start_freq != 0.0 ? 1.0 / start_freq : 1.0;
 
-       ef->max = 0.0;
-       set_foreach(freqs, freq) {
-               int idx = freq->idx;
+       for (int idx = size - 1; idx >= 0; --idx) {
+               ir_node *bb = (ir_node *) dfs_get_post_num_node(dfs, size - idx - 1);
 
                /* take abs because it sometimes can be -0 in case of endless loops */
-               freq->freq = fabs(x[idx]) * norm;
-
-               /* get the maximum exec freq */
-               ef->max = MAX(ef->max, freq->freq);
-
-               /* Get the minimum non-zero execution frequency. */
-               if(freq->freq > 0.0)
-                       ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
+               double freq = fabs(x[idx]) * norm;
+               set_block_execfreq(bb, freq);
        }
 
-       /* compute m and b of the transformation used to convert the doubles into scaled ints */
-       {
-               double smallest_diff = 1.0;
-
-               double l2 = ef->min_non_zero;
-               double h2 = ef->max;
-               double l1 = 1.0;
-               double h1 = MAX_INT_FREQ;
-
-               double *fs = malloc(set_count(freqs) * sizeof(fs[0]));
-               int i, j, n = 0;
-
-               set_foreach(freqs, freq)
-                       fs[n++] = freq->freq;
-
-               /*
-                * find the smallest difference of the execution frequencies
-                * we try to ressolve it with 1 integer.
-                */
-               for(i = 0; i < n; ++i) {
-                       if(fs[i] <= 0.0)
-                               continue;
-
-                       for(j = i + 1; j < n; ++j) {
-                               double diff = fabs(fs[i] - fs[j]);
-
-                               if(!UNDEF(diff))
-                                       smallest_diff = MIN(diff, smallest_diff);
-                       }
-               }
-
-               /* according to that the slope of the translation function is 1.0 / smallest diff */
-               ef->m = 1.0 / smallest_diff;
-
-               /* the abscissa is then given by */
-               ef->b = l1 - ef->m * l2;
-
-               /*
-                * if the slope is so high that the largest integer would be larger than MAX_INT_FREQ
-                * set the largest int freq to that upper limit and recompute the translation function
-                */
-               if(ef->m * h2 + ef->b > MAX_INT_FREQ) {
-                       ef->m = (h1 - l1) / (h2 - l2);
-                       ef->b = l1 - ef->m * l2;
-               }
-
-               free(fs);
-       }
-
-       memset(&ef->hook, 0, sizeof(ef->hook));
-       ef->hook.context = ef;
-       ef->hook.hook._hook_node_info = exec_freq_node_info;
-       register_hook(hook_node_info, &ef->hook);
+       dfs_free(dfs);
 
        xfree(x);
-
-       return ef;
-}
-
-void
-free_execfreq(ir_exec_freq *ef)
-{
-       del_set(ef->set);
-       unregister_hook(hook_node_info, &ef->hook);
-       free(ef);
 }