2 * Interblock liveness analysis.
3 * @author Sebastian Hack
14 /** The offset of the liveness information in a firm node. */
15 size_t live_irn_data_offset = 0;
17 void be_liveness_init(void)
19 live_irn_data_offset = register_additional_node_data(sizeof(live_info_t));
22 int (is_live_in)(const ir_node *block, const ir_node *irn)
24 return _is_live_in(block, irn);
27 int (is_live_out)(const ir_node *block, const ir_node *irn)
29 return _is_live_in(block, irn);
32 static INLINE void mark_live_in(ir_node *block, const ir_node *irn)
34 block_live_info_t *info = get_block_live_info(block);
35 pset_insert_ptr(info->in, irn);
38 static INLINE void mark_live_out(ir_node *block, const ir_node *irn)
40 block_live_info_t *info = get_block_live_info(block);
41 pset_insert_ptr(info->out, irn);
45 * Mark a node (value) live out at a certain block. Do this also
46 * transitively, i.e. if the block is not the block of the value's
47 * definition, all predecessors are also marked live.
48 * @param def The node (value).
49 * @param block The block to mark the value live out of.
50 * @param visited A set were all visited blocks are recorded.
52 static void live_out_at_block(ir_node *def, ir_node *block, pset *visited)
54 if(pset_find_ptr(visited, block))
57 pset_insert_ptr(visited, block);
58 mark_live_out(block, def);
61 * If this block is not the definition block, we have to go up
64 if(get_nodes_block(def) != block) {
67 mark_live_in(block, def);
69 for(i = 0, n = get_irn_arity(block); i < n; ++i)
70 live_out_at_block(def, get_nodes_block(get_irn_n(block, i)), visited);
75 * Liveness analysis for a value.
76 * This functions is meant to be called by a firm walker, to compute the
77 * set of all blocks a value is live in.
78 * @param irn The node (value).
81 static void liveness_for_node(ir_node *irn, void *env)
87 /* Don't compute liveness information fornon-data nodes. */
88 if(!is_data_node(irn))
91 visited = pset_new_ptr(512);
92 def_block = get_nodes_block(irn);
94 /* Go over all uses of the value */
95 for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
96 ir_node *use = get_irn_out(irn, i);
100 * If the usage is no data node, skip this use, since it does not
101 * affect the liveness of the node.
103 if(!is_data_node(use))
106 /* Get the block where the usage is in. */
107 use_block = get_nodes_block(use);
110 * If the block of the definition equals the use block, we can skip
111 * the following computations, since this use is local to a block.
113 if(def_block == use_block)
117 * If the use is a phi function, determine the corresponding block
118 * through which the value reaches the phi function and mark the
119 * value as live out of that block.
124 for(i = 0, n = get_irn_arity(use); i < n; ++i) {
125 if(get_irn_n(use, i) == irn) {
126 ir_node *pred_block = get_nodes_block(get_irn_n(use_block, i));
127 live_out_at_block(irn, pred_block, visited);
133 * Else, the value is live in at this block. Mark it and call live
134 * out on the predecessors.
139 mark_live_in(use_block, irn);
141 for(i = 0, n = get_irn_arity(use_block); i < n; ++i) {
142 ir_node *pred_block = get_nodes_block(get_irn_n(use_block, i));
143 live_out_at_block(irn, pred_block, visited);
151 static void create_sets(ir_node *block, void *env)
153 block_live_info_t *info = get_block_live_info(block);
155 info->in = pset_new_ptr(128);
156 info->out = pset_new_ptr(128);
159 void be_liveness(ir_graph *irg)
161 irg_block_walk_graph(irg, create_sets, NULL, NULL);
162 irg_walk_graph(irg, liveness_for_node, NULL, NULL);
166 static void liveness_dump(ir_node *block, void *env)
169 block_live_info_t *info = get_block_live_info(block);
171 assert(is_Block(block) && "Need a block here");
173 ir_fprintf(f, "liveness at block %n\n", block);
174 ir_fprintf(f, "\tlive in: %*n\n", pset_iterator, info->in);
175 ir_fprintf(f, "\tlive out: %*n\n", pset_iterator, info->out);
178 void be_liveness_dump(FILE *f, ir_graph *irg)
180 irg_block_walk_graph(irg, liveness_dump, NULL, f);