2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Compute an estimate of basic block executions.
23 * @author Adam M. Szalkowski
34 #include "gaussseidel.h"
44 #include "irgraph_t.h"
55 /* enable to also solve the equations with Gauss-Jordan */
56 #undef COMPARE_AGAINST_GAUSSJORDAN
58 #ifdef COMPARE_AGAINST_GAUSSJORDAN
59 #include "gaussjordan.h"
64 #define UNDEF(x) (fabs(x) < EPSILON)
65 #define SEIDEL_TOLERANCE 1e-7
67 #define MAX_INT_FREQ 1000000
69 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
71 typedef struct _freq_t {
83 unsigned infeasible : 1;
87 cmp_freq(const void *a, const void *b, size_t size)
93 return !(p->irn == q->irn);
97 set_find_freq(set * set, const ir_node * irn)
102 return set_find(set, &query, sizeof(query), HASH_PTR(irn));
106 set_insert_freq(set * set, const ir_node * irn)
113 return set_insert(set, &query, sizeof(query), HASH_PTR(irn));
117 get_block_execfreq(const ir_exec_freq *ef, const ir_node * irn)
119 if(!ef->infeasible) {
120 set *freqs = ef->set;
122 assert(is_Block(irn));
123 freq = set_find_freq(freqs, irn);
126 assert(freq->freq >= 0);
134 get_block_execfreq_ulong(const ir_exec_freq *ef, const ir_node *bb)
136 double f = get_block_execfreq(ef, bb);
137 int res = (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
142 solve_lgs(gs_matrix_t *mat, double *x, int size)
144 double init = 1.0 / size;
148 /* better convergence. */
149 for (i = 0; i < size; ++i)
152 stat_ev_dbl("execfreq_matrix_size", size);
156 dev = gs_matrix_gauss_seidel(mat, x, size);
157 } while(fabs(dev) > SEIDEL_TOLERANCE);
158 stat_ev_tim_pop("execfreq_seidel_time");
159 stat_ev_dbl("execfreq_seidel_iter", iter);
161 #ifdef COMPARE_AGAINST_GAUSSJORDAN
163 double *nw = XMALLOCN(double, size * size);
164 double *nx = XMALLOCNZ(double, size);
166 gs_matrix_export(mat, nw, size);
169 firm_gaussjordansolve(nw, nx, size);
170 stat_ev_tim_pop("execfreq_jordan_time");
181 get_cf_probability(ir_node *bb, int pos, double loop_weight)
185 double inv_loop_weight = 1./loop_weight;
186 const ir_node *pred = get_Block_cfgpred_block(bb, pos);
187 const ir_loop *pred_loop;
189 const ir_edge_t *edge;
197 loop = get_irn_loop(bb);
198 depth = get_loop_depth(loop);
199 pred_loop = get_irn_loop(pred);
200 pred_depth = get_loop_depth(pred_loop);
203 for (d = depth; d < pred_depth; ++d) {
204 cur *= inv_loop_weight;
207 foreach_block_succ(pred, edge) {
208 const ir_node *succ = get_edge_src_irn(edge);
209 const ir_loop *succ_loop = get_irn_loop(succ);
210 int succ_depth = get_loop_depth(succ_loop);
213 for (d = succ_depth; d < pred_depth; ++d) {
214 fac *= inv_loop_weight;
222 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
225 ir_exec_freq *ef = ctx;
226 fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
230 ir_exec_freq *create_execfreq(ir_graph *irg)
232 ir_exec_freq *execfreq = XMALLOCZ(ir_exec_freq);
233 execfreq->set = new_set(cmp_freq, 32);
235 memset(&execfreq->hook, 0, sizeof(execfreq->hook));
236 execfreq->hook.context = execfreq;
237 execfreq->hook.hook._hook_node_info = exec_freq_node_info;
238 register_hook(hook_node_info, &execfreq->hook);
244 void set_execfreq(ir_exec_freq *execfreq, const ir_node *block, double freq)
246 freq_t *f = set_insert_freq(execfreq->set, block);
250 static void collect_blocks(ir_node *bl, void *data)
253 set_insert_freq(freqs, bl);
257 compute_execfreq(ir_graph * irg, double loop_weight)
262 freq_t *freq, *s, *e;
271 * using a toposort on the CFG (without back edges) will propagate
272 * the values better for the gauss/seidel iteration.
273 * => they can "flow" from start to end.
275 dfs = dfs_new(&absgraph_irg_cfg_succ, irg);
276 ef = XMALLOCZ(ir_exec_freq);
277 ef->min_non_zero = HUGE_VAL; /* initialize with a reasonable large number. */
278 freqs = ef->set = new_set(cmp_freq, dfs_get_n_nodes(dfs));
281 * Populate the exec freq set.
282 * The DFS cannot be used alone, since the CFG might not be connected
283 * due to unreachable code.
285 irg_block_walk_graph(irg, collect_blocks, NULL, freqs);
287 construct_cf_backedges(irg);
290 size = dfs_get_n_nodes(dfs);
291 mat = gs_new_matrix(size, size);
292 x = XMALLOCN(double, size);
294 for (idx = dfs_get_n_nodes(dfs) - 1; idx >= 0; --idx) {
295 ir_node *bb = (ir_node *) dfs_get_post_num_node(dfs, size - idx - 1);
299 freq = set_insert_freq(freqs, bb);
302 for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
303 ir_node *pred = get_Block_cfgpred_block(bb, i);
304 int pred_idx = size - dfs_get_post_num(dfs, pred) - 1;
306 gs_matrix_set(mat, idx, pred_idx, get_cf_probability(bb, i, loop_weight));
308 gs_matrix_set(mat, idx, idx, -1.0);
314 * Add a loop from end to start.
315 * The problem is then an eigenvalue problem:
316 * Solve A*x = 1*x => (A-I)x = 0
318 s = set_find_freq(freqs, get_irg_start_block(irg));
319 e = set_find_freq(freqs, get_irg_end_block(irg));
321 gs_matrix_set(mat, s->idx, e->idx, 1.0);
323 /* solve the system and delete the matrix */
324 solve_lgs(mat, x, size);
325 gs_delete_matrix(mat);
328 * compute the normalization factor.
329 * 1.0 / exec freq of start block.
331 norm = x[s->idx] != 0.0 ? 1.0 / x[s->idx] : 1.0;
334 set_foreach(freqs, freq) {
337 /* take abs because it sometimes can be -0 in case of endless loops */
338 freq->freq = fabs(x[idx]) * norm;
340 /* get the maximum exec freq */
341 ef->max = MAX(ef->max, freq->freq);
343 /* Get the minimum non-zero execution frequency. */
345 ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
348 /* compute m and b of the transformation used to convert the doubles into scaled ints */
350 double smallest_diff = 1.0;
352 double l2 = ef->min_non_zero;
355 double h1 = MAX_INT_FREQ;
357 double *fs = malloc(set_count(freqs) * sizeof(fs[0]));
360 set_foreach(freqs, freq)
361 fs[n++] = freq->freq;
364 * find the smallest difference of the execution frequencies
365 * we try to ressolve it with 1 integer.
367 for(i = 0; i < n; ++i) {
371 for(j = i + 1; j < n; ++j) {
372 double diff = fabs(fs[i] - fs[j]);
375 smallest_diff = MIN(diff, smallest_diff);
379 /* according to that the slope of the translation function is 1.0 / smallest diff */
380 ef->m = 1.0 / smallest_diff;
382 /* the abscissa is then given by */
383 ef->b = l1 - ef->m * l2;
386 * if the slope is so high that the largest integer would be larger than MAX_INT_FREQ
387 * set the largest int freq to that upper limit and recompute the translation function
389 if(ef->m * h2 + ef->b > MAX_INT_FREQ) {
390 ef->m = (h1 - l1) / (h2 - l2);
391 ef->b = l1 - ef->m * l2;
397 memset(&ef->hook, 0, sizeof(ef->hook));
398 ef->hook.context = ef;
399 ef->hook.hook._hook_node_info = exec_freq_node_info;
400 register_hook(hook_node_info, &ef->hook);
408 free_execfreq(ir_exec_freq *ef)
411 unregister_hook(hook_node_info, &ef->hook);