replace is_no_Block() with !is_Block()
[libfirm] / ir / opt / funccall.c
index 1b44fa4..83f479f 100644 (file)
  * @author  Michael Beck
  * @version $Id$
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
-#include <adt/raw_bitset.h>
-
-#include "funccall_t.h"
+#include "opt_init.h"
 
 #include "irnode_t.h"
 #include "irgraph_t.h"
 #include "irvrfy.h"
 #include "dbginfo_t.h"
 #include "irflag_t.h"
+#include "irloop_t.h"
 #include "ircons.h"
 #include "iredges_t.h"
+#include "irpass_t.h"
+#include "iroptimize.h"
 #include "analyze_irg_args.h"
 #include "irhooks.h"
+#include "raw_bitset.h"
 #include "debug.h"
 
 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
@@ -52,10 +52,11 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg;)
 typedef struct _env_t {
        unsigned n_calls_SymConst;
        unsigned n_calls_Sel;
-       ir_node  *const_call_list;       /**< The list of all const function calls that will be changed. */
-       ir_node  *pure_call_list;        /**< The list of all pure function calls that will be changed. */
-       ir_node  *nothrow_call_list;     /**< The list of all nothrow function calls that will be changed. */
-       ir_node  *proj_list;             /**< The list of all potential Proj nodes that must be fixed. */
+       ir_node  *float_const_call_list;    /**< The list of all floating const function calls that will be changed. */
+       ir_node  *nonfloat_const_call_list; /**< The list of all non-floating const function calls that will be changed. */
+       ir_node  *pure_call_list;           /**< The list of all pure function calls that will be changed. */
+       ir_node  *nothrow_call_list;        /**< The list of all nothrow function calls that will be changed. */
+       ir_node  *proj_list;                /**< The list of all potential Proj nodes that must be fixed. */
 } env_t;
 
 /** If non-null, evaluates entities for being a heap alloc. */
@@ -78,11 +79,12 @@ static unsigned *busy_set;
  * Walker: Collect all calls to const and pure functions
  * to lists. Collect all Proj(Call) nodes into a Proj list.
  */
-static void collect_const_and_pure_calls(ir_node *node, void *env) {
-       env_t *ctx = env;
-       ir_node *call, *ptr;
+static void collect_const_and_pure_calls(ir_node *node, void *env)
+{
+       env_t     *ctx = env;
+       ir_node   *call, *ptr;
        ir_entity *ent;
-       unsigned prop;
+       unsigned  and_prop, or_prop, prop;
 
        if (is_Call(node)) {
                call = node;
@@ -110,17 +112,21 @@ static void collect_const_and_pure_calls(ir_node *node, void *env) {
                        }
 
                        /* note that const function are a subset of pure ones */
-                       prop = mtp_property_const | mtp_property_pure;
+                       and_prop = mtp_property_const | mtp_property_pure;
+                       or_prop  = 0;
                        for (i = 0; i < n_callees; ++i) {
                                ent = get_Call_callee(call, i);
                                if (ent == unknown_entity) {
                                        /* we don't know which entity is called here */
                                        return;
                                }
-                               prop &= get_entity_additional_properties(ent);
-                               if (prop == mtp_no_property)
+                               prop      = get_entity_additional_properties(ent);
+                               and_prop &= prop;
+                               or_prop  &= prop;
+                               if (and_prop == mtp_no_property)
                                        return;
                        }
+                       prop = and_prop | (or_prop & mtp_property_has_loop);
                        ++ctx->n_calls_Sel;
                } else
                        return;
@@ -130,8 +136,13 @@ static void collect_const_and_pure_calls(ir_node *node, void *env) {
                        set_irn_link(call, ctx->pure_call_list);
                        ctx->pure_call_list = call;
                } else {
-                       set_irn_link(call, ctx->const_call_list);
-                       ctx->const_call_list = call;
+                       if (prop & mtp_property_has_loop) {
+                               set_irn_link(call, ctx->nonfloat_const_call_list);
+                               ctx->nonfloat_const_call_list = call;
+                       } else {
+                               set_irn_link(call, ctx->float_const_call_list);
+                               ctx->float_const_call_list = call;
+                       }
                }
        } else if (is_Proj(node)) {
                /*
@@ -144,10 +155,9 @@ static void collect_const_and_pure_calls(ir_node *node, void *env) {
 
                /* collect the Proj's in the Proj list */
                switch (get_Proj_proj(node)) {
-               case pn_Call_M_regular:
+               case pn_Call_M:
                case pn_Call_X_except:
                case pn_Call_X_regular:
-               case pn_Call_M_except:
                        set_irn_link(node, ctx->proj_list);
                        ctx->proj_list = node;
                        break;
@@ -160,20 +170,21 @@ static void collect_const_and_pure_calls(ir_node *node, void *env) {
 /**
  * Fix the list of collected Calls.
  *
- * @param irg        the graph that contained calls to pure functions
- * @param call_list  the list of all call sites of const functions
- * @param proj_list  the list of all memory/exception Proj's of this call sites
+ * @param irg  the graph that contained calls to pure functions
+ * @param ctx  context
  */
-static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
+static void fix_const_call_lists(ir_graph *irg, env_t *ctx)
+{
        ir_node *call, *next, *mem, *proj;
        int exc_changed = 0;
        ir_graph *rem = current_ir_graph;
 
        current_ir_graph = irg;
 
-       /* First step: fix all calls by removing it's memory input.
-          It's original memory input is preserved in their link fields. */
-       for (call = call_list; call; call = next) {
+       /* First step: fix all calls by removing their memory input and let
+        * them floating.
+        * The original memory input is preserved in their link fields. */
+       for (call = ctx->float_const_call_list; call != NULL; call = next) {
                next = get_irn_link(call);
                mem  = get_Call_mem(call);
 
@@ -181,7 +192,7 @@ static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj
                set_Call_mem(call, get_irg_no_mem(irg));
 
                /*
-                * Sorrily we cannot simply set the node to 'float'.
+                * Unfortunately we cannot simply set the node to 'float'.
                 * There is a reason for that:
                 *
                 * - The call might be inside a loop/if that is NOT entered
@@ -195,13 +206,13 @@ static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj
                 * observable states...
                 */
 
-               /* finally, this call can float
-               set_irn_pinned(call, op_pin_state_floats); */
+               /* finally, this call can float */
+               set_irn_pinned(call, op_pin_state_floats);
                hook_func_call(irg, call);
        }
 
-       /* Second step: fix all Proj's */
-       for (proj = proj_list; proj; proj = next) {
+       /* Last step: fix all Proj's */
+       for (proj = ctx->proj_list; proj != NULL; proj = next) {
                next = get_irn_link(proj);
                call = get_Proj_pred(proj);
                mem  = get_irn_link(call);
@@ -212,25 +223,24 @@ static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj
                assert(get_irn_mode(mem) == mode_M);
 
                switch (get_Proj_proj(proj)) {
-               case pn_Call_M_regular: {
+               case pn_Call_M: {
                        /* in dead code there might be cycles where proj == mem */
                        if (proj != mem)
                                exchange(proj, mem);
                         break;
                }
                case pn_Call_X_except:
-               case pn_Call_M_except:
                        exc_changed = 1;
                        exchange(proj, get_irg_bad(irg));
                        break;
                case pn_Call_X_regular: {
                        ir_node *block = get_nodes_block(call);
                        exc_changed = 1;
-                       exchange(proj, new_r_Jmp(irg, block));
+                       exchange(proj, new_r_Jmp(block));
                        break;
                }
                default:
-                       ;
+                       break;
                }
        }
 
@@ -249,7 +259,8 @@ static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj
  * Walker: Collect all calls to nothrow functions
  * to lists. Collect all Proj(Call) nodes into a Proj list.
  */
-static void collect_nothrow_calls(ir_node *node, void *env) {
+static void collect_nothrow_calls(ir_node *node, void *env)
+{
        env_t *ctx = env;
        ir_node *call, *ptr;
        ir_entity *ent;
@@ -310,10 +321,9 @@ static void collect_nothrow_calls(ir_node *node, void *env) {
 
                /* collect the Proj's in the Proj list */
                switch (get_Proj_proj(node)) {
-               case pn_Call_M_regular:
+               case pn_Call_M:
                case pn_Call_X_except:
                case pn_Call_X_regular:
-               case pn_Call_M_except:
                        set_irn_link(node, ctx->proj_list);
                        ctx->proj_list = node;
                        break;
@@ -330,7 +340,8 @@ static void collect_nothrow_calls(ir_node *node, void *env) {
  * @param call_list  the list of all call sites of const functions
  * @param proj_list  the list of all memory/exception Proj's of this call sites
  */
-static void fix_nothrow_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
+static void fix_nothrow_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list)
+{
        ir_node *call, *next, *proj;
        int exc_changed = 0;
        ir_graph *rem = current_ir_graph;
@@ -358,18 +369,17 @@ static void fix_nothrow_call_list(ir_graph *irg, ir_node *call_list, ir_node *pr
                /* kill any exception flow */
                switch (get_Proj_proj(proj)) {
                case pn_Call_X_except:
-               case pn_Call_M_except:
                        exc_changed = 1;
                        exchange(proj, get_irg_bad(irg));
                        break;
                case pn_Call_X_regular: {
                        ir_node *block = get_nodes_block(call);
                        exc_changed = 1;
-                       exchange(proj, new_r_Jmp(irg, block));
+                       exchange(proj, new_r_Jmp(block));
                        break;
                }
                default:
-                       ;
+                       break;
                }
        }
 
@@ -393,12 +403,12 @@ static void fix_nothrow_call_list(ir_graph *irg, ir_node *call_list, ir_node *pr
 
 /* forward */
 static unsigned check_const_or_pure_function(ir_graph *irg, int top);
-static unsigned check_nothrow_or_malloc(ir_graph *irg, int top);
 
 /**
  * Calculate the bigger property of two. Handle the temporary flag right.
  */
-static unsigned max_property(unsigned a, unsigned b) {
+static unsigned max_property(unsigned a, unsigned b)
+{
        unsigned r, t = (a | b) & mtp_temporary;
        a &= ~mtp_temporary;
        b &= ~mtp_temporary;
@@ -414,11 +424,11 @@ static unsigned max_property(unsigned a, unsigned b) {
  * the mtp_property.
  *
  * @return mtp_property_const if only calls of const functions are detected
- *         mtp_property_pure if only Loads and const/pure
- *         calls detected
- *         mtp_no_property else
+ *         mtp_property_pure  if only Loads and const/pure calls detected
+ *         mtp_no_property    else
  */
-static unsigned _follow_mem(ir_node *node) {
+static unsigned _follow_mem(ir_node *node)
+{
        unsigned m, mode = mtp_property_const;
        ir_node  *ptr;
        int i;
@@ -427,11 +437,9 @@ static unsigned _follow_mem(ir_node *node) {
                if (mode == mtp_no_property)
                        return mtp_no_property;
 
-               if (irn_visited(node))
+               if (irn_visited_else_mark(node))
                        return mode;
 
-               mark_irn_visited(node);
-
                switch (get_irn_opcode(node)) {
                case iro_Proj:
                        node = get_Proj_pred(node);
@@ -463,7 +471,7 @@ static unsigned _follow_mem(ir_node *node) {
                case iro_Call:
                        /* A call is only tolerable if its either constant or pure. */
                        ptr = get_Call_ptr(node);
-                       if (is_SymConst(ptr) && get_SymConst_kind(ptr) == symconst_addr_ent) {
+                       if (is_SymConst_addr_ent(ptr)) {
                                ir_entity *ent = get_SymConst_entity(ptr);
                                ir_graph  *irg = get_entity_irg(ent);
 
@@ -496,7 +504,8 @@ static unsigned _follow_mem(ir_node *node) {
  *         mtp_property_pure  if only Loads and const/pure calls detected
  *         mtp_no_property else
  */
-static unsigned follow_mem(ir_node *node, unsigned mode) {
+static unsigned follow_mem(ir_node *node, unsigned mode)
+{
        unsigned m;
 
        m = _follow_mem(node);
@@ -509,7 +518,8 @@ static unsigned follow_mem(ir_node *node, unsigned mode) {
  * @param irg  the graph to check
  * @param top  if set, this is the top call
  */
-static unsigned check_const_or_pure_function(ir_graph *irg, int top) {
+static unsigned check_const_or_pure_function(ir_graph *irg, int top)
+{
        ir_node *end, *endbl;
        int j;
        unsigned prop = get_irg_additional_properties(irg);
@@ -541,8 +551,9 @@ static unsigned check_const_or_pure_function(ir_graph *irg, int top) {
 
        current_ir_graph = irg;
 
+       ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
        inc_irg_visited(irg);
-       /* mark the initial mem: recursion of follow_mem stops here */
+       /* mark the initial mem: recursion of follow_mem() stops here */
        mark_irn_visited(get_irg_initial_mem(irg));
 
        /* visit every Return */
@@ -576,12 +587,17 @@ static unsigned check_const_or_pure_function(ir_graph *irg, int top) {
        if (prop != mtp_no_property) {
                /* check, if a keep-alive exists */
                for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
-                       ir_node *mem = get_End_keepalive(end, j);
+                       ir_node *kept = get_End_keepalive(end, j);
 
-                       if (mode_M != get_irn_mode(mem))
+                       if (is_Block(kept)) {
+                               prop = mtp_no_property;
+                               break;
+                       }
+
+                       if (mode_M != get_irn_mode(kept))
                                continue;
 
-                       prop = max_property(prop, follow_mem(mem, prop));
+                       prop = max_property(prop, follow_mem(kept, prop));
                        if (prop == mtp_no_property)
                                break;
                }
@@ -599,6 +615,7 @@ static unsigned check_const_or_pure_function(ir_graph *irg, int top) {
        if (top)
                SET_IRG_READY(irg);
        CLEAR_IRG_BUSY(irg);
+       ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
        current_ir_graph = rem;
        return prop;
 }  /* check_const_or_pure_function */
@@ -608,7 +625,8 @@ static unsigned check_const_or_pure_function(ir_graph *irg, int top) {
  *
  * @param ctx  context
  */
-static void handle_const_Calls(env_t *ctx) {
+static void handle_const_Calls(env_t *ctx)
+{
        int i;
 
        ctx->n_calls_SymConst = 0;
@@ -618,18 +636,17 @@ static void handle_const_Calls(env_t *ctx) {
        for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
                ir_graph *irg  = get_irp_irg(i);
 
-               ctx->const_call_list = NULL;
-               ctx->pure_call_list  = NULL;
-               ctx->proj_list = NULL;
-               irg_walk_graph(irg, NULL, collect_const_and_pure_calls, ctx);
+               ctx->float_const_call_list    = NULL;
+               ctx->nonfloat_const_call_list = NULL;
+               ctx->pure_call_list           = NULL;
+               ctx->proj_list                = NULL;
 
-               if (ctx->const_call_list) {
-                       fix_const_call_list(irg, ctx->const_call_list, ctx->proj_list);
+               ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
+               irg_walk_graph(irg, NULL, collect_const_and_pure_calls, ctx);
 
-                       /* this graph was changed, invalidate analysis info */
-                       set_irg_outs_inconsistent(irg);
-                       set_irg_doms_inconsistent(irg);
-               }
+               if (ctx->float_const_call_list != NULL)
+                       fix_const_call_lists(irg, ctx);
+               ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
        }
 }  /* handle_const_Calls */
 
@@ -638,7 +655,8 @@ static void handle_const_Calls(env_t *ctx) {
  *
  * @param ctx  context
  */
-static void handle_nothrow_Calls(env_t *ctx) {
+static void handle_nothrow_Calls(env_t *ctx)
+{
        int i;
 
        ctx->n_calls_SymConst = 0;
@@ -650,15 +668,13 @@ static void handle_nothrow_Calls(env_t *ctx) {
 
                ctx->nothrow_call_list = NULL;
                ctx->proj_list         = NULL;
+
+               ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
                irg_walk_graph(irg, NULL, collect_nothrow_calls, ctx);
 
-               if (ctx->nothrow_call_list) {
+               if (ctx->nothrow_call_list)
                        fix_nothrow_call_list(irg, ctx->nothrow_call_list, ctx->proj_list);
-
-                       /* this graph was changed, invalidate analysis info */
-                       set_irg_outs_inconsistent(irg);
-                       set_irg_doms_inconsistent(irg);
-               }
+               ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
        }
 }
 
@@ -668,7 +684,8 @@ static void handle_nothrow_Calls(env_t *ctx) {
  *
  * @param node  the node to check
  */
-static int is_malloc_call_result(const ir_node *node) {
+static int is_malloc_call_result(const ir_node *node)
+{
        if (is_Alloc(node) && get_Alloc_where(node) == heap_alloc) {
                /* Firm style high-level allocation */
                return 1;
@@ -687,7 +704,8 @@ static int is_malloc_call_result(const ir_node *node) {
 /**
  * Update a property depending on a call property.
  */
-static unsigned update_property(unsigned orig_prop, unsigned call_prop) {
+static unsigned update_property(unsigned orig_prop, unsigned call_prop)
+{
        unsigned t = (orig_prop | call_prop) & mtp_temporary;
        unsigned r = orig_prop & call_prop;
        return r | t;
@@ -696,7 +714,8 @@ static unsigned update_property(unsigned orig_prop, unsigned call_prop) {
 /**
  * Check if a node is stored.
  */
-static int is_stored(const ir_node *n) {
+static int is_stored(const ir_node *n)
+{
        const ir_edge_t *edge;
        const ir_node   *ptr;
 
@@ -754,7 +773,8 @@ static int is_stored(const ir_node *n) {
  *
  * return ~mtp_property_malloc if return values are stored, ~0 else
  */
-static unsigned check_stored_result(ir_graph *irg) {
+static unsigned check_stored_result(ir_graph *irg)
+{
        ir_node  *end_blk = get_irg_end_block(irg);
        int      i, j;
        unsigned res = ~0;
@@ -787,7 +807,8 @@ finish:
  * @param irg  the graph to check
  * @param top  if set, this is the top call
  */
-static unsigned check_nothrow_or_malloc(ir_graph *irg, int top) {
+static unsigned check_nothrow_or_malloc(ir_graph *irg, int top)
+{
        ir_node   *end_blk = get_irg_end_block(irg);
        ir_entity *ent;
        ir_type   *mtp;
@@ -967,6 +988,20 @@ static unsigned check_nothrow_or_malloc(ir_graph *irg, int top) {
        return curr_prop;
 }  /* check_nothrow_or_malloc */
 
+/**
+ * When a function was detected as "const", it might be moved out of loops.
+ * This might be dangerous if the graph can contain endless loops.
+ */
+static void check_for_possible_endless_loops(ir_graph *irg)
+{
+       ir_loop *root_loop;
+       assure_cf_loop(irg);
+
+       root_loop = get_irg_loop(irg);
+       if (root_loop->flags & loop_outer_loop)
+               set_irg_additional_property(irg, mtp_property_has_loop);
+}
+
 /*
  * optimize function calls by handling const functions
  */
@@ -981,7 +1016,7 @@ void optimize_funccalls(int force_run, check_alloc_entity_func callback)
        is_alloc_entity = callback;
 
        /* prepare: mark all graphs as not analyzed */
-       last_idx = get_irp_last_idx();
+       last_idx  = get_irp_last_idx();
        ready_set = rbitset_malloc(last_idx);
        busy_set  = rbitset_malloc(last_idx);
 
@@ -1025,6 +1060,7 @@ void optimize_funccalls(int force_run, check_alloc_entity_func callback)
                if (prop & mtp_property_const) {
                        ++num_const;
                        DB((dbg, LEVEL_2, "%+F has the const property\n", irg));
+                       check_for_possible_endless_loops(irg);
                } else if (prop & mtp_property_pure) {
                        ++num_pure;
                        DB((dbg, LEVEL_2, "%+F has the pure property\n", irg));
@@ -1046,7 +1082,39 @@ void optimize_funccalls(int force_run, check_alloc_entity_func callback)
 }  /* optimize_funccalls */
 
 /* initialize the funccall optimization */
-void firm_init_funccalls(void) {
+void firm_init_funccalls(void)
+{
        FIRM_DBG_REGISTER(dbg, "firm.opt.funccalls");
-//     firm_dbg_set_mask(dbg, -1);
 }  /* firm_init_funccalls */
+
+struct pass_t {
+       ir_prog_pass_t          pass;
+       int                     force_run;
+       check_alloc_entity_func callback;
+};
+
+/**
+ * Wrapper for running optimize_funccalls() as an ir_prog pass.
+ */
+static int pass_wrapper(ir_prog *irp, void *context)
+{
+       struct pass_t *pass = context;
+
+       (void)irp;
+       optimize_funccalls(pass->force_run, pass->callback);
+       return 0;
+}  /* pass_wrapper */
+
+/* Creates an ir_prog pass for optimize_funccalls. */
+ir_prog_pass_t *optimize_funccalls_pass(
+       const char *name,
+       int force_run, check_alloc_entity_func callback)
+{
+       struct pass_t *pass = XMALLOCZ(struct pass_t);
+
+       pass->force_run = force_run;
+       pass->callback  = callback;
+
+       return def_prog_pass_constructor(
+               &pass->pass, name ? name : "funccall", pass_wrapper);
+}  /* optimize_funccalls_pass */