more aggressive node-killing to avoid dangling edges
[libfirm] / ir / be / beirgmod.c
1 /*
2  * Copyright (C) 1995-2008 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
22  * @brief       Backend IRG modification routines.
23  * @author      Sebastian Hack, Daniel Grund, Matthias Braun, Christian Wuerdig
24  * @date        04.05.2005
25  * @version     $Id$
26  *
27  * This file contains the following IRG modifications for be routines:
28  * - insertion of Perm nodes
29  * - empty block elimination
30  * - a simple dead node elimination (set inputs of unreachable nodes to BAD)
31  */
32 #include "config.h"
33
34 #include <stdlib.h>
35
36 #include "hashptr.h"
37 #include "pdeq.h"
38 #include "pset.h"
39 #include "pmap.h"
40 #include "util.h"
41 #include "debug.h"
42 #include "error.h"
43 #include "xmalloc.h"
44
45 #include "irflag_t.h"
46 #include "ircons_t.h"
47 #include "irnode_t.h"
48 #include "ircons_t.h"
49 #include "irmode_t.h"
50 #include "irdom_t.h"
51 #include "iredges_t.h"
52 #include "irgraph_t.h"
53 #include "irgopt.h"
54 #include "irgmod.h"
55 #include "irprintf_t.h"
56 #include "irgwalk.h"
57
58 #include "be_t.h"
59 #include "bechordal_t.h"
60 #include "bearch.h"
61 #include "besched.h"
62 #include "belive_t.h"
63 #include "benode.h"
64 #include "beutil.h"
65 #include "beinsn_t.h"
66 #include "bessaconstr.h"
67 #include "beirg.h"
68 #include "beirgmod.h"
69 #include "bemodule.h"
70
71 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
72
73 /*
74   ___                     _     ____
75  |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___ _ __ _ __ ___
76   | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
77   | || | | \__ \  __/ |  | |_  |  __/  __/ |  | | | | | |
78  |___|_| |_|___/\___|_|   \__| |_|   \___|_|  |_| |_| |_|
79
80 */
81
82 ir_node *insert_Perm_after(ir_graph *irg, const arch_register_class_t *cls,
83                                                    ir_node *pos)
84 {
85         be_lv_t *lv     = be_get_irg_liveness(irg);
86         ir_node *bl     = is_Block(pos) ? pos : get_nodes_block(pos);
87         ir_nodeset_t          live;
88         ir_nodeset_iterator_t iter;
89
90         ir_node *curr, *irn, *perm, **nodes;
91         size_t i, n;
92
93         DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
94
95         ir_nodeset_init(&live);
96         be_liveness_nodes_live_at(lv, cls, pos, &live);
97
98         n = ir_nodeset_size(&live);
99         if (n == 0) {
100                 ir_nodeset_destroy(&live);
101                 return NULL;
102         }
103
104         nodes = XMALLOCN(ir_node*, n);
105
106         DBG((dbg, LEVEL_1, "live:\n"));
107         i = 0;
108         foreach_ir_nodeset(&live, irn, iter) {
109                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
110                 nodes[i] = irn;
111                 i++;
112         }
113         ir_nodeset_destroy(&live);
114
115         perm = be_new_Perm(cls, bl, n, nodes);
116         sched_add_after(pos, perm);
117         free(nodes);
118
119         curr = perm;
120         for (i = 0; i < n; ++i) {
121                 ir_node *perm_op = get_irn_n(perm, i);
122                 const arch_register_t *reg = arch_get_irn_register(perm_op);
123                 be_ssa_construction_env_t senv;
124
125                 ir_mode *mode = get_irn_mode(perm_op);
126                 ir_node *proj = new_r_Proj(perm, mode, i);
127                 arch_set_irn_register(proj, reg);
128
129                 curr = proj;
130
131                 be_ssa_construction_init(&senv, irg);
132                 be_ssa_construction_add_copy(&senv, perm_op);
133                 be_ssa_construction_add_copy(&senv, proj);
134                 be_ssa_construction_fix_users(&senv, perm_op);
135                 be_ssa_construction_update_liveness_phis(&senv, lv);
136                 be_liveness_update(lv, perm_op);
137                 be_liveness_update(lv, proj);
138                 be_ssa_construction_destroy(&senv);
139         }
140
141         return perm;
142 }
143
144 static int blocks_removed;
145
146 /**
147  * Post-block-walker: Find blocks containing only one jump and
148  * remove them.
149  */
150 static void remove_empty_block(ir_node *block)
151 {
152         const ir_edge_t *edge, *next;
153         int      i, arity;
154         ir_node *node;
155         ir_node *pred;
156         ir_node *succ_block;
157         ir_node *jump = NULL;
158         ir_graph *irg = get_irn_irg(block);
159
160         if (irn_visited_else_mark(block))
161                 return;
162
163         if (get_Block_n_cfgpreds(block) != 1)
164                 goto check_preds;
165
166         sched_foreach(block, node) {
167                 if (! is_Jmp(node)
168                                 && !(arch_irn_get_flags(node) & arch_irn_flags_simple_jump))
169                         goto check_preds;
170                 if (jump != NULL) {
171                         /* we should never have 2 jumps in a block */
172                         panic("found 2 jumps in a block");
173                 }
174                 jump = node;
175         }
176
177         if (jump == NULL)
178                 goto check_preds;
179
180         pred       = get_Block_cfgpred(block, 0);
181         succ_block = NULL;
182         foreach_out_edge_safe(jump, edge, next) {
183                 int pos = get_edge_src_pos(edge);
184
185                 assert(succ_block == NULL);
186                 succ_block = get_edge_src_irn(edge);
187                 if (has_Block_entity(succ_block) && has_Block_entity(block)) {
188                         /*
189                          * Currently we can add only one label for a block.
190                          * Therefore we cannot combine them if  both block already have one.
191                          */
192                         goto check_preds;
193                 }
194
195                 set_irn_n(succ_block, pos, pred);
196         }
197
198         if (has_Block_entity(block)) {
199                 /* move the label to the successor block */
200                 ir_entity *entity = get_Block_entity(block);
201                 set_Block_entity(succ_block, entity);
202         }
203
204         /* there can be some non-scheduled Pin nodes left in the block, move them
205          * to the succ block (Pin) or pred block (Sync) */
206         foreach_out_edge_safe(block, edge, next) {
207                 node = get_edge_src_irn(edge);
208
209                 if (node == jump)
210                         continue;
211                 /* we simply kill Pins, because there are some strange interactions
212                  * between jump threading, which produce PhiMs with Pins, we simply
213                  * kill the pins here, everything is scheduled anyway */
214                 if (is_Pin(node)) {
215                         exchange(node, get_Pin_op(node));
216                         continue;
217                 }
218                 if (is_Sync(node)) {
219                         set_nodes_block(node, get_nodes_block(pred));
220                         continue;
221                 }
222                 if (is_End(node)) { /* End-keep, reroute it to the successor */
223                         int pos = get_edge_src_pos(edge);
224                         set_irn_n(node, pos, succ_block);
225                         continue;
226                 }
227                 panic("Unexpected node %+F in block %+F with empty schedule", node, block);
228         }
229
230         set_Block_cfgpred(block, 0, new_r_Bad(irg));
231         kill_node(jump);
232         blocks_removed = 1;
233
234         /* check predecessor */
235         remove_empty_block(get_nodes_block(pred));
236         return;
237
238 check_preds:
239         arity = get_Block_n_cfgpreds(block);
240         for (i = 0; i < arity; ++i) {
241                 ir_node *pred = get_Block_cfgpred_block(block, i);
242                 remove_empty_block(pred);
243         }
244 }
245
246 /* removes basic blocks that just contain a jump instruction */
247 int be_remove_empty_blocks(ir_graph *irg)
248 {
249         ir_node *end;
250         int      i, arity;
251
252         blocks_removed = 0;
253
254         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
255         inc_irg_visited(irg);
256         remove_empty_block(get_irg_end_block(irg));
257         end   = get_irg_end(irg);
258         arity = get_irn_arity(end);
259         for (i = 0; i < arity; ++i) {
260                 ir_node *pred = get_irn_n(end, i);
261                 if (!is_Block(pred))
262                         continue;
263                 remove_empty_block(pred);
264         }
265         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
266
267         if (blocks_removed) {
268                 /* invalidate analysis info */
269                 set_irg_doms_inconsistent(irg);
270                 set_irg_extblk_inconsistent(irg);
271                 set_irg_outs_inconsistent(irg);
272                 set_irg_loopinfo_inconsistent(irg);
273         }
274         return blocks_removed;
275 }
276
277 //---------------------------------------------------------------------------
278
279 typedef struct remove_dead_nodes_env_t_ {
280         bitset_t *reachable;
281         ir_graph *irg;
282         be_lv_t  *lv;
283 } remove_dead_nodes_env_t;
284
285 /**
286  * Post-walker: remember all visited nodes in a bitset.
287  */
288 static void mark_dead_nodes_walker(ir_node *node, void *data)
289 {
290         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
291         bitset_set(env->reachable, get_irn_idx(node));
292 }
293
294 /**
295  * Post-block-walker:
296  * Walk through the schedule of every block and remove all dead nodes from it.
297  */
298 static void remove_dead_nodes_walker(ir_node *block, void *data)
299 {
300         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
301         ir_node                 *node, *next;
302
303         for (node = sched_first(block); ! sched_is_end(node); node = next) {
304                 /* get next node now, as after calling sched_remove it will be invalid */
305                 next = sched_next(node);
306
307                 if (bitset_is_set(env->reachable, get_irn_idx(node)))
308                         continue;
309
310                 if (env->lv != NULL)
311                         be_liveness_remove(env->lv, node);
312                 sched_remove(node);
313
314                 /* kill projs */
315                 if (get_irn_mode(node) == mode_T) {
316                         const ir_edge_t *edge;
317                         const ir_edge_t *next_edge;
318                         foreach_out_edge_safe(node, edge, next_edge) {
319                                 ir_node *proj = get_edge_src_irn(edge);
320                                 if (!is_Proj(proj))
321                                         continue;
322                                 if (env->lv != NULL)
323                                         be_liveness_remove(env->lv, proj);
324                                 kill_node(proj);
325                         }
326                 }
327                 kill_node(node);
328         }
329 }
330
331 void be_remove_dead_nodes_from_schedule(ir_graph *irg)
332 {
333         remove_dead_nodes_env_t env;
334         env.reachable = bitset_alloca(get_irg_last_idx(irg));
335         env.lv        = be_get_irg_liveness(irg);
336         env.irg       = irg;
337
338         // mark all reachable nodes
339         irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
340
341         // walk schedule and remove non-marked nodes
342         irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
343 }
344
345 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);
346 void be_init_irgmod(void)
347 {
348         FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");
349 }