added Min, Max, Div, DivMod and Mulh nodes
[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 #undef DO_ILP_5_SEC
26 #undef DO_ILP_30_SEC
27 #undef DO_ILP
28
29 #define DEBUG_LVL SET_LEVEL_1
30 static firm_dbg_module_t *dbg = NULL;
31
32 void be_copy_opt_init(void) {
33         dbg = firm_dbg_register("ir.be.copyoptmain");
34         firm_dbg_set_mask(dbg, DEBUG_LVL);
35 }
36
37 typedef struct color_saver {
38         arch_env_t *arch_env;
39         be_chordal_env_t *chordal_env;
40         pmap *saved_colors;
41         int flag;  /* 0 save; 1 load */
42 } color_save_t;
43
44 static void save_load(ir_node *irn, void *env) {
45         color_save_t *saver = env;
46         if (saver->chordal_env->cls == arch_get_irn_reg_class(saver->arch_env, irn, arch_pos_make_out(0))) {
47                 if (saver->flag == 0) { /* save */
48                         const arch_register_t *reg = arch_get_irn_register(saver->arch_env, irn, 0);
49                         pmap_insert(saver->saved_colors, irn, (void *) reg);
50                 } else { /*load */
51                         arch_register_t *reg = pmap_get(saver->saved_colors, irn);
52                         arch_set_irn_register(saver->arch_env, irn, 0, reg);
53                 }
54         }
55 }
56
57 static void save_colors(color_save_t *color_saver) {
58         color_saver->flag = 0;
59         irg_walk_graph(color_saver->chordal_env->session_env->irg, save_load, NULL, color_saver);
60 }
61
62 static void load_colors(color_save_t *color_saver) {
63         color_saver->flag = 1;
64         irg_walk_graph(color_saver->chordal_env->session_env->irg, save_load, NULL, color_saver);
65 }
66
67 void be_copy_opt(be_chordal_env_t *chordal_env) {
68         copy_opt_t *co;
69         int costs, costs_init=-1, costs_heur=-1, costs_ilp_5_sec=-1, costs_ilp_30_sec=-1, costs_ilp=-1;
70         int lower_bound;
71         color_save_t saver;
72         saver.arch_env = chordal_env->session_env->main_env->arch_env;
73         saver.chordal_env = chordal_env;
74         saver.saved_colors = pmap_create();
75
76         /* BETTER: You can remove this if you replace all
77          * `grep get_irn_out *.c` by the irouts.h module.*/
78         compute_outs(chordal_env->session_env->irg);
79
80         co = new_copy_opt(chordal_env, get_costs_loop_depth);
81         DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
82         phi_class_compute(chordal_env->session_env->irg);
83
84 #ifdef DO_STAT
85         lower_bound = co_get_lower_bound(co);
86         DBG((dbg, LEVEL_1, "Lower Bound: %3d\n", lower_bound));
87         DBG((dbg, LEVEL_1, "Inevit Costs: %3d\n", co_get_inevit_copy_costs(co)));
88
89         costs = co_get_copy_costs(co);
90         costs_init = costs;
91         copystat_add_max_costs(co_get_max_copy_costs(co));
92         copystat_add_inevit_costs(co_get_inevit_copy_costs(co));
93         copystat_add_init_costs(costs_init);
94         DBG((dbg, LEVEL_1, "Init costs: %3d\n", costs_init));
95 #endif
96
97         save_colors(&saver);
98
99 #ifdef DO_HEUR
100         lc_timer_t *timer = lc_timer_register("heur", NULL);
101         lc_timer_reset_and_start(timer);
102         co_heur_opt(co);
103         lc_timer_stop(timer);
104         copystat_add_heur_time(lc_timer_elapsed_msec(timer));
105 #ifdef DO_STAT
106         costs = co_get_copy_costs(co);
107         costs_heur = costs;
108         copystat_add_heur_costs(costs_heur);
109         DBG((dbg, LEVEL_1, "Heur costs: %3d\n", costs_heur));
110 #endif
111         assert(lower_bound == -1 || costs_heur == -1 || lower_bound <= costs_heur);
112 #endif
113
114         int was_optimal = 0;
115
116 #ifdef DO_ILP_5_SEC
117         load_colors(&saver);
118         was_optimal = co_ilp_opt(co, 5.0);
119 #ifdef DO_STAT
120         costs = co_get_copy_costs(co);
121         costs_ilp_5_sec = costs;
122         copystat_add_ilp_5_sec_costs(costs_ilp_5_sec);
123         DBG((dbg, LEVEL_1, "5_Sec costs: %3d\n", costs_ilp_5_sec));
124 #endif
125 #endif
126
127
128 #ifdef DO_ILP_30_SEC
129         if (!was_optimal) {
130                 load_colors(&saver);
131                 co_ilp_opt(co, 30.0);
132         }
133 #ifdef DO_STAT
134         costs = co_get_copy_costs(co);
135         costs_ilp_30_sec = costs;
136         copystat_add_ilp_30_sec_costs(costs_ilp_30_sec);
137         DBG((dbg, LEVEL_1, "30_Sec costs: %3d\n", costs_ilp_30_sec));
138 #endif
139 #endif
140
141
142 #ifdef DO_ILP
143         load_colors(&saver);
144         co_ilp_opt(co, 1000.0);
145 #ifdef DO_STAT
146         costs = co_get_copy_costs(co);
147         costs_ilp = costs;
148         copystat_add_opt_costs(costs_ilp);
149         DBG((dbg, LEVEL_1, "Opt  costs: %3d\n", costs_ilp));
150 #endif
151         assert(lower_bound == -1 || costs_ilp == -1 || lower_bound <= costs_ilp);
152         assert(costs_ilp == -1 || costs_heur == -1 || costs_ilp <= costs_heur);
153 #endif
154
155         pmap_destroy(saver.saved_colors);
156         free_copy_opt(co);
157 }