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