Route all computed goto statements of a function through one IJmp.
[cparser] / driver / firm_opt.c
index 13ec9a0..f535c1c 100644 (file)
@@ -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 <config.h>
 
 #include <stdbool.h>
@@ -38,7 +35,6 @@ struct a_firm_opt {
        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 */
 };
 
 /** statistic options */
@@ -55,8 +51,6 @@ typedef enum a_firmstat_selection_tag {
 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 */
@@ -85,15 +79,12 @@ static struct a_firm_opt firm_opt = {
        .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,
@@ -139,16 +130,10 @@ static const struct params {
   { 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=<string>"),   NULL,                       0, "set dumper filter" },
 
@@ -167,6 +152,7 @@ static const struct params {
 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);
 
 /** dump all the graphs depending on cond */
@@ -182,7 +168,7 @@ static void dump_all(const char *suffix)
 }
 
 /* entities of runtime functions */
-ir_entity_ptr rts_entities[rts_max];
+ir_entity *rts_entities[rts_max];
 
 /**
  * Map runtime functions.
@@ -190,7 +176,7 @@ ir_entity_ptr rts_entities[rts_max];
 static void rts_map(void)
 {
        static const struct {
-               ir_entity_ptr *ent; /**< address of the rts entity */
+               ir_entity   **ent; /**< address of the rts entity */
                i_mapper_func func; /**< mapper function. */
        } mapper[] = {
                /* integer */
@@ -293,6 +279,39 @@ static void rts_map(void)
 
 static int *irg_dump_no;
 
+typedef enum opt_target {
+       OPT_TARGET_IRG, /**< optimization function works on a single graph */
+       OPT_TARGET_IRP  /**< optimization function works on the complete program */
+} opt_target_t;
+
+typedef enum opt_flags {
+       OPT_FLAG_NONE         = 0,
+       OPT_FLAG_ENABLED      = 1 << 0, /**< enable the optimization */
+       OPT_FLAG_NO_DUMP      = 1 << 1, /**< don't dump after transformation */
+       OPT_FLAG_NO_VERIFY    = 1 << 2, /**< don't verify after transformation */
+       OPT_FLAG_HIDE_OPTIONS = 1 << 3, /**< do not automatically process
+                                            -foptions for this transformation */
+       OPT_FLAG_ESSENTIAL    = 1 << 4, /**< output won't work without this pass
+                                            so we need it even with -O0 */
+} opt_flags_t;
+
+typedef void (*transform_irg_func)(ir_graph *irg);
+typedef void (*transform_irp_func)(void);
+
+typedef struct {
+       opt_target_t  target;
+       const char   *name;
+       union {
+               transform_irg_func transform_irg;
+               transform_irp_func transform_irp;
+       } u;
+       const char   *description;
+       opt_flags_t   flags;
+       ir_timer_t   *timer;
+} opt_config_t;
+
+static opt_config_t *get_opt(const char *name);
+
 static void do_stred(ir_graph *irg)
 {
        opt_osr(irg, osr_flag_default | osr_flag_keep_reg_pressure | osr_flag_ignore_x86_shift);
@@ -300,10 +319,15 @@ static void do_stred(ir_graph *irg)
 
 static void after_inline_opt(ir_graph *irg)
 {
+       opt_config_t *const config = get_opt("inline");
+       timer_stop(config->timer);
+
        do_irg_opt(irg, "scalar-replace");
        do_irg_opt(irg, "local");
        do_irg_opt(irg, "control-flow");
        do_irg_opt(irg, "combo");
+
+       timer_start(config->timer);
 }
 
 static void do_inline(void)
@@ -329,37 +353,6 @@ static void do_gcse(ir_graph *irg)
        set_opt_global_cse(0);
 }
 
-typedef enum opt_target {
-       OPT_TARGET_IRG, /**< optimization function works on a single graph */
-       OPT_TARGET_IRP  /**< optimization function works on the complete program */
-} opt_target_t;
-
-typedef enum opt_flags {
-       OPT_FLAG_NONE         = 0,
-       OPT_FLAG_ENABLED      = 1 << 0, /**< enable the optimization */
-       OPT_FLAG_NO_DUMP      = 1 << 1, /**< don't dump after transformation */
-       OPT_FLAG_NO_VERIFY    = 1 << 2, /**< don't verify after transformation */
-       OPT_FLAG_HIDE_OPTIONS = 1 << 3, /**< do not automatically process
-                                            -foptions for this transformation */
-       OPT_FLAG_ESSENTIAL    = 1 << 4, /**< output won't work without this pass
-                                            so we need it even with -O0 */
-} opt_flags_t;
-
-typedef void (*transform_irg_func)(ir_graph *irg);
-typedef void (*transform_irp_func)(void);
-
-typedef struct {
-       opt_target_t  target;
-       const char   *name;
-       union {
-               transform_irg_func transform_irg;
-               transform_irp_func transform_irp;
-       } u;
-       const char   *description;
-       opt_flags_t   flags;
-       ir_timer_t   *timer;
-} opt_config_t;
-
 static opt_config_t opts[] = {
 #define IRG(a, b, c, d) { OPT_TARGET_IRG, a, .u.transform_irg = (transform_irg_func)b, c, d }
 #define IRP(a, b, c, d) { OPT_TARGET_IRP, a, .u.transform_irp = b,                     c, d }
@@ -375,7 +368,7 @@ static opt_config_t opts[] = {
        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("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),
@@ -393,6 +386,8 @@ static opt_config_t opts[] = {
        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("local-const",       local_opts_const_code,    "local optimisation of constant initializers",
+                                   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),
@@ -444,15 +439,15 @@ static bool do_irg_opt(ir_graph *irg, const char *name)
        ir_graph *const old_irg = current_ir_graph;
        current_ir_graph = irg;
 
-       timer_push(config->timer);
+       timer_start(config->timer);
        config->u.transform_irg(irg);
-       timer_pop(config->timer);
+       timer_stop(config->timer);
 
        if (firm_dump.all_phases && firm_dump.ir_graph) {
                dump_ir_graph(irg, name);
        }
 
-       if (firm_opt.check_all) {
+       if (firm_opt.verify) {
                timer_push(t_verify);
                irg_verify(irg, VERIFY_ENFORCE_SSA);
                timer_pop(t_verify);
@@ -469,9 +464,9 @@ static void do_irp_opt(const char *name)
        if (! (config->flags & OPT_FLAG_ENABLED))
                return;
 
-       timer_push(config->timer);
+       timer_start(config->timer);
        config->u.transform_irp();
-       timer_pop(config->timer);
+       timer_stop(config->timer);
 
        if (firm_dump.ir_graph && firm_dump.all_phases) {
                int i;
@@ -481,7 +476,7 @@ static void do_irp_opt(const char *name)
                }
        }
 
-       if (firm_opt.check_all) {
+       if (firm_opt.verify) {
                int i;
                timer_push(t_verify);
                for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
@@ -503,6 +498,7 @@ static void enable_safe_defaults(void)
        set_opt_enabled("control-flow", true);
        set_opt_enabled("local", true);
        set_opt_enabled("lower-const", true);
+       set_opt_enabled("local-const", true);
        set_opt_enabled("scalar-replace", true);
        set_opt_enabled("place", true);
        set_opt_enabled("gcse", true);
@@ -551,8 +547,6 @@ static void do_firm_optimizations(const char *input_filename)
        if (get_opt_enabled("ivopts"))
                set_opt_enabled("remove-phi-cycles", false);
 
-       timer_start(t_all_opt);
-
        do_irp_opt("rts");
 
        /* first step: kill dead code */
@@ -649,8 +643,6 @@ static void do_firm_optimizations(const char *input_filename)
 
        if (firm_dump.statistic & STAT_AFTER_OPT)
                stat_dump_snapshot(input_filename, "opt");
-
-       timer_stop(t_all_opt);
 }
 
 /**
@@ -678,8 +670,6 @@ static void do_firm_lowering(const char *input_filename)
        if (firm_dump.statistic & STAT_AFTER_LOWER)
                stat_dump_snapshot(input_filename, "low");
 
-       timer_start(t_all_opt);
-
        for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
                ir_graph *irg = get_irp_irg(i);
 
@@ -704,15 +694,15 @@ static void do_firm_lowering(const char *input_filename)
                        do_irg_opt(irg, "control-flow");
                }
 
-               set_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2);
+               add_irg_constraints(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2);
                do_irg_opt(irg, "local");
 
                do_irg_opt(irg, "parallelize-mem");
                do_irg_opt(irg, "frame");
        }
+       do_irp_opt("local-const");
        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) {
@@ -725,7 +715,8 @@ static void do_firm_lowering(const char *input_filename)
  */
 void gen_firm_init(void)
 {
-       unsigned pattern = 0;
+       ir_init();
+       enable_safe_defaults();
 
        FOR_EACH_OPT(i) {
                i->timer = ir_timer_new();
@@ -737,6 +728,13 @@ void gen_firm_init(void)
        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");
+}
+
+static void init_statistics(void)
+{
+       unsigned pattern = 0;
 
        if (firm_dump.stat_pattern)
                pattern |= FIRMSTAT_PATTERN_ENABLED;
@@ -744,34 +742,9 @@ void gen_firm_init(void)
        if (firm_dump.stat_dag)
                pattern |= FIRMSTAT_COUNT_DAG;
 
-       ir_init(NULL);
        firm_init_stat(firm_dump.statistic == STAT_NONE ?
                        0 : FIRMSTAT_ENABLED | FIRMSTAT_COUNT_STRONG_OP
                        | FIRMSTAT_COUNT_CONSTS | pattern);
-
-       edges_init_dbg(firm_opt.verify_edges);
-
-       /* Sel node cannot produce NULL pointers */
-       set_opt_sel_based_null_check_elim(1);
-
-       /* dynamic dispatch works currently only if whole world scenarios */
-       set_opt_dyn_meth_dispatch(0);
-
-       /* do not run architecture dependent optimizations in building phase */
-       arch_dep_set_opts(arch_dep_none);
-
-       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);
-
-       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);
 }
 
 /**
@@ -780,15 +753,20 @@ 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
  */
-void gen_firm_finish(FILE *out, const char *input_filename)
+void generate_code(FILE *out, const char *input_filename)
 {
        int i;
 
+       /* initialize implicit opts, just to be sure because really the frontend
+        * should have called it already before starting graph construction */
+       init_implicit_optimizations();
+       init_statistics();
+
+       do_node_verification((firm_verification_t) firm_opt.verify);
+
        /* 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 | firm_dump.extbb);
+       firm_dump.ir_graph = (bool) (firm_dump.ir_graph | firm_dump.all_phases);
 
        ir_add_dump_flags(ir_dump_flag_keepalive_edges
                        | ir_dump_flag_consts_local | ir_dump_flag_dominance);
@@ -797,23 +775,21 @@ void gen_firm_finish(FILE *out, const char *input_filename)
        /* FIXME: cloning might ADD new graphs. */
        irg_dump_no = calloc(get_irp_last_idx(), sizeof(*irg_dump_no));
 
+       ir_timer_init_parent(t_verify);
+       ir_timer_init_parent(t_vcg_dump);
+       timer_start(t_all_opt);
+
        if (firm_dump.all_types) {
                dump_ir_prog_ext(dump_typegraph, "types.vcg");
        }
 
-       /* finalize all graphs */
-       for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
-               ir_graph *irg = get_irp_irg(i);
-               irg_finalize_cons(irg);
-       }
        dump_all("");
 
-       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);
+       if (firm_opt.verify) {
+               timer_push(t_verify);
+               tr_verify();
+               timer_pop(t_verify);
+       }
 
        /* BEWARE: kill unreachable code before doing compound lowering */
        for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
@@ -828,24 +804,25 @@ void gen_firm_finish(FILE *out, const char *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_state(get_irp_irg(i), phase_low);
+       timer_stop(t_all_opt);
 
        if (firm_dump.statistic & STAT_FINAL_IR)
                stat_dump_snapshot(input_filename, "final-ir");
 
        /* run the code generator */
-       ir_timer_t *timer = ir_timer_new();
-       timer_register(timer, "Firm: backend");
-       timer_start(timer);
+       timer_start(t_backend);
        be_main(out, input_filename);
-       timer_stop(timer);
+       timer_stop(t_backend);
 
        if (firm_dump.statistic & STAT_FINAL)
                stat_dump_snapshot(input_filename, "final");
 }
 
+void gen_firm_finish(void)
+{
+       ir_finish();
+}
+
 static void disable_all_opts(void)
 {
        firm_opt.cse             = false;
@@ -856,6 +833,7 @@ static void disable_all_opts(void)
        firm_opt.alias_analysis  = false;
        firm_opt.strict_alias    = false;
        firm_opt.no_alias        = false;
+       firm_opt.const_folding   = false;
 
        FOR_EACH_OPT(config) {
                if (config->flags & OPT_FLAG_ESSENTIAL) {
@@ -882,8 +860,6 @@ static bool firm_opt_option(const char *opt)
 
 void firm_option_help(print_option_help_func print_option_help)
 {
-       print_option_help(firm_options[0].option, firm_options[0].description);
-
        FOR_EACH_OPT(config) {
                char buf[1024];
                char buf2[1024];
@@ -991,13 +967,11 @@ void choose_optimization_pack(int level)
        }
 }
 
-/**
- * Do very early initializations
- */
-void firm_early_init(void)
+void init_implicit_optimizations(void)
 {
-       /* arg: need this here for command line options */
-       be_opt_register();
-
-       enable_safe_defaults();
+       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);
 }