Normalizing for integer frequencies improved
[libfirm] / ir / ana / execfreq.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/execfreq.c
4  * Purpose:     Compute an estimate of basic block executions.
5  * Author:      Adam M. Szalkowski
6  * Modified by:
7  * Created:     28.05.2006
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2006 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #define USE_GSL
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <limits.h>
22 #include <math.h>
23
24 #ifdef USE_GSL
25 #include <gsl/gsl_linalg.h>
26 #include <gsl/gsl_vector.h>
27 #else
28 #include "gaussjordan.h"
29 #endif
30
31 #include "execfreq.h"
32
33 #include "firm_common_t.h"
34 #include "set.h"
35 #include "hashptr.h"
36
37 #include "irprog_t.h"
38 #include "irgraph_t.h"
39 #include "irnode_t.h"
40 #include "irloop.h"
41 #include "irgwalk.h"
42 #include "irouts.h"
43 #include "irprintf.h"
44 #include "irhooks.h"
45
46 #include "execfreq.h"
47
48 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
49
50 #define MAX_INT_FREQ 1000000
51
52 typedef struct _freq_t {
53         const ir_node    *irn;
54         double            freq;
55 } freq_t;
56
57
58 typedef struct _walkerdata_t {
59   set    *set;
60   size_t  idx;
61 } walkerdata_t;
62
63 struct _exec_freq_t {
64         set *set;
65         hook_entry_t hook;
66         double max;
67         double min_non_zero;
68         double m, b;
69         unsigned infeasible : 1;
70 };
71
72 static int
73 cmp_freq(const void *a, const void *b, size_t size)
74 {
75   const freq_t *p = a;
76   const freq_t *q = b;
77
78   return !(p->irn == q->irn);
79 }
80
81 static freq_t *
82 set_find_freq(set * set, const ir_node * irn)
83 {
84   freq_t     query;
85
86   query.irn = irn;
87   return set_find(set, &query, sizeof(query), HASH_PTR(irn));
88 }
89
90 static freq_t *
91 set_insert_freq(set * set, const ir_node * irn)
92 {
93   freq_t     query;
94
95   query.irn = irn;
96   query.freq = 0.0;
97   return set_insert(set, &query, sizeof(query), HASH_PTR(irn));
98 }
99
100 double
101 get_block_execfreq(const exec_freq_t *ef, const ir_node * irn)
102 {
103         if(!ef->infeasible) {
104                 set *freqs = ef->set;
105                 freq_t *freq;
106                 assert(is_Block(irn));
107                 freq = set_find_freq(freqs, irn);
108                 assert(freq);
109                 return freq->freq;
110         }
111
112         return 1.0;
113 }
114
115 unsigned long
116 get_block_execfreq_ulong(const exec_freq_t *ef, const ir_node *bb)
117 {
118         double f       = get_block_execfreq(ef, bb);
119         return (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
120 }
121
122 #define ZERO(x)   (fabs(x) < 0.0001)
123
124 static void
125 block_walker(ir_node * bb, void * data)
126 {
127   walkerdata_t  *wd = data;
128
129   set_insert_freq(wd->set, bb);
130   set_irn_link(bb, (void*)wd->idx++);
131 }
132
133 #ifdef USE_GSL
134 static gsl_vector *
135 solve_lgs(double * a_data, double * b_data, size_t size)
136 {
137   gsl_matrix_view m
138     = gsl_matrix_view_array (a_data, size, size);
139
140   gsl_vector_view b
141     = gsl_vector_view_array (b_data, size);
142
143   gsl_vector *x = gsl_vector_alloc (size);
144
145   int s;
146
147   gsl_permutation * p = gsl_permutation_alloc (size);
148
149   gsl_linalg_LU_decomp (&m.matrix, p, &s);
150
151   gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);
152
153   gsl_permutation_free (p);
154
155   return x;
156 }
157 #else
158 static double *
159 solve_lgs(double * A, double * b, size_t size)
160 {
161   if(firm_gaussjordansolve(A,b,size) == 0) {
162     return b;
163   } else {
164     return NULL;
165   }
166 }
167 #endif
168
169 static double
170 get_cf_probability(ir_node *bb, int pos, double loop_weight)
171 {
172   double  sum = 0.0;
173   double  cur = 0.0;
174   int     i;
175   ir_node *pred = get_Block_cfgpred_block(bb, pos);
176
177   cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
178
179   for(i = get_Block_n_cfg_outs(pred) - 1; i >= 0; --i) {
180     ir_node *succ = get_Block_cfg_out(pred, i);
181
182     sum += get_loop_depth(get_irn_loop(succ)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
183   }
184
185   return cur/sum;
186 }
187
188 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
189 {
190         if(is_Block(irn)) {
191                 exec_freq_t *ef = ctx;
192                 fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
193         }
194 }
195
196 exec_freq_t *
197 compute_execfreq(ir_graph * irg, double loop_weight)
198 {
199   size_t        size;
200   double       *matrix;
201   double       *rhs;
202   int           i;
203   freq_t       *freq;
204   walkerdata_t  wd;
205         exec_freq_t  *ef;
206         set          *freqs;
207 #ifdef USE_GSL
208   gsl_vector   *x;
209 #else
210   double       *x;
211 #endif
212
213         ef = xmalloc(sizeof(ef[0]));
214         memset(ef, 0, sizeof(ef[0]));
215         ef->min_non_zero = 1e50; /* initialize with a reasonable large number. */
216   freqs = ef->set = new_set(cmp_freq, 32);
217
218   construct_cf_backedges(irg);
219
220   wd.idx = 0;
221   wd.set = freqs;
222
223   irg_block_walk_graph(irg, block_walker, NULL, &wd);
224
225   size = set_count(freqs);
226   matrix = xmalloc(size*size*sizeof(*matrix));
227   memset(matrix, 0, size*size*sizeof(*matrix));
228   rhs = xmalloc(size*sizeof(*rhs));
229   memset(rhs, 0, size*sizeof(*rhs));
230
231   set_foreach(freqs, freq) {
232     ir_node *bb = (ir_node *)freq->irn;
233     size_t  idx = (int)get_irn_link(bb);
234
235     matrix[idx * (size + 1)] = -1.0;
236
237     if (bb == get_irg_start_block(irg)) {
238       rhs[(int)get_irn_link(bb)] = -1.0;
239       continue;
240     }
241
242     for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
243       ir_node *pred    = get_Block_cfgpred_block(bb, i);
244       size_t  pred_idx = (int)get_irn_link(pred);
245
246 //      matrix[pred_idx + idx*size] += 1.0/(double)get_Block_n_cfg_outs(pred);
247       matrix[pred_idx + idx * size] += get_cf_probability(bb, i, loop_weight);
248     }
249   }
250
251   x = solve_lgs(matrix, rhs, size);
252   if(x == NULL) {
253                 ef->infeasible = 1;
254                 return ef;
255   }
256
257   ef->max = MAX_INT_FREQ;
258   set_foreach(freqs, freq) {
259     const ir_node *bb = freq->irn;
260     size_t        idx = PTR_TO_INT(get_irn_link(bb));
261
262 #ifdef USE_GSL
263     freq->freq = ZERO(gsl_vector_get(x, idx)) ? 0.0 : gsl_vector_get(x, idx);
264 #else
265     freq->freq = ZERO(x[idx]) ? 0.0 : x[idx];
266 #endif
267
268         /* get the maximum exec freq */
269         ef->max = MAX(ef->max, freq->freq);
270
271         /* Get the minimum non-zero execution frequency. */
272         if(freq->freq > 0.0)
273                 ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
274   }
275
276   /* compute m and b of the transformation used to convert the doubles into scaled ints */
277   {
278           /* double l1 = 1; */
279           double h1 = MAX_INT_FREQ;
280           double l2 = ef->min_non_zero;
281           double h2 = ef->max;
282
283           ef->m = (h1 /* - l1 */) / (h2 - l2);
284           ef->b = (/* l1 * */ h2  - l2 * h1) / (h2 - l2);
285   }
286
287 #ifdef USE_GSL
288   gsl_vector_free(x);
289 #endif
290   free(matrix);
291
292         memset(&ef->hook, 0, sizeof(ef->hook));
293         ef->hook.context = ef;
294         ef->hook.hook._hook_node_info = exec_freq_node_info;
295         register_hook(hook_node_info, &ef->hook);
296
297   return ef;
298 }
299
300 void
301 free_execfreq(exec_freq_t *ef)
302 {
303         del_set(ef->set);
304         unregister_hook(hook_node_info, &ef->hook);
305         free(ef);
306 }
307
308 #undef ELEM