2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Backend IRG modification routines.
23 * @author Sebastian Hack, Daniel Grund, Matthias Braun, Christian Wuerdig
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)
53 #include "iredges_t.h"
54 #include "irgraph_t.h"
57 #include "irprintf_t.h"
61 #include "bechordal_t.h"
63 #include "besched_t.h"
68 #include "bessaconstr.h"
73 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
77 |_ _|_ __ ___ ___ _ __| |_ | _ \ ___ _ __ _ __ ___
78 | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
79 | || | | \__ \ __/ | | |_ | __/ __/ | | | | | | |
80 |___|_| |_|___/\___|_| \__| |_| \___|_| |_| |_| |_|
84 ir_node *insert_Perm_after(be_irg_t *birg,
85 const arch_register_class_t *cls,
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);
93 ir_nodeset_iterator_t iter;
95 ir_node *curr, *irn, *perm, **nodes;
98 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
100 ir_nodeset_init(&live);
101 be_liveness_nodes_live_at(lv, arch_env, cls, pos, &live);
103 n = ir_nodeset_size(&live);
105 ir_nodeset_destroy(&live);
109 nodes = xmalloc(n * sizeof(nodes[0]));
111 DBG((dbg, LEVEL_1, "live:\n"));
113 foreach_ir_nodeset(&live, irn, iter) {
114 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
118 ir_nodeset_destroy(&live);
120 perm = be_new_Perm(cls, irg, bl, n, nodes);
121 sched_add_after(pos, 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;
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);
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);
149 static int blocks_removed;
152 * Post-block-walker: Find blocks containing only one jump and
155 static void remove_empty_block(ir_node *block)
157 const ir_edge_t *edge, *next;
162 ir_node *jump = NULL;
164 if (irn_visited(block))
167 mark_irn_visited(block);
168 if (get_Block_n_cfgpreds(block) != 1)
171 sched_foreach(block, node) {
175 /* we should never have 2 jumps in a block */
176 panic("found 2 jumps in a block");
184 pred = get_Block_cfgpred(block, 0);
186 foreach_out_edge_safe(jump, edge, next) {
187 int pos = get_edge_src_pos(edge);
189 assert(succ_block == NULL);
190 succ_block = get_edge_src_irn(edge);
192 set_irn_n(succ_block, pos, pred);
195 /* there can be some non-scheduled Pin nodes left in the block, move them
196 * to the succ block */
197 foreach_out_edge_safe(block, edge, next) {
198 node = get_edge_src_irn(edge);
202 if (is_Block(node)) {
203 /* a Block->Block edge: This should be the MacroBlock
205 assert(get_Block_MacroBlock(node) == block && "Wrong Block->Block edge");
209 set_nodes_block(node, succ_block);
213 set_nodes_block(node, get_nodes_block(pred));
216 panic("Unexpected node %+F in block %+F with empty schedule", node, block);
219 set_Block_cfgpred(block, 0, new_Bad());
223 /* check predecessor */
224 remove_empty_block(get_nodes_block(pred));
228 arity = get_Block_n_cfgpreds(block);
229 for(i = 0; i < arity; ++i) {
230 ir_node *pred = get_Block_cfgpred_block(block, i);
231 remove_empty_block(pred);
235 /* removes basic blocks that just contain a jump instruction */
236 int be_remove_empty_blocks(ir_graph *irg)
243 set_using_irn_visited(irg);
244 inc_irg_visited(irg);
245 remove_empty_block(get_irg_end_block(irg));
246 end = get_irg_end(irg);
247 arity = get_irn_arity(end);
248 for(i = 0; i < arity; ++i) {
249 ir_node *pred = get_irn_n(end, i);
252 remove_empty_block(pred);
254 clear_using_irn_visited(irg);
256 if (blocks_removed) {
257 /* invalidate analysis info */
258 set_irg_doms_inconsistent(irg);
259 set_irg_extblk_inconsistent(irg);
260 set_irg_outs_inconsistent(irg);
261 set_irg_loopinfo_inconsistent(irg);
263 return blocks_removed;
266 void be_init_irgmod(void)
268 FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");
271 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);