3 * @file firm_opt.c -- Firm-generating back end optimizations.
5 * (C) 2005-2010 Michael Beck beck@ipd.info.uni-karlsruhe.de
17 #include <libfirm/firm.h>
20 #include "firm_codegen.h"
21 #include "firm_cmdline.h"
22 #include "firm_timing.h"
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);
30 /** dump all the graphs depending on cond */
32 static void dump_all(const char *suffix)
34 if (!firm_dump.ir_graph)
37 timer_push(t_vcg_dump);
38 dump_all_ir_graphs(suffix);
39 timer_pop(t_vcg_dump);
42 /* set by the backend parameters */
43 static const ir_settings_arch_dep_t *ad_param = NULL;
44 static create_intrinsic_fkt *arch_create_intrinsic = NULL;
45 static void *create_intrinsic_ctx = NULL;
46 static const ir_settings_if_conv_t *if_conv_info = NULL;
48 /* entities of runtime functions */
49 ir_entity_ptr rts_entities[rts_max];
52 * factory for setting architecture dependent parameters
54 static const ir_settings_arch_dep_t *arch_factory(void)
56 static const ir_settings_arch_dep_t param = {
57 1, /* also use subs */
58 4, /* maximum shifts */
59 31, /* maximum shift amount */
60 NULL, /* use default evaluator */
64 32 /* Mulh allowed up to 32 bit */
67 return ad_param ? ad_param : ¶m;
71 * Map runtime functions.
73 static void rts_map(void)
76 ir_entity_ptr *ent; /**< address of the rts entity */
77 i_mapper_func func; /**< mapper function. */
80 { &rts_entities[rts_abs], i_mapper_abs },
81 { &rts_entities[rts_labs], i_mapper_abs },
82 { &rts_entities[rts_llabs], i_mapper_abs },
83 { &rts_entities[rts_imaxabs], i_mapper_abs },
85 /* double -> double */
86 { &rts_entities[rts_fabs], i_mapper_abs },
87 { &rts_entities[rts_sqrt], i_mapper_sqrt },
88 { &rts_entities[rts_cbrt], i_mapper_cbrt },
89 { &rts_entities[rts_pow], i_mapper_pow },
90 { &rts_entities[rts_exp], i_mapper_exp },
91 { &rts_entities[rts_exp2], i_mapper_exp },
92 { &rts_entities[rts_exp10], i_mapper_exp },
93 { &rts_entities[rts_log], i_mapper_log },
94 { &rts_entities[rts_log2], i_mapper_log2 },
95 { &rts_entities[rts_log10], i_mapper_log10 },
96 { &rts_entities[rts_sin], i_mapper_sin },
97 { &rts_entities[rts_cos], i_mapper_cos },
98 { &rts_entities[rts_tan], i_mapper_tan },
99 { &rts_entities[rts_asin], i_mapper_asin },
100 { &rts_entities[rts_acos], i_mapper_acos },
101 { &rts_entities[rts_atan], i_mapper_atan },
102 { &rts_entities[rts_sinh], i_mapper_sinh },
103 { &rts_entities[rts_cosh], i_mapper_cosh },
104 { &rts_entities[rts_tanh], i_mapper_tanh },
107 { &rts_entities[rts_fabsf], i_mapper_abs },
108 { &rts_entities[rts_sqrtf], i_mapper_sqrt },
109 { &rts_entities[rts_cbrtf], i_mapper_cbrt },
110 { &rts_entities[rts_powf], i_mapper_pow },
111 { &rts_entities[rts_expf], i_mapper_exp },
112 { &rts_entities[rts_exp2f], i_mapper_exp },
113 { &rts_entities[rts_exp10f], i_mapper_exp },
114 { &rts_entities[rts_logf], i_mapper_log },
115 { &rts_entities[rts_log2f], i_mapper_log2 },
116 { &rts_entities[rts_log10f], i_mapper_log10 },
117 { &rts_entities[rts_sinf], i_mapper_sin },
118 { &rts_entities[rts_cosf], i_mapper_cos },
119 { &rts_entities[rts_tanf], i_mapper_tan },
120 { &rts_entities[rts_asinf], i_mapper_asin },
121 { &rts_entities[rts_acosf], i_mapper_acos },
122 { &rts_entities[rts_atanf], i_mapper_atan },
123 { &rts_entities[rts_sinhf], i_mapper_sinh },
124 { &rts_entities[rts_coshf], i_mapper_cosh },
125 { &rts_entities[rts_tanhf], i_mapper_tanh },
127 /* long double -> long double */
128 { &rts_entities[rts_fabsl], i_mapper_abs },
129 { &rts_entities[rts_sqrtl], i_mapper_sqrt },
130 { &rts_entities[rts_cbrtl], i_mapper_cbrt },
131 { &rts_entities[rts_powl], i_mapper_pow },
132 { &rts_entities[rts_expl], i_mapper_exp },
133 { &rts_entities[rts_exp2l], i_mapper_exp },
134 { &rts_entities[rts_exp10l], i_mapper_exp },
135 { &rts_entities[rts_logl], i_mapper_log },
136 { &rts_entities[rts_log2l], i_mapper_log2 },
137 { &rts_entities[rts_log10l], i_mapper_log10 },
138 { &rts_entities[rts_sinl], i_mapper_sin },
139 { &rts_entities[rts_cosl], i_mapper_cos },
140 { &rts_entities[rts_tanl], i_mapper_tan },
141 { &rts_entities[rts_asinl], i_mapper_asin },
142 { &rts_entities[rts_acosl], i_mapper_acos },
143 { &rts_entities[rts_atanl], i_mapper_atan },
144 { &rts_entities[rts_sinhl], i_mapper_sinh },
145 { &rts_entities[rts_coshl], i_mapper_cosh },
146 { &rts_entities[rts_tanhl], i_mapper_tanh },
149 { &rts_entities[rts_strcmp], i_mapper_strcmp },
150 { &rts_entities[rts_strncmp], i_mapper_strncmp },
151 { &rts_entities[rts_strcpy], i_mapper_strcpy },
152 { &rts_entities[rts_strlen], i_mapper_strlen },
153 { &rts_entities[rts_memcpy], i_mapper_memcpy },
154 { &rts_entities[rts_mempcpy], i_mapper_mempcpy },
155 { &rts_entities[rts_memmove], i_mapper_memmove },
156 { &rts_entities[rts_memset], i_mapper_memset },
157 { &rts_entities[rts_memcmp], i_mapper_memcmp }
159 i_record rec[sizeof(mapper)/sizeof(mapper[0])];
162 for (i = n_map = 0; i < sizeof(mapper)/sizeof(mapper[0]); ++i) {
163 if (*mapper[i].ent != NULL) {
164 rec[n_map].i_call.kind = INTRINSIC_CALL;
165 rec[n_map].i_call.i_ent = *mapper[i].ent;
166 rec[n_map].i_call.i_mapper = mapper[i].func;
167 rec[n_map].i_call.ctx = NULL;
168 rec[n_map].i_call.link = NULL;
174 lower_intrinsics(rec, n_map, /* part_block_used=*/0);
177 static int *irg_dump_no;
178 static int firm_const_exists;
180 static void do_optimize_funccalls(void)
182 optimize_funccalls(firm_const_exists, NULL);
185 static void do_gcse(ir_graph *irg)
187 set_opt_global_cse(1);
188 optimize_graph_df(irg);
190 set_opt_global_cse(0);
193 static void do_lower_highlevel(ir_graph *irg)
195 lower_highlevel_graph(irg, firm_opt.lower_bitfields);
198 static void do_if_conv(ir_graph *irg)
200 opt_if_conv(irg, if_conv_info);
203 static void do_stred(ir_graph *irg)
205 opt_osr(irg, osr_flag_default | osr_flag_keep_reg_pressure | osr_flag_ignore_x86_shift);
208 static void after_inline_opt(ir_graph *irg)
210 do_irg_opt(irg, "scalar-replace");
211 do_irg_opt(irg, "local");
212 do_irg_opt(irg, "control-flow");
213 do_irg_opt(irg, "combo");
216 static void do_inline(void)
218 inline_functions(firm_opt.inline_maxsize, firm_opt.inline_threshold,
222 static void do_cloning(void)
224 proc_cloning((float) firm_opt.clone_threshold);
227 static void do_lower_switch(ir_graph *irg)
229 lower_switch(irg, firm_opt.spare_size);
232 static void do_lower_mux(ir_graph *irg)
234 lower_mux(irg, NULL);
237 static void do_lower_dw_ops(void)
239 lwrdw_param_t init = {
242 get_atomic_mode(ATOMIC_TYPE_LONGLONG),
243 get_atomic_mode(ATOMIC_TYPE_ULONGLONG),
244 get_atomic_mode(ATOMIC_TYPE_INT),
245 get_atomic_mode(ATOMIC_TYPE_UINT),
246 def_create_intrinsic_fkt,
250 if (arch_create_intrinsic) {
251 init.create_intrinsic = arch_create_intrinsic;
252 init.ctx = create_intrinsic_ctx;
257 static void do_vrp(ir_graph *irg)
262 typedef enum opt_target {
263 OPT_TARGET_IRG, /**< optimization function works on a single graph */
264 OPT_TARGET_IRP /**< optimization function works on the complete program */
267 typedef enum opt_flags {
269 OPT_FLAG_ENABLED = 1 << 0, /**< enable the optimization */
270 OPT_FLAG_NO_DUMP = 1 << 1, /**< don't dump after transformation */
271 OPT_FLAG_NO_VERIFY = 1 << 2, /**< don't verify after transformation */
272 OPT_FLAG_HIDE_OPTIONS = 1 << 3, /**< do not automatically process
273 -foptions for this transformation */
274 OPT_FLAG_ESSENTIAL = 1 << 4, /**< output won't work without this pass
275 so we need it even with -O0 */
278 typedef void (*transform_irg_func)(ir_graph *irg);
279 typedef void (*transform_irp_func)(void);
285 transform_irg_func transform_irg;
286 transform_irp_func transform_irp;
288 const char *description;
292 static opt_config_t opts[] = {
293 #define IRG(a, b, c, d) { OPT_TARGET_IRG, a, .u.transform_irg = (transform_irg_func)b, c, d }
294 #define IRP(a, b, c, d) { OPT_TARGET_IRP, a, .u.transform_irp = b, c, d }
295 IRG("bool", opt_bool, "bool simplification", OPT_FLAG_NONE),
296 IRG("combo", combo, "combined CCE, UCE and GVN", OPT_FLAG_NONE),
297 IRG("confirm", construct_confirms, "confirm optimisation", OPT_FLAG_HIDE_OPTIONS),
298 IRG("control-flow", optimize_cf, "optimization of control-flow", OPT_FLAG_HIDE_OPTIONS),
299 IRG("dead", dead_node_elimination, "dead node elimination", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
300 IRG("deconv", conv_opt, "conv node elimination", OPT_FLAG_NONE),
301 IRG("fp-vrp", fixpoint_vrp, "fixpoint value range propagation", OPT_FLAG_NONE),
302 IRG("frame", opt_frame_irg, "remove unused frame entities", OPT_FLAG_NONE),
303 IRG("gcse", do_gcse, "global common subexpression elimination", OPT_FLAG_NONE),
304 IRG("gvn-pre", do_gvn_pre, "global value numbering partial redundancy elimination", OPT_FLAG_NONE),
305 IRG("if-conversion", do_if_conv, "if-conversion", OPT_FLAG_NONE),
306 IRG("invert-loops", do_loop_inversion, "loop inversion", OPT_FLAG_NONE),
307 IRG("ivopts", do_stred, "induction variable strength reduction", OPT_FLAG_NONE),
308 IRG("local", optimize_graph_df, "local graph optimizations", OPT_FLAG_HIDE_OPTIONS),
309 IRG("lower", do_lower_highlevel, "lowering", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
310 IRG("lower-mux", do_lower_mux, "mux lowering", OPT_FLAG_NONE),
311 IRG("lower-switch", do_lower_switch, "switch lowering", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
312 IRG("one-return", normalize_one_return, "normalisation to 1 return", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
313 IRG("opt-load-store", optimize_load_store, "load store optimization", OPT_FLAG_NONE),
314 IRG("opt-tail-rec", opt_tail_rec_irg, "tail-recursion eliminiation", OPT_FLAG_NONE),
315 IRG("parallelize-mem", opt_parallelize_mem, "parallelize memory", OPT_FLAG_NONE),
316 IRG("place", place_code, "code placement", OPT_FLAG_NONE),
317 IRG("reassociation", optimize_reassociation, "reassociation", OPT_FLAG_NONE),
318 IRG("remove-confirms", remove_confirms, "confirm removal", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
319 IRG("remove-phi-cycles", remove_phi_cycles, "removal of phi cycles", OPT_FLAG_HIDE_OPTIONS),
320 IRG("scalar-replace", scalar_replacement_opt, "scalar replacement", OPT_FLAG_NONE),
321 IRG("shape-blocks", shape_blocks, "block shaping", OPT_FLAG_NONE),
322 IRG("thread-jumps", opt_jumpthreading, "path-sensitive jumpthreading", OPT_FLAG_NONE),
323 IRG("unroll-loops", do_loop_unrolling, "loop unrolling", OPT_FLAG_NONE),
324 IRG("vrp", do_vrp, "value range propagation", OPT_FLAG_NONE),
325 IRP("inline", do_inline, "inlining", OPT_FLAG_NONE),
326 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),
327 IRP("lower-dw", do_lower_dw_ops, "lowering of doubleword operations", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
328 IRP("opt-func-call", do_optimize_funccalls, "function call optimization", OPT_FLAG_NONE),
329 IRP("opt-proc-clone", do_cloning, "procedure cloning", OPT_FLAG_NONE),
330 IRP("remove-unused", garbage_collect_entities, "removal of unused functions/variables", OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
331 IRP("rts", rts_map, "optimization of known library functions", OPT_FLAG_HIDE_OPTIONS),
335 static const int n_opts = sizeof(opts) / sizeof(opts[0]);
336 ir_timer_t *timers[sizeof(opts)/sizeof(opts[0])];
338 static opt_config_t *get_opt(const char *name)
341 for (i = 0; i < n_opts; ++i) {
342 opt_config_t *config = &opts[i];
343 if (strcmp(config->name, name) == 0)
350 static void set_opt_enabled(const char *name, bool enabled)
352 opt_config_t *config = get_opt(name);
353 config->flags = (config->flags & ~OPT_FLAG_ENABLED)
354 | (enabled ? OPT_FLAG_ENABLED : 0);
357 static bool get_opt_enabled(const char *name)
359 opt_config_t *config = get_opt(name);
360 return (config->flags & OPT_FLAG_ENABLED) != 0;
364 * perform an optimisation on a single graph
366 * @return true if something changed, false otherwise
368 static bool do_irg_opt(ir_graph *irg, const char *name)
371 opt_config_t *config = get_opt(name);
372 size_t n = config - opts;
373 assert(config != NULL);
374 assert(config->target == OPT_TARGET_IRG);
375 if (! (config->flags & OPT_FLAG_ENABLED))
378 old_irg = current_ir_graph;
379 current_ir_graph = irg;
381 timer_push(timers[n]);
382 config->u.transform_irg(irg);
383 timer_pop(timers[n]);
385 if (firm_dump.all_phases && firm_dump.ir_graph) {
386 dump_ir_graph(irg, name);
389 if (firm_opt.check_all) {
390 timer_push(t_verify);
391 irg_verify(irg, VERIFY_ENFORCE_SSA);
395 current_ir_graph = old_irg;
399 static void do_irp_opt(const char *name)
401 opt_config_t *config = get_opt(name);
402 size_t n = config - opts;
403 assert(config->target == OPT_TARGET_IRP);
404 if (! (config->flags & OPT_FLAG_ENABLED))
407 timer_push(timers[n]);
408 config->u.transform_irp();
409 timer_pop(timers[n]);
411 if (firm_dump.ir_graph && firm_dump.all_phases) {
413 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
414 ir_graph *irg = get_irp_irg(i);
415 dump_ir_graph(irg, name);
419 if (firm_opt.check_all) {
421 timer_push(t_verify);
422 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
423 irg_verify(get_irp_irg(i), VERIFY_ENFORCE_SSA);
430 * Enable transformations which should be always safe (and cheap) to perform
432 static void enable_safe_defaults(void)
434 set_opt_enabled("remove-unused", true);
435 set_opt_enabled("opt-tail-rec", true);
436 set_opt_enabled("opt-func-call", true);
437 set_opt_enabled("reassociation", true);
438 set_opt_enabled("control-flow", true);
439 set_opt_enabled("local", true);
440 set_opt_enabled("lower-const", true);
441 set_opt_enabled("scalar-replace", true);
442 set_opt_enabled("place", true);
443 set_opt_enabled("confirm", true);
444 set_opt_enabled("opt-load-store", true);
445 set_opt_enabled("lower", true);
446 set_opt_enabled("deconv", true);
447 set_opt_enabled("remove-confirms", true);
448 set_opt_enabled("ivopts", true);
449 set_opt_enabled("dead", true);
450 set_opt_enabled("lower-switch", true);
451 set_opt_enabled("remove-phi-cycles", true);
452 set_opt_enabled("frame", true);
453 set_opt_enabled("combo", true);
454 set_opt_enabled("invert-loops", true);
458 * run all the Firm optimizations
460 * @param input_filename the name of the (main) source file
462 static void do_firm_optimizations(const char *input_filename)
467 set_opt_alias_analysis(firm_opt.alias_analysis);
469 aa_opt = aa_opt_no_opt;
470 if (firm_opt.strict_alias)
471 aa_opt |= aa_opt_type_based | aa_opt_byte_type_may_alias;
472 if (firm_opt.no_alias)
473 aa_opt = aa_opt_no_alias;
475 set_irp_memory_disambiguator_options(aa_opt);
477 /* parameter passing code should set them directly sometime... */
478 set_opt_enabled("rts", !firm_opt.freestanding);
479 set_opt_enabled("gcse", firm_opt.gcse);
480 set_opt_enabled("place", !firm_opt.gcse);
481 set_opt_enabled("confirm", firm_opt.confirm);
482 set_opt_enabled("remove-confirms", firm_opt.confirm);
484 /* osr supersedes remove_phi_cycles */
485 if (get_opt_enabled("ivopts"))
486 set_opt_enabled("remove-phi-cycles", false);
488 timer_start(t_all_opt);
492 /* first step: kill dead code */
493 for (i = 0; i < get_irp_n_irgs(); i++) {
494 ir_graph *irg = get_irp_irg(i);
495 do_irg_opt(irg, "combo");
496 do_irg_opt(irg, "local");
497 do_irg_opt(irg, "control-flow");
500 do_irp_opt("remove-unused");
501 for (i = 0; i < get_irp_n_irgs(); ++i) {
502 ir_graph *irg = get_irp_irg(i);
503 do_irg_opt(irg, "opt-tail-rec");
505 do_irp_opt("opt-func-call");
506 do_irp_opt("lower-const");
508 for (i = 0; i < get_irp_n_irgs(); i++) {
509 ir_graph *irg = get_irp_irg(i);
511 do_irg_opt(irg, "scalar-replace");
512 do_irg_opt(irg, "invert-loops");
513 do_irg_opt(irg, "unroll-loops");
514 do_irg_opt(irg, "local");
515 do_irg_opt(irg, "reassociation");
516 do_irg_opt(irg, "local");
517 do_irg_opt(irg, "gcse");
519 if (firm_opt.confirm) {
520 /* Confirm construction currently can only handle blocks with only
521 one control flow predecessor. Calling optimize_cf here removes
522 Bad predecessors and help the optimization of switch constructs.
524 do_irg_opt(irg, "control-flow");
525 do_irg_opt(irg, "confirm");
526 do_irg_opt(irg, "vrp");
527 do_irg_opt(irg, "local");
530 do_irg_opt(irg, "control-flow");
531 do_irg_opt(irg, "opt-load-store");
532 do_irg_opt(irg, "fp-vrp");
533 do_irg_opt(irg, "lower");
534 do_irg_opt(irg, "deconv");
535 do_irg_opt(irg, "thread-jumps");
536 do_irg_opt(irg, "remove-confirms");
537 do_irg_opt(irg, "gvn-pre");
538 do_irg_opt(irg, "place");
539 do_irg_opt(irg, "control-flow");
541 if (do_irg_opt(irg, "if-conversion")) {
542 do_irg_opt(irg, "local");
543 do_irg_opt(irg, "control-flow");
545 /* this doesn't make too much sense but tests the mux destruction... */
546 do_irg_opt(irg, "lower-mux");
548 do_irg_opt(irg, "bool");
549 do_irg_opt(irg, "shape-blocks");
550 do_irg_opt(irg, "lower-switch");
551 do_irg_opt(irg, "ivopts");
552 do_irg_opt(irg, "local");
553 do_irg_opt(irg, "dead");
554 do_irg_opt(irg, "frame");
557 do_irp_opt("inline");
558 do_irp_opt("opt-proc-clone");
560 for (i = 0; i < get_irp_n_irgs(); i++) {
561 ir_graph *irg = get_irp_irg(i);
562 do_irg_opt(irg, "local");
563 do_irg_opt(irg, "control-flow");
564 do_irg_opt(irg, "thread-jumps");
565 do_irg_opt(irg, "local");
566 do_irg_opt(irg, "control-flow");
568 if( do_irg_opt(irg, "vrp") ) { // if vrp is enabled
569 do_irg_opt(irg, "local");
570 do_irg_opt(irg, "vrp");
571 do_irg_opt(irg, "local");
572 do_irg_opt(irg, "vrp");
576 if (firm_dump.ir_graph) {
577 /* recompute backedges for nicer dumps */
578 for (i = 0; i < get_irp_n_irgs(); i++)
579 construct_cf_backedges(get_irp_irg(i));
584 if (firm_dump.statistic & STAT_AFTER_OPT)
585 stat_dump_snapshot(input_filename, "opt");
587 timer_stop(t_all_opt);
593 * @param input_filename the name of the (main) source file
595 static void do_firm_lowering(const char *input_filename)
599 do_irp_opt("lower-dw");
601 if (firm_dump.statistic & STAT_AFTER_LOWER)
602 stat_dump_snapshot(input_filename, "low");
606 if (firm_opt.enabled) {
607 timer_start(t_all_opt);
609 /* run reassociation first on all graphs BEFORE the architecture
610 dependent optimizations are enabled */
611 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
612 ir_graph *irg = get_irp_irg(i);
613 do_irg_opt(irg, "reassociation");
616 /* enable architecture dependent optimizations */
617 arch_dep_set_opts((arch_dep_opts_t)
618 ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) |
619 (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) |
620 (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) ));
622 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
623 ir_graph *irg = get_irp_irg(i);
625 do_irg_opt(irg, "local");
626 do_irg_opt(irg, "gcse");
627 do_irg_opt(irg, "opt-load-store");
628 do_irg_opt(irg, "local");
629 do_irg_opt(irg, "control-flow");
631 if (do_irg_opt(irg, "vrp")) {
632 do_irg_opt(irg, "local");
633 do_irg_opt(irg, "control-flow");
634 do_irg_opt(irg, "vrp");
635 do_irg_opt(irg, "local");
636 do_irg_opt(irg, "control-flow");
639 if (do_irg_opt(irg, "if-conversion")) {
640 do_irg_opt(irg, "local");
641 do_irg_opt(irg, "control-flow");
644 do_irg_opt(irg, "parallelize-mem");
646 timer_stop(t_all_opt);
648 do_irp_opt("remove-unused");
654 mark_private_methods();
656 /* set the phase to low */
657 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
658 ir_graph *irg = get_irp_irg(i);
659 set_irg_phase_state(irg, phase_low);
661 set_irp_phase_state(phase_low);
663 if (firm_dump.statistic & STAT_FINAL) {
664 stat_dump_snapshot(input_filename, "final");
669 * Initialize for the Firm-generating back end.
671 void gen_firm_init(void)
673 unsigned pattern = 0;
676 for (i = 0; i < n_opts; ++i) {
677 timers[i] = ir_timer_new();
678 timer_register(timers[i], opts[i].description);
680 t_verify = ir_timer_new();
681 timer_register(t_verify, "Firm: verify pass");
682 t_vcg_dump = ir_timer_new();
683 timer_register(t_vcg_dump, "Firm: vcg dumping");
684 t_all_opt = ir_timer_new();
685 timer_register(t_all_opt, "Firm: all optimizations");
687 if (firm_dump.stat_pattern)
688 pattern |= FIRMSTAT_PATTERN_ENABLED;
690 if (firm_dump.stat_dag)
691 pattern |= FIRMSTAT_COUNT_DAG;
694 firm_init_stat(firm_dump.statistic == STAT_NONE ?
695 0 : FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP
696 | FIRMSTAT_COUNT_CONSTS | pattern);
698 if (firm_be_opt.selection == BE_FIRM_BE) {
699 const backend_params *be_params = be_get_backend_param();
701 if (be_params->do_dw_lowering)
702 set_opt_enabled("lower-dw", true);
704 arch_create_intrinsic = be_params->arch_create_intrinsic_fkt;
705 create_intrinsic_ctx = be_params->create_intrinsic_ctx;
707 ad_param = be_params->dep_param;
708 if_conv_info = be_params->if_conv_info;
711 edges_init_dbg(firm_opt.verify_edges);
713 /* Sel node cannot produce NULL pointers */
714 set_opt_sel_based_null_check_elim(1);
716 /* dynamic dispatch works currently only if whole world scenarios */
717 set_opt_dyn_meth_dispatch(0);
719 arch_dep_init(arch_factory);
721 /* do not run architecture dependent optimizations in building phase */
722 arch_dep_set_opts(arch_dep_none);
724 do_node_verification((firm_verification_t) firm_opt.verify);
725 if (firm_dump.filter != NULL)
726 ir_set_dump_filter(firm_dump.filter);
728 ir_add_dump_flags(ir_dump_flag_group_extbb);
729 if (firm_dump.no_blocks)
730 ir_remove_dump_flags(ir_dump_flag_blocks_as_subgraphs);
732 if (firm_opt.enabled) {
734 set_opt_constant_folding(firm_opt.const_folding);
735 set_opt_algebraic_simplification(firm_opt.const_folding);
736 set_opt_cse(firm_opt.cse);
737 set_opt_global_cse(0);
738 set_opt_unreachable_code(1);
739 set_opt_control_flow(firm_opt.control_flow);
740 set_opt_control_flow_weak_simplification(1);
741 set_opt_control_flow_strong_simplification(1);
748 * Called, after the Firm generation is completed,
749 * do all optimizations and backend call here.
751 * @param out a file handle for the output, may be NULL
752 * @param input_filename the name of the (main) source file
753 * @param c_mode non-zero if "C" was compiled
754 * @param new_firm_const_exists non-zero, if the const attribute was used on functions
756 void gen_firm_finish(FILE *out, const char *input_filename,
757 int new_firm_const_exists)
762 if (firm_opt.enable_statev) {
764 snprintf(buf, sizeof(buf), "%s.ev", input_filename);
765 ir_stat_ev_begin(input_filename, firm_opt.statev_filter);
766 ir_stat_ev_compilation_unit(input_filename);
770 firm_const_exists = new_firm_const_exists;
772 /* the general for dumping option must be set, or the others will not work*/
774 = (a_byte) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb);
776 ir_add_dump_flags(ir_dump_flag_keepalive_edges
777 | ir_dump_flag_consts_local | ir_dump_flag_dominance);
778 ir_remove_dump_flags(ir_dump_flag_loops | ir_dump_flag_ld_names);
780 /* FIXME: cloning might ADD new graphs. */
781 irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
783 if (firm_dump.all_types) {
784 dump_ir_prog_ext(dump_typegraph, "types.vcg");
787 /* finalize all graphs */
788 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
789 ir_graph *irg = get_irp_irg(i);
790 irg_finalize_cons(irg);
794 timer_push(t_verify);
798 /* all graphs are finalized, set the irp phase to high */
799 set_irp_phase_state(phase_high);
801 /* BEWARE: kill unreachable code before doing compound lowering */
802 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
803 ir_graph *irg = get_irp_irg(i);
804 do_irg_opt(irg, "control-flow");
807 /* lower all compound call return values */
808 lower_compound_params();
810 /* lower copyb nodes */
811 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
812 ir_graph *irg = get_irp_irg(i);
813 lower_CopyB(irg, 128, 4);
816 if (firm_dump.statistic & STAT_BEFORE_OPT) {
817 stat_dump_snapshot(input_filename, "noopt");
820 if (firm_opt.enabled)
821 do_firm_optimizations(input_filename);
824 do_firm_lowering(input_filename);
826 /* set the phase to low */
827 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
828 set_irg_phase_state(get_irp_irg(i), phase_low);
830 if (firm_dump.statistic & STAT_FINAL_IR)
831 stat_dump_snapshot(input_filename, "final-ir");
833 /* run the code generator */
834 if (firm_be_opt.selection != BE_NONE)
835 do_codegen(out, input_filename);
837 if (firm_dump.statistic & STAT_FINAL)
838 stat_dump_snapshot(input_filename, "final");
841 void disable_all_opts(void)
843 for (int i = 0; i < n_opts; ++i) {
844 opt_config_t *config = &opts[i];
845 if (config->flags & OPT_FLAG_ESSENTIAL) {
846 config->flags |= OPT_FLAG_ENABLED;
848 config->flags &= ~OPT_FLAG_ENABLED;
853 int firm_opt_option(const char *opt)
856 if (strncmp(opt, "no-", 3) == 0) {
861 opt_config_t *config = get_opt(opt);
862 if (config == NULL || (config->flags & OPT_FLAG_HIDE_OPTIONS))
865 config->flags &= ~OPT_FLAG_ENABLED;
866 config->flags |= enable ? OPT_FLAG_ENABLED : 0;
870 void firm_opt_option_help(void)
874 for (i = 0; i < n_opts; ++i) {
878 const opt_config_t *config = &opts[i];
879 if (config->flags & OPT_FLAG_HIDE_OPTIONS)
882 snprintf(buf2, sizeof(buf2), "firm: enable %s", config->description);
883 print_option_help(config->name, buf2);
884 snprintf(buf, sizeof(buf), "no-%s", config->name);
885 snprintf(buf2, sizeof(buf2), "firm: disable %s", config->description);
886 print_option_help(buf, buf2);
891 * Do very early initializations
893 void firm_early_init(void)
895 /* arg: need this here for command line options */
898 enable_safe_defaults();