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