09a38b09d7f8e604214cefd3620af6ff1cd06e15
[libfirm] / ir / ana / irlivechk.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  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
41 #include <stdio.h>
42
43 #include "irgraph_t.h"
44 #include "irphase_t.h"
45 #include "iredges_t.h"
46 #include "irprintf.h"
47 #include "irdump.h"
48
49 #include "dfs_t.h"
50 #include "bitset.h"
51 #include "util.h"
52
53 #include "irlivechk.h"
54
55 typedef struct _bl_info_t {
56         ir_node *block;            /**< The block. */
57
58         int id;                    /**< a tight number for the block.
59                                                                  we're just reusing the pre num from
60                                                                  the DFS. */
61
62         bitset_t *red_reachable;   /**< Holds all id's if blocks reachable
63                                                                  in the CFG modulo back edges. */
64
65         bitset_t *be_tgt_reach;    /**< target blocks of back edges whose
66                                                                  sources are reachable from this block
67                                                                  in the reduced graph. */
68
69         bitset_t *be_tgt_dom;      /**< target blocks of back edges which
70                                                                  are dominated by this block. */
71 } bl_info_t;
72
73 #define get_block_info(lv, bl) ((bl_info_t *) phase_get_irn_data(&(lv)->ph, bl))
74
75 struct _lv_chk_t {
76         ir_phase ph;
77         dfs_t *dfs;
78         firm_dbg_module_t *dbg;
79         int n_blocks;
80         bitset_t *back_edge_src;
81         bitset_t *back_edge_tgt;
82         bl_info_t **map;
83 };
84
85 static void *init_block_data(ir_phase *ph, ir_node *irn, void *old)
86 {
87         lv_chk_t *lv      = container_of(ph, lv_chk_t, ph);
88         bl_info_t *bi     = phase_alloc(ph, sizeof(bi[0]));
89
90         bi->id            = dfs_get_pre_num(lv->dfs, irn);
91         bi->block         = irn;
92         bi->red_reachable = bitset_obstack_alloc(phase_obst(ph), lv->n_blocks);
93         bi->be_tgt_reach  = bitset_obstack_alloc(phase_obst(ph), lv->n_blocks);
94         bi->be_tgt_dom    = bitset_obstack_alloc(phase_obst(ph), lv->n_blocks);
95         return bi;
96 }
97
98 /**
99  * Filter function to select all nodes for which liveness is computed.
100  * @param irn A node.
101  * @return    1 if the node shall be considered in liveness, 0 if not.
102  */
103 static INLINE int is_liveness_node(const ir_node *irn)
104 {
105         switch(get_irn_opcode(irn)) {
106         case iro_Block:
107         case iro_Bad:
108         case iro_End:
109                 return 0;
110         default:;
111         }
112
113         return 1;
114 }
115
116 /**
117  * Compute the transitive closure on the reduced graph.
118  * The reduced graph is the original graph without back edges.
119  * Since that is a DAG, a reverse post order of the graph gives a toposort
120  * which is ideally suited to compute the transitive closure.
121  * Note also, that the DFS tree of the reduced graph is the same than the one
122  * of the original graph. This saves us computing a new reverse post order.
123  * We also can re-use the DFS tree of the original graph.
124  */
125 static void red_trans_closure(lv_chk_t *lv)
126 {
127         int i, n;
128
129         for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
130                 ir_node *bl   = dfs_get_post_num_node(lv->dfs, i);
131                 bl_info_t *bi = get_block_info(lv, bl);
132
133                 const ir_edge_t *edge;
134
135                 foreach_block_succ (bl, edge) {
136                         ir_node *succ = get_edge_src_irn(edge);
137                         bl_info_t *si = get_block_info(lv, succ);
138                         dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, bl, succ);
139
140                         /*
141                          * if the successor is no back edge, include all reachable
142                          * blocks from there into the reachable set of the current node
143                          */
144                         if (kind != DFS_EDGE_BACK) {
145                                 assert(dfs_get_post_num(lv->dfs, bl)
146                                                 > dfs_get_post_num(lv->dfs, succ));
147                                 bitset_or(bi->red_reachable, si->red_reachable);
148                                 bitset_set(bi->red_reachable, si->id);
149                         }
150
151                         /* mark the block as a back edge src and succ as back edge tgt. */
152                         else {
153                                 bitset_set(lv->back_edge_src, bi->id);
154                                 bitset_set(lv->back_edge_tgt, si->id);
155                         }
156                 }
157
158         }
159
160 }
161
162 /**
163  * Compute the two back edge sets for each block.
164  * <code>be_tgt_reach</code> contains all target blocks of a back edges reachable from a node.
165  * <code>be_tgt_dom</code> contains all target blocks of back edges strictly dominated
166  * by a node.
167  */
168 static void compute_back_edge_sets(lv_chk_t *lv, ir_node *bl)
169 {
170         bl_info_t *bi = get_block_info(lv, bl);
171         bitset_t *tmp = bitset_alloca(lv->n_blocks);
172
173         bitset_pos_t elm;
174         ir_node *n;
175
176         dominates_for_each (bl, n) {
177                 bl_info_t *ni = get_block_info(lv, n);
178
179                 /* compute information for dominance sub tree */
180                 compute_back_edge_sets(lv, n);
181
182                 /*
183                  * of course all blocks dominated by blocks in the
184                  * subtree are also dominated by bl.
185                  */
186                 bitset_or(bi->be_tgt_dom, ni->be_tgt_dom);
187
188                 /*
189                  * add the immeditate dominee to the back edge tgt dominance
190                  * bitset if it is the target node of a back edge.
191                  */
192                 if (bitset_is_set(lv->back_edge_tgt, ni->id))
193                         bitset_set(bi->be_tgt_dom, ni->id);
194         }
195
196         /*
197          * iterate over all back edge src nodes which are reachable from
198          * this nodes and put the targets of the back edges in the be_tgt_reach
199          * bitset of the node.
200          */
201         bitset_copy(tmp, bi->red_reachable);
202         bitset_set(tmp, bi->id);
203         bitset_and(tmp, lv->back_edge_src);
204         bitset_foreach (tmp, elm) {
205                 ir_node *src = lv->map[elm]->block;
206                 const ir_edge_t *edge;
207
208                 foreach_block_succ (src, edge) {
209                         ir_node *succ        = get_edge_src_irn(edge);
210                         dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, src, succ);
211
212                         if (kind == DFS_EDGE_BACK) {
213                                 bl_info_t *si = get_block_info(lv, succ);
214                                 bitset_set(bi->be_tgt_reach, si->id);
215                         }
216                 }
217         }
218 }
219
220 lv_chk_t *lv_chk_new(ir_graph *irg)
221 {
222         lv_chk_t *res = xmalloc(sizeof(res[0]));
223         struct obstack *obst;
224         int i;
225
226         phase_init(&res->ph, "liveness check", irg, PHASE_DEFAULT_GROWTH, init_block_data, NULL);
227         obst = phase_obst(&res->ph);
228
229         FIRM_DBG_REGISTER(res->dbg, "ir.ana.lvchk");
230
231         res->dfs           = dfs_new(&absgraph_irg_cfg_succ, irg);
232         res->n_blocks      = dfs_get_n_nodes(res->dfs);
233         res->back_edge_src = bitset_obstack_alloc(obst, res->n_blocks);
234         res->back_edge_tgt = bitset_obstack_alloc(obst, res->n_blocks);
235         res->map           = obstack_alloc(obst, res->n_blocks * sizeof(res->map[0]));
236
237 #if 1
238         {
239                 char name[256];
240                 FILE *f;
241                 ir_snprintf(name, sizeof(name), "dfs_%F.dot", irg);
242                 if ((f = fopen(name, "wt")) != NULL) {
243                         dfs_dump(res->dfs, f);
244                         fclose(f);
245                 }
246                 dump_ir_block_graph(irg, "-lvchk");
247         }
248 #endif
249
250         /* fill the map which maps pre_num to block infos */
251         for (i = res->n_blocks - 1; i >= 0; --i) {
252                 ir_node *irn = dfs_get_pre_num_node(res->dfs, i);
253                 res->map[i]  = phase_get_or_set_irn_data(&res->ph, irn);
254         }
255
256         /* first of all, compute the transitive closure of the CFG *without* back edges */
257         red_trans_closure(res);
258
259         /* now fill the two remaining bitsets concerning back edges */
260         compute_back_edge_sets(res, get_irg_start_block(irg));
261
262         DBG((res->dbg, LEVEL_1, "liveness chk in %+F\n", irg));
263         for (i = res->n_blocks - 1; i >= 0; --i) {
264                 ir_node *irn  = dfs_get_pre_num_node(res->dfs, i);
265                 bl_info_t *bi = get_block_info(res, irn);
266                 DBG((res->dbg, LEVEL_1, "lv_chk for %d -> %+F\n", i, irn));
267                 DBG((res->dbg, LEVEL_1, "\tred reach: %B\n", bi->red_reachable));
268                 DBG((res->dbg, LEVEL_1, "\ttgt reach: %B\n", bi->be_tgt_reach));
269                 DBG((res->dbg, LEVEL_1, "\ttgt dom:   %B\n", bi->be_tgt_dom));
270         }
271
272         DBG((res->dbg, LEVEL_1, "back edge src: %B\n", res->back_edge_src));
273         DBG((res->dbg, LEVEL_1, "back edge tgt: %B\n", res->back_edge_tgt));
274
275         return res;
276 }
277
278 void lv_chk_free(lv_chk_t *lv)
279 {
280         obstack_free(phase_obst(&lv->ph), NULL);
281         dfs_free(lv->dfs);
282         xfree(lv);
283 }
284
285 /**
286  * Check if a node is live at the end of a block.
287  * This function is for internal use as its code is shared between
288  * the in/end routines below. It is almost the "live_end" routine
289  * but passing in the bitset for recording the blocks where the variable
290  * is used saves some effort in the "live_in" routine. See below for
291  * details.
292  *
293  * @param lv    The liveness check environment.
294  * @param what  The node to check for.
295  * @param bl    The block under investigation.
296  * @param end   If 1, it is tested if the node is live at the end.
297  *              If 0, it is only tested if the node is live out.
298  * @param uses  A bitset where this routine records all ids of blocks
299  *              where this variable is used. Note that the bitset
300  *              is only guaranteed to be filled if the node was not
301  *              live at the end of the block.
302  * @return      1, if @p what is live at the end at @p bl.
303  */
304 static int check_live_internal(const lv_chk_t *lv, const ir_node *what, const ir_node *bl, int end, bitset_t *uses)
305 {
306         ir_node *what_bl;
307
308         assert(is_Block(bl) && "can only check for liveness in a block");
309
310         if (!is_liveness_node(what))
311                 return 0;
312
313         what_bl = get_nodes_block(what);
314         if (!block_dominates(what_bl, bl))
315                 return 0;
316
317         /*
318          * If the block in question is the same as the definition block,
319          * the algorithm is simple. JUst check for uses not inside this block.
320          */
321         if (what_bl == bl) {
322                 const ir_edge_t *edge;
323
324                 DBG((lv->dbg, LEVEL_2, "lv check same block %+F in %+F\n", what, bl));
325                 foreach_out_edge (what, edge) {
326                         ir_node *use    = get_edge_src_irn(edge);
327                         ir_node *use_bl;
328
329                         if (!is_liveness_node(use))
330                                 continue;
331
332                         use_bl = get_nodes_block(use);
333                         if (is_Phi(use)) {
334                                 int pos = get_edge_src_pos(edge);
335                                 use_bl  = get_Block_cfgpred_block(use_bl, pos);
336
337                                 if (end && use_bl == bl) {
338                                         DBG((lv->dbg, LEVEL_2, "\tphi %+F in succ %+F,%d -> live end\n", use, use_bl, pos));
339                                         return 1;
340                                 }
341                         }
342
343                         if (use_bl != what_bl)
344                                 return 1;
345                 }
346
347                 return 0;
348         }
349
350         /* this is the complicated case */
351         else {
352                 bitset_t *visited   = bitset_alloca(lv->n_blocks);
353                 bitset_t *to_visit  = bitset_alloca(lv->n_blocks);
354                 bitset_t *next      = bitset_alloca(lv->n_blocks);
355                 bl_info_t *def      = get_block_info(lv, what_bl);
356                 bl_info_t *bli      = get_block_info(lv, bl);
357
358                 const ir_edge_t *edge;
359
360                 foreach_out_edge (what, edge) {
361                         ir_node *user   = get_edge_src_irn(edge);
362                         ir_node *use_bl;
363
364                         if (!is_liveness_node(user))
365                                 continue;
366
367                         use_bl = get_nodes_block(user);
368                         if (is_Phi(user)) {
369                                 int pos          = get_edge_src_pos(edge);
370                                 ir_node *pred_bl = get_Block_cfgpred_block(use_bl, pos);
371                                 bl_info_t *bi    = get_block_info(lv, pred_bl);
372
373                                 if (end && pred_bl == bl)
374                                         return 1;
375
376                                 bitset_set(uses, bi->id);
377                         }
378
379                         else {
380                                 bl_info_t *bi   = get_block_info(lv, use_bl);
381                                 bitset_set(uses, bi->id);
382                         }
383                 }
384                 DBG((lv->dbg, LEVEL_2, "\tuses: %B\n", uses));
385
386                 bitset_clear(uses, def->id);
387                 bitset_set(to_visit, bli->id);
388                 do {
389                         int id        = bitset_next_set(to_visit, 0);
390                         bl_info_t *bi = lv->map[id];
391
392                         DBG((lv->dbg, LEVEL_2, "\tto visit: %B\n", to_visit));
393                         DBG((lv->dbg, LEVEL_2, "\tvisited:  %B\n", visited));
394
395                         /*
396                          * if one of the blocks is reachable, the node must be live there.
397                          * Not that this is not sufficient, since the nodes reachable
398                          * via back edges are not contained in the red_reachable set.
399                          */
400                         if (bitset_intersect(bi->red_reachable, uses))
401                                 return 1;
402
403                         /*
404                          * if not, we have to check the back edges in question, if
405                          * they lead to places which are reachable.
406                          */
407                         else {
408                                 bitset_set(visited, id);
409                                 bitset_or(visited, bi->red_reachable);
410
411                                 bitset_copy(next, bi->be_tgt_reach);
412                                 bitset_and(next, def->be_tgt_dom);
413                                 DBG((lv->dbg, LEVEL_2, "\tnext: %B\n----\n", next));
414
415                                 if (bitset_intersect(uses, next))
416                                         return 1;
417
418                                 bitset_or(to_visit, next);
419                                 bitset_andnot(to_visit, visited);
420
421                         }
422                 } while (!bitset_is_empty(to_visit));
423         }
424
425         return 0;
426 }
427
428 int lv_chk_bl_end(const lv_chk_t *lv, const ir_node *bl, const ir_node *what)
429 {
430         bitset_t *uses = bitset_alloca(lv->n_blocks);
431         return check_live_internal(lv, what, bl, 1, uses);
432 }
433
434 int lv_chk_bl_out(const lv_chk_t *lv, const ir_node *bl, const ir_node *what)
435 {
436         bitset_t *uses = bitset_alloca(lv->n_blocks);
437         return check_live_internal(lv, what, bl, 0, uses);
438 }
439
440 int lv_chk_bl_in(const lv_chk_t *lv, const ir_node *bl, const ir_node *what)
441 {
442         /*
443          * only check, if the node is not defined in this block.
444          * Under SSA, a node can never be live in at its definition block.
445          */
446         if (get_nodes_block(what) != bl) {
447                 bl_info_t *bi   = get_block_info(lv, bl);
448                 int id          = bi->id;
449                 bitset_t *uses  = bitset_alloca(lv->n_blocks);
450                 int live_at_end = check_live_internal(lv, what, bl, 1, uses);
451
452                 /* to be live in, the value must be live at the end or have a use in this block */
453                 return live_at_end || bitset_is_set(uses, id);
454         }
455
456         return 0;
457 }