8b0f655adc41c5f56ba5e800191e1d0b0011132c
[libfirm] / ir / be / bechordal_main.c
1 /**
2  * @file   bechordal_main.c
3  * @date   29.11.2005
4  * @author Sebastian Hack
5  *
6  * Copyright (C) 2005 Universitaet Karlsruhe
7  * Released under the GPL
8  *
9  * Driver for the chordal register allocator.
10  */
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include "obst.h"
16 #include "pset.h"
17 #include "list.h"
18 #include "bitset.h"
19 #include "iterator.h"
20
21 #ifdef WITH_LIBCORE
22 #include <libcore/lc_opts.h>
23 #include <libcore/lc_opts_enum.h>
24 #endif /* WITH_LIBCORE */
25
26 #include "irmode_t.h"
27 #include "irgraph_t.h"
28 #include "irprintf_t.h"
29 #include "irgwalk.h"
30 #include "irdump.h"
31 #include "irdom.h"
32 #include "debug.h"
33 #include "xmalloc.h"
34
35 #include "bechordal_t.h"
36 #include "beabi.h"
37 #include "beutil.h"
38 #include "besched.h"
39 #include "benumb_t.h"
40 #include "besched_t.h"
41 #include "belive_t.h"
42 #include "bearch.h"
43 #include "beifg_t.h"
44 #include "beifg_impl.h"
45
46 #include "bespillbelady.h"
47 #include "belower.h"
48
49 #ifdef WITH_ILP
50 #include "bespillilp.h"
51 #endif /* WITH_ILP */
52
53 #include "becopystat.h"
54 #include "becopyopt.h"
55 #include "bessadestr.h"
56
57
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;
60         struct obstack ob;
61         pmap_entry *pme;
62         ir_node **nodes, *n1, *n2;
63         int i, o;
64         DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
65
66         /* Collect all irns */
67         obstack_init(&ob);
68         pmap_foreach(chordal_env->border_heads, pme) {
69                 border_t *curr;
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);
75         }
76         obstack_ptr_grow(&ob, NULL);
77         nodes = (ir_node **) obstack_finish(&ob);
78
79         /* Check them */
80         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
81                 const arch_register_t *n1_reg, *n2_reg;
82
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");
87                 }
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!");
93                         }
94                 }
95         }
96         obstack_free(&ob, NULL);
97 }
98
99 static void check_pressure_walker(ir_node *bl, void *data)
100 {
101         be_chordal_env_t *env = data;
102         int n_regs = arch_register_class_n_regs(env->cls);
103
104         pset *live = pset_new_ptr_default();
105         int step = 0;
106         ir_node *irn;
107         irn_live_t *li;
108         DEBUG_ONLY(firm_dbg_module_t *dbg = env->dbg;)
109
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);
114                 }
115         }
116
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));
120
121         sched_foreach_reverse(bl, irn) {
122                 int i, n;
123                 int pressure = pset_count(live);
124
125                 DBG((dbg, LEVEL_1, "%+10F@%+10F: pressure %d\n", bl, irn, pressure));
126
127                 if(pressure > n_regs) {
128                         ir_node *x;
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);
132                 }
133
134                 if(chordal_has_class(env, irn))
135                         pset_remove_ptr(live, irn);
136
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);
141                 }
142                 step++;
143         }
144 }
145
146 void be_check_pressure(const be_chordal_env_t *env)
147 {
148         irg_block_walk_graph(env->irg, check_pressure_walker, NULL, (void *) env);
149 }
150
151 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
152 {
153         if(env->ifg)
154                 return be_ifg_connected(env->ifg, a, b);
155         else
156                 return values_interfere(a, b);
157 }
158
159
160 static be_ra_chordal_opts_t options = {
161         BE_CH_DUMP_NONE,
162         BE_CH_SPILL_BELADY,
163         BE_CH_COPYMIN_HEUR,
164         BE_CH_IFG_STD,
165         BE_CH_LOWER_PERM_SWAP,
166 };
167
168 #ifdef WITH_LIBCORE
169 static const lc_opt_enum_int_items_t spill_items[] = {
170         { "belady", BE_CH_SPILL_BELADY },
171 #ifdef WITH_ILP
172         { "ilp",        BE_CH_SPILL_ILP },
173 #endif
174         { NULL, 0 }
175 };
176
177 static const lc_opt_enum_int_items_t copymin_items[] = {
178         { "none", BE_CH_COPYMIN_NONE },
179         { "heur", BE_CH_COPYMIN_HEUR },
180 #ifdef WITH_ILP
181         { "ilp",  BE_CH_COPYMIN_ILP },
182 #endif
183         { NULL, 0 }
184 };
185
186 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
187         { "std",  BE_CH_IFG_STD },
188         { "fast", BE_CH_IFG_FAST },
189         { NULL, 0 }
190 };
191
192 static const lc_opt_enum_int_items_t lower_perm_items[] = {
193         { "copy", BE_CH_LOWER_PERM_COPY },
194         { "swap", BE_CH_LOWER_PERM_SWAP },
195         { NULL, 0 }
196 };
197
198 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
199         { NULL, 0 }
200 };
201
202 static const lc_opt_enum_int_items_t dump_items[] = {
203         { "spill",    BE_CH_DUMP_SPILL },
204         { "live",     BE_CH_DUMP_LIVE },
205         { "color",    BE_CH_DUMP_COLOR },
206         { "copymin",  BE_CH_DUMP_COPYMIN },
207         { "ssadestr", BE_CH_DUMP_SSADESTR },
208         { "tree",     BE_CH_DUMP_TREE_INTV },
209         { "constr",   BE_CH_DUMP_CONSTR },
210         { "lower",    BE_CH_DUMP_LOWER },
211         { NULL, 0 }
212 };
213
214 static lc_opt_enum_int_var_t spill_var = {
215         &options.spill_method, spill_items
216 };
217
218 static lc_opt_enum_int_var_t copymin_var = {
219         &options.copymin_method, copymin_items
220 };
221
222 static lc_opt_enum_int_var_t ifg_flavor_var = {
223         &options.spill_method, ifg_flavor_items
224 };
225
226 static lc_opt_enum_int_var_t lower_perm_var = {
227         &options.lower_perm_opt, lower_perm_items
228 };
229
230 static lc_opt_enum_int_var_t dump_var = {
231         &options.dump_flags, dump_items
232 };
233
234 static const lc_opt_table_entry_t be_chordal_options[] = {
235         LC_OPT_ENT_ENUM_MASK("spill", "spill method (belady or ilp)", &spill_var),
236         LC_OPT_ENT_ENUM_PTR("copymin", "copymin method (none, heur or ilp)", &copymin_var),
237         LC_OPT_ENT_ENUM_PTR("ifg", "interference graph flavour (std or fast)", &ifg_flavor_var),
238         LC_OPT_ENT_ENUM_MASK("perm", "perm lowering options (copy, swap)", &lower_perm_var),
239         LC_OPT_ENT_ENUM_MASK("dump", "select dump phases", &dump_var),
240         { NULL }
241 };
242
243 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
244 {
245         static int run_once = 0;
246         lc_opt_entry_t *chordal_grp;
247
248         if (! run_once) {
249                 run_once    = 1;
250                 chordal_grp = lc_opt_get_grp(grp, "chordal");
251
252                 lc_opt_add_table(chordal_grp, be_chordal_options);
253         }
254 }
255 #endif
256
257 static void dump(unsigned mask, ir_graph *irg,
258                                  const arch_register_class_t *cls,
259                                  const char *suffix,
260                                  void (*dump_func)(ir_graph *, const char *))
261 {
262         if(1 || ((options.dump_flags & mask) == mask)) {
263                 if(cls) {
264                         char buf[256];
265                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
266                         be_dump(irg, buf, dump_func);
267                 }
268
269                 else
270                         be_dump(irg, suffix, dump_func);
271         }
272 }
273
274 static void put_ignore_colors(be_chordal_env_t *chordal_env)
275 {
276         int n_colors = chordal_env->cls->n_regs;
277         int i;
278
279         bitset_clear_all(chordal_env->ignore_colors);
280         be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
281         for(i = 0; i < n_colors; ++i)
282                 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
283                         bitset_set(chordal_env->ignore_colors, i);
284 }
285
286 static void be_ra_chordal_main(const be_irg_t *bi)
287 {
288         const be_main_env_t *main_env = bi->main_env;
289         const arch_isa_t *isa         = arch_env_get_isa(main_env->arch_env);
290         ir_graph *irg                 = bi->irg;
291
292         int j, m;
293         be_chordal_env_t chordal_env;
294
295         compute_doms(irg);
296
297         chordal_env.opts          = &options;
298         chordal_env.irg           = irg;
299         chordal_env.birg          = bi;
300         chordal_env.dom_front     = be_compute_dominance_frontiers(irg);
301         FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
302
303         obstack_init(&chordal_env.obst);
304
305         /* Perform the following for each register class. */
306         for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
307                 chordal_env.cls           = arch_isa_get_reg_class(isa, j);
308                 chordal_env.border_heads  = pmap_create();
309                 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
310
311                 /* put all ignore registers into the ignore register set. */
312                 put_ignore_colors(&chordal_env);
313
314                 be_liveness(irg);
315                 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
316
317                 /* spilling */
318                 switch(options.spill_method) {
319                 case BE_CH_SPILL_BELADY:
320                         be_spill_belady(&chordal_env);
321                         break;
322 #ifdef WITH_ILP
323                 case BE_CH_SPILL_ILP:
324                         be_spill_ilp(&chordal_env);
325                         break;
326 #endif /* WITH_ILP */
327                 default:
328                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
329                         be_spill_belady(&chordal_env);
330                 }
331                 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
332                 be_liveness(irg);
333                 be_check_pressure(&chordal_env);
334
335                 /* Color the graph. */
336                 be_ra_chordal_color(&chordal_env);
337                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
338
339                 /* Build the interference graph. */
340                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
341                 be_ifg_check(chordal_env.ifg);
342
343                 /* copy minimization */
344 #ifdef COPYOPT_STAT
345                 co_compare_solvers(&chordal_env);
346 #else
347                 if (options.copymin_method != BE_CH_COPYMIN_NONE)
348                 {
349                         copy_opt_t *co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
350                         co_build_ou_structure(co);
351                         co_solve_heuristic(co);
352                         co_free_ou_structure(co);
353                         free_copy_opt(co);
354                 }
355 #endif
356                 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
357                 be_ra_chordal_check(&chordal_env);
358
359                 /* ssa destruction */
360                 be_ssa_destruction(&chordal_env);
361                 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
362                 be_ssa_destruction_check(&chordal_env);
363                 be_ra_chordal_check(&chordal_env);
364
365                 copystat_dump(irg);
366
367                 be_ifg_free(chordal_env.ifg);
368                 pmap_destroy(chordal_env.border_heads);
369                 bitset_free(chordal_env.ignore_colors);
370         }
371
372         be_compute_spill_offsets(&chordal_env);
373
374         dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
375
376         lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
377         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
378
379         obstack_free(&chordal_env.obst, NULL);
380         be_free_dominance_frontiers(chordal_env.dom_front);
381 }
382
383 const be_ra_t be_ra_chordal_allocator = {
384 #ifdef WITH_LIBCORE
385         be_ra_chordal_register_options,
386 #endif
387         be_ra_chordal_main
388 };