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();
252 static be_main_env_t *be_init_env(be_main_env_t *env, FILE *file_handle)
254 memset(env, 0, sizeof(*env));
255 obstack_init(&env->obst);
256 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
257 env->options = &be_options;
258 FIRM_DBG_REGISTER(env->dbg, "be.main");
260 arch_env_init(env->arch_env, isa_if, file_handle);
262 /* Register the irn handler of the architecture */
263 if (arch_isa_get_irn_handler(env->arch_env->isa))
264 arch_env_push_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
267 * Register the node handler of the back end infrastructure.
268 * This irn handler takes care of the platform independent
269 * spill, reload and perm nodes.
271 arch_env_push_irn_handler(env->arch_env, &be_node_irn_handler);
272 env->phi_handler = be_phi_handler_new(env->arch_env);
273 arch_env_push_irn_handler(env->arch_env, env->phi_handler);
278 static void be_done_env(be_main_env_t *env)
280 env->arch_env->isa->impl->done(env->arch_env->isa);
281 be_phi_handler_free(env->phi_handler);
282 obstack_free(&env->obst, NULL);
285 static void dump(int mask, ir_graph *irg, const char *suffix,
286 void (*dumper)(ir_graph *, const char *))
288 if(dump_flags & mask)
289 be_dump(irg, suffix, dumper);
293 * Prepare a backend graph for code generation.
295 static void prepare_graph(be_irg_t *birg)
297 ir_graph *irg = birg->irg;
299 /* Normalize proj nodes. */
300 normalize_proj_nodes(irg);
302 /* Make just one return node. */
303 normalize_one_return(irg);
305 /* Remove critical edges */
306 remove_critical_cf_edges(irg);
308 /* Compute the dominance information. */
312 /* Ensure, that the ir_edges are computed. */
315 /* check, if the dominance property is fulfilled. */
316 be_check_dominance(irg);
318 /* reset the phi handler. */
319 be_phi_handler_reset(birg->main_env->phi_handler);
323 * The Firm backend main loop.
324 * Do architecture specific lowering for all graphs
325 * and call the architecture specific code generator.
327 static void be_main_loop(FILE *file_handle)
333 be_init_env(&env, file_handle);
335 isa = arch_env_get_isa(env.arch_env);
337 // /* for debugging, anchors helps */
338 // dump_all_anchors(1);
341 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
342 ir_graph *irg = get_irp_irg(i);
343 const arch_code_generator_if_t *cg_if;
345 int save_optimize, save_normalize, status;
348 birg.main_env = &env;
350 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
351 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
353 be_stat_init_irg(env.arch_env, irg);
354 be_do_stat_nodes(irg, "01 Begin");
356 /* set the current graph (this is important for several firm functions) */
357 current_ir_graph = birg.irg;
359 /* Get the code generator interface. */
360 cg_if = isa->impl->get_code_generator_if(isa);
362 /* get a code generator for this graph. */
363 birg.cg = cg_if->init(&birg);
365 /* create the code generator and generate code. */
366 prepare_graph(&birg);
368 /* some transformations need to be done before abi introduce */
369 arch_code_generator_before_abi(birg.cg);
371 /* implement the ABI conventions. */
372 birg.abi = be_abi_introduce(&birg);
373 dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
375 be_do_stat_nodes(irg, "02 Abi");
378 arch_code_generator_prepare_graph(birg.cg);
380 be_do_stat_nodes(irg, "03 Prepare");
383 * Since the code generator made a lot of new nodes and skipped
384 * a lot of old ones, we should do dead node elimination here.
385 * Note that this requires disabling the edges here.
387 edges_deactivate(irg);
388 //dead_node_elimination(irg);
391 /* Compute loop nesting information (for weighting copies) */
392 construct_cf_backedges(irg);
394 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
396 /* Schedule the graphs. */
397 arch_code_generator_before_sched(birg.cg);
398 list_sched(&birg, be_enable_mris);
399 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
402 be_sched_vrfy(birg.irg, vrfy_option);
404 be_do_stat_nodes(irg, "04 Schedule");
406 /* we switch off optimizations here, because they might cause trouble */
407 save_optimize = get_optimize();
408 save_normalize = get_opt_normalize();
410 set_opt_normalize(0);
412 /* add Keeps for should_be_different constrained nodes */
413 /* beware: needs schedule due to usage of be_ssa_constr */
414 assure_constraints(&birg);
415 dump(DUMP_SCHED, irg, "-assured", dump_ir_block_graph_sched);
417 be_do_stat_nodes(irg, "05 Constraints");
419 /* connect all stack modifying nodes together (see beabi.c) */
420 be_abi_fix_stack_nodes(birg.abi);
421 dump(DUMP_SCHED, irg, "-fix_stack", dump_ir_block_graph_sched);
424 be_sched_vrfy(birg.irg, vrfy_option);
426 /* do some statistics */
427 be_do_stat_reg_pressure(&birg);
429 /* Do register allocation */
430 arch_code_generator_before_ra(birg.cg);
432 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
434 be_do_stat_nodes(irg, "06 Register Allocation");
436 arch_code_generator_after_ra(birg.cg);
437 be_abi_fix_stack_bias(birg.abi);
440 be_sched_vrfy(birg.irg, vrfy_option);
442 arch_code_generator_done(birg.cg);
443 dump(DUMP_FINAL, irg, "-end", dump_ir_extblock_graph_sched);
444 be_abi_free(birg.abi);
446 be_do_stat_nodes(irg, "07 Final");
448 /* reset the optimizations */
449 set_optimize(save_optimize);
450 set_opt_normalize(save_normalize);
452 /* switched off due to statistics (statistic module needs all irgs) */
453 // free_ir_graph(irg);
458 /* Main interface to the frontend. */
459 void be_main(FILE *file_handle)
461 /* never build code for pseudo irgs */
462 set_visit_pseudo_irgs(0);
465 be_main_loop(file_handle);
468 /** The debug info retriever function. */
469 static retrieve_dbg_func retrieve_dbg = NULL;
471 /* Sets a debug info retriever. */
472 void be_set_debug_retrieve(retrieve_dbg_func func) {
476 /* Retrieve the debug info. */
477 const char *be_retrieve_dbg_info(const dbg_info *dbg, unsigned *line) {
479 return retrieve_dbg(dbg, line);