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
33 #include "gaussseidel.h"
43 #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 typedef struct freq_t {
81 unsigned infeasible : 1;
84 static int cmp_freq(const void *a, const void *b, size_t size)
86 const freq_t *p = (const freq_t*) a;
87 const freq_t *q = (const freq_t*) b;
90 return !(p->irn == q->irn);
93 static freq_t *set_find_freq(set *freqs, const ir_node *irn)
97 return set_find(freq_t, freqs, &query, sizeof(query), hash_ptr(irn));
100 static freq_t *set_insert_freq(set *freqs, const ir_node *irn)
107 return set_insert(freq_t, freqs, &query, sizeof(query), hash_ptr(irn));
110 double get_block_execfreq(const ir_exec_freq *ef, const ir_node *irn)
112 if (!ef->infeasible) {
113 set *freqs = ef->freqs;
115 assert(is_Block(irn));
116 freq = set_find_freq(freqs, irn);
119 assert(freq->freq >= 0);
127 get_block_execfreq_ulong(const ir_exec_freq *ef, const ir_node *bb)
129 double f = get_block_execfreq(ef, bb);
130 int res = (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
134 static double *solve_lgs(gs_matrix_t *mat, double *x, int size)
136 double init = 1.0 / size;
140 /* better convergence. */
141 for (i = 0; i < size; ++i)
144 stat_ev_dbl("execfreq_matrix_size", size);
148 dev = gs_matrix_gauss_seidel(mat, x, size);
149 } while (fabs(dev) > SEIDEL_TOLERANCE);
150 stat_ev_tim_pop("execfreq_seidel_time");
151 stat_ev_dbl("execfreq_seidel_iter", iter);
153 #ifdef COMPARE_AGAINST_GAUSSJORDAN
155 double *nw = XMALLOCN(double, size * size);
156 double *nx = XMALLOCNZ(double, size);
158 gs_matrix_export(mat, nw, size);
161 firm_gaussjordansolve(nw, nx, size);
162 stat_ev_tim_pop("execfreq_jordan_time");
173 * Determine probability that predecessor pos takes this cf edge.
175 static double get_cf_probability(ir_node *bb, int pos, double loop_weight)
179 double inv_loop_weight = 1./loop_weight;
180 const ir_node *pred = get_Block_cfgpred_block(bb, pos);
181 const ir_loop *pred_loop;
190 loop = get_irn_loop(bb);
191 depth = get_loop_depth(loop);
192 pred_loop = get_irn_loop(pred);
193 pred_depth = get_loop_depth(pred_loop);
195 for (d = depth; d < pred_depth; ++d) {
196 cur *= inv_loop_weight;
199 foreach_block_succ(pred, edge) {
200 const ir_node *succ = get_edge_src_irn(edge);
201 const ir_loop *succ_loop = get_irn_loop(succ);
202 int succ_depth = get_loop_depth(succ_loop);
205 for (d = succ_depth; d < pred_depth; ++d) {
206 fac *= inv_loop_weight;
214 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
216 ir_exec_freq *ef = (ir_exec_freq*) ctx;
220 fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
223 ir_exec_freq *create_execfreq(ir_graph *irg)
225 ir_exec_freq *execfreq = XMALLOCZ(ir_exec_freq);
226 execfreq->freqs = new_set(cmp_freq, 32);
228 memset(&execfreq->hook, 0, sizeof(execfreq->hook));
230 // set reasonable values to convert double execfreq to ulong execfreq
233 execfreq->hook.context = execfreq;
234 execfreq->hook.hook._hook_node_info = exec_freq_node_info;
235 register_hook(hook_node_info, &execfreq->hook);
241 void set_execfreq(ir_exec_freq *execfreq, const ir_node *block, double freq)
243 freq_t *f = set_insert_freq(execfreq->freqs, block);
247 static void collect_blocks(ir_node *bl, void *data)
249 set *freqs = (set*) data;
250 set_insert_freq(freqs, bl);
253 ir_exec_freq *compute_execfreq(ir_graph *irg, double loop_weight)
259 freq_t *freq, *s, *e;
261 ir_node *end = get_irg_end(irg);
269 * using a toposort on the CFG (without back edges) will propagate
270 * the values better for the gauss/seidel iteration.
271 * => they can "flow" from start to end.
273 dfs = dfs_new(&absgraph_irg_cfg_succ, irg);
274 ef = XMALLOCZ(ir_exec_freq);
275 ef->min_non_zero = HUGE_VAL; /* initialize with a reasonable large number. */
276 freqs = ef->freqs = new_set(cmp_freq, dfs_get_n_nodes(dfs));
279 * Populate the exec freq set.
280 * The DFS cannot be used alone, since the CFG might not be connected
281 * due to unreachable code.
283 irg_block_walk_graph(irg, collect_blocks, NULL, freqs);
285 construct_cf_backedges(irg);
288 size = dfs_get_n_nodes(dfs);
289 mat = gs_new_matrix(size, size);
290 x = XMALLOCN(double, size);
292 for (idx = dfs_get_n_nodes(dfs) - 1; idx >= 0; --idx) {
293 ir_node *bb = (ir_node *) dfs_get_post_num_node(dfs, size - idx - 1);
296 freq = set_insert_freq(freqs, bb);
299 /* Sum of (execution frequency of predecessor * probability of cf edge) ... */
300 for (i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
301 ir_node *pred = get_Block_cfgpred_block(bb, i);
302 int pred_idx = size - dfs_get_post_num(dfs, pred) - 1;
304 gs_matrix_set(mat, idx, pred_idx, get_cf_probability(bb, i, loop_weight));
306 /* ... equals my execution frequency */
307 gs_matrix_set(mat, idx, idx, -1.0);
313 * Add an edge from end to start.
314 * The problem is then an eigenvalue problem:
315 * Solve A*x = 1*x => (A-I)x = 0
317 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);
324 * Also add an edge for each kept block to start.
326 * This avoid strange results for e.g. an irg containing a exit()-call
327 * which block has no cfg successor.
329 n_keepalives = get_End_n_keepalives(end);
330 for (idx = n_keepalives - 1; idx >= 0; --idx) {
331 ir_node *keep = get_End_keepalive(end, idx);
333 if (is_Block(keep) && get_Block_n_cfg_outs(keep) == 0) {
334 freq_t *k = set_find_freq(freqs, keep);
336 gs_matrix_set(mat, s->idx, k->idx, 1.0);
340 /* solve the system and delete the matrix */
341 solve_lgs(mat, x, size);
342 gs_delete_matrix(mat);
345 * compute the normalization factor.
346 * 1.0 / exec freq of start block.
348 norm = x[s->idx] != 0.0 ? 1.0 / x[s->idx] : 1.0;
351 foreach_set(freqs, freq_t, freq) {
354 /* take abs because it sometimes can be -0 in case of endless loops */
355 freq->freq = fabs(x[idx]) * norm;
357 /* get the maximum exec freq */
358 ef->max = MAX(ef->max, freq->freq);
360 /* Get the minimum non-zero execution frequency. */
361 if (freq->freq > 0.0)
362 ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
365 /* compute m and b of the transformation used to convert the doubles into scaled ints */
367 double smallest_diff = 1.0;
369 double l2 = ef->min_non_zero;
372 double h1 = MAX_INT_FREQ;
374 double *fs = (double*) malloc(set_count(freqs) * sizeof(fs[0]));
377 foreach_set(freqs, freq_t, freq)
378 fs[n++] = freq->freq;
381 * find the smallest difference of the execution frequencies
382 * we try to ressolve it with 1 integer.
384 for (i = 0; i < n; ++i) {
388 for (j = i + 1; j < n; ++j) {
389 double diff = fabs(fs[i] - fs[j]);
392 smallest_diff = MIN(diff, smallest_diff);
396 /* according to that the slope of the translation function is 1.0 / smallest diff */
397 ef->m = 1.0 / smallest_diff;
399 /* the abscissa is then given by */
400 ef->b = l1 - ef->m * l2;
403 * if the slope is so high that the largest integer would be larger than MAX_INT_FREQ
404 * set the largest int freq to that upper limit and recompute the translation function
406 if (ef->m * h2 + ef->b > MAX_INT_FREQ) {
407 ef->m = (h1 - l1) / (h2 - l2);
408 ef->b = l1 - ef->m * l2;
414 memset(&ef->hook, 0, sizeof(ef->hook));
415 ef->hook.context = ef;
416 ef->hook.hook._hook_node_info = exec_freq_node_info;
417 register_hook(hook_node_info, &ef->hook);
424 void free_execfreq(ir_exec_freq *ef)
427 unregister_hook(hook_node_info, &ef->hook);