2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Main Backend driver.
9 * @author Sebastian Hack
18 #include "lc_opts_enum.h"
28 #include "iredges_t.h"
33 #include "iroptimize.h"
35 #include "execfreq_t.h"
36 #include "irprofile.h"
48 #include "belistsched.h"
51 #include "bechordal_t.h"
53 #include "becopyopt.h"
54 #include "becopystat.h"
55 #include "bessadestr.h"
62 #include "beemitter.h"
64 #define NEW_ID(s) new_id_from_chars(s, sizeof(s) - 1)
66 /* options visible for anyone */
67 be_options_t be_options = {
68 DUMP_NONE, /* dump flags */
69 BE_TIME_OFF, /* no timing */
70 false, /* profile_generate */
71 false, /* profile_use */
72 0, /* try to omit frame pointer */
73 0, /* create PIC code */
74 BE_VERIFY_WARN, /* verification level: warn */
77 0, /* enable statistic event dumping */
78 "", /* print stat events */
79 1, /* verbose assembler output */
82 /* back end instruction set architecture to use */
83 static const arch_isa_if_t *isa_if = NULL;
85 /* possible dumping options */
86 static const lc_opt_enum_mask_items_t dump_items[] = {
87 { "none", DUMP_NONE },
88 { "initial", DUMP_INITIAL },
90 { "sched", DUMP_SCHED },
91 { "prepared", DUMP_PREPARED },
92 { "regalloc", DUMP_RA },
93 { "final", DUMP_FINAL },
95 { "all", 2 * DUMP_BE - 1 },
100 static const lc_opt_enum_int_items_t verify_items[] = {
101 { "off", BE_VERIFY_OFF },
102 { "warn", BE_VERIFY_WARN },
103 { "assert", BE_VERIFY_ASSERT },
107 static lc_opt_enum_mask_var_t dump_var = {
108 &be_options.dump_flags, dump_items
111 static lc_opt_enum_int_var_t verify_var = {
112 &be_options.verify_option, verify_items
115 static const lc_opt_table_entry_t be_main_options[] = {
116 LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
117 LC_OPT_ENT_BOOL ("omitfp", "omit frame pointer", &be_options.omit_fp),
118 LC_OPT_ENT_BOOL ("pic", "create PIC code", &be_options.pic),
119 LC_OPT_ENT_ENUM_INT ("verify", "verify the backend irg", &verify_var),
120 LC_OPT_ENT_BOOL ("time", "get backend timing statistics", &be_options.timing),
121 LC_OPT_ENT_BOOL ("profilegenerate", "instrument the code for execution count profiling", &be_options.opt_profile_generate),
122 LC_OPT_ENT_BOOL ("profileuse", "use existing profile data", &be_options.opt_profile_use),
123 LC_OPT_ENT_BOOL ("statev", "dump statistic events", &be_options.statev),
124 LC_OPT_ENT_STR ("filtev", "filter for stat events (regex if support is active", &be_options.filtev),
125 LC_OPT_ENT_BOOL ("verboseasm", "enable verbose assembler output", &be_options.verbose_asm),
127 LC_OPT_ENT_STR("ilp.server", "the ilp server name", &be_options.ilp_server),
128 LC_OPT_ENT_STR("ilp.solver", "the ilp solver name", &be_options.ilp_solver),
132 static be_module_list_entry_t *isa_ifs = NULL;
133 static bool isa_initialized = false;
135 asm_constraint_flags_t asm_constraint_flags[256];
137 static void initialize_isa(void)
142 isa_initialized = true;
145 static void finish_isa(void)
147 if (isa_initialized) {
149 isa_initialized = false;
153 void be_init_default_asm_constraint_flags(void)
155 asm_constraint_flags['?'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
156 asm_constraint_flags['!'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
157 asm_constraint_flags['&'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT
158 | ASM_CONSTRAINT_FLAG_MODIFIER_EARLYCLOBBER;
159 asm_constraint_flags['%'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT
160 | ASM_CONSTRAINT_FLAG_MODIFIER_COMMUTATIVE;
161 asm_constraint_flags['!'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
163 asm_constraint_flags['='] = ASM_CONSTRAINT_FLAG_MODIFIER_WRITE
164 | ASM_CONSTRAINT_FLAG_MODIFIER_NO_READ;
165 asm_constraint_flags['+'] = ASM_CONSTRAINT_FLAG_MODIFIER_READ
166 | ASM_CONSTRAINT_FLAG_MODIFIER_WRITE;
168 asm_constraint_flags['i'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
169 asm_constraint_flags['s'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
170 asm_constraint_flags['E'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
171 asm_constraint_flags['F'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
172 asm_constraint_flags['G'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
173 asm_constraint_flags['H'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
174 asm_constraint_flags['I'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
175 asm_constraint_flags['J'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
176 asm_constraint_flags['K'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
177 asm_constraint_flags['L'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
178 asm_constraint_flags['M'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
179 asm_constraint_flags['N'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
180 asm_constraint_flags['O'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
181 asm_constraint_flags['P'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
183 asm_constraint_flags['m'] = ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP;
184 asm_constraint_flags['o'] = ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP;
185 asm_constraint_flags['V'] = ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP;
186 asm_constraint_flags['<'] = ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP;
187 asm_constraint_flags['>'] = ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP;
189 asm_constraint_flags['p'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
190 asm_constraint_flags['0'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
191 asm_constraint_flags['1'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
192 asm_constraint_flags['2'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
193 asm_constraint_flags['3'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
194 asm_constraint_flags['4'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
195 asm_constraint_flags['5'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
196 asm_constraint_flags['6'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
197 asm_constraint_flags['7'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
198 asm_constraint_flags['8'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
199 asm_constraint_flags['9'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
201 asm_constraint_flags['X'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER
202 | ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP
203 | ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
205 /* these should have been catched by the parsing code already */
206 asm_constraint_flags['#'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
207 asm_constraint_flags['*'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
208 asm_constraint_flags[' '] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
209 asm_constraint_flags['\t'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
210 asm_constraint_flags['\n'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
211 asm_constraint_flags['\r'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
214 asm_constraint_flags_t be_parse_asm_constraints(const char *constraint)
216 asm_constraint_flags_t flags = ASM_CONSTRAINT_FLAG_NONE;
218 asm_constraint_flags_t tflags;
222 for (c = constraint; *c != '\0'; ++c) {
225 /* 'comment' stuff */
226 while (*c != 0 && *c != ',')
230 /* 'comment' character */
239 tflags = asm_constraint_flags[(int) *c];
243 flags |= isa_if->parse_asm_constraint(&c);
250 flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE &&
251 flags & ASM_CONSTRAINT_FLAG_MODIFIER_NO_WRITE
253 flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ &&
254 flags & ASM_CONSTRAINT_FLAG_MODIFIER_NO_READ
256 flags |= ASM_CONSTRAINT_FLAG_INVALID;
258 if (!(flags & (ASM_CONSTRAINT_FLAG_MODIFIER_READ |
259 ASM_CONSTRAINT_FLAG_MODIFIER_WRITE |
260 ASM_CONSTRAINT_FLAG_MODIFIER_NO_WRITE |
261 ASM_CONSTRAINT_FLAG_MODIFIER_NO_READ)
263 flags |= ASM_CONSTRAINT_FLAG_MODIFIER_READ;
269 int be_is_valid_clobber(const char *clobber)
273 /* memory is a valid clobber. (the frontend has to detect this case too,
274 * because it has to add memory edges to the asm) */
275 if (strcmp(clobber, "memory") == 0)
277 /* cc (condition code) is always valid */
278 if (strcmp(clobber, "cc") == 0)
281 return isa_if->is_valid_clobber(clobber);
284 void be_register_isa_if(const char *name, const arch_isa_if_t *isa)
289 be_add_module_to_list(&isa_ifs, name, (void*) isa);
292 static void be_opt_register(void)
294 lc_opt_entry_t *be_grp;
295 static int run_once = 0;
301 be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
302 lc_opt_add_table(be_grp, be_main_options);
304 be_add_module_list_opt(be_grp, "isa", "the instruction set architecture",
305 &isa_ifs, (void**) &isa_if);
310 /* Parse one argument. */
311 int be_parse_arg(const char *arg)
313 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
314 if (strcmp(arg, "help") == 0 || (arg[0] == '?' && arg[1] == '\0')) {
315 lc_opt_print_help_for_entry(be_grp, '-', stdout);
318 return lc_opt_from_single_arg(be_grp, NULL, arg, NULL);
321 /* Perform schedule verification if requested. */
322 static void be_sched_verify(ir_graph *irg, int verify_opt)
324 if (verify_opt == BE_VERIFY_WARN) {
325 be_verify_schedule(irg);
326 } else if (verify_opt == BE_VERIFY_ASSERT) {
327 assert(be_verify_schedule(irg) && "Schedule verification failed.");
331 /* Initialize the Firm backend. Must be run first in init_firm()! */
332 void firm_be_init(void)
338 /* Finalize the Firm backend. */
339 void firm_be_finish(void)
345 /* Returns the backend parameter */
346 const backend_params *be_get_backend_param(void)
349 return isa_if->get_params();
352 int be_is_big_endian(void)
354 return be_get_backend_param()->byte_order_big_endian;
357 unsigned be_get_machine_size(void)
359 return be_get_backend_param()->machine_size;
362 ir_mode *be_get_mode_float_arithmetic(void)
364 return be_get_backend_param()->mode_float_arithmetic;
367 ir_type *be_get_type_long_long(void)
369 return be_get_backend_param()->type_long_long;
372 ir_type *be_get_type_unsigned_long_long(void)
374 return be_get_backend_param()->type_unsigned_long_long;
377 ir_type *be_get_type_long_double(void)
379 return be_get_backend_param()->type_long_double;
383 * Initializes the main environment for the backend.
385 * @param env an empty environment
386 * @param file_handle the file handle where the output will be written to
388 static be_main_env_t *be_init_env(be_main_env_t *const env, char const *const compilation_unit_name)
390 memset(env, 0, sizeof(*env));
391 env->ent_trampoline_map = pmap_create();
392 env->pic_trampolines_type = new_type_segment(NEW_ID("$PIC_TRAMPOLINE_TYPE"), tf_none);
393 env->ent_pic_symbol_map = pmap_create();
394 env->pic_symbols_type = new_type_segment(NEW_ID("$PIC_SYMBOLS_TYPE"), tf_none);
395 env->cup_name = compilation_unit_name;
396 env->arch_env = isa_if->begin_codegeneration();
398 set_class_final(env->pic_trampolines_type, 1);
400 memset(asm_constraint_flags, 0, sizeof(asm_constraint_flags));
406 * Called when the be_main_env_t can be destroyed.
408 static void be_done_env(be_main_env_t *env)
410 pmap_destroy(env->ent_trampoline_map);
411 pmap_destroy(env->ent_pic_symbol_map);
412 free_type(env->pic_trampolines_type);
413 free_type(env->pic_symbols_type);
417 * A wrapper around a firm dumper. Dumps only, if
420 * @param mask a bitmask containing the reason what will be dumped
421 * @param irg the IR graph to dump
422 * @param suffix the suffix for the dumper
423 * @param dumper the dumper to be called
425 static void dump(int mask, ir_graph *irg, const char *suffix)
427 if (be_options.dump_flags & mask)
428 dump_ir_graph(irg, suffix);
432 * Prepare a backend graph for code generation and initialize its irg
434 static void initialize_birg(be_irg_t *birg, ir_graph *irg, be_main_env_t *env)
436 /* don't duplicate locals in backend when dumping... */
437 ir_remove_dump_flags(ir_dump_flag_consts_local);
439 dump(DUMP_INITIAL, irg, "begin");
443 memset(birg, 0, sizeof(*birg));
444 birg->main_env = env;
445 obstack_init(&birg->obst);
446 birg->lv = be_liveness_new(irg);
448 edges_deactivate(irg);
451 /* set the current graph (this is important for several firm functions) */
452 current_ir_graph = irg;
454 /* we do this before critical edge split. As this produces less returns,
455 because sometimes (= 164.gzip) multiple returns are slower */
456 normalize_n_returns(irg);
458 /* Remove critical edges */
459 remove_critical_cf_edges_ex(irg, /*ignore_exception_edges=*/0);
461 /* For code generation all unreachable code and Bad nodes should be gone */
462 remove_unreachable_code(irg);
465 /* Ensure, that the ir_edges are computed. */
468 be_info_init_irg(irg);
470 dump(DUMP_INITIAL, irg, "prepared");
475 static const char *get_timer_name(be_timer_id_t id)
478 case T_ABI: return "abi";
479 case T_CODEGEN: return "codegen";
480 case T_RA_PREPARATION: return "ra_preparation";
481 case T_SCHED: return "sched";
482 case T_CONSTR: return "constr";
483 case T_FINISH: return "finish";
484 case T_EMIT: return "emit";
485 case T_VERIFY: return "verify";
486 case T_OTHER: return "other";
487 case T_HEIGHTS: return "heights";
488 case T_LIVE: return "live";
489 case T_EXECFREQ: return "execfreq";
490 case T_SSA_CONSTR: return "ssa_constr";
491 case T_RA_EPILOG: return "ra_epilog";
492 case T_RA_CONSTR: return "ra_constr";
493 case T_RA_SPILL: return "ra_spill";
494 case T_RA_SPILL_APPLY: return "ra_spill_apply";
495 case T_RA_COLOR: return "ra_color";
496 case T_RA_IFG: return "ra_ifg";
497 case T_RA_COPYMIN: return "ra_copymin";
498 case T_RA_SSA: return "ra_ssa";
499 case T_RA_OTHER: return "ra_other";
503 ir_timer_t *be_timers[T_LAST+1];
505 void be_lower_for_target(void)
511 isa_if->lower_for_target();
512 /* set the phase to low */
513 for (i = get_irp_n_irgs(); i > 0;) {
514 ir_graph *irg = get_irp_irg(--i);
515 assert(!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_TARGET_LOWERED));
516 add_irg_constraints(irg, IR_GRAPH_CONSTRAINT_TARGET_LOWERED);
521 * The Firm backend main loop.
522 * Do architecture specific lowering for all graphs
523 * and call the architecture specific code generator.
525 * @param file_handle the file handle the output will be written to
526 * @param cup_name name of the compilation unit
528 static void be_main_loop(FILE *file_handle, const char *cup_name)
530 static const char suffix[] = ".prof";
536 char prof_filename[256];
538 arch_env_t *arch_env;
540 be_timing = (be_options.timing == BE_TIME_ON);
542 /* perform target lowering if it didn't happen yet */
543 if (get_irp_n_irgs() > 0 && !irg_is_constrained(get_irp_irg(0), IR_GRAPH_CONSTRAINT_TARGET_LOWERED))
544 be_lower_for_target();
547 for (i = 0; i < T_LAST+1; ++i) {
548 be_timers[i] = ir_timer_new();
552 be_init_env(&env, cup_name);
554 be_emit_init(file_handle);
555 be_gas_begin_compilation_unit(&env);
557 arch_env = env.arch_env;
559 /* we might need 1 birg more for instrumentation constructor */
560 num_irgs = get_irp_n_irgs();
561 birgs = ALLOCAN(be_irg_t, num_irgs + 1);
565 /* First: initialize all birgs */
567 for (i = 0; i < num_irgs; ++i) {
568 ir_graph *irg = get_irp_irg(i);
569 ir_entity *entity = get_irg_entity(irg);
570 if (get_entity_linkage(entity) & IR_LINKAGE_NO_CODEGEN)
572 initialize_birg(&birgs[num_birgs++], irg, &env);
574 arch_env_handle_intrinsics(arch_env);
577 Get the filename for the profiling data.
578 Beware: '\0' is already included in sizeof(suffix)
580 sprintf(prof_filename, "%.*s%s",
581 (int)(sizeof(prof_filename) - sizeof(suffix)), cup_name, suffix);
583 bool have_profile = false;
584 if (be_options.opt_profile_use) {
585 bool res = ir_profile_read(prof_filename);
587 fprintf(stderr, "Warning: Couldn't read profile data '%s'\n",
590 ir_create_execfreqs_from_profile();
596 if (num_birgs > 0 && be_options.opt_profile_generate) {
597 ir_graph *const prof_init_irg = ir_profile_instrument(prof_filename);
598 assert(prof_init_irg->be_data == NULL);
599 initialize_birg(&birgs[num_birgs], prof_init_irg, &env);
602 assert(num_irgs == get_irp_n_irgs());
605 for (be_timer_id_t t = T_FIRST; t < T_LAST+1; ++t) {
606 ir_timer_init_parent(be_timers[t]);
609 be_timer_push(T_EXECFREQ);
610 for (i = 0; i < num_irgs; ++i) {
611 ir_graph *irg = get_irp_irg(i);
612 ir_estimate_execfreq(irg);
614 be_timer_pop(T_EXECFREQ);
618 for (i = 0; i < num_irgs; ++i) {
619 ir_graph *const irg = get_irp_irg(i);
620 ir_entity *const entity = get_irg_entity(irg);
621 if (get_entity_linkage(entity) & IR_LINKAGE_NO_CODEGEN)
624 /* set the current graph (this is important for several firm functions) */
625 current_ir_graph = irg;
627 if (stat_ev_enabled) {
628 stat_ev_ctx_push_fmt("bemain_irg", "%+F", irg);
629 stat_ev_ull("bemain_insns_start", be_count_insns(irg));
630 stat_ev_ull("bemain_blocks_start", be_count_blocks(irg));
633 /* stop and reset timers */
634 be_timer_push(T_OTHER);
636 /* Verify the initial graph */
637 be_timer_push(T_VERIFY);
638 if (be_options.verify_option == BE_VERIFY_WARN) {
639 irg_verify(irg, VERIFY_ENFORCE_SSA);
640 } else if (be_options.verify_option == BE_VERIFY_ASSERT) {
641 assert(irg_verify(irg, VERIFY_ENFORCE_SSA) && "irg verification failed");
643 be_timer_pop(T_VERIFY);
645 /* get a code generator for this graph. */
646 if (arch_env->impl->init_graph)
647 arch_env->impl->init_graph(irg);
649 /* some transformations need to be done before abi introduce */
650 if (arch_env->impl->before_abi != NULL)
651 arch_env->impl->before_abi(irg);
653 /* implement the ABI conventions. */
654 if (!arch_env->custom_abi) {
655 be_timer_push(T_ABI);
656 be_abi_introduce(irg);
658 dump(DUMP_ABI, irg, "abi");
661 /* We can't have Bad-blocks or critical edges in the backend.
662 * Before removing Bads, we remove unreachable code. */
663 optimize_graph_df(irg);
664 remove_critical_cf_edges(irg);
667 /* We often have dead code reachable through out-edges here. So for
668 * now we rebuild edges (as we need correct user count for code
670 edges_deactivate(irg);
673 dump(DUMP_PREPARED, irg, "before-code-selection");
675 /* perform codeselection */
676 be_timer_push(T_CODEGEN);
677 if (arch_env->impl->prepare_graph != NULL)
678 arch_env->impl->prepare_graph(irg);
679 be_timer_pop(T_CODEGEN);
681 dump(DUMP_PREPARED, irg, "code-selection");
683 /* disabled for now, fails for EmptyFor.c and XXEndless.c */
684 /* be_live_chk_compare(irg); */
686 /* schedule the irg */
687 be_timer_push(T_SCHED);
688 be_schedule_graph(irg);
689 be_timer_pop(T_SCHED);
691 dump(DUMP_SCHED, irg, "sched");
694 be_timer_push(T_VERIFY);
695 be_sched_verify(irg, be_options.verify_option);
696 be_timer_pop(T_VERIFY);
698 /* introduce patterns to assure constraints */
699 be_timer_push(T_CONSTR);
700 /* we switch off optimizations here, because they might cause trouble */
701 optimization_state_t state;
702 save_optimization_state(&state);
706 /* add Keeps for should_be_different constrained nodes */
707 /* beware: needs schedule due to usage of be_ssa_constr */
708 assure_constraints(irg);
709 be_timer_pop(T_CONSTR);
711 dump(DUMP_SCHED, irg, "assured");
713 /* stuff needs to be done after scheduling but before register allocation */
714 be_timer_push(T_RA_PREPARATION);
715 if (arch_env->impl->before_ra != NULL)
716 arch_env->impl->before_ra(irg);
717 be_timer_pop(T_RA_PREPARATION);
719 /* connect all stack modifying nodes together (see beabi.c) */
720 be_timer_push(T_ABI);
721 be_abi_fix_stack_nodes(irg);
724 dump(DUMP_SCHED, irg, "fix_stack");
727 be_timer_push(T_VERIFY);
728 be_sched_verify(irg, be_options.verify_option);
729 be_timer_pop(T_VERIFY);
731 if (stat_ev_enabled) {
732 stat_ev_dbl("bemain_costs_before_ra", be_estimate_irg_costs(irg));
733 stat_ev_ull("bemain_insns_before_ra", be_count_insns(irg));
734 stat_ev_ull("bemain_blocks_before_ra", be_count_blocks(irg));
737 /* Do register allocation */
738 be_allocate_registers(irg);
740 stat_ev_dbl("bemain_costs_before_ra", be_estimate_irg_costs(irg));
742 dump(DUMP_RA, irg, "ra");
744 be_timer_push(T_FINISH);
745 if (arch_env->impl->finish_graph != NULL)
746 arch_env->impl->finish_graph(irg);
747 be_timer_pop(T_FINISH);
749 dump(DUMP_FINAL, irg, "finish");
751 if (stat_ev_enabled) {
752 stat_ev_ull("bemain_insns_finish", be_count_insns(irg));
753 stat_ev_ull("bemain_blocks_finish", be_count_blocks(irg));
756 /* check schedule and register allocation */
757 be_timer_push(T_VERIFY);
758 if (be_options.verify_option == BE_VERIFY_WARN) {
759 irg_verify(irg, VERIFY_ENFORCE_SSA);
760 be_verify_schedule(irg);
761 be_verify_register_allocation(irg);
762 } else if (be_options.verify_option == BE_VERIFY_ASSERT) {
763 assert(irg_verify(irg, VERIFY_ENFORCE_SSA) && "irg verification failed");
764 assert(be_verify_schedule(irg) && "Schedule verification failed");
765 assert(be_verify_register_allocation(irg)
766 && "register allocation verification failed");
769 be_timer_pop(T_VERIFY);
771 /* emit assembler code */
772 be_timer_push(T_EMIT);
773 if (arch_env->impl->emit != NULL)
774 arch_env->impl->emit(irg);
775 be_timer_pop(T_EMIT);
777 dump(DUMP_FINAL, irg, "end");
779 restore_optimization_state(&state);
781 be_timer_pop(T_OTHER);
785 if (stat_ev_enabled) {
786 for (t = T_FIRST; t < T_LAST+1; ++t) {
788 snprintf(buf, sizeof(buf), "bemain_time_%s",
790 stat_ev_dbl(buf, ir_timer_elapsed_usec(be_timers[i]));
793 printf("==>> IRG %s <<==\n",
794 get_entity_name(get_irg_entity(irg)));
795 for (t = T_FIRST; t < T_LAST+1; ++t) {
796 double val = ir_timer_elapsed_usec(be_timers[t]) / 1000.0;
797 printf("%-20s: %10.3f msec\n", get_timer_name(t), val);
800 for (t = T_FIRST; t < T_LAST+1; ++t) {
801 ir_timer_reset(be_timers[t]);
806 stat_ev_ctx_pop("bemain_irg");
809 be_gas_end_compilation_unit(&env);
812 arch_env_end_codegeneration(arch_env);
819 /* Main interface to the frontend. */
820 void be_main(FILE *file_handle, const char *cup_name)
822 ir_timer_t *t = NULL;
824 if (be_options.timing == BE_TIME_ON) {
827 if (ir_timer_enter_high_priority()) {
828 fprintf(stderr, "Warning: Could not enter high priority mode.\n");
831 ir_timer_reset_and_start(t);
834 if (be_options.statev) {
835 const char *dot = strrchr(cup_name, '.');
836 const char *pos = dot ? dot : cup_name + strlen(cup_name);
837 char *buf = ALLOCAN(char, pos - cup_name + 1);
838 strncpy(buf, cup_name, pos - cup_name);
839 buf[pos - cup_name] = '\0';
841 be_options.statev = 1;
842 stat_ev_begin(buf, be_options.filtev);
843 stat_ev_ctx_push_str("bemain_compilation_unit", cup_name);
846 be_main_loop(file_handle, cup_name);
848 if (be_options.timing == BE_TIME_ON) {
850 ir_timer_leave_high_priority();
851 if (stat_ev_enabled) {
852 stat_ev_dbl("bemain_backend_time", ir_timer_elapsed_msec(t));
854 double val = ir_timer_elapsed_usec(t) / 1000.0;
855 printf("%-20s: %10.3f msec\n", "BEMAINLOOP", val);
859 if (be_options.statev) {
860 stat_ev_ctx_pop("bemain_compilation_unit");
865 static int do_lower_for_target(ir_prog *irp, void *context)
867 be_lower_for_target();
873 ir_prog_pass_t *lower_for_target_pass(const char *name)
875 ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
876 return def_prog_pass_constructor(pass,
877 name ? name : "lower_for_target",
878 do_lower_for_target);