2010-05-30 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-codegen.c
index 0982ffb6be747783f3ad947758c21ad13074bd9a..7a1476fbb5ded54194a8e9a24aac6dde608a6348 100644 (file)
@@ -14,6 +14,7 @@
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/threads.h>
 #include <mono/metadata/profiler-private.h>
+#include <mono/metadata/mempool-internals.h>
 #include <mono/utils/mono-math.h>
 
 #include "mini.h"
 #define MONO_ARCH_CALLEE_XREGS 0
 
 #endif
+
+#define MONO_ARCH_BANK_MIRRORED -2
+
+#ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
+
+#ifndef MONO_ARCH_NEED_SIMD_BANK
+#error "MONO_ARCH_USE_SHARED_FP_SIMD_BANK needs MONO_ARCH_NEED_SIMD_BANK to work"
+#endif
+
+#define get_mirrored_bank(bank) (((bank) == MONO_REG_SIMD ) ? MONO_REG_DOUBLE : (((bank) == MONO_REG_DOUBLE ) ? MONO_REG_SIMD : -1))
+
+#define is_hreg_mirrored(rs, bank, hreg) ((rs)->symbolic [(bank)] [(hreg)] == MONO_ARCH_BANK_MIRRORED)
+
+
+#else
+
+
+#define get_mirrored_bank(bank) (-1)
+
+#define is_hreg_mirrored(rs, bank, hreg) (0)
+
+#endif
+
+
+/* If the bank is mirrored return the true logical bank that the register in the
+ * physical register bank is allocated to.
+ */
+static inline int translate_bank (MonoRegState *rs, int bank, int hreg) {
+       return is_hreg_mirrored (rs, bank, hreg) ? get_mirrored_bank (bank) : bank;
+}
+
 /*
  * Every hardware register belongs to a register type or register bank. bank 0 
  * contains the int registers, bank 1 contains the fp registers.
@@ -80,30 +113,18 @@ static const int regbank_spill_var_size[] = {
 
 #define DEBUG(a) MINI_DEBUG(cfg->verbose_level, 3, a;)
 
-static inline GSList*
-g_slist_append_mempool (MonoMemPool *mp, GSList *list, gpointer data)
-{
-       GSList *new_list;
-       GSList *last;
-       
-       new_list = mono_mempool_alloc (mp, sizeof (GSList));
-       new_list->data = data;
-       new_list->next = NULL;
-       
-       if (list) {
-               last = list;
-               while (last->next)
-                       last = last->next;
-               last->next = new_list;
-               
-               return list;
-       } else
-               return new_list;
-}
-
 static inline void
 mono_regstate_assign (MonoRegState *rs)
 {
+#ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
+       /* The regalloc may fail if fp and simd logical regbanks share the same physical reg bank and
+        * if the values here are not the same.
+        */
+       g_assert(regbank_callee_regs [MONO_REG_SIMD] == regbank_callee_regs [MONO_REG_DOUBLE]);
+       g_assert(regbank_callee_saved_regs [MONO_REG_SIMD] == regbank_callee_saved_regs [MONO_REG_DOUBLE]);
+       g_assert(regbank_size [MONO_REG_SIMD] == regbank_size [MONO_REG_DOUBLE]);
+#endif
+
        if (rs->next_vreg > rs->vassign_size) {
                g_free (rs->vassign);
                rs->vassign_size = MAX (rs->next_vreg, 256);
@@ -166,10 +187,17 @@ static inline int
 mono_regstate_alloc_general (MonoRegState *rs, regmask_t allow, int bank)
 {
        int i;
+       int mirrored_bank;
        regmask_t mask = allow & rs->free_mask [bank];
        for (i = 0; i < regbank_size [bank]; ++i) {
                if (mask & ((regmask_t)1 << i)) {
                        rs->free_mask [bank] &= ~ ((regmask_t)1 << i);
+
+                       mirrored_bank = get_mirrored_bank (bank);
+                       if (mirrored_bank == -1)
+                               return i;
+
+                       rs->free_mask [mirrored_bank] = rs->free_mask [bank];
                        return i;
                }
        }
@@ -179,9 +207,17 @@ mono_regstate_alloc_general (MonoRegState *rs, regmask_t allow, int bank)
 static inline void
 mono_regstate_free_general (MonoRegState *rs, int reg, int bank)
 {
+       int mirrored_bank;
+
        if (reg >= 0) {
                rs->free_mask [bank] |= (regmask_t)1 << reg;
                rs->symbolic [bank][reg] = 0;
+
+               mirrored_bank = get_mirrored_bank (bank);
+               if (mirrored_bank == -1)
+                       return;
+               rs->free_mask [mirrored_bank] = rs->free_mask [bank];
+               rs->symbolic [mirrored_bank][reg] = 0;
        }
 }
 
@@ -484,8 +520,6 @@ mono_print_ins_index (int i, MonoInst *ins)
        case OP_R4CONST:
                printf (" [%f]", *(float*)ins->inst_p0);
                break;
-       case CEE_CALL:
-       case CEE_CALLVIRT:
        case OP_CALL:
        case OP_CALL_MEMBASE:
        case OP_CALL_REG:
@@ -544,16 +578,6 @@ mono_print_ins_index (int i, MonoInst *ins)
        case OP_CALL_HANDLER:
                printf (" [B%d]", ins->inst_target_bb->block_num);
                break;
-       case CEE_BNE_UN:
-       case CEE_BEQ:
-       case CEE_BLT:
-       case CEE_BLT_UN:
-       case CEE_BGT:
-       case CEE_BGT_UN:
-       case CEE_BGE:
-       case CEE_BGE_UN:
-       case CEE_BLE:
-       case CEE_BLE_UN:
        case OP_IBNE_UN:
        case OP_IBEQ:
        case OP_IBLT:
@@ -659,6 +683,9 @@ get_register_force_spilling (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **la
        symbolic = rs->symbolic [bank];
        sel = rs->vassign [reg];
 
+       /* the vreg we need to spill lives in another logical reg bank */
+       bank = translate_bank (cfg->rs, bank, sel);
+
        /*i = rs->isymbolic [sel];
        g_assert (i == reg);*/
        i = reg;
@@ -727,6 +754,10 @@ get_register_spilling (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, Mo
                for (i = 0; i < regbank_size [bank]; ++i) {
                        if (regmask & (regmask (i))) {
                                sel = i;
+
+                               /* the vreg we need to load lives in another logical bank */
+                               bank = translate_bank (cfg->rs, bank, sel);
+
                                DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_regname_full (sel, bank), rs->symbolic [bank] [sel]));
                                break;
                        }
@@ -773,6 +804,7 @@ free_up_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *in
 {
        if (G_UNLIKELY (bank)) {
                if (!(cfg->rs->free_mask [1] & (regmask (hreg)))) {
+                       bank = translate_bank (cfg->rs, bank, hreg);
                        DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->symbolic [bank] [hreg]));
                        get_register_force_spilling (cfg, bb, last, ins, cfg->rs->symbolic [bank] [hreg], bank);
                        mono_regstate_free_general (cfg->rs, hreg, bank);
@@ -874,6 +906,8 @@ static inline void
 assign_reg (MonoCompile *cfg, MonoRegState *rs, int reg, int hreg, int bank)
 {
        if (G_UNLIKELY (bank)) {
+               int mirrored_bank;
+
                g_assert (reg >= regbank_size [bank]);
                g_assert (hreg < regbank_size [bank]);
                g_assert (! is_global_freg (hreg));
@@ -881,6 +915,21 @@ assign_reg (MonoCompile *cfg, MonoRegState *rs, int reg, int hreg, int bank)
                rs->vassign [reg] = hreg;
                rs->symbolic [bank] [hreg] = reg;
                rs->free_mask [bank] &= ~ (regmask (hreg));
+
+               mirrored_bank = get_mirrored_bank (bank);
+               if (mirrored_bank == -1)
+                       return;
+
+               /* Make sure the other logical reg bank that this bank shares
+                * a single hard reg bank knows that this hard reg is not free.
+                */
+               rs->free_mask [mirrored_bank] = rs->free_mask [bank];
+
+               /* Mark the other logical bank that the this bank shares
+                * a single hard reg bank with as mirrored.
+                */
+               rs->symbolic [mirrored_bank] [hreg] = MONO_ARCH_BANK_MIRRORED;
+
        }
        else {
                g_assert (reg >= MONO_MAX_IREGS);
@@ -907,6 +956,8 @@ get_callee_mask (const char spec)
 static gint8 desc_to_fixed_reg [256];
 static gboolean desc_to_fixed_reg_inited = FALSE;
 
+#ifndef DISABLE_JIT
+
 /*
  * Local register allocation.
  * We first scan the list of instructions and we save the liveness info of
@@ -940,6 +991,23 @@ mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                for (i = 0; i < 256; ++i)
                        desc_to_fixed_reg [i] = MONO_ARCH_INST_FIXED_REG (i);
                desc_to_fixed_reg_inited = TRUE;
+
+               /* Validate the cpu description against the info in mini-ops.h */
+#if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM)
+               for (i = OP_LOAD; i < OP_LAST; ++i) {
+                       const char *ispec;
+
+                       spec = ins_get_spec (i);
+                       ispec = INS_INFO (i);
+
+                       if ((spec [MONO_INST_DEST] && (ispec [MONO_INST_DEST] == ' ')))
+                               printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
+                       if ((spec [MONO_INST_SRC1] && (ispec [MONO_INST_SRC1] == ' ')))
+                               printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
+                       if ((spec [MONO_INST_SRC2] && (ispec [MONO_INST_SRC2] == ' ')))
+                               printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
+               }
+#endif
        }
 
        rs->next_vreg = bb->max_vreg;
@@ -1096,7 +1164,7 @@ mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                        ins->dreg = -1;
                }
 
-               if (spec [MONO_INST_CLOB] == 'c') {
+               if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
                        /* A call instruction implicitly uses all registers in call->out_ireg_args */
 
                        MonoCallInst *call = (MonoCallInst*)ins;
@@ -1481,8 +1549,12 @@ mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                        ins->dreg = dest_dreg;
 
                        if (G_UNLIKELY (bank)) {
-                               if (rs->symbolic [bank] [dest_dreg] >= regbank_size [bank])
-                                       free_up_reg (cfg, bb, tmp, ins, dest_dreg, bank);
+                               /* the register we need to free up may be used in another logical regbank
+                                * so do a translate just in case.
+                                */
+                               int translated_bank = translate_bank (cfg->rs, bank, dest_dreg);
+                               if (rs->symbolic [translated_bank] [dest_dreg] >= regbank_size [translated_bank])
+                                       free_up_reg (cfg, bb, tmp, ins, dest_dreg, translated_bank);
                        }
                        else {
                                if (rs->isymbolic [dest_dreg] >= MONO_MAX_IREGS)
@@ -1552,6 +1624,14 @@ mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                                                dreg = -1;
 
                                        for (j = 0; j < regbank_size [cur_bank]; ++j) {
+
+                                               /* we are looping though the banks in the outer loop
+                                                * so, we don't need to deal with mirrored hregs
+                                                * because we will get them in one of the other bank passes.
+                                                */
+                                               if (is_hreg_mirrored (rs, cur_bank, j))
+                                                       continue;
+
                                                s = regmask (j);
                                                if ((clob_mask & s) && !(rs->free_mask [cur_bank] & s) && (j != ins->sreg1)) {
                                                        if (j != dreg)
@@ -1569,7 +1649,7 @@ mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                /*
                 * TRACK ARGUMENT REGS
                 */
-               if (spec [MONO_INST_CLOB] == 'c') {
+               if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
                        MonoCallInst *call = (MonoCallInst*)ins;
                        GSList *list;
 
@@ -2062,7 +2142,6 @@ CompRelation
 mono_opcode_to_cond (int opcode)
 {
        switch (opcode) {
-       case CEE_BEQ:
        case OP_CEQ:
        case OP_IBEQ:
        case OP_ICEQ:
@@ -2075,7 +2154,6 @@ mono_opcode_to_cond (int opcode)
        case OP_CMOV_IEQ:
        case OP_CMOV_LEQ:
                return CMP_EQ;
-       case CEE_BNE_UN:
        case OP_IBNE_UN:
        case OP_LBNE_UN:
        case OP_FBNE_UN:
@@ -2084,21 +2162,18 @@ mono_opcode_to_cond (int opcode)
        case OP_CMOV_INE_UN:
        case OP_CMOV_LNE_UN:
                return CMP_NE;
-       case CEE_BLE:
        case OP_IBLE:
        case OP_LBLE:
        case OP_FBLE:
        case OP_CMOV_ILE:
        case OP_CMOV_LLE:
                return CMP_LE;
-       case CEE_BGE:
        case OP_IBGE:
        case OP_LBGE:
        case OP_FBGE:
        case OP_CMOV_IGE:
        case OP_CMOV_LGE:
                return CMP_GE;
-       case CEE_BLT:
        case OP_CLT:
        case OP_IBLT:
        case OP_ICLT:
@@ -2111,7 +2186,6 @@ mono_opcode_to_cond (int opcode)
        case OP_CMOV_ILT:
        case OP_CMOV_LLT:
                return CMP_LT;
-       case CEE_BGT:
        case OP_CGT:
        case OP_IBGT:
        case OP_ICGT:
@@ -2125,7 +2199,6 @@ mono_opcode_to_cond (int opcode)
        case OP_CMOV_LGT:
                return CMP_GT;
 
-       case CEE_BLE_UN:
        case OP_IBLE_UN:
        case OP_LBLE_UN:
        case OP_FBLE_UN:
@@ -2134,14 +2207,12 @@ mono_opcode_to_cond (int opcode)
        case OP_CMOV_ILE_UN:
        case OP_CMOV_LLE_UN:
                return CMP_LE_UN;
-       case CEE_BGE_UN:
        case OP_IBGE_UN:
        case OP_LBGE_UN:
        case OP_FBGE_UN:
        case OP_CMOV_IGE_UN:
        case OP_CMOV_LGE_UN:
                return CMP_GE_UN;
-       case CEE_BLT_UN:
        case OP_CLT_UN:
        case OP_IBLT_UN:
        case OP_ICLT_UN:
@@ -2154,7 +2225,6 @@ mono_opcode_to_cond (int opcode)
        case OP_CMOV_ILT_UN:
        case OP_CMOV_LLT_UN:
                return CMP_LT_UN;
-       case CEE_BGT_UN:
        case OP_CGT_UN:
        case OP_IBGT_UN:
        case OP_ICGT_UN:
@@ -2206,9 +2276,7 @@ mono_negate_cond (CompRelation cond)
 CompType
 mono_opcode_to_type (int opcode, int cmp_opcode)
 {
-       if ((opcode >= CEE_BEQ) && (opcode <= CEE_BLT_UN))
-               return CMP_TYPE_L;
-       else if ((opcode >= OP_CEQ) && (opcode <= OP_CLT_UN))
+       if ((opcode >= OP_CEQ) && (opcode <= OP_CLT_UN))
                return CMP_TYPE_L;
        else if ((opcode >= OP_IBEQ) && (opcode <= OP_IBLT_UN))
                return CMP_TYPE_I;
@@ -2239,6 +2307,8 @@ mono_opcode_to_type (int opcode, int cmp_opcode)
        }
 }
 
+#endif /* DISABLE_JIT */
+
 gboolean
 mono_is_regsize_var (MonoType *t)
 {
@@ -2279,6 +2349,8 @@ mono_is_regsize_var (MonoType *t)
        return FALSE;
 }
 
+#ifndef DISABLE_JIT
+
 /*
  * mono_peephole_ins:
  *
@@ -2424,7 +2496,7 @@ mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins)
                 * OP_MOVE sreg, dreg 
                 * OP_MOVE dreg, sreg
                 */
-               if (last_ins && last_ins->opcode == OP_MOVE &&
+               if (last_ins && last_ins->opcode == ins->opcode &&
                        ins->sreg1 == last_ins->dreg &&
                        ins->dreg == last_ins->sreg1) {
                        MONO_DELETE_INS (bb, ins);
@@ -2436,3 +2508,4 @@ mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins)
        }
 }
 
+#endif /* DISABLE_JIT */