fixed some bugs
[libfirm] / ir / be / ia32 / bearch_ia32.c
index f37988f..87c6d43 100644 (file)
@@ -190,8 +190,6 @@ static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
        irn = my_skip_proj(irn);
        if (is_cfop(irn))
                return arch_irn_class_branch;
-       else if (is_ia32_Call(irn))
-               return arch_irn_class_call;
        else if (is_ia32_irn(irn))
                return arch_irn_class_normal;
        else
@@ -207,15 +205,22 @@ static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
        }
 }
 
-static entity *ia32_get_frame_entity(const void *self, const ir_node *irn)
-{
-       /* TODO: Implement */
-       return NULL;
+static entity *ia32_get_frame_entity(const void *self, const ir_node *irn) {
+       return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
 }
 
 static void ia32_set_stack_bias(const void *self, ir_node *irn, int bias) {
-       if (is_ia32_use_frame(irn)) {
-               /* TODO: correct offset */
+       char buf[64];
+       const ia32_irn_ops_t *ops = self;
+
+       if (is_ia32_use_frame(irn) && bias != 0) {
+               ia32_am_flavour_t am_flav = get_ia32_am_flavour(irn);
+
+               DBG((ops->cg->mod, LEVEL_1, "stack biased %+F with %d\n", irn, bias));
+               snprintf(buf, sizeof(buf), "%d", bias);
+               add_ia32_am_offs(irn, buf);
+               am_flav |= ia32_O;
+               set_ia32_am_flavour(irn, am_flav);
        }
 }
 
@@ -260,17 +265,69 @@ static void ia32_prepare_graph(void *self) {
        dump_ir_block_graph_sched(cg->irg, "-transformed");
        edges_deactivate(cg->irg);
        edges_activate(cg->irg);
-       irg_walk_blkwise_graph(cg->irg, NULL, ia32_optimize_am, cg);
-       dump_ir_block_graph_sched(cg->irg, "-am");
+
+       if (cg->opt.doam) {
+               irg_walk_blkwise_graph(cg->irg, NULL, ia32_optimize_am, cg);
+               dump_ir_block_graph_sched(cg->irg, "-am");
+       }
 }
 
 
+/**
+ * Insert copies for all ia32 nodes where the should_be_same requirement
+ * is not fulfilled.
+ */
+static void ia32_finish_irg_walker(ir_node *irn, void *env) {
+       ia32_code_gen_t            *cg = env;
+       const ia32_register_req_t **reqs;
+       const arch_register_t      *out_reg, *in_reg;
+       int                         n_res, i;
+       ir_node                    *copy, *in_node, *block;
+
+       if (! is_ia32_irn(irn))
+               return;
+
+       /* nodes with destination address mode don't produce values */
+       if (get_ia32_op_type(irn) == ia32_AddrModeD)
+               return;
+
+       reqs  = get_ia32_out_req_all(irn);
+       n_res = get_ia32_n_res(irn);
+       block = get_nodes_block(irn);
+
+       /* check all OUT requirements, if there is a should_be_same */
+       for (i = 0; i < n_res; i++) {
+               if (arch_register_req_is(&(reqs[i]->req), should_be_same)) {
+                       /* get in and out register */
+                       out_reg = get_ia32_out_reg(irn, i);
+                       in_node = get_irn_n(irn, reqs[i]->same_pos);
+                       in_reg  = arch_get_irn_register(cg->arch_env, in_node);
+
+                       /* check if in and out register are equal */
+                       if (arch_register_get_index(out_reg) != arch_register_get_index(in_reg)) {
+                               DBG((cg->mod, LEVEL_1, "inserting copy for %+F in_pos %d\n", irn, reqs[i]->same_pos));
+
+                               /* create copy from in register */
+                               copy = be_new_Copy(arch_register_get_class(in_reg), cg->irg, block, in_node);
+
+                               /* destination is the out register */
+                               arch_set_irn_register(cg->arch_env, copy, out_reg);
+
+                               /* insert copy before the node into the schedule */
+                               sched_add_before(irn, copy);
+
+                               /* set copy as in */
+                               set_irn_n(irn, reqs[i]->same_pos, copy);
+                       }
+               }
+       }
+}
 
 /**
- * Stack reservation and StackParam lowering.
+ * Add Copy nodes for not fulfilled should_be_equal constraints
  */
 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
-
+       irg_walk_blkwise_graph(irg, NULL, ia32_finish_irg_walker, cg);
 }
 
 
@@ -285,89 +342,139 @@ static void ia32_before_ra(void *self) {
 }
 
 
+
 /**
- * Creates a Store for a Spill
+ * Transforms a be node into a Load.
  */
-static ir_node *ia32_lower_spill(void *self, ir_node *spill) {
-       ia32_code_gen_t *cg    = self;
-       ir_graph        *irg   = cg->irg;
-       dbg_info        *dbg   = get_irn_dbg_info(spill);
-       ir_node         *block = get_nodes_block(spill);
-       ir_node         *ptr   = get_irg_frame(irg);
-       ir_node         *val   = be_get_Spill_context(spill);
-       ir_node         *mem   = new_rd_NoMem(irg);
-       ir_mode         *mode  = get_irn_mode(spill);
-       entity          *ent   = be_get_spill_entity(spill);
-       unsigned         offs  = get_entity_offset_bytes(ent);
-       ir_node         *noreg, *res;
-       char             buf[64];
-
-       DB((cg->mod, LEVEL_1, "lower_spill: got offset %d for %+F\n", offs, ent));
+static void transform_to_Load(ia32_transform_env_t *env) {
+       ir_node *irn         = env->irn;
+       entity  *ent         = be_get_frame_entity(irn);
+       ir_mode *mode        = env->mode;
+       ir_node *noreg       = ia32_new_NoReg_gp(env->cg);
+       ir_node *nomem       = new_rd_NoMem(env->irg);
+       ir_node *sched_point = NULL;
+       ir_node *ptr         = get_irn_n(irn, 0);
+       ir_node *mem         = be_is_Reload(irn) ? get_irn_n(irn, 1) : nomem;
+       ir_node *new_op, *proj;
+       const arch_register_t *reg;
+
+       if (sched_is_scheduled(irn)) {
+               sched_point = sched_prev(irn);
+       }
 
        if (mode_is_float(mode)) {
-               noreg = ia32_new_NoReg_fp(cg);
-               res   = new_rd_ia32_fStore(dbg, irg, block, ptr, noreg, val, mem, mode);
+               new_op = new_rd_ia32_fLoad(env->dbg, env->irg, env->block, ptr, noreg, mem, mode_T);
        }
        else {
-               noreg = ia32_new_NoReg_gp(cg);
-               res   = new_rd_ia32_Store(dbg, irg, block, ptr, noreg, val, mem, mode);
+               new_op = new_rd_ia32_Load(env->dbg, env->irg, env->block, ptr, noreg, mem, mode_T);
        }
 
-       snprintf(buf, sizeof(buf), "%d", offs);
-       add_ia32_am_offs(res, buf);
+       set_ia32_am_support(new_op, ia32_am_Source);
+       set_ia32_op_type(new_op, ia32_AddrModeS);
+       set_ia32_am_flavour(new_op, ia32_B);
+       set_ia32_ls_mode(new_op, mode);
+       set_ia32_frame_ent(new_op, ent);
+       set_ia32_use_frame(new_op);
+
+       proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode, pn_Load_res);
+
+       if (sched_point) {
+               sched_add_after(sched_point, new_op);
+               sched_add_after(new_op, proj);
+
+               sched_remove(irn);
+       }
+
+       /* copy the register from the old node to the new Load */
+       reg = arch_get_irn_register(env->cg->arch_env, irn);
+       arch_set_irn_register(env->cg->arch_env, new_op, reg);
+
+       exchange(irn, proj);
 
-       return res;
 }
 
 /**
- * Create a Load for a Spill
+ * Transforms a be node into a Store.
  */
-static ir_node *ia32_lower_reload(void *self, ir_node *reload) {
-       ia32_code_gen_t *cg    = self;
-       ir_graph        *irg   = cg->irg;
-       dbg_info        *dbg   = get_irn_dbg_info(reload);
-       ir_node         *block = get_nodes_block(reload);
-       ir_node         *ptr   = get_irg_frame(irg);
-       ir_mode         *mode  = get_irn_mode(reload);
-       ir_node         *pred  = get_irn_n(reload, 0);
-       char             buf[64];
-       char            *ofs;
-       ir_node         *noreg, *res;
-
-       /* Get the offset to Load from. It can either be a Spill or a Store. */
-       if (be_is_Spill(pred)) {
-               entity   *ent  = be_get_spill_entity(pred);
-               unsigned  offs = get_entity_offset_bytes(ent);
-               DB((cg->mod, LEVEL_1, "lower_reload: got offset %d for %+F\n", offs, ent));
-
-               snprintf(buf, sizeof(buf), "%d", offs);
-       }
-       else if (is_ia32_Store(pred) || is_ia32_fStore(pred)) {
-               ofs = get_ia32_am_offs(pred);
-               strncpy(buf, ofs, sizeof(buf));
-               free(ofs);
-       }
-       else {
-               assert(0 && "unsupported Reload predecessor");
+static void transform_to_Store(ia32_transform_env_t *env) {
+       ir_node *irn   = env->irn;
+       entity  *ent   = be_get_frame_entity(irn);
+       ir_mode *mode  = env->mode;
+       ir_node *noreg = ia32_new_NoReg_gp(env->cg);
+       ir_node *nomem = new_rd_NoMem(env->irg);
+       ir_node *ptr   = get_irn_n(irn, 0);
+       ir_node *val   = get_irn_n(irn, 1);
+       ir_node *new_op, *proj;
+       ir_node *sched_point = NULL;
+
+       if (sched_is_scheduled(irn)) {
+               sched_point = sched_prev(irn);
        }
 
-       /* Create the Load */
        if (mode_is_float(mode)) {
-               noreg = ia32_new_NoReg_fp(cg);
-               res   = new_rd_ia32_fLoad(dbg, irg, block, ptr, noreg, pred, mode_T);
+               new_op = new_rd_ia32_fStore(env->dbg, env->irg, env->block, ptr, noreg, val, nomem, mode_T);
        }
        else {
-               noreg = ia32_new_NoReg_gp(cg);
-               res   = new_rd_ia32_Load(dbg, irg, block, ptr, noreg, pred, mode_T);
+               new_op = new_rd_ia32_Store(env->dbg, env->irg, env->block, ptr, noreg, val, nomem, mode_T);
        }
 
-       /* Set offset */
-       add_ia32_am_offs(res, buf);
+       set_ia32_am_support(new_op, ia32_am_Dest);
+       set_ia32_op_type(new_op, ia32_AddrModeD);
+       set_ia32_am_flavour(new_op, ia32_B);
+       set_ia32_ls_mode(new_op, get_irn_mode(val));
+       set_ia32_frame_ent(new_op, ent);
+       set_ia32_use_frame(new_op);
+
+       proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode, 0);
+
+       if (sched_point) {
+               sched_add_after(sched_point, new_op);
+               sched_add_after(new_op, proj);
+
+               sched_remove(irn);
+       }
+
+       exchange(irn, proj);
 
-       /* Return the result Proj */
-       return new_rd_Proj(dbg, irg, block, res, mode, 0);
 }
 
+/**
+ * Calls the transform functions for StackParam, Spill and Reload.
+ */
+static void ia32_after_ra_walker(ir_node *node, void *env) {
+       ia32_code_gen_t *cg = env;
+       ir_node *new_node   = NULL;
+       ia32_transform_env_t tenv;
+
+       if (is_Block(node))
+               return;
+
+       tenv.block = get_nodes_block(node);
+       tenv.dbg   = get_irn_dbg_info(node);
+       tenv.irg   = current_ir_graph;
+       tenv.irn   = node;
+       tenv.mod   = cg->mod;
+       tenv.mode  = get_irn_mode(node);
+       tenv.cg    = cg;
+
+       if (be_is_StackParam(node) || be_is_Reload(node)) {
+               transform_to_Load(&tenv);
+       }
+       else if (be_is_Spill(node)) {
+               transform_to_Store(&tenv);
+       }
+}
+
+/**
+ * We transform StackParam, Spill and Reload here. This needs to be done before
+ * stack biasing otherwise we would miss the corrected offset for these nodes.
+ */
+static void ia32_after_ra(void *self) {
+       ia32_code_gen_t *cg = self;
+       irg_walk_blkwise_graph(cg->irg, NULL, ia32_after_ra_walker, self);
+}
+
+
 /**
  * Emits the code, closes the output file and frees
  * the code generator interface.
@@ -383,7 +490,7 @@ static void ia32_codegen(void *self) {
        }
 
        ia32_finish_irg(irg, cg);
-       //dump_ir_block_graph_sched(irg, "-finished");
+       dump_ir_block_graph_sched(irg, "-finished");
        ia32_gen_routine(out, irg, cg);
 
        cur_reg_set = NULL;
@@ -403,8 +510,7 @@ static const arch_code_generator_if_t ia32_code_gen_if = {
        ia32_prepare_graph,
        ia32_before_sched,   /* before scheduling hook */
        ia32_before_ra,      /* before register allocation hook */
-       ia32_lower_spill,
-       ia32_lower_reload,
+       ia32_after_ra,       /* after register allocation hook */
        ia32_codegen         /* emit && done */
 };
 
@@ -425,6 +531,12 @@ static void *ia32_cg_init(FILE *F, const be_irg_t *birg) {
        cg->tv_ent   = pmap_create();
        cg->birg     = birg;
 
+       /* set optimizations */
+       cg->opt.incdec    = 0;
+       cg->opt.doam      = 1;
+       cg->opt.placecnst = 1;
+       cg->opt.immops    = 1;
+
        isa->num_codegens++;
 
        if (isa->num_codegens > 1)
@@ -530,7 +642,7 @@ static ir_type *get_between_type(void)
 
                between_type           = new_type_class(new_id_from_str("ia32_between_type"));
                old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
-               ret_addr_ent           = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
+               ret_addr_ent           = new_entity(between_type, new_id_from_str("ret_addr"), ret_addr_type);
 
                set_entity_offset_bytes(old_bp_ent, 0);
                set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
@@ -557,12 +669,13 @@ void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *ab
        int       i, ignore;
        ir_mode **modes;
        const arch_register_t *reg;
+       be_abi_call_flags_t call_flags = { 0, 0, 1, 0, 1 };
 
        /* get the between type and the frame pointer save entity */
        between_type = get_between_type();
 
        /* set stack parameter passing style */
-       be_abi_call_set_flags(abi, BE_ABI_NONE, between_type);
+       be_abi_call_set_flags(abi, call_flags, between_type);
 
        /* collect the mode for each type */
        modes = alloca(n * sizeof(modes[0]));
@@ -616,9 +729,10 @@ void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *ab
        }
        else if (n == 1) {
                tp   = get_method_res_type(method_type, 0);
+               assert(is_atomic_type(tp));
                mode = get_type_mode(tp);
 
-               be_abi_call_res_reg(abi, 0, &ia32_fp_regs[mode_is_float(mode) ? REG_XMM0 : REG_EAX]);
+               be_abi_call_res_reg(abi, 0, mode_is_float(mode) ? &ia32_fp_regs[REG_XMM0] : &ia32_gp_regs[REG_EAX]);
        }
 }