2 * Copyright (C) 1995-2007 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"
56 #include "irprintf_t.h"
60 #include "bechordal_t.h"
62 #include "besched_t.h"
67 #include "bessaconstr.h"
72 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
76 |_ _|_ __ ___ ___ _ __| |_ | _ \ ___ _ __ _ __ ___
77 | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
78 | || | | \__ \ __/ | | |_ | __/ __/ | | | | | | |
79 |___|_| |_|___/\___|_| \__| |_| \___|_| |_| |_| |_|
83 ir_node *insert_Perm_after(be_irg_t *birg,
84 const arch_register_class_t *cls,
87 const arch_env_t *arch_env = birg->main_env->arch_env;
88 be_lv_t *lv = birg->lv;
89 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
90 ir_graph *irg = get_irn_irg(bl);
92 ir_nodeset_iterator_t iter;
94 ir_node *curr, *irn, *perm, **nodes;
97 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
99 ir_nodeset_init(&live);
100 be_liveness_nodes_live_at(lv, arch_env, cls, pos, &live);
102 n = ir_nodeset_size(&live);
104 ir_nodeset_destroy(&live);
108 nodes = xmalloc(n * sizeof(nodes[0]));
110 DBG((dbg, LEVEL_1, "live:\n"));
112 foreach_ir_nodeset(&live, irn, iter) {
113 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
117 ir_nodeset_destroy(&live);
119 perm = be_new_Perm(cls, irg, bl, n, nodes);
120 sched_add_after(pos, perm);
124 for (i = 0; i < n; ++i) {
125 ir_node *perm_op = get_irn_n(perm, i);
126 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
127 be_ssa_construction_env_t senv;
129 ir_mode *mode = get_irn_mode(perm_op);
130 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
131 arch_set_irn_register(arch_env, proj, reg);
135 be_ssa_construction_init(&senv, birg);
136 be_ssa_construction_add_copy(&senv, perm_op);
137 be_ssa_construction_add_copy(&senv, proj);
138 be_ssa_construction_fix_users(&senv, perm_op);
139 be_ssa_construction_update_liveness_phis(&senv, lv);
140 be_liveness_update(lv, perm_op);
141 be_liveness_update(lv, proj);
142 be_ssa_construction_destroy(&senv);
149 * Post-block-walker: Find blocks containing only one jump and
152 static void remove_empty_block(ir_node *block, void *data) {
153 const ir_edge_t *edge, *next;
158 ir_node *jump = NULL;
160 assert(is_Block(block));
162 if (get_Block_n_cfgpreds(block) != 1)
165 sched_foreach(block, node) {
169 /* we should never have 2 jumps in a block */
170 panic("found 2 jumps in a block");
178 pred_block = get_Block_cfgpred(block, 0);
180 foreach_out_edge_safe(jump, edge, next) {
181 int pos = get_edge_src_pos(edge);
183 assert(succ_block == NULL);
184 succ_block = get_edge_src_irn(edge);
186 set_irn_n(succ_block, pos, pred_block);
189 /* there can be some non-scheduled Pin nodes left in the block, move them
190 * to the succ block */
191 foreach_out_edge_safe(block, edge, next) {
192 node = get_edge_src_irn(edge);
197 set_nodes_block(node, succ_block);
200 panic("Unexpected node %+F in block %+F with empty schedule", node,
204 set_Block_cfgpred(block, 0, new_Bad());
209 /* removes basic blocks that just contain a jump instruction */
210 int be_remove_empty_blocks(ir_graph *irg) {
213 irg_block_walk_graph(irg, remove_empty_block, NULL, &changed);
215 /* invalidate analysis info */
216 set_irg_doms_inconsistent(irg);
217 set_irg_extblk_inconsistent(irg);
218 set_irg_outs_inconsistent(irg);
223 void be_init_irgmod(void)
225 FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");
228 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);