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.
20 #include "firm_config.h"
23 #include <libcore/lc_opts.h>
24 #include <libcore/lc_opts_enum.h>
25 #include <libcore/lc_timing.h>
26 #endif /* WITH_LIBCORE */
29 #include "irgraph_t.h"
30 #include "irprintf_t.h"
38 #include "bechordal_t.h"
43 #include "besched_t.h"
47 #include "beifg_impl.h"
49 #include "bespillbelady.h"
50 #include "bespillmorgan.h"
54 #include "bespillilp.h"
55 #include "bespillremat.h"
56 #include "bespillappel.h"
59 #include "becopystat.h"
60 #include "becopyopt.h"
61 #include "bessadestr.h"
65 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
66 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
69 ir_node **nodes, *n1, *n2;
71 DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
73 /* Collect all irns */
75 pmap_foreach(chordal_env->border_heads, pme) {
77 struct list_head *head = pme->value;
78 list_for_each_entry(border_t, curr, head, list)
79 if (curr->is_def && curr->is_real)
80 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
81 obstack_ptr_grow(&ob, curr->irn);
83 obstack_ptr_grow(&ob, NULL);
84 nodes = (ir_node **) obstack_finish(&ob);
87 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
88 const arch_register_t *n1_reg, *n2_reg;
90 n1_reg = arch_get_irn_register(arch_env, n1);
91 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
92 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
93 assert(0 && "Register constraint does not hold");
95 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
96 n2_reg = arch_get_irn_register(arch_env, n2);
97 if (values_interfere(n1, n2) && n1_reg == n2_reg) {
98 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
99 assert(0 && "Interfering values have the same color!");
103 obstack_free(&ob, NULL);
106 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
109 return be_ifg_connected(env->ifg, a, b);
111 return values_interfere(a, b);
115 static be_ra_chordal_opts_t options = {
120 BE_CH_LOWER_PERM_SWAP,
125 static const lc_opt_enum_int_items_t spill_items[] = {
126 { "morgan", BE_CH_SPILL_MORGAN },
127 { "belady", BE_CH_SPILL_BELADY },
129 { "ilp", BE_CH_SPILL_ILP },
130 { "remat", BE_CH_SPILL_REMAT },
131 { "appel", BE_CH_SPILL_APPEL },
132 #endif /* WITH_ILP */
136 static const lc_opt_enum_int_items_t copymin_items[] = {
137 { "none", BE_CH_COPYMIN_NONE },
138 { "heur1", BE_CH_COPYMIN_HEUR1 },
139 { "heur2", BE_CH_COPYMIN_HEUR2 },
140 { "stat", BE_CH_COPYMIN_STAT },
141 { "park", BE_CH_COPYMIN_PARK_MOON },
143 { "ilp1", BE_CH_COPYMIN_ILP1 },
144 { "ilp2", BE_CH_COPYMIN_ILP2 },
145 #endif /* WITH_ILP */
149 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
150 { "std", BE_CH_IFG_STD },
151 { "fast", BE_CH_IFG_FAST },
152 { "clique", BE_CH_IFG_CLIQUE },
153 { "pointer", BE_CH_IFG_POINTER },
154 { "list", BE_CH_IFG_LIST },
155 { "check", BE_CH_IFG_CHECK },
159 static const lc_opt_enum_int_items_t lower_perm_items[] = {
160 { "copy", BE_CH_LOWER_PERM_COPY },
161 { "swap", BE_CH_LOWER_PERM_SWAP },
165 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
169 static const lc_opt_enum_int_items_t dump_items[] = {
170 { "spill", BE_CH_DUMP_SPILL },
171 { "live", BE_CH_DUMP_LIVE },
172 { "color", BE_CH_DUMP_COLOR },
173 { "copymin", BE_CH_DUMP_COPYMIN },
174 { "ssadestr", BE_CH_DUMP_SSADESTR },
175 { "tree", BE_CH_DUMP_TREE_INTV },
176 { "constr", BE_CH_DUMP_CONSTR },
177 { "lower", BE_CH_DUMP_LOWER },
178 { "all", BE_CH_DUMP_ALL },
182 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
183 { "off", BE_CH_VRFY_OFF },
184 { "warn", BE_CH_VRFY_WARN },
185 { "assert", BE_CH_VRFY_ASSERT },
189 static lc_opt_enum_int_var_t spill_var = {
190 &options.spill_method, spill_items
193 static lc_opt_enum_int_var_t copymin_var = {
194 &options.copymin_method, copymin_items
197 static lc_opt_enum_int_var_t ifg_flavor_var = {
198 &options.ifg_flavor, ifg_flavor_items
201 static lc_opt_enum_int_var_t lower_perm_var = {
202 &options.lower_perm_opt, lower_perm_items
205 static lc_opt_enum_int_var_t dump_var = {
206 &options.dump_flags, dump_items
209 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
210 &options.vrfy_option, be_ch_vrfy_items
213 static const lc_opt_table_entry_t be_chordal_options[] = {
214 LC_OPT_ENT_ENUM_MASK("spill", "spill method (belady, ilp, remat or appel)", &spill_var),
215 LC_OPT_ENT_ENUM_PTR ("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", ©min_var),
216 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
217 LC_OPT_ENT_ENUM_PTR ("perm", "perm lowering options (copy or swap)", &lower_perm_var),
218 LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
219 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify options (off, warn, assert)", &be_ch_vrfy_var),
223 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
225 static int run_once = 0;
226 lc_opt_entry_t *chordal_grp;
230 chordal_grp = lc_opt_get_grp(grp, "chordal");
232 lc_opt_add_table(chordal_grp, be_chordal_options);
235 co_register_options(chordal_grp);
237 #endif /* WITH_LIBCORE */
239 static void dump(unsigned mask, ir_graph *irg,
240 const arch_register_class_t *cls,
242 void (*dump_func)(ir_graph *, const char *))
244 if((options.dump_flags & mask) == mask) {
247 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
248 be_dump(irg, buf, dump_func);
251 be_dump(irg, suffix, dump_func);
255 static void put_ignore_colors(be_chordal_env_t *chordal_env)
257 int n_colors = chordal_env->cls->n_regs;
260 bitset_clear_all(chordal_env->ignore_colors);
261 be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
262 for(i = 0; i < n_colors; ++i)
263 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
264 bitset_set(chordal_env->ignore_colors, i);
267 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
271 ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
272 return fopen(buf, "wt");
275 void check_ifg_implementations(be_chordal_env_t *chordal_env)
280 f = be_chordal_open(chordal_env, "std", "log");
281 chordal_env->ifg = be_ifg_std_new(chordal_env);
282 be_ifg_check_sorted(chordal_env->ifg, f);
285 f = be_chordal_open(chordal_env, "list", "log");
286 chordal_env->ifg = be_ifg_list_new(chordal_env);
287 be_ifg_check_sorted(chordal_env->ifg, f);
290 f = be_chordal_open(chordal_env, "clique", "log");
291 chordal_env->ifg = be_ifg_clique_new(chordal_env);
292 be_ifg_check_sorted(chordal_env->ifg, f);
295 f = be_chordal_open(chordal_env, "pointer", "log");
296 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
297 be_ifg_check_sorted(chordal_env->ifg, f);
300 chordal_env->ifg = NULL;
303 static void be_ra_chordal_main(const be_irg_t *bi)
305 const be_main_env_t *main_env = bi->main_env;
306 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
307 ir_graph *irg = bi->irg;
311 be_chordal_env_t chordal_env;
315 chordal_env.opts = &options;
316 chordal_env.irg = irg;
317 chordal_env.birg = bi;
318 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
319 FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
321 obstack_init(&chordal_env.obst);
323 /* Perform the following for each register class. */
324 for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
325 chordal_env.cls = arch_isa_get_reg_class(isa, j);
326 chordal_env.border_heads = pmap_create();
327 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
329 /* put all ignore registers into the ignore register set. */
330 put_ignore_colors(&chordal_env);
333 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
336 switch(options.spill_method) {
337 case BE_CH_SPILL_MORGAN:
338 be_spill_morgan(&chordal_env);
340 case BE_CH_SPILL_BELADY:
341 be_spill_belady(&chordal_env);
344 case BE_CH_SPILL_ILP:
345 be_spill_ilp(&chordal_env);
347 case BE_CH_SPILL_REMAT:
348 be_spill_remat(&chordal_env);
350 case BE_CH_SPILL_APPEL:
351 be_spill_appel(&chordal_env);
353 #endif /* WITH_ILP */
355 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
356 be_spill_belady(&chordal_env);
358 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
359 be_abi_fix_stack_nodes(bi->abi);
361 /* verify schedule and register pressure */
362 if (options.vrfy_option == BE_CH_VRFY_WARN) {
363 be_verify_schedule(irg);
364 be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
366 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
367 assert(be_verify_schedule(irg) && "Schedule verification failed");
368 assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
369 && "Register pressure verification failed");
372 /* Color the graph. */
374 be_ra_chordal_color(&chordal_env);
375 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
377 //be_ifg_check_performance(&chordal_env);
379 /* Create the ifg with the selected flavor */
380 switch (options.ifg_flavor) {
382 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
385 chordal_env.ifg = be_ifg_std_new(&chordal_env);
387 case BE_CH_IFG_CLIQUE:
388 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
390 case BE_CH_IFG_POINTER:
391 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
394 chordal_env.ifg = be_ifg_list_new(&chordal_env);
396 case BE_CH_IFG_CHECK:
397 check_ifg_implementations(&chordal_env);
398 /* Build the interference graph. */
399 chordal_env.ifg = be_ifg_std_new(&chordal_env);
400 //be_ifg_check(chordal_env.ifg);
401 //be_ifg_check_sorted(chordal_env.ifg);
405 /* copy minimization */
407 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
408 co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
409 co_build_ou_structure(co);
410 co_build_graph_structure(co);
413 switch(options.copymin_method) {
414 case BE_CH_COPYMIN_HEUR1:
415 co_solve_heuristic(co);
417 case BE_CH_COPYMIN_HEUR2:
418 co_solve_heuristic_new(co);
420 case BE_CH_COPYMIN_PARK_MOON:
421 co_solve_park_moon(co);
423 case BE_CH_COPYMIN_STAT:
424 co_compare_solvers(&chordal_env);
427 case BE_CH_COPYMIN_ILP1:
428 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
429 co_solve_ilp1(co, 60.0);
431 case BE_CH_COPYMIN_ILP2:
432 co_solve_ilp2(co, 60.0);
434 #endif /* WITH_ILP */
435 case BE_CH_COPYMIN_NONE:
441 co_free_graph_structure(co);
442 co_free_ou_structure(co);
446 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
447 be_ra_chordal_check(&chordal_env);
449 /* ssa destruction */
450 be_ssa_destruction(&chordal_env);
451 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
452 be_ssa_destruction_check(&chordal_env);
453 be_ra_chordal_check(&chordal_env);
457 be_ifg_free(chordal_env.ifg);
458 pmap_destroy(chordal_env.border_heads);
459 bitset_free(chordal_env.ignore_colors);
462 be_compute_spill_offsets(&chordal_env);
464 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
466 lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
467 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
469 obstack_free(&chordal_env.obst, NULL);
470 be_free_dominance_frontiers(chordal_env.dom_front);
473 const be_ra_t be_ra_chordal_allocator = {
475 be_ra_chordal_register_options,