6a576ea443df8c076dfce8f678385dfe1f5a952a
[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 "beutil.h"
37 #include "besched.h"
38 #include "benumb_t.h"
39 #include "besched_t.h"
40 #include "belive_t.h"
41 #include "bearch.h"
42 #include "beifg_t.h"
43 #include "beifg_impl.h"
44
45 #include "bespillbelady.h"
46 #include "bespillilp.h"
47 #include "belower.h"
48
49 #include "becopyopt.h"
50 #include "bessadestr.h"
51 #include "becopystat.h"
52
53
54 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
55         firm_dbg_module_t *dbg = chordal_env->dbg;
56         const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
57         struct obstack ob;
58         pmap_entry *pme;
59         ir_node **nodes, *n1, *n2;
60         int i, o;
61
62         /* Collect all irns */
63         obstack_init(&ob);
64         pmap_foreach(chordal_env->border_heads, pme) {
65                 border_t *curr;
66                 struct list_head *head = pme->value;
67                 list_for_each_entry(border_t, curr, head, list)
68                         if (curr->is_def && curr->is_real)
69                                 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
70                                         obstack_ptr_grow(&ob, curr->irn);
71         }
72         obstack_ptr_grow(&ob, NULL);
73         nodes = (ir_node **) obstack_finish(&ob);
74
75         /* Check them */
76         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
77                 const arch_register_t *n1_reg, *n2_reg;
78
79                 n1_reg = arch_get_irn_register(arch_env, n1);
80                 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
81                         DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
82                         assert(0 && "Register constraint does not hold");
83                 }
84                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
85                         n2_reg = arch_get_irn_register(arch_env, n2);
86                         if (values_interfere(n1, n2) && n1_reg == n2_reg) {
87                                 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
88                                 assert(0 && "Interfering values have the same color!");
89                         }
90                 }
91         }
92         obstack_free(&ob, NULL);
93 }
94
95 static void check_pressure_walker(ir_node *bl, void *data)
96 {
97         be_chordal_env_t *env = data;
98         firm_dbg_module_t *dbg = env->dbg;
99         int n_regs = arch_register_class_n_regs(env->cls);
100
101         pset *live = pset_new_ptr_default();
102         int step = 0;
103         ir_node *irn;
104         irn_live_t *li;
105
106         live_foreach(bl, li) {
107                 if(live_is_end(li) && chordal_has_class(env, li->irn)) {
108                         ir_node *irn = (ir_node *) li->irn;
109                         pset_insert_ptr(live, irn);
110                 }
111         }
112
113         DBG((dbg, LEVEL_1, "end set for %+F\n", bl));
114         for(irn = pset_first(live); irn; irn = pset_next(live))
115                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
116
117         sched_foreach_reverse(bl, irn) {
118                 int i, n;
119                 int pressure = pset_count(live);
120
121                 DBG((dbg, LEVEL_1, "%+10F@%+10F: pressure %d\n", bl, irn, pressure));
122
123                 if(pressure > n_regs) {
124                         ir_node *x;
125                         ir_printf("%+10F@%+10F: pressure to high: %d\n", bl, irn, pressure);
126                         for(x = pset_first(live); x; x = pset_next(live))
127                                 ir_printf("\t%+10F\n", x);
128                 }
129
130                 if(chordal_has_class(env, irn))
131                         pset_remove_ptr(live, irn);
132
133                 for(i = 0, n = get_irn_arity(irn); i < n; i++) {
134                         ir_node *op = get_irn_n(irn, i);
135                         if(chordal_has_class(env, op) && !is_Phi(irn))
136                                 pset_insert_ptr(live, op);
137                 }
138                 step++;
139         }
140 }
141
142 void be_check_pressure(const be_chordal_env_t *env)
143 {
144         irg_block_walk_graph(env->irg, check_pressure_walker, NULL, (void *) env);
145 }
146
147 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
148 {
149         if(env->ifg)
150                 return be_ifg_connected(env->ifg, a, b);
151         else
152                 return values_interfere(a, b);
153 }
154
155
156 static be_ra_chordal_opts_t options = {
157         BE_CH_DUMP_NONE,
158         BE_CH_SPILL_BELADY,
159         BE_CH_COPYMIN_HEUR,
160         BE_CH_IFG_STD,
161         BE_CH_LOWER_PERM_SWAP
162 };
163
164 #ifdef WITH_LIBCORE
165 static const lc_opt_enum_int_items_t spill_items[] = {
166         { "belady", BE_CH_SPILL_BELADY },
167 #ifndef NO_ILP
168         { "ilp",        BE_CH_SPILL_ILP },
169 #endif
170         { NULL, 0 }
171 };
172
173 static const lc_opt_enum_int_items_t copymin_items[] = {
174         { "heur", BE_CH_COPYMIN_HEUR },
175         { "ilp",  BE_CH_COPYMIN_ILP },
176         { NULL, 0 }
177 };
178
179 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
180         { "std",  BE_CH_IFG_STD },
181         { "fast", BE_CH_IFG_FAST },
182         { NULL, 0 }
183 };
184
185 static const lc_opt_enum_int_items_t lower_perm_items[] = {
186         { "swap", BE_CH_LOWER_PERM_SWAP },
187         { "copy", BE_CH_LOWER_PERM_COPY },
188         { NULL, 0 }
189 };
190
191 static const lc_opt_enum_int_items_t dump_items[] = {
192         { "spill",    BE_CH_DUMP_SPILL },
193         { "live",     BE_CH_DUMP_LIVE },
194         { "color",    BE_CH_DUMP_COLOR },
195         { "copymin",  BE_CH_DUMP_COPYMIN },
196         { "ssadestr", BE_CH_DUMP_SSADESTR },
197         { "tree",     BE_CH_DUMP_TREE_INTV },
198         { "constr",   BE_CH_DUMP_CONSTR },
199         { "lower",    BE_CH_DUMP_LOWER },
200         { NULL, 0 }
201 };
202
203 static lc_opt_enum_int_var_t spill_var = {
204         &options.spill_method, spill_items
205 };
206
207 static lc_opt_enum_int_var_t copymin_var = {
208         &options.copymin_method, copymin_items
209 };
210
211 static lc_opt_enum_int_var_t ifg_flavor_var = {
212         &options.spill_method, ifg_flavor_items
213 };
214
215 static lc_opt_enum_int_var_t lower_perm_var = {
216         &options.lower_perm_method, lower_perm_items
217 };
218
219 static lc_opt_enum_int_var_t dump_var = {
220         &options.dump_flags, dump_items
221 };
222
223 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
224 {
225         grp = lc_opt_get_grp(grp, "chordal");
226 }
227 #endif
228
229 static void dump(unsigned mask, ir_graph *irg,
230                                  const arch_register_class_t *cls,
231                                  const char *suffix,
232                                  void (*dump_func)(ir_graph *, const char *))
233 {
234         if(1 || (options.dump_flags & mask) == mask) {
235                 if(cls) {
236                         char buf[256];
237                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
238                         dump_func(irg, buf);
239                 }
240
241                 else
242                         dump_func(irg, suffix);
243         }
244 }
245
246 static void be_ra_chordal_main(const be_irg_t *bi)
247 {
248         int j, m;
249         be_chordal_env_t chordal_env;
250         ir_graph *irg = bi->irg;
251         const be_main_env_t *main_env = bi->main_env;
252         const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
253
254         compute_doms(irg);
255
256         chordal_env.opts         = &options;
257         chordal_env.irg          = irg;
258         chordal_env.dbg          = firm_dbg_register("firm.be.chordal");
259         chordal_env.birg                 = bi;
260         chordal_env.dom_front    = be_compute_dominance_frontiers(irg);
261
262         obstack_init(&chordal_env.obst);
263
264         /* Perform the following for each register class. */
265         for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
266                 chordal_env.cls          = arch_isa_get_reg_class(isa, j);
267                 chordal_env.border_heads = pmap_create();
268
269                 be_liveness(irg);
270                 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
271
272                 /* spilling */
273                 switch(options.spill_method) {
274                 case BE_CH_SPILL_BELADY:
275                         be_spill_belady(&chordal_env);
276                         break;
277 #ifndef NO_ILP
278                 case BE_CH_SPILL_ILP:
279                         be_spill_ilp(&chordal_env);
280                         break;
281 #endif
282                 default:
283                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
284                         be_spill_belady(&chordal_env);
285                 }
286                 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
287                 be_liveness(irg);
288                 be_check_pressure(&chordal_env);
289
290                 be_liveness(irg);
291                 be_check_pressure(&chordal_env);
292
293                 /* Color the graph. */
294                 be_ra_chordal_color(&chordal_env);
295                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
296
297                 /* Build the interference graph. */
298                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
299                 be_ifg_check(chordal_env.ifg);
300
301                 /* copy minimization */
302                 copystat_collect_cls(&chordal_env);
303                 co_compare_solvers(&chordal_env);
304                 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
305                 be_ra_chordal_check(&chordal_env);
306
307                 /* ssa destruction */
308                 be_ssa_destruction(&chordal_env);
309                 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
310                 be_ssa_destruction_check(&chordal_env);
311                 be_ra_chordal_check(&chordal_env);
312
313                 copystat_dump(irg);
314
315                 be_ifg_free(chordal_env.ifg);
316
317                 pmap_destroy(chordal_env.border_heads);
318         }
319
320         be_compute_spill_offsets(&chordal_env);
321
322         dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
323
324         lower_nodes_after_ra(&chordal_env, options.lower_perm_method == BE_CH_LOWER_PERM_COPY ? 1 : 0);
325         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
326
327         obstack_free(&chordal_env.obst, NULL);
328         be_free_dominance_frontiers(chordal_env.dom_front);
329 }
330
331 const be_ra_t be_ra_chordal_allocator = {
332 #ifdef WITH_LIBCORE
333         be_ra_chordal_register_options,
334 #endif
335         be_ra_chordal_main
336 };