X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=driver%2Ffirm_opt.c;h=ada3dda15550c1ae6c46eb09fe52a63138759529;hb=d80c4f36144467e678d933574ff7bf48cf1e2b71;hp=de1be1583a2104d29c7305b749da126a11ba86b1;hpb=d7ecd04c10b91d4bc0bc7ac6e7123ea6ab488014;p=cparser diff --git a/driver/firm_opt.c b/driver/firm_opt.c index de1be15..ada3dda 100644 --- a/driver/firm_opt.c +++ b/driver/firm_opt.c @@ -1,12 +1,9 @@ /** - * - * @file firm_opt.c -- Firm-generating back end optimizations. - * - * (C) 2005-2007 Michael Beck beck@ipd.info.uni-karlsruhe.de - * - * $Id$ + * (C) 2005-2010 + * @file + * @author Michael Beck, Matthias Braun + * @brief Firm-generating back end optimizations. */ - #include #include @@ -15,321 +12,292 @@ #include #include #include -#include - #include "firm_opt.h" -#include "firm_codegen.h" -#include "firm_cmdline.h" #include "firm_timing.h" #include "ast2firm.h" +#include "adt/strutil.h" +#include "adt/util.h" + +/* optimization settings */ +struct a_firm_opt { + bool const_folding; /**< enable constant folding */ + bool cse; /**< enable common-subexpression elimination */ + bool confirm; /**< enable Confirm optimization */ + bool muls; /**< enable architecture dependent mul optimization */ + bool divs; /**< enable architecture dependent div optimization */ + bool mods; /**< enable architecture dependent mod optimization */ + bool alias_analysis; /**< enable Alias Analysis */ + bool strict_alias; /**< enable strict Alias Analysis (using type based AA) */ + bool no_alias; /**< no aliasing possible. */ + bool verify; /**< Firm verifier setting */ + bool check_all; /**< enable checking all Firm phases */ + int clone_threshold; /**< The threshold value for procedure cloning. */ + unsigned inline_maxsize; /**< Maximum function size for inlining. */ + unsigned inline_threshold;/**< Inlining benefice threshold. */ +}; -#if defined(_DEBUG) || defined(FIRM_DEBUG) -#define DBG(x) dbg_printf x -#else -#define DBG(x) -#endif /* _DEBUG || FIRM_DEBUG */ - - -/** dump all the graphs depending on cond */ -#define DUMP_ALL(cond, suffix) \ - do { \ - if (cond) { \ - timer_push(TV_VCG_DUMP); \ - if (firm_dump.no_blocks) \ - dump_all_ir_graphs(dump_ir_graph, suffix); \ - else if (firm_dump.extbb) \ - dump_all_ir_graphs(dump_ir_extblock_graph, suffix);\ - else \ - dump_all_ir_graphs(dump_ir_block_graph, suffix); \ - timer_pop(); \ - } \ - } while (0) - -/** dump all control flow graphs depending on cond */ -#define DUMP_ALL_CFG(cond, suffix) \ - do { \ - if (cond) { \ - timer_push(TV_VCG_DUMP); \ - dump_all_ir_graphs(dump_cfg, suffix); \ - timer_pop(); \ - } \ - } while (0) - -/** check all graphs depending on cond */ -#define CHECK_ALL(cond) \ - do { \ - if (cond) { \ - int ii; \ - timer_push(TV_VERIFY); \ - for (ii = get_irp_n_irgs() - 1; ii >= 0; --ii) \ - irg_verify(get_irp_irg(ii), VRFY_ENFORCE_SSA); \ - timer_pop(); \ - } \ - } while (0) - - - -/** dump graphs irg depending on cond */ -#define DUMP_ONE(cond, irg, suffix) \ - do { \ - if (cond) { \ - timer_push(TV_VCG_DUMP); \ - if (firm_dump.no_blocks) \ - dump_ir_graph(irg, suffix); \ - else if (firm_dump.extbb) \ - dump_ir_extblock_graph(irg, suffix); \ - else \ - dump_ir_block_graph(irg, suffix); \ - timer_pop(); \ - } \ - } while (0) - -/** dump control flow graph irg depending on cond */ -#define DUMP_ONE_CFG(cond, irg, suffix) \ - do { \ - if (cond) { \ - timer_push(TV_VCG_DUMP); \ - dump_cfg(irg, suffix); \ - timer_pop(); \ - } \ - } while (0) - -/** check a graph irg depending on cond */ -#define CHECK_ONE(cond, irg) \ - do { \ - if (cond) { \ - timer_push(TV_VERIFY); \ - irg_verify(irg, VRFY_ENFORCE_SSA); \ - timer_pop(); \ - } \ - } while (0) - - -/* set by the backend parameters */ -static const ir_settings_arch_dep_t *ad_param = NULL; -static create_intrinsic_fkt *arch_create_intrinsic = NULL; -static void *create_intrinsic_ctx = NULL; -static const ir_settings_if_conv_t *if_conv_info = NULL; - -/* entities of runtime functions */ -ir_entity_ptr rts_entities[rts_max]; +/** statistic options */ +typedef enum a_firmstat_selection_tag { + STAT_NONE = 0x00000000, + STAT_BEFORE_OPT = 0x00000001, + STAT_AFTER_OPT = 0x00000002, + STAT_AFTER_LOWER = 0x00000004, + STAT_FINAL_IR = 0x00000008, + STAT_FINAL = 0x00000010, +} a_firmstat_selection; + +/* dumping options */ +struct a_firm_dump { + bool debug_print; /**< enable debug print */ + bool all_types; /**< dump the All_types graph */ + bool ir_graph; /**< dump all graphs */ + bool all_phases; /**< dump the IR graph after all phases */ + bool statistic; /**< Firm statistic setting */ + bool stat_pattern; /**< enable Firm statistic pattern */ + bool stat_dag; /**< enable Firm DAG statistic */ +}; -/** - * factory for setting architecture dependent parameters - */ -static const ir_settings_arch_dep_t *arch_factory(void) -{ - static const ir_settings_arch_dep_t param = { - 1, /* also use subs */ - 4, /* maximum shifts */ - 31, /* maximum shift amount */ - NULL, /* use default evaluator */ - - 1, /* allow Mulhs */ - 1, /* allow Mulus */ - 32 /* Mulh allowed up to 32 bit */ - }; - - return ad_param ? ad_param : ¶m; -} +struct a_firm_be_opt { + bool selection; + bool node_stat; +}; -/** - * Map runtime functions. - */ -static void rts_map(void) { - static const struct { - ir_entity_ptr *ent; /**< address of the rts entity */ - i_mapper_func func; /**< mapper function. */ - } mapper[] = { - /* integer */ - { &rts_entities[rts_abs], i_mapper_abs }, - { &rts_entities[rts_labs], i_mapper_abs }, - { &rts_entities[rts_llabs], i_mapper_abs }, - { &rts_entities[rts_imaxabs], i_mapper_abs }, - - /* double -> double */ - { &rts_entities[rts_fabs], i_mapper_abs }, - { &rts_entities[rts_sqrt], i_mapper_sqrt }, - { &rts_entities[rts_cbrt], i_mapper_cbrt }, - { &rts_entities[rts_pow], i_mapper_pow }, - { &rts_entities[rts_exp], i_mapper_exp }, - { &rts_entities[rts_exp2], i_mapper_exp }, - { &rts_entities[rts_exp10], i_mapper_exp }, - { &rts_entities[rts_log], i_mapper_log }, - { &rts_entities[rts_log2], i_mapper_log2 }, - { &rts_entities[rts_log10], i_mapper_log10 }, - { &rts_entities[rts_sin], i_mapper_sin }, - { &rts_entities[rts_cos], i_mapper_cos }, - { &rts_entities[rts_tan], i_mapper_tan }, - { &rts_entities[rts_asin], i_mapper_asin }, - { &rts_entities[rts_acos], i_mapper_acos }, - { &rts_entities[rts_atan], i_mapper_atan }, - { &rts_entities[rts_sinh], i_mapper_sinh }, - { &rts_entities[rts_cosh], i_mapper_cosh }, - { &rts_entities[rts_tanh], i_mapper_tanh }, - - /* float -> float */ - { &rts_entities[rts_fabsf], i_mapper_abs }, - { &rts_entities[rts_sqrtf], i_mapper_sqrt }, - { &rts_entities[rts_cbrtf], i_mapper_cbrt }, - { &rts_entities[rts_powf], i_mapper_pow }, - { &rts_entities[rts_expf], i_mapper_exp }, - { &rts_entities[rts_exp2f], i_mapper_exp }, - { &rts_entities[rts_exp10f], i_mapper_exp }, - { &rts_entities[rts_logf], i_mapper_log }, - { &rts_entities[rts_log2f], i_mapper_log2 }, - { &rts_entities[rts_log10f], i_mapper_log10 }, - { &rts_entities[rts_sinf], i_mapper_sin }, - { &rts_entities[rts_cosf], i_mapper_cos }, - { &rts_entities[rts_tanf], i_mapper_tan }, - { &rts_entities[rts_asinf], i_mapper_asin }, - { &rts_entities[rts_acosf], i_mapper_acos }, - { &rts_entities[rts_atanf], i_mapper_atan }, - { &rts_entities[rts_sinhf], i_mapper_sinh }, - { &rts_entities[rts_coshf], i_mapper_cosh }, - { &rts_entities[rts_tanhf], i_mapper_tanh }, - - /* long double -> long double */ - { &rts_entities[rts_fabsl], i_mapper_abs }, - { &rts_entities[rts_sqrtl], i_mapper_sqrt }, - { &rts_entities[rts_cbrtl], i_mapper_cbrt }, - { &rts_entities[rts_powl], i_mapper_pow }, - { &rts_entities[rts_expl], i_mapper_exp }, - { &rts_entities[rts_exp2l], i_mapper_exp }, - { &rts_entities[rts_exp10l], i_mapper_exp }, - { &rts_entities[rts_logl], i_mapper_log }, - { &rts_entities[rts_log2l], i_mapper_log2 }, - { &rts_entities[rts_log10l], i_mapper_log10 }, - { &rts_entities[rts_sinl], i_mapper_sin }, - { &rts_entities[rts_cosl], i_mapper_cos }, - { &rts_entities[rts_tanl], i_mapper_tan }, - { &rts_entities[rts_asinl], i_mapper_asin }, - { &rts_entities[rts_acosl], i_mapper_acos }, - { &rts_entities[rts_atanl], i_mapper_atan }, - { &rts_entities[rts_sinhl], i_mapper_sinh }, - { &rts_entities[rts_coshl], i_mapper_cosh }, - { &rts_entities[rts_tanhl], i_mapper_tanh }, - - /* string */ - { &rts_entities[rts_memcpy], i_mapper_memcpy }, - { &rts_entities[rts_memset], i_mapper_memset }, - { &rts_entities[rts_strcmp], i_mapper_strcmp }, - { &rts_entities[rts_strncmp], i_mapper_strncmp }, - { &rts_entities[rts_strlen], i_mapper_strlen } - }; - i_record rec[sizeof(mapper)/sizeof(mapper[0])]; - unsigned i, n_map; - - for (i = n_map = 0; i < sizeof(mapper)/sizeof(mapper[0]); ++i) - if (*mapper[i].ent != NULL) { - rec[n_map].i_call.kind = INTRINSIC_CALL; - rec[n_map].i_call.i_ent = *mapper[i].ent; - rec[n_map].i_call.i_mapper = mapper[i].func; - rec[n_map].i_call.ctx = NULL; - rec[n_map].i_call.link = NULL; - ++n_map; - } /* if */ - if (n_map > 0) - lower_intrinsics(rec, n_map, /* part_block_used=*/0); -} /* rts_map */ +/* optimization settings */ +static struct a_firm_opt firm_opt = { + .const_folding = true, + .cse = true, + .confirm = true, + .muls = true, + .divs = true, + .mods = true, + .alias_analysis = true, + .strict_alias = false, + .no_alias = false, + .verify = FIRM_VERIFICATION_ON, + .check_all = true, + .clone_threshold = DEFAULT_CLONE_THRESHOLD, + .inline_maxsize = 750, + .inline_threshold = 0, +}; -static int *irg_dump_no; +/* dumping options */ +static struct a_firm_dump firm_dump = { + .debug_print = false, + .all_types = false, + .ir_graph = false, + .all_phases = false, + .statistic = STAT_NONE, + .stat_pattern = 0, + .stat_dag = 0, +}; -static void dump_graph_count(ir_graph *const irg, const char *const suffix) -{ - char name[64]; - snprintf(name, sizeof(name), "-%02d_%s", irg_dump_no[get_irg_idx(irg)]++, suffix); - DUMP_ONE(1, irg, name); -} +#define X(a) a, sizeof(a)-1 + +/** Parameter description structure */ +static const struct params { + const char *option; /**< name of the option */ + size_t opt_len; /**< length of the option string */ + bool *flag; /**< address of variable to set/reset */ + bool set; /**< iff true, variable will be set, else reset */ + const char *description; /**< description of this option */ +} firm_options[] = { + /* firm optimization options */ + { X("no-opt"), NULL, 0, "disable all FIRM optimizations" }, + { X("cse"), &firm_opt.cse, 1, "enable common subexpression elimination" }, + { X("no-cse"), &firm_opt.cse, 0, "disable common subexpression elimination" }, + { X("const-fold"), &firm_opt.const_folding, 1, "enable constant folding" }, + { X("no-const-fold"), &firm_opt.const_folding, 0, "disable constant folding" }, + { X("inline-max-size="), NULL, 0, "set maximum size for function inlining" }, + { X("inline-threshold="),NULL, 0, "set benefice threshold for function inlining" }, + { X("confirm"), &firm_opt.confirm, 1, "enable Confirm optimization" }, + { X("no-confirm"), &firm_opt.confirm, 0, "disable Confirm optimization" }, + { X("opt-mul"), &firm_opt.muls, 0, "enable multiplication optimization" }, + { X("no-opt-mul"), &firm_opt.muls, 0, "disable multiplication optimization" }, + { X("opt-div"), &firm_opt.divs, 0, "enable division optimization" }, + { X("no-opt-div"), &firm_opt.divs, 0, "disable division optimization" }, + { X("opt-mod"), &firm_opt.mods, 0, "enable remainder optimization" }, + { X("no-opt-mod"), &firm_opt.mods, 0, "disable remainder optimization" }, + { X("opt-alias"), &firm_opt.alias_analysis, 1, "enable alias analysis" }, + { X("no-opt-alias"), &firm_opt.alias_analysis, 0, "disable alias analysis" }, + { X("alias"), &firm_opt.no_alias, 0, "aliasing occurs" }, + { X("no-alias"), &firm_opt.no_alias, 1, "no aliasing occurs" }, + { X("strict-aliasing"), &firm_opt.strict_alias, 1, "strict alias rules" }, + { X("no-strict-aliasing"), &firm_opt.strict_alias, 0, "strict alias rules" }, + { X("clone-threshold="),NULL, 0, "set clone threshold to " }, + + /* other firm regarding options */ + { X("verify-off"), &firm_opt.verify, FIRM_VERIFICATION_OFF, "disable node verification" }, + { X("verify-on"), &firm_opt.verify, FIRM_VERIFICATION_ON, "enable node verification" }, + { X("verify-report"), &firm_opt.verify, FIRM_VERIFICATION_REPORT, "node verification, report only" }, + { X("check-all"), &firm_opt.check_all, 1, "enable checking all Firm phases" }, + { X("no-check-all"), &firm_opt.check_all, 0, "disable checking all Firm phases" }, + + /* dumping */ + { X("dump-ir"), &firm_dump.ir_graph, 1, "dump IR graph" }, + { X("dump-all-types"), &firm_dump.all_types, 1, "dump graph of all types" }, + { X("dump-all-phases"), &firm_dump.all_phases, 1, "dump graphs for all optimization phases" }, + { X("dump-filter="), NULL, 0, "set dumper filter" }, + + /* misc */ + { X("stat-before-opt"), &firm_dump.statistic, STAT_BEFORE_OPT, "Firm statistic output before optimizations" }, + { X("stat-after-opt"), &firm_dump.statistic, STAT_AFTER_OPT, "Firm statistic output after optimizations" }, + { X("stat-after-lower"), &firm_dump.statistic, STAT_AFTER_LOWER, "Firm statistic output after lowering" }, + { X("stat-final-ir"), &firm_dump.statistic, STAT_FINAL_IR, "Firm statistic after final optimization" }, + { X("stat-final"), &firm_dump.statistic, STAT_FINAL, "Firm statistic after code generation" }, + { X("stat-pattern"), &firm_dump.stat_pattern, 1, "Firm statistic calculates most used pattern" }, + { X("stat-dag"), &firm_dump.stat_dag, 1, "Firm calculates DAG statistics" }, +}; -#if 0 -static void dump_graph_cfg_count(ir_graph *const irg, const char *const suffix) -{ - char name[64]; - snprintf(name, sizeof(name), "-%02d_%s", irg_dump_no[get_irg_idx(irg)]++, suffix); - DUMP_ONE_CFG(1, irg, name); -} -#endif +#undef X -static void dump_all_count(const char *const suffix) -{ - const int n_irgs = get_irp_n_irgs(); - int i; +static ir_timer_t *t_vcg_dump; +static ir_timer_t *t_verify; +static ir_timer_t *t_all_opt; +static ir_timer_t *t_backend; +static bool do_irg_opt(ir_graph *irg, const char *name); - for (i = 0; i < n_irgs; ++i) - dump_graph_count(get_irp_irg(i), suffix); -} +/** dump all the graphs depending on cond */ -#define DUMP_ONE_C(cond, irg, suffix) \ - do { \ - if (cond) { \ - dump_graph_count((irg), (suffix)); \ - } \ - } while (0) - -#define DUMP_ONE_CFG_C(cond, irg, suffix) \ - do { \ - if (cond) { \ - dump_graph_cfg_count((irg), (suffix)); \ - } \ - } while (0) - -#define DUMP_ALL_C(cond, suffix) \ - do { \ - if (cond) { \ - dump_all_count((suffix)); \ - } \ - } while (0) - -static void remove_unused_functions(void) +static void dump_all(const char *suffix) { - 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); + if (!firm_dump.ir_graph) + return; - /* Remove methods that are never called. */ - gc_irgs(arr_len, keep_methods); - free(keep_methods); + timer_push(t_vcg_dump); + dump_all_ir_graphs(suffix); + timer_pop(t_vcg_dump); } -static int firm_const_exists; +/* entities of runtime functions */ +ir_entity *rts_entities[rts_max]; -static void do_optimize_funccalls(void) +/** + * Map runtime functions. + */ +static void rts_map(void) { - optimize_funccalls(firm_const_exists, NULL); -} + static const struct { + ir_entity **ent; /**< address of the rts entity */ + i_mapper_func func; /**< mapper function. */ + } mapper[] = { + /* integer */ + { &rts_entities[rts_abs], i_mapper_abs }, + { &rts_entities[rts_labs], i_mapper_abs }, + { &rts_entities[rts_llabs], i_mapper_abs }, + { &rts_entities[rts_imaxabs], i_mapper_abs }, + + /* double -> double */ + { &rts_entities[rts_fabs], i_mapper_abs }, + { &rts_entities[rts_sqrt], i_mapper_sqrt }, + { &rts_entities[rts_cbrt], i_mapper_cbrt }, + { &rts_entities[rts_pow], i_mapper_pow }, + { &rts_entities[rts_exp], i_mapper_exp }, + { &rts_entities[rts_exp2], i_mapper_exp }, + { &rts_entities[rts_exp10], i_mapper_exp }, + { &rts_entities[rts_log], i_mapper_log }, + { &rts_entities[rts_log2], i_mapper_log2 }, + { &rts_entities[rts_log10], i_mapper_log10 }, + { &rts_entities[rts_sin], i_mapper_sin }, + { &rts_entities[rts_cos], i_mapper_cos }, + { &rts_entities[rts_tan], i_mapper_tan }, + { &rts_entities[rts_asin], i_mapper_asin }, + { &rts_entities[rts_acos], i_mapper_acos }, + { &rts_entities[rts_atan], i_mapper_atan }, + { &rts_entities[rts_sinh], i_mapper_sinh }, + { &rts_entities[rts_cosh], i_mapper_cosh }, + { &rts_entities[rts_tanh], i_mapper_tanh }, + + /* float -> float */ + { &rts_entities[rts_fabsf], i_mapper_abs }, + { &rts_entities[rts_sqrtf], i_mapper_sqrt }, + { &rts_entities[rts_cbrtf], i_mapper_cbrt }, + { &rts_entities[rts_powf], i_mapper_pow }, + { &rts_entities[rts_expf], i_mapper_exp }, + { &rts_entities[rts_exp2f], i_mapper_exp }, + { &rts_entities[rts_exp10f], i_mapper_exp }, + { &rts_entities[rts_logf], i_mapper_log }, + { &rts_entities[rts_log2f], i_mapper_log2 }, + { &rts_entities[rts_log10f], i_mapper_log10 }, + { &rts_entities[rts_sinf], i_mapper_sin }, + { &rts_entities[rts_cosf], i_mapper_cos }, + { &rts_entities[rts_tanf], i_mapper_tan }, + { &rts_entities[rts_asinf], i_mapper_asin }, + { &rts_entities[rts_acosf], i_mapper_acos }, + { &rts_entities[rts_atanf], i_mapper_atan }, + { &rts_entities[rts_sinhf], i_mapper_sinh }, + { &rts_entities[rts_coshf], i_mapper_cosh }, + { &rts_entities[rts_tanhf], i_mapper_tanh }, + + /* long double -> long double */ + { &rts_entities[rts_fabsl], i_mapper_abs }, + { &rts_entities[rts_sqrtl], i_mapper_sqrt }, + { &rts_entities[rts_cbrtl], i_mapper_cbrt }, + { &rts_entities[rts_powl], i_mapper_pow }, + { &rts_entities[rts_expl], i_mapper_exp }, + { &rts_entities[rts_exp2l], i_mapper_exp }, + { &rts_entities[rts_exp10l], i_mapper_exp }, + { &rts_entities[rts_logl], i_mapper_log }, + { &rts_entities[rts_log2l], i_mapper_log2 }, + { &rts_entities[rts_log10l], i_mapper_log10 }, + { &rts_entities[rts_sinl], i_mapper_sin }, + { &rts_entities[rts_cosl], i_mapper_cos }, + { &rts_entities[rts_tanl], i_mapper_tan }, + { &rts_entities[rts_asinl], i_mapper_asin }, + { &rts_entities[rts_acosl], i_mapper_acos }, + { &rts_entities[rts_atanl], i_mapper_atan }, + { &rts_entities[rts_sinhl], i_mapper_sinh }, + { &rts_entities[rts_coshl], i_mapper_cosh }, + { &rts_entities[rts_tanhl], i_mapper_tanh }, + + /* string */ + { &rts_entities[rts_strcmp], i_mapper_strcmp }, + { &rts_entities[rts_strncmp], i_mapper_strncmp }, + { &rts_entities[rts_strcpy], i_mapper_strcpy }, + { &rts_entities[rts_strlen], i_mapper_strlen }, + { &rts_entities[rts_memcpy], i_mapper_memcpy }, + { &rts_entities[rts_mempcpy], i_mapper_mempcpy }, + { &rts_entities[rts_memmove], i_mapper_memmove }, + { &rts_entities[rts_memset], i_mapper_memset }, + { &rts_entities[rts_memcmp], i_mapper_memcmp } + }; + i_record rec[lengthof(mapper)]; + size_t n_map = 0; + + for (size_t i = 0; i != lengthof(mapper); ++i) { + if (*mapper[i].ent != NULL) { + rec[n_map].i_call.kind = INTRINSIC_CALL; + rec[n_map].i_call.i_ent = *mapper[i].ent; + rec[n_map].i_call.i_mapper = mapper[i].func; + rec[n_map].i_call.ctx = NULL; + rec[n_map].i_call.link = NULL; + ++n_map; + } + } -static void do_gcse(ir_graph *irg) -{ - set_opt_global_cse(1); - optimize_graph_df(irg); - place_code(irg); - set_opt_global_cse(0); + if (n_map > 0) + lower_intrinsics(rec, n_map, /* part_block_used=*/0); } -static void do_lower_highlevel(ir_graph *irg) -{ - lower_highlevel_graph(irg, firm_opt.lower_bitfields); -} +static int *irg_dump_no; -static void do_if_conv(ir_graph *irg) +static void do_stred(ir_graph *irg) { - opt_if_conv(irg, if_conv_info); + opt_osr(irg, osr_flag_default | osr_flag_keep_reg_pressure | osr_flag_ignore_x86_shift); } -static void do_stred(ir_graph *irg) +static void after_inline_opt(ir_graph *irg) { - opt_osr(irg, osr_flag_default | osr_flag_keep_reg_pressure | osr_flag_ignore_x86_shift); + do_irg_opt(irg, "scalar-replace"); + do_irg_opt(irg, "local"); + do_irg_opt(irg, "control-flow"); + do_irg_opt(irg, "combo"); } static void do_inline(void) { - inline_functions(firm_opt.inline_maxsize, firm_opt.inline_threshold); + inline_functions(firm_opt.inline_maxsize, firm_opt.inline_threshold, + after_inline_opt); } static void do_cloning(void) @@ -337,128 +305,210 @@ static void do_cloning(void) proc_cloning((float) firm_opt.clone_threshold); } -static void do_lower_switch(ir_graph *irg) +static void do_lower_mux(ir_graph *irg) +{ + lower_mux(irg, NULL); +} + +static void do_gcse(ir_graph *irg) { - lower_switch(irg, firm_opt.spare_size); + set_opt_global_cse(1); + optimize_graph_df(irg); + set_opt_global_cse(0); } typedef enum opt_target { - OPT_TARGET_IRG, - OPT_TARGET_IRP -} 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 void (*func_ptr_t)(void); typedef struct { - opt_target target; - const char *name; - func_ptr_t func; - bool enabled; - bool dump; - bool verify; - int timer; + 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; + ir_timer_t *timer; } opt_config_t; static opt_config_t opts[] = { - { OPT_TARGET_IRP, "rts", (func_ptr_t) rts_map, true, true, true, -1 }, - { OPT_TARGET_IRG, "combo", (func_ptr_t) combo, true, true, true, TV_COMBO }, - { OPT_TARGET_IRG, "controlflow", (func_ptr_t) optimize_cf, true, true, true, TV_CF_OPT }, - { OPT_TARGET_IRG, "local", (func_ptr_t) optimize_graph_df, true, true, true, TV_LOCAL_OPT }, - { OPT_TARGET_IRP, "gc_irgs", (func_ptr_t) remove_unused_functions, true, false, false, TV_CGANA }, - { OPT_TARGET_IRP, "tailrec", (func_ptr_t) opt_tail_recursion, true, true, true, TV_TAIL_REC }, - { OPT_TARGET_IRP, "funccalls", (func_ptr_t) do_optimize_funccalls, true, true, true, TV_REAL_FUNC_CALL }, - { OPT_TARGET_IRP, "lowerconst", (func_ptr_t) lower_const_code, true, false, false, -1 }, - { OPT_TARGET_IRG, "onereturn", (func_ptr_t) normalize_one_return, true, false, false, -1 }, - { OPT_TARGET_IRG, "scalar", (func_ptr_t) scalar_replacement_opt, true, true, true, TV_SCALAR_REPLACE }, - { OPT_TARGET_IRG, "reassoc", (func_ptr_t) optimize_reassociation, true, true, true, TV_REASSOCIATION }, - { OPT_TARGET_IRG, "gcse", (func_ptr_t) do_gcse, true, true, true, TV_CODE_PLACE }, - { OPT_TARGET_IRG, "place", (func_ptr_t) place_code, true, true, true, TV_CODE_PLACE }, - { OPT_TARGET_IRG, "confirm", (func_ptr_t) construct_confirms, true, true, true, TV_CONFIRM_CREATE }, - { OPT_TARGET_IRG, "ldst", (func_ptr_t) optimize_load_store, true, true, true, TV_LOAD_STORE }, - { OPT_TARGET_IRG, "sync", (func_ptr_t) opt_sync, true, true, true, -1 }, - { OPT_TARGET_IRG, "lower", (func_ptr_t) do_lower_highlevel, true, true, true, -1 }, - { OPT_TARGET_IRG, "deconv", (func_ptr_t) conv_opt, true, true, true, TV_DECONV }, - { OPT_TARGET_IRG, "condeval", (func_ptr_t) opt_cond_eval, true, true, true, TV_COND_EVAL }, - { OPT_TARGET_IRG, "remove_confirms", (func_ptr_t) remove_confirms, true, false, false, TV_CONFIRM_CREATE }, - { OPT_TARGET_IRG, "gvnpre", (func_ptr_t) do_gvn_pre, true, true, true, -1 }, - { OPT_TARGET_IRG, "ifconv", (func_ptr_t) do_if_conv, true, true, true, TV_IF_CONV }, - { OPT_TARGET_IRG, "bool", (func_ptr_t) opt_bool, true, true, true, -1 }, - { OPT_TARGET_IRG, "shape-blocks",(func_ptr_t) shape_blocks, true, true, true, TV_END_MELT }, - { OPT_TARGET_IRG, "stred", (func_ptr_t) do_stred, true, true, true, TV_OSR }, - { OPT_TARGET_IRG, "dead", (func_ptr_t) dead_node_elimination, true, false, true, TV_DEAD_NODE }, - { OPT_TARGET_IRP, "inline", (func_ptr_t) do_inline, true, true, true, -1 }, - { OPT_TARGET_IRP, "clone", (func_ptr_t) do_cloning, true, true, true, -1 }, - { OPT_TARGET_IRG, "lower_switch", (func_ptr_t) do_lower_switch, true, true, true, -1 }, +#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", local_opts, "local graph optimizations", OPT_FLAG_HIDE_OPTIONS), + IRG("lower", lower_highlevel_graph, "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), + 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]); + +#define FOR_EACH_OPT(i) for (opt_config_t *i = opts; i != endof(opts); ++i) 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) + FOR_EACH_OPT(config) { + if (streq(config->name, name)) return config; } - assert(0); return NULL; } static void set_opt_enabled(const char *name, bool enabled) { opt_config_t *config = get_opt(name); - config->enabled = enabled; + config->flags = (config->flags & ~OPT_FLAG_ENABLED) + | (enabled ? OPT_FLAG_ENABLED : 0); } -static void do_irg_opt(ir_graph *irg, const char *name) +static bool get_opt_enabled(const char *name) { - transform_irg_func func; - ir_graph *old_irg; - opt_config_t *config = get_opt(name); - assert(config->target == OPT_TARGET_IRG); - if (!config->enabled) - return; + opt_config_t *config = get_opt(name); + return (config->flags & OPT_FLAG_ENABLED) != 0; +} - if (config->timer != -1) - timer_push(config->timer); +/** + * perform an optimization on a single graph + * + * @return true if something changed, false otherwise + */ +static bool do_irg_opt(ir_graph *irg, const char *name) +{ + opt_config_t *const config = get_opt(name); + assert(config != NULL); + assert(config->target == OPT_TARGET_IRG); + if (! (config->flags & OPT_FLAG_ENABLED)) + return false; - old_irg = current_ir_graph; + ir_graph *const old_irg = current_ir_graph; current_ir_graph = irg; - func = (transform_irg_func) config->func; - func(irg); + timer_push(config->timer); + config->u.transform_irg(irg); + timer_pop(config->timer); - if (config->timer != -1) - timer_pop(); + if (firm_dump.all_phases && firm_dump.ir_graph) { + dump_ir_graph(irg, name); + } - DUMP_ONE_C(firm_dump.ir_graph && firm_dump.all_phases, irg, config->name); - CHECK_ONE(firm_opt.check_all, irg); + if (firm_opt.check_all) { + timer_push(t_verify); + irg_verify(irg, VERIFY_ENFORCE_SSA); + timer_pop(t_verify); + } current_ir_graph = old_irg; + return true; } static void do_irp_opt(const char *name) { - transform_irp_func func; - opt_config_t *config = get_opt(name); + opt_config_t *const config = get_opt(name); assert(config->target == OPT_TARGET_IRP); - if (!config->enabled) + if (! (config->flags & OPT_FLAG_ENABLED)) return; - if (config->timer != -1) - timer_push(config->timer); + timer_push(config->timer); + config->u.transform_irp(); + timer_pop(config->timer); - func = (transform_irp_func) config->func; - func(); + if (firm_dump.ir_graph && firm_dump.all_phases) { + int i; + for (i = get_irp_n_irgs() - 1; i >= 0; --i) { + ir_graph *irg = get_irp_irg(i); + dump_ir_graph(irg, name); + } + } - DUMP_ALL_C(firm_dump.ir_graph && firm_dump.all_phases, config->name); - CHECK_ALL(firm_opt.check_all); + if (firm_opt.check_all) { + int i; + timer_push(t_verify); + for (i = get_irp_n_irgs() - 1; i >= 0; --i) { + irg_verify(get_irp_irg(i), VERIFY_ENFORCE_SSA); + } + timer_pop(t_verify); + } +} - if (config->timer != -1) - timer_pop(); +/** + * Enable transformations which should be always safe (and cheap) to perform + */ +static void enable_safe_defaults(void) +{ + set_opt_enabled("remove-unused", true); + set_opt_enabled("opt-tail-rec", true); + set_opt_enabled("opt-func-call", true); + set_opt_enabled("reassociation", true); + set_opt_enabled("control-flow", true); + set_opt_enabled("local", true); + set_opt_enabled("lower-const", true); + set_opt_enabled("scalar-replace", true); + set_opt_enabled("place", true); + set_opt_enabled("gcse", true); + set_opt_enabled("confirm", true); + set_opt_enabled("opt-load-store", true); + set_opt_enabled("lower", true); + set_opt_enabled("deconv", true); + set_opt_enabled("remove-confirms", true); + set_opt_enabled("ivopts", true); + set_opt_enabled("dead", true); + set_opt_enabled("remove-phi-cycles", true); + set_opt_enabled("frame", true); + set_opt_enabled("combo", true); + set_opt_enabled("invert-loops", true); + set_opt_enabled("target-lowering", true); + set_opt_enabled("rts", true); + set_opt_enabled("parallelize-mem", true); + set_opt_enabled("opt-cc", true); } /** @@ -468,321 +518,128 @@ static void do_irp_opt(const char *name) */ static void do_firm_optimizations(const char *input_filename) { - int i; - 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); - set_opt_combo(firm_opt.combo); - - 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); - - /* parameter passing code should set them directly sometime... */ - set_opt_enabled("rts", firm_opt.freestanding); - set_opt_enabled("gc_irgs", firm_opt.remove_unused); - set_opt_enabled("tailrec", firm_opt.tail_rec); - set_opt_enabled("funccalls", firm_opt.func_calls); - set_opt_enabled("gcse", firm_opt.gcse); - set_opt_enabled("place", !firm_opt.gcse); - set_opt_enabled("confirm", firm_opt.confirm); - set_opt_enabled("remove_confirms", firm_opt.confirm); - set_opt_enabled("sync", firm_opt.sync); - set_opt_enabled("ldst", firm_opt.load_store); - set_opt_enabled("deconv", firm_opt.deconv); - set_opt_enabled("condeval", firm_opt.cond_eval); - set_opt_enabled("gvnpre", firm_opt.gvn_pre); - set_opt_enabled("ifconv", firm_opt.if_conversion); - set_opt_enabled("bool", firm_opt.bool_opt); - set_opt_enabled("shape-blocks", firm_opt.shape_blocks); - set_opt_enabled("inline", firm_opt.do_inline); - set_opt_enabled("clone", firm_opt.cloning); - set_opt_enabled("combo", firm_opt.combo); - - timer_start(TV_ALL_OPT); - - do_irp_opt("rts"); - - /* first step: kill dead code */ - if (firm_opt.combo) { - for (i = 0; i < get_irp_n_irgs(); i++) { - ir_graph *irg = get_irp_irg(i); - do_irg_opt(irg, "combo"); - } - } else { - for (i = 0; i < get_irp_n_irgs(); i++) { - ir_graph *irg = get_irp_irg(i); - do_irg_opt(irg, "local"); - do_irg_opt(irg, "controlflow"); - } - } - - do_irp_opt("gc_irgs"); - do_irp_opt("tailrec"); - do_irp_opt("funccalls"); - do_irp_opt("lowerconst"); - - for (i = 0; i < get_irp_n_irgs(); i++) { - ir_graph *irg = get_irp_irg(i); - - do_irg_opt(irg, "scalar"); - do_irg_opt(irg, "local"); - do_irg_opt(irg, "reassoc"); - do_irg_opt(irg, "local"); - do_irg_opt(irg, "gcse"); - - 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. */ - do_irg_opt(irg, "controlflow"); - do_irg_opt(irg, "confirm"); - do_irg_opt(irg, "local"); - } - - do_irg_opt(irg, "controlflow"); - do_irg_opt(irg, "ldst"); - do_irg_opt(irg, "lower"); - do_irg_opt(irg, "deconv"); - do_irg_opt(irg, "condeval"); - do_irg_opt(irg, "remove_confirms"); - do_irg_opt(irg, "gvnpre"); - do_irg_opt(irg, "place"); - do_irg_opt(irg, "controlflow"); - - if (firm_opt.if_conversion) { - do_irg_opt(irg, "ifconv"); - do_irg_opt(irg, "local"); - do_irg_opt(irg, "controlflow"); - } - - do_irg_opt(irg, "bool"); - do_irg_opt(irg, "shape-blocks"); - do_irg_opt(irg, "lower_switch"); - do_irg_opt(irg, "stred"); - do_irg_opt(irg, "local"); - do_irg_opt(irg, "dead"); - } - - do_irp_opt("inline"); - do_irp_opt("clone"); - - for (i = 0; i < get_irp_n_irgs(); i++) { - ir_graph *irg = get_irp_irg(i); - do_irg_opt(irg, "local"); - do_irg_opt(irg, "controlflow"); - do_irg_opt(irg, "condeval"); - do_irg_opt(irg, "local"); - do_irg_opt(irg, "controlflow"); - } - - 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)); - } - - do_irp_opt("gc_irgs"); - - 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 */ + size_t i; + unsigned aa_opt; -/** - * compute the size of a type (do implicit lowering) - * - * @param ty a Firm type - */ -static int compute_type_size(ir_type *ty) -{ - optimization_state_t state; - unsigned align_all = 1; - int n, size = 0, set = 0; - int i, dims, s; - - if (get_type_state(ty) == layout_fixed) { - /* do not layout already layouted types again */ - return 1; - } - - if (is_Method_type(ty) || ty == get_glob_type()) { - /* no need for size calculation for method types or the global type */ - return 1; - } - - DBG(("compute type size visiting: %s\n", get_type_name(ty))); - - switch (get_type_tpop_code(ty)) { - case tpo_class: - case tpo_struct: - for (i = 0, n = get_compound_n_members(ty); i < n; ++i) { - ir_entity *ent = get_compound_member(ty, i); - ir_type *ent_ty = get_entity_type(ent); - unsigned align, misalign; - - /* compute member types */ - if (! compute_type_size(ent_ty)) - return 0; - - align = get_type_alignment_bytes(ent_ty); - align_all = align > align_all ? align : align_all; - misalign = (align ? size % align : 0); - size += (misalign ? align - misalign : 0); - - set_entity_offset(ent, size); - size += get_type_size_bytes(ent_ty); - - DBG((" member %s %s -> (size: %u, align: %u)\n", - get_type_name(ent_ty), get_entity_name(ent), - get_type_size_bytes(ent_ty), get_type_alignment_bytes(ent_ty))); - } - if (align_all > 0 && size % align_all) { - DBG(("align of the struct member: %u, type size: %d\n", align_all, size)); - size += align_all - (size % align_all); - DBG(("correcting type-size to %d\n", size)); - } - set_type_alignment_bytes(ty, align_all); - set = 1; - break; - - case tpo_union: - for (i = 0, n = get_union_n_members(ty); i < n; ++i) { - ir_entity *ent = get_union_member(ty, i); - - if (! compute_type_size(get_entity_type(ent))) - return 0; - s = get_type_size_bytes(get_entity_type(ent)); - - set_entity_offset(ent, 0); - size = (s > size ? s : size); - } - set = 1; - break; - - case tpo_array: - dims = get_array_n_dimensions(ty); - - if (! compute_type_size(get_array_element_type(ty))) - return 0; - - size = 1; - - save_optimization_state(&state); - set_optimize(1); - set_opt_constant_folding(1); - set_opt_algebraic_simplification(1); - - for (i = 0; i < dims; ++i) { - ir_node *lower = get_array_lower_bound(ty, i); - ir_node *upper = get_array_upper_bound(ty, i); - ir_graph *rem = current_ir_graph; - tarval *tv_lower, *tv_upper; - long val_lower, val_upper; - - current_ir_graph = get_const_code_irg(); - local_optimize_node(lower); - local_optimize_node(upper); - current_ir_graph = rem; - - tv_lower = computed_value(lower); - tv_upper = computed_value(upper); - - if (tv_lower == tarval_bad || tv_upper == tarval_bad) { - /* - * we cannot calculate the size of this array yet, it - * even might be unknown until the end, like argv[] - */ - restore_optimization_state(&state); - return 0; - } - - val_upper = get_tarval_long(tv_upper); - val_lower = get_tarval_long(tv_lower); - size *= val_upper - val_lower; - } - restore_optimization_state(&state); - - DBG(("array %s -> (elements: %d, element type size: %d)\n", - get_type_name(ty), - size, get_type_size_bytes(get_array_element_type(ty)))); - size *= get_type_size_bytes(get_array_element_type(ty)); - set = 1; - break; - - default: - break; - } - - if (set) { - set_type_size_bytes(ty, size); - set_type_state(ty, layout_fixed); - } - - DBG(("size: %d\n", get_type_size_bytes(ty))); - - return set; -} /* compute_type_size */ + set_opt_alias_analysis(firm_opt.alias_analysis); -/** - * layout all non-frame types of the Firm graph - */ -static void compute_type_sizes(void) -{ - int i; - ir_type *tp; + 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; - /* all non-frame other types */ - for (i = get_irp_n_types() - 1; i >= 0; --i) { - tp = get_irp_type(i); - compute_type_size(tp); + set_irp_memory_disambiguator_options(aa_opt); - if (is_Method_type(tp)) { - tp = get_method_value_res_type(tp); + /* parameter passing code should set them directly sometime... */ + set_opt_enabled("confirm", firm_opt.confirm); + set_opt_enabled("remove-confirms", firm_opt.confirm); - if (tp) { - /* we have a value result type for this method, lower */ - compute_type_size(tp); - } - } - } -} /* compute_type_sizes */ + /* osr supersedes remove_phi_cycles */ + if (get_opt_enabled("ivopts")) + set_opt_enabled("remove-phi-cycles", false); -/** - * layout all frame-types of the Firm graph - */ -static void compute_frame_type_sizes(void) -{ - int i; - ir_graph *irg; - - /* all frame types */ - for (i = get_irp_n_irgs() - 1; i >= 0; --i) { - irg = get_irp_irg(i); - /* do not optimize away variables in debug mode */ - if (firm_opt.debug_mode == DBG_MODE_NONE) - opt_frame_irg(irg); - compute_type_size(get_irg_frame_type(irg)); - } -} /* compute_frame_type_sizes */ + timer_start(t_all_opt); + + do_irp_opt("rts"); + + /* first step: kill dead code */ + for (i = 0; i < get_irp_n_irgs(); i++) { + ir_graph *irg = get_irp_irg(i); + do_irg_opt(irg, "combo"); + do_irg_opt(irg, "local"); + do_irg_opt(irg, "control-flow"); + } + + do_irp_opt("remove-unused"); + for (i = 0; i < get_irp_n_irgs(); ++i) { + ir_graph *irg = get_irp_irg(i); + do_irg_opt(irg, "opt-tail-rec"); + } + do_irp_opt("opt-func-call"); + do_irp_opt("lower-const"); + + for (i = 0; i < get_irp_n_irgs(); i++) { + ir_graph *irg = get_irp_irg(i); + + do_irg_opt(irg, "scalar-replace"); + do_irg_opt(irg, "invert-loops"); + do_irg_opt(irg, "unroll-loops"); + do_irg_opt(irg, "local"); + do_irg_opt(irg, "reassociation"); + do_irg_opt(irg, "local"); + do_irg_opt(irg, "gcse"); + do_irg_opt(irg, "place"); + + 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. + */ + do_irg_opt(irg, "control-flow"); + do_irg_opt(irg, "confirm"); + do_irg_opt(irg, "vrp"); + do_irg_opt(irg, "local"); + } + + do_irg_opt(irg, "control-flow"); + do_irg_opt(irg, "opt-load-store"); + do_irg_opt(irg, "fp-vrp"); + do_irg_opt(irg, "lower"); + do_irg_opt(irg, "deconv"); + do_irg_opt(irg, "thread-jumps"); + do_irg_opt(irg, "remove-confirms"); + do_irg_opt(irg, "gvn-pre"); + do_irg_opt(irg, "gcse"); + do_irg_opt(irg, "place"); + do_irg_opt(irg, "control-flow"); + + if (do_irg_opt(irg, "if-conversion")) { + do_irg_opt(irg, "local"); + do_irg_opt(irg, "control-flow"); + } + /* this doesn't make too much sense but tests the mux destruction... */ + do_irg_opt(irg, "lower-mux"); + + do_irg_opt(irg, "bool"); + do_irg_opt(irg, "shape-blocks"); + do_irg_opt(irg, "ivopts"); + do_irg_opt(irg, "local"); + do_irg_opt(irg, "dead"); + } + + do_irp_opt("inline"); + do_irp_opt("opt-proc-clone"); + + for (i = 0; i < get_irp_n_irgs(); i++) { + ir_graph *irg = get_irp_irg(i); + do_irg_opt(irg, "local"); + do_irg_opt(irg, "control-flow"); + do_irg_opt(irg, "thread-jumps"); + do_irg_opt(irg, "local"); + do_irg_opt(irg, "control-flow"); + + if( do_irg_opt(irg, "vrp") ) { // if vrp is enabled + do_irg_opt(irg, "local"); + do_irg_opt(irg, "vrp"); + do_irg_opt(irg, "local"); + do_irg_opt(irg, "vrp"); + } + } + + 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)); + } + + dump_all("opt"); + + if (firm_dump.statistic & STAT_AFTER_OPT) + stat_dump_snapshot(input_filename, "opt"); + + timer_stop(t_all_opt); +} /** * do Firm lowering @@ -791,173 +648,102 @@ static void compute_frame_type_sizes(void) */ static void do_firm_lowering(const char *input_filename) { - int i; - - if (firm_opt.lower_ll) { - lwrdw_param_t init = { - 1, - 1, - get_atomic_mode(ATOMIC_TYPE_LONGLONG), - get_atomic_mode(ATOMIC_TYPE_ULONGLONG), - get_atomic_mode(ATOMIC_TYPE_INT), - get_atomic_mode(ATOMIC_TYPE_UINT), - def_create_intrinsic_fkt, - NULL - }; - - - if (arch_create_intrinsic) { - init.create_intrinsic = arch_create_intrinsic; - init.ctx = create_intrinsic_ctx; - } - timer_push(TV_DW_LOWER); - lower_dw_ops(&init); - timer_pop(); - } - - if (firm_dump.statistic & STAT_AFTER_LOWER) - stat_dump_snapshot(input_filename, "low"); - - /* verify lowered graphs */ - timer_push(TV_VERIFY); - for (i = get_irp_n_irgs() - 1; i >= 0; --i) - irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA); - timer_pop(); - - DUMP_ALL(firm_dump.ir_graph, "-low"); - - if (firm_opt.enabled) { - timer_start(TV_ALL_OPT); - - /* run reassociation first on all graphs BEFORE the architecture dependent optimizations - are enabled */ - for (i = get_irp_n_irgs() - 1; i >= 0; --i) { - ir_graph *irg = get_irp_irg(i); - do_irg_opt(irg, "reassoc"); - } + int i; - /* enable architecture dependent optimizations */ - arch_dep_set_opts((arch_dep_opts_t) - ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) | - (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) | - (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) )); + /* enable architecture dependent optimizations */ + arch_dep_set_opts((arch_dep_opts_t) + ((firm_opt.muls ? arch_dep_mul_to_shift : arch_dep_none) | + (firm_opt.divs ? arch_dep_div_by_const : arch_dep_none) | + (firm_opt.mods ? arch_dep_mod_by_const : arch_dep_none) )); + for (i = get_irp_n_irgs() - 1; i >= 0; --i) { + ir_graph *irg = get_irp_irg(i); + do_irg_opt(irg, "reassociation"); + do_irg_opt(irg, "local"); + } - for (i = get_irp_n_irgs() - 1; i >= 0; --i) { - ir_graph *irg = get_irp_irg(i); + do_irp_opt("target-lowering"); - current_ir_graph = irg; + if (firm_dump.statistic & STAT_AFTER_LOWER) + stat_dump_snapshot(input_filename, "low"); - do_irg_opt(irg, "local"); - do_irg_opt(irg, "gcse"); - do_irg_opt(irg, "ldst"); - do_irg_opt(irg, "local"); - do_irg_opt(irg, "controlflow"); + timer_start(t_all_opt); - if (firm_opt.if_conversion) { - do_irg_opt(irg, "ifconv"); - do_irg_opt(irg, "local"); - do_irg_opt(irg, "controlflow"); - } + for (i = get_irp_n_irgs() - 1; i >= 0; --i) { + ir_graph *irg = get_irp_irg(i); - do_irg_opt(current_ir_graph, "sync"); - } - timer_stop(TV_ALL_OPT); + do_irg_opt(irg, "local"); + do_irg_opt(irg, "deconv"); + do_irg_opt(irg, "control-flow"); + do_irg_opt(irg, "opt-load-store"); + do_irg_opt(irg, "gcse"); + do_irg_opt(irg, "place"); + do_irg_opt(irg, "control-flow"); - DUMP_ALL(firm_dump.ir_graph, "-low-opt"); - } + if (do_irg_opt(irg, "vrp")) { + do_irg_opt(irg, "local"); + do_irg_opt(irg, "control-flow"); + do_irg_opt(irg, "vrp"); + do_irg_opt(irg, "local"); + do_irg_opt(irg, "control-flow"); + } - if (firm_opt.cc_opt) - mark_private_methods(); + if (do_irg_opt(irg, "if-conversion")) { + do_irg_opt(irg, "local"); + do_irg_opt(irg, "control-flow"); + } - /* set the phase to low */ - for (i = get_irp_n_irgs() - 1; i >= 0; --i) - set_irg_phase_low(get_irp_irg(i)); + set_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2); + do_irg_opt(irg, "local"); - /* all graphs are lowered, set the irp phase to low */ - set_irp_phase_state(phase_low); + do_irg_opt(irg, "parallelize-mem"); + do_irg_opt(irg, "frame"); + } + do_irp_opt("remove-unused"); + do_irp_opt("opt-cc"); + timer_stop(t_all_opt); + dump_all("low-opt"); - if (firm_dump.statistic & STAT_FINAL) { - stat_dump_snapshot(input_filename, "final"); - } -} /* do_firm_lowering */ + if (firm_dump.statistic & STAT_FINAL) { + stat_dump_snapshot(input_filename, "final"); + } +} /** * Initialize for the Firm-generating back end. */ void gen_firm_init(void) { - firm_parameter_t params; - unsigned pattern = 0; - - /* the automatic state is only set if inlining is enabled */ - firm_opt.auto_inline = firm_opt.do_inline; - - if (firm_dump.stat_pattern) - pattern |= FIRMSTAT_PATTERN_ENABLED; - - if (firm_dump.stat_dag) - pattern |= FIRMSTAT_COUNT_DAG; - - memset(¶ms, 0, sizeof(params)); - params.size = sizeof(params); - params.enable_statistics = firm_dump.statistic == STAT_NONE ? 0 : - FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP | FIRMSTAT_COUNT_CONSTS | pattern; - params.initialize_local_func = uninitialized_local_var; - params.cc_mask = 0; /* no regparam, cdecl */ - params.builtin_dbg = NULL; - - init_firm(¶ms); - - if (firm_be_opt.selection == BE_FIRM_BE) { - const backend_params *be_params = be_get_backend_param(); - - firm_opt.lower_ll = (a_byte) be_params->do_dw_lowering; - - arch_create_intrinsic = be_params->arch_create_intrinsic_fkt; - create_intrinsic_ctx = be_params->create_intrinsic_ctx; - - ad_param = be_params->dep_param; - if_conv_info = be_params->if_conv_info; - } - - dbg_init(NULL, NULL, dbg_snprint); - edges_init_dbg(firm_opt.vrfy_edges); - - set_opt_precise_exc_context(firm_opt.precise_exc); - set_opt_fragile_ops(firm_opt.fragile_ops); - - /* Sel node cannot produce NULL pointers */ - set_opt_sel_based_null_check_elim(1); + ir_init(); + enable_safe_defaults(); - /* dynamic dispatch works currently only if whole world scenarios */ - set_opt_dyn_meth_dispatch(0); - - arch_dep_init(arch_factory); + FOR_EACH_OPT(i) { + i->timer = ir_timer_new(); + timer_register(i->timer, i->description); + } + t_verify = ir_timer_new(); + timer_register(t_verify, "Firm: verify pass"); + t_vcg_dump = ir_timer_new(); + timer_register(t_vcg_dump, "Firm: vcg dumping"); + t_all_opt = ir_timer_new(); + timer_register(t_all_opt, "Firm: all optimizations"); + t_backend = ir_timer_new(); + timer_register(t_backend, "Firm: backend"); +} - /* do not run architecture dependent optimizations in building phase */ - arch_dep_set_opts(arch_dep_none); +static void init_statistics(void) +{ + unsigned pattern = 0; - do_node_verification((firm_verification_t) firm_opt.vrfy); - if (firm_dump.filter) - only_dump_method_with_name(new_id_from_str(firm_dump.filter)); + if (firm_dump.stat_pattern) + pattern |= FIRMSTAT_PATTERN_ENABLED; - if (firm_opt.enabled) { - set_optimize(1); - set_opt_constant_folding(firm_opt.const_folding); - set_opt_algebraic_simplification(firm_opt.const_folding); - set_opt_cse(firm_opt.cse); - set_opt_global_cse(0); - set_opt_unreachable_code(1); - set_opt_control_flow(firm_opt.control_flow); - set_opt_control_flow_weak_simplification(1); - set_opt_control_flow_strong_simplification(1); - } else { - set_optimize(0); - } + if (firm_dump.stat_dag) + pattern |= FIRMSTAT_COUNT_DAG; - /* do not dump entity ld names */ - dump_ld_names(0); -} /* gen_firm_init */ + firm_init_stat(firm_dump.statistic == STAT_NONE ? + 0 : FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP + | FIRMSTAT_COUNT_CONSTS | pattern); +} /** * Called, after the Firm generation is completed, @@ -965,114 +751,217 @@ void gen_firm_init(void) * * @param out a file handle for the output, may be NULL * @param input_filename the name of the (main) source file - * @param c_mode non-zero if "C" was compiled - * @param new_firm_const_exists non-zero, if the const attribute was used on functions */ -void gen_firm_finish(FILE *out, const char *input_filename, int c_mode, int new_firm_const_exists) +void generate_code(FILE *out, const char *input_filename) { - int i; + int i; - firm_const_exists = new_firm_const_exists; + set_optimize(1); + set_opt_constant_folding(firm_opt.const_folding); + set_opt_algebraic_simplification(firm_opt.const_folding); + set_opt_cse(firm_opt.cse); + set_opt_global_cse(0); - /* the general for dumping option must be set, or the others will not work */ - firm_dump.ir_graph - = (a_byte) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb); + init_statistics(); - dump_keepalive_edges(1); - dump_consts_local(1); - dump_dominator_information(1); - dump_loop_information(0); + do_node_verification((firm_verification_t) firm_opt.verify); - if (!firm_dump.edge_labels) - turn_off_edge_labels(); + /* the general for dumping option must be set, or the others will not work*/ + firm_dump.ir_graph = (bool) (firm_dump.ir_graph | firm_dump.all_phases); - if (firm_dump.all_types) { - dump_all_types(""); - if (! c_mode) { - dump_class_hierarchy(0, ""); - dump_class_hierarchy(1, "-with-entities"); - } - } + ir_add_dump_flags(ir_dump_flag_keepalive_edges + | ir_dump_flag_consts_local | ir_dump_flag_dominance); + ir_remove_dump_flags(ir_dump_flag_loops | ir_dump_flag_ld_names); - /* finalize all graphs */ - for (i = get_irp_n_irgs() - 1; i >= 0; --i) { - ir_graph *irg = get_irp_irg(i); + /* FIXME: cloning might ADD new graphs. */ + irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no)); - irg_finalize_cons(irg); - DUMP_ONE(firm_dump.ir_graph, irg, ""); + if (firm_dump.all_types) { + dump_ir_prog_ext(dump_typegraph, "types.vcg"); + } - /* verify the graph */ - timer_push(TV_VERIFY); - irg_verify(irg, VRFY_ENFORCE_SSA); - timer_pop(); - } + dump_all(""); - timer_push(TV_VERIFY); - tr_vrfy(); - timer_pop(); + timer_push(t_verify); + tr_verify(); + timer_pop(t_verify); - /* all graphs are finalized, set the irp phase to high */ - set_irp_phase_state(phase_high); + /* all graphs are finalized, set the irp phase to high */ + set_irp_phase_state(phase_high); - /* BEWARE: kill unreachable code before doing compound lowering */ - for (i = get_irp_n_irgs() - 1; i >= 0; --i) { - ir_graph *irg = get_irp_irg(i); - optimize_cf(irg); - } + /* BEWARE: kill unreachable code before doing compound lowering */ + for (i = get_irp_n_irgs() - 1; i >= 0; --i) { + ir_graph *irg = get_irp_irg(i); + do_irg_opt(irg, "control-flow"); + } - /* lower all compound call return values */ - lower_compound_params(); + if (firm_dump.statistic & STAT_BEFORE_OPT) { + stat_dump_snapshot(input_filename, "noopt"); + } - /* computes the sizes of all types that are still not computed */ - compute_type_sizes(); + do_firm_optimizations(input_filename); + do_firm_lowering(input_filename); - /* lower copyb nodes */ - for (i = get_irp_n_irgs() - 1; i >= 0; --i) { - ir_graph *irg = get_irp_irg(i); - lower_CopyB(irg, 128, 4); - } + /* set the phase to low */ + for (i = get_irp_n_irgs() - 1; i >= 0; --i) + set_irg_phase_state(get_irp_irg(i), phase_low); - if (firm_dump.statistic & STAT_BEFORE_OPT) { - stat_dump_snapshot(input_filename, "noopt"); - } + if (firm_dump.statistic & STAT_FINAL_IR) + stat_dump_snapshot(input_filename, "final-ir"); - if (firm_opt.enabled) - do_firm_optimizations(input_filename); + /* run the code generator */ + timer_start(t_backend); + be_main(out, input_filename); + timer_stop(t_backend); - if (firm_dump.gen_firm_asm) { - timer_push(TV_FIRM_ASM); - gen_Firm_assembler(input_filename); - timer_pop(); - return; - } + if (firm_dump.statistic & STAT_FINAL) + stat_dump_snapshot(input_filename, "final"); +} - if (firm_opt.lower) - do_firm_lowering(input_filename); +void gen_firm_finish(void) +{ + ir_finish(); +} - /* computes the sizes of all frame types */ - compute_frame_type_sizes(); +static void disable_all_opts(void) +{ + firm_opt.cse = false; + firm_opt.confirm = false; + firm_opt.muls = false; + firm_opt.divs = false; + firm_opt.mods = false; + firm_opt.alias_analysis = false; + firm_opt.strict_alias = false; + firm_opt.no_alias = false; + + FOR_EACH_OPT(config) { + if (config->flags & OPT_FLAG_ESSENTIAL) { + config->flags |= OPT_FLAG_ENABLED; + } else { + config->flags &= ~OPT_FLAG_ENABLED; + } + } +} - /* set the phase to low */ - for (i = get_irp_n_irgs() - 1; i >= 0; --i) - set_irg_phase_low(get_irp_irg(i)); +static bool firm_opt_option(const char *opt) +{ + char const* const rest = strstart(opt, "no-"); + bool const enable = rest ? opt = rest, false : true; - if (firm_dump.statistic & STAT_FINAL_IR) - stat_dump_snapshot(input_filename, "final-ir"); + opt_config_t *config = get_opt(opt); + if (config == NULL || (config->flags & OPT_FLAG_HIDE_OPTIONS)) + return false; - /* run the code generator */ - if (firm_be_opt.selection != BE_NONE) - do_codegen(out, input_filename); + config->flags &= ~OPT_FLAG_ENABLED; + config->flags |= enable ? OPT_FLAG_ENABLED : 0; + return true; +} - if (firm_dump.statistic & STAT_FINAL) - stat_dump_snapshot(input_filename, "final"); +void firm_option_help(print_option_help_func print_option_help) +{ + FOR_EACH_OPT(config) { + char buf[1024]; + char buf2[1024]; + + if (config->flags & OPT_FLAG_HIDE_OPTIONS) + continue; + + snprintf(buf, sizeof(buf), "-f%s", config->name); + snprintf(buf2, sizeof(buf2), "enable %s", config->description); + print_option_help(buf, buf2); + snprintf(buf, sizeof(buf), "-fno-%s", config->name); + snprintf(buf2, sizeof(buf2), "disable %s", config->description); + print_option_help(buf, buf2); + } + + for (size_t k = 0; k != lengthof(firm_options); ++k) { + char buf[1024]; + char buf2[1024]; + snprintf(buf, sizeof(buf), "-f%s", firm_options[k].option); + snprintf(buf2, sizeof(buf2), "%s", firm_options[k].description); + print_option_help(buf, buf2); + } } -/** - * Do very early initializations - */ -void firm_early_init(void) +int firm_option(const char *const opt) { - /* arg: need this here for command line options */ - be_opt_register(); - firm_init_options(NULL, 0, NULL); + char const* val; + if ((val = strstart(opt, "dump-filter="))) { + ir_set_dump_filter(val); + return 1; + } else if ((val = strstart(opt, "clone-threshold="))) { + sscanf(val, "%d", &firm_opt.clone_threshold); + return 1; + } else if ((val = strstart(opt, "inline-max-size="))) { + sscanf(val, "%u", &firm_opt.inline_maxsize); + return 1; + } else if ((val = strstart(opt, "inline-threshold="))) { + sscanf(val, "%u", &firm_opt.inline_threshold); + return 1; + } else if (streq(opt, "no-opt")) { + disable_all_opts(); + return 1; + } + + size_t const len = strlen(opt); + for (size_t i = lengthof(firm_options); i != 0;) { + struct params const* const o = &firm_options[--i]; + if (len == o->opt_len && memcmp(opt, o->option, len) == 0) { + /* statistic options do accumulate */ + if (o->flag == &firm_dump.statistic) + *o->flag = (bool) (*o->flag | o->set); + else + *o->flag = o->set; + + return 1; + } + } + + /* maybe this enables/disables optimizations */ + if (firm_opt_option(opt)) + return 1; + + return 0; +} + +static void set_be_option(const char *arg) +{ + int res = be_parse_arg(arg); + (void) res; + assert(res); +} + +static void set_option(const char *arg) +{ + int res = firm_option(arg); + (void) res; + assert(res); +} + +void choose_optimization_pack(int level) +{ + /* apply optimization level */ + switch(level) { + case 0: + set_option("no-opt"); + break; + case 1: + set_option("no-inline"); + break; + default: + case 4: + /* use_builtins = true; */ + /* fallthrough */ + case 3: + set_option("thread-jumps"); + set_option("if-conversion"); + /* fallthrough */ + case 2: + set_option("strict-aliasing"); + set_option("inline"); + set_option("fp-vrp"); + set_option("deconv"); + set_be_option("omitfp"); + break; + } }