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