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"
52 #include "irnodehashmap.h"
54 #include "execfreq_t.h"
57 #define UNDEF(x) (fabs(x) < EPSILON)
58 #define SEIDEL_TOLERANCE 1e-7
60 #define MAX_INT_FREQ 1000000
62 typedef struct freq_t {
66 static ir_nodehashmap_t freq_map;
67 static struct obstack obst;
68 static hook_entry_t hook;
70 double get_block_execfreq(const ir_node *block)
72 const freq_t *freq = ir_nodehashmap_get(freq_t, &freq_map, block);
78 void set_block_execfreq(ir_node *block, double newfreq)
80 freq_t *freq = ir_nodehashmap_get(freq_t, &freq_map, block);
82 freq = OALLOC(&obst, freq_t);
83 ir_nodehashmap_insert(&freq_map, block, freq);
88 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
93 const freq_t *freq = ir_nodehashmap_get(freq_t, &freq_map, irn);
95 fprintf(f, "execution frequency: %g\n", get_block_execfreq(irn));
98 void init_execfreq(void)
100 ir_nodehashmap_init(&freq_map);
103 memset(&hook, 0, sizeof(hook));
104 hook.hook._hook_node_info = exec_freq_node_info;
105 register_hook(hook_node_info, &hook);
108 void exit_execfreq(void)
110 unregister_hook(hook_node_info, &hook);
112 obstack_free(&obst, NULL);
113 ir_nodehashmap_destroy(&freq_map);
117 static double *solve_lgs(gs_matrix_t *mat, double *x, int size)
119 double init = 1.0 / size;
123 /* better convergence. */
124 for (i = 0; i < size; ++i)
127 stat_ev_dbl("execfreq_matrix_size", size);
131 dev = gs_matrix_gauss_seidel(mat, x, size);
132 } while (fabs(dev) > SEIDEL_TOLERANCE);
133 stat_ev_tim_pop("execfreq_seidel_time");
134 stat_ev_dbl("execfreq_seidel_iter", iter);
140 * Determine probability that predecessor pos takes this cf edge.
142 static double get_cf_probability(const ir_node *bb, int pos, double loop_weight)
146 double inv_loop_weight = 1./loop_weight;
147 const ir_node *pred = get_Block_cfgpred_block(bb, pos);
148 const ir_loop *pred_loop;
157 loop = get_irn_loop(bb);
158 depth = get_loop_depth(loop);
159 pred_loop = get_irn_loop(pred);
160 pred_depth = get_loop_depth(pred_loop);
162 for (d = depth; d < pred_depth; ++d) {
163 cur *= inv_loop_weight;
166 foreach_block_succ(pred, edge) {
167 const ir_node *succ = get_edge_src_irn(edge);
168 const ir_loop *succ_loop = get_irn_loop(succ);
169 int succ_depth = get_loop_depth(succ_loop);
172 for (d = succ_depth; d < pred_depth; ++d) {
173 fac *= inv_loop_weight;
181 static double *freqs;
182 static double min_non_zero;
183 static double max_freq;
185 static void collect_freqs(ir_node *node, void *data)
188 double freq = get_block_execfreq(node);
191 if (freq > 0.0 && freq < min_non_zero)
193 ARR_APP1(double, freqs, freq);
196 void ir_calculate_execfreq_int_factors(ir_execfreq_int_factors *factors,
199 /* compute m and b of the transformation used to convert the doubles into
201 freqs = NEW_ARR_F(double, 0);
202 min_non_zero = HUGE_VAL;
204 irg_block_walk_graph(irg, collect_freqs, NULL, NULL);
207 * find the smallest difference of the execution frequencies
208 * we try to ressolve it with 1 integer.
210 size_t n_freqs = ARR_LEN(freqs);
211 double smallest_diff = 1.0;
212 for (size_t i = 0; i < n_freqs; ++i) {
216 for (size_t j = i + 1; j < n_freqs; ++j) {
217 double diff = fabs(freqs[i] - freqs[j]);
220 smallest_diff = MIN(diff, smallest_diff);
224 double l2 = min_non_zero;
225 double h2 = max_freq;
227 double h1 = MAX_INT_FREQ;
229 /* according to that the slope of the translation function is
230 * 1.0 / smallest_diff */
231 factors->m = 1.0 / smallest_diff;
233 /* the abscissa is then given by */
234 factors->b = l1 - factors->m * l2;
237 * if the slope is so high that the largest integer would be larger than
238 * MAX_INT_FREQ set the largest int freq to that upper limit and recompute
239 * the translation function
241 if (factors->m * h2 + factors->b > MAX_INT_FREQ) {
242 factors->m = (h1 - l1) / (h2 - l2);
243 factors->b = l1 - factors->m * l2;
249 int get_block_execfreq_int(const ir_execfreq_int_factors *factors,
250 const ir_node *block)
252 double f = get_block_execfreq(block);
253 int res = (int) (f > factors->min_non_zero ? factors->m * f + factors->b : 1.0);
257 void ir_estimate_execfreq(ir_graph *irg)
259 double loop_weight = 10.0;
261 assure_irg_properties(irg,
262 IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
263 | IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
266 * using a toposort on the CFG (without back edges) will propagate
267 * the values better for the gauss/seidel iteration.
268 * => they can "flow" from start to end.
270 dfs_t *dfs = dfs_new(&absgraph_irg_cfg_succ, irg);
272 int size = dfs_get_n_nodes(dfs);
273 gs_matrix_t *mat = gs_new_matrix(size, size);
275 ir_node *end_block = get_irg_end_block(irg);
277 for (int idx = size - 1; idx >= 0; --idx) {
278 const ir_node *bb = (ir_node*)dfs_get_post_num_node(dfs, size-idx-1);
280 /* Sum of (execution frequency of predecessor * probability of cf edge) ... */
281 for (int i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
282 const ir_node *pred = get_Block_cfgpred_block(bb, i);
283 int pred_idx = size - dfs_get_post_num(dfs, pred)-1;
284 double cf_probability = get_cf_probability(bb, i, loop_weight);
285 gs_matrix_set(mat, idx, pred_idx, cf_probability);
287 /* ... equals my execution frequency */
288 gs_matrix_set(mat, idx, idx, -1.0);
290 /* Add an edge from end to start.
291 * The problem is then an eigenvalue problem:
292 * Solve A*x = 1*x => (A-I)x = 0
294 if (bb == end_block) {
295 const ir_node *start_block = get_irg_start_block(irg);
296 int s_idx = size - dfs_get_post_num(dfs, start_block)-1;
297 gs_matrix_set(mat, s_idx, idx, 1.0);
302 * Also add an edge for each kept block to start.
304 * This avoid strange results for e.g. an irg containing a exit()-call
305 * which block has no cfg successor.
307 ir_node *start_block = get_irg_start_block(irg);
308 int s_idx = size - dfs_get_post_num(dfs, start_block)-1;
309 const ir_node *end = get_irg_end(irg);
310 int n_keepalives = get_End_n_keepalives(end);
311 for (int idx = n_keepalives - 1; idx >= 0; --idx) {
312 ir_node *keep = get_End_keepalive(end, idx);
313 if (!is_Block(keep) || get_irn_n_edges_kind(keep, EDGE_KIND_BLOCK) > 0)
316 int k_idx = size-dfs_get_post_num(dfs, keep)-1;
318 gs_matrix_set(mat, s_idx, k_idx, 1.0);
321 /* solve the system and delete the matrix */
322 double *x = XMALLOCN(double, size);
323 solve_lgs(mat, x, size);
324 gs_delete_matrix(mat);
326 /* compute the normalization factor.
327 * 1.0 / exec freq of start block.
328 * (note: start_idx is != 0 in strange cases involving endless loops,
329 * probably a misfeature/bug)
331 int start_idx = size-dfs_get_post_num(dfs, get_irg_start_block(irg))-1;
332 double start_freq = x[start_idx];
333 double norm = start_freq != 0.0 ? 1.0 / start_freq : 1.0;
335 for (int idx = size - 1; idx >= 0; --idx) {
336 ir_node *bb = (ir_node *) dfs_get_post_num_node(dfs, size - idx - 1);
338 /* take abs because it sometimes can be -0 in case of endless loops */
339 double freq = fabs(x[idx]) * norm;
340 set_block_execfreq(bb, freq);