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