copy register on benode lowering to Load
[libfirm] / ir / be / becopyopt.c
index 239ef03..06142dc 100644 (file)
 #include <malloc.h>
 #endif
 
+#include "xmalloc.h"
+#include "debug.h"
+#include "pmap.h"
+#include "irgraph.h"
+#include "irgwalk.h"
 #include "irprog.h"
 #include "irloop_t.h"
+#include "iredges_t.h"
+#include "phiclass.h"
 
-#include "xmalloc.h"
+#include "bearch.h"
 #include "beutil.h"
-#include "bechordal_t.h"
-#include "becopyopt.h"
+#include "becopyopt_t.h"
 #include "becopystat.h"
 
-static firm_dbg_module_t *dbg = NULL;
-
-#define is_curr_reg_class(irn) \
-  (arch_get_irn_reg_class(get_arch_env(co), \
-                          irn, -1) == co->chordal_env->cls)
-
-#define MIN(a,b) ((a<b)?(a):(b))
-#define MAX(a,b) ((a<b)?(b):(a))
 
+static firm_dbg_module_t *dbg = NULL;
 
 /**
  * Determines a maximum weighted independent set with respect to
  * the interference and conflict edges of all nodes in a qnode.
  */
 static int ou_max_ind_set_costs(unit_t *ou) {
-       be_chordal_env_t *chordal_env = ou->co->chordal_env;
+       be_chordal_env_t *chordal_env = ou->co->cenv;
        ir_node **safe, **unsafe;
        int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
        bitset_t *curr;
@@ -124,11 +123,10 @@ static void co_collect_units(ir_node *irn, void *env) {
        copy_opt_t *co = env;
        unit_t *unit;
        arch_register_req_t req;
-       arch_env_t *aenv = co->chordal_env->main_env->arch_env;
 
-       if (!is_curr_reg_class(irn))
+       if (!is_curr_reg_class(co, irn))
                return;
-       if (!is_optimizable(get_arch_env(co), irn, &req))
+       if (!co_is_optimizable(co->aenv, irn, &req))
                return;
 
        /* Init a new unit */
@@ -152,10 +150,10 @@ static void co_collect_units(ir_node *irn, void *env) {
                        int o, arg_pos;
                        ir_node *arg = get_irn_n(irn, i);
 
-                       assert(is_curr_reg_class(arg) && "Argument not in same register class.");
+                       assert(is_curr_reg_class(co, arg) && "Argument not in same register class.");
                        if (arg == irn)
                                continue;
-                       if (nodes_interfere(co->chordal_env, irn, arg)) {
+                       if (nodes_interfere(co->cenv, irn, arg)) {
                                unit->inevitable_costs += co->get_costs(irn, arg, i);
                                continue;
                        }
@@ -186,8 +184,8 @@ static void co_collect_units(ir_node *irn, void *env) {
        } else
 
        /* Proj of a perm with corresponding arg */
-       if (is_Perm_Proj(get_arch_env(co), irn)) {
-               assert(!nodes_interfere(co->chordal_env, irn, get_Copy_src(irn)));
+       if (is_Perm_Proj(co->aenv, irn)) {
+               assert(!nodes_interfere(co->cenv, irn, get_Copy_src(irn)));
                unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
                unit->costs = xmalloc(2 * sizeof(*unit->costs));
                unit->node_count = 2;
@@ -197,9 +195,9 @@ static void co_collect_units(ir_node *irn, void *env) {
        } else
 
        /* Src == Tgt of a 2-addr-code instruction */
-       if (is_2addr_code(get_arch_env(co), irn, &req)) {
+       if (is_2addr_code(co->aenv, irn, &req)) {
                ir_node *other = req.other_same;
-               if (!nodes_interfere(co->chordal_env, irn, other)) {
+               if (!nodes_interfere(co->cenv, irn, other)) {
                        unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
                        unit->costs = xmalloc(2 * sizeof(*unit->costs));
                        unit->node_count = 2;
@@ -234,6 +232,10 @@ static void co_collect_units(ir_node *irn, void *env) {
        }
 }
 
+void be_copy_opt_init(void) {
+       dbg = firm_dbg_register("ir.be.copyoptmain");
+}
+
 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int)) {
        const char *s1, *s2, *s3;
        int len;
@@ -242,11 +244,14 @@ copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node
        dbg = firm_dbg_register("ir.be.copyopt");
 
        co = xcalloc(1, sizeof(*co));
-       co->chordal_env = chordal_env;
+       co->cenv = chordal_env;
+       co->aenv = chordal_env->birg->main_env->arch_env;
+       co->irg = chordal_env->irg;
+       co->cls = chordal_env->cls;
        co->get_costs = get_costs;
 
        s1 = get_irp_prog_name();
-       s2 = get_entity_name(get_irg_entity(get_irg(co)));
+       s2 = get_entity_name(get_irg_entity(co->irg));
        s3 = chordal_env->cls->name;
        len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
        co->name = xmalloc(len);
@@ -254,7 +259,7 @@ copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node
 
        DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
        INIT_LIST_HEAD(&co->units);
-       irg_walk_graph(get_irg(co), co_collect_units, NULL, co);
+       irg_walk_graph(co->irg, co_collect_units, NULL, co);
        return co;
 }
 
@@ -268,22 +273,21 @@ void free_copy_opt(copy_opt_t *co) {
        }
 }
 
-int is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
-       arch_env_t *aenv = get_arch_env(co);
+int co_is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
        const ir_edge_t *edge;
 
-       if (arch_irn_is_ignore(aenv, irn))
+       if (arch_irn_is_ignore(co->aenv, irn))
                return 0;
 
        foreach_out_edge(irn, edge) {
                ir_node *n = edge->src;
 
-               if (!nodes_interfere(co->chordal_env, irn, n) || irn == n) {
+               if (!nodes_interfere(co->cenv, irn, n) || irn == n) {
                        arch_register_req_t req;
-                       arch_get_register_req(aenv, &req, n, -1);
+                       arch_get_register_req(co->aenv, &req, n, -1);
 
                        if(is_Reg_Phi(n) ||
-                          is_Perm(aenv, n) ||
+                          is_Perm(co->aenv, n) ||
                           (arch_register_req_is(&req, should_be_same) && req.other_same == irn)
                          )
                                return 1;
@@ -293,7 +297,7 @@ int is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
        return 0;
 }
 
-int get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
+int co_get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
        int cost = 0;
        ir_loop *loop;
        ir_node *root_block = get_nodes_block(root);
@@ -312,7 +316,7 @@ int get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
        return cost+1;
 }
 
-int get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
+int co_get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
        return 1;
 }