5534c258ff21d6fe7d1ace4ccc7e4542315be22b
[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_gcse(ir_graph *irg)
189 {
190         set_opt_global_cse(1);
191         optimize_graph_df(irg);
192         set_opt_global_cse(0);
193 }
194
195 static void lower_blockcopy(ir_graph *irg)
196 {
197         lower_CopyB(irg, 128, 4);
198 }
199
200 typedef enum opt_target {
201         OPT_TARGET_IRG, /**< optimization function works on a single graph */
202         OPT_TARGET_IRP  /**< optimization function works on the complete program */
203 } opt_target_t;
204
205 typedef enum opt_flags {
206         OPT_FLAG_NONE         = 0,
207         OPT_FLAG_ENABLED      = 1 << 0, /**< enable the optimization */
208         OPT_FLAG_NO_DUMP      = 1 << 1, /**< don't dump after transformation */
209         OPT_FLAG_NO_VERIFY    = 1 << 2, /**< don't verify after transformation */
210         OPT_FLAG_HIDE_OPTIONS = 1 << 3, /**< do not automatically process
211                                              -foptions for this transformation */
212         OPT_FLAG_ESSENTIAL    = 1 << 4, /**< output won't work without this pass
213                                              so we need it even with -O0 */
214 } opt_flags_t;
215
216 typedef void (*transform_irg_func)(ir_graph *irg);
217 typedef void (*transform_irp_func)(void);
218
219 typedef struct {
220         opt_target_t  target;
221         const char   *name;
222         union {
223                 transform_irg_func transform_irg;
224                 transform_irp_func transform_irp;
225         } u;
226         const char   *description;
227         opt_flags_t   flags;
228 } opt_config_t;
229
230 static opt_config_t opts[] = {
231 #define IRG(a, b, c, d) { OPT_TARGET_IRG, a, .u.transform_irg = (transform_irg_func)b, c, d }
232 #define IRP(a, b, c, d) { OPT_TARGET_IRP, a, .u.transform_irp = b,                     c, d }
233         IRG("bool",              opt_bool,                 "bool simplification",                                   OPT_FLAG_NONE),
234         IRG("combo",             combo,                    "combined CCE, UCE and GVN",                             OPT_FLAG_NONE),
235         IRG("confirm",           construct_confirms,       "confirm optimisation",                                  OPT_FLAG_HIDE_OPTIONS),
236         IRG("control-flow",      optimize_cf,              "optimization of control-flow",                          OPT_FLAG_HIDE_OPTIONS),
237         IRG("dead",              dead_node_elimination,    "dead node elimination",                                 OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
238         IRG("deconv",            conv_opt,                 "conv node elimination",                                 OPT_FLAG_NONE),
239         IRG("fp-vrp",            fixpoint_vrp,             "fixpoint value range propagation",                      OPT_FLAG_NONE),
240         IRG("frame",             opt_frame_irg,            "remove unused frame entities",                          OPT_FLAG_NONE),
241         IRG("gvn-pre",           do_gvn_pre,               "global value numbering partial redundancy elimination", OPT_FLAG_NONE),
242         IRG("if-conversion",     opt_if_conv,              "if-conversion",                                         OPT_FLAG_NONE),
243         IRG("invert-loops",      do_loop_inversion,        "loop inversion",                                        OPT_FLAG_NONE),
244         IRG("ivopts",            do_stred,                 "induction variable strength reduction",                 OPT_FLAG_NONE),
245         IRG("local",             optimize_graph_df,        "local graph optimizations",                             OPT_FLAG_HIDE_OPTIONS),
246         IRG("lower",             do_lower_highlevel,       "lowering",                                              OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
247         IRG("lower-mux",         do_lower_mux,             "mux lowering",                                          OPT_FLAG_NONE),
248         IRG("opt-load-store",    optimize_load_store,      "load store optimization",                               OPT_FLAG_NONE),
249         IRG("opt-tail-rec",      opt_tail_rec_irg,         "tail-recursion eliminiation",                           OPT_FLAG_NONE),
250         IRG("parallelize-mem",   opt_parallelize_mem,      "parallelize memory",                                    OPT_FLAG_NONE),
251         IRG("gcse",              do_gcse,                  "global common subexpression eliminiation",              OPT_FLAG_NONE),
252         IRG("place",             place_code,               "code placement",                                        OPT_FLAG_NONE),
253         IRG("reassociation",     optimize_reassociation,   "reassociation",                                         OPT_FLAG_NONE),
254         IRG("remove-confirms",   remove_confirms,          "confirm removal",                                       OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
255         IRG("remove-phi-cycles", remove_phi_cycles,        "removal of phi cycles",                                 OPT_FLAG_HIDE_OPTIONS),
256         IRG("scalar-replace",    scalar_replacement_opt,   "scalar replacement",                                    OPT_FLAG_NONE),
257         IRG("shape-blocks",      shape_blocks,             "block shaping",                                         OPT_FLAG_NONE),
258         IRG("thread-jumps",      opt_jumpthreading,        "path-sensitive jumpthreading",                          OPT_FLAG_NONE),
259         IRG("unroll-loops",      do_loop_unrolling,        "loop unrolling",                                        OPT_FLAG_NONE),
260         IRG("vrp",               set_vrp_data,             "value range propagation",                               OPT_FLAG_NONE),
261         IRP("inline",            do_inline,                "inlining",                                              OPT_FLAG_NONE),
262         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),
263         IRG("lower-blockcopy",   lower_blockcopy,          "replace small block copies with load/store sequences",  OPT_FLAG_NO_DUMP),
264         IRP("target-lowering",   be_lower_for_target,      "lowering necessary for target architecture",            OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
265         IRP("opt-func-call",     optimize_funccalls,       "function call optimization",                            OPT_FLAG_NONE),
266         IRP("opt-proc-clone",    do_cloning,               "procedure cloning",                                     OPT_FLAG_NONE),
267         IRP("remove-unused",     garbage_collect_entities, "removal of unused functions/variables",                 OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
268         IRP("rts",               rts_map,                  "optimization of known library functions",               OPT_FLAG_NONE),
269         IRP("opt-cc",            mark_private_methods,     "calling conventions optimization",                      OPT_FLAG_NONE),
270 #undef IRP
271 #undef IRG
272 };
273 static const int n_opts = sizeof(opts) / sizeof(opts[0]);
274 ir_timer_t *timers[sizeof(opts)/sizeof(opts[0])];
275
276 static opt_config_t *get_opt(const char *name)
277 {
278         int i;
279         for (i = 0; i < n_opts; ++i) {
280                 opt_config_t *config = &opts[i];
281                 if (strcmp(config->name, name) == 0)
282                         return config;
283         }
284
285         return NULL;
286 }
287
288 static void set_opt_enabled(const char *name, bool enabled)
289 {
290         opt_config_t *config = get_opt(name);
291         config->flags = (config->flags & ~OPT_FLAG_ENABLED)
292                 | (enabled ? OPT_FLAG_ENABLED : 0);
293 }
294
295 static bool get_opt_enabled(const char *name)
296 {
297         opt_config_t *config = get_opt(name);
298         return (config->flags & OPT_FLAG_ENABLED) != 0;
299 }
300
301 /**
302  * perform an optimisation on a single graph
303  *
304  * @return  true if something changed, false otherwise
305  */
306 static bool do_irg_opt(ir_graph *irg, const char *name)
307 {
308         ir_graph     *old_irg;
309         opt_config_t *config = get_opt(name);
310         size_t        n      = config - opts;
311         assert(config != NULL);
312         assert(config->target == OPT_TARGET_IRG);
313         if (! (config->flags & OPT_FLAG_ENABLED))
314                 return false;
315
316         old_irg          = current_ir_graph;
317         current_ir_graph = irg;
318
319         timer_push(timers[n]);
320         config->u.transform_irg(irg);
321         timer_pop(timers[n]);
322
323         if (firm_dump.all_phases && firm_dump.ir_graph) {
324                 dump_ir_graph(irg, name);
325         }
326
327         if (firm_opt.check_all) {
328                 timer_push(t_verify);
329                 irg_verify(irg, VERIFY_ENFORCE_SSA);
330                 timer_pop(t_verify);
331         }
332
333         current_ir_graph = old_irg;
334         return true;
335 }
336
337 static void do_irp_opt(const char *name)
338 {
339         opt_config_t *config = get_opt(name);
340         size_t        n      = config - opts;
341         assert(config->target == OPT_TARGET_IRP);
342         if (! (config->flags & OPT_FLAG_ENABLED))
343                 return;
344
345         timer_push(timers[n]);
346         config->u.transform_irp();
347         timer_pop(timers[n]);
348
349         if (firm_dump.ir_graph && firm_dump.all_phases) {
350                 int i;
351                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
352                         ir_graph *irg = get_irp_irg(i);
353                         dump_ir_graph(irg, name);
354                 }
355         }
356
357         if (firm_opt.check_all) {
358                 int i;
359                 timer_push(t_verify);
360                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
361                         irg_verify(get_irp_irg(i), VERIFY_ENFORCE_SSA);
362                 }
363                 timer_pop(t_verify);
364         }
365 }
366
367 /**
368  * Enable transformations which should be always safe (and cheap) to perform
369  */
370 static void enable_safe_defaults(void)
371 {
372         set_opt_enabled("remove-unused", true);
373         set_opt_enabled("opt-tail-rec", true);
374         set_opt_enabled("opt-func-call", true);
375         set_opt_enabled("reassociation", true);
376         set_opt_enabled("control-flow", true);
377         set_opt_enabled("local", true);
378         set_opt_enabled("lower-const", true);
379         set_opt_enabled("scalar-replace", true);
380         set_opt_enabled("place", true);
381         set_opt_enabled("gcse", true);
382         set_opt_enabled("confirm", true);
383         set_opt_enabled("opt-load-store", true);
384         set_opt_enabled("lower", true);
385         set_opt_enabled("lower-blockcopy", true);
386         set_opt_enabled("deconv", true);
387         set_opt_enabled("remove-confirms", true);
388         set_opt_enabled("ivopts", true);
389         set_opt_enabled("dead", true);
390         set_opt_enabled("remove-phi-cycles", true);
391         set_opt_enabled("frame", true);
392         set_opt_enabled("combo", true);
393         set_opt_enabled("invert-loops", true);
394         set_opt_enabled("target-lowering", true);
395         set_opt_enabled("rts", true);
396         set_opt_enabled("parallelize-mem", true);
397         set_opt_enabled("opt-cc", true);
398 }
399
400 /**
401  * run all the Firm optimizations
402  *
403  * @param input_filename     the name of the (main) source file
404  */
405 static void do_firm_optimizations(const char *input_filename)
406 {
407         size_t   i;
408         unsigned aa_opt;
409
410         set_opt_alias_analysis(firm_opt.alias_analysis);
411
412         aa_opt = aa_opt_no_opt;
413         if (firm_opt.strict_alias)
414                 aa_opt |= aa_opt_type_based | aa_opt_byte_type_may_alias;
415         if (firm_opt.no_alias)
416                 aa_opt = aa_opt_no_alias;
417
418         set_irp_memory_disambiguator_options(aa_opt);
419
420         /* parameter passing code should set them directly sometime... */
421         set_opt_enabled("confirm", firm_opt.confirm);
422         set_opt_enabled("remove-confirms", firm_opt.confirm);
423
424         /* osr supersedes remove_phi_cycles */
425         if (get_opt_enabled("ivopts"))
426                 set_opt_enabled("remove-phi-cycles", false);
427
428         timer_start(t_all_opt);
429
430         do_irp_opt("rts");
431
432         /* first step: kill dead code */
433         for (i = 0; i < get_irp_n_irgs(); i++) {
434                 ir_graph *irg = get_irp_irg(i);
435                 do_irg_opt(irg, "combo");
436                 do_irg_opt(irg, "local");
437                 do_irg_opt(irg, "control-flow");
438         }
439
440         do_irp_opt("remove-unused");
441         for (i = 0; i < get_irp_n_irgs(); ++i) {
442                 ir_graph *irg = get_irp_irg(i);
443                 do_irg_opt(irg, "opt-tail-rec");
444         }
445         do_irp_opt("opt-func-call");
446         do_irp_opt("lower-const");
447
448         for (i = 0; i < get_irp_n_irgs(); i++) {
449                 ir_graph *irg = get_irp_irg(i);
450
451                 do_irg_opt(irg, "scalar-replace");
452                 do_irg_opt(irg, "invert-loops");
453                 do_irg_opt(irg, "unroll-loops");
454                 do_irg_opt(irg, "local");
455                 do_irg_opt(irg, "reassociation");
456                 do_irg_opt(irg, "local");
457                 do_irg_opt(irg, "gcse");
458                 do_irg_opt(irg, "place");
459
460                 if (firm_opt.confirm) {
461                         /* Confirm construction currently can only handle blocks with only
462                            one control flow predecessor. Calling optimize_cf here removes
463                            Bad predecessors and help the optimization of switch constructs.
464                          */
465                         do_irg_opt(irg, "control-flow");
466                         do_irg_opt(irg, "confirm");
467                         do_irg_opt(irg, "vrp");
468                         do_irg_opt(irg, "local");
469                 }
470
471                 do_irg_opt(irg, "control-flow");
472                 do_irg_opt(irg, "opt-load-store");
473                 do_irg_opt(irg, "fp-vrp");
474                 do_irg_opt(irg, "lower");
475                 do_irg_opt(irg, "deconv");
476                 do_irg_opt(irg, "thread-jumps");
477                 do_irg_opt(irg, "remove-confirms");
478                 do_irg_opt(irg, "gvn-pre");
479                 do_irg_opt(irg, "gcse");
480                 do_irg_opt(irg, "place");
481                 do_irg_opt(irg, "control-flow");
482
483                 if (do_irg_opt(irg, "if-conversion")) {
484                         do_irg_opt(irg, "local");
485                         do_irg_opt(irg, "control-flow");
486                 }
487                 /* this doesn't make too much sense but tests the mux destruction... */
488                 do_irg_opt(irg, "lower-mux");
489
490                 do_irg_opt(irg, "bool");
491                 do_irg_opt(irg, "shape-blocks");
492                 do_irg_opt(irg, "ivopts");
493                 do_irg_opt(irg, "local");
494                 do_irg_opt(irg, "dead");
495         }
496
497         do_irp_opt("inline");
498         do_irp_opt("opt-proc-clone");
499
500         for (i = 0; i < get_irp_n_irgs(); i++) {
501                 ir_graph *irg = get_irp_irg(i);
502                 do_irg_opt(irg, "local");
503                 do_irg_opt(irg, "control-flow");
504                 do_irg_opt(irg, "thread-jumps");
505                 do_irg_opt(irg, "local");
506                 do_irg_opt(irg, "control-flow");
507
508                 if( do_irg_opt(irg, "vrp") ) { // if vrp is enabled
509                         do_irg_opt(irg, "local");
510                         do_irg_opt(irg, "vrp");
511                         do_irg_opt(irg, "local");
512                         do_irg_opt(irg, "vrp");
513                 }
514         }
515
516         if (firm_dump.ir_graph) {
517                 /* recompute backedges for nicer dumps */
518                 for (i = 0; i < get_irp_n_irgs(); i++)
519                         construct_cf_backedges(get_irp_irg(i));
520         }
521
522         dump_all("opt");
523
524         if (firm_dump.statistic & STAT_AFTER_OPT)
525                 stat_dump_snapshot(input_filename, "opt");
526
527         timer_stop(t_all_opt);
528 }
529
530 /**
531  * do Firm lowering
532  *
533  * @param input_filename  the name of the (main) source file
534  */
535 static void do_firm_lowering(const char *input_filename)
536 {
537         int i;
538
539         /* enable architecture dependent optimizations */
540         arch_dep_set_opts((arch_dep_opts_t)
541                         ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) |
542                          (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) |
543                          (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) ));
544         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
545                 ir_graph *irg = get_irp_irg(i);
546                 do_irg_opt(irg, "reassociation");
547                 do_irg_opt(irg, "local");
548         }
549
550         do_irp_opt("target-lowering");
551
552         if (firm_dump.statistic & STAT_AFTER_LOWER)
553                 stat_dump_snapshot(input_filename, "low");
554
555         timer_start(t_all_opt);
556
557         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
558                 ir_graph *irg = get_irp_irg(i);
559
560                 do_irg_opt(irg, "local");
561                 do_irg_opt(irg, "deconv");
562                 do_irg_opt(irg, "control-flow");
563                 do_irg_opt(irg, "opt-load-store");
564                 do_irg_opt(irg, "gcse");
565                 do_irg_opt(irg, "place");
566                 do_irg_opt(irg, "control-flow");
567
568                 if (do_irg_opt(irg, "vrp")) {
569                         do_irg_opt(irg, "local");
570                         do_irg_opt(irg, "control-flow");
571                         do_irg_opt(irg, "vrp");
572                         do_irg_opt(irg, "local");
573                         do_irg_opt(irg, "control-flow");
574                 }
575
576                 if (do_irg_opt(irg, "if-conversion")) {
577                         do_irg_opt(irg, "local");
578                         do_irg_opt(irg, "control-flow");
579                 }
580
581                 set_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2);
582                 do_irg_opt(irg, "local");
583
584                 do_irg_opt(irg, "parallelize-mem");
585                 do_irg_opt(irg, "frame");
586         }
587         do_irp_opt("remove-unused");
588         do_irp_opt("opt-cc");
589         timer_stop(t_all_opt);
590         dump_all("low-opt");
591
592         if (firm_dump.statistic & STAT_FINAL) {
593                 stat_dump_snapshot(input_filename, "final");
594         }
595 }
596
597 /**
598  * Initialize for the Firm-generating back end.
599  */
600 void gen_firm_init(void)
601 {
602         unsigned pattern = 0;
603         int      i;
604
605         for (i = 0; i < n_opts; ++i) {
606                 timers[i] = ir_timer_new();
607                 timer_register(timers[i], opts[i].description);
608         }
609         t_verify = ir_timer_new();
610         timer_register(t_verify, "Firm: verify pass");
611         t_vcg_dump = ir_timer_new();
612         timer_register(t_vcg_dump, "Firm: vcg dumping");
613         t_all_opt = ir_timer_new();
614         timer_register(t_all_opt, "Firm: all optimizations");
615
616         if (firm_dump.stat_pattern)
617                 pattern |= FIRMSTAT_PATTERN_ENABLED;
618
619         if (firm_dump.stat_dag)
620                 pattern |= FIRMSTAT_COUNT_DAG;
621
622         ir_init(NULL);
623         firm_init_stat(firm_dump.statistic == STAT_NONE ?
624                         0 : FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP
625                         | FIRMSTAT_COUNT_CONSTS | pattern);
626
627         edges_init_dbg(firm_opt.verify_edges);
628
629         /* Sel node cannot produce NULL pointers */
630         set_opt_sel_based_null_check_elim(1);
631
632         /* dynamic dispatch works currently only if whole world scenarios */
633         set_opt_dyn_meth_dispatch(0);
634
635         /* do not run architecture dependent optimizations in building phase */
636         arch_dep_set_opts(arch_dep_none);
637
638         do_node_verification((firm_verification_t) firm_opt.verify);
639         if (firm_dump.filter != NULL)
640                 ir_set_dump_filter(firm_dump.filter);
641         if (firm_dump.extbb)
642                 ir_add_dump_flags(ir_dump_flag_group_extbb);
643         if (firm_dump.no_blocks)
644                 ir_remove_dump_flags(ir_dump_flag_blocks_as_subgraphs);
645
646         set_optimize(1);
647         set_opt_constant_folding(firm_opt.const_folding);
648         set_opt_algebraic_simplification(firm_opt.const_folding);
649         set_opt_cse(firm_opt.cse);
650         set_opt_global_cse(0);
651         set_opt_unreachable_code(1);
652 }
653
654 /**
655  * Called, after the Firm generation is completed,
656  * do all optimizations and backend call here.
657  *
658  * @param out                a file handle for the output, may be NULL
659  * @param input_filename     the name of the (main) source file
660  * @param c_mode             non-zero if "C" was compiled
661  */
662 void gen_firm_finish(FILE *out, const char *input_filename)
663 {
664         int i;
665
666         /* the general for dumping option must be set, or the others will not work*/
667         firm_dump.ir_graph
668                 = (bool) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb);
669
670         ir_add_dump_flags(ir_dump_flag_keepalive_edges
671                         | ir_dump_flag_consts_local | ir_dump_flag_dominance);
672         ir_remove_dump_flags(ir_dump_flag_loops | ir_dump_flag_ld_names);
673
674         /* FIXME: cloning might ADD new graphs. */
675         irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
676
677         if (firm_dump.all_types) {
678                 dump_ir_prog_ext(dump_typegraph, "types.vcg");
679         }
680
681         /* finalize all graphs */
682         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
683                 ir_graph *irg = get_irp_irg(i);
684                 irg_finalize_cons(irg);
685         }
686         dump_all("");
687
688         timer_push(t_verify);
689         tr_verify();
690         timer_pop(t_verify);
691
692         /* all graphs are finalized, set the irp phase to high */
693         set_irp_phase_state(phase_high);
694
695         /* BEWARE: kill unreachable code before doing compound lowering */
696         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
697                 ir_graph *irg = get_irp_irg(i);
698                 do_irg_opt(irg, "control-flow");
699                 do_irg_opt(irg, "lower-blockcopy");
700         }
701
702         if (firm_dump.statistic & STAT_BEFORE_OPT) {
703                 stat_dump_snapshot(input_filename, "noopt");
704         }
705
706         do_firm_optimizations(input_filename);
707         do_firm_lowering(input_filename);
708
709         /* set the phase to low */
710         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
711                 set_irg_phase_state(get_irp_irg(i), phase_low);
712
713         if (firm_dump.statistic & STAT_FINAL_IR)
714                 stat_dump_snapshot(input_filename, "final-ir");
715
716         /* run the code generator */
717         ir_timer_t *timer = ir_timer_new();
718         timer_register(timer, "Firm: backend");
719         timer_start(timer);
720         be_main(out, input_filename);
721         timer_stop(timer);
722
723         if (firm_dump.statistic & STAT_FINAL)
724                 stat_dump_snapshot(input_filename, "final");
725 }
726
727 void disable_all_opts(void)
728 {
729         for (int i = 0; i < n_opts; ++i) {
730                 opt_config_t *config = &opts[i];
731                 if (config->flags & OPT_FLAG_ESSENTIAL) {
732                         config->flags |= OPT_FLAG_ENABLED;
733                 } else {
734                         config->flags &= ~OPT_FLAG_ENABLED;
735                 }
736         }
737 }
738
739 int firm_opt_option(const char *opt)
740 {
741         bool enable = true;
742         if (strncmp(opt, "no-", 3) == 0) {
743                 enable = false;
744                 opt = opt + 3;
745         }
746
747         opt_config_t *config = get_opt(opt);
748         if (config == NULL || (config->flags & OPT_FLAG_HIDE_OPTIONS))
749                 return 0;
750
751         config->flags &= ~OPT_FLAG_ENABLED;
752         config->flags |= enable ? OPT_FLAG_ENABLED : 0;
753         return 1;
754 }
755
756 void firm_opt_option_help(void)
757 {
758         int i;
759
760         for (i = 0; i < n_opts; ++i) {
761                 char buf[1024];
762                 char buf2[1024];
763
764                 const opt_config_t *config = &opts[i];
765                 if (config->flags & OPT_FLAG_HIDE_OPTIONS)
766                         continue;
767
768                 snprintf(buf2, sizeof(buf2), "firm: enable %s", config->description);
769                 print_option_help(config->name, buf2);
770                 snprintf(buf, sizeof(buf), "no-%s", config->name);
771                 snprintf(buf2, sizeof(buf2), "firm: disable %s", config->description);
772                 print_option_help(buf, buf2);
773         }
774 }
775
776 static void set_be_option(const char *arg)
777 {
778         int res = be_parse_arg(arg);
779         (void) res;
780         assert(res);
781 }
782
783 static void set_option(const char *arg)
784 {
785         int res = firm_option(arg);
786         (void) res;
787         assert(res);
788 }
789
790 void choose_optimization_pack(int level)
791 {
792         /* apply optimisation level */
793         switch(level) {
794         case 0:
795                 set_option("no-opt");
796                 break;
797         case 1:
798                 set_option("no-inline");
799                 break;
800         default:
801         case 4:
802                 /* use_builtins = true; */
803                 /* fallthrough */
804         case 3:
805                 set_option("thread-jumps");
806                 set_option("if-conversion");
807                 /* fallthrough */
808         case 2:
809                 set_option("strict-aliasing");
810                 set_option("inline");
811                 set_option("fp-vrp");
812                 set_option("deconv");
813                 set_be_option("omitfp");
814                 break;
815         }
816 }
817
818 /**
819  * Do very early initializations
820  */
821 void firm_early_init(void)
822 {
823         /* arg: need this here for command line options */
824         be_opt_register();
825
826         enable_safe_defaults();
827 }