- at blockstart emit list of predblocks in comment
[libfirm] / ir / be / bespillremat.c
index 7b75e18..93ac093 100644 (file)
@@ -87,10 +87,10 @@ static int opt_repair_schedule = 0;
 static int opt_no_enlarge_liveness = 0;
 static int opt_remat_while_live = 1;
 static int opt_timeout = 300;
-static double opt_cost_reload = 8.0
-static double opt_cost_memoperand =  7.0
-static double opt_cost_spill =  50.0
-static double opt_cost_remat =  1.0
+static double opt_cost_reload = 8.0;
+static double opt_cost_memoperand =  7.0;
+static double opt_cost_spill =  50.0;
+static double opt_cost_remat =  1.0;
 
 
 #ifdef WITH_LIBCORE
@@ -112,7 +112,7 @@ static const lc_opt_enum_mask_items_t keepalive_items[] = {
        { NULL,      0 }
 };
 
-static lc_opt_enum_mask_var_t dump_var = {
+static lc_opt_enum_mask_var_t keep_alive_var = {
        &opt_keep_alive, keepalive_items
 };
 
@@ -120,11 +120,11 @@ static const lc_opt_enum_mask_items_t remats_items[] = {
        { "none",      REMATS_NONE      },
        { "briggs",    REMATS_BRIGGS    },
        { "noinverse", REMATS_NOINVERSE },
-       { "ALL",       REMATS_ALL       },
+       { "all",       REMATS_ALL       },
        { NULL,        0 }
 };
 
-static lc_opt_enum_mask_var_t dump_var = {
+static lc_opt_enum_mask_var_t remats_var = {
        &opt_remats, remats_items
 };
 
@@ -134,9 +134,9 @@ static const lc_opt_table_entry_t options[] = {
        LC_OPT_ENT_BOOL     ("goodwin",  "activate goodwin reduction",                              &opt_goodwin),
        LC_OPT_ENT_BOOL     ("memcopies",  "activate memcopy handling",                             &opt_memcopies),
        LC_OPT_ENT_BOOL     ("memoperands",  "activate memoperands",                                &opt_memoperands),
-       LC_OPT_ENT_ENUM_INT ("remat",  "type of remats to insert (none, briggs, noinverse or all)", &remats_var),
+       LC_OPT_ENT_ENUM_INT ("remats",  "type of remats to insert (none, briggs, noinverse or all)",&remats_var),
        LC_OPT_ENT_BOOL     ("repair_schedule",  "repair the schedule by rematting once used nodes",&opt_repair_schedule),
-       LC_OPT_ENT_BOOL     ("no_enlage_liveness",  "do not enlarge liveness of operands of remats",&opt_enlarge_liveness),
+       LC_OPT_ENT_BOOL     ("no_enlage_liveness",  "do not enlarge liveness of operands of remats",&opt_no_enlarge_liveness),
        LC_OPT_ENT_BOOL     ("remat_while_live",  "remat only values that can be used by real ops", &opt_remat_while_live),
 
        LC_OPT_ENT_ENUM_MASK("dump", "dump ifg before, after or after each cloud",                  &dump_var),
@@ -157,7 +157,9 @@ void be_spill_remat_register_options(lc_opt_entry_t *grp)
 }
 #endif
 
-//#define EXECFREQ_LOOPDEPH /* compute execution frequency from loop depth only */
+
+//#define EXECFREQ_LOOPDEPH   /* compute execution frequency from loop depth only */
+//#define SCHEDULE_PHIM   /* insert phim nodes into schedule */
 
 #define  SOLVE
 //#define  SOLVE_LOCAL
@@ -165,7 +167,7 @@ void be_spill_remat_register_options(lc_opt_entry_t *grp)
 #define LPP_SOLVER "cplex"
 
 
-#define MAX_PATHS      16
+#define MAX_PATHS      INT_MAX
 #define ILP_UNDEF              -1
 
 typedef struct _spill_ilp_t {
@@ -477,7 +479,7 @@ get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node *
                const ir_node *proj = NULL;
 
                if(is_Proj(dest_value)) {
-                       op = get_irn_n(op, 0);
+                       op = get_Proj_pred(op);
                        proj = dest_value;
                }
 
@@ -598,10 +600,15 @@ static int
 get_irn_n_nonignore_args(const spill_ilp_t * si, const ir_node * irn)
 {
        int n;
-       unsigned int ret = 0;
+       int ret = 0;
+
+//     if(is_Proj(irn))
+//             irn = get_Proj_pred(irn);
 
        for(n=get_irn_arity(irn)-1; n>=0; --n) {
-               if(has_reg_class(si, irn)) ++ret;
+               const ir_node  *arg = get_irn_n(irn, n);
+
+               if(has_reg_class(si, arg)) ++ret;
        }
 
        return ret;
@@ -789,7 +796,7 @@ can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node *
 
                if(opt_no_enlarge_liveness) {
                        if(has_reg_class(si, arg) && live) {
-                               res &= pset_find_ptr(live, arg)?1:0;
+                               res &= pset_find_ptr((pset*)live, arg)?1:0;
                        } else {
                                res &= value_is_defined_before(si, pos, arg);
                        }
@@ -883,7 +890,7 @@ insert_remat_after(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const
                                                *proj_copy;
                op_t            *op;
 
-               DBG((si->dbg, LEVEL_3, "\t  >inserting remat %+F\n", remat->op));
+               DBG((si->dbg, LEVEL_3, "\t  >inserting remat2 %+F\n", remat->op));
 
                copy = insert_copy_after(si, remat->op, pos);
 
@@ -1197,33 +1204,55 @@ walker_remat_insertor(ir_node * bb, void * data)
                irn = next;
        }
 
-       be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_in, i) {
-               ir_node        *value = be_lv_get_irn(si->lv, bb, i);
+       /* add remats at end if successor has multiple predecessors */
+       if(is_merge_edge(bb)) {
+               pset     *live_out = pset_new_ptr_default();
+               ir_node  *value;
+
+               be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
+                       value = be_lv_get_irn(si->lv, bb, i);
 
-               /* add remats at end if successor has multiple predecessors */
-               if(is_merge_edge(bb)) {
-                       /* add remats at end of block */
                        if (be_is_live_end(si->lv, bb, value) && has_reg_class(si, value)) {
-                               remat_info_t   *remat_info,
-                                                          query;
-                               remat_t        *remat;
+                               pset_insert_ptr(live_out, value);
+                       }
+               }
 
-                               query.irn = value;
-                               query.remats = NULL;
-                               query.remats_by_operand = NULL;
-                               remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
+               /* add remats at end of block */
+               pset_foreach(live_out, value) {
+                       remat_info_t   *remat_info,
+                                                  query;
+                       remat_t        *remat;
 
-                               if(remat_info && remat_info->remats) {
-                                       pset_foreach(remat_info->remats, remat) {
-                                               DBG((si->dbg, LEVEL_4, "\t  considering remat %+F at end of block %+F\n", remat->op, bb));
+                       query.irn = value;
+                       query.remats = NULL;
+                       query.remats_by_operand = NULL;
+                       remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
 
-                                               insert_remat_before(si, remat, bb, NULL);
-                                       }
+                       if(remat_info && remat_info->remats) {
+                               pset_foreach(remat_info->remats, remat) {
+                                       DBG((si->dbg, LEVEL_4, "\t  considering remat %+F at end of block %+F\n", remat->op, bb));
+
+                                       insert_remat_before(si, remat, bb, live_out);
                                }
                        }
                }
-               if(is_diverge_edge(bb)) {
-                       /* add remat2s at beginning of block */
+               del_pset(live_out);
+       }
+
+       if(is_diverge_edge(bb)) {
+               pset     *live_in = pset_new_ptr_default();
+               ir_node  *value;
+
+               be_lv_foreach(si->lv, bb, be_lv_state_in, i) {
+                       value = be_lv_get_irn(si->lv, bb, i);
+
+                       if (has_reg_class(si, value)) {
+                               pset_insert_ptr(live_in, value);
+                       }
+               }
+
+               /* add remat2s at beginning of block */
+               pset_foreach(live_in, value) {
                        if ((be_is_live_in(si->lv, bb, value) || (is_Phi(value) && get_nodes_block(value)==bb)) && has_reg_class(si, value)) {
                                remat_info_t   *remat_info,
                                                           query;
@@ -1234,17 +1263,18 @@ walker_remat_insertor(ir_node * bb, void * data)
                                query.remats_by_operand = NULL;
                                remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
 
-                               if(remat_info && remat_info->remats) {
-                                       pset_foreach(remat_info->remats, remat) {
-                                               DBG((si->dbg, LEVEL_4, "\t  considering remat %+F at beginning of block %+F\n", remat->op, bb));
+                               if(remat_info && remat_info->remats_by_operand) {
+                                       pset_foreach(remat_info->remats_by_operand, remat) {
+                                               DBG((si->dbg, LEVEL_4, "\t  considering remat2 %+F at beginning of block %+F\n", remat->op, bb));
 
                                                /* put the remat here if all its args are available */
-                                               insert_remat_after(si, remat, bb, NULL);
+                                               insert_remat_after(si, remat, bb, live_in);
 
                                        }
                                }
                        }
                }
+               del_pset(live_in);
        }
 }
 
@@ -1341,7 +1371,7 @@ luke_endwalker(ir_node * bb, void * data)
                        ilp_cst_t   rel_cst;
 
                        ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
-                       reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 0.0);
+                       reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 1.0);
                        set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
 
                        /* reload <= mem_out */
@@ -2305,7 +2335,7 @@ skip_one_must_die:
                        spill->mem_in   = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, default_spilled);
                        lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
 
-                       if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
+                       if(opt_memcopies && is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
                                int   n;
                                op_t *op = get_irn_link(spill->irn);
 
@@ -2453,7 +2483,9 @@ skip_one_must_die:
                                        assert(spill_p);
 
                                        lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
-                                       lpp_set_factor_fast(si->lpp, mem_in, op->attr.live_range.args.copies[n], -1.0);
+                                       if(opt_memcopies)
+                                               lpp_set_factor_fast(si->lpp, mem_in, op->attr.live_range.args.copies[n], -1.0);
+
                                        lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
                                }
                        }
@@ -2646,6 +2678,7 @@ values_interfere_in_block(const spill_ilp_t * si, const ir_node * bb, const ir_n
                if(get_nodes_block(user) == bb
                                && !is_Phi(user)
                                && b != user
+                               && !pset_find_ptr(si->inverse_ops, user)
                                && value_dominates(b, user))
                        return 1;
        }
@@ -2669,7 +2702,7 @@ luke_interferencewalker(ir_node * bb, void * data)
 
                /* a is only interesting if it is in my register class and if it is inside a phi class */
                if (has_reg_class(si, a) && get_phi_class(a)) {
-                       if(a_op->is_remat)
+                       if(a_op->is_remat || pset_find_ptr(si->inverse_ops, a))
                                continue;
 
                        for(l2=_be_lv_next_irn(si->lv, bb, 0xff, l1+1); l2>=0; l2=_be_lv_next_irn(si->lv, bb, 0xff, l2+1)) {
@@ -2679,7 +2712,7 @@ luke_interferencewalker(ir_node * bb, void * data)
 
                                /* a and b are only interesting if they are in the same phi class */
                                if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
-                                       if(b_op->is_remat)
+                                       if(b_op->is_remat || pset_find_ptr(si->inverse_ops, b))
                                                continue;
 
                                        if(values_interfere_in_block(si, bb, a, b)) {
@@ -2757,7 +2790,7 @@ find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, il
                                paths += find_copy_path(si, arg, target, any_interfere, copies, visited);
                                pset_remove(copies, INT_TO_PTR(copy), copy);
 
-                /*if(paths > MAX_PATHS) {
+                if(paths > MAX_PATHS) {
                     if(pset_count(copies) == 0) {
                         ilp_cst_t  cst;
                         char       buf[256];
@@ -2777,7 +2810,7 @@ find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, il
                     }
                 } else if(pset_count(copies) == 0) {
                                        paths = 0;
-                               }*/
+                               }
                        }
                }
 
@@ -2812,7 +2845,7 @@ find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, il
                        paths += find_copy_path(si, user, target, any_interfere, copies, visited);
                        pset_remove(copies, INT_TO_PTR(copy), copy);
 
-            /*if(paths > MAX_PATHS) {
+            if(paths > MAX_PATHS) {
                 if(pset_count(copies) == 0) {
                     ilp_cst_t  cst;
                     char       buf[256];
@@ -2831,7 +2864,7 @@ find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, il
                 }
             } else if(pset_count(copies) == 0) {
                                paths = 0;
-                       }*/
+                       }
                }
        }
 
@@ -3206,8 +3239,8 @@ delete_unnecessary_remats(spill_ilp_t * si)
                ir_node  *bad = get_irg_bad(si->chordal_env->irg);
 
                if(si->keep) {
-                       ir_node   *end = get_irg_end(si->chordal_env->irg);
-                       ir_node  **keeps;
+//                     ir_node   *end = get_irg_end(si->chordal_env->irg);
+//                     ir_node  **keeps;
 
                        for (n=get_irn_arity(si->keep)-1; n>=0; --n) {
                                ir_node        *keep_arg = get_irn_n(si->keep, n);
@@ -3307,7 +3340,6 @@ get_spills_for_value(spill_ilp_t * si, const ir_node * value)
 /**
  * @param before   The node after which the spill will be placed in the schedule
  */
-/* TODO set context properly */
 static ir_node *
 insert_spill(spill_ilp_t * si, ir_node * irn, const ir_node * value, ir_node * before)
 {
@@ -3358,7 +3390,9 @@ insert_mem_phi(spill_ilp_t * si, ir_node * phi)
        set_irn_link(mem_phi, defs->spills);
        defs->spills = mem_phi;
 
+#ifdef SCHEDULE_PHIM
        sched_add_after(phi, mem_phi);
+#endif
 
        if(opt_keep_alive & KEEPALIVE_SPILLS)
                pset_insert_ptr(si->spills, mem_phi);