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
10 * - Two-address code instructions
17 #include <libcore/lc_timing.h>
23 #include "becopyopt.h"
24 #include "becopystat.h"
25 #include "becopyoptmain.h"
32 static firm_dbg_module_t *dbg = NULL;
36 * Helpers for saving and restoring colors of nodes.
37 * Used to get dependable and comparable benchmark results.
39 #if (defined(DO_HEUR) && defined(DO_BETTER)) || (defined(DO_HEUR) && defined(DO_ILP)) || (defined(DO_BETTER) && defined(DO_ILP))
41 typedef struct color_saver {
43 be_chordal_env_t *chordal_env;
45 int flag; /* 0 save, 1 load */
48 static void save_load(ir_node *irn, void *env) {
49 color_save_t *saver = env;
50 if (saver->chordal_env->cls == arch_get_irn_reg_class(saver->arch_env, irn, -1)) {
51 if (saver->flag == 0) { /* save */
52 const arch_register_t *reg = arch_get_irn_register(saver->arch_env, irn);
53 pmap_insert(saver->saved_colors, irn, (void *) reg);
55 arch_register_t *reg = pmap_get(saver->saved_colors, irn);
56 arch_set_irn_register(saver->arch_env, irn, reg);
61 static void save_colors(color_save_t *color_saver) {
62 color_saver->flag = 0;
63 irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
66 static void load_colors(color_save_t *color_saver) {
67 color_saver->flag = 1;
68 irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
71 #endif /* Need save/load stuff */
75 void be_copy_opt_init(void) {
76 dbg = firm_dbg_register("ir.be.copyoptmain");
81 void be_copy_opt(be_chordal_env_t *chordal_env) {
87 int costs, costs_inevit, costs_init, costs_heur, costs_classes, costs_ilp, lower_bound;
91 co = new_copy_opt(chordal_env, get_costs_loop_depth);
92 DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
93 phi_class_compute(chordal_env->irg);
97 #if (defined(DO_HEUR) && defined(DO_BETTER)) || (defined(DO_HEUR) && defined(DO_ILP)) || (defined(DO_BETTER) && defined(DO_ILP))
98 saver.arch_env = chordal_env->main_env->arch_env;
99 saver.chordal_env = chordal_env;
100 saver.saved_colors = pmap_create();
104 costs_inevit = co_get_inevit_copy_costs(co);
105 lower_bound = co_get_lower_bound(co);
106 costs_init = co_get_copy_costs(co);
108 DBG((dbg, LEVEL_1, "Inevit Costs: %3d\n", costs_inevit));
109 DBG((dbg, LEVEL_1, "Lower Bound: %3d\n", lower_bound));
110 DBG((dbg, LEVEL_1, "Init costs: %3d\n", costs_init));
112 copystat_add_inevit_costs(costs_inevit);
113 copystat_add_init_costs(costs_init);
114 copystat_add_max_costs(co_get_max_copy_costs(co));
120 timer = lc_timer_register("heur", NULL);
121 lc_timer_reset_and_start(timer);
127 lc_timer_stop(timer);
128 costs_heur = co_get_copy_costs(co);
129 DBG((dbg, LEVEL_1, "Heur costs: %3d\n", costs_heur));
130 copystat_add_heur_time(lc_timer_elapsed_msec(timer));
131 copystat_add_heur_costs(costs_heur);
132 assert(lower_bound <= costs_heur);
143 timer = lc_timer_register("classes", NULL);
144 lc_timer_reset_and_start(timer);
150 lc_timer_stop(timer);
151 costs_classes = co_get_copy_costs(co);
152 DBG((dbg, LEVEL_1, "Classes costs: %3d\n", costs_classes));
153 copystat_add_classes_time(lc_timer_elapsed_msec(timer));
154 copystat_add_classes_costs(costs_heur);
155 assert(lower_bound <= costs_classes);
157 #endif /* DO_CLASSES */
163 #if defined(DO_HEUR) || defined(DO_CLASSES)
168 co_ilp_opt(co, 60.0);
171 costs_ilp = co_get_copy_costs(co);
172 DBG((dbg, LEVEL_1, "Opt costs: %3d\n", costs_ilp));
173 copystat_add_opt_costs(costs_ilp);
174 assert(lower_bound <= costs_ilp);
180 #if (defined(DO_HEUR) && defined(DO_BETTER)) || (defined(DO_HEUR) && defined(DO_ILP)) || (defined(DO_BETTER) && defined(DO_ILP))
181 pmap_destroy(saver.saved_colors);