substituted copymin ilp option with ilp1 and ilp2
[libfirm] / ir / be / bechordal_main.c
index 3ace535..7ea2e49 100644 (file)
@@ -33,6 +33,7 @@
 #include "xmalloc.h"
 
 #include "bechordal_t.h"
+#include "beabi.h"
 #include "beutil.h"
 #include "besched.h"
 #include "benumb_t.h"
 #include "beifg_impl.h"
 
 #include "bespillbelady.h"
-#include "bespillilp.h"
-#include "beconstrperm.h"
 #include "belower.h"
 
-#include "becopyoptmain.h"
-#include "bessadestr.h"
+#ifdef WITH_ILP
+#include "bespillilp.h"
+#endif /* WITH_ILP */
+
 #include "becopystat.h"
+#include "becopyopt.h"
+#include "bessadestr.h"
 
 
 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
-       firm_dbg_module_t *dbg = chordal_env->dbg;
-       const arch_env_t *arch_env = chordal_env->main_env->arch_env;
+       const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
        struct obstack ob;
        pmap_entry *pme;
        ir_node **nodes, *n1, *n2;
        int i, o;
+       DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
 
        /* Collect all irns */
        obstack_init(&ob);
@@ -96,13 +99,13 @@ void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
 static void check_pressure_walker(ir_node *bl, void *data)
 {
        be_chordal_env_t *env = data;
-       firm_dbg_module_t *dbg = env->dbg;
        int n_regs = arch_register_class_n_regs(env->cls);
 
        pset *live = pset_new_ptr_default();
        int step = 0;
        ir_node *irn;
        irn_live_t *li;
+       DEBUG_ONLY(firm_dbg_module_t *dbg = env->dbg;)
 
        live_foreach(bl, li) {
                if(live_is_end(li) && chordal_has_class(env, li->irn)) {
@@ -155,23 +158,29 @@ int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node
 
 
 static be_ra_chordal_opts_t options = {
-       BE_CH_DUMP_ALL,
+       BE_CH_DUMP_NONE,
        BE_CH_SPILL_BELADY,
        BE_CH_COPYMIN_HEUR,
        BE_CH_IFG_STD,
-       BE_CH_LOWER_PERM_SWAP
+       BE_CH_LOWER_PERM_SWAP,
 };
 
 #ifdef WITH_LIBCORE
 static const lc_opt_enum_int_items_t spill_items[] = {
        { "belady", BE_CH_SPILL_BELADY },
+#ifdef WITH_ILP
        { "ilp",        BE_CH_SPILL_ILP },
+#endif
        { NULL, 0 }
 };
 
 static const lc_opt_enum_int_items_t copymin_items[] = {
+       { "none", BE_CH_COPYMIN_NONE },
        { "heur", BE_CH_COPYMIN_HEUR },
-       { "ilp",  BE_CH_COPYMIN_ILP },
+#ifdef WITH_ILP
+       { "ilp1",  BE_CH_COPYMIN_ILP1 },
+       { "ilp2",  BE_CH_COPYMIN_ILP2 },
+#endif
        { NULL, 0 }
 };
 
@@ -182,8 +191,24 @@ static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
 };
 
 static const lc_opt_enum_int_items_t lower_perm_items[] = {
-       { "swap", BE_CH_LOWER_PERM_SWAP },
        { "copy", BE_CH_LOWER_PERM_COPY },
+       { "swap", BE_CH_LOWER_PERM_SWAP },
+       { NULL, 0 }
+};
+
+static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
+       { NULL, 0 }
+};
+
+static const lc_opt_enum_int_items_t dump_items[] = {
+       { "spill",    BE_CH_DUMP_SPILL },
+       { "live",     BE_CH_DUMP_LIVE },
+       { "color",    BE_CH_DUMP_COLOR },
+       { "copymin",  BE_CH_DUMP_COPYMIN },
+       { "ssadestr", BE_CH_DUMP_SSADESTR },
+       { "tree",     BE_CH_DUMP_TREE_INTV },
+       { "constr",   BE_CH_DUMP_CONSTR },
+       { "lower",    BE_CH_DUMP_LOWER },
        { NULL, 0 }
 };
 
@@ -200,12 +225,33 @@ static lc_opt_enum_int_var_t ifg_flavor_var = {
 };
 
 static lc_opt_enum_int_var_t lower_perm_var = {
-       &options.lower_perm_method, lower_perm_items
+       &options.lower_perm_opt, lower_perm_items
+};
+
+static lc_opt_enum_int_var_t dump_var = {
+       &options.dump_flags, dump_items
+};
+
+static const lc_opt_table_entry_t be_chordal_options[] = {
+       LC_OPT_ENT_ENUM_MASK("spill", "spill method (belady or ilp)", &spill_var),
+       LC_OPT_ENT_ENUM_PTR("copymin", "copymin method (none, heur or ilp)", &copymin_var),
+       LC_OPT_ENT_ENUM_PTR("ifg", "interference graph flavour (std or fast)", &ifg_flavor_var),
+       LC_OPT_ENT_ENUM_MASK("perm", "perm lowering options (copy, swap)", &lower_perm_var),
+       LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
+       { NULL }
 };
 
 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
 {
-       grp = lc_opt_get_grp(grp, "chordal");
+       static int run_once = 0;
+       lc_opt_entry_t *chordal_grp;
+
+       if (! run_once) {
+               run_once    = 1;
+               chordal_grp = lc_opt_get_grp(grp, "chordal");
+
+               lc_opt_add_table(chordal_grp, be_chordal_options);
+       }
 }
 #endif
 
@@ -214,40 +260,57 @@ static void dump(unsigned mask, ir_graph *irg,
                                 const char *suffix,
                                 void (*dump_func)(ir_graph *, const char *))
 {
-       if(1 || (options.dump_flags & mask) == mask) {
+       if(1 || ((options.dump_flags & mask) == mask)) {
                if(cls) {
                        char buf[256];
                        snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
-                       dump_func(irg, buf);
+                       be_dump(irg, buf, dump_func);
                }
 
                else
-                       dump_func(irg, suffix);
+                       be_dump(irg, suffix, dump_func);
        }
 }
 
-static void be_ra_chordal_main(const be_main_env_t *main_env, ir_graph *irg)
+static void put_ignore_colors(be_chordal_env_t *chordal_env)
+{
+       int n_colors = chordal_env->cls->n_regs;
+       int i;
+
+       bitset_clear_all(chordal_env->ignore_colors);
+       be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
+       for(i = 0; i < n_colors; ++i)
+               if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
+                       bitset_set(chordal_env->ignore_colors, i);
+}
+
+static void be_ra_chordal_main(const be_irg_t *bi)
 {
+       const be_main_env_t *main_env = bi->main_env;
+       const arch_isa_t *isa         = arch_env_get_isa(main_env->arch_env);
+       ir_graph *irg                 = bi->irg;
+
        int j, m;
        be_chordal_env_t chordal_env;
-       const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
 
        compute_doms(irg);
 
-       chordal_env.irg          = irg;
-       chordal_env.dbg          = firm_dbg_register("be.chordal");
-       chordal_env.main_env     = main_env;
-       chordal_env.dom_front    = be_compute_dominance_frontiers(irg);
+       chordal_env.opts          = &options;
+       chordal_env.irg           = irg;
+       chordal_env.birg          = bi;
+       chordal_env.dom_front     = be_compute_dominance_frontiers(irg);
+       FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
 
        obstack_init(&chordal_env.obst);
 
-       lower_nodes_before_ra(&chordal_env, 0);
-       dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-before-ra", dump_ir_block_graph_sched);
-
        /* Perform the following for each register class. */
        for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
-               chordal_env.border_heads = pmap_create();
-               chordal_env.cls = arch_isa_get_reg_class(isa, j);
+               chordal_env.cls           = arch_isa_get_reg_class(isa, j);
+               chordal_env.border_heads  = pmap_create();
+               chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
+
+               /* put all ignore registers into the ignore register set. */
+               put_ignore_colors(&chordal_env);
 
                be_liveness(irg);
                dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
@@ -257,9 +320,11 @@ static void be_ra_chordal_main(const be_main_env_t *main_env, ir_graph *irg)
                case BE_CH_SPILL_BELADY:
                        be_spill_belady(&chordal_env);
                        break;
+#ifdef WITH_ILP
                case BE_CH_SPILL_ILP:
                        be_spill_ilp(&chordal_env);
                        break;
+#endif /* WITH_ILP */
                default:
                        fprintf(stderr, "no valid spiller selected. falling back to belady\n");
                        be_spill_belady(&chordal_env);
@@ -268,14 +333,6 @@ static void be_ra_chordal_main(const be_main_env_t *main_env, ir_graph *irg)
                be_liveness(irg);
                be_check_pressure(&chordal_env);
 
-               /* Insert perms before reg-constrained instructions */
-               be_insert_constr_perms(&chordal_env);
-               dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-constr", dump_ir_block_graph_sched);
-
-               be_liveness(irg);
-               be_numbering(irg);
-               be_check_pressure(&chordal_env);
-
                /* Color the graph. */
                be_ra_chordal_color(&chordal_env);
                dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
@@ -285,8 +342,36 @@ static void be_ra_chordal_main(const be_main_env_t *main_env, ir_graph *irg)
                be_ifg_check(chordal_env.ifg);
 
                /* copy minimization */
-               copystat_collect_cls(&chordal_env);
-               be_copy_opt(&chordal_env);
+#ifdef COPYOPT_STAT
+               co_compare_solvers(&chordal_env);
+#else /* COPYOPT_STAT */
+               {
+                       copy_opt_t *co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
+                       co_build_ou_structure(co);
+
+                       switch(options.copymin_method) {
+                               case BE_CH_COPYMIN_HEUR:
+                                       co_solve_heuristic(co);
+                                       break;
+#ifdef WITH_ILP
+                               case BE_CH_COPYMIN_ILP1:
+                                       co_solve_ilp1(co, 60.0);
+                                       break;
+                               case BE_CH_COPYMIN_ILP2:
+                                       co_build_graph_structure(co);
+                                       co_solve_ilp2(co, 60.0);
+                                       co_free_graph_structure(co);
+                                       break;
+#endif /* WITH_ILP */
+                               case BE_CH_COPYMIN_NONE:
+                               default:
+                                       break;
+                       }
+
+                       co_free_ou_structure(co);
+                       free_copy_opt(co);
+               }
+#endif /* COPYOPT_STAT */
                dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
                be_ra_chordal_check(&chordal_env);
 
@@ -299,20 +384,19 @@ static void be_ra_chordal_main(const be_main_env_t *main_env, ir_graph *irg)
                copystat_dump(irg);
 
                be_ifg_free(chordal_env.ifg);
-               be_numbering_done(irg);
-
                pmap_destroy(chordal_env.border_heads);
+               bitset_free(chordal_env.ignore_colors);
        }
 
        be_compute_spill_offsets(&chordal_env);
 
        dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
 
-       lower_nodes_after_ra(&chordal_env, options.lower_perm_method == BE_CH_LOWER_PERM_COPY ? 1 : 0);
+       lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
        dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
 
-       be_free_dominance_frontiers(chordal_env.dom_front);
        obstack_free(&chordal_env.obst, NULL);
+       be_free_dominance_frontiers(chordal_env.dom_front);
 }
 
 const be_ra_t be_ra_chordal_allocator = {