put register classes into arch_env struct, no need for complicated callbacks
[libfirm] / ir / be / beirgmod.c
index dc3ee7a..93cc7de 100644 (file)
@@ -29,9 +29,7 @@
  * - 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 "be_t.h"
 #include "bechordal_t.h"
-#include "bearch_t.h"
-#include "besched_t.h"
+#include "bearch.h"
+#include "besched.h"
 #include "belive_t.h"
-#include "benode_t.h"
+#include "benode.h"
 #include "beutil.h"
 #include "beinsn_t.h"
 #include "bessaconstr.h"
-#include "beirg_t.h"
+#include "beirg.h"
 #include "beirgmod.h"
 #include "bemodule.h"
 
@@ -81,14 +79,11 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
 */
 
-ir_node *insert_Perm_after(be_irg_t *birg,
-                                                  const arch_register_class_t *cls,
+ir_node *insert_Perm_after(ir_graph *irg, 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;
+       be_lv_t *lv     = be_get_irg_liveness(irg);
        ir_node *bl     = is_Block(pos) ? pos : get_nodes_block(pos);
-       ir_graph *irg   = get_irn_irg(bl);
        ir_nodeset_t          live;
        ir_nodeset_iterator_t iter;
 
@@ -98,15 +93,15 @@ ir_node *insert_Perm_after(be_irg_t *birg,
        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) {
+       if (n == 0) {
                ir_nodeset_destroy(&live);
                return NULL;
        }
 
-       nodes = xmalloc(n * sizeof(nodes[0]));
+       nodes = XMALLOCN(ir_node*, n);
 
        DBG((dbg, LEVEL_1, "live:\n"));
        i = 0;
@@ -117,23 +112,23 @@ ir_node *insert_Perm_after(be_irg_t *birg,
        }
        ir_nodeset_destroy(&live);
 
-       perm = be_new_Perm(cls, irg, bl, n, nodes);
+       perm = be_new_Perm(cls, bl, n, nodes);
        sched_add_after(pos, perm);
        free(nodes);
 
        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);
+               ir_node *proj = new_r_Proj(perm, mode, i);
+               arch_set_irn_register(proj, reg);
 
                curr = proj;
 
-               be_ssa_construction_init(&senv, birg);
+               be_ssa_construction_init(&senv, irg);
                be_ssa_construction_add_copy(&senv, perm_op);
                be_ssa_construction_add_copy(&senv, proj);
                be_ssa_construction_fix_users(&senv, perm_op);
@@ -160,16 +155,17 @@ static void remove_empty_block(ir_node *block)
        ir_node *pred;
        ir_node *succ_block;
        ir_node *jump = NULL;
+       ir_graph *irg = get_irn_irg(block);
 
-       if (irn_visited(block))
+       if (irn_visited_else_mark(block))
                return;
 
-       mark_irn_visited(block);
        if (get_Block_n_cfgpreds(block) != 1)
                goto check_preds;
 
        sched_foreach(block, node) {
-               if (! is_Jmp(node))
+               if (! is_Jmp(node)
+                               && !(arch_irn_get_flags(node) & arch_irn_flags_simple_jump))
                        goto check_preds;
                if (jump != NULL) {
                        /* we should never have 2 jumps in a block */
@@ -188,25 +184,35 @@ static void remove_empty_block(ir_node *block)
 
                assert(succ_block == NULL);
                succ_block = get_edge_src_irn(edge);
+               if (has_Block_entity(succ_block) && has_Block_entity(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);
        }
 
+       if (has_Block_entity(block)) {
+               /* move the label to the successor block */
+               ir_entity *entity = get_Block_entity(block);
+               set_Block_entity(succ_block, entity);
+       }
+
        /* 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");
+               if (node == jump)
                        continue;
-               }
+               /* we simply kill Pins, because there are some strange interactions
+                * between jump threading, which produce PhiMs with Pins, we simply
+                * kill the pins here, everything is scheduled anyway */
                if (is_Pin(node)) {
-                       set_nodes_block(node, succ_block);
+                       exchange(node, get_Pin_op(node));
                        continue;
                }
                if (is_Sync(node)) {
@@ -221,7 +227,7 @@ static void remove_empty_block(ir_node *block)
                panic("Unexpected node %+F in block %+F with empty schedule", node, block);
        }
 
-       set_Block_cfgpred(block, 0, new_Bad());
+       set_Block_cfgpred(block, 0, new_r_Bad(irg));
        kill_node(jump);
        blocks_removed = 1;
 
@@ -231,7 +237,7 @@ static void remove_empty_block(ir_node *block)
 
 check_preds:
        arity = get_Block_n_cfgpreds(block);
-       for(i = 0; i < arity; ++i) {
+       for (i = 0; i < arity; ++i) {
                ir_node *pred = get_Block_cfgpred_block(block, i);
                remove_empty_block(pred);
        }
@@ -250,9 +256,9 @@ int be_remove_empty_blocks(ir_graph *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) {
+       for (i = 0; i < arity; ++i) {
                ir_node *pred = get_irn_n(end, i);
-               if(!is_Block(pred))
+               if (!is_Block(pred))
                        continue;
                remove_empty_block(pred);
        }
@@ -268,9 +274,8 @@ int be_remove_empty_blocks(ir_graph *irg)
        return blocks_removed;
 }
 
+BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);
 void be_init_irgmod(void)
 {
        FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");
 }
-
-BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);