Add some Processor specific instruction selections
[libfirm] / ir / be / belive.c
1 /**
2  * Interblock liveness analysis.
3  * @author Sebastian Hack
4  * @date 6.12.2004
5  */
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include "impl.h"
11 #include "iredges_t.h"
12 #include "irgwalk.h"
13 #include "irprintf_t.h"
14
15 #include "beutil.h"
16 #include "belive_t.h"
17 #include "besched_t.h"
18
19 #define DBG_MODULE "firm.be.liveness"
20
21 FIRM_IMPL2(is_live_in, int, const ir_node *, const ir_node *)
22 FIRM_IMPL2(is_live_out, int, const ir_node *, const ir_node *)
23 FIRM_IMPL2(is_live_end, int, const ir_node *, const ir_node *)
24
25 /** The offset of the liveness information in a firm node. */
26 size_t live_irg_data_offset = 0;
27
28 void be_liveness_init(void)
29 {
30   live_irg_data_offset = register_additional_graph_data(sizeof(irn_live_t));
31 }
32
33 /**
34  * Mark a node as live-in in a block.
35  */
36 static INLINE void mark_live_in(ir_node *block, const ir_node *irn)
37 {
38   _get_or_set_live(block, irn, live_state_in);
39 }
40
41 /**
42  * Mark a node as live-out in a block.
43  */
44 static INLINE void mark_live_out(ir_node *block, const ir_node *irn)
45 {
46   _get_or_set_live(block, irn, live_state_out | live_state_end);
47 }
48
49 /**
50  * Mark a node as live-end in a block.
51  */
52 static INLINE void mark_live_end(ir_node *block, const ir_node *irn)
53 {
54   _get_or_set_live(block, irn, live_state_end);
55 }
56
57 /**
58  * Mark a node (value) live out at a certain block. Do this also
59  * transitively, i.e. if the block is not the block of the value's
60  * definition, all predecessors are also marked live.
61  * @param def The node (value).
62  * @param block The block to mark the value live out of.
63  * @param visited A set were all visited blocks are recorded.
64  * @param is_true_out Is the node real out there or only live at the end
65  * of the block.
66  */
67 static void live_end_at_block(ir_node *def, ir_node *block,
68     pset *visited, int is_true_out)
69 {
70   mark_live_end(block, def);
71   if(is_true_out)
72     mark_live_out(block, def);
73
74   if(!pset_find_ptr(visited, block)) {
75
76     pset_insert_ptr(visited, block);
77
78     /*
79      * If this block is not the definition block, we have to go up
80      * further.
81      */
82     if(get_nodes_block(def) != block) {
83       int i, n;
84
85       mark_live_in(block, def);
86
87       for(i = 0, n = get_irn_arity(block); i < n; ++i)
88         live_end_at_block(def, get_Block_cfgpred_block(block, i), visited, 1);
89     }
90
91   }
92 }
93
94 /**
95  * Liveness analysis for a value.
96  * This functions is meant to be called by a firm walker, to compute the
97  * set of all blocks a value is live in.
98  * @param irn The node (value).
99  * @param env Ignored.
100  */
101 static void liveness_for_node(ir_node *irn, void *env)
102 {
103   const ir_edge_t *edge;
104   ir_node *def_block;
105   pset *visited;
106
107   /* Don't compute liveness information for non-data nodes. */
108   if(!is_data_node(irn))
109     return;
110
111   visited = pset_new_ptr(512);
112   def_block = get_nodes_block(irn);
113
114   /* Go over all uses of the value */
115   foreach_out_edge(irn, edge) {
116     ir_node *use = edge->src;
117     ir_node *use_block;
118
119     /*
120      * If the usage is no data node, skip this use, since it does not
121      * affect the liveness of the node.
122      */
123     if(!is_data_node(use))
124       continue;
125
126     /* Get the block where the usage is in. */
127     use_block = get_nodes_block(use);
128
129     /*
130      * If the use is a phi function, determine the corresponding block
131      * through which the value reaches the phi function and mark the
132      * value as live out of that block.
133      */
134     if(is_Phi(use)) {
135                         ir_node *pred_block = get_Block_cfgpred_block(use_block, edge->pos);
136                         live_end_at_block(irn, pred_block, visited, 0);
137     }
138
139     /*
140      * Else, the value is live in at this block. Mark it and call live
141      * out on the predecessors.
142      */
143     else if(def_block != use_block) {
144       int i, n;
145
146       mark_live_in(use_block, irn);
147
148       for(i = 0, n = get_irn_arity(use_block); i < n; ++i) {
149         ir_node *pred_block = get_nodes_block(get_irn_n(use_block, i));
150         live_end_at_block(irn, pred_block, visited, 1);
151       }
152     }
153   }
154
155   del_pset(visited);
156 }
157
158 /**
159  * Compare two live entries.
160  */
161 static int cmp_irn_live(const void *a, const void *b, size_t size)
162 {
163   const irn_live_t *p = a;
164   const irn_live_t *q = b;
165
166   return !(p->block == q->block && p->irn == q->irn);
167 }
168
169 static int (*old_dump_block_func)(ir_node *self, FILE *F, dump_reason_t reason) = NULL;
170
171 static int dump_block_func(ir_node *self, FILE *F, dump_reason_t reason)
172 {
173         switch(reason) {
174         case dump_node_opcode_txt:
175                 fprintf(F, get_irn_opname(self));
176                 break;
177         case dump_node_mode_txt:
178                 fprintf(F, get_irn_modename(self));
179                 break;
180         case dump_node_nodeattr_txt:
181                 break;
182         case dump_node_info_txt:
183                 if(!get_irg_live_info(get_irn_irg(self))->live)
184                         return 0;
185 #if 0
186                 fprintf(F, "liveness information:\n");
187                 {
188                         irn_live_t *li;
189                         live_foreach(self, li) {
190                                 ir_fprintf(F, "%+F", li->irn);
191                                 if(live_is_in(li))
192                                         fprintf(F, " in");
193                                 if(live_is_end(li))
194                                         fprintf(F, " end");
195                                 if(live_is_out(li))
196                                         fprintf(F, " out");
197
198                                 fprintf(F, "\n");
199                         }
200                 }
201 #endif
202         }
203
204         return 0;
205 }
206
207 /* Compute the inter block liveness for a graph. */
208 void be_liveness(ir_graph *irg)
209 {
210   irg_live_info_t *live_info = get_irg_live_info(irg);
211   if(live_info->live)
212     del_set(live_info->live);
213
214   live_info->live = new_set(cmp_irn_live, 8192);
215   irg_walk_graph(irg, liveness_for_node, NULL, NULL);
216
217   old_dump_block_func     = op_Block->ops.dump_node;
218   op_Block->ops.dump_node = dump_block_func;
219 }
220
221 /**
222  * Pre-walker: dump liveness data to a file
223  */
224 static void dump_liveness_walker(ir_node *bl, void *data)
225 {
226         FILE *f = data;
227         const irn_live_t *li;
228         char buf[64];
229
230         ir_fprintf(f, "%+F\n", bl);
231         live_foreach(bl, li) {
232                 strcpy(buf, "");
233
234                 if(live_is_in(li))
235                         strcat(buf, "in ");
236
237                 if(live_is_end(li))
238                         strcat(buf, "end ");
239
240                 if(live_is_out(li))
241                         strcat(buf, "out ");
242
243                 ir_fprintf(f, "\t%+20F %s\n", li->irn, buf);
244         }
245 }
246
247 /* Dump the liveness information for a graph. */
248 void be_liveness_dump(ir_graph *irg, FILE *f)
249 {
250         irg_block_walk_graph(irg, dump_liveness_walker, NULL, f);
251 }
252
253 /* Dump the liveness information for a graph. */
254 void be_liveness_dumpto(ir_graph *irg, const char *cls_name)
255 {
256         FILE *f;
257         char buf[128];
258         ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls_name);
259         if((f = fopen(buf, "wt")) != NULL) {
260                 be_liveness_dump(irg, f);
261                 fclose(f);
262         }
263 }
264
265 /**
266  * Walker: checks the every predecessors of a node dominate
267  * the note.
268  */
269 static void dom_check(ir_node *irn, void *data)
270 {
271         if(!is_Block(irn) && irn != get_irg_end(get_irn_irg(irn))) {
272                 int i, n;
273                 ir_node *bl = get_nodes_block(irn);
274
275                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
276                         ir_node *op     = get_irn_n(irn, i);
277                         ir_node *def_bl = get_nodes_block(op);
278                         ir_node *use_bl = bl;
279
280                         if(is_Phi(irn))
281                                 use_bl = get_Block_cfgpred_block(bl, i);
282
283                         if(!block_dominates(def_bl, use_bl)) {
284                                 ir_fprintf(stderr, "%+F in %+F must dominate %+F for user %+F\n", op, def_bl, use_bl, irn);
285                                 assert(0);
286                         }
287                 }
288         }
289 }
290
291 /* Check, if the SSA dominance property is fulfilled. */
292 void be_check_dominance(ir_graph *irg)
293 {
294         irg_walk_graph(irg, dom_check, NULL, NULL);
295 }
296
297 pset *be_liveness_transfer(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_node *irn, pset *live)
298 {
299         int i, n;
300         ir_node *x;
301         FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
302
303         DBG((dbg, LEVEL_1, "%+F\n", irn));
304         for(x = pset_first(live); x; x = pset_next(live))
305                 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
306
307         if(arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
308                 pset_remove_ptr(live, irn);
309
310         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
311                 ir_node *op = get_irn_n(irn, i);
312
313                 if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
314                         pset_insert_ptr(live, op);
315         }
316
317         return live;
318 }
319
320 pset *be_liveness_end_of_block(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *bl, pset *live)
321 {
322         irn_live_t *li;
323
324         live_foreach(bl, li) {
325                 ir_node *irn = (ir_node *) li->irn;
326                 if(live_is_end(li) && arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
327                         pset_insert_ptr(live, irn);
328         }
329
330         return live;
331 }
332
333 pset *be_liveness_nodes_live_at(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
334 {
335         const ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
336         ir_node *irn;
337
338         be_liveness_end_of_block(arch_env, cls, bl, live);
339         sched_foreach_reverse(bl, irn) {
340                 /*
341                  * If we encounter the node we want to insert the Perm after,
342                  * exit immediately, so that this node is still live
343                  */
344                 if(irn == pos)
345                         return live;
346
347                 be_liveness_transfer(arch_env, cls, irn, live);
348         }
349
350         return live;
351 }