3 * File name: ir/ana/execfreq.c
4 * Purpose: Compute an estimate of basic block executions.
5 * Author: Adam M. Szalkowski
9 * Copyright: (c) 2006 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
24 #include <gsl/gsl_linalg.h>
25 #include <gsl/gsl_vector.h>
27 #include "gaussjordan.h"
30 #include "firm_common_t.h"
35 #include "irgraph_t.h"
45 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
47 #define MAX_INT_FREQ 1000000
49 typedef struct _freq_t {
55 typedef struct _walkerdata_t {
66 unsigned infeasible : 1;
70 cmp_freq(const void *a, const void *b, size_t size)
75 return !(p->irn == q->irn);
79 set_find_freq(set * set, const ir_node * irn)
84 return set_find(set, &query, sizeof(query), HASH_PTR(irn));
88 set_insert_freq(set * set, const ir_node * irn)
94 return set_insert(set, &query, sizeof(query), HASH_PTR(irn));
98 get_block_execfreq(const ir_exec_freq *ef, const ir_node * irn)
100 if(!ef->infeasible) {
101 set *freqs = ef->set;
103 assert(is_Block(irn));
104 freq = set_find_freq(freqs, irn);
107 assert(freq->freq >= 0);
115 get_block_execfreq_ulong(const ir_exec_freq *ef, const ir_node *bb)
117 double f = get_block_execfreq(ef, bb);
118 int res = (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
120 // printf("%20.6f %10d\n", f, res);
124 #define EPSILON 0.0001
125 #define UNDEF(x) !(x > EPSILON)
128 block_walker(ir_node * bb, void * data)
130 walkerdata_t *wd = data;
132 set_insert_freq(wd->set, bb);
133 set_irn_link(bb, (void*)wd->idx++);
138 solve_lgs(double * a_data, double * b_data, size_t size)
141 = gsl_matrix_view_array (a_data, size, size);
144 = gsl_vector_view_array (b_data, size);
146 gsl_vector *x = gsl_vector_alloc (size);
150 gsl_permutation * p = gsl_permutation_alloc (size);
152 gsl_linalg_LU_decomp (&m.matrix, p, &s);
154 gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);
156 gsl_permutation_free (p);
162 solve_lgs(double * A, double * b, size_t size)
164 if(firm_gaussjordansolve(A,b,size) == 0) {
173 get_cf_probability(ir_node *bb, int pos, double loop_weight)
177 const ir_node *pred = get_Block_cfgpred_block(bb, pos);
178 const ir_loop *pred_loop = get_irn_loop(pred);
179 int pred_depth = get_loop_depth(pred_loop);
180 const ir_edge_t *edge;
182 cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
184 foreach_block_succ(pred, edge) {
185 const ir_node *block = get_edge_src_irn(edge);
186 const ir_loop *loop = get_irn_loop(block);
187 int depth = get_loop_depth(loop);
188 sum += depth < pred_depth ? 1.0 : loop_weight;
194 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
197 ir_exec_freq *ef = ctx;
198 fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
202 ir_exec_freq *create_execfreq(ir_graph *irg)
204 ir_exec_freq *execfreq = xmalloc(sizeof(execfreq[0]));
205 memset(execfreq, 0, sizeof(execfreq[0]));
206 execfreq->set = new_set(cmp_freq, 32);
208 memset(&execfreq->hook, 0, sizeof(execfreq->hook));
209 execfreq->hook.context = execfreq;
210 execfreq->hook.hook._hook_node_info = exec_freq_node_info;
211 register_hook(hook_node_info, &execfreq->hook);
216 void set_execfreq(ir_exec_freq *execfreq, const ir_node *block, double freq)
218 freq_t *f = set_insert_freq(execfreq->set, block);
223 compute_execfreq(ir_graph * irg, double loop_weight)
239 ef = xmalloc(sizeof(ef[0]));
240 memset(ef, 0, sizeof(ef[0]));
241 ef->min_non_zero = 1e50; /* initialize with a reasonable large number. */
242 freqs = ef->set = new_set(cmp_freq, 32);
244 construct_cf_backedges(irg);
250 irg_block_walk_graph(irg, block_walker, NULL, &wd);
252 size = set_count(freqs);
253 matrix = xmalloc(size*size*sizeof(*matrix));
254 memset(matrix, 0, size*size*sizeof(*matrix));
255 rhs = xmalloc(size*sizeof(*rhs));
256 memset(rhs, 0, size*sizeof(*rhs));
258 set_foreach(freqs, freq) {
259 ir_node *bb = (ir_node *)freq->irn;
260 size_t idx = (int)get_irn_link(bb);
262 matrix[idx * (size + 1)] = -1.0;
264 if (bb == get_irg_start_block(irg)) {
265 rhs[(int)get_irn_link(bb)] = -1.0;
269 for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
270 ir_node *pred = get_Block_cfgpred_block(bb, i);
271 size_t pred_idx = (int)get_irn_link(pred);
273 // matrix[pred_idx + idx*size] += 1.0/(double)get_Block_n_cfg_outs(pred);
274 matrix[pred_idx + idx * size] += get_cf_probability(bb, i, loop_weight);
278 x = solve_lgs(matrix, rhs, size);
286 set_foreach(freqs, freq) {
287 const ir_node *bb = freq->irn;
288 size_t idx = PTR_TO_INT(get_irn_link(bb));
291 freq->freq = UNDEF(gsl_vector_get(x, idx)) ? EPSILON : gsl_vector_get(x, idx);
293 freq->freq = UNDEF(x[idx]) ? EPSILON : x[idx];
296 /* get the maximum exec freq */
297 ef->max = MAX(ef->max, freq->freq);
299 /* Get the minimum non-zero execution frequency. */
301 ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
304 /* compute m and b of the transformation used to convert the doubles into scaled ints */
306 double smallest_diff = 1.0;
308 double l2 = ef->min_non_zero;
311 double h1 = MAX_INT_FREQ;
313 double *fs = malloc(set_count(freqs) * sizeof(fs[0]));
316 set_foreach(freqs, freq)
317 fs[n++] = freq->freq;
320 * find the smallest difference of the execution frequencies
321 * we try to ressolve it with 1 integer.
323 for(i = 0; i < n; ++i) {
327 for(j = i + 1; j < n; ++j) {
328 double diff = fabs(fs[i] - fs[j]);
331 smallest_diff = MIN(diff, smallest_diff);
335 /* according to that the slope of the translation function is 1.0 / smallest diff */
336 ef->m = 1.0 / smallest_diff;
338 /* the abscissa is then given by */
339 ef->b = l1 - ef->m * l2;
342 * if the slope is so high that the largest integer would be larger than MAX_INT_FREQ
343 * set the largest int freq to that upper limit and recompute the translation function
345 if(ef->m * h2 + ef->b > MAX_INT_FREQ) {
346 ef->m = (h1 - l1) / (h2 - l2);
347 ef->b = l1 - ef->m * l2;
350 // 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);
359 memset(&ef->hook, 0, sizeof(ef->hook));
360 ef->hook.context = ef;
361 ef->hook.hook._hook_node_info = exec_freq_node_info;
362 register_hook(hook_node_info, &ef->hook);
368 free_execfreq(ir_exec_freq *ef)
371 unregister_hook(hook_node_info, &ef->hook);