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"
61 #include "bechordal_t.h"
63 #include "bejavacoal.h"
66 #include "besched_t.h"
70 #include "beifg_impl.h"
72 #include "bestatevent.h"
79 #include "bespillbelady.h"
80 #include "bespillmorgan.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,
102 typedef struct _post_spill_env_t {
103 be_chordal_env_t cenv;
105 const arch_register_class_t *cls;
106 double pre_spill_cost;
109 static be_ra_timer_t ra_timer = {
123 static const lc_opt_enum_int_items_t lower_perm_items[] = {
124 { "copy", BE_CH_LOWER_PERM_COPY },
125 { "swap", BE_CH_LOWER_PERM_SWAP },
129 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
133 static const lc_opt_enum_int_items_t dump_items[] = {
134 { "none", BE_CH_DUMP_NONE },
135 { "spill", BE_CH_DUMP_SPILL },
136 { "live", BE_CH_DUMP_LIVE },
137 { "color", BE_CH_DUMP_COLOR },
138 { "copymin", BE_CH_DUMP_COPYMIN },
139 { "ssadestr", BE_CH_DUMP_SSADESTR },
140 { "tree", BE_CH_DUMP_TREE_INTV },
141 { "constr", BE_CH_DUMP_CONSTR },
142 { "lower", BE_CH_DUMP_LOWER },
143 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
144 { "appel", BE_CH_DUMP_APPEL },
145 { "all", BE_CH_DUMP_ALL },
149 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
150 { "off", BE_CH_VRFY_OFF },
151 { "warn", BE_CH_VRFY_WARN },
152 { "assert", BE_CH_VRFY_ASSERT },
156 static lc_opt_enum_int_var_t lower_perm_var = {
157 &options.lower_perm_opt, lower_perm_items
160 static lc_opt_enum_int_var_t dump_var = {
161 &options.dump_flags, dump_items
164 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
165 &options.vrfy_option, be_ch_vrfy_items
168 static const lc_opt_table_entry_t be_chordal_options[] = {
169 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options", &lower_perm_var),
170 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
171 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options", &be_ch_vrfy_var),
175 static void dump(unsigned mask, ir_graph *irg,
176 const arch_register_class_t *cls,
178 void (*dump_func)(ir_graph *, const char *))
180 if((options.dump_flags & mask) == mask) {
183 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
184 be_dump(irg, buf, dump_func);
187 be_dump(irg, suffix, dump_func);
192 * Checks for every reload if it's user can perform the load on itself.
194 static void memory_operand_walker(ir_node *irn, void *env) {
195 be_chordal_env_t *cenv = env;
196 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
197 const ir_edge_t *edge, *ne;
201 if (! be_is_Reload(irn))
204 /* only use memory operands, if the reload is only used by 1 node */
205 if(get_irn_n_edges(irn) > 1)
208 spill = be_get_Reload_mem(irn);
209 block = get_nodes_block(irn);
211 foreach_out_edge_safe(irn, edge, ne) {
212 ir_node *src = get_edge_src_irn(edge);
213 int pos = get_edge_src_pos(edge);
215 assert(src && "outedges broken!");
217 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
218 arch_perform_memory_operand(aenv, src, spill, pos);
222 /* kill the Reload */
223 if (get_irn_n_edges(irn) == 0) {
225 set_irn_n(irn, be_pos_Reload_mem, new_Bad());
226 set_irn_n(irn, be_pos_Reload_frame, new_Bad());
231 * Starts a walk for memory operands if supported by the backend.
233 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
234 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
238 * Sorry for doing stats again...
240 typedef struct _node_stat_t {
241 unsigned int n_phis; /**< Phis of the current register class. */
242 unsigned int n_mem_phis; /**< Memory Phis (Phis with spill operands). */
243 unsigned int n_copies; /**< Copies */
244 unsigned int n_perms; /**< Perms */
245 unsigned int n_spills; /**< Spill nodes */
246 unsigned int n_reloads; /**< Reloads */
249 struct node_stat_walker {
251 const arch_env_t *arch_env;
253 const arch_register_class_t *cls;
256 static void node_stat_walker(ir_node *irn, void *data)
258 struct node_stat_walker *env = data;
259 const arch_env_t *aenv = env->arch_env;
261 if (arch_irn_consider_in_reg_alloc(aenv, env->cls, irn)) {
263 /* if the node is a normal phi */
267 else if(arch_irn_classify(aenv, irn) & arch_irn_class_spill)
268 ++env->stat->n_spills;
270 else if(arch_irn_classify(aenv, irn) & arch_irn_class_reload)
271 ++env->stat->n_reloads;
273 else if(arch_irn_classify(aenv, irn) & arch_irn_class_copy)
274 ++env->stat->n_copies;
276 else if(arch_irn_classify(aenv, irn) & arch_irn_class_perm)
277 ++env->stat->n_perms;
280 /* a mem phi is a PhiM with a mem phi operand or a Spill operand */
281 else if(is_Phi(irn) && get_irn_mode(irn) == mode_M) {
284 for(i = get_irn_arity(irn) - 1; i >= 0; --i) {
285 ir_node *op = get_irn_n(irn, i);
287 if((is_Phi(op) && bitset_contains_irn(env->mem_phis, op)) || (arch_irn_classify(aenv, op) & arch_irn_class_spill)) {
288 bitset_add_irn(env->mem_phis, irn);
289 env->stat->n_mem_phis++;
296 static void node_stats(be_irg_t *birg, const arch_register_class_t *cls, node_stat_t *stat)
298 struct node_stat_walker env;
300 memset(stat, 0, sizeof(stat[0]));
301 env.arch_env = birg->main_env->arch_env;
302 env.mem_phis = bitset_irg_malloc(birg->irg);
305 irg_walk_graph(birg->irg, NULL, node_stat_walker, &env);
306 bitset_free(env.mem_phis);
309 static void insn_count_walker(ir_node *irn, void *data)
313 switch(get_irn_opcode(irn)) {
324 static unsigned int count_insns(ir_graph *irg)
327 irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
332 * Initialize all timers.
334 static void be_init_timer(be_options_t *main_opts)
336 if (main_opts->timing == BE_TIME_ON) {
337 ra_timer.t_prolog = lc_timer_register("ra_prolog", "regalloc prolog");
338 ra_timer.t_epilog = lc_timer_register("ra_epilog", "regalloc epilog");
339 ra_timer.t_live = lc_timer_register("ra_liveness", "be liveness");
340 ra_timer.t_spill = lc_timer_register("ra_spill", "spiller");
341 ra_timer.t_spillslots = lc_timer_register("ra_spillslots", "spillslots");
342 ra_timer.t_color = lc_timer_register("ra_color", "graph coloring");
343 ra_timer.t_ifg = lc_timer_register("ra_ifg", "interference graph");
344 ra_timer.t_copymin = lc_timer_register("ra_copymin", "copy minimization");
345 ra_timer.t_ssa = lc_timer_register("ra_ssadestr", "ssa destruction");
346 ra_timer.t_verify = lc_timer_register("ra_verify", "graph verification");
347 ra_timer.t_other = lc_timer_register("ra_other", "other time");
349 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
350 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
351 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
352 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
353 LC_STOP_AND_RESET_TIMER(ra_timer.t_spillslots);
354 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
355 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
356 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
357 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
358 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
359 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
361 global_ra_timer = &ra_timer;
365 #define BE_TIMER_INIT(main_opts) be_init_timer(main_opts)
367 #define BE_TIMER_PUSH(timer) \
368 if (main_opts->timing == BE_TIME_ON) { \
369 if (! lc_timer_push(timer)) { \
370 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
371 assert(!"Timer already on stack, cannot be pushed twice."); \
372 else if (options.vrfy_option == BE_CH_VRFY_WARN) \
373 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
374 lc_timer_get_name(timer)); \
377 #define BE_TIMER_POP(timer) \
378 if (main_opts->timing == BE_TIME_ON) { \
379 lc_timer_t *tmp = lc_timer_pop(); \
380 if (options.vrfy_option == BE_CH_VRFY_ASSERT) \
381 assert(tmp == timer && "Attempt to pop wrong timer."); \
382 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer) \
383 fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
384 lc_timer_get_name(tmp), lc_timer_get_name(timer)); \
389 * Perform things which need to be done per register class before spilling.
391 static void pre_spill(const arch_isa_t *isa, int cls_idx, post_spill_env_t *pse) {
392 be_chordal_env_t *chordal_env = &pse->cenv;
393 be_irg_t *birg = pse->birg;
394 node_stat_t node_stat;
396 pse->cls = arch_isa_get_reg_class(isa, cls_idx);
397 chordal_env->cls = pse->cls;
398 chordal_env->border_heads = pmap_create();
399 chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
401 #ifdef FIRM_STATISTICS
402 if (be_stat_ev_is_active()) {
403 be_stat_tags[STAT_TAG_CLS] = pse->cls->name;
404 be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
406 /* perform some node statistics. */
407 node_stats(birg, pse->cls, &node_stat);
408 be_stat_ev("phis_before_spill", node_stat.n_phis);
410 #endif /* FIRM_STATISTICS */
412 /* put all ignore registers into the ignore register set. */
413 be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
415 be_pre_spill_prepare_constr(chordal_env);
416 dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
418 #ifdef FIRM_STATISTICS
419 if (be_stat_ev_is_active()) {
420 pse->pre_spill_cost = be_estimate_irg_costs(birg->irg,
421 birg->main_env->arch_env, birg->exec_freq);
424 #endif /* FIRM_STATISTICS */
428 * Perform things which need to be done per register class after spilling.
430 static void post_spill(post_spill_env_t *pse, int iteration) {
431 be_chordal_env_t *chordal_env = &pse->cenv;
432 be_irg_t *birg = pse->birg;
433 ir_graph *irg = birg->irg;
434 const be_main_env_t *main_env = birg->main_env;
435 be_options_t *main_opts = main_env->options;
436 node_stat_t node_stat;
437 int colors_n = arch_register_class_n_regs(chordal_env->cls);
438 int allocatable_regs = colors_n - be_put_ignore_regs(birg, chordal_env->cls, NULL);
440 /* some special classes contain only ignore regs, no work to be done */
441 if (allocatable_regs > 0) {
443 #ifdef FIRM_STATISTICS
444 if (be_stat_ev_is_active()) {
445 double spillcosts = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost;
447 be_stat_tags[STAT_TAG_CLS] = pse->cls->name;
448 be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
450 be_stat_ev_l("spillcosts", (long) spillcosts);
452 node_stats(birg, pse->cls, &node_stat);
453 be_stat_ev("phis_after_spill", node_stat.n_phis);
454 be_stat_ev("mem_phis", node_stat.n_mem_phis);
455 be_stat_ev("reloads", node_stat.n_reloads);
456 be_stat_ev("spills", node_stat.n_spills);
458 #endif /* FIRM_STATISTICS */
461 If we have a backend provided spiller, post spill is
462 called in a loop after spilling for each register class.
463 But we only need to fix stack nodes once in this case.
465 if (iteration == 0) {
466 check_for_memory_operands(chordal_env);
467 be_abi_fix_stack_nodes(birg->abi);
470 BE_TIMER_PUSH(ra_timer.t_verify);
472 /* verify schedule and register pressure */
473 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
474 be_verify_schedule(birg);
475 be_verify_register_pressure(birg, pse->cls, irg);
477 else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
478 assert(be_verify_schedule(birg) && "Schedule verification failed");
479 assert(be_verify_register_pressure(birg, pse->cls, irg)
480 && "Register pressure verification failed");
482 BE_TIMER_POP(ra_timer.t_verify);
484 /* Color the graph. */
485 BE_TIMER_PUSH(ra_timer.t_color);
486 be_ra_chordal_color(chordal_env);
487 BE_TIMER_POP(ra_timer.t_color);
489 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
491 /* Create the ifg with the selected flavor */
492 BE_TIMER_PUSH(ra_timer.t_ifg);
493 chordal_env->ifg = be_create_ifg(chordal_env);
494 BE_TIMER_POP(ra_timer.t_ifg);
496 #ifdef FIRM_STATISTICS
497 if (be_stat_ev_is_active()) {
500 be_ifg_stat(birg, chordal_env->ifg, &stat);
501 be_stat_ev("ifg_nodes", stat.n_nodes);
502 be_stat_ev("ifg_edges", stat.n_edges);
503 be_stat_ev("ifg_comps", stat.n_comps);
505 node_stats(birg, pse->cls, &node_stat);
506 be_stat_ev("perms_before_coal", node_stat.n_perms);
507 be_stat_ev("copies_before_coal", node_stat.n_copies);
509 #endif /* FIRM_STATISTICS */
511 /* copy minimization */
512 BE_TIMER_PUSH(ra_timer.t_copymin);
513 co_driver(chordal_env);
514 BE_TIMER_POP(ra_timer.t_copymin);
516 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "-copymin", dump_ir_block_graph_sched);
518 BE_TIMER_PUSH(ra_timer.t_ssa);
520 /* ssa destruction */
521 be_ssa_destruction(chordal_env);
523 BE_TIMER_POP(ra_timer.t_ssa);
525 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
527 BE_TIMER_PUSH(ra_timer.t_verify);
528 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
529 be_ssa_destruction_check(chordal_env);
531 BE_TIMER_POP(ra_timer.t_verify);
533 #ifdef FIRM_STATISTICS
534 if (be_stat_ev_is_active()) {
535 node_stats(birg, pse->cls, &node_stat);
536 be_stat_ev("perms_after_coal", node_stat.n_perms);
537 be_stat_ev("copies_after_coal", node_stat.n_copies);
540 #endif /* FIRM_STATISTICS */
542 /* the ifg exists only if there are allocatable regs */
543 be_ifg_free(chordal_env->ifg);
546 /* free some always allocated data structures */
547 pmap_destroy(chordal_env->border_heads);
548 bitset_free(chordal_env->ignore_colors);
552 * Performs chordal register allocation for each register class on given irg.
554 * @param birg Backend irg object
555 * @return Structure containing timer for the single phases or NULL if no timing requested.
557 static void be_ra_chordal_main(be_irg_t *birg)
559 const be_main_env_t *main_env = birg->main_env;
560 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
561 ir_graph *irg = birg->irg;
562 be_options_t *main_opts = main_env->options;
564 be_chordal_env_t chordal_env;
567 BE_TIMER_INIT(main_opts);
568 BE_TIMER_PUSH(ra_timer.t_other);
569 BE_TIMER_PUSH(ra_timer.t_prolog);
571 be_assure_dom_front(birg);
572 be_assure_liveness(birg);
574 chordal_env.obst = &obst;
575 chordal_env.opts = &options;
576 chordal_env.irg = irg;
577 chordal_env.birg = birg;
578 chordal_env.border_heads = NULL;
579 chordal_env.ifg = NULL;
580 chordal_env.ignore_colors = NULL;
584 BE_TIMER_POP(ra_timer.t_prolog);
586 be_stat_ev("insns_before", count_insns(irg));
588 if (! arch_code_generator_has_spiller(birg->cg)) {
589 /* use one of the generic spiller */
591 /* Perform the following for each register class. */
592 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
593 post_spill_env_t pse;
595 memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
597 pre_spill(isa, j, &pse);
599 BE_TIMER_PUSH(ra_timer.t_spill);
600 be_do_spill(birg, pse.cls);
601 BE_TIMER_POP(ra_timer.t_spill);
603 dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill", dump_ir_block_graph_sched);
608 post_spill_env_t *pse;
610 /* the backend has it's own spiller */
611 m = arch_isa_get_n_reg_class(isa);
613 pse = alloca(m * sizeof(pse[0]));
615 for (j = 0; j < m; ++j) {
616 memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
618 pre_spill(isa, j, &pse[j]);
621 BE_TIMER_PUSH(ra_timer.t_spill);
622 arch_code_generator_spill(birg->cg, birg);
623 BE_TIMER_POP(ra_timer.t_spill);
624 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
626 for (j = 0; j < m; ++j) {
627 post_spill(&pse[j], j);
631 BE_TIMER_PUSH(ra_timer.t_epilog);
632 lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
633 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
635 obstack_free(&obst, NULL);
636 be_invalidate_liveness(birg);
637 BE_TIMER_POP(ra_timer.t_epilog);
639 BE_TIMER_POP(ra_timer.t_other);
641 be_stat_ev("insns_after", count_insns(irg));
646 static be_ra_t be_ra_chordal_allocator = {
650 void be_init_chordal_main(void)
652 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
653 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
654 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
656 lc_opt_add_table(chordal_grp, be_chordal_options);
658 be_register_allocator("chordal", &be_ra_chordal_allocator);
661 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);