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"
66 #include "bessaconstr.h"
71 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
75 |_ _|_ __ ___ ___ _ __| |_ | _ \ ___ _ __ _ __ ___
76 | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
77 | || | | \__ \ __/ | | |_ | __/ __/ | | | | | | |
78 |___|_| |_|___/\___|_| \__| |_| \___|_| |_| |_| |_|
82 ir_node *insert_Perm_after(ir_graph *irg, const arch_register_class_t *cls,
85 be_lv_t *lv = be_get_irg_liveness(irg);
86 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
88 ir_nodeset_iterator_t iter;
90 ir_node *curr, *irn, *perm, **nodes;
93 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
95 ir_nodeset_init(&live);
96 be_liveness_nodes_live_at(lv, cls, pos, &live);
98 n = ir_nodeset_size(&live);
100 ir_nodeset_destroy(&live);
104 nodes = XMALLOCN(ir_node*, n);
106 DBG((dbg, LEVEL_1, "live:\n"));
108 foreach_ir_nodeset(&live, irn, iter) {
109 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
113 ir_nodeset_destroy(&live);
115 perm = be_new_Perm(cls, bl, n, nodes);
116 sched_add_after(pos, 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;
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);
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);
144 static int blocks_removed;
147 * Post-block-walker: Find blocks containing only one jump and
150 static void remove_empty_block(ir_node *block)
152 const ir_edge_t *edge, *next;
157 ir_node *jump = NULL;
159 if (irn_visited_else_mark(block))
162 if (get_Block_n_cfgpreds(block) != 1)
165 sched_foreach(block, node) {
167 && !(arch_irn_get_flags(node) & arch_irn_flags_simple_jump))
170 /* we should never have 2 jumps in a block */
171 panic("found 2 jumps in a block");
179 pred = get_Block_cfgpred(block, 0);
181 foreach_out_edge_safe(jump, edge, next) {
182 int pos = get_edge_src_pos(edge);
184 assert(succ_block == NULL);
185 succ_block = get_edge_src_irn(edge);
186 if (has_Block_entity(succ_block) && has_Block_entity(block)) {
188 * Currently we can add only one label for a block.
189 * Therefore we cannot combine them if both block already have one.
194 set_irn_n(succ_block, pos, pred);
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);
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);
210 /* we simply kill Pins, because there are some strange interactions
211 * between jump threading, which produce PhiMs with Pins, we simply
212 * kill the pins here, everything is scheduled anyway */
214 exchange(node, get_Pin_op(node));
218 set_nodes_block(node, get_nodes_block(pred));
221 if (is_End(node)) { /* End-keep, reroute it to the successor */
222 int pos = get_edge_src_pos(edge);
223 set_irn_n(node, pos, succ_block);
226 panic("Unexpected node %+F in block %+F with empty schedule", node, block);
229 set_Block_cfgpred(block, 0, new_Bad());
233 /* check predecessor */
234 remove_empty_block(get_nodes_block(pred));
238 arity = get_Block_n_cfgpreds(block);
239 for (i = 0; i < arity; ++i) {
240 ir_node *pred = get_Block_cfgpred_block(block, i);
241 remove_empty_block(pred);
245 /* removes basic blocks that just contain a jump instruction */
246 int be_remove_empty_blocks(ir_graph *irg)
253 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
254 inc_irg_visited(irg);
255 remove_empty_block(get_irg_end_block(irg));
256 end = get_irg_end(irg);
257 arity = get_irn_arity(end);
258 for (i = 0; i < arity; ++i) {
259 ir_node *pred = get_irn_n(end, i);
262 remove_empty_block(pred);
264 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
266 if (blocks_removed) {
267 /* invalidate analysis info */
268 set_irg_doms_inconsistent(irg);
269 set_irg_extblk_inconsistent(irg);
270 set_irg_outs_inconsistent(irg);
271 set_irg_loopinfo_inconsistent(irg);
273 return blocks_removed;
276 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);
277 void be_init_irgmod(void)
279 FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");