43591d4823d231d1e8d8496896e881e6c454dba4
[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 "irbitset.h"
33 #include "debug.h"
34 #include "xmalloc.h"
35
36 #include "bechordal_t.h"
37 #include "beabi.h"
38 #include "beutil.h"
39 #include "besched.h"
40 #include "benumb_t.h"
41 #include "besched_t.h"
42 #include "belive_t.h"
43 #include "bearch.h"
44 #include "beifg_t.h"
45 #include "beifg_impl.h"
46
47 #include "bespillbelady.h"
48 #include "belower.h"
49
50 #ifdef WITH_ILP
51 #include "bespillilp.h"
52 #endif /* WITH_ILP */
53
54 #include "becopystat.h"
55 #include "becopyopt.h"
56 #include "bessadestr.h"
57
58
59 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
60         const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
61         struct obstack ob;
62         pmap_entry *pme;
63         ir_node **nodes, *n1, *n2;
64         int i, o;
65         DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
66
67         /* Collect all irns */
68         obstack_init(&ob);
69         pmap_foreach(chordal_env->border_heads, pme) {
70                 border_t *curr;
71                 struct list_head *head = pme->value;
72                 list_for_each_entry(border_t, curr, head, list)
73                         if (curr->is_def && curr->is_real)
74                                 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
75                                         obstack_ptr_grow(&ob, curr->irn);
76         }
77         obstack_ptr_grow(&ob, NULL);
78         nodes = (ir_node **) obstack_finish(&ob);
79
80         /* Check them */
81         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
82                 const arch_register_t *n1_reg, *n2_reg;
83
84                 n1_reg = arch_get_irn_register(arch_env, n1);
85                 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
86                         DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
87                         assert(0 && "Register constraint does not hold");
88                 }
89                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
90                         n2_reg = arch_get_irn_register(arch_env, n2);
91                         if (values_interfere(n1, n2) && n1_reg == n2_reg) {
92                                 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
93                                 assert(0 && "Interfering values have the same color!");
94                         }
95                 }
96         }
97         obstack_free(&ob, NULL);
98 }
99
100 static void check_pressure_walker(ir_node *bl, void *data)
101 {
102         be_chordal_env_t *env = data;
103         int n_regs            = arch_register_class_n_regs(env->cls);
104         bitset_t *live        = bitset_irg_malloc(env->irg);
105         int step              = 0;
106         ir_node *irn;
107         bitset_pos_t elm;
108         irn_live_t *li;
109         DEBUG_ONLY(firm_dbg_module_t *dbg = env->dbg;)
110
111         live_foreach(bl, li)
112                 if(live_is_end(li) && chordal_has_class(env, li->irn))
113                         bitset_add_irn(live, li->irn);
114
115         DBG((dbg, LEVEL_1, "end set for %+F\n", bl));
116         bitset_foreach_irn(env->irg, live, elm, irn)
117                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
118
119         sched_foreach_reverse(bl, irn) {
120                 int pressure = bitset_popcnt(live);
121                 int idx      = get_irn_idx(irn);
122                 int i, n;
123
124                 DBG((dbg, LEVEL_1, "%+10F@%+10F: pressure %d\n", bl, irn, pressure));
125
126                 if(pressure > n_regs) {
127                         ir_node *x;
128                         ir_printf("%+10F@%+10F: pressure to high: %d\n", bl, irn, pressure);
129                         bitset_foreach_irn(env->irg, live, elm, x)
130                                 ir_fprintf(stderr, "\t%+10F\n", x);
131                 }
132
133                 if(chordal_has_class(env, irn)) {
134                         if(!bitset_is_set(live, idx))
135                                 ir_fprintf(stderr, "%+F is defined but was not live\n", irn);
136                         bitset_remv_irn(live, irn);
137                 }
138
139                 for(i = 0, n = get_irn_arity(irn); i < n; i++) {
140                         ir_node *op = get_irn_n(irn, i);
141                         if(chordal_has_class(env, op) && !is_Phi(irn))
142                                 bitset_add_irn(live, op);
143                 }
144                 step++;
145         }
146 }
147
148 void be_check_pressure(const be_chordal_env_t *env)
149 {
150         irg_block_walk_graph(env->irg, check_pressure_walker, NULL, (void *) env);
151 }
152
153 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
154 {
155         if(env->ifg)
156                 return be_ifg_connected(env->ifg, a, b);
157         else
158                 return values_interfere(a, b);
159 }
160
161
162 static be_ra_chordal_opts_t options = {
163         BE_CH_DUMP_NONE,
164         BE_CH_SPILL_BELADY,
165         BE_CH_COPYMIN_HEUR1,
166         BE_CH_IFG_STD,
167         BE_CH_LOWER_PERM_SWAP,
168 };
169
170 #ifdef WITH_LIBCORE
171 static const lc_opt_enum_int_items_t spill_items[] = {
172         { "belady", BE_CH_SPILL_BELADY },
173 #ifdef WITH_ILP
174         { "ilp",        BE_CH_SPILL_ILP },
175 #endif
176         { NULL, 0 }
177 };
178
179 static const lc_opt_enum_int_items_t copymin_items[] = {
180         { "none",  BE_CH_COPYMIN_NONE      },
181         { "heur1", BE_CH_COPYMIN_HEUR1     },
182         { "heur2", BE_CH_COPYMIN_HEUR2     },
183         { "stat",  BE_CH_COPYMIN_STAT      },
184         { "park",  BE_CH_COPYMIN_PARK_MOON },
185 #ifdef WITH_ILP
186         { "ilp1",  BE_CH_COPYMIN_ILP1 },
187         { "ilp2",  BE_CH_COPYMIN_ILP2 },
188 #endif
189         { NULL, 0 }
190 };
191
192 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
193         { "std",  BE_CH_IFG_STD },
194         { "fast", BE_CH_IFG_FAST },
195         { NULL, 0 }
196 };
197
198 static const lc_opt_enum_int_items_t lower_perm_items[] = {
199         { "copy", BE_CH_LOWER_PERM_COPY },
200         { "swap", BE_CH_LOWER_PERM_SWAP },
201         { NULL, 0 }
202 };
203
204 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
205         { NULL, 0 }
206 };
207
208 static const lc_opt_enum_int_items_t dump_items[] = {
209         { "spill",    BE_CH_DUMP_SPILL },
210         { "live",     BE_CH_DUMP_LIVE },
211         { "color",    BE_CH_DUMP_COLOR },
212         { "copymin",  BE_CH_DUMP_COPYMIN },
213         { "ssadestr", BE_CH_DUMP_SSADESTR },
214         { "tree",     BE_CH_DUMP_TREE_INTV },
215         { "constr",   BE_CH_DUMP_CONSTR },
216         { "lower",    BE_CH_DUMP_LOWER },
217         { NULL, 0 }
218 };
219
220 static lc_opt_enum_int_var_t spill_var = {
221         &options.spill_method, spill_items
222 };
223
224 static lc_opt_enum_int_var_t copymin_var = {
225         &options.copymin_method, copymin_items
226 };
227
228 static lc_opt_enum_int_var_t ifg_flavor_var = {
229         &options.spill_method, ifg_flavor_items
230 };
231
232 static lc_opt_enum_int_var_t lower_perm_var = {
233         &options.lower_perm_opt, lower_perm_items
234 };
235
236 static lc_opt_enum_int_var_t dump_var = {
237         &options.dump_flags, dump_items
238 };
239
240 static const lc_opt_table_entry_t be_chordal_options[] = {
241         LC_OPT_ENT_ENUM_MASK("spill",   "spill method (belady or ilp)", &spill_var),
242         LC_OPT_ENT_ENUM_PTR ("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", &copymin_var),
243         LC_OPT_ENT_ENUM_PTR ("ifg",     "interference graph flavour (std or fast)", &ifg_flavor_var),
244         LC_OPT_ENT_ENUM_MASK("perm",    "perm lowering options (copy or swap)", &lower_perm_var),
245         LC_OPT_ENT_ENUM_MASK("dump",    "select dump phases", &dump_var),
246         { NULL }
247 };
248
249 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
250 {
251         static int run_once = 0;
252         lc_opt_entry_t *chordal_grp;
253
254         if (! run_once) {
255                 run_once    = 1;
256                 chordal_grp = lc_opt_get_grp(grp, "chordal");
257
258                 lc_opt_add_table(chordal_grp, be_chordal_options);
259         }
260
261         co_register_options(chordal_grp);
262 }
263 #endif
264
265 static void dump(unsigned mask, ir_graph *irg,
266                                  const arch_register_class_t *cls,
267                                  const char *suffix,
268                                  void (*dump_func)(ir_graph *, const char *))
269 {
270         if(1 || ((options.dump_flags & mask) == mask)) {
271                 if(cls) {
272                         char buf[256];
273                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
274                         be_dump(irg, buf, dump_func);
275                 }
276
277                 else
278                         be_dump(irg, suffix, dump_func);
279         }
280 }
281
282 static void put_ignore_colors(be_chordal_env_t *chordal_env)
283 {
284         int n_colors = chordal_env->cls->n_regs;
285         int i;
286
287         bitset_clear_all(chordal_env->ignore_colors);
288         be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
289         for(i = 0; i < n_colors; ++i)
290                 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
291                         bitset_set(chordal_env->ignore_colors, i);
292 }
293
294 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
295 {
296         char buf[1024];
297
298         ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
299         return fopen(buf, "wt");
300 }
301
302 static void be_ra_chordal_main(const be_irg_t *bi)
303 {
304         const be_main_env_t *main_env = bi->main_env;
305         const arch_isa_t    *isa      = arch_env_get_isa(main_env->arch_env);
306         ir_graph            *irg      = bi->irg;
307         copy_opt_t          *co;
308
309         int j, m;
310         be_chordal_env_t chordal_env;
311
312         compute_doms(irg);
313
314         chordal_env.opts          = &options;
315         chordal_env.irg           = irg;
316         chordal_env.birg          = bi;
317         chordal_env.dom_front     = be_compute_dominance_frontiers(irg);
318         FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
319
320         obstack_init(&chordal_env.obst);
321
322         /* Perform the following for each register class. */
323         for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
324                 chordal_env.cls           = arch_isa_get_reg_class(isa, j);
325                 chordal_env.border_heads  = pmap_create();
326                 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
327
328                 /* put all ignore registers into the ignore register set. */
329                 put_ignore_colors(&chordal_env);
330
331                 be_liveness(irg);
332                 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
333
334                 /* spilling */
335                 switch(options.spill_method) {
336                 case BE_CH_SPILL_BELADY:
337                         be_spill_belady(&chordal_env);
338                         break;
339 #ifdef WITH_ILP
340                 case BE_CH_SPILL_ILP:
341                         be_spill_ilp(&chordal_env);
342                         break;
343 #endif /* WITH_ILP */
344                 default:
345                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
346                         be_spill_belady(&chordal_env);
347                 }
348                 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
349                 be_abi_fix_stack_nodes(bi->abi);
350                 be_liveness(irg);
351                 be_check_pressure(&chordal_env);
352
353                 /* Color the graph. */
354                 be_ra_chordal_color(&chordal_env);
355                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
356
357                 /* Build the interference graph. */
358                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
359                 be_ifg_check(chordal_env.ifg);
360
361                 /* copy minimization */
362                 co = NULL;
363                 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
364                         co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
365                         co_build_ou_structure(co);
366                         co_build_graph_structure(co);
367                 }
368
369                 switch(options.copymin_method) {
370                         case BE_CH_COPYMIN_HEUR1:
371                                 co_solve_heuristic(co);
372                                 break;
373                         case BE_CH_COPYMIN_HEUR2:
374                                 co_solve_heuristic_new(co);
375                                 break;
376                         case BE_CH_COPYMIN_PARK_MOON:
377                                 co_solve_park_moon(co);
378                                 break;
379                         case BE_CH_COPYMIN_STAT:
380                                 co_compare_solvers(&chordal_env);
381                                 break;
382 #ifdef WITH_ILP
383                         case BE_CH_COPYMIN_ILP1:
384                                 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
385                                 co_solve_ilp1(co, 60.0);
386                                 break;
387                         case BE_CH_COPYMIN_ILP2:
388                                 co_solve_ilp2(co, 60.0);
389                                 break;
390 #endif /* WITH_ILP */
391                         case BE_CH_COPYMIN_NONE:
392                         default:
393                                 break;
394                 }
395
396                 if (co) {
397                         co_free_graph_structure(co);
398                         co_free_ou_structure(co);
399                         free_copy_opt(co);
400                 }
401
402                 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
403                 be_ra_chordal_check(&chordal_env);
404
405                 /* ssa destruction */
406                 be_ssa_destruction(&chordal_env);
407                 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
408                 be_ssa_destruction_check(&chordal_env);
409                 be_ra_chordal_check(&chordal_env);
410
411                 copystat_dump(irg);
412
413                 be_ifg_free(chordal_env.ifg);
414                 pmap_destroy(chordal_env.border_heads);
415                 bitset_free(chordal_env.ignore_colors);
416         }
417
418         be_compute_spill_offsets(&chordal_env);
419
420         dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
421
422         lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
423         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
424
425         obstack_free(&chordal_env.obst, NULL);
426         be_free_dominance_frontiers(chordal_env.dom_front);
427 }
428
429 const be_ra_t be_ra_chordal_allocator = {
430 #ifdef WITH_LIBCORE
431         be_ra_chordal_register_options,
432 #endif
433         be_ra_chordal_main
434 };