2 * @file bechordal_main.c
4 * @author Sebastian Hack
6 * Copyright (C) 2005 Universitaet Karlsruhe
7 * Released under the GPL
9 * Driver for the chordal register allocator.
20 #include "firm_config.h"
23 #include <libcore/lc_opts.h>
24 #include <libcore/lc_opts_enum.h>
25 #include <libcore/lc_timing.h>
26 #endif /* WITH_LIBCORE */
29 #include "irgraph_t.h"
30 #include "irprintf_t.h"
38 #include "bechordal_t.h"
43 #include "besched_t.h"
47 #include "beifg_impl.h"
49 #include "bespillbelady.h"
50 #include "bespillmorgan.h"
54 #include "bespillilp.h"
55 #include "bespillremat.h"
56 #include "bespillappel.h"
59 #include "becopystat.h"
60 #include "becopyopt.h"
61 #include "bessadestr.h"
65 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
66 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
69 ir_node **nodes, *n1, *n2;
71 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
73 /* Collect all irns */
75 pmap_foreach(chordal_env->border_heads, pme) {
77 struct list_head *head = pme->value;
78 list_for_each_entry(border_t, curr, head, list)
79 if (curr->is_def && curr->is_real)
80 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
81 obstack_ptr_grow(&ob, curr->irn);
83 obstack_ptr_grow(&ob, NULL);
84 nodes = (ir_node **) obstack_finish(&ob);
87 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
88 const arch_register_t *n1_reg, *n2_reg;
90 n1_reg = arch_get_irn_register(arch_env, n1);
91 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
92 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
93 assert(0 && "Register constraint does not hold");
95 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
96 n2_reg = arch_get_irn_register(arch_env, n2);
97 if (values_interfere(n1, n2) && n1_reg == n2_reg) {
98 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
99 assert(0 && "Interfering values have the same color!");
103 obstack_free(&ob, NULL);
106 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
109 return be_ifg_connected(env->ifg, a, b);
111 return values_interfere(a, b);
115 static be_ra_chordal_opts_t options = {
120 BE_CH_LOWER_PERM_SWAP,
124 static be_ra_timer_t ra_timer = {
138 static const lc_opt_enum_int_items_t spill_items[] = {
139 { "morgan", BE_CH_SPILL_MORGAN },
140 { "belady", BE_CH_SPILL_BELADY },
142 { "ilp", BE_CH_SPILL_ILP },
143 { "remat", BE_CH_SPILL_REMAT },
144 { "appel", BE_CH_SPILL_APPEL },
145 #endif /* WITH_ILP */
149 static const lc_opt_enum_int_items_t copymin_items[] = {
150 { "none", BE_CH_COPYMIN_NONE },
151 { "heur1", BE_CH_COPYMIN_HEUR1 },
152 { "heur2", BE_CH_COPYMIN_HEUR2 },
153 { "stat", BE_CH_COPYMIN_STAT },
154 { "park", BE_CH_COPYMIN_PARK_MOON },
156 { "ilp1", BE_CH_COPYMIN_ILP1 },
157 { "ilp2", BE_CH_COPYMIN_ILP2 },
158 #endif /* WITH_ILP */
162 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
163 { "std", BE_CH_IFG_STD },
164 { "fast", BE_CH_IFG_FAST },
165 { "clique", BE_CH_IFG_CLIQUE },
166 { "pointer", BE_CH_IFG_POINTER },
167 { "list", BE_CH_IFG_LIST },
168 { "check", BE_CH_IFG_CHECK },
172 static const lc_opt_enum_int_items_t lower_perm_items[] = {
173 { "copy", BE_CH_LOWER_PERM_COPY },
174 { "swap", BE_CH_LOWER_PERM_SWAP },
178 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
182 static const lc_opt_enum_int_items_t dump_items[] = {
183 { "spill", BE_CH_DUMP_SPILL },
184 { "live", BE_CH_DUMP_LIVE },
185 { "color", BE_CH_DUMP_COLOR },
186 { "copymin", BE_CH_DUMP_COPYMIN },
187 { "ssadestr", BE_CH_DUMP_SSADESTR },
188 { "tree", BE_CH_DUMP_TREE_INTV },
189 { "constr", BE_CH_DUMP_CONSTR },
190 { "lower", BE_CH_DUMP_LOWER },
191 { "all", BE_CH_DUMP_ALL },
195 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
196 { "off", BE_CH_VRFY_OFF },
197 { "warn", BE_CH_VRFY_WARN },
198 { "assert", BE_CH_VRFY_ASSERT },
202 static lc_opt_enum_int_var_t spill_var = {
203 &options.spill_method, spill_items
206 static lc_opt_enum_int_var_t copymin_var = {
207 &options.copymin_method, copymin_items
210 static lc_opt_enum_int_var_t ifg_flavor_var = {
211 &options.ifg_flavor, ifg_flavor_items
214 static lc_opt_enum_int_var_t lower_perm_var = {
215 &options.lower_perm_opt, lower_perm_items
218 static lc_opt_enum_int_var_t dump_var = {
219 &options.dump_flags, dump_items
222 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
223 &options.vrfy_option, be_ch_vrfy_items
226 static const lc_opt_table_entry_t be_chordal_options[] = {
227 LC_OPT_ENT_ENUM_PTR ("spill", "spill method (belady, ilp, remat or appel)", &spill_var),
228 LC_OPT_ENT_ENUM_PTR ("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", ©min_var),
229 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
230 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options (copy or swap)", &lower_perm_var),
231 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
232 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options (off, warn, assert)", &be_ch_vrfy_var),
236 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
238 static int run_once = 0;
239 lc_opt_entry_t *chordal_grp;
243 chordal_grp = lc_opt_get_grp(grp, "chordal");
245 lc_opt_add_table(chordal_grp, be_chordal_options);
248 co_register_options(chordal_grp);
250 #endif /* WITH_LIBCORE */
252 static void dump(unsigned mask, ir_graph *irg,
253 const arch_register_class_t *cls,
255 void (*dump_func)(ir_graph *, const char *))
257 if((options.dump_flags & mask) == mask) {
260 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
261 be_dump(irg, buf, dump_func);
264 be_dump(irg, suffix, dump_func);
268 static void put_ignore_colors(be_chordal_env_t *chordal_env)
270 int n_colors = chordal_env->cls->n_regs;
273 bitset_clear_all(chordal_env->ignore_colors);
274 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
275 for(i = 0; i < n_colors; ++i)
276 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
277 bitset_set(chordal_env->ignore_colors, i);
280 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
284 ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
285 return fopen(buf, "wt");
288 void check_ifg_implementations(be_chordal_env_t *chordal_env)
292 f = be_chordal_open(chordal_env, "std", "log");
293 chordal_env->ifg = be_ifg_std_new(chordal_env);
294 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
297 f = be_chordal_open(chordal_env, "list", "log");
298 be_ifg_free(chordal_env->ifg);
299 chordal_env->ifg = be_ifg_list_new(chordal_env);
300 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
303 f = be_chordal_open(chordal_env, "clique", "log");
304 be_ifg_free(chordal_env->ifg);
305 chordal_env->ifg = be_ifg_clique_new(chordal_env);
306 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
309 f = be_chordal_open(chordal_env, "pointer", "log");
310 be_ifg_free(chordal_env->ifg);
311 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
312 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
315 chordal_env->ifg = NULL;
318 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
320 const be_main_env_t *main_env = bi->main_env;
321 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
322 ir_graph *irg = bi->irg;
323 be_options_t *main_opts = main_env->options;
327 be_chordal_env_t chordal_env;
329 if (main_opts->timing == BE_TIME_ON) {
330 ra_timer.t_prolog = lc_timer_register("prolog", "regalloc prolog");
331 ra_timer.t_epilog = lc_timer_register("epilog", "regalloc epilog");
332 ra_timer.t_live = lc_timer_register("liveness", "be liveness");
333 ra_timer.t_spill = lc_timer_register("spill", "spiller");
334 ra_timer.t_color = lc_timer_register("color", "graph coloring");
335 ra_timer.t_ifg = lc_timer_register("ifg", "interference graph");
336 ra_timer.t_copymin = lc_timer_register("copymin", "copy minimization");
337 ra_timer.t_ssa = lc_timer_register("ssadestr", "ssa destruction");
338 ra_timer.t_verify = lc_timer_register("verify", "graph verification");
339 ra_timer.t_other = lc_timer_register("other", "other time");
341 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
342 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
343 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
344 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
345 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
346 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
347 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
348 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
349 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
350 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
353 #define BE_TIMER_PUSH(timer) if (main_opts->timing == BE_TIME_ON) lc_timer_push(timer)
354 #define BE_TIMER_POP() if (main_opts->timing == BE_TIME_ON) lc_timer_pop()
356 BE_TIMER_PUSH(ra_timer.t_other);
357 BE_TIMER_PUSH(ra_timer.t_prolog);
361 chordal_env.opts = &options;
362 chordal_env.irg = irg;
363 chordal_env.birg = bi;
364 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
365 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
367 obstack_init(&chordal_env.obst);
371 /* Perform the following for each register class. */
372 for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
374 chordal_env.cls = arch_isa_get_reg_class(isa, j);
375 chordal_env.border_heads = pmap_create();
376 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
378 BE_TIMER_PUSH(ra_timer.t_live);
380 /* put all ignore registers into the ignore register set. */
381 put_ignore_colors(&chordal_env);
387 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
389 BE_TIMER_PUSH(ra_timer.t_spill);
392 switch(options.spill_method) {
393 case BE_CH_SPILL_MORGAN:
394 be_spill_morgan(&chordal_env);
396 case BE_CH_SPILL_BELADY:
397 be_spill_belady(&chordal_env);
400 case BE_CH_SPILL_ILP:
401 be_spill_ilp(&chordal_env);
403 case BE_CH_SPILL_REMAT:
404 be_spill_remat(&chordal_env);
406 case BE_CH_SPILL_APPEL:
407 be_spill_appel(&chordal_env);
409 #endif /* WITH_ILP */
411 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
412 be_spill_belady(&chordal_env);
417 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
418 be_abi_fix_stack_nodes(bi->abi);
420 BE_TIMER_PUSH(ra_timer.t_verify);
422 /* verify schedule and register pressure */
423 if (options.vrfy_option == BE_CH_VRFY_WARN) {
424 be_verify_schedule(irg);
425 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
427 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
428 assert(be_verify_schedule(irg) && "Schedule verification failed");
429 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
430 && "Register pressure verification failed");
434 BE_TIMER_PUSH(ra_timer.t_live);
437 BE_TIMER_PUSH(ra_timer.t_color);
439 /* Color the graph. */
440 be_ra_chordal_color(&chordal_env);
444 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
446 BE_TIMER_PUSH(ra_timer.t_ifg);
447 //be_ifg_check_performance(&chordal_env);
449 /* Create the ifg with the selected flavor */
450 switch (options.ifg_flavor) {
452 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
455 chordal_env.ifg = be_ifg_std_new(&chordal_env);
457 case BE_CH_IFG_CLIQUE:
458 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
460 case BE_CH_IFG_POINTER:
461 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
464 chordal_env.ifg = be_ifg_list_new(&chordal_env);
466 case BE_CH_IFG_CHECK:
467 check_ifg_implementations(&chordal_env);
468 /* Build the interference graph. */
469 chordal_env.ifg = be_ifg_std_new(&chordal_env);
470 //be_ifg_check(chordal_env.ifg);
471 //be_ifg_check_sorted(chordal_env.ifg);
478 be_ifg_t *std = be_ifg_std_new(&chordal_env);
479 f = be_chordal_open(&chordal_env, "std", "csv");
480 be_ifg_check_sorted_to_file(std, f);
485 f = be_chordal_open(&chordal_env, "clique", "csv");
486 be_ifg_check_sorted_to_file(chordal_env.ifg, f);
491 BE_TIMER_PUSH(ra_timer.t_verify);
493 if (options.vrfy_option != BE_CH_VRFY_OFF)
494 be_ra_chordal_check(&chordal_env);
496 // be_ifg_check_sorted(chordal_env.ifg);
498 BE_TIMER_PUSH(ra_timer.t_copymin);
500 /* copy minimization */
502 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
503 co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
504 co_build_ou_structure(co);
505 co_build_graph_structure(co);
508 switch(options.copymin_method) {
509 case BE_CH_COPYMIN_HEUR1:
510 co_solve_heuristic(co);
512 case BE_CH_COPYMIN_HEUR2:
513 co_solve_heuristic_new(co);
515 case BE_CH_COPYMIN_PARK_MOON:
516 co_solve_park_moon(co);
518 case BE_CH_COPYMIN_STAT:
519 co_compare_solvers(&chordal_env);
522 case BE_CH_COPYMIN_ILP1:
523 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
524 co_solve_ilp1(co, 60.0);
526 case BE_CH_COPYMIN_ILP2:
527 co_solve_ilp2(co, 60.0);
529 #endif /* WITH_ILP */
530 case BE_CH_COPYMIN_NONE:
536 co_free_graph_structure(co);
537 co_free_ou_structure(co);
543 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
545 BE_TIMER_PUSH(ra_timer.t_verify);
547 if (options.vrfy_option != BE_CH_VRFY_OFF)
548 be_ra_chordal_check(&chordal_env);
551 BE_TIMER_PUSH(ra_timer.t_ssa);
553 /* ssa destruction */
554 be_ssa_destruction(&chordal_env);
558 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
560 BE_TIMER_PUSH(ra_timer.t_verify);
561 if (options.vrfy_option != BE_CH_VRFY_OFF) {
562 be_ssa_destruction_check(&chordal_env);
563 be_ra_chordal_check(&chordal_env);
567 if (options.copymin_method == BE_CH_COPYMIN_STAT)
570 be_ifg_free(chordal_env.ifg);
571 pmap_destroy(chordal_env.border_heads);
572 bitset_free(chordal_env.ignore_colors);
575 BE_TIMER_PUSH(ra_timer.t_epilog);
577 be_compute_spill_offsets(&chordal_env);
579 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
581 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
582 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
584 obstack_free(&chordal_env.obst, NULL);
585 be_free_dominance_frontiers(chordal_env.dom_front);
593 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
596 const be_ra_t be_ra_chordal_allocator = {
598 be_ra_chordal_register_options,