2 * Interblock liveness analysis.
3 * @author Sebastian Hack
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 *)
21 /** The offset of the liveness information in a firm node. */
22 size_t live_irn_data_offset = 0;
24 void be_liveness_init(void)
26 live_irn_data_offset = register_additional_node_data(sizeof(live_info_t));
29 static INLINE void mark_live_in(ir_node *block, const ir_node *irn)
31 block_live_info_t *info = get_block_live_info(block);
32 pset_insert_ptr(info->in, irn);
35 static INLINE void mark_live_out(ir_node *block, const ir_node *irn)
37 block_live_info_t *info = get_block_live_info(block);
38 pset_insert_ptr(info->out, irn);
42 * Mark a node (value) live out at a certain block. Do this also
43 * transitively, i.e. if the block is not the block of the value's
44 * definition, all predecessors are also marked live.
45 * @param def The node (value).
46 * @param block The block to mark the value live out of.
47 * @param visited A set were all visited blocks are recorded.
49 static void live_out_at_block(ir_node *def, ir_node *block, pset *visited)
51 if(pset_find_ptr(visited, block))
54 pset_insert_ptr(visited, block);
55 mark_live_out(block, def);
58 * If this block is not the definition block, we have to go up
61 if(get_nodes_block(def) != block) {
64 mark_live_in(block, def);
66 for(i = 0, n = get_irn_arity(block); i < n; ++i)
67 live_out_at_block(def, get_nodes_block(get_irn_n(block, i)), visited);
72 * Liveness analysis for a value.
73 * This functions is meant to be called by a firm walker, to compute the
74 * set of all blocks a value is live in.
75 * @param irn The node (value).
78 static void liveness_for_node(ir_node *irn, void *env)
84 /* Don't compute liveness information fornon-data nodes. */
85 if(!is_data_node(irn))
88 visited = pset_new_ptr(512);
89 def_block = get_nodes_block(irn);
91 /* Go over all uses of the value */
92 for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
93 ir_node *use = get_irn_out(irn, i);
97 * If the usage is no data node, skip this use, since it does not
98 * affect the liveness of the node.
100 if(!is_data_node(use))
103 /* Get the block where the usage is in. */
104 use_block = get_nodes_block(use);
107 * If the block of the definition equals the use block, we can skip
108 * the following computations, since this use is local to a block.
110 if(def_block == use_block)
114 * If the use is a phi function, determine the corresponding block
115 * through which the value reaches the phi function and mark the
116 * value as live out of that block.
121 /* Mark the node as a phi operand, since a use by a phi was found. */
122 get_node_live_info(irn)->is_phi_op = 1;
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);
154 info->in = pset_new_ptr(128);
155 info->out = pset_new_ptr(128);
159 static void liveness_dump(ir_node *block, void *env)
162 block_live_info_t *info = get_block_live_info(block);
164 assert(is_Block(block) && "Need a block here");
165 ir_fprintf(f, "liveness at block %n\n", block);
166 ir_fprintf(f, "\tlive in: %*n\n", pset_iterator, info->in);
167 ir_fprintf(f, "\tlive out: %*n\n", pset_iterator, info->out);
170 void be_liveness_dump(FILE *f, ir_graph *irg)
172 irg_block_walk_graph(irg, liveness_dump, NULL, f);
175 void be_liveness(ir_graph *irg)
177 irg_block_walk_graph(irg, create_sets, NULL, NULL);
178 irg_walk_graph(irg, liveness_for_node, NULL, NULL);