simplify/rework lower_calls interface and code
authorMatthias Braun <matze@braunis.de>
Fri, 5 Aug 2011 08:22:30 +0000 (10:22 +0200)
committerMatthias Braun <matze@braunis.de>
Fri, 5 Aug 2011 08:22:30 +0000 (10:22 +0200)
This was done by removing unnecessary options/options that only
allowed marginal tweakings to improve performance while at the same time
making the interface cumbersome.

include/libfirm/irnode.h
include/libfirm/lowering.h
ir/be/TEMPLATE/bearch_TEMPLATE.c
ir/be/amd64/bearch_amd64.c
ir/be/arm/bearch_arm.c
ir/be/ia32/bearch_ia32.c
ir/be/sparc/bearch_sparc.c
ir/ir/irnode.c
ir/lower/lower_calls.c
ir/lower/lower_calls.h [new file with mode: 0644]

index a3f20b1..959403f 100644 (file)
@@ -420,12 +420,6 @@ FIRM_API ir_node *get_Call_param(const ir_node *node, int pos);
 /** Sets the call parameter at position pos. */
 FIRM_API void     set_Call_param(ir_node *node, int pos, ir_node *param);
 
-/**
- * Returns non-zero if a Call is surely a self-recursive Call.
- * Beware: if this functions returns 0, the call might be self-recursive!
- */
-FIRM_API int is_self_recursive_Call(const ir_node *call);
-
 /** Set, get and remove the callee information for a Call node.
  *
  *  The callee information lists all method entities that can be called
index 8f2ab69..44ea887 100644 (file)
 
 #include "begin.h"
 
-/**
- * A type telling where to add hidden parameters.
- */
-typedef enum add_hidden_params {
-       ADD_HIDDEN_ALWAYS_IN_FRONT = 0,   /**< always add hidden parameters in front (default). */
-       ADD_HIDDEN_ALWAYS_LAST     = 1,   /**< always add hidden parameters last, did not work for variadic functions. */
-       ADD_HIDDEN_SMART           = 2    /**< add hidden parameters last for non-variadic and first for variadic functions. */
-} add_hidden;
-
-/**
- * Additional flags for the lowering.
- */
-enum lowering_flags {
-       LF_NONE              = 0, /**< no additional flags */
-       LF_COMPOUND_PARAM    = 1, /**< lower calls with compound parameters */
-       LF_COMPOUND_RETURN   = 2, /**< lower calls with compound returns */
-       LF_RETURN_HIDDEN     = 4, /**< return the hidden address instead of void */
-       LF_SMALL_CMP_IN_REGS = 8  /**< return small compound values in registers */
-};
-
-/** Maximum number of registers that can be used to return compound values. */
-#define MAX_REGISTER_RET_VAL 2
-
-/**
- * A struct containing all control parameters for
- * lower_compound_ret_calls().
- */
-typedef struct {
-       int        def_ptr_alignment;   /**< Default alignment for data pointer. */
-       unsigned   flags;               /**< A bitmask of enum lowering_flags. */
-       add_hidden hidden_params;       /**< Where to add hidden parameters. */
-
-       /**
-        * A function returning a pointer type for a given type.
-        * If this pointer is NULL, a new pointer type is always created.
-        */
-       ir_type *(*find_pointer_type)(ir_type *e_type, ir_mode *mode, int alignment);
-
-       /**
-        * If the LF_SMALL_CMP_IN_REGS flag is set, this function will be called
-        * to decide, whether a compound value should be returned in registers.
-        * This function must return the number of used registers and fill in the modes
-        * of the registers to use. Up to MAX_REGISTER_RET_VAL registers can be used.
-        */
-       int (*ret_compound_in_regs)(ir_type *compound_tp, ir_mode **modes);
-} lower_params_t;
-
-/**
- * Lower calls with compound parameter and return types.
- * This function does the following transformations:
- *
- * If LF_COMPOUND_PARAM is set:
- *
- * - Copy compound parameters to a new location on the callers
- *   stack and transmit the address of this new location
- *
- * If LF_COMPOUND_RETURN is set:
- *
- * - Adds a new (hidden) pointer parameter for
- *   any return compound type. The return type is replaced by void
- *   or if LOWERING_FLAGS_RETURN_HIDDEN is set by the address.
- *
- * - 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.
- *
- * General:
- *
- * - Changes the types of methods and calls to the lowered ones
- *
- * - lower all method types of existing entities
- *
- * In pseudo-code, the following transformation is done:
- *
-   @code
-   struct x ret = func(a, b);
-   @endcode
- *
- * is translated into
-   @code
-   struct x ret;
-   func(&ret, a, b);
-   @endcode
- *
- * If the function returns only one possible result, the copy-on-return
- * optimization is done, ie.
-   @code
-   struct x func(a) {
-     struct x ret;
-     ret.a = a;
-     return ret;
-   }
-   @endcode
- *
- * is transformed into
- *
-   @code
-   void func(struct x *ret, a) {
-     ret->a = a;
-   }
-   @endcode
- *
- * @param params  A structure containing the control parameter for this
- *                transformation.
- *
- * During the transformation, pointer types must be created or reused.
- * The caller can provide params->find_pointer_type for this task to
- * reduce the number of created pointer types.
- * If params->find_pointer_type is NULL, new pointer types
- * are always created automatically.
- */
-FIRM_API void lower_calls_with_compounds(const lower_params_t *params);
-
 /**
  * Lower CopyB nodes of size smaller that max_size into Loads/Stores
  */
index c0e7f56..af48375 100644 (file)
@@ -29,6 +29,7 @@
 #include "irprintf.h"
 #include "ircons.h"
 #include "irgmod.h"
+#include "lower_calls.h"
 
 #include "bitset.h"
 #include "debug.h"
@@ -289,16 +290,8 @@ static int TEMPLATE_get_reg_class_alignment(const arch_register_class_t *cls)
 
 static void TEMPLATE_lower_for_target(void)
 {
-       lower_params_t params = {
-               4,                                     /* def_ptr_alignment */
-               LF_COMPOUND_RETURN | LF_RETURN_HIDDEN, /* flags */
-               ADD_HIDDEN_ALWAYS_IN_FRONT,            /* hidden_params */
-               NULL,                                  /* find pointer type */
-               NULL,                                  /* ret_compound_in_regs */
-       };
-
        /* lower compound param handling */
-       lower_calls_with_compounds(&params);
+       lower_calls_with_compounds(LF_RETURN_HIDDEN);
 }
 
 static int TEMPLATE_is_mux_allowed(ir_node *sel, ir_node *mux_false,
index 8b44edc..cd63314 100644 (file)
@@ -30,6 +30,7 @@
 #include "ircons.h"
 #include "irgmod.h"
 #include "irdump.h"
+#include "lower_calls.h"
 
 #include "bitset.h"
 #include "debug.h"
@@ -470,16 +471,8 @@ static int amd64_get_reg_class_alignment(const arch_register_class_t *cls)
 
 static void amd64_lower_for_target(void)
 {
-       lower_params_t params = {
-               4,                                     /* def_ptr_alignment */
-               LF_COMPOUND_RETURN | LF_RETURN_HIDDEN, /* flags */
-               ADD_HIDDEN_ALWAYS_IN_FRONT,            /* hidden_params */
-               NULL,                                  /* find pointer type */
-               NULL,                                  /* ret_compound_in_regs */
-       };
-
        /* lower compound param handling */
-       lower_calls_with_compounds(&params);
+       lower_calls_with_compounds(LF_RETURN_HIDDEN);
 }
 
 static int amd64_is_mux_allowed(ir_node *sel, ir_node *mux_false,
index a81dc02..6809ea0 100644 (file)
@@ -36,7 +36,7 @@
 #include "irgopt.h"
 #include "iroptimize.h"
 #include "irdump.h"
-#include "lowering.h"
+#include "lower_calls.h"
 #include "error.h"
 
 #include "bitset.h"
@@ -532,16 +532,8 @@ static void arm_lower_for_target(void)
 {
        size_t i, n_irgs = get_irp_n_irgs();
 
-       lower_params_t params = {
-               4,                                     /* def_ptr_alignment */
-               LF_COMPOUND_RETURN | LF_RETURN_HIDDEN, /* flags */
-               ADD_HIDDEN_ALWAYS_IN_FRONT,            /* hidden_params */
-               NULL,                                  /* find pointer type */
-               NULL,                                  /* ret_compound_in_regs */
-       };
-
        /* lower compound param handling */
-       lower_calls_with_compounds(&params);
+       lower_calls_with_compounds(LF_RETURN_HIDDEN);
 
        for (i = 0; i < n_irgs; ++i) {
                ir_graph *irg = get_irp_irg(i);
index a3a4257..97896cc 100644 (file)
@@ -52,6 +52,7 @@
 #include "instrument.h"
 #include "iropt_t.h"
 #include "lower_dw.h"
+#include "lower_calls.h"
 
 #include "../beabi.h"
 #include "../beirg.h"
@@ -2011,13 +2012,6 @@ static void ia32_lower_for_target(void)
                ia32_create_set,
                0,        /* don't lower direct compares */
        };
-       lower_params_t params = {
-               4,                                     /* def_ptr_alignment */
-               LF_COMPOUND_RETURN | LF_RETURN_HIDDEN, /* flags */
-               ADD_HIDDEN_ALWAYS_IN_FRONT,            /* hidden_params */
-               NULL,                                  /* find pointer type */
-               NULL,                                  /* ret_compound_in_regs */
-       };
 
        /* perform doubleword lowering */
        lwrdw_param_t lower_dw_params = {
@@ -2028,7 +2022,7 @@ static void ia32_lower_for_target(void)
        };
 
        /* lower compound param handling */
-       lower_calls_with_compounds(&params);
+       lower_calls_with_compounds(LF_RETURN_HIDDEN);
 
        ir_prepare_dw_lowering(&lower_dw_params);
        ir_lower_dw_ops();
index fa424b5..6094610 100644 (file)
@@ -39,6 +39,7 @@
 #include "irdump.h"
 #include "lowering.h"
 #include "lower_dw.h"
+#include "lower_calls.h"
 
 #include "bitset.h"
 #include "debug.h"
@@ -547,14 +548,7 @@ static void sparc_lower_for_target(void)
                sparc_create_set,
                0,
        };
-       lower_params_t params = {
-               4,                                     /* def_ptr_alignment */
-               LF_COMPOUND_RETURN | LF_RETURN_HIDDEN, /* flags */
-               ADD_HIDDEN_ALWAYS_IN_FRONT,            /* hidden_params */
-               NULL,                                  /* find pointer type */
-               NULL,                                  /* ret_compound_in_regs */
-       };
-       lower_calls_with_compounds(&params);
+       lower_calls_with_compounds(LF_RETURN_HIDDEN);
 
        sparc_lower_64bit();
 
index 770e797..9086fcf 100644 (file)
@@ -1071,23 +1071,6 @@ void remove_Call_callee_arr(ir_node *node)
        node->attr.call.callee_arr = NULL;
 }
 
-/*
- * Returns non-zero if a Call is surely a self-recursive Call.
- * Beware: if this functions returns 0, the call might be self-recursive!
- */
-int 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 1;
-       }
-       return 0;
-}
-
 /* Checks for upcast.
  *
  * Returns true if the Cast node casts a class type to a super type.
index b27b0f5..31da624 100644 (file)
  * @author  Michael Beck
  * @version $Id$
  */
-
 #include "config.h"
 
+#include <stdbool.h>
+
+#include "lower_calls.h"
 #include "lowering.h"
 #include "irprog_t.h"
 #include "irnode_t.h"
 #include "pmap.h"
 #include "error.h"
 
-/** A type map for def_find_pointer_type. */
-static pmap *type_map;
+static pmap *pointer_types;
+static pmap *lowered_mtps;
 
 /**
  * Default implementation for finding a pointer type for a given element type.
  * Simple 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)
 {
-       /* 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. */
-       ir_type *res = (ir_type*)pmap_get(type_map, e_type);
-       if (res == NULL || get_type_mode(res) != mode) {
-               res = new_type_pointer(e_type);
-               set_type_mode(res, mode);
-               set_type_alignment_bytes(res, alignment);
-               pmap_insert(type_map, e_type, res);
+       ir_type *res = (ir_type*)pmap_get(pointer_types, dest_type);
+       if (res == NULL) {
+               res = new_type_pointer(dest_type);
+               pmap_insert(pointer_types, dest_type, res);
        }
        return res;
 }
@@ -66,99 +63,66 @@ static ir_type *def_find_pointer_type(ir_type *e_type, ir_mode *mode,
 /**
  * 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;
-       size_t  *param_map;
-       ir_mode *modes[MAX_REGISTER_RET_VAL];
-       size_t  n_ress, n_params, nn_ress, nn_params, i;
-       add_hidden hidden_params;
-       int        changed = 0;
-       ir_variadicity var;
-
-       lowered = get_associated_type(mtp);
+       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;
+
+       if (!is_Method_type(mtp))
+               return mtp;
+
+       lowered = (ir_type*)pmap_get(lowered_mtps, mtp);
        if (lowered != NULL)
                return lowered;
 
-       n_ress   = get_method_n_ress(mtp);
-       NEW_ARR_A(ir_type *, results, n_ress);
-
-       n_params = get_method_n_params(mtp);
-       NEW_ARR_A(ir_type *, params, n_params + n_ress);
-
-       NEW_ARR_A(size_t, param_map, n_params + n_ress);
-
-       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;
-                                       param_map[nn_params] = n_params + i;
-                                       ++nn_params;
-                                       changed++;
-                                       if (lp->flags & LF_RETURN_HIDDEN)
-                                               results[nn_ress++] = ptr_tp;
-                               }
-                       } else {
-                               /* scalar result */
-                               results[nn_ress++] = res_tp;
-                       }
-               }
-
-               for (i = 0; i < n_params; ++i, ++nn_params) {
-                       params[nn_params]    = get_method_param_type(mtp, i);
-                       param_map[nn_params] = 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);
-                       param_map[nn_params] = nn_params;
+       /* check if the type has to be lowered at all */
+       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)) {
+                       must_be_lowered = true;
+                       break;
                }
-
-               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);
-                               param_map[nn_params] = n_params + i;
-                               ++nn_params;
-                       } else {
-                               results[nn_ress++] = res_tp;
-                       }
+       }
+       if (!must_be_lowered)
+               return mtp;
+
+       n_params  = get_method_n_params(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 (is_compound_type(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) {
+               params[nn_params++] = get_method_param_type(mtp, i);
+       }
+       assert(nn_ress <= n_ress);
+       assert(nn_params <= n_params + n_ress);
 
        /* create the new type */
        lowered = new_d_type_method(nn_params, nn_ress, get_type_dbg_info(mtp));
@@ -169,15 +133,13 @@ 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);
+       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);
-       }
+       set_method_calling_convention(lowered, get_method_calling_convention(mtp) | cc_compound_ret);
 
        set_lowered_type(mtp, lowered);
+       pmap_insert(lowered_mtps, mtp, lowered);
 
        return lowered;
 }
@@ -197,12 +159,10 @@ struct cl_entry {
  */
 typedef struct wlk_env_t {
        size_t               arg_shift;        /**< The Argument index shift for parameters. */
-       size_t               first_hidden;     /**< The index of the first hidden argument. */
        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. */
+       compound_call_lowering_flags flags;
        ir_type              *lowered_mtp;     /**< The lowered method type of the current irg if any. */
        ir_type              *value_params;    /**< The value params type if any. */
        unsigned             only_local_mem:1; /**< Set if only local memory access was found. */
@@ -282,6 +242,23 @@ static void check_ptr(ir_node *ptr, wlk_env *env)
        }
 }
 
+/*
+ * 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 1;
+       }
+       return 0;
+}
+
 /**
  * Post walker: shift all parameter indexes
  * and collect Calls with compound returns in the call list.
@@ -319,63 +296,63 @@ 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;
                        }
                }
                break;
-       case iro_Call:
+       case iro_Call: {
+               size_t i;
+               size_t n_res;
                if (! is_self_recursive_Call(n)) {
                        /* any non self recursive call might access global memory */
                        env->only_local_mem = 0;
                }
 
                ctp = get_Call_type(n);
-               if (env->params->flags & LF_COMPOUND_RETURN) {
-                       /* check for compound returns */
-                       size_t i, n_res;
-                       for (i = 0, n_res = get_method_n_ress(ctp); i < n_res; ++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, n_res = get_method_n_ress(ctp); i < n_res; ++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;
                        }
                }
                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);
-                       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;
-                                               }
+               /* 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)) {
+                                       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;
+       }
        default:
                /* do nothing */
                break;
@@ -389,14 +366,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;
 }
@@ -443,33 +420,28 @@ static void do_copy_return_opt(ir_node *n, void *ctx)
  * @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,
+                              wlk_env *env)
 {
        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)
+       if (e) {
                ent = (ir_entity*)e->value;
-       else {
+       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);
+               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 */
-                       assert(0 && "Fixed layout not implemented");
+                       panic("Fixed layout not implemented");
                }
        }
-       return new_r_simpleSel(
-               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);
 }
 
 /**
@@ -481,7 +453,8 @@ static ir_node *get_dummy_sel(ir_graph *irg, ir_node *block, ir_type *tp, wlk_en
  * @param entry  the call list
  * @param env    the environment
  */
-static void add_hidden_param(ir_graph *irg, size_t 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, wlk_env *env)
 {
        ir_node *p, *n, *mem, *blk;
        size_t n_args;
@@ -532,25 +505,18 @@ static void add_hidden_param(ir_graph *irg, size_t n_com, ir_node **ins, cl_entr
  */
 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;
        size_t i, n_res, 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);
+               lowered_mtp = lower_mtp(env->flags, ctp);
                set_Call_type(call, lowered_mtp);
 
-               hidden_params = lp->hidden_params;
-               if (hidden_params == ADD_HIDDEN_SMART &&
-                       get_method_variadicity(ctp) == variadicity_variadic)
-                       hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
-
                n_params = get_Call_n_params(call);
 
                n_com = 0;
@@ -561,17 +527,11 @@ static void fix_call_list(ir_graph *irg, wlk_env *env)
                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;
-               }
+               add_hidden_param(irg, n_com, &new_in[pos], p, env);
+               pos += n_com;
                /* 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;
-               }
                new_in[0] = get_Call_mem(call);
                new_in[1] = get_Call_ptr(call);
 
@@ -585,12 +545,10 @@ 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;
        size_t     i, j, k, n_ress = 0, n_ret_com = 0;
@@ -598,24 +556,16 @@ static void transform_irg(const lower_params_t *lp, ir_graph *irg)
        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);
+       /* 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;
-               }
+               if (is_compound_type(tp))
+                       ++n_ret_com;
        }
 
        if (n_ret_com) {
@@ -623,23 +573,11 @@ static void transform_irg(const lower_params_t *lp, ir_graph *irg)
                normalize_one_return(irg);
 
                /* This graph has a compound argument. Create a new type */
-               lowered_mtp = create_modified_mtd_type(lp, mtp);
+               lowered_mtp = lower_mtp(flags, 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;
@@ -648,8 +586,7 @@ static void transform_irg(const lower_params_t *lp, ir_graph *irg)
        obstack_init(&env.obst);
        env.cl_list        = NULL;
        env.dummy_map      = pmap_create_ex(8);
-       env.dnr            = 0;
-       env.params         = lp;
+       env.flags          = flags;
        env.lowered_mtp    = lowered_mtp;
        env.value_params   = get_method_value_param_type(mtp);
        env.only_local_mem = 1;
@@ -704,17 +641,17 @@ static void transform_irg(const lower_params_t *lp, ir_graph *irg)
 
                                if (is_compound_type(tp)) {
                                        ir_node *arg = get_irg_args(irg);
-                                       arg = new_r_Proj(arg, mode_P_data, env.first_hidden + k);
+                                       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. */
+                                               /* 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.
+                                                * 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.
                                                 */
@@ -734,7 +671,7 @@ static void transform_irg(const lower_params_t *lp, ir_graph *irg)
                                                        mem = new_r_Proj(copy, mode_M, pn_CopyB_M);
                                                }
                                        }
-                                       if (lp->flags & LF_RETURN_HIDDEN) {
+                                       if (flags & LF_RETURN_HIDDEN) {
                                                new_in[j] = arg;
                                                ++j;
                                        }
@@ -758,111 +695,51 @@ static void transform_irg(const lower_params_t *lp, ir_graph *irg)
                                }
                        }
                }
-       } /* if (n_ret_com) */
+       }
 
        pmap_destroy(env.dummy_map);
        obstack_free(&env.obst, NULL);
-
-       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)
-{
-  size_t 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;
 }
 
-/**
- * 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 = (const lower_params_t*)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)
 {
        size_t i, n;
-       ir_graph *irg;
-       lower_params_t param = *params;
 
-       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 = 0, n = get_irp_n_irgs(); i < n; ++i) {
-               irg = get_irp_irg(i);
-
-               transform_irg(&param, irg);
+               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, &param);
+       type_walk(NULL, lower_method_types, &flags);
 
-       if (type_map)
-               pmap_destroy(type_map);
+       pmap_destroy(lowered_mtps);
+       pmap_destroy(pointer_types);
 }
-
diff --git a/ir/lower/lower_calls.h b/ir/lower/lower_calls.h
new file mode 100644 (file)
index 0000000..8b08c41
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * 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.
+ */
+
+/**
+ * @file
+ * @brief   Lowering of calls with compound arguments
+ * @author  Michael Beck
+ * @version $Id$
+ */
+#ifndef FIRM_LOWER_CALLS_H
+#define FIRM_LOWER_CALLS_H
+
+#include "firm_types.h"
+
+/**
+ * Additional flags for the lowering.
+ */
+typedef enum compound_call_lowering_flags {
+       LF_NONE              = 0,      /**< no additional flags */
+       LF_RETURN_HIDDEN     = 1 << 0, /**< return the hidden address instead of void */
+} compound_call_lowering_flags;
+ENUM_BITSET(compound_call_lowering_flags)
+
+/**
+ * Lower calls with compound parameter and return types.
+ * This function does the following transformations:
+ *
+ * If LF_COMPOUND_PARAM is set:
+ *
+ * - Copy compound parameters to a new location on the callers
+ *   stack and transmit the address of this new location
+ *
+ * If LF_COMPOUND_RETURN is set:
+ *
+ * - Adds a new (hidden) pointer parameter for
+ *   any return compound type. The return type is replaced by void
+ *   or if LOWERING_FLAGS_RETURN_HIDDEN is set by the address.
+ *
+ * - 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.
+ *
+ * General:
+ *
+ * - Changes the types of methods and calls to the lowered ones
+ *
+ * - lower all method types of existing entities
+ *
+ * In pseudo-code, the following transformation is done:
+ *
+   @code
+   struct x ret = func(a, b);
+   @endcode
+ *
+ * is translated into
+   @code
+   struct x ret;
+   func(&ret, a, b);
+   @endcode
+ *
+ * If the function returns only one possible result, the copy-on-return
+ * optimization is done, ie.
+   @code
+   struct x func(a) {
+     struct x ret;
+     ret.a = a;
+     return ret;
+   }
+   @endcode
+ *
+ * is transformed into
+ *
+   @code
+   void func(struct x *ret, a) {
+     ret->a = a;
+   }
+   @endcode
+ */
+void lower_calls_with_compounds(compound_call_lowering_flags flags);
+
+#endif