change register allocator and related interfaces to use ir_graph* instead of be_irg_t*
[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
159         if (irn_visited_else_mark(block))
160                 return;
161
162         if (get_Block_n_cfgpreds(block) != 1)
163                 goto check_preds;
164
165         sched_foreach(block, node) {
166                 if (! is_Jmp(node)
167                                 && !(arch_irn_get_flags(node) & arch_irn_flags_simple_jump))
168                         goto check_preds;
169                 if (jump != NULL) {
170                         /* we should never have 2 jumps in a block */
171                         panic("found 2 jumps in a block");
172                 }
173                 jump = node;
174         }
175
176         if (jump == NULL)
177                 goto check_preds;
178
179         pred       = get_Block_cfgpred(block, 0);
180         succ_block = NULL;
181         foreach_out_edge_safe(jump, edge, next) {
182                 int pos = get_edge_src_pos(edge);
183
184                 assert(succ_block == NULL);
185                 succ_block = get_edge_src_irn(edge);
186                 if (has_Block_entity(succ_block) && has_Block_entity(block)) {
187                         /*
188                          * Currently we can add only one label for a block.
189                          * Therefore we cannot combine them if  both block already have one.
190                          */
191                         goto check_preds;
192                 }
193
194                 set_irn_n(succ_block, pos, pred);
195         }
196
197         if (has_Block_entity(block)) {
198                 /* move the label to the successor block */
199                 ir_entity *entity = get_Block_entity(block);
200                 set_Block_entity(succ_block, entity);
201         }
202
203         /* there can be some non-scheduled Pin nodes left in the block, move them
204          * to the succ block (Pin) or pred block (Sync) */
205         foreach_out_edge_safe(block, edge, next) {
206                 node = get_edge_src_irn(edge);
207
208                 if (node == jump)
209                         continue;
210                 if (is_Block(node)) {
211                         /* a Block->Block edge: This should be the MacroBlock
212                            edge, ignore it. */
213                         assert(get_Block_MacroBlock(node) == block && "Wrong Block->Block edge");
214                         continue;
215                 }
216                 /* we simply kill Pins, because there are some strange interactions
217                  * between jump threading, which produce PhiMs with Pins, we simply
218                  * kill the pins here, everything is scheduled anyway */
219                 if (is_Pin(node)) {
220                         exchange(node, get_Pin_op(node));
221                         continue;
222                 }
223                 if (is_Sync(node)) {
224                         set_nodes_block(node, get_nodes_block(pred));
225                         continue;
226                 }
227                 if (is_End(node)) { /* End-keep, reroute it to the successor */
228                         int pos = get_edge_src_pos(edge);
229                         set_irn_n(node, pos, succ_block);
230                         continue;
231                 }
232                 panic("Unexpected node %+F in block %+F with empty schedule", node, block);
233         }
234
235         set_Block_cfgpred(block, 0, new_Bad());
236         kill_node(jump);
237         blocks_removed = 1;
238
239         /* check predecessor */
240         remove_empty_block(get_nodes_block(pred));
241         return;
242
243 check_preds:
244         arity = get_Block_n_cfgpreds(block);
245         for (i = 0; i < arity; ++i) {
246                 ir_node *pred = get_Block_cfgpred_block(block, i);
247                 remove_empty_block(pred);
248         }
249 }
250
251 /* removes basic blocks that just contain a jump instruction */
252 int be_remove_empty_blocks(ir_graph *irg)
253 {
254         ir_node *end;
255         int      i, arity;
256
257         blocks_removed = 0;
258
259         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
260         inc_irg_visited(irg);
261         remove_empty_block(get_irg_end_block(irg));
262         end   = get_irg_end(irg);
263         arity = get_irn_arity(end);
264         for (i = 0; i < arity; ++i) {
265                 ir_node *pred = get_irn_n(end, i);
266                 if (!is_Block(pred))
267                         continue;
268                 remove_empty_block(pred);
269         }
270         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
271
272         if (blocks_removed) {
273                 /* invalidate analysis info */
274                 set_irg_doms_inconsistent(irg);
275                 set_irg_extblk_inconsistent(irg);
276                 set_irg_outs_inconsistent(irg);
277                 set_irg_loopinfo_inconsistent(irg);
278         }
279         return blocks_removed;
280 }
281
282 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);
283 void be_init_irgmod(void)
284 {
285         FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");
286 }