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"
32 #include "firm_common_t.h"
37 #include "irgraph_t.h"
47 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
49 #define MAX_INT_FREQ 1000000
51 typedef struct _freq_t {
57 typedef struct _walkerdata_t {
68 unsigned infeasible : 1;
72 cmp_freq(const void *a, const void *b, size_t size)
77 return !(p->irn == q->irn);
81 set_find_freq(set * set, const ir_node * irn)
86 return set_find(set, &query, sizeof(query), HASH_PTR(irn));
90 set_insert_freq(set * set, const ir_node * irn)
96 return set_insert(set, &query, sizeof(query), HASH_PTR(irn));
100 get_block_execfreq(const ir_exec_freq *ef, const ir_node * irn)
102 if(!ef->infeasible) {
103 set *freqs = ef->set;
105 assert(is_Block(irn));
106 freq = set_find_freq(freqs, irn);
109 assert(freq->freq >= 0);
117 get_block_execfreq_ulong(const ir_exec_freq *ef, const ir_node *bb)
119 double f = get_block_execfreq(ef, bb);
120 int res = (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
122 // printf("%20.6f %10d\n", f, res);
126 #define EPSILON 0.0001
127 #define UNDEF(x) !(x > EPSILON)
130 block_walker(ir_node * bb, void * data)
132 walkerdata_t *wd = data;
134 set_insert_freq(wd->set, bb);
135 set_irn_link(bb, (void*)wd->idx++);
140 solve_lgs(double * a_data, double * b_data, size_t size)
143 = gsl_matrix_view_array (a_data, size, size);
146 = gsl_vector_view_array (b_data, size);
148 gsl_vector *x = gsl_vector_alloc (size);
152 gsl_permutation * p = gsl_permutation_alloc (size);
154 gsl_linalg_LU_decomp (&m.matrix, p, &s);
156 gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);
158 gsl_permutation_free (p);
164 solve_lgs(double * A, double * b, size_t size)
166 if(firm_gaussjordansolve(A,b,size) == 0) {
175 get_cf_probability(ir_node *bb, int pos, double loop_weight)
180 ir_node *pred = get_Block_cfgpred_block(bb, pos);
182 cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
184 for(i = get_Block_n_cfg_outs(pred) - 1; i >= 0; --i) {
185 ir_node *succ = get_Block_cfg_out(pred, i);
187 sum += get_loop_depth(get_irn_loop(succ)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
193 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
196 ir_exec_freq *ef = ctx;
197 fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
201 ir_exec_freq *create_execfreq(ir_graph *irg)
203 ir_exec_freq *execfreq = xmalloc(sizeof(execfreq[0]));
204 memset(execfreq, 0, sizeof(execfreq[0]));
205 execfreq->set = new_set(cmp_freq, 32);
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);
215 void set_execfreq(ir_exec_freq *execfreq, const ir_node *block, double freq)
217 freq_t *f = set_insert_freq(execfreq->set, block);
222 compute_execfreq(ir_graph * irg, double loop_weight)
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);
243 construct_cf_backedges(irg);
248 irg_block_walk_graph(irg, block_walker, NULL, &wd);
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));
256 set_foreach(freqs, freq) {
257 ir_node *bb = (ir_node *)freq->irn;
258 size_t idx = (int)get_irn_link(bb);
260 matrix[idx * (size + 1)] = -1.0;
262 if (bb == get_irg_start_block(irg)) {
263 rhs[(int)get_irn_link(bb)] = -1.0;
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);
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);
276 x = solve_lgs(matrix, rhs, size);
284 set_foreach(freqs, freq) {
285 const ir_node *bb = freq->irn;
286 size_t idx = PTR_TO_INT(get_irn_link(bb));
289 freq->freq = UNDEF(gsl_vector_get(x, idx)) ? EPSILON : gsl_vector_get(x, idx);
291 freq->freq = UNDEF(x[idx]) ? EPSILON : x[idx];
294 /* get the maximum exec freq */
295 ef->max = MAX(ef->max, freq->freq);
297 /* Get the minimum non-zero execution frequency. */
299 ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
302 /* compute m and b of the transformation used to convert the doubles into scaled ints */
304 double smallest_diff = 1.0;
306 double l2 = ef->min_non_zero;
309 double h1 = MAX_INT_FREQ;
311 double *fs = malloc(set_count(freqs) * sizeof(fs[0]));
314 set_foreach(freqs, freq)
315 fs[n++] = freq->freq;
318 * find the smallest difference of the execution frequencies
319 * we try to ressolve it with 1 integer.
321 for(i = 0; i < n; ++i) {
325 for(j = i + 1; j < n; ++j) {
326 double diff = fabs(fs[i] - fs[j]);
329 smallest_diff = MIN(diff, smallest_diff);
333 /* according to that the slope of the translation function is 1.0 / smallest diff */
334 ef->m = 1.0 / smallest_diff;
336 /* the abscissa is then given by */
337 ef->b = l1 - ef->m * l2;
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
343 if(ef->m * h2 + ef->b > MAX_INT_FREQ) {
344 ef->m = (h1 - l1) / (h2 - l2);
345 ef->b = l1 - ef->m * l2;
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);
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);
366 free_execfreq(ir_exec_freq *ef)
369 unregister_hook(hook_node_info, &ef->hook);