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