Fixed a bug
[libfirm] / ir / be / becopyoptmain.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                11.04.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6
7  * Main file for the optimization reducing the copies needed for:
8  * - phi coalescing
9  * - register-constrained nodes
10  */
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include <libcore/lc_timing.h>
16
17 #include "pmap.h"
18 #include "debug.h"
19 #include "irouts.h"
20 #include "bearch.h"
21 #include "becopyopt.h"
22 #include "becopystat.h"
23 #include "becopyoptmain.h"
24 #include "phiclass.h"
25
26 #define DO_HEUR
27 #undef DO_ILP_5_SEC
28 #undef DO_ILP_30_SEC
29 #undef DO_ILP
30
31 #define DEBUG_LVL SET_LEVEL_1
32 static firm_dbg_module_t *dbg = NULL;
33
34 void be_copy_opt_init(void) {
35         dbg = firm_dbg_register("ir.be.copyoptmain");
36         firm_dbg_set_mask(dbg, DEBUG_LVL);
37 }
38
39 typedef struct color_saver {
40         arch_env_t *arch_env;
41         be_chordal_env_t *chordal_env;
42         pmap *saved_colors;
43         int flag;  /* 0 save; 1 load */
44 } color_save_t;
45
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);
52                 } else { /*load */
53                         arch_register_t *reg = pmap_get(saver->saved_colors, irn);
54                         arch_set_irn_register(saver->arch_env, irn, reg);
55                 }
56         }
57 }
58
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);
62 }
63
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);
67 }
68
69 void be_copy_opt(be_chordal_env_t *chordal_env) {
70         copy_opt_t *co;
71         int costs, costs_init=-1, costs_heur=-1, costs_ilp_5_sec=-1, costs_ilp_30_sec=-1, costs_ilp=-1;
72         int lower_bound = -1;
73         int was_optimal = 0;
74
75         color_save_t saver;
76         saver.arch_env = chordal_env->main_env->arch_env;
77         saver.chordal_env = chordal_env;
78         saver.saved_colors = pmap_create();
79
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);
83
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);
87
88 #ifdef DO_STAT
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)));
92
93         costs = co_get_copy_costs(co);
94         costs_init = costs;
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));
99 #endif
100
101 //      save_colors(&saver);
102
103 #ifdef DO_HEUR
104         {
105                 lc_timer_t *timer = lc_timer_register("heur", NULL);
106                 lc_timer_reset_and_start(timer);
107                 co_heur_opt(co);
108                 lc_timer_stop(timer);
109                 copystat_add_heur_time(lc_timer_elapsed_msec(timer));
110         }
111 #ifdef DO_STAT
112         costs = co_get_copy_costs(co);
113         costs_heur = costs;
114         copystat_add_heur_costs(costs_heur);
115         DBG((dbg, LEVEL_1, "Heur costs: %3d\n", costs_heur));
116 #endif
117         assert(lower_bound == -1 || costs_heur == -1 || lower_bound <= costs_heur);
118 #endif
119
120 #ifdef DO_ILP_5_SEC
121         load_colors(&saver);
122         was_optimal = co_ilp_opt(co, 5.0);
123 #ifdef DO_STAT
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));
128 #endif
129 #endif
130
131 #ifdef DO_ILP_30_SEC
132         if (!was_optimal) {
133                 load_colors(&saver);
134                 co_ilp_opt(co, 30.0);
135         }
136 #ifdef DO_STAT
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));
141 #endif
142 #endif
143
144 #ifdef DO_ILP
145         load_colors(&saver);
146         co_ilp_opt(co, 60.0);
147 #ifdef DO_STAT
148         costs = co_get_copy_costs(co);
149         costs_ilp = costs;
150         copystat_add_opt_costs(costs_ilp);
151         DBG((dbg, LEVEL_1, "Opt  costs: %3d\n", costs_ilp));
152 #endif
153         assert(lower_bound == -1 || costs_ilp == -1 || lower_bound <= costs_ilp);
154 #endif
155
156         pmap_destroy(saver.saved_colors);
157         free_copy_opt(co);
158 }