3 * @author Sebastian Hack
13 #include <libcore/lc_opts.h>
14 #include <libcore/lc_opts_enum.h>
26 #include "iredges_t.h"
31 #include "bechordal_t.h"
34 #include "beifg_impl.h"
36 #include "besched_t.h"
37 #include "belistsched.h"
40 #include "bechordal.h"
42 #include "becopyoptmain.h"
43 #include "becopystat.h"
44 #include "bessadestr.h"
47 #include "bespillilp.h"
48 #include "bespillbelady.h"
50 #include "firm/bearch_firm.h"
52 #define DUMP_INITIAL (1 << 0)
53 #define DUMP_SCHED (1 << 1)
54 #define DUMP_PREPARED (1 << 2)
55 #define DUMP_FINAL (1 << 3)
57 /* options visible for anyone */
58 be_options_t be_options = {
60 "i44pc52.info.uni-karlsruhe.de",
67 static unsigned dump_flags = 0;
69 /* register allocator to use. */
70 static const be_ra_t *ra = &be_ra_chordal_allocator;
72 /* back end instruction set architecture to use */
73 static const arch_isa_if_t *isa_if = &firm_isa;
77 static lc_opt_entry_t *be_grp_root = NULL;
79 /* possible dumping options */
80 static const lc_opt_enum_mask_items_t dump_items[] = {
82 { "initial", DUMP_INITIAL },
83 { "sched", DUMP_SCHED },
84 { "prepared", DUMP_PREPARED },
85 { "final", DUMP_FINAL },
86 { "all", 2 * DUMP_FINAL - 1 },
90 /* register allocators */
91 static const lc_opt_enum_const_ptr_items_t ra_items[] = {
92 { "chordal", &be_ra_chordal_allocator },
96 /* instruction set architectures. */
97 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
98 { "firm", &firm_isa },
102 static lc_opt_enum_mask_var_t dump_var = {
103 &dump_flags, dump_items
106 static lc_opt_enum_const_ptr_var_t ra_var = {
107 (const void **) &ra, ra_items
110 static lc_opt_enum_const_ptr_var_t isa_var = {
111 (const void **) &isa_if, isa_items
114 static const lc_opt_table_entry_t be_main_options[] = {
115 LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
116 LC_OPT_ENT_ENUM_PTR("ra", "register allocator", &ra_var),
117 LC_OPT_ENT_ENUM_PTR("isa", "the instruction set architecture", &isa_var),
119 LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
120 LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
126 void be_opt_register(void)
131 be_grp_root = lc_opt_get_grp(firm_opt_get_root(), "be");
133 lc_opt_add_table(be_grp_root, be_main_options);
135 /* register register allocator options */
136 for(i = 0; ra_items[i].name != NULL; ++i) {
137 const be_ra_t *ra = ra_items[i].value;
138 ra->register_options(be_grp_root);
141 /* register isa options */
142 for(i = 0; isa_items[i].name != NULL; ++i) {
143 const arch_isa_if_t *isa = isa_items[i].value;
144 isa->register_options(be_grp_root);
162 static be_main_env_t *be_init_env(be_main_env_t *env)
164 obstack_init(&env->obst);
165 env->dbg = firm_dbg_register("be.main");
167 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
168 arch_env_init(env->arch_env, isa_if);
170 /* Register the irn handler of the architecture */
171 if (arch_isa_get_irn_handler(env->arch_env->isa))
172 arch_env_add_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
175 * Register the node handler of the back end infrastructure.
176 * This irn handler takes care of the platform independent
177 * spill, reload and perm nodes.
179 env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
180 be_node_factory_init(env->node_factory, env->arch_env->isa);
181 arch_env_add_irn_handler(env->arch_env, be_node_get_irn_handler(env->node_factory));
186 static void be_done_env(be_main_env_t *env)
188 env->arch_env->isa->impl->done(env->arch_env->isa);
189 obstack_free(&env->obst, NULL);
192 static void dump(int mask, ir_graph *irg, const char *suffix,
193 void (*dumper)(ir_graph *, const char *))
195 if(dump_flags & mask)
199 static void prepare_graph(be_main_env_t *s, ir_graph *irg)
201 /* Normalize proj nodes. */
202 normalize_proj_nodes(irg);
204 /* Remove critical edges */
205 remove_critical_cf_edges(irg);
207 /* Compute the dominance information. */
208 free_dom_and_peace(irg);
211 /* Ensure, that the ir_edges are computed. */
214 /* Compute loop nesting information (for weighting copies) */
215 if (get_irg_loopinfo_state(irg) != (loopinfo_valid & loopinfo_cf_consistent))
216 construct_cf_backedges(irg);
218 be_check_dominance(irg);
221 static void be_main_loop(void)
229 isa = arch_env_get_isa(env.arch_env);
232 for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
233 ir_graph *irg = get_irp_irg(i);
235 arch_code_generator_t *cg;
237 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
238 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
240 /* set the current graph (this is important for several firm functions) */
241 current_ir_graph = irg;
243 /* get a code generator for this graph. */
244 cg = arch_isa_make_code_generator(isa, irg);
246 /* create the code generator and generate code. */
247 prepare_graph(&env, irg);
248 arch_code_generator_prepare_graph(cg);
250 edges_deactivate(irg);
251 dead_node_elimination(irg);
254 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
256 /* Schedule the graphs. */
257 arch_code_generator_before_sched(cg);
258 list_sched(isa, irg);
260 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
262 /* Verify the schedule */
263 sched_verify_irg(irg);
265 /* Do register allocation */
266 arch_code_generator_before_ra(cg);
267 ra->allocate(&env, irg);
269 arch_code_generator_done(cg);
270 dump(DUMP_FINAL, irg, "-end", dump_ir_block_graph_sched);
276 void be_main(int argc, const char *argv[])