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"
24 #include "irgraph_t.h"
29 #include "interval_analysis.h"
31 void set_irp_exec_freq_state(exec_freq_state s);
33 /*------------------------------------------------------------------*/
34 /* A hashmap mapping the frequency to block and loop nodes. Block
35 * and loop nodes are regions. */
36 /*------------------------------------------------------------------*/
44 /* We use this set for all nodes in all irgraphs. */
45 static set *exec_freq_set = NULL;
47 static int exec_freq_cmp(const void *e1, const void *e2, size_t size) {
48 reg_exec_freq *ef1 = (reg_exec_freq *)e1;
49 reg_exec_freq *ef2 = (reg_exec_freq *)e2;
50 return (ef1->reg != ef2->reg);
53 static INLINE unsigned int exec_freq_hash(reg_exec_freq *e) {
54 return HASH_PTR(e->reg);
57 static INLINE void set_region_exec_freq(void *reg, double freq) {
61 set_insert(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
64 double get_region_exec_freq(void *reg) {
65 reg_exec_freq ef, *found;
67 assert(exec_freq_set);
69 found = set_find(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
71 /* Not found if information is invalid. */
78 /* Returns the number of times the block is executed. */
79 double get_Block_exec_freq(ir_node *b) {
80 return get_region_exec_freq((void *)b);
83 double get_irn_exec_freq(ir_node *n) {
84 if (!is_Block(n)) n = get_nodes_block(n);
85 return get_Block_exec_freq(n);
89 /*------------------------------------------------------------------*/
90 /* A algorithm that precomputes whether Conds lead to an exception.
91 * Computes a field for all Projs from Conds that says the following:
92 * - The Proj projs from a normal dual Cond with probability 50:50
93 * - This Proj of the Cond leads to an exception, i.e., a raise node.
94 * It is taken with exception probability.
95 * - The Proj of the Cond avoids an exception. It is taken with
96 * 1 - exception probability. */
97 /*------------------------------------------------------------------*/
104 Cond_prob_avoid_exception,
105 Cond_prob_exception_taken,
106 Cond_prob_was_exception_taken,
109 static int just_passed_a_Raise = 0;
110 static ir_node *Cond_list = NULL;
112 /* We do not use an extra set, as Projs are not yet in the existing one. */
113 void set_ProjX_probability(ir_node *n, Cond_prob prob) {
117 set_insert(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
120 Cond_prob get_ProjX_probability(ir_node *n) {
121 reg_exec_freq ef, *found;
124 found = set_find(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
127 return (Cond_prob)found->prob;
129 return Cond_prob_none;
132 /* A walker that only visits the nodes we want to see. */
135 my_irg_walk_2_both(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env) {
137 set_irn_visited(node, current_ir_graph->visited);
141 if (node->op != op_Block) {
143 if (node->op == op_Proj)
144 pred = get_irn_n(node, 0);
146 pred = get_irn_n(node, -1);
147 if (pred->visited < current_ir_graph->visited)
148 my_irg_walk_2_both(pred, pre, post, env);
152 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
153 ir_node *pred = get_irn_n(node, i);
154 if (pred->visited < current_ir_graph->visited)
155 my_irg_walk_2_both(pred, pre, post, env);
159 if (node->op == op_End) {
160 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
161 ir_node *pred = get_irn_n(node, i);
162 if ((pred->op == op_Block) && (pred->visited < current_ir_graph->visited))
163 my_irg_walk_2_both(pred, pre, post, env);
169 static void my_irg_walk_current_graph(irg_walk_func *pre, irg_walk_func *post, void *env) {
170 inc_irg_visited(current_ir_graph);
171 my_irg_walk_2_both(get_irg_end(current_ir_graph), pre, post, env);
175 static void walk_pre(ir_node *n, void *env) {
177 if (get_irn_op(n) == op_Raise)
178 just_passed_a_Raise = 1;
180 if ( (get_irn_op(n) == op_Proj)
181 && (get_irn_op(get_Proj_pred(n)) == op_Cond)
182 && (just_passed_a_Raise)) {
184 ir_node *c = get_Proj_pred(n);
186 /* If we already visited the other Proj, and it also leads to a Raise,
187 we are in the middle of something. Continue searching. */
188 assert(get_irn_n_outs(c) == 2 && "encountered a switch cond");
189 other_proj = get_irn_out(c, 0);
190 if (other_proj == n) other_proj = get_irn_out(c, 1);
191 if (get_ProjX_probability(other_proj) == Cond_prob_exception_taken) {
192 set_ProjX_probability(other_proj, Cond_prob_was_exception_taken);
193 /* Keep searching for the Proj, so keep just_passed_a_Raise. */
195 set_ProjX_probability(n, Cond_prob_exception_taken);
196 just_passed_a_Raise = 0;
200 if (get_irn_op(n) == op_Cond) {
201 set_irn_link(n, Cond_list);
206 static void walk_post(ir_node *n, void *env) {
208 if (get_irn_op(n) == op_Raise)
209 just_passed_a_Raise = 0;
211 if ( (get_irn_op(n) == op_Proj)
212 && (get_irn_op(get_Proj_pred(n)) == op_Cond)
213 && ((get_ProjX_probability(n) == Cond_prob_exception_taken) ||
214 (get_ProjX_probability(n) == Cond_prob_was_exception_taken) )) {
215 just_passed_a_Raise = 1;
219 /** Precompute which Conds test for an exception.
221 * Operates on current_ir_graph. */
222 void precompute_cond_evaluation(void) {
225 compute_irg_outs(current_ir_graph);
227 just_passed_a_Raise = 0;
229 my_irg_walk_current_graph(walk_pre, walk_post, NULL);
231 for (c = Cond_list; c; c = get_irn_link(c)) {
234 assert(get_irn_n_outs(c) == 2 && "encountered a switch cond");
235 p0 = get_irn_out(c, 0);
236 p1 = get_irn_out(c, 1);
238 /* both are exceptions */
239 if ((get_ProjX_probability(p0) == Cond_prob_exception_taken) &&
240 (get_ProjX_probability(p1) == Cond_prob_exception_taken) ) {
241 assert(0 && "I tried to avoid these!");
243 set_ProjX_probability(p0, Cond_prob_normal);
244 set_ProjX_probability(p1, Cond_prob_normal);
247 /* p0 is exception */
248 else if (get_ProjX_probability(p0) == Cond_prob_exception_taken) {
249 set_ProjX_probability(p1, Cond_prob_avoid_exception);
252 /* p1 is exception */
253 else if (get_ProjX_probability(p1) == Cond_prob_exception_taken) {
254 set_ProjX_probability(p0, Cond_prob_avoid_exception);
257 /* none is exception */
259 set_ProjX_probability(p0, Cond_prob_normal);
260 set_ProjX_probability(p1, Cond_prob_normal);
265 int is_fragile_Proj(ir_node *n) {
266 return is_Proj(n) && (get_ProjX_probability(n) == Cond_prob_exception_taken);
269 /*------------------------------------------------------------------*/
270 /* The algorithm to compute the execution frequencies.
272 * Walk the control flow loop tree which we consider the interval
273 * tree. Compute the execution for the lowest loop, add inner loops
274 * to worklist. Consider the inner loops as simple nodes. Check that
275 * there is only one loop header in each loop. */
276 /*------------------------------------------------------------------*/
278 static double exception_prob = 0.001;
280 static INLINE int is_loop_head(ir_node *cond) {
284 /** Weight a single region in edge.
286 * Given all outs of the predecessor region, we can compute the weight of
287 * this single edge. */
288 static INLINE double get_weighted_region_exec_freq(void *reg, int pos) {
289 void *pred_reg = get_region_in(reg, pos);
290 double res, full_freq = get_region_exec_freq (pred_reg);
291 int n_outs = get_region_n_outs (pred_reg);
292 int n_exc_outs = get_region_n_exc_outs(pred_reg);
295 if (is_ir_node(reg)) {
296 cfop = get_Block_cfgpred((ir_node *)reg, pos);
297 if (is_Proj(cfop) && (get_irn_op(get_Proj_pred(cfop)) != op_Cond))
298 cfop = skip_Proj(cfop);
300 assert(is_ir_loop(reg));
301 cfop = get_loop_cfop(reg, pos);
304 if (is_fragile_op(cfop) || is_fragile_Proj(cfop)) {
305 res = full_freq * exception_prob;
308 /* Equally distribute the weight after exceptions to the left over outs. */
309 res = (full_freq *(1 - exception_prob * n_exc_outs)) / (n_outs - n_exc_outs);
315 static INLINE void compute_region_freqency(void *reg, double head_weight) {
316 int i, n_ins = get_region_n_ins(reg);
319 //printf("head weight %lf: ", head_weight); DDMR(reg);
321 for (i = 0; i < n_ins; ++i) {
322 void *pred_reg = get_region_in(reg, i);
324 my_freq += get_weighted_region_exec_freq(reg, i);
328 if (my_freq == 0.0) {
329 /* All preds are from outer loop. We are a head or so. */
330 my_freq = head_weight;
332 set_region_exec_freq(reg, my_freq);
335 static void check_proper_head(ir_loop *l, void *reg) {
336 int i, n_ins = get_region_n_ins(reg);
337 for (i = 0; i < n_ins; ++i) {
338 assert(!get_region_in(reg, i));
342 /* Compute the ex freq for current_ir_graph */
343 static void compute_frequency(int default_loop_weight) {
344 ir_loop *outermost_l = get_irg_loop(current_ir_graph);
345 pdeq *block_worklist = new_pdeq1(outermost_l);
347 /* Outermost start is considered a loop head. We will soon multiply
348 by default_loop_weight. */
349 set_region_exec_freq(outermost_l, 1.0/default_loop_weight);
351 while (!pdeq_empty(block_worklist)) {
352 ir_loop *l = (ir_loop *)pdeq_getl(block_worklist);
353 int i, n_elems = get_loop_n_elements(l);
355 /* The header is initialized with the frequency of the full loop times the iteration weight. */
356 check_proper_head(l, get_loop_element(l, 0).son);
358 for (i = 0; i < n_elems; ++i) {
359 loop_element e = get_loop_element(l, i);
360 if (is_ir_loop(e.son)) pdeq_putr(block_worklist, e.son);
361 compute_region_freqency(e.son, default_loop_weight * get_region_exec_freq(l));
364 del_pdeq(block_worklist);
367 /* Compute the execution frequency for all blocks in the given
370 * irg: The graph to be analyzed.
371 * default_loop_weight: The number of executions of a loop.
373 void compute_execution_frequency(ir_graph *irg, int default_loop_weight, double exception_probability) {
374 ir_graph *rem = current_ir_graph;
375 current_ir_graph = irg;
376 exception_prob = exception_probability;
377 if (!exec_freq_set) exec_freq_set = new_set(exec_freq_cmp, 256);
379 precompute_cond_evaluation();
380 construct_intervals(current_ir_graph);
381 compute_frequency(default_loop_weight);
383 set_irg_exec_freq_state(irg, exec_freq_consistent);
384 if (get_irp_exec_freq_state() == exec_freq_none)
385 set_irp_exec_freq_state(exec_freq_inconsistent);
388 dump_loop_tree (current_ir_graph, "-execfreq");
389 dump_ir_block_graph(current_ir_graph, "-execfreq");
390 dump_interval_graph(current_ir_graph, "-execfreq");
393 current_ir_graph = rem;
397 void compute_execution_frequencies(int default_loop_weight, double exception_probability) {
398 int i, n_irgs = get_irp_n_irgs();
400 for (i = 0; i < n_irgs; ++i) {
401 compute_execution_frequency(get_irp_irg(i), default_loop_weight, exception_probability);
403 set_irp_exec_freq_state(exec_freq_consistent);
406 /** free occupied memory, reset */
407 void free_execution_frequency(void) {
408 int i, n_irgs = get_irp_n_irgs();
410 del_set(exec_freq_set);
412 for (i = 0; i < n_irgs; ++i)
413 set_irg_exec_freq_state(get_irp_irg(i), exec_freq_none);
414 set_irp_exec_freq_state(exec_freq_none);
417 exec_freq_state get_irg_exec_freq_state(ir_graph *irg) {
418 return irg->execfreq_state;
420 void set_irg_exec_freq_state(ir_graph *irg, exec_freq_state s) {
421 if ((get_irp_exec_freq_state() == exec_freq_consistent && s != exec_freq_consistent) ||
422 (get_irp_exec_freq_state() == exec_freq_none && s != exec_freq_none))
423 irp->execfreq_state = exec_freq_inconsistent;
424 irg->execfreq_state = s;
427 /* Sets irg and irp exec freq state to inconsistent if it is set to consistent. */
428 void set_irg_exec_freq_state_inconsistent(ir_graph *irg) {
429 if (get_irg_exec_freq_state(irg) == exec_freq_consistent)
430 set_irg_exec_freq_state(irg, exec_freq_inconsistent);
433 void set_irp_exec_freq_state(exec_freq_state s) {
434 irp->execfreq_state = s;
437 exec_freq_state get_irp_exec_freq_state(void) {
438 return irp->execfreq_state;
441 /* Sets irp and all irg exec freq states to inconsistent if it is set to consistent. */
442 void set_irp_exec_freq_state_inconsistent(void) {
443 if (get_irp_exec_freq_state() != exec_freq_none) {
444 int i, n_irgs = get_irp_n_irgs();
445 set_irp_exec_freq_state(exec_freq_inconsistent);
446 for (i = 0; i < n_irgs; ++i) {
447 ir_graph *irg = get_irp_irg(i);
448 if (get_irg_exec_freq_state(irg) != exec_freq_none)
449 irg->execfreq_state = exec_freq_inconsistent;