Unconditionally include stdlib.h.
[libfirm] / ir / ana / execfreq.c
index 957dc89..e312cd3 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.
  *
  * @date        28.05.2006
  * @version     $Id$
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
-
-#undef USE_GSL
 
 #include <stdio.h>
 #include <string.h>
 #include <limits.h>
 #include <math.h>
 
-#ifdef USE_GSL
-#include <gsl/gsl_linalg.h>
-#include <gsl/gsl_vector.h>
-#else
-#include "gaussjordan.h"
-#endif
+#include "gaussseidel.h"
 
 #include "firm_common_t.h"
 #include "set.h"
 #include "hashptr.h"
 #include "debug.h"
+#include "statev.h"
+#include "dfs_t.h"
+#include "absgraph.h"
 
 #include "irprog_t.h"
 #include "irgraph_t.h"
 #include "irgwalk.h"
 #include "iredges.h"
 #include "irprintf.h"
+#include "irtools.h"
 #include "irhooks.h"
 
 #include "execfreq.h"
 
-#define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
+/* 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 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;
 } freq_t;
 
-
-typedef struct _walkerdata_t {
-  set    *set;
-  size_t  idx;
-} walkerdata_t;
-
 struct ir_exec_freq {
        set *set;
        hook_entry_t hook;
@@ -87,6 +89,7 @@ cmp_freq(const void *a, const void *b, size_t size)
 {
        const freq_t *p = a;
        const freq_t *q = b;
+       (void) size;
 
        return !(p->irn == q->irn);
 }
@@ -107,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));
 }
 
@@ -132,58 +136,47 @@ get_block_execfreq_ulong(const ir_exec_freq *ef, const ir_node *bb)
 {
        double f       = get_block_execfreq(ef, bb);
        int res        = (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
-
-       // printf("%20.6f %10d\n", f, res);
        return res;
 }
 
-#define EPSILON                0.0001
-#define UNDEF(x)    !(x > EPSILON)
-
-static void
-block_walker(ir_node * bb, void * data)
-{
-  walkerdata_t  *wd = data;
-
-  set_insert_freq(wd->set, bb);
-  set_irn_link(bb, (void*)wd->idx++);
-}
-
-#ifdef USE_GSL
-static gsl_vector *
-solve_lgs(double * a_data, double * b_data, size_t size)
-{
-  gsl_matrix_view m
-    = gsl_matrix_view_array (a_data, size, size);
-
-  gsl_vector_view b
-    = gsl_vector_view_array (b_data, size);
-
-  gsl_vector *x = gsl_vector_alloc (size);
-
-  int s;
-
-  gsl_permutation * p = gsl_permutation_alloc (size);
-
-  gsl_linalg_LU_decomp (&m.matrix, p, &s);
-
-  gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);
-
-  gsl_permutation_free (p);
-
-  return x;
-}
-#else
 static double *
-solve_lgs(double * A, double * b, size_t size)
+solve_lgs(gs_matrix_t *mat, double *x, int size)
 {
-  if(firm_gaussjordansolve(A,b,size) == 0) {
-    return b;
-  } else {
-    return NULL;
-  }
+       double init = 1.0 / size;
+       double dev;
+       int i, iter = 0;
+
+       /* better convergence. */
+       for (i = 0; i < size; ++i)
+               x[i] = init;
+
+       stat_ev_dbl("execfreq_matrix_size", size);
+       stat_ev_tim_push();
+       do {
+               ++iter;
+               dev = gs_matrix_gauss_seidel(mat, x, size);
+       } 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;
 }
-#endif /* USE_GSL */
 
 static double
 get_cf_probability(ir_node *bb, int pos, double loop_weight)
@@ -217,14 +210,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));
        execfreq->hook.context = execfreq;
        execfreq->hook.hook._hook_node_info = exec_freq_node_info;
        register_hook(hook_node_info, &execfreq->hook);
+       (void) irg;
 
        return execfreq;
 }
@@ -235,148 +228,159 @@ 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)
 {
-       size_t        size;
-       double       *matrix;
-       double       *rhs;
-       int           i;
-       freq_t       *freq;
-       walkerdata_t  wd;
-       ir_exec_freq  *ef;
+       gs_matrix_t  *mat;
+       int           size;
+       int           idx;
+       freq_t       *freq, *s, *e;
+       ir_exec_freq *ef;
        set          *freqs;
-#ifdef USE_GSL
-       gsl_vector   *x;
-#else
+       dfs_t        *dfs;
        double       *x;
-#endif
-
-       ef = xmalloc(sizeof(ef[0]));
-       memset(ef, 0, sizeof(ef[0]));
-       ef->min_non_zero = 1e50; /* initialize with a reasonable large number. */
-       freqs = ef->set = new_set(cmp_freq, 32);
+       double        norm;
+
+       /*
+        * 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);
 
-       wd.idx = 0;
-       wd.set = freqs;
-
-       irg_block_walk_graph(irg, block_walker, NULL, &wd);
-
-       size = set_count(freqs);
-       matrix = xmalloc(size*size*sizeof(*matrix));
-       memset(matrix, 0, size*size*sizeof(*matrix));
-       rhs = xmalloc(size*sizeof(*rhs));
-       memset(rhs, 0, size*sizeof(*rhs));
-
-       set_foreach(freqs, freq) {
-               ir_node *bb = (ir_node *)freq->irn;
-               size_t  idx = (int)get_irn_link(bb);
+       size = dfs_get_n_nodes(dfs);
+       mat  = gs_new_matrix(size, size);
+       x    = XMALLOCN(double, size);
 
-               matrix[idx * (size + 1)] = -1.0;
+       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;
 
-               if (bb == get_irg_start_block(irg)) {
-                       rhs[(int)get_irn_link(bb)] = -1.0;
-                       continue;
-               }
+               freq = set_insert_freq(freqs, bb);
+               freq->idx = idx;
 
                for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
-                       ir_node *pred    = get_Block_cfgpred_block(bb, i);
-                       size_t  pred_idx = (int)get_irn_link(pred);
+                       ir_node *pred = get_Block_cfgpred_block(bb, i);
+                       int pred_idx  = size - dfs_get_post_num(dfs, pred) - 1;
 
-                       //      matrix[pred_idx + idx*size] += 1.0/(double)get_Block_n_cfg_outs(pred);
-                       matrix[pred_idx + idx * size] += get_cf_probability(bb, i, loop_weight);
+                       gs_matrix_set(mat, idx, pred_idx, get_cf_probability(bb, i, loop_weight));
                }
+               gs_matrix_set(mat, idx, idx, -1.0);
        }
 
-       x = solve_lgs(matrix, rhs, size);
-       if (x == NULL) {
-               DEBUG_ONLY(ir_fprintf(stderr, "Debug Warning: Couldn't estimate execution frequencies for %+F\n", irg));
-               ef->infeasible = 1;
-       } else {
-               ef->max = 0.0;
-
-               set_foreach(freqs, freq) {
-                       const ir_node *bb = freq->irn;
-                       size_t        idx = PTR_TO_INT(get_irn_link(bb));
-
-#ifdef USE_GSL
-                       freq->freq = UNDEF(gsl_vector_get(x, idx)) ? EPSILON : gsl_vector_get(x, idx);
-#else
-                       freq->freq = UNDEF(x[idx]) ? EPSILON : x[idx];
-#endif
+       dfs_free(dfs);
+
+       /*
+        * Add a loop 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);
+
+       /* solve the system and delete the matrix */
+       solve_lgs(mat, x, size);
+       gs_delete_matrix(mat);
+
+       /*
+        * compute the normalization factor.
+        * 1.0 / exec freq of start block.
+        */
+       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;
 
-                       /* get the maximum exec freq */
-                       ef->max = MAX(ef->max, freq->freq);
+               /* take abs because it sometimes can be -0 in case of endless loops */
+               freq->freq = fabs(x[idx]) * norm;
 
-                       /* Get the minimum non-zero execution frequency. */
-                       if(freq->freq > 0.0)
-                               ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
-               }
+               /* get the maximum exec freq */
+               ef->max = MAX(ef->max, freq->freq);
 
-               /* compute m and b of the transformation used to convert the doubles into scaled ints */
-               {
-                       double smallest_diff = 1.0;
+               /* Get the minimum non-zero execution frequency. */
+               if(freq->freq > 0.0)
+                       ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
+       }
 
-                       double l2 = ef->min_non_zero;
-                       double h2 = ef->max;
-                       double l1 = 1.0;
-                       double h1 = MAX_INT_FREQ;
+       /* compute m and b of the transformation used to convert the doubles into scaled ints */
+       {
+               double smallest_diff = 1.0;
 
-                       double *fs = malloc(set_count(freqs) * sizeof(fs[0]));
-                       int i, j, n = 0;
+               double l2 = ef->min_non_zero;
+               double h2 = ef->max;
+               double l1 = 1.0;
+               double h1 = MAX_INT_FREQ;
 
-                       set_foreach(freqs, freq)
-                               fs[n++] = freq->freq;
+               double *fs = malloc(set_count(freqs) * sizeof(fs[0]));
+               int i, j, n = 0;
 
-                       /*
-                        * 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;
+               set_foreach(freqs, freq)
+                       fs[n++] = freq->freq;
 
-                               for(j = i + 1; j < n; ++j) {
-                                       double diff = fabs(fs[i] - fs[j]);
+               /*
+                * 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;
 
-                                       if(!UNDEF(diff))
-                                               smallest_diff = MIN(diff, smallest_diff);
-                               }
-                       }
+                       for(j = i + 1; j < n; ++j) {
+                               double diff = fabs(fs[i] - fs[j]);
 
-                       /* according to that the slope of the translation function is 1.0 / smallest diff */
-                       ef->m = 1.0 / smallest_diff;
+                               if(!UNDEF(diff))
+                                       smallest_diff = MIN(diff, smallest_diff);
+                       }
+               }
 
-                       /* the abscissa is then given by */
-                       ef->b = l1 - ef->m * l2;
+               /* according to that the slope of the translation function is 1.0 / smallest diff */
+               ef->m = 1.0 / smallest_diff;
 
-                       /*
-                        * 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;
-                       }
+               /* the abscissa is then given by */
+               ef->b = l1 - ef->m * l2;
 
-                       // printf("smallest_diff: %g, l1: %f, h1: %f, l2: %f, h2: %f, m: %f, b: %f\n", smallest_diff, l1, h1, l2, h2, ef->m, ef->b);
-                       free(fs);
+               /*
+                * 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;
                }
 
-#ifdef USE_GSL
-               gsl_vector_free(x);
-#endif
-               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);
+               free(fs);
        }
 
-       free(matrix);
-       free(rhs);
+       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);
+
+       xfree(x);
 
        return ef;
 }