bacf73e3ebc6af6294c98daf0cf97f16f94e765a
[libfirm] / ir / ana / execution_frequency.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/execution_frequency.c
4  * Purpose:     Compute an estimate of basic block executions.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     5.11.2004
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2004 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include "execution_frequency.h"
17
18 #include "firm_common_t.h"
19 #include "set.h"
20 #include "pdeq.h"
21
22 #include "irprog.h"
23 #include "irloop.h"
24
25 #include "interval_analysis.h"
26
27 /*------------------------------------------------------------------*/
28 /* A hashmap mapping the frequency to block and loop nodes.  Block
29  * and loop nodes are regions.                                      */
30 /*------------------------------------------------------------------*/
31
32 typedef struct {
33   void   *reg;
34   double  freq;
35 } reg_exec_freq;
36
37 /* We use this set for all nodes in all irgraphs. */
38 static set *exec_freq_set = NULL;
39
40 static int exec_freq_cmp(const void *e1, const void *e2, size_t size) {
41   reg_exec_freq *ef1 = (reg_exec_freq *)e1;
42   reg_exec_freq *ef2 = (reg_exec_freq *)e2;
43   return (ef1->reg != ef2->reg);
44 }
45
46 static INLINE unsigned int exec_freq_hash(void *e) {
47   unsigned int v = (unsigned int) ((reg_exec_freq *)e)->reg;
48   return v ^ (v>>8);
49 }
50
51 static INLINE void set_region_exec_freq(void *reg, double freq) {
52   reg_exec_freq ef;
53   ef.reg  = reg;
54   ef.freq = freq;
55   set_insert(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
56 }
57
58 INLINE double get_region_exec_freq(void *reg) {
59   reg_exec_freq ef, *found;
60   ef.reg  = reg;
61
62   found = set_find(exec_freq_set, &ef, sizeof(ef), exec_freq_hash(&ef));
63
64   /* Not found if information is invalid. */
65   if (found)
66     return found->freq;
67   else
68     return 0;
69 }
70
71 /* Returns the number of times the block is executed. */
72 double     get_Block_exec_freq(ir_node *b) {
73   return get_region_exec_freq((void *)b);
74 }
75
76 double get_irn_exec_freq(ir_node *n) {
77   if (!is_Block(n)) n = get_nodes_block(n);
78   return get_Block_exec_freq(n);
79 }
80
81
82 /*------------------------------------------------------------------*/
83 /* The algorithm to compute the execution freqencies.
84  *
85  * Walk the control flow loop tree which we consider the interval
86  * tree.  Compute the execution for the lowest loop, add inner loops
87  * to worklist.  Consider the inner loops as simple nodes.  Check that
88  * there is only one loop header in each loop.                      */
89 /*------------------------------------------------------------------*/
90
91 static double exception_prob = 0.001;
92
93 static INLINE int is_loop_head(ir_node *cond) {
94   return false;
95 }
96
97 static INLINE double get_weighted_region_exec_freq(void *reg, int pos) {
98   void *pred_reg = get_region_in(reg, pos);
99   double res, full_freq = get_region_exec_freq(pred_reg);
100   int n_outs     = get_region_n_outs    (pred_reg);
101   int n_exc_outs = get_region_n_exc_outs(pred_reg);
102
103   ir_node *cfop;
104   if (is_ir_node(reg)) {
105     cfop = skip_Proj(get_Block_cfgpred((ir_node *)reg, pos));
106   } else {
107     assert(is_ir_loop(reg));
108     cfop = get_loop_cfop(reg, pos);
109   }
110
111   if (is_fragile_op(cfop)) {
112     res = full_freq * exception_prob;
113   } else {
114
115     /* Equally distribute the weight after exceptions to the left over outs. */
116     res = (full_freq *(1 - exception_prob * n_exc_outs)) / (n_outs - n_exc_outs);
117   }
118
119   return res;
120 }
121
122 static INLINE void compute_region_freqency(void *reg, double head_weight) {
123   int i, n_ins = get_region_n_ins(reg);
124   double my_freq = 0;
125
126   //printf("head weight %lf: ", head_weight); DDMR(reg);
127
128   for (i = 0; i < n_ins; ++i) {
129     void *pred_reg = get_region_in(reg, i);
130     if (pred_reg) {
131       my_freq += get_weighted_region_exec_freq(reg, i);
132     }
133   }
134
135   if (my_freq == 0.0) {
136     /* All preds are from outer loop. We are a head or so. */
137     my_freq = head_weight;
138   }
139   set_region_exec_freq(reg, my_freq);
140 }
141
142 static void check_proper_head(ir_loop *l, void *reg) {
143   int i, n_ins = get_region_n_ins(reg);
144   for (i = 0; i < n_ins; ++i) {
145     assert(!get_region_in(reg, i));
146   }
147 }
148
149 /* Compute the ex freq for current_ir_graph */
150 static void compute_frequency(int default_loop_weight) {
151   ir_loop *outermost_l = get_irg_loop(current_ir_graph);
152   pdeq *block_worklist = new_pdeq1(outermost_l);
153
154   /* Outermost start is considered a loop head.  We will soon multiply
155      by default_loop_weight. */
156   set_region_exec_freq(outermost_l, 1.0/default_loop_weight);
157
158   while (!pdeq_empty(block_worklist)) {
159     ir_loop *l = (ir_loop *)pdeq_getl(block_worklist);
160     int i, n_elems = get_loop_n_elements(l);
161
162     /* The header is initialized with the freqency of the full loop times the iteration weight. */
163     check_proper_head(l, get_loop_element(l, 0).son);
164
165     for (i = 0; i < n_elems; ++i) {
166       loop_element e = get_loop_element(l, i);
167       if (is_ir_loop(e.son)) pdeq_putr(block_worklist, e.son);
168       compute_region_freqency(e.son, default_loop_weight * get_region_exec_freq(l));
169     }
170   }
171   del_pdeq(block_worklist);
172 }
173
174 /* Compute the execution frequency for all blocks in the given
175  * graph.
176  *
177  * irg:                 The graph to be analyzed.
178  * default_loop_weight: The number of executions of a loop.
179  */
180 void compute_execution_frequency(ir_graph *irg, int default_loop_weight, double exception_probability) {
181   ir_graph *rem = current_ir_graph;
182   current_ir_graph = irg;
183   exception_prob = exception_probability;
184   if (!exec_freq_set) exec_freq_set = new_set(exec_freq_cmp, 256);
185
186   construct_intervals(current_ir_graph);
187   compute_frequency(default_loop_weight);
188
189   /*
190     dump_loop_tree     (current_ir_graph, "-execfreq");
191     dump_ir_block_graph(current_ir_graph, "-execfreq");
192     dump_interval_graph(current_ir_graph, "-execfreq");
193   */
194
195   current_ir_graph = rem;
196 }
197
198
199 void compute_execution_frequencies(int default_loop_weight, double exception_probability) {
200   int i, n_irgs = get_irp_n_irgs();
201   free_intervals();
202   for (i = 0; i < n_irgs; ++i) {
203     compute_execution_frequency(get_irp_irg(i), default_loop_weight, exception_probability);
204   }
205 }
206
207 /** free occupied memory, reset */
208 void free_execution_frequency(void) {
209   free_intervals();
210   del_set(exec_freq_set);
211 }