add docu and prototype for find_value()
[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 #undef 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
110                 assert(freq->freq >= 0);
111                 return freq->freq;
112         }
113
114         return 1.0;
115 }
116
117 unsigned long
118 get_block_execfreq_ulong(const exec_freq_t *ef, const ir_node *bb)
119 {
120         double f       = get_block_execfreq(ef, bb);
121         int res        = (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
122
123         // printf("%20.6f %10d\n", f, res);
124         return res;
125 }
126
127 #define ZERO(x)   (fabs(x) < 0.0001)
128
129 static void
130 block_walker(ir_node * bb, void * data)
131 {
132   walkerdata_t  *wd = data;
133
134   set_insert_freq(wd->set, bb);
135   set_irn_link(bb, (void*)wd->idx++);
136 }
137
138 #ifdef USE_GSL
139 static gsl_vector *
140 solve_lgs(double * a_data, double * b_data, size_t size)
141 {
142   gsl_matrix_view m
143     = gsl_matrix_view_array (a_data, size, size);
144
145   gsl_vector_view b
146     = gsl_vector_view_array (b_data, size);
147
148   gsl_vector *x = gsl_vector_alloc (size);
149
150   int s;
151
152   gsl_permutation * p = gsl_permutation_alloc (size);
153
154   gsl_linalg_LU_decomp (&m.matrix, p, &s);
155
156   gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);
157
158   gsl_permutation_free (p);
159
160   return x;
161 }
162 #else
163 static double *
164 solve_lgs(double * A, double * b, size_t size)
165 {
166   if(firm_gaussjordansolve(A,b,size) == 0) {
167     return b;
168   } else {
169     return NULL;
170   }
171 }
172 #endif
173
174 static double
175 get_cf_probability(ir_node *bb, int pos, double loop_weight)
176 {
177   double  sum = 0.0;
178   double  cur = 0.0;
179   int     i;
180   ir_node *pred = get_Block_cfgpred_block(bb, pos);
181
182   cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
183
184   for(i = get_Block_n_cfg_outs(pred) - 1; i >= 0; --i) {
185     ir_node *succ = get_Block_cfg_out(pred, i);
186
187     sum += get_loop_depth(get_irn_loop(succ)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
188   }
189
190   return cur/sum;
191 }
192
193 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
194 {
195         if(is_Block(irn)) {
196                 exec_freq_t *ef = ctx;
197                 fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
198         }
199 }
200
201 exec_freq_t *create_execfreq(ir_graph *irg)
202 {
203         exec_freq_t *execfreq = xmalloc(sizeof(execfreq[0]));
204         memset(execfreq, 0, sizeof(execfreq[0]));
205         execfreq->set = new_set(cmp_freq, 32);
206
207         memset(&execfreq->hook, 0, sizeof(execfreq->hook));
208         execfreq->hook.context = execfreq;
209         execfreq->hook.hook._hook_node_info = exec_freq_node_info;
210         register_hook(hook_node_info, &execfreq->hook);
211
212         return execfreq;
213 }
214
215 void set_execfreq(exec_freq_t *execfreq, const ir_node *block, double freq)
216 {
217         freq_t *f = set_insert_freq(execfreq->set, block);
218         f->freq = freq;
219 }
220
221 exec_freq_t *
222 compute_execfreq(ir_graph * irg, double loop_weight)
223 {
224         size_t        size;
225         double       *matrix;
226         double       *rhs;
227         int           i;
228         freq_t       *freq;
229         walkerdata_t  wd;
230         exec_freq_t  *ef;
231         set          *freqs;
232 #ifdef USE_GSL
233         gsl_vector   *x;
234 #else
235         double       *x;
236 #endif
237
238         ef = xmalloc(sizeof(ef[0]));
239         memset(ef, 0, sizeof(ef[0]));
240         ef->min_non_zero = 1e50; /* initialize with a reasonable large number. */
241         freqs = ef->set = new_set(cmp_freq, 32);
242
243         construct_cf_backedges(irg);
244
245         wd.idx = 0;
246         wd.set = freqs;
247
248         irg_block_walk_graph(irg, block_walker, NULL, &wd);
249
250         size = set_count(freqs);
251         matrix = xmalloc(size*size*sizeof(*matrix));
252         memset(matrix, 0, size*size*sizeof(*matrix));
253         rhs = xmalloc(size*sizeof(*rhs));
254         memset(rhs, 0, size*sizeof(*rhs));
255
256         set_foreach(freqs, freq) {
257                 ir_node *bb = (ir_node *)freq->irn;
258                 size_t  idx = (int)get_irn_link(bb);
259
260                 matrix[idx * (size + 1)] = -1.0;
261
262                 if (bb == get_irg_start_block(irg)) {
263                         rhs[(int)get_irn_link(bb)] = -1.0;
264                         continue;
265                 }
266
267                 for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
268                         ir_node *pred    = get_Block_cfgpred_block(bb, i);
269                         size_t  pred_idx = (int)get_irn_link(pred);
270
271                         //      matrix[pred_idx + idx*size] += 1.0/(double)get_Block_n_cfg_outs(pred);
272                         matrix[pred_idx + idx * size] += get_cf_probability(bb, i, loop_weight);
273                 }
274         }
275
276         x = solve_lgs(matrix, rhs, size);
277         if(x == NULL) {
278                 ef->infeasible = 1;
279                 return ef;
280         }
281
282         ef->max = 0.0;
283
284         set_foreach(freqs, freq) {
285                 const ir_node *bb = freq->irn;
286                 size_t        idx = PTR_TO_INT(get_irn_link(bb));
287
288 #ifdef USE_GSL
289                 freq->freq = ZERO(gsl_vector_get(x, idx)) ? 0.0 : gsl_vector_get(x, idx);
290 #else
291                 freq->freq = ZERO(x[idx]) ? 0.0 : x[idx];
292 #endif
293
294                 /* get the maximum exec freq */
295                 ef->max = MAX(ef->max, freq->freq);
296
297                 /* Get the minimum non-zero execution frequency. */
298                 if(freq->freq > 0.0)
299                         ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
300         }
301
302         /* compute m and b of the transformation used to convert the doubles into scaled ints */
303         {
304                 double smallest_diff = 1.0;
305
306                 double l2 = ef->min_non_zero;
307                 double h2 = ef->max;
308                 double l1 = 1.0;
309                 double h1 = MAX_INT_FREQ;
310
311                 double *fs = malloc(set_count(freqs) * sizeof(fs[0]));
312                 int i, j, n = 0;
313
314                 set_foreach(freqs, freq)
315                         fs[n++] = freq->freq;
316
317                 /*
318                  * find the smallest difference of the execution frequencies
319                  * we try to ressolve it with 1 integer.
320                  */
321                 for(i = 0; i < n; ++i) {
322                         if(fs[i] <= 0.0)
323                                 continue;
324
325                         for(j = i + 1; j < n; ++j) {
326                                 double diff = fabs(fs[i] - fs[j]);
327
328                                 if(!ZERO(diff))
329                                         smallest_diff = MIN(diff, smallest_diff);
330                         }
331                 }
332
333                 /* according to that the slope of the translation function is 1.0 / smallest diff */
334                 ef->m = 1.0 / smallest_diff;
335
336                 /* the abscissa is then given by */
337                 ef->b = l1 - ef->m * l2;
338
339                 /*
340                  * if the slope is so high that the largest integer would be larger than MAX_INT_FREQ
341                  * set the largest int freq to that upper limit and recompute the translation function
342                  */
343                 if(ef->m * h2 + ef->b > MAX_INT_FREQ) {
344                         ef->m = (h1 - l1) / (h2 - l2);
345                         ef->b = l1 - ef->m * l2;
346                 }
347
348                 // 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);
349                 free(fs);
350         }
351
352 #ifdef USE_GSL
353         gsl_vector_free(x);
354 #endif
355         free(matrix);
356
357         memset(&ef->hook, 0, sizeof(ef->hook));
358         ef->hook.context = ef;
359         ef->hook.hook._hook_node_info = exec_freq_node_info;
360         register_hook(hook_node_info, &ef->hook);
361
362         return ef;
363 }
364
365 void
366 free_execfreq(exec_freq_t *ef)
367 {
368         del_set(ef->set);
369         unregister_hook(hook_node_info, &ef->hook);
370         free(ef);
371 }
372
373 #undef ELEM