Do not warn about wrong register for a Phi input if that register is a joker (i.e...
[libfirm] / ir / be / becopystat.c
index affb818..a095a20 100644 (file)
@@ -1,27 +1,54 @@
+/*
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
+ */
+
 /**
- * Author:      Daniel Grund
- * Date:               19.04.2005
- * Copyright:   (c) Universitaet Karlsruhe
- * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+ * @file
+ * @brief       Copy node statistics.
+ * @author      Daniel Grund
+ * @date        19.04.2005
+ * @version     $Id$
  */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
 #include <string.h>
+
+#include "timing.h"
+#include "xmalloc.h"
 #include "irgraph.h"
+#include "irgwalk.h"
 #include "irprog.h"
-#include "iredges.h"
-#include "phiclass_t.h"
+#include "iredges_t.h"
+#include "phiclass.h"
+#include "irnodeset.h"
+
+#include "bechordal_t.h"
 #include "beutil.h"
-#include "becopyopt.h"
+#include "becopyopt_t.h"
 #include "becopystat.h"
-#include "xmalloc.h"
-
-#ifdef DO_STAT
+#include "beirg_t.h"
+#include "bemodule.h"
+#include "beintlive_t.h"
 
 #define DEBUG_LVL SET_LEVEL_1
-static firm_dbg_module_t *dbg = NULL;
+DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
 #define MAX_ARITY 20
 #define MAX_CLS_SIZE 20
@@ -65,8 +92,8 @@ enum vals_t {
        /* ilp values */
        I_HEUR_TIME,            /* solving time in milli seconds */
        I_ILP_TIME,                     /* solving time in milli seconds */
-    I_ILP_VARS,
-    I_ILP_CSTR,
+       I_ILP_VARS,
+       I_ILP_CSTR,
        I_ILP_ITER,                     /* number of simplex iterations */
 
        /* copy instructions */
@@ -86,30 +113,28 @@ enum vals_t {
  */
 int curr_vals[ASIZE];
 
-static pset *all_phi_nodes;
-static pset *all_phi_classes;
-static pset *all_copy_nodes;
+static ir_nodeset_t *all_phi_nodes;
+static ir_nodeset_t *all_copy_nodes;
 static ir_graph *last_irg;
 
-void copystat_init(void) {
-       dbg = firm_dbg_register("ir.be.copystat");
-       firm_dbg_set_mask(dbg, DEBUG_LVL);
+void be_init_copystat(void) {
+       FIRM_DBG_REGISTER(dbg, "firm.be.copystat");
+
+       all_phi_nodes  = ir_nodeset_new(64);
+       all_copy_nodes = ir_nodeset_new(64);
+       memset(curr_vals, 0, sizeof(curr_vals));
+}
+BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copystat);
 
-       all_phi_nodes = pset_new_ptr_default();
-       all_phi_classes = pset_new_ptr_default();
-       all_copy_nodes = pset_new_ptr_default();
+void be_quit_copystat(void) {
+       ir_nodeset_del(all_phi_nodes);
+       ir_nodeset_del(all_copy_nodes);
 }
+BE_REGISTER_MODULE_DESTRUCTOR(be_quit_copystat);
 
 void copystat_reset(void) {
-       int i;
-       for (i = 0; i < ASIZE; ++i)
-               curr_vals[i] = 0;
-       del_pset(all_phi_nodes);
-       del_pset(all_phi_classes);
-       del_pset(all_copy_nodes);
-       all_phi_nodes = pset_new_ptr_default();
-       all_phi_classes = pset_new_ptr_default();
-       all_copy_nodes = pset_new_ptr_default();
+       be_quit_copystat();
+       be_init_copystat();
 }
 
 /**
@@ -119,19 +144,20 @@ static void irg_stat_walker(ir_node *node, void *env) {
        arch_env_t *arch_env = env;
        curr_vals[I_ALL_NODES]++; /* count all nodes */
 
-       if (is_Block(node)) /* count all blocks */
-               curr_vals[I_BLOCKS]++;
+       if (is_Block(node)) /* count all blocks */
+               curr_vals[I_BLOCKS]++;
+
+       if (is_Reg_Phi(node)) /* collect phis */
+               ir_nodeset_insert(all_phi_nodes, node);
 
-       if (is_Phi(node) && is_firm_be_mode(get_irn_mode(node))) /* collect phis */
-               pset_insert_ptr(all_phi_nodes, node);
+       if (is_Perm_Proj(arch_env, node))
+               ir_nodeset_insert(all_copy_nodes, node);
 
-       if (is_Copy(arch_env, node))
-               pset_insert_ptr(all_copy_nodes, node);
+       /* TODO: Add 2-Addr-Code nodes */
 }
 
 static void copystat_collect_irg(ir_graph *irg, arch_env_t *arch_env) {
        irg_walk_graph(irg, irg_stat_walker, NULL, arch_env);
-       all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
        last_irg = irg;
 }
 
@@ -153,7 +179,7 @@ static INLINE int was_edge_critical(const ir_node *bl, int pos) {
        if (get_irn_arity(bl_at_pos) != 1)
                return 0;
 
-       /* Does the pred of the pred have several sucsecessors */
+       /* Does the pred of the pred have several successors */
        bl_before = get_irn_n(bl_at_pos, 0);
        edge = get_block_succ_first(bl_before);
        return get_block_succ_next(bl_before, edge) ? 1 : 0;
@@ -162,10 +188,12 @@ static INLINE int was_edge_critical(const ir_node *bl, int pos) {
 /**
  * Collect phi node data
  */
-static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi) {
+static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi)
+{
        int arity, i;
        ir_node *phi_bl;
        assert(is_Phi(phi));
+       (void) chordal_env;
 
        /* count all phi phis */
        curr_vals[I_PHI_CNT]++;
@@ -215,7 +243,7 @@ static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi) {
 static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
        curr_vals[I_CPY_CNT]++;
        curr_vals[I_COPIES_MAX]++;
-       if (nodes_interfere(chordal_env, root, get_Copy_src(root))) {
+       if (values_interfere(chordal_env->birg, root, get_Perm_src(root))) {
                curr_vals[I_COPIES_IF]++;
                assert(0 && "A Perm pair (in/out) should never interfere!");
        }
@@ -224,83 +252,75 @@ static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
 /**
  * Collect phi class data
  */
-static void stat_phi_class(be_chordal_env_t *chordal_env, pset *pc) {
+static void stat_phi_class(be_chordal_env_t *chordal_env, ir_node **pc) {
        int i, o, size, if_free, phis;
-       ir_node **members, *p;
 
        /* phi class count */
        curr_vals[I_CLS_CNT]++;
 
        /* phi class size */
-       size = pset_count(pc);
+       size = ARR_LEN(pc);
        if (size > MAX_CLS_SIZE)
                curr_vals[I_CLS_SIZE_E]++;
        else
                curr_vals[I_CLS_SIZE_S + size]++;
 
-       /* get an array of all members for double iterating */
-       members = xmalloc(size * sizeof(*members));
-       DBG((dbg, LEVEL_2, "Phi-class:\n"));
-       for (i = 0, p = pset_first(pc); p; p = pset_next(pc)) {
-               DBG((dbg, LEVEL_2, "  %+F\n", p));
-               members[i++] = p;
-       }
-       assert(i == size);
-
        /* determine number of phis on this class */
-       phis = 0;
-       for (i = 0; i < size; ++i)
-               if (is_Phi(members[i]))
+       for (phis = i = 0; i < size; ++i)
+               if (is_Phi(pc[i]))
                        phis++;
+
        if (phis > MAX_CLS_PHIS)
                curr_vals[I_CLS_PHIS_E]++;
        else
                curr_vals[I_CLS_PHIS_S + phis]++;
 
        /* determine interference of phi class members */
-       curr_vals[I_CLS_IF_MAX] += size*(size-1)/2;
-       if_free = 1;
-       for (i = 0; i < size-1; ++i)
-               for (o = i+1; o < size; ++o)
-                       if (nodes_interfere(chordal_env, members[i], members[o])) {
+       curr_vals[I_CLS_IF_MAX] += size * (size - 1) / 2;
+       for (if_free = 1, i = 0; i < size - 1; ++i)
+               for (o = i + 1; o < size; ++o)
+                       if (values_interfere(chordal_env->birg, pc[i], pc[o])) {
                                if_free = 0;
                                curr_vals[I_CLS_IF_CNT]++;
                        }
 
        /* Does this phi class have an inner interference? */
        curr_vals[I_CLS_IF_FREE] += if_free;
-
-       xfree(members);
 }
 
-#define is_curr_reg_class(irn) \
-  (arch_get_irn_reg_class(chordal_env->main_env->arch_env, irn, \
-                          -1) == chordal_env->cls)
+static void copystat_collect_cls(be_chordal_env_t *cenv) {
+       ir_graph              *irg  = cenv->irg;
+       arch_env_t            *aenv = cenv->birg->main_env->arch_env;
+       ir_node               *n, **pc;
+       phi_classes_t         *pc_obj;
+       pset                  *all_phi_classes;
+       ir_nodeset_iterator_t iter;
 
-void copystat_collect_cls(be_chordal_env_t *chordal_env) {
-       ir_node *n;
-       pset *pc;
-       ir_graph *irg = chordal_env->irg;
+       copystat_reset();
+       copystat_collect_irg(irg, aenv);
 
-       if (last_irg != irg) {
-               copystat_reset();
-               copystat_collect_irg(irg, chordal_env->main_env->arch_env);
-       }
+       /* compute the Phi classes of the collected Phis */
+       pc_obj          = phi_class_new_from_set(cenv->irg, all_phi_nodes, 0);
+       all_phi_classes = get_all_phi_classes(pc_obj);
 
-       for (n = pset_first(all_phi_nodes); n; n = pset_next(all_phi_nodes))
-               if (is_curr_reg_class(n))
-                       stat_phi_node(chordal_env, n);
+       foreach_ir_nodeset(all_phi_nodes, n, iter) {
+               if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
+                       stat_phi_node(cenv, n);
+       }
 
-       for (n = pset_first(all_copy_nodes); n; n = pset_next(all_copy_nodes))
-               if (is_curr_reg_class(n))
-                       stat_copy_node(chordal_env, n);
+       foreach_ir_nodeset(all_copy_nodes, n, iter) {
+               if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
+                       stat_copy_node(cenv, n);
+       }
 
-       for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
-               ir_node *member = pset_first(pc);
-               pset_break(pc);
-               if (is_curr_reg_class(member))
-                       stat_phi_class(chordal_env, pc);
+       foreach_pset(all_phi_classes, pc) {
+               ir_node *member = pc[0];
+               if (arch_get_irn_reg_class(aenv, member, -1) == cenv->cls)
+                       stat_phi_class(cenv, pc);
        }
+
+       /* free the phi class object */
+       phi_class_free(pc_obj);
 }
 
 void copystat_add_max_costs(int costs) {
@@ -327,6 +347,9 @@ void copystat_add_opt_costs(int costs) {
 void copystat_add_heur_time(int time) {
        curr_vals[I_HEUR_TIME] += time;
 }
+
+#ifdef WITH_ILP
+
 void copystat_add_ilp_time(int time) {
        curr_vals[I_ILP_TIME] += time;
 }
@@ -340,13 +363,16 @@ void copystat_add_ilp_iter(int iters) {
        curr_vals[I_ILP_ITER] += iters;
 }
 
+#endif /* WITH_ILP */
+
 void copystat_dump(ir_graph *irg) {
        int i;
        char buf[1024];
        FILE *out;
 
        snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
-       out = ffopen(buf, "stat", "wt");
+       buf[sizeof(buf) - 1] = '\0';
+       out = be_ffopen(buf, "stat", "wt");
 
        fprintf(out, "%d\n", ASIZE);
        for (i = 0; i < ASIZE; i++) {
@@ -360,7 +386,7 @@ void copystat_dump(ir_graph *irg) {
                        fprintf(out, "%i\n", curr_vals[i]);
        }
 
-    fclose(out);
+       fclose(out);
 }
 
 void copystat_dump_pretty(ir_graph *irg) {
@@ -369,7 +395,8 @@ void copystat_dump_pretty(ir_graph *irg) {
        FILE *out;
 
        snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
-       out = ffopen(buf, "pstat", "wt");
+       buf[sizeof(buf) - 1] = '\0';
+       out = be_ffopen(buf, "pstat", "wt");
 
        fprintf(out, "Nodes     %4d\n", curr_vals[I_ALL_NODES]);
        fprintf(out, "Blocks    %4d\n", curr_vals[I_BLOCKS]);
@@ -410,4 +437,149 @@ void copystat_dump_pretty(ir_graph *irg) {
        fclose(out);
 }
 
+/**
+ * Helpers for saving and restoring colors of nodes.
+ * Used to get dependable and comparable benchmark results.
+ */
+typedef struct color_saver {
+       arch_env_t *arch_env;
+       be_chordal_env_t *chordal_env;
+       pmap *saved_colors;
+       int flag; /* 0 save, 1 load */
+} color_save_t;
+
+static void save_load(ir_node *irn, void *env) {
+       color_save_t *saver = env;
+       if (saver->chordal_env->cls == arch_get_irn_reg_class(saver->arch_env, irn, -1)) {
+               if (saver->flag == 0) { /* save */
+                       const arch_register_t *reg = arch_get_irn_register(saver->arch_env, irn);
+                       pmap_insert(saver->saved_colors, irn, (void *) reg);
+               } else { /*load */
+                       arch_register_t *reg = pmap_get(saver->saved_colors, irn);
+                       arch_set_irn_register(saver->arch_env, irn, reg);
+               }
+       }
+}
+
+static void save_colors(color_save_t *color_saver) {
+       color_saver->flag = 0;
+       irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
+}
+
+#ifdef WITH_ILP
+static void load_colors(color_save_t *color_saver) {
+       color_saver->flag = 1;
+       irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
+}
 #endif
+
+/**
+ * Main compare routine
+ */
+void co_compare_solvers(be_chordal_env_t *chordal_env) {
+       copy_opt_t    *co;
+       ir_timer_t    *timer;
+       color_save_t  saver;
+       int costs_inevit, costs_init, costs_solved, lower_bound;
+
+       copystat_collect_cls(chordal_env);
+
+       co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
+       co_build_ou_structure(co);
+       co_build_graph_structure(co);
+       DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
+
+       /* save colors */
+       saver.arch_env     = chordal_env->birg->main_env->arch_env;
+       saver.chordal_env  = chordal_env;
+       saver.saved_colors = pmap_create();
+       save_colors(&saver);
+
+       /* initial values */
+       costs_inevit = co_get_inevit_copy_costs(co);
+       lower_bound  = co_get_lower_bound(co);
+       costs_init   = co_get_copy_costs(co);
+
+       DBG((dbg, LEVEL_1, "Inevit Costs: %3d\n", costs_inevit));
+       DBG((dbg, LEVEL_1, "Lower Bound: %3d\n", lower_bound));
+       DBG((dbg, LEVEL_1, "Init costs: %3d\n", costs_init));
+
+       copystat_add_inevit_costs(costs_inevit);
+       copystat_add_init_costs(costs_init);
+       copystat_add_max_costs(co_get_max_copy_costs(co));
+
+       /* heuristic 1 (Daniel Grund) */
+       timer = ir_timer_register("heur1", NULL);
+       ir_timer_reset_and_start(timer);
+
+       co_solve_heuristic(co);
+
+       ir_timer_stop(timer);
+
+       costs_solved = co_get_copy_costs(co);
+       DBG((dbg, LEVEL_1, "HEUR1 costs: %3d\n", costs_solved));
+       copystat_add_heur_time(ir_timer_elapsed_msec(timer));
+       copystat_add_heur_costs(costs_solved);
+       assert(lower_bound <= costs_solved);
+
+       /* heuristic 2 (Sebastian Hack) */
+       timer = ir_timer_register("heur2", NULL);
+       ir_timer_reset_and_start(timer);
+
+       co_solve_heuristic_new(co);
+
+       ir_timer_stop(timer);
+
+       costs_solved = co_get_copy_costs(co);
+       DBG((dbg, LEVEL_1, "HEUR2 costs: %3d\n", costs_solved));
+       copystat_add_heur_time(ir_timer_elapsed_msec(timer));
+       copystat_add_heur_costs(costs_solved);
+       assert(lower_bound <= costs_solved);
+
+       /* Park & Moon register coalescing (Kimon Hoffmann) */
+       timer = ir_timer_register("park", NULL);
+       ir_timer_reset_and_start(timer);
+
+       co_solve_park_moon(co);
+
+       ir_timer_stop(timer);
+
+       costs_solved = co_get_copy_costs(co);
+       DBG((dbg, LEVEL_1, "Park/Moon costs: %3d\n", costs_solved));
+       copystat_add_heur_time(ir_timer_elapsed_msec(timer));
+       copystat_add_heur_costs(costs_solved);
+       assert(lower_bound <= costs_solved);
+
+
+#ifdef WITH_ILP
+
+       /* ILP 1 is not yet implemented, so it makes no sense to compare */
+#if 0
+       load_colors(&saver);
+
+       co_solve_ilp1(co, 60.0);
+
+       costs_solved = co_get_copy_costs(co);
+       DBG((dbg, LEVEL_1, "ILP1 costs: %3d\n", costs_solved));
+       copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
+       assert(lower_bound <= costs_solved);
+#endif /* 0 */
+
+       /* ILP 2 */
+       load_colors(&saver);
+
+       co_solve_ilp2(co);
+
+       costs_solved = co_get_copy_costs(co);
+       DBG((dbg, LEVEL_1, "ILP2 costs: %3d\n", costs_solved));
+       copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
+       assert(lower_bound <= costs_solved);
+
+#endif /* WITH_ILP */
+
+       /* free memory for statistic structures */
+       pmap_destroy(saver.saved_colors);
+       co_free_graph_structure(co);
+       co_free_ou_structure(co);
+       free_copy_opt(co);
+}