iropt: cmp(~x & 1, 0) => !cmp(x & 1, 0)
[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  * @brief
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 "irnodemap.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 struct lv_chk_t {
77         ir_nodemap     block_infos;
78         struct obstack obst;
79         const dfs_t   *dfs;
80         int            n_blocks;
81         bitset_t      *back_edge_src;
82         bitset_t      *back_edge_tgt;
83         bl_info_t    **map;
84         DEBUG_ONLY(firm_dbg_module_t *dbg;)
85 };
86
87 static bl_info_t *get_block_info(lv_chk_t *lv, const ir_node *block)
88 {
89         bl_info_t *info = ir_nodemap_get(&lv->block_infos, block);
90         if (info == NULL) {
91                 info                = obstack_alloc(&lv->obst, sizeof(*info));
92                 info->id            = get_Block_dom_tree_pre_num(block);
93                 info->block         = block;
94                 info->red_reachable = bitset_obstack_alloc(&lv->obst, lv->n_blocks);
95                 info->be_tgt_reach  = bitset_obstack_alloc(&lv->obst, lv->n_blocks);
96                 info->be_tgt_calc   = 0;
97                 ir_nodemap_insert(&lv->block_infos, block, info);
98         }
99         return info;
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                 break;
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 = (const ir_node*) 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, const 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         size_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         size_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 = (const ir_node*) 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         for (i = 0, n = dfs_get_n_nodes(lv->dfs); i < n; ++i) {
238                 const ir_node *bl = (const ir_node*) dfs_get_post_num_node(lv->dfs, i);
239                 bl_info_t *bi     = get_block_info(lv, bl);
240                 bitset_set(bi->be_tgt_reach, bi->id);
241         }
242 }
243
244 lv_chk_t *lv_chk_new(ir_graph *irg, const dfs_t *dfs)
245 {
246         lv_chk_t *res = XMALLOC(lv_chk_t);
247         int i;
248
249         assure_doms(irg);
250
251         stat_ev_tim_push();
252         ir_nodemap_init(&res->block_infos, irg);
253         obstack_init(&res->obst);
254
255         FIRM_DBG_REGISTER(res->dbg, "ir.ana.lvchk");
256
257         res->dfs           = dfs;
258         res->n_blocks      = dfs_get_n_nodes(res->dfs);
259         res->back_edge_src = bitset_obstack_alloc(&res->obst, res->n_blocks);
260         res->back_edge_tgt = bitset_obstack_alloc(&res->obst, res->n_blocks);
261         res->map           = OALLOCNZ(&res->obst, bl_info_t*, res->n_blocks);
262
263         /* fill the map which maps pre_num to block infos */
264         for (i = res->n_blocks - 1; i >= 0; --i) {
265                 ir_node *irn  = (ir_node *) dfs_get_pre_num_node(res->dfs, i);
266                 bl_info_t *bi = get_block_info(res, irn);
267                 assert(bi->id < res->n_blocks);
268                 assert(res->map[bi->id] == NULL);
269                 res->map[bi->id] = bi;
270         }
271
272         /* first of all, compute the transitive closure of the CFG *without* back edges */
273         red_trans_closure(res);
274
275         /* compute back edge chains */
276         compute_back_edge_chains(res);
277
278 #ifndef NDEBUG
279         DBG((res->dbg, LEVEL_1, "liveness chk in %+F\n", irg));
280         for (i = res->n_blocks - 1; i >= 0; --i) {
281                 const ir_node *irn = (const ir_node*) dfs_get_pre_num_node(res->dfs, i);
282                 bl_info_t *bi      = get_block_info(res, irn);
283                 DBG((res->dbg, LEVEL_1, "lv_chk for %d -> %+F\n", i, irn));
284                 DBG((res->dbg, LEVEL_1, "\tred reach: %B\n", bi->red_reachable));
285                 DBG((res->dbg, LEVEL_1, "\ttgt reach: %B\n", bi->be_tgt_reach));
286         }
287 #endif
288
289         DBG((res->dbg, LEVEL_1, "back edge src: %B\n", res->back_edge_src));
290         DBG((res->dbg, LEVEL_1, "back edge tgt: %B\n", res->back_edge_tgt));
291
292         stat_ev_tim_pop("lv_chk_cons_time");
293         return res;
294 }
295
296 void lv_chk_free(lv_chk_t *lv)
297 {
298         obstack_free(&lv->obst, NULL);
299         ir_nodemap_destroy(&lv->block_infos);
300         xfree(lv);
301 }
302
303 /**
304  * Check a nodes liveness situation of a block.
305  * This routine considers both cases, the live in and end/out case.
306  *
307  * @param lv   The liveness check environment.
308  * @param bl   The block under investigation.
309  * @param var  The node to check for.
310  * @return     A bitmask of lv_chk_state_XXX fields.
311  */
312 unsigned lv_chk_bl_xxx(lv_chk_t *lv, const ir_node *bl, const ir_node *var)
313 {
314         int res  = 0;
315         ir_node *def_bl;
316         stat_ev_cnt_decl(uses);
317         stat_ev_cnt_decl(iter);
318
319         assert(is_Block(bl) && "can only check for liveness in a block");
320
321         /* If the variable ist no liveness related var, bail out. */
322         if (!is_liveness_node(var))
323                 return 0;
324
325         stat_ev_ctx_push_fmt("lv_chk", "%u", get_irn_idx(var));
326         stat_ev_tim_push();
327
328         /* If there is no dominance relation, go out, too */
329         def_bl = get_nodes_block(var);
330         if (!block_dominates(def_bl, bl)) {
331                 stat_ev("lv_chk_no_dom");
332                 goto end;
333         }
334
335         /*
336          * If the block in question is the same as the definition block,
337          * the algorithm is simple. Just check for uses not inside this block.
338          */
339         if (def_bl == bl) {
340                 const ir_edge_t *edge;
341
342                 stat_ev("lv_chk_def_block");
343                 DBG((lv->dbg, LEVEL_2, "lv check same block %+F in %+F\n", var, bl));
344                 foreach_out_edge (var, edge) {
345                         ir_node *use    = get_edge_src_irn(edge);
346                         ir_node *use_bl;
347
348                         if (!is_liveness_node(use))
349                                 continue;
350
351                         stat_ev_cnt_inc(uses);
352                         use_bl = get_nodes_block(use);
353                         if (is_Phi(use)) {
354                                 int pos = get_edge_src_pos(edge);
355                                 use_bl  = get_Block_cfgpred_block(use_bl, pos);
356
357                                 if (use_bl == bl) {
358                                         DBG((lv->dbg, LEVEL_2, "\tphi %+F in succ %+F,%d -> live end\n", use, use_bl, pos));
359                                         res |= lv_chk_state_end;
360                                 }
361                         }
362
363                         if (use_bl != def_bl) {
364                                 res = lv_chk_state_end | lv_chk_state_out;
365                                 goto end;
366                         }
367                 }
368
369                 goto end;
370         }
371
372         /*
373          * this is the more complicated case.
374          * We try to gather as much information as possible during looking
375          * at the uses.
376          *
377          * Note that we know for sure that bl != def_bl. That is sometimes
378          * silently exploited below.
379          */
380         else {
381                 bl_info_t *def = get_block_info(lv, def_bl);
382                 bl_info_t *bli = get_block_info(lv, bl);
383                 bitset_t *uses = bitset_alloca(lv->n_blocks);
384                 bitset_t *Tq;
385
386                 size_t i;
387                 unsigned min_dom, max_dom;
388                 const ir_edge_t *edge;
389
390                 /* if the block has no DFS info, it cannot be reached.
391                  * This can happen in functions with endless loops.
392                  * we then go out, since nothing is live there.
393                  *
394                  * TODO: Is that right?
395                  */
396                 if (!bli)
397                         goto end;
398
399                 (void) def;
400                 DBG((lv->dbg, LEVEL_2, "lv check %+F (def in %+F #%d) in different block %+F #%d\n",
401                                         var, def_bl, def->id, bl, bli->id));
402
403                 foreach_out_edge (var, edge) {
404                         ir_node *user = get_edge_src_irn(edge);
405                         int mask      = lv_chk_state_in;
406
407                         ir_node *use_bl;
408                         bl_info_t *bi;
409
410                         /* if the user is no liveness node, the use does not count */
411                         if (!is_liveness_node(user))
412                                 continue;
413
414                         stat_ev_cnt_inc(uses);
415
416                         /* if the user is a phi, the use is in the predecessor
417                          * furthermore, prepare a mask so that in the case where
418                          * bl (the block in question) coincides with a use, it
419                          * can be marked live_end there. */
420                         use_bl = get_nodes_block(user);
421                         if (is_Phi(user)) {
422                                 int pos = get_edge_src_pos(edge);
423                                 use_bl  = get_Block_cfgpred_block(use_bl, pos);
424                                 mask   |= lv_chk_state_end;
425                         }
426
427
428                         /* if the use block coincides with the query block, we
429                          * already gather a little liveness information.
430                          * The variable is surely live there, since bl != def_bl
431                          * (that case is treated above). */
432                         if (use_bl == bl)
433                                 res |= mask;
434
435                         bi = get_block_info(lv, use_bl);
436
437                         if (bi)
438                                 bitset_set(uses, bi->id);
439                 }
440
441                 /* get the dominance range which really matters. all uses outside
442                  * the definition's dominance range are not to consider. note,
443                  * that the definition itself is also not considered. The case
444                  * where bl == def_bl is considered above. */
445                 min_dom = get_Block_dom_tree_pre_num(def_bl) + 1;
446                 max_dom = get_Block_dom_max_subtree_pre_num(def_bl);
447
448                 DBG((lv->dbg, LEVEL_2, "\tuses: %B\n", uses));
449
450                 /* prepare a set with all reachable back edge targets.
451                  * this will determine our "looking points" from where
452                  * we will search/find the calculated uses. */
453                 Tq = bli->be_tgt_reach;
454
455                 /* now, visit all viewing points in the temporary bitset lying
456                  * in the dominance range of the variable. Note that for reducible
457                  * flow-graphs the first iteration is sufficient and the loop
458                  * will be left. */
459                 DBG((lv->dbg, LEVEL_2, "\tbe tgt reach: %B, dom span: [%d, %d]\n", Tq, min_dom, max_dom));
460                 i = bitset_next_set(Tq, min_dom);
461                 while (i <= max_dom) {
462                         bl_info_t *ti = lv->map[i];
463                         int use_in_current_block = bitset_is_set(uses, ti->id);
464
465                         stat_ev_cnt_inc(iter);
466
467                         /*
468                          * This is somewhat tricky. Since this routine handles both, live in
469                          * and end/out we have to handle all the border cases correctly.
470                          * Each node is in its own red_reachable set (see calculation
471                          * function above). That means, that in the case where bl == t, the
472                          * intersection check of uses and reachability below will always
473                          * find an intersection, namely t.
474                          *
475                          * However, if a block contains a use and the variable is dead
476                          * afterwards, it is not live end/out at that block. Besides
477                          * back-edge target. If a var is live-in at a back-edge target it
478                          * is also live out/end there since the variable is live in the
479                          * underlying loop. So in the case where t == bl and that is not
480                          * a back-edge target, we have to remove that use from consideration
481                          * to determine if the var is live out/end there.
482                          *
483                          * Note that the live in information has been calculated by the
484                          * uses iteration above.
485                          */
486                         if (ti == bli && !bitset_is_set(lv->back_edge_tgt, ti->id)) {
487                                 DBG((lv->dbg, LEVEL_2, "\tlooking not from a back edge target and q == t. removing use: %d\n", ti->id));
488                                 bitset_clear(uses, ti->id);
489                         }
490
491                         /* If we can reach a use, the variable is live there and we say goodbye */
492                         DBG((lv->dbg, LEVEL_2, "\tlooking from %d: seeing %B\n", ti->id, ti->red_reachable));
493                         if (bitset_intersect(ti->red_reachable, uses)) {
494                                 res |= lv_chk_state_in | lv_chk_state_out | lv_chk_state_end;
495                                 goto end;
496                         }
497
498                         /*
499                          * if we deleted a use do to the commentary above, we have to
500                          * re-add it since it might be visible from further view points
501                          * (we only need that in the non-reducible case).
502                          */
503                         if (use_in_current_block)
504                                 bitset_set(uses, ti->id);
505
506                         i = bitset_next_set(Tq, get_Block_dom_max_subtree_pre_num(ti->block) + 1);
507                 }
508
509         }
510
511 end:
512         stat_ev_tim_pop("lv_chk_query_time");
513         stat_ev_cnt_done(uses, "lv_chk_uses");
514         stat_ev_cnt_done(iter, "lv_chk_iter");
515         stat_ev_ctx_pop("lv_chk");
516
517         return res;
518 }