2 * Interblock liveness analysis.
3 * @author Sebastian Hack
11 #include "iredges_t.h"
13 #include "irprintf_t.h"
17 #include "besched_t.h"
19 #define DBG_MODULE "firm.be.liveness"
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 *)
25 /** The offset of the liveness information in a firm node. */
26 size_t live_irg_data_offset = 0;
28 void be_liveness_init(void)
30 live_irg_data_offset = register_additional_graph_data(sizeof(irn_live_t));
33 static INLINE void mark_live_in(ir_node *block, const ir_node *irn)
35 _get_or_set_live(block, irn, live_state_in);
38 static INLINE void mark_live_out(ir_node *block, const ir_node *irn)
40 _get_or_set_live(block, irn, live_state_out | live_state_end);
43 static INLINE void mark_live_end(ir_node *block, const ir_node *irn)
45 _get_or_set_live(block, irn, live_state_end);
49 * Mark a node (value) live out at a certain block. Do this also
50 * transitively, i.e. if the block is not the block of the value's
51 * definition, all predecessors are also marked live.
52 * @param def The node (value).
53 * @param block The block to mark the value live out of.
54 * @param visited A set were all visited blocks are recorded.
55 * @param is_true_out Is the node real out there or only live at the end
58 static void live_end_at_block(ir_node *def, ir_node *block,
59 pset *visited, int is_true_out)
61 mark_live_end(block, def);
63 mark_live_out(block, def);
65 if(!pset_find_ptr(visited, block)) {
67 pset_insert_ptr(visited, block);
70 * If this block is not the definition block, we have to go up
73 if(get_nodes_block(def) != block) {
76 mark_live_in(block, def);
78 for(i = 0, n = get_irn_arity(block); i < n; ++i)
79 live_end_at_block(def, get_Block_cfgpred_block(block, i), visited, 1);
86 * Liveness analysis for a value.
87 * This functions is meant to be called by a firm walker, to compute the
88 * set of all blocks a value is live in.
89 * @param irn The node (value).
92 static void liveness_for_node(ir_node *irn, void *env)
94 const ir_edge_t *edge;
98 /* Don't compute liveness information for non-data nodes. */
99 if(!is_data_node(irn))
102 visited = pset_new_ptr(512);
103 def_block = get_nodes_block(irn);
105 /* Go over all uses of the value */
106 foreach_out_edge(irn, edge) {
107 ir_node *use = edge->src;
111 * If the usage is no data node, skip this use, since it does not
112 * affect the liveness of the node.
114 if(!is_data_node(use))
117 /* Get the block where the usage is in. */
118 use_block = get_nodes_block(use);
121 * If the use is a phi function, determine the corresponding block
122 * through which the value reaches the phi function and mark the
123 * value as live out of that block.
126 ir_node *pred_block = get_Block_cfgpred_block(use_block, edge->pos);
127 live_end_at_block(irn, pred_block, visited, 0);
131 * Else, the value is live in at this block. Mark it and call live
132 * out on the predecessors.
134 else if(def_block != use_block) {
137 mark_live_in(use_block, irn);
139 for(i = 0, n = get_irn_arity(use_block); i < n; ++i) {
140 ir_node *pred_block = get_nodes_block(get_irn_n(use_block, i));
141 live_end_at_block(irn, pred_block, visited, 1);
149 static int cmp_irn_live(const void *a, const void *b, size_t size)
151 const irn_live_t *p = a;
152 const irn_live_t *q = b;
154 return !(p->block == q->block && p->irn == q->irn);
157 static int (*old_dump_block_func)(ir_node *self, FILE *F, dump_reason_t reason) = NULL;
159 static int dump_block_func(ir_node *self, FILE *F, dump_reason_t reason)
162 case dump_node_opcode_txt:
163 fprintf(F, get_irn_opname(self));
165 case dump_node_mode_txt:
166 fprintf(F, get_irn_modename(self));
168 case dump_node_nodeattr_txt:
170 case dump_node_info_txt:
171 if(!get_irg_live_info(get_irn_irg(self))->live)
174 fprintf(F, "liveness information:\n");
177 live_foreach(self, li) {
178 ir_fprintf(F, "%+F", li->irn);
195 void be_liveness(ir_graph *irg)
197 irg_live_info_t *live_info = get_irg_live_info(irg);
199 del_set(live_info->live);
201 live_info->live = new_set(cmp_irn_live, 8192);
202 irg_walk_graph(irg, liveness_for_node, NULL, NULL);
204 old_dump_block_func = op_Block->ops.dump_node;
205 op_Block->ops.dump_node = dump_block_func;
208 static void dump_liveness_walker(ir_node *bl, void *data)
211 const irn_live_t *li;
214 ir_fprintf(f, "%+F\n", bl);
215 live_foreach(bl, li) {
227 ir_fprintf(f, "\t%+20F %s\n", li->irn, buf);
231 void be_liveness_dump(ir_graph *irg, FILE *f)
233 irg_block_walk_graph(irg, dump_liveness_walker, NULL, f);
236 void be_liveness_dumpto(ir_graph *irg, const char *cls_name)
240 ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls_name);
241 if((f = fopen(buf, "wt")) != NULL) {
242 be_liveness_dump(irg, f);
247 static void dom_check(ir_node *irn, void *data)
249 if(!is_Block(irn) && irn != get_irg_end(get_irn_irg(irn))) {
251 ir_node *bl = get_nodes_block(irn);
253 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
254 ir_node *op = get_irn_n(irn, i);
255 ir_node *def_bl = get_nodes_block(op);
256 ir_node *use_bl = bl;
259 use_bl = get_Block_cfgpred_block(bl, i);
261 if(!block_dominates(def_bl, use_bl)) {
262 ir_fprintf(stderr, "%+F in %+F must dominate %+F for user %+F\n", op, def_bl, use_bl, irn);
269 void be_check_dominance(ir_graph *irg)
271 irg_walk_graph(irg, dom_check, NULL, NULL);
274 pset *be_liveness_transfer(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_node *irn, pset *live)
276 firm_dbg_module_t *dbg = firm_dbg_register(DBG_MODULE);
280 DBG((dbg, LEVEL_1, "%+F\n", irn));
281 for(x = pset_first(live); x; x = pset_next(live))
282 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
284 if(arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
285 pset_remove_ptr(live, irn);
287 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
288 ir_node *op = get_irn_n(irn, i);
290 if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
291 pset_insert_ptr(live, op);
297 pset *be_liveness_end_of_block(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *bl, pset *live)
301 live_foreach(bl, li) {
302 ir_node *irn = (ir_node *) li->irn;
303 if(live_is_end(li) && arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
304 pset_insert_ptr(live, irn);
310 pset *be_liveness_nodes_live_at(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
312 firm_dbg_module_t *dbg = firm_dbg_register(DBG_MODULE);
313 const ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
316 be_liveness_end_of_block(arch_env, cls, bl, live);
318 sched_foreach_reverse(bl, irn) {
320 * If we encounter the node we want to insert the Perm after,
321 * exit immediately, so that this node is still live
326 be_liveness_transfer(arch_env, cls, irn, live);