Allow loading of stack parameters with a different mode than the parameter mode....
[libfirm] / ir / be / ia32 / bearch_ia32.c
index 35195c0..bb7e0c8 100644 (file)
@@ -294,6 +294,7 @@ static const arch_register_t *ia32_get_irn_reg(const void *self,
        if (is_ia32_irn(irn)) {
                const arch_register_t **slots;
                slots = get_ia32_slots(irn);
+               assert(pos < get_ia32_n_res(irn));
                reg   = slots[pos];
        } else {
                reg = ia32_get_firm_reg(irn, cur_reg_set);
@@ -442,7 +443,7 @@ static const arch_register_t *ia32_abi_prologue(void *self, ir_node **mem, pmap
                be_node_set_flags(get_Proj_pred(curr_bp), BE_OUT_POS(get_Proj_proj(curr_bp)), arch_irn_flags_ignore);
 
                /* push ebp */
-               push    = new_rd_ia32_Push(NULL, env->irg, bl, noreg, noreg, *mem, curr_bp, curr_sp);
+               push    = new_rd_ia32_Push(NULL, env->irg, bl, noreg, noreg, *mem, curr_sp, curr_bp);
                curr_sp = new_r_Proj(env->irg, bl, push, get_irn_mode(curr_sp), pn_ia32_Push_stack);
                *mem    = new_r_Proj(env->irg, bl, push, mode_M, pn_ia32_Push_M);
 
@@ -827,7 +828,7 @@ static int ia32_possible_memory_operand(const void *self, const ir_node *irn, un
        (void) self;
 
        if (! is_ia32_irn(irn)                                  ||  /* must be an ia32 irn */
-               get_ia32_am_arity(irn) != 2                           ||  /* must be a binary operation TODO is this necessary? */
+               get_ia32_am_arity(irn) != ia32_am_binary              ||  /* must be a binary operation TODO is this necessary? */
                get_ia32_op_type(irn) != ia32_Normal                  ||  /* must not already be a addressmode irn */
                ! (get_ia32_am_support(irn) & ia32_am_Source)         ||  /* must be capable of source addressmode */
                ! ia32_is_spillmode_compatible(mode, spillmode)       ||
@@ -1001,18 +1002,26 @@ static void turn_back_am(ir_node *node)
        ia32_copy_am_attrs(load, node);
        set_irn_n(node, n_ia32_mem, new_NoMem());
 
-       if(get_ia32_am_arity(node) == ia32_am_unary) {
-               set_irn_n(node, n_ia32_unary_op, load_res);
-       } else if(get_ia32_am_arity(node) == ia32_am_binary) {
-               if(is_ia32_Immediate(get_irn_n(node, n_ia32_Cmp_right))) {
-                       assert(is_ia32_Cmp(node) || is_ia32_Cmp8Bit(node)
-                                       || is_ia32_Test(node) || is_ia32_Test8Bit(node));
-                       set_irn_n(node, n_ia32_binary_left, load_res);
-               } else {
+       switch (get_ia32_am_arity(node)) {
+               case ia32_am_unary:
+                       set_irn_n(node, n_ia32_unary_op, load_res);
+                       break;
+
+               case ia32_am_binary:
+                       if (is_ia32_Immediate(get_irn_n(node, n_ia32_Cmp_right))) {
+                               assert(is_ia32_Cmp(node)  || is_ia32_Cmp8Bit(node) ||
+                                      is_ia32_Test(node) || is_ia32_Test8Bit(node));
+                               set_irn_n(node, n_ia32_binary_left, load_res);
+                       } else {
+                               set_irn_n(node, n_ia32_binary_right, load_res);
+                       }
+                       break;
+
+               case ia32_am_ternary:
                        set_irn_n(node, n_ia32_binary_right, load_res);
-               }
-       } else if(get_ia32_am_arity(node) == ia32_am_ternary) {
-               set_irn_n(node, n_ia32_binary_right, load_res);
+                       break;
+
+               default: break;
        }
        set_irn_n(node, n_ia32_base, noreg);
        set_irn_n(node, n_ia32_index, noreg);
@@ -1056,13 +1065,15 @@ static ir_node *flags_remat(ir_node *node, ir_node *after)
                block = get_nodes_block(after);
        }
 
-       if (type == ia32_AddrModeS) {
-               turn_back_am(node);
-       } else if (type == ia32_AddrModeD) {
-               /* TODO implement this later... */
-               panic("found DestAM with flag user %+F this should not happen", node);
-       } else {
-               assert(type == ia32_Normal);
+       switch (type) {
+               case ia32_AddrModeS: turn_back_am(node); break;
+
+               case ia32_AddrModeD:
+                       /* TODO implement this later... */
+                       panic("found DestAM with flag user %+F this should not happen", node);
+                       break;
+
+               default: assert(type == ia32_Normal); break;
        }
 
        copy = exact_copy(node);
@@ -1217,7 +1228,7 @@ static ir_node *create_push(ia32_code_gen_t *cg, ir_node *node, ir_node *schedpo
        ir_node *noreg = ia32_new_NoReg_gp(cg);
        ir_node *frame = get_irg_frame(irg);
 
-       ir_node *push = new_rd_ia32_Push(dbg, irg, block, frame, noreg, mem, noreg, sp);
+       ir_node *push = new_rd_ia32_Push(dbg, irg, block, frame, noreg, mem, sp, noreg);
 
        set_ia32_frame_ent(push, ent);
        set_ia32_use_frame(push);
@@ -1457,12 +1468,12 @@ static void ia32_finish(void *self) {
                x87_simulate_graph(cg->arch_env, cg->birg);
        }
 
+       /* do peephole optimisations */
+       ia32_peephole_optimization(cg);
+
        /* create block schedule, this also removes empty blocks which might
         * produce critical edges */
        cg->blk_sched = be_create_block_schedule(irg, cg->birg->exec_freq);
-
-       /* do peephole optimisations */
-       ia32_peephole_optimization(irg, cg);
 }
 
 /**
@@ -1604,11 +1615,7 @@ static ia32_isa_t ia32_isa_template = {
        NULL,                    /* tv_ents */
        (0                 |
        IA32_OPT_INCDEC    |     /* optimize add 1, sub 1 into inc/dec               default: on */
-       IA32_OPT_DOAM      |     /* optimize address mode                            default: on */
-       IA32_OPT_LEA       |     /* optimize for LEAs                                default: on */
-       IA32_OPT_PLACECNST |     /* place constants immediately before instructions, default: on */
-       IA32_OPT_IMMOPS    |     /* operations can use immediates,                   default: on */
-       IA32_OPT_PUSHARGS),      /* create pushs for function argument passing,      default: on */
+       IA32_OPT_CC),
        arch_pentium_4,          /* instruction architecture */
        arch_pentium_4,          /* optimize for architecture */
        fp_x87,                  /* floating point mode */
@@ -1786,14 +1793,22 @@ static void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_cal
                cc = cc_cdecl_set;
        } else {
                cc = get_method_calling_convention(method_type);
-               if (get_method_additional_properties(method_type) & mtp_property_private) {
+               if (get_method_additional_properties(method_type) & mtp_property_private
+                               && (ia32_isa_template.opt & IA32_OPT_CC)) {
                        /* set the calling conventions to register parameter */
                        cc = (cc & ~cc_bits) | cc_reg_param;
                }
        }
+
+       /* we have to pop the shadow parameter ourself for compound calls */
+       if( (get_method_calling_convention(method_type) & cc_compound_ret)
+                       && !(cc & cc_reg_param)) {
+               be_abi_call_set_pop(abi, get_mode_size_bytes(mode_P_data));
+       }
+
        n = get_method_n_params(method_type);
        for (i = regnum = 0; i < n; i++) {
-               const ir_mode         *mode;
+               ir_mode               *mode;
                const arch_register_t *reg = NULL;
 
                tp   = get_method_param_type(method_type, i);
@@ -1805,7 +1820,11 @@ static void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_cal
                        be_abi_call_param_reg(abi, i, reg);
                        ++regnum;
                } else {
-                       be_abi_call_param_stack(abi, i, 4, 0, 0);
+                       /* Micro optimisation: if the mode is shorter than 4 bytes, load 4 bytes.
+                        * movl has a shorter opcode than mov[sz][bw]l */
+                       ir_mode *load_mode = mode;
+                       if (mode != NULL && get_mode_size_bytes(mode) < 4) load_mode = mode_Iu;
+                       be_abi_call_param_stack(abi, i, load_mode, 4, 0, 0);
                }
        }
 
@@ -2162,6 +2181,7 @@ static void set_arch_costs(enum cpu_support arch) {
                arch_costs = &k6_cost;
                break;
        case arch_athlon:
+       case arch_athlon_xp:
        case arch_athlon_64:
        case arch_opteron:
                arch_costs = &athlon_cost;
@@ -2260,6 +2280,7 @@ static const lc_opt_enum_int_items_t arch_items[] = {
        { "core",       arch_core, },
        { "k6",         arch_k6, },
        { "athlon",     arch_athlon, },
+       { "athlon-xp",  arch_athlon_xp, },
        { "athlon64",   arch_athlon_64, },
        { "opteron",    arch_opteron, },
        { "generic",    arch_generic, },
@@ -2298,11 +2319,7 @@ static const lc_opt_table_entry_t ia32_options[] = {
        LC_OPT_ENT_ENUM_INT("arch",      "select the instruction architecture", &arch_var),
        LC_OPT_ENT_ENUM_INT("opt",       "optimize for instruction architecture", &opt_arch_var),
        LC_OPT_ENT_ENUM_INT("fpunit",    "select the floating point unit", &fp_unit_var),
-       LC_OPT_ENT_NEGBIT("noaddrmode",  "do not use address mode", &ia32_isa_template.opt, IA32_OPT_DOAM),
-       LC_OPT_ENT_NEGBIT("nolea",       "do not optimize for LEAs", &ia32_isa_template.opt, IA32_OPT_LEA),
-       LC_OPT_ENT_NEGBIT("noplacecnst", "do not place constants", &ia32_isa_template.opt, IA32_OPT_PLACECNST),
-       LC_OPT_ENT_NEGBIT("noimmop",     "no operations with immediates", &ia32_isa_template.opt, IA32_OPT_IMMOPS),
-       LC_OPT_ENT_NEGBIT("nopushargs",  "do not create pushs for function arguments", &ia32_isa_template.opt, IA32_OPT_PUSHARGS),
+       LC_OPT_ENT_NEGBIT("nooptcc",  "do not optimize calling convention", &ia32_isa_template.opt, IA32_OPT_CC),
        LC_OPT_ENT_ENUM_INT("gasmode",   "set the GAS compatibility mode", &gas_var),
        LC_OPT_LAST
 };