2 * Copyright (C) 1995-2008 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 "bespillslots.h"
81 #include "bespilloptions.h"
85 #include "bespillremat.h"
88 #include "bejavacoal.h"
89 #include "becopystat.h"
90 #include "becopyopt.h"
91 #include "bessadestr.h"
95 static be_ra_chordal_opts_t options = {
97 BE_CH_LOWER_PERM_SWAP,
103 typedef struct _post_spill_env_t {
104 be_chordal_env_t cenv;
106 const arch_register_class_t *cls;
107 double pre_spill_cost;
110 static be_options_t *main_opts;
112 static const lc_opt_enum_int_items_t lower_perm_items[] = {
113 { "copy", BE_CH_LOWER_PERM_COPY },
114 { "swap", BE_CH_LOWER_PERM_SWAP },
118 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
122 static const lc_opt_enum_int_items_t dump_items[] = {
123 { "none", BE_CH_DUMP_NONE },
124 { "spill", BE_CH_DUMP_SPILL },
125 { "live", BE_CH_DUMP_LIVE },
126 { "color", BE_CH_DUMP_COLOR },
127 { "copymin", BE_CH_DUMP_COPYMIN },
128 { "ssadestr", BE_CH_DUMP_SSADESTR },
129 { "tree", BE_CH_DUMP_TREE_INTV },
130 { "constr", BE_CH_DUMP_CONSTR },
131 { "lower", BE_CH_DUMP_LOWER },
132 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
133 { "appel", BE_CH_DUMP_APPEL },
134 { "all", BE_CH_DUMP_ALL },
138 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
139 { "off", BE_CH_VRFY_OFF },
140 { "warn", BE_CH_VRFY_WARN },
141 { "assert", BE_CH_VRFY_ASSERT },
145 static lc_opt_enum_int_var_t lower_perm_var = {
146 &options.lower_perm_opt, lower_perm_items
149 static lc_opt_enum_int_var_t dump_var = {
150 &options.dump_flags, dump_items
153 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
154 &options.vrfy_option, be_ch_vrfy_items
157 static const lc_opt_table_entry_t be_chordal_options[] = {
158 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options", &lower_perm_var),
159 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
160 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options", &be_ch_vrfy_var),
164 static void dump(unsigned mask, ir_graph *irg,
165 const arch_register_class_t *cls,
167 void (*dump_func)(ir_graph *, const char *))
169 if((options.dump_flags & mask) == mask) {
172 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
173 be_dump(irg, buf, dump_func);
176 be_dump(irg, suffix, dump_func);
181 * Checks for every reload if it's user can perform the load on itself.
183 static void memory_operand_walker(ir_node *irn, void *env) {
184 be_chordal_env_t *cenv = env;
185 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
186 const ir_edge_t *edge, *ne;
190 if (! be_is_Reload(irn))
193 /* only use memory operands, if the reload is only used by 1 node */
194 if(get_irn_n_edges(irn) > 1)
197 spill = be_get_Reload_mem(irn);
198 block = get_nodes_block(irn);
200 foreach_out_edge_safe(irn, edge, ne) {
201 ir_node *src = get_edge_src_irn(edge);
202 int pos = get_edge_src_pos(edge);
204 assert(src && "outedges broken!");
206 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
207 arch_perform_memory_operand(aenv, src, spill, pos);
211 /* kill the Reload */
212 if (get_irn_n_edges(irn) == 0) {
214 set_irn_n(irn, be_pos_Reload_mem, new_Bad());
215 set_irn_n(irn, be_pos_Reload_frame, new_Bad());
220 * Starts a walk for memory operands if supported by the backend.
222 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
223 irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
227 * Sorry for doing stats again...
229 typedef struct _node_stat_t {
230 unsigned int n_phis; /**< Phis of the current register class. */
231 unsigned int n_mem_phis; /**< Memory Phis (Phis with spill operands). */
232 unsigned int n_copies; /**< Copies */
233 unsigned int n_perms; /**< Perms */
234 unsigned int n_spills; /**< Spill nodes */
235 unsigned int n_reloads; /**< Reloads */
238 struct node_stat_walker {
240 const arch_env_t *arch_env;
242 const arch_register_class_t *cls;
245 static void node_stat_walker(ir_node *irn, void *data)
247 struct node_stat_walker *env = data;
248 const arch_env_t *aenv = env->arch_env;
250 if (arch_irn_consider_in_reg_alloc(aenv, env->cls, irn)) {
252 /* if the node is a normal phi */
257 arch_irn_class_t classify = arch_irn_classify(aenv, irn);
259 if(classify & arch_irn_class_spill)
260 ++env->stat->n_spills;
261 if(classify & arch_irn_class_reload)
262 ++env->stat->n_reloads;
263 if(classify & arch_irn_class_copy)
264 ++env->stat->n_copies;
265 if(classify & arch_irn_class_perm)
266 ++env->stat->n_perms;
270 /* a mem phi is a PhiM with a mem phi operand or a Spill operand */
271 /*else*/ if(is_Phi(irn) && get_irn_mode(irn) == mode_M) {
274 for(i = get_irn_arity(irn) - 1; i >= 0; --i) {
275 ir_node *op = get_irn_n(irn, i);
277 if((is_Phi(op) && bitset_contains_irn(env->mem_phis, op)) || (arch_irn_classify(aenv, op) & arch_irn_class_spill)) {
278 bitset_add_irn(env->mem_phis, irn);
279 env->stat->n_mem_phis++;
286 static void node_stats(be_irg_t *birg, const arch_register_class_t *cls, node_stat_t *stat)
288 struct node_stat_walker env;
290 memset(stat, 0, sizeof(stat[0]));
291 env.arch_env = birg->main_env->arch_env;
292 env.mem_phis = bitset_irg_malloc(birg->irg);
295 irg_walk_graph(birg->irg, NULL, node_stat_walker, &env);
296 bitset_free(env.mem_phis);
299 static void insn_count_walker(ir_node *irn, void *data)
303 switch(get_irn_opcode(irn)) {
314 static unsigned int count_insns(ir_graph *irg)
317 irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
322 * Perform things which need to be done per register class before spilling.
324 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
326 be_chordal_env_t *chordal_env = &pse->cenv;
327 be_irg_t *birg = pse->birg;
328 ir_graph *irg = be_get_birg_irg(birg);
329 const be_main_env_t *main_env = birg->main_env;
330 node_stat_t node_stat;
333 chordal_env->cls = cls;
334 chordal_env->border_heads = pmap_create();
335 chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
337 be_assure_liveness(birg);
338 be_liveness_assure_chk(be_get_birg_liveness(birg));
340 stat_ev_ctx_push_str("bechordal_cls", pse->cls->name);
341 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
342 stat_ev_do(pse->pre_spill_cost = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq));
343 stat_ev_dbl("phis_before_spill", node_stat.n_phis);
345 /* put all ignore registers into the ignore register set. */
346 be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
348 BE_TIMER_PUSH(t_ra_constr);
349 be_pre_spill_prepare_constr(chordal_env);
350 BE_TIMER_POP(t_ra_constr);
352 dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
354 stat_ev_ctx_pop("bechordal_cls");
358 * Perform things which need to be done per register class after spilling.
360 static void post_spill(post_spill_env_t *pse, int iteration) {
361 be_chordal_env_t *chordal_env = &pse->cenv;
362 be_irg_t *birg = pse->birg;
363 ir_graph *irg = birg->irg;
364 const be_main_env_t *main_env = birg->main_env;
365 node_stat_t node_stat;
366 int colors_n = arch_register_class_n_regs(chordal_env->cls);
367 int allocatable_regs = colors_n - be_put_ignore_regs(birg, chordal_env->cls, NULL);
369 /* some special classes contain only ignore regs, no work to be done */
370 if (allocatable_regs > 0) {
372 stat_ev_ctx_push_str("bechordal_cls", pse->cls->name);
373 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
374 stat_ev_dbl("phis_after_spill", node_stat.n_phis);
375 stat_ev_dbl("mem_phis", node_stat.n_mem_phis);
376 stat_ev_dbl("reloads", node_stat.n_reloads);
377 stat_ev_dbl("spills", node_stat.n_spills);
378 stat_ev_dbl("spillcosts", be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost);
381 If we have a backend provided spiller, post spill is
382 called in a loop after spilling for each register class.
383 But we only need to fix stack nodes once in this case.
385 BE_TIMER_PUSH(t_ra_spill);
386 check_for_memory_operands(chordal_env);
387 if (iteration == 0) {
388 be_abi_fix_stack_nodes(birg->abi);
390 BE_TIMER_POP(t_ra_spill);
392 BE_TIMER_PUSH(t_verify);
394 /* verify schedule and register pressure */
395 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
396 be_verify_schedule(birg);
397 be_verify_register_pressure(birg, pse->cls, irg);
399 else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
400 assert(be_verify_schedule(birg) && "Schedule verification failed");
401 assert(be_verify_register_pressure(birg, pse->cls, irg)
402 && "Register pressure verification failed");
404 BE_TIMER_POP(t_verify);
406 /* Color the graph. */
407 BE_TIMER_PUSH(t_ra_color);
408 be_ra_chordal_color(chordal_env);
409 BE_TIMER_POP(t_ra_color);
411 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
413 /* Create the ifg with the selected flavor */
414 BE_TIMER_PUSH(t_ra_ifg);
415 chordal_env->ifg = be_create_ifg(chordal_env);
416 BE_TIMER_POP(t_ra_ifg);
421 stat_ev_do(be_ifg_stat(birg, chordal_env->ifg, &stat));
422 stat_ev_dbl("ifg_nodes", stat.n_nodes);
423 stat_ev_dbl("ifg_edges", stat.n_edges);
424 stat_ev_dbl("ifg_comps", stat.n_comps);
426 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
427 stat_ev_dbl("perms_before_coal", node_stat.n_perms);
428 stat_ev_dbl("copies_before_coal", node_stat.n_copies);
431 /* copy minimization */
432 BE_TIMER_PUSH(t_ra_copymin);
433 co_driver(chordal_env);
434 BE_TIMER_POP(t_ra_copymin);
436 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "-copymin", dump_ir_block_graph_sched);
439 /* ssa destruction */
440 BE_TIMER_PUSH(t_ra_ssa);
441 stat_ev_ctx_push_str("berachordal_phase", "ssadestr");
442 be_ssa_destruction(chordal_env);
443 stat_ev_ctx_pop("berachordal_phase");
444 BE_TIMER_POP(t_ra_ssa);
446 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
448 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
449 BE_TIMER_PUSH(t_verify);
450 be_ssa_destruction_check(chordal_env);
451 BE_TIMER_POP(t_verify);
454 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
455 stat_ev_dbl("perms_after_coal", node_stat.n_perms);
456 stat_ev_dbl("copies_after_coal", node_stat.n_copies);
457 stat_ev_ctx_pop("bechordal_cls");
459 /* the ifg exists only if there are allocatable regs */
460 be_ifg_free(chordal_env->ifg);
463 /* free some always allocated data structures */
464 pmap_destroy(chordal_env->border_heads);
465 bitset_free(chordal_env->ignore_colors);
469 * Performs chordal register allocation for each register class on given irg.
471 * @param birg Backend irg object
472 * @return Structure containing timer for the single phases or NULL if no timing requested.
474 static void be_ra_chordal_main(be_irg_t *birg)
476 const be_main_env_t *main_env = birg->main_env;
477 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
478 ir_graph *irg = birg->irg;
480 be_chordal_env_t chordal_env;
483 main_opts = main_env->options;
485 BE_TIMER_PUSH(t_ra_other);
487 BE_TIMER_PUSH(t_ra_prolog);
489 be_assure_dom_front(birg);
490 be_assure_liveness(birg);
492 chordal_env.obst = &obst;
493 chordal_env.opts = &options;
494 chordal_env.irg = irg;
495 chordal_env.birg = birg;
496 chordal_env.border_heads = NULL;
497 chordal_env.ifg = NULL;
498 chordal_env.ignore_colors = NULL;
502 BE_TIMER_POP(t_ra_prolog);
505 be_stat_ev("insns_before", count_insns(irg));
508 if (! arch_code_generator_has_spiller(birg->cg)) {
509 /* use one of the generic spiller */
511 /* Perform the following for each register class. */
512 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
513 post_spill_env_t pse;
514 const arch_register_class_t *cls
515 = arch_isa_get_reg_class(isa, j);
517 if(arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
521 memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
523 pre_spill(&pse, cls);
525 BE_TIMER_PUSH(t_ra_spill);
526 be_do_spill(birg, cls);
527 BE_TIMER_POP(t_ra_spill);
529 dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill",
530 dump_ir_block_graph_sched);
535 post_spill_env_t *pse;
537 /* the backend has it's own spiller */
538 m = arch_isa_get_n_reg_class(isa);
540 pse = alloca(m * sizeof(pse[0]));
542 for (j = 0; j < m; ++j) {
543 memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
545 pre_spill(&pse[j], pse[j].cls);
548 BE_TIMER_PUSH(t_ra_spill);
549 arch_code_generator_spill(birg->cg, birg);
550 BE_TIMER_POP(t_ra_spill);
551 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
553 for (j = 0; j < m; ++j) {
554 post_spill(&pse[j], j);
558 BE_TIMER_PUSH(t_verify);
559 be_verify_register_allocation(birg);
560 BE_TIMER_POP(t_verify);
562 BE_TIMER_PUSH(t_ra_epilog);
563 lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
564 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
566 obstack_free(&obst, NULL);
567 be_liveness_invalidate(be_get_birg_liveness(birg));
568 BE_TIMER_POP(t_ra_epilog);
570 BE_TIMER_POP(t_ra_other);
573 be_stat_ev("insns_after", count_insns(irg));
579 static be_ra_t be_ra_chordal_allocator = {
583 void be_init_chordal_main(void)
585 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
586 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
587 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
589 lc_opt_add_table(chordal_grp, be_chordal_options);
591 be_register_allocator("chordal", &be_ra_chordal_allocator);
594 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);