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