removed chordal_env from be lowering
[libfirm] / ir / be / bemain.c
1 /**
2  * Backend driver.
3  * @author Sebastian Hack
4  * @date   25.11.2004
5  * @cvsid  $Id$
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <stdarg.h>
12 #include <stdio.h>
13
14 #ifdef WITH_LIBCORE
15 #include <libcore/lc_opts.h>
16 #include <libcore/lc_opts_enum.h>
17 #include <libcore/lc_timing.h>
18 #endif /* WITH_LIBCORE */
19
20 #include "obst.h"
21 #include "bitset.h"
22
23 #include "irprog.h"
24 #include "irgopt.h"
25 #include "irgraph.h"
26 #include "irdump.h"
27 #include "phiclass.h"
28 #include "irdom_t.h"
29 #include "iredges_t.h"
30 #include "irloop_t.h"
31 #include "irtools.h"
32 #include "irvrfy.h"
33 #include "irprintf.h"
34 #include "return.h"
35 #include "firmstat.h"
36 #include "cfopt.h"
37 #include "execfreq.h"
38
39 #include "bearch.h"
40 #include "be_t.h"
41 #include "bemodule.h"
42 #include "beutil.h"
43 #include "benode_t.h"
44 #include "beirgmod.h"
45 #include "besched_t.h"
46 #include "belistsched.h"
47 #include "belive_t.h"
48 #include "bespillbelady.h"
49 #include "bera.h"
50 #include "beraextern.h"
51 #include "bechordal_t.h"
52 #include "beifg.h"
53 #include "beifg_impl.h"
54 #include "becopyopt.h"
55 #include "becopystat.h"
56 #include "bessadestr.h"
57 #include "beabi.h"
58 #include "belower.h"
59 #include "beschedmris.h"
60 #include "bestat.h"
61 #include "beverify.h"
62 #include "beprofile.h"
63 #include "beblocksched.h"
64 #include "be_dbgout.h"
65
66 #ifdef WITH_ILP
67 #include "beilpsched.h"
68 #endif /* WITH_ILP */
69
70 /* options visible for anyone */
71 static be_options_t be_options = {
72         DUMP_NONE,                         /* dump flags */
73         BE_TIME_OFF,                       /* no timing */
74         0,                                 /* no opt profile */
75         1,                                 /* try to omit frame pointer */
76         0,                                 /* always stabs debugging output */
77         BE_VRFY_WARN,                      /* verification level: warn */
78         BE_SCHED_LIST,                     /* scheduler: list scheduler */
79         "i44pc52.info.uni-karlsruhe.de",   /* ilp server */
80         "cplex"                            /* ilp solver */
81 };
82
83 /* config file. */
84 static char config_file[256] = { 0 };
85
86 /* back end instruction set architecture to use */
87 static const arch_isa_if_t *isa_if = NULL;
88
89 #ifdef WITH_LIBCORE
90
91 /* possible dumping options */
92 static const lc_opt_enum_mask_items_t dump_items[] = {
93         { "none",       DUMP_NONE },
94         { "initial",    DUMP_INITIAL },
95         { "abi",        DUMP_ABI    },
96         { "sched",      DUMP_SCHED  },
97         { "prepared",   DUMP_PREPARED },
98         { "regalloc",   DUMP_RA },
99         { "final",      DUMP_FINAL },
100         { "be",         DUMP_BE },
101         { "all",        2 * DUMP_BE - 1 },
102         { NULL,         0 }
103 };
104
105 /* verify options. */
106 static const lc_opt_enum_int_items_t vrfy_items[] = {
107         { "off",    BE_VRFY_OFF    },
108         { "warn",   BE_VRFY_WARN   },
109         { "assert", BE_VRFY_ASSERT },
110         { NULL,     0 }
111 };
112
113 /* scheduling options. */
114 static const lc_opt_enum_int_items_t sched_items[] = {
115         { "list", BE_SCHED_LIST },
116 #ifdef WITH_ILP
117         { "ilp",  BE_SCHED_ILP  },
118 #endif /* WITH_ILP */
119         { NULL,   0 }
120 };
121
122 static lc_opt_enum_mask_var_t dump_var = {
123         &be_options.dump_flags, dump_items
124 };
125
126 static lc_opt_enum_int_var_t vrfy_var = {
127         &be_options.vrfy_option, vrfy_items
128 };
129
130 static lc_opt_enum_int_var_t sched_var = {
131         &be_options.scheduler, sched_items
132 };
133
134 static const lc_opt_table_entry_t be_main_options[] = {
135         LC_OPT_ENT_STR      ("config",   "read another config file containing backend options", config_file, sizeof(config_file)),
136         LC_OPT_ENT_ENUM_MASK("dump",     "dump irg on several occasions",                       &dump_var),
137         LC_OPT_ENT_NEGBOOL  ("noomitfp", "do not omit frame pointer",                           &be_options.omit_fp),
138         LC_OPT_ENT_BOOL     ("stabs",    "enable stabs debug support",                          &be_options.stabs_debug_support),
139         LC_OPT_ENT_ENUM_PTR ("vrfy",     "verify the backend irg",                              &vrfy_var),
140         LC_OPT_ENT_BOOL     ("time",     "get backend timing statistics",                       &be_options.timing),
141         LC_OPT_ENT_BOOL     ("profile",  "instrument the code for execution count profiling",   &be_options.opt_profile),
142         LC_OPT_ENT_ENUM_PTR ("sched",    "select a scheduler",                                  &sched_var),
143         LC_OPT_ENT_STR      ("statfile", "append statistics to file statfile", &be_options.stat_file_name, sizeof(be_options.stat_file_name)),
144
145 #ifdef WITH_ILP
146         LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
147         LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
148 #endif /* WITH_ILP */
149         { NULL }
150 };
151
152 #endif /* WITH_LIBCORE */
153
154 static be_module_list_entry_t *isa_ifs = NULL;
155
156 void be_register_isa_if(const char *name, const arch_isa_if_t *isa)
157 {
158         if(isa_if == NULL)
159                 isa_if = isa;
160
161         be_add_module_to_list(&isa_ifs, name, (void*) isa);
162 }
163
164 void be_opt_register(void)
165 {
166 #ifdef WITH_LIBCORE
167         lc_opt_entry_t *be_grp;
168         static int run_once = 0;
169
170         if (run_once) {
171                 return;
172         }
173         run_once     = 1;
174
175         be_init_modules();
176
177         be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
178         lc_opt_add_table(be_grp, be_main_options);
179
180         be_add_module_list_opt(be_grp, "isa", "the instruction set architecture",
181                                &isa_ifs, (void**) &isa_if);
182 #endif /* WITH_LIBCORE */
183 }
184
185 /* Parse one argument. */
186 int be_parse_arg(const char *arg) {
187 #ifdef WITH_LIBCORE
188         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
189         if (strcmp(arg, "help") == 0 || (arg[0] == '?' && arg[1] == '\0')) {
190                 lc_opt_print_help(be_grp, stdout);
191                 return -1;
192         }
193         return lc_opt_from_single_arg(be_grp, NULL, arg, NULL);
194 #else
195         return 0;
196 #endif /* WITH_LIBCORE */
197 }
198
199 /** The be parameters returned by default, all off. */
200 const static backend_params be_params = {
201         NULL,
202         NULL,
203         0,
204         NULL,
205 };
206
207 /* Perform schedule verification if requested. */
208 static void be_sched_vrfy(ir_graph *irg, int vrfy_opt) {
209         if (vrfy_opt == BE_VRFY_WARN) {
210                 be_verify_schedule(irg);
211         }
212         else if (vrfy_opt == BE_VRFY_ASSERT) {
213                 assert(be_verify_schedule(irg) && "Schedule verification failed.");
214         }
215 }
216
217 /* Initialize the Firm backend. Must be run BEFORE init_firm()! */
218 const backend_params *be_init(void)
219 {
220         be_opt_register();
221         be_init_modules();
222         phi_class_init();
223
224         if (isa_if->get_params)
225                 return isa_if->get_params();
226         return &be_params;
227 }
228
229 /**
230  * Initializes the main environment for the backend.
231  *
232  * @param env          an empty environment
233  * @param file_handle  the file handle where the output will be written to
234  */
235 static be_main_env_t *be_init_env(be_main_env_t *env, FILE *file_handle)
236 {
237         memset(env, 0, sizeof(*env));
238         obstack_init(&env->obst);
239         env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
240         env->options  = &be_options;
241         FIRM_DBG_REGISTER(env->dbg, "be.main");
242
243         arch_env_init(env->arch_env, isa_if, file_handle, env);
244
245         /* Register the irn handler of the architecture */
246         if (arch_isa_get_irn_handler(env->arch_env->isa))
247                 arch_env_push_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
248
249         /*
250          * Register the node handler of the back end infrastructure.
251          * This irn handler takes care of the platform independent
252          * spill, reload and perm nodes.
253          */
254         arch_env_push_irn_handler(env->arch_env, &be_node_irn_handler);
255         env->phi_handler = be_phi_handler_new(env->arch_env);
256         arch_env_push_irn_handler(env->arch_env, env->phi_handler);
257
258         env->db_handle = be_options.stabs_debug_support ? be_stabs_open(file_handle) : be_nulldbg_open();
259         return env;
260 }
261
262 static void be_done_env(be_main_env_t *env)
263 {
264         env->arch_env->isa->impl->done(env->arch_env->isa);
265         be_dbg_close(env->db_handle);
266         be_phi_handler_free(env->phi_handler);
267         obstack_free(&env->obst, NULL);
268 }
269
270 /**
271  * A wrapper around a firm dumper. Dumps only, if
272  * flags are enabled.
273  *
274  * @param mask    a bitmask containing the reason what will be dumped
275  * @param irg     the IR graph to dump
276  * @param suffix  the suffix for the dumper
277  * @param dumper  the dumper to be called
278  */
279 static void dump(int mask, ir_graph *irg, const char *suffix,
280                  void (*dumper)(ir_graph *, const char *))
281 {
282         if(be_options.dump_flags & mask)
283                 be_dump(irg, suffix, dumper);
284 }
285
286 /**
287  * Prepare a backend graph for code generation and initialize its birg
288  */
289 static void initialize_birg(be_irg_t *birg, ir_graph *irg, be_main_env_t *env)
290 {
291         memset(birg, 0, sizeof(*birg));
292         birg->irg = irg;
293         birg->main_env = env;
294
295         edges_deactivate_kind(irg, EDGE_KIND_DEP);
296         edges_activate_kind(irg, EDGE_KIND_DEP);
297
298         DBG((env->dbg, LEVEL_2, "====> IRG: %F\n", irg));
299         dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
300
301         be_stat_init_irg(env->arch_env, irg);
302         be_do_stat_nodes(irg, "01 Begin");
303
304         /* set the current graph (this is important for several firm functions) */
305         current_ir_graph = irg;
306
307         /* Normalize proj nodes. */
308         normalize_proj_nodes(irg);
309
310         /* Make just one return node. */
311         normalize_one_return(irg);
312
313         /* Remove critical edges */
314         remove_critical_cf_edges(irg);
315
316         /* Ensure, that the ir_edges are computed. */
317         edges_assure(irg);
318
319         /* reset the phi handler. */
320         be_phi_handler_reset(env->phi_handler);
321 }
322
323 #ifdef WITH_LIBCORE
324
325 #define BE_TIMER_PUSH(timer)                                                        \
326         if (be_options.timing == BE_TIME_ON) {                                          \
327                 int res = lc_timer_push(timer);                                             \
328                 if (be_options.vrfy_option == BE_VRFY_ASSERT)                               \
329                         assert(res && "Timer already on stack, cannot be pushed twice.");       \
330                 else if (be_options.vrfy_option == BE_VRFY_WARN && ! res)                   \
331                         fprintf(stderr, "Timer %s already on stack, cannot be pushed twice.\n", \
332                                 lc_timer_get_name(timer));                                          \
333         }
334 #define BE_TIMER_POP(timer)                                                                    \
335         if (be_options.timing == BE_TIME_ON) {                                                     \
336                 lc_timer_t *tmp = lc_timer_pop();                                                      \
337                 if (be_options.vrfy_option == BE_VRFY_ASSERT)                                          \
338                         assert(tmp == timer && "Attempt to pop wrong timer.");                             \
339                 else if (be_options.vrfy_option == BE_VRFY_WARN && tmp != timer)                       \
340                         fprintf(stderr, "Attempt to pop wrong timer. %s is on stack, trying to pop %s.\n", \
341                                 lc_timer_get_name(tmp), lc_timer_get_name(timer));                             \
342                 timer = tmp;                                                                           \
343         }
344
345 #define BE_TIMER_ONLY(code)   do { if (be_options.timing == BE_TIME_ON) { code; } } while(0)
346
347 #else
348
349 #define BE_TIMER_PUSH(timer)
350 #define BE_TIMER_POP(timer)
351 #define BE_TIMER_ONLY(code)
352
353 #endif /* WITH_LIBCORE */
354
355
356 /**
357  * The Firm backend main loop.
358  * Do architecture specific lowering for all graphs
359  * and call the architecture specific code generator.
360  *
361  * @param file_handle   the file handle the output will be written to
362  * @param cup_name      name of the compilation unit
363  */
364 static void be_main_loop(FILE *file_handle, const char *cup_name)
365 {
366         int i;
367         arch_isa_t *isa;
368         be_main_env_t env;
369         unsigned num_nodes_b = 0;
370         unsigned num_nodes_a = 0;
371         unsigned num_nodes_r = 0;
372         char prof_filename[256];
373         static const char suffix[] = ".prof";
374         be_irg_t *birgs;
375         unsigned num_birgs;
376
377         be_ra_timer_t *ra_timer;
378
379 #ifdef WITH_LIBCORE
380         lc_timer_t *t_abi      = NULL;
381         lc_timer_t *t_codegen  = NULL;
382         lc_timer_t *t_sched    = NULL;
383         lc_timer_t *t_constr   = NULL;
384         lc_timer_t *t_regalloc = NULL;
385         lc_timer_t *t_finish   = NULL;
386         lc_timer_t *t_emit     = NULL;
387         lc_timer_t *t_other    = NULL;
388         lc_timer_t *t_verify   = NULL;
389
390         if (be_options.timing == BE_TIME_ON) {
391                 t_abi      = lc_timer_register("beabi",    "be abi introduction");
392                 t_codegen  = lc_timer_register("codegen",  "codegeneration");
393                 t_sched    = lc_timer_register("sched",    "scheduling");
394                 t_constr   = lc_timer_register("constr",   "assure constraints");
395                 t_regalloc = lc_timer_register("regalloc", "register allocation");
396                 t_finish   = lc_timer_register("finish",   "graph finish");
397                 t_emit     = lc_timer_register("emiter",   "code emiter");
398                 t_verify   = lc_timer_register("verify",   "graph verification");
399                 t_other    = lc_timer_register("other",    "other");
400         }
401 #endif /* WITH_LIBCORE */
402
403         be_init_env(&env, file_handle);
404
405         isa = arch_env_get_isa(env.arch_env);
406
407         be_dbg_so(env.db_handle, cup_name);
408         be_dbg_types(env.db_handle);
409
410         /* we might need 1 birg more for instrumentation constructor */
411         num_birgs = get_irp_n_irgs();
412         birgs     = alloca(sizeof(birgs[0]) * (num_birgs + 1));
413
414         /* First: initialize all birgs */
415         for(i = 0; i < get_irp_n_irgs(); ++i) {
416                 ir_graph *irg = get_irp_irg(i);
417
418                 initialize_birg(&birgs[i], irg, &env);
419         }
420
421         /*
422                 Get the filename for the profiling data.
423                 Beware: '\0' is already included in sizeof(suffix)
424         */
425         memset(prof_filename, 0, sizeof(prof_filename));
426         strncpy(prof_filename, cup_name, sizeof(prof_filename) - sizeof(suffix));
427         strcat(prof_filename, suffix);
428
429         /*
430                 Next: Either instruments all irgs with profiling code
431                 or try to read in profile data for current translation unit.
432         */
433         if (be_options.opt_profile) {
434                 ir_graph *prof_init_irg = be_profile_instrument(prof_filename, profile_default);
435                 initialize_birg(&birgs[num_birgs], prof_init_irg, &env);
436                 num_birgs++;
437                 set_method_img_section(get_irg_entity(prof_init_irg), section_constructors);
438         }
439         else {
440                 be_profile_read(prof_filename);
441         }
442
443         /* For all graphs */
444         for (i = 0; i < num_birgs; ++i) {
445                 be_irg_t *birg = & birgs[i];
446                 ir_graph *irg  = birg->irg;
447                 optimization_state_t state;
448                 const arch_code_generator_if_t *cg_if;
449                 char irg_name[128];
450
451                 /* set the current graph (this is important for several firm functions) */
452                 current_ir_graph = irg;
453
454 #ifdef FIRM_STATISTICS
455                 if(be_stat_ev_is_active()) {
456                         ir_snprintf(irg_name, sizeof(irg_name), "%F", irg);
457                         be_stat_tags[STAT_TAG_CLS] = "<all>";
458                         be_stat_tags[STAT_TAG_IRG] = irg_name;
459                         be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
460                 }
461 #endif
462
463                 /* stop and reset timers */
464                 BE_TIMER_ONLY(
465                         LC_STOP_AND_RESET_TIMER(t_abi);
466                         LC_STOP_AND_RESET_TIMER(t_codegen);
467                         LC_STOP_AND_RESET_TIMER(t_sched);
468                         LC_STOP_AND_RESET_TIMER(t_constr);
469                         LC_STOP_AND_RESET_TIMER(t_regalloc);
470                         LC_STOP_AND_RESET_TIMER(t_finish);
471                         LC_STOP_AND_RESET_TIMER(t_emit);
472                         LC_STOP_AND_RESET_TIMER(t_verify);
473                         LC_STOP_AND_RESET_TIMER(t_other);
474                 );
475                 BE_TIMER_PUSH(t_other);   /* t_other */
476
477                 /* Verify the initial graph */
478                 BE_TIMER_PUSH(t_verify);
479                 if (be_options.vrfy_option == BE_VRFY_WARN) {
480                         irg_verify(irg, VRFY_ENFORCE_SSA);
481                         be_check_dominance(irg);
482                 } else if (be_options.vrfy_option == BE_VRFY_ASSERT) {
483                         assert(irg_verify(irg, VRFY_ENFORCE_SSA) && "irg verification failed");
484                         assert(be_check_dominance(irg) && "Dominance verification failed");
485                 }
486                 BE_TIMER_POP(t_verify);
487
488                 /**
489                  * Create execution frequencies from profile data or estimate some
490                  */
491                 if (be_profile_has_data()) {
492                         birg->exec_freq = be_create_execfreqs_from_profile(irg);
493                 } else {
494                         birg->exec_freq = compute_execfreq(irg, 10);
495                 }
496
497                 BE_TIMER_ONLY(num_nodes_b = get_num_reachable_nodes(irg));
498
499                 /* Get the code generator interface. */
500                 cg_if = isa->impl->get_code_generator_if(isa);
501
502                 /* get a code generator for this graph. */
503                 birg->cg = cg_if->init(birg);
504
505                 /* some transformations need to be done before abi introduce */
506                 arch_code_generator_before_abi(birg->cg);
507
508                 /* reset the phi handler. */
509                 be_phi_handler_reset(env.phi_handler);
510
511                 /* implement the ABI conventions. */
512                 BE_TIMER_PUSH(t_abi);
513                 birg->abi = be_abi_introduce(birg);
514                 BE_TIMER_POP(t_abi);
515
516                 dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
517                 be_do_stat_nodes(irg, "02 Abi");
518
519                 /* generate code */
520                 BE_TIMER_PUSH(t_codegen);
521                 arch_code_generator_prepare_graph(birg->cg);
522                 BE_TIMER_POP(t_codegen);
523
524                 be_do_stat_nodes(irg, "03 Prepare");
525
526                 /*
527                         Since the code generator made a lot of new nodes and skipped
528                         a lot of old ones, we should do dead node elimination here.
529                         Note that this requires disabling the edges here.
530                 */
531                 edges_deactivate(irg);
532                 //dead_node_elimination(irg);
533                 edges_activate(irg);
534
535                 /* Compute loop nesting information (for weighting copies) */
536                 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
537                 BE_TIMER_ONLY(num_nodes_r = get_num_reachable_nodes(irg));
538
539                 /* let backend prepare scheduling */
540                 BE_TIMER_PUSH(t_codegen);
541                 arch_code_generator_before_sched(birg->cg);
542                 BE_TIMER_POP(t_codegen);
543
544                 /* schedule the irg */
545                 BE_TIMER_PUSH(t_sched);
546                 switch (be_options.scheduler) {
547                         default:
548                                 fprintf(stderr, "Warning: invalid scheduler (%d) selected, falling back to list scheduler.\n", be_options.scheduler);
549                         case BE_SCHED_LIST:
550                                 list_sched(birg, &be_options);
551                                 break;
552 #ifdef WITH_ILP
553                         case BE_SCHED_ILP:
554                                 be_ilp_sched(birg);
555                                 //fprintf(stderr, "Warning: ILP scheduler not yet fully implemented, falling back to list scheduler.\n");
556                                 //list_sched(birg, &be_options);
557                                 break;
558 #endif /* WITH_ILP */
559                 };
560                 BE_TIMER_POP(t_sched);
561
562                 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
563
564                 /* check schedule */
565                 BE_TIMER_PUSH(t_verify);
566                 be_sched_vrfy(irg, be_options.vrfy_option);
567                 BE_TIMER_POP(t_verify);
568
569                 be_do_stat_nodes(irg, "04 Schedule");
570
571                 /* introduce patterns to assure constraints */
572                 BE_TIMER_PUSH(t_constr);
573                 /* we switch off optimizations here, because they might cause trouble */
574                 save_optimization_state(&state);
575                 set_optimize(0);
576                 set_opt_normalize(0);
577
578                 /* add Keeps for should_be_different constrained nodes  */
579                 /* beware: needs schedule due to usage of be_ssa_constr */
580                 assure_constraints(birg);
581                 BE_TIMER_POP(t_constr);
582
583                 dump(DUMP_SCHED, irg, "-assured", dump_ir_block_graph_sched);
584                 be_do_stat_nodes(irg, "05 Constraints");
585
586                 /* connect all stack modifying nodes together (see beabi.c) */
587                 BE_TIMER_PUSH(t_abi);
588                 be_abi_fix_stack_nodes(birg->abi, NULL);
589                 BE_TIMER_POP(t_abi);
590
591                 dump(DUMP_SCHED, irg, "-fix_stack", dump_ir_block_graph_sched);
592
593                 /* check schedule */
594                 BE_TIMER_PUSH(t_verify);
595                 be_sched_vrfy(irg, be_options.vrfy_option);
596                 BE_TIMER_POP(t_verify);
597
598                 /* do some statistics */
599                 be_do_stat_reg_pressure(birg);
600
601                 /* stuff needs to be done after scheduling but before register allocation */
602                 BE_TIMER_PUSH(t_codegen);
603                 arch_code_generator_before_ra(birg->cg);
604                 BE_TIMER_POP(t_codegen);
605
606 #ifdef FIRM_STATISTICS
607                 if(be_stat_ev_is_active()) {
608                         be_stat_ev_l("costs_before_ra",
609                                         (long) be_estimate_irg_costs(irg, env.arch_env, birg->exec_freq));
610                 }
611 #endif
612
613                 /* Do register allocation */
614                 BE_TIMER_PUSH(t_regalloc);
615                 be_allocate_registers(birg);
616                 BE_TIMER_POP(t_regalloc);
617
618 #ifdef FIRM_STATISTICS
619                 if(be_stat_ev_is_active()) {
620                         be_stat_ev_l("costs_after_ra",
621                                         (long) be_estimate_irg_costs(irg, env.arch_env, birg->exec_freq));
622                 }
623 #endif
624
625                 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
626                 be_do_stat_nodes(irg, "06 Register Allocation");
627
628                 /* let the code generator prepare the graph for emitter */
629                 BE_TIMER_PUSH(t_finish);
630                 arch_code_generator_after_ra(birg->cg);
631                 BE_TIMER_POP(t_finish);
632
633                 /* fix stack offsets */
634                 BE_TIMER_PUSH(t_abi);
635                 be_abi_fix_stack_nodes(birg->abi, NULL);
636                 be_remove_dead_nodes_from_schedule(irg);
637                 be_abi_fix_stack_bias(birg->abi);
638                 BE_TIMER_POP(t_abi);
639
640                 BE_TIMER_PUSH(t_finish);
641                 arch_code_generator_finish(birg->cg);
642                 BE_TIMER_POP(t_finish);
643
644                 dump(DUMP_FINAL, irg, "-finish", dump_ir_block_graph_sched);
645
646                 /* check schedule and register allocation */
647                 BE_TIMER_PUSH(t_verify);
648                 if (be_options.vrfy_option == BE_VRFY_WARN) {
649                         //irg_verify(irg, VRFY_ENFORCE_SSA);
650                         be_check_dominance(irg);
651                         be_verify_out_edges(irg);
652                         be_verify_schedule(irg);
653                         be_verify_register_allocation(env.arch_env, irg);
654                 }
655                 else if (be_options.vrfy_option == BE_VRFY_ASSERT) {
656                         //assert(irg_verify(irg, VRFY_ENFORCE_SSA) && "irg verification failed");
657                         assert(be_verify_out_edges(irg));
658                         assert(be_check_dominance(irg) && "Dominance verification failed");
659                         assert(be_verify_schedule(irg) && "Schedule verification failed");
660                         assert(be_verify_register_allocation(env.arch_env, irg)
661                                && "register allocation verification failed");
662                 }
663                 BE_TIMER_POP(t_verify);
664
665                 /* emit assembler code */
666                 BE_TIMER_PUSH(t_emit);
667                 arch_code_generator_done(birg->cg);
668                 BE_TIMER_POP(t_emit);
669
670                 dump(DUMP_FINAL, irg, "-end", dump_ir_extblock_graph_sched);
671
672                 BE_TIMER_PUSH(t_abi);
673                 be_abi_free(birg->abi);
674                 BE_TIMER_POP(t_abi);
675
676                 be_do_stat_nodes(irg, "07 Final");
677                 restore_optimization_state(&state);
678
679                 BE_TIMER_ONLY(num_nodes_a = get_num_reachable_nodes(irg));
680                 BE_TIMER_POP(t_other);
681
682 #define LC_EMIT(timer)  \
683         if(!be_stat_ev_is_active()) { \
684                 printf("%-20s: %.3lf msec\n", lc_timer_get_description(timer), (double)lc_timer_elapsed_usec(timer) / 1000.0); \
685         } else { \
686                 be_stat_ev_l(lc_timer_get_name(timer), lc_timer_elapsed_msec(timer)); \
687         }
688 #define LC_EMIT_RA(timer) \
689         if(!be_stat_ev_is_active()) { \
690                 printf("\t%-20s: %.3lf msec\n", lc_timer_get_description(timer), (double)lc_timer_elapsed_usec(timer) / 1000.0); \
691         } else { \
692                 be_stat_ev_l(lc_timer_get_name(timer), lc_timer_elapsed_msec(timer)); \
693         }
694                 BE_TIMER_ONLY(
695                         if(!be_stat_ev_is_active()) {
696                                 printf("==>> IRG %s <<==\n", get_entity_name(get_irg_entity(irg)));
697                                 printf("# nodes at begin:  %u\n", num_nodes_b);
698                                 printf("# nodes before ra: %u\n", num_nodes_r);
699                                 printf("# nodes at end:    %u\n\n", num_nodes_a);
700                         }
701                         LC_EMIT(t_abi);
702                         LC_EMIT(t_codegen);
703                         LC_EMIT(t_sched);
704                         LC_EMIT(t_constr);
705                         LC_EMIT(t_regalloc);
706                         LC_EMIT_RA(ra_timer->t_prolog);
707                         LC_EMIT_RA(ra_timer->t_live);
708                         LC_EMIT_RA(ra_timer->t_spill);
709                         LC_EMIT_RA(ra_timer->t_spillslots);
710                         LC_EMIT_RA(ra_timer->t_color);
711                         LC_EMIT_RA(ra_timer->t_ifg);
712                         LC_EMIT_RA(ra_timer->t_copymin);
713                         LC_EMIT_RA(ra_timer->t_ssa);
714                         LC_EMIT_RA(ra_timer->t_epilog);
715                         LC_EMIT_RA(ra_timer->t_verify);
716                         LC_EMIT_RA(ra_timer->t_other);
717                         LC_EMIT(t_finish);
718                         LC_EMIT(t_emit);
719                         LC_EMIT(t_verify);
720                         LC_EMIT(t_other);
721                 );
722 #undef LC_EMIT_RA
723 #undef LC_EMIT
724
725                 be_free_birg(birg);
726
727         /* switched off due to statistics (statistic module needs all irgs) */
728 #ifdef FIRM_STATISTICS
729                 if (! stat_is_active())
730 #endif
731                         free_ir_graph(irg);
732
733                 if(be_stat_ev_is_active()) {
734                         be_stat_ev_pop();
735                 }
736         }
737         be_profile_free();
738         be_done_env(&env);
739
740 #undef BE_TIMER_POP
741 #undef BE_TIMER_PUSH
742 #undef BE_TIMER_ONLY
743 }
744
745 /* Main interface to the frontend. */
746 void be_main(FILE *file_handle, const char *cup_name)
747 {
748 #ifdef WITH_LIBCORE
749         lc_timer_t *t = NULL;
750
751         /* The user specified another config file to read. do that now. */
752         if(strlen(config_file) > 0) {
753                 FILE *f;
754
755                 if((f = fopen(config_file, "rt")) != NULL) {
756                         lc_opt_from_file(config_file, f, NULL);
757                         fclose(f);
758                 }
759         }
760
761         if (be_options.timing == BE_TIME_ON) {
762                 t = lc_timer_register("bemain", "measure complete bemain loop");
763
764                 if (lc_timer_enter_high_priority()) {
765                         fprintf(stderr, "Warning: Could not enter high priority mode.\n");
766                 }
767
768                 lc_timer_reset_and_start(t);
769         }
770
771 #ifdef FIRM_STATISTICS
772         be_init_stat_file(be_options.stat_file_name, cup_name);
773 #endif
774 #endif /* WITH_LIBCORE */
775
776         /* never build code for pseudo irgs */
777         set_visit_pseudo_irgs(0);
778
779         be_node_init();
780
781         be_main_loop(file_handle, cup_name);
782
783 #ifdef WITH_LIBCORE
784         if (be_options.timing == BE_TIME_ON) {
785                 lc_timer_stop(t);
786                 lc_timer_leave_high_priority();
787                 if(be_stat_ev_is_active()) {
788                         be_stat_ev_l("backend_time", lc_timer_elapsed_msec(t));
789                 } else {
790                         printf("%-20s: %lu msec\n", "BEMAINLOOP", lc_timer_elapsed_msec(t));
791                 }
792         }
793
794 #ifdef FIRM_STATISTICS
795         be_close_stat_file();
796 #endif
797 #endif /* WITH_LIBCORE */
798 }
799
800 /** The debug info retriever function. */
801 static retrieve_dbg_func retrieve_dbg = NULL;
802
803 /* Sets a debug info retriever. */
804 void be_set_debug_retrieve(retrieve_dbg_func func) {
805         retrieve_dbg = func;
806 }
807
808 /* Retrieve the debug info. */
809 const char *be_retrieve_dbg_info(const dbg_info *dbg, unsigned *line) {
810         if (retrieve_dbg)
811                 return retrieve_dbg(dbg, line);
812
813         *line = 0;
814         return NULL;
815 }
816
817 int be_put_ignore_regs(const be_irg_t *birg, const arch_register_class_t *cls, bitset_t *bs)
818 {
819         if(bs == NULL)
820                 bs = bitset_alloca(cls->n_regs);
821         else
822                 bitset_clear_all(bs);
823
824         assert(bitset_size(bs) == (unsigned) cls->n_regs);
825         arch_put_non_ignore_regs(birg->main_env->arch_env, cls, bs);
826         bitset_flip_all(bs);
827         be_abi_put_ignore_regs(birg->abi, cls, bs);
828
829         return bitset_popcnt(bs);
830 }