Before benchmarking
[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 #include "pmap.h"
17 #include "debug.h"
18 #include "irouts.h"
19 #include "becopyopt.h"
20 #include "becopystat.h"
21 #include "becopyoptmain.h"
22 #include "phiclass.h"
23
24 #define DO_HEUR
25 #define DO_ILP
26 #define DO_ILP_N_SEC
27
28 #define DEBUG_LVL SET_LEVEL_0
29 static firm_dbg_module_t *dbg = NULL;
30
31 void be_copy_opt_init(void) {
32         dbg = firm_dbg_register("ir.be.copyoptmain");
33         firm_dbg_set_mask(dbg, DEBUG_LVL);
34 }
35
36 typedef struct color_saver {
37         arch_env_t *arch_env;
38         be_chordal_env_t *chordal_env;
39         pmap *saved_colors;
40         int flag;  /* 0 save; 1 load */
41 } color_save_t;
42
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);
49                 } else { /*load */
50                         arch_register_t *reg = pmap_get(saver->saved_colors, irn);
51                         arch_set_irn_register(saver->arch_env, irn, 0, reg);
52                 }
53         }
54 }
55
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);
59 }
60
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);
64 }
65
66 void be_copy_opt(be_chordal_env_t *chordal_env) {
67         copy_opt_t *co;
68         int costs, costs_init=-1, costs_heur=-1, costs_ilp_n_sec=-1, costs_ilp=-1;
69         int lower_bound;
70         color_save_t saver;
71         saver.arch_env = chordal_env->session_env->main_env->arch_env;
72         saver.chordal_env = chordal_env;
73         saver.saved_colors = pmap_create();
74
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);
78
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);
82
83 #ifdef DO_STAT
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)));
87
88         costs = co_get_copy_costs(co);
89         costs_init = costs;
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));
94 #endif
95
96         save_colors(&saver);
97
98 #ifdef DO_HEUR
99         lc_timer_t *timer = lc_timer_register("heur", NULL);
100         lc_timer_reset_and_start(timer);
101         co_heur_opt(co);
102         lc_timer_stop(timer);
103         copystat_add_heur_time(lc_timer_elapsed_msec(timer));
104 #ifdef DO_STAT
105         costs = co_get_copy_costs(co);
106         costs_heur = costs;
107         copystat_add_heur_costs(costs_heur);
108         DBG((dbg, LEVEL_1, "Heur costs: %3d\n", costs_heur));
109 #endif
110         assert(lower_bound == -1 || costs_heur == -1 || lower_bound <= costs_heur);
111 #endif
112
113         load_colors(&saver);
114
115 #ifdef DO_ILP_N_SEC
116         co_ilp_opt(co, 2.0);
117 #ifdef DO_STAT
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));
122 #endif
123         assert(lower_bound == -1 || costs_ilp == -1 || lower_bound <= costs_ilp);
124         assert(costs_ilp == -1 || costs_heur == -1 || costs_ilp <= costs_heur);
125 #endif
126
127         load_colors(&saver);
128
129 #ifdef DO_ILP
130         co_ilp_opt(co, 0.0);
131 #ifdef DO_STAT
132         costs = co_get_copy_costs(co);
133         costs_ilp = costs;
134         copystat_add_opt_costs(costs_ilp);
135         DBG((dbg, LEVEL_1, "Opt  costs: %3d\n", costs_ilp));
136 #endif
137         assert(lower_bound == -1 || costs_ilp == -1 || lower_bound <= costs_ilp);
138         assert(costs_ilp == -1 || costs_heur == -1 || costs_ilp <= costs_heur);
139 #endif
140
141         pmap_destroy(saver.saved_colors);
142         free_copy_opt(co);
143 }