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