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