X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=driver%2Ffirm_opt.c;h=5aff18b49084053b36a0970bdafc8a67d326d247;hb=866b5720ca01fb187c95c3b98b8516a22cc8eedf;hp=75256290e674a7e90e099672c2073cf5ae101c68;hpb=616cf26a1a439f8502538f30c99ca4fa8acbf53b;p=cparser diff --git a/driver/firm_opt.c b/driver/firm_opt.c index 7525629..5aff18b 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-2010 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 @@ -17,16 +14,152 @@ #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. */ + bool verify_edges; /**< verify edges */ +}; -#if defined(_DEBUG) || defined(FIRM_DEBUG) -#define DBG(x) dbg_printf x -#else -#define DBG(x) ((void)0) -#endif /* _DEBUG || FIRM_DEBUG */ +/** 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 no_blocks; /**< dump non-blocked graph */ + bool extbb; /**< dumps extended basic blocks */ + 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 */ +}; + +struct a_firm_be_opt { + bool selection; + bool node_stat; +}; + +/* 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, + .verify_edges = false, +}; + +/* dumping options */ +static struct a_firm_dump firm_dump = { + .debug_print = false, + .all_types = false, + .no_blocks = false, + .extbb = false, + .ir_graph = false, + .all_phases = false, + .statistic = STAT_NONE, + .stat_pattern = 0, + .stat_dag = 0, +}; + +#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" }, + { X("verify-edges-on"), &firm_opt.verify_edges, 1, "enable out edge verification" }, + { X("verify-edges-off"), &firm_opt.verify_edges, 0, "disable out edge verification" }, + + /* 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-no-blocks"), &firm_dump.no_blocks, 1, "dump non-blocked graph" }, + { X("dump-extbb"), &firm_dump.extbb, 1, "dump extended basic blocks" }, + { 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" }, +}; + +#undef X static ir_timer_t *t_vcg_dump; static ir_timer_t *t_verify; @@ -37,46 +170,17 @@ static bool do_irg_opt(ir_graph *irg, const char *name); static void dump_all(const char *suffix) { - if (firm_dump.ir_graph) { - timer_push(t_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(t_vcg_dump); - } -} + if (!firm_dump.ir_graph) + return; -/* 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; + timer_push(t_vcg_dump); + dump_all_ir_graphs(suffix); + timer_pop(t_vcg_dump); +} /* entities of runtime functions */ ir_entity_ptr rts_entities[rts_max]; -/** - * 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; -} - /** * Map runtime functions. */ @@ -166,10 +270,10 @@ static void rts_map(void) { &rts_entities[rts_memset], i_mapper_memset }, { &rts_entities[rts_memcmp], i_mapper_memcmp } }; - i_record rec[sizeof(mapper)/sizeof(mapper[0])]; - unsigned i, n_map; + i_record rec[lengthof(mapper)]; + size_t n_map = 0; - for (i = n_map = 0; i < sizeof(mapper)/sizeof(mapper[0]); ++i) { + 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; @@ -186,47 +290,6 @@ static void rts_map(void) static int *irg_dump_no; -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); - - timer_push(t_vcg_dump); - if (firm_dump.no_blocks) - dump_ir_graph(irg, name); - else if (firm_dump.extbb) - dump_ir_extblock_graph(irg, name); - else - dump_ir_block_graph(irg, name); - timer_pop(t_vcg_dump); -} - -static int firm_const_exists; - -static void do_optimize_funccalls(void) -{ - optimize_funccalls(firm_const_exists, NULL); -} - -static void do_gcse(ir_graph *irg) -{ - set_opt_global_cse(1); - optimize_graph_df(irg); - place_code(irg); - set_opt_global_cse(0); -} - -static void do_lower_highlevel(ir_graph *irg) -{ - lower_highlevel_graph(irg, firm_opt.lower_bitfields); -} - -static void do_if_conv(ir_graph *irg) -{ - opt_if_conv(irg, if_conv_info); -} - static void do_stred(ir_graph *irg) { opt_osr(irg, osr_flag_default | osr_flag_keep_reg_pressure | osr_flag_ignore_x86_shift); @@ -251,39 +314,16 @@ static void do_cloning(void) proc_cloning((float) firm_opt.clone_threshold); } -static void do_lower_switch(ir_graph *irg) -{ - lower_switch(irg, firm_opt.spare_size); -} - static void do_lower_mux(ir_graph *irg) { lower_mux(irg, NULL); } -static void do_lower_dw_ops(void) -{ - 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; - } - lower_dw_ops(&init); -} - -static void do_vrp(ir_graph *irg) +static void do_gcse(ir_graph *irg) { - set_vrp_data(irg); + set_opt_global_cse(1); + optimize_graph_df(irg); + set_opt_global_cse(0); } typedef enum opt_target { @@ -314,6 +354,7 @@ typedef struct { } u; const char *description; opt_flags_t flags; + ir_timer_t *timer; } opt_config_t; static opt_config_t opts[] = { @@ -321,25 +362,23 @@ static opt_config_t opts[] = { #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 optimisation", OPT_FLAG_HIDE_OPTIONS), + 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("gcse", do_gcse, "global common subexpression elimination", OPT_FLAG_NONE), IRG("gvn-pre", do_gvn_pre, "global value numbering partial redundancy elimination", OPT_FLAG_NONE), - IRG("if-conversion", do_if_conv, "if-conversion", 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("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("lower-switch", do_lower_switch, "switch lowering", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL), - IRG("one-return", normalize_one_return, "normalisation to 1 return", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_NO_DUMP | OPT_FLAG_NO_VERIFY), 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), @@ -348,26 +387,25 @@ static opt_config_t opts[] = { 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", do_vrp, "value range propagation", 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("lower-dw", do_lower_dw_ops, "lowering of doubleword operations", OPT_FLAG_HIDE_OPTIONS | OPT_FLAG_ESSENTIAL), - IRP("opt-func-call", do_optimize_funccalls, "function call optimization", OPT_FLAG_NONE), + 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_HIDE_OPTIONS), + 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]); -ir_timer_t *timers[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; } @@ -388,34 +426,32 @@ static bool get_opt_enabled(const char *name) } /** - * perform an optimisation on a single graph + * 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) { - ir_graph *old_irg; - opt_config_t *config = get_opt(name); - size_t n = config - opts; + 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; - timer_push(timers[n]); + timer_push(config->timer); config->u.transform_irg(irg); - timer_pop(timers[n]); + timer_pop(config->timer); if (firm_dump.all_phases && firm_dump.ir_graph) { - dump_graph_count(irg, name); + dump_ir_graph(irg, name); } if (firm_opt.check_all) { timer_push(t_verify); - irg_verify(irg, VRFY_ENFORCE_SSA); + irg_verify(irg, VERIFY_ENFORCE_SSA); timer_pop(t_verify); } @@ -425,21 +461,20 @@ static bool do_irg_opt(ir_graph *irg, const char *name) static void do_irp_opt(const char *name) { - opt_config_t *config = get_opt(name); - size_t n = config - opts; + opt_config_t *const config = get_opt(name); assert(config->target == OPT_TARGET_IRP); if (! (config->flags & OPT_FLAG_ENABLED)) return; - timer_push(timers[n]); + timer_push(config->timer); config->u.transform_irp(); - timer_pop(timers[n]); + timer_pop(config->timer); 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_graph_count(irg, name); + dump_ir_graph(irg, name); } } @@ -447,7 +482,7 @@ static void do_irp_opt(const char *name) int i; timer_push(t_verify); for (i = get_irp_n_irgs() - 1; i >= 0; --i) { - irg_verify(get_irp_irg(i), VRFY_ENFORCE_SSA); + irg_verify(get_irp_irg(i), VERIFY_ENFORCE_SSA); } timer_pop(t_verify); } @@ -467,6 +502,7 @@ static void enable_safe_defaults(void) 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); @@ -474,11 +510,14 @@ static void enable_safe_defaults(void) set_opt_enabled("remove-confirms", true); set_opt_enabled("ivopts", true); set_opt_enabled("dead", true); - set_opt_enabled("lower-switch", 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); } /** @@ -488,7 +527,7 @@ static void enable_safe_defaults(void) */ static void do_firm_optimizations(const char *input_filename) { - int i; + size_t i; unsigned aa_opt; set_opt_alias_analysis(firm_opt.alias_analysis); @@ -502,9 +541,6 @@ static void do_firm_optimizations(const char *input_filename) 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("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); @@ -542,6 +578,7 @@ static void do_firm_optimizations(const char *input_filename) 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 @@ -562,6 +599,7 @@ static void do_firm_optimizations(const char *input_filename) 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"); @@ -574,11 +612,9 @@ static void do_firm_optimizations(const char *input_filename) do_irg_opt(irg, "bool"); do_irg_opt(irg, "shape-blocks"); - do_irg_opt(irg, "lower-switch"); do_irg_opt(irg, "ivopts"); do_irg_opt(irg, "local"); do_irg_opt(irg, "dead"); - do_irg_opt(irg, "frame"); } do_irp_opt("inline"); @@ -606,7 +642,7 @@ static void do_firm_optimizations(const char *input_filename) construct_cf_backedges(get_irp_irg(i)); } - dump_all("-opt"); + dump_all("opt"); if (firm_dump.statistic & STAT_AFTER_OPT) stat_dump_snapshot(input_filename, "opt"); @@ -623,69 +659,58 @@ static void do_firm_lowering(const char *input_filename) { int i; - do_irp_opt("lower-dw"); + /* 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"); + } + + do_irp_opt("target-lowering"); if (firm_dump.statistic & STAT_AFTER_LOWER) stat_dump_snapshot(input_filename, "low"); - dump_all("-low"); - - if (firm_opt.enabled) { - timer_start(t_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, "reassociation"); - } + timer_start(t_all_opt); - /* 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); - for (i = get_irp_n_irgs() - 1; i >= 0; --i) { - ir_graph *irg = get_irp_irg(i); + 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"); + if (do_irg_opt(irg, "vrp")) { do_irg_opt(irg, "local"); - do_irg_opt(irg, "gcse"); - do_irg_opt(irg, "opt-load-store"); + do_irg_opt(irg, "control-flow"); + do_irg_opt(irg, "vrp"); do_irg_opt(irg, "local"); do_irg_opt(irg, "control-flow"); + } - 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 (do_irg_opt(irg, "if-conversion")) { - do_irg_opt(irg, "local"); - do_irg_opt(irg, "control-flow"); - } - - do_irg_opt(irg, "parallelize-mem"); + if (do_irg_opt(irg, "if-conversion")) { + do_irg_opt(irg, "local"); + do_irg_opt(irg, "control-flow"); } - timer_stop(t_all_opt); - do_irp_opt("remove-unused"); + set_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2); + do_irg_opt(irg, "local"); - dump_all("-low-opt"); + do_irg_opt(irg, "parallelize-mem"); + do_irg_opt(irg, "frame"); } - - if (firm_opt.cc_opt) - mark_private_methods(); - - /* set the phase to low */ - for (i = get_irp_n_irgs() - 1; i >= 0; --i) - set_irg_phase_low(get_irp_irg(i)); - - /* all graphs are lowered, set the irp phase to low */ - set_irp_phase_state(phase_low); + 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"); @@ -697,13 +722,11 @@ static void do_firm_lowering(const char *input_filename) */ void gen_firm_init(void) { - firm_parameter_t params; - unsigned pattern = 0; - int i; + unsigned pattern = 0; - for (i = 0; i < n_opts; ++i) { - timers[i] = ir_timer_new(); - timer_register(timers[i], opts[i].description); + 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"); @@ -718,30 +741,12 @@ void gen_firm_init(void) 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 */ + ir_init(); + firm_init_stat(firm_dump.statistic == STAT_NONE ? + 0 : FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP + | FIRMSTAT_COUNT_CONSTS | pattern); - ir_init(¶ms); - - if (firm_be_opt.selection == BE_FIRM_BE) { - const backend_params *be_params = be_get_backend_param(); - - if (be_params->do_dw_lowering) - set_opt_enabled("lower-dw", true); - - 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; - } - - edges_init_dbg(firm_opt.vrfy_edges); + edges_init_dbg(firm_opt.verify_edges); /* Sel node cannot produce NULL pointers */ set_opt_sel_based_null_check_elim(1); @@ -749,31 +754,20 @@ void gen_firm_init(void) /* dynamic dispatch works currently only if whole world scenarios */ set_opt_dyn_meth_dispatch(0); - arch_dep_init(arch_factory); - /* do not run architecture dependent optimizations in building phase */ arch_dep_set_opts(arch_dep_none); - 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_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); - } + do_node_verification((firm_verification_t) firm_opt.verify); + if (firm_dump.extbb) + ir_add_dump_flags(ir_dump_flag_group_extbb); + if (firm_dump.no_blocks) + ir_remove_dump_flags(ir_dump_flag_blocks_as_subgraphs); - /* do not dump entity ld names */ - dump_ld_names(0); + 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); } /** @@ -783,45 +777,24 @@ 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 gen_firm_finish(FILE *out, const char *input_filename) { int i; -#if 0 - if (firm_opt.enable_statev) { - char buf[1024]; - snprintf(buf, sizeof(buf), "%s.ev", input_filename); - ir_stat_ev_begin(input_filename, firm_opt.statev_filter); - ir_stat_ev_compilation_unit(input_filename); - } -#endif - - firm_const_exists = new_firm_const_exists; - /* 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); - - dump_keepalive_edges(1); - dump_consts_local(1); - dump_dominator_information(1); - dump_loop_information(0); + = (bool) (firm_dump.ir_graph | firm_dump.all_phases | firm_dump.extbb); - if (!firm_dump.edge_labels) - turn_off_edge_labels(); + 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); /* FIXME: cloning might ADD new graphs. */ irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no)); if (firm_dump.all_types) { - dump_all_types(""); - if (! c_mode) { - dump_class_hierarchy(0, ""); - dump_class_hierarchy(1, "-with-entities"); - } + dump_ir_prog_ext(dump_typegraph, "types.vcg"); } /* finalize all graphs */ @@ -832,7 +805,7 @@ void gen_firm_finish(FILE *out, const char *input_filename, int c_mode, dump_all(""); timer_push(t_verify); - tr_vrfy(); + tr_verify(); timer_pop(t_verify); /* all graphs are finalized, set the irp phase to high */ @@ -844,44 +817,43 @@ void gen_firm_finish(FILE *out, const char *input_filename, int c_mode, do_irg_opt(irg, "control-flow"); } - /* lower all compound call return values */ - lower_compound_params(); - - /* 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); - } - if (firm_dump.statistic & STAT_BEFORE_OPT) { stat_dump_snapshot(input_filename, "noopt"); } - if (firm_opt.enabled) - do_firm_optimizations(input_filename); - - if (firm_opt.lower) - do_firm_lowering(input_filename); + do_firm_optimizations(input_filename); + do_firm_lowering(input_filename); /* 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_phase_state(get_irp_irg(i), phase_low); if (firm_dump.statistic & STAT_FINAL_IR) stat_dump_snapshot(input_filename, "final-ir"); /* run the code generator */ - if (firm_be_opt.selection != BE_NONE) - do_codegen(out, input_filename); + ir_timer_t *timer = ir_timer_new(); + timer_register(timer, "Firm: backend"); + timer_start(timer); + be_main(out, input_filename); + timer_stop(timer); if (firm_dump.statistic & STAT_FINAL) stat_dump_snapshot(input_filename, "final"); } -void disable_all_opts(void) +static void disable_all_opts(void) { - for (int i = 0; i < n_opts; ++i) { - opt_config_t *config = &opts[i]; + 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 { @@ -890,40 +862,128 @@ void disable_all_opts(void) } } -int firm_opt_option(const char *opt) +static bool firm_opt_option(const char *opt) { - bool enable = true; - if (strncmp(opt, "no-", 3) == 0) { - enable = false; - opt = opt + 3; - } + char const* const rest = strstart(opt, "no-"); + bool const enable = rest ? opt = rest, false : true; opt_config_t *config = get_opt(opt); if (config == NULL || (config->flags & OPT_FLAG_HIDE_OPTIONS)) - return 0; + return false; config->flags &= ~OPT_FLAG_ENABLED; config->flags |= enable ? OPT_FLAG_ENABLED : 0; - return 1; + return true; } -void firm_opt_option_help(void) +void firm_option_help(print_option_help_func print_option_help) { - int i; + print_option_help(firm_options[0].option, firm_options[0].description); - for (i = 0; i < n_opts; ++i) { + FOR_EACH_OPT(config) { char buf[1024]; char buf2[1024]; - const opt_config_t *config = &opts[i]; if (config->flags & OPT_FLAG_HIDE_OPTIONS) continue; - snprintf(buf2, sizeof(buf2), "firm: enable %s", config->description); - print_option_help(config->name, buf2); - snprintf(buf, sizeof(buf), "no-%s", config->name); - snprintf(buf2, sizeof(buf2), "firm: disable %s", config->description); + 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); + } +} + +int firm_option(const char *const opt) +{ + 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; } }