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