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 "lc_opts_enum.h"
43 #include "irgraph_t.h"
44 #include "irprintf_t.h"
55 #include "iredges_t.h"
58 #include "bechordal_t.h"
67 #include "bestatevent.h"
73 #include "bedump_minir.h"
75 #include "bespillslots.h"
79 #include "becopystat.h"
80 #include "becopyopt.h"
81 #include "bessadestr.h"
85 #include "bepbqpcoloring.h"
87 static be_ra_chordal_opts_t options = {
89 BE_CH_LOWER_PERM_SWAP,
95 typedef struct _post_spill_env_t {
96 be_chordal_env_t cenv;
98 const arch_register_class_t *cls;
99 double pre_spill_cost;
102 static const lc_opt_enum_int_items_t lower_perm_items[] = {
103 { "copy", BE_CH_LOWER_PERM_COPY },
104 { "swap", BE_CH_LOWER_PERM_SWAP },
108 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
112 static const lc_opt_enum_int_items_t dump_items[] = {
113 { "none", BE_CH_DUMP_NONE },
114 { "spill", BE_CH_DUMP_SPILL },
115 { "live", BE_CH_DUMP_LIVE },
116 { "color", BE_CH_DUMP_COLOR },
117 { "copymin", BE_CH_DUMP_COPYMIN },
118 { "ssadestr", BE_CH_DUMP_SSADESTR },
119 { "tree", BE_CH_DUMP_TREE_INTV },
120 { "constr", BE_CH_DUMP_CONSTR },
121 { "lower", BE_CH_DUMP_LOWER },
122 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
123 { "appel", BE_CH_DUMP_APPEL },
124 { "all", BE_CH_DUMP_ALL },
128 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
129 { "off", BE_CH_VRFY_OFF },
130 { "warn", BE_CH_VRFY_WARN },
131 { "assert", BE_CH_VRFY_ASSERT },
135 static lc_opt_enum_int_var_t lower_perm_var = {
136 &options.lower_perm_opt, lower_perm_items
139 static lc_opt_enum_int_var_t dump_var = {
140 &options.dump_flags, dump_items
143 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
144 &options.vrfy_option, be_ch_vrfy_items
147 static char minir_file[256] = "";
149 static const lc_opt_table_entry_t be_chordal_options[] = {
150 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options", &lower_perm_var),
151 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
152 LC_OPT_ENT_ENUM_PTR ("verify", "verify options", &be_ch_vrfy_var),
153 LC_OPT_ENT_STR ("minirout", "dump MinIR to file", minir_file, sizeof(minir_file)),
157 static be_module_list_entry_t *colorings = NULL;
158 static const be_ra_chordal_coloring_t *selected_coloring = NULL;
160 void be_register_chordal_coloring(const char *name, be_ra_chordal_coloring_t *coloring)
162 if (selected_coloring == NULL)
163 selected_coloring = coloring;
165 be_add_module_to_list(&colorings, name, coloring);
168 static void be_ra_chordal_coloring(be_chordal_env_t *env)
170 assert(selected_coloring != NULL);
171 if (selected_coloring != NULL) {
172 selected_coloring->allocate(env);
176 static void dump(unsigned mask, ir_graph *irg,
177 const arch_register_class_t *cls,
180 if ((options.dump_flags & mask) == mask) {
183 snprintf(buf, sizeof(buf), "%s-%s", cls->name, suffix);
184 dump_ir_graph(irg, buf);
186 dump_ir_graph(irg, suffix);
192 * Checks for every reload if its user can perform the load on itself.
194 static void memory_operand_walker(ir_node *irn, void *env)
196 const ir_edge_t *edge, *ne;
202 if (! be_is_Reload(irn))
205 /* only use memory operands, if the reload is only used by 1 node */
206 if (get_irn_n_edges(irn) > 1)
209 spill = be_get_Reload_mem(irn);
210 block = get_nodes_block(irn);
212 foreach_out_edge_safe(irn, edge, ne) {
213 ir_node *src = get_edge_src_irn(edge);
214 int pos = get_edge_src_pos(edge);
216 assert(src && "outedges broken!");
218 if (get_nodes_block(src) == block && arch_possible_memory_operand(src, pos)) {
219 arch_perform_memory_operand(src, spill, pos);
223 /* kill the Reload */
224 if (get_irn_n_edges(irn) == 0) {
226 set_irn_n(irn, be_pos_Reload_mem, new_Bad());
227 set_irn_n(irn, be_pos_Reload_frame, new_Bad());
232 * Starts a walk for memory operands if supported by the backend.
234 void check_for_memory_operands(ir_graph *irg)
236 irg_walk_graph(irg, NULL, memory_operand_walker, NULL);
240 static be_node_stats_t last_node_stats;
243 * Perform things which need to be done per register class before spilling.
245 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
247 be_chordal_env_t *chordal_env = &pse->cenv;
248 be_irg_t *birg = pse->birg;
249 ir_graph *irg = be_get_birg_irg(birg);
252 chordal_env->cls = cls;
253 chordal_env->border_heads = pmap_create();
254 chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
256 be_assure_liveness(birg);
257 be_liveness_assure_chk(be_get_birg_liveness(birg));
259 stat_ev_do(pse->pre_spill_cost = be_estimate_irg_costs(irg, birg->exec_freq));
261 /* put all ignore registers into the ignore register set. */
262 be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
264 be_timer_push(T_RA_CONSTR);
265 be_pre_spill_prepare_constr(chordal_env->birg, chordal_env->cls);
266 be_timer_pop(T_RA_CONSTR);
268 dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "constr-pre");
272 * Perform things which need to be done per register class after spilling.
274 static void post_spill(post_spill_env_t *pse, int iteration)
276 be_chordal_env_t *chordal_env = &pse->cenv;
277 be_irg_t *birg = pse->birg;
278 ir_graph *irg = birg->irg;
279 int colors_n = arch_register_class_n_regs(chordal_env->cls);
280 int allocatable_regs = colors_n - be_put_ignore_regs(birg, chordal_env->cls, NULL);
282 /* some special classes contain only ignore regs, no work to be done */
283 if (allocatable_regs > 0) {
284 stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg, birg->exec_freq) - pse->pre_spill_cost);
287 If we have a backend provided spiller, post spill is
288 called in a loop after spilling for each register class.
289 But we only need to fix stack nodes once in this case.
291 be_timer_push(T_RA_SPILL_APPLY);
292 check_for_memory_operands(irg);
293 if (iteration == 0) {
294 be_abi_fix_stack_nodes(birg->abi);
296 be_timer_pop(T_RA_SPILL_APPLY);
299 /* verify schedule and register pressure */
300 be_timer_push(T_VERIFY);
301 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
302 be_verify_schedule(birg);
303 be_verify_register_pressure(birg, pse->cls, irg);
304 } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
305 assert(be_verify_schedule(birg) && "Schedule verification failed");
306 assert(be_verify_register_pressure(birg, pse->cls, irg)
307 && "Register pressure verification failed");
309 be_timer_pop(T_VERIFY);
311 /* Color the graph. */
312 be_timer_push(T_RA_COLOR);
313 be_ra_chordal_coloring(chordal_env);
314 be_timer_pop(T_RA_COLOR);
316 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "color");
318 /* Create the ifg with the selected flavor */
319 be_timer_push(T_RA_IFG);
320 chordal_env->ifg = be_create_ifg(chordal_env);
321 be_timer_pop(T_RA_IFG);
325 be_node_stats_t node_stats;
327 be_ifg_stat(birg, chordal_env->ifg, &stat);
328 stat_ev_dbl("bechordal_ifg_nodes", stat.n_nodes);
329 stat_ev_dbl("bechordal_ifg_edges", stat.n_edges);
330 stat_ev_dbl("bechordal_ifg_comps", stat.n_comps);
332 be_collect_node_stats(&node_stats, birg);
333 be_subtract_node_stats(&node_stats, &last_node_stats);
335 stat_ev_dbl("bechordal_perms_before_coal",
336 node_stats[BE_STAT_PERMS]);
337 stat_ev_dbl("bechordal_copies_before_coal",
338 node_stats[BE_STAT_COPIES]);
341 be_timer_push(T_RA_COPYMIN);
342 if (minir_file[0] != '\0') {
345 if (strcmp(minir_file, "-") == 0) {
348 out = fopen(minir_file, "w");
350 panic("Cound't open minir output '%s'", minir_file);
354 be_export_minir(chordal_env->birg->main_env->arch_env, out, irg);
358 co_driver(chordal_env);
359 be_timer_pop(T_RA_COPYMIN);
361 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "copymin");
363 /* ssa destruction */
364 be_timer_push(T_RA_SSA);
365 be_ssa_destruction(chordal_env);
366 be_timer_pop(T_RA_SSA);
368 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "ssadestr");
370 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
371 be_timer_push(T_VERIFY);
372 be_ssa_destruction_check(chordal_env);
373 be_timer_pop(T_VERIFY);
376 /* the ifg exists only if there are allocatable regs */
377 be_ifg_free(chordal_env->ifg);
380 /* free some always allocated data structures */
381 pmap_destroy(chordal_env->border_heads);
382 bitset_free(chordal_env->ignore_colors);
386 * Performs chordal register allocation for each register class on given irg.
388 * @param birg Backend irg object
389 * @return Structure containing timer for the single phases or NULL if no timing requested.
391 static void be_ra_chordal_main(be_irg_t *birg)
393 const arch_env_t *arch_env = birg->main_env->arch_env;
394 ir_graph *irg = birg->irg;
397 be_chordal_env_t chordal_env;
400 be_timer_push(T_RA_OTHER);
402 be_timer_push(T_RA_PROLOG);
404 be_assure_liveness(birg);
406 chordal_env.obst = &obst;
407 chordal_env.opts = &options;
408 chordal_env.irg = irg;
409 chordal_env.birg = birg;
410 chordal_env.border_heads = NULL;
411 chordal_env.ifg = NULL;
412 chordal_env.ignore_colors = NULL;
416 be_timer_pop(T_RA_PROLOG);
419 be_collect_node_stats(&last_node_stats, birg);
422 if (! arch_code_generator_has_spiller(birg->cg)) {
423 /* use one of the generic spiller */
425 /* Perform the following for each register class. */
426 for (j = 0, m = arch_env_get_n_reg_class(arch_env); j < m; ++j) {
427 post_spill_env_t pse;
428 const arch_register_class_t *cls
429 = arch_env_get_reg_class(arch_env, j);
431 if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
435 stat_ev_ctx_push_str("bechordal_cls", cls->name);
438 be_do_stat_reg_pressure(birg, cls);
441 memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
443 pre_spill(&pse, cls);
445 be_timer_push(T_RA_SPILL);
446 be_do_spill(birg, cls);
447 be_timer_pop(T_RA_SPILL);
449 dump(BE_CH_DUMP_SPILL, irg, pse.cls, "spill");
454 be_node_stats_t node_stats;
456 be_collect_node_stats(&node_stats, birg);
457 be_subtract_node_stats(&node_stats, &last_node_stats);
458 be_emit_node_stats(&node_stats, "bechordal_");
460 be_copy_node_stats(&last_node_stats, &node_stats);
461 stat_ev_ctx_pop("bechordal_cls");
465 post_spill_env_t *pse;
467 /* the backend has its own spiller */
468 m = arch_env_get_n_reg_class(arch_env);
470 pse = ALLOCAN(post_spill_env_t, m);
472 for (j = 0; j < m; ++j) {
473 memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
475 pre_spill(&pse[j], pse[j].cls);
478 be_timer_push(T_RA_SPILL);
479 arch_code_generator_spill(birg->cg, birg);
480 be_timer_pop(T_RA_SPILL);
481 dump(BE_CH_DUMP_SPILL, irg, NULL, "spill");
483 for (j = 0; j < m; ++j) {
484 post_spill(&pse[j], j);
488 be_timer_push(T_VERIFY);
489 if (chordal_env.opts->vrfy_option == BE_CH_VRFY_WARN) {
490 be_verify_register_allocation(birg);
491 } else if (chordal_env.opts->vrfy_option == BE_CH_VRFY_ASSERT) {
492 assert(be_verify_register_allocation(birg)
493 && "Register allocation invalid");
495 be_timer_pop(T_VERIFY);
497 be_timer_push(T_RA_EPILOG);
498 lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
499 dump(BE_CH_DUMP_LOWER, irg, NULL, "belower-after-ra");
501 obstack_free(&obst, NULL);
502 be_liveness_invalidate(be_get_birg_liveness(birg));
503 be_timer_pop(T_RA_EPILOG);
505 be_timer_pop(T_RA_OTHER);
508 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);
509 void be_init_chordal_main(void)
511 static be_ra_t be_ra_chordal_allocator = {
515 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
516 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
517 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
519 be_register_allocator("chordal", &be_ra_chordal_allocator);
521 lc_opt_add_table(chordal_grp, be_chordal_options);
522 be_add_module_list_opt(chordal_grp, "coloring", "select coloring methode", &colorings, (void**) &selected_coloring);