get_Call_n_params: use int for consistency
[libfirm] / ir / be / beabi.c
index 9966c83..e56c13e 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"
@@ -109,7 +108,7 @@ static int be_omit_fp = 1;
 
 static ir_node *be_abi_reg_map_get(pmap *map, const arch_register_t *reg)
 {
-       return (ir_node*)pmap_get(map, reg);
+       return pmap_get(ir_node, map, reg);
 }
 
 static void be_abi_reg_map_set(pmap *map, const arch_register_t* reg,
@@ -186,7 +185,7 @@ static be_abi_call_arg_t *get_call_arg(be_abi_call_t *call, int is_res, int pos,
 
        hash = is_res * 128 + pos;
 
-       return (be_abi_call_arg_t*)set_find(call->params, &arg, sizeof(arg), hash);
+       return set_find(be_abi_call_arg_t, call->params, &arg, sizeof(arg), hash);
 }
 
 /**
@@ -197,11 +196,11 @@ static void remember_call_arg(be_abi_call_arg_t *arg, be_abi_call_t *call, be_ab
        unsigned hash = arg->is_res * 128 + arg->pos;
        if (context & ABI_CONTEXT_CALLEE) {
                arg->callee = 1;
-               set_insert(call->params, arg, sizeof(*arg), hash);
+               (void)set_insert(be_abi_call_arg_t, call->params, arg, sizeof(*arg), hash);
        }
        if (context & ABI_CONTEXT_CALLER) {
                arg->callee = 0;
-               set_insert(call->params, arg, sizeof(*arg), hash);
+               (void)set_insert(be_abi_call_arg_t, call->params, arg, sizeof(*arg), hash);
        }
 }
 
@@ -314,13 +313,11 @@ static void be_abi_call_free(be_abi_call_t *call)
  * @param args      the stack argument layout type
  * @param between   the between layout type
  * @param locals    the method frame type
- * @param param_map an array mapping method argument positions to the stack argument type
  *
  * @return the initialized stack layout
  */
 static be_stack_layout_t *stack_frame_init(be_stack_layout_t *frame, ir_type *args,
-                                           ir_type *between, ir_type *locals,
-                                           ir_entity *param_map[])
+                                           ir_type *between, ir_type *locals)
 {
        frame->arg_type       = args;
        frame->between_type   = between;
@@ -328,7 +325,6 @@ static be_stack_layout_t *stack_frame_init(be_stack_layout_t *frame, ir_type *ar
        frame->initial_offset = 0;
        frame->initial_bias   = 0;
        frame->order[1]       = between;
-       frame->param_map      = param_map;
 
        /* typical decreasing stack: locals have the
         * lowest addresses, arguments the highest */
@@ -369,9 +365,7 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
        const arch_register_t *sp  = arch_env->sp;
        be_abi_call_t *call        = be_abi_call_new(sp->reg_class);
        ir_mode *mach_mode         = sp->reg_class->mode;
-       int no_alloc               = call->flags.bits.frame_is_setup_on_call;
        int n_res                  = get_method_n_ress(call_tp);
-       int do_seq                 = call->flags.bits.store_args_sequential && !no_alloc;
 
        ir_node *res_proj  = NULL;
        int n_reg_params   = 0;
@@ -384,10 +378,9 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
        ir_node               **in;
        ir_node               **res_projs;
        int                     n_reg_results = 0;
-       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;
@@ -397,7 +390,7 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
        arch_env_get_call_abi(arch_env, call_tp, call);
 
        /* Insert code to put the stack arguments on the stack. */
-       assert(get_Call_n_params(irn) == n_params);
+       assert((size_t)get_Call_n_params(irn) == n_params);
        stack_param_idx = ALLOCAN(int, n_params);
        for (p = 0; p < n_params; ++p) {
                be_abi_call_arg_t *arg = get_call_arg(call, 0, p, 0);
@@ -431,9 +424,7 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
         * Note: we also have to do this for stack_size == 0, because we may have
         * to adjust stack alignment for the call.
         */
-       if (!do_seq && !no_alloc) {
-               curr_sp = be_new_IncSP(sp, bl, curr_sp, stack_size, 1);
-       }
+       curr_sp = be_new_IncSP(sp, bl, curr_sp, stack_size, 1);
 
        dbgi = get_irn_dbg_info(irn);
        /* If there are some parameters which shall be passed on the stack. */
@@ -442,20 +433,8 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
                ir_node **in       = ALLOCAN(ir_node*, n_stack_params+1);
                unsigned  n_in     = 0;
 
-               /* push params in reverse direction because stack grows downwards */
-               if (do_seq) {
-                       for (i = 0; i < n_stack_params >> 1; ++i) {
-                               int other  = n_stack_params - i - 1;
-                               int tmp    = stack_param_idx[i];
-                               stack_param_idx[i]     = stack_param_idx[other];
-                               stack_param_idx[other] = tmp;
-                       }
-               }
-
                curr_mem = get_Call_mem(irn);
-               if (! do_seq) {
-                       in[n_in++] = curr_mem;
-               }
+               in[n_in++] = curr_mem;
 
                for (i = 0; i < n_stack_params; ++i) {
                        int p                  = stack_param_idx[i];
@@ -471,30 +450,23 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
                         * the stack pointer for the next must be incremented,
                         * and the memory value propagated.
                         */
-                       if (do_seq) {
-                               curr_ofs = 0;
-                               addr = curr_sp = be_new_IncSP(sp, bl, curr_sp,
-                                                             param_size + arg->space_before, 0);
-                               add_irn_dep(curr_sp, curr_mem);
-                       } else {
-                               curr_ofs += arg->space_before;
-                               curr_ofs =  round_up2(curr_ofs, arg->alignment);
-
-                               /* Make the expression to compute the argument's offset. */
-                               if (curr_ofs > 0) {
-                                       ir_mode *constmode = mach_mode;
-                                       if (mode_is_reference(mach_mode)) {
-                                               constmode = mode_Is;
-                                       }
-                                       addr = new_r_Const_long(irg, constmode, curr_ofs);
-                                       addr = new_r_Add(bl, curr_sp, addr, mach_mode);
+                       curr_ofs += arg->space_before;
+                       curr_ofs =  round_up2(curr_ofs, arg->alignment);
+
+                       /* Make the expression to compute the argument's offset. */
+                       if (curr_ofs > 0) {
+                               ir_mode *constmode = mach_mode;
+                               if (mode_is_reference(mach_mode)) {
+                                       constmode = mode_Is;
                                }
+                               addr = new_r_Const_long(irg, constmode, curr_ofs);
+                               addr = new_r_Add(bl, curr_sp, addr, mach_mode);
                        }
 
                        /* Insert a store for primitive arguments. */
                        if (is_atomic_type(param_type)) {
                                ir_node *nomem     = get_irg_no_mem(irg);
-                               ir_node *mem_input = do_seq ? curr_mem : nomem;
+                               ir_node *mem_input = nomem;
                                ir_node *store     = new_rd_Store(dbgi, bl, mem_input, addr, param, cons_none);
                                mem   = new_r_Proj(store, mode_M, pn_Store_M);
                        } else {
@@ -508,36 +480,17 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
 
                        curr_ofs += param_size;
 
-                       if (do_seq)
-                               curr_mem = mem;
-                       else
-                               in[n_in++] = mem;
+                       in[n_in++] = mem;
                }
 
                /* We need the sync only, if we didn't build the stores sequentially. */
-               if (! do_seq) {
-                       if (n_stack_params >= 1) {
-                               curr_mem = new_r_Sync(bl, n_in, in);
-                       } else {
-                               curr_mem = get_Call_mem(irn);
-                       }
+               if (n_stack_params >= 1) {
+                       curr_mem = new_r_Sync(bl, n_in, in);
+               } else {
+                       curr_mem = get_Call_mem(irn);
                }
        }
 
-       /* 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) {
@@ -558,7 +511,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);
                                }
@@ -570,8 +523,7 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
        res_projs = ALLOCANZ(ir_node*, n_res);
 
        foreach_out_edge(irn, edge) {
-               const ir_edge_t *res_edge;
-               ir_node         *irn = get_edge_src_irn(edge);
+               ir_node *irn = get_edge_src_irn(edge);
 
                if (!is_Proj(irn) || get_Proj_proj(irn) != pn_Call_T_result)
                        continue;
@@ -783,9 +735,7 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
                }
        }
        /* Clean up the stack frame or revert alignment fixes if we allocated it */
-       if (! no_alloc) {
-               curr_sp = be_new_IncSP(sp, bl, curr_sp, -stack_size, 0);
-       }
+       curr_sp = be_new_IncSP(sp, bl, curr_sp, -stack_size, 0);
 
        be_abi_call_free(call);
 
@@ -842,7 +792,6 @@ static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp
        ir_type          *type      = get_Alloc_type(alloc);
        dbg_info         *dbg;
 
-       const ir_edge_t *edge;
        ir_node *new_alloc;
        ir_node *count;
        ir_node *size;
@@ -880,7 +829,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);
@@ -945,14 +894,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;
@@ -1030,7 +979,6 @@ static int cmp_call_dependency(const void *c1, const void *c2)
 
 /**
  * Walker: links all Call/Alloc/Free nodes to the Block they are contained.
- * Clears the irg_is_leaf flag if a Call is detected.
  */
 static void link_ops_in_block_walker(ir_node *irn, void *data)
 {
@@ -1043,9 +991,6 @@ static void link_ops_in_block_walker(ir_node *irn, void *data)
                ir_node *bl       = get_nodes_block(irn);
                void *save        = get_irn_link(bl);
 
-               if (code == iro_Call)
-                       env->call->flags.bits.irg_is_leaf = 0;
-
                set_irn_link(irn, save);
                set_irn_link(bl, irn);
        }
@@ -1142,7 +1087,6 @@ static void process_calls(ir_graph *irg)
 {
        be_abi_irg_t *abi = be_get_irg_abi(irg);
 
-       abi->call->flags.bits.irg_is_leaf = 1;
        irg_walk_graph(irg, firm_clear_link, link_ops_in_block_walker, abi);
 
        ir_heights = heights_new(irg);
@@ -1157,13 +1101,11 @@ static void process_calls(ir_graph *irg)
  *
  * @param call          the current call ABI
  * @param method_type   the method type
- * @param param_map     an array mapping method arguments to the stack layout
- *                      type
  *
  * @return the stack argument layout type
  */
 static ir_type *compute_arg_type(ir_graph *irg, be_abi_call_t *call,
-                                                                ir_type *method_type, ir_entity ***param_map)
+                                                                ir_type *method_type)
 {
        struct obstack *obst = be_get_be_obst(irg);
        ir_type   *frame_type      = get_irg_frame_type(irg);
@@ -1175,9 +1117,7 @@ static ir_type *compute_arg_type(ir_graph *irg, be_abi_call_t *call,
 
        ir_type *res;
        size_t i;
-       ir_entity **map;
-
-       *param_map = map = OALLOCNZ(obst, ir_entity*, n_params);
+       ir_entity **map = OALLOCNZ(obst, ir_entity*, n_params);
        res = new_type_struct(new_id_from_chars("arg_type", 8));
 
        /* collect existing entities for value_param_types */
@@ -1286,7 +1226,7 @@ static ir_node *create_be_return(be_abi_irg_t *env, ir_node *irn, ir_node *bl,
        const arch_env_t *arch_env = be_get_irg_arch_env(irg);
        dbg_info *dbgi;
        pmap *reg_map  = pmap_create();
-       ir_node *keep  = (ir_node*)pmap_get(env->keep_map, bl);
+       ir_node *keep  = pmap_get(ir_node, env->keep_map, bl);
        size_t in_max;
        ir_node *ret;
        int i, n;
@@ -1434,7 +1374,6 @@ static void fix_start_block(ir_graph *irg)
 
        /* merge start block with successor if possible */
        {
-               const ir_edge_t *edge;
                foreach_out_edge(jmp, edge) {
                        ir_node *succ = get_edge_src_irn(edge);
                        if (!is_Block(succ))
@@ -1477,10 +1416,8 @@ static void modify_irg(ir_graph *irg)
        ir_node *start_bl;
        ir_node **args;
        ir_node *arg_tuple;
-       const ir_edge_t *edge;
        ir_type *arg_type, *bet_type;
        lower_frame_sels_env_t ctx;
-       ir_entity **param_map;
 
        DBG((dbg, LEVEL_1, "introducing abi on %+F\n", irg));
 
@@ -1488,7 +1425,7 @@ static void modify_irg(ir_graph *irg)
 
        irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
 
-       arg_type = compute_arg_type(irg, call, method_type, &param_map);
+       arg_type = compute_arg_type(irg, call, method_type);
 
        /* Convert the Sel nodes in the irg to frame addr nodes: */
        ctx.frame            = get_irg_frame(irg);
@@ -1532,7 +1469,7 @@ static void modify_irg(ir_graph *irg)
        stack_layout->sp_relative = call->flags.bits.try_omit_fp;
        bet_type = call->cb->get_between_type(irg);
        stack_frame_init(stack_layout, arg_type, bet_type,
-                        get_irg_frame_type(irg), param_map);
+                        get_irg_frame_type(irg));
 
        /* Count the register params and add them to the number of Projs for the RegParams node */
        for (i = 0; i < n_params; ++i) {
@@ -1638,7 +1575,7 @@ static void modify_irg(ir_graph *irg)
                        param_type = get_method_param_type(method_type, nr);
 
                        if (arg->in_reg) {
-                               repl = (ir_node*)pmap_get(env->regs, arg->reg);
+                               repl = pmap_get(ir_node, env->regs, arg->reg);
                        } else if (arg->on_stack) {
                                ir_node *addr = be_new_FrameAddr(sp->reg_class, start_bl, frame_pointer, arg->stack_ent);
 
@@ -1759,7 +1696,7 @@ static ir_entity *create_trampoline(be_main_env_t *be, ir_entity *method)
  */
 static ir_entity *get_trampoline(be_main_env_t *env, ir_entity *method)
 {
-       ir_entity *result = (ir_entity*)pmap_get(env->ent_trampoline_map, method);
+       ir_entity *result = pmap_get(ir_entity, env->ent_trampoline_map, method);
        if (result == NULL) {
                result = create_trampoline(env, method);
                pmap_insert(env->ent_trampoline_map, method, result);
@@ -1784,7 +1721,7 @@ static ir_entity *create_pic_symbol(be_main_env_t *be, ir_entity *entity)
 
 static ir_entity *get_pic_symbol(be_main_env_t *env, ir_entity *entity)
 {
-       ir_entity *result = (ir_entity*)pmap_get(env->ent_pic_symbol_map, entity);
+       ir_entity *result = pmap_get(ir_entity, env->ent_pic_symbol_map, entity);
        if (result == NULL) {
                result = create_pic_symbol(env, entity);
                pmap_insert(env->ent_pic_symbol_map, entity, result);
@@ -1800,8 +1737,7 @@ static ir_entity *get_pic_symbol(be_main_env_t *env, ir_entity *entity)
  */
 static int can_address_relative(ir_entity *entity)
 {
-       return get_entity_visibility(entity) != ir_visibility_external
-               && !(get_entity_linkage(entity) & IR_LINKAGE_MERGE);
+       return entity_has_definition(entity) && !(get_entity_linkage(entity) & IR_LINKAGE_MERGE);
 }
 
 static ir_node *get_pic_base(ir_graph *irg)
@@ -1893,7 +1829,6 @@ void be_abi_introduce(ir_graph *irg)
 {
        be_abi_irg_t     *env         = XMALLOCZ(be_abi_irg_t);
        ir_node          *old_frame   = get_irg_frame(irg);
-       be_options_t     *options     = be_get_irg_options(irg);
        const arch_env_t *arch_env    = be_get_irg_arch_env(irg);
        ir_entity        *entity      = get_irg_entity(irg);
        ir_type          *method_type = get_entity_type(entity);
@@ -1919,7 +1854,7 @@ void be_abi_introduce(ir_graph *irg)
         */
        be_set_irg_abi(irg, env);
 
-       be_omit_fp      = options->omit_fp;
+       be_omit_fp        = be_options.omit_fp;
 
        env->keep_map     = pmap_create();
        env->call         = be_abi_call_new(arch_env->sp->reg_class);
@@ -1928,9 +1863,9 @@ void be_abi_introduce(ir_graph *irg)
        env->init_sp = dummy;
        env->calls   = NEW_ARR_F(ir_node*, 0);
 
-       edges_assure(irg);
+       assure_edges(irg);
 
-       if (options->pic) {
+       if (be_options.pic) {
                irg_walk_graph(irg, fix_pic_symconsts, NULL, env);
        }