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"
57 #include "bechordal_t.h"
61 #include "besched_t.h"
65 #include "beifg_impl.h"
67 #include "bestatevent.h"
74 #include "bespillslots.h"
75 #include "bespilloptions.h"
78 #include "becopystat.h"
79 #include "becopyopt.h"
80 #include "bessadestr.h"
84 static be_ra_chordal_opts_t options = {
86 BE_CH_LOWER_PERM_SWAP,
92 typedef struct _post_spill_env_t {
93 be_chordal_env_t cenv;
95 const arch_register_class_t *cls;
96 double pre_spill_cost;
99 static const lc_opt_enum_int_items_t lower_perm_items[] = {
100 { "copy", BE_CH_LOWER_PERM_COPY },
101 { "swap", BE_CH_LOWER_PERM_SWAP },
105 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
109 static const lc_opt_enum_int_items_t dump_items[] = {
110 { "none", BE_CH_DUMP_NONE },
111 { "spill", BE_CH_DUMP_SPILL },
112 { "live", BE_CH_DUMP_LIVE },
113 { "color", BE_CH_DUMP_COLOR },
114 { "copymin", BE_CH_DUMP_COPYMIN },
115 { "ssadestr", BE_CH_DUMP_SSADESTR },
116 { "tree", BE_CH_DUMP_TREE_INTV },
117 { "constr", BE_CH_DUMP_CONSTR },
118 { "lower", BE_CH_DUMP_LOWER },
119 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
120 { "appel", BE_CH_DUMP_APPEL },
121 { "all", BE_CH_DUMP_ALL },
125 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
126 { "off", BE_CH_VRFY_OFF },
127 { "warn", BE_CH_VRFY_WARN },
128 { "assert", BE_CH_VRFY_ASSERT },
132 static lc_opt_enum_int_var_t lower_perm_var = {
133 &options.lower_perm_opt, lower_perm_items
136 static lc_opt_enum_int_var_t dump_var = {
137 &options.dump_flags, dump_items
140 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
141 &options.vrfy_option, be_ch_vrfy_items
144 static const lc_opt_table_entry_t be_chordal_options[] = {
145 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options", &lower_perm_var),
146 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
147 LC_OPT_ENT_ENUM_PTR ("verify", "verify options", &be_ch_vrfy_var),
151 static void dump(unsigned mask, ir_graph *irg,
152 const arch_register_class_t *cls,
154 void (*dump_func)(ir_graph *, const char *))
156 if((options.dump_flags & mask) == mask) {
159 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
160 be_dump(irg, buf, dump_func);
163 be_dump(irg, suffix, dump_func);
168 * Checks for every reload if its user can perform the load on itself.
170 static void memory_operand_walker(ir_node *irn, void *env)
172 const ir_edge_t *edge, *ne;
178 if (! be_is_Reload(irn))
181 /* only use memory operands, if the reload is only used by 1 node */
182 if(get_irn_n_edges(irn) > 1)
185 spill = be_get_Reload_mem(irn);
186 block = get_nodes_block(irn);
188 foreach_out_edge_safe(irn, edge, ne) {
189 ir_node *src = get_edge_src_irn(edge);
190 int pos = get_edge_src_pos(edge);
192 assert(src && "outedges broken!");
194 if (get_nodes_block(src) == block && arch_possible_memory_operand(src, pos)) {
195 arch_perform_memory_operand(src, spill, pos);
199 /* kill the Reload */
200 if (get_irn_n_edges(irn) == 0) {
202 set_irn_n(irn, be_pos_Reload_mem, new_Bad());
203 set_irn_n(irn, be_pos_Reload_frame, new_Bad());
208 * Starts a walk for memory operands if supported by the backend.
210 static inline void check_for_memory_operands(ir_graph *irg)
212 irg_walk_graph(irg, NULL, memory_operand_walker, NULL);
216 static be_node_stats_t last_node_stats;
219 * Perform things which need to be done per register class before spilling.
221 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
223 be_chordal_env_t *chordal_env = &pse->cenv;
224 be_irg_t *birg = pse->birg;
225 ir_graph *irg = be_get_birg_irg(birg);
228 chordal_env->cls = cls;
229 chordal_env->border_heads = pmap_create();
230 chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
232 be_assure_liveness(birg);
233 be_liveness_assure_chk(be_get_birg_liveness(birg));
235 stat_ev_do(pse->pre_spill_cost = be_estimate_irg_costs(irg, birg->exec_freq));
237 /* put all ignore registers into the ignore register set. */
238 be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
240 BE_TIMER_PUSH(t_ra_constr);
241 be_pre_spill_prepare_constr(chordal_env);
242 BE_TIMER_POP(t_ra_constr);
244 dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
248 * Perform things which need to be done per register class after spilling.
250 static void post_spill(post_spill_env_t *pse, int iteration) {
251 be_chordal_env_t *chordal_env = &pse->cenv;
252 be_irg_t *birg = pse->birg;
253 ir_graph *irg = birg->irg;
254 int colors_n = arch_register_class_n_regs(chordal_env->cls);
255 int allocatable_regs = colors_n - be_put_ignore_regs(birg, chordal_env->cls, NULL);
257 /* some special classes contain only ignore regs, no work to be done */
258 if (allocatable_regs > 0) {
259 stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg, birg->exec_freq) - pse->pre_spill_cost);
262 If we have a backend provided spiller, post spill is
263 called in a loop after spilling for each register class.
264 But we only need to fix stack nodes once in this case.
266 BE_TIMER_PUSH(t_ra_spill_apply);
267 check_for_memory_operands(irg);
268 if (iteration == 0) {
269 be_abi_fix_stack_nodes(birg->abi);
271 BE_TIMER_POP(t_ra_spill_apply);
274 /* verify schedule and register pressure */
275 BE_TIMER_PUSH(t_verify);
276 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
277 be_verify_schedule(birg);
278 be_verify_register_pressure(birg, pse->cls, irg);
279 } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
280 assert(be_verify_schedule(birg) && "Schedule verification failed");
281 assert(be_verify_register_pressure(birg, pse->cls, irg)
282 && "Register pressure verification failed");
284 BE_TIMER_POP(t_verify);
286 /* Color the graph. */
287 BE_TIMER_PUSH(t_ra_color);
288 be_ra_chordal_color(chordal_env);
289 BE_TIMER_POP(t_ra_color);
291 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
293 /* Create the ifg with the selected flavor */
294 BE_TIMER_PUSH(t_ra_ifg);
295 chordal_env->ifg = be_create_ifg(chordal_env);
296 BE_TIMER_POP(t_ra_ifg);
300 be_node_stats_t node_stats;
302 be_ifg_stat(birg, chordal_env->ifg, &stat);
303 stat_ev_dbl("bechordal_ifg_nodes", stat.n_nodes);
304 stat_ev_dbl("bechordal_ifg_edges", stat.n_edges);
305 stat_ev_dbl("bechordal_ifg_comps", stat.n_comps);
307 be_collect_node_stats(&node_stats, birg);
308 be_subtract_node_stats(&node_stats, &last_node_stats);
310 stat_ev_dbl("bechordal_perms_before_coal",
311 node_stats[BE_STAT_PERMS]);
312 stat_ev_dbl("bechordal_copies_before_coal",
313 node_stats[BE_STAT_COPIES]);
316 /* copy minimization */
317 BE_TIMER_PUSH(t_ra_copymin);
318 co_driver(chordal_env);
319 BE_TIMER_POP(t_ra_copymin);
321 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "-copymin", dump_ir_block_graph_sched);
324 /* ssa destruction */
325 BE_TIMER_PUSH(t_ra_ssa);
326 be_ssa_destruction(chordal_env);
327 BE_TIMER_POP(t_ra_ssa);
329 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
331 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
332 BE_TIMER_PUSH(t_verify);
333 be_ssa_destruction_check(chordal_env);
334 BE_TIMER_POP(t_verify);
337 /* the ifg exists only if there are allocatable regs */
338 be_ifg_free(chordal_env->ifg);
341 /* free some always allocated data structures */
342 pmap_destroy(chordal_env->border_heads);
343 bitset_free(chordal_env->ignore_colors);
347 * Performs chordal register allocation for each register class on given irg.
349 * @param birg Backend irg object
350 * @return Structure containing timer for the single phases or NULL if no timing requested.
352 static void be_ra_chordal_main(be_irg_t *birg)
354 const arch_env_t *arch_env = birg->main_env->arch_env;
355 ir_graph *irg = birg->irg;
358 be_chordal_env_t chordal_env;
361 BE_TIMER_PUSH(t_ra_other);
363 BE_TIMER_PUSH(t_ra_prolog);
365 be_assure_liveness(birg);
367 chordal_env.obst = &obst;
368 chordal_env.opts = &options;
369 chordal_env.irg = irg;
370 chordal_env.birg = birg;
371 chordal_env.border_heads = NULL;
372 chordal_env.ifg = NULL;
373 chordal_env.ignore_colors = NULL;
377 BE_TIMER_POP(t_ra_prolog);
380 be_collect_node_stats(&last_node_stats, birg);
383 if (! arch_code_generator_has_spiller(birg->cg)) {
384 /* use one of the generic spiller */
386 /* Perform the following for each register class. */
387 for (j = 0, m = arch_env_get_n_reg_class(arch_env); j < m; ++j) {
388 post_spill_env_t pse;
389 const arch_register_class_t *cls
390 = arch_env_get_reg_class(arch_env, j);
392 if(arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
396 stat_ev_ctx_push_str("bechordal_cls", cls->name);
399 be_do_stat_reg_pressure(birg, cls);
402 memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
404 pre_spill(&pse, cls);
406 BE_TIMER_PUSH(t_ra_spill);
407 be_do_spill(birg, cls);
408 BE_TIMER_POP(t_ra_spill);
410 dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill",
411 dump_ir_block_graph_sched);
416 be_node_stats_t node_stats;
418 be_collect_node_stats(&node_stats, birg);
419 be_subtract_node_stats(&node_stats, &last_node_stats);
420 be_emit_node_stats(&node_stats, "bechordal_");
422 be_copy_node_stats(&last_node_stats, &node_stats);
423 stat_ev_ctx_pop("bechordal_cls");
427 post_spill_env_t *pse;
429 /* the backend has its own spiller */
430 m = arch_env_get_n_reg_class(arch_env);
432 pse = ALLOCAN(post_spill_env_t, m);
434 for (j = 0; j < m; ++j) {
435 memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
437 pre_spill(&pse[j], pse[j].cls);
440 BE_TIMER_PUSH(t_ra_spill);
441 arch_code_generator_spill(birg->cg, birg);
442 BE_TIMER_POP(t_ra_spill);
443 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
445 for (j = 0; j < m; ++j) {
446 post_spill(&pse[j], j);
450 BE_TIMER_PUSH(t_verify);
451 if (chordal_env.opts->vrfy_option == BE_CH_VRFY_WARN) {
452 be_verify_register_allocation(birg);
453 } else if(chordal_env.opts->vrfy_option == BE_CH_VRFY_ASSERT) {
454 assert(be_verify_register_allocation(birg)
455 && "Register allocation invalid");
457 BE_TIMER_POP(t_verify);
459 BE_TIMER_PUSH(t_ra_epilog);
460 lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
461 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
463 obstack_free(&obst, NULL);
464 be_liveness_invalidate(be_get_birg_liveness(birg));
465 BE_TIMER_POP(t_ra_epilog);
467 BE_TIMER_POP(t_ra_other);
470 static be_ra_t be_ra_chordal_allocator = {
474 void be_init_chordal_main(void)
476 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
477 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
478 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
480 lc_opt_add_table(chordal_grp, be_chordal_options);
482 be_register_allocator("chordal", &be_ra_chordal_allocator);
485 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);