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"
41 #include "bechordal_t.h"
43 #include "bejavacoal.h"
47 #include "besched_t.h"
51 #include "beifg_impl.h"
54 #include "bespillbelady.h"
55 #include "bespillmorgan.h"
59 #include "bespillremat.h"
62 #include "becopystat.h"
63 #include "becopyopt.h"
64 #include "bessadestr.h"
66 #include "bespillcost.h"
68 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
69 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
72 ir_node **nodes, *n1, *n2;
74 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
76 /* Collect all irns */
78 pmap_foreach(chordal_env->border_heads, pme) {
80 struct list_head *head = pme->value;
81 list_for_each_entry(border_t, curr, head, list)
82 if (curr->is_def && curr->is_real)
83 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
84 obstack_ptr_grow(&ob, curr->irn);
86 obstack_ptr_grow(&ob, NULL);
87 nodes = (ir_node **) obstack_finish(&ob);
90 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
91 const arch_register_t *n1_reg, *n2_reg;
93 n1_reg = arch_get_irn_register(arch_env, n1);
94 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
95 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
96 assert(0 && "Register constraint does not hold");
98 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
99 n2_reg = arch_get_irn_register(arch_env, n2);
100 if (values_interfere(chordal_env->lv, n1, n2) && n1_reg == n2_reg) {
101 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
102 assert(0 && "Interfering values have the same color!");
106 obstack_free(&ob, NULL);
109 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
112 return be_ifg_connected(env->ifg, a, b);
114 return values_interfere(env->lv, a, b);
118 static be_ra_chordal_opts_t options = {
123 BE_CH_LOWER_PERM_SWAP,
127 static be_ra_timer_t ra_timer = {
141 static const lc_opt_enum_int_items_t spill_items[] = {
142 { "morgan", BE_CH_SPILL_MORGAN },
143 { "belady", BE_CH_SPILL_BELADY },
145 { "remat", BE_CH_SPILL_REMAT },
146 #endif /* WITH_ILP */
150 static const lc_opt_enum_int_items_t copymin_items[] = {
151 { "none", BE_CH_COPYMIN_NONE },
152 { "heur1", BE_CH_COPYMIN_HEUR1 },
153 { "heur2", BE_CH_COPYMIN_HEUR2 },
154 { "heur3", BE_CH_COPYMIN_HEUR3 },
155 { "stat", BE_CH_COPYMIN_STAT },
156 { "park", BE_CH_COPYMIN_PARK_MOON },
158 { "ilp", BE_CH_COPYMIN_ILP },
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 { "appel", BE_CH_DUMP_APPEL },
193 { "all", BE_CH_DUMP_ALL },
197 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
198 { "off", BE_CH_VRFY_OFF },
199 { "warn", BE_CH_VRFY_WARN },
200 { "assert", BE_CH_VRFY_ASSERT },
204 static lc_opt_enum_int_var_t spill_var = {
205 &options.spill_method, spill_items
208 static lc_opt_enum_int_var_t copymin_var = {
209 &options.copymin_method, copymin_items
212 static lc_opt_enum_int_var_t ifg_flavor_var = {
213 &options.ifg_flavor, ifg_flavor_items
216 static lc_opt_enum_int_var_t lower_perm_var = {
217 &options.lower_perm_opt, lower_perm_items
220 static lc_opt_enum_int_var_t dump_var = {
221 &options.dump_flags, dump_items
224 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
225 &options.vrfy_option, be_ch_vrfy_items
228 /** Dump copy minimization statistics. */
229 static int be_copymin_stats = 0;
231 /** Enable extreme live range splitting. */
232 static int be_elr_split = 0;
234 /** Assumed loop iteration count for execution frequency estimation. */
235 static int be_loop_weight = 9;
237 static const lc_opt_table_entry_t be_chordal_options[] = {
238 LC_OPT_ENT_ENUM_INT ("spill", "spill method (belady, morgan or remat)", &spill_var),
239 LC_OPT_ENT_ENUM_PTR ("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", ©min_var),
240 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
241 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options (copy or swap)", &lower_perm_var),
242 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
243 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options (off, warn, assert)", &be_ch_vrfy_var),
244 LC_OPT_ENT_BOOL ("copymin_stats", "dump statistics of copy minimization", &be_copymin_stats),
245 LC_OPT_ENT_BOOL ("elrsplit", "enable extreme live range splitting", &be_elr_split),
246 LC_OPT_ENT_INT ("loop_weight", "assumed amount of loop iterations for guessing the execution frequency", &be_loop_weight),
250 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
252 static int run_once = 0;
253 lc_opt_entry_t *chordal_grp;
257 chordal_grp = lc_opt_get_grp(grp, "chordal");
259 lc_opt_add_table(chordal_grp, be_chordal_options);
262 co_register_options(chordal_grp);
263 java_coal_register_options(chordal_grp);
265 #endif /* WITH_LIBCORE */
267 static void dump(unsigned mask, ir_graph *irg,
268 const arch_register_class_t *cls,
270 void (*dump_func)(ir_graph *, const char *))
272 if((options.dump_flags & mask) == mask) {
275 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
276 be_dump(irg, buf, dump_func);
279 be_dump(irg, suffix, dump_func);
283 static void put_ignore_colors(be_chordal_env_t *chordal_env)
285 int n_colors = chordal_env->cls->n_regs;
288 bitset_clear_all(chordal_env->ignore_colors);
289 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
290 for(i = 0; i < n_colors; ++i)
291 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
292 bitset_set(chordal_env->ignore_colors, i);
295 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
299 ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
300 return fopen(buf, "wt");
303 void check_ifg_implementations(be_chordal_env_t *chordal_env)
307 f = be_chordal_open(chordal_env, "std", "log");
308 chordal_env->ifg = be_ifg_std_new(chordal_env);
309 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
312 f = be_chordal_open(chordal_env, "list", "log");
313 be_ifg_free(chordal_env->ifg);
314 chordal_env->ifg = be_ifg_list_new(chordal_env);
315 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
318 f = be_chordal_open(chordal_env, "clique", "log");
319 be_ifg_free(chordal_env->ifg);
320 chordal_env->ifg = be_ifg_clique_new(chordal_env);
321 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
324 f = be_chordal_open(chordal_env, "pointer", "log");
325 be_ifg_free(chordal_env->ifg);
326 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
327 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
330 chordal_env->ifg = NULL;
334 * Checks for every reload if it's user can perform the load on itself.
336 static void memory_operand_walker(ir_node *irn, void *env) {
337 be_chordal_env_t *cenv = env;
338 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
339 const ir_edge_t *edge, *ne;
342 if (! be_is_Reload(irn))
345 block = get_nodes_block(irn);
347 foreach_out_edge_safe(irn, edge, ne) {
348 ir_node *src = get_edge_src_irn(edge);
349 int pos = get_edge_src_pos(edge);
354 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
355 DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
356 arch_perform_memory_operand(aenv, src, irn, pos);
360 /* kill the Reload */
361 if (get_irn_n_edges(irn) == 0) {
363 set_irn_n(irn, 0, new_Bad());
364 set_irn_n(irn, 1, new_Bad());
369 * Starts a walk for memory operands if supported by the backend.
371 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
372 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
376 * Performs chordal register allocation for each register class on given irg.
378 * @param bi Backend irg object
379 * @return Structure containing timer for the single phases or NULL if no timing requested.
381 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
383 const be_main_env_t *main_env = bi->main_env;
384 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
385 ir_graph *irg = bi->irg;
386 be_options_t *main_opts = main_env->options;
390 be_chordal_env_t chordal_env;
392 if (main_opts->timing == BE_TIME_ON) {
393 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
394 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
395 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
396 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
397 ra_timer.t_color = lc_timer_register("ra_color", "graph coloring");
398 ra_timer.t_ifg = lc_timer_register("ra_ifg", "interference graph");
399 ra_timer.t_copymin = lc_timer_register("ra_copymin", "copy minimization");
400 ra_timer.t_ssa = lc_timer_register("ra_ssadestr", "ssa destruction");
401 ra_timer.t_verify = lc_timer_register("ra_verify", "graph verification");
402 ra_timer.t_other = lc_timer_register("ra_other", "other time");
404 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
405 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
406 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
407 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
408 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
409 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
410 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
411 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
412 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
413 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
416 #define BE_TIMER_PUSH(timer) \
417 if (main_opts->timing == BE_TIME_ON) { \
418 int res = lc_timer_push(timer); \
419 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
420 assert(res && "Timer already on stack, cannot be pushed twice."); \
421 else if (options.vrfy_option == BE_CH_VRFY_WARN && ! res) \
422 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
423 lc_timer_get_name(timer)); \
425 #define BE_TIMER_POP(timer) \
426 if (main_opts->timing == BE_TIME_ON) { \
427 lc_timer_t *tmp = lc_timer_pop(); \
428 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
429 assert(tmp == timer && "Attempt to pop wrong timer."); \
430 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
431 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
432 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
436 BE_TIMER_PUSH(ra_timer.t_other);
437 BE_TIMER_PUSH(ra_timer.t_prolog);
441 chordal_env.opts = &options;
442 chordal_env.irg = irg;
443 chordal_env.birg = bi;
444 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
445 chordal_env.exec_freq = compute_execfreq(irg, be_loop_weight);
446 chordal_env.lv = be_liveness(irg);
447 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
449 obstack_init(&chordal_env.obst);
451 BE_TIMER_POP(ra_timer.t_prolog);
453 /* Perform the following for each register class. */
454 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
456 copy_opt_t *co = NULL;
458 chordal_env.cls = arch_isa_get_reg_class(isa, j);
459 chordal_env.border_heads = pmap_create();
460 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
462 /* put all ignore registers into the ignore register set. */
463 put_ignore_colors(&chordal_env);
465 BE_TIMER_PUSH(ra_timer.t_live);
466 be_liveness_recompute(chordal_env.lv);
467 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_TIMER_PUSH(ra_timer.t_spill);
474 switch(options.spill_method) {
475 case BE_CH_SPILL_MORGAN:
476 be_spill_morgan(&chordal_env);
478 case BE_CH_SPILL_BELADY:
479 be_spill_belady(&chordal_env);
482 case BE_CH_SPILL_REMAT:
483 be_spill_remat(&chordal_env);
485 #endif /* WITH_ILP */
487 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
488 be_spill_belady(&chordal_env);
491 BE_TIMER_POP(ra_timer.t_spill);
493 DBG((chordal_env.dbg, LEVEL_1, "spill costs for %+F in regclass %s: %g\n",
495 chordal_env.cls->name,
496 get_irg_spill_cost(&chordal_env))
499 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
500 be_abi_fix_stack_nodes(bi->abi, chordal_env.lv);
501 be_compute_spill_offsets(&chordal_env);
502 check_for_memory_operands(&chordal_env);
504 BE_TIMER_PUSH(ra_timer.t_verify);
506 /* verify schedule and register pressure */
507 if (options.vrfy_option == BE_CH_VRFY_WARN) {
508 be_verify_schedule(irg);
509 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
511 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
512 assert(be_verify_schedule(irg) && "Schedule verification failed");
513 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
514 && "Register pressure verification failed");
516 BE_TIMER_POP(ra_timer.t_verify);
518 if (be_elr_split && ! splitted) {
519 extreme_liverange_splitting(&chordal_env);
524 /* Color the graph. */
525 BE_TIMER_PUSH(ra_timer.t_color);
526 be_ra_chordal_color(&chordal_env);
527 BE_TIMER_POP(ra_timer.t_color);
529 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
531 /* Create the ifg with the selected flavor */
532 BE_TIMER_PUSH(ra_timer.t_ifg);
533 switch (options.ifg_flavor) {
535 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
538 chordal_env.ifg = be_ifg_std_new(&chordal_env);
540 case BE_CH_IFG_CLIQUE:
541 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
543 case BE_CH_IFG_POINTER:
544 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
547 chordal_env.ifg = be_ifg_list_new(&chordal_env);
549 case BE_CH_IFG_CHECK:
550 check_ifg_implementations(&chordal_env);
551 /* Build the interference graph. */
552 chordal_env.ifg = be_ifg_std_new(&chordal_env);
555 BE_TIMER_POP(ra_timer.t_ifg);
557 BE_TIMER_PUSH(ra_timer.t_verify);
558 if (options.vrfy_option != BE_CH_VRFY_OFF)
559 be_ra_chordal_check(&chordal_env);
561 BE_TIMER_POP(ra_timer.t_verify);
563 /* copy minimization */
564 BE_TIMER_PUSH(ra_timer.t_copymin);
566 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_exec_freq);
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));
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 printf("final copy costs : %d\n", co_get_copy_costs(co));
616 co_free_graph_structure(co);
617 co_free_ou_structure(co);
620 BE_TIMER_POP(ra_timer.t_copymin);
622 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
624 BE_TIMER_PUSH(ra_timer.t_verify);
626 if (options.vrfy_option != BE_CH_VRFY_OFF)
627 be_ra_chordal_check(&chordal_env);
629 BE_TIMER_POP(ra_timer.t_verify);
630 BE_TIMER_PUSH(ra_timer.t_ssa);
632 /* ssa destruction */
633 be_ssa_destruction(&chordal_env);
635 BE_TIMER_POP(ra_timer.t_ssa);
637 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
639 BE_TIMER_PUSH(ra_timer.t_verify);
640 if (options.vrfy_option != BE_CH_VRFY_OFF) {
641 be_ssa_destruction_check(&chordal_env);
642 be_ra_chordal_check(&chordal_env);
644 BE_TIMER_POP(ra_timer.t_verify);
646 if (options.copymin_method == BE_CH_COPYMIN_STAT)
649 be_ifg_free(chordal_env.ifg);
650 pmap_destroy(chordal_env.border_heads);
651 bitset_free(chordal_env.ignore_colors);
654 BE_TIMER_PUSH(ra_timer.t_epilog);
656 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
658 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
659 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
661 obstack_free(&chordal_env.obst, NULL);
662 be_free_dominance_frontiers(chordal_env.dom_front);
663 be_liveness_free(chordal_env.lv);
664 free_execfreq(chordal_env.exec_freq);
666 BE_TIMER_POP(ra_timer.t_epilog);
667 BE_TIMER_POP(ra_timer.t_other);
672 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
675 const be_ra_t be_ra_chordal_allocator = {
677 be_ra_chordal_register_options,