/*
- * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
*
* This file is part of libFirm.
*
* - empty block elimination
* - a simple dead node elimination (set inputs of unreachable nodes to BAD)
*/
-#ifdef HAVE_CONFIG_H
#include "config.h"
-#endif
#include <stdlib.h>
#include "iredges_t.h"
#include "irgraph_t.h"
#include "irgopt.h"
+#include "irgmod.h"
#include "irprintf_t.h"
#include "irgwalk.h"
const arch_register_class_t *cls,
ir_node *pos)
{
- const arch_env_t *arch_env = birg->main_env->arch_env;
be_lv_t *lv = birg->lv;
ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
ir_graph *irg = get_irn_irg(bl);
DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
ir_nodeset_init(&live);
- be_liveness_nodes_live_at(lv, arch_env, cls, pos, &live);
+ be_liveness_nodes_live_at(lv, cls, pos, &live);
n = ir_nodeset_size(&live);
if(n == 0) {
return NULL;
}
- nodes = xmalloc(n * sizeof(nodes[0]));
+ nodes = XMALLOCN(ir_node*, n);
DBG((dbg, LEVEL_1, "live:\n"));
i = 0;
curr = perm;
for (i = 0; i < n; ++i) {
ir_node *perm_op = get_irn_n(perm, i);
- const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
+ const arch_register_t *reg = arch_get_irn_register(perm_op);
be_ssa_construction_env_t senv;
ir_mode *mode = get_irn_mode(perm_op);
ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
- arch_set_irn_register(arch_env, proj, reg);
+ arch_set_irn_register(proj, reg);
-#ifdef SCHEDULE_PROJS
- sched_add_after(curr, proj);
-#endif
curr = proj;
be_ssa_construction_init(&senv, birg);
return perm;
}
+static int blocks_removed;
+
/**
* Post-block-walker: Find blocks containing only one jump and
* remove them.
*/
-static void remove_empty_block(ir_node *block, void *data) {
+static void remove_empty_block(ir_node *block)
+{
const ir_edge_t *edge, *next;
+ int i, arity;
ir_node *node;
- int *changed = data;
+ ir_node *pred;
+ ir_node *succ_block;
ir_node *jump = NULL;
- assert(is_Block(block));
+ if (irn_visited_else_mark(block))
+ return;
if (get_Block_n_cfgpreds(block) != 1)
- return;
+ goto check_preds;
sched_foreach(block, node) {
if (! is_Jmp(node))
- return;
+ goto check_preds;
if (jump != NULL) {
/* we should never have 2 jumps in a block */
- panic("We should never have 2 jumps in a block");
+ panic("found 2 jumps in a block");
}
jump = node;
}
if (jump == NULL)
- return;
+ goto check_preds;
- node = get_Block_cfgpred(block, 0);
+ pred = get_Block_cfgpred(block, 0);
+ succ_block = NULL;
foreach_out_edge_safe(jump, edge, next) {
- ir_node *block = get_edge_src_irn(edge);
- int pos = get_edge_src_pos(edge);
+ int pos = get_edge_src_pos(edge);
+
+ assert(succ_block == NULL);
+ succ_block = get_edge_src_irn(edge);
+ if (has_Block_label(succ_block) && has_Block_label(block)) {
+ /*
+ * Currently we can add only one label for a block.
+ * Therefore we cannot combine them if both block already have one.
+ */
+ goto check_preds;
+ }
+
+ set_irn_n(succ_block, pos, pred);
+ }
- set_irn_n(block, pos, node);
+ if (has_Block_label(block)) {
+ /* move the label to the successor block */
+ ir_label_t label = get_Block_label(block);
+ set_Block_label(succ_block, label);
+ }
+
+ /* there can be some non-scheduled Pin nodes left in the block, move them
+ * to the succ block (Pin) or pred block (Sync) */
+ foreach_out_edge_safe(block, edge, next) {
+ node = get_edge_src_irn(edge);
+
+ if (node == jump)
+ continue;
+ if (is_Block(node)) {
+ /* a Block->Block edge: This should be the MacroBlock
+ edge, ignore it. */
+ assert(get_Block_MacroBlock(node) == block && "Wrong Block->Block edge");
+ continue;
+ }
+ /* we simply kill Pins, because there are some strange interactions
+ * between condeval, which produce PhiMs with Pins, we simply kill
+ * the pins here, everything is scheduled anyway */
+ if (is_Pin(node)) {
+ exchange(node, get_Pin_op(node));
+ continue;
+ }
+ if (is_Sync(node)) {
+ set_nodes_block(node, get_nodes_block(pred));
+ continue;
+ }
+ if (is_End(node)) { /* End-keep, reroute it to the successor */
+ int pos = get_edge_src_pos(edge);
+ set_irn_n(node, pos, succ_block);
+ continue;
+ }
+ panic("Unexpected node %+F in block %+F with empty schedule", node, block);
}
set_Block_cfgpred(block, 0, new_Bad());
- be_kill_node(jump);
- *changed = 1;
+ kill_node(jump);
+ blocks_removed = 1;
+
+ /* check predecessor */
+ remove_empty_block(get_nodes_block(pred));
+ return;
+
+check_preds:
+ arity = get_Block_n_cfgpreds(block);
+ for(i = 0; i < arity; ++i) {
+ ir_node *pred = get_Block_cfgpred_block(block, i);
+ remove_empty_block(pred);
+ }
}
/* removes basic blocks that just contain a jump instruction */
-int be_remove_empty_blocks(ir_graph *irg) {
- int changed = 0;
+int be_remove_empty_blocks(ir_graph *irg)
+{
+ ir_node *end;
+ int i, arity;
+
+ blocks_removed = 0;
+
+ ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
+ inc_irg_visited(irg);
+ remove_empty_block(get_irg_end_block(irg));
+ end = get_irg_end(irg);
+ arity = get_irn_arity(end);
+ for (i = 0; i < arity; ++i) {
+ ir_node *pred = get_irn_n(end, i);
+ if (!is_Block(pred))
+ continue;
+ remove_empty_block(pred);
+ }
+ ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
- irg_block_walk_graph(irg, remove_empty_block, NULL, &changed);
- if (changed) {
+ if (blocks_removed) {
/* invalidate analysis info */
set_irg_doms_inconsistent(irg);
set_irg_extblk_inconsistent(irg);
set_irg_outs_inconsistent(irg);
+ set_irg_loopinfo_inconsistent(irg);
}
- return changed;
+ return blocks_removed;
}
void be_init_irgmod(void)