big refactoring of arch_XXX functions
[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 *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         for (i = 0; i < n; ++i) {
120                 ir_node *perm_op = get_irn_n(perm, i);
121                 const arch_register_t *reg = arch_get_irn_register(perm_op);
122                 be_ssa_construction_env_t senv;
123
124                 ir_mode *mode = get_irn_mode(perm_op);
125                 ir_node *proj = new_r_Proj(perm, mode, i);
126                 arch_set_irn_register(proj, reg);
127
128                 be_ssa_construction_init(&senv, irg);
129                 be_ssa_construction_add_copy(&senv, perm_op);
130                 be_ssa_construction_add_copy(&senv, proj);
131                 be_ssa_construction_fix_users(&senv, perm_op);
132                 be_ssa_construction_update_liveness_phis(&senv, lv);
133                 be_liveness_update(lv, perm_op);
134                 be_liveness_update(lv, proj);
135                 be_ssa_construction_destroy(&senv);
136         }
137
138         return perm;
139 }
140
141 static int blocks_removed;
142
143 /**
144  * Post-block-walker: Find blocks containing only one jump and
145  * remove them.
146  */
147 static void remove_empty_block(ir_node *block)
148 {
149         const ir_edge_t *edge, *next;
150         int      i, arity;
151         ir_node *node;
152         ir_node *pred;
153         ir_node *succ_block;
154         ir_node *jump = NULL;
155         ir_graph *irg = get_irn_irg(block);
156
157         if (irn_visited_else_mark(block))
158                 return;
159
160         if (get_Block_n_cfgpreds(block) != 1)
161                 goto check_preds;
162
163         sched_foreach(block, node) {
164                 if (! is_Jmp(node)
165                                 && !(arch_get_irn_flags(node) & arch_irn_flags_simple_jump))
166                         goto check_preds;
167                 if (jump != NULL) {
168                         /* we should never have 2 jumps in a block */
169                         panic("found 2 jumps in a block");
170                 }
171                 jump = node;
172         }
173
174         if (jump == NULL)
175                 goto check_preds;
176
177         pred       = get_Block_cfgpred(block, 0);
178         succ_block = NULL;
179         foreach_out_edge_safe(jump, edge, next) {
180                 int pos = get_edge_src_pos(edge);
181
182                 assert(succ_block == NULL);
183                 succ_block = get_edge_src_irn(edge);
184                 if (has_Block_entity(succ_block) && has_Block_entity(block)) {
185                         /*
186                          * Currently we can add only one label for a block.
187                          * Therefore we cannot combine them if  both block already have one.
188                          */
189                         goto check_preds;
190                 }
191
192                 set_irn_n(succ_block, pos, pred);
193         }
194
195         if (has_Block_entity(block)) {
196                 /* move the label to the successor block */
197                 ir_entity *entity = get_Block_entity(block);
198                 set_Block_entity(succ_block, entity);
199         }
200
201         /* there can be some non-scheduled Pin nodes left in the block, move them
202          * to the succ block (Pin) or pred block (Sync) */
203         foreach_out_edge_safe(block, edge, next) {
204                 node = get_edge_src_irn(edge);
205
206                 if (node == jump)
207                         continue;
208                 /* we simply kill Pins, because there are some strange interactions
209                  * between jump threading, which produce PhiMs with Pins, we simply
210                  * kill the pins here, everything is scheduled anyway */
211                 if (is_Pin(node)) {
212                         exchange(node, get_Pin_op(node));
213                         continue;
214                 }
215                 if (is_Sync(node)) {
216                         set_nodes_block(node, get_nodes_block(pred));
217                         continue;
218                 }
219                 if (is_End(node)) { /* End-keep, reroute it to the successor */
220                         int pos = get_edge_src_pos(edge);
221                         set_irn_n(node, pos, succ_block);
222                         continue;
223                 }
224                 panic("Unexpected node %+F in block %+F with empty schedule", node, block);
225         }
226
227         set_Block_cfgpred(block, 0, new_r_Bad(irg, mode_X));
228         kill_node(jump);
229         blocks_removed = 1;
230
231         /* check predecessor */
232         remove_empty_block(get_nodes_block(pred));
233         return;
234
235 check_preds:
236         arity = get_Block_n_cfgpreds(block);
237         for (i = 0; i < arity; ++i) {
238                 ir_node *pred = get_Block_cfgpred_block(block, i);
239                 remove_empty_block(pred);
240         }
241 }
242
243 /* removes basic blocks that just contain a jump instruction */
244 int be_remove_empty_blocks(ir_graph *irg)
245 {
246         ir_node *end;
247         int      i, arity;
248
249         blocks_removed = 0;
250
251         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
252         inc_irg_visited(irg);
253         remove_empty_block(get_irg_end_block(irg));
254         end   = get_irg_end(irg);
255         arity = get_irn_arity(end);
256         for (i = 0; i < arity; ++i) {
257                 ir_node *pred = get_irn_n(end, i);
258                 if (!is_Block(pred))
259                         continue;
260                 remove_empty_block(pred);
261         }
262         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
263
264         if (blocks_removed) {
265                 /* invalidate analysis info */
266                 set_irg_doms_inconsistent(irg);
267                 set_irg_extblk_inconsistent(irg);
268         }
269         return blocks_removed;
270 }
271
272 //---------------------------------------------------------------------------
273
274 typedef struct remove_dead_nodes_env_t_ {
275         bitset_t *reachable;
276         ir_graph *irg;
277         be_lv_t  *lv;
278 } remove_dead_nodes_env_t;
279
280 /**
281  * Post-walker: remember all visited nodes in a bitset.
282  */
283 static void mark_dead_nodes_walker(ir_node *node, void *data)
284 {
285         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
286         bitset_set(env->reachable, get_irn_idx(node));
287 }
288
289 /**
290  * Post-block-walker:
291  * Walk through the schedule of every block and remove all dead nodes from it.
292  */
293 static void remove_dead_nodes_walker(ir_node *block, void *data)
294 {
295         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
296         ir_node                 *node, *next;
297
298         for (node = sched_first(block); ! sched_is_end(node); node = next) {
299                 /* get next node now, as after calling sched_remove it will be invalid */
300                 next = sched_next(node);
301
302                 if (bitset_is_set(env->reachable, get_irn_idx(node)))
303                         continue;
304
305                 if (env->lv != NULL)
306                         be_liveness_remove(env->lv, node);
307                 sched_remove(node);
308
309                 /* kill projs */
310                 if (get_irn_mode(node) == mode_T) {
311                         const ir_edge_t *edge;
312                         const ir_edge_t *next_edge;
313                         foreach_out_edge_safe(node, edge, next_edge) {
314                                 ir_node *proj = get_edge_src_irn(edge);
315                                 if (!is_Proj(proj))
316                                         continue;
317                                 if (env->lv != NULL)
318                                         be_liveness_remove(env->lv, proj);
319                                 kill_node(proj);
320                         }
321                 }
322                 kill_node(node);
323         }
324 }
325
326 void be_remove_dead_nodes_from_schedule(ir_graph *irg)
327 {
328         remove_dead_nodes_env_t env;
329         env.reachable = bitset_alloca(get_irg_last_idx(irg));
330         env.lv        = be_get_irg_liveness(irg);
331         env.irg       = irg;
332
333         // mark all reachable nodes
334         irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
335
336         // walk schedule and remove non-marked nodes
337         irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
338 }
339
340 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod)
341 void be_init_irgmod(void)
342 {
343         FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");
344 }