bechordal: Remove the attribute cls from struct post_spill_env_t.
[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 } post_spill_env_t;
94
95 static const lc_opt_enum_int_items_t lower_perm_items[] = {
96         { "copy", BE_CH_LOWER_PERM_COPY },
97         { "swap", BE_CH_LOWER_PERM_SWAP },
98         { NULL, 0 }
99 };
100
101 static const lc_opt_enum_mask_items_t dump_items[] = {
102         { "none",       BE_CH_DUMP_NONE       },
103         { "spill",      BE_CH_DUMP_SPILL      },
104         { "live",       BE_CH_DUMP_LIVE       },
105         { "color",      BE_CH_DUMP_COLOR      },
106         { "copymin",    BE_CH_DUMP_COPYMIN    },
107         { "ssadestr",   BE_CH_DUMP_SSADESTR   },
108         { "tree",       BE_CH_DUMP_TREE_INTV  },
109         { "split",      BE_CH_DUMP_SPLIT      },
110         { "constr",     BE_CH_DUMP_CONSTR     },
111         { "lower",      BE_CH_DUMP_LOWER      },
112         { "spillslots", BE_CH_DUMP_SPILLSLOTS },
113         { "appel",      BE_CH_DUMP_APPEL      },
114         { "all",        BE_CH_DUMP_ALL        },
115         { NULL, 0 }
116 };
117
118 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
119         { "off",    BE_CH_VRFY_OFF    },
120         { "warn",   BE_CH_VRFY_WARN   },
121         { "assert", BE_CH_VRFY_ASSERT },
122         { NULL, 0 }
123 };
124
125 static lc_opt_enum_int_var_t lower_perm_var = {
126         &options.lower_perm_opt, lower_perm_items
127 };
128
129 static lc_opt_enum_mask_var_t dump_var = {
130         &options.dump_flags, dump_items
131 };
132
133 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
134         &options.vrfy_option, be_ch_vrfy_items
135 };
136
137 static const lc_opt_table_entry_t be_chordal_options[] = {
138         LC_OPT_ENT_ENUM_INT ("perm",          "perm lowering options", &lower_perm_var),
139         LC_OPT_ENT_ENUM_MASK("dump",          "select dump phases", &dump_var),
140         LC_OPT_ENT_ENUM_INT ("verify",        "verify options", &be_ch_vrfy_var),
141         LC_OPT_LAST
142 };
143
144 static be_module_list_entry_t *colorings = NULL;
145 static const be_ra_chordal_coloring_t *selected_coloring = NULL;
146
147 void be_register_chordal_coloring(const char *name, be_ra_chordal_coloring_t *coloring)
148 {
149         if (selected_coloring == NULL)
150                 selected_coloring = coloring;
151
152         be_add_module_to_list(&colorings, name, coloring);
153 }
154
155 static void be_ra_chordal_coloring(be_chordal_env_t *env)
156 {
157         selected_coloring->allocate(env);
158 }
159
160 static void dump(unsigned mask, ir_graph *irg,
161                                  const arch_register_class_t *cls,
162                                  const char *suffix)
163 {
164         if ((options.dump_flags & mask) == mask) {
165                 if (cls) {
166                         char buf[256];
167                         snprintf(buf, sizeof(buf), "%s-%s", cls->name, suffix);
168                         dump_ir_graph(irg, buf);
169                 } else {
170                         dump_ir_graph(irg, suffix);
171                 }
172         }
173 }
174
175 /**
176  * Post-Walker: Checks for the given reload if has only one user that can perform the
177  * reload as part of its address mode.
178  * Fold the reload into the user it that is possible.
179  */
180 static void memory_operand_walker(ir_node *irn, void *env)
181 {
182         ir_node *block;
183         ir_node *spill;
184
185         (void)env;
186
187         if (! be_is_Reload(irn))
188                 return;
189
190         /* only use memory operands, if the reload is only used by 1 node */
191         if (get_irn_n_edges(irn) > 1)
192                 return;
193
194         spill = be_get_Reload_mem(irn);
195         block = get_nodes_block(irn);
196
197         foreach_out_edge_safe(irn, edge) {
198                 ir_node *src = get_edge_src_irn(edge);
199                 int     pos  = get_edge_src_pos(edge);
200
201                 assert(src && "outedges broken!");
202
203                 if (get_nodes_block(src) == block && arch_possible_memory_operand(src, pos)) {
204                         arch_perform_memory_operand(src, spill, pos);
205                 }
206         }
207
208         /* kill the Reload if it was folded */
209         if (get_irn_n_edges(irn) == 0) {
210                 ir_graph *irg = get_irn_irg(irn);
211                 ir_mode  *frame_mode = get_irn_mode(get_irn_n(irn, n_be_Reload_frame));
212                 sched_remove(irn);
213                 set_irn_n(irn, n_be_Reload_mem, new_r_Bad(irg, mode_X));
214                 set_irn_n(irn, n_be_Reload_frame, new_r_Bad(irg, frame_mode));
215         }
216 }
217
218 /**
219  * Starts a walk for memory operands if supported by the backend.
220  */
221 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, ir_graph *const irg)
233 {
234         be_chordal_env_t *chordal_env = &pse->cenv;
235
236         chordal_env->cls              = cls;
237         chordal_env->border_heads     = pmap_create();
238         chordal_env->allocatable_regs = bitset_malloc(chordal_env->cls->n_regs);
239
240         be_assure_live_chk(irg);
241
242         /* put all ignore registers into the ignore register set. */
243         be_put_allocatable_regs(irg, cls, chordal_env->allocatable_regs);
244
245         be_timer_push(T_RA_CONSTR);
246         be_pre_spill_prepare_constr(irg, chordal_env->cls);
247         be_timer_pop(T_RA_CONSTR);
248
249         dump(BE_CH_DUMP_CONSTR, irg, cls, "constr-pre");
250 }
251
252 /**
253  * Perform things which need to be done per register class after spilling.
254  */
255 static void post_spill(post_spill_env_t *const pse, ir_graph *const irg)
256 {
257         be_chordal_env_t *chordal_env = &pse->cenv;
258         int               allocatable_regs = be_get_n_allocatable_regs(irg, chordal_env->cls);
259
260         /* some special classes contain only ignore regs, no work to be done */
261         if (allocatable_regs > 0) {
262                 /*
263                         If we have a backend provided spiller, post spill is
264                         called in a loop after spilling for each register class.
265                         But we only need to fix stack nodes once in this case.
266                 */
267                 be_timer_push(T_RA_SPILL_APPLY);
268                 check_for_memory_operands(irg);
269                 be_abi_fix_stack_nodes(irg);
270                 be_timer_pop(T_RA_SPILL_APPLY);
271
272
273                 /* verify schedule and register pressure */
274                 be_timer_push(T_VERIFY);
275                 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
276                         be_verify_schedule(irg);
277                         be_verify_register_pressure(irg, chordal_env->cls);
278                 } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
279                         assert(be_verify_schedule(irg) && "Schedule verification failed");
280                         assert(be_verify_register_pressure(irg, chordal_env->cls)
281                                 && "Register pressure verification failed");
282                 }
283                 be_timer_pop(T_VERIFY);
284
285                 /* Color the graph. */
286                 be_timer_push(T_RA_COLOR);
287                 be_ra_chordal_coloring(chordal_env);
288                 be_timer_pop(T_RA_COLOR);
289
290                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env->cls, "color");
291
292                 /* Create the ifg with the selected flavor */
293                 be_timer_push(T_RA_IFG);
294                 chordal_env->ifg = be_create_ifg(chordal_env);
295                 be_timer_pop(T_RA_IFG);
296
297                 if (stat_ev_enabled) {
298                         be_ifg_stat_t   stat;
299                         be_node_stats_t node_stats;
300
301                         be_ifg_stat(irg, chordal_env->ifg, &stat);
302                         stat_ev_dbl("bechordal_ifg_nodes", stat.n_nodes);
303                         stat_ev_dbl("bechordal_ifg_edges", stat.n_edges);
304                         stat_ev_dbl("bechordal_ifg_comps", stat.n_comps);
305
306                         be_collect_node_stats(&node_stats, irg);
307                         be_subtract_node_stats(&node_stats, &last_node_stats);
308
309                         stat_ev_dbl("bechordal_perms_before_coal",
310                                         node_stats[BE_STAT_PERMS]);
311                         stat_ev_dbl("bechordal_copies_before_coal",
312                                         node_stats[BE_STAT_COPIES]);
313                 }
314
315                 be_timer_push(T_RA_COPYMIN);
316                 co_driver(chordal_env);
317                 be_timer_pop(T_RA_COPYMIN);
318
319                 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env->cls, "copymin");
320
321                 /* ssa destruction */
322                 be_timer_push(T_RA_SSA);
323                 be_ssa_destruction(chordal_env);
324                 be_timer_pop(T_RA_SSA);
325
326                 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env->cls, "ssadestr");
327
328                 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
329                         be_timer_push(T_VERIFY);
330                         be_ssa_destruction_check(chordal_env);
331                         be_timer_pop(T_VERIFY);
332                 }
333
334                 /* the ifg exists only if there are allocatable regs */
335                 be_ifg_free(chordal_env->ifg);
336         }
337
338         /* free some always allocated data structures */
339         pmap_destroy(chordal_env->border_heads);
340         bitset_free(chordal_env->allocatable_regs);
341 }
342
343 /**
344  * Performs chordal register allocation for each register class on given irg.
345  *
346  * @param irg    the graph
347  * @return Structure containing timer for the single phases or NULL if no
348  *         timing requested.
349  */
350 static void be_ra_chordal_main(ir_graph *irg)
351 {
352         const arch_env_t *arch_env = be_get_irg_arch_env(irg);
353         int               j;
354         int               m;
355         be_chordal_env_t  chordal_env;
356         struct obstack    obst;
357
358         be_timer_push(T_RA_OTHER);
359
360         be_timer_push(T_RA_PROLOG);
361
362         chordal_env.obst             = &obst;
363         chordal_env.opts             = &options;
364         chordal_env.irg              = irg;
365         chordal_env.border_heads     = NULL;
366         chordal_env.ifg              = NULL;
367         chordal_env.allocatable_regs = NULL;
368
369         obstack_init(&obst);
370
371         be_timer_pop(T_RA_PROLOG);
372
373         if (stat_ev_enabled) {
374                 be_collect_node_stats(&last_node_stats, irg);
375         }
376
377         /* use one of the generic spiller */
378
379         /* Perform the following for each register class. */
380         for (j = 0, m = arch_env->n_register_classes; j < m; ++j) {
381                 post_spill_env_t pse;
382                 const arch_register_class_t *cls = &arch_env->register_classes[j];
383
384                 if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
385                         continue;
386
387
388                 stat_ev_ctx_push_str("bechordal_cls", cls->name);
389
390                 double pre_spill_cost = 0;
391                 if (stat_ev_enabled) {
392                         be_do_stat_reg_pressure(irg, cls);
393                         pre_spill_cost = be_estimate_irg_costs(irg);
394                 }
395
396                 pse.cenv = chordal_env;
397                 pre_spill(&pse, cls, irg);
398
399                 be_timer_push(T_RA_SPILL);
400                 be_do_spill(irg, cls);
401                 be_timer_pop(T_RA_SPILL);
402
403                 dump(BE_CH_DUMP_SPILL, irg, cls, "spill");
404
405                 stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg) - pre_spill_cost);
406
407                 post_spill(&pse, irg);
408
409                 if (stat_ev_enabled) {
410                         be_node_stats_t node_stats;
411
412                         be_collect_node_stats(&node_stats, irg);
413                         be_subtract_node_stats(&node_stats, &last_node_stats);
414                         be_emit_node_stats(&node_stats, "bechordal_");
415
416                         be_copy_node_stats(&last_node_stats, &node_stats);
417                         stat_ev_ctx_pop("bechordal_cls");
418                 }
419         }
420
421         be_timer_push(T_VERIFY);
422         if (chordal_env.opts->vrfy_option == BE_CH_VRFY_WARN) {
423                 be_verify_register_allocation(irg);
424         } else if (chordal_env.opts->vrfy_option == BE_CH_VRFY_ASSERT) {
425                 assert(be_verify_register_allocation(irg)
426                                 && "Register allocation invalid");
427         }
428         be_timer_pop(T_VERIFY);
429
430         be_timer_push(T_RA_EPILOG);
431         lower_nodes_after_ra(irg, options.lower_perm_opt == BE_CH_LOWER_PERM_COPY);
432         dump(BE_CH_DUMP_LOWER, irg, NULL, "belower-after-ra");
433
434         obstack_free(&obst, NULL);
435         be_invalidate_live_sets(irg);
436         be_timer_pop(T_RA_EPILOG);
437
438         be_timer_pop(T_RA_OTHER);
439 }
440
441 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main)
442 void be_init_chordal_main(void)
443 {
444         static be_ra_t be_ra_chordal_allocator = {
445                 be_ra_chordal_main,
446         };
447
448         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
449         lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
450         lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
451
452         be_register_allocator("chordal", &be_ra_chordal_allocator);
453
454         lc_opt_add_table(chordal_grp, be_chordal_options);
455         be_add_module_list_opt(chordal_grp, "coloring", "select coloring method", &colorings, (void**) &selected_coloring);
456 }