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"
76 #include "bespillslots.h"
80 #include "becopystat.h"
81 #include "becopyopt.h"
82 #include "bessadestr.h"
86 #include "bepbqpcoloring.h"
88 static be_ra_chordal_opts_t options = {
90 BE_CH_LOWER_PERM_SWAP,
96 typedef struct post_spill_env_t {
97 be_chordal_env_t cenv;
99 const arch_register_class_t *cls;
100 double pre_spill_cost;
103 static const lc_opt_enum_int_items_t lower_perm_items[] = {
104 { "copy", BE_CH_LOWER_PERM_COPY },
105 { "swap", BE_CH_LOWER_PERM_SWAP },
109 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
113 static const lc_opt_enum_int_items_t dump_items[] = {
114 { "none", BE_CH_DUMP_NONE },
115 { "spill", BE_CH_DUMP_SPILL },
116 { "live", BE_CH_DUMP_LIVE },
117 { "color", BE_CH_DUMP_COLOR },
118 { "copymin", BE_CH_DUMP_COPYMIN },
119 { "ssadestr", BE_CH_DUMP_SSADESTR },
120 { "tree", BE_CH_DUMP_TREE_INTV },
121 { "constr", BE_CH_DUMP_CONSTR },
122 { "lower", BE_CH_DUMP_LOWER },
123 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
124 { "appel", BE_CH_DUMP_APPEL },
125 { "all", BE_CH_DUMP_ALL },
129 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
130 { "off", BE_CH_VRFY_OFF },
131 { "warn", BE_CH_VRFY_WARN },
132 { "assert", BE_CH_VRFY_ASSERT },
136 static lc_opt_enum_int_var_t lower_perm_var = {
137 &options.lower_perm_opt, lower_perm_items
140 static lc_opt_enum_int_var_t dump_var = {
141 &options.dump_flags, dump_items
144 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
145 &options.vrfy_option, be_ch_vrfy_items
148 static char minir_file[256] = "";
150 static const lc_opt_table_entry_t be_chordal_options[] = {
151 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options", &lower_perm_var),
152 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
153 LC_OPT_ENT_ENUM_PTR ("verify", "verify options", &be_ch_vrfy_var),
154 LC_OPT_ENT_STR ("minirout", "dump MinIR to file", minir_file, sizeof(minir_file)),
158 static be_module_list_entry_t *colorings = NULL;
159 static const be_ra_chordal_coloring_t *selected_coloring = NULL;
161 void be_register_chordal_coloring(const char *name, be_ra_chordal_coloring_t *coloring)
163 if (selected_coloring == NULL)
164 selected_coloring = coloring;
166 be_add_module_to_list(&colorings, name, coloring);
169 static void be_ra_chordal_coloring(be_chordal_env_t *env)
171 assert(selected_coloring != NULL);
172 if (selected_coloring != NULL) {
173 selected_coloring->allocate(env);
177 static void dump(unsigned mask, ir_graph *irg,
178 const arch_register_class_t *cls,
181 if ((options.dump_flags & mask) == mask) {
184 snprintf(buf, sizeof(buf), "%s-%s", cls->name, suffix);
185 dump_ir_graph(irg, buf);
187 dump_ir_graph(irg, suffix);
193 * Checks for every reload if its user can perform the load on itself.
195 static void memory_operand_walker(ir_node *irn, void *env)
197 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(src, pos)) {
220 arch_perform_memory_operand(src, spill, pos);
224 /* kill the Reload */
225 if (get_irn_n_edges(irn) == 0) {
226 ir_graph *irg = get_irn_irg(irn);
228 set_irn_n(irn, be_pos_Reload_mem, new_r_Bad(irg));
229 set_irn_n(irn, be_pos_Reload_frame, new_r_Bad(irg));
234 * Starts a walk for memory operands if supported by the backend.
236 void check_for_memory_operands(ir_graph *irg)
238 irg_walk_graph(irg, NULL, memory_operand_walker, NULL);
242 static be_node_stats_t last_node_stats;
245 * Perform things which need to be done per register class before spilling.
247 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
249 be_chordal_env_t *chordal_env = &pse->cenv;
250 ir_graph *irg = pse->irg;
251 ir_exec_freq *exec_freq = be_get_irg_exec_freq(irg);
254 chordal_env->cls = cls;
255 chordal_env->border_heads = pmap_create();
256 chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
258 be_assure_liveness(irg);
259 be_liveness_assure_chk(be_get_irg_liveness(irg));
261 stat_ev_do(pse->pre_spill_cost = be_estimate_irg_costs(irg, exec_freq));
263 /* put all ignore registers into the ignore register set. */
264 be_put_ignore_regs(irg, pse->cls, chordal_env->ignore_colors);
266 be_timer_push(T_RA_CONSTR);
267 be_pre_spill_prepare_constr(irg, chordal_env->cls);
268 be_timer_pop(T_RA_CONSTR);
270 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "constr-pre");
274 * Perform things which need to be done per register class after spilling.
276 static void post_spill(post_spill_env_t *pse, int iteration)
278 be_chordal_env_t *chordal_env = &pse->cenv;
279 ir_graph *irg = pse->irg;
280 ir_exec_freq *exec_freq = be_get_irg_exec_freq(irg);
281 int colors_n = arch_register_class_n_regs(chordal_env->cls);
283 = colors_n - be_put_ignore_regs(irg, chordal_env->cls, NULL);
285 /* some special classes contain only ignore regs, no work to be done */
286 if (allocatable_regs > 0) {
287 stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg, exec_freq) - pse->pre_spill_cost);
290 If we have a backend provided spiller, post spill is
291 called in a loop after spilling for each register class.
292 But we only need to fix stack nodes once in this case.
294 be_timer_push(T_RA_SPILL_APPLY);
295 check_for_memory_operands(irg);
296 if (iteration == 0) {
297 be_abi_fix_stack_nodes(irg);
299 be_timer_pop(T_RA_SPILL_APPLY);
302 /* verify schedule and register pressure */
303 be_timer_push(T_VERIFY);
304 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
305 be_verify_schedule(irg);
306 be_verify_register_pressure(irg, pse->cls);
307 } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
308 assert(be_verify_schedule(irg) && "Schedule verification failed");
309 assert(be_verify_register_pressure(irg, pse->cls)
310 && "Register pressure verification failed");
312 be_timer_pop(T_VERIFY);
314 /* Color the graph. */
315 be_timer_push(T_RA_COLOR);
316 be_ra_chordal_coloring(chordal_env);
317 be_timer_pop(T_RA_COLOR);
319 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "color");
321 /* Create the ifg with the selected flavor */
322 be_timer_push(T_RA_IFG);
323 chordal_env->ifg = be_create_ifg(chordal_env);
324 be_timer_pop(T_RA_IFG);
328 be_node_stats_t node_stats;
330 be_ifg_stat(irg, chordal_env->ifg, &stat);
331 stat_ev_dbl("bechordal_ifg_nodes", stat.n_nodes);
332 stat_ev_dbl("bechordal_ifg_edges", stat.n_edges);
333 stat_ev_dbl("bechordal_ifg_comps", stat.n_comps);
335 be_collect_node_stats(&node_stats, irg);
336 be_subtract_node_stats(&node_stats, &last_node_stats);
338 stat_ev_dbl("bechordal_perms_before_coal",
339 node_stats[BE_STAT_PERMS]);
340 stat_ev_dbl("bechordal_copies_before_coal",
341 node_stats[BE_STAT_COPIES]);
344 be_timer_push(T_RA_COPYMIN);
345 if (minir_file[0] != '\0') {
348 if (strcmp(minir_file, "-") == 0) {
351 out = fopen(minir_file, "w");
353 panic("Cound't open minir output '%s'", minir_file);
357 be_export_minir(out, irg);
361 co_driver(chordal_env);
362 be_timer_pop(T_RA_COPYMIN);
364 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "copymin");
366 /* ssa destruction */
367 be_timer_push(T_RA_SSA);
368 be_ssa_destruction(chordal_env);
369 be_timer_pop(T_RA_SSA);
371 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "ssadestr");
373 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
374 be_timer_push(T_VERIFY);
375 be_ssa_destruction_check(chordal_env);
376 be_timer_pop(T_VERIFY);
379 /* the ifg exists only if there are allocatable regs */
380 be_ifg_free(chordal_env->ifg);
383 /* free some always allocated data structures */
384 pmap_destroy(chordal_env->border_heads);
385 bitset_free(chordal_env->ignore_colors);
389 * Performs chordal register allocation for each register class on given irg.
391 * @param irg the graph
392 * @return Structure containing timer for the single phases or NULL if no
395 static void be_ra_chordal_main(ir_graph *irg)
397 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
400 be_chordal_env_t chordal_env;
403 be_timer_push(T_RA_OTHER);
405 be_timer_push(T_RA_PROLOG);
407 be_assure_liveness(irg);
409 chordal_env.obst = &obst;
410 chordal_env.opts = &options;
411 chordal_env.irg = irg;
412 chordal_env.border_heads = NULL;
413 chordal_env.ifg = NULL;
414 chordal_env.ignore_colors = NULL;
418 be_timer_pop(T_RA_PROLOG);
421 be_collect_node_stats(&last_node_stats, irg);
424 /* use one of the generic spiller */
426 /* Perform the following for each register class. */
427 for (j = 0, m = arch_env->n_register_classes; j < m; ++j) {
428 post_spill_env_t pse;
429 const arch_register_class_t *cls = &arch_env->register_classes[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(irg, 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(irg, 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, irg);
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 be_timer_push(T_VERIFY);
466 if (chordal_env.opts->vrfy_option == BE_CH_VRFY_WARN) {
467 be_verify_register_allocation(irg);
468 } else if (chordal_env.opts->vrfy_option == BE_CH_VRFY_ASSERT) {
469 assert(be_verify_register_allocation(irg)
470 && "Register allocation invalid");
472 be_timer_pop(T_VERIFY);
474 be_timer_push(T_RA_EPILOG);
475 lower_nodes_after_ra(irg,
476 options.lower_perm_opt&BE_CH_LOWER_PERM_COPY ? 1 : 0);
477 dump(BE_CH_DUMP_LOWER, irg, NULL, "belower-after-ra");
479 obstack_free(&obst, NULL);
480 be_liveness_invalidate(be_get_irg_liveness(irg));
481 be_timer_pop(T_RA_EPILOG);
483 be_timer_pop(T_RA_OTHER);
486 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);
487 void be_init_chordal_main(void)
489 static be_ra_t be_ra_chordal_allocator = {
493 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
494 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
495 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
497 be_register_allocator("chordal", &be_ra_chordal_allocator);
499 lc_opt_add_table(chordal_grp, be_chordal_options);
500 be_add_module_list_opt(chordal_grp, "coloring", "select coloring methode", &colorings, (void**) &selected_coloring);