cleanup: Remove unnecessary #include "beirg.h".
[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  */
26 #include "config.h"
27
28 #include <stdlib.h>
29 #include <time.h>
30
31 #include "obst.h"
32 #include "pset.h"
33 #include "list.h"
34 #include "bitset.h"
35
36 #include "lc_opts.h"
37 #include "lc_opts_enum.h"
38
39 #include "ircons_t.h"
40 #include "irmode_t.h"
41 #include "irgraph_t.h"
42 #include "irprintf_t.h"
43 #include "irgwalk.h"
44 #include "ircons.h"
45 #include "irdump.h"
46 #include "irdom.h"
47 #include "ircons.h"
48 #include "irnode.h"
49 #include "ircons.h"
50 #include "irtools.h"
51 #include "debug.h"
52 #include "execfreq.h"
53 #include "iredges_t.h"
54 #include "error.h"
55
56 #include "bechordal_t.h"
57 #include "beabi.h"
58 #include "beutil.h"
59 #include "besched.h"
60 #include "besched.h"
61 #include "belive_t.h"
62 #include "bearch.h"
63 #include "beifg.h"
64 #include "benode.h"
65 #include "statev_t.h"
66 #include "bestat.h"
67 #include "bemodule.h"
68 #include "be_t.h"
69 #include "bera.h"
70 #include "beirg.h"
71 #include "bestack.h"
72
73 #include "bespillslots.h"
74 #include "bespill.h"
75 #include "belower.h"
76
77 #include "becopystat.h"
78 #include "becopyopt.h"
79 #include "bessadestr.h"
80 #include "beverify.h"
81 #include "benode.h"
82
83 #include "bepbqpcoloring.h"
84
85 static be_ra_chordal_opts_t options = {
86         BE_CH_DUMP_NONE,
87         BE_CH_LOWER_PERM_SWAP,
88         BE_CH_VRFY_WARN
89 };
90
91 typedef struct post_spill_env_t {
92         be_chordal_env_t            cenv;
93         ir_graph                    *irg;
94         const arch_register_class_t *cls;
95         double                      pre_spill_cost;
96 } post_spill_env_t;
97
98 static const lc_opt_enum_int_items_t lower_perm_items[] = {
99         { "copy", BE_CH_LOWER_PERM_COPY },
100         { "swap", BE_CH_LOWER_PERM_SWAP },
101         { NULL, 0 }
102 };
103
104 static const lc_opt_enum_mask_items_t dump_items[] = {
105         { "none",       BE_CH_DUMP_NONE       },
106         { "spill",      BE_CH_DUMP_SPILL      },
107         { "live",       BE_CH_DUMP_LIVE       },
108         { "color",      BE_CH_DUMP_COLOR      },
109         { "copymin",    BE_CH_DUMP_COPYMIN    },
110         { "ssadestr",   BE_CH_DUMP_SSADESTR   },
111         { "tree",       BE_CH_DUMP_TREE_INTV  },
112         { "split",      BE_CH_DUMP_SPLIT      },
113         { "constr",     BE_CH_DUMP_CONSTR     },
114         { "lower",      BE_CH_DUMP_LOWER      },
115         { "spillslots", BE_CH_DUMP_SPILLSLOTS },
116         { "appel",      BE_CH_DUMP_APPEL      },
117         { "all",        BE_CH_DUMP_ALL        },
118         { NULL, 0 }
119 };
120
121 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
122         { "off",    BE_CH_VRFY_OFF    },
123         { "warn",   BE_CH_VRFY_WARN   },
124         { "assert", BE_CH_VRFY_ASSERT },
125         { NULL, 0 }
126 };
127
128 static lc_opt_enum_int_var_t lower_perm_var = {
129         &options.lower_perm_opt, lower_perm_items
130 };
131
132 static lc_opt_enum_mask_var_t dump_var = {
133         &options.dump_flags, dump_items
134 };
135
136 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
137         &options.vrfy_option, be_ch_vrfy_items
138 };
139
140 static const lc_opt_table_entry_t be_chordal_options[] = {
141         LC_OPT_ENT_ENUM_INT ("perm",          "perm lowering options", &lower_perm_var),
142         LC_OPT_ENT_ENUM_MASK("dump",          "select dump phases", &dump_var),
143         LC_OPT_ENT_ENUM_INT ("verify",        "verify options", &be_ch_vrfy_var),
144         LC_OPT_LAST
145 };
146
147 static be_module_list_entry_t *colorings = NULL;
148 static const be_ra_chordal_coloring_t *selected_coloring = NULL;
149
150 void be_register_chordal_coloring(const char *name, be_ra_chordal_coloring_t *coloring)
151 {
152         if (selected_coloring == NULL)
153                 selected_coloring = coloring;
154
155         be_add_module_to_list(&colorings, name, coloring);
156 }
157
158 static void be_ra_chordal_coloring(be_chordal_env_t *env)
159 {
160         selected_coloring->allocate(env);
161 }
162
163 static void dump(unsigned mask, ir_graph *irg,
164                                  const arch_register_class_t *cls,
165                                  const char *suffix)
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                         dump_ir_graph(irg, buf);
172                 } else {
173                         dump_ir_graph(irg, suffix);
174                 }
175         }
176 }
177
178 /**
179  * Post-Walker: Checks for the given reload if has only one user that can perform the
180  * reload as part of its address mode.
181  * Fold the reload into the user it that is possible.
182  */
183 static void memory_operand_walker(ir_node *irn, void *env)
184 {
185         ir_node *block;
186         ir_node *spill;
187
188         (void)env;
189
190         if (! be_is_Reload(irn))
191                 return;
192
193         /* only use memory operands, if the reload is only used by 1 node */
194         if (get_irn_n_edges(irn) > 1)
195                 return;
196
197         spill = be_get_Reload_mem(irn);
198         block = get_nodes_block(irn);
199
200         foreach_out_edge_safe(irn, edge) {
201                 ir_node *src = get_edge_src_irn(edge);
202                 int     pos  = get_edge_src_pos(edge);
203
204                 assert(src && "outedges broken!");
205
206                 if (get_nodes_block(src) == block && arch_possible_memory_operand(src, pos)) {
207                         arch_perform_memory_operand(src, spill, pos);
208                 }
209         }
210
211         /* kill the Reload if it was folded */
212         if (get_irn_n_edges(irn) == 0) {
213                 ir_graph *irg = get_irn_irg(irn);
214                 ir_mode  *frame_mode = get_irn_mode(get_irn_n(irn, n_be_Reload_frame));
215                 sched_remove(irn);
216                 set_irn_n(irn, n_be_Reload_mem, new_r_Bad(irg, mode_X));
217                 set_irn_n(irn, n_be_Reload_frame, new_r_Bad(irg, frame_mode));
218         }
219 }
220
221 /**
222  * Starts a walk for memory operands if supported by the backend.
223  */
224 void check_for_memory_operands(ir_graph *irg)
225 {
226         irg_walk_graph(irg, NULL, memory_operand_walker, NULL);
227 }
228
229
230 static be_node_stats_t last_node_stats;
231
232 /**
233  * Perform things which need to be done per register class before spilling.
234  */
235 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
236 {
237         be_chordal_env_t *chordal_env = &pse->cenv;
238         ir_graph         *irg         = pse->irg;
239
240         pse->cls                      = cls;
241         chordal_env->cls              = cls;
242         chordal_env->border_heads     = pmap_create();
243         chordal_env->allocatable_regs = bitset_malloc(chordal_env->cls->n_regs);
244
245         be_assure_live_chk(irg);
246
247         if (stat_ev_enabled) {
248                 pse->pre_spill_cost = be_estimate_irg_costs(irg);
249         }
250
251         /* put all ignore registers into the ignore register set. */
252         be_put_allocatable_regs(irg, pse->cls, chordal_env->allocatable_regs);
253
254         be_timer_push(T_RA_CONSTR);
255         be_pre_spill_prepare_constr(irg, chordal_env->cls);
256         be_timer_pop(T_RA_CONSTR);
257
258         dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "constr-pre");
259 }
260
261 /**
262  * Perform things which need to be done per register class after spilling.
263  */
264 static void post_spill(post_spill_env_t *pse, int iteration)
265 {
266         be_chordal_env_t *chordal_env = &pse->cenv;
267         ir_graph         *irg         = pse->irg;
268         int               allocatable_regs = be_get_n_allocatable_regs(irg, chordal_env->cls);
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) - 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(irg);
283                 }
284                 be_timer_pop(T_RA_SPILL_APPLY);
285
286
287                 /* verify schedule and register pressure */
288                 be_timer_push(T_VERIFY);
289                 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
290                         be_verify_schedule(irg);
291                         be_verify_register_pressure(irg, pse->cls);
292                 } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
293                         assert(be_verify_schedule(irg) && "Schedule verification failed");
294                         assert(be_verify_register_pressure(irg, pse->cls)
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_coloring(chordal_env);
302                 be_timer_pop(T_RA_COLOR);
303
304                 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "color");
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                 if (stat_ev_enabled) {
312                         be_ifg_stat_t   stat;
313                         be_node_stats_t node_stats;
314
315                         be_ifg_stat(irg, 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, irg);
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                 be_timer_push(T_RA_COPYMIN);
330                 co_driver(chordal_env);
331                 be_timer_pop(T_RA_COPYMIN);
332
333                 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "copymin");
334
335                 /* ssa destruction */
336                 be_timer_push(T_RA_SSA);
337                 be_ssa_destruction(chordal_env);
338                 be_timer_pop(T_RA_SSA);
339
340                 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "ssadestr");
341
342                 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
343                         be_timer_push(T_VERIFY);
344                         be_ssa_destruction_check(chordal_env);
345                         be_timer_pop(T_VERIFY);
346                 }
347
348                 /* the ifg exists only if there are allocatable regs */
349                 be_ifg_free(chordal_env->ifg);
350         }
351
352         /* free some always allocated data structures */
353         pmap_destroy(chordal_env->border_heads);
354         bitset_free(chordal_env->allocatable_regs);
355 }
356
357 /**
358  * Performs chordal register allocation for each register class on given irg.
359  *
360  * @param irg    the graph
361  * @return Structure containing timer for the single phases or NULL if no
362  *         timing requested.
363  */
364 static void be_ra_chordal_main(ir_graph *irg)
365 {
366         const arch_env_t *arch_env = be_get_irg_arch_env(irg);
367         int               j;
368         int               m;
369         be_chordal_env_t  chordal_env;
370         struct obstack    obst;
371
372         be_timer_push(T_RA_OTHER);
373
374         be_timer_push(T_RA_PROLOG);
375
376         chordal_env.obst             = &obst;
377         chordal_env.opts             = &options;
378         chordal_env.irg              = irg;
379         chordal_env.border_heads     = NULL;
380         chordal_env.ifg              = NULL;
381         chordal_env.allocatable_regs = NULL;
382
383         obstack_init(&obst);
384
385         be_timer_pop(T_RA_PROLOG);
386
387         if (stat_ev_enabled) {
388                 be_collect_node_stats(&last_node_stats, irg);
389         }
390
391         /* use one of the generic spiller */
392
393         /* Perform the following for each register class. */
394         for (j = 0, m = arch_env->n_register_classes; j < m; ++j) {
395                 post_spill_env_t pse;
396                 const arch_register_class_t *cls = &arch_env->register_classes[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                 if (stat_ev_enabled) {
405                         be_do_stat_reg_pressure(irg, cls);
406                 }
407
408                 pse.cenv = chordal_env;
409                 pse.irg = irg;
410                 pre_spill(&pse, cls);
411
412                 be_timer_push(T_RA_SPILL);
413                 be_do_spill(irg, cls);
414                 be_timer_pop(T_RA_SPILL);
415
416                 dump(BE_CH_DUMP_SPILL, irg, pse.cls, "spill");
417
418                 post_spill(&pse, 0);
419
420                 if (stat_ev_enabled) {
421                         be_node_stats_t node_stats;
422
423                         be_collect_node_stats(&node_stats, irg);
424                         be_subtract_node_stats(&node_stats, &last_node_stats);
425                         be_emit_node_stats(&node_stats, "bechordal_");
426
427                         be_copy_node_stats(&last_node_stats, &node_stats);
428                         stat_ev_ctx_pop("bechordal_cls");
429                 }
430         }
431
432         be_timer_push(T_VERIFY);
433         if (chordal_env.opts->vrfy_option == BE_CH_VRFY_WARN) {
434                 be_verify_register_allocation(irg);
435         } else if (chordal_env.opts->vrfy_option == BE_CH_VRFY_ASSERT) {
436                 assert(be_verify_register_allocation(irg)
437                                 && "Register allocation invalid");
438         }
439         be_timer_pop(T_VERIFY);
440
441         be_timer_push(T_RA_EPILOG);
442         lower_nodes_after_ra(irg, options.lower_perm_opt == BE_CH_LOWER_PERM_COPY);
443         dump(BE_CH_DUMP_LOWER, irg, NULL, "belower-after-ra");
444
445         obstack_free(&obst, NULL);
446         be_invalidate_live_sets(irg);
447         be_timer_pop(T_RA_EPILOG);
448
449         be_timer_pop(T_RA_OTHER);
450 }
451
452 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main)
453 void be_init_chordal_main(void)
454 {
455         static be_ra_t be_ra_chordal_allocator = {
456                 be_ra_chordal_main,
457         };
458
459         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
460         lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
461         lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
462
463         be_register_allocator("chordal", &be_ra_chordal_allocator);
464
465         lc_opt_add_table(chordal_grp, be_chordal_options);
466         be_add_module_list_opt(chordal_grp, "coloring", "select coloring method", &colorings, (void**) &selected_coloring);
467 }