make unique types/entities part of irprog
[libfirm] / ir / be / beabi.c
index b2588b4..1e6cb71 100644 (file)
@@ -21,7 +21,6 @@
  * @file
  * @brief       Backend ABI implementation.
  * @author      Sebastian Hack, Michael Beck
- * @version     $Id$
  */
 #include "config.h"
 
 #include "irgwalk.h"
 #include "irprintf_t.h"
 #include "irgopt.h"
-#include "irbitset.h"
 #include "iropt_t.h"
+#include "irtools.h"
 #include "heights.h"
 #include "pdeq.h"
-#include "irtools.h"
+#include "util.h"
 #include "raw_bitset.h"
 #include "error.h"
 #include "pset_new.h"
@@ -118,6 +117,32 @@ static void be_abi_reg_map_set(pmap *map, const arch_register_t* reg,
        pmap_insert(map, reg, node);
 }
 
+/**
+ * Check if the given register is callee save, ie. will be saved by the callee.
+ */
+static bool arch_register_is_callee_save(
+       const arch_env_t      *arch_env,
+       const arch_register_t *reg)
+{
+       if (arch_env->impl->register_saved_by)
+               return arch_env->impl->register_saved_by(reg, /*callee=*/1);
+       return false;
+}
+
+/**
+ * Check if the given register is caller save, ie. must be saved by the caller.
+ */
+static bool arch_register_is_caller_save(
+       const arch_env_t      *arch_env,
+       const arch_register_t *reg)
+{
+       if (arch_env->impl->register_saved_by)
+               return arch_env->impl->register_saved_by(reg, /*callee=*/0);
+       return false;
+}
+
+
+
 /*
      _    ____ ___    ____      _ _ _                _
     / \  | __ )_ _|  / ___|__ _| | | |__   __ _  ___| | _____
@@ -361,7 +386,7 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
        const ir_edge_t        *edge;
        int                    *reg_param_idxs;
        int                    *stack_param_idx;
-       int                     i, n, destroy_all_regs;
+       int                     i, n;
        int                     throws_exception;
        size_t                  s;
        size_t                  p;
@@ -498,20 +523,6 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
                }
        }
 
-       /* check for the return_twice property */
-       destroy_all_regs = 0;
-       if (is_SymConst_addr_ent(call_ptr)) {
-               ir_entity *ent = get_SymConst_entity(call_ptr);
-
-               if (get_entity_additional_properties(ent) & mtp_property_returns_twice)
-                       destroy_all_regs = 1;
-       } else {
-               ir_type *call_tp = get_Call_type(irn);
-
-               if (get_method_additional_properties(call_tp) & mtp_property_returns_twice)
-                       destroy_all_regs = 1;
-       }
-
        /* Put caller save into the destroyed set and state registers in the states
         * set */
        for (i = 0, n = arch_env->n_register_classes; i < n; ++i) {
@@ -532,7 +543,7 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
                                 * checking */
                                continue;
                        }
-                       if (destroy_all_regs || arch_register_is_caller_save(arch_env, reg)) {
+                       if (arch_register_is_caller_save(arch_env, reg)) {
                                if (!(reg->type & arch_register_type_ignore)) {
                                        ARR_APP1(const arch_register_t*, destroyed_regs, reg);
                                }
@@ -581,8 +592,8 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
        /* add state registers ins */
        for (s = 0; s < ARR_LEN(states); ++s) {
                const arch_register_t       *reg = states[s];
-               const arch_register_class_t *cls = arch_register_get_class(reg);
-               ir_node *regnode = new_r_Unknown(irg, arch_register_class_mode(cls));
+               const arch_register_class_t *cls = reg->reg_class;
+               ir_node *regnode = new_r_Unknown(irg, cls->mode);
                in[n_ins++]      = regnode;
        }
        assert(n_ins == (int) (n_reg_params + ARR_LEN(states)));
@@ -732,7 +743,7 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
                keep = be_new_Keep(bl, n, in);
                for (i = 0; i < n; ++i) {
                        const arch_register_t *reg = (const arch_register_t*)get_irn_link(in[i]);
-                       be_node_set_reg_class_in(keep, i, reg->reg_class);
+                       be_node_set_reg_class_in(keep, i, arch_register_get_class(reg));
                }
        }
 
@@ -854,7 +865,7 @@ static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp
        count = get_Alloc_count(alloc);
 
        /* we might need to multiply the count with the element size */
-       if (type != firm_unknown_type && get_type_size_bytes(type) != 1) {
+       if (!is_unknown_type(type) && get_type_size_bytes(type) != 1) {
                ir_mode   *mode  = get_irn_mode(count);
                ir_tarval *tv    = new_tarval_from_long(get_type_size_bytes(type),
                                                        mode);
@@ -919,14 +930,14 @@ static ir_node *adjust_free(be_abi_irg_t *env, ir_node *free, ir_node *curr_sp)
        assert(get_Free_where(free) == stack_alloc);
 
        /* we might need to multiply the size with the element size */
-       if (type != firm_unknown_type && get_type_size_bytes(type) != 1) {
+       if (!is_unknown_type(type) && get_type_size_bytes(type) != 1) {
                ir_tarval *tv   = new_tarval_from_long(get_type_size_bytes(type), mode_Iu);
                ir_node   *cnst = new_rd_Const(dbg, irg, tv);
-               ir_node   *mul  = new_rd_Mul(dbg, block, get_Free_size(free),
+               ir_node   *mul  = new_rd_Mul(dbg, block, get_Free_count(free),
                                             cnst, mode_Iu);
                size = mul;
        } else {
-               size = get_Free_size(free);
+               size = get_Free_count(free);
        }
 
        stack_alignment = 1 << arch_env->stack_alignment;
@@ -1140,9 +1151,10 @@ static ir_type *compute_arg_type(ir_graph *irg, be_abi_call_t *call,
                                                                 ir_type *method_type, ir_entity ***param_map)
 {
        struct obstack *obst = be_get_be_obst(irg);
-       ir_type *frame_type      = get_irg_frame_type(irg);
-       size_t   n_params        = get_method_n_params(method_type);
-       size_t   n_frame_members = get_compound_n_members(frame_type);
+       ir_type   *frame_type      = get_irg_frame_type(irg);
+       size_t     n_params        = get_method_n_params(method_type);
+       size_t     n_frame_members = get_compound_n_members(frame_type);
+       ir_entity *va_start_entity = NULL;
        size_t   f;
        int      ofs  = 0;
 
@@ -1155,21 +1167,24 @@ static ir_type *compute_arg_type(ir_graph *irg, be_abi_call_t *call,
 
        /* collect existing entities for value_param_types */
        for (f = n_frame_members; f > 0; ) {
-               ir_entity         *entity = get_compound_member(frame_type, --f);
-               size_t             num;
-               be_abi_call_arg_t *arg;
+               ir_entity *entity = get_compound_member(frame_type, --f);
+               size_t     num;
 
                set_entity_link(entity, NULL);
                if (!is_parameter_entity(entity))
                        continue;
                num = get_entity_parameter_number(entity);
+               if (num == IR_VA_START_PARAMETER_NUMBER) {
+                       /* move entity to new arg_type */
+                       set_entity_owner(entity, res);
+                       va_start_entity = entity;
+                       continue;
+               }
                assert(num < n_params);
                if (map[num] != NULL)
                        panic("multiple entities for parameter %u in %+F found", f, irg);
 
-               arg = get_call_arg(call, 0, num, 1);
-               if (!arg->on_stack) {
-                       map[num] = NULL;
+               if (num != n_params && !get_call_arg(call, 0, num, 1)->on_stack) {
                        /* don't move this entity */
                        continue;
                }
@@ -1199,8 +1214,12 @@ static ir_type *compute_arg_type(ir_graph *irg, be_abi_call_t *call,
                ofs += get_type_size_bytes(param_type);
                arg->stack_ent = entity;
        }
+       if (va_start_entity != NULL) {
+               set_entity_offset(va_start_entity, ofs);
+       }
        set_type_size_bytes(res, ofs);
        set_type_state(res, layout_fixed);
+
        return res;
 }
 
@@ -1353,7 +1372,6 @@ static ir_node *create_be_return(be_abi_irg_t *env, ir_node *irn, ir_node *bl,
 }
 
 typedef struct lower_frame_sels_env_t {
-       ir_entity   **value_param_list;          /**< the list of all value param entities */
        ir_node      *frame;                     /**< the current frame */
        const arch_register_class_t *sp_class;   /**< register class of the stack pointer */
        const arch_register_class_t *link_class; /**< register class of the link pointer */
@@ -1378,111 +1396,12 @@ static void lower_frame_sels_walker(ir_node *irn, void *data)
                        ir_node      *bl  = get_nodes_block(irn);
                        ir_node      *nw;
 
-                       if (is_parameter_entity(ent) && get_entity_link(ent) == NULL) {
-                               /* replace by its copy from the argument type */
-                               ARR_APP1(ir_entity*, ctx->value_param_list, ent);
-                               /* just a mark */
-                               set_entity_link(ent, ctx->value_param_list);
-                       }
-
                        nw = be_new_FrameAddr(ctx->sp_class, bl, ctx->frame, ent);
                        exchange(irn, nw);
                }
        }
 }
 
-/**
- * Check if a value parameter is transmitted as a register.
- * This might happen if the address of an parameter is taken which is
- * transmitted in registers.
- *
- * Note that on some architectures this case must be handled specially
- * because the place of the backing store is determined by their ABI.
- *
- * In the default case we move the entity to the frame type and create
- * a backing store into the first block.
- */
-static void fix_address_of_parameter_access(be_abi_irg_t *env, ir_graph *irg,
-                                            ir_entity **value_param_list)
-{
-       be_abi_call_t    *call     = env->call;
-       const arch_env_t *arch_env = be_get_irg_arch_env(irg);
-       size_t         n = ARR_LEN(value_param_list);
-       size_t         i;
-       bool           need_fixes = false;
-       ir_node       *first_store = NULL;
-       ir_node       *start_bl;
-       ir_node       *frame;
-       ir_node       *imem;
-       ir_node       *mem;
-       ir_node       *args;
-
-       for (i = 0; i < n; ++i) {
-               ir_entity         *entity = value_param_list[i];
-               size_t             pos    = get_entity_parameter_number(entity);
-               be_abi_call_arg_t *arg    = get_call_arg(call, 0, pos, 1);
-
-               if (arg->in_reg) {
-                       need_fixes = true;
-                       DBG((dbg, LEVEL_2, "\targ #%d need backing store\n", pos));
-               }
-       }
-       if (!need_fixes)
-               return;
-
-       /* ok, change the graph */
-       start_bl = get_irg_start_block(irg);
-
-       /* now create backing stores */
-       frame = get_irg_frame(irg);
-       imem  = get_irg_initial_mem(irg);
-       mem   = imem;
-       args  = get_irg_args(irg);
-       for (i = 0; i < n; ++i) {
-               ir_entity         *entity = value_param_list[i];
-               size_t             pos    = get_entity_parameter_number(entity);
-               be_abi_call_arg_t *arg    = get_call_arg(call, 0, pos, 1);
-               ir_node           *addr;
-
-               if (!arg->in_reg)
-                       continue;
-
-               /* address for the backing store */
-               addr = be_new_FrameAddr(arch_env->sp->reg_class, start_bl, frame, entity);
-
-               if (entity->attr.parameter.doubleword_low_mode != NULL) {
-                       ir_mode *mode      = entity->attr.parameter.doubleword_low_mode;
-                       ir_node *val0      = new_r_Proj(args, mode, pos);
-                       ir_node *val1      = new_r_Proj(args, mode, pos+1);
-                       ir_node *store0    = new_r_Store(start_bl, mem, addr, val0, cons_none);
-                       ir_node *mem0      = new_r_Proj(store0, mode_M, pn_Store_M);
-                       size_t   offset    = get_mode_size_bits(mode)/8;
-                       ir_mode *addr_mode = get_irn_mode(addr);
-                       ir_node *cnst      = new_r_Const_long(irg, addr_mode, offset);
-                       ir_node *next_addr = new_r_Add(start_bl, addr, cnst, addr_mode);
-                       ir_node *store1    = new_r_Store(start_bl, mem0, next_addr, val1, cons_none);
-                       mem = new_r_Proj(store1, mode_M, pn_Store_M);
-                       if (first_store == NULL)
-                               first_store = store0;
-               } else {
-                       ir_type *tp   = get_entity_type(entity);
-                       ir_mode *mode = get_type_mode(tp);
-
-                       /* the backing store itself */
-                       ir_node *val   = new_r_Proj(args, mode, pos);
-                       ir_node *store = new_r_Store(start_bl, mem, addr, val, cons_none);
-                       mem = new_r_Proj(store, mode_M, pn_Store_M);
-
-                       if (first_store == NULL)
-                               first_store = store;
-               }
-       }
-
-       assert(mem != imem);
-       edges_reroute(imem, mem);
-       set_Store_mem(first_store, imem);
-}
-
 /**
  * The start block has no jump, instead it has an initial exec Proj.
  * The backend wants to handle all blocks the same way, so we replace
@@ -1514,64 +1433,6 @@ static void fix_start_block(ir_graph *irg)
        }
 }
 
-/**
- * Update the entity of Sels to the outer value parameters.
- */
-static void update_outer_frame_sels(ir_node *irn, void *env)
-{
-       lower_frame_sels_env_t *ctx = (lower_frame_sels_env_t*)env;
-       ir_node                *ptr;
-       ir_entity              *ent;
-
-       if (! is_Sel(irn))
-               return;
-       ptr = get_Sel_ptr(irn);
-       if (! is_arg_Proj(ptr))
-               return;
-       if (get_Proj_proj(ptr) != ctx->static_link_pos)
-               return;
-       ent = get_Sel_entity(irn);
-
-       if (is_parameter_entity(ent)) {
-               /* check, if we have not seen this entity before */
-               if (get_entity_link(ent) == NULL) {
-                       ARR_APP1(ir_entity*, ctx->value_param_list, ent);
-                       /* just a mark */
-                       set_entity_link(ent, ctx->value_param_list);
-               }
-       }
-}
-
-/**
- * Fix access to outer local variables.
- */
-static void fix_outer_variable_access(be_abi_irg_t *env,
-                                      lower_frame_sels_env_t *ctx)
-{
-       int      i;
-       ir_graph *irg;
-       (void) env;
-
-       for (i = get_class_n_members(ctx->frame_tp) - 1; i >= 0; --i) {
-               ir_entity *ent = get_class_member(ctx->frame_tp, i);
-
-               if (! is_method_entity(ent))
-                       continue;
-
-               irg = get_entity_irg(ent);
-               if (irg == NULL)
-                       continue;
-
-               /*
-                * FIXME: find the number of the static link parameter
-                * for now we assume 0 here
-                */
-               ctx->static_link_pos = 0;
-
-               irg_walk_graph(irg, NULL, update_outer_frame_sels, ctx);
-       }
-}
-
 /**
  * Modify the irg itself and the frame type.
  */
@@ -1615,7 +1476,6 @@ static void modify_irg(ir_graph *irg)
        arg_type = compute_arg_type(irg, call, method_type, &param_map);
 
        /* Convert the Sel nodes in the irg to frame addr nodes: */
-       ctx.value_param_list = NEW_ARR_F(ir_entity*, 0);
        ctx.frame            = get_irg_frame(irg);
        ctx.sp_class         = arch_env->sp->reg_class;
        ctx.link_class       = arch_env->link_class;
@@ -1626,8 +1486,6 @@ static void modify_irg(ir_graph *irg)
                default_layout_compound_type(ctx.frame_tp);
        }
 
-       irg_walk_graph(irg, lower_frame_sels_walker, NULL, &ctx);
-
        /* align stackframe to 4 byte */
        frame_size = get_type_size_bytes(ctx.frame_tp);
        if (frame_size % 4 != 0) {
@@ -1639,24 +1497,10 @@ static void modify_irg(ir_graph *irg)
        n_params = get_method_n_params(method_type);
        args     = OALLOCNZ(obst, ir_node*, n_params);
 
-       /*
-        * for inner function we must now fix access to outer frame entities.
-        */
-       fix_outer_variable_access(env, &ctx);
+       be_add_parameter_entity_stores(irg);
 
-       /* Check if a value parameter is transmitted as a register.
-        * This might happen if the address of an parameter is taken which is
-        * transmitted in registers.
-        *
-        * Note that on some architectures this case must be handled specially
-        * because the place of the backing store is determined by their ABI.
-        *
-        * In the default case we move the entity to the frame type and create
-        * a backing store into the first block.
-        */
-       fix_address_of_parameter_access(env, irg, ctx.value_param_list);
+       irg_walk_graph(irg, lower_frame_sels_walker, NULL, &ctx);
 
-       DEL_ARR_F(ctx.value_param_list);
        irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
 
        /* Fill the argument vector */
@@ -1744,7 +1588,7 @@ static void modify_irg(ir_graph *irg)
 
        /* create a new initial memory proj */
        assert(is_Proj(old_mem));
-       arch_set_out_register_req(env->start, 0, arch_no_register_req);
+       arch_set_irn_register_req_out(env->start, 0, arch_no_register_req);
        new_mem_proj = new_r_Proj(env->start, mode_M, 0);
        mem = new_mem_proj;
        set_irg_initial_mem(irg, mem);