2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * @author Sebastian Hack
27 * Liveness checks as developed by Benoit Boissinot, Fabrice Rastello and myself.
29 * The speciality here is, that nothing has to be recomputed if new nodes are created
30 * or old ones deleted.
32 * This algo has one core routine check_live_end_internal() which performs the liveness check.
33 * It only relies on the precomputation done in the constructor, which in turn needs:
35 * - the dominance tree
36 * - data obtained from a depth-first-search
38 * The precomputation remains valid as long as the CFG is not altered.
43 #include "irgraph_t.h"
44 #include "irphase_t.h"
45 #include "iredges_t.h"
53 #include "irlivechk.h"
58 #define STAT_INC(memb) ++lv->stat->memb
60 #define STAT_INC(memb)
63 typedef struct _bl_info_t {
64 ir_node *block; /**< The block. */
66 int id; /**< a tight number for the block.
67 we're just reusing the pre num from
70 bitset_t *red_reachable; /**< Holds all id's if blocks reachable
71 in the CFG modulo back edges. */
73 bitset_t *be_tgt_reach; /**< target blocks of back edges whose
74 sources are reachable from this block
75 in the reduced graph. */
77 bitset_t *be_tgt_dom; /**< target blocks of back edges which
78 are dominated by this block. */
81 #define get_block_info(lv, bl) ((bl_info_t *) phase_get_irn_data(&(lv)->ph, bl))
86 DEBUG_ONLY(firm_dbg_module_t *dbg;)
88 bitset_t *back_edge_src;
89 bitset_t *back_edge_tgt;
91 lv_chk_stat_t *stat; /**< statistics information. */
92 lv_chk_stat_t stat_data;
95 static void *init_block_data(ir_phase *ph, ir_node *irn, void *old)
97 lv_chk_t *lv = container_of(ph, lv_chk_t, ph);
98 bl_info_t *bi = phase_alloc(ph, sizeof(bi[0]));
100 bi->id = dfs_get_pre_num(lv->dfs, irn);
102 bi->red_reachable = bitset_obstack_alloc(phase_obst(ph), lv->n_blocks);
103 bi->be_tgt_reach = bitset_obstack_alloc(phase_obst(ph), lv->n_blocks);
104 bi->be_tgt_dom = bitset_obstack_alloc(phase_obst(ph), lv->n_blocks);
110 * Filter function to select all nodes for which liveness is computed.
112 * @return 1 if the node shall be considered in liveness, 0 if not.
114 static INLINE int is_liveness_node(const ir_node *irn)
116 switch(get_irn_opcode(irn)) {
128 * Compute the transitive closure on the reduced graph.
129 * The reduced graph is the original graph without back edges.
130 * Since that is a DAG, a reverse post order of the graph gives a toposort
131 * which is ideally suited to compute the transitive closure.
132 * Note also, that the DFS tree of the reduced graph is the same than the one
133 * of the original graph. This saves us computing a new reverse post order.
134 * We also can re-use the DFS tree of the original graph.
136 static void red_trans_closure(lv_chk_t *lv)
140 for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
141 ir_node *bl = dfs_get_post_num_node(lv->dfs, i);
142 bl_info_t *bi = get_block_info(lv, bl);
144 const ir_edge_t *edge;
146 foreach_block_succ (bl, edge) {
147 ir_node *succ = get_edge_src_irn(edge);
148 bl_info_t *si = get_block_info(lv, succ);
149 dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, bl, succ);
152 * if the successor is no back edge, include all reachable
153 * blocks from there into the reachable set of the current node
155 if (kind != DFS_EDGE_BACK) {
156 assert(dfs_get_post_num(lv->dfs, bl)
157 > dfs_get_post_num(lv->dfs, succ));
158 bitset_or(bi->red_reachable, si->red_reachable);
159 bitset_set(bi->red_reachable, si->id);
162 /* mark the block as a back edge src and succ as back edge tgt. */
164 bitset_set(lv->back_edge_src, bi->id);
165 bitset_set(lv->back_edge_tgt, si->id);
174 * Compute the two back edge sets for each block.
175 * <code>be_tgt_reach</code> contains all target blocks of a back edges reachable from a node.
176 * <code>be_tgt_dom</code> contains all target blocks of back edges strictly dominated
179 static void compute_back_edge_sets(lv_chk_t *lv, ir_node *bl)
181 bl_info_t *bi = phase_get_or_set_irn_data(&(lv)->ph, bl);
182 bitset_t *tmp = bitset_alloca(lv->n_blocks);
187 dominates_for_each (bl, n) {
188 bl_info_t *ni = phase_get_or_set_irn_data(&(lv)->ph, n);
190 /* compute information for dominance sub tree */
191 compute_back_edge_sets(lv, n);
194 * of course all blocks dominated by blocks in the
195 * subtree are also dominated by bl.
197 bitset_or(bi->be_tgt_dom, ni->be_tgt_dom);
200 * add the immeditate dominee to the back edge tgt dominance
201 * bitset if it is the target node of a back edge.
203 if (bitset_is_set(lv->back_edge_tgt, ni->id))
204 bitset_set(bi->be_tgt_dom, ni->id);
208 * iterate over all back edge src nodes which are reachable from
209 * this nodes and put the targets of the back edges in the be_tgt_reach
210 * bitset of the node.
212 bitset_copy(tmp, bi->red_reachable);
213 bitset_set(tmp, bi->id);
214 bitset_and(tmp, lv->back_edge_src);
215 bitset_foreach (tmp, elm) {
216 ir_node *src = lv->map[elm]->block;
217 const ir_edge_t *edge;
219 foreach_block_succ (src, edge) {
220 ir_node *succ = get_edge_src_irn(edge);
221 dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, src, succ);
223 if (kind == DFS_EDGE_BACK) {
224 bl_info_t *si = get_block_info(lv, succ);
225 bitset_set(bi->be_tgt_reach, si->id);
231 lv_chk_t *lv_chk_new(ir_graph *irg)
233 lv_chk_t *res = xmalloc(sizeof(res[0]));
234 struct obstack *obst;
237 phase_init(&res->ph, "liveness check", irg, PHASE_DEFAULT_GROWTH, init_block_data, NULL);
238 obst = phase_obst(&res->ph);
240 FIRM_DBG_REGISTER(res->dbg, "ir.ana.lvchk");
242 res->dfs = dfs_new(&absgraph_irg_cfg_succ, irg);
243 res->n_blocks = dfs_get_n_nodes(res->dfs);
244 res->back_edge_src = bitset_obstack_alloc(obst, res->n_blocks);
245 res->back_edge_tgt = bitset_obstack_alloc(obst, res->n_blocks);
246 res->map = obstack_alloc(obst, res->n_blocks * sizeof(res->map[0]));
249 memset(&res->stat_data, 0, sizeof(res->stat_data));
250 res->stat = &res->stat_data;
256 ir_snprintf(name, sizeof(name), "dfs_%F.dot", irg);
257 if ((f = fopen(name, "wt")) != NULL) {
258 dfs_dump(res->dfs, f);
261 dump_ir_block_graph(irg, "-lvchk");
265 /* fill the map which maps pre_num to block infos */
266 for (i = res->n_blocks - 1; i >= 0; --i) {
267 ir_node *irn = dfs_get_pre_num_node(res->dfs, i);
268 res->map[i] = phase_get_or_set_irn_data(&res->ph, irn);
271 /* first of all, compute the transitive closure of the CFG *without* back edges */
272 red_trans_closure(res);
274 /* now fill the two remaining bitsets concerning back edges */
275 compute_back_edge_sets(res, get_irg_start_block(irg));
278 DBG((res->dbg, LEVEL_1, "liveness chk in %+F\n", irg));
279 for (i = res->n_blocks - 1; i >= 0; --i) {
280 ir_node *irn = dfs_get_pre_num_node(res->dfs, i);
281 bl_info_t *bi = get_block_info(res, irn);
282 DBG((res->dbg, LEVEL_1, "lv_chk for %d -> %+F\n", i, irn));
283 DBG((res->dbg, LEVEL_1, "\tred reach: %B\n", bi->red_reachable));
284 DBG((res->dbg, LEVEL_1, "\ttgt reach: %B\n", bi->be_tgt_reach));
285 DBG((res->dbg, LEVEL_1, "\ttgt dom: %B\n", bi->be_tgt_dom));
289 DBG((res->dbg, LEVEL_1, "back edge src: %B\n", res->back_edge_src));
290 DBG((res->dbg, LEVEL_1, "back edge tgt: %B\n", res->back_edge_tgt));
295 void lv_chk_free(lv_chk_t *lv)
297 obstack_free(phase_obst(&lv->ph), NULL);
302 const lv_chk_stat_t *lv_chk_get_stat(const lv_chk_t *lv)
312 * Check if a node is live at the end of a block.
313 * This function is for internal use as its code is shared between
314 * the in/end routines below. It is almost the "live_end" routine
315 * but passing in the bitset for recording the blocks where the variable
316 * is used saves some effort in the "live_in" routine. See below for
319 * @param lv The liveness check environment.
320 * @param what The node to check for.
321 * @param bl The block under investigation.
322 * @param uses A bitset where this routine records all ids of blocks
323 * where this variable is used. Note that the bitset
324 * is only guaranteed to be filled if the node was not
325 * live at the end of the block.
326 * @return 1, if @p what is live at the end at @p bl.
328 unsigned lv_chk_bl_xxx(const lv_chk_t *lv, const ir_node *bl, const ir_node *what)
332 assert(is_Block(bl) && "can only check for liveness in a block");
334 if (!is_liveness_node(what))
337 what_bl = get_nodes_block(what);
338 if (!block_dominates(what_bl, bl)) {
339 STAT_INC(n_no_dominance);
344 * If the block in question is the same as the definition block,
345 * the algorithm is simple. Just check for uses not inside this block.
349 const ir_edge_t *edge;
351 STAT_INC(n_def_block);
352 DBG((lv->dbg, LEVEL_2, "lv check same block %+F in %+F\n", what, bl));
353 foreach_out_edge (what, edge) {
354 ir_node *use = get_edge_src_irn(edge);
357 if (!is_liveness_node(use))
360 use_bl = get_nodes_block(use);
362 int pos = get_edge_src_pos(edge);
363 use_bl = get_Block_cfgpred_block(use_bl, pos);
366 DBG((lv->dbg, LEVEL_2, "\tphi %+F in succ %+F,%d -> live end\n", use, use_bl, pos));
367 res |= lv_chk_state_end;
371 if (use_bl != what_bl)
372 return lv_chk_state_end | lv_chk_state_out;
378 /* this is the complicated case */
380 bitset_t *visited = bitset_alloca(lv->n_blocks);
381 bitset_t *to_visit = bitset_alloca(lv->n_blocks);
382 bitset_t *next = bitset_alloca(lv->n_blocks);
383 bitset_t *uses = bitset_alloca(lv->n_blocks);
384 bl_info_t *def = get_block_info(lv, what_bl);
385 bl_info_t *bli = get_block_info(lv, bl);
388 const ir_edge_t *edge;
390 foreach_out_edge (what, edge) {
391 ir_node *user = get_edge_src_irn(edge);
395 if (!is_liveness_node(user))
398 use_bl = get_nodes_block(user);
400 int pos = get_edge_src_pos(edge);
402 use_bl = get_Block_cfgpred_block(use_bl, pos);
403 bi = get_block_info(lv, use_bl);
406 res |= lv_chk_state_end | lv_chk_state_in;
408 bitset_set(uses, bi->id);
412 bi = get_block_info(lv, use_bl);
413 bitset_set(uses, bi->id);
415 res |= lv_chk_state_in;
419 DBG((lv->dbg, LEVEL_2, "\tuses: %B\n", uses));
421 bitset_clear(uses, def->id);
422 bitset_set(to_visit, bli->id);
424 int id = bitset_next_set(to_visit, 0);
425 bl_info_t *bi = lv->map[id];
427 DBG((lv->dbg, LEVEL_2, "\tto visit: %B\n", to_visit));
428 DBG((lv->dbg, LEVEL_2, "\tvisited: %B\n", visited));
431 * if one of the blocks is reachable, the node must be live there.
432 * Note that this is not sufficient, since the nodes reachable
433 * via back edges are not contained in the red_reachable set.
435 if (bitset_intersect(bi->red_reachable, uses))
436 return lv_chk_state_end | lv_chk_state_out | lv_chk_state_in;
439 * if not, we have to check the back edges in question, if
440 * they lead to places which are reachable.
443 bitset_set(visited, id);
444 bitset_or(visited, bi->red_reachable);
446 bitset_copy(next, bi->be_tgt_reach);
447 bitset_and(next, def->be_tgt_dom);
448 DBG((lv->dbg, LEVEL_2, "\tnext: %B\n----\n", next));
450 if (bitset_intersect(uses, next))
451 return lv_chk_state_end | lv_chk_state_out | lv_chk_state_in;
453 bitset_or(to_visit, next);
454 bitset_andnot(to_visit, visited);
457 } while (!bitset_is_empty(to_visit));