fixed CopyB emitter
[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 #include "firm_config.h"
21
22 #ifdef WITH_LIBCORE
23 #include <libcore/lc_opts.h>
24 #include <libcore/lc_opts_enum.h>
25 #include <libcore/lc_timing.h>
26 #endif /* WITH_LIBCORE */
27
28 #include "irmode_t.h"
29 #include "irgraph_t.h"
30 #include "irprintf_t.h"
31 #include "irgwalk.h"
32 #include "irdump.h"
33 #include "irdom.h"
34 #include "irbitset.h"
35 #include "debug.h"
36 #include "xmalloc.h"
37
38 #include "bechordal_t.h"
39 #include "beabi.h"
40 #include "beutil.h"
41 #include "besched.h"
42 #include "benumb_t.h"
43 #include "besched_t.h"
44 #include "belive_t.h"
45 #include "bearch.h"
46 #include "beifg_t.h"
47 #include "beifg_impl.h"
48
49 #include "bespillbelady.h"
50 #include "bespillmorgan.h"
51 #include "belower.h"
52
53 #ifdef WITH_ILP
54 #include "bespillilp.h"
55 #include "bespillremat.h"
56 #include "bespillappel.h"
57 #endif /* WITH_ILP */
58
59 #include "becopystat.h"
60 #include "becopyopt.h"
61 #include "bessadestr.h"
62 #include "beverify.h"
63
64
65 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
66         const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
67         struct obstack ob;
68         pmap_entry *pme;
69         ir_node **nodes, *n1, *n2;
70         int i, o;
71         DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
72
73         /* Collect all irns */
74         obstack_init(&ob);
75         pmap_foreach(chordal_env->border_heads, pme) {
76                 border_t *curr;
77                 struct list_head *head = pme->value;
78                 list_for_each_entry(border_t, curr, head, list)
79                         if (curr->is_def && curr->is_real)
80                                 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
81                                         obstack_ptr_grow(&ob, curr->irn);
82         }
83         obstack_ptr_grow(&ob, NULL);
84         nodes = (ir_node **) obstack_finish(&ob);
85
86         /* Check them */
87         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
88                 const arch_register_t *n1_reg, *n2_reg;
89
90                 n1_reg = arch_get_irn_register(arch_env, n1);
91                 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
92                         DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
93                         assert(0 && "Register constraint does not hold");
94                 }
95                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
96                         n2_reg = arch_get_irn_register(arch_env, n2);
97                         if (values_interfere(n1, n2) && n1_reg == n2_reg) {
98                                 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
99                                 assert(0 && "Interfering values have the same color!");
100                         }
101                 }
102         }
103         obstack_free(&ob, NULL);
104 }
105
106 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
107 {
108         if(env->ifg)
109                 return be_ifg_connected(env->ifg, a, b);
110         else
111                 return values_interfere(a, b);
112 }
113
114
115 static be_ra_chordal_opts_t options = {
116         BE_CH_DUMP_NONE,
117         BE_CH_SPILL_BELADY,
118         BE_CH_COPYMIN_HEUR2,
119         BE_CH_IFG_STD,
120         BE_CH_LOWER_PERM_SWAP,
121         BE_CH_VRFY_WARN,
122 };
123
124 static be_ra_timer_t ra_timer = {
125         NULL,
126         NULL,
127         NULL,
128         NULL,
129         NULL,
130         NULL,
131         NULL,
132         NULL,
133
134 };
135
136 #ifdef WITH_LIBCORE
137 static const lc_opt_enum_int_items_t spill_items[] = {
138         { "morgan", BE_CH_SPILL_MORGAN },
139         { "belady", BE_CH_SPILL_BELADY },
140 #ifdef WITH_ILP
141         { "ilp",    BE_CH_SPILL_ILP    },
142         { "remat",  BE_CH_SPILL_REMAT  },
143         { "appel",  BE_CH_SPILL_APPEL  },
144 #endif /* WITH_ILP */
145         { NULL, 0 }
146 };
147
148 static const lc_opt_enum_int_items_t copymin_items[] = {
149         { "none",  BE_CH_COPYMIN_NONE      },
150         { "heur1", BE_CH_COPYMIN_HEUR1     },
151         { "heur2", BE_CH_COPYMIN_HEUR2     },
152         { "stat",  BE_CH_COPYMIN_STAT      },
153         { "park",  BE_CH_COPYMIN_PARK_MOON },
154 #ifdef WITH_ILP
155         { "ilp1",  BE_CH_COPYMIN_ILP1 },
156         { "ilp2",  BE_CH_COPYMIN_ILP2 },
157 #endif /* WITH_ILP */
158         { NULL, 0 }
159 };
160
161 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
162         { "std",     BE_CH_IFG_STD     },
163         { "fast",    BE_CH_IFG_FAST    },
164         { "clique",  BE_CH_IFG_CLIQUE  },
165         { "pointer", BE_CH_IFG_POINTER },
166         { "list",    BE_CH_IFG_LIST    },
167         { "check",   BE_CH_IFG_CHECK   },
168         { NULL,      0                 }
169 };
170
171 static const lc_opt_enum_int_items_t lower_perm_items[] = {
172         { "copy", BE_CH_LOWER_PERM_COPY },
173         { "swap", BE_CH_LOWER_PERM_SWAP },
174         { NULL, 0 }
175 };
176
177 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
178         { NULL, 0 }
179 };
180
181 static const lc_opt_enum_int_items_t dump_items[] = {
182         { "spill",    BE_CH_DUMP_SPILL     },
183         { "live",     BE_CH_DUMP_LIVE      },
184         { "color",    BE_CH_DUMP_COLOR     },
185         { "copymin",  BE_CH_DUMP_COPYMIN   },
186         { "ssadestr", BE_CH_DUMP_SSADESTR  },
187         { "tree",     BE_CH_DUMP_TREE_INTV },
188         { "constr",   BE_CH_DUMP_CONSTR    },
189         { "lower",    BE_CH_DUMP_LOWER     },
190         { "all",      BE_CH_DUMP_ALL       },
191         { NULL, 0 }
192 };
193
194 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
195         { "off",    BE_CH_VRFY_OFF    },
196         { "warn",   BE_CH_VRFY_WARN   },
197         { "assert", BE_CH_VRFY_ASSERT },
198         { NULL, 0 }
199 };
200
201 static lc_opt_enum_int_var_t spill_var = {
202         &options.spill_method, spill_items
203 };
204
205 static lc_opt_enum_int_var_t copymin_var = {
206         &options.copymin_method, copymin_items
207 };
208
209 static lc_opt_enum_int_var_t ifg_flavor_var = {
210         &options.ifg_flavor, ifg_flavor_items
211 };
212
213 static lc_opt_enum_int_var_t lower_perm_var = {
214         &options.lower_perm_opt, lower_perm_items
215 };
216
217 static lc_opt_enum_int_var_t dump_var = {
218         &options.dump_flags, dump_items
219 };
220
221 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
222         &options.vrfy_option, be_ch_vrfy_items
223 };
224
225 static const lc_opt_table_entry_t be_chordal_options[] = {
226         LC_OPT_ENT_ENUM_PTR ("spill",   "spill method (belady, ilp, remat or appel)", &spill_var),
227         LC_OPT_ENT_ENUM_PTR ("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", &copymin_var),
228         LC_OPT_ENT_ENUM_PTR ("ifg",     "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
229         LC_OPT_ENT_ENUM_PTR ("perm",    "perm lowering options (copy or swap)", &lower_perm_var),
230         LC_OPT_ENT_ENUM_MASK("dump",    "select dump phases", &dump_var),
231         LC_OPT_ENT_ENUM_PTR ("vrfy",    "verify options (off, warn, assert)", &be_ch_vrfy_var),
232         { NULL }
233 };
234
235 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
236 {
237         static int run_once = 0;
238         lc_opt_entry_t *chordal_grp;
239
240         if (! run_once) {
241                 run_once    = 1;
242                 chordal_grp = lc_opt_get_grp(grp, "chordal");
243
244                 lc_opt_add_table(chordal_grp, be_chordal_options);
245         }
246
247         co_register_options(chordal_grp);
248 }
249 #endif /* WITH_LIBCORE */
250
251 static void dump(unsigned mask, ir_graph *irg,
252                                  const arch_register_class_t *cls,
253                                  const char *suffix,
254                                  void (*dump_func)(ir_graph *, const char *))
255 {
256         if((options.dump_flags & mask) == mask) {
257                 if (cls) {
258                         char buf[256];
259                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
260                         be_dump(irg, buf, dump_func);
261                 }
262                 else
263                         be_dump(irg, suffix, dump_func);
264         }
265 }
266
267 static void put_ignore_colors(be_chordal_env_t *chordal_env)
268 {
269         int n_colors = chordal_env->cls->n_regs;
270         int i;
271
272         bitset_clear_all(chordal_env->ignore_colors);
273         be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
274         for(i = 0; i < n_colors; ++i)
275                 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
276                         bitset_set(chordal_env->ignore_colors, i);
277 }
278
279 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
280 {
281         char buf[1024];
282
283         ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
284         return fopen(buf, "wt");
285 }
286
287 void check_ifg_implementations(be_chordal_env_t *chordal_env)
288 {
289         FILE *f;
290
291         f = be_chordal_open(chordal_env, "std", "log");
292         chordal_env->ifg = be_ifg_std_new(chordal_env);
293         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
294         fclose(f);
295
296         f = be_chordal_open(chordal_env, "list", "log");
297         be_ifg_free(chordal_env->ifg);
298         chordal_env->ifg = be_ifg_list_new(chordal_env);
299         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
300         fclose(f);
301
302         f = be_chordal_open(chordal_env, "clique", "log");
303         be_ifg_free(chordal_env->ifg);
304         chordal_env->ifg = be_ifg_clique_new(chordal_env);
305         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
306         fclose(f);
307
308         f = be_chordal_open(chordal_env, "pointer", "log");
309         be_ifg_free(chordal_env->ifg);
310         chordal_env->ifg = be_ifg_pointer_new(chordal_env);
311         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
312         fclose(f);
313
314         chordal_env->ifg = NULL;
315 };
316
317 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
318 {
319         const be_main_env_t *main_env  = bi->main_env;
320         const arch_isa_t    *isa       = arch_env_get_isa(main_env->arch_env);
321         ir_graph            *irg       = bi->irg;
322         be_options_t        *main_opts = main_env->options;
323         copy_opt_t          *co;
324
325         int j, m;
326         be_chordal_env_t chordal_env;
327
328         if (main_opts->timing == BE_TIME_ON) {
329                 ra_timer.t_prolog  = lc_timer_register("prolog",   "regalloc prolog");
330                 ra_timer.t_epilog  = lc_timer_register("epilog",   "regalloc epilog");
331                 ra_timer.t_live    = lc_timer_register("liveness", "be liveness");
332                 ra_timer.t_spill   = lc_timer_register("spill",    "spiller");
333                 ra_timer.t_color   = lc_timer_register("color",    "graph coloring");
334                 ra_timer.t_ifg     = lc_timer_register("ifg",      "build interference graph");
335                 ra_timer.t_copymin = lc_timer_register("copymin",  "copy minimization");
336                 ra_timer.t_ssa     = lc_timer_register("ssadestr", "ssa destruction");
337
338                 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
339                 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
340                 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
341                 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
342                 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
343                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
344                 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
345                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
346         }
347
348 #define BE_TIME_START(timer) if (main_opts->timing == BE_TIME_ON) lc_timer_start(timer)
349 #define BE_TIME_STOP(timer)  if (main_opts->timing == BE_TIME_ON) lc_timer_stop(timer)
350
351         BE_TIME_START(ra_timer.t_prolog);
352
353         compute_doms(irg);
354
355         chordal_env.opts          = &options;
356         chordal_env.irg           = irg;
357         chordal_env.birg          = bi;
358         chordal_env.dom_front     = be_compute_dominance_frontiers(irg);
359         FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
360
361         obstack_init(&chordal_env.obst);
362
363         BE_TIME_STOP(ra_timer.t_prolog);
364
365         /* Perform the following for each register class. */
366         for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
367                 FILE *f;
368                 chordal_env.cls           = arch_isa_get_reg_class(isa, j);
369                 chordal_env.border_heads  = pmap_create();
370                 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
371
372                 BE_TIME_START(ra_timer.t_live);
373
374                 /* put all ignore registers into the ignore register set. */
375                 put_ignore_colors(&chordal_env);
376
377                 be_liveness(irg);
378                 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
379
380                 BE_TIME_STOP(ra_timer.t_live);
381                 BE_TIME_START(ra_timer.t_spill);
382
383                 /* spilling */
384                 switch(options.spill_method) {
385                 case BE_CH_SPILL_MORGAN:
386                         be_spill_morgan(&chordal_env);
387                         break;
388                 case BE_CH_SPILL_BELADY:
389                         be_spill_belady(&chordal_env);
390                         break;
391 #ifdef WITH_ILP
392                 case BE_CH_SPILL_ILP:
393                         be_spill_ilp(&chordal_env);
394                         break;
395                 case BE_CH_SPILL_REMAT:
396                         be_spill_remat(&chordal_env);
397                         break;
398                 case BE_CH_SPILL_APPEL:
399                         be_spill_appel(&chordal_env);
400                         break;
401 #endif /* WITH_ILP */
402                 default:
403                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
404                         be_spill_belady(&chordal_env);
405                 }
406                 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
407                 be_abi_fix_stack_nodes(bi->abi);
408
409                 /* verify schedule and register pressure */
410                 if (options.vrfy_option == BE_CH_VRFY_WARN) {
411                         be_verify_schedule(irg);
412                         be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
413                 }
414                 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
415                         assert(be_verify_schedule(irg) && "Schedule verification failed");
416                         assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
417                                 && "Register pressure verification failed");
418                 }
419
420                 BE_TIME_STOP(ra_timer.t_spill);
421                 BE_TIME_START(ra_timer.t_live);
422                 be_liveness(irg);
423                 BE_TIME_STOP(ra_timer.t_live);
424
425                 /* Color the graph. */
426                 BE_TIME_START(ra_timer.t_color);
427                 be_ra_chordal_color(&chordal_env);
428                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
429
430                 BE_TIME_STOP(ra_timer.t_color);
431                 BE_TIME_START(ra_timer.t_ifg);
432                 //be_ifg_check_performance(&chordal_env);
433
434                 /* Create the ifg with the selected flavor */
435                 switch (options.ifg_flavor) {
436                         default:
437                                 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
438                         case BE_CH_IFG_STD:
439                         case BE_CH_IFG_FAST:
440                                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
441                                 break;
442                         case BE_CH_IFG_CLIQUE:
443                                 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
444                                 break;
445                         case BE_CH_IFG_POINTER:
446                                 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
447                                 break;
448                         case BE_CH_IFG_LIST:
449                                 chordal_env.ifg = be_ifg_list_new(&chordal_env);
450                                 break;
451                         case BE_CH_IFG_CHECK:
452                                 check_ifg_implementations(&chordal_env);
453                                 /* Build the interference graph. */
454                                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
455                                 //be_ifg_check(chordal_env.ifg);
456                                 //be_ifg_check_sorted(chordal_env.ifg);
457                                 break;
458                 }
459
460
461 #if 0
462                 {
463                         be_ifg_t *std = be_ifg_std_new(&chordal_env);
464                         f = be_chordal_open(&chordal_env, "std", "csv");
465                         be_ifg_check_sorted_to_file(std, f);
466                         be_ifg_free(std);
467                         fclose(f);
468                 }
469
470                 f = be_chordal_open(&chordal_env, "clique", "csv");
471                 be_ifg_check_sorted_to_file(chordal_env.ifg, f);
472                 fclose(f);
473 #endif
474                 if (options.vrfy_option != BE_CH_VRFY_OFF)
475                         be_ra_chordal_check(&chordal_env);
476
477 //              be_ifg_check_sorted(chordal_env.ifg);
478
479                 BE_TIME_STOP(ra_timer.t_ifg);
480                 BE_TIME_START(ra_timer.t_copymin);
481
482                 /* copy minimization */
483                 co = NULL;
484                 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
485                         co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
486                         co_build_ou_structure(co);
487                         co_build_graph_structure(co);
488                 }
489
490                 switch(options.copymin_method) {
491                         case BE_CH_COPYMIN_HEUR1:
492                                 co_solve_heuristic(co);
493                                 break;
494                         case BE_CH_COPYMIN_HEUR2:
495                                 co_solve_heuristic_new(co);
496                                 break;
497                         case BE_CH_COPYMIN_PARK_MOON:
498                                 co_solve_park_moon(co);
499                                 break;
500                         case BE_CH_COPYMIN_STAT:
501                                 co_compare_solvers(&chordal_env);
502                                 break;
503 #ifdef WITH_ILP
504                         case BE_CH_COPYMIN_ILP1:
505                                 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
506                                 co_solve_ilp1(co, 60.0);
507                                 break;
508                         case BE_CH_COPYMIN_ILP2:
509                                 co_solve_ilp2(co, 60.0);
510                                 break;
511 #endif /* WITH_ILP */
512                         case BE_CH_COPYMIN_NONE:
513                         default:
514                                 break;
515                 }
516
517                 if (co) {
518                         co_free_graph_structure(co);
519                         co_free_ou_structure(co);
520                         free_copy_opt(co);
521                 }
522
523                 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
524                 if (options.vrfy_option != BE_CH_VRFY_OFF)
525                         be_ra_chordal_check(&chordal_env);
526
527                 BE_TIME_STOP(ra_timer.t_copymin);
528                 BE_TIME_START(ra_timer.t_ssa);
529
530                 /* ssa destruction */
531                 be_ssa_destruction(&chordal_env);
532                 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
533                 if (options.vrfy_option != BE_CH_VRFY_OFF) {
534                         be_ssa_destruction_check(&chordal_env);
535                         be_ra_chordal_check(&chordal_env);
536                 }
537
538                 if (options.copymin_method == BE_CH_COPYMIN_STAT)
539                         copystat_dump(irg);
540
541                 be_ifg_free(chordal_env.ifg);
542                 pmap_destroy(chordal_env.border_heads);
543                 bitset_free(chordal_env.ignore_colors);
544
545                 BE_TIME_STOP(ra_timer.t_ssa);
546         }
547
548         BE_TIME_START(ra_timer.t_epilog);
549
550         be_compute_spill_offsets(&chordal_env);
551
552         dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
553
554         lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
555         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
556
557         obstack_free(&chordal_env.obst, NULL);
558         be_free_dominance_frontiers(chordal_env.dom_front);
559
560         BE_TIME_STOP(ra_timer.t_epilog);
561
562         if (main_opts->timing == BE_TIME_ON)
563                 return &ra_timer;
564         else
565                 return NULL;
566 }
567
568 const be_ra_t be_ra_chordal_allocator = {
569 #ifdef WITH_LIBCORE
570         be_ra_chordal_register_options,
571 #endif
572         be_ra_chordal_main
573 };