+ /* stop and reset timers */
+ BE_TIMER_ONLY(
+ LC_STOP_AND_RESET_TIMER(t_abi);
+ LC_STOP_AND_RESET_TIMER(t_codegen);
+ LC_STOP_AND_RESET_TIMER(t_sched);
+ LC_STOP_AND_RESET_TIMER(t_constr);
+ LC_STOP_AND_RESET_TIMER(t_regalloc);
+ LC_STOP_AND_RESET_TIMER(t_finish);
+ LC_STOP_AND_RESET_TIMER(t_emit);
+ LC_STOP_AND_RESET_TIMER(t_verify);
+ LC_STOP_AND_RESET_TIMER(t_other);
+ );
+ BE_TIMER_PUSH(t_other); /* t_other */
+
+ /**
+ * Create execution frequencies from profile data or estimate some
+ */
+ if (be_profile_has_data()) {
+ birg->execfreqs = be_create_execfreqs_from_profile(irg);
+ } else {
+ birg->execfreqs = compute_execfreq(irg, 10);
+ }
+
+ BE_TIMER_ONLY(num_nodes_b = get_num_reachable_nodes(irg));
+
+ /* Get the code generator interface. */
+ cg_if = isa->impl->get_code_generator_if(isa);
+
+ /* get a code generator for this graph. */
+ birg->cg = cg_if->init(birg);
+
+ /* some transformations need to be done before abi introduce */
+ arch_code_generator_before_abi(birg->cg);
+
+ /* reset the phi handler. */
+ be_phi_handler_reset(env.phi_handler);
+
+ /* implement the ABI conventions. */
+ BE_TIMER_PUSH(t_abi);
+ birg->abi = be_abi_introduce(birg);
+ BE_TIMER_POP(t_abi);
+
+ dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
+ be_do_stat_nodes(irg, "02 Abi");
+
+ /* generate code */
+ BE_TIMER_PUSH(t_codegen);
+ arch_code_generator_prepare_graph(birg->cg);
+ BE_TIMER_POP(t_codegen);
+
+ be_do_stat_nodes(irg, "03 Prepare");
+
+ /*
+ Since the code generator made a lot of new nodes and skipped
+ a lot of old ones, we should do dead node elimination here.
+ Note that this requires disabling the edges here.
+ */
+ edges_deactivate(irg);
+ //dead_node_elimination(irg);
+ edges_activate(irg);
+
+ /* Compute loop nesting information (for weighting copies) */
+ construct_cf_backedges(irg);
+ dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
+ BE_TIMER_ONLY(num_nodes_r = get_num_reachable_nodes(irg));
+
+ /* let backend prepare scheduling */
+ BE_TIMER_PUSH(t_codegen);
+ arch_code_generator_before_sched(birg->cg);
+ BE_TIMER_POP(t_codegen);
+
+ /* schedule the irg */
+ BE_TIMER_PUSH(t_sched);
+ switch (be_options.scheduler) {
+ default:
+ fprintf(stderr, "Warning: invalid scheduler (%d) selected, falling back to list scheduler.\n", be_options.scheduler);
+ case BE_SCHED_LIST:
+ list_sched(birg, &be_options);
+ break;
+#ifdef WITH_ILP
+ case BE_SCHED_ILP:
+ be_ilp_sched(birg);
+ fprintf(stderr, "Warning: ILP scheduler not yet fully implemented, falling back to list scheduler.\n");
+ list_sched(birg, &be_options);
+ break;
+#endif /* WITH_ILP */
+ };
+ BE_TIMER_POP(t_sched);
+
+ dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
+
+ /* check schedule */
+ BE_TIMER_PUSH(t_verify);
+ be_sched_vrfy(irg, be_options.vrfy_option);
+ BE_TIMER_POP(t_verify);
+
+ be_do_stat_nodes(irg, "04 Schedule");
+
+ /* introduce patterns to assure constraints */
+ BE_TIMER_PUSH(t_constr);
+ /* we switch off optimizations here, because they might cause trouble */
+ save_optimization_state(&state);
+ set_optimize(0);
+ set_opt_normalize(0);
+
+ /* add Keeps for should_be_different constrained nodes */
+ /* beware: needs schedule due to usage of be_ssa_constr */
+ assure_constraints(birg);
+ BE_TIMER_POP(t_constr);
+
+ dump(DUMP_SCHED, irg, "-assured", dump_ir_block_graph_sched);
+ be_do_stat_nodes(irg, "05 Constraints");
+
+ /* connect all stack modifying nodes together (see beabi.c) */
+ BE_TIMER_PUSH(t_abi);
+ be_abi_fix_stack_nodes(birg->abi, NULL);
+ BE_TIMER_POP(t_abi);
+
+ dump(DUMP_SCHED, irg, "-fix_stack", dump_ir_block_graph_sched);
+
+ /* check schedule */
+ BE_TIMER_PUSH(t_verify);
+ be_sched_vrfy(irg, be_options.vrfy_option);
+ BE_TIMER_POP(t_verify);
+
+ /* do some statistics */
+ be_do_stat_reg_pressure(birg);
+
+ /* stuff needs to be done after scheduling but before register allocation */
+ BE_TIMER_PUSH(t_codegen);
+ arch_code_generator_before_ra(birg->cg);
+ BE_TIMER_POP(t_codegen);
+
+ /* Do register allocation */
+ BE_TIMER_PUSH(t_regalloc);
+ ra_timer = ra->allocate(birg);
+ BE_TIMER_POP(t_regalloc);
+
+ dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
+ be_do_stat_nodes(irg, "06 Register Allocation");
+
+ /* let the code generator prepare the graph for emitter */
+ BE_TIMER_PUSH(t_finish);
+ arch_code_generator_after_ra(birg->cg);
+ BE_TIMER_POP(t_finish);
+
+ /* fix stack offsets */
+ BE_TIMER_PUSH(t_abi);
+ be_abi_fix_stack_nodes(birg->abi, NULL);
+ be_remove_dead_nodes_from_schedule(irg);
+ be_abi_fix_stack_bias(birg->abi);
+ BE_TIMER_POP(t_abi);
+
+ BE_TIMER_PUSH(t_finish);
+ arch_code_generator_finish(birg->cg);
+ BE_TIMER_POP(t_finish);
+
+ dump(DUMP_FINAL, irg, "-finish", dump_ir_block_graph_sched);
+
+ /* check schedule and register allocation */
+ BE_TIMER_PUSH(t_verify);
+ if (be_options.vrfy_option == BE_VRFY_WARN) {
+ //irg_verify(irg, VRFY_ENFORCE_SSA);
+ be_check_dominance(irg);
+ be_verify_out_edges(irg);
+ be_verify_schedule(irg);
+ be_verify_register_allocation(env.arch_env, irg);
+ }
+ else if (be_options.vrfy_option == BE_VRFY_ASSERT) {
+ //assert(irg_verify(irg, VRFY_ENFORCE_SSA) && "irg verification failed");
+ assert(be_verify_out_edges(irg));
+ assert(be_check_dominance(irg) && "Dominance verification failed");
+ assert(be_verify_schedule(irg) && "Schedule verification failed");
+ assert(be_verify_register_allocation(env.arch_env, irg)
+ && "register allocation verification failed");
+ }
+ BE_TIMER_POP(t_verify);
+
+ /* emit assembler code */
+ BE_TIMER_PUSH(t_emit);
+ arch_code_generator_done(birg->cg);
+ BE_TIMER_POP(t_emit);
+
+ dump(DUMP_FINAL, irg, "-end", dump_ir_extblock_graph_sched);
+
+ BE_TIMER_PUSH(t_abi);
+ be_abi_free(birg->abi);
+ BE_TIMER_POP(t_abi);
+
+ be_do_stat_nodes(irg, "07 Final");
+ restore_optimization_state(&state);
+
+ BE_TIMER_ONLY(num_nodes_a = get_num_reachable_nodes(irg));
+ BE_TIMER_POP(t_other);
+
+#define LC_EMIT(timer) \
+ if(!be_stat_ev_is_active()) { \
+ printf("%-20s: %.3lf msec\n", lc_timer_get_description(timer), (double)lc_timer_elapsed_usec(timer) / 1000.0); \
+ } else { \
+ be_stat_ev_l(lc_timer_get_name(timer), lc_timer_elapsed_msec(timer)); \
+ }
+#define LC_EMIT_RA(timer) \
+ if(!be_stat_ev_is_active()) { \
+ printf("\t%-20s: %.3lf msec\n", lc_timer_get_description(timer), (double)lc_timer_elapsed_usec(timer) / 1000.0); \
+ } else { \
+ be_stat_ev_l(lc_timer_get_name(timer), lc_timer_elapsed_msec(timer)); \
+ }
+ BE_TIMER_ONLY(
+ if(!be_stat_ev_is_active()) {
+ printf("==>> IRG %s <<==\n", get_entity_name(get_irg_entity(irg)));
+ printf("# nodes at begin: %u\n", num_nodes_b);
+ printf("# nodes before ra: %u\n", num_nodes_r);
+ printf("# nodes at end: %u\n\n", num_nodes_a);
+ }
+ LC_EMIT(t_abi);
+ LC_EMIT(t_codegen);
+ LC_EMIT(t_sched);
+ LC_EMIT(t_constr);
+ LC_EMIT(t_regalloc);
+ LC_EMIT_RA(ra_timer->t_prolog);
+ LC_EMIT_RA(ra_timer->t_live);
+ LC_EMIT_RA(ra_timer->t_spill);
+ LC_EMIT_RA(ra_timer->t_spillslots);
+ LC_EMIT_RA(ra_timer->t_color);
+ LC_EMIT_RA(ra_timer->t_ifg);
+ LC_EMIT_RA(ra_timer->t_copymin);
+ LC_EMIT_RA(ra_timer->t_ssa);
+ LC_EMIT_RA(ra_timer->t_epilog);
+ LC_EMIT_RA(ra_timer->t_verify);
+ LC_EMIT_RA(ra_timer->t_other);
+ LC_EMIT(t_finish);
+ LC_EMIT(t_emit);
+ LC_EMIT(t_verify);
+ LC_EMIT(t_other);
+ );
+#undef LC_EMIT_RA
+#undef LC_EMIT
+
+ free_execfreq(birg->execfreqs);
+
+ /* switched off due to statistics (statistic module needs all irgs) */
+ if (! stat_is_active())
+ free_ir_graph(irg);
+
+ if(be_stat_ev_is_active()) {
+ be_stat_ev_pop();
+ }
+ }
+ be_profile_free();
+ be_done_env(&env);
+
+#undef BE_TIMER_POP
+#undef BE_TIMER_PUSH
+#undef BE_TIMER_ONLY
+}
+
+/* Main interface to the frontend. */
+void be_main(FILE *file_handle, const char *cup_name)
+{
+#ifdef WITH_LIBCORE
+ lc_timer_t *t = NULL;
+
+ /* The user specified another config file to read. do that now. */
+ if(strlen(config_file) > 0) {
+ FILE *f;
+
+ if((f = fopen(config_file, "rt")) != NULL) {
+ lc_opt_from_file(config_file, f, NULL);
+ fclose(f);
+ }
+ }
+
+ if (be_options.timing == BE_TIME_ON) {
+ t = lc_timer_register("bemain", "measure complete bemain loop");
+
+ if (lc_timer_enter_high_priority()) {
+ fprintf(stderr, "Warning: Could not enter high priority mode.\n");
+ }
+
+ lc_timer_reset_and_start(t);
+ }
+
+ be_init_stat_file(be_options.stat_file_name, cup_name);
+#endif /* WITH_LIBCORE */
+
+ /* never build code for pseudo irgs */
+ set_visit_pseudo_irgs(0);
+
+ be_node_init();
+
+ be_main_loop(file_handle, cup_name);
+
+#ifdef WITH_LIBCORE
+ if (be_options.timing == BE_TIME_ON) {
+ lc_timer_stop(t);
+ lc_timer_leave_high_priority();
+ if(be_stat_ev_is_active()) {
+ be_stat_ev_l("backend_time", lc_timer_elapsed_msec(t));
+ } else {
+ printf("%-20s: %lu msec\n", "BEMAINLOOP", lc_timer_elapsed_msec(t));