fixed some bugs
[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  * - Two-address code instructions
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #include <libcore/lc_timing.h>
18
19 #include "pmap.h"
20 #include "debug.h"
21 #include "irouts.h"
22 #include "bearch.h"
23 #include "becopyopt.h"
24 #include "becopystat.h"
25 #include "becopyoptmain.h"
26 #include "phiclass.h"
27
28 #define DO_HEUR
29 #undef DO_CLASSES
30 #undef DO_ILP
31
32 static firm_dbg_module_t *dbg = NULL;
33
34
35 /**
36  * Helpers for saving and restoring colors of nodes.
37  * Used to get dependable and comparable benchmark results.
38  */
39 #if (defined(DO_HEUR) && defined(DO_BETTER)) || (defined(DO_HEUR) && defined(DO_ILP)) || (defined(DO_BETTER) && defined(DO_ILP))
40
41 typedef struct color_saver {
42         arch_env_t *arch_env;
43         be_chordal_env_t *chordal_env;
44         pmap *saved_colors;
45         int flag; /* 0 save, 1 load */
46 } color_save_t;
47
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);
54                 } else { /*load */
55                         arch_register_t *reg = pmap_get(saver->saved_colors, irn);
56                         arch_set_irn_register(saver->arch_env, irn, reg);
57                 }
58         }
59 }
60
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);
64 }
65
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);
69 }
70
71 #endif /* Need save/load stuff */
72
73
74
75 void be_copy_opt_init(void) {
76         dbg = firm_dbg_register("ir.be.copyoptmain");
77 }
78
79
80
81 void be_copy_opt(be_chordal_env_t *chordal_env) {
82         copy_opt_t *co;
83
84 #ifdef DO_STAT
85         lc_timer_t *timer;
86         color_save_t saver;
87         int costs, costs_inevit, costs_init, costs_heur, costs_classes, costs_ilp, lower_bound;
88 #endif
89
90
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);
94
95
96 #ifdef DO_STAT
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();
101                 save_colors(&saver);
102 #endif
103
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);
107
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));
111
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));
115 #endif
116
117
118 #ifdef DO_HEUR
119 #ifdef DO_STAT
120         timer = lc_timer_register("heur", NULL);
121         lc_timer_reset_and_start(timer);
122 #endif
123
124         co_heur_opt(co);
125
126 #ifdef DO_STAT
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);
133 #endif
134 #endif /* DO_HEUR */
135
136
137
138 #ifdef DO_CLASSES
139 #ifdef DO_STAT
140 #ifdef DO_HEUR
141         load_colors(&saver);
142 #endif
143         timer = lc_timer_register("classes", NULL);
144         lc_timer_reset_and_start(timer);
145 #endif
146
147         co_classes_opt(co);
148
149 #ifdef DO_STAT
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);
156 #endif
157 #endif /* DO_CLASSES */
158
159
160
161 #ifdef DO_ILP
162 #ifdef DO_STAT
163 #if defined(DO_HEUR) || defined(DO_CLASSES)
164         load_colors(&saver);
165 #endif
166 #endif
167
168         co_ilp_opt(co, 60.0);
169
170 #ifdef DO_STAT
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);
175 #endif
176 #endif /* DO_ILP */
177
178
179 #ifdef DO_STAT
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);
182 #endif
183 #endif
184         free_copy_opt(co);
185 }