register options for cmdline
[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_t.h"
43 #include "beifg_impl.h"
44
45 #include "bespillbelady.h"
46 #include "belower.h"
47
48 #ifdef WITH_ILP
49 #include "bespillilp.h"
50 #endif /* WITH_ILP */
51
52 #include "becopystat.h"
53 #include "becopyopt.h"
54 #include "bessadestr.h"
55
56
57 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
58         const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
59         struct obstack ob;
60         pmap_entry *pme;
61         ir_node **nodes, *n1, *n2;
62         int i, o;
63         DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
64
65         /* Collect all irns */
66         obstack_init(&ob);
67         pmap_foreach(chordal_env->border_heads, pme) {
68                 border_t *curr;
69                 struct list_head *head = pme->value;
70                 list_for_each_entry(border_t, curr, head, list)
71                         if (curr->is_def && curr->is_real)
72                                 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
73                                         obstack_ptr_grow(&ob, curr->irn);
74         }
75         obstack_ptr_grow(&ob, NULL);
76         nodes = (ir_node **) obstack_finish(&ob);
77
78         /* Check them */
79         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
80                 const arch_register_t *n1_reg, *n2_reg;
81
82                 n1_reg = arch_get_irn_register(arch_env, n1);
83                 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
84                         DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
85                         assert(0 && "Register constraint does not hold");
86                 }
87                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
88                         n2_reg = arch_get_irn_register(arch_env, n2);
89                         if (values_interfere(n1, n2) && n1_reg == n2_reg) {
90                                 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
91                                 assert(0 && "Interfering values have the same color!");
92                         }
93                 }
94         }
95         obstack_free(&ob, NULL);
96 }
97
98 static void check_pressure_walker(ir_node *bl, void *data)
99 {
100         be_chordal_env_t *env = data;
101         int n_regs = arch_register_class_n_regs(env->cls);
102
103         pset *live = pset_new_ptr_default();
104         int step = 0;
105         ir_node *irn;
106         irn_live_t *li;
107         DEBUG_ONLY(firm_dbg_module_t *dbg = env->dbg;)
108
109         live_foreach(bl, li) {
110                 if(live_is_end(li) && chordal_has_class(env, li->irn)) {
111                         ir_node *irn = (ir_node *) li->irn;
112                         pset_insert_ptr(live, irn);
113                 }
114         }
115
116         DBG((dbg, LEVEL_1, "end set for %+F\n", bl));
117         for(irn = pset_first(live); irn; irn = pset_next(live))
118                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
119
120         sched_foreach_reverse(bl, irn) {
121                 int i, n;
122                 int pressure = pset_count(live);
123
124                 DBG((dbg, LEVEL_1, "%+10F@%+10F: pressure %d\n", bl, irn, pressure));
125
126                 if(pressure > n_regs) {
127                         ir_node *x;
128                         ir_printf("%+10F@%+10F: pressure to high: %d\n", bl, irn, pressure);
129                         for(x = pset_first(live); x; x = pset_next(live))
130                                 ir_printf("\t%+10F\n", x);
131                 }
132
133                 if(chordal_has_class(env, irn))
134                         pset_remove_ptr(live, irn);
135
136                 for(i = 0, n = get_irn_arity(irn); i < n; i++) {
137                         ir_node *op = get_irn_n(irn, i);
138                         if(chordal_has_class(env, op) && !is_Phi(irn))
139                                 pset_insert_ptr(live, op);
140                 }
141                 step++;
142         }
143 }
144
145 void be_check_pressure(const be_chordal_env_t *env)
146 {
147         irg_block_walk_graph(env->irg, check_pressure_walker, NULL, (void *) env);
148 }
149
150 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
151 {
152         if(env->ifg)
153                 return be_ifg_connected(env->ifg, a, b);
154         else
155                 return values_interfere(a, b);
156 }
157
158
159 static be_ra_chordal_opts_t options = {
160         BE_CH_DUMP_NONE,
161         BE_CH_SPILL_BELADY,
162         BE_CH_COPYMIN_HEUR,
163         BE_CH_IFG_STD,
164         BE_CH_LOWER_PERM_SWAP | BE_CH_LOWER_PERM_STAT,
165 };
166
167 #ifdef WITH_LIBCORE
168 static const lc_opt_enum_int_items_t spill_items[] = {
169         { "belady", BE_CH_SPILL_BELADY },
170 #ifdef WITH_ILP
171         { "ilp",        BE_CH_SPILL_ILP },
172 #endif
173         { NULL, 0 }
174 };
175
176 static const lc_opt_enum_int_items_t copymin_items[] = {
177         { "heur", BE_CH_COPYMIN_HEUR },
178 #ifdef WITH_ILP
179         { "ilp",  BE_CH_COPYMIN_ILP },
180 #endif
181         { NULL, 0 }
182 };
183
184 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
185         { "std",  BE_CH_IFG_STD },
186         { "fast", BE_CH_IFG_FAST },
187         { NULL, 0 }
188 };
189
190 static const lc_opt_enum_int_items_t lower_perm_items[] = {
191         { "copy", BE_CH_LOWER_PERM_COPY },
192         { "swap", BE_CH_LOWER_PERM_SWAP },
193         { "stat", BE_CH_LOWER_PERM_STAT },
194         { NULL, 0 }
195 };
196
197 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
198         { NULL, 0 }
199 };
200
201 static const lc_opt_enum_int_items_t dump_items[] = {
202         { "spill",    BE_CH_DUMP_SPILL },
203         { "live",     BE_CH_DUMP_LIVE },
204         { "color",    BE_CH_DUMP_COLOR },
205         { "copymin",  BE_CH_DUMP_COPYMIN },
206         { "ssadestr", BE_CH_DUMP_SSADESTR },
207         { "tree",     BE_CH_DUMP_TREE_INTV },
208         { "constr",   BE_CH_DUMP_CONSTR },
209         { "lower",    BE_CH_DUMP_LOWER },
210         { NULL, 0 }
211 };
212
213 static lc_opt_enum_int_var_t spill_var = {
214         &options.spill_method, spill_items
215 };
216
217 static lc_opt_enum_int_var_t copymin_var = {
218         &options.copymin_method, copymin_items
219 };
220
221 static lc_opt_enum_int_var_t ifg_flavor_var = {
222         &options.spill_method, ifg_flavor_items
223 };
224
225 static lc_opt_enum_int_var_t lower_perm_var = {
226         &options.lower_perm_opt, lower_perm_items
227 };
228
229 static lc_opt_enum_int_var_t dump_var = {
230         &options.dump_flags, dump_items
231 };
232
233 static const lc_opt_table_entry_t be_chordal_options[] = {
234         LC_OPT_ENT_ENUM_MASK("spill", "spill method (belady or ilp)", &spill_var),
235         LC_OPT_ENT_ENUM_PTR("copymin", "copymin method (heur or ilp)", &copymin_var),
236         LC_OPT_ENT_ENUM_PTR("ifg", "interference graph flavour (std or fast)", &ifg_flavor_var),
237         LC_OPT_ENT_ENUM_MASK("lowerperm", "perm lowering options (copy, swap, stat)", &lower_perm_var),
238         LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
239         { NULL }
240 };
241
242 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
243 {
244         int i;
245         static int run_once = 0;
246         lc_opt_entry_t *chordal_grp;
247
248         if (! run_once) {
249                 run_once    = 1;
250                 chordal_grp = lc_opt_get_grp(grp, "chordal");
251
252                 lc_opt_add_table(chordal_grp, be_chordal_options);
253         }
254 }
255 #endif
256
257 static void dump(unsigned mask, ir_graph *irg,
258                                  const arch_register_class_t *cls,
259                                  const char *suffix,
260                                  void (*dump_func)(ir_graph *, const char *))
261 {
262         if(1 || ((options.dump_flags & mask) == mask)) {
263                 if(cls) {
264                         char buf[256];
265                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
266                         be_dump(irg, buf, dump_func);
267                 }
268
269                 else
270                         be_dump(irg, suffix, dump_func);
271         }
272 }
273
274 static void be_ra_chordal_main(const be_irg_t *bi)
275 {
276         int j, m;
277         be_chordal_env_t chordal_env;
278         ir_graph *irg = bi->irg;
279         const be_main_env_t *main_env = bi->main_env;
280         const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
281
282         compute_doms(irg);
283
284         chordal_env.opts         = &options;
285         chordal_env.irg          = irg;
286         chordal_env.birg         = bi;
287         chordal_env.dom_front    = be_compute_dominance_frontiers(irg);
288         FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
289
290         obstack_init(&chordal_env.obst);
291
292         /* Perform the following for each register class. */
293         for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
294                 chordal_env.cls          = arch_isa_get_reg_class(isa, j);
295                 chordal_env.border_heads = pmap_create();
296
297                 be_liveness(irg);
298                 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
299
300                 /* spilling */
301                 switch(options.spill_method) {
302                 case BE_CH_SPILL_BELADY:
303                         be_spill_belady(&chordal_env);
304                         break;
305 #ifdef WITH_ILP
306                 case BE_CH_SPILL_ILP:
307                         be_spill_ilp(&chordal_env);
308                         break;
309 #endif /* WITH_ILP */
310                 default:
311                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
312                         be_spill_belady(&chordal_env);
313                 }
314                 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
315                 be_liveness(irg);
316                 be_check_pressure(&chordal_env);
317
318                 /* Color the graph. */
319                 be_ra_chordal_color(&chordal_env);
320                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
321
322                 /* Build the interference graph. */
323                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
324                 be_ifg_check(chordal_env.ifg);
325
326                 /* copy minimization */
327 #ifdef COPYOPT_STAT
328                 co_compare_solvers(&chordal_env);
329 #else
330                 {
331                 copy_opt_t *co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
332                 co_build_ou_structure(co);
333                 co_solve_heuristic(co);
334                 co_free_ou_structure(co);
335                 free_copy_opt(co);
336                 }
337 #endif
338                 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
339                 be_ra_chordal_check(&chordal_env);
340
341                 /* ssa destruction */
342                 be_ssa_destruction(&chordal_env);
343                 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
344                 be_ssa_destruction_check(&chordal_env);
345                 be_ra_chordal_check(&chordal_env);
346
347                 copystat_dump(irg);
348
349                 be_ifg_free(chordal_env.ifg);
350
351                 pmap_destroy(chordal_env.border_heads);
352         }
353
354         be_compute_spill_offsets(&chordal_env);
355
356         dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
357
358         lower_nodes_after_ra(&chordal_env,
359                 options.lower_perm_opt & BE_CH_LOWER_PERM_COPY,
360                 options.lower_perm_opt & BE_CH_LOWER_PERM_STAT);
361         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
362
363         obstack_free(&chordal_env.obst, NULL);
364         be_free_dominance_frontiers(chordal_env.dom_front);
365 }
366
367 const be_ra_t be_ra_chordal_allocator = {
368 #ifdef WITH_LIBCORE
369         be_ra_chordal_register_options,
370 #endif
371         be_ra_chordal_main
372 };