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