2 * @file bechordal_main.c
4 * @author Sebastian Hack
7 * Copyright (C) 2005-2006 Universitaet Karlsruhe
8 * Released under the GPL
10 * Driver for the chordal register allocator.
23 #include "firm_config.h"
26 #include <libcore/lc_opts.h>
27 #include <libcore/lc_opts_enum.h>
28 #include <libcore/lc_timing.h>
29 #endif /* WITH_LIBCORE */
33 #include "irgraph_t.h"
34 #include "irprintf_t.h"
47 #include "bechordal_t.h"
49 #include "bejavacoal.h"
52 #include "besched_t.h"
56 #include "beifg_impl.h"
58 #include "bestatevent.h"
62 #include "bespillbelady.h"
63 #include "bespillmorgan.h"
64 #include "bespillslots.h"
65 #include "bespilloptions.h"
69 #include "bespillremat.h"
72 #include "bejavacoal.h"
73 #include "becopystat.h"
74 #include "becopyopt.h"
75 #include "bessadestr.h"
79 static be_ra_chordal_opts_t options = {
81 BE_CH_LOWER_PERM_SWAP,
85 /** Enable extreme live range splitting. */
86 static int be_elr_split = 0;
88 typedef struct _post_spill_env_t {
89 be_chordal_env_t cenv;
90 double pre_spill_cost;
93 static be_ra_timer_t ra_timer = {
107 static const lc_opt_enum_int_items_t lower_perm_items[] = {
108 { "copy", BE_CH_LOWER_PERM_COPY },
109 { "swap", BE_CH_LOWER_PERM_SWAP },
113 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
117 static const lc_opt_enum_int_items_t dump_items[] = {
118 { "spill", BE_CH_DUMP_SPILL },
119 { "live", BE_CH_DUMP_LIVE },
120 { "color", BE_CH_DUMP_COLOR },
121 { "copymin", BE_CH_DUMP_COPYMIN },
122 { "ssadestr", BE_CH_DUMP_SSADESTR },
123 { "tree", BE_CH_DUMP_TREE_INTV },
124 { "constr", BE_CH_DUMP_CONSTR },
125 { "lower", BE_CH_DUMP_LOWER },
126 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
127 { "appel", BE_CH_DUMP_APPEL },
128 { "all", BE_CH_DUMP_ALL },
132 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
133 { "off", BE_CH_VRFY_OFF },
134 { "warn", BE_CH_VRFY_WARN },
135 { "assert", BE_CH_VRFY_ASSERT },
139 static lc_opt_enum_int_var_t lower_perm_var = {
140 &options.lower_perm_opt, lower_perm_items
143 static lc_opt_enum_int_var_t dump_var = {
144 &options.dump_flags, dump_items
147 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
148 &options.vrfy_option, be_ch_vrfy_items
151 static const lc_opt_table_entry_t be_chordal_options[] = {
152 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options", &lower_perm_var),
153 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
154 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options", &be_ch_vrfy_var),
155 LC_OPT_ENT_BOOL ("elrsplit", "enable extreme live range splitting", &be_elr_split),
159 static void dump(unsigned mask, ir_graph *irg,
160 const arch_register_class_t *cls,
162 void (*dump_func)(ir_graph *, const char *))
164 if((options.dump_flags & mask) == mask) {
167 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
168 be_dump(irg, buf, dump_func);
171 be_dump(irg, suffix, dump_func);
176 * Checks for every reload if it's user can perform the load on itself.
178 static void memory_operand_walker(ir_node *irn, void *env) {
179 be_chordal_env_t *cenv = env;
180 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
181 const ir_edge_t *edge, *ne;
185 if (! be_is_Reload(irn))
188 /* only use memory operands, if the reload is only used by 1 node */
189 if(get_irn_n_edges(irn) > 1)
192 spill = be_get_Reload_mem(irn);
193 block = get_nodes_block(irn);
195 foreach_out_edge_safe(irn, edge, ne) {
196 ir_node *src = get_edge_src_irn(edge);
197 int pos = get_edge_src_pos(edge);
199 assert(src && "outedges broken!");
201 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
202 DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
203 arch_perform_memory_operand(aenv, src, spill, pos);
207 /* kill the Reload */
208 if (get_irn_n_edges(irn) == 0) {
210 set_irn_n(irn, be_pos_Reload_mem, new_Bad());
215 * Starts a walk for memory operands if supported by the backend.
217 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
218 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
222 * Sorry for doing stats again...
224 typedef struct _node_stat_t {
225 unsigned int n_phis; /**< Phis of the current register class. */
226 unsigned int n_mem_phis; /**< Memory Phis (Phis with spill operands). */
227 unsigned int n_copies; /**< Copies */
228 unsigned int n_perms; /**< Perms */
229 unsigned int n_spills; /**< Spill nodes */
230 unsigned int n_reloads; /**< Reloads */
233 struct node_stat_walker {
235 const be_chordal_env_t *cenv;
239 static void node_stat_walker(ir_node *irn, void *data)
241 struct node_stat_walker *env = data;
242 const arch_env_t *aenv = env->cenv->birg->main_env->arch_env;
244 if(arch_irn_consider_in_reg_alloc(aenv, env->cenv->cls, irn)) {
246 /* if the node is a normal phi */
250 else if(arch_irn_classify(aenv, irn) & arch_irn_class_spill)
251 ++env->stat->n_spills;
253 else if(arch_irn_classify(aenv, irn) & arch_irn_class_reload)
254 ++env->stat->n_reloads;
256 else if(arch_irn_classify(aenv, irn) & arch_irn_class_copy)
257 ++env->stat->n_copies;
259 else if(arch_irn_classify(aenv, irn) & arch_irn_class_perm)
260 ++env->stat->n_perms;
263 /* a mem phi is a PhiM with a mem phi operand or a Spill operand */
264 else if(is_Phi(irn) && get_irn_mode(irn) == mode_M) {
267 for(i = get_irn_arity(irn) - 1; i >= 0; --i) {
268 ir_node *op = get_irn_n(irn, i);
270 if((is_Phi(op) && bitset_contains_irn(env->mem_phis, op)) || (arch_irn_classify(aenv, op) & arch_irn_class_spill)) {
271 bitset_add_irn(env->mem_phis, irn);
272 env->stat->n_mem_phis++;
279 static void node_stats(const be_chordal_env_t *cenv, node_stat_t *stat)
281 struct node_stat_walker env;
283 memset(stat, 0, sizeof(stat[0]));
285 env.mem_phis = bitset_irg_malloc(cenv->irg);
287 irg_walk_graph(cenv->irg, NULL, node_stat_walker, &env);
288 bitset_free(env.mem_phis);
291 static void insn_count_walker(ir_node *irn, void *data)
295 switch(get_irn_opcode(irn)) {
306 static unsigned int count_insns(ir_graph *irg)
309 irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
315 * Initialize all timers.
317 static void be_init_timer(be_options_t *main_opts)
319 if (main_opts->timing == BE_TIME_ON) {
320 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
321 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
322 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
323 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
324 ra_timer.t_spillslots = lc_timer_register("ra_spillslots", "spillslots");
325 ra_timer.t_color = lc_timer_register("ra_color", "graph coloring");
326 ra_timer.t_ifg = lc_timer_register("ra_ifg", "interference graph");
327 ra_timer.t_copymin = lc_timer_register("ra_copymin", "copy minimization");
328 ra_timer.t_ssa = lc_timer_register("ra_ssadestr", "ssa destruction");
329 ra_timer.t_verify = lc_timer_register("ra_verify", "graph verification");
330 ra_timer.t_other = lc_timer_register("ra_other", "other time");
332 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
333 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
334 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
335 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
336 LC_STOP_AND_RESET_TIMER(ra_timer.t_spillslots);
337 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
338 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
339 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
340 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
341 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
342 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
346 #define BE_TIMER_INIT(main_opts) be_init_timer(main_opts)
348 #define BE_TIMER_PUSH(timer) \
349 if (main_opts->timing == BE_TIME_ON) { \
350 if (! lc_timer_push(timer)) { \
351 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
352 assert(!"Timer already on stack, cannot be pushed twice."); \
353 else if (options.vrfy_option == BE_CH_VRFY_WARN) \
354 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
355 lc_timer_get_name(timer)); \
358 #define BE_TIMER_POP(timer) \
359 if (main_opts->timing == BE_TIME_ON) { \
360 lc_timer_t *tmp = lc_timer_pop(); \
361 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
362 assert(tmp == timer && "Attempt to pop wrong timer."); \
363 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
364 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
365 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
370 #define BE_TIMER_INIT(main_opts)
371 #define BE_TIMER_PUSH(timer)
372 #define BE_TIMER_POP(timer)
374 #endif /* WITH_LIBCORE */
377 * Perform things which need to be done per register class before spilling.
379 static void pre_spill(const arch_isa_t *isa, int cls_idx, post_spill_env_t *pse) {
380 be_chordal_env_t *chordal_env = &pse->cenv;
381 node_stat_t node_stat;
383 chordal_env->cls = arch_isa_get_reg_class(isa, cls_idx);
384 chordal_env->border_heads = pmap_create();
385 chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
387 #ifdef FIRM_STATISTICS
388 if (be_stat_ev_is_active()) {
389 be_stat_tags[STAT_TAG_CLS] = chordal_env->cls->name;
390 be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
392 /* perform some node statistics. */
393 node_stats(chordal_env, &node_stat);
394 be_stat_ev("phis_before_spill", node_stat.n_phis);
396 #endif /* FIRM_STATISTICS */
398 /* put all ignore registers into the ignore register set. */
399 be_put_ignore_regs(chordal_env->birg, chordal_env->cls, chordal_env->ignore_colors);
401 be_pre_spill_prepare_constr(chordal_env);
402 dump(BE_CH_DUMP_CONSTR, chordal_env->irg, chordal_env->cls, "-constr-pre", dump_ir_block_graph_sched);
404 #ifdef FIRM_STATISTICS
405 if (be_stat_ev_is_active()) {
406 pse->pre_spill_cost = be_estimate_irg_costs(chordal_env->irg,
407 chordal_env->birg->main_env->arch_env, chordal_env->birg->exec_freq);
409 #endif /* FIRM_STATISTICS */
413 * Perform things which need to be done per register class after spilling.
415 static void post_spill(post_spill_env_t *pse) {
416 be_chordal_env_t *chordal_env = &pse->cenv;
417 ir_graph *irg = chordal_env->irg;
418 be_irg_t *birg = chordal_env->birg;
419 const be_main_env_t *main_env = birg->main_env;
420 be_options_t *main_opts = main_env->options;
421 static int splitted = 0;
422 node_stat_t node_stat;
424 #ifdef FIRM_STATISTICS
425 if (be_stat_ev_is_active()) {
426 double spillcosts = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost;
428 be_stat_ev_l("spillcosts", (long) spillcosts);
430 node_stats(chordal_env, &node_stat);
431 be_stat_ev("phis_after_spill", node_stat.n_phis);
432 be_stat_ev("mem_phis", node_stat.n_mem_phis);
433 be_stat_ev("reloads", node_stat.n_reloads);
434 be_stat_ev("spills", node_stat.n_spills);
436 #endif /* FIRM_STATISTICS */
438 check_for_memory_operands(chordal_env);
440 be_abi_fix_stack_nodes(birg->abi, birg->lv);
442 BE_TIMER_PUSH(ra_timer.t_verify);
444 /* verify schedule and register pressure */
445 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
446 be_verify_schedule(irg);
447 be_verify_register_pressure(chordal_env->birg, chordal_env->cls, irg);
449 else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
450 assert(be_verify_schedule(irg) && "Schedule verification failed");
451 assert(be_verify_register_pressure(chordal_env->birg, chordal_env->cls, irg)
452 && "Register pressure verification failed");
454 BE_TIMER_POP(ra_timer.t_verify);
456 if (be_elr_split && ! splitted) {
457 extreme_liverange_splitting(chordal_env);
461 /* Color the graph. */
462 BE_TIMER_PUSH(ra_timer.t_color);
463 be_ra_chordal_color(chordal_env);
464 BE_TIMER_POP(ra_timer.t_color);
466 dump(BE_CH_DUMP_CONSTR, irg, chordal_env->cls, "-color", dump_ir_block_graph_sched);
468 /* Create the ifg with the selected flavor */
469 BE_TIMER_PUSH(ra_timer.t_ifg);
470 chordal_env->ifg = be_create_ifg(chordal_env);
471 BE_TIMER_POP(ra_timer.t_ifg);
473 #ifdef FIRM_STATISTICS
474 if (be_stat_ev_is_active()) {
476 be_ifg_stat(chordal_env, &stat);
477 be_stat_ev("ifg_nodes", stat.n_nodes);
478 be_stat_ev("ifg_edges", stat.n_edges);
479 be_stat_ev("ifg_comps", stat.n_comps);
481 node_stats(chordal_env, &node_stat);
482 be_stat_ev("perms_before_coal", node_stat.n_perms);
483 be_stat_ev("copies_before_coal", node_stat.n_copies);
485 #endif /* FIRM_STATISTICS */
487 /* copy minimization */
488 BE_TIMER_PUSH(ra_timer.t_copymin);
489 co_driver(chordal_env);
490 BE_TIMER_POP(ra_timer.t_copymin);
492 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env->cls, "-copymin", dump_ir_block_graph_sched);
494 BE_TIMER_PUSH(ra_timer.t_ssa);
496 /* ssa destruction */
497 be_ssa_destruction(chordal_env);
499 BE_TIMER_POP(ra_timer.t_ssa);
501 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env->cls, "-ssadestr", dump_ir_block_graph_sched);
503 BE_TIMER_PUSH(ra_timer.t_verify);
504 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
505 be_ssa_destruction_check(chordal_env);
507 BE_TIMER_POP(ra_timer.t_verify);
509 be_ifg_free(chordal_env->ifg);
510 pmap_destroy(chordal_env->border_heads);
511 bitset_free(chordal_env->ignore_colors);
513 #ifdef FIRM_STATISTICS
514 if (be_stat_ev_is_active()) {
515 node_stats(chordal_env, &node_stat);
516 be_stat_ev("perms_after_coal", node_stat.n_perms);
517 be_stat_ev("copies_after_coal", node_stat.n_copies);
520 #endif /* FIRM_STATISTICS */
524 * Performs chordal register allocation for each register class on given irg.
526 * @param birg Backend irg object
527 * @return Structure containing timer for the single phases or NULL if no timing requested.
529 static void be_ra_chordal_main(be_irg_t *birg)
531 const be_main_env_t *main_env = birg->main_env;
532 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
533 ir_graph *irg = birg->irg;
534 be_options_t *main_opts = main_env->options;
536 be_chordal_env_t chordal_env;
538 BE_TIMER_INIT(main_opts);
539 BE_TIMER_PUSH(ra_timer.t_other);
540 BE_TIMER_PUSH(ra_timer.t_prolog);
542 be_assure_dom_front(birg);
543 be_assure_liveness(birg);
545 chordal_env.opts = &options;
546 chordal_env.irg = irg;
547 chordal_env.birg = birg;
548 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
550 obstack_init(&chordal_env.obst);
552 BE_TIMER_POP(ra_timer.t_prolog);
554 be_stat_ev("insns_before", count_insns(irg));
556 if (! arch_code_generator_has_spiller(birg->cg)) {
557 /* use one of the generic spiller */
559 /* Perform the following for each register class. */
560 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
561 post_spill_env_t pse;
563 memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
564 pre_spill(isa, j, &pse);
566 BE_TIMER_PUSH(ra_timer.t_spill);
567 be_do_spill(&pse.cenv);
568 BE_TIMER_POP(ra_timer.t_spill);
570 dump(BE_CH_DUMP_SPILL, irg, pse.cenv.cls, "-spill", dump_ir_block_graph_sched);
576 post_spill_env_t *pse;
578 /* the backend has it's own spiller */
579 m = arch_isa_get_n_reg_class(isa);
581 pse = alloca(m * sizeof(pse[0]));
583 for (j = 0; j < m; ++j) {
584 memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
585 pre_spill(isa, j, &pse[j]);
588 BE_TIMER_PUSH(ra_timer.t_spill);
589 arch_code_generator_spill(birg->cg, &chordal_env);
590 BE_TIMER_POP(ra_timer.t_spill);
591 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
593 for (j = 0; j < m; ++j) {
598 BE_TIMER_PUSH(ra_timer.t_spillslots);
600 be_coalesce_spillslots(&chordal_env);
601 dump(BE_CH_DUMP_SPILLSLOTS, irg, NULL, "-spillslots", dump_ir_block_graph_sched);
603 BE_TIMER_POP(ra_timer.t_spillslots);
605 BE_TIMER_PUSH(ra_timer.t_verify);
606 /* verify spillslots */
607 if (options.vrfy_option == BE_CH_VRFY_WARN) {
608 be_verify_spillslots(main_env->arch_env, irg);
610 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
611 assert(be_verify_spillslots(main_env->arch_env, irg) && "Spillslot verification failed");
613 BE_TIMER_POP(ra_timer.t_verify);
615 BE_TIMER_PUSH(ra_timer.t_epilog);
616 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
618 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
619 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
621 obstack_free(&chordal_env.obst, NULL);
622 BE_TIMER_POP(ra_timer.t_epilog);
624 BE_TIMER_POP(ra_timer.t_other);
626 be_stat_ev("insns_after", count_insns(irg));
631 static be_ra_t be_ra_chordal_allocator = {
635 void be_init_chordal(void)
637 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
638 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
639 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
641 lc_opt_add_table(chordal_grp, be_chordal_options);
643 be_register_allocator("chordal", &be_ra_chordal_allocator);
646 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal);