39755ed7c4eb56a63726da8aede5e4111727c08b
[libfirm] / ir / be / bechordal_main.c
1 /**
2  * @file   bechordal_main.c
3  * @date   29.11.2005
4  * @author Sebastian Hack
5  * @cvs-id $Id$
6  *
7  * Copyright (C) 2005-2006 Universitaet Karlsruhe
8  * Released under the GPL
9  *
10  * Driver for the chordal register allocator.
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <time.h>
17
18 #include "obst.h"
19 #include "pset.h"
20 #include "list.h"
21 #include "bitset.h"
22 #include "iterator.h"
23 #include "firm_config.h"
24
25 #ifdef WITH_LIBCORE
26 #include <libcore/lc_opts.h>
27 #include <libcore/lc_opts_enum.h>
28 #include <libcore/lc_timing.h>
29 #endif /* WITH_LIBCORE */
30
31 #include "ircons_t.h"
32 #include "irmode_t.h"
33 #include "irgraph_t.h"
34 #include "irprintf_t.h"
35 #include "irgwalk.h"
36 #include "ircons.h"
37 #include "irdump.h"
38 #include "irdom.h"
39 #include "ircons.h"
40 #include "irbitset.h"
41 #include "irnode.h"
42 #include "ircons.h"
43 #include "debug.h"
44 #include "xmalloc.h"
45 #include "execfreq.h"
46
47 #include "bechordal_t.h"
48 #include "beabi.h"
49 #include "bejavacoal.h"
50 #include "beutil.h"
51 #include "besched.h"
52 #include "besched_t.h"
53 #include "belive_t.h"
54 #include "bearch.h"
55 #include "beifg_t.h"
56 #include "beifg_impl.h"
57 #include "benode_t.h"
58 #include "bestatevent.h"
59 #include "bestat.h"
60 #include "bemodule.h"
61
62 #include "bespillbelady.h"
63 #include "bespillmorgan.h"
64 #include "bespillslots.h"
65 #include "bespilloptions.h"
66 #include "belower.h"
67
68 #ifdef WITH_ILP
69 #include "bespillremat.h"
70 #endif /* WITH_ILP */
71
72 #include "bejavacoal.h"
73 #include "becopystat.h"
74 #include "becopyopt.h"
75 #include "bessadestr.h"
76 #include "beverify.h"
77 #include "benode_t.h"
78
79 static be_ra_chordal_opts_t options = {
80         BE_CH_DUMP_NONE,
81         BE_CH_SPILL_BELADY,
82         BE_CH_IFG_STD,
83         BE_CH_LOWER_PERM_SWAP,
84         BE_CH_VRFY_WARN,
85 };
86
87 /** Enable extreme live range splitting. */
88 static int be_elr_split = 0;
89
90 /** Assumed loop iteration count for execution frequency estimation. */
91 static int be_loop_weight = 9;
92
93 typedef struct _post_spill_env_t {
94         be_chordal_env_t cenv;
95         double           pre_spill_cost;
96 } post_spill_env_t;
97
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         { "belady", BE_CH_SPILL_BELADY },
114         { "morgan", BE_CH_SPILL_MORGAN },
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 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
195         const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
196         struct obstack ob;
197         pmap_entry *pme;
198         ir_node **nodes, *n1, *n2;
199         int i, o;
200         be_lv_t *lv = chordal_env->birg->lv;
201         DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
202
203         /* Collect all irns */
204         obstack_init(&ob);
205         pmap_foreach(chordal_env->border_heads, pme) {
206                 border_t *curr;
207                 struct list_head *head = pme->value;
208                 list_for_each_entry(border_t, curr, head, list)
209                         if (curr->is_def && curr->is_real)
210                                 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
211                                         obstack_ptr_grow(&ob, curr->irn);
212         }
213         obstack_ptr_grow(&ob, NULL);
214         nodes = (ir_node **) obstack_finish(&ob);
215
216         /* Check them */
217         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
218                 const arch_register_t *n1_reg, *n2_reg;
219
220                 n1_reg = arch_get_irn_register(arch_env, n1);
221                 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
222                         DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
223                         assert(0 && "Register constraint does not hold");
224                 }
225                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
226                         n2_reg = arch_get_irn_register(arch_env, n2);
227                         if (values_interfere(lv, n1, n2) && n1_reg == n2_reg) {
228                                 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned: %s\n", n1, n2, n1_reg->name));
229                                 assert(0 && "Interfering values have the same color!");
230                         }
231                 }
232         }
233         obstack_free(&ob, NULL);
234 }
235
236 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
237 {
238         if(env->ifg)
239                 return be_ifg_connected(env->ifg, a, b);
240         else
241                 return values_interfere(env->birg->lv, a, b);
242 }
243
244 static void dump(unsigned mask, ir_graph *irg,
245                                  const arch_register_class_t *cls,
246                                  const char *suffix,
247                                  void (*dump_func)(ir_graph *, const char *))
248 {
249         if((options.dump_flags & mask) == mask) {
250                 if (cls) {
251                         char buf[256];
252                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
253                         be_dump(irg, buf, dump_func);
254                 }
255                 else
256                         be_dump(irg, suffix, dump_func);
257         }
258 }
259
260 static void put_ignore_colors(be_chordal_env_t *chordal_env)
261 {
262         int n_colors = chordal_env->cls->n_regs;
263         int i;
264
265         bitset_clear_all(chordal_env->ignore_colors);
266         be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
267         for(i = 0; i < n_colors; ++i)
268                 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
269                         bitset_set(chordal_env->ignore_colors, i);
270 }
271
272 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
273 {
274         char buf[1024];
275
276         ir_snprintf(buf, sizeof(buf), "%s%F_%s%s", prefix, env->irg, env->cls->name, suffix);
277         return fopen(buf, "wt");
278 }
279
280 void check_ifg_implementations(be_chordal_env_t *chordal_env)
281 {
282         FILE *f;
283
284         f = be_chordal_open(chordal_env, "std", ".log");
285         chordal_env->ifg = be_ifg_std_new(chordal_env);
286         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
287         fclose(f);
288
289         f = be_chordal_open(chordal_env, "list", ".log");
290         be_ifg_free(chordal_env->ifg);
291         chordal_env->ifg = be_ifg_list_new(chordal_env);
292         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
293         fclose(f);
294
295         f = be_chordal_open(chordal_env, "clique", ".log");
296         be_ifg_free(chordal_env->ifg);
297         chordal_env->ifg = be_ifg_clique_new(chordal_env);
298         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
299         fclose(f);
300
301         f = be_chordal_open(chordal_env, "pointer", ".log");
302         be_ifg_free(chordal_env->ifg);
303         chordal_env->ifg = be_ifg_pointer_new(chordal_env);
304         be_ifg_check_sorted_to_file(chordal_env->ifg, f);
305         fclose(f);
306
307         chordal_env->ifg = NULL;
308 };
309
310 /**
311  * Checks for every reload if it's user can perform the load on itself.
312  */
313 static void memory_operand_walker(ir_node *irn, void *env) {
314         be_chordal_env_t *cenv = env;
315         const arch_env_t *aenv = cenv->birg->main_env->arch_env;
316         const ir_edge_t  *edge, *ne;
317         ir_node          *block;
318         ir_node          *spill;
319
320         if (! be_is_Reload(irn))
321                 return;
322
323         /* only use memory operands, if the reload is only used by 1 node */
324         if(get_irn_n_edges(irn) > 1)
325                 return;
326
327         spill = be_get_Reload_mem(irn);
328         block = get_nodes_block(irn);
329
330         foreach_out_edge_safe(irn, edge, ne) {
331                 ir_node *src = get_edge_src_irn(edge);
332                 int     pos  = get_edge_src_pos(edge);
333
334                 assert(src && "outedges broken!");
335
336                 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
337                         DBG((cenv->dbg, LEVEL_3, "performing memory operand %+F at %+F\n", irn, src));
338                         arch_perform_memory_operand(aenv, src, spill, pos);
339                 }
340         }
341
342         /* kill the Reload */
343         if (get_irn_n_edges(irn) == 0) {
344                 sched_remove(irn);
345                 set_irn_n(irn, be_pos_Reload_mem, new_Bad());
346         }
347 }
348
349 /**
350  * Starts a walk for memory operands if supported by the backend.
351  */
352 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
353         irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
354 }
355
356 /**
357  * Sorry for doing stats again...
358  */
359 typedef struct _node_stat_t {
360         unsigned int n_phis;      /**< Phis of the current register class. */
361         unsigned int n_mem_phis;  /**< Memory Phis (Phis with spill operands). */
362         unsigned int n_copies;    /**< Copies */
363         unsigned int n_perms;     /**< Perms */
364         unsigned int n_spills;    /**< Spill nodes */
365         unsigned int n_reloads;   /**< Reloads */
366 } node_stat_t;
367
368 struct node_stat_walker {
369         node_stat_t *stat;
370         const be_chordal_env_t *cenv;
371         bitset_t *mem_phis;
372 };
373
374 static void node_stat_walker(ir_node *irn, void *data)
375 {
376         struct node_stat_walker *env = data;
377         const arch_env_t *aenv       = env->cenv->birg->main_env->arch_env;
378
379         if(arch_irn_consider_in_reg_alloc(aenv, env->cenv->cls, irn)) {
380
381                 /* if the node is a normal phi */
382                 if(is_Phi(irn))
383                         env->stat->n_phis++;
384
385                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_spill)
386                         ++env->stat->n_spills;
387
388                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_reload)
389                         ++env->stat->n_reloads;
390
391                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_copy)
392                         ++env->stat->n_copies;
393
394                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_perm)
395                         ++env->stat->n_perms;
396         }
397
398         /* a mem phi is a PhiM with a mem phi operand or a Spill operand */
399         else if(is_Phi(irn) && get_irn_mode(irn) == mode_M) {
400                 int i;
401
402                 for(i = get_irn_arity(irn) - 1; i >= 0; --i) {
403                         ir_node *op = get_irn_n(irn, i);
404
405                         if((is_Phi(op) && bitset_contains_irn(env->mem_phis, op)) || (arch_irn_classify(aenv, op) & arch_irn_class_spill)) {
406                                 bitset_add_irn(env->mem_phis, irn);
407                                 env->stat->n_mem_phis++;
408                                 break;
409                         }
410                 }
411         }
412 }
413
414 static void node_stats(const be_chordal_env_t *cenv, node_stat_t *stat)
415 {
416         struct node_stat_walker env;
417
418         memset(stat, 0, sizeof(stat[0]));
419         env.cenv     = cenv;
420         env.mem_phis = bitset_irg_malloc(cenv->irg);
421         env.stat     = stat;
422         irg_walk_graph(cenv->irg, NULL, node_stat_walker, &env);
423         bitset_free(env.mem_phis);
424 }
425
426 static void insn_count_walker(ir_node *irn, void *data)
427 {
428         int *cnt = data;
429
430         switch(get_irn_opcode(irn)) {
431         case iro_Proj:
432         case iro_Phi:
433         case iro_Start:
434         case iro_End:
435                 break;
436         default:
437                 (*cnt)++;
438         }
439 }
440
441 static unsigned int count_insns(ir_graph *irg)
442 {
443         int cnt = 0;
444         irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
445         return cnt;
446 }
447
448 #ifdef WITH_LIBCORE
449 /**
450  * Initialize all timers.
451  */
452 static void be_init_timer(be_options_t *main_opts)
453 {
454         if (main_opts->timing == BE_TIME_ON) {
455                 ra_timer.t_prolog     = lc_timer_register("ra_prolog",     "regalloc prolog");
456                 ra_timer.t_epilog     = lc_timer_register("ra_epilog",     "regalloc epilog");
457                 ra_timer.t_live       = lc_timer_register("ra_liveness",   "be liveness");
458                 ra_timer.t_spill      = lc_timer_register("ra_spill",      "spiller");
459                 ra_timer.t_spillslots = lc_timer_register("ra_spillslots", "spillslots");
460                 ra_timer.t_color      = lc_timer_register("ra_color",      "graph coloring");
461                 ra_timer.t_ifg        = lc_timer_register("ra_ifg",        "interference graph");
462                 ra_timer.t_copymin    = lc_timer_register("ra_copymin",    "copy minimization");
463                 ra_timer.t_ssa        = lc_timer_register("ra_ssadestr",   "ssa destruction");
464                 ra_timer.t_verify     = lc_timer_register("ra_verify",     "graph verification");
465                 ra_timer.t_other      = lc_timer_register("ra_other",      "other time");
466
467                 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
468                 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
469                 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
470                 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
471                 LC_STOP_AND_RESET_TIMER(ra_timer.t_spillslots);
472                 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
473                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
474                 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
475                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
476                 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
477                 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
478         }
479 }
480
481 #define BE_TIMER_INIT(main_opts)        be_init_timer(main_opts)
482
483 #define BE_TIMER_PUSH(timer)                                                            \
484         if (main_opts->timing == BE_TIME_ON) {                                              \
485                 if (! lc_timer_push(timer)) {                                                   \
486                         if (options.vrfy_option == BE_CH_VRFY_ASSERT)                               \
487                                 assert(!"Timer already on stack, cannot be pushed twice.");             \
488                         else if (options.vrfy_option == BE_CH_VRFY_WARN)                            \
489                                 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
490                                         lc_timer_get_name(timer));                                          \
491                 }                                                                               \
492         }
493 #define BE_TIMER_POP(timer)                                                                    \
494         if (main_opts->timing == BE_TIME_ON) {                                                     \
495                 lc_timer_t *tmp = lc_timer_pop();                                                      \
496                 if (options.vrfy_option == BE_CH_VRFY_ASSERT)                                          \
497                         assert(tmp == timer && "Attempt to pop wrong timer.");                             \
498                 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer)                       \
499                         fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
500                                 lc_timer_get_name(tmp), lc_timer_get_name(timer));                             \
501                 timer = tmp;                                                                           \
502         }
503 #else
504
505 #define BE_TIMER_INIT(main_opts)
506 #define BE_TIMER_PUSH(timer)
507 #define BE_TIMER_POP(timer)
508
509 #endif /* WITH_LIBCORE */
510
511 /**
512  * Perform things which need to be done per register class before spilling.
513  */
514 static void pre_spill(const arch_isa_t *isa, int cls_idx, post_spill_env_t *pse) {
515         be_chordal_env_t *chordal_env = &pse->cenv;
516         node_stat_t      node_stat;
517
518         chordal_env->cls           = arch_isa_get_reg_class(isa, cls_idx);
519         chordal_env->border_heads  = pmap_create();
520         chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
521
522 #ifdef FIRM_STATISTICS
523         if (be_stat_ev_is_active()) {
524                 be_stat_tags[STAT_TAG_CLS] = chordal_env->cls->name;
525                 be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
526
527                 /* perform some node statistics. */
528                 node_stats(chordal_env, &node_stat);
529                 be_stat_ev("phis_before_spill", node_stat.n_phis);
530         }
531 #endif /* FIRM_STATISTICS */
532
533         /* put all ignore registers into the ignore register set. */
534         put_ignore_colors(chordal_env);
535
536         be_pre_spill_prepare_constr(chordal_env);
537         dump(BE_CH_DUMP_CONSTR, chordal_env->irg, chordal_env->cls, "-constr-pre", dump_ir_block_graph_sched);
538
539 #ifdef FIRM_STATISTICS
540         if (be_stat_ev_is_active()) {
541                 pse->pre_spill_cost = be_estimate_irg_costs(chordal_env->irg,
542                         chordal_env->birg->main_env->arch_env, chordal_env->birg->exec_freq);
543         }
544 #endif /* FIRM_STATISTICS */
545 }
546
547 /**
548  * Perform things which need to be done per register class after spilling.
549  */
550 static void post_spill(post_spill_env_t *pse) {
551         be_chordal_env_t    *chordal_env = &pse->cenv;
552         ir_graph            *irg         = chordal_env->irg;
553         be_irg_t            *birg        = chordal_env->birg;
554         const be_main_env_t *main_env    = birg->main_env;
555         be_options_t        *main_opts   = main_env->options;
556         static int          splitted     = 0;
557         node_stat_t         node_stat;
558
559 #ifdef FIRM_STATISTICS
560         if (be_stat_ev_is_active()) {
561                 double spillcosts = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost;
562
563                 be_stat_ev_l("spillcosts", (long) spillcosts);
564
565                 node_stats(chordal_env, &node_stat);
566                 be_stat_ev("phis_after_spill", node_stat.n_phis);
567                 be_stat_ev("mem_phis", node_stat.n_mem_phis);
568                 be_stat_ev("reloads", node_stat.n_reloads);
569                 be_stat_ev("spills", node_stat.n_spills);
570         }
571 #endif /* FIRM_STATISTICS */
572
573         check_for_memory_operands(chordal_env);
574
575         be_abi_fix_stack_nodes(birg->abi, birg->lv);
576
577         BE_TIMER_PUSH(ra_timer.t_verify);
578
579         /* verify schedule and register pressure */
580         if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
581                 be_verify_schedule(irg);
582                 be_verify_register_pressure(chordal_env->birg, chordal_env->cls, irg);
583         }
584         else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
585                 assert(be_verify_schedule(irg) && "Schedule verification failed");
586                 assert(be_verify_register_pressure(chordal_env->birg, chordal_env->cls, irg)
587                         && "Register pressure verification failed");
588         }
589         BE_TIMER_POP(ra_timer.t_verify);
590
591         if (be_elr_split && ! splitted) {
592                 extreme_liverange_splitting(chordal_env);
593                 splitted = 1;
594         }
595
596         /* Color the graph. */
597         BE_TIMER_PUSH(ra_timer.t_color);
598         be_ra_chordal_color(chordal_env);
599         BE_TIMER_POP(ra_timer.t_color);
600
601         dump(BE_CH_DUMP_CONSTR, irg, chordal_env->cls, "-color", dump_ir_block_graph_sched);
602
603         /* Create the ifg with the selected flavor */
604         BE_TIMER_PUSH(ra_timer.t_ifg);
605         switch (chordal_env->opts->ifg_flavor) {
606                 default:
607                         fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
608                 case BE_CH_IFG_STD:
609                 case BE_CH_IFG_FAST:
610                         chordal_env->ifg = be_ifg_std_new(chordal_env);
611                         break;
612                 case BE_CH_IFG_CLIQUE:
613                         chordal_env->ifg = be_ifg_clique_new(chordal_env);
614                         break;
615                 case BE_CH_IFG_POINTER:
616                         chordal_env->ifg = be_ifg_pointer_new(chordal_env);
617                         break;
618                 case BE_CH_IFG_LIST:
619                         chordal_env->ifg = be_ifg_list_new(chordal_env);
620                         break;
621                 case BE_CH_IFG_CHECK:
622                         check_ifg_implementations(chordal_env);
623                         /* Build the interference graph. */
624                         chordal_env->ifg = be_ifg_std_new(chordal_env);
625                         break;
626         }
627         BE_TIMER_POP(ra_timer.t_ifg);
628
629 #ifdef FIRM_STATISTICS
630         if (be_stat_ev_is_active()) {
631                 be_ifg_stat_t stat;
632                 be_ifg_stat(chordal_env, &stat);
633                 be_stat_ev("ifg_nodes", stat.n_nodes);
634                 be_stat_ev("ifg_edges", stat.n_edges);
635                 be_stat_ev("ifg_comps", stat.n_comps);
636
637                 node_stats(chordal_env, &node_stat);
638                 be_stat_ev("perms_before_coal", node_stat.n_perms);
639                 be_stat_ev("copies_before_coal", node_stat.n_copies);
640         }
641 #endif /* FIRM_STATISTICS */
642
643         /* copy minimization */
644         BE_TIMER_PUSH(ra_timer.t_copymin);
645         co_driver(chordal_env);
646         BE_TIMER_POP(ra_timer.t_copymin);
647
648         dump(BE_CH_DUMP_COPYMIN, irg, chordal_env->cls, "-copymin", dump_ir_block_graph_sched);
649
650         BE_TIMER_PUSH(ra_timer.t_ssa);
651
652         /* ssa destruction */
653         be_ssa_destruction(chordal_env);
654
655         BE_TIMER_POP(ra_timer.t_ssa);
656
657         dump(BE_CH_DUMP_SSADESTR, irg, chordal_env->cls, "-ssadestr", dump_ir_block_graph_sched);
658
659         BE_TIMER_PUSH(ra_timer.t_verify);
660         if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
661                 be_ssa_destruction_check(chordal_env);
662         }
663         BE_TIMER_POP(ra_timer.t_verify);
664
665         be_ifg_free(chordal_env->ifg);
666         pmap_destroy(chordal_env->border_heads);
667         bitset_free(chordal_env->ignore_colors);
668
669 #ifdef FIRM_STATISTICS
670         if (be_stat_ev_is_active()) {
671                 node_stats(chordal_env, &node_stat);
672                 be_stat_ev("perms_after_coal", node_stat.n_perms);
673                 be_stat_ev("copies_after_coal", node_stat.n_copies);
674                 be_stat_ev_pop();
675         }
676 #endif /* FIRM_STATISTICS */
677 }
678
679 /**
680  * Performs chordal register allocation for each register class on given irg.
681  *
682  * @param birg  Backend irg object
683  * @return Structure containing timer for the single phases or NULL if no timing requested.
684  */
685 static void be_ra_chordal_main(be_irg_t *birg)
686 {
687         const be_main_env_t *main_env  = birg->main_env;
688         const arch_isa_t    *isa       = arch_env_get_isa(main_env->arch_env);
689         ir_graph            *irg       = birg->irg;
690         be_options_t        *main_opts = main_env->options;
691         int                 j, m;
692         be_chordal_env_t    chordal_env;
693
694         BE_TIMER_INIT(main_opts);
695         BE_TIMER_PUSH(ra_timer.t_other);
696         BE_TIMER_PUSH(ra_timer.t_prolog);
697
698         be_assure_dom_front(birg);
699         be_assure_liveness(birg);
700
701         chordal_env.opts      = &options;
702         chordal_env.irg       = irg;
703         chordal_env.birg      = birg;
704         FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
705
706         obstack_init(&chordal_env.obst);
707
708         BE_TIMER_POP(ra_timer.t_prolog);
709
710         be_stat_ev("insns_before", count_insns(irg));
711
712         if (! arch_code_generator_has_spiller(birg->cg)) {
713                 /* use one of the generic spiller */
714
715                 /* Perform the following for each register class. */
716                 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
717                         post_spill_env_t pse;
718
719                         memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
720                         pre_spill(isa, j, &pse);
721
722                         BE_TIMER_PUSH(ra_timer.t_spill);
723                         /* spilling */
724                         switch(options.spill_method) {
725                         case BE_CH_SPILL_MORGAN:
726                                 be_spill_morgan(&pse.cenv);
727                                 break;
728                         case BE_CH_SPILL_BELADY:
729                                 be_spill_belady(&pse.cenv);
730                                 break;
731         #ifdef WITH_ILP
732                         case BE_CH_SPILL_REMAT:
733                                 be_spill_remat(&pse.cenv);
734                                 break;
735         #endif /* WITH_ILP */
736                         default:
737                                 fprintf(stderr, "no valid spiller selected. falling back to belady\n");
738                                 be_spill_belady(&pse.cenv);
739                         }
740                         BE_TIMER_POP(ra_timer.t_spill);
741
742                         dump(BE_CH_DUMP_SPILL, irg, pse.cenv.cls, "-spill", dump_ir_block_graph_sched);
743
744                         post_spill(&pse);
745                 }
746         }
747         else {
748                 post_spill_env_t *pse;
749
750                 /* the backend has it's own spiller */
751                 m = arch_isa_get_n_reg_class(isa);
752
753                 pse = alloca(m * sizeof(pse[0]));
754
755                 for (j = 0; j < m; ++j) {
756                         memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
757                         pre_spill(isa, j, &pse[j]);
758                 }
759
760                 BE_TIMER_PUSH(ra_timer.t_spill);
761                 arch_code_generator_spill(birg->cg, &chordal_env);
762                 BE_TIMER_POP(ra_timer.t_spill);
763                 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
764
765                 for (j = 0; j < m; ++j) {
766                         post_spill(&pse[j]);
767                 }
768         }
769
770         BE_TIMER_PUSH(ra_timer.t_spillslots);
771
772         be_coalesce_spillslots(&chordal_env);
773         dump(BE_CH_DUMP_SPILLSLOTS, irg, NULL, "-spillslots", dump_ir_block_graph_sched);
774
775         BE_TIMER_POP(ra_timer.t_spillslots);
776
777         BE_TIMER_PUSH(ra_timer.t_verify);
778         /* verify spillslots */
779         if (options.vrfy_option == BE_CH_VRFY_WARN) {
780                 be_verify_spillslots(main_env->arch_env, irg);
781         }
782         else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
783                 assert(be_verify_spillslots(main_env->arch_env, irg) && "Spillslot verification failed");
784         }
785         BE_TIMER_POP(ra_timer.t_verify);
786
787         BE_TIMER_PUSH(ra_timer.t_epilog);
788         dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
789
790         lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
791         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
792
793         obstack_free(&chordal_env.obst, NULL);
794         BE_TIMER_POP(ra_timer.t_epilog);
795
796         BE_TIMER_POP(ra_timer.t_other);
797
798         be_stat_ev("insns_after", count_insns(irg));
799
800         return;
801 }
802
803 static be_ra_t be_ra_chordal_allocator = {
804         be_ra_chordal_main,
805 };
806
807 void be_init_chordal(void)
808 {
809         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
810         lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
811         lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
812
813         lc_opt_add_table(chordal_grp, be_chordal_options);
814
815         be_register_allocator("chordal", &be_ra_chordal_allocator);
816 }
817
818 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal);