Correct three bugs in bemit_incsp(): Offset 0 must emit no code, get_signed_imm_size...
[libfirm] / ir / be / beabi.c
index 1a6a3e3..00dee8f 100644 (file)
 
 #include "be.h"
 #include "beabi.h"
-#include "bearch_t.h"
-#include "benode_t.h"
+#include "bearch.h"
+#include "benode.h"
 #include "belive_t.h"
-#include "besched_t.h"
-#include "beirg_t.h"
+#include "besched.h"
+#include "beirg.h"
 #include "bessaconstr.h"
 
 typedef struct _be_abi_call_arg_t {
@@ -93,7 +93,7 @@ struct _be_abi_irg_t {
        ir_node              *init_sp;      /**< The node representing the stack pointer
                                                 at the start of the function. */
 
-       ir_node              *reg_params;   /**< The reg params node. */
+       ir_node              *start;        /**< The be_Start params node. */
        pmap                 *regs;         /**< A map of all callee-save and ignore regs to
                                                 their Projs to the RegParams node. */
 
@@ -106,8 +106,7 @@ struct _be_abi_irg_t {
 
        ir_node              **calls;       /**< flexible array containing all be_Call nodes */
 
-       arch_register_req_t  sp_req;
-       arch_register_req_t  sp_cls_req;
+       arch_register_req_t  *sp_req;
 
        be_stack_layout_t    frame;         /**< The stack frame model. */
 
@@ -816,7 +815,7 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
 
                /* create the Keep for the caller save registers */
                in   = (ir_node **) obstack_finish(obst);
-               keep = be_new_Keep(NULL, bl, n, in);
+               keep = be_new_Keep(bl, n, in);
                for (i = 0; i < n; ++i) {
                        const arch_register_t *reg = get_irn_link(in[i]);
                        be_node_set_reg_class_in(keep, i, reg->reg_class);
@@ -1209,7 +1208,7 @@ static void process_ops_in_block(ir_node *bl, void *data)
                if (curr_sp != env->init_sp &&
                    !(is_Proj(curr_sp) && be_is_Call(get_Proj_pred(curr_sp)))) {
                        nodes[0] = curr_sp;
-                       keep     = be_new_Keep(env->arch_env->sp->reg_class, bl, 1, nodes);
+                       keep     = be_new_Keep(bl, 1, nodes);
                        pmap_insert(env->keep_map, bl, keep);
                }
        }
@@ -1261,7 +1260,7 @@ static ir_type *compute_arg_type(be_abi_irg_t *env, be_abi_call_t *call,
        ident *id = get_entity_ident(get_irg_entity(env->birg->irg));
        ir_entity **map;
 
-       *param_map = map = obstack_alloc(&env->obst, n * sizeof(ir_entity *));
+       *param_map = map = OALLOCN(&env->obst, ir_entity*, n);
        res = new_type_struct(id_mangle_u(id, new_id_from_chars("arg_type", 8)));
        for (i = 0; i < n; ++i, curr += inc) {
                ir_type *param_type    = get_method_param_type(method_type, curr);
@@ -1316,7 +1315,7 @@ static reg_node_map_t *reg_map_to_arr(struct obstack *obst, pmap *reg_map)
        pmap_entry *ent;
        int n = pmap_count(reg_map);
        int i = 0;
-       reg_node_map_t *res = obstack_alloc(obst, n * sizeof(res[0]));
+       reg_node_map_t *res = OALLOCN(obst, reg_node_map_t, n);
 
        foreach_pmap(reg_map, ent) {
                res[i].reg = ent->key;
@@ -1354,14 +1353,16 @@ static ir_node *create_barrier(be_abi_irg_t *env, ir_node *bl, ir_node **mem, pm
        obstack_free(&env->obst, in);
 
        for (n = 0; n < n_regs; ++n) {
-               ir_node                   *pred     = rm[n].irn;
-               const arch_register_t     *reg      = rm[n].reg;
-               arch_register_type_t       add_type = 0;
-               ir_node                   *proj;
+               ir_node               *pred     = rm[n].irn;
+               const arch_register_t *reg      = rm[n].reg;
+               arch_register_type_t   add_type = 0;
+               ir_node               *proj;
+               const backend_info_t  *info;
 
                /* stupid workaround for now... as not all nodes report register
                 * requirements. */
-               if (!is_Phi(pred)) {
+               info = be_get_info(skip_Proj(pred));
+               if (info != NULL && info->out_infos != NULL) {
                        const arch_register_req_t *ireq = arch_get_register_req_out(pred);
                        if (ireq->type & arch_register_req_type_ignore)
                                add_type |= arch_register_req_type_ignore;
@@ -1411,7 +1412,7 @@ static ir_node *create_be_return(be_abi_irg_t *env, ir_node *irn, ir_node *bl,
        ir_node **in;
        ir_node *stack;
        const arch_register_t **regs;
-       pmap_entry *ent ;
+       pmap_entry *ent;
 
        /*
                get the valid stack node in this block.
@@ -1454,8 +1455,8 @@ static ir_node *create_be_return(be_abi_irg_t *env, ir_node *irn, ir_node *bl,
        */
        in_max = pmap_count(reg_map) + n_res + 2;
 
-       in   = obstack_alloc(&env->obst, in_max * sizeof(in[0]));
-       regs = obstack_alloc(&env->obst, in_max * sizeof(regs[0]));
+       in   = OALLOCN(&env->obst, ir_node*,               in_max);
+       regs = OALLOCN(&env->obst, arch_register_t const*, in_max);
 
        in[0]   = mem;
        in[1]   = be_abi_reg_map_get(reg_map, arch_env->sp);
@@ -1743,8 +1744,8 @@ static void fix_start_block(ir_graph *irg)
                        continue;
                if (block != start_block) {
                        ir_node *jmp = new_r_Jmp(start_block);
-
                        set_Block_cfgpred(block, get_edge_src_pos(edge), jmp);
+                       set_irg_initial_exec(irg, jmp);
                        return;
                }
        }
@@ -1827,7 +1828,6 @@ static void modify_irg(be_abi_irg_t *env)
        const arch_env_t *arch_env= env->birg->main_env->arch_env;
        const arch_register_t *sp = arch_env->sp;
        ir_graph *irg             = env->birg->irg;
-       ir_node *start_bl;
        ir_node *end;
        ir_node *old_mem;
        ir_node *new_mem_proj;
@@ -1837,11 +1837,12 @@ static void modify_irg(be_abi_irg_t *env)
        int n_params;
        int i, n;
        unsigned j;
+       unsigned frame_size;
 
        reg_node_map_t *rm;
        const arch_register_t *fp_reg;
        ir_node *frame_pointer;
-       ir_node *reg_params_bl;
+       ir_node *start_bl;
        ir_node **args;
        ir_node *arg_tuple;
        const ir_edge_t *edge;
@@ -1889,12 +1890,16 @@ static void modify_irg(be_abi_irg_t *env)
 
        /* fix the frame type layout again */
        set_type_state(ctx.frame_tp, layout_fixed);
+       /* align stackframe to 4 byte */
+       frame_size = get_type_size_bytes(ctx.frame_tp);
+       if (frame_size % 4 != 0) {
+               set_type_size_bytes(ctx.frame_tp, frame_size + 4 - (frame_size % 4));
+       }
 
        env->regs  = pmap_create();
 
        n_params = get_method_n_params(method_type);
-       args     = obstack_alloc(&env->obst, n_params * sizeof(args[0]));
-       memset(args, 0, n_params * sizeof(args[0]));
+       args     = OALLOCNZ(&env->obst, ir_node*, n_params);
 
        /*
         * for inner function we must now fix access to outer frame entities.
@@ -1955,11 +1960,13 @@ static void modify_irg(be_abi_irg_t *env)
                }
        }
 
+       /* handle start block here (place a jump in the block) */
+       fix_start_block(irg);
+
        pmap_insert(env->regs, (void *) sp, NULL);
        pmap_insert(env->regs, (void *) arch_env->bp, NULL);
-       reg_params_bl   = get_irg_start_block(irg);
-       env->reg_params = be_new_RegParams(reg_params_bl, pmap_count(env->regs));
-       add_irn_dep(env->reg_params, get_irg_start(irg));
+       start_bl   = get_irg_start_block(irg);
+       env->start = be_new_Start(start_bl, pmap_count(env->regs) + 1);
 
        /*
         * make proj nodes for the callee save registers.
@@ -1981,9 +1988,9 @@ static void modify_irg(be_abi_irg_t *env)
                        add_type |= arch_register_req_type_produces_sp | arch_register_req_type_ignore;
 
                assert(nr >= 0);
-               proj = new_r_Proj(reg_params_bl, env->reg_params, mode, nr);
+               proj = new_r_Proj(start_bl, env->start, mode, nr + 1);
                pmap_insert(env->regs, (void *) reg, proj);
-               be_set_constr_single_reg_out(env->reg_params, nr, reg, add_type);
+               be_set_constr_single_reg_out(env->start, nr + 1, reg, add_type);
                arch_set_irn_register(proj, reg);
 
                DBG((dbg, LEVEL_2, "\tregister save proj #%d -> reg %s\n", nr, reg->name));
@@ -1992,10 +1999,10 @@ static void modify_irg(be_abi_irg_t *env)
 
        /* create a new initial memory proj */
        assert(is_Proj(old_mem));
-       new_mem_proj = new_r_Proj(get_nodes_block(old_mem),
-                                 new_r_Unknown(irg, mode_T), mode_M,
-                                 get_Proj_proj(old_mem));
+       arch_set_out_register_req(env->start, 0, arch_no_register_req);
+       new_mem_proj = new_r_Proj(start_bl, env->start, mode_M, 0);
        mem = new_mem_proj;
+       set_irg_initial_mem(irg, mem);
 
        /* Generate the Prologue */
        fp_reg = call->cb->prologue(env->cb, &mem, env->regs, &env->frame.initial_bias);
@@ -2003,7 +2010,6 @@ static void modify_irg(be_abi_irg_t *env)
        /* do the stack allocation BEFORE the barrier, or spill code
           might be added before it */
        env->init_sp = be_abi_reg_map_get(env->regs, sp);
-       start_bl     = get_irg_start_block(irg);
        env->init_sp = be_new_IncSP(sp, start_bl, env->init_sp, BE_STACK_FRAME_SIZE_EXPAND, 0);
        be_abi_reg_map_set(env->regs, sp, env->init_sp);
 
@@ -2017,7 +2023,6 @@ static void modify_irg(be_abi_irg_t *env)
        pset_insert_ptr(env->ignore_regs, fp_reg);
 
        /* rewire old mem users to new mem */
-       set_Proj_pred(new_mem_proj, get_Proj_pred(old_mem));
        exchange(old_mem, mem);
 
        set_irg_initial_mem(irg, mem);
@@ -2040,18 +2045,18 @@ static void modify_irg(be_abi_irg_t *env)
                        if (arg->in_reg) {
                                repl = pmap_get(env->regs, (void *) arg->reg);
                        } else if (arg->on_stack) {
-                               ir_node *addr = be_new_FrameAddr(sp->reg_class, reg_params_bl, frame_pointer, arg->stack_ent);
+                               ir_node *addr = be_new_FrameAddr(sp->reg_class, start_bl, frame_pointer, arg->stack_ent);
 
                                /* For atomic parameters which are actually used, we create a Load node. */
                                if (is_atomic_type(param_type) && get_irn_n_edges(args[i]) > 0) {
                                        ir_mode *mode      = get_type_mode(param_type);
                                        ir_mode *load_mode = arg->load_mode;
 
-                                       ir_node *load = new_r_Load(reg_params_bl, new_NoMem(), addr, load_mode, cons_floats);
-                                       repl = new_r_Proj(reg_params_bl, load, load_mode, pn_Load_res);
+                                       ir_node *load = new_r_Load(start_bl, new_NoMem(), addr, load_mode, cons_floats);
+                                       repl = new_r_Proj(start_bl, load, load_mode, pn_Load_res);
 
                                        if (mode != load_mode) {
-                                               repl = new_r_Conv(reg_params_bl, repl, mode);
+                                               repl = new_r_Conv(start_bl, repl, mode);
                                        }
                                } else {
                                        /* The stack parameter is not primitive (it is a struct or array),
@@ -2094,9 +2099,6 @@ static void modify_irg(be_abi_irg_t *env)
           the code is dead and will never be executed. */
 
        obstack_free(&env->obst, args);
-
-       /* handle start block here (place a jump in the block) */
-       fix_start_block(irg);
 }
 
 /** Fix the state inputs of calls that still hang on unknowns */
@@ -2298,6 +2300,7 @@ be_abi_irg_t *be_abi_introduce(be_irg_t *birg)
        ir_node *dummy;
        optimization_state_t state;
        unsigned *limited_bitset;
+       arch_register_req_t *sp_req;
 
        be_omit_fp      = birg->main_env->options->omit_fp;
        be_omit_leaf_fp = birg->main_env->options->omit_leaf_fp;
@@ -2314,18 +2317,20 @@ be_abi_irg_t *be_abi_introduce(be_irg_t *birg)
        env->dce_survivor = new_survive_dce();
        env->birg         = birg;
 
-       env->sp_req.type    = arch_register_req_type_limited;
-       env->sp_req.cls     = arch_register_get_class(env->arch_env->sp);
-       limited_bitset      = rbitset_obstack_alloc(&env->obst, env->sp_req.cls->n_regs);
+       sp_req = OALLOCZ(&env->obst, arch_register_req_t);
+       env->sp_req = sp_req;
+
+       sp_req->type = arch_register_req_type_limited
+                    | arch_register_req_type_produces_sp;
+       sp_req->cls  = arch_register_get_class(env->arch_env->sp);
+
+       limited_bitset = rbitset_obstack_alloc(&env->obst, sp_req->cls->n_regs);
        rbitset_set(limited_bitset, arch_register_get_index(env->arch_env->sp));
-       env->sp_req.limited = limited_bitset;
+       sp_req->limited = limited_bitset;
        if (env->arch_env->sp->type & arch_register_type_ignore) {
-               env->sp_req.type |= arch_register_req_type_ignore;
+               sp_req->type |= arch_register_req_type_ignore;
        }
 
-       env->sp_cls_req.type  = arch_register_req_type_normal;
-       env->sp_cls_req.cls   = arch_register_get_class(env->arch_env->sp);
-
        /* Beware: later we replace this node by the real one, ensure it is not CSE'd
           to another Unknown or the stack pointer gets used */
        save_optimization_state(&state);
@@ -2446,10 +2451,15 @@ typedef struct fix_stack_walker_env_t {
  */
 static void collect_stack_nodes_walker(ir_node *node, void *data)
 {
+       ir_node                   *insn = node;
        fix_stack_walker_env_t    *env = data;
        const arch_register_req_t *req;
 
-       if (get_irn_mode(node) == mode_T)
+       if (is_Proj(node)) {
+               insn = get_Proj_pred(node);
+       }
+
+       if (arch_irn_get_n_outs(insn) == 0)
                return;
 
        req = arch_get_register_req_out(node);
@@ -2502,7 +2512,7 @@ void be_abi_fix_stack_nodes(be_abi_irg_t *env)
        len = ARR_LEN(phis);
        for (i = 0; i < len; ++i) {
                ir_node *phi = phis[i];
-               be_set_phi_reg_req(phi, &env->sp_req, arch_register_req_type_produces_sp);
+               be_set_phi_reg_req(phi, env->sp_req);
                arch_set_irn_register(phi, env->arch_env->sp);
        }
        be_ssa_construction_destroy(&senv);