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)
51 #include "iredges_t.h"
52 #include "irgraph_t.h"
55 #include "irprintf_t.h"
59 #include "bechordal_t.h"
61 #include "besched_t.h"
66 #include "bessaconstr.h"
71 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
75 |_ _|_ __ ___ ___ _ __| |_ | _ \ ___ _ __ _ __ ___
76 | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
77 | || | | \__ \ __/ | | |_ | __/ __/ | | | | | | |
78 |___|_| |_|___/\___|_| \__| |_| \___|_| |_| |_| |_|
82 ir_node *insert_Perm_after(be_irg_t *birg,
83 const arch_register_class_t *cls,
86 be_lv_t *lv = birg->lv;
87 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
88 ir_graph *irg = get_irn_irg(bl);
90 ir_nodeset_iterator_t iter;
92 ir_node *curr, *irn, *perm, **nodes;
95 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
97 ir_nodeset_init(&live);
98 be_liveness_nodes_live_at(lv, cls, pos, &live);
100 n = ir_nodeset_size(&live);
102 ir_nodeset_destroy(&live);
106 nodes = XMALLOCN(ir_node*, n);
108 DBG((dbg, LEVEL_1, "live:\n"));
110 foreach_ir_nodeset(&live, irn, iter) {
111 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
115 ir_nodeset_destroy(&live);
117 perm = be_new_Perm(cls, irg, bl, n, nodes);
118 sched_add_after(pos, perm);
122 for (i = 0; i < n; ++i) {
123 ir_node *perm_op = get_irn_n(perm, i);
124 const arch_register_t *reg = arch_get_irn_register(perm_op);
125 be_ssa_construction_env_t senv;
127 ir_mode *mode = get_irn_mode(perm_op);
128 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
129 arch_set_irn_register(proj, reg);
133 be_ssa_construction_init(&senv, birg);
134 be_ssa_construction_add_copy(&senv, perm_op);
135 be_ssa_construction_add_copy(&senv, proj);
136 be_ssa_construction_fix_users(&senv, perm_op);
137 be_ssa_construction_update_liveness_phis(&senv, lv);
138 be_liveness_update(lv, perm_op);
139 be_liveness_update(lv, proj);
140 be_ssa_construction_destroy(&senv);
146 static int blocks_removed;
149 * Post-block-walker: Find blocks containing only one jump and
152 static void remove_empty_block(ir_node *block)
154 const ir_edge_t *edge, *next;
159 ir_node *jump = NULL;
161 if (irn_visited_else_mark(block))
164 if (get_Block_n_cfgpreds(block) != 1)
167 sched_foreach(block, node) {
171 /* we should never have 2 jumps in a block */
172 panic("found 2 jumps in a block");
180 pred = get_Block_cfgpred(block, 0);
182 foreach_out_edge_safe(jump, edge, next) {
183 int pos = get_edge_src_pos(edge);
185 assert(succ_block == NULL);
186 succ_block = get_edge_src_irn(edge);
188 set_irn_n(succ_block, pos, pred);
191 /* there can be some non-scheduled Pin nodes left in the block, move them
192 * to the succ block (Pin) or pred block (Sync) */
193 foreach_out_edge_safe(block, edge, next) {
194 node = get_edge_src_irn(edge);
198 if (is_Block(node)) {
199 /* a Block->Block edge: This should be the MacroBlock
201 assert(get_Block_MacroBlock(node) == block && "Wrong Block->Block edge");
205 set_nodes_block(node, succ_block);
209 set_nodes_block(node, get_nodes_block(pred));
212 if (is_End(node)) { /* End-keep, reroute it to the successor */
213 int pos = get_edge_src_pos(edge);
214 set_irn_n(node, pos, succ_block);
217 panic("Unexpected node %+F in block %+F with empty schedule", node, block);
220 set_Block_cfgpred(block, 0, new_Bad());
224 /* check predecessor */
225 remove_empty_block(get_nodes_block(pred));
229 arity = get_Block_n_cfgpreds(block);
230 for(i = 0; i < arity; ++i) {
231 ir_node *pred = get_Block_cfgpred_block(block, i);
232 remove_empty_block(pred);
236 /* removes basic blocks that just contain a jump instruction */
237 int be_remove_empty_blocks(ir_graph *irg)
244 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
245 inc_irg_visited(irg);
246 remove_empty_block(get_irg_end_block(irg));
247 end = get_irg_end(irg);
248 arity = get_irn_arity(end);
249 for(i = 0; i < arity; ++i) {
250 ir_node *pred = get_irn_n(end, i);
253 remove_empty_block(pred);
255 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
257 if (blocks_removed) {
258 /* invalidate analysis info */
259 set_irg_doms_inconsistent(irg);
260 set_irg_extblk_inconsistent(irg);
261 set_irg_outs_inconsistent(irg);
262 set_irg_loopinfo_inconsistent(irg);
264 return blocks_removed;
267 void be_init_irgmod(void)
269 FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");
272 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);