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"
36 #include "bechordal_t.h"
41 #include "besched_t.h"
45 #include "beifg_impl.h"
47 #include "bespillbelady.h"
48 #include "bespillmorgan.h"
52 #include "bespillilp.h"
55 #include "becopystat.h"
56 #include "becopyopt.h"
57 #include "bessadestr.h"
61 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
62 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
65 ir_node **nodes, *n1, *n2;
67 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
69 /* Collect all irns */
71 pmap_foreach(chordal_env->border_heads, pme) {
73 struct list_head *head = pme->value;
74 list_for_each_entry(border_t, curr, head, list)
75 if (curr->is_def && curr->is_real)
76 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
77 obstack_ptr_grow(&ob, curr->irn);
79 obstack_ptr_grow(&ob, NULL);
80 nodes = (ir_node **) obstack_finish(&ob);
83 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
84 const arch_register_t *n1_reg, *n2_reg;
86 n1_reg = arch_get_irn_register(arch_env, n1);
87 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
88 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
89 assert(0 && "Register constraint does not hold");
91 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
92 n2_reg = arch_get_irn_register(arch_env, n2);
93 if (values_interfere(n1, n2) && n1_reg == n2_reg) {
94 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
95 assert(0 && "Interfering values have the same color!");
99 obstack_free(&ob, NULL);
102 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
105 return be_ifg_connected(env->ifg, a, b);
107 return values_interfere(a, b);
111 static be_ra_chordal_opts_t options = {
116 BE_CH_LOWER_PERM_SWAP,
121 static const lc_opt_enum_int_items_t spill_items[] = {
122 { "morgan", BE_CH_SPILL_MORGAN },
123 { "belady", BE_CH_SPILL_BELADY },
125 { "ilp", BE_CH_SPILL_ILP },
130 static const lc_opt_enum_int_items_t copymin_items[] = {
131 { "none", BE_CH_COPYMIN_NONE },
132 { "heur1", BE_CH_COPYMIN_HEUR1 },
133 { "heur2", BE_CH_COPYMIN_HEUR2 },
134 { "stat", BE_CH_COPYMIN_STAT },
135 { "park", BE_CH_COPYMIN_PARK_MOON },
137 { "ilp1", BE_CH_COPYMIN_ILP1 },
138 { "ilp2", BE_CH_COPYMIN_ILP2 },
143 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
144 { "std", BE_CH_IFG_STD },
145 { "fast", BE_CH_IFG_FAST },
149 static const lc_opt_enum_int_items_t lower_perm_items[] = {
150 { "copy", BE_CH_LOWER_PERM_COPY },
151 { "swap", BE_CH_LOWER_PERM_SWAP },
155 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
159 static const lc_opt_enum_int_items_t dump_items[] = {
160 { "spill", BE_CH_DUMP_SPILL },
161 { "live", BE_CH_DUMP_LIVE },
162 { "color", BE_CH_DUMP_COLOR },
163 { "copymin", BE_CH_DUMP_COPYMIN },
164 { "ssadestr", BE_CH_DUMP_SSADESTR },
165 { "tree", BE_CH_DUMP_TREE_INTV },
166 { "constr", BE_CH_DUMP_CONSTR },
167 { "lower", BE_CH_DUMP_LOWER },
168 { "all", BE_CH_DUMP_ALL },
172 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
173 { "off", BE_CH_VRFY_OFF },
174 { "warn", BE_CH_VRFY_WARN },
175 { "assert", BE_CH_VRFY_ASSERT },
179 static lc_opt_enum_int_var_t spill_var = {
180 &options.spill_method, spill_items
183 static lc_opt_enum_int_var_t copymin_var = {
184 &options.copymin_method, copymin_items
187 static lc_opt_enum_int_var_t ifg_flavor_var = {
188 &options.spill_method, ifg_flavor_items
191 static lc_opt_enum_int_var_t lower_perm_var = {
192 &options.lower_perm_opt, lower_perm_items
195 static lc_opt_enum_int_var_t dump_var = {
196 &options.dump_flags, dump_items
199 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
200 &options.vrfy_option, be_ch_vrfy_items
203 static const lc_opt_table_entry_t be_chordal_options[] = {
204 LC_OPT_ENT_ENUM_MASK("spill", "spill method (belady or ilp)", &spill_var),
205 LC_OPT_ENT_ENUM_PTR ("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", ©min_var),
206 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour (std or fast)", &ifg_flavor_var),
207 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options (copy or swap)", &lower_perm_var),
208 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
209 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options (off, warn, assert)", &be_ch_vrfy_var),
213 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
215 static int run_once = 0;
216 lc_opt_entry_t *chordal_grp;
220 chordal_grp = lc_opt_get_grp(grp, "chordal");
222 lc_opt_add_table(chordal_grp, be_chordal_options);
225 co_register_options(chordal_grp);
227 #endif /* WITH_LIBCORE */
229 static void dump(unsigned mask, ir_graph *irg,
230 const arch_register_class_t *cls,
232 void (*dump_func)(ir_graph *, const char *))
234 if((options.dump_flags & mask) == mask) {
237 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
238 be_dump(irg, buf, dump_func);
241 be_dump(irg, suffix, dump_func);
245 static void put_ignore_colors(be_chordal_env_t *chordal_env)
247 int n_colors = chordal_env->cls->n_regs;
250 bitset_clear_all(chordal_env->ignore_colors);
251 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
252 for(i = 0; i < n_colors; ++i)
253 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
254 bitset_set(chordal_env->ignore_colors, i);
257 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
261 ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
262 return fopen(buf, "wt");
265 static void be_ra_chordal_main(const be_irg_t *bi)
267 const be_main_env_t *main_env = bi->main_env;
268 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
269 ir_graph *irg = bi->irg;
273 be_chordal_env_t chordal_env;
277 chordal_env.opts = &options;
278 chordal_env.irg = irg;
279 chordal_env.birg = bi;
280 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
281 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
283 obstack_init(&chordal_env.obst);
285 /* Perform the following for each register class. */
286 for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
287 chordal_env.cls = arch_isa_get_reg_class(isa, j);
288 chordal_env.border_heads = pmap_create();
289 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
291 /* put all ignore registers into the ignore register set. */
292 put_ignore_colors(&chordal_env);
295 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
298 switch(options.spill_method) {
299 case BE_CH_SPILL_MORGAN:
300 be_spill_morgan(&chordal_env);
302 case BE_CH_SPILL_BELADY:
303 be_spill_belady(&chordal_env);
306 case BE_CH_SPILL_ILP:
307 be_spill_ilp(&chordal_env);
309 #endif /* WITH_ILP */
311 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
312 be_spill_belady(&chordal_env);
314 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
315 be_abi_fix_stack_nodes(bi->abi);
317 /* verify schedule and register pressure */
318 if (options.vrfy_option == BE_CH_VRFY_WARN) {
319 be_verify_schedule(irg);
320 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
322 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
323 assert(be_verify_schedule(irg) && "Schedule verification failed");
324 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
325 && "Register pressure verification failed");
328 /* Color the graph. */
330 be_ra_chordal_color(&chordal_env);
331 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
333 /* Build the interference graph. */
334 chordal_env.ifg = be_ifg_std_new(&chordal_env);
335 be_ifg_check(chordal_env.ifg);
337 /* copy minimization */
339 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
340 co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
341 co_build_ou_structure(co);
342 co_build_graph_structure(co);
345 switch(options.copymin_method) {
346 case BE_CH_COPYMIN_HEUR1:
347 co_solve_heuristic(co);
349 case BE_CH_COPYMIN_HEUR2:
350 co_solve_heuristic_new(co);
352 case BE_CH_COPYMIN_PARK_MOON:
353 co_solve_park_moon(co);
355 case BE_CH_COPYMIN_STAT:
356 co_compare_solvers(&chordal_env);
359 case BE_CH_COPYMIN_ILP1:
360 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
361 co_solve_ilp1(co, 60.0);
363 case BE_CH_COPYMIN_ILP2:
364 co_solve_ilp2(co, 60.0);
366 #endif /* WITH_ILP */
367 case BE_CH_COPYMIN_NONE:
373 co_free_graph_structure(co);
374 co_free_ou_structure(co);
378 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
379 be_ra_chordal_check(&chordal_env);
381 /* ssa destruction */
382 be_ssa_destruction(&chordal_env);
383 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
384 be_ssa_destruction_check(&chordal_env);
385 be_ra_chordal_check(&chordal_env);
389 be_ifg_free(chordal_env.ifg);
390 pmap_destroy(chordal_env.border_heads);
391 bitset_free(chordal_env.ignore_colors);
394 be_compute_spill_offsets(&chordal_env);
396 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
398 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
399 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
401 obstack_free(&chordal_env.obst, NULL);
402 be_free_dominance_frontiers(chordal_env.dom_front);
405 const be_ra_t be_ra_chordal_allocator = {
407 be_ra_chordal_register_options,