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"
42 #include "irprintf_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 typedef struct post_spill_env_t {
92 be_chordal_env_t cenv;
93 const arch_register_class_t *cls;
94 double pre_spill_cost;
97 static const lc_opt_enum_int_items_t lower_perm_items[] = {
98 { "copy", BE_CH_LOWER_PERM_COPY },
99 { "swap", BE_CH_LOWER_PERM_SWAP },
103 static const lc_opt_enum_mask_items_t dump_items[] = {
104 { "none", BE_CH_DUMP_NONE },
105 { "spill", BE_CH_DUMP_SPILL },
106 { "live", BE_CH_DUMP_LIVE },
107 { "color", BE_CH_DUMP_COLOR },
108 { "copymin", BE_CH_DUMP_COPYMIN },
109 { "ssadestr", BE_CH_DUMP_SSADESTR },
110 { "tree", BE_CH_DUMP_TREE_INTV },
111 { "split", BE_CH_DUMP_SPLIT },
112 { "constr", BE_CH_DUMP_CONSTR },
113 { "lower", BE_CH_DUMP_LOWER },
114 { "spillslots", BE_CH_DUMP_SPILLSLOTS },
115 { "appel", BE_CH_DUMP_APPEL },
116 { "all", BE_CH_DUMP_ALL },
120 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
121 { "off", BE_CH_VRFY_OFF },
122 { "warn", BE_CH_VRFY_WARN },
123 { "assert", BE_CH_VRFY_ASSERT },
127 static lc_opt_enum_int_var_t lower_perm_var = {
128 &options.lower_perm_opt, lower_perm_items
131 static lc_opt_enum_mask_var_t dump_var = {
132 &options.dump_flags, dump_items
135 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
136 &options.vrfy_option, be_ch_vrfy_items
139 static const lc_opt_table_entry_t be_chordal_options[] = {
140 LC_OPT_ENT_ENUM_INT ("perm", "perm lowering options", &lower_perm_var),
141 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
142 LC_OPT_ENT_ENUM_INT ("verify", "verify options", &be_ch_vrfy_var),
146 static be_module_list_entry_t *colorings = NULL;
147 static const be_ra_chordal_coloring_t *selected_coloring = NULL;
149 void be_register_chordal_coloring(const char *name, be_ra_chordal_coloring_t *coloring)
151 if (selected_coloring == NULL)
152 selected_coloring = coloring;
154 be_add_module_to_list(&colorings, name, coloring);
157 static void be_ra_chordal_coloring(be_chordal_env_t *env)
159 selected_coloring->allocate(env);
162 static void dump(unsigned mask, ir_graph *irg,
163 const arch_register_class_t *cls,
166 if ((options.dump_flags & mask) == mask) {
169 snprintf(buf, sizeof(buf), "%s-%s", cls->name, suffix);
170 dump_ir_graph(irg, buf);
172 dump_ir_graph(irg, suffix);
178 * Post-Walker: Checks for the given reload if has only one user that can perform the
179 * reload as part of its address mode.
180 * Fold the reload into the user it that is possible.
182 static void memory_operand_walker(ir_node *irn, void *env)
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) {
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(src, pos)) {
206 arch_perform_memory_operand(src, spill, pos);
210 /* kill the Reload if it was folded */
211 if (get_irn_n_edges(irn) == 0) {
212 ir_graph *irg = get_irn_irg(irn);
213 ir_mode *frame_mode = get_irn_mode(get_irn_n(irn, n_be_Reload_frame));
215 set_irn_n(irn, n_be_Reload_mem, new_r_Bad(irg, mode_X));
216 set_irn_n(irn, n_be_Reload_frame, new_r_Bad(irg, frame_mode));
221 * Starts a walk for memory operands if supported by the backend.
223 void check_for_memory_operands(ir_graph *irg)
225 irg_walk_graph(irg, NULL, memory_operand_walker, NULL);
229 static be_node_stats_t last_node_stats;
232 * Perform things which need to be done per register class before spilling.
234 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls, ir_graph *const irg)
236 be_chordal_env_t *chordal_env = &pse->cenv;
239 chordal_env->cls = cls;
240 chordal_env->border_heads = pmap_create();
241 chordal_env->allocatable_regs = bitset_malloc(chordal_env->cls->n_regs);
243 be_assure_live_chk(irg);
245 if (stat_ev_enabled) {
246 pse->pre_spill_cost = be_estimate_irg_costs(irg);
249 /* put all ignore registers into the ignore register set. */
250 be_put_allocatable_regs(irg, pse->cls, chordal_env->allocatable_regs);
252 be_timer_push(T_RA_CONSTR);
253 be_pre_spill_prepare_constr(irg, chordal_env->cls);
254 be_timer_pop(T_RA_CONSTR);
256 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "constr-pre");
260 * Perform things which need to be done per register class after spilling.
262 static void post_spill(post_spill_env_t *const pse, ir_graph *const irg)
264 be_chordal_env_t *chordal_env = &pse->cenv;
265 int allocatable_regs = be_get_n_allocatable_regs(irg, chordal_env->cls);
267 /* some special classes contain only ignore regs, no work to be done */
268 if (allocatable_regs > 0) {
269 stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg) - pse->pre_spill_cost);
272 If we have a backend provided spiller, post spill is
273 called in a loop after spilling for each register class.
274 But we only need to fix stack nodes once in this case.
276 be_timer_push(T_RA_SPILL_APPLY);
277 check_for_memory_operands(irg);
278 be_abi_fix_stack_nodes(irg);
279 be_timer_pop(T_RA_SPILL_APPLY);
282 /* verify schedule and register pressure */
283 be_timer_push(T_VERIFY);
284 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
285 be_verify_schedule(irg);
286 be_verify_register_pressure(irg, pse->cls);
287 } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
288 assert(be_verify_schedule(irg) && "Schedule verification failed");
289 assert(be_verify_register_pressure(irg, pse->cls)
290 && "Register pressure verification failed");
292 be_timer_pop(T_VERIFY);
294 /* Color the graph. */
295 be_timer_push(T_RA_COLOR);
296 be_ra_chordal_coloring(chordal_env);
297 be_timer_pop(T_RA_COLOR);
299 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "color");
301 /* Create the ifg with the selected flavor */
302 be_timer_push(T_RA_IFG);
303 chordal_env->ifg = be_create_ifg(chordal_env);
304 be_timer_pop(T_RA_IFG);
306 if (stat_ev_enabled) {
308 be_node_stats_t node_stats;
310 be_ifg_stat(irg, chordal_env->ifg, &stat);
311 stat_ev_dbl("bechordal_ifg_nodes", stat.n_nodes);
312 stat_ev_dbl("bechordal_ifg_edges", stat.n_edges);
313 stat_ev_dbl("bechordal_ifg_comps", stat.n_comps);
315 be_collect_node_stats(&node_stats, irg);
316 be_subtract_node_stats(&node_stats, &last_node_stats);
318 stat_ev_dbl("bechordal_perms_before_coal",
319 node_stats[BE_STAT_PERMS]);
320 stat_ev_dbl("bechordal_copies_before_coal",
321 node_stats[BE_STAT_COPIES]);
324 be_timer_push(T_RA_COPYMIN);
325 co_driver(chordal_env);
326 be_timer_pop(T_RA_COPYMIN);
328 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "copymin");
330 /* ssa destruction */
331 be_timer_push(T_RA_SSA);
332 be_ssa_destruction(chordal_env);
333 be_timer_pop(T_RA_SSA);
335 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "ssadestr");
337 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
338 be_timer_push(T_VERIFY);
339 be_ssa_destruction_check(chordal_env);
340 be_timer_pop(T_VERIFY);
343 /* the ifg exists only if there are allocatable regs */
344 be_ifg_free(chordal_env->ifg);
347 /* free some always allocated data structures */
348 pmap_destroy(chordal_env->border_heads);
349 bitset_free(chordal_env->allocatable_regs);
353 * Performs chordal register allocation for each register class on given irg.
355 * @param irg the graph
356 * @return Structure containing timer for the single phases or NULL if no
359 static void be_ra_chordal_main(ir_graph *irg)
361 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
364 be_chordal_env_t chordal_env;
367 be_timer_push(T_RA_OTHER);
369 be_timer_push(T_RA_PROLOG);
371 chordal_env.obst = &obst;
372 chordal_env.opts = &options;
373 chordal_env.irg = irg;
374 chordal_env.border_heads = NULL;
375 chordal_env.ifg = NULL;
376 chordal_env.allocatable_regs = NULL;
380 be_timer_pop(T_RA_PROLOG);
382 if (stat_ev_enabled) {
383 be_collect_node_stats(&last_node_stats, irg);
386 /* use one of the generic spiller */
388 /* Perform the following for each register class. */
389 for (j = 0, m = arch_env->n_register_classes; j < m; ++j) {
390 post_spill_env_t pse;
391 const arch_register_class_t *cls = &arch_env->register_classes[j];
393 if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
397 stat_ev_ctx_push_str("bechordal_cls", cls->name);
399 if (stat_ev_enabled) {
400 be_do_stat_reg_pressure(irg, cls);
403 pse.cenv = chordal_env;
404 pre_spill(&pse, cls, irg);
406 be_timer_push(T_RA_SPILL);
407 be_do_spill(irg, cls);
408 be_timer_pop(T_RA_SPILL);
410 dump(BE_CH_DUMP_SPILL, irg, pse.cls, "spill");
412 post_spill(&pse, irg);
414 if (stat_ev_enabled) {
415 be_node_stats_t node_stats;
417 be_collect_node_stats(&node_stats, irg);
418 be_subtract_node_stats(&node_stats, &last_node_stats);
419 be_emit_node_stats(&node_stats, "bechordal_");
421 be_copy_node_stats(&last_node_stats, &node_stats);
422 stat_ev_ctx_pop("bechordal_cls");
426 be_timer_push(T_VERIFY);
427 if (chordal_env.opts->vrfy_option == BE_CH_VRFY_WARN) {
428 be_verify_register_allocation(irg);
429 } else if (chordal_env.opts->vrfy_option == BE_CH_VRFY_ASSERT) {
430 assert(be_verify_register_allocation(irg)
431 && "Register allocation invalid");
433 be_timer_pop(T_VERIFY);
435 be_timer_push(T_RA_EPILOG);
436 lower_nodes_after_ra(irg, options.lower_perm_opt == BE_CH_LOWER_PERM_COPY);
437 dump(BE_CH_DUMP_LOWER, irg, NULL, "belower-after-ra");
439 obstack_free(&obst, NULL);
440 be_invalidate_live_sets(irg);
441 be_timer_pop(T_RA_EPILOG);
443 be_timer_pop(T_RA_OTHER);
446 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main)
447 void be_init_chordal_main(void)
449 static be_ra_t be_ra_chordal_allocator = {
453 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
454 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
455 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
457 be_register_allocator("chordal", &be_ra_chordal_allocator);
459 lc_opt_add_table(chordal_grp, be_chordal_options);
460 be_add_module_list_opt(chordal_grp, "coloring", "select coloring method", &colorings, (void**) &selected_coloring);