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;)
72 static int cmp_node_nr(const void *a, const void *b)
74 ir_node **p1 = (ir_node**)a;
75 ir_node **p2 = (ir_node**)b;
76 long n1 = get_irn_node_nr(*p1);
77 long n2 = get_irn_node_nr(*p2);
78 return (n1>n2) - (n1<n2);
83 |_ _|_ __ ___ ___ _ __| |_ | _ \ ___ _ __ _ __ ___
84 | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
85 | || | | \__ \ __/ | | |_ | __/ __/ | | | | | | |
86 |___|_| |_|___/\___|_| \__| |_| \___|_| |_| |_| |_|
90 ir_node *insert_Perm_after(ir_graph *irg, const arch_register_class_t *cls,
93 be_lv_t *lv = be_get_irg_liveness(irg);
94 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
97 ir_node *perm, **nodes;
100 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
102 ir_nodeset_init(&live);
103 be_liveness_nodes_live_at(lv, cls, pos, &live);
105 n = ir_nodeset_size(&live);
107 ir_nodeset_destroy(&live);
111 nodes = XMALLOCN(ir_node*, n);
113 DBG((dbg, LEVEL_1, "live:\n"));
115 foreach_ir_nodeset(&live, irn, iter) {
116 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
120 ir_nodeset_destroy(&live);
121 /* make the input order deterministic */
122 qsort(nodes, n, sizeof(nodes[0]), cmp_node_nr);
124 perm = be_new_Perm(cls, bl, n, nodes);
125 sched_add_after(pos, perm);
128 for (i = 0; i < n; ++i) {
129 ir_node *perm_op = get_irn_n(perm, i);
130 const arch_register_t *reg = arch_get_irn_register(perm_op);
131 be_ssa_construction_env_t senv;
133 ir_mode *mode = get_irn_mode(perm_op);
134 ir_node *proj = new_r_Proj(perm, mode, i);
135 arch_set_irn_register(proj, reg);
137 be_ssa_construction_init(&senv, irg);
138 be_ssa_construction_add_copy(&senv, perm_op);
139 be_ssa_construction_add_copy(&senv, proj);
140 be_ssa_construction_fix_users(&senv, perm_op);
141 be_ssa_construction_update_liveness_phis(&senv, lv);
142 be_liveness_update(lv, perm_op);
143 be_liveness_update(lv, proj);
144 be_ssa_construction_destroy(&senv);
150 static int blocks_removed;
153 * Post-block-walker: Find blocks containing only one jump and
156 static void remove_empty_block(ir_node *block)
162 ir_node *jump = NULL;
163 ir_graph *irg = get_irn_irg(block);
166 if (irn_visited_else_mark(block))
169 if (get_Block_n_cfgpreds(block) != 1)
172 sched_foreach(block, node) {
174 && !(arch_get_irn_flags(node) & arch_irn_flags_simple_jump))
177 /* we should never have 2 jumps in a block */
178 panic("found 2 jumps in a block");
186 entity = get_Block_entity(block);
187 pred = get_Block_cfgpred(block, 0);
189 foreach_out_edge_safe(jump, edge) {
190 int pos = get_edge_src_pos(edge);
192 assert(succ_block == NULL);
193 succ_block = get_edge_src_irn(edge);
194 if (get_Block_entity(succ_block) != NULL && entity != NULL) {
196 * Currently we can add only one label for a block.
197 * Therefore we cannot combine them if both block already have one.
202 set_irn_n(succ_block, pos, pred);
205 if (entity != NULL) {
206 /* move the label to the successor block */
207 set_Block_entity(succ_block, entity);
210 /* there can be some non-scheduled Pin nodes left in the block, move them
211 * to the succ block (Pin) or pred block (Sync) */
212 foreach_out_edge_safe(block, edge) {
213 ir_node *const node = get_edge_src_irn(edge);
217 /* we simply kill Pins, because there are some strange interactions
218 * between jump threading, which produce PhiMs with Pins, we simply
219 * kill the pins here, everything is scheduled anyway */
221 exchange(node, get_Pin_op(node));
225 set_nodes_block(node, get_nodes_block(pred));
228 if (is_End(node)) { /* End-keep, reroute it to the successor */
229 int pos = get_edge_src_pos(edge);
230 set_irn_n(node, pos, succ_block);
233 panic("Unexpected node %+F in block %+F with empty schedule", node, block);
236 set_Block_cfgpred(block, 0, new_r_Bad(irg, mode_X));
240 /* check predecessor */
241 remove_empty_block(get_nodes_block(pred));
245 arity = get_Block_n_cfgpreds(block);
246 for (i = 0; i < arity; ++i) {
247 ir_node *pred = get_Block_cfgpred_block(block, i);
248 remove_empty_block(pred);
252 /* removes basic blocks that just contain a jump instruction */
253 int be_remove_empty_blocks(ir_graph *irg)
260 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
261 inc_irg_visited(irg);
262 remove_empty_block(get_irg_end_block(irg));
263 end = get_irg_end(irg);
264 arity = get_irn_arity(end);
265 for (i = 0; i < arity; ++i) {
266 ir_node *pred = get_irn_n(end, i);
269 remove_empty_block(pred);
271 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
273 if (blocks_removed) {
274 /* invalidate analysis info */
275 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
277 return blocks_removed;
280 //---------------------------------------------------------------------------
282 typedef struct remove_dead_nodes_env_t_ {
286 } remove_dead_nodes_env_t;
289 * Post-walker: remember all visited nodes in a bitset.
291 static void mark_dead_nodes_walker(ir_node *node, void *data)
293 remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
294 bitset_set(env->reachable, get_irn_idx(node));
299 * Walk through the schedule of every block and remove all dead nodes from it.
301 static void remove_dead_nodes_walker(ir_node *block, void *data)
303 remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
304 ir_node *node, *next;
306 for (node = sched_first(block); ! sched_is_end(node); node = next) {
307 /* get next node now, as after calling sched_remove it will be invalid */
308 next = sched_next(node);
310 if (bitset_is_set(env->reachable, get_irn_idx(node)))
314 be_liveness_remove(env->lv, node);
318 if (get_irn_mode(node) == mode_T) {
319 foreach_out_edge_safe(node, edge) {
320 ir_node *proj = get_edge_src_irn(edge);
324 be_liveness_remove(env->lv, proj);
332 void be_remove_dead_nodes_from_schedule(ir_graph *irg)
334 remove_dead_nodes_env_t env;
335 env.reachable = bitset_alloca(get_irg_last_idx(irg));
336 env.lv = be_get_irg_liveness(irg);
339 // mark all reachable nodes
340 irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
342 // walk schedule and remove non-marked nodes
343 irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
346 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod)
347 void be_init_irgmod(void)
349 FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");