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"
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_mask_items_t dump_items[] = {
109 { "none", BE_CH_DUMP_NONE },
110 { "spill", BE_CH_DUMP_SPILL },
111 { "live", BE_CH_DUMP_LIVE },
112 { "color", BE_CH_DUMP_COLOR },
113 { "copymin", BE_CH_DUMP_COPYMIN },
114 { "ssadestr", BE_CH_DUMP_SSADESTR },
115 { "tree", BE_CH_DUMP_TREE_INTV },
116 { "constr", BE_CH_DUMP_CONSTR },
117 { "lower", BE_CH_DUMP_LOWER },
118 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
119 { "appel", BE_CH_DUMP_APPEL },
120 { "all", BE_CH_DUMP_ALL },
124 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
125 { "off", BE_CH_VRFY_OFF },
126 { "warn", BE_CH_VRFY_WARN },
127 { "assert", BE_CH_VRFY_ASSERT },
131 static lc_opt_enum_int_var_t lower_perm_var = {
132 &options.lower_perm_opt, lower_perm_items
135 static lc_opt_enum_mask_var_t dump_var = {
136 &options.dump_flags, dump_items
139 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
140 &options.vrfy_option, be_ch_vrfy_items
143 static const lc_opt_table_entry_t be_chordal_options[] = {
144 LC_OPT_ENT_ENUM_INT ("perm", "perm lowering options", &lower_perm_var),
145 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
146 LC_OPT_ENT_ENUM_INT ("verify", "verify options", &be_ch_vrfy_var),
150 static be_module_list_entry_t *colorings = NULL;
151 static const be_ra_chordal_coloring_t *selected_coloring = NULL;
153 void be_register_chordal_coloring(const char *name, be_ra_chordal_coloring_t *coloring)
155 if (selected_coloring == NULL)
156 selected_coloring = coloring;
158 be_add_module_to_list(&colorings, name, coloring);
161 static void be_ra_chordal_coloring(be_chordal_env_t *env)
163 selected_coloring->allocate(env);
166 static void dump(unsigned mask, ir_graph *irg,
167 const arch_register_class_t *cls,
170 if ((options.dump_flags & mask) == mask) {
173 snprintf(buf, sizeof(buf), "%s-%s", cls->name, suffix);
174 dump_ir_graph(irg, buf);
176 dump_ir_graph(irg, suffix);
182 * Post-Walker: Checks for the given reload if has only one user that can perform the
183 * reload as part of its address mode.
184 * Fold the reload into the user it that is possible.
186 static void memory_operand_walker(ir_node *irn, void *env)
188 const ir_edge_t *edge, *ne;
194 if (! be_is_Reload(irn))
197 /* only use memory operands, if the reload is only used by 1 node */
198 if (get_irn_n_edges(irn) > 1)
201 spill = be_get_Reload_mem(irn);
202 block = get_nodes_block(irn);
204 foreach_out_edge_safe(irn, edge, ne) {
205 ir_node *src = get_edge_src_irn(edge);
206 int pos = get_edge_src_pos(edge);
208 assert(src && "outedges broken!");
210 if (get_nodes_block(src) == block && arch_possible_memory_operand(src, pos)) {
211 arch_perform_memory_operand(src, spill, pos);
215 /* kill the Reload if it was folded */
216 if (get_irn_n_edges(irn) == 0) {
217 ir_graph *irg = get_irn_irg(irn);
218 ir_mode *frame_mode = get_irn_mode(get_irn_n(irn, n_be_Reload_frame));
220 set_irn_n(irn, n_be_Reload_mem, new_r_Bad(irg, mode_X));
221 set_irn_n(irn, n_be_Reload_frame, new_r_Bad(irg, frame_mode));
226 * Starts a walk for memory operands if supported by the backend.
228 void check_for_memory_operands(ir_graph *irg)
230 irg_walk_graph(irg, NULL, memory_operand_walker, NULL);
234 static be_node_stats_t last_node_stats;
237 * Perform things which need to be done per register class before spilling.
239 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
241 be_chordal_env_t *chordal_env = &pse->cenv;
242 ir_graph *irg = pse->irg;
243 ir_exec_freq *exec_freq = be_get_irg_exec_freq(irg);
246 chordal_env->cls = cls;
247 chordal_env->border_heads = pmap_create();
248 chordal_env->allocatable_regs = bitset_malloc(chordal_env->cls->n_regs);
250 be_assure_liveness(irg);
251 be_liveness_assure_chk(be_get_irg_liveness(irg));
253 if (stat_ev_enabled) {
254 pse->pre_spill_cost = be_estimate_irg_costs(irg, exec_freq);
257 /* put all ignore registers into the ignore register set. */
258 be_put_allocatable_regs(irg, pse->cls, chordal_env->allocatable_regs);
260 be_timer_push(T_RA_CONSTR);
261 be_pre_spill_prepare_constr(irg, chordal_env->cls);
262 be_timer_pop(T_RA_CONSTR);
264 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "constr-pre");
268 * Perform things which need to be done per register class after spilling.
270 static void post_spill(post_spill_env_t *pse, int iteration)
272 be_chordal_env_t *chordal_env = &pse->cenv;
273 ir_graph *irg = pse->irg;
274 ir_exec_freq *exec_freq = be_get_irg_exec_freq(irg);
275 int allocatable_regs = be_get_n_allocatable_regs(irg, chordal_env->cls);
277 /* some special classes contain only ignore regs, no work to be done */
278 if (allocatable_regs > 0) {
279 stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg, exec_freq) - pse->pre_spill_cost);
282 If we have a backend provided spiller, post spill is
283 called in a loop after spilling for each register class.
284 But we only need to fix stack nodes once in this case.
286 be_timer_push(T_RA_SPILL_APPLY);
287 check_for_memory_operands(irg);
288 if (iteration == 0) {
289 be_abi_fix_stack_nodes(irg);
291 be_timer_pop(T_RA_SPILL_APPLY);
294 /* verify schedule and register pressure */
295 be_timer_push(T_VERIFY);
296 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
297 be_verify_schedule(irg);
298 be_verify_register_pressure(irg, pse->cls);
299 } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
300 assert(be_verify_schedule(irg) && "Schedule verification failed");
301 assert(be_verify_register_pressure(irg, pse->cls)
302 && "Register pressure verification failed");
304 be_timer_pop(T_VERIFY);
306 /* Color the graph. */
307 be_timer_push(T_RA_COLOR);
308 be_ra_chordal_coloring(chordal_env);
309 be_timer_pop(T_RA_COLOR);
311 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "color");
313 /* Create the ifg with the selected flavor */
314 be_timer_push(T_RA_IFG);
315 chordal_env->ifg = be_create_ifg(chordal_env);
316 be_timer_pop(T_RA_IFG);
318 if (stat_ev_enabled) {
320 be_node_stats_t node_stats;
322 be_ifg_stat(irg, chordal_env->ifg, &stat);
323 stat_ev_dbl("bechordal_ifg_nodes", stat.n_nodes);
324 stat_ev_dbl("bechordal_ifg_edges", stat.n_edges);
325 stat_ev_dbl("bechordal_ifg_comps", stat.n_comps);
327 be_collect_node_stats(&node_stats, irg);
328 be_subtract_node_stats(&node_stats, &last_node_stats);
330 stat_ev_dbl("bechordal_perms_before_coal",
331 node_stats[BE_STAT_PERMS]);
332 stat_ev_dbl("bechordal_copies_before_coal",
333 node_stats[BE_STAT_COPIES]);
336 be_timer_push(T_RA_COPYMIN);
337 co_driver(chordal_env);
338 be_timer_pop(T_RA_COPYMIN);
340 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "copymin");
342 /* ssa destruction */
343 be_timer_push(T_RA_SSA);
344 be_ssa_destruction(chordal_env);
345 be_timer_pop(T_RA_SSA);
347 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "ssadestr");
349 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
350 be_timer_push(T_VERIFY);
351 be_ssa_destruction_check(chordal_env);
352 be_timer_pop(T_VERIFY);
355 /* the ifg exists only if there are allocatable regs */
356 be_ifg_free(chordal_env->ifg);
359 /* free some always allocated data structures */
360 pmap_destroy(chordal_env->border_heads);
361 bitset_free(chordal_env->allocatable_regs);
365 * Performs chordal register allocation for each register class on given irg.
367 * @param irg the graph
368 * @return Structure containing timer for the single phases or NULL if no
371 static void be_ra_chordal_main(ir_graph *irg)
373 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
376 be_chordal_env_t chordal_env;
379 be_timer_push(T_RA_OTHER);
381 be_timer_push(T_RA_PROLOG);
383 be_assure_liveness(irg);
385 chordal_env.obst = &obst;
386 chordal_env.opts = &options;
387 chordal_env.irg = irg;
388 chordal_env.border_heads = NULL;
389 chordal_env.ifg = NULL;
390 chordal_env.allocatable_regs = NULL;
394 be_timer_pop(T_RA_PROLOG);
396 if (stat_ev_enabled) {
397 be_collect_node_stats(&last_node_stats, irg);
400 /* use one of the generic spiller */
402 /* Perform the following for each register class. */
403 for (j = 0, m = arch_env->n_register_classes; j < m; ++j) {
404 post_spill_env_t pse;
405 const arch_register_class_t *cls = &arch_env->register_classes[j];
407 if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
411 stat_ev_ctx_push_str("bechordal_cls", cls->name);
413 if (stat_ev_enabled) {
414 be_do_stat_reg_pressure(irg, cls);
417 pse.cenv = chordal_env;
419 pre_spill(&pse, cls);
421 be_timer_push(T_RA_SPILL);
422 be_do_spill(irg, cls);
423 be_timer_pop(T_RA_SPILL);
425 dump(BE_CH_DUMP_SPILL, irg, pse.cls, "spill");
429 if (stat_ev_enabled) {
430 be_node_stats_t node_stats;
432 be_collect_node_stats(&node_stats, irg);
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 be_timer_push(T_VERIFY);
442 if (chordal_env.opts->vrfy_option == BE_CH_VRFY_WARN) {
443 be_verify_register_allocation(irg);
444 } else if (chordal_env.opts->vrfy_option == BE_CH_VRFY_ASSERT) {
445 assert(be_verify_register_allocation(irg)
446 && "Register allocation invalid");
448 be_timer_pop(T_VERIFY);
450 be_timer_push(T_RA_EPILOG);
451 lower_nodes_after_ra(irg,
452 options.lower_perm_opt&BE_CH_LOWER_PERM_COPY ? 1 : 0);
453 dump(BE_CH_DUMP_LOWER, irg, NULL, "belower-after-ra");
455 obstack_free(&obst, NULL);
456 be_liveness_invalidate(be_get_irg_liveness(irg));
457 be_timer_pop(T_RA_EPILOG);
459 be_timer_pop(T_RA_OTHER);
462 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main)
463 void be_init_chordal_main(void)
465 static be_ra_t be_ra_chordal_allocator = {
469 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
470 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
471 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
473 be_register_allocator("chordal", &be_ra_chordal_allocator);
475 lc_opt_add_table(chordal_grp, be_chordal_options);
476 be_add_module_list_opt(chordal_grp, "coloring", "select coloring methode", &colorings, (void**) &selected_coloring);