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
26 * This file contains the following IRG modifications for be routines:
27 * - insertion of Perm nodes
28 * - empty block elimination
29 * - a simple dead node elimination (set inputs of unreachable nodes to BAD)
50 #include "iredges_t.h"
51 #include "irgraph_t.h"
54 #include "irprintf_t.h"
58 #include "bechordal_t.h"
65 #include "bessaconstr.h"
70 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
74 |_ _|_ __ ___ ___ _ __| |_ | _ \ ___ _ __ _ __ ___
75 | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
76 | || | | \__ \ __/ | | |_ | __/ __/ | | | | | | |
77 |___|_| |_|___/\___|_| \__| |_| \___|_| |_| |_| |_|
81 ir_node *insert_Perm_after(ir_graph *irg, const arch_register_class_t *cls,
84 be_lv_t *lv = be_get_irg_liveness(irg);
85 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
87 ir_nodeset_iterator_t iter;
89 ir_node *irn, *perm, **nodes;
92 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
94 ir_nodeset_init(&live);
95 be_liveness_nodes_live_at(lv, cls, pos, &live);
97 n = ir_nodeset_size(&live);
99 ir_nodeset_destroy(&live);
103 nodes = XMALLOCN(ir_node*, n);
105 DBG((dbg, LEVEL_1, "live:\n"));
107 foreach_ir_nodeset(&live, irn, iter) {
108 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
112 ir_nodeset_destroy(&live);
114 perm = be_new_Perm(cls, bl, n, nodes);
115 sched_add_after(pos, perm);
118 for (i = 0; i < n; ++i) {
119 ir_node *perm_op = get_irn_n(perm, i);
120 const arch_register_t *reg = arch_get_irn_register(perm_op);
121 be_ssa_construction_env_t senv;
123 ir_mode *mode = get_irn_mode(perm_op);
124 ir_node *proj = new_r_Proj(perm, mode, i);
125 arch_set_irn_register(proj, reg);
127 be_ssa_construction_init(&senv, irg);
128 be_ssa_construction_add_copy(&senv, perm_op);
129 be_ssa_construction_add_copy(&senv, proj);
130 be_ssa_construction_fix_users(&senv, perm_op);
131 be_ssa_construction_update_liveness_phis(&senv, lv);
132 be_liveness_update(lv, perm_op);
133 be_liveness_update(lv, proj);
134 be_ssa_construction_destroy(&senv);
140 static int blocks_removed;
143 * Post-block-walker: Find blocks containing only one jump and
146 static void remove_empty_block(ir_node *block)
148 const ir_edge_t *edge;
149 const ir_edge_t *next;
155 ir_node *jump = NULL;
156 ir_graph *irg = get_irn_irg(block);
159 if (irn_visited_else_mark(block))
162 if (get_Block_n_cfgpreds(block) != 1)
165 sched_foreach(block, node) {
167 && !(arch_get_irn_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 entity = get_Block_entity(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);
187 if (get_Block_entity(succ_block) != NULL && entity != NULL) {
189 * Currently we can add only one label for a block.
190 * Therefore we cannot combine them if both block already have one.
195 set_irn_n(succ_block, pos, pred);
198 if (entity != NULL) {
199 /* move the label to the successor 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_r_Bad(irg, mode_X));
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 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
270 return blocks_removed;
273 //---------------------------------------------------------------------------
275 typedef struct remove_dead_nodes_env_t_ {
279 } remove_dead_nodes_env_t;
282 * Post-walker: remember all visited nodes in a bitset.
284 static void mark_dead_nodes_walker(ir_node *node, void *data)
286 remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
287 bitset_set(env->reachable, get_irn_idx(node));
292 * Walk through the schedule of every block and remove all dead nodes from it.
294 static void remove_dead_nodes_walker(ir_node *block, void *data)
296 remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
297 ir_node *node, *next;
299 for (node = sched_first(block); ! sched_is_end(node); node = next) {
300 /* get next node now, as after calling sched_remove it will be invalid */
301 next = sched_next(node);
303 if (bitset_is_set(env->reachable, get_irn_idx(node)))
307 be_liveness_remove(env->lv, node);
311 if (get_irn_mode(node) == mode_T) {
312 const ir_edge_t *edge;
313 const ir_edge_t *next_edge;
314 foreach_out_edge_safe(node, edge, next_edge) {
315 ir_node *proj = get_edge_src_irn(edge);
319 be_liveness_remove(env->lv, proj);
327 void be_remove_dead_nodes_from_schedule(ir_graph *irg)
329 remove_dead_nodes_env_t env;
330 env.reachable = bitset_alloca(get_irg_last_idx(irg));
331 env.lv = be_get_irg_liveness(irg);
334 // mark all reachable nodes
335 irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
337 // walk schedule and remove non-marked nodes
338 irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
341 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod)
342 void be_init_irgmod(void)
344 FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");