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>
21 #include "becopyopt.h"
22 #include "becopystat.h"
23 #include "becopyoptmain.h"
31 #define DEBUG_LVL SET_LEVEL_1
32 static firm_dbg_module_t *dbg = NULL;
34 void be_copy_opt_init(void) {
35 dbg = firm_dbg_register("ir.be.copyoptmain");
36 firm_dbg_set_mask(dbg, DEBUG_LVL);
39 typedef struct color_saver {
41 be_chordal_env_t *chordal_env;
43 int flag; /* 0 save; 1 load */
46 static void save_load(ir_node *irn, void *env) {
47 color_save_t *saver = env;
48 if (saver->chordal_env->cls == arch_get_irn_reg_class(saver->arch_env, irn, -1)) {
49 if (saver->flag == 0) { /* save */
50 const arch_register_t *reg = arch_get_irn_register(saver->arch_env, irn);
51 pmap_insert(saver->saved_colors, irn, (void *) reg);
53 arch_register_t *reg = pmap_get(saver->saved_colors, irn);
54 arch_set_irn_register(saver->arch_env, irn, reg);
59 static void save_colors(color_save_t *color_saver) {
60 color_saver->flag = 0;
61 irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
64 static void load_colors(color_save_t *color_saver) {
65 color_saver->flag = 1;
66 irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
69 void be_copy_opt(be_chordal_env_t *chordal_env) {
71 int costs, costs_init=-1, costs_heur=-1, costs_ilp_5_sec=-1, costs_ilp_30_sec=-1, costs_ilp=-1;
76 saver.arch_env = chordal_env->main_env->arch_env;
77 saver.chordal_env = chordal_env;
78 saver.saved_colors = pmap_create();
80 /* BETTER: You can remove this if you replace all
81 * `grep get_irn_out *.c` by the irouts.h module.*/
82 compute_irg_outs(chordal_env->irg);
84 co = new_copy_opt(chordal_env, get_costs_loop_depth);
85 DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
86 phi_class_compute(chordal_env->irg);
89 lower_bound = co_get_lower_bound(co);
90 DBG((dbg, LEVEL_1, "Lower Bound: %3d\n", lower_bound));
91 DBG((dbg, LEVEL_1, "Inevit Costs: %3d\n", co_get_inevit_copy_costs(co)));
93 costs = co_get_copy_costs(co);
95 copystat_add_max_costs(co_get_max_copy_costs(co));
96 copystat_add_inevit_costs(co_get_inevit_copy_costs(co));
97 copystat_add_init_costs(costs_init);
98 DBG((dbg, LEVEL_1, "Init costs: %3d\n", costs_init));
101 // save_colors(&saver);
105 lc_timer_t *timer = lc_timer_register("heur", NULL);
106 lc_timer_reset_and_start(timer);
108 lc_timer_stop(timer);
109 copystat_add_heur_time(lc_timer_elapsed_msec(timer));
112 costs = co_get_copy_costs(co);
114 copystat_add_heur_costs(costs_heur);
115 DBG((dbg, LEVEL_1, "Heur costs: %3d\n", costs_heur));
117 assert(lower_bound == -1 || costs_heur == -1 || lower_bound <= costs_heur);
122 was_optimal = co_ilp_opt(co, 5.0);
124 costs = co_get_copy_costs(co);
125 costs_ilp_5_sec = costs;
126 copystat_add_ilp_5_sec_costs(costs_ilp_5_sec);
127 DBG((dbg, LEVEL_1, "5_Sec costs: %3d\n", costs_ilp_5_sec));
134 co_ilp_opt(co, 30.0);
137 costs = co_get_copy_costs(co);
138 costs_ilp_30_sec = costs;
139 copystat_add_ilp_30_sec_costs(costs_ilp_30_sec);
140 DBG((dbg, LEVEL_1, "30_Sec costs: %3d\n", costs_ilp_30_sec));
146 co_ilp_opt(co, 60.0);
148 costs = co_get_copy_costs(co);
150 copystat_add_opt_costs(costs_ilp);
151 DBG((dbg, LEVEL_1, "Opt costs: %3d\n", costs_ilp));
153 assert(lower_bound == -1 || costs_ilp == -1 || lower_bound <= costs_ilp);
156 pmap_destroy(saver.saved_colors);