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"
42 #include "bejavacoal.h"
46 #include "besched_t.h"
50 #include "beifg_impl.h"
52 #include "bespillbelady.h"
53 #include "bespillmorgan.h"
57 #include "bespillremat.h"
60 #include "becopystat.h"
61 #include "becopyopt.h"
62 #include "bessadestr.h"
64 #include "bespillcost.h"
66 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
67 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
70 ir_node **nodes, *n1, *n2;
72 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
74 /* Collect all irns */
76 pmap_foreach(chordal_env->border_heads, pme) {
78 struct list_head *head = pme->value;
79 list_for_each_entry(border_t, curr, head, list)
80 if (curr->is_def && curr->is_real)
81 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
82 obstack_ptr_grow(&ob, curr->irn);
84 obstack_ptr_grow(&ob, NULL);
85 nodes = (ir_node **) obstack_finish(&ob);
88 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
89 const arch_register_t *n1_reg, *n2_reg;
91 n1_reg = arch_get_irn_register(arch_env, n1);
92 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
93 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
94 assert(0 && "Register constraint does not hold");
96 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
97 n2_reg = arch_get_irn_register(arch_env, n2);
98 if (values_interfere(chordal_env->lv, n1, n2) && n1_reg == n2_reg) {
99 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
100 assert(0 && "Interfering values have the same color!");
104 obstack_free(&ob, NULL);
107 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
110 return be_ifg_connected(env->ifg, a, b);
112 return values_interfere(env->lv, a, b);
116 static be_ra_chordal_opts_t options = {
121 BE_CH_LOWER_PERM_SWAP,
125 static be_ra_timer_t ra_timer = {
139 static const lc_opt_enum_int_items_t spill_items[] = {
140 { "morgan", BE_CH_SPILL_MORGAN },
141 { "belady", BE_CH_SPILL_BELADY },
143 { "remat", BE_CH_SPILL_REMAT },
144 #endif /* WITH_ILP */
148 static const lc_opt_enum_int_items_t copymin_items[] = {
149 { "none", BE_CH_COPYMIN_NONE },
150 { "heur1", BE_CH_COPYMIN_HEUR1 },
151 { "heur2", BE_CH_COPYMIN_HEUR2 },
152 { "heur3", BE_CH_COPYMIN_HEUR3 },
153 { "stat", BE_CH_COPYMIN_STAT },
154 { "park", BE_CH_COPYMIN_PARK_MOON },
156 { "ilp", BE_CH_COPYMIN_ILP },
157 #endif /* WITH_ILP */
161 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
162 { "std", BE_CH_IFG_STD },
163 { "fast", BE_CH_IFG_FAST },
164 { "clique", BE_CH_IFG_CLIQUE },
165 { "pointer", BE_CH_IFG_POINTER },
166 { "list", BE_CH_IFG_LIST },
167 { "check", BE_CH_IFG_CHECK },
171 static const lc_opt_enum_int_items_t lower_perm_items[] = {
172 { "copy", BE_CH_LOWER_PERM_COPY },
173 { "swap", BE_CH_LOWER_PERM_SWAP },
177 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
181 static const lc_opt_enum_int_items_t dump_items[] = {
182 { "spill", BE_CH_DUMP_SPILL },
183 { "live", BE_CH_DUMP_LIVE },
184 { "color", BE_CH_DUMP_COLOR },
185 { "copymin", BE_CH_DUMP_COPYMIN },
186 { "ssadestr", BE_CH_DUMP_SSADESTR },
187 { "tree", BE_CH_DUMP_TREE_INTV },
188 { "constr", BE_CH_DUMP_CONSTR },
189 { "lower", BE_CH_DUMP_LOWER },
190 { "appel", BE_CH_DUMP_APPEL },
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 /** Dump copy minimization statistics. */
227 static int be_copymin_stats = 0;
229 /** Enable extreme live range splitting. */
230 static int be_elr_split = 0;
232 /** Assumed loop iteration count for execution frequency estimation. */
233 static int be_loop_weight = 9;
235 static const lc_opt_table_entry_t be_chordal_options[] = {
236 LC_OPT_ENT_ENUM_INT ("spill", "spill method (belady, morgan or remat)", &spill_var),
237 LC_OPT_ENT_ENUM_PTR ("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", ©min_var),
238 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
239 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options (copy or swap)", &lower_perm_var),
240 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
241 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options (off, warn, assert)", &be_ch_vrfy_var),
242 LC_OPT_ENT_BOOL ("copymin_stats", "dump statistics of copy minimization", &be_copymin_stats),
243 LC_OPT_ENT_BOOL ("elrsplit", "enable extreme live range splitting", &be_elr_split),
244 LC_OPT_ENT_INT ("loop_weight", "assumed amount of loop iterations for guessing the execution frequency", &be_loop_weight),
248 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
250 static int run_once = 0;
251 lc_opt_entry_t *chordal_grp;
255 chordal_grp = lc_opt_get_grp(grp, "chordal");
257 lc_opt_add_table(chordal_grp, be_chordal_options);
260 co_register_options(chordal_grp);
261 java_coal_register_options(chordal_grp);
263 #endif /* WITH_LIBCORE */
265 static void dump(unsigned mask, ir_graph *irg,
266 const arch_register_class_t *cls,
268 void (*dump_func)(ir_graph *, const char *))
270 if((options.dump_flags & mask) == mask) {
273 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
274 be_dump(irg, buf, dump_func);
277 be_dump(irg, suffix, dump_func);
281 static void put_ignore_colors(be_chordal_env_t *chordal_env)
283 int n_colors = chordal_env->cls->n_regs;
286 bitset_clear_all(chordal_env->ignore_colors);
287 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
288 for(i = 0; i < n_colors; ++i)
289 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
290 bitset_set(chordal_env->ignore_colors, i);
293 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
297 ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
298 return fopen(buf, "wt");
301 void check_ifg_implementations(be_chordal_env_t *chordal_env)
305 f = be_chordal_open(chordal_env, "std", "log");
306 chordal_env->ifg = be_ifg_std_new(chordal_env);
307 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
310 f = be_chordal_open(chordal_env, "list", "log");
311 be_ifg_free(chordal_env->ifg);
312 chordal_env->ifg = be_ifg_list_new(chordal_env);
313 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
316 f = be_chordal_open(chordal_env, "clique", "log");
317 be_ifg_free(chordal_env->ifg);
318 chordal_env->ifg = be_ifg_clique_new(chordal_env);
319 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
322 f = be_chordal_open(chordal_env, "pointer", "log");
323 be_ifg_free(chordal_env->ifg);
324 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
325 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
328 chordal_env->ifg = NULL;
332 * Checks for every reload if it's user can perform the load on itself.
334 static void memory_operand_walker(ir_node *irn, void *env) {
335 be_chordal_env_t *cenv = env;
336 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
337 const ir_edge_t *edge, *ne;
340 if (! be_is_Reload(irn))
343 block = get_nodes_block(irn);
345 foreach_out_edge_safe(irn, edge, ne) {
346 ir_node *src = get_edge_src_irn(edge);
347 int pos = get_edge_src_pos(edge);
352 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
353 DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
354 arch_perform_memory_operand(aenv, src, irn, pos);
358 /* kill the Reload */
359 if (get_irn_n_edges(irn) == 0) {
361 set_irn_n(irn, 0, new_Bad());
362 set_irn_n(irn, 1, new_Bad());
367 * Starts a walk for memory operands if supported by the backend.
369 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
370 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
374 * Performs chordal register allocation for each register class on given irg.
376 * @param bi Backend irg object
377 * @return Structure containing timer for the single phases or NULL if no timing requested.
379 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
381 const be_main_env_t *main_env = bi->main_env;
382 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
383 ir_graph *irg = bi->irg;
384 be_options_t *main_opts = main_env->options;
389 be_chordal_env_t chordal_env;
391 if (main_opts->timing == BE_TIME_ON) {
392 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
393 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
394 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
395 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
396 ra_timer.t_color = lc_timer_register("ra_color", "graph coloring");
397 ra_timer.t_ifg = lc_timer_register("ra_ifg", "interference graph");
398 ra_timer.t_copymin = lc_timer_register("ra_copymin", "copy minimization");
399 ra_timer.t_ssa = lc_timer_register("ra_ssadestr", "ssa destruction");
400 ra_timer.t_verify = lc_timer_register("ra_verify", "graph verification");
401 ra_timer.t_other = lc_timer_register("ra_other", "other time");
403 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
404 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
405 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
406 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
407 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
408 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
409 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
410 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
411 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
412 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
415 #define BE_TIMER_PUSH(timer) \
416 if (main_opts->timing == BE_TIME_ON) { \
417 int res = lc_timer_push(timer); \
418 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
419 assert(res && "Timer already on stack, cannot be pushed twice."); \
420 else if (options.vrfy_option == BE_CH_VRFY_WARN && ! res) \
421 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
422 lc_timer_get_name(timer)); \
424 #define BE_TIMER_POP(timer) \
425 if (main_opts->timing == BE_TIME_ON) { \
426 lc_timer_t *tmp = lc_timer_pop(); \
427 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
428 assert(tmp == timer && "Attempt to pop wrong timer."); \
429 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
430 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
431 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
435 BE_TIMER_PUSH(ra_timer.t_other);
436 BE_TIMER_PUSH(ra_timer.t_prolog);
440 chordal_env.opts = &options;
441 chordal_env.irg = irg;
442 chordal_env.birg = bi;
443 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
444 chordal_env.exec_freq = compute_execfreq(irg, be_loop_weight);
445 chordal_env.lv = be_liveness(irg);
446 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
448 obstack_init(&chordal_env.obst);
450 BE_TIMER_POP(ra_timer.t_prolog);
452 /* Perform the following for each register class. */
453 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
455 copy_opt_t *co = NULL;
457 chordal_env.cls = arch_isa_get_reg_class(isa, j);
458 chordal_env.border_heads = pmap_create();
459 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
461 /* put all ignore registers into the ignore register set. */
462 put_ignore_colors(&chordal_env);
464 BE_TIMER_PUSH(ra_timer.t_live);
465 be_liveness_recompute(chordal_env.lv);
466 BE_TIMER_POP(ra_timer.t_live);
468 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
470 BE_TIMER_PUSH(ra_timer.t_spill);
473 switch(options.spill_method) {
474 case BE_CH_SPILL_MORGAN:
475 be_spill_morgan(&chordal_env);
477 case BE_CH_SPILL_BELADY:
478 be_spill_belady(&chordal_env);
481 case BE_CH_SPILL_REMAT:
482 be_spill_remat(&chordal_env);
484 #endif /* WITH_ILP */
486 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
487 be_spill_belady(&chordal_env);
490 BE_TIMER_POP(ra_timer.t_spill);
492 DBG((chordal_env.dbg, LEVEL_1, "spill costs for %+F in regclass %s: %g\n",
494 chordal_env.cls->name,
495 get_irg_spill_cost(&chordal_env))
498 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
499 be_abi_fix_stack_nodes(bi->abi, chordal_env.lv);
500 be_compute_spill_offsets(&chordal_env);
501 check_for_memory_operands(&chordal_env);
503 BE_TIMER_PUSH(ra_timer.t_verify);
505 /* verify schedule and register pressure */
506 if (options.vrfy_option == BE_CH_VRFY_WARN) {
507 be_verify_schedule(irg);
508 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
510 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
511 assert(be_verify_schedule(irg) && "Schedule verification failed");
512 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
513 && "Register pressure verification failed");
515 BE_TIMER_POP(ra_timer.t_verify);
517 if(be_elr_split && !splitted) {
518 extreme_liverange_splitting(&chordal_env);
523 /* Color the graph. */
524 BE_TIMER_PUSH(ra_timer.t_color);
525 be_ra_chordal_color(&chordal_env);
526 BE_TIMER_POP(ra_timer.t_color);
528 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
530 /* Create the ifg with the selected flavor */
531 BE_TIMER_PUSH(ra_timer.t_ifg);
532 switch (options.ifg_flavor) {
534 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
537 chordal_env.ifg = be_ifg_std_new(&chordal_env);
539 case BE_CH_IFG_CLIQUE:
540 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
542 case BE_CH_IFG_POINTER:
543 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
546 chordal_env.ifg = be_ifg_list_new(&chordal_env);
548 case BE_CH_IFG_CHECK:
549 check_ifg_implementations(&chordal_env);
550 /* Build the interference graph. */
551 chordal_env.ifg = be_ifg_std_new(&chordal_env);
554 BE_TIMER_POP(ra_timer.t_ifg);
556 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_TIMER_POP(ra_timer.t_verify);
562 /* copy minimization */
563 BE_TIMER_PUSH(ra_timer.t_copymin);
565 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
566 co = new_copy_opt(&chordal_env, co_get_costs_exec_freq);
567 co_build_ou_structure(co);
568 co_build_graph_structure(co);
569 if(be_copymin_stats) {
570 ir_printf("%40F %20s\n", current_ir_graph, chordal_env.cls->name);
571 printf("max copy costs: %d\n", co_get_max_copy_costs(co));
572 printf("init copy costs: %d\n", co_get_copy_costs(co));
573 printf("inevit copy costs: %d\n", co_get_inevit_copy_costs(co));
574 printf("copy costs lower bound: %d\n", co_get_lower_bound(co));
577 /* Dump the interference graph in Appel's format. */
578 if(options.dump_flags & BE_CH_DUMP_APPEL) {
579 FILE *f = be_chordal_open(&chordal_env, "appel-", "apl");
580 co_dump_appel_graph(co, f);
585 switch(options.copymin_method) {
586 case BE_CH_COPYMIN_HEUR1:
587 co_solve_heuristic(co);
589 case BE_CH_COPYMIN_HEUR2:
590 co_solve_heuristic_new(co);
592 case BE_CH_COPYMIN_HEUR3:
593 co_solve_heuristic_java(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_ILP:
603 co_solve_ilp2(co, 60.0);
605 #endif /* WITH_ILP */
606 case BE_CH_COPYMIN_NONE:
612 if(be_copymin_stats) {
613 printf("final copy costs : %d\n", co_get_copy_costs(co));
615 co_free_graph_structure(co);
616 co_free_ou_structure(co);
619 BE_TIMER_POP(ra_timer.t_copymin);
621 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
623 BE_TIMER_PUSH(ra_timer.t_verify);
625 if (options.vrfy_option != BE_CH_VRFY_OFF)
626 be_ra_chordal_check(&chordal_env);
628 BE_TIMER_POP(ra_timer.t_verify);
629 BE_TIMER_PUSH(ra_timer.t_ssa);
631 /* ssa destruction */
632 be_ssa_destruction(&chordal_env);
634 BE_TIMER_POP(ra_timer.t_ssa);
636 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
638 BE_TIMER_PUSH(ra_timer.t_verify);
639 if (options.vrfy_option != BE_CH_VRFY_OFF) {
640 be_ssa_destruction_check(&chordal_env);
641 be_ra_chordal_check(&chordal_env);
643 BE_TIMER_POP(ra_timer.t_verify);
645 if (options.copymin_method == BE_CH_COPYMIN_STAT)
648 be_ifg_free(chordal_env.ifg);
649 pmap_destroy(chordal_env.border_heads);
650 bitset_free(chordal_env.ignore_colors);
653 BE_TIMER_PUSH(ra_timer.t_epilog);
655 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
657 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
658 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
660 obstack_free(&chordal_env.obst, NULL);
661 be_free_dominance_frontiers(chordal_env.dom_front);
662 be_liveness_free(chordal_env.lv);
663 free_execfreq(chordal_env.exec_freq);
665 BE_TIMER_POP(ra_timer.t_epilog);
666 BE_TIMER_POP(ra_timer.t_other);
671 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
674 const be_ra_t be_ra_chordal_allocator = {
676 be_ra_chordal_register_options,