2 * @file bechordal_main.c
4 * @author Sebastian Hack
6 * Copyright (C) 2005 Universitaet Karlsruhe
7 * Released under the GPL
9 * Driver for the chordal register allocator.
20 #include "firm_config.h"
23 #include <libcore/lc_opts.h>
24 #include <libcore/lc_opts_enum.h>
25 #include <libcore/lc_timing.h>
26 #endif /* WITH_LIBCORE */
29 #include "irgraph_t.h"
30 #include "irprintf_t.h"
39 #include "bechordal_t.h"
44 #include "besched_t.h"
48 #include "beifg_impl.h"
50 #include "bespillbelady.h"
51 #include "bespillmorgan.h"
55 #include "bespillilp.h"
56 #include "bespillremat.h"
59 #include "becopystat.h"
60 #include "becopyopt.h"
61 #include "bessadestr.h"
64 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
65 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
68 ir_node **nodes, *n1, *n2;
70 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
72 /* Collect all irns */
74 pmap_foreach(chordal_env->border_heads, pme) {
76 struct list_head *head = pme->value;
77 list_for_each_entry(border_t, curr, head, list)
78 if (curr->is_def && curr->is_real)
79 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
80 obstack_ptr_grow(&ob, curr->irn);
82 obstack_ptr_grow(&ob, NULL);
83 nodes = (ir_node **) obstack_finish(&ob);
86 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
87 const arch_register_t *n1_reg, *n2_reg;
89 n1_reg = arch_get_irn_register(arch_env, n1);
90 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
91 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
92 assert(0 && "Register constraint does not hold");
94 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
95 n2_reg = arch_get_irn_register(arch_env, n2);
96 if (values_interfere(n1, n2) && n1_reg == n2_reg) {
97 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
98 assert(0 && "Interfering values have the same color!");
102 obstack_free(&ob, NULL);
105 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
108 return be_ifg_connected(env->ifg, a, b);
110 return values_interfere(a, b);
114 static be_ra_chordal_opts_t options = {
119 BE_CH_LOWER_PERM_SWAP,
123 static be_ra_timer_t ra_timer = {
137 static const lc_opt_enum_int_items_t spill_items[] = {
138 { "morgan", BE_CH_SPILL_MORGAN },
139 { "belady", BE_CH_SPILL_BELADY },
141 { "ilp", BE_CH_SPILL_ILP },
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, ilp, remat or appel)", &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;
323 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
325 const be_main_env_t *main_env = bi->main_env;
326 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
327 ir_graph *irg = bi->irg;
328 be_options_t *main_opts = main_env->options;
332 be_chordal_env_t chordal_env;
334 if (main_opts->timing == BE_TIME_ON) {
335 ra_timer.t_prolog = lc_timer_register("prolog", "regalloc prolog");
336 ra_timer.t_epilog = lc_timer_register("epilog", "regalloc epilog");
337 ra_timer.t_live = lc_timer_register("liveness", "be liveness");
338 ra_timer.t_spill = lc_timer_register("spill", "spiller");
339 ra_timer.t_color = lc_timer_register("color", "graph coloring");
340 ra_timer.t_ifg = lc_timer_register("ifg", "interference graph");
341 ra_timer.t_copymin = lc_timer_register("copymin", "copy minimization");
342 ra_timer.t_ssa = lc_timer_register("ssadestr", "ssa destruction");
343 ra_timer.t_verify = lc_timer_register("verify", "graph verification");
344 ra_timer.t_other = lc_timer_register("other", "other time");
346 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
347 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
348 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
349 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
350 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
351 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
352 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
353 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
354 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
355 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
358 #define BE_TIMER_PUSH(timer) if (main_opts->timing == BE_TIME_ON) lc_timer_push(timer)
359 #define BE_TIMER_POP() if (main_opts->timing == BE_TIME_ON) lc_timer_pop()
361 BE_TIMER_PUSH(ra_timer.t_other);
362 BE_TIMER_PUSH(ra_timer.t_prolog);
366 chordal_env.opts = &options;
367 chordal_env.irg = irg;
368 chordal_env.birg = bi;
369 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
370 chordal_env.exec_freq = compute_execfreq(irg, be_loop_weight);
371 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
373 obstack_init(&chordal_env.obst);
377 /* Perform the following for each register class. */
378 for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
380 chordal_env.cls = arch_isa_get_reg_class(isa, j);
381 chordal_env.border_heads = pmap_create();
382 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
384 BE_TIMER_PUSH(ra_timer.t_live);
386 /* put all ignore registers into the ignore register set. */
387 put_ignore_colors(&chordal_env);
393 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
395 BE_TIMER_PUSH(ra_timer.t_spill);
398 switch(options.spill_method) {
399 case BE_CH_SPILL_MORGAN:
400 be_spill_morgan(&chordal_env);
402 case BE_CH_SPILL_BELADY:
403 be_spill_belady(&chordal_env);
406 case BE_CH_SPILL_ILP:
407 be_spill_ilp(&chordal_env);
409 case BE_CH_SPILL_REMAT:
410 be_spill_remat(&chordal_env);
412 #endif /* WITH_ILP */
414 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
415 be_spill_belady(&chordal_env);
420 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
421 be_abi_fix_stack_nodes(bi->abi);
423 BE_TIMER_PUSH(ra_timer.t_verify);
425 /* verify schedule and register pressure */
426 if (options.vrfy_option == BE_CH_VRFY_WARN) {
427 be_verify_schedule(irg);
428 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
430 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
431 assert(be_verify_schedule(irg) && "Schedule verification failed");
432 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
433 && "Register pressure verification failed");
437 BE_TIMER_PUSH(ra_timer.t_live);
440 BE_TIMER_PUSH(ra_timer.t_color);
442 /* Color the graph. */
443 be_ra_chordal_color(&chordal_env);
447 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
449 BE_TIMER_PUSH(ra_timer.t_ifg);
451 /* Create the ifg with the selected flavor */
452 switch (options.ifg_flavor) {
454 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
457 chordal_env.ifg = be_ifg_std_new(&chordal_env);
459 case BE_CH_IFG_CLIQUE:
460 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
462 case BE_CH_IFG_POINTER:
463 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
466 chordal_env.ifg = be_ifg_list_new(&chordal_env);
468 case BE_CH_IFG_CHECK:
469 check_ifg_implementations(&chordal_env);
470 /* Build the interference graph. */
471 chordal_env.ifg = be_ifg_std_new(&chordal_env);
478 be_ifg_t *std = be_ifg_std_new(&chordal_env);
479 f = be_chordal_open(&chordal_env, "std", "csv");
480 be_ifg_check_sorted_to_file(std, f);
485 f = be_chordal_open(&chordal_env, "clique", "csv");
486 be_ifg_check_sorted_to_file(chordal_env.ifg, f);
491 BE_TIMER_PUSH(ra_timer.t_verify);
493 if (options.vrfy_option != BE_CH_VRFY_OFF)
494 be_ra_chordal_check(&chordal_env);
496 // be_ifg_check_sorted(chordal_env.ifg);
498 BE_TIMER_PUSH(ra_timer.t_copymin);
500 /* copy minimization */
502 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
504 co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
505 co_build_ou_structure(co);
506 co_build_graph_structure(co);
507 if(be_copymin_stats) {
508 ir_printf("%40F %20s\n", current_ir_graph, chordal_env.cls->name);
509 printf("max copy costs: %d\n", co_get_max_copy_costs(co));
510 printf("init copy costs: %d\n", co_get_copy_costs(co));
511 printf("inevit copy costs: %d\n", co_get_inevit_copy_costs(co));
512 printf("copy costs lower bound: %d\n", co_get_lower_bound(co));
516 f = be_chordal_open(&chordal_env, "appel-", "apl");
517 co_dump_appel_graph(co, f);
519 f = be_chordal_open(&chordal_env, "appel-clique-", "p");
520 co_dump_appel_graph_cliques(co, f);
525 switch(options.copymin_method) {
526 case BE_CH_COPYMIN_HEUR1:
527 co_solve_heuristic(co);
529 case BE_CH_COPYMIN_HEUR2:
530 co_solve_heuristic_new(co);
532 case BE_CH_COPYMIN_PARK_MOON:
533 co_solve_park_moon(co);
535 case BE_CH_COPYMIN_STAT:
536 co_compare_solvers(&chordal_env);
539 case BE_CH_COPYMIN_ILP1:
540 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
541 co_solve_ilp1(co, 60.0);
543 case BE_CH_COPYMIN_ILP2:
544 co_solve_ilp2(co, 60.0);
546 #endif /* WITH_ILP */
547 case BE_CH_COPYMIN_NONE:
553 if(be_copymin_stats) {
554 printf("final copy costs : %d\n", co_get_copy_costs(co));
556 co_free_graph_structure(co);
557 co_free_ou_structure(co);
563 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
565 BE_TIMER_PUSH(ra_timer.t_verify);
567 if (options.vrfy_option != BE_CH_VRFY_OFF)
568 be_ra_chordal_check(&chordal_env);
571 BE_TIMER_PUSH(ra_timer.t_ssa);
573 /* ssa destruction */
574 be_ssa_destruction(&chordal_env);
578 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
580 BE_TIMER_PUSH(ra_timer.t_verify);
581 if (options.vrfy_option != BE_CH_VRFY_OFF) {
582 be_ssa_destruction_check(&chordal_env);
583 be_ra_chordal_check(&chordal_env);
587 if (options.copymin_method == BE_CH_COPYMIN_STAT)
590 be_ifg_free(chordal_env.ifg);
591 pmap_destroy(chordal_env.border_heads);
592 bitset_free(chordal_env.ignore_colors);
595 BE_TIMER_PUSH(ra_timer.t_epilog);
597 be_compute_spill_offsets(&chordal_env);
599 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
601 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
602 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
604 obstack_free(&chordal_env.obst, NULL);
605 be_free_dominance_frontiers(chordal_env.dom_front);
606 free_execfreq(chordal_env.exec_freq);
614 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
617 const be_ra_t be_ra_chordal_allocator = {
619 be_ra_chordal_register_options,