Remove arch_get_allocatable_regs().
[libfirm] / ir / ana / irlivechk.c
1 /*
2  * Copyright (C) 1995-2007 Inria Rhone-Alpes.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file    livechk.c
22  * @date    21.04.2007
23  * @author  Sebastian Hack
24  * @version $Id$
25  * @summary
26  *
27  * Liveness checks as developed by Benoit Boissinot, Fabrice Rastello and myself.
28  *
29  * The speciality here is, that nothing has to be recomputed if new nodes are created
30  * or old ones deleted.
31  *
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:
34  * - out edges
35  * - the dominance tree
36  * - data obtained from a depth-first-search
37  *
38  * The precomputation remains valid as long as the CFG is not altered.
39  */
40 #include <config.h>
41
42 #include <stdio.h>
43
44 #include "irgraph_t.h"
45 #include "irnode_t.h"
46 #include "irphase_t.h"
47 #include "iredges_t.h"
48
49 #include "irprintf.h"
50 #include "irdom.h"
51 #include "irdump.h"
52
53 #include "dfs_t.h"
54 #include "bitset.h"
55 #include "util.h"
56
57 #include "irlivechk.h"
58
59 #include "statev.h"
60
61 typedef struct _bl_info_t {
62         const ir_node *block;      /**< The block. */
63
64         int be_tgt_calc : 1;
65         int id : 31;               /**< a tight number for the block.
66                                                                  we're just reusing the pre num from
67                                                                  the DFS. */
68         bitset_t *red_reachable;   /**< Holds all id's if blocks reachable
69                                                                  in the CFG modulo back edges. */
70
71         bitset_t *be_tgt_reach;    /**< target blocks of back edges whose
72                                                                  sources are reachable from this block
73                                                                  in the reduced graph. */
74 } bl_info_t;
75
76 #define get_block_info(lv, bl) ((bl_info_t *) phase_get_irn_data(&(lv)->ph, bl))
77
78 struct _lv_chk_t {
79         ir_phase ph;
80         const dfs_t *dfs;
81         int n_blocks;
82         bitset_t *back_edge_src;
83         bitset_t *back_edge_tgt;
84         bl_info_t **map;
85         DEBUG_ONLY(firm_dbg_module_t *dbg;)
86 };
87
88 static void *init_block_data(ir_phase *ph, const ir_node *irn, void *old)
89 {
90         lv_chk_t *lv      = container_of(ph, lv_chk_t, ph);
91         bl_info_t *bi     = phase_alloc(ph, sizeof(bi[0]));
92
93         bi->id            = get_Block_dom_tree_pre_num(irn);
94         bi->block         = irn;
95         bi->red_reachable = bitset_obstack_alloc(phase_obst(ph), lv->n_blocks);
96         bi->be_tgt_reach  = bitset_obstack_alloc(phase_obst(ph), lv->n_blocks);
97         bi->be_tgt_calc   = 0;
98         (void) old;
99         return bi;
100 }
101
102 /**
103  * Filter function to select all nodes for which liveness is computed.
104  * @param irn A node.
105  * @return    1 if the node shall be considered in liveness, 0 if not.
106  */
107 static inline int is_liveness_node(const ir_node *irn)
108 {
109         switch (get_irn_opcode(irn)) {
110         case iro_Block:
111         case iro_Bad:
112         case iro_End:
113         case iro_Anchor:
114                 return 0;
115         default:;
116         }
117
118         return 1;
119 }
120
121 /**
122  * Compute the transitive closure on the reduced graph.
123  * The reduced graph is the original graph without back edges.
124  * Since that is a DAG, a reverse post order of the graph gives a toposort
125  * which is ideally suited to compute the transitive closure.
126  * Note also, that the DFS tree of the reduced graph is the same than the one
127  * of the original graph. This saves us computing a new reverse post order.
128  * We also can re-use the DFS tree of the original graph.
129  */
130 static void red_trans_closure(lv_chk_t *lv)
131 {
132         int i, n;
133
134         for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
135                 const ir_node *bl   = dfs_get_post_num_node(lv->dfs, i);
136                 bl_info_t *bi = get_block_info(lv, bl);
137
138                 const ir_edge_t *edge;
139
140                 bitset_set(bi->red_reachable, bi->id);
141                 foreach_block_succ (bl, edge) {
142                         ir_node *succ = get_edge_src_irn(edge);
143                         bl_info_t *si = get_block_info(lv, succ);
144                         dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, bl, succ);
145
146                         /*
147                          * if the successor is no back edge, include all reachable
148                          * blocks from there into the reachable set of the current node
149                          */
150                         if (kind != DFS_EDGE_BACK) {
151                                 assert(dfs_get_post_num(lv->dfs, bl) > dfs_get_post_num(lv->dfs, succ));
152                                 bitset_or(bi->red_reachable, si->red_reachable);
153                         }
154
155                         /* mark the block as a back edge src and succ as back edge tgt. */
156                         else {
157                                 bitset_set(lv->back_edge_src, bi->id);
158                                 bitset_set(lv->back_edge_tgt, si->id);
159                         }
160                 }
161
162         }
163
164 }
165
166 static void compute_back_edge_chain(lv_chk_t *lv, const ir_node *bl)
167 {
168         bitset_t *tmp = bitset_alloca(lv->n_blocks);
169         bl_info_t *bi = get_block_info(lv, bl);
170
171         bitset_pos_t elm;
172
173         DBG((lv->dbg, LEVEL_2, "computing T_%d\n", bi->id));
174
175         /* put all back edge sources reachable (reduced) from here in tmp */
176         bitset_copy(tmp, bi->red_reachable);
177         bitset_set(tmp, bi->id);
178         bitset_and(tmp, lv->back_edge_src);
179         bi->be_tgt_calc = 1;
180
181         DBG((lv->dbg, LEVEL_2, "\treachable be src: %B\n", tmp));
182
183         /* iterate over them ... */
184         bitset_foreach(tmp, elm) {
185                 bl_info_t *si = lv->map[elm];
186                 const ir_edge_t *edge;
187
188                 /* and find back edge targets which are not reduced reachable from bl */
189                 foreach_block_succ (si->block, edge) {
190                         ir_node *tgt         = get_edge_src_irn(edge);
191                         bl_info_t *ti        = get_block_info(lv, tgt);
192                         dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, si->block, tgt);
193
194                         if (kind == DFS_EDGE_BACK && !bitset_is_set(bi->red_reachable, ti->id)) {
195                                 if (!ti->be_tgt_calc)
196                                         compute_back_edge_chain(lv, tgt);
197                                 bitset_set(bi->be_tgt_reach, ti->id);
198                                 bitset_or(bi->be_tgt_reach, ti->be_tgt_reach);
199                         }
200                 }
201                 bitset_clear(bi->be_tgt_reach, bi->id);
202         }
203 }
204
205
206 static inline void compute_back_edge_chains(lv_chk_t *lv)
207 {
208         bitset_pos_t elm;
209         int i, n;
210
211         DBG((lv->dbg, LEVEL_2, "back edge sources: %B\n", lv->back_edge_src));
212         bitset_foreach(lv->back_edge_src, elm) {
213                 compute_back_edge_chain(lv, lv->map[elm]->block);
214         }
215
216         for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
217                 const ir_node *bl = dfs_get_post_num_node(lv->dfs, i);
218                 bl_info_t *bi     = get_block_info(lv, bl);
219
220                 const ir_edge_t *edge;
221
222                 if (!bitset_is_set(lv->back_edge_tgt, bi->id)) {
223                         foreach_block_succ (bl, edge) {
224                                 ir_node *succ = get_edge_src_irn(edge);
225                                 bl_info_t *si = get_block_info(lv, succ);
226                                 dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, bl, succ);
227
228                                 if (kind != DFS_EDGE_BACK) {
229                                         assert(dfs_get_post_num(lv->dfs, bl) > dfs_get_post_num(lv->dfs, succ));
230                                         bitset_or(bi->be_tgt_reach, si->be_tgt_reach);
231                                 }
232                         }
233                 }
234         }
235
236         for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
237                 const ir_node *bl = dfs_get_post_num_node(lv->dfs, i);
238                 bl_info_t *bi     = get_block_info(lv, bl);
239                 bitset_set(bi->be_tgt_reach, bi->id);
240         }
241 }
242
243 lv_chk_t *lv_chk_new(ir_graph *irg, const dfs_t *dfs)
244 {
245         lv_chk_t *res = XMALLOC(lv_chk_t);
246         struct obstack *obst;
247         int i;
248
249         edges_deactivate(irg);
250         edges_activate(irg);
251         compute_doms(irg);
252
253         stat_ev_tim_push();
254         phase_init(&res->ph, "liveness check", irg, PHASE_DEFAULT_GROWTH, init_block_data, NULL);
255         obst = phase_obst(&res->ph);
256
257         FIRM_DBG_REGISTER(res->dbg, "ir.ana.lvchk");
258
259         res->dfs           = dfs;
260         res->n_blocks      = dfs_get_n_nodes(res->dfs);
261         res->back_edge_src = bitset_obstack_alloc(obst, res->n_blocks);
262         res->back_edge_tgt = bitset_obstack_alloc(obst, res->n_blocks);
263         res->map           = obstack_alloc(obst, res->n_blocks * sizeof(res->map[0]));
264         memset(res->map, 0, res->n_blocks * sizeof(res->map[0]));
265
266 #if 0
267         {
268                 char name[256];
269                 FILE *f;
270                 ir_snprintf(name, sizeof(name), "dfs_%F.dot", irg);
271                 if ((f = fopen(name, "wt")) != NULL) {
272                         dfs_dump(res->dfs, f);
273                         fclose(f);
274                 }
275                 dump_ir_block_graph(irg, "-lvchk");
276         }
277 #endif
278
279         /* fill the map which maps pre_num to block infos */
280         for (i = res->n_blocks - 1; i >= 0; --i) {
281                 ir_node *irn  = (ir_node *) dfs_get_pre_num_node(res->dfs, i);
282                 bl_info_t *bi = phase_get_or_set_irn_data(&res->ph, irn);
283                 assert(bi->id < res->n_blocks);
284                 assert(res->map[bi->id] == NULL);
285                 res->map[bi->id] = bi;
286         }
287
288         /* first of all, compute the transitive closure of the CFG *without* back edges */
289         red_trans_closure(res);
290
291         /* compute back edge chains */
292         compute_back_edge_chains(res);
293
294 #ifndef NDEBUG
295         DBG((res->dbg, LEVEL_1, "liveness chk in %+F\n", irg));
296         for (i = res->n_blocks - 1; i >= 0; --i) {
297                 const ir_node *irn = dfs_get_pre_num_node(res->dfs, i);
298                 bl_info_t *bi      = get_block_info(res, irn);
299                 DBG((res->dbg, LEVEL_1, "lv_chk for %d -> %+F\n", i, irn));
300                 DBG((res->dbg, LEVEL_1, "\tred reach: %B\n", bi->red_reachable));
301                 DBG((res->dbg, LEVEL_1, "\ttgt reach: %B\n", bi->be_tgt_reach));
302         }
303 #endif
304
305         DBG((res->dbg, LEVEL_1, "back edge src: %B\n", res->back_edge_src));
306         DBG((res->dbg, LEVEL_1, "back edge tgt: %B\n", res->back_edge_tgt));
307
308         stat_ev_tim_pop("lv_chk_cons_time");
309         return res;
310 }
311
312 void lv_chk_free(lv_chk_t *lv)
313 {
314         phase_free(&lv->ph);
315         xfree(lv);
316 }
317
318 /**
319  * Check if a node is live at the end of a block.
320  * This function is for internal use as its code is shared between
321  * the in/end routines below. It is almost the "live_end" routine
322  * but passing in the bitset for recording the blocks where the variable
323  * is used saves some effort in the "live_in" routine. See below for
324  * details.
325  *
326  * @param lv    The liveness check environment.
327  * @param what  The node to check for.
328  * @param bl    The block under investigation.
329  * @param uses  A bitset where this routine records all ids of blocks
330  *              where this variable is used. Note that the bitset
331  *              is only guaranteed to be filled if the node was not
332  *              live at the end of the block.
333  * @return      1, if @p what is live at the end at @p bl.
334  */
335 unsigned lv_chk_bl_in_mask(const lv_chk_t *lv, const ir_node *bl, const ir_node *var)
336 {
337         ir_node *def_bl;
338         const ir_edge_t *edge;
339
340         stat_ev_cnt_decl(uses);
341
342         int res = 0;
343
344         assert(is_Block(bl) && "can only check for liveness in a block");
345
346         if (!is_liveness_node(var))
347                 return 0;
348
349         def_bl = get_nodes_block(var);
350         if (def_bl == bl || !block_dominates(def_bl, bl)) {
351                 goto end;
352         }
353
354         else {
355                 bitset_t *uses = bitset_alloca(lv->n_blocks);
356                 bitset_t *tmp  = bitset_alloca(lv->n_blocks);
357                 int min_dom    = get_Block_dom_tree_pre_num(def_bl) + 1;
358                 int max_dom    = get_Block_dom_max_subtree_pre_num(def_bl);
359                 bl_info_t *bli = get_block_info(lv, bl);
360                 int i;
361
362                 DBG((lv->dbg, LEVEL_2, "lv check of %+F, def=%+F,%d != q=%+F,%d\n",
363                                         var, def_bl, min_dom - 1, bl, bli->id));
364
365                 foreach_out_edge (var, edge) {
366                         ir_node *user = get_edge_src_irn(edge);
367                         ir_node *use_bl;
368                         bl_info_t *bi;
369
370                         if (!is_liveness_node(user))
371                                 continue;
372
373                         stat_ev_cnt_inc(uses);
374                         use_bl = get_nodes_block(user);
375                         if (is_Phi(user)) {
376                                 int pos = get_edge_src_pos(edge);
377                                 use_bl  = get_Block_cfgpred_block(use_bl, pos);
378                         }
379
380                         if (use_bl == bl) {
381                                 res = lv_chk_state_in;
382                                 DBG((lv->dbg, LEVEL_2, "\tuse directly in block %+F by %+F\n", use_bl, user));
383                                 goto end;
384                         }
385
386                         bi = get_block_info(lv, use_bl);
387                         bitset_set(uses, bi->id);
388                 }
389
390                 DBG((lv->dbg, LEVEL_2, "\tuses: %B\n", uses));
391
392                 {
393
394                         bitset_copy(tmp, bli->be_tgt_reach);
395                         bitset_set(tmp, bli->id);
396
397                         DBG((lv->dbg, LEVEL_2, "\tbe tgt reach: %B, dom span: [%d, %d]\n", tmp, min_dom, max_dom));
398                         for (i = bitset_next_set(tmp, min_dom); i >= 0 && i <= max_dom; i = bitset_next_set(tmp, i + 1)) {
399                                 bl_info_t *ti = lv->map[i];
400                                 DBG((lv->dbg, LEVEL_2, "\tlooking from %d: seeing %B\n", ti->id, ti->red_reachable));
401                                 if (bitset_intersect(ti->red_reachable, uses)) {
402                                         res = lv_chk_state_in;
403                                         goto end;
404                                 }
405
406                                 bitset_andnot(tmp, ti->red_reachable);
407                         }
408                 }
409         }
410
411 end:
412         return res;
413 }
414
415 unsigned lv_chk_bl_end_mask(const lv_chk_t *lv, const ir_node *bl, const ir_node *var)
416 {
417         ir_node *def_bl;
418         const ir_edge_t *edge;
419
420         stat_ev_cnt_decl(uses);
421
422         int res = 0;
423
424         assert(is_Block(bl) && "can only check for liveness in a block");
425
426         if (!is_liveness_node(var))
427                 return 0;
428
429         def_bl = get_nodes_block(var);
430         if (!block_dominates(def_bl, bl)) {
431                 goto end;
432         } else {
433                 bitset_t *uses = bitset_alloca(lv->n_blocks);
434                 bitset_t *tmp  = bitset_alloca(lv->n_blocks);
435                 int min_dom    = get_Block_dom_tree_pre_num(def_bl) + 1;
436                 int max_dom    = get_Block_dom_max_subtree_pre_num(def_bl);
437                 bl_info_t *bli = get_block_info(lv, bl);
438                 int i;
439
440                 DBG((lv->dbg, LEVEL_2, "lv end check of %+F, def=%+F,%d != q=%+F,%d\n",
441                                         var, def_bl, min_dom - 1, bl, bli->id));
442
443                 foreach_out_edge (var, edge) {
444                         ir_node *user = get_edge_src_irn(edge);
445                         ir_node *use_bl;
446                         bl_info_t *bi;
447
448                         if (!is_liveness_node(user))
449                                 continue;
450
451                         stat_ev_cnt_inc(uses);
452                         use_bl = get_nodes_block(user);
453                         if (is_Phi(user)) {
454                                 int pos = get_edge_src_pos(edge);
455                                 use_bl  = get_Block_cfgpred_block(use_bl, pos);
456
457                                 if (bl == use_bl)
458                                         res |= lv_chk_state_end;
459                         }
460
461                         bi = get_block_info(lv, use_bl);
462                         if (use_bl != bl || bitset_is_set(lv->back_edge_tgt, bi->id))
463                                 bitset_set(uses, bi->id);
464                 }
465
466                 DBG((lv->dbg, LEVEL_2, "\tuses: %B\n", uses));
467
468                 bitset_copy(tmp, bli->be_tgt_reach);
469                 bitset_set(tmp, bli->id);
470
471                 DBG((lv->dbg, LEVEL_2, "\tbe tgt reach + current: %B, dom span: [%d, %d]\n", tmp, min_dom, max_dom));
472                 for (i = bitset_next_set(tmp, min_dom); i >= 0 && i <= max_dom; i = bitset_next_set(tmp, i + 1)) {
473                         bl_info_t *ti = lv->map[i];
474                         DBG((lv->dbg, LEVEL_2, "\tlooking from %d: seeing %B\n", ti->id, ti->red_reachable));
475                         if (bitset_intersect(ti->red_reachable, uses)) {
476                                 res = lv_chk_state_out | lv_chk_state_end;
477                                 goto end;
478                         }
479
480                         bitset_andnot(tmp, ti->red_reachable);
481                 }
482         }
483
484 end:
485         return res;
486 }
487
488 /**
489  * Check a nodes liveness situation of a block.
490  * This routine considers both cases, the live in and end/out case.
491  *
492  * @param lv   The liveness check environment.
493  * @param bl   The block under investigation.
494  * @param var  The node to check for.
495  * @return     A bitmask of lv_chk_state_XXX fields.
496  */
497 unsigned lv_chk_bl_xxx(const lv_chk_t *lv, const ir_node *bl, const ir_node *var)
498 {
499         int res  = 0;
500         ir_node *def_bl;
501         stat_ev_cnt_decl(uses);
502         stat_ev_cnt_decl(iter);
503
504         assert(is_Block(bl) && "can only check for liveness in a block");
505
506         /* If the variable ist no liveness related var, bail out. */
507         if (!is_liveness_node(var))
508                 return 0;
509
510         stat_ev_ctx_push_fmt("lv_chk", "%u", get_irn_idx(var));
511         stat_ev_tim_push();
512
513         /* If there is no dominance relation, go out, too */
514         def_bl = get_nodes_block(var);
515         if (!block_dominates(def_bl, bl)) {
516                 stat_ev("lv_chk_no_dom");
517                 goto end;
518         }
519
520         /*
521          * If the block in question is the same as the definition block,
522          * the algorithm is simple. Just check for uses not inside this block.
523          */
524         if (def_bl == bl) {
525                 const ir_edge_t *edge;
526
527                 stat_ev("lv_chk_def_block");
528                 DBG((lv->dbg, LEVEL_2, "lv check same block %+F in %+F\n", var, bl));
529                 foreach_out_edge (var, edge) {
530                         ir_node *use    = get_edge_src_irn(edge);
531                         ir_node *use_bl;
532
533                         if (!is_liveness_node(use))
534                                 continue;
535
536                         stat_ev_cnt_inc(uses);
537                         use_bl = get_nodes_block(use);
538                         if (is_Phi(use)) {
539                                 int pos = get_edge_src_pos(edge);
540                                 use_bl  = get_Block_cfgpred_block(use_bl, pos);
541
542                                 if (use_bl == bl) {
543                                         DBG((lv->dbg, LEVEL_2, "\tphi %+F in succ %+F,%d -> live end\n", use, use_bl, pos));
544                                         res |= lv_chk_state_end;
545                                 }
546                         }
547
548                         if (use_bl != def_bl) {
549                                 res = lv_chk_state_end | lv_chk_state_out;
550                                 goto end;
551                         }
552                 }
553
554                 goto end;
555         }
556
557         /*
558          * this is the more complicated case.
559          * We try to gather as much information as possible during looking
560          * at the uses.
561          *
562          * Note that we know for sure that bl != def_bl. That is sometimes
563          * silently exploited below.
564          */
565         else {
566                 bl_info_t *def = get_block_info(lv, def_bl);
567                 bl_info_t *bli = get_block_info(lv, bl);
568                 bitset_t *uses = bitset_alloca(lv->n_blocks);
569                 bitset_t *Tq;
570
571                 unsigned i, min_dom, max_dom;
572                 const ir_edge_t *edge;
573
574                 /* if the block has no DFS info, it cannot be reached.
575                  * This can happen in functions with endless loops.
576                  * we then go out, since nothing is live there.
577                  *
578                  * TODO: Is that right?
579                  */
580                 if (!bli)
581                         goto end;
582
583                 (void) def;
584                 DBG((lv->dbg, LEVEL_2, "lv check %+F (def in %+F #%d) in different block %+F #%d\n",
585                                         var, def_bl, def->id, bl, bli->id));
586
587                 foreach_out_edge (var, edge) {
588                         ir_node *user = get_edge_src_irn(edge);
589                         int mask      = lv_chk_state_in;
590
591                         ir_node *use_bl;
592                         bl_info_t *bi;
593
594                         /* if the user is no liveness node, the use does not count */
595                         if (!is_liveness_node(user))
596                                 continue;
597
598                         stat_ev_cnt_inc(uses);
599
600                         /* if the user is a phi, the use is in the predecessor
601                          * furthermore, prepare a mask so that in the case where
602                          * bl (the block in question) coincides with a use, it
603                          * can be marked live_end there. */
604                         use_bl = get_nodes_block(user);
605                         if (is_Phi(user)) {
606                                 int pos = get_edge_src_pos(edge);
607                                 use_bl  = get_Block_cfgpred_block(use_bl, pos);
608                                 mask   |= lv_chk_state_end;
609                         }
610
611
612                         /* if the use block coincides with the query block, we
613                          * already gather a little liveness information.
614                          * The variable is surely live there, since bl != def_bl
615                          * (that case is treated above). */
616                         if (use_bl == bl)
617                                 res |= mask;
618
619                         bi = get_block_info(lv, use_bl);
620
621                         if (bi)
622                                 bitset_set(uses, bi->id);
623                 }
624
625                 /* get the dominance range which really matters. all uses outside
626                  * the definition's dominance range are not to consider. note,
627                  * that the definition itself is also not considered. The case
628                  * where bl == def_bl is considered above. */
629                 min_dom = get_Block_dom_tree_pre_num(def_bl) + 1;
630                 max_dom = get_Block_dom_max_subtree_pre_num(def_bl);
631
632                 DBG((lv->dbg, LEVEL_2, "\tuses: %B\n", uses));
633
634                 /* prepare a set with all reachable back edge targets.
635                  * this will determine our "looking points" from where
636                  * we will search/find the calculated uses. */
637                 Tq = bli->be_tgt_reach;
638
639                 /* now, visit all viewing points in the temporary bitset lying
640                  * in the dominance range of the variable. Note that for reducible
641                  * flow-graphs the first iteration is sufficient and the loop
642                  * will be left. */
643                 DBG((lv->dbg, LEVEL_2, "\tbe tgt reach: %B, dom span: [%d, %d]\n", Tq, min_dom, max_dom));
644                 i = bitset_next_set(Tq, min_dom);
645                 while(i <= max_dom) {
646                         bl_info_t *ti = lv->map[i];
647                         int use_in_current_block = bitset_is_set(uses, ti->id);
648
649                         stat_ev_cnt_inc(iter);
650
651                         /*
652                          * This is somewhat tricky. Since this routine handles both, live in
653                          * and end/out we have to handle all the border cases correctly.
654                          * Each node is in its own red_reachable set (see calculation
655                          * function above). That means, that in the case where bl == t, the
656                          * intersection check of uses and reachability below will always
657                          * find an intersection, namely t.
658                          *
659                          * However, if a block contains a use and the variable is dead
660                          * afterwards, it is not live end/out at that block. Besides
661                          * back-edge target. If a var is live-in at a back-edge target it
662                          * is also live out/end there since the variable is live in the
663                          * underlying loop. So in the case where t == bl and that is not
664                          * a back-edge target, we have to remove that use from consideration
665                          * to determine if the var is live out/end there.
666                          *
667                          * Note that the live in information has been calculated by the
668                          * uses iteration above.
669                          */
670                         if (ti == bli && !bitset_is_set(lv->back_edge_tgt, ti->id)) {
671                                 DBG((lv->dbg, LEVEL_2, "\tlooking not from a back edge target and q == t. removing use: %d\n", ti->id));
672                                 bitset_clear(uses, ti->id);
673                         }
674
675                         /* If we can reach a use, the variable is live there and we say goodbye */
676                         DBG((lv->dbg, LEVEL_2, "\tlooking from %d: seeing %B\n", ti->id, ti->red_reachable));
677                         if (bitset_intersect(ti->red_reachable, uses)) {
678                                 res |= lv_chk_state_in | lv_chk_state_out | lv_chk_state_end;
679                                 goto end;
680                         }
681
682                         /*
683                          * if we deleted a use do to the commentary above, we have to
684                          * re-add it since it might be visible from further view points
685                          * (we only need that in the non-reducible case).
686                          */
687                         if (use_in_current_block)
688                                 bitset_set(uses, ti->id);
689
690                         i = bitset_next_set(Tq, get_Block_dom_max_subtree_pre_num(ti->block) + 1);
691                 }
692
693         }
694
695 end:
696         stat_ev_tim_pop("lv_chk_query_time");
697         stat_ev_cnt_done(uses, "lv_chk_uses");
698         stat_ev_cnt_done(iter, "lv_chk_iter");
699         stat_ev_ctx_pop("lv_chk");
700
701         return res;
702 }