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