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