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