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"
42 #include "lc_opts_enum.h"
46 #include "irgraph_t.h"
47 #include "irprintf_t.h"
59 #include "iredges_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 "bespillslots.h"
80 #include "bespilloptions.h"
84 #include "bespillremat.h"
87 #include "bejavacoal.h"
88 #include "becopystat.h"
89 #include "becopyopt.h"
90 #include "bessadestr.h"
94 static be_ra_chordal_opts_t options = {
96 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_options_t *main_opts;
111 static const lc_opt_enum_int_items_t lower_perm_items[] = {
112 { "copy", BE_CH_LOWER_PERM_COPY },
113 { "swap", BE_CH_LOWER_PERM_SWAP },
117 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
121 static const lc_opt_enum_int_items_t dump_items[] = {
122 { "none", BE_CH_DUMP_NONE },
123 { "spill", BE_CH_DUMP_SPILL },
124 { "live", BE_CH_DUMP_LIVE },
125 { "color", BE_CH_DUMP_COLOR },
126 { "copymin", BE_CH_DUMP_COPYMIN },
127 { "ssadestr", BE_CH_DUMP_SSADESTR },
128 { "tree", BE_CH_DUMP_TREE_INTV },
129 { "constr", BE_CH_DUMP_CONSTR },
130 { "lower", BE_CH_DUMP_LOWER },
131 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
132 { "appel", BE_CH_DUMP_APPEL },
133 { "all", BE_CH_DUMP_ALL },
137 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
138 { "off", BE_CH_VRFY_OFF },
139 { "warn", BE_CH_VRFY_WARN },
140 { "assert", BE_CH_VRFY_ASSERT },
144 static lc_opt_enum_int_var_t lower_perm_var = {
145 &options.lower_perm_opt, lower_perm_items
148 static lc_opt_enum_int_var_t dump_var = {
149 &options.dump_flags, dump_items
152 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
153 &options.vrfy_option, be_ch_vrfy_items
156 static const lc_opt_table_entry_t be_chordal_options[] = {
157 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options", &lower_perm_var),
158 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
159 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options", &be_ch_vrfy_var),
163 static void dump(unsigned mask, ir_graph *irg,
164 const arch_register_class_t *cls,
166 void (*dump_func)(ir_graph *, const char *))
168 if((options.dump_flags & mask) == mask) {
171 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
172 be_dump(irg, buf, dump_func);
175 be_dump(irg, suffix, dump_func);
180 * Checks for every reload if it's user can perform the load on itself.
182 static void memory_operand_walker(ir_node *irn, void *env) {
183 be_chordal_env_t *cenv = env;
184 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
185 const ir_edge_t *edge, *ne;
189 if (! be_is_Reload(irn))
192 /* only use memory operands, if the reload is only used by 1 node */
193 if(get_irn_n_edges(irn) > 1)
196 spill = be_get_Reload_mem(irn);
197 block = get_nodes_block(irn);
199 foreach_out_edge_safe(irn, edge, ne) {
200 ir_node *src = get_edge_src_irn(edge);
201 int pos = get_edge_src_pos(edge);
203 assert(src && "outedges broken!");
205 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
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 */
256 arch_irn_class_t classify = arch_irn_classify(aenv, irn);
258 if(classify & arch_irn_class_spill)
259 ++env->stat->n_spills;
260 if(classify & arch_irn_class_reload)
261 ++env->stat->n_reloads;
262 if(classify & arch_irn_class_copy)
263 ++env->stat->n_copies;
264 if(classify & arch_irn_class_perm)
265 ++env->stat->n_perms;
269 /* a mem phi is a PhiM with a mem phi operand or a Spill operand */
270 /*else*/ if(is_Phi(irn) && get_irn_mode(irn) == mode_M) {
273 for(i = get_irn_arity(irn) - 1; i >= 0; --i) {
274 ir_node *op = get_irn_n(irn, i);
276 if((is_Phi(op) && bitset_contains_irn(env->mem_phis, op)) || (arch_irn_classify(aenv, op) & arch_irn_class_spill)) {
277 bitset_add_irn(env->mem_phis, irn);
278 env->stat->n_mem_phis++;
285 static void node_stats(be_irg_t *birg, const arch_register_class_t *cls, node_stat_t *stat)
287 struct node_stat_walker env;
289 memset(stat, 0, sizeof(stat[0]));
290 env.arch_env = birg->main_env->arch_env;
291 env.mem_phis = bitset_irg_malloc(birg->irg);
294 irg_walk_graph(birg->irg, NULL, node_stat_walker, &env);
295 bitset_free(env.mem_phis);
298 static void insn_count_walker(ir_node *irn, void *data)
302 switch(get_irn_opcode(irn)) {
313 static unsigned int count_insns(ir_graph *irg)
316 irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
321 * Perform things which need to be done per register class before spilling.
323 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
325 be_chordal_env_t *chordal_env = &pse->cenv;
326 be_irg_t *birg = pse->birg;
327 ir_graph *irg = be_get_birg_irg(birg);
328 const be_main_env_t *main_env = birg->main_env;
329 node_stat_t node_stat;
332 chordal_env->cls = cls;
333 chordal_env->border_heads = pmap_create();
334 chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
336 be_assure_liveness(birg);
337 be_liveness_assure_chk(be_get_birg_liveness(birg));
339 stat_ev_ctx_push_str("bechordal_cls", pse->cls->name);
340 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
341 stat_ev_do(pse->pre_spill_cost = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq));
342 stat_ev_dbl("phis_before_spill", node_stat.n_phis);
344 /* put all ignore registers into the ignore register set. */
345 be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
347 BE_TIMER_PUSH(t_ra_constr);
348 be_pre_spill_prepare_constr(chordal_env);
349 BE_TIMER_POP(t_ra_constr);
351 dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
353 stat_ev_ctx_pop("bechordal_cls");
357 * Perform things which need to be done per register class after spilling.
359 static void post_spill(post_spill_env_t *pse, int iteration) {
360 be_chordal_env_t *chordal_env = &pse->cenv;
361 be_irg_t *birg = pse->birg;
362 ir_graph *irg = birg->irg;
363 const be_main_env_t *main_env = birg->main_env;
364 node_stat_t node_stat;
365 int colors_n = arch_register_class_n_regs(chordal_env->cls);
366 int allocatable_regs = colors_n - be_put_ignore_regs(birg, chordal_env->cls, NULL);
368 /* some special classes contain only ignore regs, no work to be done */
369 if (allocatable_regs > 0) {
371 stat_ev_ctx_push_str("bechordal_cls", pse->cls->name);
372 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
373 stat_ev_dbl("phis_after_spill", node_stat.n_phis);
374 stat_ev_dbl("mem_phis", node_stat.n_mem_phis);
375 stat_ev_dbl("reloads", node_stat.n_reloads);
376 stat_ev_dbl("spills", node_stat.n_spills);
377 stat_ev_dbl("spillcosts", be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost);
380 If we have a backend provided spiller, post spill is
381 called in a loop after spilling for each register class.
382 But we only need to fix stack nodes once in this case.
384 BE_TIMER_PUSH(t_ra_spill);
385 check_for_memory_operands(chordal_env);
386 if (iteration == 0) {
387 be_abi_fix_stack_nodes(birg->abi);
389 BE_TIMER_POP(t_ra_spill);
391 BE_TIMER_PUSH(t_verify);
393 /* verify schedule and register pressure */
394 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
395 be_verify_schedule(birg);
396 be_verify_register_pressure(birg, pse->cls, irg);
398 else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
399 assert(be_verify_schedule(birg) && "Schedule verification failed");
400 assert(be_verify_register_pressure(birg, pse->cls, irg)
401 && "Register pressure verification failed");
403 BE_TIMER_POP(t_verify);
405 /* Color the graph. */
406 BE_TIMER_PUSH(t_ra_color);
407 be_ra_chordal_color(chordal_env);
408 BE_TIMER_POP(t_ra_color);
410 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
412 /* Create the ifg with the selected flavor */
413 BE_TIMER_PUSH(t_ra_ifg);
414 chordal_env->ifg = be_create_ifg(chordal_env);
415 BE_TIMER_POP(t_ra_ifg);
420 stat_ev_do(be_ifg_stat(birg, chordal_env->ifg, &stat));
421 stat_ev_dbl("ifg_nodes", stat.n_nodes);
422 stat_ev_dbl("ifg_edges", stat.n_edges);
423 stat_ev_dbl("ifg_comps", stat.n_comps);
425 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
426 stat_ev_dbl("perms_before_coal", node_stat.n_perms);
427 stat_ev_dbl("copies_before_coal", node_stat.n_copies);
430 /* copy minimization */
431 BE_TIMER_PUSH(t_ra_copymin);
432 co_driver(chordal_env);
433 BE_TIMER_POP(t_ra_copymin);
435 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "-copymin", dump_ir_block_graph_sched);
438 /* ssa destruction */
439 BE_TIMER_PUSH(t_ra_ssa);
440 stat_ev_ctx_push_str("berachordal_phase", "ssadestr");
441 be_ssa_destruction(chordal_env);
442 stat_ev_ctx_pop("berachordal_phase");
443 BE_TIMER_POP(t_ra_ssa);
445 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
447 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
448 BE_TIMER_PUSH(t_verify);
449 be_ssa_destruction_check(chordal_env);
450 BE_TIMER_POP(t_verify);
453 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
454 stat_ev_dbl("perms_after_coal", node_stat.n_perms);
455 stat_ev_dbl("copies_after_coal", node_stat.n_copies);
456 stat_ev_ctx_pop("bechordal_cls");
458 /* the ifg exists only if there are allocatable regs */
459 be_ifg_free(chordal_env->ifg);
462 /* free some always allocated data structures */
463 pmap_destroy(chordal_env->border_heads);
464 bitset_free(chordal_env->ignore_colors);
468 * Performs chordal register allocation for each register class on given irg.
470 * @param birg Backend irg object
471 * @return Structure containing timer for the single phases or NULL if no timing requested.
473 static void be_ra_chordal_main(be_irg_t *birg)
475 const be_main_env_t *main_env = birg->main_env;
476 const arch_env_t *arch_env = main_env->arch_env;
477 ir_graph *irg = birg->irg;
479 be_chordal_env_t chordal_env;
482 main_opts = main_env->options;
484 BE_TIMER_PUSH(t_ra_other);
486 BE_TIMER_PUSH(t_ra_prolog);
488 be_assure_dom_front(birg);
489 be_assure_liveness(birg);
491 chordal_env.obst = &obst;
492 chordal_env.opts = &options;
493 chordal_env.irg = irg;
494 chordal_env.birg = birg;
495 chordal_env.border_heads = NULL;
496 chordal_env.ifg = NULL;
497 chordal_env.ignore_colors = NULL;
501 BE_TIMER_POP(t_ra_prolog);
504 be_stat_ev("insns_before", count_insns(irg));
507 if (! arch_code_generator_has_spiller(birg->cg)) {
508 /* use one of the generic spiller */
510 /* Perform the following for each register class. */
511 for (j = 0, m = arch_env_get_n_reg_class(arch_env); j < m; ++j) {
512 post_spill_env_t pse;
513 const arch_register_class_t *cls
514 = arch_env_get_reg_class(arch_env, j);
516 if(arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
520 memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
522 pre_spill(&pse, cls);
524 BE_TIMER_PUSH(t_ra_spill);
525 be_do_spill(birg, cls);
526 BE_TIMER_POP(t_ra_spill);
528 dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill",
529 dump_ir_block_graph_sched);
534 post_spill_env_t *pse;
536 /* the backend has it's own spiller */
537 m = arch_env_get_n_reg_class(arch_env);
539 pse = alloca(m * sizeof(pse[0]));
541 for (j = 0; j < m; ++j) {
542 memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
544 pre_spill(&pse[j], pse[j].cls);
547 BE_TIMER_PUSH(t_ra_spill);
548 arch_code_generator_spill(birg->cg, birg);
549 BE_TIMER_POP(t_ra_spill);
550 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
552 for (j = 0; j < m; ++j) {
553 post_spill(&pse[j], j);
557 BE_TIMER_PUSH(t_verify);
558 be_verify_register_allocation(birg);
559 BE_TIMER_POP(t_verify);
561 BE_TIMER_PUSH(t_ra_epilog);
562 lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
563 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
565 obstack_free(&obst, NULL);
566 be_liveness_invalidate(be_get_birg_liveness(birg));
567 BE_TIMER_POP(t_ra_epilog);
569 BE_TIMER_POP(t_ra_other);
572 be_stat_ev("insns_after", count_insns(irg));
578 static be_ra_t be_ra_chordal_allocator = {
582 void be_init_chordal_main(void)
584 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
585 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
586 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
588 lc_opt_add_table(chordal_grp, be_chordal_options);
590 be_register_allocator("chordal", &be_ra_chordal_allocator);
593 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);