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"
42 #include "bechordal_t.h"
44 #include "bejavacoal.h"
48 #include "besched_t.h"
52 #include "beifg_impl.h"
55 #include "bespillbelady.h"
56 #include "bespillmorgan.h"
60 #include "bespillremat.h"
63 #include "becopystat.h"
64 #include "becopyopt.h"
65 #include "bessadestr.h"
67 #include "bespillcost.h"
69 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
70 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
73 ir_node **nodes, *n1, *n2;
75 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
77 /* Collect all irns */
79 pmap_foreach(chordal_env->border_heads, pme) {
81 struct list_head *head = pme->value;
82 list_for_each_entry(border_t, curr, head, list)
83 if (curr->is_def && curr->is_real)
84 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
85 obstack_ptr_grow(&ob, curr->irn);
87 obstack_ptr_grow(&ob, NULL);
88 nodes = (ir_node **) obstack_finish(&ob);
91 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
92 const arch_register_t *n1_reg, *n2_reg;
94 n1_reg = arch_get_irn_register(arch_env, n1);
95 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
96 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
97 assert(0 && "Register constraint does not hold");
99 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
100 n2_reg = arch_get_irn_register(arch_env, n2);
101 if (values_interfere(chordal_env->lv, n1, n2) && n1_reg == n2_reg) {
102 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
103 assert(0 && "Interfering values have the same color!");
107 obstack_free(&ob, NULL);
110 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
113 return be_ifg_connected(env->ifg, a, b);
115 return values_interfere(env->lv, a, b);
119 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 ifg_flavor_items[] = {
151 { "std", BE_CH_IFG_STD },
152 { "fast", BE_CH_IFG_FAST },
153 { "clique", BE_CH_IFG_CLIQUE },
154 { "pointer", BE_CH_IFG_POINTER },
155 { "list", BE_CH_IFG_LIST },
156 { "check", BE_CH_IFG_CHECK },
160 static const lc_opt_enum_int_items_t lower_perm_items[] = {
161 { "copy", BE_CH_LOWER_PERM_COPY },
162 { "swap", BE_CH_LOWER_PERM_SWAP },
166 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
170 static const lc_opt_enum_int_items_t dump_items[] = {
171 { "spill", BE_CH_DUMP_SPILL },
172 { "live", BE_CH_DUMP_LIVE },
173 { "color", BE_CH_DUMP_COLOR },
174 { "copymin", BE_CH_DUMP_COPYMIN },
175 { "ssadestr", BE_CH_DUMP_SSADESTR },
176 { "tree", BE_CH_DUMP_TREE_INTV },
177 { "constr", BE_CH_DUMP_CONSTR },
178 { "lower", BE_CH_DUMP_LOWER },
179 { "appel", BE_CH_DUMP_APPEL },
180 { "all", BE_CH_DUMP_ALL },
184 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
185 { "off", BE_CH_VRFY_OFF },
186 { "warn", BE_CH_VRFY_WARN },
187 { "assert", BE_CH_VRFY_ASSERT },
191 static lc_opt_enum_int_var_t spill_var = {
192 &options.spill_method, spill_items
195 static lc_opt_enum_int_var_t ifg_flavor_var = {
196 &options.ifg_flavor, ifg_flavor_items
199 static lc_opt_enum_int_var_t lower_perm_var = {
200 &options.lower_perm_opt, lower_perm_items
203 static lc_opt_enum_int_var_t dump_var = {
204 &options.dump_flags, dump_items
207 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
208 &options.vrfy_option, be_ch_vrfy_items
211 /** Enable extreme live range splitting. */
212 static int be_elr_split = 0;
214 /** Assumed loop iteration count for execution frequency estimation. */
215 static int be_loop_weight = 9;
217 static const lc_opt_table_entry_t be_chordal_options[] = {
218 LC_OPT_ENT_ENUM_INT ("spill", "spill method (belady, morgan or remat)", &spill_var),
219 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
220 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options (copy or swap)", &lower_perm_var),
221 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
222 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options (off, warn, assert)", &be_ch_vrfy_var),
223 LC_OPT_ENT_BOOL ("elrsplit", "enable extreme live range splitting", &be_elr_split),
224 LC_OPT_ENT_INT ("loop_weight", "assumed amount of loop iterations for guessing the execution frequency", &be_loop_weight),
228 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
230 static int run_once = 0;
231 lc_opt_entry_t *chordal_grp;
235 chordal_grp = lc_opt_get_grp(grp, "chordal");
237 lc_opt_add_table(chordal_grp, be_chordal_options);
240 co_register_options(chordal_grp);
241 java_coal_register_options(chordal_grp);
243 #endif /* WITH_LIBCORE */
245 static void dump(unsigned mask, ir_graph *irg,
246 const arch_register_class_t *cls,
248 void (*dump_func)(ir_graph *, const char *))
250 if((options.dump_flags & mask) == mask) {
253 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
254 be_dump(irg, buf, dump_func);
257 be_dump(irg, suffix, dump_func);
261 static void put_ignore_colors(be_chordal_env_t *chordal_env)
263 int n_colors = chordal_env->cls->n_regs;
266 bitset_clear_all(chordal_env->ignore_colors);
267 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
268 for(i = 0; i < n_colors; ++i)
269 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
270 bitset_set(chordal_env->ignore_colors, i);
273 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
277 ir_snprintf(buf, sizeof(buf), "%s%F_%s%s", prefix, env->irg, env->cls->name, suffix);
278 return fopen(buf, "wt");
281 void check_ifg_implementations(be_chordal_env_t *chordal_env)
285 f = be_chordal_open(chordal_env, "std", ".log");
286 chordal_env->ifg = be_ifg_std_new(chordal_env);
287 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
290 f = be_chordal_open(chordal_env, "list", ".log");
291 be_ifg_free(chordal_env->ifg);
292 chordal_env->ifg = be_ifg_list_new(chordal_env);
293 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
296 f = be_chordal_open(chordal_env, "clique", ".log");
297 be_ifg_free(chordal_env->ifg);
298 chordal_env->ifg = be_ifg_clique_new(chordal_env);
299 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
302 f = be_chordal_open(chordal_env, "pointer", ".log");
303 be_ifg_free(chordal_env->ifg);
304 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
305 be_ifg_check_sorted_to_file(chordal_env->ifg, f);
308 chordal_env->ifg = NULL;
312 * Checks for every reload if it's user can perform the load on itself.
314 static void memory_operand_walker(ir_node *irn, void *env) {
315 be_chordal_env_t *cenv = env;
316 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
317 const ir_edge_t *edge, *ne;
320 if (! be_is_Reload(irn))
323 block = get_nodes_block(irn);
325 foreach_out_edge_safe(irn, edge, ne) {
326 ir_node *src = get_edge_src_irn(edge);
327 int pos = get_edge_src_pos(edge);
332 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
333 DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
334 arch_perform_memory_operand(aenv, src, irn, pos);
338 /* kill the Reload */
339 if (get_irn_n_edges(irn) == 0) {
341 set_irn_n(irn, 0, new_Bad());
342 set_irn_n(irn, 1, new_Bad());
347 * Starts a walk for memory operands if supported by the backend.
349 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
350 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
354 * Performs chordal register allocation for each register class on given irg.
356 * @param bi Backend irg object
357 * @return Structure containing timer for the single phases or NULL if no timing requested.
359 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
361 const be_main_env_t *main_env = bi->main_env;
362 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
363 ir_graph *irg = bi->irg;
364 be_options_t *main_opts = main_env->options;
368 be_chordal_env_t chordal_env;
370 if (main_opts->timing == BE_TIME_ON) {
371 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
372 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
373 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
374 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
375 ra_timer.t_color = lc_timer_register("ra_color", "graph coloring");
376 ra_timer.t_ifg = lc_timer_register("ra_ifg", "interference graph");
377 ra_timer.t_copymin = lc_timer_register("ra_copymin", "copy minimization");
378 ra_timer.t_ssa = lc_timer_register("ra_ssadestr", "ssa destruction");
379 ra_timer.t_verify = lc_timer_register("ra_verify", "graph verification");
380 ra_timer.t_other = lc_timer_register("ra_other", "other time");
382 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
383 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
384 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
385 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
386 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
387 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
388 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
389 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
390 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
391 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
394 #define BE_TIMER_PUSH(timer) \
395 if (main_opts->timing == BE_TIME_ON) { \
396 int res = lc_timer_push(timer); \
397 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
398 assert(res && "Timer already on stack, cannot be pushed twice."); \
399 else if (options.vrfy_option == BE_CH_VRFY_WARN && ! res) \
400 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
401 lc_timer_get_name(timer)); \
403 #define BE_TIMER_POP(timer) \
404 if (main_opts->timing == BE_TIME_ON) { \
405 lc_timer_t *tmp = lc_timer_pop(); \
406 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
407 assert(tmp == timer && "Attempt to pop wrong timer."); \
408 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
409 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
410 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
414 BE_TIMER_PUSH(ra_timer.t_other);
415 BE_TIMER_PUSH(ra_timer.t_prolog);
419 chordal_env.opts = &options;
420 chordal_env.irg = irg;
421 chordal_env.birg = bi;
422 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
423 chordal_env.exec_freq = compute_execfreq(irg, be_loop_weight);
424 chordal_env.lv = be_liveness(irg);
425 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
427 obstack_init(&chordal_env.obst);
429 BE_TIMER_POP(ra_timer.t_prolog);
431 /* Perform the following for each register class. */
432 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
434 copy_opt_t *co = NULL;
436 chordal_env.cls = arch_isa_get_reg_class(isa, j);
437 chordal_env.border_heads = pmap_create();
438 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
440 /* put all ignore registers into the ignore register set. */
441 put_ignore_colors(&chordal_env);
443 BE_TIMER_PUSH(ra_timer.t_live);
444 be_liveness_recompute(chordal_env.lv);
445 BE_TIMER_POP(ra_timer.t_live);
446 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
448 be_pre_spill_prepare_constr(&chordal_env);
449 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-constr-pre", dump_ir_block_graph_sched);
451 BE_TIMER_PUSH(ra_timer.t_spill);
454 switch(options.spill_method) {
455 case BE_CH_SPILL_MORGAN:
456 be_spill_morgan(&chordal_env);
458 case BE_CH_SPILL_BELADY:
459 be_spill_belady(&chordal_env);
462 case BE_CH_SPILL_REMAT:
463 be_spill_remat(&chordal_env);
465 #endif /* WITH_ILP */
467 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
468 be_spill_belady(&chordal_env);
471 BE_TIMER_POP(ra_timer.t_spill);
473 DBG((chordal_env.dbg, LEVEL_1, "spill costs for %+F in regclass %s: %g\n",
475 chordal_env.cls->name,
476 get_irg_spill_cost(&chordal_env))
479 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
480 be_compute_spill_offsets(&chordal_env);
481 check_for_memory_operands(&chordal_env);
482 be_abi_fix_stack_nodes(bi->abi, chordal_env.lv);
484 BE_TIMER_PUSH(ra_timer.t_verify);
486 /* verify schedule and register pressure */
487 if (options.vrfy_option == BE_CH_VRFY_WARN) {
488 be_verify_schedule(irg);
489 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
491 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
492 assert(be_verify_schedule(irg) && "Schedule verification failed");
493 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
494 && "Register pressure verification failed");
496 BE_TIMER_POP(ra_timer.t_verify);
498 if (be_elr_split && ! splitted) {
499 extreme_liverange_splitting(&chordal_env);
504 /* Color the graph. */
505 BE_TIMER_PUSH(ra_timer.t_color);
506 be_ra_chordal_color(&chordal_env);
507 BE_TIMER_POP(ra_timer.t_color);
509 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
511 /* Create the ifg with the selected flavor */
512 BE_TIMER_PUSH(ra_timer.t_ifg);
513 switch (options.ifg_flavor) {
515 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
518 chordal_env.ifg = be_ifg_std_new(&chordal_env);
520 case BE_CH_IFG_CLIQUE:
521 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
523 case BE_CH_IFG_POINTER:
524 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
527 chordal_env.ifg = be_ifg_list_new(&chordal_env);
529 case BE_CH_IFG_CHECK:
530 check_ifg_implementations(&chordal_env);
531 /* Build the interference graph. */
532 chordal_env.ifg = be_ifg_std_new(&chordal_env);
535 BE_TIMER_POP(ra_timer.t_ifg);
537 BE_TIMER_PUSH(ra_timer.t_verify);
538 if (options.vrfy_option != BE_CH_VRFY_OFF)
539 be_ra_chordal_check(&chordal_env);
541 BE_TIMER_POP(ra_timer.t_verify);
543 /* copy minimization */
544 BE_TIMER_PUSH(ra_timer.t_copymin);
545 co_driver(&chordal_env);
546 BE_TIMER_POP(ra_timer.t_copymin);
547 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
549 BE_TIMER_PUSH(ra_timer.t_verify);
551 if (options.vrfy_option != BE_CH_VRFY_OFF)
552 be_ra_chordal_check(&chordal_env);
554 BE_TIMER_POP(ra_timer.t_verify);
555 BE_TIMER_PUSH(ra_timer.t_ssa);
557 /* ssa destruction */
558 be_ssa_destruction(&chordal_env);
560 BE_TIMER_POP(ra_timer.t_ssa);
562 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
564 BE_TIMER_PUSH(ra_timer.t_verify);
565 if (options.vrfy_option != BE_CH_VRFY_OFF) {
566 be_ssa_destruction_check(&chordal_env);
567 be_ra_chordal_check(&chordal_env);
569 BE_TIMER_POP(ra_timer.t_verify);
571 be_ifg_free(chordal_env.ifg);
572 pmap_destroy(chordal_env.border_heads);
573 bitset_free(chordal_env.ignore_colors);
576 BE_TIMER_PUSH(ra_timer.t_epilog);
578 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
580 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
581 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
583 obstack_free(&chordal_env.obst, NULL);
584 be_free_dominance_frontiers(chordal_env.dom_front);
585 be_liveness_free(chordal_env.lv);
586 free_execfreq(chordal_env.exec_freq);
588 BE_TIMER_POP(ra_timer.t_epilog);
589 BE_TIMER_POP(ra_timer.t_other);
594 return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
597 const be_ra_t be_ra_chordal_allocator = {
599 be_ra_chordal_register_options,