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);
116 get_block_execfreq_ulong(const exec_freq_t *ef, const ir_node *bb)
118 double f = get_block_execfreq(ef, bb);
119 return (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
122 #define ZERO(x) (fabs(x) < 0.0001)
125 block_walker(ir_node * bb, void * data)
127 walkerdata_t *wd = data;
129 set_insert_freq(wd->set, bb);
130 set_irn_link(bb, (void*)wd->idx++);
135 solve_lgs(double * a_data, double * b_data, size_t size)
138 = gsl_matrix_view_array (a_data, size, size);
141 = gsl_vector_view_array (b_data, size);
143 gsl_vector *x = gsl_vector_alloc (size);
147 gsl_permutation * p = gsl_permutation_alloc (size);
149 gsl_linalg_LU_decomp (&m.matrix, p, &s);
151 gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);
153 gsl_permutation_free (p);
159 solve_lgs(double * A, double * b, size_t size)
161 if(firm_gaussjordansolve(A,b,size) == 0) {
170 get_cf_probability(ir_node *bb, int pos, double loop_weight)
175 ir_node *pred = get_Block_cfgpred_block(bb, pos);
177 cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
179 for(i = get_Block_n_cfg_outs(pred) - 1; i >= 0; --i) {
180 ir_node *succ = get_Block_cfg_out(pred, i);
182 sum += get_loop_depth(get_irn_loop(succ)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
188 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
191 exec_freq_t *ef = ctx;
192 fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
197 compute_execfreq(ir_graph * irg, double loop_weight)
213 ef = xmalloc(sizeof(ef[0]));
214 memset(ef, 0, sizeof(ef[0]));
215 ef->min_non_zero = 1e50; /* initialize with a reasonable large number. */
216 freqs = ef->set = new_set(cmp_freq, 32);
218 construct_cf_backedges(irg);
223 irg_block_walk_graph(irg, block_walker, NULL, &wd);
225 size = set_count(freqs);
226 matrix = xmalloc(size*size*sizeof(*matrix));
227 memset(matrix, 0, size*size*sizeof(*matrix));
228 rhs = xmalloc(size*sizeof(*rhs));
229 memset(rhs, 0, size*sizeof(*rhs));
231 set_foreach(freqs, freq) {
232 ir_node *bb = (ir_node *)freq->irn;
233 size_t idx = (int)get_irn_link(bb);
235 matrix[idx * (size + 1)] = -1.0;
237 if (bb == get_irg_start_block(irg)) {
238 rhs[(int)get_irn_link(bb)] = -1.0;
242 for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
243 ir_node *pred = get_Block_cfgpred_block(bb, i);
244 size_t pred_idx = (int)get_irn_link(pred);
246 // matrix[pred_idx + idx*size] += 1.0/(double)get_Block_n_cfg_outs(pred);
247 matrix[pred_idx + idx * size] += get_cf_probability(bb, i, loop_weight);
251 x = solve_lgs(matrix, rhs, size);
257 ef->max = MAX_INT_FREQ;
258 set_foreach(freqs, freq) {
259 const ir_node *bb = freq->irn;
260 size_t idx = PTR_TO_INT(get_irn_link(bb));
263 freq->freq = ZERO(gsl_vector_get(x, idx)) ? 0.0 : gsl_vector_get(x, idx);
265 freq->freq = ZERO(x[idx]) ? 0.0 : x[idx];
268 /* get the maximum exec freq */
269 ef->max = MAX(ef->max, freq->freq);
271 /* Get the minimum non-zero execution frequency. */
273 ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
276 /* compute m and b of the transformation used to convert the doubles into scaled ints */
279 double h1 = MAX_INT_FREQ;
280 double l2 = ef->min_non_zero;
283 ef->m = (h1 /* - l1 */) / (h2 - l2);
284 ef->b = (/* l1 * */ h2 - l2 * h1) / (h2 - l2);
292 memset(&ef->hook, 0, sizeof(ef->hook));
293 ef->hook.context = ef;
294 ef->hook.hook._hook_node_info = exec_freq_node_info;
295 register_hook(hook_node_info, &ef->hook);
301 free_execfreq(exec_freq_t *ef)
304 unregister_hook(hook_node_info, &ef->hook);