2 * Copyright (C) 1995-2007 Inria Rhone-Alpes. 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.
46 #include "irgraph_t.h"
48 #include "irphase_t.h"
49 #include "iredges_t.h"
59 #include "irlivechk.h"
63 typedef struct _bl_info_t {
64 ir_node *block; /**< The block. */
67 int id : 31; /**< a tight number for the block.
68 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. */
78 #define get_block_info(lv, bl) ((bl_info_t *) phase_get_irn_data(&(lv)->ph, bl))
83 DEBUG_ONLY(firm_dbg_module_t *dbg;)
85 bitset_t *back_edge_src;
86 bitset_t *back_edge_tgt;
90 static void *init_block_data(ir_phase *ph, ir_node *irn, void *old)
92 lv_chk_t *lv = container_of(ph, lv_chk_t, ph);
93 bl_info_t *bi = phase_alloc(ph, sizeof(bi[0]));
95 bi->id = get_Block_dom_tree_pre_num(irn);
97 bi->red_reachable = bitset_obstack_alloc(phase_obst(ph), lv->n_blocks);
98 bi->be_tgt_reach = bitset_obstack_alloc(phase_obst(ph), lv->n_blocks);
105 * Filter function to select all nodes for which liveness is computed.
107 * @return 1 if the node shall be considered in liveness, 0 if not.
109 static INLINE int is_liveness_node(const ir_node *irn)
111 switch(get_irn_opcode(irn)) {
123 * Compute the transitive closure on the reduced graph.
124 * The reduced graph is the original graph without back edges.
125 * Since that is a DAG, a reverse post order of the graph gives a toposort
126 * which is ideally suited to compute the transitive closure.
127 * Note also, that the DFS tree of the reduced graph is the same than the one
128 * of the original graph. This saves us computing a new reverse post order.
129 * We also can re-use the DFS tree of the original graph.
131 static void red_trans_closure(lv_chk_t *lv)
135 for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
136 const ir_node *bl = dfs_get_post_num_node(lv->dfs, i);
137 bl_info_t *bi = get_block_info(lv, bl);
139 const ir_edge_t *edge;
141 bitset_set(bi->red_reachable, bi->id);
142 foreach_block_succ (bl, edge) {
143 ir_node *succ = get_edge_src_irn(edge);
144 bl_info_t *si = get_block_info(lv, succ);
145 dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, bl, succ);
148 * if the successor is no back edge, include all reachable
149 * blocks from there into the reachable set of the current node
151 if (kind != DFS_EDGE_BACK) {
152 assert(dfs_get_post_num(lv->dfs, bl) > dfs_get_post_num(lv->dfs, succ));
153 bitset_or(bi->red_reachable, si->red_reachable);
156 /* mark the block as a back edge src and succ as back edge tgt. */
158 bitset_set(lv->back_edge_src, bi->id);
159 bitset_set(lv->back_edge_tgt, si->id);
167 static void compute_back_edge_chain(lv_chk_t *lv, ir_node *bl)
169 bitset_t *tmp = bitset_alloca(lv->n_blocks);
170 bl_info_t *bi = get_block_info(lv, bl);
174 DBG((lv->dbg, LEVEL_2, "computing T_%d\n", bi->id));
176 /* put all back edge sources reachable (reduced) from here in tmp */
177 bitset_copy(tmp, bi->red_reachable);
178 bitset_set(tmp, bi->id);
179 bitset_and(tmp, lv->back_edge_src);
182 DBG((lv->dbg, LEVEL_2, "\treachable be src: %B\n", tmp));
184 /* iterate over them ... */
185 bitset_foreach(tmp, elm) {
186 bl_info_t *si = lv->map[elm];
187 const ir_edge_t *edge;
189 /* and find back edge targets which are not reduced reachable from bl */
190 foreach_block_succ (si->block, edge) {
191 ir_node *tgt = get_edge_src_irn(edge);
192 bl_info_t *ti = get_block_info(lv, tgt);
193 dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, si->block, tgt);
195 if (kind == DFS_EDGE_BACK && !bitset_is_set(bi->red_reachable, ti->id)) {
196 if (!ti->be_tgt_calc)
197 compute_back_edge_chain(lv, tgt);
198 bitset_set(bi->be_tgt_reach, ti->id);
199 bitset_or(bi->be_tgt_reach, ti->be_tgt_reach);
202 bitset_clear(bi->be_tgt_reach, bi->id);
207 static INLINE void compute_back_edge_chains(lv_chk_t *lv)
212 DBG((lv->dbg, LEVEL_2, "back edge sources: %B\n", lv->back_edge_src));
213 bitset_foreach(lv->back_edge_src, elm) {
214 compute_back_edge_chain(lv, lv->map[elm]->block);
217 for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
218 const ir_node *bl = dfs_get_post_num_node(lv->dfs, i);
219 bl_info_t *bi = get_block_info(lv, bl);
221 const ir_edge_t *edge;
223 if (!bitset_is_set(lv->back_edge_tgt, bi->id)) {
224 foreach_block_succ (bl, edge) {
225 ir_node *succ = get_edge_src_irn(edge);
226 bl_info_t *si = get_block_info(lv, succ);
227 dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, bl, succ);
229 if (kind != DFS_EDGE_BACK) {
230 assert(dfs_get_post_num(lv->dfs, bl) > dfs_get_post_num(lv->dfs, succ));
231 bitset_or(bi->be_tgt_reach, si->be_tgt_reach);
237 for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
238 const ir_node *bl = dfs_get_post_num_node(lv->dfs, i);
239 bl_info_t *bi = get_block_info(lv, bl);
240 bitset_set(bi->be_tgt_reach, bi->id);
244 lv_chk_t *lv_chk_new(ir_graph *irg, const dfs_t *dfs)
246 lv_chk_t *res = xmalloc(sizeof(res[0]));
247 struct obstack *obst;
250 edges_deactivate(irg);
255 phase_init(&res->ph, "liveness check", irg, PHASE_DEFAULT_GROWTH, init_block_data, NULL);
256 obst = phase_obst(&res->ph);
258 FIRM_DBG_REGISTER(res->dbg, "ir.ana.lvchk");
261 res->n_blocks = dfs_get_n_nodes(res->dfs);
262 res->back_edge_src = bitset_obstack_alloc(obst, res->n_blocks);
263 res->back_edge_tgt = bitset_obstack_alloc(obst, res->n_blocks);
264 res->map = obstack_alloc(obst, res->n_blocks * sizeof(res->map[0]));
265 memset(res->map, 0, res->n_blocks * sizeof(res->map[0]));
271 ir_snprintf(name, sizeof(name), "dfs_%F.dot", irg);
272 if ((f = fopen(name, "wt")) != NULL) {
273 dfs_dump(res->dfs, f);
276 dump_ir_block_graph(irg, "-lvchk");
280 /* fill the map which maps pre_num to block infos */
281 for (i = res->n_blocks - 1; i >= 0; --i) {
282 ir_node *irn = (ir_node *) dfs_get_pre_num_node(res->dfs, i);
283 bl_info_t *bi = phase_get_or_set_irn_data(&res->ph, irn);
284 assert(bi->id < res->n_blocks);
285 assert(res->map[bi->id] == NULL);
286 res->map[bi->id] = bi;
289 /* first of all, compute the transitive closure of the CFG *without* back edges */
290 red_trans_closure(res);
292 /* compute back edge chains */
293 compute_back_edge_chains(res);
296 DBG((res->dbg, LEVEL_1, "liveness chk in %+F\n", irg));
297 for (i = res->n_blocks - 1; i >= 0; --i) {
298 const ir_node *irn = dfs_get_pre_num_node(res->dfs, i);
299 bl_info_t *bi = get_block_info(res, irn);
300 DBG((res->dbg, LEVEL_1, "lv_chk for %d -> %+F\n", i, irn));
301 DBG((res->dbg, LEVEL_1, "\tred reach: %B\n", bi->red_reachable));
302 DBG((res->dbg, LEVEL_1, "\ttgt reach: %B\n", bi->be_tgt_reach));
306 DBG((res->dbg, LEVEL_1, "back edge src: %B\n", res->back_edge_src));
307 DBG((res->dbg, LEVEL_1, "back edge tgt: %B\n", res->back_edge_tgt));
309 stat_ev_tim_pop("lv_chk_cons_time");
313 void lv_chk_free(lv_chk_t *lv)
315 obstack_free(phase_obst(&lv->ph), NULL);
320 * Check if a node is live at the end of a block.
321 * This function is for internal use as its code is shared between
322 * the in/end routines below. It is almost the "live_end" routine
323 * but passing in the bitset for recording the blocks where the variable
324 * is used saves some effort in the "live_in" routine. See below for
327 * @param lv The liveness check environment.
328 * @param what The node to check for.
329 * @param bl The block under investigation.
330 * @param uses A bitset where this routine records all ids of blocks
331 * where this variable is used. Note that the bitset
332 * is only guaranteed to be filled if the node was not
333 * live at the end of the block.
334 * @return 1, if @p what is live at the end at @p bl.
336 unsigned lv_chk_bl_in_mask(const lv_chk_t *lv, const ir_node *bl, const ir_node *var)
338 stat_ev_cnt_decl(uses);
341 const ir_edge_t *edge;
345 assert(is_Block(bl) && "can only check for liveness in a block");
347 if (!is_liveness_node(var))
350 def_bl = get_nodes_block(var);
351 if (def_bl == bl || !block_dominates(def_bl, bl)) {
356 bitset_t *uses = bitset_alloca(lv->n_blocks);
357 bitset_t *tmp = bitset_alloca(lv->n_blocks);
358 int min_dom = get_Block_dom_tree_pre_num(def_bl) + 1;
359 int max_dom = get_Block_dom_max_subtree_pre_num(def_bl);
360 bl_info_t *bli = get_block_info(lv, bl);
363 DBG((lv->dbg, LEVEL_2, "lv check of %+F, def=%+F,%d != q=%+F,%d\n",
364 var, def_bl, min_dom - 1, bl, bli->id));
366 foreach_out_edge (var, edge) {
367 ir_node *user = get_edge_src_irn(edge);
371 if (!is_liveness_node(user))
374 stat_ev_cnt_inc(uses);
375 use_bl = get_nodes_block(user);
377 int pos = get_edge_src_pos(edge);
378 use_bl = get_Block_cfgpred_block(use_bl, pos);
382 res = lv_chk_state_in;
383 DBG((lv->dbg, LEVEL_2, "\tuse directly in block %+F by %+F\n", use_bl, user));
387 bi = get_block_info(lv, use_bl);
388 bitset_set(uses, bi->id);
391 DBG((lv->dbg, LEVEL_2, "\tuses: %B\n", uses));
395 bitset_copy(tmp, bli->be_tgt_reach);
396 bitset_set(tmp, bli->id);
398 DBG((lv->dbg, LEVEL_2, "\tbe tgt reach: %B, dom span: [%d, %d]\n", tmp, min_dom, max_dom));
399 for (i = bitset_next_set(tmp, min_dom); i >= 0 && i <= max_dom; i = bitset_next_set(tmp, i + 1)) {
400 bl_info_t *ti = lv->map[i];
401 DBG((lv->dbg, LEVEL_2, "\tlooking from %d: seeing %B\n", ti->id, ti->red_reachable));
402 if (bitset_intersect(ti->red_reachable, uses)) {
403 res = lv_chk_state_in;
407 bitset_andnot(tmp, ti->red_reachable);
416 unsigned lv_chk_bl_end_mask(const lv_chk_t *lv, const ir_node *bl, const ir_node *var)
418 stat_ev_cnt_decl(uses);
421 const ir_edge_t *edge;
425 assert(is_Block(bl) && "can only check for liveness in a block");
427 if (!is_liveness_node(var))
430 def_bl = get_nodes_block(var);
431 if (!block_dominates(def_bl, bl)) {
436 bitset_t *uses = bitset_alloca(lv->n_blocks);
437 bitset_t *tmp = bitset_alloca(lv->n_blocks);
438 int min_dom = get_Block_dom_tree_pre_num(def_bl) + 1;
439 int max_dom = get_Block_dom_max_subtree_pre_num(def_bl);
440 bl_info_t *bli = get_block_info(lv, bl);
443 DBG((lv->dbg, LEVEL_2, "lv end check of %+F, def=%+F,%d != q=%+F,%d\n",
444 var, def_bl, min_dom - 1, bl, bli->id));
446 foreach_out_edge (var, edge) {
447 ir_node *user = get_edge_src_irn(edge);
451 if (!is_liveness_node(user))
454 stat_ev_cnt_inc(uses);
455 use_bl = get_nodes_block(user);
457 int pos = get_edge_src_pos(edge);
458 use_bl = get_Block_cfgpred_block(use_bl, pos);
461 res |= lv_chk_state_end;
464 bi = get_block_info(lv, use_bl);
465 if (use_bl != bl || bitset_is_set(lv->back_edge_tgt, bi->id))
466 bitset_set(uses, bi->id);
469 DBG((lv->dbg, LEVEL_2, "\tuses: %B\n", uses));
471 bitset_copy(tmp, bli->be_tgt_reach);
472 bitset_set(tmp, bli->id);
474 DBG((lv->dbg, LEVEL_2, "\tbe tgt reach + current: %B, dom span: [%d, %d]\n", tmp, min_dom, max_dom));
475 for (i = bitset_next_set(tmp, min_dom); i >= 0 && i <= max_dom; i = bitset_next_set(tmp, i + 1)) {
476 bl_info_t *ti = lv->map[i];
477 DBG((lv->dbg, LEVEL_2, "\tlooking from %d: seeing %B\n", ti->id, ti->red_reachable));
478 if (bitset_intersect(ti->red_reachable, uses)) {
479 res = lv_chk_state_out | lv_chk_state_end;
483 bitset_andnot(tmp, ti->red_reachable);
492 * Check a nodes liveness situation of a block.
493 * This routine considers both cases, the live in and end/out case.
495 * @param lv The liveness check environment.
496 * @param bl The block under investigation.
497 * @param var The node to check for.
498 * @return A bitmask of lv_chk_state_XXX fields.
500 unsigned lv_chk_bl_xxx(const lv_chk_t *lv, const ir_node *bl, const ir_node *var)
502 stat_ev_cnt_decl(uses);
503 stat_ev_cnt_decl(iter);
508 assert(is_Block(bl) && "can only check for liveness in a block");
510 /* If the variable ist no liveness related var, bail out. */
511 if (!is_liveness_node(var))
514 stat_ev_ctx_push_fmt("lv_chk", "%u", get_irn_idx(var));
517 /* If there is no dominance relation, go out, too */
518 def_bl = get_nodes_block(var);
519 if (!block_dominates(def_bl, bl)) {
520 stat_ev("lv_chk_no_dom");
525 * If the block in question is the same as the definition block,
526 * the algorithm is simple. Just check for uses not inside this block.
529 const ir_edge_t *edge;
531 stat_ev("lv_chk_def_block");
532 DBG((lv->dbg, LEVEL_2, "lv check same block %+F in %+F\n", var, bl));
533 foreach_out_edge (var, edge) {
534 ir_node *use = get_edge_src_irn(edge);
537 if (!is_liveness_node(use))
540 stat_ev_cnt_inc(uses);
541 use_bl = get_nodes_block(use);
543 int pos = get_edge_src_pos(edge);
544 use_bl = get_Block_cfgpred_block(use_bl, pos);
547 DBG((lv->dbg, LEVEL_2, "\tphi %+F in succ %+F,%d -> live end\n", use, use_bl, pos));
548 res |= lv_chk_state_end;
552 if (use_bl != def_bl) {
553 res = lv_chk_state_end | lv_chk_state_out;
562 * this is the more complicated case.
563 * We try to gather as much information as possible during looking
566 * Note that we know for shure that bl != def_bl. That is sometimes
567 * silently exploited below.
570 bl_info_t *def = get_block_info(lv, def_bl);
571 bl_info_t *bli = get_block_info(lv, bl);
572 bitset_t *uses = bitset_alloca(lv->n_blocks);
575 unsigned i, min_dom, max_dom;
576 const ir_edge_t *edge;
578 /* if the block has no DFS info, it cannot be reached.
579 * This can happen in functions with endless loops.
580 * we then go out, since nothing is live there.
582 * TODO: Is that right?
588 DBG((lv->dbg, LEVEL_2, "lv check %+F (def in %+F #%d) in different block %+F #%d\n",
589 var, def_bl, def->id, bl, bli->id));
591 foreach_out_edge (var, edge) {
592 ir_node *user = get_edge_src_irn(edge);
593 int mask = lv_chk_state_in;
598 /* if the user is no liveness node, the use does not count */
599 if (!is_liveness_node(user))
602 stat_ev_cnt_inc(uses);
604 /* if the user is a phi, the use is in the predecessor
605 * furthermore, prepare a mask so that in the case where
606 * bl (the block in question) coincides with a use, it
607 * can be marked live_end there. */
608 use_bl = get_nodes_block(user);
610 int pos = get_edge_src_pos(edge);
611 use_bl = get_Block_cfgpred_block(use_bl, pos);
612 mask |= lv_chk_state_end;
616 /* if the use block coincides with the query block, we
617 * already gather a little liveness information.
618 * The variable is surely live there, since bl != def_bl
619 * (that case is treated above). */
623 bi = get_block_info(lv, use_bl);
626 bitset_set(uses, bi->id);
629 /* get the dominance range which really matters. all uses outside
630 * the definition's dominance range are not to consider. note,
631 * that the definition itself is also not considered. The case
632 * where bl == def_bl is considered above. */
633 min_dom = get_Block_dom_tree_pre_num(def_bl) + 1;
634 max_dom = get_Block_dom_max_subtree_pre_num(def_bl);
636 DBG((lv->dbg, LEVEL_2, "\tuses: %B\n", uses));
638 /* prepare a set with all reachable back edge targets.
639 * this will determine our "looking points" from where
640 * we will search/find the calculated uses. */
641 Tq = bli->be_tgt_reach;
643 /* now, visit all viewing points in the temporary bitset lying
644 * in the dominance range of the variable. Note that for reducible
645 * flow-graphs the first iteration is sufficient and the loop
647 DBG((lv->dbg, LEVEL_2, "\tbe tgt reach: %B, dom span: [%d, %d]\n", Tq, min_dom, max_dom));
648 i = bitset_next_set(Tq, min_dom);
649 while(i <= max_dom) {
650 bl_info_t *ti = lv->map[i];
651 int use_in_current_block = bitset_is_set(uses, ti->id);
653 stat_ev_cnt_inc(iter);
656 * This is somehat tricky. Since this routine handles both, live in
657 * and end/out we have to handle all the border cases correctly.
658 * Each node is in its own red_reachable set (see calculation
659 * function above). That means, that in the case where bl == t, the
660 * intersection check of uses and rechability below will always
661 * find an intersection, namely t.
663 * However, if a block contains a use and the variable is dead
664 * afterwards, it is not live end/out at that block. Besides
665 * back-edge target. If a var is live-in at a back-edge target it
666 * is also live out/end there since the variable is live in the
667 * underlying loop. So in the case where t == bl and that is not
668 * a back-edge target, we have to remove that use from consideration
669 * to determine if the var is live out/end there.
671 * Note that the live in information has been calculated by the
672 * uses iteration above.
674 if (ti == bli && !bitset_is_set(lv->back_edge_tgt, ti->id)) {
675 DBG((lv->dbg, LEVEL_2, "\tlooking not from a back edge target and q == t. removing use: %d\n", ti->id));
676 bitset_clear(uses, ti->id);
679 /* If we can reach a use, the variable is live there and we say goodbye */
680 DBG((lv->dbg, LEVEL_2, "\tlooking from %d: seeing %B\n", ti->id, ti->red_reachable));
681 if (bitset_intersect(ti->red_reachable, uses)) {
682 res |= lv_chk_state_in | lv_chk_state_out | lv_chk_state_end;
687 * if we deleted a use do to the commentary above, we have to
688 * re-add it since it might be visible from further view points
689 * (we only need that in the non-reducible case).
691 if (use_in_current_block)
692 bitset_set(uses, ti->id);
694 i = bitset_next_set(Tq, get_Block_dom_max_subtree_pre_num(ti->block) + 1);
700 stat_ev_tim_pop("lv_chk_query_time");
701 stat_ev_cnt_done(uses, "lv_chk_uses");
702 stat_ev_cnt_done(iter, "lv_chk_iter");
703 stat_ev_ctx_pop("lv_chk");