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 * Checks for every reload if it's user can perform the load on itself.
325 static void memory_operand_walker(ir_node *irn, void *env) {
326 be_chordal_env_t *cenv = env;
327 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
328 const ir_edge_t *edge, *ne;
331 if (! be_is_Reload(irn))
334 block = get_nodes_block(irn);
336 foreach_out_edge_safe(irn, edge, ne) {
337 ir_node *src = get_edge_src_irn(edge);
338 int pos = get_edge_src_pos(edge);
343 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
344 DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
345 arch_perform_memory_operand(aenv, src, irn, pos);
349 /* kill the Reload */
350 if (get_irn_n_edges(irn) == 0) {
352 set_irn_n(irn, 0, new_Bad());
353 set_irn_n(irn, 1, new_Bad());
358 * Starts a walk for memory operands if supported by the backend.
360 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
361 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
365 * Performs chordal register allocation for each register class on given irg.
367 * @param bi Backend irg object
368 * @return Structure containing timer for the single phases or NULL if no timing requested.
370 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
372 const be_main_env_t *main_env = bi->main_env;
373 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
374 ir_graph *irg = bi->irg;
375 be_options_t *main_opts = main_env->options;
378 be_chordal_env_t chordal_env;
380 if (main_opts->timing == BE_TIME_ON) {
381 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
382 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
383 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
384 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
385 ra_timer.t_color = lc_timer_register("ra_color", "graph coloring");
386 ra_timer.t_ifg = lc_timer_register("ra_ifg", "interference graph");
387 ra_timer.t_copymin = lc_timer_register("ra_copymin", "copy minimization");
388 ra_timer.t_ssa = lc_timer_register("ra_ssadestr", "ssa destruction");
389 ra_timer.t_verify = lc_timer_register("ra_verify", "graph verification");
390 ra_timer.t_other = lc_timer_register("ra_other", "other time");
392 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
393 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
394 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
395 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
396 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
397 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
398 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
399 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
400 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
401 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
404 #define BE_TIMER_PUSH(timer) \
405 if (main_opts->timing == BE_TIME_ON) { \
406 int res = lc_timer_push(timer); \
407 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
408 assert(res && "Timer already on stack, cannot be pushed twice."); \
409 else if (options.vrfy_option == BE_CH_VRFY_WARN && ! res) \
410 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
411 lc_timer_get_name(timer)); \
413 #define BE_TIMER_POP(timer) \
414 if (main_opts->timing == BE_TIME_ON) { \
415 lc_timer_t *tmp = lc_timer_pop(); \
416 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
417 assert(tmp == timer && "Attempt to pop wrong timer."); \
418 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
419 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
420 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
424 BE_TIMER_PUSH(ra_timer.t_other);
425 BE_TIMER_PUSH(ra_timer.t_prolog);
429 chordal_env.opts = &options;
430 chordal_env.irg = irg;
431 chordal_env.birg = bi;
432 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
433 chordal_env.exec_freq = compute_execfreq(irg, be_loop_weight);
434 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
436 obstack_init(&chordal_env.obst);
438 BE_TIMER_POP(ra_timer.t_prolog);
440 /* Perform the following for each register class. */
441 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
443 copy_opt_t *co = NULL;
445 chordal_env.cls = arch_isa_get_reg_class(isa, j);
446 chordal_env.border_heads = pmap_create();
447 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
449 BE_TIMER_PUSH(ra_timer.t_live);
451 /* put all ignore registers into the ignore register set. */
452 put_ignore_colors(&chordal_env);
456 BE_TIMER_POP(ra_timer.t_live);
458 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
460 BE_TIMER_PUSH(ra_timer.t_spill);
463 switch(options.spill_method) {
464 case BE_CH_SPILL_MORGAN:
465 be_spill_morgan(&chordal_env);
467 case BE_CH_SPILL_BELADY:
468 be_spill_belady(&chordal_env);
471 case BE_CH_SPILL_REMAT:
472 be_spill_remat(&chordal_env);
474 #endif /* WITH_ILP */
476 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
477 be_spill_belady(&chordal_env);
480 BE_TIMER_POP(ra_timer.t_spill);
482 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
483 be_abi_fix_stack_nodes(bi->abi);
484 be_compute_spill_offsets(&chordal_env);
485 check_for_memory_operands(&chordal_env);
487 BE_TIMER_PUSH(ra_timer.t_verify);
489 /* verify schedule and register pressure */
490 if (options.vrfy_option == BE_CH_VRFY_WARN) {
491 be_verify_schedule(irg);
492 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
494 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
495 assert(be_verify_schedule(irg) && "Schedule verification failed");
496 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
497 && "Register pressure verification failed");
500 BE_TIMER_POP(ra_timer.t_verify);
501 BE_TIMER_PUSH(ra_timer.t_live);
503 BE_TIMER_POP(ra_timer.t_live);
504 BE_TIMER_PUSH(ra_timer.t_color);
506 /* Color the graph. */
507 be_ra_chordal_color(&chordal_env);
509 BE_TIMER_POP(ra_timer.t_color);
511 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
513 BE_TIMER_PUSH(ra_timer.t_ifg);
515 /* Create the ifg with the selected flavor */
516 switch (options.ifg_flavor) {
518 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
521 chordal_env.ifg = be_ifg_std_new(&chordal_env);
523 case BE_CH_IFG_CLIQUE:
524 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
526 case BE_CH_IFG_POINTER:
527 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
530 chordal_env.ifg = be_ifg_list_new(&chordal_env);
532 case BE_CH_IFG_CHECK:
533 check_ifg_implementations(&chordal_env);
534 /* Build the interference graph. */
535 chordal_env.ifg = be_ifg_std_new(&chordal_env);
542 be_ifg_t *std = be_ifg_std_new(&chordal_env);
543 f = be_chordal_open(&chordal_env, "std", "csv");
544 be_ifg_check_sorted_to_file(std, f);
549 f = be_chordal_open(&chordal_env, "clique", "csv");
550 be_ifg_check_sorted_to_file(chordal_env.ifg, f);
553 BE_TIMER_POP(ra_timer.t_ifg);
555 BE_TIMER_PUSH(ra_timer.t_verify);
557 if (options.vrfy_option != BE_CH_VRFY_OFF)
558 be_ra_chordal_check(&chordal_env);
560 // be_ifg_check_sorted(chordal_env.ifg);
561 BE_TIMER_POP(ra_timer.t_verify);
562 BE_TIMER_PUSH(ra_timer.t_copymin);
564 /* copy minimization */
565 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
567 co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
568 co_build_ou_structure(co);
569 co_build_graph_structure(co);
570 if(be_copymin_stats) {
571 ir_printf("%40F %20s\n", current_ir_graph, chordal_env.cls->name);
572 printf("max copy costs: %d\n", co_get_max_copy_costs(co));
573 printf("init copy costs: %d\n", co_get_copy_costs(co));
574 printf("inevit copy costs: %d\n", co_get_inevit_copy_costs(co));
575 printf("copy costs lower bound: %d\n", co_get_lower_bound(co));
579 f = be_chordal_open(&chordal_env, "appel-", "apl");
580 co_dump_appel_graph(co, f);
582 f = be_chordal_open(&chordal_env, "appel-clique-", "p");
583 co_dump_appel_graph_cliques(co, f);
588 switch(options.copymin_method) {
589 case BE_CH_COPYMIN_HEUR1:
590 co_solve_heuristic(co);
592 case BE_CH_COPYMIN_HEUR2:
593 co_solve_heuristic_new(co);
595 case BE_CH_COPYMIN_PARK_MOON:
596 co_solve_park_moon(co);
598 case BE_CH_COPYMIN_STAT:
599 co_compare_solvers(&chordal_env);
602 case BE_CH_COPYMIN_ILP1:
603 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
604 co_solve_ilp1(co, 60.0);
606 case BE_CH_COPYMIN_ILP2:
607 co_solve_ilp2(co, 60.0);
609 #endif /* WITH_ILP */
610 case BE_CH_COPYMIN_NONE:
616 if(be_copymin_stats) {
617 printf("final copy costs : %d\n", co_get_copy_costs(co));
619 co_free_graph_structure(co);
620 co_free_ou_structure(co);
624 BE_TIMER_POP(ra_timer.t_copymin);
626 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
628 BE_TIMER_PUSH(ra_timer.t_verify);
630 if (options.vrfy_option != BE_CH_VRFY_OFF)
631 be_ra_chordal_check(&chordal_env);
633 BE_TIMER_POP(ra_timer.t_verify);
634 BE_TIMER_PUSH(ra_timer.t_ssa);
636 /* ssa destruction */
637 be_ssa_destruction(&chordal_env);
639 BE_TIMER_POP(ra_timer.t_ssa);
641 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
643 BE_TIMER_PUSH(ra_timer.t_verify);
644 if (options.vrfy_option != BE_CH_VRFY_OFF) {
645 be_ssa_destruction_check(&chordal_env);
646 be_ra_chordal_check(&chordal_env);
648 BE_TIMER_POP(ra_timer.t_verify);
650 if (options.copymin_method == BE_CH_COPYMIN_STAT)
653 be_ifg_free(chordal_env.ifg);
654 pmap_destroy(chordal_env.border_heads);
655 bitset_free(chordal_env.ignore_colors);
658 BE_TIMER_PUSH(ra_timer.t_epilog);
660 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
662 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
663 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
665 obstack_free(&chordal_env.obst, NULL);
666 be_free_dominance_frontiers(chordal_env.dom_front);
667 free_execfreq(chordal_env.exec_freq);
669 BE_TIMER_POP(ra_timer.t_epilog);
670 BE_TIMER_POP(ra_timer.t_other);
675 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
678 const be_ra_t be_ra_chordal_allocator = {
680 be_ra_chordal_register_options,