cdb3bc156ecbf8ad221c49ec96f9afee6f7bedf9
[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 typedef enum opt_target {
283         OPT_TARGET_IRG, /**< optimization function works on a single graph */
284         OPT_TARGET_IRP  /**< optimization function works on the complete program */
285 } opt_target_t;
286
287 typedef enum opt_flags {
288         OPT_FLAG_NONE         = 0,
289         OPT_FLAG_ENABLED      = 1 << 0, /**< enable the optimization */
290         OPT_FLAG_NO_DUMP      = 1 << 1, /**< don't dump after transformation */
291         OPT_FLAG_NO_VERIFY    = 1 << 2, /**< don't verify after transformation */
292         OPT_FLAG_HIDE_OPTIONS = 1 << 3, /**< do not automatically process
293                                              -foptions for this transformation */
294         OPT_FLAG_ESSENTIAL    = 1 << 4, /**< output won't work without this pass
295                                              so we need it even with -O0 */
296 } opt_flags_t;
297
298 typedef void (*transform_irg_func)(ir_graph *irg);
299 typedef void (*transform_irp_func)(void);
300 typedef void (*func_ptr_t)(void);
301
302 typedef struct {
303         opt_target_t  target;
304         const char   *name;
305         func_ptr_t    func;
306         const char   *description;
307         opt_flags_t   flags;
308 } opt_config_t;
309
310 static opt_config_t opts[] = {
311         { OPT_TARGET_IRP, "rts",             (func_ptr_t) rts_map,                 "optimization of known library functions", OPT_FLAG_HIDE_OPTIONS },
312         { OPT_TARGET_IRG, "combo",           (func_ptr_t) combo,                   "combined CCE, UCE and GVN",               OPT_FLAG_NONE},
313         { OPT_TARGET_IRG, "control-flow",    (func_ptr_t) optimize_cf,             "optimization of control-flow",            OPT_FLAG_HIDE_OPTIONS },
314         { OPT_TARGET_IRG, "local",           (func_ptr_t) optimize_graph_df,       "local graph optimizations",               OPT_FLAG_HIDE_OPTIONS },
315         { OPT_TARGET_IRP, "remove-unused",   (func_ptr_t) garbage_collect_entities,"removal of unused functions/variables",   OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY },
316         { OPT_TARGET_IRP, "opt-tail-rec",    (func_ptr_t) opt_tail_recursion,      "tail-recursion eliminiation",             OPT_FLAG_NONE },
317         { OPT_TARGET_IRP, "opt-func-call",   (func_ptr_t) do_optimize_funccalls,   "function call optimization",              OPT_FLAG_NONE },
318         { OPT_TARGET_IRG, "lower",           (func_ptr_t) do_lower_highlevel,      "lowering",                                OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL },
319         { OPT_TARGET_IRP, "lower-const",     (func_ptr_t) lower_const_code,        "lowering of constant code",               OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY | OPT_FLAG_ESSENTIAL },
320         { OPT_TARGET_IRP, "lower-dw",        (func_ptr_t) do_lower_dw_ops,         "lowering of doubleword operations",       OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL },
321         { OPT_TARGET_IRG, "lower-switch",    (func_ptr_t) do_lower_switch,         "switch lowering",                         OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL },
322         { OPT_TARGET_IRG, "one-return",      (func_ptr_t) normalize_one_return,    "normalisation to 1 return",               OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY },
323         { OPT_TARGET_IRG, "scalar-replace",  (func_ptr_t) scalar_replacement_opt,  "scalar replacement",                      OPT_FLAG_NONE },
324         { OPT_TARGET_IRG, "reassociation",   (func_ptr_t) optimize_reassociation,  "reassociation",                           OPT_FLAG_NONE },
325         { OPT_TARGET_IRG, "gcse",            (func_ptr_t) do_gcse,                 "global common subexpression elimination", OPT_FLAG_NONE },
326         { OPT_TARGET_IRG, "place",           (func_ptr_t) place_code,              "code placement",                          OPT_FLAG_NONE },
327         { OPT_TARGET_IRG, "confirm",         (func_ptr_t) construct_confirms,      "confirm optimisation",                    OPT_FLAG_HIDE_OPTIONS },
328         { OPT_TARGET_IRG, "opt-load-store",  (func_ptr_t) optimize_load_store,     "load store optimization",                 OPT_FLAG_NONE },
329         { OPT_TARGET_IRG, "parallelize-mem", (func_ptr_t) opt_parallelize_mem,     "parallelize memory",                      OPT_FLAG_NONE },
330         { OPT_TARGET_IRG, "deconv",          (func_ptr_t) conv_opt,                "conv node elimination",                   OPT_FLAG_NONE },
331         { OPT_TARGET_IRG, "thread-jumps",    (func_ptr_t) opt_jumpthreading,       "path-sensitive jumpthreading",            OPT_FLAG_NONE },
332         { OPT_TARGET_IRG, "remove-confirms", (func_ptr_t) remove_confirms,         "confirm removal",                         OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY },
333         { OPT_TARGET_IRG, "gvn-pre",         (func_ptr_t) do_gvn_pre,              "global value numbering partial redundancy elimination", OPT_FLAG_NONE },
334         { OPT_TARGET_IRG, "if-conversion",   (func_ptr_t) do_if_conv,              "if-conversion",                           OPT_FLAG_NONE },
335         { OPT_TARGET_IRG, "bool",            (func_ptr_t) opt_bool,                "bool simplification",                     OPT_FLAG_NONE },
336         { OPT_TARGET_IRG, "shape-blocks",    (func_ptr_t) shape_blocks,            "block shaping",                           OPT_FLAG_NONE },
337         { OPT_TARGET_IRG, "ivopts",          (func_ptr_t) do_stred,                "induction variable strength reduction",   OPT_FLAG_NONE },
338         { OPT_TARGET_IRG, "remove-phi-cycles", (func_ptr_t) remove_phi_cycles,     "removal of phi cycles",                   OPT_FLAG_HIDE_OPTIONS },
339         { OPT_TARGET_IRG, "dead",            (func_ptr_t) dead_node_elimination,   "dead node elimination",                   OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY },
340         { OPT_TARGET_IRP, "inline",          (func_ptr_t) do_inline,               "inlining",                                OPT_FLAG_NONE },
341         { OPT_TARGET_IRP, "opt-proc-clone",  (func_ptr_t) do_cloning,              "procedure cloning",                       OPT_FLAG_NONE },
342         { OPT_TARGET_IRG, "invert-loops",    (func_ptr_t) do_loop_inversion,       "loop inversion",                          OPT_FLAG_NONE },
343         { OPT_TARGET_IRG, "unroll-loops",    (func_ptr_t) do_loop_unrolling,       "loop unrolling",                          OPT_FLAG_NONE },
344         { OPT_TARGET_IRG, "lower-mux",       (func_ptr_t) do_lower_mux,            "mux lowering",                            OPT_FLAG_NONE },
345 };
346 static const int n_opts = sizeof(opts) / sizeof(opts[0]);
347 ir_timer_t *timers[sizeof(opts)/sizeof(opts[0])];
348
349 static opt_config_t *get_opt(const char *name)
350 {
351         int i;
352         for (i = 0; i < n_opts; ++i) {
353                 opt_config_t *config = &opts[i];
354                 if (strcmp(config->name, name) == 0)
355                         return config;
356         }
357
358         return NULL;
359 }
360
361 static void set_opt_enabled(const char *name, bool enabled)
362 {
363         opt_config_t *config = get_opt(name);
364         config->flags = (config->flags & ~OPT_FLAG_ENABLED)
365                 | (enabled ? OPT_FLAG_ENABLED : 0);
366 }
367
368 static bool get_opt_enabled(const char *name)
369 {
370         opt_config_t *config = get_opt(name);
371         return (config->flags & OPT_FLAG_ENABLED) != 0;
372 }
373
374 /**
375  * perform an optimisation on a single graph
376  *
377  * @return  true if something changed, false otherwise
378  */
379 static bool do_irg_opt(ir_graph *irg, const char *name)
380 {
381         transform_irg_func  func;
382         ir_graph           *old_irg;
383         opt_config_t       *config = get_opt(name);
384         size_t              n      = config - opts;
385         assert(config != NULL);
386         assert(config->target == OPT_TARGET_IRG);
387         if (! (config->flags & OPT_FLAG_ENABLED))
388                 return false;
389
390
391         old_irg          = current_ir_graph;
392         current_ir_graph = irg;
393
394         func = (transform_irg_func) config->func;
395
396         timer_push(timers[n]);
397         func(irg);
398         timer_pop(timers[n]);
399
400         if (firm_dump.all_phases && firm_dump.ir_graph) {
401                 dump_graph_count(irg, name);
402         }
403
404         if (firm_opt.check_all) {
405                 timer_push(t_verify);
406                 irg_verify(irg, VRFY_ENFORCE_SSA);
407                 timer_pop(t_verify);
408         }
409
410         current_ir_graph = old_irg;
411         return true;
412 }
413
414 static void do_irp_opt(const char *name)
415 {
416         transform_irp_func  func;
417         opt_config_t       *config = get_opt(name);
418         size_t              n      = config - opts;
419         assert(config->target == OPT_TARGET_IRP);
420         if (! (config->flags & OPT_FLAG_ENABLED))
421                 return;
422
423         func = (transform_irp_func) config->func;
424
425         timer_push(timers[n]);
426         func();
427         timer_pop(timers[n]);
428
429         if (firm_dump.ir_graph && firm_dump.all_phases) {
430                 int i;
431                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
432                         ir_graph *irg = get_irp_irg(i);
433                         dump_graph_count(irg, name);
434                 }
435         }
436
437         if (firm_opt.check_all) {
438                 int i;
439                 timer_push(t_verify);
440                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
441                         irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA);
442                 }
443                 timer_pop(t_verify);
444         }
445 }
446
447 /**
448  * Enable transformations which should be always safe (and cheap) to perform
449  */
450 static void enable_safe_defaults(void)
451 {
452         set_opt_enabled("remove-unused", true);
453         set_opt_enabled("opt-tail-rec", true);
454         set_opt_enabled("opt-func-call", true);
455         set_opt_enabled("reassociation", true);
456         set_opt_enabled("control-flow", true);
457         set_opt_enabled("local", true);
458         set_opt_enabled("lower-const", true);
459         set_opt_enabled("scalar-replace", true);
460         set_opt_enabled("place", true);
461         set_opt_enabled("confirm", true);
462         set_opt_enabled("opt-load-store", true);
463         set_opt_enabled("lower", true);
464         set_opt_enabled("deconv", true);
465         set_opt_enabled("remove-confirms", true);
466         set_opt_enabled("ivopts", true);
467         set_opt_enabled("dead", true);
468         set_opt_enabled("lower-switch", true);
469         set_opt_enabled("remove-phi-cycles", true);
470 }
471
472 /**
473  * run all the Firm optimizations
474  *
475  * @param input_filename     the name of the (main) source file
476  */
477 static void do_firm_optimizations(const char *input_filename)
478 {
479         int      i;
480         unsigned aa_opt;
481
482         set_opt_alias_analysis(firm_opt.alias_analysis);
483
484         aa_opt = aa_opt_no_opt;
485         if (firm_opt.strict_alias)
486                 aa_opt |= aa_opt_type_based | aa_opt_byte_type_may_alias;
487         if (firm_opt.no_alias)
488                 aa_opt = aa_opt_no_alias;
489
490         set_irp_memory_disambiguator_options(aa_opt);
491
492         /* parameter passing code should set them directly sometime... */
493         set_opt_enabled("rts", !firm_opt.freestanding);
494         set_opt_enabled("gcse", firm_opt.gcse);
495         set_opt_enabled("place", !firm_opt.gcse);
496         set_opt_enabled("confirm", firm_opt.confirm);
497         set_opt_enabled("remove-confirms", firm_opt.confirm);
498
499         /* osr supersedes remove_phi_cycles */
500         if (get_opt_enabled("ivopts"))
501                 set_opt_enabled("remove-phi-cycles", false);
502
503         timer_start(t_all_opt);
504
505         do_irp_opt("rts");
506
507         /* first step: kill dead code */
508         for (i = 0; i < get_irp_n_irgs(); i++) {
509                 ir_graph *irg = get_irp_irg(i);
510                 do_irg_opt(irg, "combo");
511                 do_irg_opt(irg, "local");
512                 do_irg_opt(irg, "control-flow");
513         }
514
515         do_irp_opt("remove-unused");
516         do_irp_opt("opt-tail-rec");
517         do_irp_opt("opt-func-call");
518         do_irp_opt("lower-const");
519
520         for (i = 0; i < get_irp_n_irgs(); i++) {
521                 ir_graph *irg = get_irp_irg(i);
522
523                 do_irg_opt(irg, "scalar-replace");
524                 do_irg_opt(irg, "invert-loops");
525                 do_irg_opt(irg, "unroll-loops");
526                 do_irg_opt(irg, "local");
527                 do_irg_opt(irg, "reassociation");
528                 do_irg_opt(irg, "local");
529                 do_irg_opt(irg, "gcse");
530
531                 if (firm_opt.confirm) {
532                         /* Confirm construction currently can only handle blocks with only
533                            one control flow predecessor. Calling optimize_cf here removes
534                            Bad predecessors and help the optimization of switch constructs.
535                          */
536                         do_irg_opt(irg, "control-flow");
537                         do_irg_opt(irg, "confirm");
538                         do_irg_opt(irg, "local");
539                 }
540
541                 do_irg_opt(irg, "control-flow");
542                 do_irg_opt(irg, "opt-load-store");
543                 do_irg_opt(irg, "lower");
544                 do_irg_opt(irg, "deconv");
545                 do_irg_opt(irg, "thread-jumps");
546                 do_irg_opt(irg, "remove-confirms");
547                 do_irg_opt(irg, "gvn-pre");
548                 do_irg_opt(irg, "place");
549                 do_irg_opt(irg, "control-flow");
550
551                 if (do_irg_opt(irg, "if-conversion")) {
552                         do_irg_opt(irg, "local");
553                         do_irg_opt(irg, "control-flow");
554                 }
555                 /* this doesn't make too much sense but tests the mux destruction... */
556                 do_irg_opt(irg, "lower-mux");
557
558                 do_irg_opt(irg, "bool");
559                 do_irg_opt(irg, "shape-blocks");
560                 do_irg_opt(irg, "lower-switch");
561                 do_irg_opt(irg, "ivopts");
562                 do_irg_opt(irg, "local");
563                 do_irg_opt(irg, "dead");
564         }
565
566         do_irp_opt("inline");
567         do_irp_opt("opt-proc-clone");
568
569         for (i = 0; i < get_irp_n_irgs(); i++) {
570                 ir_graph *irg = get_irp_irg(i);
571                 do_irg_opt(irg, "local");
572                 do_irg_opt(irg, "control-flow");
573                 do_irg_opt(irg, "thread-jumps");
574                 do_irg_opt(irg, "local");
575                 do_irg_opt(irg, "control-flow");
576         }
577
578         if (firm_dump.ir_graph) {
579                 /* recompute backedges for nicer dumps */
580                 for (i = 0; i < get_irp_n_irgs(); i++)
581                         construct_cf_backedges(get_irp_irg(i));
582         }
583
584         do_irp_opt("remove-unused");
585
586         dump_all("-opt");
587
588         if (firm_dump.statistic & STAT_AFTER_OPT)
589                 stat_dump_snapshot(input_filename, "opt");
590
591         timer_stop(t_all_opt);
592 }
593
594 /**
595  * do Firm lowering
596  *
597  * @param input_filename  the name of the (main) source file
598  */
599 static void do_firm_lowering(const char *input_filename)
600 {
601         int i;
602
603         do_irp_opt("lower-dw");
604
605         if (firm_dump.statistic & STAT_AFTER_LOWER)
606                 stat_dump_snapshot(input_filename, "low");
607
608         dump_all("-low");
609
610         if (firm_opt.enabled) {
611                 timer_start(t_all_opt);
612
613                 /* run reassociation first on all graphs BEFORE the architecture
614                    dependent optimizations are enabled */
615                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
616                         ir_graph *irg = get_irp_irg(i);
617                         do_irg_opt(irg, "reassociation");
618                 }
619
620                 /* enable architecture dependent optimizations */
621                 arch_dep_set_opts((arch_dep_opts_t)
622                                 ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) |
623                                  (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) |
624                                  (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) ));
625
626                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
627                         ir_graph *irg = get_irp_irg(i);
628
629                         current_ir_graph = irg;
630
631                         do_irg_opt(irg, "local");
632                         do_irg_opt(irg, "gcse");
633                         do_irg_opt(irg, "opt-load-store");
634                         do_irg_opt(irg, "local");
635                         do_irg_opt(irg, "control-flow");
636
637                         if (do_irg_opt(irg, "if-conversion")) {
638                                 do_irg_opt(irg, "local");
639                                 do_irg_opt(irg, "control-flow");
640                         }
641
642                         do_irg_opt(irg, "parallelize-mem");
643                 }
644                 timer_stop(t_all_opt);
645
646                 dump_all("-low-opt");
647         }
648
649         if (firm_opt.cc_opt)
650                 mark_private_methods();
651
652         /* set the phase to low */
653         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
654                 set_irg_phase_low(get_irp_irg(i));
655
656         /* all graphs are lowered, set the irp phase to low */
657         set_irp_phase_state(phase_low);
658
659         if (firm_dump.statistic & STAT_FINAL) {
660                 stat_dump_snapshot(input_filename, "final");
661         }
662 }
663
664 /**
665  * Initialize for the Firm-generating back end.
666  */
667 void gen_firm_init(void)
668 {
669         firm_parameter_t params;
670         unsigned         pattern = 0;
671         int              i;
672
673         for (i = 0; i < n_opts; ++i) {
674                 timers[i] = ir_timer_new();
675                 timer_register(timers[i], opts[i].description);
676         }
677         t_verify = ir_timer_new();
678         timer_register(t_verify, "Firm: verify pass");
679         t_vcg_dump = ir_timer_new();
680         timer_register(t_vcg_dump, "Firm: vcg dumping");
681         t_all_opt = ir_timer_new();
682         timer_register(t_all_opt, "Firm: all optimizations");
683
684         if (firm_dump.stat_pattern)
685                 pattern |= FIRMSTAT_PATTERN_ENABLED;
686
687         if (firm_dump.stat_dag)
688                 pattern |= FIRMSTAT_COUNT_DAG;
689
690         memset(&params, 0, sizeof(params));
691         params.size                  = sizeof(params);
692         params.enable_statistics     = firm_dump.statistic == STAT_NONE ? 0 :
693                 FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP | FIRMSTAT_COUNT_CONSTS
694                 | pattern;
695         params.initialize_local_func = uninitialized_local_var;
696         params.cc_mask               = 0; /* no regparam, cdecl */
697
698         ir_init(&params);
699
700         if (firm_be_opt.selection == BE_FIRM_BE) {
701                 const backend_params *be_params = be_get_backend_param();
702
703                 if (be_params->do_dw_lowering)
704                         set_opt_enabled("lower-dw", true);
705
706                 arch_create_intrinsic   = be_params->arch_create_intrinsic_fkt;
707                 create_intrinsic_ctx    = be_params->create_intrinsic_ctx;
708
709                 ad_param                = be_params->dep_param;
710                 if_conv_info            = be_params->if_conv_info;
711         }
712
713         edges_init_dbg(firm_opt.vrfy_edges);
714
715         /* Sel node cannot produce NULL pointers */
716         set_opt_sel_based_null_check_elim(1);
717
718         /* dynamic dispatch works currently only if whole world scenarios */
719         set_opt_dyn_meth_dispatch(0);
720
721         arch_dep_init(arch_factory);
722
723         /* do not run architecture dependent optimizations in building phase */
724         arch_dep_set_opts(arch_dep_none);
725
726         do_node_verification((firm_verification_t) firm_opt.vrfy);
727         if (firm_dump.filter)
728                 only_dump_method_with_name(new_id_from_str(firm_dump.filter));
729
730         if (firm_opt.enabled) {
731                 set_optimize(1);
732                 set_opt_constant_folding(firm_opt.const_folding);
733                 set_opt_algebraic_simplification(firm_opt.const_folding);
734                 set_opt_cse(firm_opt.cse);
735                 set_opt_global_cse(0);
736                 set_opt_unreachable_code(1);
737                 set_opt_control_flow(firm_opt.control_flow);
738                 set_opt_control_flow_weak_simplification(1);
739                 set_opt_control_flow_strong_simplification(1);
740         } else {
741                 set_optimize(0);
742         }
743
744         /* do not dump entity ld names */
745         dump_ld_names(0);
746 }
747
748 /**
749  * Called, after the Firm generation is completed,
750  * do all optimizations and backend call here.
751  *
752  * @param out                a file handle for the output, may be NULL
753  * @param input_filename     the name of the (main) source file
754  * @param c_mode             non-zero if "C" was compiled
755  * @param new_firm_const_exists  non-zero, if the const attribute was used on functions
756  */
757 void gen_firm_finish(FILE *out, const char *input_filename, int c_mode,
758                      int new_firm_const_exists)
759 {
760         int i;
761
762 #if 0
763         if (firm_opt.enable_statev) {
764                 char buf[1024];
765                 snprintf(buf, sizeof(buf), "%s.ev", input_filename);
766                 ir_stat_ev_begin(input_filename, firm_opt.statev_filter);
767                 ir_stat_ev_compilation_unit(input_filename);
768         }
769 #endif
770
771         firm_const_exists = new_firm_const_exists;
772
773         /* the general for dumping option must be set, or the others will not work*/
774         firm_dump.ir_graph
775                 = (a_byte) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb);
776
777         dump_keepalive_edges(1);
778         dump_consts_local(1);
779         dump_dominator_information(1);
780         dump_loop_information(0);
781
782         if (!firm_dump.edge_labels)
783                 turn_off_edge_labels();
784
785         /* FIXME: cloning might ADD new graphs. */
786         irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
787
788         if (firm_dump.all_types) {
789                 dump_all_types("");
790                 if (! c_mode) {
791                         dump_class_hierarchy(0, "");
792                         dump_class_hierarchy(1, "-with-entities");
793                 }
794         }
795
796         /* finalize all graphs */
797         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
798                 ir_graph *irg = get_irp_irg(i);
799                 irg_finalize_cons(irg);
800         }
801         dump_all("");
802
803         timer_push(t_verify);
804         tr_vrfy();
805         timer_pop(t_verify);
806
807         /* all graphs are finalized, set the irp phase to high */
808         set_irp_phase_state(phase_high);
809
810         /* BEWARE: kill unreachable code before doing compound lowering */
811         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
812                 ir_graph *irg = get_irp_irg(i);
813                 do_irg_opt(irg, "control-flow");
814         }
815
816         /* lower all compound call return values */
817         lower_compound_params();
818
819         /* lower copyb nodes */
820         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
821                 ir_graph *irg = get_irp_irg(i);
822                 lower_CopyB(irg, 128, 4);
823         }
824
825         if (firm_dump.statistic & STAT_BEFORE_OPT) {
826                 stat_dump_snapshot(input_filename, "noopt");
827         }
828
829         if (firm_opt.enabled)
830                 do_firm_optimizations(input_filename);
831
832         if (firm_opt.lower)
833                 do_firm_lowering(input_filename);
834
835         /* set the phase to low */
836         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
837                 set_irg_phase_low(get_irp_irg(i));
838
839         if (firm_dump.statistic & STAT_FINAL_IR)
840                 stat_dump_snapshot(input_filename, "final-ir");
841
842         /* run the code generator */
843         if (firm_be_opt.selection != BE_NONE)
844                 do_codegen(out, input_filename);
845
846         if (firm_dump.statistic & STAT_FINAL)
847                 stat_dump_snapshot(input_filename, "final");
848 }
849
850 void disable_all_opts(void)
851 {
852         for (int i = 0; i < n_opts; ++i) {
853                 opt_config_t *config = &opts[i];
854                 if (config->flags & OPT_FLAG_ESSENTIAL) {
855                         config->flags |= OPT_FLAG_ENABLED;
856                 } else {
857                         config->flags &= ~OPT_FLAG_ENABLED;
858                 }
859         }
860 }
861
862 int firm_opt_option(const char *opt)
863 {
864         bool enable = true;
865         if (strncmp(opt, "no-", 3) == 0) {
866                 enable = false;
867                 opt = opt + 3;
868         }
869
870         opt_config_t *config = get_opt(opt);
871         if (config == NULL || (config->flags & OPT_FLAG_HIDE_OPTIONS))
872                 return 0;
873
874         config->flags &= ~OPT_FLAG_ENABLED;
875         config->flags |= enable ? OPT_FLAG_ENABLED : 0;
876         return 1;
877 }
878
879 void firm_opt_option_help(void)
880 {
881         int i;
882
883         for (i = 0; i < n_opts; ++i) {
884                 char buf[1024];
885                 char buf2[1024];
886
887                 const opt_config_t *config = &opts[i];
888                 if (config->flags & OPT_FLAG_HIDE_OPTIONS)
889                         continue;
890
891                 snprintf(buf2, sizeof(buf2), "firm: enable %s", config->description);
892                 print_option_help(config->name, buf2);
893                 snprintf(buf, sizeof(buf), "no-%s", config->name);
894                 snprintf(buf2, sizeof(buf2), "firm: disable %s", config->description);
895                 print_option_help(buf, buf2);
896         }
897 }
898
899 /**
900  * Do very early initializations
901  */
902 void firm_early_init(void)
903 {
904         /* arg: need this here for command line options */
905         be_opt_register();
906
907         enable_safe_defaults();
908 }