fixed output
[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 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #undef USE_GSL
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <limits.h>
21 #include <math.h>
22
23 #ifdef USE_GSL
24 #include <gsl/gsl_linalg.h>
25 #include <gsl/gsl_vector.h>
26 #else
27 #include "gaussjordan.h"
28 #endif
29
30 #include "execfreq.h"
31
32 #include "firm_common_t.h"
33 #include "set.h"
34 #include "hashptr.h"
35
36 #include "irprog_t.h"
37 #include "irgraph_t.h"
38 #include "irnode_t.h"
39 #include "irloop.h"
40 #include "irgwalk.h"
41 #include "iredges.h"
42 #include "irprintf.h"
43 #include "irhooks.h"
44
45 #include "execfreq.h"
46
47 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
48
49 #define MAX_INT_FREQ 1000000
50
51 typedef struct _freq_t {
52         const ir_node    *irn;
53         double            freq;
54 } freq_t;
55
56
57 typedef struct _walkerdata_t {
58   set    *set;
59   size_t  idx;
60 } walkerdata_t;
61
62 struct ir_exec_freq {
63         set *set;
64         hook_entry_t hook;
65         double max;
66         double min_non_zero;
67         double m, b;
68         unsigned infeasible : 1;
69 };
70
71 static int
72 cmp_freq(const void *a, const void *b, size_t size)
73 {
74         const freq_t *p = a;
75         const freq_t *q = b;
76
77         return !(p->irn == q->irn);
78 }
79
80 static freq_t *
81 set_find_freq(set * set, const ir_node * irn)
82 {
83         freq_t     query;
84
85         query.irn = irn;
86         return set_find(set, &query, sizeof(query), HASH_PTR(irn));
87 }
88
89 static freq_t *
90 set_insert_freq(set * set, const ir_node * irn)
91 {
92         freq_t query;
93
94         query.irn = irn;
95         query.freq = 0.0;
96         return set_insert(set, &query, sizeof(query), HASH_PTR(irn));
97 }
98
99 double
100 get_block_execfreq(const ir_exec_freq *ef, const ir_node * irn)
101 {
102         if(!ef->infeasible) {
103                 set *freqs = ef->set;
104                 freq_t *freq;
105                 assert(is_Block(irn));
106                 freq = set_find_freq(freqs, irn);
107                 assert(freq);
108
109                 assert(freq->freq >= 0);
110                 return freq->freq;
111         }
112
113         return 1.0;
114 }
115
116 unsigned long
117 get_block_execfreq_ulong(const ir_exec_freq *ef, const ir_node *bb)
118 {
119         double f       = get_block_execfreq(ef, bb);
120         int res        = (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
121
122         // printf("%20.6f %10d\n", f, res);
123         return res;
124 }
125
126 #define EPSILON         0.0001
127 #define UNDEF(x)    !(x > EPSILON)
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         const ir_node *pred = get_Block_cfgpred_block(bb, pos);
180         const ir_loop *pred_loop = get_irn_loop(pred);
181         int pred_depth = get_loop_depth(pred_loop);
182         const ir_edge_t *edge;
183
184         cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
185
186         foreach_block_succ(pred, edge) {
187                 const ir_node *block = get_edge_src_irn(edge);
188                 const ir_loop *loop = get_irn_loop(block);
189                 int depth = get_loop_depth(loop);
190                 sum += depth < pred_depth ? 1.0 : loop_weight;
191         }
192
193         return cur/sum;
194 }
195
196 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
197 {
198         if(is_Block(irn)) {
199                 ir_exec_freq *ef = ctx;
200                 fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
201         }
202 }
203
204 ir_exec_freq *create_execfreq(ir_graph *irg)
205 {
206         ir_exec_freq *execfreq = xmalloc(sizeof(execfreq[0]));
207         memset(execfreq, 0, sizeof(execfreq[0]));
208         execfreq->set = new_set(cmp_freq, 32);
209
210         memset(&execfreq->hook, 0, sizeof(execfreq->hook));
211         execfreq->hook.context = execfreq;
212         execfreq->hook.hook._hook_node_info = exec_freq_node_info;
213         register_hook(hook_node_info, &execfreq->hook);
214
215         return execfreq;
216 }
217
218 void set_execfreq(ir_exec_freq *execfreq, const ir_node *block, double freq)
219 {
220         freq_t *f = set_insert_freq(execfreq->set, block);
221         f->freq = freq;
222 }
223
224 ir_exec_freq *
225 compute_execfreq(ir_graph * irg, double loop_weight)
226 {
227         size_t        size;
228         double       *matrix;
229         double       *rhs;
230         int           i;
231         freq_t       *freq;
232         walkerdata_t  wd;
233         ir_exec_freq  *ef;
234         set          *freqs;
235 #ifdef USE_GSL
236         gsl_vector   *x;
237 #else
238         double       *x;
239 #endif
240
241         ef = xmalloc(sizeof(ef[0]));
242         memset(ef, 0, sizeof(ef[0]));
243         ef->min_non_zero = 1e50; /* initialize with a reasonable large number. */
244         freqs = ef->set = new_set(cmp_freq, 32);
245
246         construct_cf_backedges(irg);
247         edges_assure(irg);
248
249         wd.idx = 0;
250         wd.set = freqs;
251
252         irg_block_walk_graph(irg, block_walker, NULL, &wd);
253
254         size = set_count(freqs);
255         matrix = xmalloc(size*size*sizeof(*matrix));
256         memset(matrix, 0, size*size*sizeof(*matrix));
257         rhs = xmalloc(size*sizeof(*rhs));
258         memset(rhs, 0, size*sizeof(*rhs));
259
260         set_foreach(freqs, freq) {
261                 ir_node *bb = (ir_node *)freq->irn;
262                 size_t  idx = (int)get_irn_link(bb);
263
264                 matrix[idx * (size + 1)] = -1.0;
265
266                 if (bb == get_irg_start_block(irg)) {
267                         rhs[(int)get_irn_link(bb)] = -1.0;
268                         continue;
269                 }
270
271                 for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
272                         ir_node *pred    = get_Block_cfgpred_block(bb, i);
273                         size_t  pred_idx = (int)get_irn_link(pred);
274
275                         //      matrix[pred_idx + idx*size] += 1.0/(double)get_Block_n_cfg_outs(pred);
276                         matrix[pred_idx + idx * size] += get_cf_probability(bb, i, loop_weight);
277                 }
278         }
279
280         x = solve_lgs(matrix, rhs, size);
281         if(x == NULL) {
282                 ef->infeasible = 1;
283                 return ef;
284         }
285
286         ef->max = 0.0;
287
288         set_foreach(freqs, freq) {
289                 const ir_node *bb = freq->irn;
290                 size_t        idx = PTR_TO_INT(get_irn_link(bb));
291
292 #ifdef USE_GSL
293                 freq->freq = UNDEF(gsl_vector_get(x, idx)) ? EPSILON : gsl_vector_get(x, idx);
294 #else
295                 freq->freq = UNDEF(x[idx]) ? EPSILON : x[idx];
296 #endif
297
298                 /* get the maximum exec freq */
299                 ef->max = MAX(ef->max, freq->freq);
300
301                 /* Get the minimum non-zero execution frequency. */
302                 if(freq->freq > 0.0)
303                         ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
304         }
305
306         /* compute m and b of the transformation used to convert the doubles into scaled ints */
307         {
308                 double smallest_diff = 1.0;
309
310                 double l2 = ef->min_non_zero;
311                 double h2 = ef->max;
312                 double l1 = 1.0;
313                 double h1 = MAX_INT_FREQ;
314
315                 double *fs = malloc(set_count(freqs) * sizeof(fs[0]));
316                 int i, j, n = 0;
317
318                 set_foreach(freqs, freq)
319                         fs[n++] = freq->freq;
320
321                 /*
322                  * find the smallest difference of the execution frequencies
323                  * we try to ressolve it with 1 integer.
324                  */
325                 for(i = 0; i < n; ++i) {
326                         if(fs[i] <= 0.0)
327                                 continue;
328
329                         for(j = i + 1; j < n; ++j) {
330                                 double diff = fabs(fs[i] - fs[j]);
331
332                                 if(!UNDEF(diff))
333                                         smallest_diff = MIN(diff, smallest_diff);
334                         }
335                 }
336
337                 /* according to that the slope of the translation function is 1.0 / smallest diff */
338                 ef->m = 1.0 / smallest_diff;
339
340                 /* the abscissa is then given by */
341                 ef->b = l1 - ef->m * l2;
342
343                 /*
344                  * if the slope is so high that the largest integer would be larger than MAX_INT_FREQ
345                  * set the largest int freq to that upper limit and recompute the translation function
346                  */
347                 if(ef->m * h2 + ef->b > MAX_INT_FREQ) {
348                         ef->m = (h1 - l1) / (h2 - l2);
349                         ef->b = l1 - ef->m * l2;
350                 }
351
352                 // 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);
353                 free(fs);
354         }
355
356 #ifdef USE_GSL
357         gsl_vector_free(x);
358 #endif
359         free(matrix);
360
361         memset(&ef->hook, 0, sizeof(ef->hook));
362         ef->hook.context = ef;
363         ef->hook.hook._hook_node_info = exec_freq_node_info;
364         register_hook(hook_node_info, &ef->hook);
365
366         return ef;
367 }
368
369 void
370 free_execfreq(ir_exec_freq *ef)
371 {
372         del_set(ef->set);
373         unregister_hook(hook_node_info, &ef->hook);
374         free(ef);
375 }
376
377 #undef ELEM