[llvm] Load rgctx/imt arguments using a volatile load on arm to prevent LLVM from...
[mono.git] / mono / mini / decompose.c
index 29f2e348017cabd5ca9768b998ffcda681056d63..88097b2257897627e388ca8babc88c40cddb2a88 100644 (file)
@@ -17,7 +17,7 @@
 #ifndef DISABLE_JIT
 
 /* FIXME: This conflicts with the definition in mini.c, so it cannot be moved to mini.h */
-MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
+MONO_API MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
 void mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native);
 void mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass);
 
@@ -269,6 +269,7 @@ mono_decompose_opcode (MonoCompile *cfg, MonoInst *ins)
        MonoInst *repl = NULL;
        int type = ins->type;
        int dreg = ins->dreg;
+       gboolean emulate = FALSE;
 
        /* FIXME: Instead of = NOP, don't emit the original ins at all */
 
@@ -410,18 +411,55 @@ mono_decompose_opcode (MonoCompile *cfg, MonoInst *ins)
                cfg->exception_message = g_strdup_printf ("float conv.ovf.un opcodes not supported.");
                break;
 
-       default: {
-               MonoJitICallInfo *info;
+#if defined(MONO_ARCH_EMULATE_DIV) && defined(MONO_ARCH_HAVE_OPCODE_NEEDS_EMULATION)
+       case OP_IDIV:
+       case OP_IREM:
+       case OP_IDIV_UN:
+       case OP_IREM_UN:
+               if (!mono_arch_opcode_needs_emulation (cfg, ins->opcode)) {
+#ifdef MONO_ARCH_NEED_DIV_CHECK
+                       int reg1 = alloc_ireg (cfg);
+                       int reg2 = alloc_ireg (cfg);
+                       /* b == 0 */
+                       MONO_EMIT_NEW_ICOMPARE_IMM (cfg, ins->sreg2, 0);
+                       MONO_EMIT_NEW_COND_EXC (cfg, IEQ, "DivideByZeroException");
+                       if (ins->opcode == OP_IDIV || ins->opcode == OP_IREM) {
+                               /* b == -1 && a == 0x80000000 */
+                               MONO_EMIT_NEW_ICOMPARE_IMM (cfg, ins->sreg2, -1);
+                               MONO_EMIT_NEW_UNALU (cfg, OP_ICEQ, reg1, -1);
+                               MONO_EMIT_NEW_ICOMPARE_IMM (cfg, ins->sreg1, 0x80000000);
+                               MONO_EMIT_NEW_UNALU (cfg, OP_ICEQ, reg2, -1);
+                               MONO_EMIT_NEW_BIALU (cfg, OP_IAND, reg1, reg1, reg2);
+                               MONO_EMIT_NEW_ICOMPARE_IMM (cfg, reg1, 1);
+                               MONO_EMIT_NEW_COND_EXC (cfg, IEQ, "DivideByZeroException");
+                       }
+#endif
+                       MONO_EMIT_NEW_BIALU (cfg, ins->opcode, ins->dreg, ins->sreg1, ins->sreg2);
+                       ins->opcode = OP_NOP;
+               } else {
+                       emulate = TRUE;
+               }
+               break;
+#endif
+
+       default:
+               emulate = TRUE;
+               break;
+       }
+
+       if (emulate) {
+               MonoJitICallInfo *info = NULL;
 
 #if SIZEOF_REGISTER == 8
                if (decompose_long_opcode (cfg, ins, &repl))
-                       break;
+                       emulate = FALSE;
 #else
                if (COMPILE_LLVM (cfg) && decompose_long_opcode (cfg, ins, &repl))
-                       break;
+                       emulate = FALSE;
 #endif
 
-               info = mono_find_jit_opcode_emulation (ins->opcode);
+               if (emulate)
+                       info = mono_find_jit_opcode_emulation (ins->opcode);
                if (info) {
                        MonoInst **args;
                        MonoInst *call;
@@ -447,8 +485,6 @@ mono_decompose_opcode (MonoCompile *cfg, MonoInst *ins)
 
                        NULLIFY_INS (ins);
                }
-               break;
-       }
        }
 
        if (ins->opcode == OP_NOP) {
@@ -1325,6 +1361,71 @@ mono_decompose_vtype_opts (MonoCompile *cfg)
        }
 }
 
+void
+mono_decompose_vtype_opts_llvm (MonoCompile *cfg)
+{
+       MonoBasicBlock *bb, *first_bb;
+
+       /* Decompose only the OP_STOREV_MEMBASE opcodes, which need write barriers */
+
+       cfg->cbb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
+       first_bb = cfg->cbb;
+
+       for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+               MonoInst *ins;
+               MonoInst *prev = NULL;
+               MonoInst *src_var, *src, *dest;
+               gboolean restart;
+               int dreg;
+
+               if (cfg->verbose_level > 2) mono_print_bb (bb, "BEFORE LOWER-VTYPE-OPTS(LLVM) ");
+
+               cfg->cbb->code = cfg->cbb->last_ins = NULL;
+               restart = TRUE;
+
+               while (restart) {
+                       restart = FALSE;
+
+                       for (ins = bb->code; ins; ins = ins->next) {
+                               switch (ins->opcode) {
+                               case OP_STOREV_MEMBASE: {
+                                       src_var = get_vreg_to_inst (cfg, ins->sreg1);
+
+                                       if (!src_var) {
+                                               g_assert (ins->klass);
+                                               src_var = mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, ins->sreg1);
+                                       }
+
+                                       EMIT_NEW_VARLOADA_VREG ((cfg), (src), ins->sreg1, &ins->klass->byval_arg);
+
+                                       dreg = alloc_preg (cfg);
+                                       EMIT_NEW_BIALU_IMM (cfg, dest, OP_ADD_IMM, dreg, ins->inst_destbasereg, ins->inst_offset);
+                                       mini_emit_stobj (cfg, dest, src, src_var->klass, src_var->backend.is_pinvoke);
+                                       break;
+                               }
+                               default:
+                                       break;
+                               }
+
+                               g_assert (cfg->cbb == first_bb);
+
+                               if (cfg->cbb->code || (cfg->cbb != first_bb)) {
+                                       /* Replace the original instruction with the new code sequence */
+
+                                       mono_replace_ins (cfg, bb, ins, &prev, first_bb, cfg->cbb);
+                                       first_bb->code = first_bb->last_ins = NULL;
+                                       first_bb->in_count = first_bb->out_count = 0;
+                                       cfg->cbb = first_bb;
+                               }
+                               else
+                                       prev = ins;
+                       }
+               }
+
+               if (cfg->verbose_level > 2) mono_print_bb (bb, "AFTER LOWER-VTYPE-OPTS(LLVM) ");
+       }
+}
+
 inline static MonoInst *
 mono_get_domainvar (MonoCompile *cfg)
 {
@@ -1377,7 +1478,7 @@ mono_decompose_array_access_opts (MonoCompile *cfg)
                                switch (ins->opcode) {
                                case OP_LDLEN:
                                        NEW_LOAD_MEMBASE_FLAGS (cfg, dest, OP_LOADI4_MEMBASE, ins->dreg, ins->sreg1,
-                                                                                       G_STRUCT_OFFSET (MonoArray, max_length), ins->flags | MONO_INST_CONSTANT_LOAD);
+                                                                                       G_STRUCT_OFFSET (MonoArray, max_length), ins->flags | MONO_INST_INVARIANT_LOAD);
                                        MONO_ADD_INS (cfg->cbb, dest);
                                        break;
                                case OP_BOUNDS_CHECK:
@@ -1397,8 +1498,9 @@ mono_decompose_array_access_opts (MonoCompile *cfg)
                                                dest = mono_emit_jit_icall (cfg, mono_array_new, iargs);
                                                dest->dreg = ins->dreg;
                                        } else {
-                                               MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (ins->inst_newa_class, 1));
-                                               MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (vtable, 1);
+                                               MonoClass *array_class = mono_array_class_get (ins->inst_newa_class, 1);
+                                               MonoVTable *vtable = mono_class_vtable (cfg->domain, array_class);
+                                               MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
 
                                                g_assert (vtable); /*This shall not fail since we check for this condition on OP_NEWARR creation*/
                                                NEW_VTABLECONST (cfg, iargs [0], vtable);
@@ -1415,7 +1517,7 @@ mono_decompose_array_access_opts (MonoCompile *cfg)
                                        break;
                                case OP_STRLEN:
                                        MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS (cfg, OP_LOADI4_MEMBASE, ins->dreg,
-                                                                                                                ins->sreg1, G_STRUCT_OFFSET (MonoString, length), ins->flags | MONO_INST_CONSTANT_LOAD);
+                                                                                                                ins->sreg1, G_STRUCT_OFFSET (MonoString, length), ins->flags | MONO_INST_INVARIANT_LOAD);
                                        break;
                                default:
                                        break;
@@ -1446,7 +1548,7 @@ typedef union {
        double vald;
 } DVal;
 
-#ifdef MONO_ARCH_SOFT_FLOAT
+#ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
 
 /**
  * mono_decompose_soft_float: