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