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