more support for exceptions added
[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 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <time.h>
33
34 #include "obst.h"
35 #include "pset.h"
36 #include "list.h"
37 #include "bitset.h"
38 #include "iterator.h"
39 #include "firm_config.h"
40
41 #include <libcore/lc_opts.h>
42 #include <libcore/lc_opts_enum.h>
43 #include <libcore/lc_timing.h>
44
45 #include "ircons_t.h"
46 #include "irmode_t.h"
47 #include "irgraph_t.h"
48 #include "irprintf_t.h"
49 #include "irgwalk.h"
50 #include "ircons.h"
51 #include "irdump.h"
52 #include "irdom.h"
53 #include "ircons.h"
54 #include "irbitset.h"
55 #include "irnode.h"
56 #include "ircons.h"
57 #include "debug.h"
58 #include "xmalloc.h"
59 #include "execfreq.h"
60 #include "iredges_t.h"
61
62 #include "bechordal_t.h"
63 #include "beabi.h"
64 #include "bejavacoal.h"
65 #include "beutil.h"
66 #include "besched.h"
67 #include "besched_t.h"
68 #include "belive_t.h"
69 #include "bearch_t.h"
70 #include "beifg_t.h"
71 #include "beifg_impl.h"
72 #include "benode_t.h"
73 #include "bestatevent.h"
74 #include "bestat.h"
75 #include "bemodule.h"
76 #include "be_t.h"
77 #include "bera.h"
78 #include "beirg_t.h"
79
80 #include "bespillslots.h"
81 #include "bespilloptions.h"
82 #include "belower.h"
83
84 #ifdef WITH_ILP
85 #include "bespillremat.h"
86 #endif /* WITH_ILP */
87
88 #include "bejavacoal.h"
89 #include "becopystat.h"
90 #include "becopyopt.h"
91 #include "bessadestr.h"
92 #include "beverify.h"
93 #include "benode_t.h"
94
95 static be_ra_chordal_opts_t options = {
96         BE_CH_DUMP_NONE,
97         BE_CH_LOWER_PERM_SWAP,
98         BE_CH_VRFY_WARN,
99         "",
100         ""
101 };
102
103 typedef struct _post_spill_env_t {
104         be_chordal_env_t            cenv;
105         be_irg_t                    *birg;
106         const arch_register_class_t *cls;
107         double                      pre_spill_cost;
108 } post_spill_env_t;
109
110 static be_options_t  *main_opts;
111
112 static const lc_opt_enum_int_items_t lower_perm_items[] = {
113         { "copy", BE_CH_LOWER_PERM_COPY },
114         { "swap", BE_CH_LOWER_PERM_SWAP },
115         { NULL, 0 }
116 };
117
118 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
119         { NULL, 0 }
120 };
121
122 static const lc_opt_enum_int_items_t dump_items[] = {
123         { "none",       BE_CH_DUMP_NONE       },
124         { "spill",      BE_CH_DUMP_SPILL      },
125         { "live",       BE_CH_DUMP_LIVE       },
126         { "color",      BE_CH_DUMP_COLOR      },
127         { "copymin",    BE_CH_DUMP_COPYMIN    },
128         { "ssadestr",   BE_CH_DUMP_SSADESTR   },
129         { "tree",       BE_CH_DUMP_TREE_INTV  },
130         { "constr",     BE_CH_DUMP_CONSTR     },
131         { "lower",      BE_CH_DUMP_LOWER      },
132         { "spillslots", BE_CH_DUMP_SPILLSLOTS },
133         { "appel",      BE_CH_DUMP_APPEL      },
134         { "all",        BE_CH_DUMP_ALL        },
135         { NULL, 0 }
136 };
137
138 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
139         { "off",    BE_CH_VRFY_OFF    },
140         { "warn",   BE_CH_VRFY_WARN   },
141         { "assert", BE_CH_VRFY_ASSERT },
142         { NULL, 0 }
143 };
144
145 static lc_opt_enum_int_var_t lower_perm_var = {
146         &options.lower_perm_opt, lower_perm_items
147 };
148
149 static lc_opt_enum_int_var_t dump_var = {
150         &options.dump_flags, dump_items
151 };
152
153 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
154         &options.vrfy_option, be_ch_vrfy_items
155 };
156
157 static const lc_opt_table_entry_t be_chordal_options[] = {
158         LC_OPT_ENT_ENUM_PTR ("perm",          "perm lowering options", &lower_perm_var),
159         LC_OPT_ENT_ENUM_MASK("dump",          "select dump phases", &dump_var),
160         LC_OPT_ENT_ENUM_PTR ("vrfy",          "verify options", &be_ch_vrfy_var),
161         LC_OPT_LAST
162 };
163
164 static void dump(unsigned mask, ir_graph *irg,
165                                  const arch_register_class_t *cls,
166                                  const char *suffix,
167                                  void (*dump_func)(ir_graph *, const char *))
168 {
169         if((options.dump_flags & mask) == mask) {
170                 if (cls) {
171                         char buf[256];
172                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
173                         be_dump(irg, buf, dump_func);
174                 }
175                 else
176                         be_dump(irg, suffix, dump_func);
177         }
178 }
179
180 /**
181  * Checks for every reload if it's user can perform the load on itself.
182  */
183 static void memory_operand_walker(ir_node *irn, void *env) {
184         be_chordal_env_t *cenv = env;
185         const arch_env_t *aenv = cenv->birg->main_env->arch_env;
186         const ir_edge_t  *edge, *ne;
187         ir_node          *block;
188         ir_node          *spill;
189
190         if (! be_is_Reload(irn))
191                 return;
192
193         /* only use memory operands, if the reload is only used by 1 node */
194         if(get_irn_n_edges(irn) > 1)
195                 return;
196
197         spill = be_get_Reload_mem(irn);
198         block = get_nodes_block(irn);
199
200         foreach_out_edge_safe(irn, edge, ne) {
201                 ir_node *src = get_edge_src_irn(edge);
202                 int     pos  = get_edge_src_pos(edge);
203
204                 assert(src && "outedges broken!");
205
206                 if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
207                         arch_perform_memory_operand(aenv, src, spill, pos);
208                 }
209         }
210
211         /* kill the Reload */
212         if (get_irn_n_edges(irn) == 0) {
213                 sched_remove(irn);
214                 set_irn_n(irn, be_pos_Reload_mem, new_Bad());
215                 set_irn_n(irn, be_pos_Reload_frame, new_Bad());
216         }
217 }
218
219 /**
220  * Starts a walk for memory operands if supported by the backend.
221  */
222 static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
223         irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
224 }
225
226 /**
227  * Sorry for doing stats again...
228  */
229 typedef struct _node_stat_t {
230         unsigned int n_phis;      /**< Phis of the current register class. */
231         unsigned int n_mem_phis;  /**< Memory Phis (Phis with spill operands). */
232         unsigned int n_copies;    /**< Copies */
233         unsigned int n_perms;     /**< Perms */
234         unsigned int n_spills;    /**< Spill nodes */
235         unsigned int n_reloads;   /**< Reloads */
236 } node_stat_t;
237
238 struct node_stat_walker {
239         node_stat_t      *stat;
240         const arch_env_t *arch_env;
241         bitset_t         *mem_phis;
242         const arch_register_class_t *cls;
243 };
244
245 static void node_stat_walker(ir_node *irn, void *data)
246 {
247         struct node_stat_walker *env  = data;
248         const arch_env_t        *aenv = env->arch_env;
249
250         if (arch_irn_consider_in_reg_alloc(aenv, env->cls, irn)) {
251
252                 /* if the node is a normal phi */
253                 if(is_Phi(irn))
254                         env->stat->n_phis++;
255
256                 else {
257                         arch_irn_class_t classify = arch_irn_classify(aenv, irn);
258
259                         if(classify & arch_irn_class_spill)
260                                 ++env->stat->n_spills;
261                         if(classify & arch_irn_class_reload)
262                                 ++env->stat->n_reloads;
263                         if(classify & arch_irn_class_copy)
264                                 ++env->stat->n_copies;
265                         if(classify & arch_irn_class_perm)
266                                 ++env->stat->n_perms;
267                 }
268         }
269
270         /* a mem phi is a PhiM with a mem phi operand or a Spill operand */
271         /*else*/ if(is_Phi(irn) && get_irn_mode(irn) == mode_M) {
272                 int i;
273
274                 for(i = get_irn_arity(irn) - 1; i >= 0; --i) {
275                         ir_node *op = get_irn_n(irn, i);
276
277                         if((is_Phi(op) && bitset_contains_irn(env->mem_phis, op)) || (arch_irn_classify(aenv, op) & arch_irn_class_spill)) {
278                                 bitset_add_irn(env->mem_phis, irn);
279                                 env->stat->n_mem_phis++;
280                                 break;
281                         }
282                 }
283         }
284 }
285
286 static void node_stats(be_irg_t *birg, const arch_register_class_t *cls, node_stat_t *stat)
287 {
288         struct node_stat_walker env;
289
290         memset(stat, 0, sizeof(stat[0]));
291         env.arch_env = birg->main_env->arch_env;
292         env.mem_phis = bitset_irg_malloc(birg->irg);
293         env.stat     = stat;
294         env.cls      = cls;
295         irg_walk_graph(birg->irg, NULL, node_stat_walker, &env);
296         bitset_free(env.mem_phis);
297 }
298
299 static void insn_count_walker(ir_node *irn, void *data)
300 {
301         int *cnt = data;
302
303         switch(get_irn_opcode(irn)) {
304         case iro_Proj:
305         case iro_Phi:
306         case iro_Start:
307         case iro_End:
308                 break;
309         default:
310                 (*cnt)++;
311         }
312 }
313
314 static unsigned int count_insns(ir_graph *irg)
315 {
316         int cnt = 0;
317         irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
318         return cnt;
319 }
320
321 /**
322  * Perform things which need to be done per register class before spilling.
323  */
324 static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
325 {
326         be_chordal_env_t    *chordal_env = &pse->cenv;
327         be_irg_t            *birg        = pse->birg;
328         ir_graph            *irg         = be_get_birg_irg(birg);
329         const be_main_env_t *main_env    = birg->main_env;
330         node_stat_t          node_stat;
331
332         pse->cls                   = cls;
333         chordal_env->cls           = cls;
334         chordal_env->border_heads  = pmap_create();
335         chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
336
337         be_assure_liveness(birg);
338         be_liveness_assure_chk(be_get_birg_liveness(birg));
339
340         stat_ev_ctx_push_str("bechordal_cls", pse->cls->name);
341         stat_ev_do(node_stats(birg, pse->cls, &node_stat));
342         stat_ev_do(pse->pre_spill_cost = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq));
343         stat_ev_dbl("phis_before_spill", node_stat.n_phis);
344
345         /* put all ignore registers into the ignore register set. */
346         be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
347
348         BE_TIMER_PUSH(t_ra_constr);
349         be_pre_spill_prepare_constr(chordal_env);
350         BE_TIMER_POP(t_ra_constr);
351
352         dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
353
354         stat_ev_ctx_pop("bechordal_cls");
355 }
356
357 /**
358  * Perform things which need to be done per register class after spilling.
359  */
360 static void post_spill(post_spill_env_t *pse, int iteration) {
361         be_chordal_env_t    *chordal_env = &pse->cenv;
362         be_irg_t            *birg        = pse->birg;
363         ir_graph            *irg         = birg->irg;
364         const be_main_env_t *main_env    = birg->main_env;
365         node_stat_t          node_stat;
366         int                  colors_n     = arch_register_class_n_regs(chordal_env->cls);
367         int             allocatable_regs = colors_n - be_put_ignore_regs(birg, chordal_env->cls, NULL);
368
369         /* some special classes contain only ignore regs, no work to be done */
370         if (allocatable_regs > 0) {
371
372                 stat_ev_ctx_push_str("bechordal_cls", pse->cls->name);
373                 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
374                 stat_ev_dbl("phis_after_spill", node_stat.n_phis);
375                 stat_ev_dbl("mem_phis", node_stat.n_mem_phis);
376                 stat_ev_dbl("reloads", node_stat.n_reloads);
377                 stat_ev_dbl("spills", node_stat.n_spills);
378                 stat_ev_dbl("spillcosts", be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost);
379
380                 /*
381                         If we have a backend provided spiller, post spill is
382                         called in a loop after spilling for each register class.
383                         But we only need to fix stack nodes once in this case.
384                 */
385                 BE_TIMER_PUSH(t_ra_spill);
386                 check_for_memory_operands(chordal_env);
387                 if (iteration == 0) {
388                         be_abi_fix_stack_nodes(birg->abi);
389                 }
390                 BE_TIMER_POP(t_ra_spill);
391
392                 BE_TIMER_PUSH(t_verify);
393
394                 /* verify schedule and register pressure */
395                 if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
396                         be_verify_schedule(birg);
397                         be_verify_register_pressure(birg, pse->cls, irg);
398                 }
399                 else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
400                         assert(be_verify_schedule(birg) && "Schedule verification failed");
401                         assert(be_verify_register_pressure(birg, pse->cls, irg)
402                                 && "Register pressure verification failed");
403                 }
404                 BE_TIMER_POP(t_verify);
405
406                 /* Color the graph. */
407                 BE_TIMER_PUSH(t_ra_color);
408                 be_ra_chordal_color(chordal_env);
409                 BE_TIMER_POP(t_ra_color);
410
411                 dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
412
413                 /* Create the ifg with the selected flavor */
414                 BE_TIMER_PUSH(t_ra_ifg);
415                 chordal_env->ifg = be_create_ifg(chordal_env);
416                 BE_TIMER_POP(t_ra_ifg);
417
418                 {
419                         be_ifg_stat_t stat;
420
421                         stat_ev_do(be_ifg_stat(birg, chordal_env->ifg, &stat));
422                         stat_ev_dbl("ifg_nodes", stat.n_nodes);
423                         stat_ev_dbl("ifg_edges", stat.n_edges);
424                         stat_ev_dbl("ifg_comps", stat.n_comps);
425
426                         stat_ev_do(node_stats(birg, pse->cls, &node_stat));
427                         stat_ev_dbl("perms_before_coal", node_stat.n_perms);
428                         stat_ev_dbl("copies_before_coal", node_stat.n_copies);
429                 }
430
431                 /* copy minimization */
432                 BE_TIMER_PUSH(t_ra_copymin);
433                 co_driver(chordal_env);
434                 BE_TIMER_POP(t_ra_copymin);
435
436                 dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "-copymin", dump_ir_block_graph_sched);
437
438
439                 /* ssa destruction */
440                 BE_TIMER_PUSH(t_ra_ssa);
441                 stat_ev_ctx_push_str("berachordal_phase", "ssadestr");
442                 be_ssa_destruction(chordal_env);
443                 stat_ev_ctx_pop("berachordal_phase");
444                 BE_TIMER_POP(t_ra_ssa);
445
446                 dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
447
448                 if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
449                         BE_TIMER_PUSH(t_verify);
450                         be_ssa_destruction_check(chordal_env);
451                         BE_TIMER_POP(t_verify);
452                 }
453
454                 stat_ev_do(node_stats(birg, pse->cls, &node_stat));
455                 stat_ev_dbl("perms_after_coal", node_stat.n_perms);
456                 stat_ev_dbl("copies_after_coal", node_stat.n_copies);
457                 stat_ev_ctx_pop("bechordal_cls");
458
459                 /* the ifg exists only if there are allocatable regs */
460                 be_ifg_free(chordal_env->ifg);
461         }
462
463         /* free some always allocated data structures */
464         pmap_destroy(chordal_env->border_heads);
465         bitset_free(chordal_env->ignore_colors);
466 }
467
468 /**
469  * Performs chordal register allocation for each register class on given irg.
470  *
471  * @param birg  Backend irg object
472  * @return Structure containing timer for the single phases or NULL if no timing requested.
473  */
474 static void be_ra_chordal_main(be_irg_t *birg)
475 {
476         const be_main_env_t *main_env  = birg->main_env;
477         const arch_isa_t    *isa       = arch_env_get_isa(main_env->arch_env);
478         ir_graph            *irg       = birg->irg;
479         int                 j, m;
480         be_chordal_env_t    chordal_env;
481         struct obstack      obst;
482
483         main_opts = main_env->options;
484
485         BE_TIMER_PUSH(t_ra_other);
486
487         BE_TIMER_PUSH(t_ra_prolog);
488
489         be_assure_dom_front(birg);
490         be_assure_liveness(birg);
491
492         chordal_env.obst          = &obst;
493         chordal_env.opts          = &options;
494         chordal_env.irg           = irg;
495         chordal_env.birg          = birg;
496         chordal_env.border_heads  = NULL;
497         chordal_env.ifg           = NULL;
498         chordal_env.ignore_colors = NULL;
499
500         obstack_init(&obst);
501
502         BE_TIMER_POP(t_ra_prolog);
503
504         stat_ev_if {
505                 be_stat_ev("insns_before", count_insns(irg));
506         }
507
508         if (! arch_code_generator_has_spiller(birg->cg)) {
509                 /* use one of the generic spiller */
510
511                 /* Perform the following for each register class. */
512                 for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
513                         post_spill_env_t pse;
514                         const arch_register_class_t *cls
515                                 = arch_isa_get_reg_class(isa, j);
516
517                         if(arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
518                                 continue;
519
520
521                         memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
522                         pse.birg = birg;
523                         pre_spill(&pse, cls);
524
525                         BE_TIMER_PUSH(t_ra_spill);
526                         be_do_spill(birg, cls);
527                         BE_TIMER_POP(t_ra_spill);
528
529                         dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill",
530                              dump_ir_block_graph_sched);
531
532                         post_spill(&pse, 0);
533                 }
534         } else {
535                 post_spill_env_t *pse;
536
537                 /* the backend has it's own spiller */
538                 m = arch_isa_get_n_reg_class(isa);
539
540                 pse = alloca(m * sizeof(pse[0]));
541
542                 for (j = 0; j < m; ++j) {
543                         memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
544                         pse[j].birg = birg;
545                         pre_spill(&pse[j], pse[j].cls);
546                 }
547
548                 BE_TIMER_PUSH(t_ra_spill);
549                 arch_code_generator_spill(birg->cg, birg);
550                 BE_TIMER_POP(t_ra_spill);
551                 dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
552
553                 for (j = 0; j < m; ++j) {
554                         post_spill(&pse[j], j);
555                 }
556         }
557
558         BE_TIMER_PUSH(t_verify);
559         be_verify_register_allocation(birg);
560         BE_TIMER_POP(t_verify);
561
562         BE_TIMER_PUSH(t_ra_epilog);
563         lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
564         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
565
566         obstack_free(&obst, NULL);
567         be_liveness_invalidate(be_get_birg_liveness(birg));
568         BE_TIMER_POP(t_ra_epilog);
569
570         BE_TIMER_POP(t_ra_other);
571
572         stat_ev_if {
573                 be_stat_ev("insns_after", count_insns(irg));
574         }
575
576         return;
577 }
578
579 static be_ra_t be_ra_chordal_allocator = {
580         be_ra_chordal_main,
581 };
582
583 void be_init_chordal_main(void)
584 {
585         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
586         lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
587         lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
588
589         lc_opt_add_table(chordal_grp, be_chordal_options);
590
591         be_register_allocator("chordal", &be_ra_chordal_allocator);
592 }
593
594 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);