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
37 #include "lc_opts_enum.h"
41 #include "irgraph_t.h"
53 #include "iredges_t.h"
56 #include "bechordal_t.h"
73 #include "bespillslots.h"
77 #include "becopystat.h"
78 #include "becopyopt.h"
79 #include "bessadestr.h"
83 #include "bepbqpcoloring.h"
85 static be_ra_chordal_opts_t options = {
87 BE_CH_LOWER_PERM_SWAP,
91 static const lc_opt_enum_int_items_t lower_perm_items[] = {
92 { "copy", BE_CH_LOWER_PERM_COPY },
93 { "swap", BE_CH_LOWER_PERM_SWAP },
97 static const lc_opt_enum_mask_items_t dump_items[] = {
98 { "none", BE_CH_DUMP_NONE },
99 { "spill", BE_CH_DUMP_SPILL },
100 { "live", BE_CH_DUMP_LIVE },
101 { "color", BE_CH_DUMP_COLOR },
102 { "copymin", BE_CH_DUMP_COPYMIN },
103 { "ssadestr", BE_CH_DUMP_SSADESTR },
104 { "tree", BE_CH_DUMP_TREE_INTV },
105 { "split", BE_CH_DUMP_SPLIT },
106 { "constr", BE_CH_DUMP_CONSTR },
107 { "lower", BE_CH_DUMP_LOWER },
108 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
109 { "appel", BE_CH_DUMP_APPEL },
110 { "all", BE_CH_DUMP_ALL },
114 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
115 { "off", BE_CH_VRFY_OFF },
116 { "warn", BE_CH_VRFY_WARN },
117 { "assert", BE_CH_VRFY_ASSERT },
121 static lc_opt_enum_int_var_t lower_perm_var = {
122 &options.lower_perm_opt, lower_perm_items
125 static lc_opt_enum_mask_var_t dump_var = {
126 &options.dump_flags, dump_items
129 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
130 &options.vrfy_option, be_ch_vrfy_items
133 static const lc_opt_table_entry_t be_chordal_options[] = {
134 LC_OPT_ENT_ENUM_INT ("perm", "perm lowering options", &lower_perm_var),
135 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
136 LC_OPT_ENT_ENUM_INT ("verify", "verify options", &be_ch_vrfy_var),
140 static be_module_list_entry_t *colorings = NULL;
141 static const be_ra_chordal_coloring_t *selected_coloring = NULL;
143 void be_register_chordal_coloring(const char *name, be_ra_chordal_coloring_t *coloring)
145 if (selected_coloring == NULL)
146 selected_coloring = coloring;
148 be_add_module_to_list(&colorings, name, coloring);
151 static void be_ra_chordal_coloring(be_chordal_env_t *env)
153 selected_coloring->allocate(env);
156 static void dump(unsigned mask, ir_graph *irg,
157 const arch_register_class_t *cls,
160 if ((options.dump_flags & mask) == mask) {
163 snprintf(buf, sizeof(buf), "%s-%s", cls->name, suffix);
164 dump_ir_graph(irg, buf);
166 dump_ir_graph(irg, suffix);
172 * Post-Walker: Checks for the given reload if has only one user that can perform the
173 * reload as part of its address mode.
174 * Fold the reload into the user it that is possible.
176 static void memory_operand_walker(ir_node *irn, void *env)
183 if (! be_is_Reload(irn))
186 /* only use memory operands, if the reload is only used by 1 node */
187 if (get_irn_n_edges(irn) > 1)
190 spill = be_get_Reload_mem(irn);
191 block = get_nodes_block(irn);
193 foreach_out_edge_safe(irn, edge) {
194 ir_node *src = get_edge_src_irn(edge);
195 int pos = get_edge_src_pos(edge);
197 assert(src && "outedges broken!");
199 if (get_nodes_block(src) == block && arch_possible_memory_operand(src, pos)) {
200 arch_perform_memory_operand(src, spill, pos);
204 /* kill the Reload if it was folded */
205 if (get_irn_n_edges(irn) == 0) {
206 ir_graph *irg = get_irn_irg(irn);
207 ir_mode *frame_mode = get_irn_mode(get_irn_n(irn, n_be_Reload_frame));
209 set_irn_n(irn, n_be_Reload_mem, new_r_Bad(irg, mode_X));
210 set_irn_n(irn, n_be_Reload_frame, new_r_Bad(irg, frame_mode));
215 * Starts a walk for memory operands if supported by the backend.
217 void check_for_memory_operands(ir_graph *irg)
219 irg_walk_graph(irg, NULL, memory_operand_walker, NULL);
223 static be_node_stats_t last_node_stats;
226 * Perform things which need to be done per register class before spilling.
228 static void pre_spill(be_chordal_env_t *const chordal_env, arch_register_class_t const *const cls, ir_graph *const irg)
230 chordal_env->cls = cls;
231 chordal_env->border_heads = pmap_create();
232 chordal_env->allocatable_regs = bitset_malloc(chordal_env->cls->n_regs);
234 be_assure_live_chk(irg);
236 /* put all ignore registers into the ignore register set. */
237 be_put_allocatable_regs(irg, cls, chordal_env->allocatable_regs);
239 be_timer_push(T_RA_CONSTR);
240 be_pre_spill_prepare_constr(irg, chordal_env->cls);
241 be_timer_pop(T_RA_CONSTR);
243 dump(BE_CH_DUMP_CONSTR, irg, cls, "constr-pre");
247 * Perform things which need to be done per register class after spilling.
249 static void post_spill(be_chordal_env_t *const chordal_env, ir_graph *const irg)
251 /* some special classes contain only ignore regs, no work to be done */
252 int const allocatable_regs = be_get_n_allocatable_regs(irg, chordal_env->cls);
253 if (allocatable_regs > 0) {
255 If we have a backend provided spiller, post spill is
256 called in a loop after spilling for each register class.
257 But we only need to fix stack nodes once in this case.
259 be_timer_push(T_RA_SPILL_APPLY);
260 check_for_memory_operands(irg);
261 be_abi_fix_stack_nodes(irg);
262 be_timer_pop(T_RA_SPILL_APPLY);
265 /* verify schedule and register pressure */
266 be_timer_push(T_VERIFY);
267 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
268 be_verify_schedule(irg);
269 be_verify_register_pressure(irg, chordal_env->cls);
270 } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
271 assert(be_verify_schedule(irg) && "Schedule verification failed");
272 assert(be_verify_register_pressure(irg, chordal_env->cls)
273 && "Register pressure verification failed");
275 be_timer_pop(T_VERIFY);
277 /* Color the graph. */
278 be_timer_push(T_RA_COLOR);
279 be_ra_chordal_coloring(chordal_env);
280 be_timer_pop(T_RA_COLOR);
282 dump(BE_CH_DUMP_CONSTR, irg, chordal_env->cls, "color");
284 /* Create the ifg with the selected flavor */
285 be_timer_push(T_RA_IFG);
286 chordal_env->ifg = be_create_ifg(chordal_env);
287 be_timer_pop(T_RA_IFG);
289 if (stat_ev_enabled) {
291 be_node_stats_t node_stats;
293 be_ifg_stat(irg, chordal_env->ifg, &stat);
294 stat_ev_dbl("bechordal_ifg_nodes", stat.n_nodes);
295 stat_ev_dbl("bechordal_ifg_edges", stat.n_edges);
296 stat_ev_dbl("bechordal_ifg_comps", stat.n_comps);
298 be_collect_node_stats(&node_stats, irg);
299 be_subtract_node_stats(&node_stats, &last_node_stats);
301 stat_ev_dbl("bechordal_perms_before_coal",
302 node_stats[BE_STAT_PERMS]);
303 stat_ev_dbl("bechordal_copies_before_coal",
304 node_stats[BE_STAT_COPIES]);
307 be_timer_push(T_RA_COPYMIN);
308 co_driver(chordal_env);
309 be_timer_pop(T_RA_COPYMIN);
311 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env->cls, "copymin");
313 /* ssa destruction */
314 be_timer_push(T_RA_SSA);
315 be_ssa_destruction(chordal_env);
316 be_timer_pop(T_RA_SSA);
318 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env->cls, "ssadestr");
320 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
321 be_timer_push(T_VERIFY);
322 be_ssa_destruction_check(chordal_env);
323 be_timer_pop(T_VERIFY);
326 /* the ifg exists only if there are allocatable regs */
327 be_ifg_free(chordal_env->ifg);
330 /* free some always allocated data structures */
331 pmap_destroy(chordal_env->border_heads);
332 bitset_free(chordal_env->allocatable_regs);
336 * Performs chordal register allocation for each register class on given irg.
338 * @param irg the graph
339 * @return Structure containing timer for the single phases or NULL if no
342 static void be_ra_chordal_main(ir_graph *irg)
344 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
348 be_timer_push(T_RA_OTHER);
350 be_chordal_env_t chordal_env;
351 obstack_init(&chordal_env.obst);
352 chordal_env.opts = &options;
353 chordal_env.irg = irg;
354 chordal_env.border_heads = NULL;
355 chordal_env.ifg = NULL;
356 chordal_env.allocatable_regs = NULL;
358 if (stat_ev_enabled) {
359 be_collect_node_stats(&last_node_stats, irg);
362 /* use one of the generic spiller */
364 /* Perform the following for each register class. */
365 for (j = 0, m = arch_env->n_register_classes; j < m; ++j) {
366 const arch_register_class_t *cls = &arch_env->register_classes[j];
368 if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
372 stat_ev_ctx_push_str("bechordal_cls", cls->name);
374 double pre_spill_cost = 0;
375 if (stat_ev_enabled) {
376 be_do_stat_reg_pressure(irg, cls);
377 pre_spill_cost = be_estimate_irg_costs(irg);
380 pre_spill(&chordal_env, cls, irg);
382 be_timer_push(T_RA_SPILL);
383 be_do_spill(irg, cls);
384 be_timer_pop(T_RA_SPILL);
386 dump(BE_CH_DUMP_SPILL, irg, cls, "spill");
388 stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg) - pre_spill_cost);
390 post_spill(&chordal_env, irg);
392 if (stat_ev_enabled) {
393 be_node_stats_t node_stats;
395 be_collect_node_stats(&node_stats, irg);
396 be_subtract_node_stats(&node_stats, &last_node_stats);
397 be_emit_node_stats(&node_stats, "bechordal_");
399 be_copy_node_stats(&last_node_stats, &node_stats);
400 stat_ev_ctx_pop("bechordal_cls");
404 be_timer_push(T_VERIFY);
405 if (chordal_env.opts->vrfy_option == BE_CH_VRFY_WARN) {
406 be_verify_register_allocation(irg);
407 } else if (chordal_env.opts->vrfy_option == BE_CH_VRFY_ASSERT) {
408 assert(be_verify_register_allocation(irg)
409 && "Register allocation invalid");
411 be_timer_pop(T_VERIFY);
413 be_timer_push(T_RA_EPILOG);
414 lower_nodes_after_ra(irg, options.lower_perm_opt == BE_CH_LOWER_PERM_COPY);
415 dump(BE_CH_DUMP_LOWER, irg, NULL, "belower-after-ra");
417 obstack_free(&chordal_env.obst, NULL);
418 be_invalidate_live_sets(irg);
419 be_timer_pop(T_RA_EPILOG);
421 be_timer_pop(T_RA_OTHER);
424 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main)
425 void be_init_chordal_main(void)
427 static be_ra_t be_ra_chordal_allocator = {
431 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
432 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
433 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
435 be_register_allocator("chordal", &be_ra_chordal_allocator);
437 lc_opt_add_table(chordal_grp, be_chordal_options);
438 be_add_module_list_opt(chordal_grp, "coloring", "select coloring method", &colorings, (void**) &selected_coloring);