- int i;
- ir_graph *irg;
- unsigned aa_opt;
-
- /* FIXME: cloning might ADD new graphs. */
- irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
-
- set_opt_strength_red(firm_opt.strength_red);
- set_opt_scalar_replacement(firm_opt.scalar_replace);
- set_opt_auto_create_sync(firm_opt.auto_sync);
- set_opt_alias_analysis(firm_opt.alias_analysis);
-
- aa_opt = aa_opt_no_opt;
- if (firm_opt.strict_alias)
- aa_opt |= aa_opt_type_based | aa_opt_byte_type_may_alias;
- if (firm_opt.no_alias)
- aa_opt = aa_opt_no_alias;
-
- set_irp_memory_disambiguator_options(aa_opt);
-
- timer_start(TV_ALL_OPT);
-
- if (firm_opt.remove_unused) {
- ir_entity **keep_methods;
- int arr_len;
-
- /* Analysis that finds the free methods,
- i.e. methods that are dereferenced.
- Optimizes polymorphic calls :-). */
- cgana(&arr_len, &keep_methods);
-
- /* Remove methods that are never called. */
- gc_irgs(arr_len, keep_methods);
-
- free(keep_methods);
- }
-
- if (! firm_opt.freestanding) {
- rts_map();
- DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "rts");
- CHECK_ALL(firm_opt.check_all);
- }
-
- /* first step: kill dead code */
- for (i = 0; i < get_irp_n_irgs(); i++) {
- irg = current_ir_graph = get_irp_irg(i);
- /* Confirm construction currently can only handle blocks with only one control
- flow predecessor. Calling optimize_cf here removes Bad predecessors and help
- the optimization of switch constructs. */
- timer_push(TV_CF_OPT);
- optimize_graph_df(irg);
- optimize_cf(irg);
- timer_pop();
- }
-
- if (firm_opt.func_calls) {
- timer_push(TV_REAL_FUNC_CALL);
- optimize_funccalls(firm_const_exists, NULL);
- timer_pop();
- DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "func_call");
- CHECK_ALL(firm_opt.check_all);
- }
-
- /* do lowering on the const code irg */
- lower_const_code();
-
- for (i = 0; i < get_irp_n_irgs(); i++) {
- irg = current_ir_graph = get_irp_irg(i);
-
-#ifdef FIRM_EXT_GRS
- /* If SIMD optimization is on, make sure we have only 1 return */
- if (firm_ext_grs.create_pattern || firm_ext_grs.simd_opt)
- normalize_one_return(irg);
-#endif
-
-
-#if 0
- if (firm_opt.modes) {
- /* convert all modes into integer if possible */
- arch_mode_conversion(irg, predefs.mode_uint);
- }
-#endif
- timer_push(TV_SCALAR_REPLACE);
- scalar_replacement_opt(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "scalar");
- CHECK_ONE(firm_opt.check_all, irg);
-
- timer_push(TV_LOCAL_OPT);
- optimize_graph_df(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
- CHECK_ONE(firm_opt.check_all, irg);
-
- timer_push(TV_REASSOCIATION);
- optimize_reassociation(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "reassoc");
- CHECK_ONE(firm_opt.check_all, irg);
-
- compute_doms(irg);
- CHECK_ONE(firm_opt.check_all, irg);
-
- if (firm_opt.code_place) {
- timer_push(TV_CODE_PLACE);
- set_opt_global_cse(1);
- optimize_graph_df(irg);
- place_code(irg);
- set_opt_global_cse(0);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "place");
- CHECK_ONE(firm_opt.check_all, irg);
- }
-
- if (firm_opt.confirm) {
- /* Confirm construction currently can only handle blocks with only one control
- flow predecessor. Calling optimize_cf here removes Bad predecessors and help
- the optimization of switch constructs. */
- timer_push(TV_CF_OPT);
- optimize_cf(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
- CHECK_ONE(firm_opt.check_all, irg);
- timer_push(TV_CONFIRM_CREATE);
- construct_confirms(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "confirms");
- CHECK_ONE(firm_opt.check_all, irg);
-
- timer_push(TV_LOCAL_OPT);
- optimize_graph_df(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
- CHECK_ONE(firm_opt.check_all, irg);
- }
-
- if (firm_opt.luffig) {
- opt_ldst2(irg);
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "ldst2");
- CHECK_ONE(firm_opt.check_all, irg);
- }
-
- timer_push(TV_CF_OPT);
- optimize_cf(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
- CHECK_ONE(firm_opt.check_all, irg);
-
- irg_verify(irg, VRFY_ENFORCE_SSA);
- if (firm_opt.gvn_pre) {
- do_gvn_pre(irg);
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "pre");
- CHECK_ONE(firm_opt.check_all, irg);
- irg_verify(irg, VRFY_ENFORCE_SSA);
- }
-
- if (firm_opt.load_store) {
- timer_push(TV_LOAD_STORE);
- optimize_load_store(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "ldst");
- CHECK_ONE(firm_opt.check_all, irg);
- }
-
- lower_highlevel_graph(irg, firm_opt.lower_bitfields);
-
- if (firm_opt.deconv) {
- timer_push(TV_DECONV);
- conv_opt(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "deconv");
- CHECK_ONE(firm_opt.check_all, irg);
- }
-
- if (firm_opt.cond_eval) {
- timer_push(TV_COND_EVAL);
- opt_cond_eval(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cond_eval");
- CHECK_ONE(firm_opt.check_all, irg);
- }
-
- /* should we really remove the Confirm here? */
- if (firm_opt.confirm) {
- timer_push(TV_CONFIRM_CREATE);
- remove_confirms(irg);
- timer_pop();
- }
-
- compute_doms(irg);
- compute_postdoms(irg);
- DUMP_ONE_CFG_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "dom");
- CHECK_ONE(firm_opt.check_all, irg);
-
- construct_backedges(irg);
-
- timer_push(TV_CF_OPT);
- optimize_cf(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
- CHECK_ONE(firm_opt.check_all, irg);
-
- if (firm_opt.if_conversion) {
- timer_push(TV_IF_CONV);
- opt_if_conv(current_ir_graph, if_conv_info);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "if");
- CHECK_ONE(firm_opt.check_all, current_ir_graph);
-
- timer_push(TV_LOCAL_OPT);
- optimize_graph_df(current_ir_graph);
- timer_pop();
- timer_push(TV_CF_OPT);
- optimize_cf(current_ir_graph);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "after_if");
- CHECK_ONE(firm_opt.check_all, current_ir_graph);
- }
-
- if (firm_opt.bool_opt) {
- opt_bool(irg);
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "bool");
- CHECK_ONE(firm_opt.check_all, irg);
- }
-
- timer_push(TV_OSR);
- opt_osr(current_ir_graph, osr_flag_default | osr_flag_keep_reg_pressure | osr_flag_ignore_x86_shift);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "stred");
- CHECK_ONE(firm_opt.check_all, irg);
-
- timer_push(TV_LOCAL_OPT);
- optimize_graph_df(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "lopt");
- CHECK_ONE(firm_opt.check_all, irg);
-
- edges_deactivate(irg);
- timer_push(TV_DEAD_NODE);
- dead_node_elimination(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "dead");
- CHECK_ONE(firm_opt.check_all, irg);
- }
-
- if (firm_opt.do_inline) {
- inline_functions(500, 50);
- DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "inl");
- CHECK_ALL(firm_opt.check_all);
- }
- if (firm_opt.cloning) {
- proc_cloning((float)firm_opt.clone_threshold);
- DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "clone");
- CHECK_ALL(firm_opt.check_all);
- }
- if (firm_opt.tail_rec) {
- timer_push(TV_TAIL_REC);
- opt_tail_recursion();
- timer_pop();
-
- DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, "tail_rec");
- CHECK_ALL(firm_opt.check_all);
- }
-
- if (firm_opt.cond_eval) {
- for (i = 0; i < get_irp_n_irgs(); i++) {
- irg = get_irp_irg(i);
- timer_push(TV_COND_EVAL);
- opt_cond_eval(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cond_eval");
- CHECK_ONE(firm_opt.check_all, irg);
- }
- }
-
- /* final run of local optimizations */
- for (i = 0; i < get_irp_n_irgs(); i++) {
- irg = get_irp_irg(i);
- timer_push(TV_LOCAL_OPT);
- optimize_graph_df(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "local_opt");
- CHECK_ONE(firm_opt.check_all, irg);
-
- timer_push(TV_CF_OPT);
- optimize_cf(irg);
- timer_pop();
- DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, "cfopt");
- CHECK_ONE(firm_opt.check_all, irg);
- }
-
- if (firm_dump.ir_graph) {
- /* recompute backedges for nicer dumps */
- for (i = 0; i < get_irp_n_irgs(); i++)
- construct_cf_backedges(get_irp_irg(i));
- }
-
- if (firm_opt.remove_unused) {
- ir_entity **keep_methods;
- int arr_len;
-
- /* Analysis that finds the free methods,
- i.e. methods that are dereferenced.
- Optimizes polymorphic calls :-). */
- cgana(&arr_len, &keep_methods);
-
- /* Remove methods that are never called. */
- gc_irgs(arr_len, keep_methods);
-
- free(keep_methods);
- }
-
-
- DUMP_ALL(firm_dump.ir_graph, "-opt");
-
- /* verify optimized graphs */
- for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
- ir_graph *irg = get_irp_irg(i);
- irg_verify(irg, VRFY_ENFORCE_SSA);
- }
-
- if (firm_dump.statistic & STAT_AFTER_OPT)
- stat_dump_snapshot(input_filename, "opt");
-
- timer_stop(TV_ALL_OPT);
-} /* do_firm_optimizations */
+ set_opt_global_cse(1);
+ optimize_graph_df(irg);
+ set_opt_global_cse(0);
+}
+
+static void lower_blockcopy(ir_graph *irg)
+{
+ lower_CopyB(irg, 128, 4);
+}
+
+typedef enum opt_target {
+ OPT_TARGET_IRG, /**< optimization function works on a single graph */
+ OPT_TARGET_IRP /**< optimization function works on the complete program */
+} opt_target_t;
+
+typedef enum opt_flags {
+ OPT_FLAG_NONE = 0,
+ OPT_FLAG_ENABLED = 1 << 0, /**< enable the optimization */
+ OPT_FLAG_NO_DUMP = 1 << 1, /**< don't dump after transformation */
+ OPT_FLAG_NO_VERIFY = 1 << 2, /**< don't verify after transformation */
+ OPT_FLAG_HIDE_OPTIONS = 1 << 3, /**< do not automatically process
+ -foptions for this transformation */
+ OPT_FLAG_ESSENTIAL = 1 << 4, /**< output won't work without this pass
+ so we need it even with -O0 */
+} opt_flags_t;
+
+typedef void (*transform_irg_func)(ir_graph *irg);
+typedef void (*transform_irp_func)(void);
+
+typedef struct {
+ opt_target_t target;
+ const char *name;
+ union {
+ transform_irg_func transform_irg;
+ transform_irp_func transform_irp;
+ } u;
+ const char *description;
+ opt_flags_t flags;
+} opt_config_t;
+
+static opt_config_t opts[] = {
+#define IRG(a, b, c, d) { OPT_TARGET_IRG, a, .u.transform_irg = (transform_irg_func)b, c, d }
+#define IRP(a, b, c, d) { OPT_TARGET_IRP, a, .u.transform_irp = b, c, d }
+ IRG("bool", opt_bool, "bool simplification", OPT_FLAG_NONE),
+ IRG("combo", combo, "combined CCE, UCE and GVN", OPT_FLAG_NONE),
+ IRG("confirm", construct_confirms, "confirm optimization", OPT_FLAG_HIDE_OPTIONS),
+ IRG("control-flow", optimize_cf, "optimization of control-flow", OPT_FLAG_HIDE_OPTIONS),
+ IRG("dead", dead_node_elimination, "dead node elimination", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
+ IRG("deconv", conv_opt, "conv node elimination", OPT_FLAG_NONE),
+ IRG("fp-vrp", fixpoint_vrp, "fixpoint value range propagation", OPT_FLAG_NONE),
+ IRG("frame", opt_frame_irg, "remove unused frame entities", OPT_FLAG_NONE),
+ IRG("gvn-pre", do_gvn_pre, "global value numbering partial redundancy elimination", OPT_FLAG_NONE),
+ IRG("if-conversion", opt_if_conv, "if-conversion", OPT_FLAG_NONE),
+ IRG("invert-loops", do_loop_inversion, "loop inversion", OPT_FLAG_NONE),
+ IRG("ivopts", do_stred, "induction variable strength reduction", OPT_FLAG_NONE),
+ IRG("local", optimize_graph_df, "local graph optimizations", OPT_FLAG_HIDE_OPTIONS),
+ IRG("lower", do_lower_highlevel, "lowering", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
+ IRG("lower-mux", do_lower_mux, "mux lowering", OPT_FLAG_NONE),
+ IRG("opt-load-store", optimize_load_store, "load store optimization", OPT_FLAG_NONE),
+ IRG("opt-tail-rec", opt_tail_rec_irg, "tail-recursion eliminiation", OPT_FLAG_NONE),
+ IRG("parallelize-mem", opt_parallelize_mem, "parallelize memory", OPT_FLAG_NONE),
+ IRG("gcse", do_gcse, "global common subexpression eliminiation", OPT_FLAG_NONE),
+ IRG("place", place_code, "code placement", OPT_FLAG_NONE),
+ IRG("reassociation", optimize_reassociation, "reassociation", OPT_FLAG_NONE),
+ IRG("remove-confirms", remove_confirms, "confirm removal", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
+ IRG("remove-phi-cycles", remove_phi_cycles, "removal of phi cycles", OPT_FLAG_HIDE_OPTIONS),
+ IRG("scalar-replace", scalar_replacement_opt, "scalar replacement", OPT_FLAG_NONE),
+ IRG("shape-blocks", shape_blocks, "block shaping", OPT_FLAG_NONE),
+ IRG("thread-jumps", opt_jumpthreading, "path-sensitive jumpthreading", OPT_FLAG_NONE),
+ IRG("unroll-loops", do_loop_unrolling, "loop unrolling", OPT_FLAG_NONE),
+ IRG("vrp", set_vrp_data, "value range propagation", OPT_FLAG_NONE),
+ IRP("inline", do_inline, "inlining", OPT_FLAG_NONE),
+ 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),
+ IRG("lower-blockcopy", lower_blockcopy, "replace small block copies with load/store sequences", OPT_FLAG_NO_DUMP),
+ IRP("target-lowering", be_lower_for_target, "lowering necessary for target architecture", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL),
+ IRP("opt-func-call", optimize_funccalls, "function call optimization", OPT_FLAG_NONE),
+ IRP("opt-proc-clone", do_cloning, "procedure cloning", OPT_FLAG_NONE),
+ IRP("remove-unused", garbage_collect_entities, "removal of unused functions/variables", OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY),
+ IRP("rts", rts_map, "optimization of known library functions", OPT_FLAG_NONE),
+ IRP("opt-cc", mark_private_methods, "calling conventions optimization", OPT_FLAG_NONE),
+#undef IRP
+#undef IRG
+};
+static const int n_opts = sizeof(opts) / sizeof(opts[0]);
+static ir_timer_t *timers[sizeof(opts)/sizeof(opts[0])];
+
+static opt_config_t *get_opt(const char *name)
+{
+ int i;
+ for (i = 0; i < n_opts; ++i) {
+ opt_config_t *config = &opts[i];
+ if (strcmp(config->name, name) == 0)
+ return config;
+ }
+
+ return NULL;
+}
+
+static void set_opt_enabled(const char *name, bool enabled)
+{
+ opt_config_t *config = get_opt(name);
+ config->flags = (config->flags & ~OPT_FLAG_ENABLED)
+ | (enabled ? OPT_FLAG_ENABLED : 0);
+}
+
+static bool get_opt_enabled(const char *name)
+{
+ opt_config_t *config = get_opt(name);
+ return (config->flags & OPT_FLAG_ENABLED) != 0;
+}