cleanup: Remove duplicate MIN/MAX macros.
[libfirm] / ir / ana / irlivechk.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 Inria Rhone-Alpes.
4  */
5
6 /**
7  * @file
8  * @date    21.04.2007
9  * @author  Sebastian Hack
10  * @brief
11  *
12  * Liveness checks as developed by Benoit Boissinot, Fabrice Rastello and myself.
13  *
14  * The speciality here is, that nothing has to be recomputed if new nodes are created
15  * or old ones deleted.
16  *
17  * This algo has one core routine check_live_end_internal() which performs the liveness check.
18  * It only relies on the precomputation done in the constructor, which in turn needs:
19  * - out edges
20  * - the dominance tree
21  * - data obtained from a depth-first-search
22  *
23  * The precomputation remains valid as long as the CFG is not altered.
24  */
25 #include <config.h>
26
27 #include <stdio.h>
28
29 /* statev is expensive here, only enable when needed */
30 #define DISABLE_STATEV
31
32 #include "irgraph_t.h"
33 #include "irnode_t.h"
34 #include "irnodemap.h"
35 #include "iredges_t.h"
36 #include "irdom.h"
37 #include "irdump.h"
38
39 #include "dfs_t.h"
40 #include "bitset.h"
41 #include "irlivechk.h"
42
43 #include "statev_t.h"
44
45 typedef struct bl_info_t {
46         const ir_node *block;      /**< The block. */
47
48         unsigned be_tgt_calc : 1;
49         unsigned id : 31;          /**< a tight number for the block.
50                                                                  we're just reusing the pre num from
51                                                                  the DFS. */
52         bitset_t *red_reachable;   /**< Holds all id's if blocks reachable
53                                                                  in the CFG modulo back edges. */
54
55         bitset_t *be_tgt_reach;    /**< target blocks of back edges whose
56                                                                  sources are reachable from this block
57                                                                  in the reduced graph. */
58 } bl_info_t;
59
60 struct lv_chk_t {
61         ir_nodemap     block_infos;
62         struct obstack obst;
63         dfs_t         *dfs;
64         int            n_blocks;
65         bitset_t      *back_edge_src;
66         bitset_t      *back_edge_tgt;
67         bl_info_t    **map;
68         DEBUG_ONLY(firm_dbg_module_t *dbg;)
69 };
70
71 static bl_info_t *get_block_info(lv_chk_t *lv, const ir_node *block)
72 {
73         bl_info_t *info = ir_nodemap_get(bl_info_t, &lv->block_infos, block);
74         if (info == NULL) {
75                 info                = OALLOC(&lv->obst, bl_info_t);
76                 info->id            = get_Block_dom_tree_pre_num(block);
77                 info->block         = block;
78                 info->red_reachable = bitset_obstack_alloc(&lv->obst, lv->n_blocks);
79                 info->be_tgt_reach  = bitset_obstack_alloc(&lv->obst, lv->n_blocks);
80                 info->be_tgt_calc   = 0;
81                 ir_nodemap_insert(&lv->block_infos, block, info);
82         }
83         return info;
84 }
85
86 /**
87  * Compute the transitive closure on the reduced graph.
88  * The reduced graph is the original graph without back edges.
89  * Since that is a DAG, a reverse post order of the graph gives a toposort
90  * which is ideally suited to compute the transitive closure.
91  * Note also, that the DFS tree of the reduced graph is the same than the one
92  * of the original graph. This saves us computing a new reverse post order.
93  * We also can re-use the DFS tree of the original graph.
94  */
95 static void red_trans_closure(lv_chk_t *lv)
96 {
97         int i, n;
98
99         for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
100                 const ir_node *bl = (const ir_node*) dfs_get_post_num_node(lv->dfs, i);
101                 bl_info_t *bi = get_block_info(lv, bl);
102
103                 bitset_set(bi->red_reachable, bi->id);
104                 foreach_block_succ (bl, edge) {
105                         ir_node *succ = get_edge_src_irn(edge);
106                         bl_info_t *si = get_block_info(lv, succ);
107                         dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, bl, succ);
108
109                         /*
110                          * if the successor is no back edge, include all reachable
111                          * blocks from there into the reachable set of the current node
112                          */
113                         if (kind != DFS_EDGE_BACK) {
114                                 assert(dfs_get_post_num(lv->dfs, bl) > dfs_get_post_num(lv->dfs, succ));
115                                 bitset_or(bi->red_reachable, si->red_reachable);
116                         }
117
118                         /* mark the block as a back edge src and succ as back edge tgt. */
119                         else {
120                                 bitset_set(lv->back_edge_src, bi->id);
121                                 bitset_set(lv->back_edge_tgt, si->id);
122                         }
123                 }
124
125         }
126
127 }
128
129 static void compute_back_edge_chain(lv_chk_t *lv, const ir_node *bl)
130 {
131         bitset_t *tmp = bitset_alloca(lv->n_blocks);
132         bl_info_t *bi = get_block_info(lv, bl);
133
134         DBG((lv->dbg, LEVEL_2, "computing T_%d\n", bi->id));
135
136         /* put all back edge sources reachable (reduced) from here in tmp */
137         bitset_copy(tmp, bi->red_reachable);
138         bitset_set(tmp, bi->id);
139         bitset_and(tmp, lv->back_edge_src);
140         bi->be_tgt_calc = 1;
141
142         DBG((lv->dbg, LEVEL_2, "\treachable be src: %B\n", tmp));
143
144         /* iterate over them ... */
145         bitset_foreach(tmp, elm) {
146                 bl_info_t *si = lv->map[elm];
147
148                 /* and find back edge targets which are not reduced reachable from bl */
149                 foreach_block_succ (si->block, edge) {
150                         ir_node *tgt         = get_edge_src_irn(edge);
151                         bl_info_t *ti        = get_block_info(lv, tgt);
152                         dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, si->block, tgt);
153
154                         if (kind == DFS_EDGE_BACK && !bitset_is_set(bi->red_reachable, ti->id)) {
155                                 if (!ti->be_tgt_calc)
156                                         compute_back_edge_chain(lv, tgt);
157                                 bitset_set(bi->be_tgt_reach, ti->id);
158                                 bitset_or(bi->be_tgt_reach, ti->be_tgt_reach);
159                         }
160                 }
161                 bitset_clear(bi->be_tgt_reach, bi->id);
162         }
163 }
164
165
166 static inline void compute_back_edge_chains(lv_chk_t *lv)
167 {
168         int i, n;
169
170         DBG((lv->dbg, LEVEL_2, "back edge sources: %B\n", lv->back_edge_src));
171         bitset_foreach(lv->back_edge_src, elm) {
172                 compute_back_edge_chain(lv, lv->map[elm]->block);
173         }
174
175         for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
176                 const ir_node *bl = (const ir_node*) dfs_get_post_num_node(lv->dfs, i);
177                 bl_info_t *bi     = get_block_info(lv, bl);
178
179                 if (!bitset_is_set(lv->back_edge_tgt, bi->id)) {
180                         foreach_block_succ (bl, edge) {
181                                 ir_node *succ = get_edge_src_irn(edge);
182                                 bl_info_t *si = get_block_info(lv, succ);
183                                 dfs_edge_kind_t kind = dfs_get_edge_kind(lv->dfs, bl, succ);
184
185                                 if (kind != DFS_EDGE_BACK) {
186                                         assert(dfs_get_post_num(lv->dfs, bl) > dfs_get_post_num(lv->dfs, succ));
187                                         bitset_or(bi->be_tgt_reach, si->be_tgt_reach);
188                                 }
189                         }
190                 }
191         }
192
193         for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
194                 const ir_node *bl = (const ir_node*) dfs_get_post_num_node(lv->dfs, i);
195                 bl_info_t *bi     = get_block_info(lv, bl);
196                 bitset_set(bi->be_tgt_reach, bi->id);
197         }
198 }
199
200 lv_chk_t *lv_chk_new(ir_graph *irg)
201 {
202         lv_chk_t *res = XMALLOC(lv_chk_t);
203         int i;
204
205         assure_doms(irg);
206
207         stat_ev_tim_push();
208         ir_nodemap_init(&res->block_infos, irg);
209         obstack_init(&res->obst);
210
211         FIRM_DBG_REGISTER(res->dbg, "ir.ana.lvchk");
212
213         res->dfs           = dfs_new(&absgraph_irg_cfg_succ, irg);
214         res->n_blocks      = dfs_get_n_nodes(res->dfs);
215         res->back_edge_src = bitset_obstack_alloc(&res->obst, res->n_blocks);
216         res->back_edge_tgt = bitset_obstack_alloc(&res->obst, res->n_blocks);
217         res->map           = OALLOCNZ(&res->obst, bl_info_t*, res->n_blocks);
218
219         /* fill the map which maps pre_num to block infos */
220         for (i = res->n_blocks - 1; i >= 0; --i) {
221                 ir_node *irn  = (ir_node *) dfs_get_pre_num_node(res->dfs, i);
222                 bl_info_t *bi = get_block_info(res, irn);
223                 assert(bi->id < res->n_blocks);
224                 assert(res->map[bi->id] == NULL);
225                 res->map[bi->id] = bi;
226         }
227
228         /* first of all, compute the transitive closure of the CFG *without* back edges */
229         red_trans_closure(res);
230
231         /* compute back edge chains */
232         compute_back_edge_chains(res);
233
234 #ifndef NDEBUG
235         DBG((res->dbg, LEVEL_1, "liveness chk in %+F\n", irg));
236         for (i = res->n_blocks - 1; i >= 0; --i) {
237                 const ir_node *irn = (const ir_node*) dfs_get_pre_num_node(res->dfs, i);
238                 bl_info_t *bi      = get_block_info(res, irn);
239                 DBG((res->dbg, LEVEL_1, "lv_chk for %d -> %+F\n", i, irn));
240                 DBG((res->dbg, LEVEL_1, "\tred reach: %B\n", bi->red_reachable));
241                 DBG((res->dbg, LEVEL_1, "\ttgt reach: %B\n", bi->be_tgt_reach));
242         }
243 #endif
244
245         DBG((res->dbg, LEVEL_1, "back edge src: %B\n", res->back_edge_src));
246         DBG((res->dbg, LEVEL_1, "back edge tgt: %B\n", res->back_edge_tgt));
247
248         stat_ev_tim_pop("lv_chk_cons_time");
249         return res;
250 }
251
252 void lv_chk_free(lv_chk_t *lv)
253 {
254         dfs_free(lv->dfs);
255         obstack_free(&lv->obst, NULL);
256         ir_nodemap_destroy(&lv->block_infos);
257         xfree(lv);
258 }
259
260 unsigned lv_chk_bl_xxx(lv_chk_t *lv, const ir_node *bl, const ir_node *var)
261 {
262         int res  = 0;
263         ir_node *def_bl;
264         stat_ev_cnt_decl(uses);
265         stat_ev_cnt_decl(iter);
266
267         assert(is_Block(bl) && "can only check for liveness in a block");
268
269         /* If the variable ist no liveness related var, bail out. */
270         if (!is_liveness_node(var))
271                 return 0;
272
273         stat_ev_ctx_push_fmt("lv_chk", "%u", get_irn_idx(var));
274         stat_ev_tim_push();
275
276         /* If there is no dominance relation, go out, too */
277         def_bl = get_nodes_block(var);
278         if (!block_dominates(def_bl, bl)) {
279                 stat_ev("lv_chk_no_dom");
280                 goto end;
281         }
282
283         /*
284          * If the block in question is the same as the definition block,
285          * the algorithm is simple. Just check for uses not inside this block.
286          */
287         if (def_bl == bl) {
288                 stat_ev("lv_chk_def_block");
289                 DBG((lv->dbg, LEVEL_2, "lv check same block %+F in %+F\n", var, bl));
290                 foreach_out_edge (var, edge) {
291                         ir_node *use    = get_edge_src_irn(edge);
292                         ir_node *use_bl;
293
294                         if (!is_liveness_node(use))
295                                 continue;
296
297                         stat_ev_cnt_inc(uses);
298                         use_bl = get_nodes_block(use);
299                         if (is_Phi(use)) {
300                                 int pos = get_edge_src_pos(edge);
301                                 use_bl  = get_Block_cfgpred_block(use_bl, pos);
302
303                                 if (use_bl == bl) {
304                                         DBG((lv->dbg, LEVEL_2, "\tphi %+F in succ %+F,%d -> live end\n", use, use_bl, pos));
305                                         res |= lv_chk_state_end;
306                                 }
307                         }
308
309                         if (use_bl != def_bl) {
310                                 res = lv_chk_state_end | lv_chk_state_out;
311                                 goto end;
312                         }
313                 }
314
315                 goto end;
316         }
317
318         /*
319          * this is the more complicated case.
320          * We try to gather as much information as possible during looking
321          * at the uses.
322          *
323          * Note that we know for sure that bl != def_bl. That is sometimes
324          * silently exploited below.
325          */
326         else {
327                 bl_info_t *def = get_block_info(lv, def_bl);
328                 bl_info_t *bli = get_block_info(lv, bl);
329                 bitset_t *uses = bitset_alloca(lv->n_blocks);
330                 bitset_t *Tq;
331
332                 size_t i;
333                 unsigned min_dom, max_dom;
334
335                 /* if the block has no DFS info, it cannot be reached.
336                  * This can happen in functions with endless loops.
337                  * we then go out, since nothing is live there.
338                  *
339                  * TODO: Is that right?
340                  */
341                 if (!bli)
342                         goto end;
343
344                 (void) def;
345                 DBG((lv->dbg, LEVEL_2, "lv check %+F (def in %+F #%d) in different block %+F #%d\n",
346                                         var, def_bl, def->id, bl, bli->id));
347
348                 foreach_out_edge (var, edge) {
349                         ir_node *user = get_edge_src_irn(edge);
350                         int mask      = lv_chk_state_in;
351
352                         ir_node *use_bl;
353                         bl_info_t *bi;
354
355                         /* if the user is no liveness node, the use does not count */
356                         if (!is_liveness_node(user))
357                                 continue;
358
359                         stat_ev_cnt_inc(uses);
360
361                         /* if the user is a phi, the use is in the predecessor
362                          * furthermore, prepare a mask so that in the case where
363                          * bl (the block in question) coincides with a use, it
364                          * can be marked live_end there. */
365                         use_bl = get_nodes_block(user);
366                         if (is_Phi(user)) {
367                                 int pos = get_edge_src_pos(edge);
368                                 use_bl  = get_Block_cfgpred_block(use_bl, pos);
369                                 mask   |= lv_chk_state_end;
370                         }
371
372
373                         /* if the use block coincides with the query block, we
374                          * already gather a little liveness information.
375                          * The variable is surely live there, since bl != def_bl
376                          * (that case is treated above). */
377                         if (use_bl == bl)
378                                 res |= mask;
379
380                         bi = get_block_info(lv, use_bl);
381
382                         if (bi)
383                                 bitset_set(uses, bi->id);
384                 }
385
386                 /* get the dominance range which really matters. all uses outside
387                  * the definition's dominance range are not to consider. note,
388                  * that the definition itself is also not considered. The case
389                  * where bl == def_bl is considered above. */
390                 min_dom = get_Block_dom_tree_pre_num(def_bl) + 1;
391                 max_dom = get_Block_dom_max_subtree_pre_num(def_bl);
392
393                 DBG((lv->dbg, LEVEL_2, "\tuses: %B\n", uses));
394
395                 /* prepare a set with all reachable back edge targets.
396                  * this will determine our "looking points" from where
397                  * we will search/find the calculated uses. */
398                 Tq = bli->be_tgt_reach;
399
400                 /* now, visit all viewing points in the temporary bitset lying
401                  * in the dominance range of the variable. Note that for reducible
402                  * flow-graphs the first iteration is sufficient and the loop
403                  * will be left. */
404                 DBG((lv->dbg, LEVEL_2, "\tbe tgt reach: %B, dom span: [%d, %d]\n", Tq, min_dom, max_dom));
405                 i = bitset_next_set(Tq, min_dom);
406                 while (i <= max_dom) {
407                         bl_info_t *ti = lv->map[i];
408                         int use_in_current_block = bitset_is_set(uses, ti->id);
409
410                         stat_ev_cnt_inc(iter);
411
412                         /*
413                          * This is somewhat tricky. Since this routine handles both, live in
414                          * and end/out we have to handle all the border cases correctly.
415                          * Each node is in its own red_reachable set (see calculation
416                          * function above). That means, that in the case where bl == t, the
417                          * intersection check of uses and reachability below will always
418                          * find an intersection, namely t.
419                          *
420                          * However, if a block contains a use and the variable is dead
421                          * afterwards, it is not live end/out at that block. Besides
422                          * back-edge target. If a var is live-in at a back-edge target it
423                          * is also live out/end there since the variable is live in the
424                          * underlying loop. So in the case where t == bl and that is not
425                          * a back-edge target, we have to remove that use from consideration
426                          * to determine if the var is live out/end there.
427                          *
428                          * Note that the live in information has been calculated by the
429                          * uses iteration above.
430                          */
431                         if (ti == bli && !bitset_is_set(lv->back_edge_tgt, ti->id)) {
432                                 DBG((lv->dbg, LEVEL_2, "\tlooking not from a back edge target and q == t. removing use: %d\n", ti->id));
433                                 bitset_clear(uses, ti->id);
434                         }
435
436                         /* If we can reach a use, the variable is live there and we say goodbye */
437                         DBG((lv->dbg, LEVEL_2, "\tlooking from %d: seeing %B\n", ti->id, ti->red_reachable));
438                         if (bitset_intersect(ti->red_reachable, uses)) {
439                                 res |= lv_chk_state_in | lv_chk_state_out | lv_chk_state_end;
440                                 goto end;
441                         }
442
443                         /*
444                          * if we deleted a use do to the commentary above, we have to
445                          * re-add it since it might be visible from further view points
446                          * (we only need that in the non-reducible case).
447                          */
448                         if (use_in_current_block)
449                                 bitset_set(uses, ti->id);
450
451                         i = bitset_next_set(Tq, get_Block_dom_max_subtree_pre_num(ti->block) + 1);
452                 }
453
454         }
455
456 end:
457         stat_ev_tim_pop("lv_chk_query_time");
458         stat_ev_cnt_done(uses, "lv_chk_uses");
459         stat_ev_cnt_done(iter, "lv_chk_iter");
460         stat_ev_ctx_pop("lv_chk");
461
462         return res;
463 }