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"
59 #define DUMP_INITIAL (1 << 0)
60 #define DUMP_ABI (1 << 1)
61 #define DUMP_SCHED (1 << 2)
62 #define DUMP_PREPARED (1 << 3)
63 #define DUMP_RA (1 << 4)
64 #define DUMP_FINAL (1 << 5)
66 /* options visible for anyone */
67 static be_options_t be_options = {
69 "i44pc52.info.uni-karlsruhe.de",
76 static unsigned dump_flags = 2 * DUMP_FINAL - 1;
78 /* register allocator to use. */
79 static const be_ra_t *ra = &be_ra_chordal_allocator;
81 /* back end instruction set architecture to use */
82 static const arch_isa_if_t *isa_if = &ia32_isa_if;
85 static lc_opt_entry_t *be_grp_root = NULL;
87 /* possible dumping options */
88 static const lc_opt_enum_mask_items_t dump_items[] = {
90 { "initial", DUMP_INITIAL },
92 { "sched", DUMP_SCHED },
93 { "prepared", DUMP_PREPARED },
94 { "regalloc", DUMP_RA },
95 { "final", DUMP_FINAL },
96 { "all", 2 * DUMP_FINAL - 1 },
100 /* register allocators */
101 static const lc_opt_enum_const_ptr_items_t ra_items[] = {
102 { "chordal", &be_ra_chordal_allocator },
103 { "external", &be_ra_external_allocator },
107 /* instruction set architectures. */
108 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
109 { "firm", &firm_isa },
110 { "ia32", &ia32_isa_if },
111 { "arm", &arm_isa_if },
112 { "ppc", &ppc32_isa_if },
113 { "mips", &mips_isa_if },
117 static lc_opt_enum_mask_var_t dump_var = {
118 &dump_flags, dump_items
121 static lc_opt_enum_const_ptr_var_t ra_var = {
122 (const void **) &ra, ra_items
125 static lc_opt_enum_const_ptr_var_t isa_var = {
126 (const void **) &isa_if, isa_items
129 static const lc_opt_table_entry_t be_main_options[] = {
130 LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
131 LC_OPT_ENT_ENUM_PTR("ra", "register allocator", &ra_var),
132 LC_OPT_ENT_ENUM_PTR("isa", "the instruction set architecture", &isa_var),
135 LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
136 LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
137 #endif /* WITH_ILP */
141 #endif /* WITH_LIBCORE */
143 void be_opt_register(void)
147 lc_opt_entry_t *be_grp_ra;
148 static int run_once = 0;
152 be_grp_root = lc_opt_get_grp(firm_opt_get_root(), "be");
153 be_grp_ra = lc_opt_get_grp(be_grp_root, "ra");
155 lc_opt_add_table(be_grp_root, be_main_options);
157 /* register allocator options */
158 for(i = 0; ra_items[i].name != NULL; ++i) {
159 const be_ra_t *ra = ra_items[i].value;
160 ra->register_options(be_grp_ra);
163 /* register isa options */
164 for(i = 0; isa_items[i].name != NULL; ++i) {
165 const arch_isa_if_t *isa = isa_items[i].value;
166 isa->register_options(be_grp_root);
169 #endif /* WITH_LIBCORE */
172 static int be_opt_error_handler(const char *prefix, const lc_opt_err_info_t *err)
177 /* Parse one argument. */
178 int be_parse_arg(const char *arg) {
180 return lc_opt_from_single_arg(be_grp_root, NULL, arg, be_opt_error_handler);
181 #endif /* WITH_LIBCORE */
197 static be_main_env_t *be_init_env(be_main_env_t *env)
199 memset(env, 0, sizeof(*env));
200 obstack_init(&env->obst);
201 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
202 env->options = &be_options;
203 FIRM_DBG_REGISTER(env->dbg, "be.main");
205 arch_env_init(env->arch_env, isa_if);
207 /* Register the irn handler of the architecture */
208 if (arch_isa_get_irn_handler(env->arch_env->isa))
209 arch_env_push_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
212 * Register the node handler of the back end infrastructure.
213 * This irn handler takes care of the platform independent
214 * spill, reload and perm nodes.
216 arch_env_push_irn_handler(env->arch_env, &be_node_irn_handler);
217 env->phi_handler = be_phi_handler_new(env->arch_env);
218 arch_env_push_irn_handler(env->arch_env, env->phi_handler);
223 static void be_done_env(be_main_env_t *env)
225 env->arch_env->isa->impl->done(env->arch_env->isa);
226 be_phi_handler_free(env->phi_handler);
227 obstack_free(&env->obst, NULL);
230 static void dump(int mask, ir_graph *irg, const char *suffix,
231 void (*dumper)(ir_graph *, const char *))
233 if(dump_flags & mask)
234 be_dump(irg, suffix, dumper);
238 * Prepare a backend graph for code generation.
240 static void prepare_graph(be_irg_t *birg)
242 ir_graph *irg = birg->irg;
244 /* Normalize proj nodes. */
245 normalize_proj_nodes(irg);
247 /* Make just one return node. */
248 normalize_one_return(irg);
250 /* Remove critical edges */
251 remove_critical_cf_edges(irg);
253 /* Compute the dominance information. */
257 /* Ensure, that the ir_edges are computed. */
260 /* check, if the dominance property is fulfilled. */
261 be_check_dominance(irg);
263 /* reset the phi handler. */
264 be_phi_handler_reset(birg->main_env->phi_handler);
268 * The Firm backend main loop.
269 * Do architecture specific lowering for all graphs
270 * and call the architecture specific code generator.
272 static void be_main_loop(FILE *file_handle)
280 isa = arch_env_get_isa(env.arch_env);
283 for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
284 ir_graph *irg = get_irp_irg(i);
285 const arch_code_generator_if_t *cg_if;
289 birg.main_env = &env;
291 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
292 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
294 /* set the current graph (this is important for several firm functions) */
295 current_ir_graph = birg.irg;
297 /* Get the code generator interface. */
298 cg_if = isa->impl->get_code_generator_if(isa);
300 /* get a code generator for this graph. */
301 birg.cg = cg_if->init(file_handle, &birg);
303 /* create the code generator and generate code. */
304 prepare_graph(&birg);
306 /* some transformations need to be done before abi introduce */
307 arch_code_generator_before_abi(birg.cg);
309 /* implement the ABI conventions. */
310 birg.abi = be_abi_introduce(&birg);
311 dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
314 arch_code_generator_prepare_graph(birg.cg);
317 * Since the code generator made a lot of new nodes and skipped
318 * a lot of old ones, we should do dead node elimination here.
319 * Note that this requires disabling the edges here.
321 edges_deactivate(irg);
322 //dead_node_elimination(irg);
325 /* Compute loop nesting information (for weighting copies) */
326 construct_cf_backedges(irg);
328 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
330 /* add Keeps for should_be_different constrained nodes */
331 assure_constraints(&birg);
332 dump(DUMP_PREPARED, irg, "-assured", dump_ir_block_graph);
334 /* Schedule the graphs. */
335 arch_code_generator_before_sched(birg.cg);
336 list_sched(env.arch_env, irg);
338 /* connect all stack modifying nodes together (see beabi.c) */
339 be_abi_fix_stack_nodes(birg.abi);
340 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
342 /* Verify the schedule */
343 sched_verify_irg(irg);
345 /* Do register allocation */
346 arch_code_generator_before_ra(birg.cg);
348 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
350 arch_code_generator_after_ra(birg.cg);
351 be_abi_fix_stack_bias(birg.abi);
353 arch_code_generator_done(birg.cg);
354 dump(DUMP_FINAL, irg, "-end", dump_ir_block_graph_sched);
355 be_abi_free(birg.abi);
363 /* Main interface to the frontend. */
364 void be_main(FILE *file_handle)
366 /* never build code for pseudo irgs */
367 set_visit_pseudo_irgs(0);
370 be_main_loop(file_handle);
373 /** The debug info retriever function. */
374 static retrieve_dbg_func retrieve_dbg = NULL;
376 /* Sets a debug info retriever. */
377 void be_set_debug_retrieve(retrieve_dbg_func func) {
381 /* Retrieve the debug info. */
382 const char *be_retrieve_dbg_info(const dbg_info *dbg, unsigned *line) {
384 return retrieve_dbg(dbg, line);