Changes API a little bit :-)
[libfirm] / ir / be / bechordal_main.c
1 /**
2  * @file   bechordal_main.c
3  * @date   29.11.2005
4  * @author Sebastian Hack
5  *
6  * Copyright (C) 2005 Universitaet Karlsruhe
7  * Released under the GPL
8  *
9  * Driver for the chordal register allocator.
10  */
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include "obst.h"
16 #include "pset.h"
17 #include "list.h"
18 #include "bitset.h"
19 #include "iterator.h"
20
21 #ifdef WITH_LIBCORE
22 #include <libcore/lc_opts.h>
23 #include <libcore/lc_opts_enum.h>
24 #endif /* WITH_LIBCORE */
25
26 #include "irmode_t.h"
27 #include "irgraph_t.h"
28 #include "irprintf_t.h"
29 #include "irgwalk.h"
30 #include "irdump.h"
31 #include "irdom.h"
32 #include "debug.h"
33 #include "xmalloc.h"
34
35 #include "bechordal_t.h"
36 #include "beutil.h"
37 #include "besched.h"
38 #include "benumb_t.h"
39 #include "besched_t.h"
40 #include "belive_t.h"
41 #include "bearch.h"
42 #include "beifg.h"
43 #include "beifg_impl.h"
44
45 #include "bespillbelady.h"
46 #include "bespillilp.h"
47 #include "beconstrperm.h"
48 #include "belower.h"
49
50 #define DO_SSADESTR
51
52 #ifdef DO_SSADESTR
53 #include "bessadestr.h"
54 #include "becopystat.h"
55 #include "becopyoptmain.h"
56 #endif /* DO_SSADESTR */
57
58
59 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
60         firm_dbg_module_t *dbg = chordal_env->dbg;
61         const arch_env_t *arch_env = chordal_env->main_env->arch_env;
62         struct obstack ob;
63         pmap_entry *pme;
64         ir_node **nodes, *n1, *n2;
65         int i, o;
66
67         /* Collect all irns */
68         obstack_init(&ob);
69         pmap_foreach(chordal_env->border_heads, pme) {
70                 border_t *curr;
71                 struct list_head *head = pme->value;
72                 list_for_each_entry(border_t, curr, head, list)
73                         if (curr->is_def && curr->is_real)
74                                 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
75                                         obstack_ptr_grow(&ob, curr->irn);
76         }
77         obstack_ptr_grow(&ob, NULL);
78         nodes = (ir_node **) obstack_finish(&ob);
79
80         /* Check them */
81         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
82                 const arch_register_t *n1_reg, *n2_reg;
83
84                 n1_reg = arch_get_irn_register(arch_env, n1);
85                 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
86                         DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
87 //                      assert(0 && "Register constraint does not hold");
88                 }
89                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
90                         n2_reg = arch_get_irn_register(arch_env, n2);
91                         if (values_interfere(n1, n2) && n1_reg == n2_reg) {
92                                 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
93                                 assert(0 && "Interfering values have the same color!");
94                         }
95                 }
96         }
97         obstack_free(&ob, NULL);
98 }
99
100 static void check_pressure_walker(ir_node *bl, void *data)
101 {
102         be_chordal_env_t *env = data;
103         firm_dbg_module_t *dbg = env->dbg;
104         int n_regs = arch_register_class_n_regs(env->cls);
105
106         pset *live = pset_new_ptr_default();
107         int step = 0;
108         ir_node *irn;
109         irn_live_t *li;
110
111         live_foreach(bl, li) {
112                 if(live_is_end(li) && chordal_has_class(env, li->irn)) {
113                         ir_node *irn = (ir_node *) li->irn;
114                         pset_insert_ptr(live, irn);
115                 }
116         }
117
118         DBG((dbg, LEVEL_1, "end set for %+F\n", bl));
119         for(irn = pset_first(live); irn; irn = pset_next(live))
120                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
121
122         sched_foreach_reverse(bl, irn) {
123                 int i, n;
124                 int pressure = pset_count(live);
125
126                 DBG((dbg, LEVEL_1, "%+10F@%+10F: pressure %d\n", bl, irn, pressure));
127
128                 if(pressure > n_regs) {
129                         ir_node *x;
130                         ir_printf("%+10F@%+10F: pressure to high: %d\n", bl, irn, pressure);
131                         for(x = pset_first(live); x; x = pset_next(live))
132                                 ir_printf("\t%+10F\n", x);
133                 }
134
135                 if(chordal_has_class(env, irn))
136                         pset_remove_ptr(live, irn);
137
138                 for(i = 0, n = get_irn_arity(irn); i < n; i++) {
139                         ir_node *op = get_irn_n(irn, i);
140                         if(chordal_has_class(env, op) && !is_Phi(irn))
141                                 pset_insert_ptr(live, op);
142                 }
143                 step++;
144         }
145 }
146
147 void be_check_pressure(const be_chordal_env_t *env)
148 {
149         irg_block_walk_graph(env->irg, check_pressure_walker, NULL, (void *) env);
150 }
151
152 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
153 {
154         if(env->ifg)
155                 return be_ifg_connected(env->ifg, a, b);
156         else
157                 return values_interfere(a, b);
158 }
159
160
161 static be_ra_chordal_opts_t options = {
162         BE_CH_DUMP_NONE,
163         BE_CH_SPILL_BELADY,
164         BE_CH_COPYMIN_HEUR,
165         BE_CH_IFG_STD,
166         BE_CH_LOWER_PERM_SWAP
167 };
168
169 #ifdef WITH_LIBCORE
170 static const lc_opt_enum_int_items_t spill_items[] = {
171         { "belady", BE_CH_SPILL_BELADY },
172         { "ilp",        BE_CH_SPILL_ILP },
173         { NULL, 0 }
174 };
175
176 static const lc_opt_enum_int_items_t copymin_items[] = {
177         { "heur", BE_CH_COPYMIN_HEUR },
178         { "ilp",  BE_CH_COPYMIN_ILP },
179         { NULL, 0 }
180 };
181
182 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
183         { "std",  BE_CH_IFG_STD },
184         { "fast", BE_CH_IFG_FAST },
185         { NULL, 0 }
186 };
187
188 static const lc_opt_enum_int_items_t lower_perm_items[] = {
189         { "swap", BE_CH_LOWER_PERM_SWAP },
190         { "copy", BE_CH_LOWER_PERM_COPY },
191         { NULL, 0 }
192 };
193
194 static lc_opt_enum_int_var_t spill_var = {
195         &options.spill_method, spill_items
196 };
197
198 static lc_opt_enum_int_var_t copymin_var = {
199         &options.copymin_method, copymin_items
200 };
201
202 static lc_opt_enum_int_var_t ifg_flavor_var = {
203         &options.spill_method, ifg_flavor_items
204 };
205
206 static lc_opt_enum_int_var_t lower_perm_var = {
207         &options.lower_perm_method, lower_perm_items
208 };
209
210 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
211 {
212         lc_opt_entry_t *dump;
213         grp = lc_opt_get_grp(grp, "chordal");
214
215 }
216 #endif
217
218 static void dump(int mask, ir_graph *irg, const char *suffix,
219                                  void (*dump)(ir_graph *, const char *))
220 {
221         if((options.dump_flags & mask) == mask)
222                 dump(irg, suffix);
223 }
224
225 static void be_ra_chordal_main(const be_main_env_t *main_env, ir_graph *irg)
226 {
227         int j, m;
228         be_chordal_env_t chordal_env;
229         const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
230
231         compute_doms(irg);
232
233         chordal_env.irg          = irg;
234         chordal_env.dbg          = firm_dbg_register("firm.be.chordal");
235         chordal_env.main_env     = main_env;
236         chordal_env.dom_front    = be_compute_dominance_frontiers(irg);
237
238         obstack_init(&chordal_env.obst);
239
240         /* Perform the following for each register class. */
241         for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
242                 chordal_env.border_heads = pmap_create();
243                 chordal_env.cls = arch_isa_get_reg_class(isa, j);
244
245                 be_liveness(irg);
246
247                 /* spilling */
248                 switch(options.spill_method) {
249                 case BE_CH_SPILL_BELADY:
250                         be_spill_belady(&chordal_env);
251                         break;
252                 case BE_CH_SPILL_ILP:
253                         be_spill_ilp(&chordal_env);
254                         break;
255                 default:
256                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
257                         be_spill_belady(&chordal_env);
258                 }
259                 dump(BE_CH_DUMP_SPILL, irg, "-spill", dump_ir_block_graph_sched);
260                 be_liveness(irg);
261                 be_check_pressure(&chordal_env);
262
263                 /* Insert perms before reg-constrained instructions */
264                 be_insert_constr_perms(&chordal_env);
265                 dump(BE_CH_DUMP_CONSTR, irg, "-constr", dump_ir_block_graph_sched);
266
267                 be_liveness(irg);
268                 be_numbering(irg);
269                 be_check_pressure(&chordal_env);
270
271                 /* Color the graph. */
272                 be_ra_chordal_color(&chordal_env);
273
274                 /* Build the interference graph. */
275                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
276
277 #ifdef DO_SSADESTR
278                 /* copy minimization */
279                 copystat_collect_cls(&chordal_env);
280                 be_copy_opt(&chordal_env);
281                 dump(BE_CH_DUMP_COPYMIN, irg, "-copymin", dump_ir_block_graph_sched);
282
283                 /* ssa destruction */
284                 be_ssa_destruction(&chordal_env);
285                 be_ssa_destruction_check(&chordal_env);
286                 be_ra_chordal_check(&chordal_env);
287                 dump(BE_CH_DUMP_SSADESTR, irg, "-ssadestr", dump_ir_block_graph_sched);
288
289                 copystat_dump(irg);
290 #endif /* DO_SSADESTR */
291
292                 be_ifg_free(chordal_env.ifg);
293                 be_numbering_done(irg);
294
295                 pmap_destroy(chordal_env.border_heads);
296         }
297
298 #ifdef DO_SSADESTR
299         lower_perms(&chordal_env, options.lower_perm_method == BE_CH_LOWER_PERM_COPY ? 1 : 0);
300 #endif /* DO_SSADESTR */
301
302         be_free_dominance_frontiers(chordal_env.dom_front);
303         obstack_free(&chordal_env.obst, NULL);
304 }
305
306 const be_ra_t be_ra_chordal_allocator = {
307 #ifdef WITH_LIBCORE
308         be_ra_chordal_register_options,
309 #endif
310         be_ra_chordal_main
311 };