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"
63 #include "bespillcost.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 { "remat", BE_CH_SPILL_REMAT },
143 #endif /* WITH_ILP */
147 static const lc_opt_enum_int_items_t copymin_items[] = {
148 { "none", BE_CH_COPYMIN_NONE },
149 { "heur1", BE_CH_COPYMIN_HEUR1 },
150 { "heur2", BE_CH_COPYMIN_HEUR2 },
151 { "stat", BE_CH_COPYMIN_STAT },
152 { "park", BE_CH_COPYMIN_PARK_MOON },
154 { "ilp1", BE_CH_COPYMIN_ILP1 },
155 { "ilp2", BE_CH_COPYMIN_ILP2 },
156 #endif /* WITH_ILP */
160 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
161 { "std", BE_CH_IFG_STD },
162 { "fast", BE_CH_IFG_FAST },
163 { "clique", BE_CH_IFG_CLIQUE },
164 { "pointer", BE_CH_IFG_POINTER },
165 { "list", BE_CH_IFG_LIST },
166 { "check", BE_CH_IFG_CHECK },
170 static const lc_opt_enum_int_items_t lower_perm_items[] = {
171 { "copy", BE_CH_LOWER_PERM_COPY },
172 { "swap", BE_CH_LOWER_PERM_SWAP },
176 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
180 static const lc_opt_enum_int_items_t dump_items[] = {
181 { "spill", BE_CH_DUMP_SPILL },
182 { "live", BE_CH_DUMP_LIVE },
183 { "color", BE_CH_DUMP_COLOR },
184 { "copymin", BE_CH_DUMP_COPYMIN },
185 { "ssadestr", BE_CH_DUMP_SSADESTR },
186 { "tree", BE_CH_DUMP_TREE_INTV },
187 { "constr", BE_CH_DUMP_CONSTR },
188 { "lower", BE_CH_DUMP_LOWER },
189 { "all", BE_CH_DUMP_ALL },
193 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
194 { "off", BE_CH_VRFY_OFF },
195 { "warn", BE_CH_VRFY_WARN },
196 { "assert", BE_CH_VRFY_ASSERT },
200 static lc_opt_enum_int_var_t spill_var = {
201 &options.spill_method, spill_items
204 static lc_opt_enum_int_var_t copymin_var = {
205 &options.copymin_method, copymin_items
208 static lc_opt_enum_int_var_t ifg_flavor_var = {
209 &options.ifg_flavor, ifg_flavor_items
212 static lc_opt_enum_int_var_t lower_perm_var = {
213 &options.lower_perm_opt, lower_perm_items
216 static lc_opt_enum_int_var_t dump_var = {
217 &options.dump_flags, dump_items
220 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
221 &options.vrfy_option, be_ch_vrfy_items
224 static int be_copymin_stats = 0;
226 /** Assumed loop iteration count for execution frequency estimation. */
227 static int be_loop_weight = 9;
229 static const lc_opt_table_entry_t be_chordal_options[] = {
230 LC_OPT_ENT_ENUM_INT ("spill", "spill method (belady, morgan or remat)", &spill_var),
231 LC_OPT_ENT_ENUM_PTR ("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", ©min_var),
232 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
233 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options (copy or swap)", &lower_perm_var),
234 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
235 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options (off, warn, assert)", &be_ch_vrfy_var),
236 LC_OPT_ENT_BOOL ("copymin_stats", "dump statistics of copy minimization", &be_copymin_stats),
237 LC_OPT_ENT_INT ("loop_weight", "assumed amount of loop iterations for guessing the execution frequency", &be_loop_weight),
241 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
243 static int run_once = 0;
244 lc_opt_entry_t *chordal_grp;
248 chordal_grp = lc_opt_get_grp(grp, "chordal");
250 lc_opt_add_table(chordal_grp, be_chordal_options);
253 co_register_options(chordal_grp);
255 #endif /* WITH_LIBCORE */
257 static void dump(unsigned mask, ir_graph *irg,
258 const arch_register_class_t *cls,
260 void (*dump_func)(ir_graph *, const char *))
262 if((options.dump_flags & mask) == mask) {
265 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
266 be_dump(irg, buf, dump_func);
269 be_dump(irg, suffix, dump_func);
273 static void put_ignore_colors(be_chordal_env_t *chordal_env)
275 int n_colors = chordal_env->cls->n_regs;
278 bitset_clear_all(chordal_env->ignore_colors);
279 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
280 for(i = 0; i < n_colors; ++i)
281 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
282 bitset_set(chordal_env->ignore_colors, i);
285 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
289 ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
290 return fopen(buf, "wt");
293 void check_ifg_implementations(be_chordal_env_t *chordal_env)
297 f = be_chordal_open(chordal_env, "std", "log");
298 chordal_env->ifg = be_ifg_std_new(chordal_env);
299 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
302 f = be_chordal_open(chordal_env, "list", "log");
303 be_ifg_free(chordal_env->ifg);
304 chordal_env->ifg = be_ifg_list_new(chordal_env);
305 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
308 f = be_chordal_open(chordal_env, "clique", "log");
309 be_ifg_free(chordal_env->ifg);
310 chordal_env->ifg = be_ifg_clique_new(chordal_env);
311 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
314 f = be_chordal_open(chordal_env, "pointer", "log");
315 be_ifg_free(chordal_env->ifg);
316 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
317 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
320 chordal_env->ifg = NULL;
324 * Checks for every reload if it's user can perform the load on itself.
326 static void memory_operand_walker(ir_node *irn, void *env) {
327 be_chordal_env_t *cenv = env;
328 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
329 const ir_edge_t *edge, *ne;
332 if (! be_is_Reload(irn))
335 block = get_nodes_block(irn);
337 foreach_out_edge_safe(irn, edge, ne) {
338 ir_node *src = get_edge_src_irn(edge);
339 int pos = get_edge_src_pos(edge);
344 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
345 DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
346 arch_perform_memory_operand(aenv, src, irn, pos);
350 /* kill the Reload */
351 if (get_irn_n_edges(irn) == 0) {
353 set_irn_n(irn, 0, new_Bad());
354 set_irn_n(irn, 1, new_Bad());
359 * Starts a walk for memory operands if supported by the backend.
361 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
362 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
366 * Performs chordal register allocation for each register class on given irg.
368 * @param bi Backend irg object
369 * @return Structure containing timer for the single phases or NULL if no timing requested.
371 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
373 const be_main_env_t *main_env = bi->main_env;
374 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
375 ir_graph *irg = bi->irg;
376 be_options_t *main_opts = main_env->options;
379 be_chordal_env_t chordal_env;
381 if (main_opts->timing == BE_TIME_ON) {
382 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
383 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
384 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
385 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
386 ra_timer.t_color = lc_timer_register("ra_color", "graph coloring");
387 ra_timer.t_ifg = lc_timer_register("ra_ifg", "interference graph");
388 ra_timer.t_copymin = lc_timer_register("ra_copymin", "copy minimization");
389 ra_timer.t_ssa = lc_timer_register("ra_ssadestr", "ssa destruction");
390 ra_timer.t_verify = lc_timer_register("ra_verify", "graph verification");
391 ra_timer.t_other = lc_timer_register("ra_other", "other time");
393 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
394 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
395 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
396 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
397 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
398 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
399 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
400 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
401 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
402 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
405 #define BE_TIMER_PUSH(timer) \
406 if (main_opts->timing == BE_TIME_ON) { \
407 int res = lc_timer_push(timer); \
408 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
409 assert(res && "Timer already on stack, cannot be pushed twice."); \
410 else if (options.vrfy_option == BE_CH_VRFY_WARN && ! res) \
411 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
412 lc_timer_get_name(timer)); \
414 #define BE_TIMER_POP(timer) \
415 if (main_opts->timing == BE_TIME_ON) { \
416 lc_timer_t *tmp = lc_timer_pop(); \
417 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
418 assert(tmp == timer && "Attempt to pop wrong timer."); \
419 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
420 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
421 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
425 BE_TIMER_PUSH(ra_timer.t_other);
426 BE_TIMER_PUSH(ra_timer.t_prolog);
430 chordal_env.opts = &options;
431 chordal_env.irg = irg;
432 chordal_env.birg = bi;
433 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
434 chordal_env.exec_freq = compute_execfreq(irg, be_loop_weight);
435 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
437 obstack_init(&chordal_env.obst);
439 BE_TIMER_POP(ra_timer.t_prolog);
441 /* Perform the following for each register class. */
442 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
444 copy_opt_t *co = NULL;
446 chordal_env.cls = arch_isa_get_reg_class(isa, j);
447 chordal_env.border_heads = pmap_create();
448 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
450 BE_TIMER_PUSH(ra_timer.t_live);
452 /* put all ignore registers into the ignore register set. */
453 put_ignore_colors(&chordal_env);
457 BE_TIMER_POP(ra_timer.t_live);
459 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
461 BE_TIMER_PUSH(ra_timer.t_spill);
464 switch(options.spill_method) {
465 case BE_CH_SPILL_MORGAN:
466 be_spill_morgan(&chordal_env);
468 case BE_CH_SPILL_BELADY:
469 be_spill_belady(&chordal_env);
472 case BE_CH_SPILL_REMAT:
473 be_spill_remat(&chordal_env);
475 #endif /* WITH_ILP */
477 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
478 be_spill_belady(&chordal_env);
481 BE_TIMER_POP(ra_timer.t_spill);
483 DBG((chordal_env.dbg, LEVEL_1, "spill costs for %+F in regclass %s: %g\n",
485 chordal_env.cls->name,
486 get_irg_spill_cost(&chordal_env))
489 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
490 be_abi_fix_stack_nodes(bi->abi);
491 be_compute_spill_offsets(&chordal_env);
492 check_for_memory_operands(&chordal_env);
494 BE_TIMER_PUSH(ra_timer.t_verify);
496 /* verify schedule and register pressure */
497 if (options.vrfy_option == BE_CH_VRFY_WARN) {
498 be_verify_schedule(irg);
499 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
501 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
502 assert(be_verify_schedule(irg) && "Schedule verification failed");
503 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
504 && "Register pressure verification failed");
507 BE_TIMER_POP(ra_timer.t_verify);
508 BE_TIMER_PUSH(ra_timer.t_live);
510 BE_TIMER_POP(ra_timer.t_live);
511 BE_TIMER_PUSH(ra_timer.t_color);
513 /* Color the graph. */
514 be_ra_chordal_color(&chordal_env);
516 BE_TIMER_POP(ra_timer.t_color);
518 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
520 BE_TIMER_PUSH(ra_timer.t_ifg);
522 /* Create the ifg with the selected flavor */
523 switch (options.ifg_flavor) {
525 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
528 chordal_env.ifg = be_ifg_std_new(&chordal_env);
530 case BE_CH_IFG_CLIQUE:
531 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
533 case BE_CH_IFG_POINTER:
534 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
537 chordal_env.ifg = be_ifg_list_new(&chordal_env);
539 case BE_CH_IFG_CHECK:
540 check_ifg_implementations(&chordal_env);
541 /* Build the interference graph. */
542 chordal_env.ifg = be_ifg_std_new(&chordal_env);
549 be_ifg_t *std = be_ifg_std_new(&chordal_env);
550 f = be_chordal_open(&chordal_env, "std", "csv");
551 be_ifg_check_sorted_to_file(std, f);
556 f = be_chordal_open(&chordal_env, "clique", "csv");
557 be_ifg_check_sorted_to_file(chordal_env.ifg, f);
560 BE_TIMER_POP(ra_timer.t_ifg);
562 BE_TIMER_PUSH(ra_timer.t_verify);
564 if (options.vrfy_option != BE_CH_VRFY_OFF)
565 be_ra_chordal_check(&chordal_env);
567 // be_ifg_check_sorted(chordal_env.ifg);
568 BE_TIMER_POP(ra_timer.t_verify);
569 BE_TIMER_PUSH(ra_timer.t_copymin);
571 /* copy minimization */
572 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
574 co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
575 co_build_ou_structure(co);
576 co_build_graph_structure(co);
577 if(be_copymin_stats) {
578 ir_printf("%40F %20s\n", current_ir_graph, chordal_env.cls->name);
579 printf("max copy costs: %d\n", co_get_max_copy_costs(co));
580 printf("init copy costs: %d\n", co_get_copy_costs(co));
581 printf("inevit copy costs: %d\n", co_get_inevit_copy_costs(co));
582 printf("copy costs lower bound: %d\n", co_get_lower_bound(co));
586 f = be_chordal_open(&chordal_env, "appel-", "apl");
587 co_dump_appel_graph(co, f);
589 f = be_chordal_open(&chordal_env, "appel-clique-", "p");
590 co_dump_appel_graph_cliques(co, f);
595 switch(options.copymin_method) {
596 case BE_CH_COPYMIN_HEUR1:
597 co_solve_heuristic(co);
599 case BE_CH_COPYMIN_HEUR2:
600 co_solve_heuristic_new(co);
602 case BE_CH_COPYMIN_PARK_MOON:
603 co_solve_park_moon(co);
605 case BE_CH_COPYMIN_STAT:
606 co_compare_solvers(&chordal_env);
609 case BE_CH_COPYMIN_ILP1:
610 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
611 co_solve_ilp1(co, 60.0);
613 case BE_CH_COPYMIN_ILP2:
614 co_solve_ilp2(co, 60.0);
616 #endif /* WITH_ILP */
617 case BE_CH_COPYMIN_NONE:
623 if(be_copymin_stats) {
624 printf("final copy costs : %d\n", co_get_copy_costs(co));
626 co_free_graph_structure(co);
627 co_free_ou_structure(co);
631 BE_TIMER_POP(ra_timer.t_copymin);
633 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
635 BE_TIMER_PUSH(ra_timer.t_verify);
637 if (options.vrfy_option != BE_CH_VRFY_OFF)
638 be_ra_chordal_check(&chordal_env);
640 BE_TIMER_POP(ra_timer.t_verify);
641 BE_TIMER_PUSH(ra_timer.t_ssa);
643 /* ssa destruction */
644 be_ssa_destruction(&chordal_env);
646 BE_TIMER_POP(ra_timer.t_ssa);
648 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
650 BE_TIMER_PUSH(ra_timer.t_verify);
651 if (options.vrfy_option != BE_CH_VRFY_OFF) {
652 be_ssa_destruction_check(&chordal_env);
653 be_ra_chordal_check(&chordal_env);
655 BE_TIMER_POP(ra_timer.t_verify);
657 if (options.copymin_method == BE_CH_COPYMIN_STAT)
660 be_ifg_free(chordal_env.ifg);
661 pmap_destroy(chordal_env.border_heads);
662 bitset_free(chordal_env.ignore_colors);
665 BE_TIMER_PUSH(ra_timer.t_epilog);
667 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
669 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
670 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
672 obstack_free(&chordal_env.obst, NULL);
673 be_free_dominance_frontiers(chordal_env.dom_front);
674 free_execfreq(chordal_env.exec_freq);
676 BE_TIMER_POP(ra_timer.t_epilog);
677 BE_TIMER_POP(ra_timer.t_other);
682 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
685 const be_ra_t be_ra_chordal_allocator = {
687 be_ra_chordal_register_options,