add note/error message about critical edge splitting and IJmps
[libfirm] / ir / opt / opt_inline.c
index bf36024..f1843c7 100644 (file)
@@ -164,15 +164,10 @@ static void copy_node(ir_node *n, void *env) {
        }
        copy_node_attr(n, nn);
 
-#ifdef DEBUG_libfirm
-       {
-               int copy_node_nr = env != NULL;
-               if (copy_node_nr) {
-                       /* for easier debugging, we want to copy the node numbers too */
-                       nn->node_nr = n->node_nr;
-               }
+       if (env != NULL) {
+               /* for easier debugging, we want to copy the node numbers too */
+               nn->node_nr = n->node_nr;
        }
-#endif
 
        set_new_node(n, nn);
        hook_dead_node_elim_subst(current_ir_graph, n, nn);
@@ -221,9 +216,10 @@ static void copy_preds(ir_node *n, void *env) {
                   in array contained Bads.  Now it's possible.
                   We don't call optimize_in_place as it requires
                   that the fields in ir_graph are set properly. */
-               if ((get_opt_control_flow_straightening()) &&
-                       (get_Block_n_cfgpreds(nn) == 1) &&
-                       is_Jmp(get_Block_cfgpred(nn, 0))) {
+               if (!has_Block_entity(nn) &&
+                   get_opt_control_flow_straightening() &&
+                   get_Block_n_cfgpreds(nn) == 1 &&
+                   is_Jmp(get_Block_cfgpred(nn, 0))) {
                        ir_node *old = get_nodes_block(get_Block_cfgpred(nn, 0));
                        if (nn == old) {
                                /* Jmp jumps into the block it is in -- deal self cycle. */
@@ -767,10 +763,16 @@ static void copy_preds_inline(ir_node *n, void *env) {
  */
 static void find_addr(ir_node *node, void *env) {
        int *allow_inline = env;
-       if (is_Proj(node) &&
-                       is_Start(get_Proj_pred(node)) &&
-                       get_Proj_proj(node) == pn_Start_P_value_arg_base) {
-               *allow_inline = 0;
+       if (is_Sel(node)) {
+               ir_graph *irg = current_ir_graph;
+               if (get_Sel_ptr(node) == get_irg_frame(irg)) {
+                       /* access to frame */
+                       ir_entity *ent = get_Sel_entity(node);
+                       if (get_entity_owner(ent) != get_irg_frame_type(irg)) {
+                               /* access to value_type */
+                               *allow_inline = 0;
+                       }
+               }
        } else if (is_Alloc(node) && get_Alloc_where(node) == stack_alloc) {
                /* From GCC:
                 * Refuse to inline alloca call unless user explicitly forced so as this
@@ -859,19 +861,22 @@ int inline_method(ir_node *call, ir_graph *called_graph) {
 
        mtp = get_entity_type(ent);
        ctp = get_Call_type(call);
-       if (get_method_n_params(mtp) > get_method_n_params(ctp)) {
-               /* this is a bad feature of C: without a prototype, we can can call a function with less
-               parameters than needed. Currently we don't support this, although it would be
-               to use Unknown than. */
+       n_params = get_method_n_params(mtp);
+       n_res    = get_method_n_ress(mtp);
+       if (n_params > get_method_n_params(ctp)) {
+               /* this is a bad feature of C: without a prototype, we can
+                * call a function with less parameters than needed. Currently
+                * we don't support this, although we could use Unknown than. */
+               return 0;
+       }
+       if (n_res != get_method_n_ress(ctp)) {
                return 0;
        }
 
        /* Argh, compiling C has some bad consequences:
-          the call type AND the method type might be different.
-          It is implementation defendant what happens in that case.
-          We support inlining, if the bitsize of the types matches AND
-          the same arithmetic is used. */
-       n_params = get_method_n_params(mtp);
+        * It is implementation dependent what happens in that case.
+        * We support inlining, if the bitsize of the types matches AND
+        * the same arithmetic is used. */
        for (i = n_params - 1; i >= 0; --i) {
                ir_type *param_tp = get_method_param_type(mtp, i);
                ir_type *arg_tp   = get_method_param_type(ctp, i);
@@ -889,6 +894,22 @@ int inline_method(ir_node *call, ir_graph *called_graph) {
                        /* otherwise we can simply "reinterpret" the bits */
                }
        }
+       for (i = n_res - 1; i >= 0; --i) {
+               ir_type *decl_res_tp = get_method_res_type(mtp, i);
+               ir_type *used_res_tp = get_method_res_type(ctp, i);
+
+               if (decl_res_tp != used_res_tp) {
+                       ir_mode *decl_mode = get_type_mode(decl_res_tp);
+                       ir_mode *used_mode = get_type_mode(used_res_tp);
+                       if (decl_mode == NULL || used_mode == NULL)
+                               return 0;
+                       if (get_mode_size_bits(decl_mode) != get_mode_size_bits(used_mode))
+                               return 0;
+                       if (get_mode_arithmetic(decl_mode) != get_mode_arithmetic(used_mode))
+                               return 0;
+                       /* otherwise we can "reinterpret" the bits */
+               }
+       }
 
        irg = get_irn_irg(call);
 
@@ -925,6 +946,7 @@ int inline_method(ir_node *call, ir_graph *called_graph) {
        set_irg_doms_inconsistent(irg);
        set_irg_loopinfo_inconsistent(irg);
        set_irg_callee_info_state(irg, irg_callee_info_inconsistent);
+       set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
 
        /* -- Check preconditions -- */
        assert(is_Call(call));
@@ -977,9 +999,7 @@ int inline_method(ir_node *call, ir_graph *called_graph) {
        in[pn_Start_P_frame_base]     = get_irg_frame(irg);
        in[pn_Start_P_tls]            = get_irg_tls(irg);
        in[pn_Start_T_args]           = new_Tuple(n_params, args_in);
-       /* in[pn_Start_P_value_arg_base] = ??? */
-       assert(pn_Start_P_value_arg_base == pn_Start_max - 1 && "pn_Start_P_value_arg_base not supported, fix");
-       pre_call = new_Tuple(pn_Start_max - 1, in);
+       pre_call = new_Tuple(pn_Start_max, in);
        post_call = call;
 
        /* --
@@ -1115,11 +1135,18 @@ int inline_method(ir_node *call, ir_graph *called_graph) {
        /* Now the real results */
        if (n_res > 0) {
                for (j = 0; j < n_res; j++) {
+                       ir_type *res_type = get_method_res_type(ctp, j);
+                       ir_mode *res_mode = get_type_mode(res_type);
                        n_ret = 0;
                        for (i = 0; i < arity; i++) {
                                ret = get_Block_cfgpred(end_bl, i);
                                if (is_Return(ret)) {
-                                       cf_pred[n_ret] = get_Return_res(ret, j);
+                                       ir_node *res = get_Return_res(ret, j);
+                                       if (get_irn_mode(res) != res_mode) {
+                                               ir_node *block = get_nodes_block(res);
+                                               res = new_r_Conv(irg, block, res, res_mode);
+                                       }
+                                       cf_pred[n_ret] = res;
                                        n_ret++;
                                }
                        }
@@ -1166,7 +1193,9 @@ int inline_method(ir_node *call, ir_graph *called_graph) {
                        }
                }
                if (n_exc > 0) {
-                       new_Block(n_exc, cf_pred);      /* watch it: current_block is changed! */
+                       ir_node *block = new_Block(n_exc, cf_pred);
+                       set_cur_block(block);
+
                        set_Tuple_pred(call, pn_Call_X_except, new_Jmp());
                        /* The Phi for the memories with the exception objects */
                        n_exc = 0;
@@ -1325,6 +1354,7 @@ void inline_small_irgs(ir_graph *irg, int size) {
 
        if (! list_empty(&env.calls)) {
                /* There are calls to inline */
+               ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
                collect_phiprojs(irg);
 
                list_for_each_entry(call_entry, entry, &env.calls, list) {
@@ -1341,6 +1371,7 @@ void inline_small_irgs(ir_graph *irg, int size) {
                                inline_method(entry->call, callee);
                        }
                }
+               ir_free_resources(irg, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
        }
        obstack_free(&env.obst, NULL);
        current_ir_graph = rem;
@@ -1574,6 +1605,7 @@ void inline_leave_functions(unsigned maxsize, unsigned leavesize,
                        current_ir_graph = get_irp_irg(i);
                        env              = get_irg_link(current_ir_graph);
 
+                       ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
                        list_for_each_entry_safe(call_entry, entry, next, &env->calls, list) {
                                ir_graph            *callee;
                                irg_inline_property  prop;
@@ -1616,6 +1648,7 @@ void inline_leave_functions(unsigned maxsize, unsigned leavesize,
                                        }
                                }
                        }
+                       ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
                }
        } while (did_inline);
 
@@ -1627,6 +1660,8 @@ void inline_leave_functions(unsigned maxsize, unsigned leavesize,
                current_ir_graph = get_irp_irg(i);
                env              = get_irg_link(current_ir_graph);
 
+               ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
+
                /* note that the list of possible calls is updated during the process */
                list_for_each_entry_safe(call_entry, entry, next, &env->calls, list) {
                        irg_inline_property prop;
@@ -1663,6 +1698,8 @@ void inline_leave_functions(unsigned maxsize, unsigned leavesize,
                                        inline_irg_env *callee_env;
                                        ir_graph       *copy;
 
+                                       ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
+
                                        /*
                                         * No copy yet, create one.
                                         * Note that recursive methods are never leaves, so it is sufficient
@@ -1673,6 +1710,8 @@ void inline_leave_functions(unsigned maxsize, unsigned leavesize,
                                        /* create_irg_copy() destroys the Proj links, recompute them */
                                        phiproj_computed = 0;
 
+                                       ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
+
                                        /* allocate new environment */
                                        callee_env = alloc_inline_irg_env();
                                        set_irg_link(copy, callee_env);
@@ -1726,6 +1765,7 @@ void inline_leave_functions(unsigned maxsize, unsigned leavesize,
                                }
                        }
                }
+               ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
        }
 
        for (i = 0; i < n_irgs; ++i) {
@@ -2071,6 +2111,7 @@ static void inline_into(ir_graph *irg, unsigned maxsize,
        }
 
        current_ir_graph = irg;
+       ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
 
        /* put irgs into the pqueue */
        pqueue = new_pqueue();
@@ -2135,6 +2176,8 @@ static void inline_into(ir_graph *irg, unsigned maxsize,
                        if (benefice < inline_threshold)
                                continue;
 
+                       ir_free_resources(irg, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
+
                        /*
                         * No copy yet, create one.
                         * Note that recursive methods are never leaves, so it is
@@ -2145,6 +2188,8 @@ static void inline_into(ir_graph *irg, unsigned maxsize,
                        /* create_irg_copy() destroys the Proj links, recompute them */
                        phiproj_computed = 0;
 
+                       ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
+
                        /* allocate a new environment */
                        callee_env = alloc_inline_irg_env();
                        set_irg_link(copy, callee_env);
@@ -2214,7 +2259,7 @@ static void inline_into(ir_graph *irg, unsigned maxsize,
                env->n_nodes += callee_env->n_nodes;
                --callee_env->n_callers;
        }
-
+       ir_free_resources(irg, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
        del_pqueue(pqueue);
 }