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"
31 #include "firm/bearch_firm.h"
32 #include "ia32/bearch_ia32.h"
39 #include "besched_t.h"
40 #include "belistsched.h"
42 #include "bespillilp.h"
43 #include "bespillbelady.h"
45 #include "beraextern.h"
46 #include "bechordal_t.h"
48 #include "beifg_impl.h"
49 #include "becopyoptmain.h"
50 #include "becopystat.h"
51 #include "bessadestr.h"
54 #define DUMP_INITIAL (1 << 0)
55 #define DUMP_SCHED (1 << 1)
56 #define DUMP_PREPARED (1 << 2)
57 #define DUMP_RA (1 << 3)
58 #define DUMP_FINAL (1 << 4)
60 /* options visible for anyone */
61 be_options_t be_options = {
63 "i44pc52.info.uni-karlsruhe.de",
70 static unsigned dump_flags = DUMP_INITIAL | DUMP_SCHED | DUMP_PREPARED | DUMP_RA | DUMP_FINAL;
72 /* register allocator to use. */
73 static const be_ra_t *ra = &be_ra_chordal_allocator;
75 /* back end instruction set architecture to use */
76 static const arch_isa_if_t *isa_if = &ia32_isa_if;
80 static lc_opt_entry_t *be_grp_root = NULL;
82 /* possible dumping options */
83 static const lc_opt_enum_mask_items_t dump_items[] = {
85 { "initial", DUMP_INITIAL },
86 { "sched", DUMP_SCHED },
87 { "prepared", DUMP_PREPARED },
88 { "regalloc", DUMP_RA },
89 { "final", DUMP_FINAL },
90 { "all", 2 * DUMP_FINAL - 1 },
94 /* register allocators */
95 static const lc_opt_enum_const_ptr_items_t ra_items[] = {
96 { "chordal", &be_ra_chordal_allocator },
97 { "external", &be_ra_external_allocator },
101 /* instruction set architectures. */
102 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
103 { "firm", &firm_isa },
104 { "ia32", &ia32_isa_if },
108 static lc_opt_enum_mask_var_t dump_var = {
109 &dump_flags, dump_items
112 static lc_opt_enum_const_ptr_var_t ra_var = {
113 (const void **) &ra, ra_items
116 static lc_opt_enum_const_ptr_var_t isa_var = {
117 (const void **) &isa_if, isa_items
120 static const lc_opt_table_entry_t be_main_options[] = {
121 LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
122 LC_OPT_ENT_ENUM_PTR("ra", "register allocator", &ra_var),
123 LC_OPT_ENT_ENUM_PTR("isa", "the instruction set architecture", &isa_var),
125 LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
126 LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
130 #endif /* WITH_LIBCORE */
132 void be_opt_register(void)
136 lc_opt_entry_t *be_grp_ra;
138 be_grp_root = lc_opt_get_grp(firm_opt_get_root(), "be");
139 be_grp_ra = lc_opt_get_grp(be_grp_root, "ra");
141 lc_opt_add_table(be_grp_root, be_main_options);
143 /* register allocator options */
144 for(i = 0; ra_items[i].name != NULL; ++i) {
145 const be_ra_t *ra = ra_items[i].value;
146 ra->register_options(be_grp_ra);
149 /* register isa options */
150 for(i = 0; isa_items[i].name != NULL; ++i) {
151 const arch_isa_if_t *isa = isa_items[i].value;
152 isa->register_options(be_grp_root);
154 #endif /* WITH_LIBCORE */
170 static be_main_env_t *be_init_env(be_main_env_t *env)
172 obstack_init(&env->obst);
173 env->dbg = firm_dbg_register("be.main");
175 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
176 arch_env_init(env->arch_env, isa_if);
178 /* Register the irn handler of the architecture */
179 if (arch_isa_get_irn_handler(env->arch_env->isa))
180 arch_env_add_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
183 * Register the node handler of the back end infrastructure.
184 * This irn handler takes care of the platform independent
185 * spill, reload and perm nodes.
187 arch_env_add_irn_handler(env->arch_env, &be_node_irn_handler);
192 static void be_done_env(be_main_env_t *env)
194 env->arch_env->isa->impl->done(env->arch_env->isa);
195 obstack_free(&env->obst, NULL);
198 static void dump(int mask, ir_graph *irg, const char *suffix,
199 void (*dumper)(ir_graph *, const char *))
201 if(dump_flags & mask)
205 static void prepare_graph(be_main_env_t *s, ir_graph *irg)
207 /* Normalize proj nodes. */
208 normalize_proj_nodes(irg);
210 /* Remove critical edges */
211 remove_critical_cf_edges(irg);
213 /* Compute the dominance information. */
217 /* Ensure, that the ir_edges are computed. */
220 /* Compute loop nesting information (for weighting copies) */
221 if (get_irg_loopinfo_state(irg) != (loopinfo_valid & loopinfo_cf_consistent))
222 construct_cf_backedges(irg);
224 be_check_dominance(irg);
227 static void be_main_loop(FILE *file_handle)
235 isa = arch_env_get_isa(env.arch_env);
238 for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
239 ir_graph *irg = get_irp_irg(i);
241 const arch_code_generator_if_t *cg_if;
243 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
244 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
246 /* set the current graph (this is important for several firm functions) */
247 current_ir_graph = irg;
249 /* Get the code generator interface. */
250 cg_if = isa->impl->get_code_generator_if(isa);
252 /* get a code generator for this graph. */
253 env.cg = cg_if->init(file_handle, irg, env.arch_env);
255 /* create the code generator and generate code. */
256 prepare_graph(&env, irg);
257 arch_code_generator_prepare_graph(env.cg);
259 edges_deactivate(irg);
260 dead_node_elimination(irg);
263 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
265 /* Schedule the graphs. */
266 arch_code_generator_before_sched(env.cg);
267 list_sched(isa, irg);
269 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
271 /* Verify the schedule */
272 sched_verify_irg(irg);
274 /* Do register allocation */
275 arch_code_generator_before_ra(env.cg);
276 ra->allocate(&env, irg);
278 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
280 arch_code_generator_done(env.cg);
281 dump(DUMP_FINAL, irg, "-end", dump_ir_block_graph_sched);
287 void be_main(FILE *file_handle)
290 be_main_loop(file_handle);