Merge pull request #3244 from henricm/fix-for-synchronization-attribute
[mono.git] / mono / mini / simd-intrinsics.c
index 0324d22a99b7f3662eccf15dfd1509536b06d89d..6bffbd05c4927183446441b0f290e53e1595debf 100644 (file)
@@ -28,7 +28,7 @@ TODO add support for fusing a XMOVE into a simd op in mono_spill_global_vars.
 TODO add stuff to man pages
 TODO document this under /docs
 TODO make passing a xmm as argument not cause it to be LDADDR'ed (introduce an OP_XPUSH)
-TODO revamp the .ctor sequence as it looks very fragile, maybe use a var just like iconv_to_r8_raw. (or just pinst sse ops) 
+TODO revamp the .ctor sequence as it looks very fragile, maybe use a var just like move_i4_to_f. (or just pinst sse ops) 
 TODO figure out what's wrong with OP_STOREX_MEMBASE_REG and OP_STOREX_MEMBASE (the 2nd is for imm operands)
 TODO maybe add SSE3 emulation on top of SSE2, or just implement the corresponding functions using SSE2 intrinsics.
 TODO pass simd arguments in registers or, at least, add SSE support for pushing large (>=16) valuetypes 
@@ -61,7 +61,16 @@ The advantage of this change is that it could have a _membase version and promot
 without a OP_LDADDR.
 */
 
-#ifdef MONO_ARCH_SIMD_INTRINSICS
+#if defined (MONO_ARCH_SIMD_INTRINSICS)
+
+#if defined (DISABLE_JIT)
+
+void
+mono_simd_intrinsics_init (void)
+{
+}
+
+#else
 
 //#define IS_DEBUG_ON(cfg) (0)
 
@@ -677,7 +686,7 @@ mono_simd_simplify_indirection (MonoCompile *cfg)
        }
 
        DEBUG (printf ("[simd-simplify] max vreg is %d\n", max_vreg));
-       vreg_flags = g_malloc0 (max_vreg + 1);
+       vreg_flags = (char *)g_malloc0 (max_vreg + 1);
        target_bb = g_new0 (MonoBasicBlock*, max_vreg + 1);
 
        for (i = 0; i < cfg->num_varinfo; i++) {
@@ -772,13 +781,15 @@ mono_simd_simplify_indirection (MonoCompile *cfg)
 
                        num_sregs = mono_inst_get_src_registers (ins, sregs);
                        for (j = 0; j < num_sregs; ++j) {
-                               if (sregs [i] == var->dreg)
+                               if (sregs [j] == var->dreg)
                                        found = TRUE;
                        }
                        /*We can avoid inserting the XZERO if the first use doesn't depend on the zero'ed value.*/
                        if (ins->dreg == var->dreg && !found) {
+                               DEBUG (printf ("[simd-simplify] INGORING R%d on BB %d because first op is a def", i, target_bb [var->dreg]->block_num););
                                break;
                        } else if (found) {
+                               DEBUG (printf ("[simd-simplify] Adding XZERO for R%d on BB %d: ", i, target_bb [var->dreg]->block_num); );
                                MonoInst *tmp;
                                MONO_INST_NEW (cfg, tmp, OP_XZERO);
                                tmp->dreg = var->dreg;
@@ -791,8 +802,10 @@ mono_simd_simplify_indirection (MonoCompile *cfg)
        }
 
        for (ins = first_bb->code; ins; ins = ins->next) {
-               if (ins->opcode == OP_XZERO && (vreg_flags [ins->dreg] & VREG_SINGLE_BB_USE))
+               if (ins->opcode == OP_XZERO && (vreg_flags [ins->dreg] & VREG_SINGLE_BB_USE)) {
+                       DEBUG (printf ("[simd-simplify] Nullify %d on first BB: ", ins->dreg); mono_print_ins(ins));
                        NULLIFY_INS (ins);
+               }
        }
 
        g_free (vreg_flags);
@@ -849,16 +862,6 @@ load_simd_vreg (MonoCompile *cfg, MonoMethod *cmethod, MonoInst *src, gboolean *
        g_assert_not_reached ();
 }
 
-static MonoInst*
-get_int_to_float_spill_area (MonoCompile *cfg)
-{
-       if (!cfg->iconv_raw_var) {
-               cfg->iconv_raw_var = mono_compile_create_var (cfg, &mono_defaults.int32_class->byval_arg, OP_LOCAL);
-               cfg->iconv_raw_var->flags |= MONO_INST_VOLATILE; /*FIXME, use the don't regalloc flag*/
-       }       
-       return cfg->iconv_raw_var;
-}
-
 /*We share the var with fconv_to_r8_x to save some stack space.*/
 static MonoInst*
 get_double_spill_area (MonoCompile *cfg)
@@ -926,7 +929,7 @@ get_simd_vreg_or_expanded_scalar (MonoCompile *cfg, MonoMethod *cmethod, MonoIns
        MONO_ADD_INS (cfg->cbb, ins);
 
        if (expand_op == OP_EXPAND_R4)
-               ins->backend.spill_var = get_int_to_float_spill_area (cfg);
+               ins->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
        else if (expand_op == OP_EXPAND_R8)
                ins->backend.spill_var = get_double_spill_area (cfg);
 
@@ -1088,7 +1091,7 @@ simd_intrinsic_emit_setter (const SimdIntrinsc *intrinsic, MonoCompile *cfg, Mon
                ins->sreg2 = args [1]->dreg;
                ins->inst_c0 = intrinsic->opcode;
                if (sig->params [0]->type == MONO_TYPE_R4)
-                       ins->backend.spill_var = get_int_to_float_spill_area (cfg);
+                       ins->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
                else if (sig->params [0]->type == MONO_TYPE_R8)
                        ins->backend.spill_var = get_double_spill_area (cfg);
                MONO_ADD_INS (cfg->cbb, ins);
@@ -1153,13 +1156,13 @@ simd_intrinsic_emit_getter (const SimdIntrinsc *intrinsic, MonoCompile *cfg, Mon
        MONO_ADD_INS (cfg->cbb, ins);
 
        if (sig->ret->type == MONO_TYPE_R4) {
-               MONO_INST_NEW (cfg, ins, OP_ICONV_TO_R8_RAW);
+               MONO_INST_NEW (cfg, ins, cfg->r4fp ? OP_ICONV_TO_R4_RAW : OP_MOVE_I4_TO_F);
                ins->klass = mono_defaults.single_class;
                ins->sreg1 = vreg;
-               ins->type = STACK_R8;
+               ins->type = cfg->r4_stack_type;
                ins->dreg = alloc_freg (cfg);
-               ins->backend.spill_var = get_int_to_float_spill_area (cfg);
-               MONO_ADD_INS (cfg->cbb, ins);   
+               ins->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
+               MONO_ADD_INS (cfg->cbb, ins);
        }
        return ins;
 }
@@ -1219,7 +1222,7 @@ simd_intrinsic_emit_ctor (const SimdIntrinsc *intrinsic, MonoCompile *cfg, MonoM
 
                MONO_ADD_INS (cfg->cbb, ins);
                if (sig->params [0]->type == MONO_TYPE_R4)
-                       ins->backend.spill_var = get_int_to_float_spill_area (cfg);
+                       ins->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
                else if (sig->params [0]->type == MONO_TYPE_R8)
                        ins->backend.spill_var = get_double_spill_area (cfg);
 
@@ -1479,7 +1482,7 @@ simd_version_name (guint32 version)
 static MonoInst*
 emit_intrinsics (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args, const SimdIntrinsc *intrinsics, guint32 size)
 {
-       const SimdIntrinsc * result = mono_binary_search (cmethod->name, intrinsics, size, sizeof (SimdIntrinsc), &simd_intrinsic_compare_by_name);
+       const SimdIntrinsc *result = (const SimdIntrinsc *)mono_binary_search (cmethod->name, intrinsics, size, sizeof (SimdIntrinsc), &simd_intrinsic_compare_by_name);
        if (!result) {
                DEBUG (printf ("function doesn't have a simd intrinsic %s::%s/%d\n", cmethod->klass->name, cmethod->name, fsig->param_count));
                return NULL;
@@ -1679,4 +1682,5 @@ mono_emit_simd_intrinsics (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSign
        return NULL;
 }
 
-#endif
+#endif /* DISABLE_JIT */
+#endif /* MONO_ARCH_SIMD_INTRINSICS */