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"
39 #include "besched_t.h"
43 #include "beifg_impl.h"
45 #include "bespillbelady.h"
49 #include "bespillilp.h"
52 #include "becopystat.h"
53 #include "becopyopt.h"
54 #include "bessadestr.h"
57 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
58 firm_dbg_module_t *dbg = chordal_env->dbg;
59 const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
62 ir_node **nodes, *n1, *n2;
65 /* Collect all irns */
67 pmap_foreach(chordal_env->border_heads, pme) {
69 struct list_head *head = pme->value;
70 list_for_each_entry(border_t, curr, head, list)
71 if (curr->is_def && curr->is_real)
72 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
73 obstack_ptr_grow(&ob, curr->irn);
75 obstack_ptr_grow(&ob, NULL);
76 nodes = (ir_node **) obstack_finish(&ob);
79 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
80 const arch_register_t *n1_reg, *n2_reg;
82 n1_reg = arch_get_irn_register(arch_env, n1);
83 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
84 DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
85 assert(0 && "Register constraint does not hold");
87 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
88 n2_reg = arch_get_irn_register(arch_env, n2);
89 if (values_interfere(n1, n2) && n1_reg == n2_reg) {
90 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
91 assert(0 && "Interfering values have the same color!");
95 obstack_free(&ob, NULL);
98 static void check_pressure_walker(ir_node *bl, void *data)
100 be_chordal_env_t *env = data;
101 firm_dbg_module_t *dbg = env->dbg;
102 int n_regs = arch_register_class_n_regs(env->cls);
104 pset *live = pset_new_ptr_default();
109 live_foreach(bl, li) {
110 if(live_is_end(li) && chordal_has_class(env, li->irn)) {
111 ir_node *irn = (ir_node *) li->irn;
112 pset_insert_ptr(live, irn);
116 DBG((dbg, LEVEL_1, "end set for %+F\n", bl));
117 for(irn = pset_first(live); irn; irn = pset_next(live))
118 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
120 sched_foreach_reverse(bl, irn) {
122 int pressure = pset_count(live);
124 DBG((dbg, LEVEL_1, "%+10F@%+10F: pressure %d\n", bl, irn, pressure));
126 if(pressure > n_regs) {
128 ir_printf("%+10F@%+10F: pressure to high: %d\n", bl, irn, pressure);
129 for(x = pset_first(live); x; x = pset_next(live))
130 ir_printf("\t%+10F\n", x);
133 if(chordal_has_class(env, irn))
134 pset_remove_ptr(live, irn);
136 for(i = 0, n = get_irn_arity(irn); i < n; i++) {
137 ir_node *op = get_irn_n(irn, i);
138 if(chordal_has_class(env, op) && !is_Phi(irn))
139 pset_insert_ptr(live, op);
145 void be_check_pressure(const be_chordal_env_t *env)
147 irg_block_walk_graph(env->irg, check_pressure_walker, NULL, (void *) env);
150 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
153 return be_ifg_connected(env->ifg, a, b);
155 return values_interfere(a, b);
159 static be_ra_chordal_opts_t options = {
164 BE_CH_LOWER_PERM_SWAP
168 static const lc_opt_enum_int_items_t spill_items[] = {
169 { "belady", BE_CH_SPILL_BELADY },
171 { "ilp", BE_CH_SPILL_ILP },
176 static const lc_opt_enum_int_items_t copymin_items[] = {
177 { "heur", BE_CH_COPYMIN_HEUR },
179 { "ilp", BE_CH_COPYMIN_ILP },
184 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
185 { "std", BE_CH_IFG_STD },
186 { "fast", BE_CH_IFG_FAST },
190 static const lc_opt_enum_int_items_t lower_perm_items[] = {
191 { "swap", BE_CH_LOWER_PERM_SWAP },
192 { "copy", BE_CH_LOWER_PERM_COPY },
196 static const lc_opt_enum_int_items_t dump_items[] = {
197 { "spill", BE_CH_DUMP_SPILL },
198 { "live", BE_CH_DUMP_LIVE },
199 { "color", BE_CH_DUMP_COLOR },
200 { "copymin", BE_CH_DUMP_COPYMIN },
201 { "ssadestr", BE_CH_DUMP_SSADESTR },
202 { "tree", BE_CH_DUMP_TREE_INTV },
203 { "constr", BE_CH_DUMP_CONSTR },
204 { "lower", BE_CH_DUMP_LOWER },
208 static lc_opt_enum_int_var_t spill_var = {
209 &options.spill_method, spill_items
212 static lc_opt_enum_int_var_t copymin_var = {
213 &options.copymin_method, copymin_items
216 static lc_opt_enum_int_var_t ifg_flavor_var = {
217 &options.spill_method, ifg_flavor_items
220 static lc_opt_enum_int_var_t lower_perm_var = {
221 &options.lower_perm_method, lower_perm_items
224 static lc_opt_enum_int_var_t dump_var = {
225 &options.dump_flags, dump_items
228 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
230 grp = lc_opt_get_grp(grp, "chordal");
234 static void dump(unsigned mask, ir_graph *irg,
235 const arch_register_class_t *cls,
237 void (*dump_func)(ir_graph *, const char *))
239 if(1 || ((options.dump_flags & mask) == mask)) {
242 snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
247 dump_func(irg, suffix);
251 static void be_ra_chordal_main(const be_irg_t *bi)
254 be_chordal_env_t chordal_env;
255 ir_graph *irg = bi->irg;
256 const be_main_env_t *main_env = bi->main_env;
257 const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
261 chordal_env.opts = &options;
262 chordal_env.irg = irg;
263 chordal_env.dbg = firm_dbg_register("firm.be.chordal");
264 chordal_env.birg = bi;
265 chordal_env.dom_front = be_compute_dominance_frontiers(irg);
267 obstack_init(&chordal_env.obst);
269 /* Perform the following for each register class. */
270 for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
271 chordal_env.cls = arch_isa_get_reg_class(isa, j);
272 chordal_env.border_heads = pmap_create();
275 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
278 switch(options.spill_method) {
279 case BE_CH_SPILL_BELADY:
280 be_spill_belady(&chordal_env);
283 case BE_CH_SPILL_ILP:
284 be_spill_ilp(&chordal_env);
286 #endif /* WITH_ILP */
288 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
289 be_spill_belady(&chordal_env);
291 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
293 be_check_pressure(&chordal_env);
296 be_check_pressure(&chordal_env);
298 /* Color the graph. */
299 be_ra_chordal_color(&chordal_env);
300 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
302 /* Build the interference graph. */
303 chordal_env.ifg = be_ifg_std_new(&chordal_env);
304 be_ifg_check(chordal_env.ifg);
306 /* copy minimization */
307 copystat_collect_cls(&chordal_env);
309 co_compare_solvers(&chordal_env);
312 copy_opt_t *co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
313 co_build_ou_structure(co);
314 co_solve_heuristic(co);
315 co_free_ou_structure(co);
319 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
320 be_ra_chordal_check(&chordal_env);
322 /* ssa destruction */
323 be_ssa_destruction(&chordal_env);
324 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
325 be_ssa_destruction_check(&chordal_env);
326 // be_ra_chordal_check(&chordal_env);
330 be_ifg_free(chordal_env.ifg);
332 pmap_destroy(chordal_env.border_heads);
335 be_compute_spill_offsets(&chordal_env);
337 dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
339 lower_nodes_after_ra(&chordal_env, options.lower_perm_method == BE_CH_LOWER_PERM_COPY ? 1 : 0);
340 dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
342 obstack_free(&chordal_env.obst, NULL);
343 be_free_dominance_frontiers(chordal_env.dom_front);
346 const be_ra_t be_ra_chordal_allocator = {
348 be_ra_chordal_register_options,