fix lower_calls sometimes not lowering method type
[libfirm] / ir / lower / lower_calls.c
index 1131b4a..f51ef6a 100644 (file)
@@ -1,20 +1,6 @@
 /*
- * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
- *
  * This file is part of libFirm.
- *
- * This file may be distributed and/or modified under the terms of the
- * GNU General Public License version 2 as published by the Free Software
- * Foundation and appearing in the file LICENSE.GPL included in the
- * packaging of this file.
- *
- * Licensees holding valid libFirm Professional Edition licenses may use
- * this file in accordance with the libFirm Commercial License.
- * Agreement provided with the Software.
- *
- * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
- * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE.
+ * Copyright (C) 2012 University of Karlsruhe.
  */
 
 /**
 #include "array_t.h"
 #include "pmap.h"
 #include "error.h"
+#include "util.h"
 
 static pmap *pointer_types;
 static pmap *lowered_mtps;
 
+static bool needs_lowering(const ir_type *type)
+{
+       return is_compound_type(type) || is_Array_type(type);
+}
+
 /**
  * Default implementation for finding a pointer type for a given element type.
- * Simple create a new one.
+ * Simply create a new one.
  */
 static ir_type *get_pointer_type(ir_type *dest_type)
 {
-       ir_type *res = (ir_type*)pmap_get(pointer_types, dest_type);
+       ir_type *res = pmap_get(ir_type, pointer_types, dest_type);
        if (res == NULL) {
                res = new_type_pointer(dest_type);
                pmap_insert(pointer_types, dest_type, res);
@@ -92,7 +84,7 @@ static void remove_compound_param_entities(ir_graph *irg)
                        continue;
 
                type = get_entity_type(member);
-               if (is_compound_type(type)) {
+               if (needs_lowering(type)) {
                        free_entity(member);
                }
        }
@@ -119,7 +111,7 @@ static ir_type *lower_mtp(compound_call_lowering_flags flags, ir_type *mtp)
        if (!is_Method_type(mtp))
                return mtp;
 
-       lowered = (ir_type*)pmap_get(lowered_mtps, mtp);
+       lowered = pmap_get(ir_type, lowered_mtps, mtp);
        if (lowered != NULL)
                return lowered;
 
@@ -128,7 +120,7 @@ static ir_type *lower_mtp(compound_call_lowering_flags flags, ir_type *mtp)
        n_ress   = get_method_n_ress(mtp);
        for (i = 0; i < n_ress; ++i) {
                ir_type *res_tp = get_method_res_type(mtp, i);
-               if (is_compound_type(res_tp)) {
+               if (needs_lowering(res_tp)) {
                        must_be_lowered = true;
                        break;
                }
@@ -136,7 +128,7 @@ static ir_type *lower_mtp(compound_call_lowering_flags flags, ir_type *mtp)
        if (!must_be_lowered && !(flags & LF_DONT_LOWER_ARGUMENTS)) {
                for (i = 0; i < n_params; ++i) {
                        ir_type *param_type = get_method_param_type(mtp, i);
-                       if (is_compound_type(param_type)) {
+                       if (needs_lowering(param_type)) {
                                must_be_lowered = true;
                                break;
                        }
@@ -154,7 +146,7 @@ static ir_type *lower_mtp(compound_call_lowering_flags flags, ir_type *mtp)
        for (i = 0; i < n_ress; ++i) {
                ir_type *res_tp = get_method_res_type(mtp, i);
 
-               if (is_compound_type(res_tp)) {
+               if (needs_lowering(res_tp)) {
                        /* this compound will be allocated on callers stack and its
                           address will be transmitted as a hidden parameter. */
                        ir_type *ptr_tp = get_pointer_type(res_tp);
@@ -169,8 +161,7 @@ static ir_type *lower_mtp(compound_call_lowering_flags flags, ir_type *mtp)
        /* copy over parameter types */
        for (i = 0; i < n_params; ++i) {
                ir_type *param_type = get_method_param_type(mtp, i);
-               if (! (flags & LF_DONT_LOWER_ARGUMENTS)
-                   && is_compound_type(param_type)) {
+               if (! (flags & LF_DONT_LOWER_ARGUMENTS) && needs_lowering(param_type)) {
                    /* turn parameter into a pointer type */
                    param_type = new_type_pointer(param_type);
                }
@@ -228,7 +219,6 @@ typedef struct wlk_env_t {
        size_t               arg_shift;        /**< The Argument index shift for parameters. */
        struct obstack       obst;             /**< An obstack to allocate the data on. */
        cl_entry             *cl_list;         /**< The call list. */
-       pmap                 *dummy_map;       /**< A map for finding the dummy arguments. */
        compound_call_lowering_flags flags;
        ir_type              *lowered_mtp;     /**< The lowered method type of the current irg if any. */
        bool                  only_local_mem:1;/**< Set if only local memory access was found. */
@@ -370,7 +360,7 @@ static void fix_args_and_collect_calls(ir_node *n, void *ctx)
                /* check for compound returns */
                for (i = 0; i < n_ress; ++i) {
                        ir_type *type = get_method_res_type(ctp, i);
-                       if (is_compound_type(type)) {
+                       if (needs_lowering(type)) {
                                /*
                                 * This is a call with a compound return. As the result
                                 * might be ignored, we must put it in the list.
@@ -382,7 +372,7 @@ static void fix_args_and_collect_calls(ir_node *n, void *ctx)
                }
                for (i = 0; i < n_params; ++i) {
                        ir_type *type = get_method_param_type(ctp, i);
-                       if (is_compound_type(type)) {
+                       if (needs_lowering(type)) {
                                cl_entry *entry = get_call_entry(n, env);
                                entry->has_compound_param = true;
                                break;
@@ -404,7 +394,7 @@ static void fix_args_and_collect_calls(ir_node *n, void *ctx)
                                ir_node *call = get_Proj_pred(proj);
                                if (is_Call(call)) {
                                        ir_type *ctp = get_Call_type(call);
-                                       if (is_compound_type(get_method_res_type(ctp, get_Proj_proj(src)))) {
+                                       if (needs_lowering(get_method_res_type(ctp, get_Proj_proj(src)))) {
                                                /* found a CopyB from compound Call result */
                                                cl_entry *e = get_call_entry(call, env);
                                                set_irn_link(n, e->copyb);
@@ -419,7 +409,7 @@ static void fix_args_and_collect_calls(ir_node *n, void *ctx)
                ir_entity *entity = get_Sel_entity(n);
                ir_type   *type   = get_entity_type(entity);
 
-               if (is_parameter_entity(entity) && is_compound_type(type)) {
+               if (is_parameter_entity(entity) && needs_lowering(type)) {
                        if (! (env->flags & LF_DONT_LOWER_ARGUMENTS)) {
                                /* note that num was already modified by fix_parameter_entities
                                 * so no need to add env->arg_shift again */
@@ -494,32 +484,22 @@ static void do_copy_return_opt(ir_node *n, void *ctx)
 
 /**
  * Return a Sel node that selects a dummy argument of type tp.
- * Dummy arguments are only needed once and we use a map
- * to store them.
- * We could even assign all dummy arguments the same offset
- * in the frame type ...
  *
  * @param irg    the graph
  * @param block  the block where a newly create Sel should be placed
  * @param tp     the type of the dummy entity that should be create
- * @param env    the environment
  */
-static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp,
-                              wlk_env *env)
+static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp)
 {
-       /* use a map the check if we already create such an entity */
-       ir_entity *ent = pmap_get(env->dummy_map, tp);
-       if (ent == NULL) {
-               ir_type *ft = get_irg_frame_type(irg);
-               ident   *dummy_id = id_unique("dummy.%u");
-               ent = new_entity(ft, dummy_id, tp);
-               pmap_insert(env->dummy_map, tp, ent);
-
-               if (get_type_state(ft) == layout_fixed) {
-                       /* Fix the layout again */
-                       panic("Fixed layout not implemented");
-               }
+       ir_type   *ft       = get_irg_frame_type(irg);
+       ident     *dummy_id = id_unique("dummy.%u");
+       ir_entity *ent      = new_entity(ft, dummy_id, tp);
+
+       if (get_type_state(ft) == layout_fixed) {
+               /* Fix the layout again */
+               panic("Fixed layout not implemented");
        }
+
        return new_r_simpleSel(block, get_irg_no_mem(irg), get_irg_frame(irg), ent);
 }
 
@@ -527,8 +507,7 @@ static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp,
  * Add the hidden parameter from the CopyB node to the Call node.
  */
 static void add_hidden_param(ir_graph *irg, size_t n_com, ir_node **ins,
-                             cl_entry *entry, wlk_env *env,
-                             ir_type *ctp)
+                             cl_entry *entry, ir_type *ctp)
 {
        ir_node *p, *n;
        size_t n_args;
@@ -553,13 +532,15 @@ static void add_hidden_param(ir_graph *irg, size_t n_com, ir_node **ins,
 
                        /* get rid of the CopyB */
                        if (ir_throws_exception(p)) {
-                               turn_into_tuple(p, pn_CopyB_max+1);
-                               set_Tuple_pred(p, pn_CopyB_M,         mem);
-                               set_Tuple_pred(p, pn_CopyB_X_regular, new_r_Jmp(block));
-                               set_Tuple_pred(p, pn_CopyB_X_except,  new_r_Bad(irg, mode_X));
+                               ir_node *const in[] = {
+                                       [pn_CopyB_M]         = mem,
+                                       [pn_CopyB_X_regular] = new_r_Jmp(block),
+                                       [pn_CopyB_X_except]  = new_r_Bad(irg, mode_X),
+                               };
+                               turn_into_tuple(p, ARRAY_SIZE(in), in);
                        } else {
-                               turn_into_tuple(p, pn_CopyB_M+1);
-                               set_Tuple_pred(p, pn_CopyB_M, mem);
+                               ir_node *const in[] = { mem };
+                               turn_into_tuple(p, ARRAY_SIZE(in), in);
                        }
                        ++n_args;
                }
@@ -567,21 +548,21 @@ static void add_hidden_param(ir_graph *irg, size_t n_com, ir_node **ins,
 
        /* now create dummy entities for function with ignored return value */
        if (n_args < n_com) {
-               size_t   i;
-               size_t   j;
+               size_t i;
+               size_t j;
 
                for (j = i = 0; i < get_method_n_ress(ctp); ++i) {
                        ir_type *rtp = get_method_res_type(ctp, i);
-                       if (is_compound_type(rtp)) {
+                       if (needs_lowering(rtp)) {
                                if (ins[j] == NULL)
-                                       ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp, env);
+                                       ins[j] = get_dummy_sel(irg, get_nodes_block(entry->call), rtp);
                                ++j;
                        }
                }
        }
 }
 
-static void fix_compound_ret(wlk_env *env, cl_entry *entry, ir_type *ctp)
+static void fix_compound_ret(cl_entry *entry, ir_type *ctp)
 {
        ir_node  *call     = entry->call;
        ir_graph *irg      = get_irn_irg(call);
@@ -594,7 +575,7 @@ static void fix_compound_ret(wlk_env *env, cl_entry *entry, ir_type *ctp)
 
        for (i = 0; i < n_res; ++i) {
                ir_type *type = get_method_res_type(ctp, i);
-               if (is_compound_type(type))
+               if (needs_lowering(type))
                        ++n_com;
        }
 
@@ -602,7 +583,7 @@ static void fix_compound_ret(wlk_env *env, cl_entry *entry, ir_type *ctp)
        new_in[pos++] = get_Call_mem(call);
        new_in[pos++] = get_Call_ptr(call);
        assert(pos == n_Call_max+1);
-       add_hidden_param(irg, n_com, &new_in[pos], entry, env, ctp);
+       add_hidden_param(irg, n_com, &new_in[pos], entry, ctp);
        pos += n_com;
 
        /* copy all other parameters */
@@ -614,7 +595,7 @@ static void fix_compound_ret(wlk_env *env, cl_entry *entry, ir_type *ctp)
        set_irn_in(call, pos, new_in);
 }
 
-static ir_entity *create_compound_arg_entitiy(ir_graph *irg, ir_type *type)
+static ir_entity *create_compound_arg_entity(ir_graph *irg, ir_type *type)
 {
        ir_type   *frame  = get_irg_frame_type(irg);
        ident     *id     = id_unique("$compound_param.%u");
@@ -643,11 +624,11 @@ static void fix_compound_params(cl_entry *entry, ir_type *ctp)
                ir_node   *sel;
                ir_node   *copyb;
                ir_entity *arg_entity;
-               if (!is_compound_type(type))
+               if (!needs_lowering(type))
                        continue;
 
                arg        = get_Call_param(call, i);
-               arg_entity = create_compound_arg_entitiy(irg, type);
+               arg_entity = create_compound_arg_entity(irg, type);
                block      = get_nodes_block(call);
                sel        = new_rd_simpleSel(dbgi, block, nomem, frame, arg_entity);
                copyb      = new_rd_CopyB(dbgi, block, mem, sel, arg, type);
@@ -670,7 +651,7 @@ static void fix_calls(wlk_env *env)
                        fix_compound_params(entry, ctp);
                }
                if (entry->has_compound_ret) {
-                       fix_compound_ret(env, entry, ctp);
+                       fix_compound_ret(entry, ctp);
                }
        }
 }
@@ -689,7 +670,6 @@ static void transform_irg(compound_call_lowering_flags flags, ir_graph *irg)
        ir_type   *mtp         = get_entity_type(ent);
        size_t     n_ress      = get_method_n_ress(mtp);
        size_t     n_params    = get_method_n_params(mtp);
-       size_t     n_ret_com   = 0;
        size_t     n_param_com = 0;
 
        ir_type   *lowered_mtp, *tp, *ft;
@@ -700,14 +680,15 @@ static void transform_irg(compound_call_lowering_flags flags, ir_graph *irg)
        wlk_env   env;
 
        /* calculate the number of compound returns */
-       for (n_ret_com = i = 0; i < n_ress; ++i) {
+       size_t n_ret_com = 0;
+       for (i = 0; i < n_ress; ++i) {
                ir_type *type = get_method_res_type(mtp, i);
-               if (is_compound_type(type))
+               if (needs_lowering(type))
                        ++n_ret_com;
        }
        for (i = 0; i < n_params; ++i) {
                ir_type *type = get_method_param_type(mtp, i);
-               if (is_compound_type(type))
+               if (needs_lowering(type))
                        ++n_param_com;
        }
 
@@ -718,10 +699,6 @@ static void transform_irg(compound_call_lowering_flags flags, ir_graph *irg)
                /* much easier if we have only one return */
                normalize_one_return(irg);
 
-               /* This graph has a compound argument. Create a new type */
-               lowered_mtp = lower_mtp(flags, mtp);
-               set_entity_type(ent, lowered_mtp);
-
                /* hidden arguments are added first */
                env.arg_shift = n_ret_com;
        } else {
@@ -729,9 +706,12 @@ static void transform_irg(compound_call_lowering_flags flags, ir_graph *irg)
                env.arg_shift = 0;
                lowered_mtp   = NULL;
        }
+
+       lowered_mtp = lower_mtp(flags, mtp);
+       set_entity_type(ent, lowered_mtp);
+
        obstack_init(&env.obst);
        env.cl_list        = NULL;
-       env.dummy_map      = pmap_create_ex(8);
        env.flags          = flags;
        env.lowered_mtp    = lowered_mtp;
        env.only_local_mem = true;
@@ -788,7 +768,7 @@ static void transform_irg(compound_call_lowering_flags flags, ir_graph *irg)
                                ir_node *pred = get_Return_res(ret, i);
                                tp = get_method_res_type(mtp, i);
 
-                               if (is_compound_type(tp)) {
+                               if (needs_lowering(tp)) {
                                        ir_node *arg = get_irg_args(irg);
                                        arg = new_r_Proj(arg, mode_P_data, k);
                                        ++k;
@@ -846,7 +826,6 @@ static void transform_irg(compound_call_lowering_flags flags, ir_graph *irg)
                }
        }
 
-       pmap_destroy(env.dummy_map);
        obstack_free(&env.obst, NULL);
 }