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) {
35 timer_push(t_vcg_dump);
36 if (firm_dump.no_blocks)
37 dump_all_ir_graphs(dump_ir_graph, suffix);
38 else if (firm_dump.extbb)
39 dump_all_ir_graphs(dump_ir_extblock_graph, suffix);
41 dump_all_ir_graphs(dump_ir_block_graph, suffix);
42 timer_pop(t_vcg_dump);
46 /* set by the backend parameters */
47 static const ir_settings_arch_dep_t *ad_param = NULL;
48 static create_intrinsic_fkt *arch_create_intrinsic = NULL;
49 static void *create_intrinsic_ctx = NULL;
50 static const ir_settings_if_conv_t *if_conv_info = NULL;
52 /* entities of runtime functions */
53 ir_entity_ptr rts_entities[rts_max];
56 * factory for setting architecture dependent parameters
58 static const ir_settings_arch_dep_t *arch_factory(void)
60 static const ir_settings_arch_dep_t param = {
61 1, /* also use subs */
62 4, /* maximum shifts */
63 31, /* maximum shift amount */
64 NULL, /* use default evaluator */
68 32 /* Mulh allowed up to 32 bit */
71 return ad_param ? ad_param : ¶m;
75 * Map runtime functions.
77 static void rts_map(void)
80 ir_entity_ptr *ent; /**< address of the rts entity */
81 i_mapper_func func; /**< mapper function. */
84 { &rts_entities[rts_abs], i_mapper_abs },
85 { &rts_entities[rts_labs], i_mapper_abs },
86 { &rts_entities[rts_llabs], i_mapper_abs },
87 { &rts_entities[rts_imaxabs], i_mapper_abs },
89 /* double -> double */
90 { &rts_entities[rts_fabs], i_mapper_abs },
91 { &rts_entities[rts_sqrt], i_mapper_sqrt },
92 { &rts_entities[rts_cbrt], i_mapper_cbrt },
93 { &rts_entities[rts_pow], i_mapper_pow },
94 { &rts_entities[rts_exp], i_mapper_exp },
95 { &rts_entities[rts_exp2], i_mapper_exp },
96 { &rts_entities[rts_exp10], i_mapper_exp },
97 { &rts_entities[rts_log], i_mapper_log },
98 { &rts_entities[rts_log2], i_mapper_log2 },
99 { &rts_entities[rts_log10], i_mapper_log10 },
100 { &rts_entities[rts_sin], i_mapper_sin },
101 { &rts_entities[rts_cos], i_mapper_cos },
102 { &rts_entities[rts_tan], i_mapper_tan },
103 { &rts_entities[rts_asin], i_mapper_asin },
104 { &rts_entities[rts_acos], i_mapper_acos },
105 { &rts_entities[rts_atan], i_mapper_atan },
106 { &rts_entities[rts_sinh], i_mapper_sinh },
107 { &rts_entities[rts_cosh], i_mapper_cosh },
108 { &rts_entities[rts_tanh], i_mapper_tanh },
111 { &rts_entities[rts_fabsf], i_mapper_abs },
112 { &rts_entities[rts_sqrtf], i_mapper_sqrt },
113 { &rts_entities[rts_cbrtf], i_mapper_cbrt },
114 { &rts_entities[rts_powf], i_mapper_pow },
115 { &rts_entities[rts_expf], i_mapper_exp },
116 { &rts_entities[rts_exp2f], i_mapper_exp },
117 { &rts_entities[rts_exp10f], i_mapper_exp },
118 { &rts_entities[rts_logf], i_mapper_log },
119 { &rts_entities[rts_log2f], i_mapper_log2 },
120 { &rts_entities[rts_log10f], i_mapper_log10 },
121 { &rts_entities[rts_sinf], i_mapper_sin },
122 { &rts_entities[rts_cosf], i_mapper_cos },
123 { &rts_entities[rts_tanf], i_mapper_tan },
124 { &rts_entities[rts_asinf], i_mapper_asin },
125 { &rts_entities[rts_acosf], i_mapper_acos },
126 { &rts_entities[rts_atanf], i_mapper_atan },
127 { &rts_entities[rts_sinhf], i_mapper_sinh },
128 { &rts_entities[rts_coshf], i_mapper_cosh },
129 { &rts_entities[rts_tanhf], i_mapper_tanh },
131 /* long double -> long double */
132 { &rts_entities[rts_fabsl], i_mapper_abs },
133 { &rts_entities[rts_sqrtl], i_mapper_sqrt },
134 { &rts_entities[rts_cbrtl], i_mapper_cbrt },
135 { &rts_entities[rts_powl], i_mapper_pow },
136 { &rts_entities[rts_expl], i_mapper_exp },
137 { &rts_entities[rts_exp2l], i_mapper_exp },
138 { &rts_entities[rts_exp10l], i_mapper_exp },
139 { &rts_entities[rts_logl], i_mapper_log },
140 { &rts_entities[rts_log2l], i_mapper_log2 },
141 { &rts_entities[rts_log10l], i_mapper_log10 },
142 { &rts_entities[rts_sinl], i_mapper_sin },
143 { &rts_entities[rts_cosl], i_mapper_cos },
144 { &rts_entities[rts_tanl], i_mapper_tan },
145 { &rts_entities[rts_asinl], i_mapper_asin },
146 { &rts_entities[rts_acosl], i_mapper_acos },
147 { &rts_entities[rts_atanl], i_mapper_atan },
148 { &rts_entities[rts_sinhl], i_mapper_sinh },
149 { &rts_entities[rts_coshl], i_mapper_cosh },
150 { &rts_entities[rts_tanhl], i_mapper_tanh },
153 { &rts_entities[rts_strcmp], i_mapper_strcmp },
154 { &rts_entities[rts_strncmp], i_mapper_strncmp },
155 { &rts_entities[rts_strcpy], i_mapper_strcpy },
156 { &rts_entities[rts_strlen], i_mapper_strlen },
157 { &rts_entities[rts_memcpy], i_mapper_memcpy },
158 { &rts_entities[rts_mempcpy], i_mapper_mempcpy },
159 { &rts_entities[rts_memmove], i_mapper_memmove },
160 { &rts_entities[rts_memset], i_mapper_memset },
161 { &rts_entities[rts_memcmp], i_mapper_memcmp }
163 i_record rec[sizeof(mapper)/sizeof(mapper[0])];
166 for (i = n_map = 0; i < sizeof(mapper)/sizeof(mapper[0]); ++i) {
167 if (*mapper[i].ent != NULL) {
168 rec[n_map].i_call.kind = INTRINSIC_CALL;
169 rec[n_map].i_call.i_ent = *mapper[i].ent;
170 rec[n_map].i_call.i_mapper = mapper[i].func;
171 rec[n_map].i_call.ctx = NULL;
172 rec[n_map].i_call.link = NULL;
178 lower_intrinsics(rec, n_map, /* part_block_used=*/0);
181 static int *irg_dump_no;
183 static void dump_graph_count(ir_graph *const irg, const char *const suffix)
186 snprintf(name, sizeof(name), "-%02d_%s", irg_dump_no[get_irg_idx(irg)]++,
189 timer_push(t_vcg_dump);
190 if (firm_dump.no_blocks)
191 dump_ir_graph(irg, name);
192 else if (firm_dump.extbb)
193 dump_ir_extblock_graph(irg, name);
195 dump_ir_block_graph(irg, name);
196 timer_pop(t_vcg_dump);
199 static int firm_const_exists;
201 static void do_optimize_funccalls(void)
203 optimize_funccalls(firm_const_exists, NULL);
206 static void do_gcse(ir_graph *irg)
208 set_opt_global_cse(1);
209 optimize_graph_df(irg);
211 set_opt_global_cse(0);
214 static void do_lower_highlevel(ir_graph *irg)
216 lower_highlevel_graph(irg, firm_opt.lower_bitfields);
219 static void do_if_conv(ir_graph *irg)
221 opt_if_conv(irg, if_conv_info);
224 static void do_stred(ir_graph *irg)
226 opt_osr(irg, osr_flag_default | osr_flag_keep_reg_pressure | osr_flag_ignore_x86_shift);
229 static void after_inline_opt(ir_graph *irg)
231 do_irg_opt(irg, "scalar-replace");
232 do_irg_opt(irg, "local");
233 do_irg_opt(irg, "control-flow");
234 do_irg_opt(irg, "combo");
237 static void do_inline(void)
239 inline_functions(firm_opt.inline_maxsize, firm_opt.inline_threshold,
243 static void do_cloning(void)
245 proc_cloning((float) firm_opt.clone_threshold);
248 static void do_lower_switch(ir_graph *irg)
250 lower_switch(irg, firm_opt.spare_size);
253 static void do_lower_mux(ir_graph *irg)
255 lower_mux(irg, NULL);
258 static void do_lower_dw_ops(void)
260 lwrdw_param_t init = {
263 get_atomic_mode(ATOMIC_TYPE_LONGLONG),
264 get_atomic_mode(ATOMIC_TYPE_ULONGLONG),
265 get_atomic_mode(ATOMIC_TYPE_INT),
266 get_atomic_mode(ATOMIC_TYPE_UINT),
267 def_create_intrinsic_fkt,
271 if (arch_create_intrinsic) {
272 init.create_intrinsic = arch_create_intrinsic;
273 init.ctx = create_intrinsic_ctx;
278 static void do_vrp(ir_graph *irg)
283 typedef enum opt_target {
284 OPT_TARGET_IRG, /**< optimization function works on a single graph */
285 OPT_TARGET_IRP /**< optimization function works on the complete program */
288 typedef enum opt_flags {
290 OPT_FLAG_ENABLED = 1 << 0, /**< enable the optimization */
291 OPT_FLAG_NO_DUMP = 1 << 1, /**< don't dump after transformation */
292 OPT_FLAG_NO_VERIFY = 1 << 2, /**< don't verify after transformation */
293 OPT_FLAG_HIDE_OPTIONS = 1 << 3, /**< do not automatically process
294 -foptions for this transformation */
295 OPT_FLAG_ESSENTIAL = 1 << 4, /**< output won't work without this pass
296 so we need it even with -O0 */
299 typedef void (*transform_irg_func)(ir_graph *irg);
300 typedef void (*transform_irp_func)(void);
306 transform_irg_func transform_irg;
307 transform_irp_func transform_irp;
309 const char *description;
313 static opt_config_t opts[] = {
314 #define IRG(a, b, c, d) { OPT_TARGET_IRG, a, .u.transform_irg = (transform_irg_func)b, c, d }
315 #define IRP(a, b, c, d) { OPT_TARGET_IRP, a, .u.transform_irp = b, c, d }
316 IRG("bool", opt_bool, "bool simplification", OPT_FLAG_NONE),
317 IRG("combo", combo, "combined CCE, UCE and GVN", OPT_FLAG_NONE),
318 IRG("confirm", construct_confirms, "confirm optimisation", OPT_FLAG_HIDE_OPTIONS),
319 IRG("control-flow", optimize_cf, "optimization of control-flow", OPT_FLAG_HIDE_OPTIONS),
320 IRG("dead", dead_node_elimination, "dead node elimination", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
321 IRG("deconv", conv_opt, "conv node elimination", OPT_FLAG_NONE),
322 IRG("fp-vrp", fixpoint_vrp, "fixpoint value range propagation", OPT_FLAG_NONE),
323 IRG("frame", opt_frame_irg, "remove unused frame entities", OPT_FLAG_NONE),
324 IRG("gcse", do_gcse, "global common subexpression elimination", OPT_FLAG_NONE),
325 IRG("gvn-pre", do_gvn_pre, "global value numbering partial redundancy elimination", OPT_FLAG_NONE),
326 IRG("if-conversion", do_if_conv, "if-conversion", OPT_FLAG_NONE),
327 IRG("invert-loops", do_loop_inversion, "loop inversion", OPT_FLAG_NONE),
328 IRG("ivopts", do_stred, "induction variable strength reduction", OPT_FLAG_NONE),
329 IRG("local", optimize_graph_df, "local graph optimizations", OPT_FLAG_HIDE_OPTIONS),
330 IRG("lower", do_lower_highlevel, "lowering", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
331 IRG("lower-mux", do_lower_mux, "mux lowering", OPT_FLAG_NONE),
332 IRG("lower-switch", do_lower_switch, "switch lowering", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
333 IRG("one-return", normalize_one_return, "normalisation to 1 return", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
334 IRG("opt-load-store", optimize_load_store, "load store optimization", OPT_FLAG_NONE),
335 IRG("opt-tail-rec", opt_tail_rec_irg, "tail-recursion eliminiation", OPT_FLAG_NONE),
336 IRG("parallelize-mem", opt_parallelize_mem, "parallelize memory", OPT_FLAG_NONE),
337 IRG("place", place_code, "code placement", OPT_FLAG_NONE),
338 IRG("reassociation", optimize_reassociation, "reassociation", OPT_FLAG_NONE),
339 IRG("remove-confirms", remove_confirms, "confirm removal", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
340 IRG("remove-phi-cycles", remove_phi_cycles, "removal of phi cycles", OPT_FLAG_HIDE_OPTIONS),
341 IRG("scalar-replace", scalar_replacement_opt, "scalar replacement", OPT_FLAG_NONE),
342 IRG("shape-blocks", shape_blocks, "block shaping", OPT_FLAG_NONE),
343 IRG("thread-jumps", opt_jumpthreading, "path-sensitive jumpthreading", OPT_FLAG_NONE),
344 IRG("unroll-loops", do_loop_unrolling, "loop unrolling", OPT_FLAG_NONE),
345 IRG("vrp", do_vrp, "value range propagation", OPT_FLAG_NONE),
346 IRP("inline", do_inline, "inlining", OPT_FLAG_NONE),
347 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),
348 IRP("lower-dw", do_lower_dw_ops, "lowering of doubleword operations", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
349 IRP("opt-func-call", do_optimize_funccalls, "function call optimization", OPT_FLAG_NONE),
350 IRP("opt-proc-clone", do_cloning, "procedure cloning", OPT_FLAG_NONE),
351 IRP("remove-unused", garbage_collect_entities, "removal of unused functions/variables", OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
352 IRP("rts", rts_map, "optimization of known library functions", OPT_FLAG_HIDE_OPTIONS),
356 static const int n_opts = sizeof(opts) / sizeof(opts[0]);
357 ir_timer_t *timers[sizeof(opts)/sizeof(opts[0])];
359 static opt_config_t *get_opt(const char *name)
362 for (i = 0; i < n_opts; ++i) {
363 opt_config_t *config = &opts[i];
364 if (strcmp(config->name, name) == 0)
371 static void set_opt_enabled(const char *name, bool enabled)
373 opt_config_t *config = get_opt(name);
374 config->flags = (config->flags & ~OPT_FLAG_ENABLED)
375 | (enabled ? OPT_FLAG_ENABLED : 0);
378 static bool get_opt_enabled(const char *name)
380 opt_config_t *config = get_opt(name);
381 return (config->flags & OPT_FLAG_ENABLED) != 0;
385 * perform an optimisation on a single graph
387 * @return true if something changed, false otherwise
389 static bool do_irg_opt(ir_graph *irg, const char *name)
392 opt_config_t *config = get_opt(name);
393 size_t n = config - opts;
394 assert(config != NULL);
395 assert(config->target == OPT_TARGET_IRG);
396 if (! (config->flags & OPT_FLAG_ENABLED))
399 old_irg = current_ir_graph;
400 current_ir_graph = irg;
402 timer_push(timers[n]);
403 config->u.transform_irg(irg);
404 timer_pop(timers[n]);
406 if (firm_dump.all_phases && firm_dump.ir_graph) {
407 dump_graph_count(irg, name);
410 if (firm_opt.check_all) {
411 timer_push(t_verify);
412 irg_verify(irg, VRFY_ENFORCE_SSA);
416 current_ir_graph = old_irg;
420 static void do_irp_opt(const char *name)
422 opt_config_t *config = get_opt(name);
423 size_t n = config - opts;
424 assert(config->target == OPT_TARGET_IRP);
425 if (! (config->flags & OPT_FLAG_ENABLED))
428 timer_push(timers[n]);
429 config->u.transform_irp();
430 timer_pop(timers[n]);
432 if (firm_dump.ir_graph && firm_dump.all_phases) {
434 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
435 ir_graph *irg = get_irp_irg(i);
436 dump_graph_count(irg, name);
440 if (firm_opt.check_all) {
442 timer_push(t_verify);
443 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
444 irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA);
451 * Enable transformations which should be always safe (and cheap) to perform
453 static void enable_safe_defaults(void)
455 set_opt_enabled("remove-unused", true);
456 set_opt_enabled("opt-tail-rec", true);
457 set_opt_enabled("opt-func-call", true);
458 set_opt_enabled("reassociation", true);
459 set_opt_enabled("control-flow", true);
460 set_opt_enabled("local", true);
461 set_opt_enabled("lower-const", true);
462 set_opt_enabled("scalar-replace", true);
463 set_opt_enabled("place", true);
464 set_opt_enabled("confirm", true);
465 set_opt_enabled("opt-load-store", true);
466 set_opt_enabled("lower", true);
467 set_opt_enabled("deconv", true);
468 set_opt_enabled("remove-confirms", true);
469 set_opt_enabled("ivopts", true);
470 set_opt_enabled("dead", true);
471 set_opt_enabled("lower-switch", true);
472 set_opt_enabled("remove-phi-cycles", true);
473 set_opt_enabled("frame", true);
474 set_opt_enabled("combo", true);
475 set_opt_enabled("invert-loops", true);
479 * run all the Firm optimizations
481 * @param input_filename the name of the (main) source file
483 static void do_firm_optimizations(const char *input_filename)
488 set_opt_alias_analysis(firm_opt.alias_analysis);
490 aa_opt = aa_opt_no_opt;
491 if (firm_opt.strict_alias)
492 aa_opt |= aa_opt_type_based | aa_opt_byte_type_may_alias;
493 if (firm_opt.no_alias)
494 aa_opt = aa_opt_no_alias;
496 set_irp_memory_disambiguator_options(aa_opt);
498 /* parameter passing code should set them directly sometime... */
499 set_opt_enabled("rts", !firm_opt.freestanding);
500 set_opt_enabled("gcse", firm_opt.gcse);
501 set_opt_enabled("place", !firm_opt.gcse);
502 set_opt_enabled("confirm", firm_opt.confirm);
503 set_opt_enabled("remove-confirms", firm_opt.confirm);
505 /* osr supersedes remove_phi_cycles */
506 if (get_opt_enabled("ivopts"))
507 set_opt_enabled("remove-phi-cycles", false);
509 timer_start(t_all_opt);
513 /* first step: kill dead code */
514 for (i = 0; i < get_irp_n_irgs(); i++) {
515 ir_graph *irg = get_irp_irg(i);
516 do_irg_opt(irg, "combo");
517 do_irg_opt(irg, "local");
518 do_irg_opt(irg, "control-flow");
521 do_irp_opt("remove-unused");
522 for (i = 0; i < get_irp_n_irgs(); ++i) {
523 ir_graph *irg = get_irp_irg(i);
524 do_irg_opt(irg, "opt-tail-rec");
526 do_irp_opt("opt-func-call");
527 do_irp_opt("lower-const");
529 for (i = 0; i < get_irp_n_irgs(); i++) {
530 ir_graph *irg = get_irp_irg(i);
532 do_irg_opt(irg, "scalar-replace");
533 do_irg_opt(irg, "invert-loops");
534 do_irg_opt(irg, "unroll-loops");
535 do_irg_opt(irg, "local");
536 do_irg_opt(irg, "reassociation");
537 do_irg_opt(irg, "local");
538 do_irg_opt(irg, "gcse");
540 if (firm_opt.confirm) {
541 /* Confirm construction currently can only handle blocks with only
542 one control flow predecessor. Calling optimize_cf here removes
543 Bad predecessors and help the optimization of switch constructs.
545 do_irg_opt(irg, "control-flow");
546 do_irg_opt(irg, "confirm");
547 do_irg_opt(irg, "vrp");
548 do_irg_opt(irg, "local");
551 do_irg_opt(irg, "control-flow");
552 do_irg_opt(irg, "opt-load-store");
553 do_irg_opt(irg, "fp-vrp");
554 do_irg_opt(irg, "lower");
555 do_irg_opt(irg, "deconv");
556 do_irg_opt(irg, "thread-jumps");
557 do_irg_opt(irg, "remove-confirms");
558 do_irg_opt(irg, "gvn-pre");
559 do_irg_opt(irg, "place");
560 do_irg_opt(irg, "control-flow");
562 if (do_irg_opt(irg, "if-conversion")) {
563 do_irg_opt(irg, "local");
564 do_irg_opt(irg, "control-flow");
566 /* this doesn't make too much sense but tests the mux destruction... */
567 do_irg_opt(irg, "lower-mux");
569 do_irg_opt(irg, "bool");
570 do_irg_opt(irg, "shape-blocks");
571 do_irg_opt(irg, "lower-switch");
572 do_irg_opt(irg, "ivopts");
573 do_irg_opt(irg, "local");
574 do_irg_opt(irg, "dead");
575 do_irg_opt(irg, "frame");
578 do_irp_opt("inline");
579 do_irp_opt("opt-proc-clone");
581 for (i = 0; i < get_irp_n_irgs(); i++) {
582 ir_graph *irg = get_irp_irg(i);
583 do_irg_opt(irg, "local");
584 do_irg_opt(irg, "control-flow");
585 do_irg_opt(irg, "thread-jumps");
586 do_irg_opt(irg, "local");
587 do_irg_opt(irg, "control-flow");
589 if( do_irg_opt(irg, "vrp") ) { // if vrp is enabled
590 do_irg_opt(irg, "local");
591 do_irg_opt(irg, "vrp");
592 do_irg_opt(irg, "local");
593 do_irg_opt(irg, "vrp");
597 if (firm_dump.ir_graph) {
598 /* recompute backedges for nicer dumps */
599 for (i = 0; i < get_irp_n_irgs(); i++)
600 construct_cf_backedges(get_irp_irg(i));
605 if (firm_dump.statistic & STAT_AFTER_OPT)
606 stat_dump_snapshot(input_filename, "opt");
608 timer_stop(t_all_opt);
614 * @param input_filename the name of the (main) source file
616 static void do_firm_lowering(const char *input_filename)
620 do_irp_opt("lower-dw");
622 if (firm_dump.statistic & STAT_AFTER_LOWER)
623 stat_dump_snapshot(input_filename, "low");
627 if (firm_opt.enabled) {
628 timer_start(t_all_opt);
630 /* run reassociation first on all graphs BEFORE the architecture
631 dependent optimizations are enabled */
632 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
633 ir_graph *irg = get_irp_irg(i);
634 do_irg_opt(irg, "reassociation");
637 /* enable architecture dependent optimizations */
638 arch_dep_set_opts((arch_dep_opts_t)
639 ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) |
640 (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) |
641 (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) ));
643 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
644 ir_graph *irg = get_irp_irg(i);
646 do_irg_opt(irg, "local");
647 do_irg_opt(irg, "gcse");
648 do_irg_opt(irg, "opt-load-store");
649 do_irg_opt(irg, "local");
650 do_irg_opt(irg, "control-flow");
652 if (do_irg_opt(irg, "vrp")) {
653 do_irg_opt(irg, "local");
654 do_irg_opt(irg, "control-flow");
655 do_irg_opt(irg, "vrp");
656 do_irg_opt(irg, "local");
657 do_irg_opt(irg, "control-flow");
660 if (do_irg_opt(irg, "if-conversion")) {
661 do_irg_opt(irg, "local");
662 do_irg_opt(irg, "control-flow");
665 do_irg_opt(irg, "parallelize-mem");
667 timer_stop(t_all_opt);
669 do_irp_opt("remove-unused");
671 dump_all("-low-opt");
675 mark_private_methods();
677 /* set the phase to low */
678 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
679 set_irg_phase_low(get_irp_irg(i));
681 /* all graphs are lowered, set the irp phase to low */
682 set_irp_phase_state(phase_low);
684 if (firm_dump.statistic & STAT_FINAL) {
685 stat_dump_snapshot(input_filename, "final");
690 * Initialize for the Firm-generating back end.
692 void gen_firm_init(void)
694 unsigned pattern = 0;
697 for (i = 0; i < n_opts; ++i) {
698 timers[i] = ir_timer_new();
699 timer_register(timers[i], opts[i].description);
701 t_verify = ir_timer_new();
702 timer_register(t_verify, "Firm: verify pass");
703 t_vcg_dump = ir_timer_new();
704 timer_register(t_vcg_dump, "Firm: vcg dumping");
705 t_all_opt = ir_timer_new();
706 timer_register(t_all_opt, "Firm: all optimizations");
708 if (firm_dump.stat_pattern)
709 pattern |= FIRMSTAT_PATTERN_ENABLED;
711 if (firm_dump.stat_dag)
712 pattern |= FIRMSTAT_COUNT_DAG;
715 firm_init_stat(firm_dump.statistic == STAT_NONE ?
716 0 : FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP
717 | FIRMSTAT_COUNT_CONSTS | pattern);
719 if (firm_be_opt.selection == BE_FIRM_BE) {
720 const backend_params *be_params = be_get_backend_param();
722 if (be_params->do_dw_lowering)
723 set_opt_enabled("lower-dw", true);
725 arch_create_intrinsic = be_params->arch_create_intrinsic_fkt;
726 create_intrinsic_ctx = be_params->create_intrinsic_ctx;
728 ad_param = be_params->dep_param;
729 if_conv_info = be_params->if_conv_info;
732 edges_init_dbg(firm_opt.vrfy_edges);
734 /* Sel node cannot produce NULL pointers */
735 set_opt_sel_based_null_check_elim(1);
737 /* dynamic dispatch works currently only if whole world scenarios */
738 set_opt_dyn_meth_dispatch(0);
740 arch_dep_init(arch_factory);
742 /* do not run architecture dependent optimizations in building phase */
743 arch_dep_set_opts(arch_dep_none);
745 do_node_verification((firm_verification_t) firm_opt.vrfy);
746 if (firm_dump.filter)
747 only_dump_method_with_name(new_id_from_str(firm_dump.filter));
749 if (firm_opt.enabled) {
751 set_opt_constant_folding(firm_opt.const_folding);
752 set_opt_algebraic_simplification(firm_opt.const_folding);
753 set_opt_cse(firm_opt.cse);
754 set_opt_global_cse(0);
755 set_opt_unreachable_code(1);
756 set_opt_control_flow(firm_opt.control_flow);
757 set_opt_control_flow_weak_simplification(1);
758 set_opt_control_flow_strong_simplification(1);
763 /* do not dump entity ld names */
768 * Called, after the Firm generation is completed,
769 * do all optimizations and backend call here.
771 * @param out a file handle for the output, may be NULL
772 * @param input_filename the name of the (main) source file
773 * @param c_mode non-zero if "C" was compiled
774 * @param new_firm_const_exists non-zero, if the const attribute was used on functions
776 void gen_firm_finish(FILE *out, const char *input_filename, int c_mode,
777 int new_firm_const_exists)
782 if (firm_opt.enable_statev) {
784 snprintf(buf, sizeof(buf), "%s.ev", input_filename);
785 ir_stat_ev_begin(input_filename, firm_opt.statev_filter);
786 ir_stat_ev_compilation_unit(input_filename);
790 firm_const_exists = new_firm_const_exists;
792 /* the general for dumping option must be set, or the others will not work*/
794 = (a_byte) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb);
796 dump_keepalive_edges(1);
797 dump_consts_local(1);
798 dump_dominator_information(1);
799 dump_loop_information(0);
801 if (!firm_dump.edge_labels)
802 turn_off_edge_labels();
804 /* FIXME: cloning might ADD new graphs. */
805 irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
807 if (firm_dump.all_types) {
810 dump_class_hierarchy(0, "");
811 dump_class_hierarchy(1, "-with-entities");
815 /* finalize all graphs */
816 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
817 ir_graph *irg = get_irp_irg(i);
818 irg_finalize_cons(irg);
822 timer_push(t_verify);
826 /* all graphs are finalized, set the irp phase to high */
827 set_irp_phase_state(phase_high);
829 /* BEWARE: kill unreachable code before doing compound lowering */
830 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
831 ir_graph *irg = get_irp_irg(i);
832 do_irg_opt(irg, "control-flow");
835 /* lower all compound call return values */
836 lower_compound_params();
838 /* lower copyb nodes */
839 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
840 ir_graph *irg = get_irp_irg(i);
841 lower_CopyB(irg, 128, 4);
844 if (firm_dump.statistic & STAT_BEFORE_OPT) {
845 stat_dump_snapshot(input_filename, "noopt");
848 if (firm_opt.enabled)
849 do_firm_optimizations(input_filename);
852 do_firm_lowering(input_filename);
854 /* set the phase to low */
855 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
856 set_irg_phase_low(get_irp_irg(i));
858 if (firm_dump.statistic & STAT_FINAL_IR)
859 stat_dump_snapshot(input_filename, "final-ir");
861 /* run the code generator */
862 if (firm_be_opt.selection != BE_NONE)
863 do_codegen(out, input_filename);
865 if (firm_dump.statistic & STAT_FINAL)
866 stat_dump_snapshot(input_filename, "final");
869 void disable_all_opts(void)
871 for (int i = 0; i < n_opts; ++i) {
872 opt_config_t *config = &opts[i];
873 if (config->flags & OPT_FLAG_ESSENTIAL) {
874 config->flags |= OPT_FLAG_ENABLED;
876 config->flags &= ~OPT_FLAG_ENABLED;
881 int firm_opt_option(const char *opt)
884 if (strncmp(opt, "no-", 3) == 0) {
889 opt_config_t *config = get_opt(opt);
890 if (config == NULL || (config->flags & OPT_FLAG_HIDE_OPTIONS))
893 config->flags &= ~OPT_FLAG_ENABLED;
894 config->flags |= enable ? OPT_FLAG_ENABLED : 0;
898 void firm_opt_option_help(void)
902 for (i = 0; i < n_opts; ++i) {
906 const opt_config_t *config = &opts[i];
907 if (config->flags & OPT_FLAG_HIDE_OPTIONS)
910 snprintf(buf2, sizeof(buf2), "firm: enable %s", config->description);
911 print_option_help(config->name, buf2);
912 snprintf(buf, sizeof(buf), "no-%s", config->name);
913 snprintf(buf2, sizeof(buf2), "firm: disable %s", config->description);
914 print_option_help(buf, buf2);
919 * Do very early initializations
921 void firm_early_init(void)
923 /* arg: need this here for command line options */
926 enable_safe_defaults();