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"
62 #include "bespillremat.h"
65 #include "bejavacoal.h"
66 #include "becopystat.h"
67 #include "becopyopt.h"
68 #include "bessadestr.h"
70 #include "bespillcost.h"
73 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
74 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
77 ir_node **nodes, *n1, *n2;
79 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
81 /* Collect all irns */
83 pmap_foreach(chordal_env->border_heads, pme) {
85 struct list_head *head = pme->value;
86 list_for_each_entry(border_t, curr, head, list)
87 if (curr->is_def && curr->is_real)
88 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
89 obstack_ptr_grow(&ob, curr->irn);
91 obstack_ptr_grow(&ob, NULL);
92 nodes = (ir_node **) obstack_finish(&ob);
95 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
96 const arch_register_t *n1_reg, *n2_reg;
98 n1_reg = arch_get_irn_register(arch_env, n1);
99 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
100 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
101 assert(0 && "Register constraint does not hold");
103 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
104 n2_reg = arch_get_irn_register(arch_env, n2);
105 if (values_interfere(chordal_env->lv, n1, n2) && n1_reg == n2_reg) {
106 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
107 assert(0 && "Interfering values have the same color!");
111 obstack_free(&ob, NULL);
114 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
117 return be_ifg_connected(env->ifg, a, b);
119 return values_interfere(env->lv, a, b);
123 static be_ra_chordal_opts_t options = {
127 BE_CH_LOWER_PERM_SWAP,
131 static be_ra_timer_t ra_timer = {
145 static const lc_opt_enum_int_items_t spill_items[] = {
146 { "morgan", BE_CH_SPILL_MORGAN },
147 { "belady", BE_CH_SPILL_BELADY },
149 { "remat", BE_CH_SPILL_REMAT },
150 #endif /* WITH_ILP */
154 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
155 { "std", BE_CH_IFG_STD },
156 { "fast", BE_CH_IFG_FAST },
157 { "clique", BE_CH_IFG_CLIQUE },
158 { "pointer", BE_CH_IFG_POINTER },
159 { "list", BE_CH_IFG_LIST },
160 { "check", BE_CH_IFG_CHECK },
164 static const lc_opt_enum_int_items_t lower_perm_items[] = {
165 { "copy", BE_CH_LOWER_PERM_COPY },
166 { "swap", BE_CH_LOWER_PERM_SWAP },
170 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
174 static const lc_opt_enum_int_items_t dump_items[] = {
175 { "spill", BE_CH_DUMP_SPILL },
176 { "live", BE_CH_DUMP_LIVE },
177 { "color", BE_CH_DUMP_COLOR },
178 { "copymin", BE_CH_DUMP_COPYMIN },
179 { "ssadestr", BE_CH_DUMP_SSADESTR },
180 { "tree", BE_CH_DUMP_TREE_INTV },
181 { "constr", BE_CH_DUMP_CONSTR },
182 { "lower", BE_CH_DUMP_LOWER },
183 { "appel", BE_CH_DUMP_APPEL },
184 { "all", BE_CH_DUMP_ALL },
188 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
189 { "off", BE_CH_VRFY_OFF },
190 { "warn", BE_CH_VRFY_WARN },
191 { "assert", BE_CH_VRFY_ASSERT },
195 static lc_opt_enum_int_var_t spill_var = {
196 &options.spill_method, spill_items
199 static lc_opt_enum_int_var_t ifg_flavor_var = {
200 &options.ifg_flavor, ifg_flavor_items
203 static lc_opt_enum_int_var_t lower_perm_var = {
204 &options.lower_perm_opt, lower_perm_items
207 static lc_opt_enum_int_var_t dump_var = {
208 &options.dump_flags, dump_items
211 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
212 &options.vrfy_option, be_ch_vrfy_items
215 /** Enable extreme live range splitting. */
216 static int be_elr_split = 0;
218 /** Assumed loop iteration count for execution frequency estimation. */
219 static int be_loop_weight = 9;
221 static const lc_opt_table_entry_t be_chordal_options[] = {
222 LC_OPT_ENT_ENUM_INT ("spill", "spill method (belady, morgan or remat)", &spill_var),
223 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
224 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options (copy or swap)", &lower_perm_var),
225 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
226 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options (off, warn, assert)", &be_ch_vrfy_var),
227 LC_OPT_ENT_BOOL ("elrsplit", "enable extreme live range splitting", &be_elr_split),
228 LC_OPT_ENT_INT ("loop_weight", "assumed amount of loop iterations for guessing the execution frequency", &be_loop_weight),
232 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
234 static int run_once = 0;
235 lc_opt_entry_t *chordal_grp;
239 chordal_grp = lc_opt_get_grp(grp, "chordal");
241 lc_opt_add_table(chordal_grp, be_chordal_options);
244 co_register_options(chordal_grp);
245 be_java_coal_register_options(chordal_grp);
247 #endif /* WITH_LIBCORE */
249 static void dump(unsigned mask, ir_graph *irg,
250 const arch_register_class_t *cls,
252 void (*dump_func)(ir_graph *, const char *))
254 if((options.dump_flags & mask) == mask) {
257 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
258 be_dump(irg, buf, dump_func);
261 be_dump(irg, suffix, dump_func);
265 static void put_ignore_colors(be_chordal_env_t *chordal_env)
267 int n_colors = chordal_env->cls->n_regs;
270 bitset_clear_all(chordal_env->ignore_colors);
271 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
272 for(i = 0; i < n_colors; ++i)
273 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
274 bitset_set(chordal_env->ignore_colors, i);
277 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
281 ir_snprintf(buf, sizeof(buf), "%s%F_%s%s", prefix, env->irg, env->cls->name, suffix);
282 return fopen(buf, "wt");
285 void check_ifg_implementations(be_chordal_env_t *chordal_env)
289 f = be_chordal_open(chordal_env, "std", ".log");
290 chordal_env->ifg = be_ifg_std_new(chordal_env);
291 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
294 f = be_chordal_open(chordal_env, "list", ".log");
295 be_ifg_free(chordal_env->ifg);
296 chordal_env->ifg = be_ifg_list_new(chordal_env);
297 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
300 f = be_chordal_open(chordal_env, "clique", ".log");
301 be_ifg_free(chordal_env->ifg);
302 chordal_env->ifg = be_ifg_clique_new(chordal_env);
303 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
306 f = be_chordal_open(chordal_env, "pointer", ".log");
307 be_ifg_free(chordal_env->ifg);
308 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
309 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
312 chordal_env->ifg = NULL;
316 * Checks for every reload if it's user can perform the load on itself.
318 static void memory_operand_walker(ir_node *irn, void *env) {
319 be_chordal_env_t *cenv = env;
320 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
321 const ir_edge_t *edge, *ne;
324 if (! be_is_Reload(irn))
327 block = get_nodes_block(irn);
329 foreach_out_edge_safe(irn, edge, ne) {
330 ir_node *src = get_edge_src_irn(edge);
331 int pos = get_edge_src_pos(edge);
336 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
337 DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
338 arch_perform_memory_operand(aenv, src, irn, pos);
342 /* kill the Reload */
343 if (get_irn_n_edges(irn) == 0) {
345 set_irn_n(irn, 0, new_Bad());
346 set_irn_n(irn, 1, new_Bad());
351 * Starts a walk for memory operands if supported by the backend.
353 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
354 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
358 * Performs chordal register allocation for each register class on given irg.
360 * @param bi Backend irg object
361 * @return Structure containing timer for the single phases or NULL if no timing requested.
363 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
365 const be_main_env_t *main_env = bi->main_env;
366 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
367 ir_graph *irg = bi->irg;
368 be_options_t *main_opts = main_env->options;
372 be_chordal_env_t chordal_env;
374 if (main_opts->timing == BE_TIME_ON) {
375 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
376 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
377 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
378 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
379 ra_timer.t_color = lc_timer_register("ra_color", "graph coloring");
380 ra_timer.t_ifg = lc_timer_register("ra_ifg", "interference graph");
381 ra_timer.t_copymin = lc_timer_register("ra_copymin", "copy minimization");
382 ra_timer.t_ssa = lc_timer_register("ra_ssadestr", "ssa destruction");
383 ra_timer.t_verify = lc_timer_register("ra_verify", "graph verification");
384 ra_timer.t_other = lc_timer_register("ra_other", "other time");
386 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
387 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
388 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
389 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
390 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
391 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
392 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
393 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
394 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
395 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
398 #define BE_TIMER_PUSH(timer) \
399 if (main_opts->timing == BE_TIME_ON) { \
400 int res = lc_timer_push(timer); \
401 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
402 assert(res && "Timer already on stack, cannot be pushed twice."); \
403 else if (options.vrfy_option == BE_CH_VRFY_WARN && ! res) \
404 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
405 lc_timer_get_name(timer)); \
407 #define BE_TIMER_POP(timer) \
408 if (main_opts->timing == BE_TIME_ON) { \
409 lc_timer_t *tmp = lc_timer_pop(); \
410 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
411 assert(tmp == timer && "Attempt to pop wrong timer."); \
412 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
413 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
414 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
418 BE_TIMER_PUSH(ra_timer.t_other);
419 BE_TIMER_PUSH(ra_timer.t_prolog);
423 chordal_env.opts = &options;
424 chordal_env.irg = irg;
425 chordal_env.birg = bi;
426 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
427 chordal_env.exec_freq = compute_execfreq(irg, be_loop_weight);
428 chordal_env.lv = be_liveness(irg);
429 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
431 obstack_init(&chordal_env.obst);
433 BE_TIMER_POP(ra_timer.t_prolog);
435 /* Perform the following for each register class. */
436 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
438 copy_opt_t *co = NULL;
440 chordal_env.cls = arch_isa_get_reg_class(isa, j);
441 chordal_env.border_heads = pmap_create();
442 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
444 /* put all ignore registers into the ignore register set. */
445 put_ignore_colors(&chordal_env);
447 BE_TIMER_PUSH(ra_timer.t_live);
448 be_liveness_recompute(chordal_env.lv);
449 BE_TIMER_POP(ra_timer.t_live);
450 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
452 be_pre_spill_prepare_constr(&chordal_env);
453 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-constr-pre", dump_ir_block_graph_sched);
455 BE_TIMER_PUSH(ra_timer.t_spill);
458 switch(options.spill_method) {
459 case BE_CH_SPILL_MORGAN:
460 be_spill_morgan(&chordal_env);
462 case BE_CH_SPILL_BELADY:
463 be_spill_belady(&chordal_env);
466 case BE_CH_SPILL_REMAT:
467 be_spill_remat(&chordal_env);
469 #endif /* WITH_ILP */
471 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
472 be_spill_belady(&chordal_env);
475 BE_TIMER_POP(ra_timer.t_spill);
477 DBG((chordal_env.dbg, LEVEL_1, "spill costs for %+F in regclass %s: %g\n",
479 chordal_env.cls->name,
480 get_irg_spill_cost(&chordal_env))
483 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
484 be_compute_spill_offsets(&chordal_env);
485 //be_coalesce_spillslots(&chordal_env);
486 check_for_memory_operands(&chordal_env);
487 be_abi_fix_stack_nodes(bi->abi, chordal_env.lv);
489 BE_TIMER_PUSH(ra_timer.t_verify);
491 /* verify schedule and register pressure */
492 if (options.vrfy_option == BE_CH_VRFY_WARN) {
493 be_verify_schedule(irg);
494 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
496 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
497 assert(be_verify_schedule(irg) && "Schedule verification failed");
498 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
499 && "Register pressure verification failed");
501 BE_TIMER_POP(ra_timer.t_verify);
503 if (be_elr_split && ! splitted) {
504 extreme_liverange_splitting(&chordal_env);
509 /* Color the graph. */
510 BE_TIMER_PUSH(ra_timer.t_color);
511 be_ra_chordal_color(&chordal_env);
512 BE_TIMER_POP(ra_timer.t_color);
514 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
516 /* Create the ifg with the selected flavor */
517 BE_TIMER_PUSH(ra_timer.t_ifg);
518 switch (options.ifg_flavor) {
520 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
523 chordal_env.ifg = be_ifg_std_new(&chordal_env);
525 case BE_CH_IFG_CLIQUE:
526 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
528 case BE_CH_IFG_POINTER:
529 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
532 chordal_env.ifg = be_ifg_list_new(&chordal_env);
534 case BE_CH_IFG_CHECK:
535 check_ifg_implementations(&chordal_env);
536 /* Build the interference graph. */
537 chordal_env.ifg = be_ifg_std_new(&chordal_env);
540 BE_TIMER_POP(ra_timer.t_ifg);
542 BE_TIMER_PUSH(ra_timer.t_verify);
543 if (options.vrfy_option != BE_CH_VRFY_OFF)
544 be_ra_chordal_check(&chordal_env);
546 BE_TIMER_POP(ra_timer.t_verify);
548 /* copy minimization */
549 BE_TIMER_PUSH(ra_timer.t_copymin);
550 co_driver(&chordal_env);
551 BE_TIMER_POP(ra_timer.t_copymin);
552 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
554 BE_TIMER_PUSH(ra_timer.t_verify);
556 if (options.vrfy_option != BE_CH_VRFY_OFF)
557 be_ra_chordal_check(&chordal_env);
559 BE_TIMER_POP(ra_timer.t_verify);
560 BE_TIMER_PUSH(ra_timer.t_ssa);
562 /* ssa destruction */
563 be_ssa_destruction(&chordal_env);
565 BE_TIMER_POP(ra_timer.t_ssa);
567 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
569 BE_TIMER_PUSH(ra_timer.t_verify);
570 if (options.vrfy_option != BE_CH_VRFY_OFF) {
571 be_ssa_destruction_check(&chordal_env);
572 be_ra_chordal_check(&chordal_env);
574 BE_TIMER_POP(ra_timer.t_verify);
576 be_ifg_free(chordal_env.ifg);
577 pmap_destroy(chordal_env.border_heads);
578 bitset_free(chordal_env.ignore_colors);
581 BE_TIMER_PUSH(ra_timer.t_epilog);
583 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
585 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
586 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
588 obstack_free(&chordal_env.obst, NULL);
589 be_free_dominance_frontiers(chordal_env.dom_front);
590 be_liveness_free(chordal_env.lv);
591 free_execfreq(chordal_env.exec_freq);
593 BE_TIMER_POP(ra_timer.t_epilog);
594 BE_TIMER_POP(ra_timer.t_other);
599 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
602 const be_ra_t be_ra_chordal_allocator = {
604 be_ra_chordal_register_options,