2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Driver for the chordal register allocator.
23 * @author Sebastian Hack
39 #include "firm_config.h"
41 #include <libcore/lc_opts.h>
42 #include <libcore/lc_opts_enum.h>
43 #include <libcore/lc_timing.h>
47 #include "irgraph_t.h"
48 #include "irprintf_t.h"
60 #include "iredges_t.h"
62 #include "bechordal_t.h"
64 #include "bejavacoal.h"
67 #include "besched_t.h"
71 #include "beifg_impl.h"
73 #include "bestatevent.h"
80 #include "bespillbelady.h"
81 #include "bespillslots.h"
82 #include "bespilloptions.h"
86 #include "bespillremat.h"
89 #include "bejavacoal.h"
90 #include "becopystat.h"
91 #include "becopyopt.h"
92 #include "bessadestr.h"
96 static be_ra_chordal_opts_t options = {
98 BE_CH_LOWER_PERM_SWAP,
104 typedef struct _post_spill_env_t {
105 be_chordal_env_t cenv;
107 const arch_register_class_t *cls;
108 double pre_spill_cost;
111 static be_ra_timer_t ra_timer = {
125 static const lc_opt_enum_int_items_t lower_perm_items[] = {
126 { "copy", BE_CH_LOWER_PERM_COPY },
127 { "swap", BE_CH_LOWER_PERM_SWAP },
131 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
135 static const lc_opt_enum_int_items_t dump_items[] = {
136 { "none", BE_CH_DUMP_NONE },
137 { "spill", BE_CH_DUMP_SPILL },
138 { "live", BE_CH_DUMP_LIVE },
139 { "color", BE_CH_DUMP_COLOR },
140 { "copymin", BE_CH_DUMP_COPYMIN },
141 { "ssadestr", BE_CH_DUMP_SSADESTR },
142 { "tree", BE_CH_DUMP_TREE_INTV },
143 { "constr", BE_CH_DUMP_CONSTR },
144 { "lower", BE_CH_DUMP_LOWER },
145 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
146 { "appel", BE_CH_DUMP_APPEL },
147 { "all", BE_CH_DUMP_ALL },
151 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
152 { "off", BE_CH_VRFY_OFF },
153 { "warn", BE_CH_VRFY_WARN },
154 { "assert", BE_CH_VRFY_ASSERT },
158 static lc_opt_enum_int_var_t lower_perm_var = {
159 &options.lower_perm_opt, lower_perm_items
162 static lc_opt_enum_int_var_t dump_var = {
163 &options.dump_flags, dump_items
166 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
167 &options.vrfy_option, be_ch_vrfy_items
170 static const lc_opt_table_entry_t be_chordal_options[] = {
171 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options", &lower_perm_var),
172 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
173 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options", &be_ch_vrfy_var),
177 static void dump(unsigned mask, ir_graph *irg,
178 const arch_register_class_t *cls,
180 void (*dump_func)(ir_graph *, const char *))
182 if((options.dump_flags & mask) == mask) {
185 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
186 be_dump(irg, buf, dump_func);
189 be_dump(irg, suffix, dump_func);
194 * Checks for every reload if it's user can perform the load on itself.
196 static void memory_operand_walker(ir_node *irn, void *env) {
197 be_chordal_env_t *cenv = env;
198 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
199 const ir_edge_t *edge, *ne;
203 if (! be_is_Reload(irn))
206 /* only use memory operands, if the reload is only used by 1 node */
207 if(get_irn_n_edges(irn) > 1)
210 spill = be_get_Reload_mem(irn);
211 block = get_nodes_block(irn);
213 foreach_out_edge_safe(irn, edge, ne) {
214 ir_node *src = get_edge_src_irn(edge);
215 int pos = get_edge_src_pos(edge);
217 assert(src && "outedges broken!");
219 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
220 arch_perform_memory_operand(aenv, src, spill, pos);
224 /* kill the Reload */
225 if (get_irn_n_edges(irn) == 0) {
227 set_irn_n(irn, be_pos_Reload_mem, new_Bad());
228 set_irn_n(irn, be_pos_Reload_frame, new_Bad());
233 * Starts a walk for memory operands if supported by the backend.
235 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
236 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
240 * Sorry for doing stats again...
242 typedef struct _node_stat_t {
243 unsigned int n_phis; /**< Phis of the current register class. */
244 unsigned int n_mem_phis; /**< Memory Phis (Phis with spill operands). */
245 unsigned int n_copies; /**< Copies */
246 unsigned int n_perms; /**< Perms */
247 unsigned int n_spills; /**< Spill nodes */
248 unsigned int n_reloads; /**< Reloads */
251 struct node_stat_walker {
253 const arch_env_t *arch_env;
255 const arch_register_class_t *cls;
258 static void node_stat_walker(ir_node *irn, void *data)
260 struct node_stat_walker *env = data;
261 const arch_env_t *aenv = env->arch_env;
263 if (arch_irn_consider_in_reg_alloc(aenv, env->cls, irn)) {
265 /* if the node is a normal phi */
269 else if(arch_irn_classify(aenv, irn) & arch_irn_class_spill)
270 ++env->stat->n_spills;
272 else if(arch_irn_classify(aenv, irn) & arch_irn_class_reload)
273 ++env->stat->n_reloads;
275 else if(arch_irn_classify(aenv, irn) & arch_irn_class_copy)
276 ++env->stat->n_copies;
278 else if(arch_irn_classify(aenv, irn) & arch_irn_class_perm)
279 ++env->stat->n_perms;
282 /* a mem phi is a PhiM with a mem phi operand or a Spill operand */
283 else if(is_Phi(irn) && get_irn_mode(irn) == mode_M) {
286 for(i = get_irn_arity(irn) - 1; i >= 0; --i) {
287 ir_node *op = get_irn_n(irn, i);
289 if((is_Phi(op) && bitset_contains_irn(env->mem_phis, op)) || (arch_irn_classify(aenv, op) & arch_irn_class_spill)) {
290 bitset_add_irn(env->mem_phis, irn);
291 env->stat->n_mem_phis++;
298 static void node_stats(be_irg_t *birg, const arch_register_class_t *cls, node_stat_t *stat)
300 struct node_stat_walker env;
302 memset(stat, 0, sizeof(stat[0]));
303 env.arch_env = birg->main_env->arch_env;
304 env.mem_phis = bitset_irg_malloc(birg->irg);
307 irg_walk_graph(birg->irg, NULL, node_stat_walker, &env);
308 bitset_free(env.mem_phis);
311 static void insn_count_walker(ir_node *irn, void *data)
315 switch(get_irn_opcode(irn)) {
326 static unsigned int count_insns(ir_graph *irg)
329 irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
334 * Initialize all timers.
336 static void be_init_timer(be_options_t *main_opts)
338 if (main_opts->timing == BE_TIME_ON) {
339 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
340 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
341 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
342 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
343 ra_timer.t_spillslots = lc_timer_register("ra_spillslots", "spillslots");
344 ra_timer.t_color = lc_timer_register("ra_color", "graph coloring");
345 ra_timer.t_ifg = lc_timer_register("ra_ifg", "interference graph");
346 ra_timer.t_copymin = lc_timer_register("ra_copymin", "copy minimization");
347 ra_timer.t_ssa = lc_timer_register("ra_ssadestr", "ssa destruction");
348 ra_timer.t_verify = lc_timer_register("ra_verify", "graph verification");
349 ra_timer.t_other = lc_timer_register("ra_other", "other time");
351 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
352 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
353 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
354 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
355 LC_STOP_AND_RESET_TIMER(ra_timer.t_spillslots);
356 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
357 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
358 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
359 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
360 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
361 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
363 global_ra_timer = &ra_timer;
367 #define BE_TIMER_INIT(main_opts) be_init_timer(main_opts)
369 #define BE_TIMER_PUSH(timer) \
370 if (main_opts->timing == BE_TIME_ON) { \
371 if (! lc_timer_push(timer)) { \
372 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
373 assert(!"Timer already on stack, cannot be pushed twice."); \
374 else if (options.vrfy_option == BE_CH_VRFY_WARN) \
375 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
376 lc_timer_get_name(timer)); \
379 #define BE_TIMER_POP(timer) \
380 if (main_opts->timing == BE_TIME_ON) { \
381 lc_timer_t *tmp = lc_timer_pop(); \
382 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
383 assert(tmp == timer && "Attempt to pop wrong timer."); \
384 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
385 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
386 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
391 * Perform things which need to be done per register class before spilling.
393 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
395 be_chordal_env_t *chordal_env = &pse->cenv;
396 be_irg_t *birg = pse->birg;
397 ir_graph *irg = be_get_birg_irg(birg);
398 const be_main_env_t *main_env = birg->main_env;
399 node_stat_t node_stat;
402 chordal_env->cls = cls;
403 chordal_env->border_heads = pmap_create();
404 chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
406 be_assure_liveness(birg);
407 be_liveness_assure_chk(be_get_birg_liveness(birg));
408 stat_ev_ctx_push_str("bechordal_cls", pse->cls->name);
409 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
410 stat_ev_do(pse->pre_spill_cost = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq));
411 stat_ev_dbl("phis_before_spill", node_stat.n_phis);
413 /* put all ignore registers into the ignore register set. */
414 be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
416 be_pre_spill_prepare_constr(chordal_env);
417 dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
419 stat_ev_ctx_pop("bechordal_cls");
423 * Perform things which need to be done per register class after spilling.
425 static void post_spill(post_spill_env_t *pse, int iteration) {
426 be_chordal_env_t *chordal_env = &pse->cenv;
427 be_irg_t *birg = pse->birg;
428 ir_graph *irg = birg->irg;
429 const be_main_env_t *main_env = birg->main_env;
430 be_options_t *main_opts = main_env->options;
431 node_stat_t node_stat;
432 int colors_n = arch_register_class_n_regs(chordal_env->cls);
433 int allocatable_regs = colors_n - be_put_ignore_regs(birg, chordal_env->cls, NULL);
435 /* some special classes contain only ignore regs, no work to be done */
436 if (allocatable_regs > 0) {
438 stat_ev_ctx_push_str("cls", pse->cls->name);
439 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
440 stat_ev_dbl("phis_after_spill", node_stat.n_phis);
441 stat_ev_dbl("mem_phis", node_stat.n_mem_phis);
442 stat_ev_dbl("reloads", node_stat.n_reloads);
443 stat_ev_dbl("spills", node_stat.n_spills);
444 stat_ev_dbl("spillcosts", be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost);
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);
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(birg);
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(birg) && "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 /* Color the graph. */
471 BE_TIMER_PUSH(ra_timer.t_color);
472 be_ra_chordal_color(chordal_env);
473 BE_TIMER_POP(ra_timer.t_color);
475 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
477 /* Create the ifg with the selected flavor */
478 BE_TIMER_PUSH(ra_timer.t_ifg);
479 chordal_env->ifg = be_create_ifg(chordal_env);
480 BE_TIMER_POP(ra_timer.t_ifg);
485 stat_ev_do(be_ifg_stat(birg, chordal_env->ifg, &stat));
486 stat_ev_dbl("ifg_nodes", stat.n_nodes);
487 stat_ev_dbl("ifg_edges", stat.n_edges);
488 stat_ev_dbl("ifg_comps", stat.n_comps);
490 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
491 stat_ev_dbl("perms_before_coal", node_stat.n_perms);
492 stat_ev_dbl("copies_before_coal", node_stat.n_copies);
495 /* copy minimization */
496 BE_TIMER_PUSH(ra_timer.t_copymin);
497 co_driver(chordal_env);
498 BE_TIMER_POP(ra_timer.t_copymin);
500 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "-copymin", dump_ir_block_graph_sched);
502 BE_TIMER_PUSH(ra_timer.t_ssa);
504 /* ssa destruction */
505 stat_ev_ctx_push_str("berachordal_phase", "ssadestr");
506 be_ssa_destruction(chordal_env);
507 stat_ev_ctx_pop("berachordal_phase");
509 BE_TIMER_POP(ra_timer.t_ssa);
511 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
513 BE_TIMER_PUSH(ra_timer.t_verify);
514 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
515 be_ssa_destruction_check(chordal_env);
517 BE_TIMER_POP(ra_timer.t_verify);
519 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
520 stat_ev_dbl("perms_after_coal", node_stat.n_perms);
521 stat_ev_dbl("copies_after_coal", node_stat.n_copies);
522 stat_ev_ctx_pop("cls");
524 /* the ifg exists only if there are allocatable regs */
525 be_ifg_free(chordal_env->ifg);
528 /* free some always allocated data structures */
529 pmap_destroy(chordal_env->border_heads);
530 bitset_free(chordal_env->ignore_colors);
534 * Performs chordal register allocation for each register class on given irg.
536 * @param birg Backend irg object
537 * @return Structure containing timer for the single phases or NULL if no timing requested.
539 static void be_ra_chordal_main(be_irg_t *birg)
541 const be_main_env_t *main_env = birg->main_env;
542 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
543 ir_graph *irg = birg->irg;
544 be_options_t *main_opts = main_env->options;
546 be_chordal_env_t chordal_env;
549 BE_TIMER_INIT(main_opts);
550 BE_TIMER_PUSH(ra_timer.t_other);
551 BE_TIMER_PUSH(ra_timer.t_prolog);
553 be_assure_dom_front(birg);
554 be_assure_liveness(birg);
556 chordal_env.obst = &obst;
557 chordal_env.opts = &options;
558 chordal_env.irg = irg;
559 chordal_env.birg = birg;
560 chordal_env.border_heads = NULL;
561 chordal_env.ifg = NULL;
562 chordal_env.ignore_colors = NULL;
566 BE_TIMER_POP(ra_timer.t_prolog);
568 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;
578 const arch_register_class_t *cls
579 = arch_isa_get_reg_class(isa, j);
581 if(arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
585 memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
587 pre_spill(&pse, cls);
590 /* this is a hack, TODO remove me later */
592 be_do_stat_reg_pressure(birg);
596 BE_TIMER_PUSH(ra_timer.t_spill);
597 be_do_spill(birg, cls);
598 BE_TIMER_POP(ra_timer.t_spill);
600 dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill",
601 dump_ir_block_graph_sched);
606 post_spill_env_t *pse;
608 /* the backend has it's own spiller */
609 m = arch_isa_get_n_reg_class(isa);
611 pse = alloca(m * sizeof(pse[0]));
613 for (j = 0; j < m; ++j) {
614 memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
616 pre_spill(&pse[j], pse[j].cls);
619 BE_TIMER_PUSH(ra_timer.t_spill);
620 arch_code_generator_spill(birg->cg, birg);
621 BE_TIMER_POP(ra_timer.t_spill);
622 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
624 for (j = 0; j < m; ++j) {
625 post_spill(&pse[j], j);
630 be_verify_register_allocation(birg);
632 BE_TIMER_PUSH(ra_timer.t_epilog);
633 lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
634 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
636 obstack_free(&obst, NULL);
637 be_liveness_invalidate(be_get_birg_liveness(birg));
638 BE_TIMER_POP(ra_timer.t_epilog);
640 BE_TIMER_POP(ra_timer.t_other);
642 be_stat_ev("insns_after", count_insns(irg));
647 static be_ra_t be_ra_chordal_allocator = {
651 void be_init_chordal_main(void)
653 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
654 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
655 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
657 lc_opt_add_table(chordal_grp, be_chordal_options);
659 be_register_allocator("chordal", &be_ra_chordal_allocator);
662 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);