Moved some ia32 independent code to bemain.
[libfirm] / ir / ana / execfreq.c
index ff9ef3e..2e7a136 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
@@ -24,9 +24,7 @@
  * @date        28.05.2006
  * @version     $Id$
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #include <stdio.h>
 #include <string.h>
@@ -35,7 +33,6 @@
 
 #include "gaussseidel.h"
 
-#include "firm_common_t.h"
 #include "set.h"
 #include "hashptr.h"
 #include "debug.h"
@@ -49,6 +46,7 @@
 #include "irloop.h"
 #include "irgwalk.h"
 #include "iredges.h"
+#include "irouts.h"
 #include "irprintf.h"
 #include "irtools.h"
 #include "irhooks.h"
@@ -112,6 +110,7 @@ set_insert_freq(set * set, const ir_node * irn)
 
        query.irn = irn;
        query.freq = 0.0;
+       query.idx  = -1;
        return set_insert(set, &query, sizeof(query), HASH_PTR(irn));
 }
 
@@ -162,10 +161,9 @@ solve_lgs(gs_matrix_t *mat, double *x, int size)
 
 #ifdef COMPARE_AGAINST_GAUSSJORDAN
        {
-               double *nw = xmalloc(size * size * sizeof(*nw));
-               double *nx = xmalloc(size * sizeof(*nx));
+               double *nw = XMALLOCN(double, size * size);
+               double *nx = XMALLOCNZ(double, size);
 
-               memset(nx, 0, size * sizeof(*nx));
                gs_matrix_export(mat, nw, size);
 
                stat_ev_tim_push();
@@ -180,23 +178,45 @@ solve_lgs(gs_matrix_t *mat, double *x, int size)
        return x;
 }
 
+/*
+ * Determine probability that predecessor pos takes this cf edge.
+ */
 static double
 get_cf_probability(ir_node *bb, int pos, double loop_weight)
 {
-       double  sum = 0.0;
-       double  cur = 0.0;
-       const ir_node *pred = get_Block_cfgpred_block(bb, pos);
-       const ir_loop *pred_loop = get_irn_loop(pred);
-       int pred_depth = get_loop_depth(pred_loop);
+       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;
+
+       if (is_Bad(pred))
+               return 0;
 
-       cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
+       loop       = get_irn_loop(bb);
+       depth      = get_loop_depth(loop);
+       pred_loop  = get_irn_loop(pred);
+       pred_depth = get_loop_depth(pred_loop);
+
+       for (d = depth; d < pred_depth; ++d) {
+               cur *= inv_loop_weight;
+       }
 
        foreach_block_succ(pred, edge) {
-               const ir_node *block = get_edge_src_irn(edge);
-               const ir_loop *loop = get_irn_loop(block);
-               int depth = get_loop_depth(loop);
-               sum += depth < pred_depth ? 1.0 : loop_weight;
+               const ir_node *succ       = get_edge_src_irn(edge);
+               const ir_loop *succ_loop  = get_irn_loop(succ);
+               int            succ_depth = get_loop_depth(succ_loop);
+
+               double         fac = 1.0;
+               for (d = succ_depth; d < pred_depth; ++d) {
+                       fac *= inv_loop_weight;
+               }
+               sum += fac;
        }
 
        return cur/sum;
@@ -212,11 +232,14 @@ static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
 
 ir_exec_freq *create_execfreq(ir_graph *irg)
 {
-       ir_exec_freq *execfreq = xmalloc(sizeof(execfreq[0]));
-       memset(execfreq, 0, sizeof(execfreq[0]));
+       ir_exec_freq *execfreq = XMALLOCZ(ir_exec_freq);
        execfreq->set = new_set(cmp_freq, 32);
 
        memset(&execfreq->hook, 0, sizeof(execfreq->hook));
+
+       // set reasonable values to convert double execfreq to ulong execfreq
+       execfreq->m = 1.0;
+
        execfreq->hook.context = execfreq;
        execfreq->hook.hook._hook_node_info = exec_freq_node_info;
        register_hook(hook_node_info, &execfreq->hook);
@@ -231,14 +254,22 @@ void set_execfreq(ir_exec_freq *execfreq, const ir_node *block, double freq)
        f->freq = freq;
 }
 
+static void collect_blocks(ir_node *bl, void *data)
+{
+       set *freqs = data;
+       set_insert_freq(freqs, bl);
+}
+
 ir_exec_freq *
 compute_execfreq(ir_graph * irg, double loop_weight)
 {
        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;
@@ -251,21 +282,23 @@ compute_execfreq(ir_graph * irg, double loop_weight)
         * => they can "flow" from start to end.
         */
        dfs = dfs_new(&absgraph_irg_cfg_succ, irg);
-       ef = xmalloc(sizeof(ef[0]));
-       memset(ef, 0, sizeof(ef[0]));
+       ef = XMALLOCZ(ir_exec_freq);
        ef->min_non_zero = HUGE_VAL; /* initialize with a reasonable large number. */
-       freqs = ef->set = new_set(cmp_freq, 32);
+       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);
-       /* TODO: edges are corrupt for EDGE_KIND_BLOCK after the local optimize
-                graph phase merges blocks in the x86 backend */
-       edges_deactivate(irg);
-       edges_activate(irg);
-       /* edges_assure(irg); */
+       edges_assure(irg);
 
        size = dfs_get_n_nodes(dfs);
        mat  = gs_new_matrix(size, size);
-       x    = xmalloc(size*sizeof(*x));
+       x    = XMALLOCN(double, 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);
@@ -275,23 +308,46 @@ compute_execfreq(ir_graph * irg, double loop_weight)
                freq = set_insert_freq(freqs, bb);
                freq->idx = idx;
 
-               gs_matrix_set(mat, idx, idx, -1.0);
+               /* 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));
                }
+               /* ... equals my execution frequency */
+               gs_matrix_set(mat, idx, idx, -1.0);
        }
 
+       dfs_free(dfs);
+
        /*
-        * Add a loop from end to start.
+        * 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));
-       gs_matrix_set(mat, s->idx, e->idx, 1.0);
+       if (e->idx >= 0)
+               gs_matrix_set(mat, s->idx, e->idx, 1.0);
+
+       /*
+        * Also add an edge for each kept block to start.
+        *
+        * 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 *keep = get_End_keepalive(end, idx);
+
+               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);
+               }
+       }
 
        /* solve the system and delete the matrix */
        solve_lgs(mat, x, size);
@@ -301,16 +357,14 @@ compute_execfreq(ir_graph * irg, double loop_weight)
         * compute the normalization factor.
         * 1.0 / exec freq of start block.
         */
-       assert(x[s->idx] > 0.0);
-       norm = 1.0 / x[s->idx];
+       norm = x[s->idx] != 0.0 ? 1.0 / x[s->idx] : 1.0;
 
        ef->max = 0.0;
        set_foreach(freqs, freq) {
                int idx = freq->idx;
 
-               /* freq->freq = UNDEF(x[idx]) ? EPSILON : x[idx]; */
-               /* TODO: Do we need the check for zero? */
-               freq->freq = x[idx] * norm;
+               /* 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);