3 * @author Sebastian Hack
14 #include <libcore/lc_opts.h>
15 #include <libcore/lc_opts_enum.h>
16 #include <libcore/lc_timing.h>
17 #endif /* WITH_LIBCORE */
28 #include "iredges_t.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"
48 #include "besched_t.h"
49 #include "belistsched.h"
51 #include "bespillbelady.h"
53 #include "beraextern.h"
54 #include "bechordal_t.h"
56 #include "beifg_impl.h"
57 #include "becopyopt.h"
58 #include "becopystat.h"
59 #include "bessadestr.h"
62 #include "beschedmris.h"
65 #include "beprofile.h"
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 */
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 */
81 static char config_file[256] = { 0 };
83 /* register allocator to use. */
84 static const be_ra_t *ra = &be_ra_chordal_allocator;
86 /* back end instruction set architecture to use */
87 static const arch_isa_if_t *isa_if = &ia32_isa_if;
91 static lc_opt_entry_t *be_grp_root = NULL;
93 /* possible dumping options */
94 static const lc_opt_enum_mask_items_t dump_items[] = {
95 { "none", DUMP_NONE },
96 { "initial", DUMP_INITIAL },
98 { "sched", DUMP_SCHED },
99 { "prepared", DUMP_PREPARED },
100 { "regalloc", DUMP_RA },
101 { "final", DUMP_FINAL },
103 { "all", 2 * DUMP_BE - 1 },
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 },
114 /* instruction set architectures. */
115 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
116 { "ia32", &ia32_isa_if },
118 { "arm", &arm_isa_if },
119 { "ppc32", &ppc32_isa_if },
120 { "mips", &mips_isa_if },
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 },
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 },
143 static lc_opt_enum_mask_var_t dump_var = {
144 &be_options.dump_flags, dump_items
147 static lc_opt_enum_const_ptr_var_t ra_var = {
148 (const void **) &ra, ra_items
151 static lc_opt_enum_const_ptr_var_t isa_var = {
152 (const void **) &isa_if, isa_items
155 static lc_opt_enum_int_var_t vrfy_var = {
156 &be_options.vrfy_option, vrfy_items
159 static lc_opt_enum_int_var_t sched_select_var = {
160 &be_options.sched_select, sched_select_items
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),
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 */
182 #endif /* WITH_LIBCORE */
184 void be_opt_register(void)
188 lc_opt_entry_t *be_grp_ra;
189 static int run_once = 0;
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");
196 lc_opt_add_table(be_grp_root, be_main_options);
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);
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);
210 #endif /* WITH_LIBCORE */
213 /* Parse one argument. */
214 int be_parse_arg(const char *arg) {
216 if (strcmp(arg, "help") == 0 || (arg[0] == '?' && arg[1] == '\0')) {
217 lc_opt_print_help(be_grp_root, stdout);
220 return lc_opt_from_single_arg(be_grp_root, NULL, arg, NULL);
223 #endif /* WITH_LIBCORE */
226 /** The be parameters returned by default, all off. */
227 const static backend_params be_params = {
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);
239 else if (vrfy_opt == BE_VRFY_ASSERT) {
240 assert(be_verify_schedule(irg) && "Schedule verification failed.");
244 /* Initialize the Firm backend. Must be run BEFORE init_firm()! */
245 const backend_params *be_init(void)
255 if (isa_if->get_params)
256 return isa_if->get_params();
261 * Initializes the main environment for the backend.
263 * @param env an empty environment
264 * @param file_handle the file handle where the output will be written to
266 static be_main_env_t *be_init_env(be_main_env_t *env, FILE *file_handle)
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");
274 arch_env_init(env->arch_env, isa_if, file_handle, env);
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));
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.
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);
292 static void be_done_env(be_main_env_t *env)
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);
300 * A wrapper around a firm dumper. Dumps only, if
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
308 static void dump(int mask, ir_graph *irg, const char *suffix,
309 void (*dumper)(ir_graph *, const char *))
311 if(be_options.dump_flags & mask)
312 be_dump(irg, suffix, dumper);
316 * Prepare a backend graph for code generation.
318 static void prepare_graph(be_irg_t *birg)
320 ir_graph *irg = birg->irg;
322 /* Normalize proj nodes. */
323 normalize_proj_nodes(irg);
325 /* Make just one return node. */
326 normalize_one_return(irg);
330 /* Remove critical edges */
331 remove_critical_cf_edges(irg);
333 /* Compute the dominance information. */
337 /* Ensure, that the ir_edges are computed. */
340 /* check, if the dominance property is fulfilled. */
341 be_check_dominance(irg);
343 /* reset the phi handler. */
344 be_phi_handler_reset(birg->main_env->phi_handler);
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)); \
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)); \
369 #define BE_TIMER_ONLY(code) do { if (be_options.timing == BE_TIME_ON) { code; } } while(0)
373 #define BE_TIMER_PUSH(timer)
374 #define BE_TIMER_POP(timer)
375 #define BE_TIMER_ONLY(code)
377 #endif /* WITH_LIBCORE */
381 * The Firm backend main loop.
382 * Do architecture specific lowering for all graphs
383 * and call the architecture specific code generator.
385 * @param file_handle the file handle the output will be written to
387 static void be_main_loop(FILE *file_handle)
392 unsigned num_nodes_b = 0;
393 unsigned num_nodes_a = 0;
394 unsigned num_nodes_r = 0;
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;
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");
421 #endif /* WITH_LIBCORE */
423 be_init_env(&env, file_handle);
425 isa = arch_env_get_isa(env.arch_env);
427 /* for debugging, anchors helps */
428 // dump_all_anchors(1);
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));
434 be_profile_read("test.c.prof"); //FIXME
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;
441 optimization_state_t state;
443 /* stop and reset timers */
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);
456 BE_TIMER_PUSH(t_other); /* t_other */
458 BE_TIMER_ONLY(num_nodes_b = get_num_reachable_nodes(irg));
461 birg.main_env = &env;
463 edges_deactivate_kind(irg, EDGE_KIND_DEP);
464 edges_activate_kind(irg, EDGE_KIND_DEP);
466 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
467 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
469 BE_TIMER_PUSH(t_prolog);
471 be_stat_init_irg(env.arch_env, irg);
472 be_do_stat_nodes(irg, "01 Begin");
474 /* set the current graph (this is important for several firm functions) */
475 current_ir_graph = birg.irg;
477 /* Get the code generator interface. */
478 cg_if = isa->impl->get_code_generator_if(isa);
480 /* get a code generator for this graph. */
481 birg.cg = cg_if->init(&birg);
483 /* create the code generator and generate code. */
484 prepare_graph(&birg);
486 BE_TIMER_POP(t_prolog);
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);
493 /* implement the ABI conventions. */
494 BE_TIMER_PUSH(t_abi);
495 birg.abi = be_abi_introduce(&birg);
498 dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
499 be_do_stat_nodes(irg, "02 Abi");
502 BE_TIMER_PUSH(t_codegen);
503 arch_code_generator_prepare_graph(birg.cg);
504 BE_TIMER_POP(t_codegen);
506 be_do_stat_nodes(irg, "03 Prepare");
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.
513 edges_deactivate(irg);
514 //dead_node_elimination(irg);
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));
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);
527 /* schedule the irg */
528 BE_TIMER_PUSH(t_sched);
529 list_sched(&birg, &be_options);
530 BE_TIMER_POP(t_sched);
532 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
535 BE_TIMER_PUSH(t_verify);
536 be_sched_vrfy(birg.irg, be_options.vrfy_option);
537 BE_TIMER_POP(t_verify);
539 be_do_stat_nodes(irg, "04 Schedule");
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);
546 set_opt_normalize(0);
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);
553 dump(DUMP_SCHED, irg, "-assured", dump_ir_block_graph_sched);
554 be_do_stat_nodes(irg, "05 Constraints");
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);
561 dump(DUMP_SCHED, irg, "-fix_stack", dump_ir_block_graph_sched);
564 BE_TIMER_PUSH(t_verify);
565 be_sched_vrfy(birg.irg, be_options.vrfy_option);
566 BE_TIMER_POP(t_verify);
568 /* do some statistics */
569 be_do_stat_reg_pressure(&birg);
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);
576 /* Do register allocation */
577 BE_TIMER_PUSH(t_regalloc);
578 ra_timer = ra->allocate(&birg);
579 BE_TIMER_POP(t_regalloc);
581 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
582 be_do_stat_nodes(irg, "06 Register Allocation");
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);
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);
596 BE_TIMER_PUSH(t_finish);
597 arch_code_generator_finish(birg.cg);
598 BE_TIMER_POP(t_finish);
600 dump(DUMP_FINAL, irg, "-finish", dump_ir_block_graph_sched);
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);
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");
619 BE_TIMER_POP(t_verify);
621 /* emit assembler code */
622 BE_TIMER_PUSH(t_emit);
623 arch_code_generator_done(birg.cg);
624 BE_TIMER_POP(t_emit);
626 dump(DUMP_FINAL, irg, "-end", dump_ir_extblock_graph_sched);
628 BE_TIMER_PUSH(t_abi);
629 be_abi_free(birg.abi);
632 be_do_stat_nodes(irg, "07 Final");
633 restore_optimization_state(&state);
635 BE_TIMER_ONLY(num_nodes_a = get_num_reachable_nodes(irg));
636 BE_TIMER_POP(t_other);
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)
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);
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);
670 /* switched off due to statistics (statistic module needs all irgs) */
671 if (! stat_is_active())
683 /* Main interface to the frontend. */
684 void be_main(FILE *file_handle)
687 lc_timer_t *t = NULL;
688 #endif /* WITH_LIBCORE */
691 /* The user specified another config file to read. do that now. */
692 if(strlen(config_file) > 0) {
695 if((f = fopen(config_file, "rt")) != NULL) {
696 lc_opt_from_file(config_file, f, NULL);
701 if (be_options.timing == BE_TIME_ON) {
702 t = lc_timer_register("bemain", "measure complete bemain loop");
704 if (lc_timer_enter_high_priority()) {
705 fprintf(stderr, "Warning: Could not enter high priority mode.\n");
708 lc_timer_reset_and_start(t);
710 #endif /* WITH_LIBCORE */
712 /* never build code for pseudo irgs */
713 set_visit_pseudo_irgs(0);
716 be_main_loop(file_handle);
719 if (be_options.timing == BE_TIME_ON) {
721 lc_timer_leave_high_priority();
722 printf("%-20s: %lu msec\n", "BEMAINLOOP", lc_timer_elapsed_msec(t));
724 #endif /* WITH_LIBCORE */
727 /** The debug info retriever function. */
728 static retrieve_dbg_func retrieve_dbg = NULL;
730 /* Sets a debug info retriever. */
731 void be_set_debug_retrieve(retrieve_dbg_func func) {
735 /* Retrieve the debug info. */
736 const char *be_retrieve_dbg_info(const dbg_info *dbg, unsigned *line) {
738 return retrieve_dbg(dbg, line);
743 int be_put_ignore_regs(const be_irg_t *birg, const arch_register_class_t *cls, bitset_t *bs)
746 bs = bitset_alloca(cls->n_regs);
748 bitset_clear_all(bs);
750 assert(bitset_size(bs) == (unsigned) cls->n_regs);
751 arch_put_non_ignore_regs(birg->main_env->arch_env, cls, bs);
753 be_abi_put_ignore_regs(birg->abi, cls, bs);
754 return bitset_popcnt(bs);