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)
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;
184 cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
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;
196 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *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));
204 ir_exec_freq *create_execfreq(ir_graph *irg)
206 ir_exec_freq *execfreq = xmalloc(sizeof(execfreq[0]));
207 memset(execfreq, 0, sizeof(execfreq[0]));
208 execfreq->set = new_set(cmp_freq, 32);
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);
218 void set_execfreq(ir_exec_freq *execfreq, const ir_node *block, double freq)
220 freq_t *f = set_insert_freq(execfreq->set, block);
225 compute_execfreq(ir_graph * irg, double loop_weight)
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);
246 construct_cf_backedges(irg);
252 irg_block_walk_graph(irg, block_walker, NULL, &wd);
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));
260 set_foreach(freqs, freq) {
261 ir_node *bb = (ir_node *)freq->irn;
262 size_t idx = (int)get_irn_link(bb);
264 matrix[idx * (size + 1)] = -1.0;
266 if (bb == get_irg_start_block(irg)) {
267 rhs[(int)get_irn_link(bb)] = -1.0;
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);
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);
280 x = solve_lgs(matrix, rhs, size);
288 set_foreach(freqs, freq) {
289 const ir_node *bb = freq->irn;
290 size_t idx = PTR_TO_INT(get_irn_link(bb));
293 freq->freq = UNDEF(gsl_vector_get(x, idx)) ? EPSILON : gsl_vector_get(x, idx);
295 freq->freq = UNDEF(x[idx]) ? EPSILON : x[idx];
298 /* get the maximum exec freq */
299 ef->max = MAX(ef->max, freq->freq);
301 /* Get the minimum non-zero execution frequency. */
303 ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
306 /* compute m and b of the transformation used to convert the doubles into scaled ints */
308 double smallest_diff = 1.0;
310 double l2 = ef->min_non_zero;
313 double h1 = MAX_INT_FREQ;
315 double *fs = malloc(set_count(freqs) * sizeof(fs[0]));
318 set_foreach(freqs, freq)
319 fs[n++] = freq->freq;
322 * find the smallest difference of the execution frequencies
323 * we try to ressolve it with 1 integer.
325 for(i = 0; i < n; ++i) {
329 for(j = i + 1; j < n; ++j) {
330 double diff = fabs(fs[i] - fs[j]);
333 smallest_diff = MIN(diff, smallest_diff);
337 /* according to that the slope of the translation function is 1.0 / smallest diff */
338 ef->m = 1.0 / smallest_diff;
340 /* the abscissa is then given by */
341 ef->b = l1 - ef->m * l2;
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
347 if(ef->m * h2 + ef->b > MAX_INT_FREQ) {
348 ef->m = (h1 - l1) / (h2 - l2);
349 ef->b = l1 - ef->m * l2;
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);
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);
370 free_execfreq(ir_exec_freq *ef)
373 unregister_hook(hook_node_info, &ef->hook);