Added phi perm inserter
[libfirm] / ir / be / becopyopt.c
index c544e9c..a85fd97 100644 (file)
 #include "becopyopt.h"
 #include "becopystat.h"
 
-#define DEBUG_LVL 0 //SET_LEVEL_1
 static firm_dbg_module_t *dbg = NULL;
 
 #define is_curr_reg_class(irn) (arch_get_irn_reg_class(co->chordal_env->arch_env, irn, arch_pos_make_out(0)) == co->chordal_env->cls)
 
 #define MIN(a,b) ((a<b)?(a):(b))
+#define MAX(a,b) ((a<b)?(b):(a))
 
 /**
  * Computes the weight of a 'max independent set' wrt. ifg-edges only
@@ -50,8 +50,13 @@ static int ou_max_ind_set_costs(unit_t *ou) {
        while ((max = bitset_popcnt(curr)) != 0) {
                /* check if curr is a stable set */
                int i, o, is_stable_set = 1;
+
+               /* copy the irns */
+               i = 0;
                bitset_foreach(curr, pos)
-                       irns[pos] = ou->nodes[1+pos];
+                       irns[i++] = ou->nodes[1+pos];
+               assert(i==max);
+
                for(i=0; i<max; ++i)
                        for(o=i+1; o<max; ++o) /* !!!!! difference to qnode_max_ind_set(): NOT o=i */
                                if (nodes_interfere(ou->co->chordal_env, irns[i], irns[o])) {
@@ -99,45 +104,44 @@ static void co_append_unit(copy_opt_t *co, ir_node *root) {
        arity = get_irn_arity(root);
        unit = xcalloc(1, sizeof(*unit));
        unit->co = co;
-       unit->node_count = 1;
        unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
        unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
+       unit->node_count = 1;
        unit->nodes[0] = root;
-       unit->complete_costs = 0;
-       unit->avg_costs = 0;
        INIT_LIST_HEAD(&unit->queue);
 
        /* check all args */
        if (is_Phi(root)) {
                for (i=0; i<arity; ++i) {
+                       int o, arg_pos = 0;
                        ir_node *arg = get_irn_n(root, i);
+
                        assert(is_curr_reg_class(arg) && "Argument not in same register class.");
-                       if (arg != root) {
-                               int o, arg_pos = 0;
-                               if (nodes_interfere(co->chordal_env, root, arg))
-                                       assert(0 && "root and arg interfere");
-                               //TODO do not insert duplicate args
-                               DBG((dbg, LEVEL_1, "\t   Member: %n %N\n", arg, arg));
-
-                               /* check if arg has occurred at a prior position in the arg/list */
-                               for (o=0; o<unit->node_count; ++o)
-                                       if (unit->nodes[o] == arg) {
-                                               arg_pos = o;
-                                               break;
-                                       }
-
-                               if (!arg_pos) { /* a new argument */
-                                       /* TODO Think about the next 2 lines. (inserting in arg-order) */
-                                       if (is_optimizable(co->chordal_env->arch_env, arg))
-                                               co_append_unit(co, arg);
-                                       /* insert node, set costs */
-                                       unit->nodes[unit->node_count] = arg;
-                                       unit->costs[unit->node_count] = co->get_costs(root, arg, i);
-                                       unit->node_count++;
-                               } else { /* arg has occured before in same phi */
-                                       /* increase costs for existing arg */
-                                       unit->costs[arg_pos] = co->get_costs(root, arg, i);
+                       if (arg == root)
+                               continue;
+                       if (nodes_interfere(co->chordal_env, root, arg)) {
+                               unit->inevitable_costs += co->get_costs(root, arg, i);
+                               continue;
+                       }
+
+                       /* Else insert the argument of the phi to the members of this ou */
+                       DBG((dbg, LEVEL_1, "\t   Member: %n %N\n", arg, arg));
+
+                       /* Check if arg has occurred at a prior position in the arg/list */
+                       for (o=0; o<unit->node_count; ++o)
+                               if (unit->nodes[o] == arg) {
+                                       arg_pos = o;
+                                       break;
                                }
+
+                       if (!arg_pos) { /* a new argument */
+                               /* insert node, set costs */
+                               unit->nodes[unit->node_count] = arg;
+                               unit->costs[unit->node_count] = co->get_costs(root, arg, i);
+                               unit->node_count++;
+                       } else { /* arg has occured before in same phi */
+                               /* increase costs for existing arg */
+                               unit->costs[arg_pos] += co->get_costs(root, arg, i);
                        }
                }
                unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
@@ -154,20 +158,20 @@ static void co_append_unit(copy_opt_t *co, ir_node *root) {
        /* TODO add ou's for 2-addr-code instructions */
 
 
-       for(i=1; i<unit->node_count; ++i)
-               unit->complete_costs += unit->costs[i];
+       /* Determine the maximum costs this unit can cause: all_nodes_cost */
+       for(i=1; i<unit->node_count; ++i) {
+               unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
+               unit->all_nodes_costs += unit->costs[i];
+       }
 
-       assert(unit->node_count > 1);
-       unit->avg_costs = (100 * unit->complete_costs) / (unit->node_count-1);
+       /* Determine the minimal costs this unit will cause: min_nodes_costs */
+       unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
 
-       /* insert according to average costs */
+       /* Insert the new ou according to its sort_key */
        tmp = &co->units;
-       while (tmp->next != &co->units && list_entry_units(tmp->next)->avg_costs > unit->avg_costs)
+       while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
                tmp = tmp->next;
        list_add(&unit->units, tmp);
-
-       /* Init ifg_mis_size to node_count. So get_lower_bound returns correct results. */
-       unit->minimal_costs = unit->complete_costs - ou_max_ind_set_costs(unit);
 }
 
 static void co_collect_in_block(ir_node *block, void *env) {
@@ -193,7 +197,7 @@ copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node
        copy_opt_t *co;
 
        dbg = firm_dbg_register("ir.be.copyopt");
-       firm_dbg_set_mask(dbg, DEBUG_LVL);
+       firm_dbg_set_mask(dbg, DEBUG_LVL_CO);
 
        co = xcalloc(1, sizeof(*co));
        co->chordal_env = chordal_env;
@@ -206,9 +210,9 @@ copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node
        co->name = xmalloc(len);
        snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
        if (!strcmp(co->name, DEBUG_IRG))
-               firm_dbg_set_mask(dbg, DEBUG_LVL_CO);
+               firm_dbg_set_mask(dbg, DEBUG_IRG_LVL_CO);
        else
-               firm_dbg_set_mask(dbg, DEBUG_LVL);
+               firm_dbg_set_mask(dbg, DEBUG_LVL_CO);
 
        INIT_LIST_HEAD(&co->units);
        co_collect_units(co);
@@ -259,13 +263,18 @@ int get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
 int co_get_copy_costs(const copy_opt_t *co) {
        int i, res = 0;
        unit_t *curr;
+
        list_for_each_entry(unit_t, curr, &co->units, units) {
                int root_col = get_irn_col(co, curr->nodes[0]);
-               for (i=1; i<curr->node_count; ++i)
-                       if (root_col != get_irn_col(co, curr->nodes[i])) {
-                               DBG((dbg, LEVEL_1, "  %n %N\n", curr->nodes[i], curr->nodes[i]));
+               res += curr->inevitable_costs;
+               DBG((dbg, LEVEL_1, "  Adding costs for root %+F color %d\n", curr->nodes[0], root_col));
+               for (i=1; i<curr->node_count; ++i) {
+                       int arg_col = get_irn_col(co, curr->nodes[i]);
+                       if (root_col != arg_col) {
+                               DBG((dbg, LEVEL_1, "  Arg %+F color %d costs %d\n", curr->nodes[i], arg_col, curr->costs[i]));
                                res += curr->costs[i];
                        }
+               }
        }
        return res;
 }
@@ -274,6 +283,6 @@ int co_get_lower_bound(const copy_opt_t *co) {
        int res = 0;
        unit_t *curr;
        list_for_each_entry(unit_t, curr, &co->units, units)
-               res += curr->minimal_costs;
+               res += curr->inevitable_costs + curr->min_nodes_costs;
        return res;
 }