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