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 */
57 bool stat_pattern; /**< enable Firm statistic pattern */
58 bool stat_dag; /**< enable Firm DAG statistic */
61 struct a_firm_be_opt {
66 /* optimization settings */
67 static struct a_firm_opt firm_opt = {
68 .const_folding = true,
74 .alias_analysis = true,
75 .strict_alias = false,
77 .verify = FIRM_VERIFICATION_ON,
79 .clone_threshold = DEFAULT_CLONE_THRESHOLD,
80 .inline_maxsize = 750,
81 .inline_threshold = 0,
85 static struct a_firm_dump firm_dump = {
90 .statistic = STAT_NONE,
95 #define X(a) a, sizeof(a)-1
97 /** Parameter description structure */
98 static const struct params {
99 const char *option; /**< name of the option */
100 size_t opt_len; /**< length of the option string */
101 bool *flag; /**< address of variable to set/reset */
102 bool set; /**< iff true, variable will be set, else reset */
103 const char *description; /**< description of this option */
105 /* firm optimization options */
106 { X("no-opt"), NULL, 0, "disable all FIRM optimizations" },
107 { X("cse"), &firm_opt.cse, 1, "enable common subexpression elimination" },
108 { X("no-cse"), &firm_opt.cse, 0, "disable common subexpression elimination" },
109 { X("const-fold"), &firm_opt.const_folding, 1, "enable constant folding" },
110 { X("no-const-fold"), &firm_opt.const_folding, 0, "disable constant folding" },
111 { X("inline-max-size=<size>"), NULL, 0, "set maximum size for function inlining" },
112 { X("inline-threshold=<size>"),NULL, 0, "set benefice threshold for function inlining" },
113 { X("confirm"), &firm_opt.confirm, 1, "enable Confirm optimization" },
114 { X("no-confirm"), &firm_opt.confirm, 0, "disable Confirm optimization" },
115 { X("opt-mul"), &firm_opt.muls, 0, "enable multiplication optimization" },
116 { X("no-opt-mul"), &firm_opt.muls, 0, "disable multiplication optimization" },
117 { X("opt-div"), &firm_opt.divs, 0, "enable division optimization" },
118 { X("no-opt-div"), &firm_opt.divs, 0, "disable division optimization" },
119 { X("opt-mod"), &firm_opt.mods, 0, "enable remainder optimization" },
120 { X("no-opt-mod"), &firm_opt.mods, 0, "disable remainder optimization" },
121 { X("opt-alias"), &firm_opt.alias_analysis, 1, "enable alias analysis" },
122 { X("no-opt-alias"), &firm_opt.alias_analysis, 0, "disable alias analysis" },
123 { X("alias"), &firm_opt.no_alias, 0, "aliasing occurs" },
124 { X("no-alias"), &firm_opt.no_alias, 1, "no aliasing occurs" },
125 { X("strict-aliasing"), &firm_opt.strict_alias, 1, "strict alias rules" },
126 { X("no-strict-aliasing"), &firm_opt.strict_alias, 0, "strict alias rules" },
127 { X("clone-threshold=<value>"),NULL, 0, "set clone threshold to <value>" },
129 /* other firm regarding options */
130 { X("verify-off"), &firm_opt.verify, FIRM_VERIFICATION_OFF, "disable node verification" },
131 { X("verify-on"), &firm_opt.verify, FIRM_VERIFICATION_ON, "enable node verification" },
132 { X("verify-report"), &firm_opt.verify, FIRM_VERIFICATION_REPORT, "node verification, report only" },
133 { X("check-all"), &firm_opt.check_all, 1, "enable checking all Firm phases" },
134 { X("no-check-all"), &firm_opt.check_all, 0, "disable checking all Firm phases" },
137 { X("dump-ir"), &firm_dump.ir_graph, 1, "dump IR graph" },
138 { X("dump-all-types"), &firm_dump.all_types, 1, "dump graph of all types" },
139 { X("dump-all-phases"), &firm_dump.all_phases, 1, "dump graphs for all optimization phases" },
140 { X("dump-filter=<string>"), NULL, 0, "set dumper filter" },
143 { X("stat-before-opt"), &firm_dump.statistic, STAT_BEFORE_OPT, "Firm statistic output before optimizations" },
144 { X("stat-after-opt"), &firm_dump.statistic, STAT_AFTER_OPT, "Firm statistic output after optimizations" },
145 { X("stat-after-lower"), &firm_dump.statistic, STAT_AFTER_LOWER, "Firm statistic output after lowering" },
146 { X("stat-final-ir"), &firm_dump.statistic, STAT_FINAL_IR, "Firm statistic after final optimization" },
147 { X("stat-final"), &firm_dump.statistic, STAT_FINAL, "Firm statistic after code generation" },
148 { X("stat-pattern"), &firm_dump.stat_pattern, 1, "Firm statistic calculates most used pattern" },
149 { X("stat-dag"), &firm_dump.stat_dag, 1, "Firm calculates DAG statistics" },
154 static ir_timer_t *t_vcg_dump;
155 static ir_timer_t *t_verify;
156 static ir_timer_t *t_all_opt;
157 static ir_timer_t *t_backend;
158 static bool do_irg_opt(ir_graph *irg, const char *name);
160 /** dump all the graphs depending on cond */
162 static void dump_all(const char *suffix)
164 if (!firm_dump.ir_graph)
167 timer_push(t_vcg_dump);
168 dump_all_ir_graphs(suffix);
169 timer_pop(t_vcg_dump);
172 /* entities of runtime functions */
173 ir_entity_ptr rts_entities[rts_max];
176 * Map runtime functions.
178 static void rts_map(void)
180 static const struct {
181 ir_entity_ptr *ent; /**< address of the rts entity */
182 i_mapper_func func; /**< mapper function. */
185 { &rts_entities[rts_abs], i_mapper_abs },
186 { &rts_entities[rts_labs], i_mapper_abs },
187 { &rts_entities[rts_llabs], i_mapper_abs },
188 { &rts_entities[rts_imaxabs], i_mapper_abs },
190 /* double -> double */
191 { &rts_entities[rts_fabs], i_mapper_abs },
192 { &rts_entities[rts_sqrt], i_mapper_sqrt },
193 { &rts_entities[rts_cbrt], i_mapper_cbrt },
194 { &rts_entities[rts_pow], i_mapper_pow },
195 { &rts_entities[rts_exp], i_mapper_exp },
196 { &rts_entities[rts_exp2], i_mapper_exp },
197 { &rts_entities[rts_exp10], i_mapper_exp },
198 { &rts_entities[rts_log], i_mapper_log },
199 { &rts_entities[rts_log2], i_mapper_log2 },
200 { &rts_entities[rts_log10], i_mapper_log10 },
201 { &rts_entities[rts_sin], i_mapper_sin },
202 { &rts_entities[rts_cos], i_mapper_cos },
203 { &rts_entities[rts_tan], i_mapper_tan },
204 { &rts_entities[rts_asin], i_mapper_asin },
205 { &rts_entities[rts_acos], i_mapper_acos },
206 { &rts_entities[rts_atan], i_mapper_atan },
207 { &rts_entities[rts_sinh], i_mapper_sinh },
208 { &rts_entities[rts_cosh], i_mapper_cosh },
209 { &rts_entities[rts_tanh], i_mapper_tanh },
212 { &rts_entities[rts_fabsf], i_mapper_abs },
213 { &rts_entities[rts_sqrtf], i_mapper_sqrt },
214 { &rts_entities[rts_cbrtf], i_mapper_cbrt },
215 { &rts_entities[rts_powf], i_mapper_pow },
216 { &rts_entities[rts_expf], i_mapper_exp },
217 { &rts_entities[rts_exp2f], i_mapper_exp },
218 { &rts_entities[rts_exp10f], i_mapper_exp },
219 { &rts_entities[rts_logf], i_mapper_log },
220 { &rts_entities[rts_log2f], i_mapper_log2 },
221 { &rts_entities[rts_log10f], i_mapper_log10 },
222 { &rts_entities[rts_sinf], i_mapper_sin },
223 { &rts_entities[rts_cosf], i_mapper_cos },
224 { &rts_entities[rts_tanf], i_mapper_tan },
225 { &rts_entities[rts_asinf], i_mapper_asin },
226 { &rts_entities[rts_acosf], i_mapper_acos },
227 { &rts_entities[rts_atanf], i_mapper_atan },
228 { &rts_entities[rts_sinhf], i_mapper_sinh },
229 { &rts_entities[rts_coshf], i_mapper_cosh },
230 { &rts_entities[rts_tanhf], i_mapper_tanh },
232 /* long double -> long double */
233 { &rts_entities[rts_fabsl], i_mapper_abs },
234 { &rts_entities[rts_sqrtl], i_mapper_sqrt },
235 { &rts_entities[rts_cbrtl], i_mapper_cbrt },
236 { &rts_entities[rts_powl], i_mapper_pow },
237 { &rts_entities[rts_expl], i_mapper_exp },
238 { &rts_entities[rts_exp2l], i_mapper_exp },
239 { &rts_entities[rts_exp10l], i_mapper_exp },
240 { &rts_entities[rts_logl], i_mapper_log },
241 { &rts_entities[rts_log2l], i_mapper_log2 },
242 { &rts_entities[rts_log10l], i_mapper_log10 },
243 { &rts_entities[rts_sinl], i_mapper_sin },
244 { &rts_entities[rts_cosl], i_mapper_cos },
245 { &rts_entities[rts_tanl], i_mapper_tan },
246 { &rts_entities[rts_asinl], i_mapper_asin },
247 { &rts_entities[rts_acosl], i_mapper_acos },
248 { &rts_entities[rts_atanl], i_mapper_atan },
249 { &rts_entities[rts_sinhl], i_mapper_sinh },
250 { &rts_entities[rts_coshl], i_mapper_cosh },
251 { &rts_entities[rts_tanhl], i_mapper_tanh },
254 { &rts_entities[rts_strcmp], i_mapper_strcmp },
255 { &rts_entities[rts_strncmp], i_mapper_strncmp },
256 { &rts_entities[rts_strcpy], i_mapper_strcpy },
257 { &rts_entities[rts_strlen], i_mapper_strlen },
258 { &rts_entities[rts_memcpy], i_mapper_memcpy },
259 { &rts_entities[rts_mempcpy], i_mapper_mempcpy },
260 { &rts_entities[rts_memmove], i_mapper_memmove },
261 { &rts_entities[rts_memset], i_mapper_memset },
262 { &rts_entities[rts_memcmp], i_mapper_memcmp }
264 i_record rec[lengthof(mapper)];
267 for (size_t i = 0; i != lengthof(mapper); ++i) {
268 if (*mapper[i].ent != NULL) {
269 rec[n_map].i_call.kind = INTRINSIC_CALL;
270 rec[n_map].i_call.i_ent = *mapper[i].ent;
271 rec[n_map].i_call.i_mapper = mapper[i].func;
272 rec[n_map].i_call.ctx = NULL;
273 rec[n_map].i_call.link = NULL;
279 lower_intrinsics(rec, n_map, /* part_block_used=*/0);
282 static int *irg_dump_no;
284 static void do_stred(ir_graph *irg)
286 opt_osr(irg, osr_flag_default | osr_flag_keep_reg_pressure | osr_flag_ignore_x86_shift);
289 static void after_inline_opt(ir_graph *irg)
291 do_irg_opt(irg, "scalar-replace");
292 do_irg_opt(irg, "local");
293 do_irg_opt(irg, "control-flow");
294 do_irg_opt(irg, "combo");
297 static void do_inline(void)
299 inline_functions(firm_opt.inline_maxsize, firm_opt.inline_threshold,
303 static void do_cloning(void)
305 proc_cloning((float) firm_opt.clone_threshold);
308 static void do_lower_mux(ir_graph *irg)
310 lower_mux(irg, NULL);
313 static void do_gcse(ir_graph *irg)
315 set_opt_global_cse(1);
316 optimize_graph_df(irg);
317 set_opt_global_cse(0);
320 typedef enum opt_target {
321 OPT_TARGET_IRG, /**< optimization function works on a single graph */
322 OPT_TARGET_IRP /**< optimization function works on the complete program */
325 typedef enum opt_flags {
327 OPT_FLAG_ENABLED = 1 << 0, /**< enable the optimization */
328 OPT_FLAG_NO_DUMP = 1 << 1, /**< don't dump after transformation */
329 OPT_FLAG_NO_VERIFY = 1 << 2, /**< don't verify after transformation */
330 OPT_FLAG_HIDE_OPTIONS = 1 << 3, /**< do not automatically process
331 -foptions for this transformation */
332 OPT_FLAG_ESSENTIAL = 1 << 4, /**< output won't work without this pass
333 so we need it even with -O0 */
336 typedef void (*transform_irg_func)(ir_graph *irg);
337 typedef void (*transform_irp_func)(void);
343 transform_irg_func transform_irg;
344 transform_irp_func transform_irp;
346 const char *description;
351 static opt_config_t opts[] = {
352 #define IRG(a, b, c, d) { OPT_TARGET_IRG, a, .u.transform_irg = (transform_irg_func)b, c, d }
353 #define IRP(a, b, c, d) { OPT_TARGET_IRP, a, .u.transform_irp = b, c, d }
354 IRG("bool", opt_bool, "bool simplification", OPT_FLAG_NONE),
355 IRG("combo", combo, "combined CCE, UCE and GVN", OPT_FLAG_NONE),
356 IRG("confirm", construct_confirms, "confirm optimization", OPT_FLAG_HIDE_OPTIONS),
357 IRG("control-flow", optimize_cf, "optimization of control-flow", OPT_FLAG_HIDE_OPTIONS),
358 IRG("dead", dead_node_elimination, "dead node elimination", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
359 IRG("deconv", conv_opt, "conv node elimination", OPT_FLAG_NONE),
360 IRG("fp-vrp", fixpoint_vrp, "fixpoint value range propagation", OPT_FLAG_NONE),
361 IRG("frame", opt_frame_irg, "remove unused frame entities", OPT_FLAG_NONE),
362 IRG("gvn-pre", do_gvn_pre, "global value numbering partial redundancy elimination", OPT_FLAG_NONE),
363 IRG("if-conversion", opt_if_conv, "if-conversion", OPT_FLAG_NONE),
364 IRG("invert-loops", do_loop_inversion, "loop inversion", OPT_FLAG_NONE),
365 IRG("ivopts", do_stred, "induction variable strength reduction", OPT_FLAG_NONE),
366 IRG("local", local_opts, "local graph optimizations", OPT_FLAG_HIDE_OPTIONS),
367 IRG("lower", lower_highlevel_graph, "lowering", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
368 IRG("lower-mux", do_lower_mux, "mux lowering", OPT_FLAG_NONE),
369 IRG("opt-load-store", optimize_load_store, "load store optimization", OPT_FLAG_NONE),
370 IRG("opt-tail-rec", opt_tail_rec_irg, "tail-recursion eliminiation", OPT_FLAG_NONE),
371 IRG("parallelize-mem", opt_parallelize_mem, "parallelize memory", OPT_FLAG_NONE),
372 IRG("gcse", do_gcse, "global common subexpression eliminiation", OPT_FLAG_NONE),
373 IRG("place", place_code, "code placement", OPT_FLAG_NONE),
374 IRG("reassociation", optimize_reassociation, "reassociation", OPT_FLAG_NONE),
375 IRG("remove-confirms", remove_confirms, "confirm removal", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
376 IRG("remove-phi-cycles", remove_phi_cycles, "removal of phi cycles", OPT_FLAG_HIDE_OPTIONS),
377 IRG("scalar-replace", scalar_replacement_opt, "scalar replacement", OPT_FLAG_NONE),
378 IRG("shape-blocks", shape_blocks, "block shaping", OPT_FLAG_NONE),
379 IRG("thread-jumps", opt_jumpthreading, "path-sensitive jumpthreading", OPT_FLAG_NONE),
380 IRG("unroll-loops", do_loop_unrolling, "loop unrolling", OPT_FLAG_NONE),
381 IRG("vrp", set_vrp_data, "value range propagation", OPT_FLAG_NONE),
382 IRP("inline", do_inline, "inlining", OPT_FLAG_NONE),
383 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),
384 IRP("target-lowering", be_lower_for_target, "lowering necessary for target architecture", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
385 IRP("opt-func-call", optimize_funccalls, "function call optimization", OPT_FLAG_NONE),
386 IRP("opt-proc-clone", do_cloning, "procedure cloning", OPT_FLAG_NONE),
387 IRP("remove-unused", garbage_collect_entities, "removal of unused functions/variables", OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
388 IRP("rts", rts_map, "optimization of known library functions", OPT_FLAG_NONE),
389 IRP("opt-cc", mark_private_methods, "calling conventions optimization", OPT_FLAG_NONE),
394 #define FOR_EACH_OPT(i) for (opt_config_t *i = opts; i != endof(opts); ++i)
396 static opt_config_t *get_opt(const char *name)
398 FOR_EACH_OPT(config) {
399 if (streq(config->name, name))
406 static void set_opt_enabled(const char *name, bool enabled)
408 opt_config_t *config = get_opt(name);
409 config->flags = (config->flags & ~OPT_FLAG_ENABLED)
410 | (enabled ? OPT_FLAG_ENABLED : 0);
413 static bool get_opt_enabled(const char *name)
415 opt_config_t *config = get_opt(name);
416 return (config->flags & OPT_FLAG_ENABLED) != 0;
420 * perform an optimization on a single graph
422 * @return true if something changed, false otherwise
424 static bool do_irg_opt(ir_graph *irg, const char *name)
426 opt_config_t *const config = get_opt(name);
427 assert(config != NULL);
428 assert(config->target == OPT_TARGET_IRG);
429 if (! (config->flags & OPT_FLAG_ENABLED))
432 ir_graph *const old_irg = current_ir_graph;
433 current_ir_graph = irg;
435 timer_push(config->timer);
436 config->u.transform_irg(irg);
437 timer_pop(config->timer);
439 if (firm_dump.all_phases && firm_dump.ir_graph) {
440 dump_ir_graph(irg, name);
443 if (firm_opt.check_all) {
444 timer_push(t_verify);
445 irg_verify(irg, VERIFY_ENFORCE_SSA);
449 current_ir_graph = old_irg;
453 static void do_irp_opt(const char *name)
455 opt_config_t *const config = get_opt(name);
456 assert(config->target == OPT_TARGET_IRP);
457 if (! (config->flags & OPT_FLAG_ENABLED))
460 timer_push(config->timer);
461 config->u.transform_irp();
462 timer_pop(config->timer);
464 if (firm_dump.ir_graph && firm_dump.all_phases) {
466 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
467 ir_graph *irg = get_irp_irg(i);
468 dump_ir_graph(irg, name);
472 if (firm_opt.check_all) {
474 timer_push(t_verify);
475 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
476 irg_verify(get_irp_irg(i), VERIFY_ENFORCE_SSA);
483 * Enable transformations which should be always safe (and cheap) to perform
485 static void enable_safe_defaults(void)
487 set_opt_enabled("remove-unused", true);
488 set_opt_enabled("opt-tail-rec", true);
489 set_opt_enabled("opt-func-call", true);
490 set_opt_enabled("reassociation", true);
491 set_opt_enabled("control-flow", true);
492 set_opt_enabled("local", true);
493 set_opt_enabled("lower-const", true);
494 set_opt_enabled("scalar-replace", true);
495 set_opt_enabled("place", true);
496 set_opt_enabled("gcse", true);
497 set_opt_enabled("confirm", true);
498 set_opt_enabled("opt-load-store", true);
499 set_opt_enabled("lower", true);
500 set_opt_enabled("deconv", true);
501 set_opt_enabled("remove-confirms", true);
502 set_opt_enabled("ivopts", true);
503 set_opt_enabled("dead", true);
504 set_opt_enabled("remove-phi-cycles", true);
505 set_opt_enabled("frame", true);
506 set_opt_enabled("combo", true);
507 set_opt_enabled("invert-loops", true);
508 set_opt_enabled("target-lowering", true);
509 set_opt_enabled("rts", true);
510 set_opt_enabled("parallelize-mem", true);
511 set_opt_enabled("opt-cc", true);
515 * run all the Firm optimizations
517 * @param input_filename the name of the (main) source file
519 static void do_firm_optimizations(const char *input_filename)
524 set_opt_alias_analysis(firm_opt.alias_analysis);
526 aa_opt = aa_opt_no_opt;
527 if (firm_opt.strict_alias)
528 aa_opt |= aa_opt_type_based | aa_opt_byte_type_may_alias;
529 if (firm_opt.no_alias)
530 aa_opt = aa_opt_no_alias;
532 set_irp_memory_disambiguator_options(aa_opt);
534 /* parameter passing code should set them directly sometime... */
535 set_opt_enabled("confirm", firm_opt.confirm);
536 set_opt_enabled("remove-confirms", firm_opt.confirm);
538 /* osr supersedes remove_phi_cycles */
539 if (get_opt_enabled("ivopts"))
540 set_opt_enabled("remove-phi-cycles", false);
542 timer_start(t_all_opt);
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");
641 timer_stop(t_all_opt);
647 * @param input_filename the name of the (main) source file
649 static void do_firm_lowering(const char *input_filename)
653 /* enable architecture dependent optimizations */
654 arch_dep_set_opts((arch_dep_opts_t)
655 ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) |
656 (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) |
657 (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) ));
658 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
659 ir_graph *irg = get_irp_irg(i);
660 do_irg_opt(irg, "reassociation");
661 do_irg_opt(irg, "local");
664 do_irp_opt("target-lowering");
666 if (firm_dump.statistic & STAT_AFTER_LOWER)
667 stat_dump_snapshot(input_filename, "low");
669 timer_start(t_all_opt);
671 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
672 ir_graph *irg = get_irp_irg(i);
674 do_irg_opt(irg, "local");
675 do_irg_opt(irg, "deconv");
676 do_irg_opt(irg, "control-flow");
677 do_irg_opt(irg, "opt-load-store");
678 do_irg_opt(irg, "gcse");
679 do_irg_opt(irg, "place");
680 do_irg_opt(irg, "control-flow");
682 if (do_irg_opt(irg, "vrp")) {
683 do_irg_opt(irg, "local");
684 do_irg_opt(irg, "control-flow");
685 do_irg_opt(irg, "vrp");
686 do_irg_opt(irg, "local");
687 do_irg_opt(irg, "control-flow");
690 if (do_irg_opt(irg, "if-conversion")) {
691 do_irg_opt(irg, "local");
692 do_irg_opt(irg, "control-flow");
695 set_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2);
696 do_irg_opt(irg, "local");
698 do_irg_opt(irg, "parallelize-mem");
699 do_irg_opt(irg, "frame");
701 do_irp_opt("remove-unused");
702 do_irp_opt("opt-cc");
703 timer_stop(t_all_opt);
706 if (firm_dump.statistic & STAT_FINAL) {
707 stat_dump_snapshot(input_filename, "final");
712 * Initialize for the Firm-generating back end.
714 void gen_firm_init(void)
717 enable_safe_defaults();
720 i->timer = ir_timer_new();
721 timer_register(i->timer, i->description);
723 t_verify = ir_timer_new();
724 timer_register(t_verify, "Firm: verify pass");
725 t_vcg_dump = ir_timer_new();
726 timer_register(t_vcg_dump, "Firm: vcg dumping");
727 t_all_opt = ir_timer_new();
728 timer_register(t_all_opt, "Firm: all optimizations");
729 t_backend = ir_timer_new();
730 timer_register(t_backend, "Firm: backend");
733 static void init_statistics(void)
735 unsigned pattern = 0;
737 if (firm_dump.stat_pattern)
738 pattern |= FIRMSTAT_PATTERN_ENABLED;
740 if (firm_dump.stat_dag)
741 pattern |= FIRMSTAT_COUNT_DAG;
743 firm_init_stat(firm_dump.statistic == STAT_NONE ?
744 0 : FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP
745 | FIRMSTAT_COUNT_CONSTS | pattern);
749 * Called, after the Firm generation is completed,
750 * do all optimizations and backend call here.
752 * @param out a file handle for the output, may be NULL
753 * @param input_filename the name of the (main) source file
755 void generate_code(FILE *out, const char *input_filename)
760 set_opt_constant_folding(firm_opt.const_folding);
761 set_opt_algebraic_simplification(firm_opt.const_folding);
762 set_opt_cse(firm_opt.cse);
763 set_opt_global_cse(0);
767 do_node_verification((firm_verification_t) firm_opt.verify);
769 /* the general for dumping option must be set, or the others will not work*/
770 firm_dump.ir_graph = (bool) (firm_dump.ir_graph | firm_dump.all_phases);
772 ir_add_dump_flags(ir_dump_flag_keepalive_edges
773 | ir_dump_flag_consts_local | ir_dump_flag_dominance);
774 ir_remove_dump_flags(ir_dump_flag_loops | ir_dump_flag_ld_names);
776 /* FIXME: cloning might ADD new graphs. */
777 irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
779 if (firm_dump.all_types) {
780 dump_ir_prog_ext(dump_typegraph, "types.vcg");
785 timer_push(t_verify);
789 /* all graphs are finalized, set the irp phase to high */
790 set_irp_phase_state(phase_high);
792 /* BEWARE: kill unreachable code before doing compound lowering */
793 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
794 ir_graph *irg = get_irp_irg(i);
795 do_irg_opt(irg, "control-flow");
798 if (firm_dump.statistic & STAT_BEFORE_OPT) {
799 stat_dump_snapshot(input_filename, "noopt");
802 do_firm_optimizations(input_filename);
803 do_firm_lowering(input_filename);
805 /* set the phase to low */
806 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
807 set_irg_phase_state(get_irp_irg(i), phase_low);
809 if (firm_dump.statistic & STAT_FINAL_IR)
810 stat_dump_snapshot(input_filename, "final-ir");
812 /* run the code generator */
813 timer_start(t_backend);
814 be_main(out, input_filename);
815 timer_stop(t_backend);
817 if (firm_dump.statistic & STAT_FINAL)
818 stat_dump_snapshot(input_filename, "final");
821 void gen_firm_finish(void)
826 static void disable_all_opts(void)
828 firm_opt.cse = false;
829 firm_opt.confirm = false;
830 firm_opt.muls = false;
831 firm_opt.divs = false;
832 firm_opt.mods = false;
833 firm_opt.alias_analysis = false;
834 firm_opt.strict_alias = false;
835 firm_opt.no_alias = false;
837 FOR_EACH_OPT(config) {
838 if (config->flags & OPT_FLAG_ESSENTIAL) {
839 config->flags |= OPT_FLAG_ENABLED;
841 config->flags &= ~OPT_FLAG_ENABLED;
846 static bool firm_opt_option(const char *opt)
848 char const* const rest = strstart(opt, "no-");
849 bool const enable = rest ? opt = rest, false : true;
851 opt_config_t *config = get_opt(opt);
852 if (config == NULL || (config->flags & OPT_FLAG_HIDE_OPTIONS))
855 config->flags &= ~OPT_FLAG_ENABLED;
856 config->flags |= enable ? OPT_FLAG_ENABLED : 0;
860 void firm_option_help(print_option_help_func print_option_help)
862 print_option_help(firm_options[0].option, firm_options[0].description);
864 FOR_EACH_OPT(config) {
868 if (config->flags & OPT_FLAG_HIDE_OPTIONS)
871 snprintf(buf, sizeof(buf), "-f%s", config->name);
872 snprintf(buf2, sizeof(buf2), "enable %s", config->description);
873 print_option_help(buf, buf2);
874 snprintf(buf, sizeof(buf), "-fno-%s", config->name);
875 snprintf(buf2, sizeof(buf2), "disable %s", config->description);
876 print_option_help(buf, buf2);
879 for (size_t k = 0; k != lengthof(firm_options); ++k) {
882 snprintf(buf, sizeof(buf), "-f%s", firm_options[k].option);
883 snprintf(buf2, sizeof(buf2), "%s", firm_options[k].description);
884 print_option_help(buf, buf2);
888 int firm_option(const char *const opt)
891 if ((val = strstart(opt, "dump-filter="))) {
892 ir_set_dump_filter(val);
894 } else if ((val = strstart(opt, "clone-threshold="))) {
895 sscanf(val, "%d", &firm_opt.clone_threshold);
897 } else if ((val = strstart(opt, "inline-max-size="))) {
898 sscanf(val, "%u", &firm_opt.inline_maxsize);
900 } else if ((val = strstart(opt, "inline-threshold="))) {
901 sscanf(val, "%u", &firm_opt.inline_threshold);
903 } else if (streq(opt, "no-opt")) {
908 size_t const len = strlen(opt);
909 for (size_t i = lengthof(firm_options); i != 0;) {
910 struct params const* const o = &firm_options[--i];
911 if (len == o->opt_len && memcmp(opt, o->option, len) == 0) {
912 /* statistic options do accumulate */
913 if (o->flag == &firm_dump.statistic)
914 *o->flag = (bool) (*o->flag | o->set);
922 /* maybe this enables/disables optimizations */
923 if (firm_opt_option(opt))
929 static void set_be_option(const char *arg)
931 int res = be_parse_arg(arg);
936 static void set_option(const char *arg)
938 int res = firm_option(arg);
943 void choose_optimization_pack(int level)
945 /* apply optimization level */
948 set_option("no-opt");
951 set_option("no-inline");
955 /* use_builtins = true; */
958 set_option("thread-jumps");
959 set_option("if-conversion");
962 set_option("strict-aliasing");
963 set_option("inline");
964 set_option("fp-vrp");
965 set_option("deconv");
966 set_be_option("omitfp");