7ca168fc363610c0ead00d73b3acb0266f32594e
[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.h"
43 #include "beifg_impl.h"
44
45 #include "bespillbelady.h"
46 #include "bespillilp.h"
47
48 #define DO_SSADESTR
49
50 #ifdef DO_SSADESTR
51 #include "bessadestr.h"
52 #include "becopystat.h"
53 #include "becopyoptmain.h"
54 #endif /* DO_SSADESTR */
55
56
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->main_env->arch_env;
60         struct obstack ob;
61         pmap_entry *pme;
62         ir_node **nodes, *n1, *n2;
63         int i, o;
64
65         /* Collect all irns */
66         obstack_init(&ob);
67         pmap_foreach(chordal_env->border_heads, pme) {
68                 border_t *curr;
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);
74         }
75         obstack_ptr_grow(&ob, NULL);
76         nodes = (ir_node **) obstack_finish(&ob);
77
78         /* Check them */
79         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
80                 const arch_register_t *n1_reg, *n2_reg;
81
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 assigned to %+F is not allowed\n", n1));
85                         assert(0 && "Register constraint does not hold");
86                 }
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!");
92                         }
93                 }
94         }
95         obstack_free(&ob, NULL);
96 }
97
98 static void check_pressure_walker(ir_node *bl, void *data)
99 {
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);
103
104         pset *live = pset_new_ptr_default();
105         int step = 0;
106         ir_node *irn;
107         irn_live_t *li;
108
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);
113                 }
114         }
115
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));
119
120         sched_foreach_reverse(bl, irn) {
121                 int i, n;
122                 int pressure = pset_count(live);
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                         for(x = pset_first(live); x; x = pset_next(live))
130                                 ir_printf("\t%+10F\n", x);
131                 }
132
133                 if(chordal_has_class(env, irn))
134                         pset_remove_ptr(live, irn);
135
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);
140                 }
141                 step++;
142         }
143 }
144
145 void be_check_pressure(const be_chordal_env_t *env)
146 {
147         irg_block_walk_graph(env->irg, check_pressure_walker, NULL, (void *) env);
148 }
149
150 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
151 {
152         if(env->ifg)
153                 return be_ifg_connected(env->ifg, a, b);
154         else
155                 return values_interfere(a, b);
156 }
157
158
159 static be_ra_chordal_opts_t options = {
160         BE_CH_DUMP_NONE,
161         BE_CH_SPILL_BELADY,
162         BE_CH_COPYMIN_HEUR,
163         BE_CH_IFG_STD
164 };
165
166 #ifdef WITH_LIBCORE
167 static const lc_opt_enum_int_items_t spill_items[] = {
168         { "belady", BE_CH_SPILL_BELADY },
169         { "ilp",        BE_CH_SPILL_ILP },
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 lc_opt_enum_int_var_t spill_var = {
186         &options.spill_method, spill_items
187 };
188
189 static lc_opt_enum_int_var_t copymin_var = {
190         &options.copymin_method, copymin_items
191 };
192
193 static lc_opt_enum_int_var_t ifg_flavor_var = {
194         &options.spill_method, ifg_flavor_items
195 };
196
197 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
198 {
199         lc_opt_entry_t *dump;
200         grp = lc_opt_get_grp(grp, "chordal");
201
202 }
203 #endif
204
205 static void dump(int mask, ir_graph *irg, const char *suffix,
206                                  void (*dump)(ir_graph *, const char *))
207 {
208         if((options.dump_flags & mask) == mask)
209                 dump(irg, suffix);
210 }
211
212 static void be_ra_chordal_main(const be_main_env_t *main_env, ir_graph *irg)
213 {
214         int j, m;
215         be_chordal_env_t chordal_env;
216         const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
217
218         compute_doms(irg);
219
220         chordal_env.irg          = irg;
221         chordal_env.dbg          = firm_dbg_register("firm.be.chordal");
222         chordal_env.main_env     = main_env;
223         chordal_env.dom_front    = be_compute_dominance_frontiers(irg);
224
225         obstack_init(&chordal_env.obst);
226
227         /* Perform the following for each register class. */
228         for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
229                 chordal_env.border_heads = pmap_create();
230                 chordal_env.cls = arch_isa_get_reg_class(isa, j);
231
232                 be_liveness(irg);
233
234                 /* spilling */
235                 switch(options.spill_method) {
236                 case BE_CH_SPILL_BELADY:
237                         be_spill_belady(&chordal_env);
238                         break;
239                 case BE_CH_SPILL_ILP:
240                         be_spill_ilp(&chordal_env);
241                         break;
242                 default:
243                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
244                         be_spill_belady(&chordal_env);
245                 }
246
247                 dump(BE_CH_DUMP_SPILL, irg, "-spill", dump_ir_block_graph_sched);
248
249                 be_liveness(irg);
250                 be_numbering(irg);
251                 be_check_pressure(&chordal_env);
252
253                 /* Color the graph. */
254                 be_ra_chordal_color(&chordal_env);
255
256                 /* Build the interference graph. */
257                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
258
259 #ifdef DO_SSADESTR
260                 /* copy minimization */
261                 copystat_collect_cls(&chordal_env);
262                 be_copy_opt(&chordal_env);
263                 dump(BE_CH_DUMP_COPYMIN, irg, "-copymin", dump_ir_block_graph_sched);
264
265                 /* ssa destruction */
266                 be_ssa_destruction(&chordal_env);
267                 be_ssa_destruction_check(&chordal_env);
268                 be_ra_chordal_check(&chordal_env);
269                 dump(BE_CH_DUMP_SSADESTR, irg, "-ssadestr", dump_ir_block_graph_sched);
270
271                 copystat_dump(irg);
272 #endif /* DO_SSADESTR */
273
274                 be_ifg_free(chordal_env.ifg);
275                 be_numbering_done(irg);
276
277                 pmap_destroy(chordal_env.border_heads);
278         }
279
280         be_free_dominance_frontiers(chordal_env.dom_front);
281         obstack_free(&chordal_env.obst, NULL);
282 }
283
284 const be_ra_t be_ra_chordal_allocator = {
285 #ifdef WITH_LIBCORE
286         be_ra_chordal_register_options,
287 #endif
288         be_ra_chordal_main
289 };