Merge pull request #1109 from adbre/iss358
[mono.git] / mono / mini / local-propagation.c
index 363e7b03860d1f110c95b4804aaca0e51a1eb68f..7bdb517bd2bf5724a85a0a70c98d103ce7701b37 100644 (file)
@@ -9,8 +9,11 @@
  *   Massimiliano Mantione (massi@ximian.com)
  *
  * (C) 2006 Novell, Inc.  http://www.novell.com
+ * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
  */
 
+#include <config.h>
+#ifndef DISABLE_JIT
 
 #include <string.h>
 #include <stdio.h>
@@ -64,23 +67,25 @@ restart:
 
                /* Manually init the defs entries used by the bblock */
                MONO_BB_FOR_EACH_INS (bb, ins) {
+                       int sregs [MONO_MAX_SRC_REGS];
+                       int num_sregs, i;
+
                        if ((ins->dreg != -1) && (ins->dreg < max)) {
                                defs [ins->dreg] = NULL;
 #if SIZEOF_REGISTER == 4
                                defs [ins->dreg + 1] = NULL;
 #endif
                        }
-                       if ((ins->sreg1 != -1) && (ins->sreg1 < max)) {
-                               defs [ins->sreg1] = NULL;
-#if SIZEOF_REGISTER == 4
-                               defs [ins->sreg1 + 1] = NULL;
-#endif
-                       }
-                       if ((ins->sreg2 != -1) && (ins->sreg2 < max)) {
-                               defs [ins->sreg2] = NULL;
+
+                       num_sregs = mono_inst_get_src_registers (ins, sregs);
+                       for (i = 0; i < num_sregs; ++i) {
+                               int sreg = sregs [i];
+                               if (sreg < max) {
+                                       defs [sreg] = NULL;
 #if SIZEOF_REGISTER == 4
-                               defs [ins->sreg2 + 1] = NULL;
+                                       defs [sreg + 1] = NULL;
 #endif
+                               }
                        }
                }
 
@@ -89,6 +94,8 @@ restart:
                MONO_BB_FOR_EACH_INS (bb, ins) {
                        const char *spec = INS_INFO (ins->opcode);
                        int regtype, srcindex, sreg;
+                       int num_sregs;
+                       int sregs [MONO_MAX_SRC_REGS];
 
                        if (ins->opcode == OP_NOP) {
                                MONO_DELETE_INS (bb, ins);
@@ -117,17 +124,21 @@ restart:
 
                                        if ((def->opcode == OP_MOVE) && (!defs [def->sreg1] || (def_index [def->sreg1] < def_index [sreg])) && !vreg_is_volatile (cfg, def->sreg1)) {
                                                int vreg = def->sreg1;
-                                               //printf ("CCOPY: R%d -> R%d\n", sreg, vreg);
+                                               if (cfg->verbose_level > 2) printf ("CCOPY: R%d -> R%d\n", sreg, vreg);
                                                ins->dreg = vreg;
                                        }
                                }
                        }
 
-                       for (srcindex = 0; srcindex < 2; ++srcindex) {
+                       num_sregs = mono_inst_get_src_registers (ins, sregs);
+                       for (srcindex = 0; srcindex < num_sregs; ++srcindex) {
                                MonoInst *def;
+                               int nregs;
+
+                               nregs = mono_inst_get_src_registers (ins, sregs);
 
-                               regtype = srcindex == 0 ? spec [MONO_INST_SRC1] : spec [MONO_INST_SRC2];
-                               sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
+                               regtype = spec [MONO_INST_SRC1 + srcindex];
+                               sreg = sregs [srcindex];
 
                                if ((regtype == ' ') || (sreg == -1) || (!defs [sreg]))
                                        continue;
@@ -162,11 +173,9 @@ restart:
                                        (def->opcode != OP_FMOVE)) {
                                        int vreg = def->sreg1;
 
-                                       //printf ("CCOPY: R%d -> R%d\n", sreg, vreg);
-                                       if (srcindex == 0)
-                                               ins->sreg1 = vreg;
-                                       else
-                                               ins->sreg2 = vreg;
+                                       if (cfg->verbose_level > 2) printf ("CCOPY/2: R%d -> R%d\n", sreg, vreg);
+                                       sregs [srcindex] = vreg;
+                                       mono_inst_set_src_registers (ins, sregs);
 
                                        /* Allow further iterations */
                                        srcindex = -1;
@@ -209,10 +218,8 @@ restart:
                                                } else {
                                                        ins->inst_imm = def->inst_c0;
                                                }
-                                               if (srcindex == 0)
-                                                       ins->sreg1 = -1;
-                                               else
-                                                       ins->sreg2 = -1;
+                                               sregs [srcindex] = -1;
+                                               mono_inst_set_src_registers (ins, sregs);
 
                                                if ((opcode2 == OP_VOIDCALL) || (opcode2 == OP_CALL) || (opcode2 == OP_LCALL) || (opcode2 == OP_FCALL))
                                                        ((MonoCallInst*)ins)->fptr = (gpointer)ins->inst_imm;
@@ -223,7 +230,7 @@ restart:
                                        }
                                        else {
                                                /* Special cases */
-#if defined(__i386__) || defined(__x86_64__)
+#if defined(TARGET_X86) || defined(TARGET_AMD64)
                                                if ((ins->opcode == OP_X86_LEA) && (srcindex == 1)) {
 #if SIZEOF_REGISTER == 8
                                                        /* FIXME: Use OP_PADD_IMM when the new JIT is done */
@@ -253,10 +260,10 @@ restart:
                                                ins->inst_basereg = def->sreg1;
                                                ins->inst_offset += def->inst_imm;
                                        }
-                               } else if ((ins->opcode == OP_ISUB_IMM) && (def->opcode == OP_IADD_IMM) && (def->next == ins)) {
+                               } else if ((ins->opcode == OP_ISUB_IMM) && (def->opcode == OP_IADD_IMM) && (def->next == ins) && (def->dreg != def->sreg1)) {
                                        ins->sreg1 = def->sreg1;
                                        ins->inst_imm -= def->inst_imm;
-                               } else if ((ins->opcode == OP_IADD_IMM) && (def->opcode == OP_ISUB_IMM) && (def->next == ins)) {
+                               } else if ((ins->opcode == OP_IADD_IMM) && (def->opcode == OP_ISUB_IMM) && (def->next == ins) && (def->dreg != def->sreg1)) {
                                        ins->sreg1 = def->sreg1;
                                        ins->inst_imm -= def->inst_imm;
                                } else if (ins->opcode == OP_STOREI1_MEMBASE_REG &&
@@ -406,10 +413,10 @@ restart:
 static inline gboolean
 reg_is_softreg_no_fpstack (int reg, const char spec)
 {
-       return (spec == 'i' && reg > MONO_MAX_IREGS)
-               || ((spec == 'f' && reg > MONO_MAX_FREGS) && !MONO_ARCH_USE_FPSTACK)
+       return (spec == 'i' && reg >= MONO_MAX_IREGS)
+               || ((spec == 'f' && reg >= MONO_MAX_FREGS) && !MONO_ARCH_USE_FPSTACK)
 #ifdef MONO_ARCH_SIMD_INTRINSICS
-               || (spec == 'x' && reg > MONO_MAX_XREGS)
+               || (spec == 'x' && reg >= MONO_MAX_XREGS)
 #endif
                || (spec == 'v');
 }
@@ -417,14 +424,38 @@ reg_is_softreg_no_fpstack (int reg, const char spec)
 static inline gboolean
 reg_is_softreg (int reg, const char spec)
 {
-       return (spec == 'i' && reg > MONO_MAX_IREGS)
-               || (spec == 'f' && reg > MONO_MAX_FREGS)
+       return (spec == 'i' && reg >= MONO_MAX_IREGS)
+               || (spec == 'f' && reg >= MONO_MAX_FREGS)
 #ifdef MONO_ARCH_SIMD_INTRINSICS
-               || (spec == 'x' && reg > MONO_MAX_XREGS)
+               || (spec == 'x' && reg >= MONO_MAX_XREGS)
 #endif
                || (spec == 'v');
 }
 
+static inline gboolean
+mono_is_simd_accessor (MonoInst *ins)
+{
+       switch (ins->opcode) {
+#ifdef MONO_ARCH_SIMD_INTRINSICS
+       case OP_INSERT_I1:
+       case OP_INSERT_I2:
+       case OP_INSERT_I4:
+       case OP_INSERT_I8:
+       case OP_INSERT_R4:
+       case OP_INSERT_R8:
+
+       case OP_INSERTX_U1_SLOW:
+       case OP_INSERTX_I4_SLOW:
+       case OP_INSERTX_R4_SLOW:
+       case OP_INSERTX_R8_SLOW:
+       case OP_INSERTX_I8_SLOW:
+               return TRUE;
+#endif
+       default:
+               return FALSE;
+       }
+}
+
 /**
  * mono_local_deadce:
  *
@@ -453,6 +484,8 @@ mono_local_deadce (MonoCompile *cfg)
                /* Manually init the defs entries used by the bblock */
                MONO_BB_FOR_EACH_INS (bb, ins) {
                        const char *spec = INS_INFO (ins->opcode);
+                       int sregs [MONO_MAX_SRC_REGS];
+                       int num_sregs, i;
 
                        if (spec [MONO_INST_DEST] != ' ') {
                                mono_bitset_clear_fast (used, ins->dreg);
@@ -463,16 +496,11 @@ mono_local_deadce (MonoCompile *cfg)
                                mono_bitset_clear_fast (defined, ins->dreg + 1);
 #endif
                        }
-                       if (spec [MONO_INST_SRC1] != ' ') {
-                               mono_bitset_clear_fast (used, ins->sreg1);
+                       num_sregs = mono_inst_get_src_registers (ins, sregs);
+                       for (i = 0; i < num_sregs; ++i) {
+                               mono_bitset_clear_fast (used, sregs [i]);
 #if SIZEOF_REGISTER == 4
-                               mono_bitset_clear_fast (used, ins->sreg1 + 1);
-#endif
-                       }
-                       if (spec [MONO_INST_SRC2] != ' ') {
-                               mono_bitset_clear_fast (used, ins->sreg2);
-#if SIZEOF_REGISTER == 4
-                               mono_bitset_clear_fast (used, ins->sreg2 + 1);
+                               mono_bitset_clear_fast (used, sregs [i] + 1);
 #endif
                        }
                }
@@ -482,6 +510,8 @@ mono_local_deadce (MonoCompile *cfg)
                 */
                MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
                        const char *spec = INS_INFO (ins->opcode);
+                       int sregs [MONO_MAX_SRC_REGS];
+                       int num_sregs, i;
 
                        if (ins->opcode == OP_NOP) {
                                MONO_DELETE_INS (bb, ins);
@@ -505,7 +535,7 @@ mono_local_deadce (MonoCompile *cfg)
                                 * This isn't copyprop, not deadce, but it can only be performed
                                 * after handle_global_vregs () has run.
                                 */
-                               if (!get_vreg_to_inst (cfg, ins->sreg1) && (spec2 [MONO_INST_DEST] != ' ') && (def->dreg == ins->sreg1) && !mono_bitset_test_fast (used, ins->sreg1) && !MONO_IS_STORE_MEMBASE (def) && reg_is_softreg (ins->sreg1, spec [MONO_INST_DEST])) {
+                               if (!get_vreg_to_inst (cfg, ins->sreg1) && (spec2 [MONO_INST_DEST] != ' ') && (def->dreg == ins->sreg1) && !mono_bitset_test_fast (used, ins->sreg1) && !MONO_IS_STORE_MEMBASE (def) && reg_is_softreg (ins->sreg1, spec [MONO_INST_DEST]) && !mono_is_simd_accessor (def)) {
                                        if (cfg->verbose_level > 2) {
                                                printf ("\tReverse copyprop in BB%d on ", bb->block_num);
                                                mono_print_ins (ins);
@@ -546,10 +576,9 @@ mono_local_deadce (MonoCompile *cfg)
 
                        if (spec [MONO_INST_DEST] != ' ')
                                mono_bitset_set_fast (defined, ins->dreg);
-                       if (spec [MONO_INST_SRC1] != ' ')
-                               mono_bitset_set_fast (used, ins->sreg1);
-                       if (spec [MONO_INST_SRC2] != ' ')
-                               mono_bitset_set_fast (used, ins->sreg2);
+                       num_sregs = mono_inst_get_src_registers (ins, sregs);
+                       for (i = 0; i < num_sregs; ++i)
+                               mono_bitset_set_fast (used, sregs [i]);
                        if (MONO_IS_STORE_MEMBASE (ins))
                                mono_bitset_set_fast (used, ins->dreg);
 
@@ -584,3 +613,5 @@ mono_local_deadce (MonoCompile *cfg)
 
        //mono_print_code (cfg, "AFTER LOCAL-DEADCE");
 }
+
+#endif /* DISABLE_JIT */