backends can specify costs for spill and reload
[libfirm] / ir / be / bechordal_main.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Driver for the chordal register allocator.
23  * @author      Sebastian Hack
24  * @date        29.11.2005
25  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <time.h>
33
34 #include "obst.h"
35 #include "pset.h"
36 #include "list.h"
37 #include "bitset.h"
38 #include "iterator.h"
39 #include "firm_config.h"
40
41 #include <libcore/lc_opts.h>
42 #include <libcore/lc_opts_enum.h>
43 #include <libcore/lc_timing.h>
44
45 #include "ircons_t.h"
46 #include "irmode_t.h"
47 #include "irgraph_t.h"
48 #include "irprintf_t.h"
49 #include "irgwalk.h"
50 #include "ircons.h"
51 #include "irdump.h"
52 #include "irdom.h"
53 #include "ircons.h"
54 #include "irbitset.h"
55 #include "irnode.h"
56 #include "ircons.h"
57 #include "debug.h"
58 #include "xmalloc.h"
59 #include "execfreq.h"
60 #include "iredges_t.h"
61
62 #include "bechordal_t.h"
63 #include "beabi.h"
64 #include "bejavacoal.h"
65 #include "beutil.h"
66 #include "besched.h"
67 #include "besched_t.h"
68 #include "belive_t.h"
69 #include "bearch_t.h"
70 #include "beifg_t.h"
71 #include "beifg_impl.h"
72 #include "benode_t.h"
73 #include "bestatevent.h"
74 #include "bestat.h"
75 #include "bemodule.h"
76 #include "be_t.h"
77 #include "bera.h"
78 #include "beirg_t.h"
79
80 #include "bespillbelady.h"
81 #include "bespillmorgan.h"
82 #include "bespillslots.h"
83 #include "bespilloptions.h"
84 #include "belower.h"
85
86 #ifdef WITH_ILP
87 #include "bespillremat.h"
88 #endif /* WITH_ILP */
89
90 #include "bejavacoal.h"
91 #include "becopystat.h"
92 #include "becopyopt.h"
93 #include "bessadestr.h"
94 #include "beverify.h"
95 #include "benode_t.h"
96
97 static be_ra_chordal_opts_t options = {
98         BE_CH_DUMP_NONE,
99         BE_CH_LOWER_PERM_SWAP,
100         BE_CH_VRFY_WARN,
101 };
102
103 typedef struct _post_spill_env_t {
104         be_chordal_env_t            cenv;
105         be_irg_t                    *birg;
106         const arch_register_class_t *cls;
107         double                      pre_spill_cost;
108 } post_spill_env_t;
109
110 static be_ra_timer_t ra_timer = {
111         NULL,
112         NULL,
113         NULL,
114         NULL,
115         NULL,
116         NULL,
117         NULL,
118         NULL,
119         NULL,
120         NULL,
121         NULL,
122 };
123
124 static const lc_opt_enum_int_items_t lower_perm_items[] = {
125         { "copy", BE_CH_LOWER_PERM_COPY },
126         { "swap", BE_CH_LOWER_PERM_SWAP },
127         { NULL, 0 }
128 };
129
130 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
131         { NULL, 0 }
132 };
133
134 static const lc_opt_enum_int_items_t dump_items[] = {
135         { "none",       BE_CH_DUMP_NONE       },
136         { "spill",      BE_CH_DUMP_SPILL      },
137         { "live",       BE_CH_DUMP_LIVE       },
138         { "color",      BE_CH_DUMP_COLOR      },
139         { "copymin",    BE_CH_DUMP_COPYMIN    },
140         { "ssadestr",   BE_CH_DUMP_SSADESTR   },
141         { "tree",       BE_CH_DUMP_TREE_INTV  },
142         { "constr",     BE_CH_DUMP_CONSTR     },
143         { "lower",      BE_CH_DUMP_LOWER      },
144         { "spillslots", BE_CH_DUMP_SPILLSLOTS },
145         { "appel",      BE_CH_DUMP_APPEL      },
146         { "all",        BE_CH_DUMP_ALL        },
147         { NULL, 0 }
148 };
149
150 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
151         { "off",    BE_CH_VRFY_OFF    },
152         { "warn",   BE_CH_VRFY_WARN   },
153         { "assert", BE_CH_VRFY_ASSERT },
154         { NULL, 0 }
155 };
156
157 static lc_opt_enum_int_var_t lower_perm_var = {
158         &options.lower_perm_opt, lower_perm_items
159 };
160
161 static lc_opt_enum_int_var_t dump_var = {
162         &options.dump_flags, dump_items
163 };
164
165 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
166         &options.vrfy_option, be_ch_vrfy_items
167 };
168
169 static const lc_opt_table_entry_t be_chordal_options[] = {
170         LC_OPT_ENT_ENUM_PTR ("perm",          "perm lowering options", &lower_perm_var),
171         LC_OPT_ENT_ENUM_MASK("dump",          "select dump phases", &dump_var),
172         LC_OPT_ENT_ENUM_PTR ("vrfy",          "verify options", &be_ch_vrfy_var),
173         { NULL }
174 };
175
176 static void dump(unsigned mask, ir_graph *irg,
177                                  const arch_register_class_t *cls,
178                                  const char *suffix,
179                                  void (*dump_func)(ir_graph *, const char *))
180 {
181         if((options.dump_flags & mask) == mask) {
182                 if (cls) {
183                         char buf[256];
184                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
185                         be_dump(irg, buf, dump_func);
186                 }
187                 else
188                         be_dump(irg, suffix, dump_func);
189         }
190 }
191
192 /**
193  * Checks for every reload if it's user can perform the load on itself.
194  */
195 static void memory_operand_walker(ir_node *irn, void *env) {
196         be_chordal_env_t *cenv = env;
197         const arch_env_t *aenv = cenv->birg->main_env->arch_env;
198         const ir_edge_t  *edge, *ne;
199         ir_node          *block;
200         ir_node          *spill;
201
202         if (! be_is_Reload(irn))
203                 return;
204
205         /* only use memory operands, if the reload is only used by 1 node */
206         if(get_irn_n_edges(irn) > 1)
207                 return;
208
209         spill = be_get_Reload_mem(irn);
210         block = get_nodes_block(irn);
211
212         foreach_out_edge_safe(irn, edge, ne) {
213                 ir_node *src = get_edge_src_irn(edge);
214                 int     pos  = get_edge_src_pos(edge);
215
216                 assert(src && "outedges broken!");
217
218                 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
219                         arch_perform_memory_operand(aenv, src, spill, pos);
220                 }
221         }
222
223         /* kill the Reload */
224         if (get_irn_n_edges(irn) == 0) {
225                 sched_remove(irn);
226                 set_irn_n(irn, be_pos_Reload_mem, new_Bad());
227                 set_irn_n(irn, be_pos_Reload_frame, new_Bad());
228         }
229 }
230
231 /**
232  * Starts a walk for memory operands if supported by the backend.
233  */
234 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
235         irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
236 }
237
238 /**
239  * Sorry for doing stats again...
240  */
241 typedef struct _node_stat_t {
242         unsigned int n_phis;      /**< Phis of the current register class. */
243         unsigned int n_mem_phis;  /**< Memory Phis (Phis with spill operands). */
244         unsigned int n_copies;    /**< Copies */
245         unsigned int n_perms;     /**< Perms */
246         unsigned int n_spills;    /**< Spill nodes */
247         unsigned int n_reloads;   /**< Reloads */
248 } node_stat_t;
249
250 struct node_stat_walker {
251         node_stat_t      *stat;
252         const arch_env_t *arch_env;
253         bitset_t         *mem_phis;
254         const arch_register_class_t *cls;
255 };
256
257 static void node_stat_walker(ir_node *irn, void *data)
258 {
259         struct node_stat_walker *env  = data;
260         const arch_env_t        *aenv = env->arch_env;
261
262         if (arch_irn_consider_in_reg_alloc(aenv, env->cls, irn)) {
263
264                 /* if the node is a normal phi */
265                 if(is_Phi(irn))
266                         env->stat->n_phis++;
267
268                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_spill)
269                         ++env->stat->n_spills;
270
271                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_reload)
272                         ++env->stat->n_reloads;
273
274                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_copy)
275                         ++env->stat->n_copies;
276
277                 else if(arch_irn_classify(aenv, irn) & arch_irn_class_perm)
278                         ++env->stat->n_perms;
279         }
280
281         /* a mem phi is a PhiM with a mem phi operand or a Spill operand */
282         else if(is_Phi(irn) && get_irn_mode(irn) == mode_M) {
283                 int i;
284
285                 for(i = get_irn_arity(irn) - 1; i >= 0; --i) {
286                         ir_node *op = get_irn_n(irn, i);
287
288                         if((is_Phi(op) && bitset_contains_irn(env->mem_phis, op)) || (arch_irn_classify(aenv, op) & arch_irn_class_spill)) {
289                                 bitset_add_irn(env->mem_phis, irn);
290                                 env->stat->n_mem_phis++;
291                                 break;
292                         }
293                 }
294         }
295 }
296
297 static void node_stats(be_irg_t *birg, const arch_register_class_t *cls, node_stat_t *stat)
298 {
299         struct node_stat_walker env;
300
301         memset(stat, 0, sizeof(stat[0]));
302         env.arch_env = birg->main_env->arch_env;
303         env.mem_phis = bitset_irg_malloc(birg->irg);
304         env.stat     = stat;
305         env.cls      = cls;
306         irg_walk_graph(birg->irg, NULL, node_stat_walker, &env);
307         bitset_free(env.mem_phis);
308 }
309
310 static void insn_count_walker(ir_node *irn, void *data)
311 {
312         int *cnt = data;
313
314         switch(get_irn_opcode(irn)) {
315         case iro_Proj:
316         case iro_Phi:
317         case iro_Start:
318         case iro_End:
319                 break;
320         default:
321                 (*cnt)++;
322         }
323 }
324
325 static unsigned int count_insns(ir_graph *irg)
326 {
327         int cnt = 0;
328         irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
329         return cnt;
330 }
331
332 /**
333  * Initialize all timers.
334  */
335 static void be_init_timer(be_options_t *main_opts)
336 {
337         if (main_opts->timing == BE_TIME_ON) {
338                 ra_timer.t_prolog     = lc_timer_register("ra_prolog",     "regalloc prolog");
339                 ra_timer.t_epilog     = lc_timer_register("ra_epilog",     "regalloc epilog");
340                 ra_timer.t_live       = lc_timer_register("ra_liveness",   "be liveness");
341                 ra_timer.t_spill      = lc_timer_register("ra_spill",      "spiller");
342                 ra_timer.t_spillslots = lc_timer_register("ra_spillslots", "spillslots");
343                 ra_timer.t_color      = lc_timer_register("ra_color",      "graph coloring");
344                 ra_timer.t_ifg        = lc_timer_register("ra_ifg",        "interference graph");
345                 ra_timer.t_copymin    = lc_timer_register("ra_copymin",    "copy minimization");
346                 ra_timer.t_ssa        = lc_timer_register("ra_ssadestr",   "ssa destruction");
347                 ra_timer.t_verify     = lc_timer_register("ra_verify",     "graph verification");
348                 ra_timer.t_other      = lc_timer_register("ra_other",      "other time");
349
350                 LC_STOP_AND_RESET_TIMER(ra_timer.t_prolog);
351                 LC_STOP_AND_RESET_TIMER(ra_timer.t_epilog);
352                 LC_STOP_AND_RESET_TIMER(ra_timer.t_live);
353                 LC_STOP_AND_RESET_TIMER(ra_timer.t_spill);
354                 LC_STOP_AND_RESET_TIMER(ra_timer.t_spillslots);
355                 LC_STOP_AND_RESET_TIMER(ra_timer.t_color);
356                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ifg);
357                 LC_STOP_AND_RESET_TIMER(ra_timer.t_copymin);
358                 LC_STOP_AND_RESET_TIMER(ra_timer.t_ssa);
359                 LC_STOP_AND_RESET_TIMER(ra_timer.t_verify);
360                 LC_STOP_AND_RESET_TIMER(ra_timer.t_other);
361
362                 global_ra_timer = &ra_timer;
363         }
364 }
365
366 #define BE_TIMER_INIT(main_opts)        be_init_timer(main_opts)
367
368 #define BE_TIMER_PUSH(timer)                                                            \
369         if (main_opts->timing == BE_TIME_ON) {                                              \
370                 if (! lc_timer_push(timer)) {                                                   \
371                         if (options.vrfy_option == BE_CH_VRFY_ASSERT)                               \
372                                 assert(!"Timer already on stack, cannot be pushed twice.");             \
373                         else if (options.vrfy_option == BE_CH_VRFY_WARN)                            \
374                                 fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
375                                         lc_timer_get_name(timer));                                          \
376                 }                                                                               \
377         }
378 #define BE_TIMER_POP(timer)                                                                    \
379         if (main_opts->timing == BE_TIME_ON) {                                                     \
380                 lc_timer_t *tmp = lc_timer_pop();                                                      \
381                 if (options.vrfy_option == BE_CH_VRFY_ASSERT)                                          \
382                         assert(tmp == timer && "Attempt to pop wrong timer.");                             \
383                 else if (options.vrfy_option == BE_CH_VRFY_WARN && tmp != timer)                       \
384                         fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
385                                 lc_timer_get_name(tmp), lc_timer_get_name(timer));                             \
386                 timer = tmp;                                                                           \
387         }
388
389 /**
390  * Perform things which need to be done per register class before spilling.
391  */
392 static void pre_spill(const arch_isa_t *isa, int cls_idx, post_spill_env_t *pse) {
393         be_chordal_env_t *chordal_env = &pse->cenv;
394         be_irg_t         *birg        = pse->birg;
395         node_stat_t      node_stat;
396
397         pse->cls                   = arch_isa_get_reg_class(isa, cls_idx);
398         chordal_env->cls           = pse->cls;
399         chordal_env->border_heads  = pmap_create();
400         chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
401
402 #ifdef FIRM_STATISTICS
403         if (be_stat_ev_is_active()) {
404                 be_stat_tags[STAT_TAG_CLS] = pse->cls->name;
405                 be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
406
407                 /* perform some node statistics. */
408                 node_stats(birg, pse->cls, &node_stat);
409                 be_stat_ev("phis_before_spill", node_stat.n_phis);
410         }
411 #endif /* FIRM_STATISTICS */
412
413         /* put all ignore registers into the ignore register set. */
414         be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
415
416         be_pre_spill_prepare_constr(chordal_env);
417         dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
418
419 #ifdef FIRM_STATISTICS
420         if (be_stat_ev_is_active()) {
421                 pse->pre_spill_cost = be_estimate_irg_costs(birg->irg,
422                         birg->main_env->arch_env, birg->exec_freq);
423                 be_stat_ev_pop();
424         }
425 #endif /* FIRM_STATISTICS */
426 }
427
428 /**
429  * Perform things which need to be done per register class after spilling.
430  */
431 static void post_spill(post_spill_env_t *pse, int iteration) {
432         be_chordal_env_t    *chordal_env = &pse->cenv;
433         be_irg_t            *birg        = pse->birg;
434         ir_graph            *irg         = birg->irg;
435         const be_main_env_t *main_env    = birg->main_env;
436         be_options_t        *main_opts   = main_env->options;
437         node_stat_t         node_stat;
438         int                 colors_n     = arch_register_class_n_regs(chordal_env->cls);
439         int             allocatable_regs = colors_n - be_put_ignore_regs(birg, chordal_env->cls, NULL);
440
441         /* some special classes contain only ignore regs, no work to be done */
442         if (allocatable_regs > 0) {
443
444 #ifdef FIRM_STATISTICS
445                 if (be_stat_ev_is_active()) {
446                         double spillcosts = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost;
447
448                         be_stat_tags[STAT_TAG_CLS] = pse->cls->name;
449                         be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
450
451                         be_stat_ev_l("spillcosts", (long) spillcosts);
452
453                         node_stats(birg, pse->cls, &node_stat);
454                         be_stat_ev("phis_after_spill", node_stat.n_phis);
455                         be_stat_ev("mem_phis", node_stat.n_mem_phis);
456                         be_stat_ev("reloads", node_stat.n_reloads);
457                         be_stat_ev("spills", node_stat.n_spills);
458                 }
459 #endif /* FIRM_STATISTICS */
460
461                 /*
462                         If we have a backend provided spiller, post spill is
463                         called in a loop after spilling for each register class.
464                         But we only need to fix stack nodes once in this case.
465                 */
466                 if (iteration == 0) {
467                         check_for_memory_operands(chordal_env);
468                         be_abi_fix_stack_nodes(birg->abi);
469                 }
470
471                 BE_TIMER_PUSH(ra_timer.t_verify);
472
473                 /* verify schedule and register pressure */
474                 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
475                         be_verify_schedule(birg);
476                         be_verify_register_pressure(birg, pse->cls, irg);
477                 }
478                 else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
479                         assert(be_verify_schedule(birg) && "Schedule verification failed");
480                         assert(be_verify_register_pressure(birg, pse->cls, irg)
481                                 && "Register pressure verification failed");
482                 }
483                 BE_TIMER_POP(ra_timer.t_verify);
484
485                 /* Color the graph. */
486                 BE_TIMER_PUSH(ra_timer.t_color);
487                 be_ra_chordal_color(chordal_env);
488                 BE_TIMER_POP(ra_timer.t_color);
489
490                 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
491
492                 /* Create the ifg with the selected flavor */
493                 BE_TIMER_PUSH(ra_timer.t_ifg);
494                 chordal_env->ifg = be_create_ifg(chordal_env);
495                 BE_TIMER_POP(ra_timer.t_ifg);
496
497 #ifdef FIRM_STATISTICS
498                 if (be_stat_ev_is_active()) {
499                         be_ifg_stat_t stat;
500
501                         be_ifg_stat(birg, chordal_env->ifg, &stat);
502                         be_stat_ev("ifg_nodes", stat.n_nodes);
503                         be_stat_ev("ifg_edges", stat.n_edges);
504                         be_stat_ev("ifg_comps", stat.n_comps);
505
506                         node_stats(birg, pse->cls, &node_stat);
507                         be_stat_ev("perms_before_coal", node_stat.n_perms);
508                         be_stat_ev("copies_before_coal", node_stat.n_copies);
509                 }
510 #endif /* FIRM_STATISTICS */
511
512                 /* copy minimization */
513                 BE_TIMER_PUSH(ra_timer.t_copymin);
514                 co_driver(chordal_env);
515                 BE_TIMER_POP(ra_timer.t_copymin);
516
517                 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "-copymin", dump_ir_block_graph_sched);
518
519                 BE_TIMER_PUSH(ra_timer.t_ssa);
520
521                 /* ssa destruction */
522                 be_ssa_destruction(chordal_env);
523
524                 BE_TIMER_POP(ra_timer.t_ssa);
525
526                 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
527
528                 BE_TIMER_PUSH(ra_timer.t_verify);
529                 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
530                         be_ssa_destruction_check(chordal_env);
531                 }
532                 BE_TIMER_POP(ra_timer.t_verify);
533
534 #ifdef FIRM_STATISTICS
535                 if (be_stat_ev_is_active()) {
536                         node_stats(birg, pse->cls, &node_stat);
537                         be_stat_ev("perms_after_coal", node_stat.n_perms);
538                         be_stat_ev("copies_after_coal", node_stat.n_copies);
539                         be_stat_ev_pop();
540                 }
541 #endif /* FIRM_STATISTICS */
542
543                 /* the ifg exists only if there are allocatable regs */
544                 be_ifg_free(chordal_env->ifg);
545         }
546
547         /* free some always allocated data structures */
548         pmap_destroy(chordal_env->border_heads);
549         bitset_free(chordal_env->ignore_colors);
550 }
551
552 /**
553  * Performs chordal register allocation for each register class on given irg.
554  *
555  * @param birg  Backend irg object
556  * @return Structure containing timer for the single phases or NULL if no timing requested.
557  */
558 static void be_ra_chordal_main(be_irg_t *birg)
559 {
560         const be_main_env_t *main_env  = birg->main_env;
561         const arch_isa_t    *isa       = arch_env_get_isa(main_env->arch_env);
562         ir_graph            *irg       = birg->irg;
563         be_options_t        *main_opts = main_env->options;
564         int                 j, m;
565         be_chordal_env_t    chordal_env;
566         struct obstack      obst;
567
568         BE_TIMER_INIT(main_opts);
569         BE_TIMER_PUSH(ra_timer.t_other);
570         BE_TIMER_PUSH(ra_timer.t_prolog);
571
572         be_assure_dom_front(birg);
573         be_assure_liveness(birg);
574
575         chordal_env.obst          = &obst;
576         chordal_env.opts          = &options;
577         chordal_env.irg           = irg;
578         chordal_env.birg          = birg;
579         chordal_env.border_heads  = NULL;
580         chordal_env.ifg           = NULL;
581         chordal_env.ignore_colors = NULL;
582
583         obstack_init(&obst);
584
585         BE_TIMER_POP(ra_timer.t_prolog);
586
587         be_stat_ev("insns_before", count_insns(irg));
588
589         if (! arch_code_generator_has_spiller(birg->cg)) {
590                 /* use one of the generic spiller */
591
592                 /* Perform the following for each register class. */
593                 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
594                         post_spill_env_t pse;
595
596                         memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
597                         pse.birg = birg;
598                         pre_spill(isa, j, &pse);
599
600                         BE_TIMER_PUSH(ra_timer.t_spill);
601                         be_do_spill(birg, pse.cls);
602                         BE_TIMER_POP(ra_timer.t_spill);
603
604                         dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill", dump_ir_block_graph_sched);
605
606                         post_spill(&pse, 0);
607                 }
608         } else {
609                 post_spill_env_t *pse;
610
611                 /* the backend has it's own spiller */
612                 m = arch_isa_get_n_reg_class(isa);
613
614                 pse = alloca(m * sizeof(pse[0]));
615
616                 for (j = 0; j < m; ++j) {
617                         memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
618                         pse[j].birg = birg;
619                         pre_spill(isa, j, &pse[j]);
620                 }
621
622                 BE_TIMER_PUSH(ra_timer.t_spill);
623                 arch_code_generator_spill(birg->cg, birg);
624                 BE_TIMER_POP(ra_timer.t_spill);
625                 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
626
627                 for (j = 0; j < m; ++j) {
628                         post_spill(&pse[j], j);
629                 }
630         }
631
632         BE_TIMER_PUSH(ra_timer.t_epilog);
633         lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
634         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
635
636         obstack_free(&obst, NULL);
637         be_invalidate_liveness(birg);
638         BE_TIMER_POP(ra_timer.t_epilog);
639
640         BE_TIMER_POP(ra_timer.t_other);
641
642         be_stat_ev("insns_after", count_insns(irg));
643
644         return;
645 }
646
647 static be_ra_t be_ra_chordal_allocator = {
648         be_ra_chordal_main,
649 };
650
651 void be_init_chordal_main(void)
652 {
653         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
654         lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
655         lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
656
657         lc_opt_add_table(chordal_grp, be_chordal_options);
658
659         be_register_allocator("chordal", &be_ra_chordal_allocator);
660 }
661
662 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);