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