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 *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);
119 for (i = 0; i < n; ++i) {
120 ir_node *perm_op = get_irn_n(perm, i);
121 const arch_register_t *reg = arch_get_irn_register(perm_op);
122 be_ssa_construction_env_t senv;
124 ir_mode *mode = get_irn_mode(perm_op);
125 ir_node *proj = new_r_Proj(perm, mode, i);
126 arch_set_irn_register(proj, reg);
128 be_ssa_construction_init(&senv, irg);
129 be_ssa_construction_add_copy(&senv, perm_op);
130 be_ssa_construction_add_copy(&senv, proj);
131 be_ssa_construction_fix_users(&senv, perm_op);
132 be_ssa_construction_update_liveness_phis(&senv, lv);
133 be_liveness_update(lv, perm_op);
134 be_liveness_update(lv, proj);
135 be_ssa_construction_destroy(&senv);
141 static int blocks_removed;
144 * Post-block-walker: Find blocks containing only one jump and
147 static void remove_empty_block(ir_node *block)
149 const ir_edge_t *edge, *next;
154 ir_node *jump = NULL;
155 ir_graph *irg = get_irn_irg(block);
157 if (irn_visited_else_mark(block))
160 if (get_Block_n_cfgpreds(block) != 1)
163 sched_foreach(block, node) {
165 && !(arch_get_irn_flags(node) & arch_irn_flags_simple_jump))
168 /* we should never have 2 jumps in a block */
169 panic("found 2 jumps in a block");
177 pred = get_Block_cfgpred(block, 0);
179 foreach_out_edge_safe(jump, edge, next) {
180 int pos = get_edge_src_pos(edge);
182 assert(succ_block == NULL);
183 succ_block = get_edge_src_irn(edge);
184 if (has_Block_entity(succ_block) && has_Block_entity(block)) {
186 * Currently we can add only one label for a block.
187 * Therefore we cannot combine them if both block already have one.
192 set_irn_n(succ_block, pos, pred);
195 if (has_Block_entity(block)) {
196 /* move the label to the successor block */
197 ir_entity *entity = get_Block_entity(block);
198 set_Block_entity(succ_block, entity);
201 /* there can be some non-scheduled Pin nodes left in the block, move them
202 * to the succ block (Pin) or pred block (Sync) */
203 foreach_out_edge_safe(block, edge, next) {
204 node = get_edge_src_irn(edge);
208 /* we simply kill Pins, because there are some strange interactions
209 * between jump threading, which produce PhiMs with Pins, we simply
210 * kill the pins here, everything is scheduled anyway */
212 exchange(node, get_Pin_op(node));
216 set_nodes_block(node, get_nodes_block(pred));
219 if (is_End(node)) { /* End-keep, reroute it to the successor */
220 int pos = get_edge_src_pos(edge);
221 set_irn_n(node, pos, succ_block);
224 panic("Unexpected node %+F in block %+F with empty schedule", node, block);
227 set_Block_cfgpred(block, 0, new_r_Bad(irg, mode_X));
231 /* check predecessor */
232 remove_empty_block(get_nodes_block(pred));
236 arity = get_Block_n_cfgpreds(block);
237 for (i = 0; i < arity; ++i) {
238 ir_node *pred = get_Block_cfgpred_block(block, i);
239 remove_empty_block(pred);
243 /* removes basic blocks that just contain a jump instruction */
244 int be_remove_empty_blocks(ir_graph *irg)
251 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
252 inc_irg_visited(irg);
253 remove_empty_block(get_irg_end_block(irg));
254 end = get_irg_end(irg);
255 arity = get_irn_arity(end);
256 for (i = 0; i < arity; ++i) {
257 ir_node *pred = get_irn_n(end, i);
260 remove_empty_block(pred);
262 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
264 if (blocks_removed) {
265 /* invalidate analysis info */
266 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
267 set_irg_extblk_inconsistent(irg);
269 return blocks_removed;
272 //---------------------------------------------------------------------------
274 typedef struct remove_dead_nodes_env_t_ {
278 } remove_dead_nodes_env_t;
281 * Post-walker: remember all visited nodes in a bitset.
283 static void mark_dead_nodes_walker(ir_node *node, void *data)
285 remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
286 bitset_set(env->reachable, get_irn_idx(node));
291 * Walk through the schedule of every block and remove all dead nodes from it.
293 static void remove_dead_nodes_walker(ir_node *block, void *data)
295 remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
296 ir_node *node, *next;
298 for (node = sched_first(block); ! sched_is_end(node); node = next) {
299 /* get next node now, as after calling sched_remove it will be invalid */
300 next = sched_next(node);
302 if (bitset_is_set(env->reachable, get_irn_idx(node)))
306 be_liveness_remove(env->lv, node);
310 if (get_irn_mode(node) == mode_T) {
311 const ir_edge_t *edge;
312 const ir_edge_t *next_edge;
313 foreach_out_edge_safe(node, edge, next_edge) {
314 ir_node *proj = get_edge_src_irn(edge);
318 be_liveness_remove(env->lv, proj);
326 void be_remove_dead_nodes_from_schedule(ir_graph *irg)
328 remove_dead_nodes_env_t env;
329 env.reachable = bitset_alloca(get_irg_last_idx(irg));
330 env.lv = be_get_irg_liveness(irg);
333 // mark all reachable nodes
334 irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
336 // walk schedule and remove non-marked nodes
337 irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
340 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod)
341 void be_init_irgmod(void)
343 FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");