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
36 #include "gaussseidel.h"
38 #include "firm_common_t.h"
47 #include "irgraph_t.h"
58 /* enable to also solve the equations with Gauss-Jordan */
59 #undef COMPARE_AGAINST_GAUSSJORDAN
61 #ifdef COMPARE_AGAINST_GAUSSJORDAN
62 #include "gaussjordan.h"
67 #define UNDEF(x) (fabs(x) < EPSILON)
68 #define SEIDEL_TOLERANCE 1e-7
70 #define MAX_INT_FREQ 1000000
72 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
74 typedef struct _freq_t {
86 unsigned infeasible : 1;
90 cmp_freq(const void *a, const void *b, size_t size)
96 return !(p->irn == q->irn);
100 set_find_freq(set * set, const ir_node * irn)
105 return set_find(set, &query, sizeof(query), HASH_PTR(irn));
109 set_insert_freq(set * set, const ir_node * irn)
116 return set_insert(set, &query, sizeof(query), HASH_PTR(irn));
120 get_block_execfreq(const ir_exec_freq *ef, const ir_node * irn)
122 if(!ef->infeasible) {
123 set *freqs = ef->set;
125 assert(is_Block(irn));
126 freq = set_find_freq(freqs, irn);
129 assert(freq->freq >= 0);
137 get_block_execfreq_ulong(const ir_exec_freq *ef, const ir_node *bb)
139 double f = get_block_execfreq(ef, bb);
140 int res = (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
145 solve_lgs(gs_matrix_t *mat, double *x, int size)
147 double init = 1.0 / size;
151 /* better convergence. */
152 for (i = 0; i < size; ++i)
155 stat_ev_dbl("execfreq_matrix_size", size);
159 dev = gs_matrix_gauss_seidel(mat, x, size);
160 } while(fabs(dev) > SEIDEL_TOLERANCE);
161 stat_ev_tim_pop("execfreq_seidel_time");
162 stat_ev_dbl("execfreq_seidel_iter", iter);
164 #ifdef COMPARE_AGAINST_GAUSSJORDAN
166 double *nw = xmalloc(size * size * sizeof(*nw));
167 double *nx = xmalloc(size * sizeof(*nx));
169 memset(nx, 0, size * sizeof(*nx));
170 gs_matrix_export(mat, nw, size);
173 firm_gaussjordansolve(nw, nx, size);
174 stat_ev_tim_pop("execfreq_jordan_time");
185 get_cf_probability(ir_node *bb, int pos, double loop_weight)
189 const ir_node *pred = get_Block_cfgpred_block(bb, pos);
190 const ir_loop *pred_loop = get_irn_loop(pred);
191 int pred_depth = get_loop_depth(pred_loop);
192 const ir_edge_t *edge;
194 cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;
196 foreach_block_succ(pred, edge) {
197 const ir_node *block = get_edge_src_irn(edge);
198 const ir_loop *loop = get_irn_loop(block);
199 int depth = get_loop_depth(loop);
200 sum += depth < pred_depth ? 1.0 : loop_weight;
206 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
209 ir_exec_freq *ef = ctx;
210 fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
214 ir_exec_freq *create_execfreq(ir_graph *irg)
216 ir_exec_freq *execfreq = xmalloc(sizeof(execfreq[0]));
217 memset(execfreq, 0, sizeof(execfreq[0]));
218 execfreq->set = new_set(cmp_freq, 32);
220 memset(&execfreq->hook, 0, sizeof(execfreq->hook));
221 execfreq->hook.context = execfreq;
222 execfreq->hook.hook._hook_node_info = exec_freq_node_info;
223 register_hook(hook_node_info, &execfreq->hook);
229 void set_execfreq(ir_exec_freq *execfreq, const ir_node *block, double freq)
231 freq_t *f = set_insert_freq(execfreq->set, block);
235 static void collect_blocks(ir_node *bl, void *data)
238 set_insert_freq(freqs, bl);
242 compute_execfreq(ir_graph * irg, double loop_weight)
247 freq_t *freq, *s, *e;
256 * using a toposort on the CFG (without back edges) will propagate
257 * the values better for the gauss/seidel iteration.
258 * => they can "flow" from start to end.
260 dfs = dfs_new(&absgraph_irg_cfg_succ, irg);
261 ef = xmalloc(sizeof(ef[0]));
262 memset(ef, 0, sizeof(ef[0]));
263 ef->min_non_zero = HUGE_VAL; /* initialize with a reasonable large number. */
264 freqs = ef->set = new_set(cmp_freq, dfs_get_n_nodes(dfs));
267 * Populate the exec freq set.
268 * The DFS cannot be used alone, since the CFG might not be connected
269 * due to unreachable code.
271 irg_block_walk_graph(irg, collect_blocks, NULL, freqs);
273 construct_cf_backedges(irg);
276 size = dfs_get_n_nodes(dfs);
277 mat = gs_new_matrix(size, size);
278 x = xmalloc(size*sizeof(*x));
280 for (idx = dfs_get_n_nodes(dfs) - 1; idx >= 0; --idx) {
281 ir_node *bb = (ir_node *) dfs_get_post_num_node(dfs, size - idx - 1);
285 freq = set_insert_freq(freqs, bb);
288 for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
289 ir_node *pred = get_Block_cfgpred_block(bb, i);
290 int pred_idx = size - dfs_get_post_num(dfs, pred) - 1;
292 gs_matrix_set(mat, idx, pred_idx, get_cf_probability(bb, i, loop_weight));
294 gs_matrix_set(mat, idx, idx, -1.0);
300 * Add a loop from end to start.
301 * The problem is then an eigenvalue problem:
302 * Solve A*x = 1*x => (A-I)x = 0
304 s = set_find_freq(freqs, get_irg_start_block(irg));
305 e = set_find_freq(freqs, get_irg_end_block(irg));
307 gs_matrix_set(mat, s->idx, e->idx, 1.0);
309 /* solve the system and delete the matrix */
310 solve_lgs(mat, x, size);
311 gs_delete_matrix(mat);
314 * compute the normalization factor.
315 * 1.0 / exec freq of start block.
317 norm = x[s->idx] != 0.0 ? 1.0 / x[s->idx] : 1.0;
320 set_foreach(freqs, freq) {
323 /* take abs because it sometimes can be -0 in case of endless loops */
324 freq->freq = fabs(x[idx]) * norm;
326 /* get the maximum exec freq */
327 ef->max = MAX(ef->max, freq->freq);
329 /* Get the minimum non-zero execution frequency. */
331 ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
334 /* compute m and b of the transformation used to convert the doubles into scaled ints */
336 double smallest_diff = 1.0;
338 double l2 = ef->min_non_zero;
341 double h1 = MAX_INT_FREQ;
343 double *fs = malloc(set_count(freqs) * sizeof(fs[0]));
346 set_foreach(freqs, freq)
347 fs[n++] = freq->freq;
350 * find the smallest difference of the execution frequencies
351 * we try to ressolve it with 1 integer.
353 for(i = 0; i < n; ++i) {
357 for(j = i + 1; j < n; ++j) {
358 double diff = fabs(fs[i] - fs[j]);
361 smallest_diff = MIN(diff, smallest_diff);
365 /* according to that the slope of the translation function is 1.0 / smallest diff */
366 ef->m = 1.0 / smallest_diff;
368 /* the abscissa is then given by */
369 ef->b = l1 - ef->m * l2;
372 * if the slope is so high that the largest integer would be larger than MAX_INT_FREQ
373 * set the largest int freq to that upper limit and recompute the translation function
375 if(ef->m * h2 + ef->b > MAX_INT_FREQ) {
376 ef->m = (h1 - l1) / (h2 - l2);
377 ef->b = l1 - ef->m * l2;
383 memset(&ef->hook, 0, sizeof(ef->hook));
384 ef->hook.context = ef;
385 ef->hook.hook._hook_node_info = exec_freq_node_info;
386 register_hook(hook_node_info, &ef->hook);
394 free_execfreq(ir_exec_freq *ef)
397 unregister_hook(hook_node_info, &ef->hook);