create execution frequencies from profile data
[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         return (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
122 }
123
124 #define ZERO(x)   (fabs(x) < 0.0001)
125
126 static void
127 block_walker(ir_node * bb, void * data)
128 {
129   walkerdata_t  *wd = data;
130
131   set_insert_freq(wd->set, bb);
132   set_irn_link(bb, (void*)wd->idx++);
133 }
134
135 #ifdef USE_GSL
136 static gsl_vector *
137 solve_lgs(double * a_data, double * b_data, size_t size)
138 {
139   gsl_matrix_view m
140     = gsl_matrix_view_array (a_data, size, size);
141
142   gsl_vector_view b
143     = gsl_vector_view_array (b_data, size);
144
145   gsl_vector *x = gsl_vector_alloc (size);
146
147   int s;
148
149   gsl_permutation * p = gsl_permutation_alloc (size);
150
151   gsl_linalg_LU_decomp (&m.matrix, p, &s);
152
153   gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);
154
155   gsl_permutation_free (p);
156
157   return x;
158 }
159 #else
160 static double *
161 solve_lgs(double * A, double * b, size_t size)
162 {
163   if(firm_gaussjordansolve(A,b,size) == 0) {
164     return b;
165   } else {
166     return NULL;
167   }
168 }
169 #endif
170
171 static double
172 get_cf_probability(ir_node *bb, int pos, double loop_weight)
173 {
174   double  sum = 0.0;
175   double  cur = 0.0;
176   int     i;
177   ir_node *pred = get_Block_cfgpred_block(bb, pos);
178
179   cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
180
181   for(i = get_Block_n_cfg_outs(pred) - 1; i >= 0; --i) {
182     ir_node *succ = get_Block_cfg_out(pred, i);
183
184     sum += get_loop_depth(get_irn_loop(succ)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
185   }
186
187   return cur/sum;
188 }
189
190 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
191 {
192         if(is_Block(irn)) {
193                 exec_freq_t *ef = ctx;
194                 fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
195         }
196 }
197
198 exec_freq_t *create_execfreq(ir_graph *irg)
199 {
200         exec_freq_t *execfreq = xmalloc(sizeof(execfreq[0]));
201         memset(execfreq, 0, sizeof(execfreq[0]));
202         execfreq->set = new_set(cmp_freq, 32);
203
204         memset(&execfreq->hook, 0, sizeof(execfreq->hook));
205         execfreq->hook.context = execfreq;
206         execfreq->hook.hook._hook_node_info = exec_freq_node_info;
207         register_hook(hook_node_info, &execfreq->hook);
208
209         return execfreq;
210 }
211
212 void set_execfreq(exec_freq_t *execfreq, const ir_node *block, double freq)
213 {
214         freq_t *f = set_insert_freq(execfreq->set, block);
215         f->freq = freq;
216 }
217
218 exec_freq_t *
219 compute_execfreq(ir_graph * irg, double loop_weight)
220 {
221         size_t        size;
222         double       *matrix;
223         double       *rhs;
224         int           i;
225         freq_t       *freq;
226         walkerdata_t  wd;
227         exec_freq_t  *ef;
228         set          *freqs;
229 #ifdef USE_GSL
230         gsl_vector   *x;
231 #else
232         double       *x;
233 #endif
234
235         ef = xmalloc(sizeof(ef[0]));
236         memset(ef, 0, sizeof(ef[0]));
237         ef->min_non_zero = 1e50; /* initialize with a reasonable large number. */
238         freqs = ef->set = new_set(cmp_freq, 32);
239
240         construct_cf_backedges(irg);
241
242         wd.idx = 0;
243         wd.set = freqs;
244
245         irg_block_walk_graph(irg, block_walker, NULL, &wd);
246
247         size = set_count(freqs);
248         matrix = xmalloc(size*size*sizeof(*matrix));
249         memset(matrix, 0, size*size*sizeof(*matrix));
250         rhs = xmalloc(size*sizeof(*rhs));
251         memset(rhs, 0, size*sizeof(*rhs));
252
253         set_foreach(freqs, freq) {
254                 ir_node *bb = (ir_node *)freq->irn;
255                 size_t  idx = (int)get_irn_link(bb);
256
257                 matrix[idx * (size + 1)] = -1.0;
258
259                 if (bb == get_irg_start_block(irg)) {
260                         rhs[(int)get_irn_link(bb)] = -1.0;
261                         continue;
262                 }
263
264                 for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
265                         ir_node *pred    = get_Block_cfgpred_block(bb, i);
266                         size_t  pred_idx = (int)get_irn_link(pred);
267
268                         //      matrix[pred_idx + idx*size] += 1.0/(double)get_Block_n_cfg_outs(pred);
269                         matrix[pred_idx + idx * size] += get_cf_probability(bb, i, loop_weight);
270                 }
271         }
272
273         x = solve_lgs(matrix, rhs, size);
274         if(x == NULL) {
275                 ef->infeasible = 1;
276                 return ef;
277         }
278
279         ef->max = MAX_INT_FREQ;
280         set_foreach(freqs, freq) {
281                 const ir_node *bb = freq->irn;
282                 size_t        idx = PTR_TO_INT(get_irn_link(bb));
283
284 #ifdef USE_GSL
285                 freq->freq = ZERO(gsl_vector_get(x, idx)) ? 0.0 : gsl_vector_get(x, idx);
286 #else
287                 freq->freq = ZERO(x[idx]) ? 0.0 : x[idx];
288 #endif
289
290                 /* get the maximum exec freq */
291                 ef->max = MAX(ef->max, freq->freq);
292
293                 /* Get the minimum non-zero execution frequency. */
294                 if(freq->freq > 0.0)
295                         ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
296         }
297
298         /* compute m and b of the transformation used to convert the doubles into scaled ints */
299         {
300                 double l1 = 1.0;
301                 double h1 = MAX_INT_FREQ;
302                 double l2 = ef->min_non_zero;
303                 double h2 = ef->max;
304
305                 ef->m = (h1 - l1) / (h2 - l2);
306                 ef->b = (l1 * h2  - l2 * h1) / (h2 - l2);
307         }
308
309 #ifdef USE_GSL
310         gsl_vector_free(x);
311 #endif
312         free(matrix);
313
314         memset(&ef->hook, 0, sizeof(ef->hook));
315         ef->hook.context = ef;
316         ef->hook.hook._hook_node_info = exec_freq_node_info;
317         register_hook(hook_node_info, &ef->hook);
318
319         return ef;
320 }
321
322 void
323 free_execfreq(exec_freq_t *ef)
324 {
325         del_set(ef->set);
326         unregister_hook(hook_node_info, &ef->hook);
327         free(ef);
328 }
329
330 #undef ELEM