3 * File name: ir/ana/execution_frequency.c
4 * Purpose: Compute an estimate of basic block executions.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 2004 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 #include "execution_frequency.h"
18 #include "firm_common_t.h"
27 #include "interval_analysis.h"
29 /*------------------------------------------------------------------*/
30 /* A hashmap mapping the frequency to block and loop nodes. Block
31 * and loop nodes are regions. */
32 /*------------------------------------------------------------------*/
40 /* We use this set for all nodes in all irgraphs. */
41 static set *exec_freq_set = NULL;
43 static int exec_freq_cmp(const void *e1, const void *e2, size_t size) {
44 reg_exec_freq *ef1 = (reg_exec_freq *)e1;
45 reg_exec_freq *ef2 = (reg_exec_freq *)e2;
46 return (ef1->reg != ef2->reg);
49 static INLINE unsigned int exec_freq_hash(void *e) {
50 unsigned int v = (unsigned int) ((reg_exec_freq *)e)->reg;
54 static INLINE void set_region_exec_freq(void *reg, double freq) {
58 set_insert(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
61 INLINE double get_region_exec_freq(void *reg) {
62 reg_exec_freq ef, *found;
64 assert(exec_freq_set);
66 found = set_find(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
68 /* Not found if information is invalid. */
75 /* Returns the number of times the block is executed. */
76 double get_Block_exec_freq(ir_node *b) {
77 return get_region_exec_freq((void *)b);
80 double get_irn_exec_freq(ir_node *n) {
81 if (!is_Block(n)) n = get_nodes_block(n);
82 return get_Block_exec_freq(n);
86 /*------------------------------------------------------------------*/
87 /* A algorithm that precomputes whether Conds lead to an exception.
88 * Computes a field for all Projs from Conds that says the following:
89 * - The Proj projs from a normal dual Cond with probability 50:50
90 * - This Proj of the Cond leads to an exception, i.e., a raise node.
91 * It is taken with exception probability.
92 * - The Proj of the Cond avoids an exception. It is taken with
93 * 1 - exception probability. */
94 /*------------------------------------------------------------------*/
101 Cond_prob_avoid_exception,
102 Cond_prob_exception_taken,
103 Cond_prob_was_exception_taken,
106 static int just_passed_a_Raise = 0;
107 static ir_node *Cond_list = NULL;
109 /* We do not use an extra set, as Projs are not yet in the existing one. */
110 void set_ProjX_probability(ir_node *n, Cond_prob prob) {
114 set_insert(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
117 Cond_prob get_ProjX_probability(ir_node *n) {
118 reg_exec_freq ef, *found;
121 found = set_find(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
124 return (Cond_prob)found->prob;
126 return Cond_prob_none;
129 /* A walker that only visits the nodes we want to see. */
132 my_irg_walk_2_both(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env) {
134 set_irn_visited(node, current_ir_graph->visited);
138 if (node->op != op_Block) {
140 if (node->op == op_Proj)
141 pred = get_irn_n(node, 0);
143 pred = get_irn_n(node, -1);
144 if (pred->visited < current_ir_graph->visited)
145 my_irg_walk_2_both(pred, pre, post, env);
149 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
150 ir_node *pred = get_irn_n(node, i);
151 if (pred->visited < current_ir_graph->visited)
152 my_irg_walk_2_both(pred, pre, post, env);
156 if (node->op == op_End) {
157 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
158 ir_node *pred = get_irn_n(node, i);
159 if ((pred->op == op_Block) && (pred->visited < current_ir_graph->visited))
160 my_irg_walk_2_both(pred, pre, post, env);
166 static void my_irg_walk_current_graph(irg_walk_func *pre, irg_walk_func *post, void *env) {
167 inc_irg_visited(current_ir_graph);
168 my_irg_walk_2_both(get_irg_end(current_ir_graph), pre, post, env);
172 static void walk_pre(ir_node *n, void *env) {
174 if (get_irn_op(n) == op_Raise)
175 just_passed_a_Raise = 1;
177 if ( (get_irn_op(n) == op_Proj)
178 && (get_irn_op(get_Proj_pred(n)) == op_Cond)
179 && (just_passed_a_Raise)) {
181 ir_node *c = get_Proj_pred(n);
183 /* If we already visited the other Proj, and it also leads to a Raise,
184 we are in the middle of something. Continue searching. */
185 assert(get_irn_n_outs(c) == 2 && "encountered a switch cond");
186 other_proj = get_irn_out(c, 0);
187 if (other_proj == n) other_proj = get_irn_out(c, 1);
188 if (get_ProjX_probability(other_proj) == Cond_prob_exception_taken) {
189 set_ProjX_probability(other_proj, Cond_prob_was_exception_taken);
190 /* Keep searching for the Proj, so keep just_passed_a_Raise. */
192 set_ProjX_probability(n, Cond_prob_exception_taken);
193 just_passed_a_Raise = 0;
197 if (get_irn_op(n) == op_Cond) {
198 set_irn_link(n, Cond_list);
203 static void walk_post(ir_node *n, void *env) {
205 if (get_irn_op(n) == op_Raise)
206 just_passed_a_Raise = 0;
208 if ( (get_irn_op(n) == op_Proj)
209 && (get_irn_op(get_Proj_pred(n)) == op_Cond)
210 && ((get_ProjX_probability(n) == Cond_prob_exception_taken) ||
211 (get_ProjX_probability(n) == Cond_prob_was_exception_taken) )) {
212 just_passed_a_Raise = 1;
216 /** Precompute which Conds test for an exception.
218 * Operates on current_ir_graph. */
219 void precompute_cond_evaluation(void) {
222 compute_outs(current_ir_graph);
224 just_passed_a_Raise = 0;
226 my_irg_walk_current_graph(walk_pre, walk_post, NULL);
228 for (c = Cond_list; c; c = get_irn_link(c)) {
231 assert(get_irn_n_outs(c) == 2 && "encountered a switch cond");
232 p0 = get_irn_out(c, 0);
233 p1 = get_irn_out(c, 1);
235 /* both are exceptions */
236 if ((get_ProjX_probability(p0) == Cond_prob_exception_taken) &&
237 (get_ProjX_probability(p1) == Cond_prob_exception_taken) ) {
238 assert(0 && "I tried to avoid these!");
240 set_ProjX_probability(p0, Cond_prob_normal);
241 set_ProjX_probability(p1, Cond_prob_normal);
244 /* p0 is exception */
245 else if (get_ProjX_probability(p0) == Cond_prob_exception_taken) {
246 set_ProjX_probability(p1, Cond_prob_avoid_exception);
249 /* p1 is exception */
250 else if (get_ProjX_probability(p1) == Cond_prob_exception_taken) {
251 set_ProjX_probability(p0, Cond_prob_avoid_exception);
254 /* none is exception */
256 set_ProjX_probability(p0, Cond_prob_normal);
257 set_ProjX_probability(p1, Cond_prob_normal);
262 int is_fragile_Proj(ir_node *n) {
263 return is_Proj(n) && (get_ProjX_probability(n) == Cond_prob_exception_taken);
266 /*------------------------------------------------------------------*/
267 /* The algorithm to compute the execution frequencies.
269 * Walk the control flow loop tree which we consider the interval
270 * tree. Compute the execution for the lowest loop, add inner loops
271 * to worklist. Consider the inner loops as simple nodes. Check that
272 * there is only one loop header in each loop. */
273 /*------------------------------------------------------------------*/
275 static double exception_prob = 0.001;
277 static INLINE int is_loop_head(ir_node *cond) {
281 /** Weight a single region in edge.
283 * Given all outs of the predecessor region, we can compute the weight of
284 * this single edge. */
285 static INLINE double get_weighted_region_exec_freq(void *reg, int pos) {
286 void *pred_reg = get_region_in(reg, pos);
287 double res, full_freq = get_region_exec_freq (pred_reg);
288 int n_outs = get_region_n_outs (pred_reg);
289 int n_exc_outs = get_region_n_exc_outs(pred_reg);
292 if (is_ir_node(reg)) {
293 cfop = get_Block_cfgpred((ir_node *)reg, pos);
294 if (is_Proj(cfop) && (get_irn_op(get_Proj_pred(cfop)) != op_Cond))
295 cfop = skip_Proj(cfop);
297 assert(is_ir_loop(reg));
298 cfop = get_loop_cfop(reg, pos);
301 if (is_fragile_op(cfop) || is_fragile_Proj(cfop)) {
302 res = full_freq * exception_prob;
305 /* Equally distribute the weight after exceptions to the left over outs. */
306 res = (full_freq *(1 - exception_prob * n_exc_outs)) / (n_outs - n_exc_outs);
312 static INLINE void compute_region_freqency(void *reg, double head_weight) {
313 int i, n_ins = get_region_n_ins(reg);
316 //printf("head weight %lf: ", head_weight); DDMR(reg);
318 for (i = 0; i < n_ins; ++i) {
319 void *pred_reg = get_region_in(reg, i);
321 my_freq += get_weighted_region_exec_freq(reg, i);
325 if (my_freq == 0.0) {
326 /* All preds are from outer loop. We are a head or so. */
327 my_freq = head_weight;
329 set_region_exec_freq(reg, my_freq);
332 static void check_proper_head(ir_loop *l, void *reg) {
333 int i, n_ins = get_region_n_ins(reg);
334 for (i = 0; i < n_ins; ++i) {
335 assert(!get_region_in(reg, i));
339 /* Compute the ex freq for current_ir_graph */
340 static void compute_frequency(int default_loop_weight) {
341 ir_loop *outermost_l = get_irg_loop(current_ir_graph);
342 pdeq *block_worklist = new_pdeq1(outermost_l);
344 /* Outermost start is considered a loop head. We will soon multiply
345 by default_loop_weight. */
346 set_region_exec_freq(outermost_l, 1.0/default_loop_weight);
348 while (!pdeq_empty(block_worklist)) {
349 ir_loop *l = (ir_loop *)pdeq_getl(block_worklist);
350 int i, n_elems = get_loop_n_elements(l);
352 /* The header is initialized with the frequency of the full loop times the iteration weight. */
353 check_proper_head(l, get_loop_element(l, 0).son);
355 for (i = 0; i < n_elems; ++i) {
356 loop_element e = get_loop_element(l, i);
357 if (is_ir_loop(e.son)) pdeq_putr(block_worklist, e.son);
358 compute_region_freqency(e.son, default_loop_weight * get_region_exec_freq(l));
361 del_pdeq(block_worklist);
364 /* Compute the execution frequency for all blocks in the given
367 * irg: The graph to be analyzed.
368 * default_loop_weight: The number of executions of a loop.
370 void compute_execution_frequency(ir_graph *irg, int default_loop_weight, double exception_probability) {
371 ir_graph *rem = current_ir_graph;
372 current_ir_graph = irg;
373 exception_prob = exception_probability;
374 if (!exec_freq_set) exec_freq_set = new_set(exec_freq_cmp, 256);
376 precompute_cond_evaluation();
377 construct_intervals(current_ir_graph);
378 compute_frequency(default_loop_weight);
381 dump_loop_tree (current_ir_graph, "-execfreq");
382 dump_ir_block_graph(current_ir_graph, "-execfreq");
383 dump_interval_graph(current_ir_graph, "-execfreq");
386 current_ir_graph = rem;
390 void compute_execution_frequencies(int default_loop_weight, double exception_probability) {
391 int i, n_irgs = get_irp_n_irgs();
393 for (i = 0; i < n_irgs; ++i) {
394 compute_execution_frequency(get_irp_irg(i), default_loop_weight, exception_probability);
398 /** free occupied memory, reset */
399 void free_execution_frequency(void) {
401 del_set(exec_freq_set);