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"
44 #include "bechordal_t.h"
46 #include "bejavacoal.h"
50 #include "besched_t.h"
54 #include "beifg_impl.h"
57 #include "bespillbelady.h"
58 #include "bespillmorgan.h"
59 #include "bespillslots.h"
63 #include "bespillremat.h"
66 #include "bejavacoal.h"
67 #include "becopystat.h"
68 #include "becopyopt.h"
69 #include "bessadestr.h"
71 #include "bespillcost.h"
74 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
75 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
78 ir_node **nodes, *n1, *n2;
80 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
82 /* Collect all irns */
84 pmap_foreach(chordal_env->border_heads, pme) {
86 struct list_head *head = pme->value;
87 list_for_each_entry(border_t, curr, head, list)
88 if (curr->is_def && curr->is_real)
89 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
90 obstack_ptr_grow(&ob, curr->irn);
92 obstack_ptr_grow(&ob, NULL);
93 nodes = (ir_node **) obstack_finish(&ob);
96 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
97 const arch_register_t *n1_reg, *n2_reg;
99 n1_reg = arch_get_irn_register(arch_env, n1);
100 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
101 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
102 assert(0 && "Register constraint does not hold");
104 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
105 n2_reg = arch_get_irn_register(arch_env, n2);
106 if (values_interfere(chordal_env->lv, n1, n2) && n1_reg == n2_reg) {
107 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
108 assert(0 && "Interfering values have the same color!");
112 obstack_free(&ob, NULL);
115 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
118 return be_ifg_connected(env->ifg, a, b);
120 return values_interfere(env->lv, a, b);
124 static be_ra_chordal_opts_t options = {
128 BE_CH_LOWER_PERM_SWAP,
132 static be_ra_timer_t ra_timer = {
147 static const lc_opt_enum_int_items_t spill_items[] = {
148 { "morgan", BE_CH_SPILL_MORGAN },
149 { "belady", BE_CH_SPILL_BELADY },
151 { "remat", BE_CH_SPILL_REMAT },
152 #endif /* WITH_ILP */
156 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
157 { "std", BE_CH_IFG_STD },
158 { "fast", BE_CH_IFG_FAST },
159 { "clique", BE_CH_IFG_CLIQUE },
160 { "pointer", BE_CH_IFG_POINTER },
161 { "list", BE_CH_IFG_LIST },
162 { "check", BE_CH_IFG_CHECK },
166 static const lc_opt_enum_int_items_t lower_perm_items[] = {
167 { "copy", BE_CH_LOWER_PERM_COPY },
168 { "swap", BE_CH_LOWER_PERM_SWAP },
172 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
176 static const lc_opt_enum_int_items_t dump_items[] = {
177 { "spill", BE_CH_DUMP_SPILL },
178 { "live", BE_CH_DUMP_LIVE },
179 { "color", BE_CH_DUMP_COLOR },
180 { "copymin", BE_CH_DUMP_COPYMIN },
181 { "ssadestr", BE_CH_DUMP_SSADESTR },
182 { "tree", BE_CH_DUMP_TREE_INTV },
183 { "constr", BE_CH_DUMP_CONSTR },
184 { "lower", BE_CH_DUMP_LOWER },
185 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
186 { "appel", BE_CH_DUMP_APPEL },
187 { "all", BE_CH_DUMP_ALL },
191 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
192 { "off", BE_CH_VRFY_OFF },
193 { "warn", BE_CH_VRFY_WARN },
194 { "assert", BE_CH_VRFY_ASSERT },
198 static lc_opt_enum_int_var_t spill_var = {
199 &options.spill_method, spill_items
202 static lc_opt_enum_int_var_t ifg_flavor_var = {
203 &options.ifg_flavor, ifg_flavor_items
206 static lc_opt_enum_int_var_t lower_perm_var = {
207 &options.lower_perm_opt, lower_perm_items
210 static lc_opt_enum_int_var_t dump_var = {
211 &options.dump_flags, dump_items
214 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
215 &options.vrfy_option, be_ch_vrfy_items
218 /** Enable extreme live range splitting. */
219 static int be_elr_split = 0;
221 /** Assumed loop iteration count for execution frequency estimation. */
222 static int be_loop_weight = 9;
224 static const lc_opt_table_entry_t be_chordal_options[] = {
225 LC_OPT_ENT_ENUM_INT ("spill", "spill method (belady, morgan or remat)", &spill_var),
226 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
227 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options (copy or swap)", &lower_perm_var),
228 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
229 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options (off, warn, assert)", &be_ch_vrfy_var),
230 LC_OPT_ENT_BOOL ("elrsplit", "enable extreme live range splitting", &be_elr_split),
231 LC_OPT_ENT_INT ("loop_weight", "assumed amount of loop iterations for guessing the execution frequency", &be_loop_weight),
235 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
237 static int run_once = 0;
238 lc_opt_entry_t *chordal_grp;
242 chordal_grp = lc_opt_get_grp(grp, "chordal");
244 lc_opt_add_table(chordal_grp, be_chordal_options);
247 co_register_options(chordal_grp);
248 be_java_coal_register_options(chordal_grp);
250 be_spill_remat_register_options(chordal_grp);
253 #endif /* WITH_LIBCORE */
255 static void dump(unsigned mask, ir_graph *irg,
256 const arch_register_class_t *cls,
258 void (*dump_func)(ir_graph *, const char *))
260 if((options.dump_flags & mask) == mask) {
263 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
264 be_dump(irg, buf, dump_func);
267 be_dump(irg, suffix, dump_func);
271 static void put_ignore_colors(be_chordal_env_t *chordal_env)
273 int n_colors = chordal_env->cls->n_regs;
276 bitset_clear_all(chordal_env->ignore_colors);
277 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
278 for(i = 0; i < n_colors; ++i)
279 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
280 bitset_set(chordal_env->ignore_colors, i);
283 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
287 ir_snprintf(buf, sizeof(buf), "%s%F_%s%s", prefix, env->irg, env->cls->name, suffix);
288 return fopen(buf, "wt");
291 void check_ifg_implementations(be_chordal_env_t *chordal_env)
295 f = be_chordal_open(chordal_env, "std", ".log");
296 chordal_env->ifg = be_ifg_std_new(chordal_env);
297 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
300 f = be_chordal_open(chordal_env, "list", ".log");
301 be_ifg_free(chordal_env->ifg);
302 chordal_env->ifg = be_ifg_list_new(chordal_env);
303 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
306 f = be_chordal_open(chordal_env, "clique", ".log");
307 be_ifg_free(chordal_env->ifg);
308 chordal_env->ifg = be_ifg_clique_new(chordal_env);
309 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
312 f = be_chordal_open(chordal_env, "pointer", ".log");
313 be_ifg_free(chordal_env->ifg);
314 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
315 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
318 chordal_env->ifg = NULL;
322 * Checks for every reload if it's user can perform the load on itself.
324 static void memory_operand_walker(ir_node *irn, void *env) {
325 be_chordal_env_t *cenv = env;
326 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
327 const ir_edge_t *edge, *ne;
330 if (! be_is_Reload(irn))
333 block = get_nodes_block(irn);
335 foreach_out_edge_safe(irn, edge, ne) {
336 ir_node *src = get_edge_src_irn(edge);
337 int pos = get_edge_src_pos(edge);
342 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
343 DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
344 arch_perform_memory_operand(aenv, src, irn, pos);
348 /* kill the Reload */
349 if (get_irn_n_edges(irn) == 0) {
351 set_irn_n(irn, 0, new_Bad());
352 set_irn_n(irn, 1, new_Bad());
357 * Starts a walk for memory operands if supported by the backend.
359 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
360 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
364 * Performs chordal register allocation for each register class on given irg.
366 * @param bi Backend irg object
367 * @return Structure containing timer for the single phases or NULL if no timing requested.
369 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
371 const be_main_env_t *main_env = bi->main_env;
372 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
373 ir_graph *irg = bi->irg;
374 be_options_t *main_opts = main_env->options;
378 be_chordal_env_t chordal_env;
380 if (main_opts->timing == BE_TIME_ON) {
381 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
382 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
383 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
384 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
385 ra_timer.t_spillslots = lc_timer_register("ra_spillslots", "spillslots");
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_spillslots);
398 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
399 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
400 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
401 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
402 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
403 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
406 #define BE_TIMER_PUSH(timer) \
407 if (main_opts->timing == BE_TIME_ON) { \
408 int res = lc_timer_push(timer); \
409 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
410 assert(res && "Timer already on stack, cannot be pushed twice."); \
411 else if (options.vrfy_option == BE_CH_VRFY_WARN && ! res) \
412 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
413 lc_timer_get_name(timer)); \
415 #define BE_TIMER_POP(timer) \
416 if (main_opts->timing == BE_TIME_ON) { \
417 lc_timer_t *tmp = lc_timer_pop(); \
418 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
419 assert(tmp == timer && "Attempt to pop wrong timer."); \
420 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
421 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
422 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
426 BE_TIMER_PUSH(ra_timer.t_other);
427 BE_TIMER_PUSH(ra_timer.t_prolog);
431 chordal_env.opts = &options;
432 chordal_env.irg = irg;
433 chordal_env.birg = bi;
434 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
435 chordal_env.exec_freq = compute_execfreq(irg, be_loop_weight);
436 chordal_env.lv = be_liveness(irg);
437 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
439 obstack_init(&chordal_env.obst);
441 BE_TIMER_POP(ra_timer.t_prolog);
443 /* Perform the following for each register class. */
444 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
445 chordal_env.cls = arch_isa_get_reg_class(isa, j);
446 chordal_env.border_heads = pmap_create();
447 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
449 /* put all ignore registers into the ignore register set. */
450 put_ignore_colors(&chordal_env);
452 BE_TIMER_PUSH(ra_timer.t_live);
453 be_liveness_recompute(chordal_env.lv);
454 BE_TIMER_POP(ra_timer.t_live);
455 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
457 be_pre_spill_prepare_constr(&chordal_env);
458 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-constr-pre", dump_ir_block_graph_sched);
460 BE_TIMER_PUSH(ra_timer.t_spill);
463 switch(options.spill_method) {
464 case BE_CH_SPILL_MORGAN:
465 be_spill_morgan(&chordal_env);
467 case BE_CH_SPILL_BELADY:
468 be_spill_belady(&chordal_env);
471 case BE_CH_SPILL_REMAT:
472 be_spill_remat(&chordal_env);
474 #endif /* WITH_ILP */
476 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
477 be_spill_belady(&chordal_env);
480 BE_TIMER_POP(ra_timer.t_spill);
482 DBG((chordal_env.dbg, LEVEL_1, "spill costs for %+F in regclass %s: %g\n",
484 chordal_env.cls->name,
485 get_irg_spill_cost(&chordal_env))
488 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
489 be_abi_fix_stack_nodes(bi->abi, chordal_env.lv);
491 BE_TIMER_PUSH(ra_timer.t_verify);
493 /* verify schedule and register pressure */
494 if (options.vrfy_option == BE_CH_VRFY_WARN) {
495 be_verify_schedule(irg);
496 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
498 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
499 assert(be_verify_schedule(irg) && "Schedule verification failed");
500 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
501 && "Register pressure verification failed");
503 BE_TIMER_POP(ra_timer.t_verify);
505 if (be_elr_split && ! splitted) {
506 extreme_liverange_splitting(&chordal_env);
511 /* Color the graph. */
512 BE_TIMER_PUSH(ra_timer.t_color);
513 be_ra_chordal_color(&chordal_env);
514 BE_TIMER_POP(ra_timer.t_color);
516 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
518 /* Create the ifg with the selected flavor */
519 BE_TIMER_PUSH(ra_timer.t_ifg);
520 switch (options.ifg_flavor) {
522 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
525 chordal_env.ifg = be_ifg_std_new(&chordal_env);
527 case BE_CH_IFG_CLIQUE:
528 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
530 case BE_CH_IFG_POINTER:
531 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
534 chordal_env.ifg = be_ifg_list_new(&chordal_env);
536 case BE_CH_IFG_CHECK:
537 check_ifg_implementations(&chordal_env);
538 /* Build the interference graph. */
539 chordal_env.ifg = be_ifg_std_new(&chordal_env);
542 BE_TIMER_POP(ra_timer.t_ifg);
544 BE_TIMER_PUSH(ra_timer.t_verify);
545 if (options.vrfy_option != BE_CH_VRFY_OFF)
546 be_ra_chordal_check(&chordal_env);
548 BE_TIMER_POP(ra_timer.t_verify);
550 /* copy minimization */
551 BE_TIMER_PUSH(ra_timer.t_copymin);
552 co_driver(&chordal_env);
553 BE_TIMER_POP(ra_timer.t_copymin);
554 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
556 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);
562 BE_TIMER_PUSH(ra_timer.t_ssa);
564 /* ssa destruction */
565 be_ssa_destruction(&chordal_env);
567 BE_TIMER_POP(ra_timer.t_ssa);
569 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
571 BE_TIMER_PUSH(ra_timer.t_verify);
572 if (options.vrfy_option != BE_CH_VRFY_OFF) {
573 be_ssa_destruction_check(&chordal_env);
574 be_ra_chordal_check(&chordal_env);
576 BE_TIMER_POP(ra_timer.t_verify);
578 be_ifg_free(chordal_env.ifg);
579 pmap_destroy(chordal_env.border_heads);
580 bitset_free(chordal_env.ignore_colors);
583 BE_TIMER_PUSH(ra_timer.t_spillslots);
585 be_coalesce_spillslots(&chordal_env);
586 dump(BE_CH_DUMP_SPILLSLOTS, irg, NULL, "-spillslots", dump_ir_block_graph_sched);
588 BE_TIMER_POP(ra_timer.t_spillslots);
590 BE_TIMER_PUSH(ra_timer.t_verify);
592 /* verify spillslots */
593 if (options.vrfy_option == BE_CH_VRFY_WARN) {
594 be_verify_spillslots(irg);
596 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
597 assert(be_verify_spillslots(irg) && "Spillslot verification failed");
599 BE_TIMER_POP(ra_timer.t_verify);
601 check_for_memory_operands(&chordal_env);
603 BE_TIMER_PUSH(ra_timer.t_epilog);
605 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
607 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
608 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
610 obstack_free(&chordal_env.obst, NULL);
611 be_free_dominance_frontiers(chordal_env.dom_front);
612 be_liveness_free(chordal_env.lv);
613 free_execfreq(chordal_env.exec_freq);
615 BE_TIMER_POP(ra_timer.t_epilog);
616 BE_TIMER_POP(ra_timer.t_other);
621 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
624 const be_ra_t be_ra_chordal_allocator = {
626 be_ra_chordal_register_options,