5c6ffe1916a0eca3b44f46257d7d961e7e032a2f
[libfirm] / ir / be / bemain.c
1 /**
2  * Backend driver.
3  * @author Sebastian Hack
4  * @date 25.11.2004
5  */
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include <stdarg.h>
11
12 #ifdef WITH_LIBCORE
13 #include <libcore/lc_opts.h>
14 #include <libcore/lc_opts_enum.h>
15 #endif /* WITH_LIBCORE */
16
17 #include "obst.h"
18 #include "bitset.h"
19
20 #include "irprog.h"
21 #include "irgopt.h"
22 #include "irgraph.h"
23 #include "irdump.h"
24 #include "phiclass.h"
25 #include "irdom_t.h"
26 #include "iredges_t.h"
27 #include "irloop_t.h"
28 #include "irtools.h"
29 #include "return.h"
30
31 #include "bearch.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"
37
38 #include "be_t.h"
39 #include "benumb_t.h"
40 #include "beutil.h"
41 #include "benode_t.h"
42 #include "beirgmod.h"
43 #include "besched_t.h"
44 #include "belistsched.h"
45 #include "belive_t.h"
46 #include "bespillilp.h"
47 #include "bespillbelady.h"
48 #include "bera.h"
49 #include "beraextern.h"
50 #include "bechordal_t.h"
51 #include "beifg.h"
52 #include "beifg_impl.h"
53 #include "becopyopt.h"
54 #include "becopystat.h"
55 #include "bessadestr.h"
56 #include "beabi.h"
57 #include "belower.h"
58 #include "bestat.h"
59
60 #define DUMP_INITIAL    (1 << 0)
61 #define DUMP_ABI        (1 << 1)
62 #define DUMP_SCHED      (1 << 2)
63 #define DUMP_PREPARED   (1 << 3)
64 #define DUMP_RA         (1 << 4)
65 #define DUMP_FINAL      (1 << 5)
66
67 /* options visible for anyone */
68 static be_options_t be_options = {
69         /* ilp server */
70         "i44pc52.info.uni-karlsruhe.de",
71
72         /* ilp solver */
73         "cplex"
74 };
75
76 /* dump flags */
77 static unsigned dump_flags = 2 * DUMP_FINAL - 1;
78
79 /* register allocator to use. */
80 static const be_ra_t *ra = &be_ra_chordal_allocator;
81
82 /* back end instruction set architecture to use */
83 static const arch_isa_if_t *isa_if = &ia32_isa_if;
84 #ifdef WITH_LIBCORE
85
86 static lc_opt_entry_t *be_grp_root = NULL;
87
88 /* possible dumping options */
89 static const lc_opt_enum_mask_items_t dump_items[] = {
90         { "none",       0 },
91         { "initial",    DUMP_INITIAL },
92         { "abi",        DUMP_ABI    },
93         { "sched",      DUMP_SCHED  },
94         { "prepared",   DUMP_PREPARED },
95         { "regalloc",   DUMP_RA },
96         { "final",      DUMP_FINAL },
97         { "all",        2 * DUMP_FINAL - 1 },
98         { NULL,         0 }
99 };
100
101 /* register allocators */
102 static const lc_opt_enum_const_ptr_items_t ra_items[] = {
103         { "chordal", &be_ra_chordal_allocator },
104         { "external", &be_ra_external_allocator },
105         { NULL,      NULL }
106 };
107
108 /* instruction set architectures. */
109 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
110         { "firm",    &firm_isa },
111         { "ia32",    &ia32_isa_if },
112         { "arm",     &arm_isa_if },
113         { "ppc32",   &ppc32_isa_if },
114         { "mips",    &mips_isa_if },
115         { NULL,      NULL }
116 };
117
118 static lc_opt_enum_mask_var_t dump_var = {
119         &dump_flags, dump_items
120 };
121
122 static lc_opt_enum_const_ptr_var_t ra_var = {
123         (const void **) &ra, ra_items
124 };
125
126 static lc_opt_enum_const_ptr_var_t isa_var = {
127         (const void **) &isa_if, isa_items
128 };
129
130 static const lc_opt_table_entry_t be_main_options[] = {
131         LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
132         LC_OPT_ENT_ENUM_PTR("ra", "register allocator", &ra_var),
133         LC_OPT_ENT_ENUM_PTR("isa", "the instruction set architecture", &isa_var),
134         LC_OPT_ENT_NEGBOOL("noomitfp", "do not omit frame pointer", &be_omit_fp),
135
136 #ifdef WITH_ILP
137         LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
138         LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
139 #endif /* WITH_ILP */
140         { NULL }
141 };
142
143 #endif /* WITH_LIBCORE */
144
145 void be_opt_register(void)
146 {
147 #ifdef WITH_LIBCORE
148         int i;
149         lc_opt_entry_t *be_grp_ra;
150         static int run_once = 0;
151
152         if (! run_once) {
153                 run_once    = 1;
154                 be_grp_root = lc_opt_get_grp(firm_opt_get_root(), "be");
155                 be_grp_ra   = lc_opt_get_grp(be_grp_root, "ra");
156
157                 lc_opt_add_table(be_grp_root, be_main_options);
158
159                 /* register allocator options */
160                 for(i = 0; ra_items[i].name != NULL; ++i) {
161                         const be_ra_t *ra = ra_items[i].value;
162                         ra->register_options(be_grp_ra);
163                 }
164
165                 /* register isa options */
166                 for(i = 0; isa_items[i].name != NULL; ++i) {
167                         const arch_isa_if_t *isa = isa_items[i].value;
168                         isa->register_options(be_grp_root);
169                 }
170         }
171 #endif /* WITH_LIBCORE */
172 }
173
174 /* Parse one argument. */
175 int be_parse_arg(const char *arg) {
176 #ifdef WITH_LIBCORE
177         return lc_opt_from_single_arg(be_grp_root, NULL, arg, NULL);
178 #endif /* WITH_LIBCORE */
179 }
180
181
182 void be_init(void)
183 {
184         be_opt_register();
185
186         be_sched_init();
187         be_liveness_init();
188         be_numbering_init();
189         be_copy_opt_init();
190         copystat_init();
191         phi_class_init();
192 }
193
194 static be_main_env_t *be_init_env(be_main_env_t *env, FILE *file_handle)
195 {
196         memset(env, 0, sizeof(*env));
197         obstack_init(&env->obst);
198         env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
199         env->options  = &be_options;
200         FIRM_DBG_REGISTER(env->dbg, "be.main");
201
202         arch_env_init(env->arch_env, isa_if, file_handle);
203
204         /* Register the irn handler of the architecture */
205         if (arch_isa_get_irn_handler(env->arch_env->isa))
206                 arch_env_push_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
207
208         /*
209          * Register the node handler of the back end infrastructure.
210          * This irn handler takes care of the platform independent
211          * spill, reload and perm nodes.
212          */
213         arch_env_push_irn_handler(env->arch_env, &be_node_irn_handler);
214         env->phi_handler = be_phi_handler_new(env->arch_env);
215         arch_env_push_irn_handler(env->arch_env, env->phi_handler);
216
217         return env;
218 }
219
220 static void be_done_env(be_main_env_t *env)
221 {
222         env->arch_env->isa->impl->done(env->arch_env->isa);
223         be_phi_handler_free(env->phi_handler);
224         obstack_free(&env->obst, NULL);
225 }
226
227 static void dump(int mask, ir_graph *irg, const char *suffix,
228                  void (*dumper)(ir_graph *, const char *))
229 {
230         if(dump_flags & mask)
231                 be_dump(irg, suffix, dumper);
232 }
233
234 /**
235  * Prepare a backend graph for code generation.
236  */
237 static void prepare_graph(be_irg_t *birg)
238 {
239         ir_graph *irg = birg->irg;
240
241         /* Normalize proj nodes. */
242         normalize_proj_nodes(irg);
243
244         /* Make just one return node. */
245         normalize_one_return(irg);
246
247         /* Remove critical edges */
248         remove_critical_cf_edges(irg);
249
250         /* Compute the dominance information. */
251         free_dom(irg);
252         compute_doms(irg);
253
254         /* Ensure, that the ir_edges are computed. */
255         edges_activate(irg);
256
257         /* check, if the dominance property is fulfilled. */
258         be_check_dominance(irg);
259
260         /* reset the phi handler. */
261         be_phi_handler_reset(birg->main_env->phi_handler);
262 }
263
264 /**
265  * The Firm backend main loop.
266  * Do architecture specific lowering for all graphs
267  * and call the architecture specific code generator.
268  */
269 static void be_main_loop(FILE *file_handle)
270 {
271         int i, n;
272         arch_isa_t *isa;
273         be_main_env_t env;
274
275         be_init_env(&env, file_handle);
276
277         isa = arch_env_get_isa(env.arch_env);
278
279         /* For all graphs */
280         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
281                 ir_graph *irg = get_irp_irg(i);
282                 const arch_code_generator_if_t *cg_if;
283                 be_irg_t birg;
284
285                 birg.irg      = irg;
286                 birg.main_env = &env;
287
288                 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
289                 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
290
291                 /* set the current graph (this is important for several firm functions) */
292                 current_ir_graph = birg.irg;
293
294                 /* Get the code generator interface. */
295                 cg_if = isa->impl->get_code_generator_if(isa);
296
297                 /* get a code generator for this graph. */
298                 birg.cg = cg_if->init(&birg);
299
300                 /* create the code generator and generate code. */
301                 prepare_graph(&birg);
302
303                 /* some transformations need to be done before abi introduce */
304                 arch_code_generator_before_abi(birg.cg);
305
306                 /* implement the ABI conventions. */
307                 birg.abi = be_abi_introduce(&birg);
308                 dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
309
310                 /* generate code */
311                 arch_code_generator_prepare_graph(birg.cg);
312
313                 /*
314                  * Since the code generator made a lot of new nodes and skipped
315                  * a lot of old ones, we should do dead node elimination here.
316                  * Note that this requires disabling the edges here.
317                  */
318                 edges_deactivate(irg);
319                 dead_node_elimination(irg);
320                 edges_activate(irg);
321
322                 /* Compute loop nesting information (for weighting copies) */
323                 construct_cf_backedges(irg);
324
325                 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
326
327                 /* add Keeps for should_be_different constrained nodes */
328                 assure_constraints(&birg);
329                 dump(DUMP_PREPARED, irg, "-assured", dump_ir_block_graph);
330
331                 /* Schedule the graphs. */
332                 arch_code_generator_before_sched(birg.cg);
333                 list_sched(env.arch_env, irg);
334
335                 /* connect all stack modifying nodes together (see beabi.c) */
336                 be_abi_fix_stack_nodes(birg.abi);
337                 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
338
339                 /* Verify the schedule */
340                 sched_verify_irg(irg);
341
342                 /* do some statistics */
343                 be_do_stat_reg_pressure(&birg);
344
345                 /* Do register allocation */
346                 arch_code_generator_before_ra(birg.cg);
347                 ra->allocate(&birg);
348                 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
349
350                 arch_code_generator_after_ra(birg.cg);
351                 be_abi_fix_stack_bias(birg.abi);
352
353                 arch_code_generator_done(birg.cg);
354                 dump(DUMP_FINAL, irg, "-end", dump_ir_block_graph_sched);
355                 be_abi_free(birg.abi);
356
357 //              free_ir_graph(irg);
358         }
359
360         be_done_env(&env);
361 }
362
363 /* Main interface to the frontend. */
364 void be_main(FILE *file_handle)
365 {
366         /* never build code for pseudo irgs */
367         set_visit_pseudo_irgs(0);
368
369         be_node_init();
370         be_main_loop(file_handle);
371 }
372
373 /** The debug info retriever function. */
374 static retrieve_dbg_func retrieve_dbg = NULL;
375
376 /* Sets a debug info retriever. */
377 void be_set_debug_retrieve(retrieve_dbg_func func) {
378         retrieve_dbg = func;
379 }
380
381 /* Retrieve the debug info. */
382 const char *be_retrieve_dbg_info(const dbg_info *dbg, unsigned *line) {
383         if (retrieve_dbg)
384                 return retrieve_dbg(dbg, line);
385         *line = 0;
386         return NULL;
387 }