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