4 * @author Michael Beck, Matthias Braun
5 * @brief Firm-generating back end optimizations.
14 #include <libfirm/firm.h>
17 #include "firm_timing.h"
19 #include "adt/strutil.h"
22 /* optimization settings */
24 bool const_folding; /**< enable constant folding */
25 bool cse; /**< enable common-subexpression elimination */
26 bool confirm; /**< enable Confirm optimization */
27 bool muls; /**< enable architecture dependent mul optimization */
28 bool divs; /**< enable architecture dependent div optimization */
29 bool mods; /**< enable architecture dependent mod optimization */
30 bool alias_analysis; /**< enable Alias Analysis */
31 bool strict_alias; /**< enable strict Alias Analysis (using type based AA) */
32 bool no_alias; /**< no aliasing possible. */
33 bool verify; /**< Firm verifier setting */
34 bool check_all; /**< enable checking all Firm phases */
35 int clone_threshold; /**< The threshold value for procedure cloning. */
36 unsigned inline_maxsize; /**< Maximum function size for inlining. */
37 unsigned inline_threshold;/**< Inlining benefice threshold. */
40 /** statistic options */
41 typedef enum a_firmstat_selection_tag {
42 STAT_NONE = 0x00000000,
43 STAT_BEFORE_OPT = 0x00000001,
44 STAT_AFTER_OPT = 0x00000002,
45 STAT_AFTER_LOWER = 0x00000004,
46 STAT_FINAL_IR = 0x00000008,
47 STAT_FINAL = 0x00000010,
48 } a_firmstat_selection;
52 bool debug_print; /**< enable debug print */
53 bool all_types; /**< dump the All_types graph */
54 bool ir_graph; /**< dump all graphs */
55 bool all_phases; /**< dump the IR graph after all phases */
56 bool statistic; /**< Firm statistic setting */
59 struct a_firm_be_opt {
64 /* optimization settings */
65 static struct a_firm_opt firm_opt = {
66 .const_folding = true,
72 .alias_analysis = true,
73 .strict_alias = false,
75 .verify = FIRM_VERIFICATION_ON,
77 .clone_threshold = DEFAULT_CLONE_THRESHOLD,
78 .inline_maxsize = 750,
79 .inline_threshold = 0,
83 static struct a_firm_dump firm_dump = {
88 .statistic = STAT_NONE,
91 #define X(a) a, sizeof(a)-1
93 /** Parameter description structure */
94 static const struct params {
95 const char *option; /**< name of the option */
96 size_t opt_len; /**< length of the option string */
97 bool *flag; /**< address of variable to set/reset */
98 bool set; /**< iff true, variable will be set, else reset */
99 const char *description; /**< description of this option */
101 /* firm optimization options */
102 { X("no-opt"), NULL, 0, "disable all FIRM optimizations" },
103 { X("cse"), &firm_opt.cse, 1, "enable common subexpression elimination" },
104 { X("no-cse"), &firm_opt.cse, 0, "disable common subexpression elimination" },
105 { X("const-fold"), &firm_opt.const_folding, 1, "enable constant folding" },
106 { X("no-const-fold"), &firm_opt.const_folding, 0, "disable constant folding" },
107 { X("inline-max-size=<size>"), NULL, 0, "set maximum size for function inlining" },
108 { X("inline-threshold=<size>"),NULL, 0, "set benefice threshold for function inlining" },
109 { X("confirm"), &firm_opt.confirm, 1, "enable Confirm optimization" },
110 { X("no-confirm"), &firm_opt.confirm, 0, "disable Confirm optimization" },
111 { X("opt-mul"), &firm_opt.muls, 0, "enable multiplication optimization" },
112 { X("no-opt-mul"), &firm_opt.muls, 0, "disable multiplication optimization" },
113 { X("opt-div"), &firm_opt.divs, 0, "enable division optimization" },
114 { X("no-opt-div"), &firm_opt.divs, 0, "disable division optimization" },
115 { X("opt-mod"), &firm_opt.mods, 0, "enable remainder optimization" },
116 { X("no-opt-mod"), &firm_opt.mods, 0, "disable remainder optimization" },
117 { X("opt-alias"), &firm_opt.alias_analysis, 1, "enable alias analysis" },
118 { X("no-opt-alias"), &firm_opt.alias_analysis, 0, "disable alias analysis" },
119 { X("alias"), &firm_opt.no_alias, 0, "aliasing occurs" },
120 { X("no-alias"), &firm_opt.no_alias, 1, "no aliasing occurs" },
121 { X("strict-aliasing"), &firm_opt.strict_alias, 1, "strict alias rules" },
122 { X("no-strict-aliasing"), &firm_opt.strict_alias, 0, "strict alias rules" },
123 { X("clone-threshold=<value>"),NULL, 0, "set clone threshold to <value>" },
125 /* other firm regarding options */
126 { X("verify-off"), &firm_opt.verify, FIRM_VERIFICATION_OFF, "disable node verification" },
127 { X("verify-on"), &firm_opt.verify, FIRM_VERIFICATION_ON, "enable node verification" },
128 { X("verify-report"), &firm_opt.verify, FIRM_VERIFICATION_REPORT, "node verification, report only" },
131 { X("dump-ir"), &firm_dump.ir_graph, 1, "dump IR graph" },
132 { X("dump-all-types"), &firm_dump.all_types, 1, "dump graph of all types" },
133 { X("dump-all-phases"), &firm_dump.all_phases, 1, "dump graphs for all optimization phases" },
134 { X("dump-filter=<string>"), NULL, 0, "set dumper filter" },
137 { X("stat-before-opt"), &firm_dump.statistic, STAT_BEFORE_OPT, "Firm statistic output before optimizations" },
138 { X("stat-after-opt"), &firm_dump.statistic, STAT_AFTER_OPT, "Firm statistic output after optimizations" },
139 { X("stat-after-lower"), &firm_dump.statistic, STAT_AFTER_LOWER, "Firm statistic output after lowering" },
140 { X("stat-final-ir"), &firm_dump.statistic, STAT_FINAL_IR, "Firm statistic after final optimization" },
141 { X("stat-final"), &firm_dump.statistic, STAT_FINAL, "Firm statistic after code generation" },
146 static ir_timer_t *t_vcg_dump;
147 static ir_timer_t *t_verify;
148 static ir_timer_t *t_all_opt;
149 static ir_timer_t *t_backend;
150 static bool do_irg_opt(ir_graph *irg, const char *name);
152 /** dump all the graphs depending on cond */
154 static void dump_all(const char *suffix)
156 if (!firm_dump.ir_graph)
159 timer_push(t_vcg_dump);
160 dump_all_ir_graphs(suffix);
161 timer_pop(t_vcg_dump);
164 /* entities of runtime functions */
165 ir_entity *rts_entities[rts_max];
168 * Map runtime functions.
170 static void rts_map(void)
172 static const struct {
173 ir_entity **ent; /**< address of the rts entity */
174 i_mapper_func func; /**< mapper function. */
177 { &rts_entities[rts_abs], i_mapper_abs },
178 { &rts_entities[rts_labs], i_mapper_abs },
179 { &rts_entities[rts_llabs], i_mapper_abs },
180 { &rts_entities[rts_imaxabs], i_mapper_abs },
182 /* double -> double */
183 { &rts_entities[rts_fabs], i_mapper_abs },
184 { &rts_entities[rts_sqrt], i_mapper_sqrt },
185 { &rts_entities[rts_cbrt], i_mapper_cbrt },
186 { &rts_entities[rts_pow], i_mapper_pow },
187 { &rts_entities[rts_exp], i_mapper_exp },
188 { &rts_entities[rts_exp2], i_mapper_exp },
189 { &rts_entities[rts_exp10], i_mapper_exp },
190 { &rts_entities[rts_log], i_mapper_log },
191 { &rts_entities[rts_log2], i_mapper_log2 },
192 { &rts_entities[rts_log10], i_mapper_log10 },
193 { &rts_entities[rts_sin], i_mapper_sin },
194 { &rts_entities[rts_cos], i_mapper_cos },
195 { &rts_entities[rts_tan], i_mapper_tan },
196 { &rts_entities[rts_asin], i_mapper_asin },
197 { &rts_entities[rts_acos], i_mapper_acos },
198 { &rts_entities[rts_atan], i_mapper_atan },
199 { &rts_entities[rts_sinh], i_mapper_sinh },
200 { &rts_entities[rts_cosh], i_mapper_cosh },
201 { &rts_entities[rts_tanh], i_mapper_tanh },
204 { &rts_entities[rts_fabsf], i_mapper_abs },
205 { &rts_entities[rts_sqrtf], i_mapper_sqrt },
206 { &rts_entities[rts_cbrtf], i_mapper_cbrt },
207 { &rts_entities[rts_powf], i_mapper_pow },
208 { &rts_entities[rts_expf], i_mapper_exp },
209 { &rts_entities[rts_exp2f], i_mapper_exp },
210 { &rts_entities[rts_exp10f], i_mapper_exp },
211 { &rts_entities[rts_logf], i_mapper_log },
212 { &rts_entities[rts_log2f], i_mapper_log2 },
213 { &rts_entities[rts_log10f], i_mapper_log10 },
214 { &rts_entities[rts_sinf], i_mapper_sin },
215 { &rts_entities[rts_cosf], i_mapper_cos },
216 { &rts_entities[rts_tanf], i_mapper_tan },
217 { &rts_entities[rts_asinf], i_mapper_asin },
218 { &rts_entities[rts_acosf], i_mapper_acos },
219 { &rts_entities[rts_atanf], i_mapper_atan },
220 { &rts_entities[rts_sinhf], i_mapper_sinh },
221 { &rts_entities[rts_coshf], i_mapper_cosh },
222 { &rts_entities[rts_tanhf], i_mapper_tanh },
224 /* long double -> long double */
225 { &rts_entities[rts_fabsl], i_mapper_abs },
226 { &rts_entities[rts_sqrtl], i_mapper_sqrt },
227 { &rts_entities[rts_cbrtl], i_mapper_cbrt },
228 { &rts_entities[rts_powl], i_mapper_pow },
229 { &rts_entities[rts_expl], i_mapper_exp },
230 { &rts_entities[rts_exp2l], i_mapper_exp },
231 { &rts_entities[rts_exp10l], i_mapper_exp },
232 { &rts_entities[rts_logl], i_mapper_log },
233 { &rts_entities[rts_log2l], i_mapper_log2 },
234 { &rts_entities[rts_log10l], i_mapper_log10 },
235 { &rts_entities[rts_sinl], i_mapper_sin },
236 { &rts_entities[rts_cosl], i_mapper_cos },
237 { &rts_entities[rts_tanl], i_mapper_tan },
238 { &rts_entities[rts_asinl], i_mapper_asin },
239 { &rts_entities[rts_acosl], i_mapper_acos },
240 { &rts_entities[rts_atanl], i_mapper_atan },
241 { &rts_entities[rts_sinhl], i_mapper_sinh },
242 { &rts_entities[rts_coshl], i_mapper_cosh },
243 { &rts_entities[rts_tanhl], i_mapper_tanh },
246 { &rts_entities[rts_strcmp], i_mapper_strcmp },
247 { &rts_entities[rts_strncmp], i_mapper_strncmp },
248 { &rts_entities[rts_strcpy], i_mapper_strcpy },
249 { &rts_entities[rts_strlen], i_mapper_strlen },
250 { &rts_entities[rts_memcpy], i_mapper_memcpy },
251 { &rts_entities[rts_mempcpy], i_mapper_mempcpy },
252 { &rts_entities[rts_memmove], i_mapper_memmove },
253 { &rts_entities[rts_memset], i_mapper_memset },
254 { &rts_entities[rts_memcmp], i_mapper_memcmp }
256 i_record rec[lengthof(mapper)];
259 for (size_t i = 0; i != lengthof(mapper); ++i) {
260 if (*mapper[i].ent != NULL) {
261 rec[n_map].i_call.kind = INTRINSIC_CALL;
262 rec[n_map].i_call.i_ent = *mapper[i].ent;
263 rec[n_map].i_call.i_mapper = mapper[i].func;
264 rec[n_map].i_call.ctx = NULL;
265 rec[n_map].i_call.link = NULL;
271 lower_intrinsics(rec, n_map, /* part_block_used=*/0);
274 static int *irg_dump_no;
276 typedef enum opt_target {
277 OPT_TARGET_IRG, /**< optimization function works on a single graph */
278 OPT_TARGET_IRP /**< optimization function works on the complete program */
281 typedef enum opt_flags {
283 OPT_FLAG_ENABLED = 1 << 0, /**< enable the optimization */
284 OPT_FLAG_NO_DUMP = 1 << 1, /**< don't dump after transformation */
285 OPT_FLAG_NO_VERIFY = 1 << 2, /**< don't verify after transformation */
286 OPT_FLAG_HIDE_OPTIONS = 1 << 3, /**< do not automatically process
287 -foptions for this transformation */
288 OPT_FLAG_ESSENTIAL = 1 << 4, /**< output won't work without this pass
289 so we need it even with -O0 */
292 typedef void (*transform_irg_func)(ir_graph *irg);
293 typedef void (*transform_irp_func)(void);
299 transform_irg_func transform_irg;
300 transform_irp_func transform_irp;
302 const char *description;
307 static opt_config_t *get_opt(const char *name);
309 static void do_stred(ir_graph *irg)
311 opt_osr(irg, osr_flag_default | osr_flag_keep_reg_pressure | osr_flag_ignore_x86_shift);
314 static void after_inline_opt(ir_graph *irg)
316 opt_config_t *const config = get_opt("inline");
317 timer_stop(config->timer);
319 do_irg_opt(irg, "scalar-replace");
320 do_irg_opt(irg, "local");
321 do_irg_opt(irg, "control-flow");
322 do_irg_opt(irg, "combo");
324 timer_start(config->timer);
327 static void do_inline(void)
329 inline_functions(firm_opt.inline_maxsize, firm_opt.inline_threshold,
333 static void do_cloning(void)
335 proc_cloning((float) firm_opt.clone_threshold);
338 static void do_lower_mux(ir_graph *irg)
340 lower_mux(irg, NULL);
343 static void do_gcse(ir_graph *irg)
345 set_opt_global_cse(1);
346 optimize_graph_df(irg);
347 set_opt_global_cse(0);
350 static opt_config_t opts[] = {
351 #define IRG(a, b, c, d) { OPT_TARGET_IRG, a, .u.transform_irg = (transform_irg_func)b, c, d }
352 #define IRP(a, b, c, d) { OPT_TARGET_IRP, a, .u.transform_irp = b, c, d }
353 IRG("bool", opt_bool, "bool simplification", OPT_FLAG_NONE),
354 IRG("combo", combo, "combined CCE, UCE and GVN", OPT_FLAG_NONE),
355 IRG("confirm", construct_confirms, "confirm optimization", OPT_FLAG_HIDE_OPTIONS),
356 IRG("control-flow", optimize_cf, "optimization of control-flow", OPT_FLAG_HIDE_OPTIONS),
357 IRG("dead", dead_node_elimination, "dead node elimination", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
358 IRG("deconv", conv_opt, "conv node elimination", OPT_FLAG_NONE),
359 IRG("fp-vrp", fixpoint_vrp, "fixpoint value range propagation", OPT_FLAG_NONE),
360 IRG("frame", opt_frame_irg, "remove unused frame entities", OPT_FLAG_NONE),
361 IRG("gvn-pre", do_gvn_pre, "global value numbering partial redundancy elimination", OPT_FLAG_NONE),
362 IRG("if-conversion", opt_if_conv, "if-conversion", OPT_FLAG_NONE),
363 IRG("invert-loops", do_loop_inversion, "loop inversion", OPT_FLAG_NONE),
364 IRG("ivopts", do_stred, "induction variable strength reduction", OPT_FLAG_NONE),
365 IRG("local", local_opts, "local graph optimizations", OPT_FLAG_HIDE_OPTIONS),
366 IRG("lower", lower_highlevel_graph, "lowering", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
367 IRG("lower-mux", do_lower_mux, "mux lowering", OPT_FLAG_NONE),
368 IRG("opt-load-store", optimize_load_store, "load store optimization", OPT_FLAG_NONE),
369 IRG("opt-tail-rec", opt_tail_rec_irg, "tail-recursion eliminiation", OPT_FLAG_NONE),
370 IRG("parallelize-mem", opt_parallelize_mem, "parallelize memory", OPT_FLAG_NONE),
371 IRG("gcse", do_gcse, "global common subexpression eliminiation", OPT_FLAG_NONE),
372 IRG("place", place_code, "code placement", OPT_FLAG_NONE),
373 IRG("reassociation", optimize_reassociation, "reassociation", OPT_FLAG_NONE),
374 IRG("remove-confirms", remove_confirms, "confirm removal", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
375 IRG("remove-phi-cycles", remove_phi_cycles, "removal of phi cycles", OPT_FLAG_HIDE_OPTIONS),
376 IRG("scalar-replace", scalar_replacement_opt, "scalar replacement", OPT_FLAG_NONE),
377 IRG("shape-blocks", shape_blocks, "block shaping", OPT_FLAG_NONE),
378 IRG("thread-jumps", opt_jumpthreading, "path-sensitive jumpthreading", OPT_FLAG_NONE),
379 IRG("unroll-loops", do_loop_unrolling, "loop unrolling", OPT_FLAG_NONE),
380 IRG("vrp", set_vrp_data, "value range propagation", OPT_FLAG_NONE),
381 IRP("inline", do_inline, "inlining", OPT_FLAG_NONE),
382 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),
383 IRP("local-const", local_opts_const_code, "local optimisation of constant initializers",
384 OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY | OPT_FLAG_ESSENTIAL),
385 IRP("target-lowering", be_lower_for_target, "lowering necessary for target architecture", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
386 IRP("opt-func-call", optimize_funccalls, "function call optimization", OPT_FLAG_NONE),
387 IRP("opt-proc-clone", do_cloning, "procedure cloning", OPT_FLAG_NONE),
388 IRP("remove-unused", garbage_collect_entities, "removal of unused functions/variables", OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
389 IRP("rts", rts_map, "optimization of known library functions", OPT_FLAG_NONE),
390 IRP("opt-cc", mark_private_methods, "calling conventions optimization", OPT_FLAG_NONE),
395 #define FOR_EACH_OPT(i) for (opt_config_t *i = opts; i != endof(opts); ++i)
397 static opt_config_t *get_opt(const char *name)
399 FOR_EACH_OPT(config) {
400 if (streq(config->name, name))
407 static void set_opt_enabled(const char *name, bool enabled)
409 opt_config_t *config = get_opt(name);
410 config->flags = (config->flags & ~OPT_FLAG_ENABLED)
411 | (enabled ? OPT_FLAG_ENABLED : 0);
414 static bool get_opt_enabled(const char *name)
416 opt_config_t *config = get_opt(name);
417 return (config->flags & OPT_FLAG_ENABLED) != 0;
421 * perform an optimization on a single graph
423 * @return true if something changed, false otherwise
425 static bool do_irg_opt(ir_graph *irg, const char *name)
427 opt_config_t *const config = get_opt(name);
428 assert(config != NULL);
429 assert(config->target == OPT_TARGET_IRG);
430 if (! (config->flags & OPT_FLAG_ENABLED))
433 ir_graph *const old_irg = current_ir_graph;
434 current_ir_graph = irg;
436 timer_start(config->timer);
437 config->u.transform_irg(irg);
438 timer_stop(config->timer);
440 if (firm_dump.all_phases && firm_dump.ir_graph) {
441 dump_ir_graph(irg, name);
444 if (firm_opt.verify) {
445 timer_push(t_verify);
446 irg_verify(irg, VERIFY_ENFORCE_SSA);
450 current_ir_graph = old_irg;
454 static void do_irp_opt(const char *name)
456 opt_config_t *const config = get_opt(name);
457 assert(config->target == OPT_TARGET_IRP);
458 if (! (config->flags & OPT_FLAG_ENABLED))
461 timer_start(config->timer);
462 config->u.transform_irp();
463 timer_stop(config->timer);
465 if (firm_dump.ir_graph && firm_dump.all_phases) {
467 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
468 ir_graph *irg = get_irp_irg(i);
469 dump_ir_graph(irg, name);
473 if (firm_opt.verify) {
475 timer_push(t_verify);
476 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
477 irg_verify(get_irp_irg(i), VERIFY_ENFORCE_SSA);
484 * Enable transformations which should be always safe (and cheap) to perform
486 static void enable_safe_defaults(void)
488 set_opt_enabled("remove-unused", true);
489 set_opt_enabled("opt-tail-rec", true);
490 set_opt_enabled("opt-func-call", true);
491 set_opt_enabled("reassociation", true);
492 set_opt_enabled("control-flow", true);
493 set_opt_enabled("local", true);
494 set_opt_enabled("lower-const", true);
495 set_opt_enabled("local-const", true);
496 set_opt_enabled("scalar-replace", true);
497 set_opt_enabled("place", true);
498 set_opt_enabled("gcse", true);
499 set_opt_enabled("confirm", true);
500 set_opt_enabled("opt-load-store", true);
501 set_opt_enabled("lower", true);
502 set_opt_enabled("deconv", true);
503 set_opt_enabled("remove-confirms", true);
504 set_opt_enabled("ivopts", true);
505 set_opt_enabled("dead", true);
506 set_opt_enabled("remove-phi-cycles", true);
507 set_opt_enabled("frame", true);
508 set_opt_enabled("combo", true);
509 set_opt_enabled("invert-loops", true);
510 set_opt_enabled("target-lowering", true);
511 set_opt_enabled("rts", true);
512 set_opt_enabled("parallelize-mem", true);
513 set_opt_enabled("opt-cc", true);
517 * run all the Firm optimizations
519 * @param input_filename the name of the (main) source file
521 static void do_firm_optimizations(const char *input_filename)
526 set_opt_alias_analysis(firm_opt.alias_analysis);
528 aa_opt = aa_opt_no_opt;
529 if (firm_opt.strict_alias)
530 aa_opt |= aa_opt_type_based | aa_opt_byte_type_may_alias;
531 if (firm_opt.no_alias)
532 aa_opt = aa_opt_no_alias;
534 set_irp_memory_disambiguator_options(aa_opt);
536 /* parameter passing code should set them directly sometime... */
537 set_opt_enabled("confirm", firm_opt.confirm);
538 set_opt_enabled("remove-confirms", firm_opt.confirm);
540 /* osr supersedes remove_phi_cycles */
541 if (get_opt_enabled("ivopts"))
542 set_opt_enabled("remove-phi-cycles", false);
546 /* first step: kill dead code */
547 for (i = 0; i < get_irp_n_irgs(); i++) {
548 ir_graph *irg = get_irp_irg(i);
549 do_irg_opt(irg, "combo");
550 do_irg_opt(irg, "local");
551 do_irg_opt(irg, "control-flow");
554 do_irp_opt("remove-unused");
555 for (i = 0; i < get_irp_n_irgs(); ++i) {
556 ir_graph *irg = get_irp_irg(i);
557 do_irg_opt(irg, "opt-tail-rec");
559 do_irp_opt("opt-func-call");
560 do_irp_opt("lower-const");
562 for (i = 0; i < get_irp_n_irgs(); i++) {
563 ir_graph *irg = get_irp_irg(i);
565 do_irg_opt(irg, "scalar-replace");
566 do_irg_opt(irg, "invert-loops");
567 do_irg_opt(irg, "unroll-loops");
568 do_irg_opt(irg, "local");
569 do_irg_opt(irg, "reassociation");
570 do_irg_opt(irg, "local");
571 do_irg_opt(irg, "gcse");
572 do_irg_opt(irg, "place");
574 if (firm_opt.confirm) {
575 /* Confirm construction currently can only handle blocks with only
576 one control flow predecessor. Calling optimize_cf here removes
577 Bad predecessors and help the optimization of switch constructs.
579 do_irg_opt(irg, "control-flow");
580 do_irg_opt(irg, "confirm");
581 do_irg_opt(irg, "vrp");
582 do_irg_opt(irg, "local");
585 do_irg_opt(irg, "control-flow");
586 do_irg_opt(irg, "opt-load-store");
587 do_irg_opt(irg, "fp-vrp");
588 do_irg_opt(irg, "lower");
589 do_irg_opt(irg, "deconv");
590 do_irg_opt(irg, "thread-jumps");
591 do_irg_opt(irg, "remove-confirms");
592 do_irg_opt(irg, "gvn-pre");
593 do_irg_opt(irg, "gcse");
594 do_irg_opt(irg, "place");
595 do_irg_opt(irg, "control-flow");
597 if (do_irg_opt(irg, "if-conversion")) {
598 do_irg_opt(irg, "local");
599 do_irg_opt(irg, "control-flow");
601 /* this doesn't make too much sense but tests the mux destruction... */
602 do_irg_opt(irg, "lower-mux");
604 do_irg_opt(irg, "bool");
605 do_irg_opt(irg, "shape-blocks");
606 do_irg_opt(irg, "ivopts");
607 do_irg_opt(irg, "local");
608 do_irg_opt(irg, "dead");
611 do_irp_opt("inline");
612 do_irp_opt("opt-proc-clone");
614 for (i = 0; i < get_irp_n_irgs(); i++) {
615 ir_graph *irg = get_irp_irg(i);
616 do_irg_opt(irg, "local");
617 do_irg_opt(irg, "control-flow");
618 do_irg_opt(irg, "thread-jumps");
619 do_irg_opt(irg, "local");
620 do_irg_opt(irg, "control-flow");
622 if( do_irg_opt(irg, "vrp") ) { // if vrp is enabled
623 do_irg_opt(irg, "local");
624 do_irg_opt(irg, "vrp");
625 do_irg_opt(irg, "local");
626 do_irg_opt(irg, "vrp");
630 if (firm_dump.ir_graph) {
631 /* recompute backedges for nicer dumps */
632 for (i = 0; i < get_irp_n_irgs(); i++)
633 construct_cf_backedges(get_irp_irg(i));
638 if (firm_dump.statistic & STAT_AFTER_OPT)
639 stat_dump_snapshot(input_filename, "opt");
645 * @param input_filename the name of the (main) source file
647 static void do_firm_lowering(const char *input_filename)
651 /* enable architecture dependent optimizations */
652 arch_dep_set_opts((arch_dep_opts_t)
653 ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) |
654 (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) |
655 (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) ));
656 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
657 ir_graph *irg = get_irp_irg(i);
658 do_irg_opt(irg, "reassociation");
659 do_irg_opt(irg, "local");
662 do_irp_opt("target-lowering");
664 if (firm_dump.statistic & STAT_AFTER_LOWER)
665 stat_dump_snapshot(input_filename, "low");
667 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
668 ir_graph *irg = get_irp_irg(i);
670 do_irg_opt(irg, "local");
671 do_irg_opt(irg, "deconv");
672 do_irg_opt(irg, "control-flow");
673 do_irg_opt(irg, "opt-load-store");
674 do_irg_opt(irg, "gcse");
675 do_irg_opt(irg, "place");
676 do_irg_opt(irg, "control-flow");
678 if (do_irg_opt(irg, "vrp")) {
679 do_irg_opt(irg, "local");
680 do_irg_opt(irg, "control-flow");
681 do_irg_opt(irg, "vrp");
682 do_irg_opt(irg, "local");
683 do_irg_opt(irg, "control-flow");
686 if (do_irg_opt(irg, "if-conversion")) {
687 do_irg_opt(irg, "local");
688 do_irg_opt(irg, "control-flow");
691 add_irg_constraints(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2);
692 do_irg_opt(irg, "local");
694 do_irg_opt(irg, "parallelize-mem");
695 do_irg_opt(irg, "frame");
697 /* hack so we get global initializers constant folded even at -O0 */
698 set_opt_constant_folding(1);
699 set_opt_algebraic_simplification(1);
700 do_irp_opt("local-const");
701 set_opt_constant_folding(firm_opt.const_folding);
702 set_opt_algebraic_simplification(firm_opt.const_folding);
703 do_irp_opt("remove-unused");
704 do_irp_opt("opt-cc");
707 if (firm_dump.statistic & STAT_FINAL) {
708 stat_dump_snapshot(input_filename, "final");
713 * Initialize for the Firm-generating back end.
715 void gen_firm_init(void)
718 enable_safe_defaults();
721 i->timer = ir_timer_new();
722 timer_register(i->timer, i->description);
724 t_verify = ir_timer_new();
725 timer_register(t_verify, "Firm: verify pass");
726 t_vcg_dump = ir_timer_new();
727 timer_register(t_vcg_dump, "Firm: vcg dumping");
728 t_all_opt = ir_timer_new();
729 timer_register(t_all_opt, "Firm: all optimizations");
730 t_backend = ir_timer_new();
731 timer_register(t_backend, "Firm: backend");
735 * Called, after the Firm generation is completed,
736 * do all optimizations and backend call here.
738 * @param out a file handle for the output, may be NULL
739 * @param input_filename the name of the (main) source file
741 void generate_code(FILE *out, const char *input_filename)
745 /* initialize implicit opts, just to be sure because really the frontend
746 * should have called it already before starting graph construction */
747 init_implicit_optimizations();
750 do_node_verification((firm_verification_t) firm_opt.verify);
752 /* the general for dumping option must be set, or the others will not work*/
753 firm_dump.ir_graph = (bool) (firm_dump.ir_graph | firm_dump.all_phases);
755 ir_add_dump_flags(ir_dump_flag_keepalive_edges
756 | ir_dump_flag_consts_local | ir_dump_flag_dominance);
757 ir_remove_dump_flags(ir_dump_flag_loops | ir_dump_flag_ld_names);
759 /* FIXME: cloning might ADD new graphs. */
760 irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
762 ir_timer_init_parent(t_verify);
763 ir_timer_init_parent(t_vcg_dump);
764 timer_start(t_all_opt);
766 if (firm_dump.all_types) {
767 dump_ir_prog_ext(dump_typegraph, "types.vcg");
772 if (firm_opt.verify) {
773 timer_push(t_verify);
778 /* BEWARE: kill unreachable code before doing compound lowering */
779 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
780 ir_graph *irg = get_irp_irg(i);
781 do_irg_opt(irg, "control-flow");
784 if (firm_dump.statistic & STAT_BEFORE_OPT) {
785 stat_dump_snapshot(input_filename, "noopt");
788 do_firm_optimizations(input_filename);
789 do_firm_lowering(input_filename);
791 timer_stop(t_all_opt);
793 if (firm_dump.statistic & STAT_FINAL_IR)
794 stat_dump_snapshot(input_filename, "final-ir");
796 /* run the code generator */
797 timer_start(t_backend);
798 be_main(out, input_filename);
799 timer_stop(t_backend);
801 if (firm_dump.statistic & STAT_FINAL)
802 stat_dump_snapshot(input_filename, "final");
805 void gen_firm_finish(void)
810 static void disable_all_opts(void)
812 firm_opt.cse = false;
813 firm_opt.confirm = false;
814 firm_opt.muls = false;
815 firm_opt.divs = false;
816 firm_opt.mods = false;
817 firm_opt.alias_analysis = false;
818 firm_opt.strict_alias = false;
819 firm_opt.no_alias = false;
820 firm_opt.const_folding = false;
822 FOR_EACH_OPT(config) {
823 if (config->flags & OPT_FLAG_ESSENTIAL) {
824 config->flags |= OPT_FLAG_ENABLED;
826 config->flags &= ~OPT_FLAG_ENABLED;
831 static bool firm_opt_option(const char *opt)
833 char const* const rest = strstart(opt, "no-");
834 bool const enable = rest ? opt = rest, false : true;
836 opt_config_t *config = get_opt(opt);
837 if (config == NULL || (config->flags & OPT_FLAG_HIDE_OPTIONS))
840 config->flags &= ~OPT_FLAG_ENABLED;
841 config->flags |= enable ? OPT_FLAG_ENABLED : 0;
845 void firm_option_help(print_option_help_func print_option_help)
847 FOR_EACH_OPT(config) {
851 if (config->flags & OPT_FLAG_HIDE_OPTIONS)
854 snprintf(buf, sizeof(buf), "-f%s", config->name);
855 snprintf(buf2, sizeof(buf2), "enable %s", config->description);
856 print_option_help(buf, buf2);
857 snprintf(buf, sizeof(buf), "-fno-%s", config->name);
858 snprintf(buf2, sizeof(buf2), "disable %s", config->description);
859 print_option_help(buf, buf2);
862 for (size_t k = 0; k != lengthof(firm_options); ++k) {
865 snprintf(buf, sizeof(buf), "-f%s", firm_options[k].option);
866 snprintf(buf2, sizeof(buf2), "%s", firm_options[k].description);
867 print_option_help(buf, buf2);
871 int firm_option(const char *const opt)
874 if ((val = strstart(opt, "dump-filter="))) {
875 ir_set_dump_filter(val);
877 } else if ((val = strstart(opt, "clone-threshold="))) {
878 sscanf(val, "%d", &firm_opt.clone_threshold);
880 } else if ((val = strstart(opt, "inline-max-size="))) {
881 sscanf(val, "%u", &firm_opt.inline_maxsize);
883 } else if ((val = strstart(opt, "inline-threshold="))) {
884 sscanf(val, "%u", &firm_opt.inline_threshold);
886 } else if (streq(opt, "no-opt")) {
891 size_t const len = strlen(opt);
892 for (size_t i = lengthof(firm_options); i != 0;) {
893 struct params const* const o = &firm_options[--i];
894 if (len == o->opt_len && memcmp(opt, o->option, len) == 0) {
895 /* statistic options do accumulate */
896 if (o->flag == &firm_dump.statistic)
897 *o->flag = (bool) (*o->flag | o->set);
905 /* maybe this enables/disables optimizations */
906 if (firm_opt_option(opt))
912 static void set_be_option(const char *arg)
914 int res = be_parse_arg(arg);
919 static void set_option(const char *arg)
921 int res = firm_option(arg);
926 void choose_optimization_pack(int level)
928 /* apply optimization level */
931 set_option("no-opt");
934 set_option("no-inline");
938 /* use_builtins = true; */
941 set_option("thread-jumps");
942 set_option("if-conversion");
945 set_option("strict-aliasing");
946 set_option("inline");
947 set_option("fp-vrp");
948 set_option("deconv");
949 set_be_option("omitfp");
954 void init_implicit_optimizations(void)
957 set_opt_constant_folding(firm_opt.const_folding);
958 set_opt_algebraic_simplification(firm_opt.const_folding);
959 set_opt_cse(firm_opt.cse);
960 set_opt_global_cse(0);