values may die at every use
[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
126         NULL,
127
128         NULL,
129
130         NULL,
131
132         NULL,
133
134         NULL,
135
136         NULL,
137
138         NULL,
139
140         NULL,
141
142 };
143
144 #ifdef WITH_LIBCORE
145 static const lc_opt_enum_int_items_t spill_items[] = {
146         { "morgan", BE_CH_SPILL_MORGAN },
147         { "belady", BE_CH_SPILL_BELADY },
148 #ifdef WITH_ILP
149         { "ilp",    BE_CH_SPILL_ILP    },
150         { "remat",  BE_CH_SPILL_REMAT  },
151         { "appel",  BE_CH_SPILL_APPEL  },
152 #endif /* WITH_ILP */
153         { NULL, 0 }
154 };
155
156 static const lc_opt_enum_int_items_t copymin_items[] = {
157         { "none",  BE_CH_COPYMIN_NONE      },
158         { "heur1", BE_CH_COPYMIN_HEUR1     },
159         { "heur2", BE_CH_COPYMIN_HEUR2     },
160         { "stat",  BE_CH_COPYMIN_STAT      },
161         { "park",  BE_CH_COPYMIN_PARK_MOON },
162 #ifdef WITH_ILP
163         { "ilp1",  BE_CH_COPYMIN_ILP1 },
164         { "ilp2",  BE_CH_COPYMIN_ILP2 },
165 #endif /* WITH_ILP */
166         { NULL, 0 }
167 };
168
169 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
170         { "std",     BE_CH_IFG_STD     },
171         { "fast",    BE_CH_IFG_FAST    },
172         { "clique",  BE_CH_IFG_CLIQUE  },
173         { "pointer", BE_CH_IFG_POINTER },
174         { "list",    BE_CH_IFG_LIST    },
175         { "check",   BE_CH_IFG_CHECK   },
176         { NULL,      0                 }
177 };
178
179 static const lc_opt_enum_int_items_t lower_perm_items[] = {
180         { "copy", BE_CH_LOWER_PERM_COPY },
181         { "swap", BE_CH_LOWER_PERM_SWAP },
182         { NULL, 0 }
183 };
184
185 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
186         { NULL, 0 }
187 };
188
189 static const lc_opt_enum_int_items_t dump_items[] = {
190         { "spill",    BE_CH_DUMP_SPILL     },
191         { "live",     BE_CH_DUMP_LIVE      },
192         { "color",    BE_CH_DUMP_COLOR     },
193         { "copymin",  BE_CH_DUMP_COPYMIN   },
194         { "ssadestr", BE_CH_DUMP_SSADESTR  },
195         { "tree",     BE_CH_DUMP_TREE_INTV },
196         { "constr",   BE_CH_DUMP_CONSTR    },
197         { "lower",    BE_CH_DUMP_LOWER     },
198         { "all",      BE_CH_DUMP_ALL       },
199         { NULL, 0 }
200 };
201
202 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
203         { "off",    BE_CH_VRFY_OFF    },
204         { "warn",   BE_CH_VRFY_WARN   },
205         { "assert", BE_CH_VRFY_ASSERT },
206         { NULL, 0 }
207 };
208
209 static lc_opt_enum_int_var_t spill_var = {
210         &options.spill_method, spill_items
211 };
212
213 static lc_opt_enum_int_var_t copymin_var = {
214         &options.copymin_method, copymin_items
215 };
216
217 static lc_opt_enum_int_var_t ifg_flavor_var = {
218         &options.ifg_flavor, ifg_flavor_items
219 };
220
221 static lc_opt_enum_int_var_t lower_perm_var = {
222         &options.lower_perm_opt, lower_perm_items
223 };
224
225 static lc_opt_enum_int_var_t dump_var = {
226         &options.dump_flags, dump_items
227 };
228
229 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
230         &options.vrfy_option, be_ch_vrfy_items
231 };
232
233 static const lc_opt_table_entry_t be_chordal_options[] = {
234         LC_OPT_ENT_ENUM_MASK("spill",   "spill method (belady, ilp, remat or appel)", &spill_var),
235         LC_OPT_ENT_ENUM_PTR ("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", &copymin_var),
236         LC_OPT_ENT_ENUM_PTR ("ifg",     "interference graph flavour (std, fast, clique, pointer, list, check)", &ifg_flavor_var),
237         LC_OPT_ENT_ENUM_PTR ("perm",    "perm lowering options (copy or swap)", &lower_perm_var),
238         LC_OPT_ENT_ENUM_MASK("dump",    "select dump phases", &dump_var),
239         LC_OPT_ENT_ENUM_PTR ("vrfy",    "verify options (off, warn, assert)", &be_ch_vrfy_var),
240         { NULL }
241 };
242
243 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
244 {
245         static int run_once = 0;
246         lc_opt_entry_t *chordal_grp;
247
248         if (! run_once) {
249                 run_once    = 1;
250                 chordal_grp = lc_opt_get_grp(grp, "chordal");
251
252                 lc_opt_add_table(chordal_grp, be_chordal_options);
253         }
254
255         co_register_options(chordal_grp);
256 }
257 #endif /* WITH_LIBCORE */
258
259 static void dump(unsigned mask, ir_graph *irg,
260                                  const arch_register_class_t *cls,
261                                  const char *suffix,
262                                  void (*dump_func)(ir_graph *, const char *))
263 {
264         if((options.dump_flags & mask) == mask) {
265                 if (cls) {
266                         char buf[256];
267                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
268                         be_dump(irg, buf, dump_func);
269                 }
270                 else
271                         be_dump(irg, suffix, dump_func);
272         }
273 }
274
275 static void put_ignore_colors(be_chordal_env_t *chordal_env)
276 {
277         int n_colors = chordal_env->cls->n_regs;
278         int i;
279
280         bitset_clear_all(chordal_env->ignore_colors);
281         be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
282         for(i = 0; i < n_colors; ++i)
283                 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
284                         bitset_set(chordal_env->ignore_colors, i);
285 }
286
287 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
288 {
289         char buf[1024];
290
291         ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
292         return fopen(buf, "wt");
293 }
294
295 void check_ifg_implementations(be_chordal_env_t *chordal_env)
296 {
297         FILE *f;
298
299         f = be_chordal_open(chordal_env, "std", "log");
300         chordal_env->ifg = be_ifg_std_new(chordal_env);
301         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
302         fclose(f);
303
304         f = be_chordal_open(chordal_env, "list", "log");
305         be_ifg_free(chordal_env->ifg);
306         chordal_env->ifg = be_ifg_list_new(chordal_env);
307         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
308         fclose(f);
309
310         f = be_chordal_open(chordal_env, "clique", "log");
311         be_ifg_free(chordal_env->ifg);
312         chordal_env->ifg = be_ifg_clique_new(chordal_env);
313         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
314         fclose(f);
315
316         f = be_chordal_open(chordal_env, "pointer", "log");
317         be_ifg_free(chordal_env->ifg);
318         chordal_env->ifg = be_ifg_pointer_new(chordal_env);
319         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
320         fclose(f);
321
322         chordal_env->ifg = NULL;
323 };
324
325 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
326 {
327         const be_main_env_t *main_env  = bi->main_env;
328         const arch_isa_t    *isa       = arch_env_get_isa(main_env->arch_env);
329         ir_graph            *irg       = bi->irg;
330         be_options_t        *main_opts = main_env->options;
331         copy_opt_t          *co;
332
333         int j, m;
334         be_chordal_env_t chordal_env;
335
336         if (main_opts->timing == BE_TIME_ON) {
337                 ra_timer.t_prolog  = lc_timer_register("prolog",   "regalloc prolog");
338                 ra_timer.t_epilog  = lc_timer_register("epilog",   "regalloc epilog");
339                 ra_timer.t_live    = lc_timer_register("liveness", "be liveness");
340                 ra_timer.t_spill   = lc_timer_register("spill",    "spiller");
341                 ra_timer.t_color   = lc_timer_register("color",    "graph coloring");
342                 ra_timer.t_ifg     = lc_timer_register("ifg",      "build interference graph");
343                 ra_timer.t_copymin = lc_timer_register("copymin",  "copy minimization");
344                 ra_timer.t_ssa     = lc_timer_register("ssadestr", "ssa destruction");
345
346                 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
347                 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
348                 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
349                 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
350                 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
351                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
352                 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
353                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
354         }
355
356 #define BE_TIME_START(timer) if (main_opts->timing == BE_TIME_ON) lc_timer_start(timer)
357 #define BE_TIME_STOP(timer)  if (main_opts->timing == BE_TIME_ON) lc_timer_stop(timer)
358
359         BE_TIME_START(ra_timer.t_prolog);
360
361         compute_doms(irg);
362
363         chordal_env.opts          = &options;
364         chordal_env.irg           = irg;
365         chordal_env.birg          = bi;
366         chordal_env.dom_front     = be_compute_dominance_frontiers(irg);
367         FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
368
369         obstack_init(&chordal_env.obst);
370
371         BE_TIME_STOP(ra_timer.t_prolog);
372
373         /* Perform the following for each register class. */
374         for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
375                 FILE *f;
376                 chordal_env.cls           = arch_isa_get_reg_class(isa, j);
377                 chordal_env.border_heads  = pmap_create();
378                 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
379
380                 BE_TIME_START(ra_timer.t_live);
381
382                 /* put all ignore registers into the ignore register set. */
383                 put_ignore_colors(&chordal_env);
384
385                 be_liveness(irg);
386                 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
387
388                 BE_TIME_STOP(ra_timer.t_live);
389                 BE_TIME_START(ra_timer.t_spill);
390
391                 /* spilling */
392                 switch(options.spill_method) {
393                 case BE_CH_SPILL_MORGAN:
394                         be_spill_morgan(&chordal_env);
395                         break;
396                 case BE_CH_SPILL_BELADY:
397                         be_spill_belady(&chordal_env);
398                         break;
399 #ifdef WITH_ILP
400                 case BE_CH_SPILL_ILP:
401                         be_spill_ilp(&chordal_env);
402                         break;
403                 case BE_CH_SPILL_REMAT:
404                         be_spill_remat(&chordal_env);
405                         break;
406                 case BE_CH_SPILL_APPEL:
407                         be_spill_appel(&chordal_env);
408                         break;
409 #endif /* WITH_ILP */
410                 default:
411                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
412                         be_spill_belady(&chordal_env);
413                 }
414                 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
415                 be_abi_fix_stack_nodes(bi->abi);
416
417                 /* verify schedule and register pressure */
418                 if (options.vrfy_option == BE_CH_VRFY_WARN) {
419                         be_verify_schedule(irg);
420                         be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
421                 }
422                 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
423                         assert(be_verify_schedule(irg) && "Schedule verification failed");
424                         assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
425                                 && "Register pressure verification failed");
426                 }
427
428                 BE_TIME_STOP(ra_timer.t_spill);
429                 BE_TIME_START(ra_timer.t_live);
430                 be_liveness(irg);
431                 BE_TIME_STOP(ra_timer.t_live);
432
433                 /* Color the graph. */
434                 BE_TIME_START(ra_timer.t_color);
435                 be_ra_chordal_color(&chordal_env);
436                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
437
438                 BE_TIME_STOP(ra_timer.t_color);
439                 BE_TIME_START(ra_timer.t_ifg);
440                 //be_ifg_check_performance(&chordal_env);
441
442                 /* Create the ifg with the selected flavor */
443                 switch (options.ifg_flavor) {
444                         default:
445                                 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
446                         case BE_CH_IFG_STD:
447                         case BE_CH_IFG_FAST:
448                                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
449                                 break;
450                         case BE_CH_IFG_CLIQUE:
451                                 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
452                                 break;
453                         case BE_CH_IFG_POINTER:
454                                 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
455                                 break;
456                         case BE_CH_IFG_LIST:
457                                 chordal_env.ifg = be_ifg_list_new(&chordal_env);
458                                 break;
459                         case BE_CH_IFG_CHECK:
460                                 check_ifg_implementations(&chordal_env);
461                                 /* Build the interference graph. */
462                                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
463                                 //be_ifg_check(chordal_env.ifg);
464                                 //be_ifg_check_sorted(chordal_env.ifg);
465                                 break;
466                 }
467
468
469 #if 0
470                 {
471                         be_ifg_t *std = be_ifg_std_new(&chordal_env);
472                         f = be_chordal_open(&chordal_env, "std", "csv");
473                         be_ifg_check_sorted_to_file(std, f);
474                         be_ifg_free(std);
475                         fclose(f);
476                 }
477
478                 f = be_chordal_open(&chordal_env, "clique", "csv");
479                 be_ifg_check_sorted_to_file(chordal_env.ifg, f);
480                 fclose(f);
481 #endif
482                 if (options.vrfy_option != BE_CH_VRFY_OFF)
483                         be_ra_chordal_check(&chordal_env);
484
485 //              be_ifg_check_sorted(chordal_env.ifg);
486
487                 BE_TIME_STOP(ra_timer.t_ifg);
488                 BE_TIME_START(ra_timer.t_copymin);
489
490                 /* copy minimization */
491                 co = NULL;
492                 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
493                         co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
494                         co_build_ou_structure(co);
495                         co_build_graph_structure(co);
496                 }
497
498                 switch(options.copymin_method) {
499                         case BE_CH_COPYMIN_HEUR1:
500                                 co_solve_heuristic(co);
501                                 break;
502                         case BE_CH_COPYMIN_HEUR2:
503                                 co_solve_heuristic_new(co);
504                                 break;
505                         case BE_CH_COPYMIN_PARK_MOON:
506                                 co_solve_park_moon(co);
507                                 break;
508                         case BE_CH_COPYMIN_STAT:
509                                 co_compare_solvers(&chordal_env);
510                                 break;
511 #ifdef WITH_ILP
512                         case BE_CH_COPYMIN_ILP1:
513                                 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
514                                 co_solve_ilp1(co, 60.0);
515                                 break;
516                         case BE_CH_COPYMIN_ILP2:
517                                 co_solve_ilp2(co, 60.0);
518                                 break;
519 #endif /* WITH_ILP */
520                         case BE_CH_COPYMIN_NONE:
521                         default:
522                                 break;
523                 }
524
525                 if (co) {
526                         co_free_graph_structure(co);
527                         co_free_ou_structure(co);
528                         free_copy_opt(co);
529                 }
530
531                 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
532                 if (options.vrfy_option != BE_CH_VRFY_OFF)
533                         be_ra_chordal_check(&chordal_env);
534
535                 BE_TIME_STOP(ra_timer.t_copymin);
536                 BE_TIME_START(ra_timer.t_ssa);
537
538                 /* ssa destruction */
539                 be_ssa_destruction(&chordal_env);
540                 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
541                 if (options.vrfy_option != BE_CH_VRFY_OFF) {
542                         be_ssa_destruction_check(&chordal_env);
543                         be_ra_chordal_check(&chordal_env);
544                 }
545
546                 if (options.copymin_method == BE_CH_COPYMIN_STAT)
547                         copystat_dump(irg);
548
549                 be_ifg_free(chordal_env.ifg);
550                 pmap_destroy(chordal_env.border_heads);
551                 bitset_free(chordal_env.ignore_colors);
552
553                 BE_TIME_STOP(ra_timer.t_ssa);
554         }
555
556         BE_TIME_START(ra_timer.t_epilog);
557
558         be_compute_spill_offsets(&chordal_env);
559
560         dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
561
562         lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
563         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
564
565         obstack_free(&chordal_env.obst, NULL);
566         be_free_dominance_frontiers(chordal_env.dom_front);
567
568         BE_TIME_STOP(ra_timer.t_epilog);
569
570         if (main_opts->timing == BE_TIME_ON)
571                 return &ra_timer;
572         else
573                 return NULL;
574 }
575
576 const be_ra_t be_ra_chordal_allocator = {
577 #ifdef WITH_LIBCORE
578         be_ra_chordal_register_options,
579 #endif
580         be_ra_chordal_main
581 };