Implement binary emitter for Store8Bit.
[libfirm] / ir / ana / execution_frequency.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief     Compute an estimate of basic block executions.
23  * @author    Goetz Lindenmaier
24  * @date      5.11.2004
25  * @version   $Id$
26  */
27 #include "config.h"
28
29 #include "execution_frequency.h"
30
31 #include "set.h"
32 #include "pdeq.h"
33 #include "hashptr.h"
34
35 #include "irprog_t.h"
36 #include "irgraph_t.h"
37 #include "irnode_t.h"
38 #include "irloop.h"
39 #include "irgwalk.h"
40
41 #include "interval_analysis.h"
42
43 void set_irp_exec_freq_state(exec_freq_state s);
44
45 /*------------------------------------------------------------------*/
46 /* A hashmap mapping the frequency to block and loop nodes.  Block
47  * and loop nodes are regions.                                      */
48 /*------------------------------------------------------------------*/
49
50 typedef struct {
51   void   *reg;
52   double  freq;
53   int     prob;
54 } reg_exec_freq;
55
56 /* We use this set for all nodes in all irgraphs. */
57 static set *exec_freq_set = NULL;
58
59 static int exec_freq_cmp(const void *e1, const void *e2, size_t size) {
60   reg_exec_freq *ef1 = (reg_exec_freq *)e1;
61   reg_exec_freq *ef2 = (reg_exec_freq *)e2;
62   (void) size;
63
64   return (ef1->reg != ef2->reg);
65 }
66
67 static inline unsigned int exec_freq_hash(reg_exec_freq *e) {
68   return HASH_PTR(e->reg);
69 }
70
71 static inline void set_region_exec_freq(void *reg, double freq) {
72   reg_exec_freq ef;
73   ef.reg  = reg;
74   ef.freq = freq;
75   set_insert(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
76 }
77
78 double get_region_exec_freq(void *reg) {
79   reg_exec_freq ef, *found;
80   ef.reg  = reg;
81   assert(exec_freq_set);
82
83   found = set_find(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
84
85   /* Not found if information is invalid. */
86   if (found)
87     return found->freq;
88   else
89     return 0;
90 }
91
92 /* Returns the number of times the block is executed. */
93 double     get_Block_exec_freq(ir_node *b) {
94   return get_region_exec_freq((void *)b);
95 }
96
97 double get_irn_exec_freq(ir_node *n) {
98   if (!is_Block(n)) n = get_nodes_block(n);
99   return get_Block_exec_freq(n);
100 }
101
102
103 /*------------------------------------------------------------------*/
104 /* A algorithm that precomputes whether Conds lead to an exception.
105  * Computes a field for all Projs from Conds that says the following:
106  *   - The Proj projs from a normal dual Cond with probability 50:50
107  *   - This Proj of the Cond leads to an exception, i.e., a raise node.
108  *     It is taken with exception probability.
109  *   - The Proj of the Cond avoids an exception.  It is taken with
110  *     1 - exception probability.                                   */
111 /*------------------------------------------------------------------*/
112
113 #include "irouts.h"
114
115 typedef enum {
116   Cond_prob_none,
117   Cond_prob_normal,
118   Cond_prob_avoid_exception,
119   Cond_prob_exception_taken,
120   Cond_prob_was_exception_taken,
121 } Cond_prob;
122
123 static int just_passed_a_Raise = 0;
124 static ir_node *Cond_list = NULL;
125
126 /* We do not use an extra set, as Projs are not yet in the existing one. */
127 void set_ProjX_probability(ir_node *n, Cond_prob prob) {
128   reg_exec_freq ef;
129   ef.reg  = n;
130   ef.prob = prob;
131   set_insert(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
132 }
133
134 Cond_prob get_ProjX_probability(ir_node *n) {
135   reg_exec_freq ef, *found;
136   ef.reg  = n;
137
138   found = set_find(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
139
140   if (found)
141     return (Cond_prob)found->prob;
142   else
143     return Cond_prob_none;
144 }
145
146 /* A walker that only visits the nodes we want to see. */
147
148 static void
149 my_irg_walk_2_both(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env) {
150   int i;
151   set_irn_visited(node, current_ir_graph->visited);
152
153   pre(node, env);
154
155   if (node->op != op_Block) {
156     ir_node *pred;
157     if (node->op == op_Proj)
158       pred = get_irn_n(node, 0);
159     else
160       pred = get_irn_n(node, -1);
161     if (pred->visited < current_ir_graph->visited)
162       my_irg_walk_2_both(pred, pre, post, env);
163   }
164
165   else {  /* a Block */
166     for (i = get_irn_arity(node) - 1; i >= 0; --i) {
167       ir_node *pred = get_irn_n(node, i);
168       if (pred->visited < current_ir_graph->visited)
169         my_irg_walk_2_both(pred, pre, post, env);
170     }
171   }
172
173   if (node->op == op_End) {
174     for (i = get_irn_arity(node) - 1; i >= 0; --i) {
175       ir_node *pred = get_irn_n(node, i);
176       if ((pred->op == op_Block) && (pred->visited < current_ir_graph->visited))
177         my_irg_walk_2_both(pred, pre, post, env);
178     }
179   }
180
181   post(node, env);
182 }
183 static void my_irg_walk_current_graph(irg_walk_func *pre, irg_walk_func *post, void *env) {
184   inc_irg_visited(current_ir_graph);
185   my_irg_walk_2_both(get_irg_end(current_ir_graph), pre, post, env);
186 }
187
188
189 static void walk_pre(ir_node *n, void *env)
190 {
191   (void) env;
192   if (is_Raise(n))
193     just_passed_a_Raise = 1;
194
195   if (get_irn_op(n) == op_Proj  &&
196       is_Cond(get_Proj_pred(n)) &&
197       just_passed_a_Raise) {
198     ir_node *other_proj;
199     ir_node *c = get_Proj_pred(n);
200
201     /* If we already visited the other Proj, and it also leads to a Raise,
202        we are in the middle of something. Continue searching. */
203     assert(get_irn_n_outs(c) == 2 && "encountered a switch cond");
204     other_proj = get_irn_out(c, 0);
205     if (other_proj == n) other_proj = get_irn_out(c, 1);
206     if (get_ProjX_probability(other_proj) == Cond_prob_exception_taken) {
207       set_ProjX_probability(other_proj, Cond_prob_was_exception_taken);
208       /* Keep searching for the Proj, so keep just_passed_a_Raise. */
209     } else {
210       set_ProjX_probability(n, Cond_prob_exception_taken);
211       just_passed_a_Raise = 0;
212     }
213   }
214
215   if (is_Cond(n)) {
216     set_irn_link(n, Cond_list);
217     Cond_list = n;
218   }
219 }
220
221 static void walk_post(ir_node *n, void *env)
222 {
223   (void) env;
224   if (is_Raise(n))
225     just_passed_a_Raise = 0;
226
227   if (get_irn_op(n) == op_Proj  &&
228       is_Cond(get_Proj_pred(n)) && (
229         get_ProjX_probability(n) == Cond_prob_exception_taken ||
230         get_ProjX_probability(n) == Cond_prob_was_exception_taken
231       )) {
232     just_passed_a_Raise = 1;
233   }
234 }
235
236 /** Precompute which Conds test for an exception.
237  *
238  *  Operates on current_ir_graph. */
239 void precompute_cond_evaluation(void) {
240   ir_node *c;
241
242   compute_irg_outs(current_ir_graph);
243
244   just_passed_a_Raise = 0;
245   Cond_list = NULL;
246   my_irg_walk_current_graph(walk_pre, walk_post, NULL);
247
248   for (c = Cond_list; c; c = get_irn_link(c)) {
249     ir_node *p0, *p1;
250
251     assert(get_irn_n_outs(c) == 2 && "encountered a switch cond");
252     p0 = get_irn_out(c, 0);
253     p1 = get_irn_out(c, 1);
254
255     /* both are exceptions */
256     if ((get_ProjX_probability(p0) == Cond_prob_exception_taken) &&
257         (get_ProjX_probability(p1) == Cond_prob_exception_taken)   ) {
258       assert(0 && "I tried to avoid these!");
259       /* It's a */
260       set_ProjX_probability(p0, Cond_prob_normal);
261       set_ProjX_probability(p1, Cond_prob_normal);
262     }
263
264     /* p0 is exception */
265     else if (get_ProjX_probability(p0) == Cond_prob_exception_taken) {
266       set_ProjX_probability(p1, Cond_prob_avoid_exception);
267     }
268
269     /* p1 is exception */
270     else if (get_ProjX_probability(p1) == Cond_prob_exception_taken) {
271       set_ProjX_probability(p0, Cond_prob_avoid_exception);
272     }
273
274     /* none is exception */
275     else {
276       set_ProjX_probability(p0, Cond_prob_normal);
277       set_ProjX_probability(p1, Cond_prob_normal);
278     }
279   }
280 }
281
282 int is_fragile_Proj(ir_node *n) {
283   return is_Proj(n) && (get_ProjX_probability(n) == Cond_prob_exception_taken);
284 }
285
286 /*------------------------------------------------------------------*/
287 /* The algorithm to compute the execution frequencies.
288  *
289  * Walk the control flow loop tree which we consider the interval
290  * tree.  Compute the execution for the lowest loop, add inner loops
291  * to worklist.  Consider the inner loops as simple nodes.  Check that
292  * there is only one loop header in each loop.                      */
293 /*------------------------------------------------------------------*/
294
295 static double exception_prob = 0.001;
296
297 static inline int is_loop_head(ir_node *cond)
298 {
299   (void) cond;
300   return 0;
301 }
302
303 /** Weight a single region in edge.
304  *
305  *  Given all outs of the predecessor region, we can compute the weight of
306  *  this single edge. */
307 static inline double get_weighted_region_exec_freq(void *reg, int pos) {
308   void *pred_reg        = get_region_in(reg, pos);
309   double res, full_freq = get_region_exec_freq (pred_reg);
310   int n_outs            = get_region_n_outs    (pred_reg);
311   int n_exc_outs        = get_region_n_exc_outs(pred_reg);
312
313   ir_node *cfop;
314   if (is_ir_node(reg)) {
315     cfop = get_Block_cfgpred((ir_node *)reg, pos);
316     if (is_Proj(cfop) && !is_Cond(get_Proj_pred(cfop)))
317       cfop = skip_Proj(cfop);
318   } else {
319     assert(is_ir_loop(reg));
320     cfop = get_loop_cfop(reg, pos);
321   }
322
323   if (is_fragile_op(cfop) || is_fragile_Proj(cfop)) {
324     res = full_freq * exception_prob;
325   } else {
326
327     /* Equally distribute the weight after exceptions to the left over outs. */
328     res = (full_freq *(1 - exception_prob * n_exc_outs)) / (n_outs - n_exc_outs);
329   }
330
331   return res;
332 }
333
334 static inline void compute_region_freqency(void *reg, double head_weight) {
335   int i, n_ins = get_region_n_ins(reg);
336   double my_freq = 0;
337
338   //printf("head weight %lf: ", head_weight); DDMR(reg);
339
340   for (i = 0; i < n_ins; ++i) {
341     void *pred_reg = get_region_in(reg, i);
342     if (pred_reg) {
343       my_freq += get_weighted_region_exec_freq(reg, i);
344     }
345   }
346
347   if (my_freq == 0.0) {
348     /* All preds are from outer loop. We are a head or so. */
349     my_freq = head_weight;
350   }
351   set_region_exec_freq(reg, my_freq);
352 }
353
354 static void check_proper_head(ir_loop *l, void *reg)
355 {
356   int i, n_ins = get_region_n_ins(reg);
357   (void) l;
358   for (i = 0; i < n_ins; ++i) {
359     assert(!get_region_in(reg, i));
360   }
361 }
362
363 /* Compute the ex freq for current_ir_graph */
364 static void compute_frequency(int default_loop_weight) {
365   ir_loop *outermost_l = get_irg_loop(current_ir_graph);
366   pdeq *block_worklist = new_pdeq1(outermost_l);
367
368   /* Outermost start is considered a loop head.  We will soon multiply
369      by default_loop_weight. */
370   set_region_exec_freq(outermost_l, 1.0/default_loop_weight);
371
372   while (!pdeq_empty(block_worklist)) {
373     ir_loop *l = (ir_loop *)pdeq_getl(block_worklist);
374     int i, n_elems = get_loop_n_elements(l);
375
376     /* The header is initialized with the frequency of the full loop times the iteration weight. */
377     check_proper_head(l, get_loop_element(l, 0).son);
378
379     for (i = 0; i < n_elems; ++i) {
380       loop_element e = get_loop_element(l, i);
381       if (is_ir_loop(e.son)) pdeq_putr(block_worklist, e.son);
382       compute_region_freqency(e.son, default_loop_weight * get_region_exec_freq(l));
383     }
384   }
385   del_pdeq(block_worklist);
386 }
387
388 /* Compute the execution frequency for all blocks in the given
389  * graph.
390  *
391  * irg:                 The graph to be analyzed.
392  * default_loop_weight: The number of executions of a loop.
393  */
394 void compute_execution_frequency(ir_graph *irg, int default_loop_weight, double exception_probability) {
395   ir_graph *rem = current_ir_graph;
396   current_ir_graph = irg;
397   exception_prob = exception_probability;
398   if (!exec_freq_set) exec_freq_set = new_set(exec_freq_cmp, 256);
399
400   precompute_cond_evaluation();
401   construct_intervals(current_ir_graph);
402   compute_frequency(default_loop_weight);
403
404   set_irg_exec_freq_state(irg, exec_freq_consistent);
405   if (get_irp_exec_freq_state() == exec_freq_none)
406     set_irp_exec_freq_state(exec_freq_inconsistent);
407
408   /*
409     dump_loop_tree     (current_ir_graph, "-execfreq");
410     dump_ir_block_graph(current_ir_graph, "-execfreq");
411     dump_interval_graph(current_ir_graph, "-execfreq");
412   */
413
414   current_ir_graph = rem;
415 }
416
417
418 void compute_execution_frequencies(int default_loop_weight, double exception_probability) {
419   int i, n_irgs = get_irp_n_irgs();
420   free_intervals();
421   for (i = 0; i < n_irgs; ++i) {
422     compute_execution_frequency(get_irp_irg(i), default_loop_weight, exception_probability);
423   }
424   set_irp_exec_freq_state(exec_freq_consistent);
425 }
426
427 /** free occupied memory, reset */
428 void free_execution_frequency(void) {
429   int i, n_irgs = get_irp_n_irgs();
430   free_intervals();
431   del_set(exec_freq_set);
432
433   for (i = 0; i < n_irgs; ++i)
434     set_irg_exec_freq_state(get_irp_irg(i), exec_freq_none);
435   set_irp_exec_freq_state(exec_freq_none);
436 }
437
438 exec_freq_state get_irg_exec_freq_state(ir_graph *irg) {
439   return irg->execfreq_state;
440 }
441 void            set_irg_exec_freq_state(ir_graph *irg, exec_freq_state s) {
442   if ((get_irp_exec_freq_state() == exec_freq_consistent && s != exec_freq_consistent) ||
443       (get_irp_exec_freq_state() == exec_freq_none       && s != exec_freq_none))
444     irp->execfreq_state = exec_freq_inconsistent;
445   irg->execfreq_state = s;
446 }
447
448 /* Sets irg and irp exec freq state to inconsistent if it is set to consistent. */
449 void            set_irg_exec_freq_state_inconsistent(ir_graph *irg) {
450   if (get_irg_exec_freq_state(irg) == exec_freq_consistent)
451     set_irg_exec_freq_state(irg, exec_freq_inconsistent);
452 }
453
454 void set_irp_exec_freq_state(exec_freq_state s) {
455   irp->execfreq_state = s;
456 }
457
458 exec_freq_state get_irp_exec_freq_state(void) {
459   return irp->execfreq_state;
460 }
461
462 /* Sets irp and all irg exec freq states to inconsistent if it is set to consistent. */
463 void            set_irp_exec_freq_state_inconsistent(void) {
464   if (get_irp_exec_freq_state() != exec_freq_none) {
465     int i, n_irgs = get_irp_n_irgs();
466     set_irp_exec_freq_state(exec_freq_inconsistent);
467     for (i = 0; i < n_irgs; ++i) {
468       ir_graph *irg = get_irp_irg(i);
469       if (get_irg_exec_freq_state(irg) != exec_freq_none)
470         irg->execfreq_state = exec_freq_inconsistent;
471     }
472   }
473 }