Do not mark the transformed as visited. It makes no sense at all.
[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 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <stdlib.h>
37
38 #include "hashptr.h"
39 #include "pdeq.h"
40 #include "pset.h"
41 #include "pmap.h"
42 #include "util.h"
43 #include "debug.h"
44 #include "error.h"
45 #include "xmalloc.h"
46
47 #include "irflag_t.h"
48 #include "ircons_t.h"
49 #include "irnode_t.h"
50 #include "ircons_t.h"
51 #include "irmode_t.h"
52 #include "irdom_t.h"
53 #include "iredges_t.h"
54 #include "irgraph_t.h"
55 #include "irgopt.h"
56 #include "irgmod.h"
57 #include "irprintf_t.h"
58 #include "irgwalk.h"
59
60 #include "be_t.h"
61 #include "bechordal_t.h"
62 #include "bearch_t.h"
63 #include "besched_t.h"
64 #include "belive_t.h"
65 #include "benode_t.h"
66 #include "beutil.h"
67 #include "beinsn_t.h"
68 #include "bessaconstr.h"
69 #include "beirg_t.h"
70 #include "beirgmod.h"
71 #include "bemodule.h"
72
73 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
74
75 /*
76   ___                     _     ____
77  |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___ _ __ _ __ ___
78   | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
79   | || | | \__ \  __/ |  | |_  |  __/  __/ |  | | | | | |
80  |___|_| |_|___/\___|_|   \__| |_|   \___|_|  |_| |_| |_|
81
82 */
83
84 ir_node *insert_Perm_after(be_irg_t *birg,
85                                                    const arch_register_class_t *cls,
86                                                    ir_node *pos)
87 {
88         const arch_env_t *arch_env = birg->main_env->arch_env;
89         be_lv_t *lv     = birg->lv;
90         ir_node *bl     = is_Block(pos) ? pos : get_nodes_block(pos);
91         ir_graph *irg   = get_irn_irg(bl);
92         ir_nodeset_t          live;
93         ir_nodeset_iterator_t iter;
94
95         ir_node *curr, *irn, *perm, **nodes;
96         size_t i, n;
97
98         DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
99
100         ir_nodeset_init(&live);
101         be_liveness_nodes_live_at(lv, arch_env, cls, pos, &live);
102
103         n = ir_nodeset_size(&live);
104         if(n == 0) {
105                 ir_nodeset_destroy(&live);
106                 return NULL;
107         }
108
109         nodes = XMALLOCN(ir_node*, n);
110
111         DBG((dbg, LEVEL_1, "live:\n"));
112         i = 0;
113         foreach_ir_nodeset(&live, irn, iter) {
114                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
115                 nodes[i] = irn;
116                 i++;
117         }
118         ir_nodeset_destroy(&live);
119
120         perm = be_new_Perm(cls, irg, bl, n, nodes);
121         sched_add_after(pos, perm);
122         free(nodes);
123
124         curr = perm;
125         for (i = 0; i < n; ++i) {
126                 ir_node *perm_op = get_irn_n(perm, i);
127                 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
128                 be_ssa_construction_env_t senv;
129
130                 ir_mode *mode = get_irn_mode(perm_op);
131                 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
132                 arch_set_irn_register(arch_env, proj, reg);
133
134                 curr = proj;
135
136                 be_ssa_construction_init(&senv, birg);
137                 be_ssa_construction_add_copy(&senv, perm_op);
138                 be_ssa_construction_add_copy(&senv, proj);
139                 be_ssa_construction_fix_users(&senv, perm_op);
140                 be_ssa_construction_update_liveness_phis(&senv, lv);
141                 be_liveness_update(lv, perm_op);
142                 be_liveness_update(lv, proj);
143                 be_ssa_construction_destroy(&senv);
144         }
145
146         return perm;
147 }
148
149 static int blocks_removed;
150
151 /**
152  * Post-block-walker: Find blocks containing only one jump and
153  * remove them.
154  */
155 static void remove_empty_block(ir_node *block)
156 {
157         const ir_edge_t *edge, *next;
158         int      i, arity;
159         ir_node *node;
160         ir_node *pred;
161         ir_node *succ_block;
162         ir_node *jump = NULL;
163
164         if (irn_visited_else_mark(block))
165                 return;
166
167         if (get_Block_n_cfgpreds(block) != 1)
168                 goto check_preds;
169
170         sched_foreach(block, node) {
171                 if (! is_Jmp(node))
172                         goto check_preds;
173                 if (jump != NULL) {
174                         /* we should never have 2 jumps in a block */
175                         panic("found 2 jumps in a block");
176                 }
177                 jump = node;
178         }
179
180         if (jump == NULL)
181                 goto check_preds;
182
183         pred       = get_Block_cfgpred(block, 0);
184         succ_block = NULL;
185         foreach_out_edge_safe(jump, edge, next) {
186                 int pos = get_edge_src_pos(edge);
187
188                 assert(succ_block == NULL);
189                 succ_block = get_edge_src_irn(edge);
190
191                 set_irn_n(succ_block, pos, pred);
192         }
193
194         /* there can be some non-scheduled Pin nodes left in the block, move them
195          * to the succ block (Pin) or pred block (Sync) */
196         foreach_out_edge_safe(block, edge, next) {
197                 node = get_edge_src_irn(edge);
198
199                 if(node == jump)
200                         continue;
201                 if (is_Block(node)) {
202                         /* a Block->Block edge: This should be the MacroBlock
203                            edge, ignore it. */
204                         assert(get_Block_MacroBlock(node) == block && "Wrong Block->Block edge");
205                         continue;
206                 }
207                 if (is_Pin(node)) {
208                         set_nodes_block(node, succ_block);
209                         continue;
210                 }
211                 if (is_Sync(node)) {
212                         set_nodes_block(node, get_nodes_block(pred));
213                         continue;
214                 }
215                 if (is_End(node)) { /* End-keep, reroute it to the successor */
216                         int pos = get_edge_src_pos(edge);
217                         set_irn_n(node, pos, succ_block);
218                         continue;
219                 }
220                 panic("Unexpected node %+F in block %+F with empty schedule", node, block);
221         }
222
223         set_Block_cfgpred(block, 0, new_Bad());
224         kill_node(jump);
225         blocks_removed = 1;
226
227         /* check predecessor */
228         remove_empty_block(get_nodes_block(pred));
229         return;
230
231 check_preds:
232         arity = get_Block_n_cfgpreds(block);
233         for(i = 0; i < arity; ++i) {
234                 ir_node *pred = get_Block_cfgpred_block(block, i);
235                 remove_empty_block(pred);
236         }
237 }
238
239 /* removes basic blocks that just contain a jump instruction */
240 int be_remove_empty_blocks(ir_graph *irg)
241 {
242         ir_node *end;
243         int      i, arity;
244
245         blocks_removed = 0;
246
247         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
248         inc_irg_visited(irg);
249         remove_empty_block(get_irg_end_block(irg));
250         end   = get_irg_end(irg);
251         arity = get_irn_arity(end);
252         for(i = 0; i < arity; ++i) {
253                 ir_node *pred = get_irn_n(end, i);
254                 if(!is_Block(pred))
255                         continue;
256                 remove_empty_block(pred);
257         }
258         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
259
260         if (blocks_removed) {
261                 /* invalidate analysis info */
262                 set_irg_doms_inconsistent(irg);
263                 set_irg_extblk_inconsistent(irg);
264                 set_irg_outs_inconsistent(irg);
265                 set_irg_loopinfo_inconsistent(irg);
266         }
267         return blocks_removed;
268 }
269
270 void be_init_irgmod(void)
271 {
272         FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");
273 }
274
275 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);