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