8e598f5e6f18f55e4df7b805673b3d4979612bfb
[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
12 #include "obst.h"
13 #include "pset.h"
14 #include "list.h"
15 #include "bitset.h"
16 #include "iterator.h"
17
18 #ifdef WITH_LIBCORE
19 #include <libcore/lc_opts.h>
20 #include <libcore/lc_opts_enum.h>
21 #endif
22
23 #include "irmode_t.h"
24 #include "irgraph_t.h"
25 #include "irprintf_t.h"
26 #include "irgwalk.h"
27 #include "irdump.h"
28 #include "irdom.h"
29 #include "debug.h"
30 #include "xmalloc.h"
31
32 #include "bechordal_t.h"
33 #include "beutil.h"
34 #include "besched.h"
35 #include "benumb_t.h"
36 #include "besched_t.h"
37 #include "belive_t.h"
38 #include "bearch.h"
39 #include "beifg.h"
40 #include "beifg_impl.h"
41
42 #include "bespillbelady.h"
43 #include "bespillilp.h"
44
45
46 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
47         firm_dbg_module_t *dbg = chordal_env->dbg;
48         const arch_env_t *arch_env = chordal_env->main_env->arch_env;
49         struct obstack ob;
50         pmap_entry *pme;
51         ir_node **nodes, *n1, *n2;
52         int i, o;
53
54         /* Collect all irns */
55         obstack_init(&ob);
56         pmap_foreach(chordal_env->border_heads, pme) {
57                 border_t *curr;
58                 struct list_head *head = pme->value;
59                 list_for_each_entry(border_t, curr, head, list)
60                         if (curr->is_def && curr->is_real)
61                                 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
62                                         obstack_ptr_grow(&ob, curr->irn);
63         }
64         obstack_ptr_grow(&ob, NULL);
65         nodes = (ir_node **) obstack_finish(&ob);
66
67         /* Check them */
68         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
69                 const arch_register_t *n1_reg, *n2_reg;
70
71                 n1_reg = arch_get_irn_register(arch_env, n1);
72                 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
73                         DBG((dbg, 0, "Register assigned to %+F is not allowed\n", n1));
74                         assert(0 && "Register constraint does not hold");
75                 }
76                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
77                         n2_reg = arch_get_irn_register(arch_env, n2);
78                         if (values_interfere(n1, n2) && n1_reg == n2_reg) {
79                                 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
80                                 assert(0 && "Interfering values have the same color!");
81                         }
82                 }
83         }
84         obstack_free(&ob, NULL);
85 }
86
87 static void check_pressure_walker(ir_node *bl, void *data)
88 {
89         be_chordal_env_t *env = data;
90         firm_dbg_module_t *dbg = env->dbg;
91         int n_regs = arch_register_class_n_regs(env->cls);
92
93         pset *live = pset_new_ptr_default();
94         int step = 0;
95         ir_node *irn;
96         irn_live_t *li;
97
98         live_foreach(bl, li) {
99                 if(live_is_end(li) && chordal_has_class(env, li->irn)) {
100                         ir_node *irn = (ir_node *) li->irn;
101                         pset_insert_ptr(live, irn);
102                 }
103         }
104
105         DBG((dbg, LEVEL_1, "end set for %+F\n", bl));
106         for(irn = pset_first(live); irn; irn = pset_next(live))
107                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
108
109         sched_foreach_reverse(bl, irn) {
110                 int i, n;
111                 int pressure = pset_count(live);
112
113                 DBG((dbg, LEVEL_1, "%+10F@%+10F: pressure %d\n", bl, irn, pressure));
114
115                 if(pressure > n_regs) {
116                         ir_node *x;
117                         ir_printf("%+10F@%+10F: pressure to high: %d\n", bl, irn, pressure);
118                         for(x = pset_first(live); x; x = pset_next(live))
119                                 ir_printf("\t%+10F\n", x);
120                 }
121
122                 if(chordal_has_class(env, irn))
123                         pset_remove_ptr(live, irn);
124
125                 for(i = 0, n = get_irn_arity(irn); i < n; i++) {
126                         ir_node *op = get_irn_n(irn, i);
127                         if(chordal_has_class(env, op) && !is_Phi(irn))
128                                 pset_insert_ptr(live, op);
129                 }
130                 step++;
131         }
132 }
133
134 void be_check_pressure(const be_chordal_env_t *env)
135 {
136         irg_block_walk_graph(env->irg, check_pressure_walker, NULL, (void *) env);
137 }
138
139 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
140 {
141         if(env->ifg)
142                 return be_ifg_connected(env->ifg, a, b);
143         else
144                 return values_interfere(a, b);
145 }
146
147
148 static be_ra_chordal_opts_t options = {
149         BE_CH_DUMP_NONE,
150         BE_CH_SPILL_BELADY,
151         BE_CH_COPYMIN_HEUR,
152         BE_CH_IFG_STD
153 };
154
155 #ifdef WITH_LIBCORE
156 static const lc_opt_enum_int_items_t spill_items[] = {
157         { "belady", BE_CH_SPILL_BELADY },
158         { "ilp",        BE_CH_SPILL_ILP },
159         { NULL, 0 }
160 };
161
162 static const lc_opt_enum_int_items_t copymin_items[] = {
163         { "heur", BE_CH_COPYMIN_HEUR },
164         { "ilp",  BE_CH_COPYMIN_ILP },
165         { NULL, 0 }
166 };
167
168 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
169         { "std",  BE_CH_IFG_STD },
170         { "fast", BE_CH_IFG_FAST },
171         { NULL, 0 }
172 };
173
174 static lc_opt_enum_int_var_t spill_var = {
175         &options.spill_method, spill_items
176 };
177
178 static lc_opt_enum_int_var_t copymin_var = {
179         &options.copymin_method, copymin_items
180 };
181
182 static lc_opt_enum_int_var_t ifg_flavor_var = {
183         &options.spill_method, ifg_flavor_items
184 };
185
186 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
187 {
188         lc_opt_entry_t *dump;
189         grp = lc_opt_get_grp(grp, "chordal");
190
191 }
192 #endif
193
194 static void dump(int mask, ir_graph *irg, const char *suffix,
195                                  void (*dump)(ir_graph *, const char *))
196 {
197         if((options.dump_flags & mask) == mask)
198                 dump(irg, suffix);
199 }
200
201 static void be_ra_chordal_main(const be_main_env_t *main_env, ir_graph *irg)
202 {
203         int j, m;
204         be_chordal_env_t chordal_env;
205         const arch_isa_t *isa = arch_env_get_isa(main_env->arch_env);
206
207         compute_doms(irg);
208
209         chordal_env.irg          = irg;
210         chordal_env.dbg          = firm_dbg_register("firm.be.chordal");
211         chordal_env.main_env     = main_env;
212         chordal_env.dom_front    = be_compute_dominance_frontiers(irg);
213
214         obstack_init(&chordal_env.obst);
215
216         /* Perform the following for each register class. */
217         for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
218                 chordal_env.border_heads = pmap_create();
219                 chordal_env.cls = arch_isa_get_reg_class(isa, j);
220
221                 be_liveness(irg);
222
223                 /* spilling */
224                 switch(options.spill_method) {
225                 case BE_CH_SPILL_BELADY:
226                         be_spill_belady(&chordal_env);
227                         break;
228                 case BE_CH_SPILL_ILP:
229                         be_spill_ilp(&chordal_env);
230                         break;
231                 default:
232                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
233                         be_spill_belady(&chordal_env);
234                 }
235
236                 dump(BE_CH_DUMP_SPILL, irg, "-spill", dump_ir_block_graph_sched);
237
238                 be_liveness(irg);
239                 be_numbering(irg);
240                 be_check_pressure(&chordal_env);
241
242                 /* Color the graph. */
243                 be_ra_chordal_color(&chordal_env);
244
245                 /* Build the interference graph. */
246                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
247
248 #ifdef DO_SSADESTR
249                 /* copy minimization */
250                 copystat_collect_cls(&chordal_env);
251                 be_copy_opt(&chordal_env);
252                 dump(BE_CH_DUMP_COPYMIN, irg, "-copymin", dump_ir_block_graph_sched);
253
254                 /* ssa destruction */
255                 be_ssa_destruction(&chordal_env);
256                 be_ssa_destruction_check(&chordal_env);
257                 be_ra_chordal_check(&chordal_env);
258                 dump(BE_CH_DUMP_SSADESTR, irg, "-ssadestr", dump_ir_block_graph_sched);
259
260                 copystat_dump(irg);
261 #endif
262
263                 be_ifg_free(chordal_env.ifg);
264                 be_numbering_done(irg);
265
266                 pmap_destroy(chordal_env.border_heads);
267         }
268
269         be_free_dominance_frontiers(chordal_env.dom_front);
270         obstack_free(&chordal_env.obst, NULL);
271 }
272
273 const be_ra_t be_ra_chordal_allocator = {
274 #ifdef WITH_LIBCORE
275         be_ra_chordal_register_options,
276 #endif
277         be_ra_chordal_main
278 };