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