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 #if defined(_DEBUG) || defined(FIRM_DEBUG)
26 #define DBG(x) dbg_printf x
28 #define DBG(x) ((void)0)
29 #endif /* _DEBUG || FIRM_DEBUG */
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);
36 /** dump all the graphs depending on cond */
38 static void dump_all(const char *suffix)
40 if (firm_dump.ir_graph) {
41 timer_push(t_vcg_dump);
42 if (firm_dump.no_blocks)
43 dump_all_ir_graphs(dump_ir_graph, suffix);
44 else if (firm_dump.extbb)
45 dump_all_ir_graphs(dump_ir_extblock_graph, suffix);
47 dump_all_ir_graphs(dump_ir_block_graph, suffix);
48 timer_pop(t_vcg_dump);
52 /* set by the backend parameters */
53 static const ir_settings_arch_dep_t *ad_param = NULL;
54 static create_intrinsic_fkt *arch_create_intrinsic = NULL;
55 static void *create_intrinsic_ctx = NULL;
56 static const ir_settings_if_conv_t *if_conv_info = NULL;
58 /* entities of runtime functions */
59 ir_entity_ptr rts_entities[rts_max];
62 * factory for setting architecture dependent parameters
64 static const ir_settings_arch_dep_t *arch_factory(void)
66 static const ir_settings_arch_dep_t param = {
67 1, /* also use subs */
68 4, /* maximum shifts */
69 31, /* maximum shift amount */
70 NULL, /* use default evaluator */
74 32 /* Mulh allowed up to 32 bit */
77 return ad_param ? ad_param : ¶m;
81 * Map runtime functions.
83 static void rts_map(void)
86 ir_entity_ptr *ent; /**< address of the rts entity */
87 i_mapper_func func; /**< mapper function. */
90 { &rts_entities[rts_abs], i_mapper_abs },
91 { &rts_entities[rts_labs], i_mapper_abs },
92 { &rts_entities[rts_llabs], i_mapper_abs },
93 { &rts_entities[rts_imaxabs], i_mapper_abs },
95 /* double -> double */
96 { &rts_entities[rts_fabs], i_mapper_abs },
97 { &rts_entities[rts_sqrt], i_mapper_sqrt },
98 { &rts_entities[rts_cbrt], i_mapper_cbrt },
99 { &rts_entities[rts_pow], i_mapper_pow },
100 { &rts_entities[rts_exp], i_mapper_exp },
101 { &rts_entities[rts_exp2], i_mapper_exp },
102 { &rts_entities[rts_exp10], i_mapper_exp },
103 { &rts_entities[rts_log], i_mapper_log },
104 { &rts_entities[rts_log2], i_mapper_log2 },
105 { &rts_entities[rts_log10], i_mapper_log10 },
106 { &rts_entities[rts_sin], i_mapper_sin },
107 { &rts_entities[rts_cos], i_mapper_cos },
108 { &rts_entities[rts_tan], i_mapper_tan },
109 { &rts_entities[rts_asin], i_mapper_asin },
110 { &rts_entities[rts_acos], i_mapper_acos },
111 { &rts_entities[rts_atan], i_mapper_atan },
112 { &rts_entities[rts_sinh], i_mapper_sinh },
113 { &rts_entities[rts_cosh], i_mapper_cosh },
114 { &rts_entities[rts_tanh], i_mapper_tanh },
117 { &rts_entities[rts_fabsf], i_mapper_abs },
118 { &rts_entities[rts_sqrtf], i_mapper_sqrt },
119 { &rts_entities[rts_cbrtf], i_mapper_cbrt },
120 { &rts_entities[rts_powf], i_mapper_pow },
121 { &rts_entities[rts_expf], i_mapper_exp },
122 { &rts_entities[rts_exp2f], i_mapper_exp },
123 { &rts_entities[rts_exp10f], i_mapper_exp },
124 { &rts_entities[rts_logf], i_mapper_log },
125 { &rts_entities[rts_log2f], i_mapper_log2 },
126 { &rts_entities[rts_log10f], i_mapper_log10 },
127 { &rts_entities[rts_sinf], i_mapper_sin },
128 { &rts_entities[rts_cosf], i_mapper_cos },
129 { &rts_entities[rts_tanf], i_mapper_tan },
130 { &rts_entities[rts_asinf], i_mapper_asin },
131 { &rts_entities[rts_acosf], i_mapper_acos },
132 { &rts_entities[rts_atanf], i_mapper_atan },
133 { &rts_entities[rts_sinhf], i_mapper_sinh },
134 { &rts_entities[rts_coshf], i_mapper_cosh },
135 { &rts_entities[rts_tanhf], i_mapper_tanh },
137 /* long double -> long double */
138 { &rts_entities[rts_fabsl], i_mapper_abs },
139 { &rts_entities[rts_sqrtl], i_mapper_sqrt },
140 { &rts_entities[rts_cbrtl], i_mapper_cbrt },
141 { &rts_entities[rts_powl], i_mapper_pow },
142 { &rts_entities[rts_expl], i_mapper_exp },
143 { &rts_entities[rts_exp2l], i_mapper_exp },
144 { &rts_entities[rts_exp10l], i_mapper_exp },
145 { &rts_entities[rts_logl], i_mapper_log },
146 { &rts_entities[rts_log2l], i_mapper_log2 },
147 { &rts_entities[rts_log10l], i_mapper_log10 },
148 { &rts_entities[rts_sinl], i_mapper_sin },
149 { &rts_entities[rts_cosl], i_mapper_cos },
150 { &rts_entities[rts_tanl], i_mapper_tan },
151 { &rts_entities[rts_asinl], i_mapper_asin },
152 { &rts_entities[rts_acosl], i_mapper_acos },
153 { &rts_entities[rts_atanl], i_mapper_atan },
154 { &rts_entities[rts_sinhl], i_mapper_sinh },
155 { &rts_entities[rts_coshl], i_mapper_cosh },
156 { &rts_entities[rts_tanhl], i_mapper_tanh },
159 { &rts_entities[rts_strcmp], i_mapper_strcmp },
160 { &rts_entities[rts_strncmp], i_mapper_strncmp },
161 { &rts_entities[rts_strcpy], i_mapper_strcpy },
162 { &rts_entities[rts_strlen], i_mapper_strlen },
163 { &rts_entities[rts_memcpy], i_mapper_memcpy },
164 { &rts_entities[rts_mempcpy], i_mapper_mempcpy },
165 { &rts_entities[rts_memmove], i_mapper_memmove },
166 { &rts_entities[rts_memset], i_mapper_memset },
167 { &rts_entities[rts_memcmp], i_mapper_memcmp }
169 i_record rec[sizeof(mapper)/sizeof(mapper[0])];
172 for (i = n_map = 0; i < sizeof(mapper)/sizeof(mapper[0]); ++i) {
173 if (*mapper[i].ent != NULL) {
174 rec[n_map].i_call.kind = INTRINSIC_CALL;
175 rec[n_map].i_call.i_ent = *mapper[i].ent;
176 rec[n_map].i_call.i_mapper = mapper[i].func;
177 rec[n_map].i_call.ctx = NULL;
178 rec[n_map].i_call.link = NULL;
184 lower_intrinsics(rec, n_map, /* part_block_used=*/0);
187 static int *irg_dump_no;
189 static void dump_graph_count(ir_graph *const irg, const char *const suffix)
192 snprintf(name, sizeof(name), "-%02d_%s", irg_dump_no[get_irg_idx(irg)]++,
195 timer_push(t_vcg_dump);
196 if (firm_dump.no_blocks)
197 dump_ir_graph(irg, name);
198 else if (firm_dump.extbb)
199 dump_ir_extblock_graph(irg, name);
201 dump_ir_block_graph(irg, name);
202 timer_pop(t_vcg_dump);
205 static int firm_const_exists;
207 static void do_optimize_funccalls(void)
209 optimize_funccalls(firm_const_exists, NULL);
212 static void do_gcse(ir_graph *irg)
214 set_opt_global_cse(1);
215 optimize_graph_df(irg);
217 set_opt_global_cse(0);
220 static void do_lower_highlevel(ir_graph *irg)
222 lower_highlevel_graph(irg, firm_opt.lower_bitfields);
225 static void do_if_conv(ir_graph *irg)
227 opt_if_conv(irg, if_conv_info);
230 static void do_stred(ir_graph *irg)
232 opt_osr(irg, osr_flag_default | osr_flag_keep_reg_pressure | osr_flag_ignore_x86_shift);
235 static void after_inline_opt(ir_graph *irg)
237 do_irg_opt(irg, "scalar-replace");
238 do_irg_opt(irg, "local");
239 do_irg_opt(irg, "control-flow");
240 do_irg_opt(irg, "combo");
243 static void do_inline(void)
245 inline_functions(firm_opt.inline_maxsize, firm_opt.inline_threshold,
249 static void do_cloning(void)
251 proc_cloning((float) firm_opt.clone_threshold);
254 static void do_lower_switch(ir_graph *irg)
256 lower_switch(irg, firm_opt.spare_size);
259 static void do_lower_mux(ir_graph *irg)
261 lower_mux(irg, NULL);
264 static void do_lower_dw_ops(void)
266 lwrdw_param_t init = {
269 get_atomic_mode(ATOMIC_TYPE_LONGLONG),
270 get_atomic_mode(ATOMIC_TYPE_ULONGLONG),
271 get_atomic_mode(ATOMIC_TYPE_INT),
272 get_atomic_mode(ATOMIC_TYPE_UINT),
273 def_create_intrinsic_fkt,
277 if (arch_create_intrinsic) {
278 init.create_intrinsic = arch_create_intrinsic;
279 init.ctx = create_intrinsic_ctx;
284 static void do_vrp(ir_graph *irg)
289 typedef enum opt_target {
290 OPT_TARGET_IRG, /**< optimization function works on a single graph */
291 OPT_TARGET_IRP /**< optimization function works on the complete program */
294 typedef enum opt_flags {
296 OPT_FLAG_ENABLED = 1 << 0, /**< enable the optimization */
297 OPT_FLAG_NO_DUMP = 1 << 1, /**< don't dump after transformation */
298 OPT_FLAG_NO_VERIFY = 1 << 2, /**< don't verify after transformation */
299 OPT_FLAG_HIDE_OPTIONS = 1 << 3, /**< do not automatically process
300 -foptions for this transformation */
301 OPT_FLAG_ESSENTIAL = 1 << 4, /**< output won't work without this pass
302 so we need it even with -O0 */
305 typedef void (*transform_irg_func)(ir_graph *irg);
306 typedef void (*transform_irp_func)(void);
312 transform_irg_func transform_irg;
313 transform_irp_func transform_irp;
315 const char *description;
319 static opt_config_t opts[] = {
320 #define IRG(a, b, c, d) { OPT_TARGET_IRG, a, .u.transform_irg = (transform_irg_func)b, c, d }
321 #define IRP(a, b, c, d) { OPT_TARGET_IRP, a, .u.transform_irp = b, c, d }
322 IRG("bool", opt_bool, "bool simplification", OPT_FLAG_NONE),
323 IRG("combo", combo, "combined CCE, UCE and GVN", OPT_FLAG_NONE),
324 IRG("confirm", construct_confirms, "confirm optimisation", OPT_FLAG_HIDE_OPTIONS),
325 IRG("control-flow", optimize_cf, "optimization of control-flow", OPT_FLAG_HIDE_OPTIONS),
326 IRG("dead", dead_node_elimination, "dead node elimination", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
327 IRG("deconv", conv_opt, "conv node elimination", OPT_FLAG_NONE),
328 IRG("fp-vrp", fixpoint_vrp, "fixpoint value range propagation", OPT_FLAG_NONE),
329 IRG("frame", opt_frame_irg, "remove unused frame entities", OPT_FLAG_NONE),
330 IRG("gcse", do_gcse, "global common subexpression elimination", OPT_FLAG_NONE),
331 IRG("gvn-pre", do_gvn_pre, "global value numbering partial redundancy elimination", OPT_FLAG_NONE),
332 IRG("if-conversion", do_if_conv, "if-conversion", OPT_FLAG_NONE),
333 IRG("invert-loops", do_loop_inversion, "loop inversion", OPT_FLAG_NONE),
334 IRG("ivopts", do_stred, "induction variable strength reduction", OPT_FLAG_NONE),
335 IRG("local", optimize_graph_df, "local graph optimizations", OPT_FLAG_HIDE_OPTIONS),
336 IRG("lower", do_lower_highlevel, "lowering", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
337 IRG("lower-mux", do_lower_mux, "mux lowering", OPT_FLAG_NONE),
338 IRG("lower-switch", do_lower_switch, "switch lowering", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
339 IRG("one-return", normalize_one_return, "normalisation to 1 return", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
340 IRG("opt-load-store", optimize_load_store, "load store optimization", OPT_FLAG_NONE),
341 IRG("opt-tail-rec", opt_tail_rec_irg, "tail-recursion eliminiation", OPT_FLAG_NONE),
342 IRG("parallelize-mem", opt_parallelize_mem, "parallelize memory", OPT_FLAG_NONE),
343 IRG("place", place_code, "code placement", OPT_FLAG_NONE),
344 IRG("reassociation", optimize_reassociation, "reassociation", OPT_FLAG_NONE),
345 IRG("remove-confirms", remove_confirms, "confirm removal", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
346 IRG("remove-phi-cycles", remove_phi_cycles, "removal of phi cycles", OPT_FLAG_HIDE_OPTIONS),
347 IRG("scalar-replace", scalar_replacement_opt, "scalar replacement", OPT_FLAG_NONE),
348 IRG("shape-blocks", shape_blocks, "block shaping", OPT_FLAG_NONE),
349 IRG("thread-jumps", opt_jumpthreading, "path-sensitive jumpthreading", OPT_FLAG_NONE),
350 IRG("unroll-loops", do_loop_unrolling, "loop unrolling", OPT_FLAG_NONE),
351 IRG("vrp", do_vrp, "value range propagation", OPT_FLAG_NONE),
352 IRP("inline", do_inline, "inlining", OPT_FLAG_NONE),
353 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),
354 IRP("lower-dw", do_lower_dw_ops, "lowering of doubleword operations", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
355 IRP("opt-func-call", do_optimize_funccalls, "function call optimization", OPT_FLAG_NONE),
356 IRP("opt-proc-clone", do_cloning, "procedure cloning", OPT_FLAG_NONE),
357 IRP("remove-unused", garbage_collect_entities, "removal of unused functions/variables", OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
358 IRP("rts", rts_map, "optimization of known library functions", OPT_FLAG_HIDE_OPTIONS),
362 static const int n_opts = sizeof(opts) / sizeof(opts[0]);
363 ir_timer_t *timers[sizeof(opts)/sizeof(opts[0])];
365 static opt_config_t *get_opt(const char *name)
368 for (i = 0; i < n_opts; ++i) {
369 opt_config_t *config = &opts[i];
370 if (strcmp(config->name, name) == 0)
377 static void set_opt_enabled(const char *name, bool enabled)
379 opt_config_t *config = get_opt(name);
380 config->flags = (config->flags & ~OPT_FLAG_ENABLED)
381 | (enabled ? OPT_FLAG_ENABLED : 0);
384 static bool get_opt_enabled(const char *name)
386 opt_config_t *config = get_opt(name);
387 return (config->flags & OPT_FLAG_ENABLED) != 0;
391 * perform an optimisation on a single graph
393 * @return true if something changed, false otherwise
395 static bool do_irg_opt(ir_graph *irg, const char *name)
398 opt_config_t *config = get_opt(name);
399 size_t n = config - opts;
400 assert(config != NULL);
401 assert(config->target == OPT_TARGET_IRG);
402 if (! (config->flags & OPT_FLAG_ENABLED))
405 old_irg = current_ir_graph;
406 current_ir_graph = irg;
408 timer_push(timers[n]);
409 config->u.transform_irg(irg);
410 timer_pop(timers[n]);
412 if (firm_dump.all_phases && firm_dump.ir_graph) {
413 dump_graph_count(irg, name);
416 if (firm_opt.check_all) {
417 timer_push(t_verify);
418 irg_verify(irg, VRFY_ENFORCE_SSA);
422 current_ir_graph = old_irg;
426 static void do_irp_opt(const char *name)
428 opt_config_t *config = get_opt(name);
429 size_t n = config - opts;
430 assert(config->target == OPT_TARGET_IRP);
431 if (! (config->flags & OPT_FLAG_ENABLED))
434 timer_push(timers[n]);
435 config->u.transform_irp();
436 timer_pop(timers[n]);
438 if (firm_dump.ir_graph && firm_dump.all_phases) {
440 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
441 ir_graph *irg = get_irp_irg(i);
442 dump_graph_count(irg, name);
446 if (firm_opt.check_all) {
448 timer_push(t_verify);
449 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
450 irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA);
457 * Enable transformations which should be always safe (and cheap) to perform
459 static void enable_safe_defaults(void)
461 set_opt_enabled("remove-unused", true);
462 set_opt_enabled("opt-tail-rec", true);
463 set_opt_enabled("opt-func-call", true);
464 set_opt_enabled("reassociation", true);
465 set_opt_enabled("control-flow", true);
466 set_opt_enabled("local", true);
467 set_opt_enabled("lower-const", true);
468 set_opt_enabled("scalar-replace", true);
469 set_opt_enabled("place", true);
470 set_opt_enabled("confirm", true);
471 set_opt_enabled("opt-load-store", true);
472 set_opt_enabled("lower", true);
473 set_opt_enabled("deconv", true);
474 set_opt_enabled("remove-confirms", true);
475 set_opt_enabled("ivopts", true);
476 set_opt_enabled("dead", true);
477 set_opt_enabled("lower-switch", true);
478 set_opt_enabled("remove-phi-cycles", true);
479 set_opt_enabled("frame", true);
480 set_opt_enabled("combo", true);
481 set_opt_enabled("invert-loops", true);
485 * run all the Firm optimizations
487 * @param input_filename the name of the (main) source file
489 static void do_firm_optimizations(const char *input_filename)
494 set_opt_alias_analysis(firm_opt.alias_analysis);
496 aa_opt = aa_opt_no_opt;
497 if (firm_opt.strict_alias)
498 aa_opt |= aa_opt_type_based | aa_opt_byte_type_may_alias;
499 if (firm_opt.no_alias)
500 aa_opt = aa_opt_no_alias;
502 set_irp_memory_disambiguator_options(aa_opt);
504 /* parameter passing code should set them directly sometime... */
505 set_opt_enabled("rts", !firm_opt.freestanding);
506 set_opt_enabled("gcse", firm_opt.gcse);
507 set_opt_enabled("place", !firm_opt.gcse);
508 set_opt_enabled("confirm", firm_opt.confirm);
509 set_opt_enabled("remove-confirms", firm_opt.confirm);
511 /* osr supersedes remove_phi_cycles */
512 if (get_opt_enabled("ivopts"))
513 set_opt_enabled("remove-phi-cycles", false);
515 timer_start(t_all_opt);
519 /* first step: kill dead code */
520 for (i = 0; i < get_irp_n_irgs(); i++) {
521 ir_graph *irg = get_irp_irg(i);
522 do_irg_opt(irg, "combo");
523 do_irg_opt(irg, "local");
524 do_irg_opt(irg, "control-flow");
527 do_irp_opt("remove-unused");
528 for (i = 0; i < get_irp_n_irgs(); ++i) {
529 ir_graph *irg = get_irp_irg(i);
530 do_irg_opt(irg, "opt-tail-rec");
532 do_irp_opt("opt-func-call");
533 do_irp_opt("lower-const");
535 for (i = 0; i < get_irp_n_irgs(); i++) {
536 ir_graph *irg = get_irp_irg(i);
538 do_irg_opt(irg, "scalar-replace");
539 do_irg_opt(irg, "invert-loops");
540 do_irg_opt(irg, "unroll-loops");
541 do_irg_opt(irg, "local");
542 do_irg_opt(irg, "reassociation");
543 do_irg_opt(irg, "local");
544 do_irg_opt(irg, "gcse");
546 if (firm_opt.confirm) {
547 /* Confirm construction currently can only handle blocks with only
548 one control flow predecessor. Calling optimize_cf here removes
549 Bad predecessors and help the optimization of switch constructs.
551 do_irg_opt(irg, "control-flow");
552 do_irg_opt(irg, "confirm");
553 do_irg_opt(irg, "vrp");
554 do_irg_opt(irg, "local");
557 do_irg_opt(irg, "control-flow");
558 do_irg_opt(irg, "opt-load-store");
559 do_irg_opt(irg, "fp-vrp");
560 do_irg_opt(irg, "lower");
561 do_irg_opt(irg, "deconv");
562 do_irg_opt(irg, "thread-jumps");
563 do_irg_opt(irg, "remove-confirms");
564 do_irg_opt(irg, "gvn-pre");
565 do_irg_opt(irg, "place");
566 do_irg_opt(irg, "control-flow");
568 if (do_irg_opt(irg, "if-conversion")) {
569 do_irg_opt(irg, "local");
570 do_irg_opt(irg, "control-flow");
572 /* this doesn't make too much sense but tests the mux destruction... */
573 do_irg_opt(irg, "lower-mux");
575 do_irg_opt(irg, "bool");
576 do_irg_opt(irg, "shape-blocks");
577 do_irg_opt(irg, "lower-switch");
578 do_irg_opt(irg, "ivopts");
579 do_irg_opt(irg, "local");
580 do_irg_opt(irg, "dead");
581 do_irg_opt(irg, "frame");
584 do_irp_opt("inline");
585 do_irp_opt("opt-proc-clone");
587 for (i = 0; i < get_irp_n_irgs(); i++) {
588 ir_graph *irg = get_irp_irg(i);
589 do_irg_opt(irg, "local");
590 do_irg_opt(irg, "control-flow");
591 do_irg_opt(irg, "thread-jumps");
592 do_irg_opt(irg, "local");
593 do_irg_opt(irg, "control-flow");
595 if( do_irg_opt(irg, "vrp") ) { // if vrp is enabled
596 do_irg_opt(irg, "local");
597 do_irg_opt(irg, "vrp");
598 do_irg_opt(irg, "local");
599 do_irg_opt(irg, "vrp");
603 if (firm_dump.ir_graph) {
604 /* recompute backedges for nicer dumps */
605 for (i = 0; i < get_irp_n_irgs(); i++)
606 construct_cf_backedges(get_irp_irg(i));
611 if (firm_dump.statistic & STAT_AFTER_OPT)
612 stat_dump_snapshot(input_filename, "opt");
614 timer_stop(t_all_opt);
620 * @param input_filename the name of the (main) source file
622 static void do_firm_lowering(const char *input_filename)
626 do_irp_opt("lower-dw");
628 if (firm_dump.statistic & STAT_AFTER_LOWER)
629 stat_dump_snapshot(input_filename, "low");
633 if (firm_opt.enabled) {
634 timer_start(t_all_opt);
636 /* run reassociation first on all graphs BEFORE the architecture
637 dependent optimizations are enabled */
638 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
639 ir_graph *irg = get_irp_irg(i);
640 do_irg_opt(irg, "reassociation");
643 /* enable architecture dependent optimizations */
644 arch_dep_set_opts((arch_dep_opts_t)
645 ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) |
646 (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) |
647 (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) ));
649 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
650 ir_graph *irg = get_irp_irg(i);
652 do_irg_opt(irg, "local");
653 do_irg_opt(irg, "gcse");
654 do_irg_opt(irg, "opt-load-store");
655 do_irg_opt(irg, "local");
656 do_irg_opt(irg, "control-flow");
658 if (do_irg_opt(irg, "vrp")) {
659 do_irg_opt(irg, "local");
660 do_irg_opt(irg, "control-flow");
661 do_irg_opt(irg, "vrp");
662 do_irg_opt(irg, "local");
663 do_irg_opt(irg, "control-flow");
666 if (do_irg_opt(irg, "if-conversion")) {
667 do_irg_opt(irg, "local");
668 do_irg_opt(irg, "control-flow");
671 do_irg_opt(irg, "parallelize-mem");
673 timer_stop(t_all_opt);
675 do_irp_opt("remove-unused");
677 dump_all("-low-opt");
681 mark_private_methods();
683 /* set the phase to low */
684 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
685 set_irg_phase_low(get_irp_irg(i));
687 /* all graphs are lowered, set the irp phase to low */
688 set_irp_phase_state(phase_low);
690 if (firm_dump.statistic & STAT_FINAL) {
691 stat_dump_snapshot(input_filename, "final");
696 * Initialize for the Firm-generating back end.
698 void gen_firm_init(void)
700 firm_parameter_t params;
701 unsigned pattern = 0;
704 for (i = 0; i < n_opts; ++i) {
705 timers[i] = ir_timer_new();
706 timer_register(timers[i], opts[i].description);
708 t_verify = ir_timer_new();
709 timer_register(t_verify, "Firm: verify pass");
710 t_vcg_dump = ir_timer_new();
711 timer_register(t_vcg_dump, "Firm: vcg dumping");
712 t_all_opt = ir_timer_new();
713 timer_register(t_all_opt, "Firm: all optimizations");
715 if (firm_dump.stat_pattern)
716 pattern |= FIRMSTAT_PATTERN_ENABLED;
718 if (firm_dump.stat_dag)
719 pattern |= FIRMSTAT_COUNT_DAG;
721 memset(¶ms, 0, sizeof(params));
722 params.size = sizeof(params);
723 params.enable_statistics = firm_dump.statistic == STAT_NONE ? 0 :
724 FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP | FIRMSTAT_COUNT_CONSTS
726 params.initialize_local_func = uninitialized_local_var;
727 params.cc_mask = 0; /* no regparam, cdecl */
731 if (firm_be_opt.selection == BE_FIRM_BE) {
732 const backend_params *be_params = be_get_backend_param();
734 if (be_params->do_dw_lowering)
735 set_opt_enabled("lower-dw", true);
737 arch_create_intrinsic = be_params->arch_create_intrinsic_fkt;
738 create_intrinsic_ctx = be_params->create_intrinsic_ctx;
740 ad_param = be_params->dep_param;
741 if_conv_info = be_params->if_conv_info;
744 edges_init_dbg(firm_opt.vrfy_edges);
746 /* Sel node cannot produce NULL pointers */
747 set_opt_sel_based_null_check_elim(1);
749 /* dynamic dispatch works currently only if whole world scenarios */
750 set_opt_dyn_meth_dispatch(0);
752 arch_dep_init(arch_factory);
754 /* do not run architecture dependent optimizations in building phase */
755 arch_dep_set_opts(arch_dep_none);
757 do_node_verification((firm_verification_t) firm_opt.vrfy);
758 if (firm_dump.filter)
759 only_dump_method_with_name(new_id_from_str(firm_dump.filter));
761 if (firm_opt.enabled) {
763 set_opt_constant_folding(firm_opt.const_folding);
764 set_opt_algebraic_simplification(firm_opt.const_folding);
765 set_opt_cse(firm_opt.cse);
766 set_opt_global_cse(0);
767 set_opt_unreachable_code(1);
768 set_opt_control_flow(firm_opt.control_flow);
769 set_opt_control_flow_weak_simplification(1);
770 set_opt_control_flow_strong_simplification(1);
775 /* do not dump entity ld names */
780 * Called, after the Firm generation is completed,
781 * do all optimizations and backend call here.
783 * @param out a file handle for the output, may be NULL
784 * @param input_filename the name of the (main) source file
785 * @param c_mode non-zero if "C" was compiled
786 * @param new_firm_const_exists non-zero, if the const attribute was used on functions
788 void gen_firm_finish(FILE *out, const char *input_filename, int c_mode,
789 int new_firm_const_exists)
794 if (firm_opt.enable_statev) {
796 snprintf(buf, sizeof(buf), "%s.ev", input_filename);
797 ir_stat_ev_begin(input_filename, firm_opt.statev_filter);
798 ir_stat_ev_compilation_unit(input_filename);
802 firm_const_exists = new_firm_const_exists;
804 /* the general for dumping option must be set, or the others will not work*/
806 = (a_byte) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb);
808 dump_keepalive_edges(1);
809 dump_consts_local(1);
810 dump_dominator_information(1);
811 dump_loop_information(0);
813 if (!firm_dump.edge_labels)
814 turn_off_edge_labels();
816 /* FIXME: cloning might ADD new graphs. */
817 irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
819 if (firm_dump.all_types) {
822 dump_class_hierarchy(0, "");
823 dump_class_hierarchy(1, "-with-entities");
827 /* finalize all graphs */
828 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
829 ir_graph *irg = get_irp_irg(i);
830 irg_finalize_cons(irg);
834 timer_push(t_verify);
838 /* all graphs are finalized, set the irp phase to high */
839 set_irp_phase_state(phase_high);
841 /* BEWARE: kill unreachable code before doing compound lowering */
842 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
843 ir_graph *irg = get_irp_irg(i);
844 do_irg_opt(irg, "control-flow");
847 /* lower all compound call return values */
848 lower_compound_params();
850 /* lower copyb nodes */
851 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
852 ir_graph *irg = get_irp_irg(i);
853 lower_CopyB(irg, 128, 4);
856 if (firm_dump.statistic & STAT_BEFORE_OPT) {
857 stat_dump_snapshot(input_filename, "noopt");
860 if (firm_opt.enabled)
861 do_firm_optimizations(input_filename);
864 do_firm_lowering(input_filename);
866 /* set the phase to low */
867 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
868 set_irg_phase_low(get_irp_irg(i));
870 if (firm_dump.statistic & STAT_FINAL_IR)
871 stat_dump_snapshot(input_filename, "final-ir");
873 /* run the code generator */
874 if (firm_be_opt.selection != BE_NONE)
875 do_codegen(out, input_filename);
877 if (firm_dump.statistic & STAT_FINAL)
878 stat_dump_snapshot(input_filename, "final");
881 void disable_all_opts(void)
883 for (int i = 0; i < n_opts; ++i) {
884 opt_config_t *config = &opts[i];
885 if (config->flags & OPT_FLAG_ESSENTIAL) {
886 config->flags |= OPT_FLAG_ENABLED;
888 config->flags &= ~OPT_FLAG_ENABLED;
893 int firm_opt_option(const char *opt)
896 if (strncmp(opt, "no-", 3) == 0) {
901 opt_config_t *config = get_opt(opt);
902 if (config == NULL || (config->flags & OPT_FLAG_HIDE_OPTIONS))
905 config->flags &= ~OPT_FLAG_ENABLED;
906 config->flags |= enable ? OPT_FLAG_ENABLED : 0;
910 void firm_opt_option_help(void)
914 for (i = 0; i < n_opts; ++i) {
918 const opt_config_t *config = &opts[i];
919 if (config->flags & OPT_FLAG_HIDE_OPTIONS)
922 snprintf(buf2, sizeof(buf2), "firm: enable %s", config->description);
923 print_option_help(config->name, buf2);
924 snprintf(buf, sizeof(buf), "no-%s", config->name);
925 snprintf(buf2, sizeof(buf2), "firm: disable %s", config->description);
926 print_option_help(buf, buf2);
931 * Do very early initializations
933 void firm_early_init(void)
935 /* arg: need this here for command line options */
938 enable_safe_defaults();