2 * @file bechordal_main.c
4 * @author Sebastian Hack
7 * Copyright (C) 2005 Universitaet Karlsruhe
8 * Released under the GPL
10 * Driver for the chordal register allocator.
21 #include "firm_config.h"
24 #include <libcore/lc_opts.h>
25 #include <libcore/lc_opts_enum.h>
26 #include <libcore/lc_timing.h>
27 #endif /* WITH_LIBCORE */
30 #include "irgraph_t.h"
31 #include "irprintf_t.h"
40 #include "bechordal_t.h"
45 #include "besched_t.h"
49 #include "beifg_impl.h"
51 #include "bespillbelady.h"
52 #include "bespillmorgan.h"
56 #include "bespillremat.h"
59 #include "becopystat.h"
60 #include "becopyopt.h"
61 #include "bessadestr.h"
64 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
65 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
68 ir_node **nodes, *n1, *n2;
70 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
72 /* Collect all irns */
74 pmap_foreach(chordal_env->border_heads, pme) {
76 struct list_head *head = pme->value;
77 list_for_each_entry(border_t, curr, head, list)
78 if (curr->is_def && curr->is_real)
79 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
80 obstack_ptr_grow(&ob, curr->irn);
82 obstack_ptr_grow(&ob, NULL);
83 nodes = (ir_node **) obstack_finish(&ob);
86 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
87 const arch_register_t *n1_reg, *n2_reg;
89 n1_reg = arch_get_irn_register(arch_env, n1);
90 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
91 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
92 assert(0 && "Register constraint does not hold");
94 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
95 n2_reg = arch_get_irn_register(arch_env, n2);
96 if (values_interfere(n1, n2) && n1_reg == n2_reg) {
97 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
98 assert(0 && "Interfering values have the same color!");
102 obstack_free(&ob, NULL);
105 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
108 return be_ifg_connected(env->ifg, a, b);
110 return values_interfere(a, b);
114 static be_ra_chordal_opts_t options = {
119 BE_CH_LOWER_PERM_SWAP,
123 static be_ra_timer_t ra_timer = {
137 static const lc_opt_enum_int_items_t spill_items[] = {
138 { "morgan", BE_CH_SPILL_MORGAN },
139 { "belady", BE_CH_SPILL_BELADY },
141 { "remat", BE_CH_SPILL_REMAT },
142 #endif /* WITH_ILP */
146 static const lc_opt_enum_int_items_t copymin_items[] = {
147 { "none", BE_CH_COPYMIN_NONE },
148 { "heur1", BE_CH_COPYMIN_HEUR1 },
149 { "heur2", BE_CH_COPYMIN_HEUR2 },
150 { "stat", BE_CH_COPYMIN_STAT },
151 { "park", BE_CH_COPYMIN_PARK_MOON },
153 { "ilp1", BE_CH_COPYMIN_ILP1 },
154 { "ilp2", BE_CH_COPYMIN_ILP2 },
155 #endif /* WITH_ILP */
159 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
160 { "std", BE_CH_IFG_STD },
161 { "fast", BE_CH_IFG_FAST },
162 { "clique", BE_CH_IFG_CLIQUE },
163 { "pointer", BE_CH_IFG_POINTER },
164 { "list", BE_CH_IFG_LIST },
165 { "check", BE_CH_IFG_CHECK },
169 static const lc_opt_enum_int_items_t lower_perm_items[] = {
170 { "copy", BE_CH_LOWER_PERM_COPY },
171 { "swap", BE_CH_LOWER_PERM_SWAP },
175 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
179 static const lc_opt_enum_int_items_t dump_items[] = {
180 { "spill", BE_CH_DUMP_SPILL },
181 { "live", BE_CH_DUMP_LIVE },
182 { "color", BE_CH_DUMP_COLOR },
183 { "copymin", BE_CH_DUMP_COPYMIN },
184 { "ssadestr", BE_CH_DUMP_SSADESTR },
185 { "tree", BE_CH_DUMP_TREE_INTV },
186 { "constr", BE_CH_DUMP_CONSTR },
187 { "lower", BE_CH_DUMP_LOWER },
188 { "all", BE_CH_DUMP_ALL },
192 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
193 { "off", BE_CH_VRFY_OFF },
194 { "warn", BE_CH_VRFY_WARN },
195 { "assert", BE_CH_VRFY_ASSERT },
199 static lc_opt_enum_int_var_t spill_var = {
200 &options.spill_method, spill_items
203 static lc_opt_enum_int_var_t copymin_var = {
204 &options.copymin_method, copymin_items
207 static lc_opt_enum_int_var_t ifg_flavor_var = {
208 &options.ifg_flavor, ifg_flavor_items
211 static lc_opt_enum_int_var_t lower_perm_var = {
212 &options.lower_perm_opt, lower_perm_items
215 static lc_opt_enum_int_var_t dump_var = {
216 &options.dump_flags, dump_items
219 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
220 &options.vrfy_option, be_ch_vrfy_items
223 static int be_copymin_stats = 0;
225 /** Assumed loop iteration count for execution frequency estimation. */
226 static int be_loop_weight = 9;
228 static const lc_opt_table_entry_t be_chordal_options[] = {
229 LC_OPT_ENT_ENUM_INT ("spill", "spill method (belady, morgan or remat)", &spill_var),
230 LC_OPT_ENT_ENUM_PTR ("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", ©min_var),
231 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
232 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options (copy or swap)", &lower_perm_var),
233 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
234 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options (off, warn, assert)", &be_ch_vrfy_var),
235 LC_OPT_ENT_BOOL ("copymin_stats", "dump statistics of copy minimization", &be_copymin_stats),
236 LC_OPT_ENT_INT ("loop_weight", "assumed amount of loop iterations for guessing the execution frequency", &be_loop_weight),
240 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
242 static int run_once = 0;
243 lc_opt_entry_t *chordal_grp;
247 chordal_grp = lc_opt_get_grp(grp, "chordal");
249 lc_opt_add_table(chordal_grp, be_chordal_options);
252 co_register_options(chordal_grp);
254 #endif /* WITH_LIBCORE */
256 static void dump(unsigned mask, ir_graph *irg,
257 const arch_register_class_t *cls,
259 void (*dump_func)(ir_graph *, const char *))
261 if((options.dump_flags & mask) == mask) {
264 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
265 be_dump(irg, buf, dump_func);
268 be_dump(irg, suffix, dump_func);
272 static void put_ignore_colors(be_chordal_env_t *chordal_env)
274 int n_colors = chordal_env->cls->n_regs;
277 bitset_clear_all(chordal_env->ignore_colors);
278 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
279 for(i = 0; i < n_colors; ++i)
280 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
281 bitset_set(chordal_env->ignore_colors, i);
284 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
288 ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
289 return fopen(buf, "wt");
292 void check_ifg_implementations(be_chordal_env_t *chordal_env)
296 f = be_chordal_open(chordal_env, "std", "log");
297 chordal_env->ifg = be_ifg_std_new(chordal_env);
298 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
301 f = be_chordal_open(chordal_env, "list", "log");
302 be_ifg_free(chordal_env->ifg);
303 chordal_env->ifg = be_ifg_list_new(chordal_env);
304 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
307 f = be_chordal_open(chordal_env, "clique", "log");
308 be_ifg_free(chordal_env->ifg);
309 chordal_env->ifg = be_ifg_clique_new(chordal_env);
310 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
313 f = be_chordal_open(chordal_env, "pointer", "log");
314 be_ifg_free(chordal_env->ifg);
315 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
316 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
319 chordal_env->ifg = NULL;
323 * Performs chordal register allocation for each register class on given irg.
325 * @param bi Backend irg object
326 * @return Structure containing timer for the single phases or NULL if no timing requested.
328 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
330 const be_main_env_t *main_env = bi->main_env;
331 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
332 ir_graph *irg = bi->irg;
333 be_options_t *main_opts = main_env->options;
336 be_chordal_env_t chordal_env;
338 if (main_opts->timing == BE_TIME_ON) {
339 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
340 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
341 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
342 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
343 ra_timer.t_color = lc_timer_register("ra_color", "graph coloring");
344 ra_timer.t_ifg = lc_timer_register("ra_ifg", "interference graph");
345 ra_timer.t_copymin = lc_timer_register("ra_copymin", "copy minimization");
346 ra_timer.t_ssa = lc_timer_register("ra_ssadestr", "ssa destruction");
347 ra_timer.t_verify = lc_timer_register("ra_verify", "graph verification");
348 ra_timer.t_other = lc_timer_register("ra_other", "other time");
350 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
351 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
352 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
353 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
354 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
355 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
356 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
357 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
358 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
359 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
362 #define BE_TIMER_PUSH(timer) \
363 if (main_opts->timing == BE_TIME_ON) { \
364 int res = lc_timer_push(timer); \
365 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
366 assert(res && "Timer already on stack, cannot be pushed twice."); \
367 else if (options.vrfy_option == BE_CH_VRFY_WARN && ! res) \
368 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
369 lc_timer_get_name(timer)); \
371 #define BE_TIMER_POP(timer) \
372 if (main_opts->timing == BE_TIME_ON) { \
373 lc_timer_t *tmp = lc_timer_pop(); \
374 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
375 assert(tmp == timer && "Attempt to pop wrong timer."); \
376 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
377 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
378 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
382 BE_TIMER_PUSH(ra_timer.t_other);
383 BE_TIMER_PUSH(ra_timer.t_prolog);
387 chordal_env.opts = &options;
388 chordal_env.irg = irg;
389 chordal_env.birg = bi;
390 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
391 chordal_env.exec_freq = compute_execfreq(irg, be_loop_weight);
392 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
394 obstack_init(&chordal_env.obst);
396 BE_TIMER_POP(ra_timer.t_prolog);
398 /* Perform the following for each register class. */
399 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
401 copy_opt_t *co = NULL;
403 chordal_env.cls = arch_isa_get_reg_class(isa, j);
404 chordal_env.border_heads = pmap_create();
405 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
407 BE_TIMER_PUSH(ra_timer.t_live);
409 /* put all ignore registers into the ignore register set. */
410 put_ignore_colors(&chordal_env);
414 BE_TIMER_POP(ra_timer.t_live);
416 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
418 BE_TIMER_PUSH(ra_timer.t_spill);
421 switch(options.spill_method) {
422 case BE_CH_SPILL_MORGAN:
423 be_spill_morgan(&chordal_env);
425 case BE_CH_SPILL_BELADY:
426 be_spill_belady(&chordal_env);
429 case BE_CH_SPILL_REMAT:
430 be_spill_remat(&chordal_env);
432 #endif /* WITH_ILP */
434 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
435 be_spill_belady(&chordal_env);
438 BE_TIMER_POP(ra_timer.t_spill);
440 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
441 be_abi_fix_stack_nodes(bi->abi);
443 BE_TIMER_PUSH(ra_timer.t_verify);
445 /* verify schedule and register pressure */
446 if (options.vrfy_option == BE_CH_VRFY_WARN) {
447 be_verify_schedule(irg);
448 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
450 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
451 assert(be_verify_schedule(irg) && "Schedule verification failed");
452 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
453 && "Register pressure verification failed");
456 BE_TIMER_POP(ra_timer.t_verify);
457 BE_TIMER_PUSH(ra_timer.t_live);
459 BE_TIMER_POP(ra_timer.t_live);
460 BE_TIMER_PUSH(ra_timer.t_color);
462 /* Color the graph. */
463 be_ra_chordal_color(&chordal_env);
465 BE_TIMER_POP(ra_timer.t_color);
467 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
469 BE_TIMER_PUSH(ra_timer.t_ifg);
471 /* Create the ifg with the selected flavor */
472 switch (options.ifg_flavor) {
474 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
477 chordal_env.ifg = be_ifg_std_new(&chordal_env);
479 case BE_CH_IFG_CLIQUE:
480 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
482 case BE_CH_IFG_POINTER:
483 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
486 chordal_env.ifg = be_ifg_list_new(&chordal_env);
488 case BE_CH_IFG_CHECK:
489 check_ifg_implementations(&chordal_env);
490 /* Build the interference graph. */
491 chordal_env.ifg = be_ifg_std_new(&chordal_env);
498 be_ifg_t *std = be_ifg_std_new(&chordal_env);
499 f = be_chordal_open(&chordal_env, "std", "csv");
500 be_ifg_check_sorted_to_file(std, f);
505 f = be_chordal_open(&chordal_env, "clique", "csv");
506 be_ifg_check_sorted_to_file(chordal_env.ifg, f);
509 BE_TIMER_POP(ra_timer.t_ifg);
511 BE_TIMER_PUSH(ra_timer.t_verify);
513 if (options.vrfy_option != BE_CH_VRFY_OFF)
514 be_ra_chordal_check(&chordal_env);
516 // be_ifg_check_sorted(chordal_env.ifg);
517 BE_TIMER_POP(ra_timer.t_verify);
518 BE_TIMER_PUSH(ra_timer.t_copymin);
520 /* copy minimization */
521 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
523 co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
524 co_build_ou_structure(co);
525 co_build_graph_structure(co);
526 if(be_copymin_stats) {
527 ir_printf("%40F %20s\n", current_ir_graph, chordal_env.cls->name);
528 printf("max copy costs: %d\n", co_get_max_copy_costs(co));
529 printf("init copy costs: %d\n", co_get_copy_costs(co));
530 printf("inevit copy costs: %d\n", co_get_inevit_copy_costs(co));
531 printf("copy costs lower bound: %d\n", co_get_lower_bound(co));
535 f = be_chordal_open(&chordal_env, "appel-", "apl");
536 co_dump_appel_graph(co, f);
538 f = be_chordal_open(&chordal_env, "appel-clique-", "p");
539 co_dump_appel_graph_cliques(co, f);
544 switch(options.copymin_method) {
545 case BE_CH_COPYMIN_HEUR1:
546 co_solve_heuristic(co);
548 case BE_CH_COPYMIN_HEUR2:
549 co_solve_heuristic_new(co);
551 case BE_CH_COPYMIN_PARK_MOON:
552 co_solve_park_moon(co);
554 case BE_CH_COPYMIN_STAT:
555 co_compare_solvers(&chordal_env);
558 case BE_CH_COPYMIN_ILP1:
559 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
560 co_solve_ilp1(co, 60.0);
562 case BE_CH_COPYMIN_ILP2:
563 co_solve_ilp2(co, 60.0);
565 #endif /* WITH_ILP */
566 case BE_CH_COPYMIN_NONE:
572 if(be_copymin_stats) {
573 printf("final copy costs : %d\n", co_get_copy_costs(co));
575 co_free_graph_structure(co);
576 co_free_ou_structure(co);
580 BE_TIMER_POP(ra_timer.t_copymin);
582 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
584 BE_TIMER_PUSH(ra_timer.t_verify);
586 if (options.vrfy_option != BE_CH_VRFY_OFF)
587 be_ra_chordal_check(&chordal_env);
589 BE_TIMER_POP(ra_timer.t_verify);
590 BE_TIMER_PUSH(ra_timer.t_ssa);
592 /* ssa destruction */
593 be_ssa_destruction(&chordal_env);
595 BE_TIMER_POP(ra_timer.t_ssa);
597 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
599 BE_TIMER_PUSH(ra_timer.t_verify);
600 if (options.vrfy_option != BE_CH_VRFY_OFF) {
601 be_ssa_destruction_check(&chordal_env);
602 be_ra_chordal_check(&chordal_env);
604 BE_TIMER_POP(ra_timer.t_verify);
606 if (options.copymin_method == BE_CH_COPYMIN_STAT)
609 be_ifg_free(chordal_env.ifg);
610 pmap_destroy(chordal_env.border_heads);
611 bitset_free(chordal_env.ignore_colors);
614 BE_TIMER_PUSH(ra_timer.t_epilog);
616 be_compute_spill_offsets(&chordal_env);
618 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
620 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
621 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
623 obstack_free(&chordal_env.obst, NULL);
624 be_free_dominance_frontiers(chordal_env.dom_front);
625 free_execfreq(chordal_env.exec_freq);
627 BE_TIMER_POP(ra_timer.t_epilog);
628 BE_TIMER_POP(ra_timer.t_other);
633 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
636 const be_ra_t be_ra_chordal_allocator = {
638 be_ra_chordal_register_options,