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