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