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"
42 #include "bechordal_t.h"
44 #include "bejavacoal.h"
48 #include "besched_t.h"
52 #include "beifg_impl.h"
55 #include "bespillbelady.h"
56 #include "bespillmorgan.h"
60 #include "bespillremat.h"
63 #include "becopystat.h"
64 #include "becopyopt.h"
65 #include "bessadestr.h"
67 #include "bespillcost.h"
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;
73 ir_node **nodes, *n1, *n2;
75 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
77 /* Collect all irns */
79 pmap_foreach(chordal_env->border_heads, pme) {
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);
87 obstack_ptr_grow(&ob, NULL);
88 nodes = (ir_node **) obstack_finish(&ob);
91 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
92 const arch_register_t *n1_reg, *n2_reg;
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");
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!");
107 obstack_free(&ob, NULL);
110 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
113 return be_ifg_connected(env->ifg, a, b);
115 return values_interfere(env->lv, a, b);
119 static be_ra_chordal_opts_t options = {
124 BE_CH_LOWER_PERM_SWAP,
128 static be_ra_timer_t ra_timer = {
142 static const lc_opt_enum_int_items_t spill_items[] = {
143 { "morgan", BE_CH_SPILL_MORGAN },
144 { "belady", BE_CH_SPILL_BELADY },
146 { "remat", BE_CH_SPILL_REMAT },
147 #endif /* WITH_ILP */
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 },
159 { "ilp", BE_CH_COPYMIN_ILP },
160 #endif /* WITH_ILP */
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 },
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 },
180 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
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 },
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 },
205 static lc_opt_enum_int_var_t spill_var = {
206 &options.spill_method, spill_items
209 static lc_opt_enum_int_var_t copymin_var = {
210 &options.copymin_method, copymin_items
213 static lc_opt_enum_int_var_t ifg_flavor_var = {
214 &options.ifg_flavor, ifg_flavor_items
217 static lc_opt_enum_int_var_t lower_perm_var = {
218 &options.lower_perm_opt, lower_perm_items
221 static lc_opt_enum_int_var_t dump_var = {
222 &options.dump_flags, dump_items
225 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
226 &options.vrfy_option, be_ch_vrfy_items
229 /** Dump copy minimization statistics. */
230 static int be_copymin_stats = 0;
232 /** Enable extreme live range splitting. */
233 static int be_elr_split = 0;
235 /** Assumed loop iteration count for execution frequency estimation. */
236 static int be_loop_weight = 9;
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)", ©min_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),
251 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
253 static int run_once = 0;
254 lc_opt_entry_t *chordal_grp;
258 chordal_grp = lc_opt_get_grp(grp, "chordal");
260 lc_opt_add_table(chordal_grp, be_chordal_options);
263 co_register_options(chordal_grp);
264 java_coal_register_options(chordal_grp);
266 #endif /* WITH_LIBCORE */
268 static void dump(unsigned mask, ir_graph *irg,
269 const arch_register_class_t *cls,
271 void (*dump_func)(ir_graph *, const char *))
273 if((options.dump_flags & mask) == mask) {
276 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
277 be_dump(irg, buf, dump_func);
280 be_dump(irg, suffix, dump_func);
284 static void put_ignore_colors(be_chordal_env_t *chordal_env)
286 int n_colors = chordal_env->cls->n_regs;
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);
296 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
300 ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
301 return fopen(buf, "wt");
304 void check_ifg_implementations(be_chordal_env_t *chordal_env)
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);
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);
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);
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);
331 chordal_env->ifg = NULL;
335 * Checks for every reload if it's user can perform the load on itself.
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;
343 if (! be_is_Reload(irn))
346 block = get_nodes_block(irn);
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);
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);
361 /* kill the Reload */
362 if (get_irn_n_edges(irn) == 0) {
364 set_irn_n(irn, 0, new_Bad());
365 set_irn_n(irn, 1, new_Bad());
370 * Starts a walk for memory operands if supported by the backend.
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);
377 * Performs chordal register allocation for each register class on given irg.
379 * @param bi Backend irg object
380 * @return Structure containing timer for the single phases or NULL if no timing requested.
382 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
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;
391 be_chordal_env_t chordal_env;
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");
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);
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)); \
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)); \
437 BE_TIMER_PUSH(ra_timer.t_other);
438 BE_TIMER_PUSH(ra_timer.t_prolog);
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");
450 obstack_init(&chordal_env.obst);
452 BE_TIMER_POP(ra_timer.t_prolog);
454 /* Perform the following for each register class. */
455 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
457 copy_opt_t *co = NULL;
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);
463 /* put all ignore registers into the ignore register set. */
464 put_ignore_colors(&chordal_env);
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);
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);
474 BE_TIMER_PUSH(ra_timer.t_spill);
477 switch(options.spill_method) {
478 case BE_CH_SPILL_MORGAN:
479 be_spill_morgan(&chordal_env);
481 case BE_CH_SPILL_BELADY:
482 be_spill_belady(&chordal_env);
485 case BE_CH_SPILL_REMAT:
486 be_spill_remat(&chordal_env);
488 #endif /* WITH_ILP */
490 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
491 be_spill_belady(&chordal_env);
494 BE_TIMER_POP(ra_timer.t_spill);
496 DBG((chordal_env.dbg, LEVEL_1, "spill costs for %+F in regclass %s: %g\n",
498 chordal_env.cls->name,
499 get_irg_spill_cost(&chordal_env))
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);
507 BE_TIMER_PUSH(ra_timer.t_verify);
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);
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");
519 BE_TIMER_POP(ra_timer.t_verify);
521 if (be_elr_split && ! splitted) {
522 extreme_liverange_splitting(&chordal_env);
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);
532 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
534 /* Create the ifg with the selected flavor */
535 BE_TIMER_PUSH(ra_timer.t_ifg);
536 switch (options.ifg_flavor) {
538 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
541 chordal_env.ifg = be_ifg_std_new(&chordal_env);
543 case BE_CH_IFG_CLIQUE:
544 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
546 case BE_CH_IFG_POINTER:
547 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
550 chordal_env.ifg = be_ifg_list_new(&chordal_env);
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);
558 BE_TIMER_POP(ra_timer.t_ifg);
560 BE_TIMER_PUSH(ra_timer.t_verify);
561 if (options.vrfy_option != BE_CH_VRFY_OFF)
562 be_ra_chordal_check(&chordal_env);
564 BE_TIMER_POP(ra_timer.t_verify);
566 /* copy minimization */
567 BE_TIMER_PUSH(ra_timer.t_copymin);
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));
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);
586 switch(options.copymin_method) {
587 case BE_CH_COPYMIN_HEUR1:
588 co_solve_heuristic(co);
590 case BE_CH_COPYMIN_HEUR2:
591 co_solve_heuristic_new(co);
593 case BE_CH_COPYMIN_HEUR3:
594 co_solve_heuristic_java(co);
596 case BE_CH_COPYMIN_PARK_MOON:
597 co_solve_park_moon(co);
599 case BE_CH_COPYMIN_STAT:
600 co_compare_solvers(&chordal_env);
603 case BE_CH_COPYMIN_ILP:
604 co_solve_ilp2(co, 60.0);
606 #endif /* WITH_ILP */
607 case BE_CH_COPYMIN_NONE:
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);
618 if(optimizable_costs > 0)
619 printf("%5d %5.2f\n", remaining, (evitable * 100.0) / optimizable_costs);
621 printf("%5d %5s\n", remaining, "-");
623 co_free_graph_structure(co);
624 co_free_ou_structure(co);
627 BE_TIMER_POP(ra_timer.t_copymin);
629 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
631 BE_TIMER_PUSH(ra_timer.t_verify);
633 if (options.vrfy_option != BE_CH_VRFY_OFF)
634 be_ra_chordal_check(&chordal_env);
636 BE_TIMER_POP(ra_timer.t_verify);
637 BE_TIMER_PUSH(ra_timer.t_ssa);
639 /* ssa destruction */
640 be_ssa_destruction(&chordal_env);
642 BE_TIMER_POP(ra_timer.t_ssa);
644 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
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);
651 BE_TIMER_POP(ra_timer.t_verify);
653 if (options.copymin_method == BE_CH_COPYMIN_STAT)
656 be_ifg_free(chordal_env.ifg);
657 pmap_destroy(chordal_env.border_heads);
658 bitset_free(chordal_env.ignore_colors);
661 BE_TIMER_PUSH(ra_timer.t_epilog);
663 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
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);
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);
673 BE_TIMER_POP(ra_timer.t_epilog);
674 BE_TIMER_POP(ra_timer.t_other);
679 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
682 const be_ra_t be_ra_chordal_allocator = {
684 be_ra_chordal_register_options,