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