45af41322247eca293fddb0e62b7df57dcf0522d
[libfirm] / ir / be / bechordal_main.c
1 /**
2  * @file   bechordal_main.c
3  * @date   29.11.2005
4  * @author Sebastian Hack
5  * @cvs-id $Id$
6  *
7  * Copyright (C) 2005 Universitaet Karlsruhe
8  * Released under the GPL
9  *
10  * Driver for the chordal register allocator.
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include "obst.h"
17 #include "pset.h"
18 #include "list.h"
19 #include "bitset.h"
20 #include "iterator.h"
21 #include "firm_config.h"
22
23 #ifdef WITH_LIBCORE
24 #include <libcore/lc_opts.h>
25 #include <libcore/lc_opts_enum.h>
26 #include <libcore/lc_timing.h>
27 #endif /* WITH_LIBCORE */
28
29 #include "irmode_t.h"
30 #include "irgraph_t.h"
31 #include "irprintf_t.h"
32 #include "irgwalk.h"
33 #include "ircons.h"
34 #include "irdump.h"
35 #include "irdom.h"
36 #include "ircons.h"
37 #include "irbitset.h"
38 #include "debug.h"
39 #include "xmalloc.h"
40 #include "execfreq.h"
41
42 #include "bechordal_t.h"
43 #include "beabi.h"
44 #include "bejavacoal.h"
45 #include "beutil.h"
46 #include "besched.h"
47 #include "benumb_t.h"
48 #include "besched_t.h"
49 #include "belive_t.h"
50 #include "bearch.h"
51 #include "beifg_t.h"
52 #include "beifg_impl.h"
53 #include "benode_t.h"
54
55 #include "bespillbelady.h"
56 #include "bespillmorgan.h"
57 #include "belower.h"
58
59 #ifdef WITH_ILP
60 #include "bespillremat.h"
61 #endif /* WITH_ILP */
62
63 #include "becopystat.h"
64 #include "becopyopt.h"
65 #include "bessadestr.h"
66 #include "beverify.h"
67 #include "bespillcost.h"
68
69 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
70         const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
71         struct obstack ob;
72         pmap_entry *pme;
73         ir_node **nodes, *n1, *n2;
74         int i, o;
75         DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
76
77         /* Collect all irns */
78         obstack_init(&ob);
79         pmap_foreach(chordal_env->border_heads, pme) {
80                 border_t *curr;
81                 struct list_head *head = pme->value;
82                 list_for_each_entry(border_t, curr, head, list)
83                         if (curr->is_def && curr->is_real)
84                                 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
85                                         obstack_ptr_grow(&ob, curr->irn);
86         }
87         obstack_ptr_grow(&ob, NULL);
88         nodes = (ir_node **) obstack_finish(&ob);
89
90         /* Check them */
91         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
92                 const arch_register_t *n1_reg, *n2_reg;
93
94                 n1_reg = arch_get_irn_register(arch_env, n1);
95                 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
96                         DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
97                         assert(0 && "Register constraint does not hold");
98                 }
99                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
100                         n2_reg = arch_get_irn_register(arch_env, n2);
101                         if (values_interfere(chordal_env->lv, n1, n2) && n1_reg == n2_reg) {
102                                 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
103                                 assert(0 && "Interfering values have the same color!");
104                         }
105                 }
106         }
107         obstack_free(&ob, NULL);
108 }
109
110 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
111 {
112         if(env->ifg)
113                 return be_ifg_connected(env->ifg, a, b);
114         else
115                 return values_interfere(env->lv, a, b);
116 }
117
118
119 static be_ra_chordal_opts_t options = {
120         BE_CH_DUMP_NONE,
121         BE_CH_SPILL_BELADY,
122         BE_CH_COPYMIN_HEUR2,
123         BE_CH_IFG_STD,
124         BE_CH_LOWER_PERM_SWAP,
125         BE_CH_VRFY_WARN,
126 };
127
128 static be_ra_timer_t ra_timer = {
129         NULL,
130         NULL,
131         NULL,
132         NULL,
133         NULL,
134         NULL,
135         NULL,
136         NULL,
137         NULL,
138         NULL,
139 };
140
141 #ifdef WITH_LIBCORE
142 static const lc_opt_enum_int_items_t spill_items[] = {
143         { "morgan", BE_CH_SPILL_MORGAN },
144         { "belady", BE_CH_SPILL_BELADY },
145 #ifdef WITH_ILP
146         { "remat",  BE_CH_SPILL_REMAT },
147 #endif /* WITH_ILP */
148         { NULL, 0 }
149 };
150
151 static const lc_opt_enum_int_items_t copymin_items[] = {
152         { "none",  BE_CH_COPYMIN_NONE      },
153         { "heur1", BE_CH_COPYMIN_HEUR1     },
154         { "heur2", BE_CH_COPYMIN_HEUR2     },
155         { "heur3", BE_CH_COPYMIN_HEUR3     },
156         { "stat",  BE_CH_COPYMIN_STAT      },
157         { "park",  BE_CH_COPYMIN_PARK_MOON },
158 #ifdef WITH_ILP
159         { "ilp",   BE_CH_COPYMIN_ILP },
160 #endif /* WITH_ILP */
161         { NULL, 0 }
162 };
163
164 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
165         { "std",     BE_CH_IFG_STD     },
166         { "fast",    BE_CH_IFG_FAST    },
167         { "clique",  BE_CH_IFG_CLIQUE  },
168         { "pointer", BE_CH_IFG_POINTER },
169         { "list",    BE_CH_IFG_LIST    },
170         { "check",   BE_CH_IFG_CHECK   },
171         { NULL,      0                 }
172 };
173
174 static const lc_opt_enum_int_items_t lower_perm_items[] = {
175         { "copy", BE_CH_LOWER_PERM_COPY },
176         { "swap", BE_CH_LOWER_PERM_SWAP },
177         { NULL, 0 }
178 };
179
180 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
181         { NULL, 0 }
182 };
183
184 static const lc_opt_enum_int_items_t dump_items[] = {
185         { "spill",    BE_CH_DUMP_SPILL     },
186         { "live",     BE_CH_DUMP_LIVE      },
187         { "color",    BE_CH_DUMP_COLOR     },
188         { "copymin",  BE_CH_DUMP_COPYMIN   },
189         { "ssadestr", BE_CH_DUMP_SSADESTR  },
190         { "tree",     BE_CH_DUMP_TREE_INTV },
191         { "constr",   BE_CH_DUMP_CONSTR    },
192         { "lower",    BE_CH_DUMP_LOWER     },
193         { "appel",    BE_CH_DUMP_APPEL     },
194         { "all",      BE_CH_DUMP_ALL       },
195         { NULL, 0 }
196 };
197
198 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
199         { "off",    BE_CH_VRFY_OFF    },
200         { "warn",   BE_CH_VRFY_WARN   },
201         { "assert", BE_CH_VRFY_ASSERT },
202         { NULL, 0 }
203 };
204
205 static lc_opt_enum_int_var_t spill_var = {
206         &options.spill_method, spill_items
207 };
208
209 static lc_opt_enum_int_var_t copymin_var = {
210         &options.copymin_method, copymin_items
211 };
212
213 static lc_opt_enum_int_var_t ifg_flavor_var = {
214         &options.ifg_flavor, ifg_flavor_items
215 };
216
217 static lc_opt_enum_int_var_t lower_perm_var = {
218         &options.lower_perm_opt, lower_perm_items
219 };
220
221 static lc_opt_enum_int_var_t dump_var = {
222         &options.dump_flags, dump_items
223 };
224
225 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
226         &options.vrfy_option, be_ch_vrfy_items
227 };
228
229 /** Dump copy minimization statistics. */
230 static int be_copymin_stats = 0;
231
232 /** Enable extreme live range splitting. */
233 static int be_elr_split = 0;
234
235 /** Assumed loop iteration count for execution frequency estimation. */
236 static int be_loop_weight = 9;
237
238 static const lc_opt_table_entry_t be_chordal_options[] = {
239         LC_OPT_ENT_ENUM_INT ("spill",         "spill method (belady, morgan or remat)", &spill_var),
240         LC_OPT_ENT_ENUM_PTR ("copymin",       "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", &copymin_var),
241         LC_OPT_ENT_ENUM_PTR ("ifg",           "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
242         LC_OPT_ENT_ENUM_PTR ("perm",          "perm lowering options (copy or swap)", &lower_perm_var),
243         LC_OPT_ENT_ENUM_MASK("dump",          "select dump phases", &dump_var),
244         LC_OPT_ENT_ENUM_PTR ("vrfy",          "verify options (off, warn, assert)", &be_ch_vrfy_var),
245         LC_OPT_ENT_BOOL     ("copymin_stats", "dump statistics of copy minimization", &be_copymin_stats),
246         LC_OPT_ENT_BOOL     ("elrsplit",      "enable extreme live range splitting", &be_elr_split),
247         LC_OPT_ENT_INT      ("loop_weight",   "assumed amount of loop iterations for guessing the execution frequency", &be_loop_weight),
248         { NULL }
249 };
250
251 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
252 {
253         static int run_once = 0;
254         lc_opt_entry_t *chordal_grp;
255
256         if (! run_once) {
257                 run_once    = 1;
258                 chordal_grp = lc_opt_get_grp(grp, "chordal");
259
260                 lc_opt_add_table(chordal_grp, be_chordal_options);
261         }
262
263         co_register_options(chordal_grp);
264         java_coal_register_options(chordal_grp);
265 }
266 #endif /* WITH_LIBCORE */
267
268 static void dump(unsigned mask, ir_graph *irg,
269                                  const arch_register_class_t *cls,
270                                  const char *suffix,
271                                  void (*dump_func)(ir_graph *, const char *))
272 {
273         if((options.dump_flags & mask) == mask) {
274                 if (cls) {
275                         char buf[256];
276                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
277                         be_dump(irg, buf, dump_func);
278                 }
279                 else
280                         be_dump(irg, suffix, dump_func);
281         }
282 }
283
284 static void put_ignore_colors(be_chordal_env_t *chordal_env)
285 {
286         int n_colors = chordal_env->cls->n_regs;
287         int i;
288
289         bitset_clear_all(chordal_env->ignore_colors);
290         be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
291         for(i = 0; i < n_colors; ++i)
292                 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
293                         bitset_set(chordal_env->ignore_colors, i);
294 }
295
296 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
297 {
298         char buf[1024];
299
300         ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
301         return fopen(buf, "wt");
302 }
303
304 void check_ifg_implementations(be_chordal_env_t *chordal_env)
305 {
306         FILE *f;
307
308         f = be_chordal_open(chordal_env, "std", "log");
309         chordal_env->ifg = be_ifg_std_new(chordal_env);
310         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
311         fclose(f);
312
313         f = be_chordal_open(chordal_env, "list", "log");
314         be_ifg_free(chordal_env->ifg);
315         chordal_env->ifg = be_ifg_list_new(chordal_env);
316         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
317         fclose(f);
318
319         f = be_chordal_open(chordal_env, "clique", "log");
320         be_ifg_free(chordal_env->ifg);
321         chordal_env->ifg = be_ifg_clique_new(chordal_env);
322         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
323         fclose(f);
324
325         f = be_chordal_open(chordal_env, "pointer", "log");
326         be_ifg_free(chordal_env->ifg);
327         chordal_env->ifg = be_ifg_pointer_new(chordal_env);
328         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
329         fclose(f);
330
331         chordal_env->ifg = NULL;
332 };
333
334 /**
335  * Checks for every reload if it's user can perform the load on itself.
336  */
337 static void memory_operand_walker(ir_node *irn, void *env) {
338         be_chordal_env_t *cenv = env;
339         const arch_env_t *aenv = cenv->birg->main_env->arch_env;
340         const ir_edge_t  *edge, *ne;
341         ir_node          *block;
342
343         if (! be_is_Reload(irn))
344                 return;
345
346         block = get_nodes_block(irn);
347
348         foreach_out_edge_safe(irn, edge, ne) {
349                 ir_node *src = get_edge_src_irn(edge);
350                 int     pos  = get_edge_src_pos(edge);
351
352                 if (! src)
353                         continue;
354
355                 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
356                         DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
357                         arch_perform_memory_operand(aenv, src, irn, pos);
358                 }
359         }
360
361         /* kill the Reload */
362         if (get_irn_n_edges(irn) == 0) {
363                 sched_remove(irn);
364                 set_irn_n(irn, 0, new_Bad());
365                 set_irn_n(irn, 1, new_Bad());
366         }
367 }
368
369 /**
370  * Starts a walk for memory operands if supported by the backend.
371  */
372 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
373         irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
374 }
375
376 /**
377  * Performs chordal register allocation for each register class on given irg.
378  *
379  * @param bi  Backend irg object
380  * @return Structure containing timer for the single phases or NULL if no timing requested.
381  */
382 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
383 {
384         const be_main_env_t *main_env  = bi->main_env;
385         const arch_isa_t    *isa       = arch_env_get_isa(main_env->arch_env);
386         ir_graph            *irg       = bi->irg;
387         be_options_t        *main_opts = main_env->options;
388         int                  splitted  = 0;
389
390         int j, m;
391         be_chordal_env_t chordal_env;
392
393         if (main_opts->timing == BE_TIME_ON) {
394                 ra_timer.t_prolog  = lc_timer_register("ra_prolog",   "regalloc prolog");
395                 ra_timer.t_epilog  = lc_timer_register("ra_epilog",   "regalloc epilog");
396                 ra_timer.t_live    = lc_timer_register("ra_liveness", "be liveness");
397                 ra_timer.t_spill   = lc_timer_register("ra_spill",    "spiller");
398                 ra_timer.t_color   = lc_timer_register("ra_color",    "graph coloring");
399                 ra_timer.t_ifg     = lc_timer_register("ra_ifg",      "interference graph");
400                 ra_timer.t_copymin = lc_timer_register("ra_copymin",  "copy minimization");
401                 ra_timer.t_ssa     = lc_timer_register("ra_ssadestr", "ssa destruction");
402                 ra_timer.t_verify  = lc_timer_register("ra_verify",   "graph verification");
403                 ra_timer.t_other   = lc_timer_register("ra_other",    "other time");
404
405                 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
406                 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
407                 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
408                 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
409                 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
410                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
411                 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
412                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
413                 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
414                 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
415         }
416
417 #define BE_TIMER_PUSH(timer)                                                        \
418         if (main_opts->timing == BE_TIME_ON) {                                          \
419                 int res = lc_timer_push(timer);                                             \
420                 if (options.vrfy_option == BE_CH_VRFY_ASSERT)                               \
421                         assert(res && "Timer already on stack, cannot be pushed twice.");       \
422                 else if (options.vrfy_option == BE_CH_VRFY_WARN && ! res)                   \
423                         fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
424                                 lc_timer_get_name(timer));                                          \
425         }
426 #define BE_TIMER_POP(timer)                                                                    \
427         if (main_opts->timing == BE_TIME_ON) {                                                     \
428                 lc_timer_t *tmp = lc_timer_pop();                                                      \
429                 if (options.vrfy_option == BE_CH_VRFY_ASSERT)                                          \
430                         assert(tmp == timer && "Attempt to pop wrong timer.");                             \
431                 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer)                       \
432                         fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
433                                 lc_timer_get_name(tmp), lc_timer_get_name(timer));                             \
434                 timer = tmp;                                                                           \
435         }
436
437         BE_TIMER_PUSH(ra_timer.t_other);
438         BE_TIMER_PUSH(ra_timer.t_prolog);
439
440         compute_doms(irg);
441
442         chordal_env.opts      = &options;
443         chordal_env.irg       = irg;
444         chordal_env.birg      = bi;
445         chordal_env.dom_front = be_compute_dominance_frontiers(irg);
446         chordal_env.exec_freq = compute_execfreq(irg, be_loop_weight);
447         chordal_env.lv        = be_liveness(irg);
448         FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
449
450         obstack_init(&chordal_env.obst);
451
452         BE_TIMER_POP(ra_timer.t_prolog);
453
454         /* Perform the following for each register class. */
455         for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
456                 FILE *f;
457                 copy_opt_t *co = NULL;
458
459                 chordal_env.cls           = arch_isa_get_reg_class(isa, j);
460                 chordal_env.border_heads  = pmap_create();
461                 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
462
463                 /* put all ignore registers into the ignore register set. */
464                 put_ignore_colors(&chordal_env);
465
466                 BE_TIMER_PUSH(ra_timer.t_live);
467                 be_liveness_recompute(chordal_env.lv);
468                 BE_TIMER_POP(ra_timer.t_live);
469                 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
470
471                 be_pre_spill_prepare_constr(&chordal_env);
472                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-constr-pre", dump_ir_block_graph_sched);
473
474                 BE_TIMER_PUSH(ra_timer.t_spill);
475
476                 /* spilling */
477                 switch(options.spill_method) {
478                 case BE_CH_SPILL_MORGAN:
479                         be_spill_morgan(&chordal_env);
480                         break;
481                 case BE_CH_SPILL_BELADY:
482                         be_spill_belady(&chordal_env);
483                         break;
484 #ifdef WITH_ILP
485                 case BE_CH_SPILL_REMAT:
486                         be_spill_remat(&chordal_env);
487                         break;
488 #endif /* WITH_ILP */
489                 default:
490                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
491                         be_spill_belady(&chordal_env);
492                 }
493
494                 BE_TIMER_POP(ra_timer.t_spill);
495
496                 DBG((chordal_env.dbg, LEVEL_1, "spill costs for %+F in regclass %s: %g\n",
497                       irg,
498                       chordal_env.cls->name,
499                       get_irg_spill_cost(&chordal_env))
500                     );
501
502                 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
503                 be_compute_spill_offsets(&chordal_env);
504                 check_for_memory_operands(&chordal_env);
505                 be_abi_fix_stack_nodes(bi->abi, chordal_env.lv);
506
507                 BE_TIMER_PUSH(ra_timer.t_verify);
508
509                 /* verify schedule and register pressure */
510                 if (options.vrfy_option == BE_CH_VRFY_WARN) {
511                         be_verify_schedule(irg);
512                         be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
513                 }
514                 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
515                         assert(be_verify_schedule(irg) && "Schedule verification failed");
516                         assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
517                                 && "Register pressure verification failed");
518                 }
519                 BE_TIMER_POP(ra_timer.t_verify);
520
521                 if (be_elr_split && ! splitted) {
522                         extreme_liverange_splitting(&chordal_env);
523                         splitted = 1;
524                 }
525
526
527                 /* Color the graph. */
528                 BE_TIMER_PUSH(ra_timer.t_color);
529                 be_ra_chordal_color(&chordal_env);
530                 BE_TIMER_POP(ra_timer.t_color);
531
532                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
533
534                 /* Create the ifg with the selected flavor */
535                 BE_TIMER_PUSH(ra_timer.t_ifg);
536                 switch (options.ifg_flavor) {
537                         default:
538                                 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
539                         case BE_CH_IFG_STD:
540                         case BE_CH_IFG_FAST:
541                                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
542                                 break;
543                         case BE_CH_IFG_CLIQUE:
544                                 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
545                                 break;
546                         case BE_CH_IFG_POINTER:
547                                 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
548                                 break;
549                         case BE_CH_IFG_LIST:
550                                 chordal_env.ifg = be_ifg_list_new(&chordal_env);
551                                 break;
552                         case BE_CH_IFG_CHECK:
553                                 check_ifg_implementations(&chordal_env);
554                                 /* Build the interference graph. */
555                                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
556                                 break;
557                 }
558                 BE_TIMER_POP(ra_timer.t_ifg);
559
560                 BE_TIMER_PUSH(ra_timer.t_verify);
561                 if (options.vrfy_option != BE_CH_VRFY_OFF)
562                         be_ra_chordal_check(&chordal_env);
563
564                 BE_TIMER_POP(ra_timer.t_verify);
565
566                 /* copy minimization */
567                 BE_TIMER_PUSH(ra_timer.t_copymin);
568                 co = NULL;
569                 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
570                         co = new_copy_opt(&chordal_env, co_get_costs_exec_freq);
571                         co_build_ou_structure(co);
572                         co_build_graph_structure(co);
573                         if(be_copymin_stats) {
574                                 ir_printf("%30F %10s %7d%7d%7d%7d", current_ir_graph, chordal_env.cls->name,
575                                                 co_get_max_copy_costs(co), co_get_copy_costs(co), co_get_inevit_copy_costs(co), co_get_lower_bound(co));
576                         }
577
578                         /* Dump the interference graph in Appel's format. */
579                         if(options.dump_flags & BE_CH_DUMP_APPEL) {
580                                 FILE *f = be_chordal_open(&chordal_env, "appel-", "apl");
581                                 co_dump_appel_graph(co, f);
582                                 fclose(f);
583                         }
584                 }
585
586                 switch(options.copymin_method) {
587                         case BE_CH_COPYMIN_HEUR1:
588                                 co_solve_heuristic(co);
589                                 break;
590                         case BE_CH_COPYMIN_HEUR2:
591                                 co_solve_heuristic_new(co);
592                                 break;
593                         case BE_CH_COPYMIN_HEUR3:
594                                 co_solve_heuristic_java(co);
595                                 break;
596                         case BE_CH_COPYMIN_PARK_MOON:
597                                 co_solve_park_moon(co);
598                                 break;
599                         case BE_CH_COPYMIN_STAT:
600                                 co_compare_solvers(&chordal_env);
601                                 break;
602 #ifdef WITH_ILP
603                         case BE_CH_COPYMIN_ILP:
604                                 co_solve_ilp2(co, 60.0);
605                                 break;
606 #endif /* WITH_ILP */
607                         case BE_CH_COPYMIN_NONE:
608                         default:
609                                 break;
610                 }
611
612                 if (co) {
613                         if(be_copymin_stats) {
614                                 int optimizable_costs = co_get_max_copy_costs(co) - co_get_lower_bound(co);
615                                 int remaining         = co_get_copy_costs(co);
616                                 int evitable          = remaining - co_get_lower_bound(co);
617
618                                 if(optimizable_costs > 0)
619                                         printf("%5d %5.2f\n", remaining, (evitable * 100.0) / optimizable_costs);
620                                 else
621                                         printf("%5d %5s\n", remaining, "-");
622                         }
623                         co_free_graph_structure(co);
624                         co_free_ou_structure(co);
625                         free_copy_opt(co);
626                 }
627                 BE_TIMER_POP(ra_timer.t_copymin);
628
629                 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
630
631                 BE_TIMER_PUSH(ra_timer.t_verify);
632
633                 if (options.vrfy_option != BE_CH_VRFY_OFF)
634                         be_ra_chordal_check(&chordal_env);
635
636                 BE_TIMER_POP(ra_timer.t_verify);
637                 BE_TIMER_PUSH(ra_timer.t_ssa);
638
639                 /* ssa destruction */
640                 be_ssa_destruction(&chordal_env);
641
642                 BE_TIMER_POP(ra_timer.t_ssa);
643
644                 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
645
646                 BE_TIMER_PUSH(ra_timer.t_verify);
647                 if (options.vrfy_option != BE_CH_VRFY_OFF) {
648                         be_ssa_destruction_check(&chordal_env);
649                         be_ra_chordal_check(&chordal_env);
650                 }
651                 BE_TIMER_POP(ra_timer.t_verify);
652
653                 if (options.copymin_method == BE_CH_COPYMIN_STAT)
654                         copystat_dump(irg);
655
656                 be_ifg_free(chordal_env.ifg);
657                 pmap_destroy(chordal_env.border_heads);
658                 bitset_free(chordal_env.ignore_colors);
659         }
660
661         BE_TIMER_PUSH(ra_timer.t_epilog);
662
663         dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
664
665         lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
666         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
667
668         obstack_free(&chordal_env.obst, NULL);
669         be_free_dominance_frontiers(chordal_env.dom_front);
670         be_liveness_free(chordal_env.lv);
671         free_execfreq(chordal_env.exec_freq);
672
673         BE_TIMER_POP(ra_timer.t_epilog);
674         BE_TIMER_POP(ra_timer.t_other);
675
676 #undef BE_TIMER_PUSH
677 #undef BE_TIMER_POP
678
679         return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
680 }
681
682 const be_ra_t be_ra_chordal_allocator = {
683 #ifdef WITH_LIBCORE
684         be_ra_chordal_register_options,
685 #endif
686         be_ra_chordal_main
687 };