Add some Processor specific instruction selections
[libfirm] / ir / be / bessadestr.c
index f904611..8608025 100644 (file)
 #include "benode_t.h"
 #include "besched_t.h"
 
-static firm_dbg_module_t *dbg = NULL;
+DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 #define DUMP_GRAPHS
 
-#define get_chordal_arch(ce) ((ce)->main_env->arch_env)
+#define get_chordal_arch(ce) ((ce)->birg->main_env->arch_env)
 #define get_reg(irn) arch_get_irn_register(get_chordal_arch(chordal_env), irn)
 #define set_reg(irn, reg) arch_set_irn_register(get_chordal_arch(chordal_env), irn, reg)
 
@@ -85,7 +85,6 @@ static void insert_all_perms_walker(ir_node *bl, void *data) {
        be_chordal_env_t *chordal_env = data;
        pmap *perm_map = chordal_env->data;
        ir_graph *irg = chordal_env->irg;
-       const be_node_factory_t *fact = chordal_env->main_env->node_factory;
        int i, n;
 
        assert(is_Block(bl));
@@ -135,13 +134,13 @@ static void insert_all_perms_walker(ir_node *bl, void *data) {
                         * Create a new Perm with the arguments just collected
                         * above in the arg_set and insert it into the schedule.
                         */
-                       in = malloc(n_projs * sizeof(in[0]));
+                       in = xmalloc(n_projs * sizeof(in[0]));
                        for(pp = set_first(arg_set); pp; pp = set_next(arg_set))
                                in[pp->pos] = pp->arg;
 
-                       perm = new_Perm(fact, chordal_env->cls, irg, pred_bl, n_projs, in);
+                       perm = be_new_Perm(chordal_env->cls, irg, pred_bl, n_projs, in);
                        free(in);
-                       insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, chordal_env->main_env->arch_env);
+                       insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env);
                        sched_add_after(insert_after, perm);
 
                        /*
@@ -223,60 +222,71 @@ static void       set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
                                 * insert it into schedule,
                                 * pin it
                                 */
-                               ir_node *dupl = new_Copy(chordal_env->main_env->node_factory, cls, chordal_env->irg, arg_block, arg);
+                               ir_node *dupl = be_new_Copy(cls, chordal_env->irg, arg_block, arg);
                                assert(get_irn_mode(phi) == get_irn_mode(dupl));
                                set_irn_n(phi, i, dupl);
                                set_reg(dupl, phi_reg);
-                               sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, chordal_env->main_env->arch_env), dupl);
+                               sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env), dupl);
                                pin_irn(dupl, phi_block);
                                DBG((dbg, LEVEL_1, "    they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
-                       } else {
-                               /*
-                                * First check if there is a phi
-                                * - in the same block
-                                * - having arg at the current pos in its arg-list
-                                * - having the same color as arg
-                                *
-                                * If found, then pin the arg
+                               continue; /* with next argument */
+                       }
+
+                       if (phi_reg == arg_reg) {
+                               /* Phi and arg have the same register,
+                                * so pin and continue
                                 */
-                               DBG((dbg, LEVEL_1, "    they do not interfere\n"));
-                               assert(is_Proj(arg));
-                               if (!is_pinned(arg)) {
-                                       ir_node *other_phi;
-                                       DBG((dbg, LEVEL_1, "      searching for phi with same arg having args register\n"));
-                                       for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
-                                               assert(is_Phi(other_phi) && get_nodes_block(phi) == get_nodes_block(other_phi) && "link fields are screwed up");
-                                               if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
-                                                       DBG((dbg, LEVEL_1, "        found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
-                                                       pin_irn(arg, phi_block);
-                                               }
+                               pin_irn(arg, phi_block);
+                               DBG((dbg, LEVEL_1, "      arg has same reg: pin %+F(%s)\n", arg, get_reg(arg)->name));
+                               continue;
+                       }
+
+                       DBG((dbg, LEVEL_1, "    they do not interfere\n"));
+                       assert(is_Proj(arg));
+                       /*
+                        * First check if there is an other phi
+                        * - in the same block
+                        * - having arg at the current pos in its arg-list
+                        * - having the same color as arg
+                        *
+                        * If found, then pin the arg (for that phi)
+                        */
+                       if (!is_pinned(arg)) {
+                               ir_node *other_phi;
+                               DBG((dbg, LEVEL_1, "      searching for phi with same arg having args register\n"));
+                               for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
+                                       assert(is_Phi(other_phi) && get_nodes_block(phi) == get_nodes_block(other_phi) && "link fields are screwed up");
+                                       if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
+                                               DBG((dbg, LEVEL_1, "        found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
+                                               pin_irn(arg, phi_block);
+                                               break;
                                        }
                                }
+                       }
 
-                               if (is_pinned(arg)) {
-                                       /* Insert a duplicate of the original value in arguments block,
-                                        * make it the new phi arg,
-                                        * set its register,
-                                        * insert it into schedule,
-                                        * pin it
-                                        */
-                                       ir_node *perm = get_Proj_pred(arg);
-                                       ir_node *orig_val = get_irn_n(perm, get_Proj_proj(arg));
-                                       ir_node *dupl = new_Copy(chordal_env->main_env->node_factory, cls, chordal_env->irg, arg_block, orig_val);
-                                       assert(get_irn_mode(phi) == get_irn_mode(dupl));
-                                       set_irn_n(phi, i, dupl);
-                                       set_reg(dupl, phi_reg);
-                                       sched_add_before(perm, dupl);
-                                       pin_irn(dupl, phi_block);
-                                       DBG((dbg, LEVEL_1, "      arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
-                               } else {
-                                       /* No other phi has the same color (else arg would have been pinned),
-                                        * so just set the register and pin
-                                        */
-                                       set_reg(arg, phi_reg);
-                                       pin_irn(arg, phi_block);
-                                       DBG((dbg, LEVEL_1, "      arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
-                               }
+                       if (is_pinned(arg)) {
+                               /* Insert a duplicate of the original value in arguments block,
+                                * make it the new phi arg,
+                                * set its register,
+                                * insert it into schedule,
+                                * pin it
+                                */
+                               ir_node *perm = get_Proj_pred(arg);
+                               ir_node *orig_val = get_irn_n(perm, get_Proj_proj(arg));
+                               ir_node *dupl = be_new_Copy(cls, chordal_env->irg, arg_block, orig_val);
+                               assert(get_irn_mode(phi) == get_irn_mode(dupl));
+                               set_irn_n(phi, i, dupl);
+                               set_reg(dupl, phi_reg);
+                               sched_add_before(perm, dupl);
+                               pin_irn(dupl, phi_block);
+                               DBG((dbg, LEVEL_1, "      arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
+                       } else {
+                               /* No other phi has the same color (else arg would have been pinned),
+                                * so just set the register and pin
+                                */
+                               set_reg(arg, phi_reg);
+                               pin_irn(arg, phi_block);
+                               DBG((dbg, LEVEL_1, "      arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
                        }
                }
        }
@@ -286,7 +296,7 @@ void be_ssa_destruction(be_chordal_env_t *chordal_env) {
        pmap *perm_map = pmap_create();
        ir_graph *irg = chordal_env->irg;
 
-       dbg = firm_dbg_register("ir.be.ssadestr");
+       FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
 
        /* create a map for fast lookup of perms: block --> perm */
        chordal_env->data = perm_map;
@@ -295,13 +305,15 @@ void be_ssa_destruction(be_chordal_env_t *chordal_env) {
        DBG((dbg, LEVEL_1, "Placing perms...\n"));
        irg_block_walk_graph(irg, insert_all_perms_walker, NULL, chordal_env);
 #ifdef DUMP_GRAPHS
-       dump_ir_block_graph_sched(irg, "-ssa_destr_perms_placed");
+       be_dump(irg, "-ssa_destr_perms_placed", dump_ir_block_graph_sched);
 #endif
 
+       be_liveness(irg);
+
        DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
        irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
 #ifdef DUMP_GRAPHS
-       dump_ir_block_graph_sched(irg, "-ssa_destr_regs_set");
+       be_dump(irg, "-ssa_destr_regs_set", dump_ir_block_graph_sched);
 #endif
 
        pmap_destroy(perm_map);