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.
25 #include <gsl/gsl_linalg.h>
26 #include <gsl/gsl_vector.h>
28 #include "gaussjordan.h"
33 #include "firm_common_t.h"
38 #include "irgraph_t.h"
48 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
50 #define MAX_INT_FREQ 1000000
52 typedef struct _freq_t {
58 typedef struct _walkerdata_t {
69 unsigned infeasible : 1;
73 cmp_freq(const void *a, const void *b, size_t size)
78 return !(p->irn == q->irn);
82 set_find_freq(set * set, const ir_node * irn)
87 return set_find(set, &query, sizeof(query), HASH_PTR(irn));
91 set_insert_freq(set * set, const ir_node * irn)
97 return set_insert(set, &query, sizeof(query), HASH_PTR(irn));
101 get_block_execfreq(const exec_freq_t *ef, const ir_node * irn)
103 if(!ef->infeasible) {
104 set *freqs = ef->set;
106 assert(is_Block(irn));
107 freq = set_find_freq(freqs, irn);
110 assert(freq->freq >= 0);
118 get_block_execfreq_ulong(const exec_freq_t *ef, const ir_node *bb)
120 double f = get_block_execfreq(ef, bb);
121 return (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
124 #define ZERO(x) (fabs(x) < 0.0001)
127 block_walker(ir_node * bb, void * data)
129 walkerdata_t *wd = data;
131 set_insert_freq(wd->set, bb);
132 set_irn_link(bb, (void*)wd->idx++);
137 solve_lgs(double * a_data, double * b_data, size_t size)
140 = gsl_matrix_view_array (a_data, size, size);
143 = gsl_vector_view_array (b_data, size);
145 gsl_vector *x = gsl_vector_alloc (size);
149 gsl_permutation * p = gsl_permutation_alloc (size);
151 gsl_linalg_LU_decomp (&m.matrix, p, &s);
153 gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);
155 gsl_permutation_free (p);
161 solve_lgs(double * A, double * b, size_t size)
163 if(firm_gaussjordansolve(A,b,size) == 0) {
172 get_cf_probability(ir_node *bb, int pos, double loop_weight)
177 ir_node *pred = get_Block_cfgpred_block(bb, pos);
179 cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
181 for(i = get_Block_n_cfg_outs(pred) - 1; i >= 0; --i) {
182 ir_node *succ = get_Block_cfg_out(pred, i);
184 sum += get_loop_depth(get_irn_loop(succ)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
190 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *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));
198 exec_freq_t *create_execfreq(ir_graph *irg)
200 exec_freq_t *execfreq = xmalloc(sizeof(execfreq[0]));
201 memset(execfreq, 0, sizeof(execfreq[0]));
202 execfreq->set = new_set(cmp_freq, 32);
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);
212 void set_execfreq(exec_freq_t *execfreq, const ir_node *block, double freq)
214 freq_t *f = set_insert_freq(execfreq->set, block);
219 compute_execfreq(ir_graph * irg, double loop_weight)
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);
240 construct_cf_backedges(irg);
245 irg_block_walk_graph(irg, block_walker, NULL, &wd);
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));
253 set_foreach(freqs, freq) {
254 ir_node *bb = (ir_node *)freq->irn;
255 size_t idx = (int)get_irn_link(bb);
257 matrix[idx * (size + 1)] = -1.0;
259 if (bb == get_irg_start_block(irg)) {
260 rhs[(int)get_irn_link(bb)] = -1.0;
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);
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);
273 x = solve_lgs(matrix, rhs, size);
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));
285 freq->freq = ZERO(gsl_vector_get(x, idx)) ? 0.0 : gsl_vector_get(x, idx);
287 freq->freq = ZERO(x[idx]) ? 0.0 : x[idx];
290 /* get the maximum exec freq */
291 ef->max = MAX(ef->max, freq->freq);
293 /* Get the minimum non-zero execution frequency. */
295 ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
298 /* compute m and b of the transformation used to convert the doubles into scaled ints */
301 double h1 = MAX_INT_FREQ;
302 double l2 = ef->min_non_zero;
305 ef->m = (h1 - l1) / (h2 - l2);
306 ef->b = (l1 * h2 - l2 * h1) / (h2 - l2);
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);
323 free_execfreq(exec_freq_t *ef)
326 unregister_hook(hook_node_info, &ef->hook);