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