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 static be_node_stats_t last_node_stats;
229 * Perform things which need to be done per register class before spilling.
231 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
233 be_chordal_env_t *chordal_env = &pse->cenv;
234 be_irg_t *birg = pse->birg;
235 ir_graph *irg = be_get_birg_irg(birg);
236 const be_main_env_t *main_env = birg->main_env;
239 chordal_env->cls = cls;
240 chordal_env->border_heads = pmap_create();
241 chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
243 be_assure_liveness(birg);
244 be_liveness_assure_chk(be_get_birg_liveness(birg));
246 stat_ev_do(pse->pre_spill_cost = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq));
248 /* put all ignore registers into the ignore register set. */
249 be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
251 BE_TIMER_PUSH(t_ra_constr);
252 be_pre_spill_prepare_constr(chordal_env);
253 BE_TIMER_POP(t_ra_constr);
255 dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
259 * Perform things which need to be done per register class after spilling.
261 static void post_spill(post_spill_env_t *pse, int iteration) {
262 be_chordal_env_t *chordal_env = &pse->cenv;
263 be_irg_t *birg = pse->birg;
264 ir_graph *irg = birg->irg;
265 const be_main_env_t *main_env = birg->main_env;
266 int colors_n = arch_register_class_n_regs(chordal_env->cls);
267 int allocatable_regs = colors_n - be_put_ignore_regs(birg, chordal_env->cls, NULL);
269 /* some special classes contain only ignore regs, no work to be done */
270 if (allocatable_regs > 0) {
271 stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost);
274 If we have a backend provided spiller, post spill is
275 called in a loop after spilling for each register class.
276 But we only need to fix stack nodes once in this case.
278 BE_TIMER_PUSH(t_ra_spill_apply);
279 check_for_memory_operands(chordal_env);
280 if (iteration == 0) {
281 be_abi_fix_stack_nodes(birg->abi);
283 BE_TIMER_POP(t_ra_spill_apply);
285 BE_TIMER_PUSH(t_verify);
287 /* verify schedule and register pressure */
288 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
289 be_verify_schedule(birg);
290 be_verify_register_pressure(birg, pse->cls, irg);
291 } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
292 assert(be_verify_schedule(birg) && "Schedule verification failed");
293 assert(be_verify_register_pressure(birg, pse->cls, irg)
294 && "Register pressure verification failed");
296 BE_TIMER_POP(t_verify);
298 /* Color the graph. */
299 BE_TIMER_PUSH(t_ra_color);
300 be_ra_chordal_color(chordal_env);
301 BE_TIMER_POP(t_ra_color);
303 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
305 /* Create the ifg with the selected flavor */
306 BE_TIMER_PUSH(t_ra_ifg);
307 chordal_env->ifg = be_create_ifg(chordal_env);
308 BE_TIMER_POP(t_ra_ifg);
312 be_node_stats_t node_stats;
314 be_ifg_stat(birg, chordal_env->ifg, &stat);
315 stat_ev_dbl("bechordal_ifg_nodes", stat.n_nodes);
316 stat_ev_dbl("bechordal_ifg_edges", stat.n_edges);
317 stat_ev_dbl("bechordal_ifg_comps", stat.n_comps);
319 be_collect_node_stats(&node_stats, birg);
320 be_subtract_node_stats(&node_stats, &last_node_stats);
322 stat_ev_dbl("bechordal_perms_before_coal",
323 node_stats[BE_STAT_PERMS]);
324 stat_ev_dbl("bechordal_copies_before_coal",
325 node_stats[BE_STAT_COPIES]);
328 /* copy minimization */
329 BE_TIMER_PUSH(t_ra_copymin);
330 co_driver(chordal_env);
331 BE_TIMER_POP(t_ra_copymin);
333 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "-copymin", dump_ir_block_graph_sched);
336 /* ssa destruction */
337 BE_TIMER_PUSH(t_ra_ssa);
338 be_ssa_destruction(chordal_env);
339 BE_TIMER_POP(t_ra_ssa);
341 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
343 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
344 BE_TIMER_PUSH(t_verify);
345 be_ssa_destruction_check(chordal_env);
346 BE_TIMER_POP(t_verify);
349 /* the ifg exists only if there are allocatable regs */
350 be_ifg_free(chordal_env->ifg);
353 /* free some always allocated data structures */
354 pmap_destroy(chordal_env->border_heads);
355 bitset_free(chordal_env->ignore_colors);
359 * Performs chordal register allocation for each register class on given irg.
361 * @param birg Backend irg object
362 * @return Structure containing timer for the single phases or NULL if no timing requested.
364 static void be_ra_chordal_main(be_irg_t *birg)
366 const be_main_env_t *main_env = birg->main_env;
367 const arch_env_t *arch_env = main_env->arch_env;
368 ir_graph *irg = birg->irg;
370 be_chordal_env_t chordal_env;
373 main_opts = main_env->options;
375 BE_TIMER_PUSH(t_ra_other);
377 BE_TIMER_PUSH(t_ra_prolog);
379 be_assure_liveness(birg);
381 chordal_env.obst = &obst;
382 chordal_env.opts = &options;
383 chordal_env.irg = irg;
384 chordal_env.birg = birg;
385 chordal_env.border_heads = NULL;
386 chordal_env.ifg = NULL;
387 chordal_env.ignore_colors = NULL;
391 BE_TIMER_POP(t_ra_prolog);
394 be_collect_node_stats(&last_node_stats, birg);
397 if (! arch_code_generator_has_spiller(birg->cg)) {
398 /* use one of the generic spiller */
400 /* Perform the following for each register class. */
401 for (j = 0, m = arch_env_get_n_reg_class(arch_env); j < m; ++j) {
402 post_spill_env_t pse;
403 const arch_register_class_t *cls
404 = arch_env_get_reg_class(arch_env, j);
406 if(arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
410 stat_ev_ctx_push_str("bechordal_cls", cls->name);
413 be_do_stat_reg_pressure(birg, cls);
416 memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
418 pre_spill(&pse, cls);
420 BE_TIMER_PUSH(t_ra_spill);
421 be_do_spill(birg, cls);
422 BE_TIMER_POP(t_ra_spill);
424 dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill",
425 dump_ir_block_graph_sched);
430 be_node_stats_t node_stats;
432 be_collect_node_stats(&node_stats, birg);
433 be_subtract_node_stats(&node_stats, &last_node_stats);
434 be_emit_node_stats(&node_stats, "bechordal_");
436 be_copy_node_stats(&last_node_stats, &node_stats);
437 stat_ev_ctx_pop("bechordal_cls");
441 post_spill_env_t *pse;
443 /* the backend has its own spiller */
444 m = arch_env_get_n_reg_class(arch_env);
446 pse = alloca(m * sizeof(pse[0]));
448 for (j = 0; j < m; ++j) {
449 memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
451 pre_spill(&pse[j], pse[j].cls);
454 BE_TIMER_PUSH(t_ra_spill);
455 arch_code_generator_spill(birg->cg, birg);
456 BE_TIMER_POP(t_ra_spill);
457 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
459 for (j = 0; j < m; ++j) {
460 post_spill(&pse[j], j);
464 BE_TIMER_PUSH(t_verify);
465 be_verify_register_allocation(birg);
466 BE_TIMER_POP(t_verify);
468 BE_TIMER_PUSH(t_ra_epilog);
469 lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
470 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
472 obstack_free(&obst, NULL);
473 be_liveness_invalidate(be_get_birg_liveness(birg));
474 BE_TIMER_POP(t_ra_epilog);
476 BE_TIMER_POP(t_ra_other);
479 static be_ra_t be_ra_chordal_allocator = {
483 void be_init_chordal_main(void)
485 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
486 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
487 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
489 lc_opt_add_table(chordal_grp, be_chordal_options);
491 be_register_allocator("chordal", &be_ra_chordal_allocator);
494 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);