2009-12-30 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / mini-ia64.c
index efa669d42249436ce778d0c28b00f0fdfa5884a8..a61142d7eb526190312e2d3c9d6e9cc0f1b8e22b 100644 (file)
 
 #include "trace.h"
 #include "mini-ia64.h"
-#include "inssel.h"
 #include "cpu-ia64.h"
 #include "jit-icalls.h"
-
-static gint appdomain_tls_offset = -1;
-static gint thread_tls_offset = -1;
+#include "ir-emit.h"
 
 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
 
@@ -198,13 +195,6 @@ typedef struct {
 
 #define DEBUG(a) if (cfg->verbose_level > 1) a
 
-#define NEW_ICONST(cfg,dest,val) do {  \
-               (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
-               (dest)->opcode = OP_ICONST;     \
-               (dest)->inst_c0 = (val);        \
-               (dest)->type = STACK_I4;        \
-       } while (0)
-
 #define PARAM_REGS 8
 
 static void inline
@@ -411,7 +401,7 @@ get_call_info (MonoCompile *cfg, MonoMemPool *mp, MonoMethodSignature *sig, gboo
                        cinfo->ret.reg = 8;
                        break;
                case MONO_TYPE_GENERICINST:
-                       if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
+                       if (!mono_type_generic_inst_is_valuetype (ret_type)) {
                                cinfo->ret.storage = ArgInIReg;
                                cinfo->ret.reg = IA64_R8;
                                break;
@@ -504,7 +494,7 @@ get_call_info (MonoCompile *cfg, MonoMemPool *mp, MonoMethodSignature *sig, gboo
                        add_general (&gr, &stack_size, ainfo);
                        break;
                case MONO_TYPE_GENERICINST:
-                       if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
+                       if (!mono_type_generic_inst_is_valuetype (ptype)) {
                                add_general (&gr, &stack_size, ainfo);
                                break;
                        }
@@ -789,6 +779,8 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                cfg->arch.omit_fp = FALSE;
        if ((sig->ret->type != MONO_TYPE_VOID) && (cinfo->ret.storage == ArgAggregate))
                cfg->arch.omit_fp = FALSE;
+       if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
+               cfg->arch.omit_fp = FALSE;
        for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
                ArgInfo *ainfo = &cinfo->args [i];
 
@@ -989,37 +981,7 @@ mono_arch_create_vars (MonoCompile *cfg)
 }
 
 static void
-add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, MonoInst *arg, ArgStorage storage, int reg, MonoInst *tree)
-{
-       switch (storage) {
-       case ArgInIReg:
-               arg->opcode = OP_OUTARG_REG;
-               arg->inst_left = tree;
-               arg->inst_right = (MonoInst*)call;
-               arg->backend.reg3 = reg;
-               call->used_iregs |= 1 << reg;
-               break;
-       case ArgInFloatReg:
-               arg->opcode = OP_OUTARG_FREG;
-               arg->inst_left = tree;
-               arg->inst_right = (MonoInst*)call;
-               arg->backend.reg3 = reg;
-               call->used_fregs |= 1 << reg;
-               break;
-       case ArgInFloatRegR4:
-               arg->opcode = OP_OUTARG_FREG_R4;
-               arg->inst_left = tree;
-               arg->inst_right = (MonoInst*)call;
-               arg->backend.reg3 = reg;
-               call->used_fregs |= 1 << reg;
-               break;
-       default:
-               g_assert_not_reached ();
-       }
-}
-
-static void
-add_outarg_reg2 (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
+add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
 {
        MonoInst *arg;
 
@@ -1054,268 +1016,6 @@ add_outarg_reg2 (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int r
 
 static void
 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
-{
-       MonoInst *arg;
-       MonoMethodSignature *tmp_sig;
-       MonoInst *sig_arg;
-
-       /* FIXME: Add support for signature tokens to AOT */
-       cfg->disable_aot = TRUE;
-
-       g_assert (cinfo->sig_cookie.storage == ArgOnStack);
-
-       /*
-        * mono_ArgIterator_Setup assumes the signature cookie is 
-        * passed first and all the arguments which were before it are
-        * passed on the stack after the signature. So compensate by 
-        * passing a different signature.
-        */
-       tmp_sig = mono_metadata_signature_dup (call->signature);
-       tmp_sig->param_count -= call->signature->sentinelpos;
-       tmp_sig->sentinelpos = 0;
-       memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
-
-       MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
-       sig_arg->inst_p0 = tmp_sig;
-
-       MONO_INST_NEW (cfg, arg, OP_OUTARG);
-       arg->inst_left = sig_arg;
-       arg->inst_imm = 16 + cinfo->sig_cookie.offset;
-       arg->type = STACK_PTR;
-
-       /* prepend, so they get reversed */
-       arg->next = call->out_args;
-       call->out_args = arg;
-}
-
-/* 
- * take the arguments and generate the arch-specific
- * instructions to properly call the function in call.
- * This includes pushing, moving arguments to the right register
- * etc.
- */
-MonoCallInst*
-mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual)
-{
-       MonoInst *arg, *in;
-       MonoMethodSignature *sig;
-       int i, n, stack_size;
-       CallInfo *cinfo;
-       ArgInfo *ainfo;
-
-       stack_size = 0;
-
-       mono_ia64_alloc_stacked_registers (cfg);
-
-       sig = call->signature;
-       n = sig->param_count + sig->hasthis;
-
-       cinfo = get_call_info (cfg, cfg->mempool, sig, sig->pinvoke);
-
-       if (cinfo->ret.storage == ArgAggregate) {
-               /* The code in emit_this_vret_arg needs a local */
-               cfg->arch.ret_var_addr_local = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
-               ((MonoInst*)cfg->arch.ret_var_addr_local)->flags |= MONO_INST_VOLATILE;
-       }
-
-       for (i = 0; i < n; ++i) {
-               ainfo = cinfo->args + i;
-
-               if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
-                       /* Emit the signature cookie just before the implicit arguments */
-                       emit_sig_cookie (cfg, call, cinfo);
-               }
-
-               if (is_virtual && i == 0) {
-                       /* the argument will be attached to the call instruction */
-                       in = call->args [i];
-               } else {
-                       MonoType *arg_type;
-
-                       MONO_INST_NEW (cfg, arg, OP_OUTARG);
-                       in = call->args [i];
-                       arg->cil_code = in->cil_code;
-                       arg->inst_left = in;
-                       arg->type = in->type;
-                       /* prepend, so they get reversed */
-                       arg->next = call->out_args;
-                       call->out_args = arg;
-
-                       if (sig->hasthis && (i == 0))
-                               arg_type = &mono_defaults.object_class->byval_arg;
-                       else
-                               arg_type = sig->params [i - sig->hasthis];
-
-                       if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(arg_type))) {
-                               MonoInst *stack_addr;
-                               guint32 align;
-                               guint32 size;
-
-                               if (arg_type->type == MONO_TYPE_TYPEDBYREF) {
-                                       size = sizeof (MonoTypedRef);
-                                       align = sizeof (gpointer);
-                               }
-                               else
-                               if (sig->pinvoke)
-                                       size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
-                               else {
-                                       /* 
-                                        * Other backends use mini_type_stack_size (), but that
-                                        * aligns the size to 8, which is larger than the size of
-                                        * the source, leading to reads of invalid memory if the
-                                        * source is at the end of address space.
-                                        */
-                                       size = mono_class_value_size (in->klass, &align);
-                               }
-
-                               if (ainfo->storage == ArgAggregate) {
-                                       MonoInst *vtaddr, *load, *load2, *offset_ins, *set_reg;
-                                       int slot, j;
-
-                                       vtaddr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
-
-                                       /* 
-                                        * Part of the structure is passed in registers.
-                                        */
-                                       for (j = 0; j < ainfo->nregs; ++j) {
-                                               int offset, load_op, dest_reg, arg_storage;
-
-                                               slot = ainfo->reg + j;
-                                               
-                                               if (ainfo->atype == AggregateSingleHFA) {
-                                                       load_op = CEE_LDIND_R4;
-                                                       offset = j * 4;
-                                                       dest_reg = ainfo->reg + j;
-                                                       arg_storage = ArgInFloatReg;
-                                               } else if (ainfo->atype == AggregateDoubleHFA) {
-                                                       load_op = CEE_LDIND_R8;
-                                                       offset = j * 8;
-                                                       dest_reg = ainfo->reg + j;
-                                                       arg_storage = ArgInFloatReg;
-                                               } else {
-                                                       load_op = CEE_LDIND_I;
-                                                       offset = j * 8;
-                                                       dest_reg = cfg->arch.reg_out0 + ainfo->reg + j;
-                                                       arg_storage = ArgInIReg;
-                                               }
-
-                                               MONO_INST_NEW (cfg, load, CEE_LDIND_I);
-                                               load->ssa_op = MONO_SSA_LOAD;
-                                               load->inst_i0 = (cfg)->varinfo [vtaddr->inst_c0];
-
-                                               NEW_ICONST (cfg, offset_ins, offset);
-                                               MONO_INST_NEW (cfg, load2, CEE_ADD);
-                                               load2->inst_left = load;
-                                               load2->inst_right = offset_ins;
-
-                                               MONO_INST_NEW (cfg, load, load_op);
-                                               load->inst_left = load2;
-
-                                               if (j == 0)
-                                                       set_reg = arg;
-                                               else
-                                                       MONO_INST_NEW (cfg, set_reg, OP_OUTARG_REG);
-                                               add_outarg_reg (cfg, call, set_reg, arg_storage, dest_reg, load);
-                                               if (set_reg != call->out_args) {
-                                                       set_reg->next = call->out_args;
-                                                       call->out_args = set_reg;
-                                               }
-                                       }
-
-                                       /* 
-                                        * Part of the structure is passed on the stack.
-                                        */
-                                       for (j = ainfo->nregs; j < ainfo->nslots; ++j) {
-                                               MonoInst *outarg;
-
-                                               slot = ainfo->reg + j;
-
-                                               MONO_INST_NEW (cfg, load, CEE_LDIND_I);
-                                               load->ssa_op = MONO_SSA_LOAD;
-                                               load->inst_i0 = (cfg)->varinfo [vtaddr->inst_c0];
-
-                                               NEW_ICONST (cfg, offset_ins, (j * sizeof (gpointer)));
-                                               MONO_INST_NEW (cfg, load2, CEE_ADD);
-                                               load2->inst_left = load;
-                                               load2->inst_right = offset_ins;
-
-                                               MONO_INST_NEW (cfg, load, CEE_LDIND_I);
-                                               load->inst_left = load2;
-
-                                               if (j == 0)
-                                                       outarg = arg;
-                                               else
-                                                       MONO_INST_NEW (cfg, outarg, OP_OUTARG);
-                                               outarg->inst_left = load;
-                                               outarg->inst_imm = 16 + ainfo->offset + (slot - 8) * 8;
-
-                                               if (outarg != call->out_args) {
-                                                       outarg->next = call->out_args;
-                                                       call->out_args = outarg;
-                                               }
-                                       }
-
-                                       /* Trees can't be shared so make a copy */
-                                       MONO_INST_NEW (cfg, arg, CEE_STIND_I);
-                                       arg->cil_code = in->cil_code;
-                                       arg->ssa_op = MONO_SSA_STORE;
-                                       arg->inst_left = vtaddr;
-                                       arg->inst_right = in;
-                                       arg->type = in->type;
-                                       
-                                       /* prepend, so they get reversed */
-                                       arg->next = call->out_args;
-                                       call->out_args = arg;
-                               }
-                               else {
-                                       MONO_INST_NEW (cfg, stack_addr, OP_REGOFFSET);
-                                       stack_addr->inst_basereg = IA64_SP;
-                                       stack_addr->inst_offset = 16 + ainfo->offset;
-                                       stack_addr->inst_imm = size;
-
-                                       arg->opcode = OP_OUTARG_VT;
-                                       arg->inst_right = stack_addr;
-                               }
-                       }
-                       else {
-                               switch (ainfo->storage) {
-                               case ArgInIReg:
-                                       add_outarg_reg (cfg, call, arg, ainfo->storage, cfg->arch.reg_out0 + ainfo->reg, in);
-                                       break;
-                               case ArgInFloatReg:
-                               case ArgInFloatRegR4:
-                                       add_outarg_reg (cfg, call, arg, ainfo->storage, ainfo->reg, in);
-                                       break;
-                               case ArgOnStack:
-                                       if (arg_type->type == MONO_TYPE_R4 && !arg_type->byref) {
-                                               arg->opcode = OP_OUTARG_R4;
-                                       }
-                                       else
-                                               arg->opcode = OP_OUTARG;
-                                       arg->inst_imm = 16 + ainfo->offset;
-                                       break;
-                               default:
-                                       g_assert_not_reached ();
-                               }
-                       }
-               }
-       }
-
-       /* Handle the case where there are no implicit arguments */
-       if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
-               emit_sig_cookie (cfg, call, cinfo);
-       }
-
-       call->stack_usage = cinfo->stack_usage;
-       cfg->param_area = MAX (cfg->param_area, call->stack_usage);
-       cfg->arch.n_out_regs = MAX (cfg->arch.n_out_regs, cinfo->reg_usage);
-       cfg->flags |= MONO_CFG_HAS_CALLS;
-
-       return call;
-}
-
-static void
-emit_sig_cookie2 (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
 {
        MonoMethodSignature *tmp_sig;
 
@@ -1383,7 +1083,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
        }
 
        if (cinfo->ret.storage == ArgValuetypeAddrInIReg) {
-               add_outarg_reg2 (cfg, call, ArgInIReg, cfg->arch.reg_out0 + cinfo->ret.reg, call->vret_var);
+               add_outarg_reg (cfg, call, ArgInIReg, cfg->arch.reg_out0 + cinfo->ret.reg, call->vret_var);
        }
 
        for (i = 0; i < n; ++i) {
@@ -1393,7 +1093,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
 
                if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
                        /* Emit the signature cookie just before the implicit arguments */
-                       emit_sig_cookie2 (cfg, call, cinfo);
+                       emit_sig_cookie (cfg, call, cinfo);
                }
 
                in = call->args [i];
@@ -1440,11 +1140,11 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
                else {
                        switch (ainfo->storage) {
                        case ArgInIReg:
-                               add_outarg_reg2 (cfg, call, ainfo->storage, cfg->arch.reg_out0 + ainfo->reg, in);
+                               add_outarg_reg (cfg, call, ainfo->storage, cfg->arch.reg_out0 + ainfo->reg, in);
                                break;
                        case ArgInFloatReg:
                        case ArgInFloatRegR4:
-                               add_outarg_reg2 (cfg, call, ainfo->storage, ainfo->reg, in);
+                               add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
                                break;
                        case ArgOnStack:
                                if (arg_type->type == MONO_TYPE_R4 && !arg_type->byref)
@@ -1462,7 +1162,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
 
        /* Handle the case where there are no implicit arguments */
        if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
-               emit_sig_cookie2 (cfg, call, cinfo);
+               emit_sig_cookie (cfg, call, cinfo);
        }
 
        call->stack_usage = cinfo->stack_usage;
@@ -1530,7 +1230,7 @@ mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
                        MONO_ADD_INS (cfg->cbb, store);
                }
        } else {
-               mini_emit_memcpy2 (cfg, IA64_SP, 16 + ainfo->offset, src->dreg, 0, size, 4);
+               mini_emit_memcpy (cfg, IA64_SP, 16 + ainfo->offset, src->dreg, 0, size, 4);
        }
 }
 
@@ -1666,9 +1366,6 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
        MonoInst *ins, *n, *next, *temp, *temp2, *temp3, *last_ins = NULL;
        ins = bb->code;
 
-       if (bb->max_vreg > cfg->rs->next_vreg)
-               cfg->rs->next_vreg = bb->max_vreg;
-
        MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
                switch (ins->opcode) {
                case OP_STOREI1_MEMBASE_IMM:
@@ -1683,17 +1380,17 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                NEW_INS (cfg, temp2, OP_ADD_IMM);
                                temp2->sreg1 = ins->inst_destbasereg;
                                temp2->inst_imm = ins->inst_offset;
-                               temp2->dreg = mono_regstate_next_int (cfg->rs);
+                               temp2->dreg = mono_alloc_ireg (cfg);
                        }
                        else {
                                NEW_INS (cfg, temp, OP_I8CONST);
                                temp->inst_c0 = ins->inst_offset;
-                               temp->dreg = mono_regstate_next_int (cfg->rs);
+                               temp->dreg = mono_alloc_ireg (cfg);
 
                                NEW_INS (cfg, temp2, OP_LADD);
                                temp2->sreg1 = ins->inst_destbasereg;
                                temp2->sreg2 = temp->dreg;
-                               temp2->dreg = mono_regstate_next_int (cfg->rs);
+                               temp2->dreg = mono_alloc_ireg (cfg);
                        }
 
                        switch (ins->opcode) {
@@ -1719,7 +1416,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                        else {
                                NEW_INS (cfg, temp3, OP_I8CONST);
                                temp3->inst_c0 = ins->inst_imm;
-                               temp3->dreg = mono_regstate_next_int (cfg->rs);
+                               temp3->dreg = mono_alloc_ireg (cfg);
                                ins->sreg1 = temp3->dreg;
                        }
 
@@ -1742,16 +1439,16 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                NEW_INS (cfg, temp2, OP_ADD_IMM);
                                temp2->sreg1 = ins->inst_destbasereg;
                                temp2->inst_imm = ins->inst_offset;
-                               temp2->dreg = mono_regstate_next_int (cfg->rs);
+                               temp2->dreg = mono_alloc_ireg (cfg);
                        }
                        else {
                                NEW_INS (cfg, temp, OP_I8CONST);
                                temp->inst_c0 = ins->inst_offset;
-                               temp->dreg = mono_regstate_next_int (cfg->rs);
+                               temp->dreg = mono_alloc_ireg (cfg);
                                NEW_INS (cfg, temp2, OP_LADD);
                                temp2->sreg1 = ins->inst_destbasereg;
                                temp2->sreg2 = temp->dreg;
-                               temp2->dreg = mono_regstate_next_int (cfg->rs);
+                               temp2->dreg = mono_alloc_ireg (cfg);
                        }
 
                        ins->inst_offset = 0;
@@ -1781,16 +1478,16 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                NEW_INS (cfg, temp2, OP_ADD_IMM);
                                temp2->sreg1 = ins->inst_basereg;
                                temp2->inst_imm = ins->inst_offset;
-                               temp2->dreg = mono_regstate_next_int (cfg->rs);
+                               temp2->dreg = mono_alloc_ireg (cfg);
                        }
                        else {
                                NEW_INS (cfg, temp, OP_I8CONST);
                                temp->inst_c0 = ins->inst_offset;
-                               temp->dreg = mono_regstate_next_int (cfg->rs);
+                               temp->dreg = mono_alloc_ireg (cfg);
                                NEW_INS (cfg, temp2, OP_LADD);
                                temp2->sreg1 = ins->inst_basereg;
                                temp2->sreg2 = temp->dreg;
-                               temp2->dreg = mono_regstate_next_int (cfg->rs);
+                               temp2->dreg = mono_alloc_ireg (cfg);
                        }
 
                        ins->inst_offset = 0;
@@ -1878,7 +1575,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                        else {
                                NEW_INS (cfg, temp, OP_I8CONST);
                                temp->inst_c0 = ins->inst_imm;
-                               temp->dreg = mono_regstate_next_int (cfg->rs);
+                               temp->dreg = mono_alloc_ireg (cfg);
                                ins->sreg2 = temp->dreg;
                        }
                        break;
@@ -1894,7 +1591,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
 
                        /* Branch opts can eliminate the branch */
                        if (!next || (!(MONO_IS_COND_BRANCH_OP (next) || MONO_IS_COND_EXC (next) || MONO_IS_SETCC (next)))) {
-                               ins->opcode = OP_NOP;
+                               NULLIFY_INS (ins);
                                break;
                        }
 
@@ -1923,15 +1620,14 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                else {
                                        NEW_INS (cfg, temp, OP_I8CONST);
                                        temp->inst_c0 = ins->inst_imm;
-                                       temp->dreg = mono_regstate_next_int (cfg->rs);
+                                       temp->dreg = mono_alloc_ireg (cfg);
                                        ins->sreg2 = temp->dreg;
                                }
                        }
 
                        if (MONO_IS_COND_BRANCH_OP (next)) {
                                next->opcode = OP_IA64_BR_COND;
-                               if (! (next->flags & MONO_INST_BRLABEL))
-                                       next->inst_target_bb = next->inst_true_bb;
+                               next->inst_target_bb = next->inst_true_bb;
                        } else if (MONO_IS_COND_EXC (next)) {
                                next->opcode = OP_IA64_COND_EXC;
                        } else if (MONO_IS_SETCC (next)) {
@@ -1953,7 +1649,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
 
                        /* Branch opts can eliminate the branch */
                        if (!next || (!(MONO_IS_COND_BRANCH_OP (next) || MONO_IS_COND_EXC (next) || MONO_IS_SETCC (next)))) {
-                               ins->opcode = OP_NOP;
+                               NULLIFY_INS (ins);
                                break;
                        }
 
@@ -1961,8 +1657,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
 
                        if (MONO_IS_COND_BRANCH_OP (next)) {
                                next->opcode = OP_IA64_BR_COND;
-                               if (! (next->flags & MONO_INST_BRLABEL))
-                                       next->inst_target_bb = next->inst_true_bb;
+                               next->inst_target_bb = next->inst_true_bb;
                        } else if (MONO_IS_COND_EXC (next)) {
                                next->opcode = OP_IA64_COND_EXC;
                        } else if (MONO_IS_SETCC (next)) {
@@ -1985,6 +1680,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                        temp->sreg2 = ins->sreg2;
                        
                        ins->opcode = OP_IA64_CSET;
+                       MONO_INST_NULLIFY_SREGS (ins);
                        break;
                case OP_MUL_IMM:
                case OP_LMUL_IMM:
@@ -2012,7 +1708,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                for (i = 0; i < 64; ++i) {
                                        if (ins->inst_imm & (((gint64)1) << i)) {
                                                NEW_INS (cfg, temp, shl_op);
-                                               temp->dreg = mono_regstate_next_int (cfg->rs);
+                                               temp->dreg = mono_alloc_ireg (cfg);
                                                temp->sreg1 = ins->sreg1;
                                                temp->inst_imm = i;
 
@@ -2020,7 +1716,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                                                        sum_reg = temp->dreg;
                                                else {
                                                        NEW_INS (cfg, temp2, OP_LADD);
-                                                       temp2->dreg = mono_regstate_next_int (cfg->rs);
+                                                       temp2->dreg = mono_alloc_ireg (cfg);
                                                        temp2->sreg1 = sum_reg;
                                                        temp2->sreg2 = temp->dreg;
                                                        sum_reg = temp2->dreg;
@@ -2045,7 +1741,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_LCONV_TO_OVF_I4_UN:
                        NEW_INS (cfg, temp, OP_ICONST);
                        temp->inst_c0 = 0x7fffffff;
-                       temp->dreg = mono_regstate_next_int (cfg->rs);
+                       temp->dreg = mono_alloc_ireg (cfg);
 
                        NEW_INS (cfg, temp2, OP_IA64_CMP4_GT_UN);
                        temp2->sreg1 = ins->sreg1;
@@ -2094,7 +1790,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
        }
        bb->last_ins = last_ins;
 
-       bb->max_vreg = cfg->rs->next_vreg;
+       bb->max_vreg = cfg->next_vreg;
 }
 
 /*
@@ -2210,6 +1906,8 @@ emit_move_return_value (MonoCompile *cfg, MonoInst *ins, Ia64CodegenState code)
        case OP_FCALL_REG:
        case OP_FCALL_MEMBASE:
                g_assert (ins->dreg == 8);
+               if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4)
+                       ia64_fnorm_d_sf (code, ins->dreg, ins->dreg, 0);
                break;
        case OP_VCALL:
        case OP_VCALL_REG:
@@ -2370,25 +2068,16 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        int pred = 0;
                        if (ins->opcode == OP_IA64_BR_COND)
                                pred = 6;
-                       if (ins->flags & MONO_INST_BRLABEL) {
-                               if (ins->inst_i0->inst_c0) {
-                                       NOT_IMPLEMENTED;
-                               } else {
-                                       add_patch_info (cfg, code, MONO_PATCH_INFO_LABEL, ins->inst_i0);
-                                       ia64_br_cond_pred (code, pred, 0);
-                               }
-                       } else {
-                               if (ins->inst_target_bb->native_offset) {
-                                       guint8 *pos = code.buf + code.nins;
+                       if (ins->inst_target_bb->native_offset) {
+                               guint8 *pos = code.buf + code.nins;
 
-                                       ia64_br_cond_pred (code, pred, 0);
-                                       ia64_begin_bundle (code);
-                                       ia64_patch (pos, cfg->native_code + ins->inst_target_bb->native_offset);
-                               } else {
-                                       add_patch_info (cfg, code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
-                                       ia64_br_cond_pred (code, pred, 0);
-                               } 
-                       }
+                               ia64_br_cond_pred (code, pred, 0);
+                               ia64_begin_bundle (code);
+                               ia64_patch (pos, cfg->native_code + ins->inst_target_bb->native_offset);
+                       } else {
+                               add_patch_info (cfg, code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
+                               ia64_br_cond_pred (code, pred, 0);
+                       } 
                        break;
                }
                case OP_LABEL:
@@ -2396,6 +2085,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        ins->inst_c0 = code.buf - cfg->native_code;
                        break;
                case OP_NOP:
+               case OP_RELAXED_NOP:
                case OP_DUMMY_USE:
                case OP_DUMMY_STORE:
                case OP_NOT_REACHED:
@@ -2438,6 +2128,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        ia64_shl (code, ins->dreg, ins->sreg1, ins->sreg2);
                        break;
                case OP_ISHR:
+                       ia64_sxt4 (code, GP_SCRATCH_REG, ins->sreg1);
+                       ia64_shr (code, ins->dreg, GP_SCRATCH_REG, ins->sreg2);
+                       break;
                case OP_LSHR:
                        ia64_shr (code, ins->dreg, ins->sreg1, ins->sreg2);
                        break;
@@ -2537,10 +2230,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        ia64_shl_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
                        break;
                case OP_SHR_IMM:
-               case OP_ISHR_IMM:
                case OP_LSHR_IMM:
                        ia64_shr_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
                        break;
+               case OP_ISHR_IMM:
+                       g_assert (ins->inst_imm <= 64);
+                       ia64_extr (code, ins->dreg, ins->sreg1, ins->inst_imm, 32 - ins->inst_imm);
+                       break;
                case OP_ISHR_UN_IMM:
                        ia64_zxt4 (code, GP_SCRATCH_REG, ins->sreg1);
                        ia64_shr_u_imm (code, ins->dreg, GP_SCRATCH_REG, ins->inst_imm);
@@ -2956,6 +2652,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        ia64_fnorm_s_sf (code, ins->dreg, ins->sreg1, 0);
                        break;
                case OP_FCONV_TO_I8:
+               case OP_FCONV_TO_I:
                        ia64_fcvt_fx_trunc_sf (code, FP_SCRATCH_REG, ins->sreg1, 0);
                        ia64_getf_sig (code, ins->dreg, FP_SCRATCH_REG);
                        break;
@@ -4661,7 +4358,7 @@ mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean ena
 }
 
 void*
-mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
+mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
 {
        Ia64CodegenState code;
        CallInfo *cinfo = NULL;
@@ -4809,7 +4506,7 @@ mono_arch_get_patch_offset (guint8 *code)
 }
 
 gpointer
-mono_arch_get_vcall_slot (guint8* code, gpointer *regs, int *displacement)
+mono_arch_get_vcall_slot (guint8* code, mgreg_t *regs, int *displacement)
 {
        guint8 *bundle2 = code - 48;
        guint8 *bundle3 = code - 32;
@@ -4860,42 +4557,23 @@ mono_arch_get_vcall_slot (guint8* code, gpointer *regs, int *displacement)
 
                *displacement = (gssize)regs [IA64_R8] - (gssize)regs [IA64_R11];
 
-               return regs [IA64_R11];
+               return (gpointer)regs [IA64_R11];
        }
 
        return NULL;
 }
 
 gpointer*
-mono_arch_get_vcall_slot_addr (guint8* code, gpointer *regs)
-{
-       gpointer vt;
-       int displacement;
-       vt = mono_arch_get_vcall_slot (code, regs, &displacement);
-       if (!vt)
-               return NULL;
-       return (gpointer*)(gpointer)((char*)vt + displacement);
-}
-
-gpointer*
-mono_arch_get_delegate_method_ptr_addr (guint8* code, gpointer *regs)
+mono_arch_get_delegate_method_ptr_addr (guint8* code, mgreg_t *regs)
 {
        NOT_IMPLEMENTED;
 
        return NULL;
 }
 
-static gboolean tls_offset_inited = FALSE;
-
 void
 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
 {
-       if (!tls_offset_inited) {
-               tls_offset_inited = TRUE;
-
-               appdomain_tls_offset = mono_domain_get_tls_offset ();
-               thread_tls_offset = mono_thread_get_tls_offset ();
-       }               
 }
 
 void
@@ -4903,60 +4581,14 @@ mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
 {
 }
 
-void
-mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
-{
-       MonoCallInst *call = (MonoCallInst*)inst;
-       int out_reg = cfg->arch.reg_out0;
-
-       if (vt_reg != -1) {
-               CallInfo * cinfo = get_call_info (cfg, cfg->mempool, inst->signature, FALSE);
-               MonoInst *vtarg;
-
-               if (cinfo->ret.storage == ArgAggregate) {
-                       MonoInst *local = (MonoInst*)cfg->arch.ret_var_addr_local;
-
-                       /* 
-                        * The valuetype is in registers after the call, need to be copied 
-                        * to the stack. Save the address to a local here, so the call 
-                        * instruction can access it.
-                        */
-                       g_assert (local->opcode == OP_REGOFFSET);
-                       MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, local->inst_basereg, local->inst_offset, vt_reg);
-               }
-               else {
-                       MONO_INST_NEW (cfg, vtarg, OP_MOVE);
-                       vtarg->sreg1 = vt_reg;
-                       vtarg->dreg = mono_regstate_next_int (cfg->rs);
-                       mono_bblock_add_inst (cfg->cbb, vtarg);
-
-                       mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, out_reg, FALSE);
-
-                       out_reg ++;
-               }
-       }
-
-       /* add the this argument */
-       if (this_reg != -1) {
-               MonoInst *this;
-               MONO_INST_NEW (cfg, this, OP_MOVE);
-               this->type = this_type;
-               this->sreg1 = this_reg;
-               this->dreg = mono_regstate_next_int (cfg->rs);
-               mono_bblock_add_inst (cfg->cbb, this);
-
-               mono_call_inst_add_outarg_reg (cfg, call, this->dreg, out_reg, FALSE);
-       }
-}
-
-
 #ifdef MONO_ARCH_HAVE_IMT
 
 /*
  * LOCKING: called with the domain lock held
  */
 gpointer
-mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count)
+mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
+       gpointer fail_tramp)
 {
        int i;
        int size = 0;
@@ -4974,24 +4606,35 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                ia64_begin_bundle (code);
                item->code_target = (guint8*)code.buf + code.nins;
                if (item->is_equals) {
-                       if (item->check_target_idx) {
-                               if (!item->compare_done) {
-                                       ia64_movl (code, GP_SCRATCH_REG, item->method);
+                       gboolean fail_case = !item->check_target_idx && fail_tramp;
+
+                       if (item->check_target_idx || fail_case) {
+                               if (!item->compare_done || fail_case) {
+                                       ia64_movl (code, GP_SCRATCH_REG, item->key);
                                        ia64_cmp_eq (code, 6, 7, IA64_R9, GP_SCRATCH_REG);
                                }
                                item->jmp_code = (guint8*)code.buf + code.nins;
                                ia64_br_cond_pred (code, 7, 0);
 
-                               ia64_movl (code, GP_SCRATCH_REG, &(vtable->vtable [item->vtable_slot]));
+                               ia64_movl (code, GP_SCRATCH_REG, &(vtable->vtable [item->value.vtable_slot]));
                                ia64_ld8 (code, GP_SCRATCH_REG, GP_SCRATCH_REG);
                                ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG);
                                ia64_br_cond_reg (code, IA64_B6);
+
+                               if (fail_case) {
+                                       ia64_patch (item->jmp_code, (guint8*)code.buf + code.nins);
+                                       ia64_movl (code, GP_SCRATCH_REG, fail_tramp);
+                                       ia64_ld8 (code, GP_SCRATCH_REG, GP_SCRATCH_REG);
+                                       ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG);
+                                       ia64_br_cond_reg (code, IA64_B6);
+                                       item->jmp_code = NULL;
+                               }
                        } else {
                                /* enable the commented code to assert on wrong method */
 #if ENABLE_WRONG_METHOD_CHECK
                                g_assert_not_reached ();
 #endif
-                               ia64_movl (code, GP_SCRATCH_REG, &(vtable->vtable [item->vtable_slot]));
+                               ia64_movl (code, GP_SCRATCH_REG, &(vtable->vtable [item->value.vtable_slot]));
                                ia64_ld8 (code, GP_SCRATCH_REG, GP_SCRATCH_REG);
                                ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG);
                                ia64_br_cond_reg (code, IA64_B6);
@@ -5000,7 +4643,7 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
 #endif
                        }
                } else {
-                       ia64_movl (code, GP_SCRATCH_REG, item->method);
+                       ia64_movl (code, GP_SCRATCH_REG, item->key);
                        ia64_cmp_geu (code, 6, 7, IA64_R9, GP_SCRATCH_REG);
                        item->jmp_code = (guint8*)code.buf + code.nins;
                        ia64_br_cond_pred (code, 6, 0);
@@ -5020,7 +4663,12 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
        g_assert (code.buf - buf <= size);
 
        size = code.buf - buf;
-       start = mono_code_manager_reserve (domain->code_mp, size);
+       if (fail_tramp) {
+               start = mono_method_alloc_generic_virtual_thunk (domain, size + 16);
+               start = (gpointer)ALIGN_TO (start, 16);
+       } else {
+               start = mono_domain_code_reserve (domain, size);
+       }
        memcpy (start, buf, size);
 
        mono_arch_flush_icache (start, size);
@@ -5031,92 +4679,30 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
 }
 
 MonoMethod*
-mono_arch_find_imt_method (gpointer *regs, guint8 *code)
+mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
 {
-       return regs [IA64_R9];
+       return (MonoMethod*)regs [IA64_R9];
 }
 
 void
-mono_arch_emit_imt_argument (MonoCompile *cfg, MonoCallInst *call)
+mono_arch_emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg)
 {
        /* Done by the implementation of the CALL_MEMBASE opcodes */
 }
 #endif
 
 gpointer
-mono_arch_get_this_arg_from_call (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, gssize *regs, guint8 *code)
+mono_arch_get_this_arg_from_call (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, mgreg_t *regs, guint8 *code)
 {
        return (gpointer)regs [IA64_R10];
 }
 
-MonoObject*
-mono_arch_find_this_argument (gpointer *regs, MonoMethod *method, MonoGenericSharingContext *gsctx)
-{
-       return mono_arch_get_this_arg_from_call (gsctx, mono_method_signature (method), (gssize*)regs, NULL);
-}
-
 gpointer
 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
 {
        return NULL;
 }
 
-MonoInst*
-mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
-{
-       MonoInst *ins = NULL;
-
-       if(cmethod->klass->image == mono_defaults.corlib &&
-                          (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
-                          (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
-               /* 
-                * We don't use the generic version in mini_get_inst_for_method () since the
-                * ia64 has atomic_add_imm opcodes.
-                */
-               if (strcmp (cmethod->name, "Increment") == 0) {
-                       guint32 opcode;
-
-                       if (fsig->params [0]->type == MONO_TYPE_I4)
-                               opcode = OP_ATOMIC_ADD_IMM_NEW_I4;
-                       else if (fsig->params [0]->type == MONO_TYPE_I8)
-                               opcode = OP_ATOMIC_ADD_IMM_NEW_I8;
-                       else
-                               g_assert_not_reached ();
-                       MONO_INST_NEW (cfg, ins, opcode);
-                       ins->inst_imm = 1;
-                       ins->inst_i0 = args [0];
-               } else if (strcmp (cmethod->name, "Decrement") == 0) {
-                       guint32 opcode;
-
-                       if (fsig->params [0]->type == MONO_TYPE_I4)
-                               opcode = OP_ATOMIC_ADD_IMM_NEW_I4;
-                       else if (fsig->params [0]->type == MONO_TYPE_I8)
-                               opcode = OP_ATOMIC_ADD_IMM_NEW_I8;
-                       else
-                               g_assert_not_reached ();
-                       MONO_INST_NEW (cfg, ins, opcode);
-                       ins->inst_imm = -1;
-                       ins->inst_i0 = args [0];
-               } else if (strcmp (cmethod->name, "Add") == 0) {
-                       guint32 opcode;
-
-                       if (fsig->params [0]->type == MONO_TYPE_I4)
-                               opcode = OP_ATOMIC_ADD_NEW_I4;
-                       else if (fsig->params [0]->type == MONO_TYPE_I8)
-                               opcode = OP_ATOMIC_ADD_NEW_I8;
-                       else
-                               g_assert_not_reached ();
-                       
-                       MONO_INST_NEW (cfg, ins, opcode);
-
-                       ins->inst_i0 = args [0];
-                       ins->inst_i1 = args [1];
-               }
-       }
-
-       return ins;
-}
-
 MonoInst*
 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
 {
@@ -5213,28 +4799,10 @@ mono_arch_print_tree (MonoInst *tree, int arity)
        return 0;
 }
 
-MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
-{
-       MonoInst* ins;
-       
-       if (appdomain_tls_offset == -1)
-               return NULL;
-       
-       MONO_INST_NEW (cfg, ins, OP_TLS_GET);
-       ins->inst_offset = appdomain_tls_offset;
-       return ins;
-}
-
-MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
+MonoInst*
+mono_arch_get_domain_intrinsic (MonoCompile* cfg)
 {
-       MonoInst* ins;
-       
-       if (thread_tls_offset == -1)
-               return NULL;
-       
-       MONO_INST_NEW (cfg, ins, OP_TLS_GET);
-       ins->inst_offset = thread_tls_offset;
-       return ins;
+       return mono_get_domain_intrinsic (cfg);
 }
 
 gpointer