3 * @author Sebastian Hack
13 #include <libcore/lc_opts.h>
14 #include <libcore/lc_opts_enum.h>
15 #endif /* WITH_LIBCORE */
26 #include "iredges_t.h"
32 #include "firm/bearch_firm.h"
33 #include "ia32/bearch_ia32.h"
34 #include "arm/bearch_arm.h"
35 #include "ppc32/bearch_ppc32.h"
36 #include "mips/bearch_mips.h"
43 #include "besched_t.h"
44 #include "belistsched.h"
46 #include "bespillilp.h"
47 #include "bespillbelady.h"
49 #include "beraextern.h"
50 #include "bechordal_t.h"
52 #include "beifg_impl.h"
53 #include "becopyopt.h"
54 #include "becopystat.h"
55 #include "bessadestr.h"
58 #include "beschedmris.h"
62 #define DUMP_INITIAL (1 << 0)
63 #define DUMP_ABI (1 << 1)
64 #define DUMP_SCHED (1 << 2)
65 #define DUMP_PREPARED (1 << 3)
66 #define DUMP_RA (1 << 4)
67 #define DUMP_FINAL (1 << 5)
69 /* options visible for anyone */
70 static be_options_t be_options = {
72 "i44pc52.info.uni-karlsruhe.de",
79 static unsigned dump_flags = 0;
81 /* register allocator to use. */
82 static const be_ra_t *ra = &be_ra_chordal_allocator;
84 /* back end instruction set architecture to use */
85 static const arch_isa_if_t *isa_if = &ia32_isa_if;
87 static int be_disable_mris = 0;
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[] = {
96 { "initial", DUMP_INITIAL },
98 { "sched", DUMP_SCHED },
99 { "prepared", DUMP_PREPARED },
100 { "regalloc", DUMP_RA },
101 { "final", DUMP_FINAL },
102 { "all", 2 * DUMP_FINAL - 1 },
106 /* register allocators */
107 static const lc_opt_enum_const_ptr_items_t ra_items[] = {
108 { "chordal", &be_ra_chordal_allocator },
109 { "external", &be_ra_external_allocator },
113 /* instruction set architectures. */
114 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
115 { "ia32", &ia32_isa_if },
117 { "arm", &arm_isa_if },
118 { "ppc32", &ppc32_isa_if },
119 { "mips", &mips_isa_if },
124 static lc_opt_enum_mask_var_t dump_var = {
125 &dump_flags, dump_items
128 static lc_opt_enum_const_ptr_var_t ra_var = {
129 (const void **) &ra, ra_items
132 static lc_opt_enum_const_ptr_var_t isa_var = {
133 (const void **) &isa_if, isa_items
136 static const lc_opt_table_entry_t be_main_options[] = {
137 LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
138 LC_OPT_ENT_ENUM_PTR ("ra", "register allocator", &ra_var),
139 LC_OPT_ENT_ENUM_PTR ("isa", "the instruction set architecture", &isa_var),
140 LC_OPT_ENT_NEGBOOL ("noomitfp", "do not omit frame pointer", &be_omit_fp),
141 LC_OPT_ENT_NEGBOOL ("nomris", "disable mris schedule preparation", &be_disable_mris),
144 LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
145 LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
146 #endif /* WITH_ILP */
150 #endif /* WITH_LIBCORE */
152 void be_opt_register(void)
156 lc_opt_entry_t *be_grp_ra;
157 static int run_once = 0;
161 be_grp_root = lc_opt_get_grp(firm_opt_get_root(), "be");
162 be_grp_ra = lc_opt_get_grp(be_grp_root, "ra");
164 lc_opt_add_table(be_grp_root, be_main_options);
166 /* register allocator options */
167 for(i = 0; ra_items[i].name != NULL; ++i) {
168 const be_ra_t *ra = ra_items[i].value;
169 ra->register_options(be_grp_ra);
172 /* register isa options */
173 for(i = 0; isa_items[i].name != NULL; ++i) {
174 const arch_isa_if_t *isa = isa_items[i].value;
175 isa->register_options(be_grp_root);
178 #endif /* WITH_LIBCORE */
181 /* Parse one argument. */
182 int be_parse_arg(const char *arg) {
184 if (strcmp(arg, "help") == 0 || (arg[0] == '?' && arg[1] == '\0')) {
185 lc_opt_print_help(be_grp_root, stdout);
188 return lc_opt_from_single_arg(be_grp_root, NULL, arg, NULL);
189 #endif /* WITH_LIBCORE */
192 /** The be parameters returned by default, all off. */
193 const static backend_params be_params = {
200 /* Initialize the Firm backend. Must be run BEFORE init_firm()! */
201 const backend_params *be_init(void)
212 if (isa_if->get_params)
213 return isa_if->get_params();
217 static be_main_env_t *be_init_env(be_main_env_t *env, FILE *file_handle)
219 memset(env, 0, sizeof(*env));
220 obstack_init(&env->obst);
221 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
222 env->options = &be_options;
223 FIRM_DBG_REGISTER(env->dbg, "be.main");
225 arch_env_init(env->arch_env, isa_if, file_handle);
227 /* Register the irn handler of the architecture */
228 if (arch_isa_get_irn_handler(env->arch_env->isa))
229 arch_env_push_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
232 * Register the node handler of the back end infrastructure.
233 * This irn handler takes care of the platform independent
234 * spill, reload and perm nodes.
236 arch_env_push_irn_handler(env->arch_env, &be_node_irn_handler);
237 env->phi_handler = be_phi_handler_new(env->arch_env);
238 arch_env_push_irn_handler(env->arch_env, env->phi_handler);
243 static void be_done_env(be_main_env_t *env)
245 env->arch_env->isa->impl->done(env->arch_env->isa);
246 be_phi_handler_free(env->phi_handler);
247 obstack_free(&env->obst, NULL);
250 static void dump(int mask, ir_graph *irg, const char *suffix,
251 void (*dumper)(ir_graph *, const char *))
253 if(dump_flags & mask)
254 be_dump(irg, suffix, dumper);
258 * Prepare a backend graph for code generation.
260 static void prepare_graph(be_irg_t *birg)
262 ir_graph *irg = birg->irg;
264 /* Normalize proj nodes. */
265 normalize_proj_nodes(irg);
267 /* Make just one return node. */
268 normalize_one_return(irg);
270 /* Remove critical edges */
271 remove_critical_cf_edges(irg);
273 /* Compute the dominance information. */
277 /* Ensure, that the ir_edges are computed. */
280 /* check, if the dominance property is fulfilled. */
281 be_check_dominance(irg);
283 /* reset the phi handler. */
284 be_phi_handler_reset(birg->main_env->phi_handler);
288 * The Firm backend main loop.
289 * Do architecture specific lowering for all graphs
290 * and call the architecture specific code generator.
292 static void be_main_loop(FILE *file_handle)
298 be_init_env(&env, file_handle);
300 isa = arch_env_get_isa(env.arch_env);
302 // /* for debugging, anchors helps */
303 // dump_all_anchors(1);
306 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
307 ir_graph *irg = get_irp_irg(i);
308 const arch_code_generator_if_t *cg_if;
312 birg.main_env = &env;
314 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
315 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
317 be_stat_init_irg(env.arch_env, irg);
318 be_do_stat_nodes(irg, "01 Begin");
320 /* set the current graph (this is important for several firm functions) */
321 current_ir_graph = birg.irg;
323 /* Get the code generator interface. */
324 cg_if = isa->impl->get_code_generator_if(isa);
326 /* get a code generator for this graph. */
327 birg.cg = cg_if->init(&birg);
329 /* create the code generator and generate code. */
330 prepare_graph(&birg);
332 /* some transformations need to be done before abi introduce */
333 arch_code_generator_before_abi(birg.cg);
335 /* implement the ABI conventions. */
336 birg.abi = be_abi_introduce(&birg);
337 dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
339 be_do_stat_nodes(irg, "02 Abi");
342 arch_code_generator_prepare_graph(birg.cg);
344 be_do_stat_nodes(irg, "03 Prepare");
347 * Since the code generator made a lot of new nodes and skipped
348 * a lot of old ones, we should do dead node elimination here.
349 * Note that this requires disabling the edges here.
351 edges_deactivate(irg);
352 //dead_node_elimination(irg);
355 /* Compute loop nesting information (for weighting copies) */
356 construct_cf_backedges(irg);
358 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
360 /* Schedule the graphs. */
361 arch_code_generator_before_sched(birg.cg);
362 list_sched(&birg, be_disable_mris);
363 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
365 DEBUG_ONLY(be_verify_schedule(birg.irg);)
367 be_do_stat_nodes(irg, "04 Schedule");
369 /* add Keeps for should_be_different constrained nodes */
370 /* beware: needs schedule due to usage of be_ssa_constr */
371 assure_constraints(&birg);
372 dump(DUMP_SCHED, irg, "-assured", dump_ir_block_graph_sched);
374 be_do_stat_nodes(irg, "05 Constraints");
376 /* connect all stack modifying nodes together (see beabi.c) */
377 be_abi_fix_stack_nodes(birg.abi);
378 dump(DUMP_SCHED, irg, "-fix_stack", dump_ir_block_graph_sched);
380 /* Verify the schedule */
381 assert(sched_verify_irg(irg));
383 /* do some statistics */
384 be_do_stat_reg_pressure(&birg);
386 /* Do register allocation */
387 arch_code_generator_before_ra(birg.cg);
389 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
391 be_do_stat_nodes(irg, "06 Register Allocation");
393 arch_code_generator_after_ra(birg.cg);
394 be_abi_fix_stack_bias(birg.abi);
396 DEBUG_ONLY(be_verify_schedule(birg.irg);)
398 arch_code_generator_done(birg.cg);
399 dump(DUMP_FINAL, irg, "-end", dump_ir_extblock_graph_sched);
400 be_abi_free(birg.abi);
402 be_do_stat_nodes(irg, "07 Final");
404 // free_ir_graph(irg);
409 /* Main interface to the frontend. */
410 void be_main(FILE *file_handle)
412 /* never build code for pseudo irgs */
413 set_visit_pseudo_irgs(0);
416 be_main_loop(file_handle);
419 /** The debug info retriever function. */
420 static retrieve_dbg_func retrieve_dbg = NULL;
422 /* Sets a debug info retriever. */
423 void be_set_debug_retrieve(retrieve_dbg_func func) {
427 /* Retrieve the debug info. */
428 const char *be_retrieve_dbg_info(const dbg_info *dbg, unsigned *line) {
430 return retrieve_dbg(dbg, line);