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