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