2 * @file bechordal_main.c
4 * @author Sebastian Hack
7 * Copyright (C) 2005-2006 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 */
31 #include "irgraph_t.h"
32 #include "irprintf_t.h"
45 #include "bechordal_t.h"
47 #include "bejavacoal.h"
51 #include "besched_t.h"
55 #include "beifg_impl.h"
58 #include "bespillbelady.h"
59 #include "bespillmorgan.h"
60 #include "bespillslots.h"
64 #include "bespillremat.h"
67 #include "bejavacoal.h"
68 #include "becopystat.h"
69 #include "becopyopt.h"
70 #include "bessadestr.h"
72 #include "bespillcost.h"
75 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
76 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
79 ir_node **nodes, *n1, *n2;
81 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
83 /* Collect all irns */
85 pmap_foreach(chordal_env->border_heads, pme) {
87 struct list_head *head = pme->value;
88 list_for_each_entry(border_t, curr, head, list)
89 if (curr->is_def && curr->is_real)
90 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
91 obstack_ptr_grow(&ob, curr->irn);
93 obstack_ptr_grow(&ob, NULL);
94 nodes = (ir_node **) obstack_finish(&ob);
97 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
98 const arch_register_t *n1_reg, *n2_reg;
100 n1_reg = arch_get_irn_register(arch_env, n1);
101 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
102 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
103 assert(0 && "Register constraint does not hold");
105 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
106 n2_reg = arch_get_irn_register(arch_env, n2);
107 if (values_interfere(chordal_env->lv, n1, n2) && n1_reg == n2_reg) {
108 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
109 assert(0 && "Interfering values have the same color!");
113 obstack_free(&ob, NULL);
116 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
119 return be_ifg_connected(env->ifg, a, b);
121 return values_interfere(env->lv, a, b);
125 static be_ra_chordal_opts_t options = {
129 BE_CH_LOWER_PERM_SWAP,
133 /** Enable extreme live range splitting. */
134 static int be_elr_split = 0;
136 /** Assumed loop iteration count for execution frequency estimation. */
137 static int be_loop_weight = 9;
140 static be_ra_timer_t ra_timer = {
154 static const lc_opt_enum_int_items_t spill_items[] = {
155 { "morgan", BE_CH_SPILL_MORGAN },
156 { "belady", BE_CH_SPILL_BELADY },
158 { "remat", BE_CH_SPILL_REMAT },
159 #endif /* WITH_ILP */
163 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
164 { "std", BE_CH_IFG_STD },
165 { "fast", BE_CH_IFG_FAST },
166 { "clique", BE_CH_IFG_CLIQUE },
167 { "pointer", BE_CH_IFG_POINTER },
168 { "list", BE_CH_IFG_LIST },
169 { "check", BE_CH_IFG_CHECK },
173 static const lc_opt_enum_int_items_t lower_perm_items[] = {
174 { "copy", BE_CH_LOWER_PERM_COPY },
175 { "swap", BE_CH_LOWER_PERM_SWAP },
179 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
183 static const lc_opt_enum_int_items_t dump_items[] = {
184 { "spill", BE_CH_DUMP_SPILL },
185 { "live", BE_CH_DUMP_LIVE },
186 { "color", BE_CH_DUMP_COLOR },
187 { "copymin", BE_CH_DUMP_COPYMIN },
188 { "ssadestr", BE_CH_DUMP_SSADESTR },
189 { "tree", BE_CH_DUMP_TREE_INTV },
190 { "constr", BE_CH_DUMP_CONSTR },
191 { "lower", BE_CH_DUMP_LOWER },
192 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
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 ifg_flavor_var = {
210 &options.ifg_flavor, ifg_flavor_items
213 static lc_opt_enum_int_var_t lower_perm_var = {
214 &options.lower_perm_opt, lower_perm_items
217 static lc_opt_enum_int_var_t dump_var = {
218 &options.dump_flags, dump_items
221 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
222 &options.vrfy_option, be_ch_vrfy_items
225 static const lc_opt_table_entry_t be_chordal_options[] = {
226 LC_OPT_ENT_ENUM_INT ("spill", "spill method (belady, morgan or remat)", &spill_var),
227 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
228 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options (copy or swap)", &lower_perm_var),
229 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
230 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options (off, warn, assert)", &be_ch_vrfy_var),
231 LC_OPT_ENT_BOOL ("elrsplit", "enable extreme live range splitting", &be_elr_split),
232 LC_OPT_ENT_INT ("loop_weight", "assumed amount of loop iterations for guessing the execution frequency", &be_loop_weight),
236 extern void be_spill_remat_register_options(lc_opt_entry_t *ent);
239 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
241 static int run_once = 0;
242 lc_opt_entry_t *chordal_grp;
246 chordal_grp = lc_opt_get_grp(grp, "chordal");
248 lc_opt_add_table(chordal_grp, be_chordal_options);
250 co_register_options(chordal_grp);
251 be_java_coal_register_options(chordal_grp);
253 be_spill_remat_register_options(chordal_grp);
257 #endif /* WITH_LIBCORE */
259 static void dump(unsigned mask, ir_graph *irg,
260 const arch_register_class_t *cls,
262 void (*dump_func)(ir_graph *, const char *))
264 if((options.dump_flags & mask) == mask) {
267 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
268 be_dump(irg, buf, dump_func);
271 be_dump(irg, suffix, dump_func);
275 static void put_ignore_colors(be_chordal_env_t *chordal_env)
277 int n_colors = chordal_env->cls->n_regs;
280 bitset_clear_all(chordal_env->ignore_colors);
281 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
282 for(i = 0; i < n_colors; ++i)
283 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
284 bitset_set(chordal_env->ignore_colors, i);
287 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
291 ir_snprintf(buf, sizeof(buf), "%s%F_%s%s", prefix, env->irg, env->cls->name, suffix);
292 return fopen(buf, "wt");
295 void check_ifg_implementations(be_chordal_env_t *chordal_env)
299 f = be_chordal_open(chordal_env, "std", ".log");
300 chordal_env->ifg = be_ifg_std_new(chordal_env);
301 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
304 f = be_chordal_open(chordal_env, "list", ".log");
305 be_ifg_free(chordal_env->ifg);
306 chordal_env->ifg = be_ifg_list_new(chordal_env);
307 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
310 f = be_chordal_open(chordal_env, "clique", ".log");
311 be_ifg_free(chordal_env->ifg);
312 chordal_env->ifg = be_ifg_clique_new(chordal_env);
313 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
316 f = be_chordal_open(chordal_env, "pointer", ".log");
317 be_ifg_free(chordal_env->ifg);
318 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
319 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
322 chordal_env->ifg = NULL;
326 * Checks for every reload if it's user can perform the load on itself.
328 static void memory_operand_walker(ir_node *irn, void *env) {
329 be_chordal_env_t *cenv = env;
330 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
331 const ir_edge_t *edge, *ne;
335 if (! be_is_Reload(irn))
338 // only use memory operands, if the reload is only used by 1 node
339 if(get_irn_n_edges(irn) > 1)
342 spill = be_get_Reload_mem(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, spill, 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);
375 * Initialize all timers.
377 static void be_init_timer(be_options_t *main_opts)
379 if (main_opts->timing == BE_TIME_ON) {
380 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
381 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
382 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
383 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
384 ra_timer.t_spillslots = lc_timer_register("ra_spillslots", "spillslots");
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_spillslots);
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);
406 #define BE_TIMER_INIT(main_opts) be_init_timer(main_opts)
408 #define BE_TIMER_PUSH(timer) \
409 if (main_opts->timing == BE_TIME_ON) { \
410 if (! lc_timer_push(timer)) { \
411 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
412 assert(!"Timer already on stack, cannot be pushed twice."); \
413 else if (options.vrfy_option == BE_CH_VRFY_WARN) \
414 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
415 lc_timer_get_name(timer)); \
418 #define BE_TIMER_POP(timer) \
419 if (main_opts->timing == BE_TIME_ON) { \
420 lc_timer_t *tmp = lc_timer_pop(); \
421 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
422 assert(tmp == timer && "Attempt to pop wrong timer."); \
423 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
424 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
425 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
430 #define BE_TIMER_INIT(main_opts)
431 #define BE_TIMER_PUSH(timer)
432 #define BE_TIMER_POP(timer)
434 #endif /* WITH_LIBCORE */
437 * Performs chordal register allocation for each register class on given irg.
439 * @param bi Backend irg object
440 * @return Structure containing timer for the single phases or NULL if no timing requested.
442 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
444 const be_main_env_t *main_env = bi->main_env;
445 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
446 ir_graph *irg = bi->irg;
447 be_options_t *main_opts = main_env->options;
451 be_chordal_env_t chordal_env;
453 BE_TIMER_INIT(main_opts);
454 BE_TIMER_PUSH(ra_timer.t_other);
455 BE_TIMER_PUSH(ra_timer.t_prolog);
459 chordal_env.opts = &options;
460 chordal_env.irg = irg;
461 chordal_env.birg = bi;
462 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
463 chordal_env.exec_freq = compute_execfreq(irg, be_loop_weight);
464 chordal_env.lv = be_liveness(irg);
465 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
467 obstack_init(&chordal_env.obst);
469 BE_TIMER_POP(ra_timer.t_prolog);
471 /* Perform the following for each register class. */
472 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
473 chordal_env.cls = arch_isa_get_reg_class(isa, j);
474 chordal_env.border_heads = pmap_create();
475 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
477 /* put all ignore registers into the ignore register set. */
478 put_ignore_colors(&chordal_env);
480 BE_TIMER_PUSH(ra_timer.t_live);
481 be_liveness_recompute(chordal_env.lv);
482 BE_TIMER_POP(ra_timer.t_live);
483 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
485 be_pre_spill_prepare_constr(&chordal_env);
486 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-constr-pre", dump_ir_block_graph_sched);
488 BE_TIMER_PUSH(ra_timer.t_spill);
491 switch(options.spill_method) {
492 case BE_CH_SPILL_MORGAN:
493 be_spill_morgan(&chordal_env);
495 case BE_CH_SPILL_BELADY:
496 be_spill_belady(&chordal_env);
499 case BE_CH_SPILL_REMAT:
500 be_spill_remat(&chordal_env);
502 #endif /* WITH_ILP */
504 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
505 be_spill_belady(&chordal_env);
508 BE_TIMER_POP(ra_timer.t_spill);
510 DBG((chordal_env.dbg, LEVEL_1, "spill costs for %+F in regclass %s: %g\n",
512 chordal_env.cls->name,
513 get_irg_spill_cost(&chordal_env))
516 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
518 check_for_memory_operands(&chordal_env);
520 be_abi_fix_stack_nodes(bi->abi, chordal_env.lv);
522 BE_TIMER_PUSH(ra_timer.t_verify);
524 /* verify schedule and register pressure */
525 if (options.vrfy_option == BE_CH_VRFY_WARN) {
526 be_verify_schedule(irg);
527 be_verify_register_pressure(chordal_env.birg, chordal_env.cls, irg);
529 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
530 assert(be_verify_schedule(irg) && "Schedule verification failed");
531 assert(be_verify_register_pressure(chordal_env.birg, chordal_env.cls, irg)
532 && "Register pressure verification failed");
534 BE_TIMER_POP(ra_timer.t_verify);
536 if (be_elr_split && ! splitted) {
537 extreme_liverange_splitting(&chordal_env);
542 /* Color the graph. */
543 BE_TIMER_PUSH(ra_timer.t_color);
544 be_ra_chordal_color(&chordal_env);
545 BE_TIMER_POP(ra_timer.t_color);
547 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
549 /* Create the ifg with the selected flavor */
550 BE_TIMER_PUSH(ra_timer.t_ifg);
551 switch (options.ifg_flavor) {
553 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
556 chordal_env.ifg = be_ifg_std_new(&chordal_env);
558 case BE_CH_IFG_CLIQUE:
559 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
561 case BE_CH_IFG_POINTER:
562 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
565 chordal_env.ifg = be_ifg_list_new(&chordal_env);
567 case BE_CH_IFG_CHECK:
568 check_ifg_implementations(&chordal_env);
569 /* Build the interference graph. */
570 chordal_env.ifg = be_ifg_std_new(&chordal_env);
573 BE_TIMER_POP(ra_timer.t_ifg);
575 BE_TIMER_PUSH(ra_timer.t_verify);
576 if (options.vrfy_option != BE_CH_VRFY_OFF)
577 be_ra_chordal_check(&chordal_env);
579 BE_TIMER_POP(ra_timer.t_verify);
581 /* copy minimization */
582 BE_TIMER_PUSH(ra_timer.t_copymin);
583 co_driver(&chordal_env);
584 BE_TIMER_POP(ra_timer.t_copymin);
585 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
587 BE_TIMER_PUSH(ra_timer.t_verify);
589 if (options.vrfy_option != BE_CH_VRFY_OFF)
590 be_ra_chordal_check(&chordal_env);
592 BE_TIMER_POP(ra_timer.t_verify);
593 BE_TIMER_PUSH(ra_timer.t_ssa);
595 /* ssa destruction */
596 be_ssa_destruction(&chordal_env);
598 BE_TIMER_POP(ra_timer.t_ssa);
600 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
602 BE_TIMER_PUSH(ra_timer.t_verify);
603 if (options.vrfy_option != BE_CH_VRFY_OFF) {
604 be_ssa_destruction_check(&chordal_env);
605 be_ra_chordal_check(&chordal_env);
607 BE_TIMER_POP(ra_timer.t_verify);
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_spillslots);
616 be_coalesce_spillslots(&chordal_env);
617 dump(BE_CH_DUMP_SPILLSLOTS, irg, NULL, "-spillslots", dump_ir_block_graph_sched);
619 BE_TIMER_POP(ra_timer.t_spillslots);
621 BE_TIMER_PUSH(ra_timer.t_verify);
623 /* verify spillslots */
624 if (options.vrfy_option == BE_CH_VRFY_WARN) {
625 be_verify_spillslots(main_env->arch_env, irg);
627 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
628 assert(be_verify_spillslots(main_env->arch_env, irg) && "Spillslot verification failed");
630 BE_TIMER_POP(ra_timer.t_verify);
632 BE_TIMER_PUSH(ra_timer.t_epilog);
634 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
636 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
637 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
639 obstack_free(&chordal_env.obst, NULL);
640 be_free_dominance_frontiers(chordal_env.dom_front);
641 be_liveness_free(chordal_env.lv);
642 free_execfreq(chordal_env.exec_freq);
644 BE_TIMER_POP(ra_timer.t_epilog);
645 BE_TIMER_POP(ra_timer.t_other);
648 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
649 #endif /* WITH_LIBCORE */
653 const be_ra_t be_ra_chordal_allocator = {
655 be_ra_chordal_register_options,