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