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;
91 const arch_register_class_t *cls;
92 double pre_spill_cost;
95 static be_ra_timer_t ra_timer = {
109 static const lc_opt_enum_int_items_t lower_perm_items[] = {
110 { "copy", BE_CH_LOWER_PERM_COPY },
111 { "swap", BE_CH_LOWER_PERM_SWAP },
115 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
119 static const lc_opt_enum_int_items_t dump_items[] = {
120 { "none", BE_CH_DUMP_NONE },
121 { "spill", BE_CH_DUMP_SPILL },
122 { "live", BE_CH_DUMP_LIVE },
123 { "color", BE_CH_DUMP_COLOR },
124 { "copymin", BE_CH_DUMP_COPYMIN },
125 { "ssadestr", BE_CH_DUMP_SSADESTR },
126 { "tree", BE_CH_DUMP_TREE_INTV },
127 { "constr", BE_CH_DUMP_CONSTR },
128 { "lower", BE_CH_DUMP_LOWER },
129 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
130 { "appel", BE_CH_DUMP_APPEL },
131 { "all", BE_CH_DUMP_ALL },
135 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
136 { "off", BE_CH_VRFY_OFF },
137 { "warn", BE_CH_VRFY_WARN },
138 { "assert", BE_CH_VRFY_ASSERT },
142 static lc_opt_enum_int_var_t lower_perm_var = {
143 &options.lower_perm_opt, lower_perm_items
146 static lc_opt_enum_int_var_t dump_var = {
147 &options.dump_flags, dump_items
150 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
151 &options.vrfy_option, be_ch_vrfy_items
154 static const lc_opt_table_entry_t be_chordal_options[] = {
155 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options", &lower_perm_var),
156 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
157 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options", &be_ch_vrfy_var),
158 LC_OPT_ENT_BOOL ("elrsplit", "enable extreme live range splitting", &be_elr_split),
162 static void dump(unsigned mask, ir_graph *irg,
163 const arch_register_class_t *cls,
165 void (*dump_func)(ir_graph *, const char *))
167 if((options.dump_flags & mask) == mask) {
170 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
171 be_dump(irg, buf, dump_func);
174 be_dump(irg, suffix, dump_func);
179 * Checks for every reload if it's user can perform the load on itself.
181 static void memory_operand_walker(ir_node *irn, void *env) {
182 be_chordal_env_t *cenv = env;
183 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
184 const ir_edge_t *edge, *ne;
188 if (! be_is_Reload(irn))
191 /* only use memory operands, if the reload is only used by 1 node */
192 if(get_irn_n_edges(irn) > 1)
195 spill = be_get_Reload_mem(irn);
196 block = get_nodes_block(irn);
198 foreach_out_edge_safe(irn, edge, ne) {
199 ir_node *src = get_edge_src_irn(edge);
200 int pos = get_edge_src_pos(edge);
202 assert(src && "outedges broken!");
204 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
205 DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
206 arch_perform_memory_operand(aenv, src, spill, pos);
210 /* kill the Reload */
211 if (get_irn_n_edges(irn) == 0) {
213 set_irn_n(irn, be_pos_Reload_mem, new_Bad());
214 set_irn_n(irn, be_pos_Reload_frame, new_Bad());
219 * Starts a walk for memory operands if supported by the backend.
221 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
222 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
226 * Sorry for doing stats again...
228 typedef struct _node_stat_t {
229 unsigned int n_phis; /**< Phis of the current register class. */
230 unsigned int n_mem_phis; /**< Memory Phis (Phis with spill operands). */
231 unsigned int n_copies; /**< Copies */
232 unsigned int n_perms; /**< Perms */
233 unsigned int n_spills; /**< Spill nodes */
234 unsigned int n_reloads; /**< Reloads */
237 struct node_stat_walker {
239 const arch_env_t *arch_env;
241 const arch_register_class_t *cls;
244 static void node_stat_walker(ir_node *irn, void *data)
246 struct node_stat_walker *env = data;
247 const arch_env_t *aenv = env->arch_env;
249 if (arch_irn_consider_in_reg_alloc(aenv, env->cls, irn)) {
251 /* if the node is a normal phi */
255 else if(arch_irn_classify(aenv, irn) & arch_irn_class_spill)
256 ++env->stat->n_spills;
258 else if(arch_irn_classify(aenv, irn) & arch_irn_class_reload)
259 ++env->stat->n_reloads;
261 else if(arch_irn_classify(aenv, irn) & arch_irn_class_copy)
262 ++env->stat->n_copies;
264 else if(arch_irn_classify(aenv, irn) & arch_irn_class_perm)
265 ++env->stat->n_perms;
268 /* a mem phi is a PhiM with a mem phi operand or a Spill operand */
269 else if(is_Phi(irn) && get_irn_mode(irn) == mode_M) {
272 for(i = get_irn_arity(irn) - 1; i >= 0; --i) {
273 ir_node *op = get_irn_n(irn, i);
275 if((is_Phi(op) && bitset_contains_irn(env->mem_phis, op)) || (arch_irn_classify(aenv, op) & arch_irn_class_spill)) {
276 bitset_add_irn(env->mem_phis, irn);
277 env->stat->n_mem_phis++;
284 static void node_stats(be_irg_t *birg, const arch_register_class_t *cls, node_stat_t *stat)
286 struct node_stat_walker env;
288 memset(stat, 0, sizeof(stat[0]));
289 env.arch_env = birg->main_env->arch_env;
290 env.mem_phis = bitset_irg_malloc(birg->irg);
293 irg_walk_graph(birg->irg, NULL, node_stat_walker, &env);
294 bitset_free(env.mem_phis);
297 static void insn_count_walker(ir_node *irn, void *data)
301 switch(get_irn_opcode(irn)) {
312 static unsigned int count_insns(ir_graph *irg)
315 irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
321 * Initialize all timers.
323 static void be_init_timer(be_options_t *main_opts)
325 if (main_opts->timing == BE_TIME_ON) {
326 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
327 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
328 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
329 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
330 ra_timer.t_spillslots = lc_timer_register("ra_spillslots", "spillslots");
331 ra_timer.t_color = lc_timer_register("ra_color", "graph coloring");
332 ra_timer.t_ifg = lc_timer_register("ra_ifg", "interference graph");
333 ra_timer.t_copymin = lc_timer_register("ra_copymin", "copy minimization");
334 ra_timer.t_ssa = lc_timer_register("ra_ssadestr", "ssa destruction");
335 ra_timer.t_verify = lc_timer_register("ra_verify", "graph verification");
336 ra_timer.t_other = lc_timer_register("ra_other", "other time");
338 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
339 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
340 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
341 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
342 LC_STOP_AND_RESET_TIMER(ra_timer.t_spillslots);
343 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
344 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
345 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
346 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
347 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
348 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
352 #define BE_TIMER_INIT(main_opts) be_init_timer(main_opts)
354 #define BE_TIMER_PUSH(timer) \
355 if (main_opts->timing == BE_TIME_ON) { \
356 if (! lc_timer_push(timer)) { \
357 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
358 assert(!"Timer already on stack, cannot be pushed twice."); \
359 else if (options.vrfy_option == BE_CH_VRFY_WARN) \
360 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
361 lc_timer_get_name(timer)); \
364 #define BE_TIMER_POP(timer) \
365 if (main_opts->timing == BE_TIME_ON) { \
366 lc_timer_t *tmp = lc_timer_pop(); \
367 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
368 assert(tmp == timer && "Attempt to pop wrong timer."); \
369 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
370 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
371 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
376 #define BE_TIMER_INIT(main_opts)
377 #define BE_TIMER_PUSH(timer)
378 #define BE_TIMER_POP(timer)
380 #endif /* WITH_LIBCORE */
383 * Perform things which need to be done per register class before spilling.
385 static void pre_spill(const arch_isa_t *isa, int cls_idx, post_spill_env_t *pse) {
386 be_chordal_env_t *chordal_env = &pse->cenv;
387 be_irg_t *birg = pse->birg;
388 node_stat_t node_stat;
390 pse->cls = arch_isa_get_reg_class(isa, cls_idx);
391 chordal_env->cls = pse->cls;
392 chordal_env->border_heads = pmap_create();
393 chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
395 #ifdef FIRM_STATISTICS
396 if (be_stat_ev_is_active()) {
397 be_stat_tags[STAT_TAG_CLS] = pse->cls->name;
398 be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
400 /* perform some node statistics. */
401 node_stats(birg, pse->cls, &node_stat);
402 be_stat_ev("phis_before_spill", node_stat.n_phis);
404 #endif /* FIRM_STATISTICS */
406 /* put all ignore registers into the ignore register set. */
407 be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
409 be_pre_spill_prepare_constr(chordal_env);
410 dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
412 #ifdef FIRM_STATISTICS
413 if (be_stat_ev_is_active()) {
414 pse->pre_spill_cost = be_estimate_irg_costs(birg->irg,
415 birg->main_env->arch_env, birg->exec_freq);
417 #endif /* FIRM_STATISTICS */
421 * Perform things which need to be done per register class after spilling.
423 static void post_spill(post_spill_env_t *pse, int iteration) {
424 be_chordal_env_t *chordal_env = &pse->cenv;
425 be_irg_t *birg = pse->birg;
426 ir_graph *irg = birg->irg;
427 const be_main_env_t *main_env = birg->main_env;
428 be_options_t *main_opts = main_env->options;
429 static int splitted = 0;
430 node_stat_t node_stat;
432 #ifdef FIRM_STATISTICS
433 if (be_stat_ev_is_active()) {
434 double spillcosts = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost;
436 be_stat_ev_l("spillcosts", (long) spillcosts);
438 node_stats(birg, pse->cls, &node_stat);
439 be_stat_ev("phis_after_spill", node_stat.n_phis);
440 be_stat_ev("mem_phis", node_stat.n_mem_phis);
441 be_stat_ev("reloads", node_stat.n_reloads);
442 be_stat_ev("spills", node_stat.n_spills);
444 #endif /* FIRM_STATISTICS */
447 If we have a backend provided spiller, post spill is
448 called in a loop after spilling for each register class.
449 But we only need to fix stack nodes once in this case.
451 if (iteration == 0) {
452 check_for_memory_operands(chordal_env);
453 be_abi_fix_stack_nodes(birg->abi, birg->lv);
456 BE_TIMER_PUSH(ra_timer.t_verify);
458 /* verify schedule and register pressure */
459 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
460 be_verify_schedule(irg);
461 be_verify_register_pressure(birg, pse->cls, irg);
463 else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
464 assert(be_verify_schedule(irg) && "Schedule verification failed");
465 assert(be_verify_register_pressure(birg, pse->cls, irg)
466 && "Register pressure verification failed");
468 BE_TIMER_POP(ra_timer.t_verify);
470 if (be_elr_split && ! splitted) {
471 extreme_liverange_splitting(chordal_env);
475 /* Color the graph. */
476 BE_TIMER_PUSH(ra_timer.t_color);
477 be_ra_chordal_color(chordal_env);
478 BE_TIMER_POP(ra_timer.t_color);
480 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
482 /* Create the ifg with the selected flavor */
483 BE_TIMER_PUSH(ra_timer.t_ifg);
484 chordal_env->ifg = be_create_ifg(chordal_env);
485 BE_TIMER_POP(ra_timer.t_ifg);
487 #ifdef FIRM_STATISTICS
488 if (be_stat_ev_is_active()) {
491 be_ifg_stat(birg, chordal_env->ifg, &stat);
492 be_stat_ev("ifg_nodes", stat.n_nodes);
493 be_stat_ev("ifg_edges", stat.n_edges);
494 be_stat_ev("ifg_comps", stat.n_comps);
496 node_stats(birg, pse->cls, &node_stat);
497 be_stat_ev("perms_before_coal", node_stat.n_perms);
498 be_stat_ev("copies_before_coal", node_stat.n_copies);
500 #endif /* FIRM_STATISTICS */
502 /* copy minimization */
503 BE_TIMER_PUSH(ra_timer.t_copymin);
504 co_driver(chordal_env);
505 BE_TIMER_POP(ra_timer.t_copymin);
507 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "-copymin", dump_ir_block_graph_sched);
509 BE_TIMER_PUSH(ra_timer.t_ssa);
511 /* ssa destruction */
512 be_ssa_destruction(chordal_env);
514 BE_TIMER_POP(ra_timer.t_ssa);
516 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
518 BE_TIMER_PUSH(ra_timer.t_verify);
519 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
520 be_ssa_destruction_check(chordal_env);
522 BE_TIMER_POP(ra_timer.t_verify);
524 /* free some data structures */
525 be_ifg_free(chordal_env->ifg);
526 pmap_destroy(chordal_env->border_heads);
527 bitset_free(chordal_env->ignore_colors);
529 #ifdef FIRM_STATISTICS
530 if (be_stat_ev_is_active()) {
531 node_stats(birg, pse->cls, &node_stat);
532 be_stat_ev("perms_after_coal", node_stat.n_perms);
533 be_stat_ev("copies_after_coal", node_stat.n_copies);
536 #endif /* FIRM_STATISTICS */
540 * Performs chordal register allocation for each register class on given irg.
542 * @param birg Backend irg object
543 * @return Structure containing timer for the single phases or NULL if no timing requested.
545 static void be_ra_chordal_main(be_irg_t *birg)
547 const be_main_env_t *main_env = birg->main_env;
548 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
549 ir_graph *irg = birg->irg;
550 be_options_t *main_opts = main_env->options;
552 be_chordal_env_t chordal_env;
554 BE_TIMER_INIT(main_opts);
555 BE_TIMER_PUSH(ra_timer.t_other);
556 BE_TIMER_PUSH(ra_timer.t_prolog);
558 be_assure_dom_front(birg);
559 be_assure_liveness(birg);
561 chordal_env.opts = &options;
562 chordal_env.irg = irg;
563 chordal_env.birg = birg;
564 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
566 obstack_init(&chordal_env.obst);
568 BE_TIMER_POP(ra_timer.t_prolog);
570 be_stat_ev("insns_before", count_insns(irg));
572 if (! arch_code_generator_has_spiller(birg->cg)) {
573 /* use one of the generic spiller */
575 /* Perform the following for each register class. */
576 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
577 post_spill_env_t pse;
579 memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
581 pre_spill(isa, j, &pse);
583 BE_TIMER_PUSH(ra_timer.t_spill);
584 be_do_spill(birg, pse.cls);
585 BE_TIMER_POP(ra_timer.t_spill);
587 dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill", dump_ir_block_graph_sched);
593 post_spill_env_t *pse;
595 /* the backend has it's own spiller */
596 m = arch_isa_get_n_reg_class(isa);
598 pse = alloca(m * sizeof(pse[0]));
600 for (j = 0; j < m; ++j) {
601 memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
603 pre_spill(isa, j, &pse[j]);
606 BE_TIMER_PUSH(ra_timer.t_spill);
607 arch_code_generator_spill(birg->cg, birg);
608 BE_TIMER_POP(ra_timer.t_spill);
609 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
611 for (j = 0; j < m; ++j) {
612 post_spill(&pse[j], j);
616 BE_TIMER_PUSH(ra_timer.t_spillslots);
618 be_coalesce_spillslots(&chordal_env);
619 dump(BE_CH_DUMP_SPILLSLOTS, irg, NULL, "-spillslots", dump_ir_block_graph_sched);
621 BE_TIMER_POP(ra_timer.t_spillslots);
623 BE_TIMER_PUSH(ra_timer.t_verify);
624 /* verify spillslots */
625 if (options.vrfy_option == BE_CH_VRFY_WARN) {
626 be_verify_spillslots(main_env->arch_env, irg);
628 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
629 assert(be_verify_spillslots(main_env->arch_env, irg) && "Spillslot verification failed");
631 BE_TIMER_POP(ra_timer.t_verify);
633 BE_TIMER_PUSH(ra_timer.t_epilog);
634 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
636 lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
637 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
639 obstack_free(&chordal_env.obst, NULL);
640 be_invalidate_liveness(birg);
641 BE_TIMER_POP(ra_timer.t_epilog);
643 BE_TIMER_POP(ra_timer.t_other);
645 be_stat_ev("insns_after", count_insns(irg));
650 static be_ra_t be_ra_chordal_allocator = {
654 void be_init_chordal(void)
656 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
657 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
658 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
660 lc_opt_add_table(chordal_grp, be_chordal_options);
662 be_register_allocator("chordal", &be_ra_chordal_allocator);
665 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal);