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