Move Syncs out of the way before removing a block.
[libfirm] / ir / be / beirgmod.c
index b19e689..cfcbac6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
  *
@@ -130,9 +130,6 @@ ir_node *insert_Perm_after(be_irg_t *birg,
                ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
                arch_set_irn_register(arch_env, proj, reg);
 
-#ifdef SCHEDULE_PROJS
-               sched_add_after(curr, proj);
-#endif
                curr = proj;
 
                be_ssa_construction_init(&senv, birg);
@@ -148,59 +145,120 @@ ir_node *insert_Perm_after(be_irg_t *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(block))
+               return;
 
+       mark_irn_visited(block);
        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);
 
-               set_irn_n(block, pos, node);
+               set_irn_n(succ_block, pos, pred);
+       }
+
+       /* there can be some non-scheduled Pin nodes left in the block, move them
+        * to the succ block */
+       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;
+               }
+               if (is_Pin(node)) {
+                       set_nodes_block(node, succ_block);
+                       continue;
+               }
+               if (is_Sync(node)) {
+                       set_nodes_block(node, get_nodes_block(pred));
+                       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;
+       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;
+
+       set_using_irn_visited(irg);
+       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);
+       }
+       clear_using_irn_visited(irg);
 
-       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);
        }
-       return changed;
+       return blocks_removed;
 }
 
 void be_init_irgmod(void)