fixed struct initializer
[libfirm] / ir / be / bemain.c
1 /**
2  * Backend driver.
3  * @author Sebastian Hack
4  * @date 25.11.2004
5  * $Id$
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <stdarg.h>
12
13 #ifdef WITH_LIBCORE
14 #include <libcore/lc_opts.h>
15 #include <libcore/lc_opts_enum.h>
16 #endif /* WITH_LIBCORE */
17
18 #include "obst.h"
19 #include "bitset.h"
20
21 #include "irprog.h"
22 #include "irgopt.h"
23 #include "irgraph.h"
24 #include "irdump.h"
25 #include "phiclass.h"
26 #include "irdom_t.h"
27 #include "iredges_t.h"
28 #include "irloop_t.h"
29 #include "irtools.h"
30 #include "return.h"
31
32 #include "bearch.h"
33 #include "firm/bearch_firm.h"
34 #include "ia32/bearch_ia32.h"
35 #include "arm/bearch_arm.h"
36 #include "ppc32/bearch_ppc32.h"
37 #include "mips/bearch_mips.h"
38
39 #include "be_t.h"
40 #include "benumb_t.h"
41 #include "beutil.h"
42 #include "benode_t.h"
43 #include "beirgmod.h"
44 #include "besched_t.h"
45 #include "belistsched.h"
46 #include "belive_t.h"
47 #include "bespillilp.h"
48 #include "bespillbelady.h"
49 #include "bera.h"
50 #include "beraextern.h"
51 #include "bechordal_t.h"
52 #include "beifg.h"
53 #include "beifg_impl.h"
54 #include "becopyopt.h"
55 #include "becopystat.h"
56 #include "bessadestr.h"
57 #include "beabi.h"
58 #include "belower.h"
59 #include "beschedmris.h"
60 #include "bestat.h"
61 #include "beverify.h"
62
63 #define DUMP_INITIAL    (1 << 0)
64 #define DUMP_ABI        (1 << 1)
65 #define DUMP_SCHED      (1 << 2)
66 #define DUMP_PREPARED   (1 << 3)
67 #define DUMP_RA         (1 << 4)
68 #define DUMP_FINAL      (1 << 5)
69
70 enum {
71         BE_VRFY_OFF,
72         BE_VRFY_WARN,
73         BE_VRFY_ASSERT
74 };
75
76 /* options visible for anyone */
77 static be_options_t be_options = {
78         /* ilp server */
79         "i44pc52.info.uni-karlsruhe.de",
80
81         /* ilp solver */
82         "cplex"
83 };
84
85 /* dump flags */
86 static unsigned dump_flags = 0;
87
88 /* verify options */
89 static unsigned vrfy_option = BE_VRFY_WARN;
90
91 /* register allocator to use. */
92 static const be_ra_t *ra = &be_ra_chordal_allocator;
93
94 /* back end instruction set architecture to use */
95 static const arch_isa_if_t *isa_if = &ia32_isa_if;
96
97 /* mris option */
98 static int be_enable_mris = 0;
99
100 #ifdef WITH_LIBCORE
101
102 static lc_opt_entry_t *be_grp_root = NULL;
103
104 /* possible dumping options */
105 static const lc_opt_enum_mask_items_t dump_items[] = {
106         { "none",       0 },
107         { "initial",    DUMP_INITIAL },
108         { "abi",        DUMP_ABI    },
109         { "sched",      DUMP_SCHED  },
110         { "prepared",   DUMP_PREPARED },
111         { "regalloc",   DUMP_RA },
112         { "final",      DUMP_FINAL },
113         { "all",        2 * DUMP_FINAL - 1 },
114         { NULL,         0 }
115 };
116
117 /* register allocators */
118 static const lc_opt_enum_const_ptr_items_t ra_items[] = {
119         { "chordal", &be_ra_chordal_allocator },
120         { "external", &be_ra_external_allocator },
121         { NULL,      NULL }
122 };
123
124 /* instruction set architectures. */
125 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
126         { "ia32",    &ia32_isa_if },
127 #if 0
128         { "arm",     &arm_isa_if },
129         { "ppc32",   &ppc32_isa_if },
130         { "mips",    &mips_isa_if },
131 #endif
132         { NULL,      NULL }
133 };
134
135 /* verify options. */
136 static const lc_opt_enum_int_items_t vrfy_items[] = {
137         { "off",    BE_VRFY_OFF    },
138         { "warn",   BE_VRFY_WARN   },
139         { "assert", BE_VRFY_ASSERT },
140         { NULL,     0 }
141 };
142 static lc_opt_enum_mask_var_t dump_var = {
143         &dump_flags, dump_items
144 };
145
146 static lc_opt_enum_const_ptr_var_t ra_var = {
147         (const void **) &ra, ra_items
148 };
149
150 static lc_opt_enum_const_ptr_var_t isa_var = {
151         (const void **) &isa_if, isa_items
152 };
153
154 static lc_opt_enum_int_var_t vrfy_var = {
155         &vrfy_option, vrfy_items
156 };
157
158 static const lc_opt_table_entry_t be_main_options[] = {
159         LC_OPT_ENT_ENUM_MASK("dump",     "dump irg on several occasions",     &dump_var),
160         LC_OPT_ENT_ENUM_PTR ("ra",       "register allocator",                &ra_var),
161         LC_OPT_ENT_ENUM_PTR ("isa",      "the instruction set architecture",  &isa_var),
162         LC_OPT_ENT_NEGBOOL  ("noomitfp", "do not omit frame pointer",         &be_omit_fp),
163         LC_OPT_ENT_BOOL     ("mris",     "enable mris schedule preparation",  &be_enable_mris),
164         LC_OPT_ENT_ENUM_PTR ("vrfy",     "verify the backend irg (off, warn, assert)",  &vrfy_var),
165
166 #ifdef WITH_ILP
167         LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
168         LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
169 #endif /* WITH_ILP */
170         { NULL }
171 };
172
173 #endif /* WITH_LIBCORE */
174
175 void be_opt_register(void)
176 {
177 #ifdef WITH_LIBCORE
178         int i;
179         lc_opt_entry_t *be_grp_ra;
180         static int run_once = 0;
181
182         if (! run_once) {
183                 run_once    = 1;
184                 be_grp_root = lc_opt_get_grp(firm_opt_get_root(), "be");
185                 be_grp_ra   = lc_opt_get_grp(be_grp_root, "ra");
186
187                 lc_opt_add_table(be_grp_root, be_main_options);
188
189                 /* register allocator options */
190                 for(i = 0; ra_items[i].name != NULL; ++i) {
191                         const be_ra_t *ra = ra_items[i].value;
192                         ra->register_options(be_grp_ra);
193                 }
194
195                 /* register isa options */
196                 for(i = 0; isa_items[i].name != NULL; ++i) {
197                         const arch_isa_if_t *isa = isa_items[i].value;
198                         isa->register_options(be_grp_root);
199                 }
200         }
201 #endif /* WITH_LIBCORE */
202 }
203
204 /* Parse one argument. */
205 int be_parse_arg(const char *arg) {
206 #ifdef WITH_LIBCORE
207         if (strcmp(arg, "help") == 0 || (arg[0] == '?' && arg[1] == '\0')) {
208                 lc_opt_print_help(be_grp_root, stdout);
209                 return -1;
210         }
211         return lc_opt_from_single_arg(be_grp_root, NULL, arg, NULL);
212 #else
213         return 0;
214 #endif /* WITH_LIBCORE */
215 }
216
217 /** The be parameters returned by default, all off. */
218 const static backend_params be_params = {
219         NULL,
220         NULL,
221         0,
222         NULL,
223 };
224
225 /* Perform schedule verification if requested. */
226 static void be_sched_vrfy(ir_graph *irg, int vrfy_opt) {
227         if (vrfy_opt == BE_VRFY_WARN) {
228                 be_verify_schedule(irg);
229         }
230         else if (vrfy_opt == BE_VRFY_ASSERT) {
231                 assert(be_verify_schedule(irg) && "Schedule verification failed.");
232         }
233 }
234
235 /* Initialize the Firm backend. Must be run BEFORE init_firm()! */
236 const backend_params *be_init(void)
237 {
238         be_opt_register();
239
240         be_sched_init();
241         be_liveness_init();
242         be_numbering_init();
243         be_copy_opt_init();
244         copystat_init();
245         phi_class_init();
246
247         if (isa_if->get_params)
248                 return isa_if->get_params();
249         return &be_params;
250 }
251
252 static be_main_env_t *be_init_env(be_main_env_t *env, FILE *file_handle)
253 {
254         memset(env, 0, sizeof(*env));
255         obstack_init(&env->obst);
256         env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
257         env->options  = &be_options;
258         FIRM_DBG_REGISTER(env->dbg, "be.main");
259
260         arch_env_init(env->arch_env, isa_if, file_handle);
261
262         /* Register the irn handler of the architecture */
263         if (arch_isa_get_irn_handler(env->arch_env->isa))
264                 arch_env_push_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
265
266         /*
267          * Register the node handler of the back end infrastructure.
268          * This irn handler takes care of the platform independent
269          * spill, reload and perm nodes.
270          */
271         arch_env_push_irn_handler(env->arch_env, &be_node_irn_handler);
272         env->phi_handler = be_phi_handler_new(env->arch_env);
273         arch_env_push_irn_handler(env->arch_env, env->phi_handler);
274
275         return env;
276 }
277
278 static void be_done_env(be_main_env_t *env)
279 {
280         env->arch_env->isa->impl->done(env->arch_env->isa);
281         be_phi_handler_free(env->phi_handler);
282         obstack_free(&env->obst, NULL);
283 }
284
285 static void dump(int mask, ir_graph *irg, const char *suffix,
286                  void (*dumper)(ir_graph *, const char *))
287 {
288         if(dump_flags & mask)
289                 be_dump(irg, suffix, dumper);
290 }
291
292 /**
293  * Prepare a backend graph for code generation.
294  */
295 static void prepare_graph(be_irg_t *birg)
296 {
297         ir_graph *irg = birg->irg;
298
299         /* Normalize proj nodes. */
300         normalize_proj_nodes(irg);
301
302         /* Make just one return node. */
303         normalize_one_return(irg);
304
305         /* Remove critical edges */
306         remove_critical_cf_edges(irg);
307
308         /* Compute the dominance information. */
309         free_dom(irg);
310         compute_doms(irg);
311
312         /* Ensure, that the ir_edges are computed. */
313         edges_activate(irg);
314
315         /* check, if the dominance property is fulfilled. */
316         be_check_dominance(irg);
317
318         /* reset the phi handler. */
319         be_phi_handler_reset(birg->main_env->phi_handler);
320 }
321
322 /**
323  * The Firm backend main loop.
324  * Do architecture specific lowering for all graphs
325  * and call the architecture specific code generator.
326  */
327 static void be_main_loop(FILE *file_handle)
328 {
329         int i, n;
330         arch_isa_t *isa;
331         be_main_env_t env;
332
333         be_init_env(&env, file_handle);
334
335         isa = arch_env_get_isa(env.arch_env);
336
337         // /* for debugging, anchors helps */
338         // dump_all_anchors(1);
339
340         /* For all graphs */
341         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
342                 ir_graph *irg = get_irp_irg(i);
343                 const arch_code_generator_if_t *cg_if;
344                 be_irg_t birg;
345                 int save_optimize, save_normalize;
346
347                 birg.irg      = irg;
348                 birg.main_env = &env;
349
350                 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
351                 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
352
353                 be_stat_init_irg(env.arch_env, irg);
354                 be_do_stat_nodes(irg, "01 Begin");
355
356                 /* set the current graph (this is important for several firm functions) */
357                 current_ir_graph = birg.irg;
358
359                 /* Get the code generator interface. */
360                 cg_if = isa->impl->get_code_generator_if(isa);
361
362                 /* get a code generator for this graph. */
363                 birg.cg = cg_if->init(&birg);
364
365                 /* create the code generator and generate code. */
366                 prepare_graph(&birg);
367
368                 /* some transformations need to be done before abi introduce */
369                 arch_code_generator_before_abi(birg.cg);
370
371                 /* implement the ABI conventions. */
372                 birg.abi = be_abi_introduce(&birg);
373                 dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
374
375                 be_do_stat_nodes(irg, "02 Abi");
376
377                 /* generate code */
378                 arch_code_generator_prepare_graph(birg.cg);
379
380                 be_do_stat_nodes(irg, "03 Prepare");
381
382                 /*
383                  * Since the code generator made a lot of new nodes and skipped
384                  * a lot of old ones, we should do dead node elimination here.
385                  * Note that this requires disabling the edges here.
386                  */
387                 edges_deactivate(irg);
388                 //dead_node_elimination(irg);
389                 edges_activate(irg);
390
391                 /* Compute loop nesting information (for weighting copies) */
392                 construct_cf_backedges(irg);
393
394                 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
395
396                 /* Schedule the graphs. */
397                 arch_code_generator_before_sched(birg.cg);
398                 list_sched(&birg, be_enable_mris);
399                 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
400
401                 /* check schedule */
402                 be_sched_vrfy(birg.irg, vrfy_option);
403
404                 be_do_stat_nodes(irg, "04 Schedule");
405
406                 /* we switch off optimizations here, because they might cause trouble */
407                 save_optimize  = get_optimize();
408                 save_normalize = get_opt_normalize();
409                 set_optimize(0);
410                 set_opt_normalize(0);
411
412                 /* add Keeps for should_be_different constrained nodes  */
413                 /* beware: needs schedule due to usage of be_ssa_constr */
414                 assure_constraints(&birg);
415                 dump(DUMP_SCHED, irg, "-assured", dump_ir_block_graph_sched);
416
417                 be_do_stat_nodes(irg, "05 Constraints");
418
419                 /* connect all stack modifying nodes together (see beabi.c) */
420                 be_abi_fix_stack_nodes(birg.abi);
421                 dump(DUMP_SCHED, irg, "-fix_stack", dump_ir_block_graph_sched);
422
423                 /* check schedule */
424                 be_sched_vrfy(birg.irg, vrfy_option);
425
426                 /* do some statistics */
427                 be_do_stat_reg_pressure(&birg);
428
429                 /* Do register allocation */
430                 arch_code_generator_before_ra(birg.cg);
431                 ra->allocate(&birg);
432                 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
433
434                 be_do_stat_nodes(irg, "06 Register Allocation");
435
436                 arch_code_generator_after_ra(birg.cg);
437                 be_abi_fix_stack_bias(birg.abi);
438
439                 /* check schedule */
440                 be_sched_vrfy(birg.irg, vrfy_option);
441
442                 arch_code_generator_done(birg.cg);
443                 dump(DUMP_FINAL, irg, "-end", dump_ir_extblock_graph_sched);
444                 be_abi_free(birg.abi);
445
446                 be_do_stat_nodes(irg, "07 Final");
447
448                 /* reset the optimizations */
449                 set_optimize(save_optimize);
450                 set_opt_normalize(save_normalize);
451
452                 /* switched off due to statistics (statistic module needs all irgs) */
453                 //              free_ir_graph(irg);
454         }
455         be_done_env(&env);
456 }
457
458 /* Main interface to the frontend. */
459 void be_main(FILE *file_handle)
460 {
461         /* never build code for pseudo irgs */
462         set_visit_pseudo_irgs(0);
463
464         be_node_init();
465         be_main_loop(file_handle);
466 }
467
468 /** The debug info retriever function. */
469 static retrieve_dbg_func retrieve_dbg = NULL;
470
471 /* Sets a debug info retriever. */
472 void be_set_debug_retrieve(retrieve_dbg_func func) {
473         retrieve_dbg = func;
474 }
475
476 /* Retrieve the debug info. */
477 const char *be_retrieve_dbg_info(const dbg_info *dbg, unsigned *line) {
478         if (retrieve_dbg)
479                 return retrieve_dbg(dbg, line);
480         *line = 0;
481         return NULL;
482 }