X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Flower%2Flower_calls.c;h=f51ef6ae69971009e77ec5678d933e177fe53972;hb=25313dd42cf428b81b32ca83296ef3dfac9702b7;hp=72a93ab2e336f3deab6586e7c3cfff1462814e2c;hpb=c28fcf22c08f71b02ccd8edb2e091c1b6c22e540;p=libfirm diff --git a/ir/lower/lower_calls.c b/ir/lower/lower_calls.c index 72a93ab2e..f51ef6ae6 100644 --- a/ir/lower/lower_calls.c +++ b/ir/lower/lower_calls.c @@ -1,33 +1,18 @@ /* - * Copyright (C) 1995-2008 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. */ /** * @file * @brief Lowering of Calls with compound parameters and return types. - * @author Michael Beck - * @version $Id$ + * @author Michael Beck, Matthias Braun */ - -#ifdef HAVE_CONFIG_H #include "config.h" -#endif +#include + +#include "lower_calls.h" #include "lowering.h" #include "irprog_t.h" #include "irnode_t.h" @@ -39,135 +24,154 @@ #include "irmemory.h" #include "irtools.h" #include "iroptimize.h" -#include "array.h" +#include "array_t.h" #include "pmap.h" -#include "xmalloc.h" #include "error.h" +#include "util.h" -/** A type map for def_find_pointer_type. */ -static pmap *type_map; +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 *def_find_pointer_type(ir_type *e_type, ir_mode *mode, int alignment) +static ir_type *get_pointer_type(ir_type *dest_type) { - ir_type *res; - pmap_entry *e; - - /* Mode and alignment are always identical in all calls to def_find_pointer_type(), so - we simply can use a map from the element type to the pointer type. */ - e = pmap_find(type_map, e_type); - if (e) - res = e->value; - else { - res = new_type_pointer(mangle_u(get_type_ident(e_type), new_id_from_chars("Ptr", 3)), e_type, mode); - set_type_alignment_bytes(res, alignment); - pmap_insert(type_map, e_type, res); + 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); } return res; } +static void fix_parameter_entities(ir_graph *irg, size_t n_compound_ret) +{ + ir_type *frame_type = get_irg_frame_type(irg); + size_t n_members = get_compound_n_members(frame_type); + size_t i; + + for (i = 0; i < n_members; ++i) { + ir_entity *member = get_compound_member(frame_type, i); + size_t num; + if (!is_parameter_entity(member)) + continue; + + /* increase parameter number since we added a new parameter in front */ + num = get_entity_parameter_number(member); + if (num == IR_VA_START_PARAMETER_NUMBER) + continue; + set_entity_parameter_number(member, num + n_compound_ret); + } +} + +static void remove_compound_param_entities(ir_graph *irg) +{ + ir_type *frame_type = get_irg_frame_type(irg); + size_t n_members = get_compound_n_members(frame_type); + size_t i; + + for (i = n_members; i > 0; ) { + ir_entity *member = get_compound_member(frame_type, --i); + ir_type *type; + if (!is_parameter_entity(member)) + continue; + + type = get_entity_type(member); + if (needs_lowering(type)) { + free_entity(member); + } + } +} + /** * Creates a new lowered type for a method type with compound * arguments. The new type is associated to the old one and returned. - * - * @param lp parameter struct - * @param mtp the method type to lower - * - * The current implementation expects that a lowered type already - * includes the necessary changes ... */ -static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp) +static ir_type *lower_mtp(compound_call_lowering_flags flags, ir_type *mtp) { - ir_type *lowered, *ptr_tp; - ir_type **params, **results, *res_tp; - ir_mode *modes[MAX_REGISTER_RET_VAL]; - int n_ress, n_params, nn_ress, nn_params, i, first_variadic; - ident *id; - add_hidden hidden_params; - int changed = 0; - variadicity var; - - if (is_lowered_type(mtp)) { - /* the type is already lowered. Not handled yet. */ - assert(0 && "lowered types NYI"); - } - - lowered = get_associated_type(mtp); - if (lowered) + bool must_be_lowered = false; + ir_type *lowered; + ir_type **params; + ir_type **results; + size_t n_ress; + size_t n_params; + size_t nn_ress; + size_t nn_params; + size_t i; + unsigned cconv; + mtp_additional_properties mtp_properties; + + if (!is_Method_type(mtp)) + return mtp; + + lowered = pmap_get(ir_type, lowered_mtps, mtp); + if (lowered != NULL) return lowered; - n_ress = get_method_n_ress(mtp); - NEW_ARR_A(ir_type *, results, n_ress); - + /* check if the type has to be lowered at all */ n_params = get_method_n_params(mtp); - NEW_ARR_A(ir_type *, params, n_params + n_ress); - - first_variadic = get_method_first_variadic_param_index(mtp); - - hidden_params = lp->hidden_params; - if (hidden_params == ADD_HIDDEN_SMART && - get_method_variadicity(mtp) == variadicity_variadic) - hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT; - - if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) { - /* add hidden in front */ - for (nn_ress = nn_params = i = 0; i < n_ress; ++i) { - res_tp = get_method_res_type(mtp, i); - - if (is_compound_type(res_tp)) { - int n_regs = 0; - - if (lp->flags & LF_SMALL_CMP_IN_REGS) - n_regs = lp->ret_compound_in_regs(res_tp, modes); - - if (n_regs > 0) { - /* this compound will be returned solely in registers */ - panic("Returning compounds in registers not yet implemented"); - } - else { - /* this compound will be allocated on callers stack and its - address will be transmitted as a hidden parameter. */ - ptr_tp = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment); - params[nn_params++] = ptr_tp; - changed++; - if (lp->flags & LF_RETURN_HIDDEN) - results[nn_ress++] = ptr_tp; - } + 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 (needs_lowering(res_tp)) { + must_be_lowered = true; + break; + } + } + 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 (needs_lowering(param_type)) { + must_be_lowered = true; + break; } - else - results[nn_ress++] = res_tp; } - - /* move the index of the first variadic parameter */ - first_variadic += nn_params; - - for (i = 0; i < n_params; ++i) - params[nn_params++] = get_method_param_type(mtp, i); } - else { - /* add hidden parameters last */ - assert(get_method_variadicity(mtp) == variadicity_non_variadic && - "Cannot add hidden parameters at end of variadic function"); - - for (nn_params = 0; nn_params < n_params; ++nn_params) - params[nn_params] = get_method_param_type(mtp, nn_params); - - for (nn_ress = i = 0; i < n_ress; ++i) { - res_tp = get_method_res_type(mtp, i); - - if (is_compound_type(res_tp)) - params[nn_params++] = lp->find_pointer_type(res_tp, get_modeP_data(), lp->def_ptr_alignment); - else - results[nn_ress++] = res_tp; + if (!must_be_lowered) + return mtp; + + results = ALLOCANZ(ir_type*, n_ress); + params = ALLOCANZ(ir_type*, n_params + n_ress); + nn_ress = 0; + nn_params = 0; + + /* add a hidden parameter in front for every compound result */ + for (i = 0; i < n_ress; ++i) { + ir_type *res_tp = get_method_res_type(mtp, i); + + 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); + params[nn_params++] = ptr_tp; + if (flags & LF_RETURN_HIDDEN) + results[nn_ress++] = ptr_tp; + } else { + /* scalar result */ + results[nn_ress++] = res_tp; } } + /* 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) && needs_lowering(param_type)) { + /* turn parameter into a pointer type */ + param_type = new_type_pointer(param_type); + } + params[nn_params++] = param_type; + } + assert(nn_ress <= n_ress); + assert(nn_params <= n_params + n_ress); /* create the new type */ - id = mangle_u(new_id_from_chars("L", 1), get_type_ident(mtp)); - lowered = new_d_type_method(id, nn_params, nn_ress, get_type_dbg_info(mtp)); + lowered = new_d_type_method(nn_params, nn_ress, get_type_dbg_info(mtp)); /* fill it */ for (i = 0; i < nn_params; ++i) @@ -175,16 +179,23 @@ static ir_type *create_modified_mtd_type(const lower_params_t *lp, ir_type *mtp) for (i = 0; i < nn_ress; ++i) set_method_res_type(lowered, i, results[i]); - var = get_method_variadicity(mtp); - set_method_variadicity(lowered, var); - if (var == variadicity_variadic) - set_method_first_variadic_param_index(lowered, first_variadic); + set_method_variadicity(lowered, get_method_variadicity(mtp)); - /* associate the lowered type with the original one for easier access */ - if(changed) { - set_method_calling_convention(lowered, get_method_calling_convention(mtp) | cc_compound_ret); + cconv = get_method_calling_convention(mtp); + if (nn_params > n_params) { + cconv |= cc_compound_ret; } - set_lowered_type(mtp, lowered); + set_method_calling_convention(lowered, cconv); + + mtp_properties = get_method_additional_properties(mtp); + /* after lowering the call is not const anymore, since it writes to the + * memory for the return value passed to it */ + mtp_properties &= ~mtp_property_const; + set_method_additional_properties(lowered, mtp_properties); + + /* associate the lowered type with the original one for easier access */ + set_higher_type(lowered, mtp); + pmap_insert(lowered_mtps, mtp, lowered); return lowered; } @@ -197,21 +208,21 @@ struct cl_entry { cl_entry *next; /**< Pointer to the next entry. */ ir_node *call; /**< Pointer to the Call node. */ ir_node *copyb; /**< List of all CopyB nodes. */ + bool has_compound_ret : 1; + bool has_compound_param : 1; }; /** * Walker environment for fix_args_and_collect_calls(). */ -typedef struct _wlk_env_t { - int arg_shift; /**< The Argument index shift for parameters. */ - int first_hidden; /**< The index of the first hidden argument. */ +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. */ - unsigned dnr; /**< The dummy index number. */ - const lower_params_t *params; /**< Lowering parameters. */ - unsigned only_local_mem:1; /**< Set if only local memory access was found. */ - unsigned changed:1; /**< Set if the current graph was changed. */ + 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. */ + bool changed:1; /**< Set if the current graph was changed. */ } wlk_env; /** @@ -222,10 +233,11 @@ typedef struct _wlk_env_t { * @param call A Call node. * @param env The environment. */ -static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) { - cl_entry *res = get_irn_link(call); +static cl_entry *get_call_entry(ir_node *call, wlk_env *env) +{ + cl_entry *res = (cl_entry*)get_irn_link(call); if (res == NULL) { - cl_entry *res = obstack_alloc(&env->obst, sizeof(*res)); + res = OALLOC(&env->obst, cl_entry); res->next = env->cl_list; res->call = call; res->copyb = NULL; @@ -242,7 +254,8 @@ static cl_entry *get_Call_entry(ir_node *call, wlk_env *env) { * @param adr the address * @param pEnt points to the base entity if any */ -static ir_node *find_base_adr(ir_node *ptr, ir_entity **pEnt) { +static ir_node *find_base_adr(ir_node *ptr, ir_entity **pEnt) +{ ir_entity *ent = NULL; assert(mode_is_reference(get_irn_mode(ptr))); @@ -271,29 +284,46 @@ static ir_node *find_base_adr(ir_node *ptr, ir_entity **pEnt) { /** * Check if a given pointer represents non-local memory. */ -static void check_ptr(ir_node *ptr, wlk_env *env) { +static void check_ptr(ir_node *ptr, wlk_env *env) +{ ir_storage_class_class_t sc; ir_entity *ent; /* still alias free */ ptr = find_base_adr(ptr, &ent); - sc = classify_pointer(current_ir_graph, ptr, ent); + sc = get_base_sc(classify_pointer(ptr, ent)); if (sc != ir_sc_localvar && sc != ir_sc_malloced) { /* non-local memory access */ - env->only_local_mem = 0; + env->only_local_mem = false; } } +/* + * Returns non-zero if a Call is surely a self-recursive Call. + * Beware: if this functions returns 0, the call might be self-recursive! + */ +static bool is_self_recursive_Call(const ir_node *call) +{ + const ir_node *callee = get_Call_ptr(call); + + if (is_SymConst_addr_ent(callee)) { + const ir_entity *ent = get_SymConst_entity(callee); + const ir_graph *irg = get_entity_irg(ent); + if (irg == get_irn_irg(call)) + return true; + } + return false; +} + /** * Post walker: shift all parameter indexes * and collect Calls with compound returns in the call list. * If a non-alias free memory access is found, reset the alias free * flag. */ -static void fix_args_and_collect_calls(ir_node *n, void *ctx) { - wlk_env *env = ctx; - int i; - ir_type *ctp; +static void fix_args_and_collect_calls(ir_node *n, void *ctx) +{ + wlk_env *env = (wlk_env*)ctx; ir_node *ptr; switch (get_irn_opcode(n)) { @@ -307,77 +337,96 @@ static void fix_args_and_collect_calls(ir_node *n, void *ctx) { case iro_Proj: if (env->arg_shift > 0) { ir_node *pred = get_Proj_pred(n); + ir_graph *irg = get_irn_irg(n); /* Fix the argument numbers */ - if (pred == get_irg_args(current_ir_graph)) { + if (pred == get_irg_args(irg)) { long pnr = get_Proj_proj(n); set_Proj_proj(n, pnr + env->arg_shift); - env->changed = 1; + env->changed = true; } } break; - case iro_Call: + case iro_Call: { + ir_type *ctp = get_Call_type(n); + size_t n_ress = get_method_n_ress(ctp); + size_t n_params = get_method_n_params(ctp); + size_t i; if (! is_self_recursive_Call(n)) { /* any non self recursive call might access global memory */ - env->only_local_mem = 0; + env->only_local_mem = false; } - ctp = get_Call_type(n); - if (env->params->flags & LF_COMPOUND_RETURN) { - /* check for compound returns */ - for (i = get_method_n_ress(ctp) -1; i >= 0; --i) { - if (is_compound_type(get_method_res_type(ctp, i))) { - /* - * This is a call with a compound return. As the result - * might be ignored, we must put it in the list. - */ - (void)get_Call_entry(n, env); - break; - } + /* check for compound returns */ + for (i = 0; i < n_ress; ++i) { + ir_type *type = get_method_res_type(ctp, i); + 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. + */ + cl_entry *entry = get_call_entry(n, env); + entry->has_compound_ret = true; + break; + } + } + for (i = 0; i < n_params; ++i) { + ir_type *type = get_method_param_type(ctp, i); + if (needs_lowering(type)) { + cl_entry *entry = get_call_entry(n, env); + entry->has_compound_param = true; + break; } } break; - case iro_CopyB: + } + case iro_CopyB: { + ir_node *src = get_CopyB_src(n); if (env->only_local_mem) { check_ptr(get_CopyB_src(n), env); if (env->only_local_mem) check_ptr(get_CopyB_dst(n), env); } - if (env->params->flags & LF_COMPOUND_RETURN) { - /* check for compound returns */ - ir_node *src = get_CopyB_src(n); - /* older scheme using value_res_ent */ - if (is_Sel(src)) { - ir_node *proj = get_Sel_ptr(src); - if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_P_value_res_base) { - ir_node *call = get_Proj_pred(proj); - if (is_Call(call)) { + /* check for compound returns */ + if (is_Proj(src)) { + ir_node *proj = get_Proj_pred(src); + if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) { + ir_node *call = get_Proj_pred(proj); + if (is_Call(call)) { + ir_type *ctp = get_Call_type(call); + 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); + cl_entry *e = get_call_entry(call, env); set_irn_link(n, e->copyb); e->copyb = n; } } - } else { - /* new scheme: compound results are determined by the call type only */ - if (is_Proj(src)) { - ir_node *proj = get_Proj_pred(src); - if (is_Proj(proj) && get_Proj_proj(proj) == pn_Call_T_result) { - ir_node *call = get_Proj_pred(proj); - if (is_Call(call)) { - ctp = get_Call_type(call); - if (is_compound_type(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); - e->copyb = n; - } - } - } - } } } break; + } + case iro_Sel: { + ir_entity *entity = get_Sel_entity(n); + ir_type *type = get_entity_type(entity); + + 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 */ + size_t num = get_entity_parameter_number(entity); + ir_graph *irg = get_irn_irg(n); + ir_node *args = get_irg_args(irg); + ir_node *ptr = new_r_Proj(args, mode_P, num); + exchange(n, ptr); + /* hack to avoid us visiting the proj again */ + mark_irn_visited(ptr); + } + + /* we need to copy compound parameters */ + env->only_local_mem = false; + } + break; + } default: /* do nothing */ break; @@ -391,14 +440,14 @@ static void fix_args_and_collect_calls(ir_node *n, void *ctx) { * @param ft the frame type * @param adr the node */ -static int is_compound_address(ir_type *ft, ir_node *adr) +static bool is_compound_address(ir_type *ft, ir_node *adr) { ir_entity *ent; if (! is_Sel(adr)) - return 0; + return false; if (get_Sel_n_indexs(adr) != 0) - return 0; + return false; ent = get_Sel_entity(adr); return get_entity_owner(ent) == ft; } @@ -413,18 +462,18 @@ typedef struct cr_pair { * Post walker: fixes all entities addresses for the copy-return * optimization. * - * Note: We expect the length of the cr_pair array (ie number of compound + * Note: We expect the length of the cr_pair array (i.e. number of compound * return values) to be 1 (C, C++) in almost all cases, so ignore the * linear search complexity here. */ -static void do_copy_return_opt(ir_node *n, void *ctx) { - cr_pair *arr = ctx; - int i; - +static void do_copy_return_opt(ir_node *n, void *ctx) +{ if (is_Sel(n)) { ir_entity *ent = get_Sel_entity(n); + cr_pair *arr = (cr_pair*)ctx; + size_t i, l; - for (i = ARR_LEN(arr) - 1; i >= 0; --i) { + for (i = 0, l = ARR_LEN(arr); i < l; ++i) { if (ent == arr[i].ent) { exchange(n, arr[i].arg); break; @@ -435,165 +484,175 @@ 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) { - ir_entity *ent; - pmap_entry *e; - - /* use a map the check if we already create such an entity */ - e = pmap_find(env->dummy_map, tp); - if (e) - ent = e->value; - else { - ir_type *ft = get_irg_frame_type(irg); - char buf[16]; - - snprintf(buf, sizeof(buf), "dummy.%u", env->dnr++); - ent = new_entity(ft, new_id_from_str(buf), tp); - pmap_insert(env->dummy_map, tp, ent); - - if (get_type_state(ft) == layout_fixed) { - /* Fix the layout again */ - assert(0 && "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( - irg, - block, - get_irg_no_mem(irg), - get_irg_frame(irg), - ent); + + return new_r_simpleSel(block, get_irg_no_mem(irg), get_irg_frame(irg), ent); } /** * Add the hidden parameter from the CopyB node to the Call node. - * - * @param irg the graph - * @param n_com number of compound results (will be number of hidden parameters) - * @param ins in array to store the hidden parameters into - * @param entry the call list - * @param env the environment */ -static void add_hidden_param(ir_graph *irg, int n_com, ir_node **ins, cl_entry *entry, wlk_env *env) +static void add_hidden_param(ir_graph *irg, size_t n_com, ir_node **ins, + cl_entry *entry, ir_type *ctp) { - ir_node *p, *n, *src, *mem, *blk; - ir_entity *ent; - ir_type *owner; - int idx, n_args; + ir_node *p, *n; + size_t n_args; n_args = 0; for (p = entry->copyb; p; p = n) { - n = get_irn_link(p); - src = get_CopyB_src(p); - - /* old scheme using value_res_ent */ - if (is_Sel(src)) { - ent = get_Sel_entity(src); - owner = get_entity_owner(ent); - - /* find the hidden parameter index */ - for (idx = 0; idx < get_struct_n_members(owner); ++idx) - if (get_struct_member(owner, idx) == ent) - break; - assert(idx < get_struct_n_members(owner)); - } else { - /* new scheme: compound returns are determined by the call type and are Proj's */ - idx = get_Proj_proj(src); + ir_node *src = get_CopyB_src(p); + size_t idx = get_Proj_proj(src); + n = (ir_node*)get_irn_link(p); + + /* consider only the first CopyB */ + if (ins[idx] == NULL) { + ir_node *block = get_nodes_block(p); + + /* use the memory output of the call and not the input of the CopyB + * otherwise stuff breaks if the call was mtp_property_const, because + * then the copyb skips the call. But after lowering the call is not + * const anymore, and its memory has to be used */ + ir_node *mem = new_r_Proj(entry->call, mode_M, pn_Call_M); + + ins[idx] = get_CopyB_dst(p); + + /* get rid of the CopyB */ + if (ir_throws_exception(p)) { + 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 { + ir_node *const in[] = { mem }; + turn_into_tuple(p, ARRAY_SIZE(in), in); + } + ++n_args; } - - ins[idx] = get_CopyB_dst(p); - mem = get_CopyB_mem(p); - blk = get_nodes_block(p); - - /* get rid of the CopyB */ - turn_into_tuple(p, pn_CopyB_max); - set_Tuple_pred(p, pn_CopyB_M_regular, mem); - set_Tuple_pred(p, pn_CopyB_M_except, get_irg_bad(irg)); - set_Tuple_pred(p, pn_CopyB_X_regular, new_r_Jmp(irg, blk)); - set_Tuple_pred(p, pn_CopyB_X_except, get_irg_bad(irg)); - ++n_args; } /* now create dummy entities for function with ignored return value */ if (n_args < n_com) { - ir_type *ctp = get_Call_type(entry->call); - int i, j; - - if (is_lowered_type(ctp)) - ctp = get_associated_type(ctp); + 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; } } } } -/** - * Fix all calls on a call list by adding hidden parameters. - * - * @param irg the graph - * @param env the environment - */ -static void fix_call_list(ir_graph *irg, wlk_env *env) { - const lower_params_t *lp = env->params; - cl_entry *p; - ir_node *call, **new_in; - ir_type *ctp, *lowered_mtp; - add_hidden hidden_params; - int i, n_params, n_com, pos; - - new_in = NEW_ARR_F(ir_node *, 0); - for (p = env->cl_list; p; p = p->next) { - call = p->call; - ctp = get_Call_type(call); - lowered_mtp = create_modified_mtd_type(lp, ctp); - set_Call_type(call, lowered_mtp); +static void fix_compound_ret(cl_entry *entry, ir_type *ctp) +{ + ir_node *call = entry->call; + ir_graph *irg = get_irn_irg(call); + size_t n_params = get_Call_n_params(call); + size_t n_com = 0; + size_t n_res = get_method_n_ress(ctp); + size_t pos = 0; + ir_node **new_in; + size_t i; + + for (i = 0; i < n_res; ++i) { + ir_type *type = get_method_res_type(ctp, i); + if (needs_lowering(type)) + ++n_com; + } - hidden_params = lp->hidden_params; - if (hidden_params == ADD_HIDDEN_SMART && - get_method_variadicity(ctp) == variadicity_variadic) - hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT; + new_in = ALLOCANZ(ir_node*, n_params + n_com + (n_Call_max+1)); + 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, ctp); + pos += n_com; + + /* copy all other parameters */ + for (i = 0; i < n_params; ++i) { + ir_node *param = get_Call_param(call, i); + new_in[pos++] = param; + } + assert(pos == n_params+n_com+(n_Call_max+1)); + set_irn_in(call, pos, new_in); +} - n_params = get_Call_n_params(call); +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"); + ir_entity *entity = new_entity(frame, id, type); + /* TODO: + * we could do some optimisations here and create a big union type for all + * different call types in a function */ + return entity; +} - n_com = 0; - for (i = get_method_n_ress(ctp) - 1; i >= 0; --i) { - if (is_compound_type(get_method_res_type(ctp, i))) - ++n_com; - } - pos = 2; - ARR_RESIZE(ir_node *, new_in, n_params + n_com + pos); - memset(new_in, 0, sizeof(*new_in) * (n_params + n_com + pos)); - if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) { - add_hidden_param(irg, n_com, &new_in[pos], p, env); - pos += n_com; +static void fix_compound_params(cl_entry *entry, ir_type *ctp) +{ + ir_node *call = entry->call; + dbg_info *dbgi = get_irn_dbg_info(call); + ir_node *mem = get_Call_mem(call); + ir_graph *irg = get_irn_irg(call); + ir_node *nomem = new_r_NoMem(irg); + ir_node *frame = get_irg_frame(irg); + size_t n_params = get_method_n_params(ctp); + size_t i; + + for (i = 0; i < n_params; ++i) { + ir_type *type = get_method_param_type(ctp, i); + ir_node *block; + ir_node *arg; + ir_node *sel; + ir_node *copyb; + ir_entity *arg_entity; + if (!needs_lowering(type)) + continue; + + arg = get_Call_param(call, i); + 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); + mem = new_r_Proj(copyb, mode_M, pn_CopyB_M); + set_Call_param(call, i, sel); + } + set_Call_mem(call, mem); +} + +static void fix_calls(wlk_env *env) +{ + cl_entry *entry; + for (entry = env->cl_list; entry; entry = entry->next) { + ir_node *call = entry->call; + ir_type *ctp = get_Call_type(call); + ir_type *lowered_mtp = lower_mtp(env->flags, ctp); + set_Call_type(call, lowered_mtp); + + if (entry->has_compound_param) { + fix_compound_params(entry, ctp); } - /* copy all other parameters */ - for (i = 0; i < n_params; ++i) - new_in[pos++] = get_Call_param(call, i); - if (hidden_params == ADD_HIDDEN_ALWAYS_LAST) { - add_hidden_param(irg, n_com, &new_in[pos], p, env); - pos += n_com; + if (entry->has_compound_ret) { + fix_compound_ret(entry, ctp); } - new_in[0] = get_Call_mem(call); - new_in[1] = get_Call_ptr(call); - - set_irn_in(call, n_params + n_com + 2, new_in); } } @@ -603,280 +662,212 @@ static void fix_call_list(ir_graph *irg, wlk_env *env) { * If it calls methods with compound parameter returns, add hidden * parameters. * - * @param lp parameter struct * @param irg the graph to transform */ -static void transform_irg(const lower_params_t *lp, ir_graph *irg) +static void transform_irg(compound_call_lowering_flags flags, ir_graph *irg) { - ir_graph * rem = current_ir_graph; - ir_entity *ent = get_irg_entity(irg); - ir_type *mtp, *lowered_mtp, *tp, *ft; - int i, j, k, n_ress = 0, n_ret_com = 0, n_cr_opt; - ir_node **new_in, *ret, *endbl, *bl, *mem, *copy; - cr_pair *cr_opt; - wlk_env env; - add_hidden hidden_params; - - current_ir_graph = irg; - - assert(ent && "Cannot transform graph without an entity"); - assert(get_irg_phase_state(irg) == phase_high && "call lowering must be done in phase high"); - - mtp = get_entity_type(ent); - - if (lp->flags & LF_COMPOUND_RETURN) { - /* calculate the number of compound returns */ - n_ress = get_method_n_ress(mtp); - for (n_ret_com = i = 0; i < n_ress; ++i) { - tp = get_method_res_type(mtp, i); - - if (is_compound_type(tp)) - ++n_ret_com; - } + ir_entity *ent = get_irg_entity(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_param_com = 0; + + ir_type *lowered_mtp, *tp, *ft; + size_t i, j, k; + size_t n_cr_opt; + ir_node **new_in, *ret, *endbl, *bl, *mem, *copy; + cr_pair *cr_opt; + wlk_env env; + + /* calculate the number of compound returns */ + size_t n_ret_com = 0; + for (i = 0; i < n_ress; ++i) { + ir_type *type = get_method_res_type(mtp, i); + if (needs_lowering(type)) + ++n_ret_com; } + for (i = 0; i < n_params; ++i) { + ir_type *type = get_method_param_type(mtp, i); + if (needs_lowering(type)) + ++n_param_com; + } + + if (n_ret_com > 0) + fix_parameter_entities(irg, n_ret_com); if (n_ret_com) { /* much easier if we have only one return */ normalize_one_return(irg); - /* This graph has a compound argument. Create a new type */ - lowered_mtp = create_modified_mtd_type(lp, mtp); - set_entity_type(ent, lowered_mtp); - - hidden_params = lp->hidden_params; - if (hidden_params == ADD_HIDDEN_SMART && - get_method_variadicity(mtp) == variadicity_variadic) - hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT; - - if (hidden_params == ADD_HIDDEN_ALWAYS_IN_FRONT) { - /* hidden arguments are added first */ - env.arg_shift = n_ret_com; - env.first_hidden = 0; - } else { - /* hidden arguments are added last */ - env.arg_shift = 0; - env.first_hidden = get_method_n_params(mtp); - } + /* hidden arguments are added first */ + env.arg_shift = n_ret_com; } else { /* we must only search for calls */ 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.dnr = 0; - env.params = lp; - env.only_local_mem = 1; - env.changed = 0; + env.flags = flags; + env.lowered_mtp = lowered_mtp; + env.only_local_mem = true; + env.changed = false; /* scan the code, fix argument numbers and collect calls. */ - irg_walk_graph(irg, firm_clear_link, fix_args_and_collect_calls, &env); + irg_walk_graph(irg, firm_clear_link, NULL, &env); + irg_walk_graph(irg, fix_args_and_collect_calls, NULL, &env); + + if (n_param_com > 0 && !(flags & LF_DONT_LOWER_ARGUMENTS)) + remove_compound_param_entities(irg); /* fix all calls */ - if (env.cl_list) { - fix_call_list(irg, &env); - env.changed = 1; + if (env.cl_list != NULL) { + fix_calls(&env); + env.changed = true; } if (n_ret_com) { - /* - * Now fix the Return node of the current graph. - */ - env.changed = 1; + int idx; /* STEP 1: find the return. This is simple, we have normalized the graph. */ endbl = get_irg_end_block(irg); ret = NULL; - for (i = get_Block_n_cfgpreds(endbl) - 1; i >= 0; --i) { - ir_node *pred = get_Block_cfgpred(endbl, i); + for (idx = get_Block_n_cfgpreds(endbl) - 1; idx >= 0; --idx) { + ir_node *pred = get_Block_cfgpred(endbl, idx); if (is_Return(pred)) { ret = pred; break; } } - /* there should always be a return */ - assert(ret); - - /* - * STEP 2: fix it. For all compound return values add a CopyB, - * all others are copied. - */ - NEW_ARR_A(ir_node *, new_in, n_ress + 1); - - bl = get_nodes_block(ret); - mem = get_Return_mem(ret); - - ft = get_irg_frame_type(irg); - NEW_ARR_A(cr_pair, cr_opt, n_ret_com); - n_cr_opt = 0; - for (j = 1, i = k = 0; i < n_ress; ++i) { - ir_node *pred = get_Return_res(ret, i); - tp = get_method_res_type(mtp, i); - - if (is_compound_type(tp)) { - ir_node *arg = get_irg_args(irg); - arg = new_r_Proj(irg, get_nodes_block(arg), arg, mode_P_data, env.first_hidden + k); - ++k; - - if (is_Unknown(pred)) { - /* The Return(Unknown) is the Firm construct for a missing return. - Do nothing. */ - } else { - /** - * Sorrily detecting that copy-return is possible isn't that simple. - * We must check, that the hidden address is alias free during the whole - * function. - * A simple heuristic: all Loads/Stores inside - * the function access only local frame. - */ - if (env.only_local_mem && is_compound_address(ft, pred)) { - /* we can do the copy-return optimization here */ - cr_opt[n_cr_opt].ent = get_Sel_entity(pred); - cr_opt[n_cr_opt].arg = arg; - ++n_cr_opt; - } else { /* copy-return optimization is impossible, do the copy. */ - copy = new_r_CopyB( - irg, bl, - mem, - arg, - pred, - tp - ); - mem = new_r_Proj(irg, bl, copy, mode_M, pn_CopyB_M_regular); + + /* in case of infinite loops, there might be no return */ + if (ret != NULL) { + /* + * Now fix the Return node of the current graph. + */ + env.changed = true; + + /* + * STEP 2: fix it. For all compound return values add a CopyB, + * all others are copied. + */ + NEW_ARR_A(ir_node *, new_in, n_ress + 1); + + bl = get_nodes_block(ret); + mem = get_Return_mem(ret); + + ft = get_irg_frame_type(irg); + NEW_ARR_A(cr_pair, cr_opt, n_ret_com); + n_cr_opt = 0; + for (j = 1, i = k = 0; i < n_ress; ++i) { + ir_node *pred = get_Return_res(ret, i); + tp = get_method_res_type(mtp, i); + + if (needs_lowering(tp)) { + ir_node *arg = get_irg_args(irg); + arg = new_r_Proj(arg, mode_P_data, k); + ++k; + + if (is_Unknown(pred)) { + /* The Return(Unknown) is the Firm construct for a + * missing return. Do nothing. */ + } else { + /** + * Sorrily detecting that copy-return is possible isn't + * that simple. We must check, that the hidden address + * is alias free during the whole function. + * A simple heuristic: all Loads/Stores inside + * the function access only local frame. + */ + if (env.only_local_mem && is_compound_address(ft, pred)) { + /* we can do the copy-return optimization here */ + cr_opt[n_cr_opt].ent = get_Sel_entity(pred); + cr_opt[n_cr_opt].arg = arg; + ++n_cr_opt; + } else { /* copy-return optimization is impossible, do the copy. */ + copy = new_r_CopyB( + bl, + mem, + arg, + pred, + tp + ); + mem = new_r_Proj(copy, mode_M, pn_CopyB_M); + } } - } - if (lp->flags & LF_RETURN_HIDDEN) { - new_in[j] = arg; + if (flags & LF_RETURN_HIDDEN) { + new_in[j] = arg; + ++j; + } + } else { /* scalar return value */ + new_in[j] = pred; ++j; } - } else { /* scalar return value */ - new_in[j] = pred; - ++j; } - } - /* replace the in of the Return */ - new_in[0] = mem; - set_irn_in(ret, j, new_in); + /* replace the in of the Return */ + new_in[0] = mem; + set_irn_in(ret, j, new_in); - if (n_cr_opt > 0) { - irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt); + if (n_cr_opt > 0) { + size_t c; + size_t n; - for (i = ARR_LEN(cr_opt) - 1; i >= 0; --i) { - remove_class_member(ft, cr_opt[i].ent); + irg_walk_graph(irg, NULL, do_copy_return_opt, cr_opt); + + for (c = 0, n = ARR_LEN(cr_opt); c < n; ++c) { + free_entity(cr_opt[c].ent); + } } } - } /* if (n_ret_com) */ - - pmap_destroy(env.dummy_map); - obstack_free(&env.obst, NULL); - - if (env.changed) { - /* invalidate the analysis info */ - set_irg_outs_inconsistent(irg); - set_irg_loopinfo_state(irg, loopinfo_inconsistent); } - current_ir_graph = rem; -} -/** - * Returns non-zero if the given type is a method - * type that must be lowered. - * - * @param lp lowering parameters - * @param tp The type. - */ -static int must_be_lowered(const lower_params_t *lp, ir_type *tp) { - int i, n_ress; - ir_type *res_tp; - - if (is_Method_type(tp)) { - if (lp->flags & LF_COMPOUND_RETURN) { - /* check for compound returns */ - n_ress = get_method_n_ress(tp); - for (i = 0; i < n_ress; ++i) { - res_tp = get_method_res_type(tp, i); - - if (is_compound_type(res_tp)) - return 1; - } - } - } - return 0; + obstack_free(&env.obst, NULL); } -/** - * type-walker: lower all method types of entities - * and points-to types. - */ static void lower_method_types(type_or_ent tore, void *env) { - const lower_params_t *lp = env; - ir_type *tp; + const compound_call_lowering_flags *flags + = (const compound_call_lowering_flags*)env; /* fix method entities */ if (is_entity(tore.ent)) { - ir_entity *ent = tore.ent; - tp = get_entity_type(ent); - - if (must_be_lowered(lp, tp)) { - tp = create_modified_mtd_type(lp, tp); - set_entity_type(ent, tp); - } + ir_entity *ent = tore.ent; + ir_type *tp = get_entity_type(ent); + ir_type *lowered = lower_mtp(*flags, tp); + set_entity_type(ent, lowered); } else { - tp = tore.typ; + ir_type *tp = tore.typ; /* fix pointer to methods */ if (is_Pointer_type(tp)) { - ir_type *etp = get_pointer_points_to_type(tp); - if (must_be_lowered(lp, etp)) { - etp = create_modified_mtd_type(lp, etp); - set_pointer_points_to_type(tp, etp); - } + ir_type *points_to = get_pointer_points_to_type(tp); + ir_type *lowered_points_to = lower_mtp(*flags, points_to); + set_pointer_points_to_type(tp, lowered_points_to); } } } -/* - * Lower calls with compound parameters and return types. - * This function does the following transformations: - * - * - Adds a new (hidden) pointer parameter for - * any return compound type. - * - * - Use of the hidden parameters in the function code. - * - * - Change all calls to functions with compound return - * by providing space for the hidden parameter on the callers - * stack. - * - * - Replace a possible block copy after the function call. - */ -void lower_calls_with_compounds(const lower_params_t *params) +void lower_calls_with_compounds(compound_call_lowering_flags flags) { - int i; - ir_graph *irg; - lower_params_t param = *params; + size_t i, n; - if (param.find_pointer_type == NULL) { - param.find_pointer_type = def_find_pointer_type; - type_map = pmap_create_ex(8); - } else - type_map = NULL; + pointer_types = pmap_create(); + lowered_mtps = pmap_create(); /* first step: Transform all graphs */ - for (i = get_irp_n_irgs() - 1; i >= 0; --i) { - irg = get_irp_irg(i); - - if (irg == get_const_code_irg()) - continue; - - transform_irg(¶m, irg); + for (i = 0, n = get_irp_n_irgs(); i < n; ++i) { + ir_graph *irg = get_irp_irg(i); + transform_irg(flags, irg); } /* second step: Lower all method types of visible entities */ - type_walk(NULL, lower_method_types, ¶m); + type_walk(NULL, lower_method_types, &flags); - if (type_map) - pmap_destroy(type_map); + pmap_destroy(lowered_mtps); + pmap_destroy(pointer_types); }