removed warning for ILP scheduler, as the scheduler can now schedule on it's own
[libfirm] / ir / be / bechordal_main.c
1 /**
2  * @file   bechordal_main.c
3  * @date   29.11.2005
4  * @author Sebastian Hack
5  * @cvs-id $Id$
6  *
7  * Copyright (C) 2005-2006 Universitaet Karlsruhe
8  * Released under the GPL
9  *
10  * Driver for the chordal register allocator.
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <time.h>
17
18 #include "obst.h"
19 #include "pset.h"
20 #include "list.h"
21 #include "bitset.h"
22 #include "iterator.h"
23 #include "firm_config.h"
24
25 #ifdef WITH_LIBCORE
26 #include <libcore/lc_opts.h>
27 #include <libcore/lc_opts_enum.h>
28 #include <libcore/lc_timing.h>
29 #endif /* WITH_LIBCORE */
30
31 #include "ircons_t.h"
32 #include "irmode_t.h"
33 #include "irgraph_t.h"
34 #include "irprintf_t.h"
35 #include "irgwalk.h"
36 #include "ircons.h"
37 #include "irdump.h"
38 #include "irdom.h"
39 #include "ircons.h"
40 #include "irbitset.h"
41 #include "irnode.h"
42 #include "ircons.h"
43 #include "debug.h"
44 #include "xmalloc.h"
45 #include "execfreq.h"
46
47 #include "bechordal_t.h"
48 #include "beabi.h"
49 #include "bejavacoal.h"
50 #include "beutil.h"
51 #include "besched.h"
52 #include "benumb_t.h"
53 #include "besched_t.h"
54 #include "belive_t.h"
55 #include "bearch.h"
56 #include "beifg_t.h"
57 #include "beifg_impl.h"
58 #include "benode_t.h"
59 #include "bestatevent.h"
60 #include "bestat.h"
61
62 #include "bespillbelady.h"
63 #include "bespillmorgan.h"
64 #include "bespillslots.h"
65 #include "bespilloptions.h"
66 #include "belower.h"
67
68 #ifdef WITH_ILP
69 #include "bespillremat.h"
70 #endif /* WITH_ILP */
71
72 #include "bejavacoal.h"
73 #include "becopystat.h"
74 #include "becopyopt.h"
75 #include "bessadestr.h"
76 #include "beverify.h"
77 #include "bespillcost.h"
78 #include "benode_t.h"
79
80 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
81         const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
82         struct obstack ob;
83         pmap_entry *pme;
84         ir_node **nodes, *n1, *n2;
85         int i, o;
86         DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
87
88         /* Collect all irns */
89         obstack_init(&ob);
90         pmap_foreach(chordal_env->border_heads, pme) {
91                 border_t *curr;
92                 struct list_head *head = pme->value;
93                 list_for_each_entry(border_t, curr, head, list)
94                         if (curr->is_def && curr->is_real)
95                                 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
96                                         obstack_ptr_grow(&ob, curr->irn);
97         }
98         obstack_ptr_grow(&ob, NULL);
99         nodes = (ir_node **) obstack_finish(&ob);
100
101         /* Check them */
102         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
103                 const arch_register_t *n1_reg, *n2_reg;
104
105                 n1_reg = arch_get_irn_register(arch_env, n1);
106                 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
107                         DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
108                         assert(0 && "Register constraint does not hold");
109                 }
110                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
111                         n2_reg = arch_get_irn_register(arch_env, n2);
112                         if (values_interfere(chordal_env->lv, n1, n2) && n1_reg == n2_reg) {
113                                 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
114                                 assert(0 && "Interfering values have the same color!");
115                         }
116                 }
117         }
118         obstack_free(&ob, NULL);
119 }
120
121 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
122 {
123         if(env->ifg)
124                 return be_ifg_connected(env->ifg, a, b);
125         else
126                 return values_interfere(env->lv, a, b);
127 }
128
129
130 static be_ra_chordal_opts_t options = {
131         BE_CH_DUMP_NONE,
132         BE_CH_SPILL_BELADY,
133         BE_CH_IFG_STD,
134         BE_CH_LOWER_PERM_SWAP,
135         BE_CH_VRFY_WARN,
136 };
137
138 /** Enable extreme live range splitting. */
139 static int be_elr_split = 0;
140
141 /** Assumed loop iteration count for execution frequency estimation. */
142 static int be_loop_weight = 9;
143
144 #ifdef WITH_LIBCORE
145 static be_ra_timer_t ra_timer = {
146         NULL,
147         NULL,
148         NULL,
149         NULL,
150         NULL,
151         NULL,
152         NULL,
153         NULL,
154         NULL,
155         NULL,
156         NULL,
157 };
158
159 static const lc_opt_enum_int_items_t spill_items[] = {
160         { "morgan", BE_CH_SPILL_MORGAN },
161         { "belady", BE_CH_SPILL_BELADY },
162 #ifdef WITH_ILP
163         { "remat",  BE_CH_SPILL_REMAT },
164 #endif /* WITH_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         { "clique",  BE_CH_IFG_CLIQUE  },
172         { "pointer", BE_CH_IFG_POINTER },
173         { "list",    BE_CH_IFG_LIST    },
174         { "check",   BE_CH_IFG_CHECK   },
175         { NULL,      0                 }
176 };
177
178 static const lc_opt_enum_int_items_t lower_perm_items[] = {
179         { "copy", BE_CH_LOWER_PERM_COPY },
180         { "swap", BE_CH_LOWER_PERM_SWAP },
181         { NULL, 0 }
182 };
183
184 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
185         { NULL, 0 }
186 };
187
188 static const lc_opt_enum_int_items_t dump_items[] = {
189         { "spill",      BE_CH_DUMP_SPILL      },
190         { "live",       BE_CH_DUMP_LIVE       },
191         { "color",      BE_CH_DUMP_COLOR      },
192         { "copymin",    BE_CH_DUMP_COPYMIN    },
193         { "ssadestr",   BE_CH_DUMP_SSADESTR   },
194         { "tree",       BE_CH_DUMP_TREE_INTV  },
195         { "constr",     BE_CH_DUMP_CONSTR     },
196         { "lower",      BE_CH_DUMP_LOWER      },
197         { "spillslots", BE_CH_DUMP_SPILLSLOTS },
198         { "appel",      BE_CH_DUMP_APPEL      },
199         { "all",        BE_CH_DUMP_ALL        },
200         { NULL, 0 }
201 };
202
203 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
204         { "off",    BE_CH_VRFY_OFF    },
205         { "warn",   BE_CH_VRFY_WARN   },
206         { "assert", BE_CH_VRFY_ASSERT },
207         { NULL, 0 }
208 };
209
210 static lc_opt_enum_int_var_t spill_var = {
211         &options.spill_method, spill_items
212 };
213
214 static lc_opt_enum_int_var_t ifg_flavor_var = {
215         &options.ifg_flavor, ifg_flavor_items
216 };
217
218 static lc_opt_enum_int_var_t lower_perm_var = {
219         &options.lower_perm_opt, lower_perm_items
220 };
221
222 static lc_opt_enum_int_var_t dump_var = {
223         &options.dump_flags, dump_items
224 };
225
226 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
227         &options.vrfy_option, be_ch_vrfy_items
228 };
229
230 static const lc_opt_table_entry_t be_chordal_options[] = {
231         LC_OPT_ENT_ENUM_INT ("spill",         "spill method", &spill_var),
232         LC_OPT_ENT_ENUM_PTR ("ifg",           "interference graph flavour", &ifg_flavor_var),
233         LC_OPT_ENT_ENUM_PTR ("perm",          "perm lowering options", &lower_perm_var),
234         LC_OPT_ENT_ENUM_MASK("dump",          "select dump phases", &dump_var),
235         LC_OPT_ENT_ENUM_PTR ("vrfy",          "verify options", &be_ch_vrfy_var),
236         LC_OPT_ENT_BOOL     ("elrsplit",      "enable extreme live range splitting", &be_elr_split),
237         LC_OPT_ENT_INT      ("loop_weight",   "assumed amount of loop iterations for guessing the execution frequency", &be_loop_weight),
238         { NULL }
239 };
240
241 extern void be_spill_remat_register_options(lc_opt_entry_t *ent);
242
243
244 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
245 {
246         static int run_once = 0;
247         lc_opt_entry_t *chordal_grp;
248
249         if (! run_once) {
250                 run_once    = 1;
251                 chordal_grp = lc_opt_get_grp(grp, "chordal");
252
253                 lc_opt_add_table(chordal_grp, be_chordal_options);
254
255                 co_register_options(chordal_grp);
256 #ifdef WITH_JVM
257                 be_java_coal_register_options(chordal_grp);
258 #endif
259 #ifdef WITH_ILP
260                 be_spill_remat_register_options(chordal_grp);
261 #endif
262                 be_spill_register_options(chordal_grp);
263         }
264 }
265 #endif /* WITH_LIBCORE */
266
267 static void dump(unsigned mask, ir_graph *irg,
268                                  const arch_register_class_t *cls,
269                                  const char *suffix,
270                                  void (*dump_func)(ir_graph *, const char *))
271 {
272         if((options.dump_flags & mask) == mask) {
273                 if (cls) {
274                         char buf[256];
275                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
276                         be_dump(irg, buf, dump_func);
277                 }
278                 else
279                         be_dump(irg, suffix, dump_func);
280         }
281 }
282
283 static void put_ignore_colors(be_chordal_env_t *chordal_env)
284 {
285         int n_colors = chordal_env->cls->n_regs;
286         int i;
287
288         bitset_clear_all(chordal_env->ignore_colors);
289         be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
290         for(i = 0; i < n_colors; ++i)
291                 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
292                         bitset_set(chordal_env->ignore_colors, i);
293 }
294
295 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
296 {
297         char buf[1024];
298
299         ir_snprintf(buf, sizeof(buf), "%s%F_%s%s", prefix, env->irg, env->cls->name, suffix);
300         return fopen(buf, "wt");
301 }
302
303 void check_ifg_implementations(be_chordal_env_t *chordal_env)
304 {
305         FILE *f;
306
307         f = be_chordal_open(chordal_env, "std", ".log");
308         chordal_env->ifg = be_ifg_std_new(chordal_env);
309         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
310         fclose(f);
311
312         f = be_chordal_open(chordal_env, "list", ".log");
313         be_ifg_free(chordal_env->ifg);
314         chordal_env->ifg = be_ifg_list_new(chordal_env);
315         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
316         fclose(f);
317
318         f = be_chordal_open(chordal_env, "clique", ".log");
319         be_ifg_free(chordal_env->ifg);
320         chordal_env->ifg = be_ifg_clique_new(chordal_env);
321         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
322         fclose(f);
323
324         f = be_chordal_open(chordal_env, "pointer", ".log");
325         be_ifg_free(chordal_env->ifg);
326         chordal_env->ifg = be_ifg_pointer_new(chordal_env);
327         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
328         fclose(f);
329
330         chordal_env->ifg = NULL;
331 };
332
333 /**
334  * Checks for every reload if it's user can perform the load on itself.
335  */
336 static void memory_operand_walker(ir_node *irn, void *env) {
337         be_chordal_env_t *cenv = env;
338         const arch_env_t *aenv = cenv->birg->main_env->arch_env;
339         const ir_edge_t  *edge, *ne;
340         ir_node          *block;
341         ir_node          *spill;
342
343         if (! be_is_Reload(irn))
344                 return;
345
346         /* always use addressmode, it's good for x86 */
347 #if 0
348         /* only use memory operands, if the reload is only used by 1 node */
349         if(get_irn_n_edges(irn) > 1)
350                 return;
351 #endif
352
353         spill = be_get_Reload_mem(irn);
354         block = get_nodes_block(irn);
355
356         foreach_out_edge_safe(irn, edge, ne) {
357                 ir_node *src = get_edge_src_irn(edge);
358                 int     pos  = get_edge_src_pos(edge);
359
360                 assert(src && "outedges broken!");
361
362                 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
363                         DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
364                         arch_perform_memory_operand(aenv, src, spill, pos);
365                 }
366         }
367
368         /* kill the Reload */
369         if (get_irn_n_edges(irn) == 0) {
370                 sched_remove(irn);
371                 set_irn_n(irn, 0, new_Bad());
372                 set_irn_n(irn, 1, new_Bad());
373         }
374 }
375
376 /**
377  * Starts a walk for memory operands if supported by the backend.
378  */
379 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
380         irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
381 }
382
383 /**
384  * Sorry for doing stats again...
385  */
386 typedef struct _node_stat_t {
387         unsigned int n_phis;      /**< Phis of the current register class. */
388         unsigned int n_mem_phis;  /**< Memory Phis (Phis with spill operands). */
389         unsigned int n_copies;    /**< Copies */
390         unsigned int n_perms;     /**< Perms */
391         unsigned int n_spills;    /**< Spill nodes */
392         unsigned int n_reloads;   /**< Reloads */
393 } node_stat_t;
394
395 struct node_stat_walker {
396         node_stat_t *stat;
397         const be_chordal_env_t *cenv;
398         bitset_t *mem_phis;
399 };
400
401 static void node_stat_walker(ir_node *irn, void *data)
402 {
403         struct node_stat_walker *env = data;
404         const arch_env_t *aenv       = env->cenv->birg->main_env->arch_env;
405
406         if(arch_irn_consider_in_reg_alloc(aenv, env->cenv->cls, irn)) {
407
408                 /* if the node is a normal phi */
409                 if(is_Phi(irn))
410                         env->stat->n_phis++;
411
412                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_spill)
413                         ++env->stat->n_spills;
414
415                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_reload)
416                         ++env->stat->n_reloads;
417
418                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_copy)
419                         ++env->stat->n_copies;
420
421                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_perm)
422                         ++env->stat->n_perms;
423         }
424
425         /* a mem phi is a PhiM with a mem phi operand or a Spill operand */
426         else if(is_Phi(irn) && get_irn_mode(irn) == mode_M) {
427                 int i;
428
429                 for(i = get_irn_arity(irn) - 1; i >= 0; --i) {
430                         ir_node *op = get_irn_n(irn, i);
431
432                         if((is_Phi(op) && bitset_contains_irn(env->mem_phis, op)) || (arch_irn_classify(aenv, op) & arch_irn_class_spill)) {
433                                 bitset_add_irn(env->mem_phis, irn);
434                                 env->stat->n_mem_phis++;
435                                 break;
436                         }
437                 }
438         }
439 }
440
441 static void node_stats(const be_chordal_env_t *cenv, node_stat_t *stat)
442 {
443         struct node_stat_walker env;
444
445         memset(stat, 0, sizeof(stat[0]));
446         env.cenv     = cenv;
447         env.mem_phis = bitset_irg_malloc(cenv->irg);
448         env.stat     = stat;
449         irg_walk_graph(cenv->irg, NULL, node_stat_walker, &env);
450         bitset_free(env.mem_phis);
451 }
452
453 static void insn_count_walker(ir_node *irn, void *data)
454 {
455         int *cnt = data;
456
457         switch(get_irn_opcode(irn)) {
458         case iro_Proj:
459         case iro_Phi:
460         case iro_Start:
461         case iro_End:
462                 break;
463         default:
464                 (*cnt)++;
465         }
466 }
467
468 static unsigned int count_insns(ir_graph *irg)
469 {
470         int cnt = 0;
471         irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
472         return cnt;
473 }
474
475 #ifdef WITH_LIBCORE
476 /**
477  * Initialize all timers.
478  */
479 static void be_init_timer(be_options_t *main_opts)
480 {
481         if (main_opts->timing == BE_TIME_ON) {
482                 ra_timer.t_prolog     = lc_timer_register("ra_prolog",     "regalloc prolog");
483                 ra_timer.t_epilog     = lc_timer_register("ra_epilog",     "regalloc epilog");
484                 ra_timer.t_live       = lc_timer_register("ra_liveness",   "be liveness");
485                 ra_timer.t_spill      = lc_timer_register("ra_spill",      "spiller");
486                 ra_timer.t_spillslots = lc_timer_register("ra_spillslots", "spillslots");
487                 ra_timer.t_color      = lc_timer_register("ra_color",      "graph coloring");
488                 ra_timer.t_ifg        = lc_timer_register("ra_ifg",        "interference graph");
489                 ra_timer.t_copymin    = lc_timer_register("ra_copymin",    "copy minimization");
490                 ra_timer.t_ssa        = lc_timer_register("ra_ssadestr",   "ssa destruction");
491                 ra_timer.t_verify     = lc_timer_register("ra_verify",     "graph verification");
492                 ra_timer.t_other      = lc_timer_register("ra_other",      "other time");
493
494                 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
495                 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
496                 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
497                 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
498                 LC_STOP_AND_RESET_TIMER(ra_timer.t_spillslots);
499                 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
500                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
501                 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
502                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
503                 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
504                 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
505         }
506 }
507
508 #define BE_TIMER_INIT(main_opts)        be_init_timer(main_opts)
509
510 #define BE_TIMER_PUSH(timer)                                                            \
511         if (main_opts->timing == BE_TIME_ON) {                                              \
512                 if (! lc_timer_push(timer)) {                                                   \
513                         if (options.vrfy_option == BE_CH_VRFY_ASSERT)                               \
514                                 assert(!"Timer already on stack, cannot be pushed twice.");             \
515                         else if (options.vrfy_option == BE_CH_VRFY_WARN)                            \
516                                 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
517                                         lc_timer_get_name(timer));                                          \
518                 }                                                                               \
519         }
520 #define BE_TIMER_POP(timer)                                                                    \
521         if (main_opts->timing == BE_TIME_ON) {                                                     \
522                 lc_timer_t *tmp = lc_timer_pop();                                                      \
523                 if (options.vrfy_option == BE_CH_VRFY_ASSERT)                                          \
524                         assert(tmp == timer && "Attempt to pop wrong timer.");                             \
525                 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer)                       \
526                         fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
527                                 lc_timer_get_name(tmp), lc_timer_get_name(timer));                             \
528                 timer = tmp;                                                                           \
529         }
530 #else
531
532 #define BE_TIMER_INIT(main_opts)
533 #define BE_TIMER_PUSH(timer)
534 #define BE_TIMER_POP(timer)
535
536 #endif /* WITH_LIBCORE */
537
538 /**
539  * Performs chordal register allocation for each register class on given irg.
540  *
541  * @param bi  Backend irg object
542  * @return Structure containing timer for the single phases or NULL if no timing requested.
543  */
544 static be_ra_timer_t *be_ra_chordal_main(const be_irg_t *bi)
545 {
546         const be_main_env_t *main_env  = bi->main_env;
547         const arch_isa_t    *isa       = arch_env_get_isa(main_env->arch_env);
548         ir_graph            *irg       = bi->irg;
549         be_options_t        *main_opts = main_env->options;
550         int                   splitted = 0;
551
552         int j, m;
553         be_chordal_env_t chordal_env;
554
555         BE_TIMER_INIT(main_opts);
556         BE_TIMER_PUSH(ra_timer.t_other);
557         BE_TIMER_PUSH(ra_timer.t_prolog);
558
559         compute_doms(irg);
560
561         chordal_env.opts      = &options;
562         chordal_env.irg       = irg;
563         chordal_env.birg      = bi;
564         chordal_env.dom_front = be_compute_dominance_frontiers(irg);
565         chordal_env.exec_freq = bi->execfreqs;
566         chordal_env.lv        = be_liveness(irg);
567         FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
568
569         obstack_init(&chordal_env.obst);
570
571         BE_TIMER_POP(ra_timer.t_prolog);
572
573         be_stat_ev("insns_before", count_insns(irg));
574
575         /* Perform the following for each register class. */
576         for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
577                 node_stat_t node_stat;
578                 double spillcosts = 0;
579
580                 chordal_env.cls           = arch_isa_get_reg_class(isa, j);
581                 chordal_env.border_heads  = pmap_create();
582                 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
583
584                 if(be_stat_ev_is_active()) {
585                         be_stat_tags[STAT_TAG_CLS] = chordal_env.cls->name;
586                         be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
587
588                         /* perform some node statistics. */
589                         node_stats(&chordal_env, &node_stat);
590                         be_stat_ev("phis_before_spill", node_stat.n_phis);
591                 }
592
593                 /* put all ignore registers into the ignore register set. */
594                 put_ignore_colors(&chordal_env);
595
596                 BE_TIMER_PUSH(ra_timer.t_live);
597                 be_liveness_recompute(chordal_env.lv);
598                 BE_TIMER_POP(ra_timer.t_live);
599                 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
600
601                 be_pre_spill_prepare_constr(&chordal_env);
602                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-constr-pre", dump_ir_block_graph_sched);
603
604                 if(be_stat_ev_is_active()) {
605                         spillcosts = be_estimate_irg_costs(irg, main_env->arch_env, chordal_env.exec_freq);
606                 }
607
608                 BE_TIMER_PUSH(ra_timer.t_spill);
609
610                 /* spilling */
611                 switch(options.spill_method) {
612                 case BE_CH_SPILL_MORGAN:
613                         be_spill_morgan(&chordal_env);
614                         break;
615                 case BE_CH_SPILL_BELADY:
616                         be_spill_belady(&chordal_env);
617                         break;
618 #ifdef WITH_ILP
619                 case BE_CH_SPILL_REMAT:
620                         be_spill_remat(&chordal_env);
621                         break;
622 #endif /* WITH_ILP */
623                 default:
624                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
625                         be_spill_belady(&chordal_env);
626                 }
627
628                 BE_TIMER_POP(ra_timer.t_spill);
629
630                 if(be_stat_ev_is_active()) {
631                         spillcosts = be_estimate_irg_costs(irg, main_env->arch_env, chordal_env.exec_freq) - spillcosts;
632                         be_stat_ev_l("spillcosts", (long) spillcosts);
633
634                         node_stats(&chordal_env, &node_stat);
635                         be_stat_ev("phis_after_spill", node_stat.n_phis);
636                         be_stat_ev("mem_phis", node_stat.n_mem_phis);
637                         be_stat_ev("reloads", node_stat.n_reloads);
638                         be_stat_ev("spills", node_stat.n_spills);
639                 }
640
641                 DBG((chordal_env.dbg, LEVEL_1, "spill costs for %+F in regclass %s: %g\n",irg, chordal_env.cls->name, get_irg_spill_cost(&chordal_env)));
642
643                 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
644
645                 check_for_memory_operands(&chordal_env);
646
647                 be_abi_fix_stack_nodes(bi->abi, chordal_env.lv);
648
649                 BE_TIMER_PUSH(ra_timer.t_verify);
650
651                 /* verify schedule and register pressure */
652                 if (options.vrfy_option == BE_CH_VRFY_WARN) {
653                         be_verify_schedule(irg);
654                         be_verify_register_pressure(chordal_env.birg, chordal_env.cls, irg);
655                 }
656                 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
657                         assert(be_verify_schedule(irg) && "Schedule verification failed");
658                         assert(be_verify_register_pressure(chordal_env.birg, chordal_env.cls, irg)
659                                 && "Register pressure verification failed");
660                 }
661                 BE_TIMER_POP(ra_timer.t_verify);
662
663                 if (be_elr_split && ! splitted) {
664                         extreme_liverange_splitting(&chordal_env);
665                         splitted = 1;
666                 }
667
668
669                 /* Color the graph. */
670                 BE_TIMER_PUSH(ra_timer.t_color);
671                 be_ra_chordal_color(&chordal_env);
672                 BE_TIMER_POP(ra_timer.t_color);
673
674                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
675
676                 /* Create the ifg with the selected flavor */
677                 BE_TIMER_PUSH(ra_timer.t_ifg);
678                 switch (options.ifg_flavor) {
679                         default:
680                                 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
681                         case BE_CH_IFG_STD:
682                         case BE_CH_IFG_FAST:
683                                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
684                                 break;
685                         case BE_CH_IFG_CLIQUE:
686                                 chordal_env.ifg = be_ifg_clique_new(&chordal_env);
687                                 break;
688                         case BE_CH_IFG_POINTER:
689                                 chordal_env.ifg = be_ifg_pointer_new(&chordal_env);
690                                 break;
691                         case BE_CH_IFG_LIST:
692                                 chordal_env.ifg = be_ifg_list_new(&chordal_env);
693                                 break;
694                         case BE_CH_IFG_CHECK:
695                                 check_ifg_implementations(&chordal_env);
696                                 /* Build the interference graph. */
697                                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
698                                 break;
699                 }
700                 BE_TIMER_POP(ra_timer.t_ifg);
701
702                 if(be_stat_ev_is_active()) {
703                         be_ifg_stat_t stat;
704                         be_ifg_stat(&chordal_env, &stat);
705                         be_stat_ev("ifg_nodes", stat.n_nodes);
706                         be_stat_ev("ifg_edges", stat.n_edges);
707                         be_stat_ev("ifg_comps", stat.n_comps);
708                 }
709
710                 BE_TIMER_PUSH(ra_timer.t_verify);
711                 if (options.vrfy_option != BE_CH_VRFY_OFF) {
712                         //be_ra_chordal_check(&chordal_env);
713                 }
714
715                 BE_TIMER_POP(ra_timer.t_verify);
716
717                 if(be_stat_ev_is_active()) {
718                         node_stats(&chordal_env, &node_stat);
719                         be_stat_ev("perms_before_coal", node_stat.n_perms);
720                         be_stat_ev("copies_before_coal", node_stat.n_copies);
721                 }
722
723                 /* copy minimization */
724                 BE_TIMER_PUSH(ra_timer.t_copymin);
725                 co_driver(&chordal_env);
726                 BE_TIMER_POP(ra_timer.t_copymin);
727
728                 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
729
730                 BE_TIMER_PUSH(ra_timer.t_verify);
731
732                 if (options.vrfy_option != BE_CH_VRFY_OFF) {
733                         //be_ra_chordal_check(&chordal_env);
734                 }
735
736                 BE_TIMER_POP(ra_timer.t_verify);
737                 BE_TIMER_PUSH(ra_timer.t_ssa);
738
739                 /* ssa destruction */
740                 be_ssa_destruction(&chordal_env);
741
742                 BE_TIMER_POP(ra_timer.t_ssa);
743
744                 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
745
746                 BE_TIMER_PUSH(ra_timer.t_verify);
747                 if (options.vrfy_option != BE_CH_VRFY_OFF) {
748                         be_ssa_destruction_check(&chordal_env);
749                         //be_ra_chordal_check(&chordal_env);
750                 }
751                 BE_TIMER_POP(ra_timer.t_verify);
752
753                 be_ifg_free(chordal_env.ifg);
754                 pmap_destroy(chordal_env.border_heads);
755                 bitset_free(chordal_env.ignore_colors);
756
757                 if(be_stat_ev_is_active()) {
758                         node_stats(&chordal_env, &node_stat);
759                         be_stat_ev("perms_after_coal", node_stat.n_perms);
760                         be_stat_ev("copies_after_coal", node_stat.n_copies);
761                 }
762
763                 be_stat_ev_pop();
764         }
765
766         BE_TIMER_PUSH(ra_timer.t_spillslots);
767
768         be_coalesce_spillslots(&chordal_env);
769         dump(BE_CH_DUMP_SPILLSLOTS, irg, NULL, "-spillslots", dump_ir_block_graph_sched);
770
771         BE_TIMER_POP(ra_timer.t_spillslots);
772
773         BE_TIMER_PUSH(ra_timer.t_verify);
774
775         /* verify spillslots */
776         if (options.vrfy_option == BE_CH_VRFY_WARN) {
777                 be_verify_spillslots(main_env->arch_env, irg);
778         }
779         else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
780                 assert(be_verify_spillslots(main_env->arch_env, irg) && "Spillslot verification failed");
781         }
782         BE_TIMER_POP(ra_timer.t_verify);
783
784         BE_TIMER_PUSH(ra_timer.t_epilog);
785
786         dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
787
788         lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
789         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
790
791         obstack_free(&chordal_env.obst, NULL);
792         be_free_dominance_frontiers(chordal_env.dom_front);
793         be_liveness_free(chordal_env.lv);
794
795         BE_TIMER_POP(ra_timer.t_epilog);
796         BE_TIMER_POP(ra_timer.t_other);
797
798         be_stat_ev("insns_after", count_insns(irg));
799
800 #ifdef WITH_LIBCORE
801         return main_opts->timing == BE_TIME_ON ? &ra_timer : NULL;
802 #endif /* WITH_LIBCORE */
803         return NULL;
804 }
805
806 const be_ra_t be_ra_chordal_allocator = {
807 #ifdef WITH_LIBCORE
808         be_ra_chordal_register_options,
809 #else
810         NULL,
811 #endif
812         be_ra_chordal_main,
813 };