2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Compute an estimate of basic block executions.
9 * @author Adam M. Szalkowski
19 #include "gaussseidel.h"
29 #include "irgraph_t.h"
38 #include "irnodehashmap.h"
40 #include "execfreq_t.h"
43 #define UNDEF(x) (fabs(x) < EPSILON)
44 #define SEIDEL_TOLERANCE 1e-7
46 #define MAX_INT_FREQ 1000000
48 static hook_entry_t hook;
50 double get_block_execfreq(const ir_node *block)
52 return block->attr.block.execfreq;
55 void set_block_execfreq(ir_node *block, double newfreq)
57 block->attr.block.execfreq = newfreq;
60 static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
65 double freq = get_block_execfreq(irn);
67 fprintf(f, "execution frequency: %g\n", freq);
70 void init_execfreq(void)
72 memset(&hook, 0, sizeof(hook));
73 hook.hook._hook_node_info = exec_freq_node_info;
74 register_hook(hook_node_info, &hook);
77 void exit_execfreq(void)
79 unregister_hook(hook_node_info, &hook);
83 static double *solve_lgs(gs_matrix_t *mat, double *x, int size)
85 double init = 1.0 / size;
89 /* better convergence. */
90 for (i = 0; i < size; ++i)
93 stat_ev_dbl("execfreq_matrix_size", size);
97 dev = gs_matrix_gauss_seidel(mat, x, size);
98 } while (fabs(dev) > SEIDEL_TOLERANCE);
99 stat_ev_tim_pop("execfreq_seidel_time");
100 stat_ev_dbl("execfreq_seidel_iter", iter);
106 * Determine probability that predecessor pos takes this cf edge.
108 static double get_cf_probability(const ir_node *bb, int pos, double loop_weight)
112 double inv_loop_weight = 1./loop_weight;
113 const ir_node *pred = get_Block_cfgpred_block(bb, pos);
114 const ir_loop *pred_loop;
123 loop = get_irn_loop(bb);
124 depth = get_loop_depth(loop);
125 pred_loop = get_irn_loop(pred);
126 pred_depth = get_loop_depth(pred_loop);
128 for (d = depth; d < pred_depth; ++d) {
129 cur *= inv_loop_weight;
132 foreach_block_succ(pred, edge) {
133 const ir_node *succ = get_edge_src_irn(edge);
134 const ir_loop *succ_loop = get_irn_loop(succ);
135 int succ_depth = get_loop_depth(succ_loop);
138 for (d = succ_depth; d < pred_depth; ++d) {
139 fac *= inv_loop_weight;
147 static double *freqs;
148 static double min_non_zero;
149 static double max_freq;
151 static void collect_freqs(ir_node *node, void *data)
154 double freq = get_block_execfreq(node);
157 if (freq > 0.0 && freq < min_non_zero)
159 ARR_APP1(double, freqs, freq);
162 void ir_calculate_execfreq_int_factors(ir_execfreq_int_factors *factors,
165 /* compute m and b of the transformation used to convert the doubles into
167 freqs = NEW_ARR_F(double, 0);
168 min_non_zero = HUGE_VAL;
170 irg_block_walk_graph(irg, collect_freqs, NULL, NULL);
173 * find the smallest difference of the execution frequencies
174 * we try to ressolve it with 1 integer.
176 size_t n_freqs = ARR_LEN(freqs);
177 double smallest_diff = 1.0;
178 for (size_t i = 0; i < n_freqs; ++i) {
182 for (size_t j = i + 1; j < n_freqs; ++j) {
183 double diff = fabs(freqs[i] - freqs[j]);
186 smallest_diff = MIN(diff, smallest_diff);
190 double l2 = min_non_zero;
191 double h2 = max_freq;
193 double h1 = MAX_INT_FREQ;
195 /* according to that the slope of the translation function is
196 * 1.0 / smallest_diff */
197 factors->m = 1.0 / smallest_diff;
199 /* the abscissa is then given by */
200 factors->b = l1 - factors->m * l2;
203 * if the slope is so high that the largest integer would be larger than
204 * MAX_INT_FREQ set the largest int freq to that upper limit and recompute
205 * the translation function
207 if (factors->m * h2 + factors->b > MAX_INT_FREQ) {
208 factors->m = (h1 - l1) / (h2 - l2);
209 factors->b = l1 - factors->m * l2;
215 int get_block_execfreq_int(const ir_execfreq_int_factors *factors,
216 const ir_node *block)
218 double f = get_block_execfreq(block);
219 int res = (int) (f > factors->min_non_zero ? factors->m * f + factors->b : 1.0);
223 void ir_estimate_execfreq(ir_graph *irg)
225 double loop_weight = 10.0;
227 assure_irg_properties(irg,
228 IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
229 | IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO
230 | IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
233 * using a toposort on the CFG (without back edges) will propagate
234 * the values better for the gauss/seidel iteration.
235 * => they can "flow" from start to end.
237 dfs_t *dfs = dfs_new(&absgraph_irg_cfg_succ, irg);
239 int size = dfs_get_n_nodes(dfs);
240 gs_matrix_t *mat = gs_new_matrix(size, size);
242 ir_node *const start_block = get_irg_start_block(irg);
243 ir_node *const end_block = get_irg_end_block(irg);
245 for (int idx = size - 1; idx >= 0; --idx) {
246 const ir_node *bb = (ir_node*)dfs_get_post_num_node(dfs, size-idx-1);
248 /* Sum of (execution frequency of predecessor * probability of cf edge) ... */
249 for (int i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
250 const ir_node *pred = get_Block_cfgpred_block(bb, i);
251 int pred_idx = size - dfs_get_post_num(dfs, pred)-1;
252 double cf_probability = get_cf_probability(bb, i, loop_weight);
253 gs_matrix_set(mat, idx, pred_idx, cf_probability);
255 /* ... equals my execution frequency */
256 gs_matrix_set(mat, idx, idx, -1.0);
258 /* Add an edge from end to start.
259 * The problem is then an eigenvalue problem:
260 * Solve A*x = 1*x => (A-I)x = 0
262 if (bb == end_block) {
263 int const s_idx = size - dfs_get_post_num(dfs, start_block) - 1;
264 gs_matrix_set(mat, s_idx, idx, 1.0);
269 * Also add an edge for each kept block to start.
271 * This avoid strange results for e.g. an irg containing a exit()-call
272 * which block has no cfg successor.
274 int s_idx = size - dfs_get_post_num(dfs, start_block)-1;
275 const ir_node *end = get_irg_end(irg);
276 int n_keepalives = get_End_n_keepalives(end);
277 for (int idx = n_keepalives - 1; idx >= 0; --idx) {
278 ir_node *keep = get_End_keepalive(end, idx);
279 if (!is_Block(keep) || get_irn_n_edges_kind(keep, EDGE_KIND_BLOCK) > 0)
282 int k_idx = size-dfs_get_post_num(dfs, keep)-1;
284 gs_matrix_set(mat, s_idx, k_idx, 1.0);
287 /* solve the system and delete the matrix */
288 double *x = XMALLOCN(double, size);
289 solve_lgs(mat, x, size);
290 gs_delete_matrix(mat);
292 /* compute the normalization factor.
293 * 1.0 / exec freq of start block.
294 * (note: start_idx is != 0 in strange cases involving endless loops,
295 * probably a misfeature/bug)
297 int start_idx = size - dfs_get_post_num(dfs, start_block) - 1;
298 double start_freq = x[start_idx];
299 double norm = start_freq != 0.0 ? 1.0 / start_freq : 1.0;
301 for (int idx = size - 1; idx >= 0; --idx) {
302 ir_node *bb = (ir_node *) dfs_get_post_num_node(dfs, size - idx - 1);
304 /* take abs because it sometimes can be -0 in case of endless loops */
305 double freq = fabs(x[idx]) * norm;
306 set_block_execfreq(bb, freq);