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