2 * Interblock liveness analysis.
3 * @author Sebastian Hack
11 #include "iredges_t.h"
13 #include "irprintf_t.h"
18 FIRM_IMPL2(is_live_in, int, const ir_node *, const ir_node *)
19 FIRM_IMPL2(is_live_out, int, const ir_node *, const ir_node *)
20 FIRM_IMPL2(is_live_end, int, const ir_node *, const ir_node *)
22 /** The offset of the liveness information in a firm node. */
23 size_t live_irg_data_offset = 0;
25 void be_liveness_init(void)
27 live_irg_data_offset = register_additional_graph_data(sizeof(irn_live_t));
30 static INLINE void mark_live_in(ir_node *block, const ir_node *irn)
32 _get_or_set_live(block, irn, live_state_in);
35 static INLINE void mark_live_out(ir_node *block, const ir_node *irn)
37 _get_or_set_live(block, irn, live_state_out | live_state_end);
40 static INLINE void mark_live_end(ir_node *block, const ir_node *irn)
42 _get_or_set_live(block, irn, live_state_end);
46 * Mark a node (value) live out at a certain block. Do this also
47 * transitively, i.e. if the block is not the block of the value's
48 * definition, all predecessors are also marked live.
49 * @param def The node (value).
50 * @param block The block to mark the value live out of.
51 * @param visited A set were all visited blocks are recorded.
52 * @param is_true_out Is the node real out there or only live at the end
55 static void live_end_at_block(ir_node *def, ir_node *block,
56 pset *visited, int is_true_out)
58 mark_live_end(block, def);
60 mark_live_out(block, def);
62 if(!pset_find_ptr(visited, block)) {
64 pset_insert_ptr(visited, block);
67 * If this block is not the definition block, we have to go up
70 if(get_nodes_block(def) != block) {
73 mark_live_in(block, def);
75 for(i = 0, n = get_irn_arity(block); i < n; ++i)
76 live_end_at_block(def, get_Block_cfgpred_block(block, i), visited, 1);
83 * Liveness analysis for a value.
84 * This functions is meant to be called by a firm walker, to compute the
85 * set of all blocks a value is live in.
86 * @param irn The node (value).
89 static void liveness_for_node(ir_node *irn, void *env)
91 const ir_edge_t *edge;
95 /* Don't compute liveness information for non-data nodes. */
96 if(!is_data_node(irn))
99 visited = pset_new_ptr(512);
100 def_block = get_nodes_block(irn);
102 /* Go over all uses of the value */
103 foreach_out_edge(irn, edge) {
104 ir_node *use = edge->src;
108 * If the usage is no data node, skip this use, since it does not
109 * affect the liveness of the node.
111 if(!is_data_node(use))
114 /* Get the block where the usage is in. */
115 use_block = get_nodes_block(use);
118 * If the use is a phi function, determine the corresponding block
119 * through which the value reaches the phi function and mark the
120 * value as live out of that block.
123 ir_node *pred_block = get_Block_cfgpred_block(use_block, edge->pos);
124 live_end_at_block(irn, pred_block, visited, 0);
128 * Else, the value is live in at this block. Mark it and call live
129 * out on the predecessors.
131 else if(def_block != use_block) {
134 mark_live_in(use_block, irn);
136 for(i = 0, n = get_irn_arity(use_block); i < n; ++i) {
137 ir_node *pred_block = get_nodes_block(get_irn_n(use_block, i));
138 live_end_at_block(irn, pred_block, visited, 1);
146 static int cmp_irn_live(const void *a, const void *b, size_t size)
148 const irn_live_t *p = a;
149 const irn_live_t *q = b;
151 return !(p->block == q->block && p->irn == q->irn);
154 void be_liveness(ir_graph *irg)
156 irg_live_info_t *live_info = get_irg_live_info(irg);
158 del_set(live_info->live);
160 ir_fprintf(stderr, "%F\n", irg);
162 live_info->live = new_set(cmp_irn_live, 8192);
163 irg_walk_graph(irg, liveness_for_node, NULL, NULL);
166 static void dump_liveness_walker(ir_node *bl, void *data)
169 const irn_live_t *li;
172 ir_fprintf(f, "%+F\n", bl);
173 live_foreach(bl, li) {
185 ir_fprintf(f, "\t%+20F %s\n", li->irn, buf);
189 void be_liveness_dump(ir_graph *irg, FILE *f)
191 irg_block_walk_graph(irg, dump_liveness_walker, NULL, f);