3 * @author Sebastian Hack
14 #include <libcore/lc_opts.h>
15 #include <libcore/lc_opts_enum.h>
16 #endif /* WITH_LIBCORE */
27 #include "iredges_t.h"
33 #include "firm/bearch_firm.h"
34 #include "ia32/bearch_ia32.h"
35 #include "arm/bearch_arm.h"
36 #include "ppc32/bearch_ppc32.h"
37 #include "mips/bearch_mips.h"
44 #include "besched_t.h"
45 #include "belistsched.h"
47 #include "bespillilp.h"
48 #include "bespillbelady.h"
50 #include "beraextern.h"
51 #include "bechordal_t.h"
53 #include "beifg_impl.h"
54 #include "becopyopt.h"
55 #include "becopystat.h"
56 #include "bessadestr.h"
59 #include "beschedmris.h"
63 #define DUMP_INITIAL (1 << 0)
64 #define DUMP_ABI (1 << 1)
65 #define DUMP_SCHED (1 << 2)
66 #define DUMP_PREPARED (1 << 3)
67 #define DUMP_RA (1 << 4)
68 #define DUMP_FINAL (1 << 5)
76 /* options visible for anyone */
77 static be_options_t be_options = {
79 "i44pc52.info.uni-karlsruhe.de",
86 static unsigned dump_flags = 0;
89 static unsigned vrfy_option = BE_VRFY_WARN;
91 /* register allocator to use. */
92 static const be_ra_t *ra = &be_ra_chordal_allocator;
94 /* back end instruction set architecture to use */
95 static const arch_isa_if_t *isa_if = &ia32_isa_if;
98 static int be_enable_mris = 0;
102 static lc_opt_entry_t *be_grp_root = NULL;
104 /* possible dumping options */
105 static const lc_opt_enum_mask_items_t dump_items[] = {
107 { "initial", DUMP_INITIAL },
109 { "sched", DUMP_SCHED },
110 { "prepared", DUMP_PREPARED },
111 { "regalloc", DUMP_RA },
112 { "final", DUMP_FINAL },
113 { "all", 2 * DUMP_FINAL - 1 },
117 /* register allocators */
118 static const lc_opt_enum_const_ptr_items_t ra_items[] = {
119 { "chordal", &be_ra_chordal_allocator },
120 { "external", &be_ra_external_allocator },
124 /* instruction set architectures. */
125 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
126 { "ia32", &ia32_isa_if },
128 { "arm", &arm_isa_if },
129 { "ppc32", &ppc32_isa_if },
130 { "mips", &mips_isa_if },
135 /* verify options. */
136 static const lc_opt_enum_int_items_t vrfy_items[] = {
137 { "off", BE_VRFY_OFF },
138 { "warn", BE_VRFY_WARN },
139 { "assert", BE_VRFY_ASSERT },
142 static lc_opt_enum_mask_var_t dump_var = {
143 &dump_flags, dump_items
146 static lc_opt_enum_const_ptr_var_t ra_var = {
147 (const void **) &ra, ra_items
150 static lc_opt_enum_const_ptr_var_t isa_var = {
151 (const void **) &isa_if, isa_items
154 static lc_opt_enum_int_var_t vrfy_var = {
155 &vrfy_option, vrfy_items
158 static const lc_opt_table_entry_t be_main_options[] = {
159 LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
160 LC_OPT_ENT_ENUM_PTR ("ra", "register allocator", &ra_var),
161 LC_OPT_ENT_ENUM_PTR ("isa", "the instruction set architecture", &isa_var),
162 LC_OPT_ENT_NEGBOOL ("noomitfp", "do not omit frame pointer", &be_omit_fp),
163 LC_OPT_ENT_BOOL ("mris", "enable mris schedule preparation", &be_enable_mris),
164 LC_OPT_ENT_ENUM_PTR ("vrfy", "verify the backend irg (off, warn, assert)", &vrfy_var),
167 LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
168 LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
169 #endif /* WITH_ILP */
173 #endif /* WITH_LIBCORE */
175 void be_opt_register(void)
179 lc_opt_entry_t *be_grp_ra;
180 static int run_once = 0;
184 be_grp_root = lc_opt_get_grp(firm_opt_get_root(), "be");
185 be_grp_ra = lc_opt_get_grp(be_grp_root, "ra");
187 lc_opt_add_table(be_grp_root, be_main_options);
189 /* register allocator options */
190 for(i = 0; ra_items[i].name != NULL; ++i) {
191 const be_ra_t *ra = ra_items[i].value;
192 ra->register_options(be_grp_ra);
195 /* register isa options */
196 for(i = 0; isa_items[i].name != NULL; ++i) {
197 const arch_isa_if_t *isa = isa_items[i].value;
198 isa->register_options(be_grp_root);
201 #endif /* WITH_LIBCORE */
204 /* Parse one argument. */
205 int be_parse_arg(const char *arg) {
207 if (strcmp(arg, "help") == 0 || (arg[0] == '?' && arg[1] == '\0')) {
208 lc_opt_print_help(be_grp_root, stdout);
211 return lc_opt_from_single_arg(be_grp_root, NULL, arg, NULL);
214 #endif /* WITH_LIBCORE */
217 /** The be parameters returned by default, all off. */
218 const static backend_params be_params = {
225 /* Perform schedule verification if requested. */
226 static void be_sched_vrfy(ir_graph *irg, int vrfy_opt) {
227 if (vrfy_opt == BE_VRFY_WARN) {
228 be_verify_schedule(irg);
230 else if (vrfy_opt == BE_VRFY_ASSERT) {
231 assert(be_verify_schedule(irg) && "Schedule verification failed.");
235 /* Initialize the Firm backend. Must be run BEFORE init_firm()! */
236 const backend_params *be_init(void)
247 if (isa_if->get_params)
248 return isa_if->get_params();
253 * Initializes the main environment for the backend.
255 * @param env an empty environment
256 * @param file_handle the file handle where the output will be written to
258 static be_main_env_t *be_init_env(be_main_env_t *env, FILE *file_handle)
260 memset(env, 0, sizeof(*env));
261 obstack_init(&env->obst);
262 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
263 env->options = &be_options;
264 FIRM_DBG_REGISTER(env->dbg, "be.main");
266 arch_env_init(env->arch_env, isa_if, file_handle);
268 /* Register the irn handler of the architecture */
269 if (arch_isa_get_irn_handler(env->arch_env->isa))
270 arch_env_push_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
273 * Register the node handler of the back end infrastructure.
274 * This irn handler takes care of the platform independent
275 * spill, reload and perm nodes.
277 arch_env_push_irn_handler(env->arch_env, &be_node_irn_handler);
278 env->phi_handler = be_phi_handler_new(env->arch_env);
279 arch_env_push_irn_handler(env->arch_env, env->phi_handler);
284 static void be_done_env(be_main_env_t *env)
286 env->arch_env->isa->impl->done(env->arch_env->isa);
287 be_phi_handler_free(env->phi_handler);
288 obstack_free(&env->obst, NULL);
292 * A wrapper around a firm dumper. Dumps only, if
295 * @param mask a bitmask containing the reason what will be dumped
296 * @param irg the IR graph to dump
297 * @param suffix the suffix for the dumper
298 * @param dumper the dumper to be called
300 static void dump(int mask, ir_graph *irg, const char *suffix,
301 void (*dumper)(ir_graph *, const char *))
303 if(dump_flags & mask)
304 be_dump(irg, suffix, dumper);
308 * Prepare a backend graph for code generation.
310 static void prepare_graph(be_irg_t *birg)
312 ir_graph *irg = birg->irg;
314 /* Normalize proj nodes. */
315 normalize_proj_nodes(irg);
317 /* Make just one return node. */
318 normalize_one_return(irg);
320 /* Remove critical edges */
321 remove_critical_cf_edges(irg);
323 /* Compute the dominance information. */
327 /* Ensure, that the ir_edges are computed. */
330 /* check, if the dominance property is fulfilled. */
331 be_check_dominance(irg);
333 /* reset the phi handler. */
334 be_phi_handler_reset(birg->main_env->phi_handler);
338 * The Firm backend main loop.
339 * Do architecture specific lowering for all graphs
340 * and call the architecture specific code generator.
342 * @param file_handle the file handle the output will be written to
344 static void be_main_loop(FILE *file_handle)
350 be_init_env(&env, file_handle);
352 isa = arch_env_get_isa(env.arch_env);
354 /* for debugging, anchors helps */
355 // dump_all_anchors(1);
358 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
359 ir_graph *irg = get_irp_irg(i);
360 const arch_code_generator_if_t *cg_if;
362 optimization_state_t state;
365 birg.main_env = &env;
367 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
368 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
370 be_stat_init_irg(env.arch_env, irg);
371 be_do_stat_nodes(irg, "01 Begin");
373 /* set the current graph (this is important for several firm functions) */
374 current_ir_graph = birg.irg;
376 /* Get the code generator interface. */
377 cg_if = isa->impl->get_code_generator_if(isa);
379 /* get a code generator for this graph. */
380 birg.cg = cg_if->init(&birg);
382 /* create the code generator and generate code. */
383 prepare_graph(&birg);
385 /* some transformations need to be done before abi introduce */
386 arch_code_generator_before_abi(birg.cg);
388 /* implement the ABI conventions. */
389 birg.abi = be_abi_introduce(&birg);
390 dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
392 be_do_stat_nodes(irg, "02 Abi");
395 arch_code_generator_prepare_graph(birg.cg);
397 be_do_stat_nodes(irg, "03 Prepare");
400 * Since the code generator made a lot of new nodes and skipped
401 * a lot of old ones, we should do dead node elimination here.
402 * Note that this requires disabling the edges here.
404 edges_deactivate(irg);
405 //dead_node_elimination(irg);
408 /* Compute loop nesting information (for weighting copies) */
409 construct_cf_backedges(irg);
411 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
413 /* Schedule the graphs. */
414 arch_code_generator_before_sched(birg.cg);
415 list_sched(&birg, be_enable_mris);
416 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
419 be_sched_vrfy(birg.irg, vrfy_option);
421 be_do_stat_nodes(irg, "04 Schedule");
423 /* we switch off optimizations here, because they might cause trouble */
424 save_optimization_state(&state);
426 set_opt_normalize(0);
428 /* add Keeps for should_be_different constrained nodes */
429 /* beware: needs schedule due to usage of be_ssa_constr */
430 assure_constraints(&birg);
431 dump(DUMP_SCHED, irg, "-assured", dump_ir_block_graph_sched);
433 be_do_stat_nodes(irg, "05 Constraints");
435 /* connect all stack modifying nodes together (see beabi.c) */
436 be_abi_fix_stack_nodes(birg.abi);
437 dump(DUMP_SCHED, irg, "-fix_stack", dump_ir_block_graph_sched);
440 be_sched_vrfy(birg.irg, vrfy_option);
442 /* do some statistics */
443 be_do_stat_reg_pressure(&birg);
445 /* Do register allocation */
446 arch_code_generator_before_ra(birg.cg);
448 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
450 be_do_stat_nodes(irg, "06 Register Allocation");
452 arch_code_generator_after_ra(birg.cg);
453 be_abi_fix_stack_bias(birg.abi);
456 be_sched_vrfy(birg.irg, vrfy_option);
458 arch_code_generator_done(birg.cg);
459 dump(DUMP_FINAL, irg, "-end", dump_ir_extblock_graph_sched);
460 be_abi_free(birg.abi);
462 be_do_stat_nodes(irg, "07 Final");
464 restore_optimization_state(&state);
466 /* switched off due to statistics (statistic module needs all irgs) */
467 // free_ir_graph(irg);
472 /* Main interface to the frontend. */
473 void be_main(FILE *file_handle)
475 /* never build code for pseudo irgs */
476 set_visit_pseudo_irgs(0);
479 be_main_loop(file_handle);
482 /** The debug info retriever function. */
483 static retrieve_dbg_func retrieve_dbg = NULL;
485 /* Sets a debug info retriever. */
486 void be_set_debug_retrieve(retrieve_dbg_func func) {
490 /* Retrieve the debug info. */
491 const char *be_retrieve_dbg_info(const dbg_info *dbg, unsigned *line) {
493 return retrieve_dbg(dbg, line);