4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
7 * Main file for the optimization reducing the copies needed for:
9 * - register-constrained nodes
15 #include <libcore/lc_timing.h>
19 #include "becopyopt.h"
20 #include "becopystat.h"
21 #include "becopyoptmain.h"
28 #define DEBUG_LVL SET_LEVEL_0
29 static firm_dbg_module_t *dbg = NULL;
31 void be_copy_opt_init(void) {
32 dbg = firm_dbg_register("ir.be.copyoptmain");
33 firm_dbg_set_mask(dbg, DEBUG_LVL);
36 typedef struct color_saver {
38 be_chordal_env_t *chordal_env;
40 int flag; /* 0 save; 1 load */
43 static void save_load(ir_node *irn, void *env) {
44 color_save_t *saver = env;
45 if (saver->chordal_env->cls == arch_get_irn_reg_class(saver->arch_env, irn, arch_pos_make_out(0))) {
46 if (saver->flag == 0) { /* save */
47 const arch_register_t *reg = arch_get_irn_register(saver->arch_env, irn, 0);
48 pmap_insert(saver->saved_colors, irn, (void *) reg);
50 arch_register_t *reg = pmap_get(saver->saved_colors, irn);
51 arch_set_irn_register(saver->arch_env, irn, 0, reg);
56 static void save_colors(color_save_t *color_saver) {
57 color_saver->flag = 0;
58 irg_walk_graph(color_saver->chordal_env->session_env->irg, save_load, NULL, color_saver);
61 static void load_colors(color_save_t *color_saver) {
62 color_saver->flag = 1;
63 irg_walk_graph(color_saver->chordal_env->session_env->irg, save_load, NULL, color_saver);
66 void be_copy_opt(be_chordal_env_t *chordal_env) {
68 int costs, costs_init=-1, costs_heur=-1, costs_ilp_n_sec=-1, costs_ilp=-1;
71 saver.arch_env = chordal_env->session_env->main_env->arch_env;
72 saver.chordal_env = chordal_env;
73 saver.saved_colors = pmap_create();
75 /* BETTER: You can remove this if you replace all
76 * `grep get_irn_out *.c` by the irouts.h module.*/
77 compute_outs(chordal_env->session_env->irg);
79 co = new_copy_opt(chordal_env, get_costs_loop_depth);
80 DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
81 phi_class_compute(chordal_env->session_env->irg);
84 lower_bound = co_get_lower_bound(co);
85 DBG((dbg, LEVEL_1, "Lower Bound: %3d\n", lower_bound));
86 DBG((dbg, LEVEL_1, "Inevit Costs: %3d\n", co_get_inevit_copy_costs(co)));
88 costs = co_get_copy_costs(co);
90 copystat_add_max_costs(co_get_max_copy_costs(co));
91 copystat_add_inevit_costs(co_get_inevit_copy_costs(co));
92 copystat_add_init_costs(costs_init);
93 DBG((dbg, LEVEL_1, "Init costs: %3d\n", costs_init));
99 lc_timer_t *timer = lc_timer_register("heur", NULL);
100 lc_timer_reset_and_start(timer);
102 lc_timer_stop(timer);
103 copystat_add_heur_time(lc_timer_elapsed_msec(timer));
105 costs = co_get_copy_costs(co);
107 copystat_add_heur_costs(costs_heur);
108 DBG((dbg, LEVEL_1, "Heur costs: %3d\n", costs_heur));
110 assert(lower_bound == -1 || costs_heur == -1 || lower_bound <= costs_heur);
118 costs = co_get_copy_costs(co);
119 costs_ilp_n_sec = costs;
120 copystat_add_ilp_n_sec_costs(costs_ilp_n_sec);
121 DBG((dbg, LEVEL_1, "N_Sec costs: %3d\n", costs_ilp_n_sec));
123 assert(lower_bound == -1 || costs_ilp == -1 || lower_bound <= costs_ilp);
124 assert(costs_ilp == -1 || costs_heur == -1 || costs_ilp <= costs_heur);
132 costs = co_get_copy_costs(co);
134 copystat_add_opt_costs(costs_ilp);
135 DBG((dbg, LEVEL_1, "Opt costs: %3d\n", costs_ilp));
137 assert(lower_bound == -1 || costs_ilp == -1 || lower_bound <= costs_ilp);
138 assert(costs_ilp == -1 || costs_heur == -1 || costs_ilp <= costs_heur);
141 pmap_destroy(saver.saved_colors);