8530d00b027c02968c04272c69fb7dc37c48b64b
[cparser] / driver / firm_opt.c
1 /**
2  *
3  * @file firm_opt.c -- Firm-generating back end optimizations.
4  *
5  * (C) 2005-2010  Michael Beck   beck@ipd.info.uni-karlsruhe.de
6  *
7  * $Id$
8  */
9
10 #include <config.h>
11
12 #include <stdbool.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdbool.h>
16 #include <assert.h>
17 #include <libfirm/firm.h>
18
19 #include "firm_opt.h"
20 #include "firm_codegen.h"
21 #include "firm_cmdline.h"
22 #include "firm_timing.h"
23 #include "ast2firm.h"
24
25 static ir_timer_t *t_vcg_dump;
26 static ir_timer_t *t_verify;
27 static ir_timer_t *t_all_opt;
28 static bool do_irg_opt(ir_graph *irg, const char *name);
29
30 /** dump all the graphs depending on cond */
31
32 static void dump_all(const char *suffix)
33 {
34         if (firm_dump.ir_graph) {
35                 timer_push(t_vcg_dump);
36                 if (firm_dump.no_blocks)
37                         dump_all_ir_graphs(dump_ir_graph, suffix);
38                 else if (firm_dump.extbb)
39                         dump_all_ir_graphs(dump_ir_extblock_graph, suffix);
40                 else
41                         dump_all_ir_graphs(dump_ir_block_graph, suffix);
42                 timer_pop(t_vcg_dump);
43         }
44 }
45
46 /* set by the backend parameters */
47 static const ir_settings_arch_dep_t *ad_param              = NULL;
48 static create_intrinsic_fkt         *arch_create_intrinsic = NULL;
49 static void                         *create_intrinsic_ctx  = NULL;
50 static const ir_settings_if_conv_t  *if_conv_info          = NULL;
51
52 /* entities of runtime functions */
53 ir_entity_ptr rts_entities[rts_max];
54
55 /**
56  * factory for setting architecture dependent parameters
57  */
58 static const ir_settings_arch_dep_t *arch_factory(void)
59 {
60         static const ir_settings_arch_dep_t param = {
61                 1,   /* also use subs */
62                 4,   /* maximum shifts */
63                 31,   /* maximum shift amount */
64                 NULL, /* use default evaluator */
65
66                 1, /* allow Mulhs */
67                 1, /* allow Mulus */
68                 32  /* Mulh allowed up to 32 bit */
69         };
70
71         return ad_param ? ad_param : &param;
72 }
73
74 /**
75  * Map runtime functions.
76  */
77 static void rts_map(void)
78 {
79         static const struct {
80                 ir_entity_ptr *ent; /**< address of the rts entity */
81                 i_mapper_func func; /**< mapper function. */
82         } mapper[] = {
83                 /* integer */
84                 { &rts_entities[rts_abs],     i_mapper_abs },
85                 { &rts_entities[rts_labs],    i_mapper_abs },
86                 { &rts_entities[rts_llabs],   i_mapper_abs },
87                 { &rts_entities[rts_imaxabs], i_mapper_abs },
88
89                 /* double -> double */
90                 { &rts_entities[rts_fabs],    i_mapper_abs },
91                 { &rts_entities[rts_sqrt],    i_mapper_sqrt },
92                 { &rts_entities[rts_cbrt],    i_mapper_cbrt },
93                 { &rts_entities[rts_pow],     i_mapper_pow },
94                 { &rts_entities[rts_exp],     i_mapper_exp },
95                 { &rts_entities[rts_exp2],    i_mapper_exp },
96                 { &rts_entities[rts_exp10],   i_mapper_exp },
97                 { &rts_entities[rts_log],     i_mapper_log },
98                 { &rts_entities[rts_log2],    i_mapper_log2 },
99                 { &rts_entities[rts_log10],   i_mapper_log10 },
100                 { &rts_entities[rts_sin],     i_mapper_sin },
101                 { &rts_entities[rts_cos],     i_mapper_cos },
102                 { &rts_entities[rts_tan],     i_mapper_tan },
103                 { &rts_entities[rts_asin],    i_mapper_asin },
104                 { &rts_entities[rts_acos],    i_mapper_acos },
105                 { &rts_entities[rts_atan],    i_mapper_atan },
106                 { &rts_entities[rts_sinh],    i_mapper_sinh },
107                 { &rts_entities[rts_cosh],    i_mapper_cosh },
108                 { &rts_entities[rts_tanh],    i_mapper_tanh },
109
110                 /* float -> float */
111                 { &rts_entities[rts_fabsf],   i_mapper_abs },
112                 { &rts_entities[rts_sqrtf],   i_mapper_sqrt },
113                 { &rts_entities[rts_cbrtf],   i_mapper_cbrt },
114                 { &rts_entities[rts_powf],    i_mapper_pow },
115                 { &rts_entities[rts_expf],    i_mapper_exp },
116                 { &rts_entities[rts_exp2f],   i_mapper_exp },
117                 { &rts_entities[rts_exp10f],  i_mapper_exp },
118                 { &rts_entities[rts_logf],    i_mapper_log },
119                 { &rts_entities[rts_log2f],   i_mapper_log2 },
120                 { &rts_entities[rts_log10f],  i_mapper_log10 },
121                 { &rts_entities[rts_sinf],    i_mapper_sin },
122                 { &rts_entities[rts_cosf],    i_mapper_cos },
123                 { &rts_entities[rts_tanf],    i_mapper_tan },
124                 { &rts_entities[rts_asinf],   i_mapper_asin },
125                 { &rts_entities[rts_acosf],   i_mapper_acos },
126                 { &rts_entities[rts_atanf],   i_mapper_atan },
127                 { &rts_entities[rts_sinhf],   i_mapper_sinh },
128                 { &rts_entities[rts_coshf],   i_mapper_cosh },
129                 { &rts_entities[rts_tanhf],   i_mapper_tanh },
130
131                 /* long double -> long double */
132                 { &rts_entities[rts_fabsl],   i_mapper_abs },
133                 { &rts_entities[rts_sqrtl],   i_mapper_sqrt },
134                 { &rts_entities[rts_cbrtl],   i_mapper_cbrt },
135                 { &rts_entities[rts_powl],    i_mapper_pow },
136                 { &rts_entities[rts_expl],    i_mapper_exp },
137                 { &rts_entities[rts_exp2l],   i_mapper_exp },
138                 { &rts_entities[rts_exp10l],  i_mapper_exp },
139                 { &rts_entities[rts_logl],    i_mapper_log },
140                 { &rts_entities[rts_log2l],   i_mapper_log2 },
141                 { &rts_entities[rts_log10l],  i_mapper_log10 },
142                 { &rts_entities[rts_sinl],    i_mapper_sin },
143                 { &rts_entities[rts_cosl],    i_mapper_cos },
144                 { &rts_entities[rts_tanl],    i_mapper_tan },
145                 { &rts_entities[rts_asinl],   i_mapper_asin },
146                 { &rts_entities[rts_acosl],   i_mapper_acos },
147                 { &rts_entities[rts_atanl],   i_mapper_atan },
148                 { &rts_entities[rts_sinhl],   i_mapper_sinh },
149                 { &rts_entities[rts_coshl],   i_mapper_cosh },
150                 { &rts_entities[rts_tanhl],   i_mapper_tanh },
151
152                 /* string */
153                 { &rts_entities[rts_strcmp],  i_mapper_strcmp },
154                 { &rts_entities[rts_strncmp], i_mapper_strncmp },
155                 { &rts_entities[rts_strcpy],  i_mapper_strcpy },
156                 { &rts_entities[rts_strlen],  i_mapper_strlen },
157                 { &rts_entities[rts_memcpy],  i_mapper_memcpy },
158                 { &rts_entities[rts_mempcpy], i_mapper_mempcpy },
159                 { &rts_entities[rts_memmove], i_mapper_memmove },
160                 { &rts_entities[rts_memset],  i_mapper_memset },
161                 { &rts_entities[rts_memcmp],  i_mapper_memcmp }
162         };
163         i_record rec[sizeof(mapper)/sizeof(mapper[0])];
164         unsigned i, n_map;
165
166         for (i = n_map = 0; i < sizeof(mapper)/sizeof(mapper[0]); ++i) {
167                 if (*mapper[i].ent != NULL) {
168                         rec[n_map].i_call.kind     = INTRINSIC_CALL;
169                         rec[n_map].i_call.i_ent    = *mapper[i].ent;
170                         rec[n_map].i_call.i_mapper = mapper[i].func;
171                         rec[n_map].i_call.ctx      = NULL;
172                         rec[n_map].i_call.link     = NULL;
173                         ++n_map;
174                 }
175         }
176
177         if (n_map > 0)
178                 lower_intrinsics(rec, n_map, /* part_block_used=*/0);
179 }
180
181 static int *irg_dump_no;
182
183 static void dump_graph_count(ir_graph *const irg, const char *const suffix)
184 {
185         char name[64];
186         snprintf(name, sizeof(name), "-%02d_%s", irg_dump_no[get_irg_idx(irg)]++,
187                  suffix);
188
189         timer_push(t_vcg_dump);
190         if (firm_dump.no_blocks)
191                 dump_ir_graph(irg, name);
192         else if (firm_dump.extbb)
193                 dump_ir_extblock_graph(irg, name);
194         else
195                 dump_ir_block_graph(irg, name);
196         timer_pop(t_vcg_dump);
197 }
198
199 static int firm_const_exists;
200
201 static void do_optimize_funccalls(void)
202 {
203         optimize_funccalls(firm_const_exists, NULL);
204 }
205
206 static void do_gcse(ir_graph *irg)
207 {
208         set_opt_global_cse(1);
209         optimize_graph_df(irg);
210         place_code(irg);
211         set_opt_global_cse(0);
212 }
213
214 static void do_lower_highlevel(ir_graph *irg)
215 {
216         lower_highlevel_graph(irg, firm_opt.lower_bitfields);
217 }
218
219 static void do_if_conv(ir_graph *irg)
220 {
221         opt_if_conv(irg, if_conv_info);
222 }
223
224 static void do_stred(ir_graph *irg)
225 {
226         opt_osr(irg, osr_flag_default | osr_flag_keep_reg_pressure | osr_flag_ignore_x86_shift);
227 }
228
229 static void after_inline_opt(ir_graph *irg)
230 {
231         do_irg_opt(irg, "scalar-replace");
232         do_irg_opt(irg, "local");
233         do_irg_opt(irg, "control-flow");
234         do_irg_opt(irg, "combo");
235 }
236
237 static void do_inline(void)
238 {
239         inline_functions(firm_opt.inline_maxsize, firm_opt.inline_threshold,
240                          after_inline_opt);
241 }
242
243 static void do_cloning(void)
244 {
245         proc_cloning((float) firm_opt.clone_threshold);
246 }
247
248 static void do_lower_switch(ir_graph *irg)
249 {
250         lower_switch(irg, firm_opt.spare_size);
251 }
252
253 static void do_lower_mux(ir_graph *irg)
254 {
255         lower_mux(irg, NULL);
256 }
257
258 static void do_lower_dw_ops(void)
259 {
260         lwrdw_param_t init = {
261                 1,
262                 1,
263                 get_atomic_mode(ATOMIC_TYPE_LONGLONG),
264                 get_atomic_mode(ATOMIC_TYPE_ULONGLONG),
265                 get_atomic_mode(ATOMIC_TYPE_INT),
266                 get_atomic_mode(ATOMIC_TYPE_UINT),
267                 def_create_intrinsic_fkt,
268                 NULL
269         };
270
271         if (arch_create_intrinsic) {
272                 init.create_intrinsic = arch_create_intrinsic;
273                 init.ctx              = create_intrinsic_ctx;
274         }
275         lower_dw_ops(&init);
276 }
277
278 static void do_vrp(ir_graph *irg)
279 {
280         set_vrp_data(irg);
281 }
282
283 typedef enum opt_target {
284         OPT_TARGET_IRG, /**< optimization function works on a single graph */
285         OPT_TARGET_IRP  /**< optimization function works on the complete program */
286 } opt_target_t;
287
288 typedef enum opt_flags {
289         OPT_FLAG_NONE         = 0,
290         OPT_FLAG_ENABLED      = 1 << 0, /**< enable the optimization */
291         OPT_FLAG_NO_DUMP      = 1 << 1, /**< don't dump after transformation */
292         OPT_FLAG_NO_VERIFY    = 1 << 2, /**< don't verify after transformation */
293         OPT_FLAG_HIDE_OPTIONS = 1 << 3, /**< do not automatically process
294                                              -foptions for this transformation */
295         OPT_FLAG_ESSENTIAL    = 1 << 4, /**< output won't work without this pass
296                                              so we need it even with -O0 */
297 } opt_flags_t;
298
299 typedef void (*transform_irg_func)(ir_graph *irg);
300 typedef void (*transform_irp_func)(void);
301
302 typedef struct {
303         opt_target_t  target;
304         const char   *name;
305         union {
306                 transform_irg_func transform_irg;
307                 transform_irp_func transform_irp;
308         } u;
309         const char   *description;
310         opt_flags_t   flags;
311 } opt_config_t;
312
313 static opt_config_t opts[] = {
314 #define IRG(a, b, c, d) { OPT_TARGET_IRG, a, .u.transform_irg = (transform_irg_func)b, c, d }
315 #define IRP(a, b, c, d) { OPT_TARGET_IRP, a, .u.transform_irp = b,                     c, d }
316         IRG("bool",              opt_bool,                 "bool simplification",                                   OPT_FLAG_NONE),
317         IRG("combo",             combo,                    "combined CCE, UCE and GVN",                             OPT_FLAG_NONE),
318         IRG("confirm",           construct_confirms,       "confirm optimisation",                                  OPT_FLAG_HIDE_OPTIONS),
319         IRG("control-flow",      optimize_cf,              "optimization of control-flow",                          OPT_FLAG_HIDE_OPTIONS),
320         IRG("dead",              dead_node_elimination,    "dead node elimination",                                 OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
321         IRG("deconv",            conv_opt,                 "conv node elimination",                                 OPT_FLAG_NONE),
322         IRG("fp-vrp",            fixpoint_vrp,             "fixpoint value range propagation",                      OPT_FLAG_NONE),
323         IRG("frame",             opt_frame_irg,            "remove unused frame entities",                          OPT_FLAG_NONE),
324         IRG("gcse",              do_gcse,                  "global common subexpression elimination",               OPT_FLAG_NONE),
325         IRG("gvn-pre",           do_gvn_pre,               "global value numbering partial redundancy elimination", OPT_FLAG_NONE),
326         IRG("if-conversion",     do_if_conv,               "if-conversion",                                         OPT_FLAG_NONE),
327         IRG("invert-loops",      do_loop_inversion,        "loop inversion",                                        OPT_FLAG_NONE),
328         IRG("ivopts",            do_stred,                 "induction variable strength reduction",                 OPT_FLAG_NONE),
329         IRG("local",             optimize_graph_df,        "local graph optimizations",                             OPT_FLAG_HIDE_OPTIONS),
330         IRG("lower",             do_lower_highlevel,       "lowering",                                              OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
331         IRG("lower-mux",         do_lower_mux,             "mux lowering",                                          OPT_FLAG_NONE),
332         IRG("lower-switch",      do_lower_switch,          "switch lowering",                                       OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
333         IRG("one-return",        normalize_one_return,     "normalisation to 1 return",                             OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
334         IRG("opt-load-store",    optimize_load_store,      "load store optimization",                               OPT_FLAG_NONE),
335         IRG("opt-tail-rec",      opt_tail_rec_irg,         "tail-recursion eliminiation",                           OPT_FLAG_NONE),
336         IRG("parallelize-mem",   opt_parallelize_mem,      "parallelize memory",                                    OPT_FLAG_NONE),
337         IRG("place",             place_code,               "code placement",                                        OPT_FLAG_NONE),
338         IRG("reassociation",     optimize_reassociation,   "reassociation",                                         OPT_FLAG_NONE),
339         IRG("remove-confirms",   remove_confirms,          "confirm removal",                                       OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
340         IRG("remove-phi-cycles", remove_phi_cycles,        "removal of phi cycles",                                 OPT_FLAG_HIDE_OPTIONS),
341         IRG("scalar-replace",    scalar_replacement_opt,   "scalar replacement",                                    OPT_FLAG_NONE),
342         IRG("shape-blocks",      shape_blocks,             "block shaping",                                         OPT_FLAG_NONE),
343         IRG("thread-jumps",      opt_jumpthreading,        "path-sensitive jumpthreading",                          OPT_FLAG_NONE),
344         IRG("unroll-loops",      do_loop_unrolling,        "loop unrolling",                                        OPT_FLAG_NONE),
345         IRG("vrp",               do_vrp,                   "value range propagation",                               OPT_FLAG_NONE),
346         IRP("inline",            do_inline,                "inlining",                                              OPT_FLAG_NONE),
347         IRP("lower-const",       lower_const_code,         "lowering of constant code",                             OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY | OPT_FLAG_ESSENTIAL),
348         IRP("lower-dw",          do_lower_dw_ops,          "lowering of doubleword operations",                     OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
349         IRP("opt-func-call",     do_optimize_funccalls,    "function call optimization",                            OPT_FLAG_NONE),
350         IRP("opt-proc-clone",    do_cloning,               "procedure cloning",                                     OPT_FLAG_NONE),
351         IRP("remove-unused",     garbage_collect_entities, "removal of unused functions/variables",                 OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
352         IRP("rts",               rts_map,                  "optimization of known library functions",               OPT_FLAG_HIDE_OPTIONS),
353 #undef IRP
354 #undef IRG
355 };
356 static const int n_opts = sizeof(opts) / sizeof(opts[0]);
357 ir_timer_t *timers[sizeof(opts)/sizeof(opts[0])];
358
359 static opt_config_t *get_opt(const char *name)
360 {
361         int i;
362         for (i = 0; i < n_opts; ++i) {
363                 opt_config_t *config = &opts[i];
364                 if (strcmp(config->name, name) == 0)
365                         return config;
366         }
367
368         return NULL;
369 }
370
371 static void set_opt_enabled(const char *name, bool enabled)
372 {
373         opt_config_t *config = get_opt(name);
374         config->flags = (config->flags & ~OPT_FLAG_ENABLED)
375                 | (enabled ? OPT_FLAG_ENABLED : 0);
376 }
377
378 static bool get_opt_enabled(const char *name)
379 {
380         opt_config_t *config = get_opt(name);
381         return (config->flags & OPT_FLAG_ENABLED) != 0;
382 }
383
384 /**
385  * perform an optimisation on a single graph
386  *
387  * @return  true if something changed, false otherwise
388  */
389 static bool do_irg_opt(ir_graph *irg, const char *name)
390 {
391         ir_graph     *old_irg;
392         opt_config_t *config = get_opt(name);
393         size_t        n      = config - opts;
394         assert(config != NULL);
395         assert(config->target == OPT_TARGET_IRG);
396         if (! (config->flags & OPT_FLAG_ENABLED))
397                 return false;
398
399         old_irg          = current_ir_graph;
400         current_ir_graph = irg;
401
402         timer_push(timers[n]);
403         config->u.transform_irg(irg);
404         timer_pop(timers[n]);
405
406         if (firm_dump.all_phases && firm_dump.ir_graph) {
407                 dump_graph_count(irg, name);
408         }
409
410         if (firm_opt.check_all) {
411                 timer_push(t_verify);
412                 irg_verify(irg, VRFY_ENFORCE_SSA);
413                 timer_pop(t_verify);
414         }
415
416         current_ir_graph = old_irg;
417         return true;
418 }
419
420 static void do_irp_opt(const char *name)
421 {
422         opt_config_t *config = get_opt(name);
423         size_t        n      = config - opts;
424         assert(config->target == OPT_TARGET_IRP);
425         if (! (config->flags & OPT_FLAG_ENABLED))
426                 return;
427
428         timer_push(timers[n]);
429         config->u.transform_irp();
430         timer_pop(timers[n]);
431
432         if (firm_dump.ir_graph && firm_dump.all_phases) {
433                 int i;
434                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
435                         ir_graph *irg = get_irp_irg(i);
436                         dump_graph_count(irg, name);
437                 }
438         }
439
440         if (firm_opt.check_all) {
441                 int i;
442                 timer_push(t_verify);
443                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
444                         irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA);
445                 }
446                 timer_pop(t_verify);
447         }
448 }
449
450 /**
451  * Enable transformations which should be always safe (and cheap) to perform
452  */
453 static void enable_safe_defaults(void)
454 {
455         set_opt_enabled("remove-unused", true);
456         set_opt_enabled("opt-tail-rec", true);
457         set_opt_enabled("opt-func-call", true);
458         set_opt_enabled("reassociation", true);
459         set_opt_enabled("control-flow", true);
460         set_opt_enabled("local", true);
461         set_opt_enabled("lower-const", true);
462         set_opt_enabled("scalar-replace", true);
463         set_opt_enabled("place", true);
464         set_opt_enabled("confirm", true);
465         set_opt_enabled("opt-load-store", true);
466         set_opt_enabled("lower", true);
467         set_opt_enabled("deconv", true);
468         set_opt_enabled("remove-confirms", true);
469         set_opt_enabled("ivopts", true);
470         set_opt_enabled("dead", true);
471         set_opt_enabled("lower-switch", true);
472         set_opt_enabled("remove-phi-cycles", true);
473         set_opt_enabled("frame", true);
474         set_opt_enabled("combo", true);
475         set_opt_enabled("invert-loops", true);
476 }
477
478 /**
479  * run all the Firm optimizations
480  *
481  * @param input_filename     the name of the (main) source file
482  */
483 static void do_firm_optimizations(const char *input_filename)
484 {
485         int      i;
486         unsigned aa_opt;
487
488         set_opt_alias_analysis(firm_opt.alias_analysis);
489
490         aa_opt = aa_opt_no_opt;
491         if (firm_opt.strict_alias)
492                 aa_opt |= aa_opt_type_based | aa_opt_byte_type_may_alias;
493         if (firm_opt.no_alias)
494                 aa_opt = aa_opt_no_alias;
495
496         set_irp_memory_disambiguator_options(aa_opt);
497
498         /* parameter passing code should set them directly sometime... */
499         set_opt_enabled("rts", !firm_opt.freestanding);
500         set_opt_enabled("gcse", firm_opt.gcse);
501         set_opt_enabled("place", !firm_opt.gcse);
502         set_opt_enabled("confirm", firm_opt.confirm);
503         set_opt_enabled("remove-confirms", firm_opt.confirm);
504
505         /* osr supersedes remove_phi_cycles */
506         if (get_opt_enabled("ivopts"))
507                 set_opt_enabled("remove-phi-cycles", false);
508
509         timer_start(t_all_opt);
510
511         do_irp_opt("rts");
512
513         /* first step: kill dead code */
514         for (i = 0; i < get_irp_n_irgs(); i++) {
515                 ir_graph *irg = get_irp_irg(i);
516                 do_irg_opt(irg, "combo");
517                 do_irg_opt(irg, "local");
518                 do_irg_opt(irg, "control-flow");
519         }
520
521         do_irp_opt("remove-unused");
522         for (i = 0; i < get_irp_n_irgs(); ++i) {
523                 ir_graph *irg = get_irp_irg(i);
524                 do_irg_opt(irg, "opt-tail-rec");
525         }
526         do_irp_opt("opt-func-call");
527         do_irp_opt("lower-const");
528
529         for (i = 0; i < get_irp_n_irgs(); i++) {
530                 ir_graph *irg = get_irp_irg(i);
531
532                 do_irg_opt(irg, "scalar-replace");
533                 do_irg_opt(irg, "invert-loops");
534                 do_irg_opt(irg, "unroll-loops");
535                 do_irg_opt(irg, "local");
536                 do_irg_opt(irg, "reassociation");
537                 do_irg_opt(irg, "local");
538                 do_irg_opt(irg, "gcse");
539
540                 if (firm_opt.confirm) {
541                         /* Confirm construction currently can only handle blocks with only
542                            one control flow predecessor. Calling optimize_cf here removes
543                            Bad predecessors and help the optimization of switch constructs.
544                          */
545                         do_irg_opt(irg, "control-flow");
546                         do_irg_opt(irg, "confirm");
547                         do_irg_opt(irg, "vrp");
548                         do_irg_opt(irg, "local");
549                 }
550
551                 do_irg_opt(irg, "control-flow");
552                 do_irg_opt(irg, "opt-load-store");
553                 do_irg_opt(irg, "fp-vrp");
554                 do_irg_opt(irg, "lower");
555                 do_irg_opt(irg, "deconv");
556                 do_irg_opt(irg, "thread-jumps");
557                 do_irg_opt(irg, "remove-confirms");
558                 do_irg_opt(irg, "gvn-pre");
559                 do_irg_opt(irg, "place");
560                 do_irg_opt(irg, "control-flow");
561
562                 if (do_irg_opt(irg, "if-conversion")) {
563                         do_irg_opt(irg, "local");
564                         do_irg_opt(irg, "control-flow");
565                 }
566                 /* this doesn't make too much sense but tests the mux destruction... */
567                 do_irg_opt(irg, "lower-mux");
568
569                 do_irg_opt(irg, "bool");
570                 do_irg_opt(irg, "shape-blocks");
571                 do_irg_opt(irg, "lower-switch");
572                 do_irg_opt(irg, "ivopts");
573                 do_irg_opt(irg, "local");
574                 do_irg_opt(irg, "dead");
575                 do_irg_opt(irg, "frame");
576         }
577
578         do_irp_opt("inline");
579         do_irp_opt("opt-proc-clone");
580
581         for (i = 0; i < get_irp_n_irgs(); i++) {
582                 ir_graph *irg = get_irp_irg(i);
583                 do_irg_opt(irg, "local");
584                 do_irg_opt(irg, "control-flow");
585                 do_irg_opt(irg, "thread-jumps");
586                 do_irg_opt(irg, "local");
587                 do_irg_opt(irg, "control-flow");
588
589                 if( do_irg_opt(irg, "vrp") ) { // if vrp is enabled
590                         do_irg_opt(irg, "local");
591                         do_irg_opt(irg, "vrp");
592                         do_irg_opt(irg, "local");
593                         do_irg_opt(irg, "vrp");
594                 }
595         }
596
597         if (firm_dump.ir_graph) {
598                 /* recompute backedges for nicer dumps */
599                 for (i = 0; i < get_irp_n_irgs(); i++)
600                         construct_cf_backedges(get_irp_irg(i));
601         }
602
603         dump_all("-opt");
604
605         if (firm_dump.statistic & STAT_AFTER_OPT)
606                 stat_dump_snapshot(input_filename, "opt");
607
608         timer_stop(t_all_opt);
609 }
610
611 /**
612  * do Firm lowering
613  *
614  * @param input_filename  the name of the (main) source file
615  */
616 static void do_firm_lowering(const char *input_filename)
617 {
618         int i;
619
620         do_irp_opt("lower-dw");
621
622         if (firm_dump.statistic & STAT_AFTER_LOWER)
623                 stat_dump_snapshot(input_filename, "low");
624
625         dump_all("-low");
626
627         if (firm_opt.enabled) {
628                 timer_start(t_all_opt);
629
630                 /* run reassociation first on all graphs BEFORE the architecture
631                    dependent optimizations are enabled */
632                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
633                         ir_graph *irg = get_irp_irg(i);
634                         do_irg_opt(irg, "reassociation");
635                 }
636
637                 /* enable architecture dependent optimizations */
638                 arch_dep_set_opts((arch_dep_opts_t)
639                                 ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) |
640                                  (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) |
641                                  (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) ));
642
643                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
644                         ir_graph *irg = get_irp_irg(i);
645
646                         do_irg_opt(irg, "local");
647                         do_irg_opt(irg, "gcse");
648                         do_irg_opt(irg, "opt-load-store");
649                         do_irg_opt(irg, "local");
650                         do_irg_opt(irg, "control-flow");
651
652                         if (do_irg_opt(irg, "vrp")) {
653                                 do_irg_opt(irg, "local");
654                                 do_irg_opt(irg, "control-flow");
655                                 do_irg_opt(irg, "vrp");
656                                 do_irg_opt(irg, "local");
657                                 do_irg_opt(irg, "control-flow");
658                         }
659
660                         if (do_irg_opt(irg, "if-conversion")) {
661                                 do_irg_opt(irg, "local");
662                                 do_irg_opt(irg, "control-flow");
663                         }
664
665                         do_irg_opt(irg, "parallelize-mem");
666                 }
667                 timer_stop(t_all_opt);
668
669                 do_irp_opt("remove-unused");
670
671                 dump_all("-low-opt");
672         }
673
674         if (firm_opt.cc_opt)
675                 mark_private_methods();
676
677         /* set the phase to low */
678         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
679                 set_irg_phase_low(get_irp_irg(i));
680
681         /* all graphs are lowered, set the irp phase to low */
682         set_irp_phase_state(phase_low);
683
684         if (firm_dump.statistic & STAT_FINAL) {
685                 stat_dump_snapshot(input_filename, "final");
686         }
687 }
688
689 /**
690  * Initialize for the Firm-generating back end.
691  */
692 void gen_firm_init(void)
693 {
694         unsigned pattern = 0;
695         int      i;
696
697         for (i = 0; i < n_opts; ++i) {
698                 timers[i] = ir_timer_new();
699                 timer_register(timers[i], opts[i].description);
700         }
701         t_verify = ir_timer_new();
702         timer_register(t_verify, "Firm: verify pass");
703         t_vcg_dump = ir_timer_new();
704         timer_register(t_vcg_dump, "Firm: vcg dumping");
705         t_all_opt = ir_timer_new();
706         timer_register(t_all_opt, "Firm: all optimizations");
707
708         if (firm_dump.stat_pattern)
709                 pattern |= FIRMSTAT_PATTERN_ENABLED;
710
711         if (firm_dump.stat_dag)
712                 pattern |= FIRMSTAT_COUNT_DAG;
713
714         ir_init(NULL);
715         firm_init_stat(firm_dump.statistic == STAT_NONE ?
716                         0 : FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP
717                         | FIRMSTAT_COUNT_CONSTS | pattern);
718
719         if (firm_be_opt.selection == BE_FIRM_BE) {
720                 const backend_params *be_params = be_get_backend_param();
721
722                 if (be_params->do_dw_lowering)
723                         set_opt_enabled("lower-dw", true);
724
725                 arch_create_intrinsic   = be_params->arch_create_intrinsic_fkt;
726                 create_intrinsic_ctx    = be_params->create_intrinsic_ctx;
727
728                 ad_param                = be_params->dep_param;
729                 if_conv_info            = be_params->if_conv_info;
730         }
731
732         edges_init_dbg(firm_opt.vrfy_edges);
733
734         /* Sel node cannot produce NULL pointers */
735         set_opt_sel_based_null_check_elim(1);
736
737         /* dynamic dispatch works currently only if whole world scenarios */
738         set_opt_dyn_meth_dispatch(0);
739
740         arch_dep_init(arch_factory);
741
742         /* do not run architecture dependent optimizations in building phase */
743         arch_dep_set_opts(arch_dep_none);
744
745         do_node_verification((firm_verification_t) firm_opt.vrfy);
746         if (firm_dump.filter)
747                 only_dump_method_with_name(new_id_from_str(firm_dump.filter));
748
749         if (firm_opt.enabled) {
750                 set_optimize(1);
751                 set_opt_constant_folding(firm_opt.const_folding);
752                 set_opt_algebraic_simplification(firm_opt.const_folding);
753                 set_opt_cse(firm_opt.cse);
754                 set_opt_global_cse(0);
755                 set_opt_unreachable_code(1);
756                 set_opt_control_flow(firm_opt.control_flow);
757                 set_opt_control_flow_weak_simplification(1);
758                 set_opt_control_flow_strong_simplification(1);
759         } else {
760                 set_optimize(0);
761         }
762
763         /* do not dump entity ld names */
764         dump_ld_names(0);
765 }
766
767 /**
768  * Called, after the Firm generation is completed,
769  * do all optimizations and backend call here.
770  *
771  * @param out                a file handle for the output, may be NULL
772  * @param input_filename     the name of the (main) source file
773  * @param c_mode             non-zero if "C" was compiled
774  * @param new_firm_const_exists  non-zero, if the const attribute was used on functions
775  */
776 void gen_firm_finish(FILE *out, const char *input_filename, int c_mode,
777                      int new_firm_const_exists)
778 {
779         int i;
780
781 #if 0
782         if (firm_opt.enable_statev) {
783                 char buf[1024];
784                 snprintf(buf, sizeof(buf), "%s.ev", input_filename);
785                 ir_stat_ev_begin(input_filename, firm_opt.statev_filter);
786                 ir_stat_ev_compilation_unit(input_filename);
787         }
788 #endif
789
790         firm_const_exists = new_firm_const_exists;
791
792         /* the general for dumping option must be set, or the others will not work*/
793         firm_dump.ir_graph
794                 = (a_byte) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb);
795
796         dump_keepalive_edges(1);
797         dump_consts_local(1);
798         dump_dominator_information(1);
799         dump_loop_information(0);
800
801         if (!firm_dump.edge_labels)
802                 turn_off_edge_labels();
803
804         /* FIXME: cloning might ADD new graphs. */
805         irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
806
807         if (firm_dump.all_types) {
808                 dump_all_types("");
809                 if (! c_mode) {
810                         dump_class_hierarchy(0, "");
811                         dump_class_hierarchy(1, "-with-entities");
812                 }
813         }
814
815         /* finalize all graphs */
816         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
817                 ir_graph *irg = get_irp_irg(i);
818                 irg_finalize_cons(irg);
819         }
820         dump_all("");
821
822         timer_push(t_verify);
823         tr_vrfy();
824         timer_pop(t_verify);
825
826         /* all graphs are finalized, set the irp phase to high */
827         set_irp_phase_state(phase_high);
828
829         /* BEWARE: kill unreachable code before doing compound lowering */
830         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
831                 ir_graph *irg = get_irp_irg(i);
832                 do_irg_opt(irg, "control-flow");
833         }
834
835         /* lower all compound call return values */
836         lower_compound_params();
837
838         /* lower copyb nodes */
839         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
840                 ir_graph *irg = get_irp_irg(i);
841                 lower_CopyB(irg, 128, 4);
842         }
843
844         if (firm_dump.statistic & STAT_BEFORE_OPT) {
845                 stat_dump_snapshot(input_filename, "noopt");
846         }
847
848         if (firm_opt.enabled)
849                 do_firm_optimizations(input_filename);
850
851         if (firm_opt.lower)
852                 do_firm_lowering(input_filename);
853
854         /* set the phase to low */
855         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
856                 set_irg_phase_low(get_irp_irg(i));
857
858         if (firm_dump.statistic & STAT_FINAL_IR)
859                 stat_dump_snapshot(input_filename, "final-ir");
860
861         /* run the code generator */
862         if (firm_be_opt.selection != BE_NONE)
863                 do_codegen(out, input_filename);
864
865         if (firm_dump.statistic & STAT_FINAL)
866                 stat_dump_snapshot(input_filename, "final");
867 }
868
869 void disable_all_opts(void)
870 {
871         for (int i = 0; i < n_opts; ++i) {
872                 opt_config_t *config = &opts[i];
873                 if (config->flags & OPT_FLAG_ESSENTIAL) {
874                         config->flags |= OPT_FLAG_ENABLED;
875                 } else {
876                         config->flags &= ~OPT_FLAG_ENABLED;
877                 }
878         }
879 }
880
881 int firm_opt_option(const char *opt)
882 {
883         bool enable = true;
884         if (strncmp(opt, "no-", 3) == 0) {
885                 enable = false;
886                 opt = opt + 3;
887         }
888
889         opt_config_t *config = get_opt(opt);
890         if (config == NULL || (config->flags & OPT_FLAG_HIDE_OPTIONS))
891                 return 0;
892
893         config->flags &= ~OPT_FLAG_ENABLED;
894         config->flags |= enable ? OPT_FLAG_ENABLED : 0;
895         return 1;
896 }
897
898 void firm_opt_option_help(void)
899 {
900         int i;
901
902         for (i = 0; i < n_opts; ++i) {
903                 char buf[1024];
904                 char buf2[1024];
905
906                 const opt_config_t *config = &opts[i];
907                 if (config->flags & OPT_FLAG_HIDE_OPTIONS)
908                         continue;
909
910                 snprintf(buf2, sizeof(buf2), "firm: enable %s", config->description);
911                 print_option_help(config->name, buf2);
912                 snprintf(buf, sizeof(buf), "no-%s", config->name);
913                 snprintf(buf2, sizeof(buf2), "firm: disable %s", config->description);
914                 print_option_help(buf, buf2);
915         }
916 }
917
918 /**
919  * Do very early initializations
920  */
921 void firm_early_init(void)
922 {
923         /* arg: need this here for command line options */
924         be_opt_register();
925
926         enable_safe_defaults();
927 }