Put opening curly brace of functions on a separate line.
[libfirm] / ir / be / bechordal_main.c
1 /*
2  * Copyright (C) 1995-2008 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 #include "config.h"
28
29 #include <stdlib.h>
30 #include <time.h>
31
32 #include "obst.h"
33 #include "pset.h"
34 #include "list.h"
35 #include "bitset.h"
36 #include "iterator.h"
37
38 #include "lc_opts.h"
39 #include "lc_opts_enum.h"
40
41 #include "ircons_t.h"
42 #include "irmode_t.h"
43 #include "irgraph_t.h"
44 #include "irprintf_t.h"
45 #include "irgwalk.h"
46 #include "ircons.h"
47 #include "irdump.h"
48 #include "irdom.h"
49 #include "ircons.h"
50 #include "irbitset.h"
51 #include "irnode.h"
52 #include "ircons.h"
53 #include "debug.h"
54 #include "execfreq.h"
55 #include "iredges_t.h"
56
57 #include "bechordal_t.h"
58 #include "beabi.h"
59 #include "beutil.h"
60 #include "besched.h"
61 #include "besched.h"
62 #include "belive_t.h"
63 #include "bearch.h"
64 #include "beifg_t.h"
65 #include "beifg_impl.h"
66 #include "benode.h"
67 #include "bestatevent.h"
68 #include "bestat.h"
69 #include "bemodule.h"
70 #include "be_t.h"
71 #include "bera.h"
72 #include "beirg.h"
73
74 #include "bespillslots.h"
75 #include "bespill.h"
76 #include "belower.h"
77
78 #include "becopystat.h"
79 #include "becopyopt.h"
80 #include "bessadestr.h"
81 #include "beverify.h"
82 #include "benode.h"
83
84 #include "bepbqpcoloring.h"
85
86 static be_ra_chordal_opts_t options = {
87         BE_CH_DUMP_NONE,
88         BE_CH_LOWER_PERM_SWAP,
89         BE_CH_VRFY_WARN,
90         "",
91         ""
92 };
93
94 typedef struct _post_spill_env_t {
95         be_chordal_env_t            cenv;
96         be_irg_t                    *birg;
97         const arch_register_class_t *cls;
98         double                      pre_spill_cost;
99 } post_spill_env_t;
100
101 static const lc_opt_enum_int_items_t lower_perm_items[] = {
102         { "copy", BE_CH_LOWER_PERM_COPY },
103         { "swap", BE_CH_LOWER_PERM_SWAP },
104         { NULL, 0 }
105 };
106
107 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
108         { NULL, 0 }
109 };
110
111 static const lc_opt_enum_int_items_t dump_items[] = {
112         { "none",       BE_CH_DUMP_NONE       },
113         { "spill",      BE_CH_DUMP_SPILL      },
114         { "live",       BE_CH_DUMP_LIVE       },
115         { "color",      BE_CH_DUMP_COLOR      },
116         { "copymin",    BE_CH_DUMP_COPYMIN    },
117         { "ssadestr",   BE_CH_DUMP_SSADESTR   },
118         { "tree",       BE_CH_DUMP_TREE_INTV  },
119         { "constr",     BE_CH_DUMP_CONSTR     },
120         { "lower",      BE_CH_DUMP_LOWER      },
121         { "spillslots", BE_CH_DUMP_SPILLSLOTS },
122         { "appel",      BE_CH_DUMP_APPEL      },
123         { "all",        BE_CH_DUMP_ALL        },
124         { NULL, 0 }
125 };
126
127 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
128         { "off",    BE_CH_VRFY_OFF    },
129         { "warn",   BE_CH_VRFY_WARN   },
130         { "assert", BE_CH_VRFY_ASSERT },
131         { NULL, 0 }
132 };
133
134 static lc_opt_enum_int_var_t lower_perm_var = {
135         &options.lower_perm_opt, lower_perm_items
136 };
137
138 static lc_opt_enum_int_var_t dump_var = {
139         &options.dump_flags, dump_items
140 };
141
142 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
143         &options.vrfy_option, be_ch_vrfy_items
144 };
145
146 static const lc_opt_table_entry_t be_chordal_options[] = {
147         LC_OPT_ENT_ENUM_PTR ("perm",          "perm lowering options", &lower_perm_var),
148         LC_OPT_ENT_ENUM_MASK("dump",          "select dump phases", &dump_var),
149         LC_OPT_ENT_ENUM_PTR ("verify",        "verify options", &be_ch_vrfy_var),
150         LC_OPT_LAST
151 };
152
153 static be_module_list_entry_t *colorings = NULL;
154 static const be_ra_chordal_coloring_t *selected_coloring = NULL;
155
156 void be_register_chordal_coloring(const char *name, be_ra_chordal_coloring_t *coloring)
157 {
158         if (selected_coloring == NULL)
159                 selected_coloring = coloring;
160
161         be_add_module_to_list(&colorings, name, coloring);
162 }
163
164 void be_ra_chordal_coloring(be_chordal_env_t *env)
165 {
166         assert(selected_coloring != NULL);
167         if(selected_coloring != NULL) {
168                 selected_coloring->allocate(env);
169         }
170 }
171
172
173 static void dump(unsigned mask, ir_graph *irg,
174                                  const arch_register_class_t *cls,
175                                  const char *suffix,
176                                  void (*dump_func)(ir_graph *, const char *))
177 {
178         if((options.dump_flags & mask) == mask) {
179                 if (cls) {
180                         char buf[256];
181                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
182                         be_dump(irg, buf, dump_func);
183                 }
184                 else
185                         be_dump(irg, suffix, dump_func);
186         }
187 }
188
189 /**
190  * Checks for every reload if its user can perform the load on itself.
191  */
192 static void memory_operand_walker(ir_node *irn, void *env)
193 {
194         const ir_edge_t  *edge, *ne;
195         ir_node          *block;
196         ir_node          *spill;
197
198         (void)env;
199
200         if (! be_is_Reload(irn))
201                 return;
202
203         /* only use memory operands, if the reload is only used by 1 node */
204         if(get_irn_n_edges(irn) > 1)
205                 return;
206
207         spill = be_get_Reload_mem(irn);
208         block = get_nodes_block(irn);
209
210         foreach_out_edge_safe(irn, edge, ne) {
211                 ir_node *src = get_edge_src_irn(edge);
212                 int     pos  = get_edge_src_pos(edge);
213
214                 assert(src && "outedges broken!");
215
216                 if (get_nodes_block(src) == block && arch_possible_memory_operand(src, pos)) {
217                         arch_perform_memory_operand(src, spill, pos);
218                 }
219         }
220
221         /* kill the Reload */
222         if (get_irn_n_edges(irn) == 0) {
223                 sched_remove(irn);
224                 set_irn_n(irn, be_pos_Reload_mem, new_Bad());
225                 set_irn_n(irn, be_pos_Reload_frame, new_Bad());
226         }
227 }
228
229 /**
230  * Starts a walk for memory operands if supported by the backend.
231  */
232 void check_for_memory_operands(ir_graph *irg)
233 {
234         irg_walk_graph(irg, NULL, memory_operand_walker, NULL);
235 }
236
237
238 static be_node_stats_t last_node_stats;
239
240 /**
241  * Perform things which need to be done per register class before spilling.
242  */
243 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
244 {
245         be_chordal_env_t    *chordal_env = &pse->cenv;
246         be_irg_t            *birg        = pse->birg;
247         ir_graph            *irg         = be_get_birg_irg(birg);
248
249         pse->cls                   = cls;
250         chordal_env->cls           = cls;
251         chordal_env->border_heads  = pmap_create();
252         chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
253
254         be_assure_liveness(birg);
255         be_liveness_assure_chk(be_get_birg_liveness(birg));
256
257         stat_ev_do(pse->pre_spill_cost = be_estimate_irg_costs(irg, birg->exec_freq));
258
259         /* put all ignore registers into the ignore register set. */
260         be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
261
262         be_timer_push(T_RA_CONSTR);
263         be_pre_spill_prepare_constr(chordal_env->birg, chordal_env->cls);
264         be_timer_pop(T_RA_CONSTR);
265
266         dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
267 }
268
269 /**
270  * Perform things which need to be done per register class after spilling.
271  */
272 static void post_spill(post_spill_env_t *pse, int iteration)
273 {
274         be_chordal_env_t    *chordal_env = &pse->cenv;
275         be_irg_t            *birg        = pse->birg;
276         ir_graph            *irg         = birg->irg;
277         int                  colors_n    = arch_register_class_n_regs(chordal_env->cls);
278         int             allocatable_regs = colors_n - be_put_ignore_regs(birg, chordal_env->cls, NULL);
279
280         /* some special classes contain only ignore regs, no work to be done */
281         if (allocatable_regs > 0) {
282                 stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg, birg->exec_freq) - pse->pre_spill_cost);
283
284                 /*
285                         If we have a backend provided spiller, post spill is
286                         called in a loop after spilling for each register class.
287                         But we only need to fix stack nodes once in this case.
288                 */
289                 be_timer_push(T_RA_SPILL_APPLY);
290                 check_for_memory_operands(irg);
291                 if (iteration == 0) {
292                         be_abi_fix_stack_nodes(birg->abi);
293                 }
294                 be_timer_pop(T_RA_SPILL_APPLY);
295
296
297                 /* verify schedule and register pressure */
298                 be_timer_push(T_VERIFY);
299                 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
300                         be_verify_schedule(birg);
301                         be_verify_register_pressure(birg, pse->cls, irg);
302                 } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
303                         assert(be_verify_schedule(birg) && "Schedule verification failed");
304                         assert(be_verify_register_pressure(birg, pse->cls, irg)
305                                 && "Register pressure verification failed");
306                 }
307                 be_timer_pop(T_VERIFY);
308
309                 /* Color the graph. */
310                 be_timer_push(T_RA_COLOR);
311                 be_ra_chordal_coloring(chordal_env);
312                 be_timer_pop(T_RA_COLOR);
313
314                 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
315
316                 /* Create the ifg with the selected flavor */
317                 be_timer_push(T_RA_IFG);
318                 chordal_env->ifg = be_create_ifg(chordal_env);
319                 be_timer_pop(T_RA_IFG);
320
321                 stat_ev_if {
322                         be_ifg_stat_t   stat;
323                         be_node_stats_t node_stats;
324
325                         be_ifg_stat(birg, chordal_env->ifg, &stat);
326                         stat_ev_dbl("bechordal_ifg_nodes", stat.n_nodes);
327                         stat_ev_dbl("bechordal_ifg_edges", stat.n_edges);
328                         stat_ev_dbl("bechordal_ifg_comps", stat.n_comps);
329
330                         be_collect_node_stats(&node_stats, birg);
331                         be_subtract_node_stats(&node_stats, &last_node_stats);
332
333                         stat_ev_dbl("bechordal_perms_before_coal",
334                                         node_stats[BE_STAT_PERMS]);
335                         stat_ev_dbl("bechordal_copies_before_coal",
336                                         node_stats[BE_STAT_COPIES]);
337                 }
338
339                 be_timer_push(T_RA_COPYMIN);
340                 co_driver(chordal_env);
341                 be_timer_pop(T_RA_COPYMIN);
342
343                 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "-copymin", dump_ir_block_graph_sched);
344
345                 /* ssa destruction */
346                 be_timer_push(T_RA_SSA);
347                 be_ssa_destruction(chordal_env);
348                 be_timer_pop(T_RA_SSA);
349
350                 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
351
352                 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
353                         be_timer_push(T_VERIFY);
354                         be_ssa_destruction_check(chordal_env);
355                         be_timer_pop(T_VERIFY);
356                 }
357
358                 /* the ifg exists only if there are allocatable regs */
359                 be_ifg_free(chordal_env->ifg);
360         }
361
362         /* free some always allocated data structures */
363         pmap_destroy(chordal_env->border_heads);
364         bitset_free(chordal_env->ignore_colors);
365 }
366
367 /**
368  * Performs chordal register allocation for each register class on given irg.
369  *
370  * @param birg  Backend irg object
371  * @return Structure containing timer for the single phases or NULL if no timing requested.
372  */
373 static void be_ra_chordal_main(be_irg_t *birg)
374 {
375         const arch_env_t *arch_env = birg->main_env->arch_env;
376         ir_graph         *irg      = birg->irg;
377         int               j;
378         int               m;
379         be_chordal_env_t  chordal_env;
380         struct obstack    obst;
381
382         be_timer_push(T_RA_OTHER);
383
384         be_timer_push(T_RA_PROLOG);
385
386         be_assure_liveness(birg);
387
388         chordal_env.obst          = &obst;
389         chordal_env.opts          = &options;
390         chordal_env.irg           = irg;
391         chordal_env.birg          = birg;
392         chordal_env.border_heads  = NULL;
393         chordal_env.ifg           = NULL;
394         chordal_env.ignore_colors = NULL;
395
396         obstack_init(&obst);
397
398         be_timer_pop(T_RA_PROLOG);
399
400         stat_ev_if {
401                 be_collect_node_stats(&last_node_stats, birg);
402         }
403
404         if (! arch_code_generator_has_spiller(birg->cg)) {
405                 /* use one of the generic spiller */
406
407                 /* Perform the following for each register class. */
408                 for (j = 0, m = arch_env_get_n_reg_class(arch_env); j < m; ++j) {
409                         post_spill_env_t pse;
410                         const arch_register_class_t *cls
411                                 = arch_env_get_reg_class(arch_env, j);
412
413                         if(arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
414                                 continue;
415
416
417                         stat_ev_ctx_push_str("bechordal_cls", cls->name);
418
419                         stat_ev_if {
420                                 be_do_stat_reg_pressure(birg, cls);
421                         }
422
423                         memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
424                         pse.birg = birg;
425                         pre_spill(&pse, cls);
426
427                         be_timer_push(T_RA_SPILL);
428                         be_do_spill(birg, cls);
429                         be_timer_pop(T_RA_SPILL);
430
431                         dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill",
432                              dump_ir_block_graph_sched);
433
434                         post_spill(&pse, 0);
435
436                         stat_ev_if {
437                                 be_node_stats_t node_stats;
438
439                                 be_collect_node_stats(&node_stats, birg);
440                                 be_subtract_node_stats(&node_stats, &last_node_stats);
441                                 be_emit_node_stats(&node_stats, "bechordal_");
442
443                                 be_copy_node_stats(&last_node_stats, &node_stats);
444                                 stat_ev_ctx_pop("bechordal_cls");
445                         }
446                 }
447         } else {
448                 post_spill_env_t *pse;
449
450                 /* the backend has its own spiller */
451                 m = arch_env_get_n_reg_class(arch_env);
452
453                 pse = ALLOCAN(post_spill_env_t, m);
454
455                 for (j = 0; j < m; ++j) {
456                         memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
457                         pse[j].birg = birg;
458                         pre_spill(&pse[j], pse[j].cls);
459                 }
460
461                 be_timer_push(T_RA_SPILL);
462                 arch_code_generator_spill(birg->cg, birg);
463                 be_timer_pop(T_RA_SPILL);
464                 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
465
466                 for (j = 0; j < m; ++j) {
467                         post_spill(&pse[j], j);
468                 }
469         }
470
471         be_timer_push(T_VERIFY);
472         if (chordal_env.opts->vrfy_option == BE_CH_VRFY_WARN) {
473                 be_verify_register_allocation(birg);
474         } else if(chordal_env.opts->vrfy_option == BE_CH_VRFY_ASSERT) {
475                 assert(be_verify_register_allocation(birg)
476                                 && "Register allocation invalid");
477         }
478         be_timer_pop(T_VERIFY);
479
480         be_timer_push(T_RA_EPILOG);
481         lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
482         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
483
484         obstack_free(&obst, NULL);
485         be_liveness_invalidate(be_get_birg_liveness(birg));
486         be_timer_pop(T_RA_EPILOG);
487
488         be_timer_pop(T_RA_OTHER);
489 }
490
491 void be_init_chordal_main(void)
492 {
493         static be_ra_t be_ra_chordal_allocator = {
494                 be_ra_chordal_main,
495         };
496
497         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
498         lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
499         lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
500
501         be_register_allocator("chordal", &be_ra_chordal_allocator);
502
503         lc_opt_add_table(chordal_grp, be_chordal_options);
504         be_add_module_list_opt(chordal_grp, "coloring", "select coloring methode", &colorings, (void**) &selected_coloring);
505 }
506
507 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);