2 * @file bechordal_main.c
4 * @author Sebastian Hack
6 * Copyright (C) 2005 Universitaet Karlsruhe
7 * Released under the GPL
9 * Driver for the chordal register allocator.
22 #include <libcore/lc_opts.h>
23 #include <libcore/lc_opts_enum.h>
24 #endif /* WITH_LIBCORE */
27 #include "irgraph_t.h"
28 #include "irprintf_t.h"
35 #include "bechordal_t.h"
40 #include "besched_t.h"
44 #include "beifg_impl.h"
46 #include "bespillbelady.h"
50 #include "bespillilp.h"
53 #include "becopystat.h"
54 #include "becopyopt.h"
55 #include "bessadestr.h"
58 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
59 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
62 ir_node **nodes, *n1, *n2;
64 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
66 /* Collect all irns */
68 pmap_foreach(chordal_env->border_heads, pme) {
70 struct list_head *head = pme->value;
71 list_for_each_entry(border_t, curr, head, list)
72 if (curr->is_def && curr->is_real)
73 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
74 obstack_ptr_grow(&ob, curr->irn);
76 obstack_ptr_grow(&ob, NULL);
77 nodes = (ir_node **) obstack_finish(&ob);
80 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
81 const arch_register_t *n1_reg, *n2_reg;
83 n1_reg = arch_get_irn_register(arch_env, n1);
84 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
85 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
86 assert(0 && "Register constraint does not hold");
88 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
89 n2_reg = arch_get_irn_register(arch_env, n2);
90 if (values_interfere(n1, n2) && n1_reg == n2_reg) {
91 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
92 assert(0 && "Interfering values have the same color!");
96 obstack_free(&ob, NULL);
99 static void check_pressure_walker(ir_node *bl, void *data)
101 be_chordal_env_t *env = data;
102 int n_regs = arch_register_class_n_regs(env->cls);
104 pset *live = pset_new_ptr_default();
108 DEBUG_ONLY(firm_dbg_module_t *dbg = env->dbg;)
110 live_foreach(bl, li) {
111 if(live_is_end(li) && chordal_has_class(env, li->irn)) {
112 ir_node *irn = (ir_node *) li->irn;
113 pset_insert_ptr(live, irn);
117 DBG((dbg, LEVEL_1, "end set for %+F\n", bl));
118 for(irn = pset_first(live); irn; irn = pset_next(live))
119 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
121 sched_foreach_reverse(bl, irn) {
123 int pressure = pset_count(live);
125 DBG((dbg, LEVEL_1, "%+10F@%+10F: pressure %d\n", bl, irn, pressure));
127 if(pressure > n_regs) {
129 ir_printf("%+10F@%+10F: pressure to high: %d\n", bl, irn, pressure);
130 for(x = pset_first(live); x; x = pset_next(live))
131 ir_printf("\t%+10F\n", x);
134 if(chordal_has_class(env, irn))
135 pset_remove_ptr(live, irn);
137 for(i = 0, n = get_irn_arity(irn); i < n; i++) {
138 ir_node *op = get_irn_n(irn, i);
139 if(chordal_has_class(env, op) && !is_Phi(irn))
140 pset_insert_ptr(live, op);
146 void be_check_pressure(const be_chordal_env_t *env)
148 irg_block_walk_graph(env->irg, check_pressure_walker, NULL, (void *) env);
151 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
154 return be_ifg_connected(env->ifg, a, b);
156 return values_interfere(a, b);
160 static be_ra_chordal_opts_t options = {
165 BE_CH_LOWER_PERM_SWAP,
169 static const lc_opt_enum_int_items_t spill_items[] = {
170 { "belady", BE_CH_SPILL_BELADY },
172 { "ilp", BE_CH_SPILL_ILP },
177 static const lc_opt_enum_int_items_t copymin_items[] = {
178 { "none", BE_CH_COPYMIN_NONE },
179 { "heur1", BE_CH_COPYMIN_HEUR1 },
180 { "heur2", BE_CH_COPYMIN_HEUR2 },
181 { "stat", BE_CH_COPYMIN_STAT },
183 { "ilp1", BE_CH_COPYMIN_ILP1 },
184 { "ilp2", BE_CH_COPYMIN_ILP2 },
189 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
190 { "std", BE_CH_IFG_STD },
191 { "fast", BE_CH_IFG_FAST },
195 static const lc_opt_enum_int_items_t lower_perm_items[] = {
196 { "copy", BE_CH_LOWER_PERM_COPY },
197 { "swap", BE_CH_LOWER_PERM_SWAP },
201 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
205 static const lc_opt_enum_int_items_t dump_items[] = {
206 { "spill", BE_CH_DUMP_SPILL },
207 { "live", BE_CH_DUMP_LIVE },
208 { "color", BE_CH_DUMP_COLOR },
209 { "copymin", BE_CH_DUMP_COPYMIN },
210 { "ssadestr", BE_CH_DUMP_SSADESTR },
211 { "tree", BE_CH_DUMP_TREE_INTV },
212 { "constr", BE_CH_DUMP_CONSTR },
213 { "lower", BE_CH_DUMP_LOWER },
217 static lc_opt_enum_int_var_t spill_var = {
218 &options.spill_method, spill_items
221 static lc_opt_enum_int_var_t copymin_var = {
222 &options.copymin_method, copymin_items
225 static lc_opt_enum_int_var_t ifg_flavor_var = {
226 &options.spill_method, ifg_flavor_items
229 static lc_opt_enum_int_var_t lower_perm_var = {
230 &options.lower_perm_opt, lower_perm_items
233 static lc_opt_enum_int_var_t dump_var = {
234 &options.dump_flags, dump_items
237 static const lc_opt_table_entry_t be_chordal_options[] = {
238 LC_OPT_ENT_ENUM_MASK("spill", "spill method (belady or ilp)", &spill_var),
239 LC_OPT_ENT_ENUM_PTR("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", ©min_var),
240 LC_OPT_ENT_ENUM_PTR("ifg", "interference graph flavour (std or fast)", &ifg_flavor_var),
241 LC_OPT_ENT_ENUM_MASK("perm", "perm lowering options (copy or swap)", &lower_perm_var),
242 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
246 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
248 static int run_once = 0;
249 lc_opt_entry_t *chordal_grp;
253 chordal_grp = lc_opt_get_grp(grp, "chordal");
255 lc_opt_add_table(chordal_grp, be_chordal_options);
260 static void dump(unsigned mask, ir_graph *irg,
261 const arch_register_class_t *cls,
263 void (*dump_func)(ir_graph *, const char *))
265 if(1 || ((options.dump_flags & mask) == mask)) {
268 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
269 be_dump(irg, buf, dump_func);
273 be_dump(irg, suffix, dump_func);
277 static void put_ignore_colors(be_chordal_env_t *chordal_env)
279 int n_colors = chordal_env->cls->n_regs;
282 bitset_clear_all(chordal_env->ignore_colors);
283 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
284 for(i = 0; i < n_colors; ++i)
285 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
286 bitset_set(chordal_env->ignore_colors, i);
289 static void be_ra_chordal_main(const be_irg_t *bi)
291 const be_main_env_t *main_env = bi->main_env;
292 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
293 ir_graph *irg = bi->irg;
297 be_chordal_env_t chordal_env;
301 chordal_env.opts = &options;
302 chordal_env.irg = irg;
303 chordal_env.birg = bi;
304 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
305 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
307 obstack_init(&chordal_env.obst);
309 /* Perform the following for each register class. */
310 for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
311 chordal_env.cls = arch_isa_get_reg_class(isa, j);
312 chordal_env.border_heads = pmap_create();
313 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
315 /* put all ignore registers into the ignore register set. */
316 put_ignore_colors(&chordal_env);
319 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
322 switch(options.spill_method) {
323 case BE_CH_SPILL_BELADY:
324 be_spill_belady(&chordal_env);
327 case BE_CH_SPILL_ILP:
328 be_spill_ilp(&chordal_env);
330 #endif /* WITH_ILP */
332 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
333 be_spill_belady(&chordal_env);
335 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
337 be_check_pressure(&chordal_env);
339 /* Color the graph. */
340 be_ra_chordal_color(&chordal_env);
341 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
343 /* Build the interference graph. */
344 chordal_env.ifg = be_ifg_std_new(&chordal_env);
345 be_ifg_check(chordal_env.ifg);
347 /* copy minimization */
349 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
350 co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
351 co_build_ou_structure(co);
354 switch(options.copymin_method) {
355 case BE_CH_COPYMIN_HEUR1:
356 co_solve_heuristic(co);
358 case BE_CH_COPYMIN_HEUR2:
359 co_build_graph_structure(co);
360 co_solve_heuristic_new(co);
361 co_free_graph_structure(co);
363 case BE_CH_COPYMIN_STAT:
364 co_compare_solvers(&chordal_env);
367 case BE_CH_COPYMIN_ILP1:
368 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
369 co_solve_ilp1(co, 60.0);
371 case BE_CH_COPYMIN_ILP2:
372 co_build_graph_structure(co);
373 co_solve_ilp2(co, 60.0);
374 co_free_graph_structure(co);
376 #endif /* WITH_ILP */
377 case BE_CH_COPYMIN_NONE:
383 co_free_ou_structure(co);
387 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
388 be_ra_chordal_check(&chordal_env);
390 /* ssa destruction */
391 be_ssa_destruction(&chordal_env);
392 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
393 be_ssa_destruction_check(&chordal_env);
394 be_ra_chordal_check(&chordal_env);
398 be_ifg_free(chordal_env.ifg);
399 pmap_destroy(chordal_env.border_heads);
400 bitset_free(chordal_env.ignore_colors);
403 be_compute_spill_offsets(&chordal_env);
405 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
407 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
408 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
410 obstack_free(&chordal_env.obst, NULL);
411 be_free_dominance_frontiers(chordal_env.dom_front);
414 const be_ra_t be_ra_chordal_allocator = {
416 be_ra_chordal_register_options,