[jit] Add a new ICALL_ADDR_CALL patch type which is only used on calls, since ICALL_A...
[mono.git] / mono / mini / ir-emit.h
index 44f5df686264554a53f3753fd4be808b9fa87f14..8f7b81b30a217b43f87cf259b4fd02b59454e039 100644 (file)
@@ -44,13 +44,13 @@ alloc_lreg (MonoCompile *cfg)
 static inline guint32
 alloc_freg (MonoCompile *cfg)
 {
-#ifdef MONO_ARCH_SOFT_FLOAT
-       /* Allocate an lvreg so float ops can be decomposed into long ops */
-       return alloc_lreg (cfg);
-#else
-       /* Allocate these from the same pool as the int regs */
-       return cfg->next_vreg ++;
-#endif
+       if (mono_arch_is_soft_float ()) {
+               /* Allocate an lvreg so float ops can be decomposed into long ops */
+               return alloc_lreg (cfg);
+       } else {
+               /* Allocate these from the same pool as the int regs */
+               return cfg->next_vreg ++;
+       }
 }
 
 static inline guint32
@@ -86,6 +86,7 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
                return alloc_ireg_mp (cfg);
        case STACK_OBJ:
                return alloc_ireg_ref (cfg);
+       case STACK_R4:
        case STACK_R8:
                return alloc_freg (cfg);
        case STACK_I8:
@@ -105,7 +106,7 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
  * JIT code still uses the left and right fields, so it has to stay.
  */
 #define MONO_INST_NEW(cfg,dest,op) do {        \
-               (dest) = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoInst));        \
+               (dest) = (MonoInst *)mono_mempool_alloc ((cfg)->mempool, sizeof (MonoInst));    \
                (dest)->inst_c0 = (dest)->inst_c1 = 0; \
                (dest)->next = (dest)->prev = NULL;    \
                (dest)->opcode = (op);  \
@@ -229,7 +230,7 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do {    \
         MONO_INST_NEW ((cfg), (dest), cfg->compile_aot ? OP_AOTCONST : OP_PCONST); \
                (dest)->inst_p0 = (cons);       \
-               (dest)->inst_i1 = (gpointer)(patch_type); \
+               (dest)->inst_i1 = (MonoInst *)(patch_type); \
                (dest)->type = STACK_PTR;       \
                (dest)->dreg = alloc_dreg ((cfg), STACK_PTR);   \
     } while (0)
@@ -259,10 +260,21 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
 
 #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), NULL, STACK_OBJ, mono_defaults.string_class)
 
+#define NEW_LDSTRLITCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_LDSTR_LIT, (val))
+
 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token,generic_context) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), (generic_context), STACK_OBJ, mono_defaults.monotype_class)
 
 #define NEW_LDTOKENCONST(cfg,dest,image,token,generic_context) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), (generic_context), STACK_PTR, NULL)
 
+#define NEW_TLS_OFFSETCONST(cfg,dest,key) do { \
+       if (cfg->compile_aot) { \
+               NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_TLS_OFFSET, GINT_TO_POINTER (key)); \
+       } else {                                                                                                                        \
+               int _offset = mini_get_tls_offset ((key));                                                      \
+               NEW_PCONST ((cfg), (dest), GINT_TO_POINTER (_offset)); \
+               } \
+       } while (0)
+
 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
                if (cfg->compile_aot) { \
                        NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, NULL, STACK_OBJ, NULL); \
@@ -293,23 +305,31 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
 
 #define NEW_JIT_ICALL_ADDRCONST(cfg,dest,name) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_JIT_ICALL_ADDR, (name))
 
-#define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
-
 #define NEW_VARLOAD(cfg,dest,var,vartype) do { \
         MONO_INST_NEW ((cfg), (dest), OP_MOVE); \
                (dest)->opcode = mono_type_to_regmove ((cfg), (vartype));  \
                type_to_eval_stack_type ((cfg), (vartype), (dest));     \
                (dest)->klass = var->klass;     \
                (dest)->sreg1 = var->dreg;   \
-        (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
+        (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
         if ((dest)->opcode == OP_VMOVE) (dest)->klass = mono_class_from_mono_type ((vartype)); \
        } while (0)
 
-#ifdef MONO_ARCH_SOFT_FLOAT
-#define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8 || (stack_type) == STACK_R8)
-#else
-#define DECOMPOSE_INTO_REGPAIR(stack_type) ((stack_type) == STACK_I8)
-#endif
+#define DECOMPOSE_INTO_REGPAIR(stack_type) (mono_arch_is_soft_float () ? ((stack_type) == STACK_I8 || (stack_type) == STACK_R8) : ((stack_type) == STACK_I8))
+
+static inline void
+handle_gsharedvt_ldaddr (MonoCompile *cfg)
+{
+       /* The decomposition of ldaddr makes use of these two variables, so add uses for them */
+       MonoInst *use;
+
+       MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
+       use->sreg1 = cfg->gsharedvt_info_var->dreg;
+       MONO_ADD_INS (cfg->cbb, use);
+       MONO_INST_NEW (cfg, use, OP_DUMMY_USE);
+       use->sreg1 = cfg->gsharedvt_locals_var->dreg;
+       MONO_ADD_INS (cfg->cbb, use);
+}
 
 #define NEW_VARLOADA(cfg,dest,var,vartype) do {        \
         MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
@@ -318,7 +338,9 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
                (dest)->type = STACK_MP;        \
                (dest)->klass = (var)->klass;   \
         (dest)->dreg = alloc_dreg ((cfg), STACK_MP); \
-               if (SIZEOF_REGISTER == 4 && DECOMPOSE_INTO_REGPAIR ((var)->type)) { MonoInst *var1 = get_vreg_to_inst (cfg, (var)->dreg + 1); MonoInst *var2 = get_vreg_to_inst (cfg, (var)->dreg + 2); g_assert (var1); g_assert (var2); var1->flags |= MONO_INST_INDIRECT; var2->flags |= MONO_INST_INDIRECT; } \
+               (cfg)->has_indirection = TRUE;  \
+                         if (G_UNLIKELY (cfg->gsharedvt) && mini_is_gsharedvt_variable_type ((var)->inst_vtype)) { handle_gsharedvt_ldaddr ((cfg)); } \
+               if (SIZEOF_REGISTER == 4 && DECOMPOSE_INTO_REGPAIR ((var)->type)) { MonoInst *var1 = get_vreg_to_inst (cfg, MONO_LVREG_LS ((var)->dreg)); MonoInst *var2 = get_vreg_to_inst (cfg, MONO_LVREG_MS ((var)->dreg)); g_assert (var1); g_assert (var2); var1->flags |= MONO_INST_INDIRECT; var2->flags |= MONO_INST_INDIRECT; } \
        } while (0)
 
 #define NEW_VARSTORE(cfg,dest,var,vartype,inst) do {   \
@@ -351,7 +373,7 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
         (dest)->type = STACK_MP; \
            (dest)->klass = cfg->ret->klass;    \
            (dest)->sreg1 = cfg->vret_addr->dreg;   \
-        (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
+        (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
        } while (0)
 
 #define NEW_ARGLOADA(cfg,dest,num) NEW_VARLOADA ((cfg), (dest), arg_array [(num)], param_types [(num)])
@@ -373,7 +395,7 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
 #define NEW_LOAD_MEMBASE_TYPE(cfg,dest,ltype,base,offset) do { \
            NEW_LOAD_MEMBASE ((cfg), (dest), mono_type_to_load_membase ((cfg), (ltype)), 0, (base), (offset)); \
            type_to_eval_stack_type ((cfg), (ltype), (dest)); \
-           (dest)->dreg = alloc_dreg ((cfg), (dest)->type); \
+           (dest)->dreg = alloc_dreg ((cfg), (MonoStackType)(dest)->type); \
     } while (0)
 
 #define NEW_STORE_MEMBASE_TYPE(cfg,dest,ltype,base,offset,sr) do { \
@@ -386,7 +408,7 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
        } while (0)
 
 #define NEW_SEQ_POINT(cfg,dest,il_offset,intr_loc) do {         \
-       MONO_INST_NEW ((cfg), (dest), OP_SEQ_POINT); \
+       MONO_INST_NEW ((cfg), (dest), cfg->gen_sdb_seq_points ? OP_SEQ_POINT : OP_IL_SEQ_POINT); \
        (dest)->inst_imm = (il_offset); \
        (dest)->flags = intr_loc ? MONO_INST_SINGLE_STEP_LOC : 0; \
        } while (0)
@@ -424,10 +446,14 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
 
 #define EMIT_NEW_LDSTRCONST(cfg,dest,image,token) do { NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), NULL, STACK_OBJ, mono_defaults.string_class); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
 
+#define EMIT_NEW_LDSTRLITCONST(cfg,dest,val) do { NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_LDSTR_LIT, (val)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
+
 #define EMIT_NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token,generic_context) do { NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), (generic_context), STACK_OBJ, mono_defaults.monotype_class); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
 
 #define EMIT_NEW_LDTOKENCONST(cfg,dest,image,token,generic_context) do { NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), (generic_context), STACK_PTR, NULL); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
 
+#define EMIT_NEW_TLS_OFFSETCONST(cfg,dest,key) do { NEW_TLS_OFFSETCONST ((cfg), (dest), (key)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
+
 #define EMIT_NEW_DOMAINCONST(cfg,dest) do { NEW_DOMAINCONST ((cfg), (dest)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
 
 #define EMIT_NEW_DECLSECCONST(cfg,dest,image,entry) do { NEW_DECLSECCONST ((cfg), (dest), (image), (entry)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
@@ -442,7 +468,7 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
 
 #define EMIT_NEW_VARLOADA(cfg,dest,var,vartype) do { NEW_VARLOADA ((cfg), (dest), (var), (vartype)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
 
-#ifdef MONO_ARCH_SOFT_FLOAT
+#ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
 
 /*
  * Since the IL stack (and our vregs) contain double values, we have to do a conversion
@@ -450,33 +476,61 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
  */
 
 #define EMIT_NEW_VARLOAD_SFLOAT(cfg,dest,var,vartype) do { \
-               if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
-             MonoInst *iargs [1]; \
-             EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
-             (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
-        } else { \
-             EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
-        } \
-    } while (0)
+               if (!COMPILE_LLVM ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
+                       MonoInst *iargs [1]; \
+                       EMIT_NEW_VARLOADA (cfg, iargs [0], (var), (vartype)); \
+                       (dest) = mono_emit_jit_icall (cfg, mono_fload_r4, iargs); \
+               } else { \
+                       EMIT_NEW_VARLOAD ((cfg), (dest), (var), (vartype)); \
+               } \
+       } while (0)
 
 #define EMIT_NEW_VARSTORE_SFLOAT(cfg,dest,var,vartype,inst) do {       \
                if (COMPILE_SOFT_FLOAT ((cfg)) && !(vartype)->byref && (vartype)->type == MONO_TYPE_R4) { \
-             MonoInst *iargs [2]; \
-             iargs [0] = (inst); \
-             EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
-             mono_emit_jit_icall (cfg, mono_fstore_r4, iargs); \
-        } else { \
-             EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
-        } \
-    } while (0)
+                       MonoInst *iargs [2]; \
+                       iargs [0] = (inst); \
+                       EMIT_NEW_VARLOADA (cfg, iargs [1], (var), (vartype)); \
+                       (dest) = mono_emit_jit_icall (cfg, mono_fstore_r4, iargs);      \
+               } else { \
+                       EMIT_NEW_VARSTORE ((cfg), (dest), (var), (vartype), (inst)); \
+               } \
+       } while (0)
 
-#define EMIT_NEW_ARGLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)])
+#define EMIT_NEW_ARGLOAD(cfg,dest,num) do {    \
+               if (mono_arch_is_soft_float ()) {       \
+                       EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)]);     \
+               } else {        \
+                       NEW_ARGLOAD ((cfg), (dest), (num));     \
+                       MONO_ADD_INS ((cfg)->cbb, (dest));      \
+               }       \
+       } while (0)
 
-#define EMIT_NEW_LOCLOAD(cfg,dest,num) EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)])
+#define EMIT_NEW_LOCLOAD(cfg,dest,num) do {    \
+               if (mono_arch_is_soft_float ()) {       \
+                       EMIT_NEW_VARLOAD_SFLOAT ((cfg), (dest), cfg->locals [(num)], header->locals [(num)]);   \
+               } else {        \
+                       NEW_LOCLOAD ((cfg), (dest), (num));     \
+                       MONO_ADD_INS ((cfg)->cbb, (dest));      \
+               }       \
+       } while (0)
 
-#define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst))
+#define EMIT_NEW_LOCSTORE(cfg,dest,num,inst) do {      \
+               if (mono_arch_is_soft_float ()) {       \
+                       EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), (cfg)->locals [(num)], (cfg)->locals [(num)]->inst_vtype, (inst));     \
+               } else {        \
+                       NEW_LOCSTORE ((cfg), (dest), (num), (inst));    \
+                       MONO_ADD_INS ((cfg)->cbb, (dest));      \
+               }       \
+       } while (0)
 
-#define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst))
+#define EMIT_NEW_ARGSTORE(cfg,dest,num,inst) do {      \
+               if (mono_arch_is_soft_float ()) {       \
+                       EMIT_NEW_VARSTORE_SFLOAT ((cfg), (dest), cfg->args [(num)], cfg->arg_types [(num)], (inst));    \
+               } else {        \
+                       NEW_ARGSTORE ((cfg), (dest), (num), (inst));    \
+                       MONO_ADD_INS ((cfg)->cbb, (dest));      \
+               }       \
+       } while (0)
 
 #else
 
@@ -549,6 +603,13 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
            MONO_ADD_INS ((cfg)->cbb, inst); \
        } while (0)
 
+#define MONO_EMIT_NEW_DUMMY_INIT(cfg,dr,op) do {                         \
+               MonoInst *inst;                                                                           \
+               MONO_INST_NEW ((cfg), (inst), (op));                              \
+               inst->dreg = dr;                                                                          \
+               MONO_ADD_INS ((cfg)->cbb, inst);                                          \
+       } while (0)
+
 #ifdef MONO_ARCH_NEED_GOT_VAR
 
 #define MONO_EMIT_NEW_AOTCONST(cfg,dr,cons,patch_type) do { \
@@ -682,7 +743,7 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
  * block_num: unique ID assigned at bblock creation
  */
 #define NEW_BBLOCK(cfg,bblock) do { \
-       (bblock) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
+       (bblock) = (MonoBasicBlock *)mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
        (bblock)->block_num = cfg->num_bblocks++; \
     } while (0)
 
@@ -693,8 +754,8 @@ alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
                (b)->real_offset = cfg->real_offset;    \
        } while (0)
 
-/* Emit a one-way conditional branch */
-/* 
+/*
+ * Emit a one-way conditional branch and start a new bblock.
  * The inst_false_bb field of the cond branch will not be set, the JIT code should be
  * prepared to deal with this.
  */
@@ -717,8 +778,8 @@ static int ccount = 0;
             ins->inst_false_bb = NULL; \
             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
             MONO_ADD_INS ((cfg)->cbb, ins); \
-            if (getenv ("COUNT2") && ccount == atoi (getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
-            if (getenv ("COUNT2") && ccount < atoi (getenv ("COUNT2"))) { \
+            if (g_getenv ("COUNT2") && ccount == atoi (g_getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
+            if (g_getenv ("COUNT2") && ccount < atoi (g_getenv ("COUNT2"))) { \
                  cfg->cbb->extended = TRUE; \
             } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
         } \
@@ -735,7 +796,7 @@ static int ccount = 0;
             MONO_ADD_INS ((cfg)->cbb, ins); \
             MONO_START_BB ((cfg), falsebb); \
         } else { \
-                   ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);  \
+                   ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);       \
             ins->inst_true_bb = (truebb); \
             ins->inst_false_bb = NULL; \
             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
@@ -756,7 +817,7 @@ static int ccount = 0;
 #define        MONO_EMIT_NEW_BRANCH_BLOCK2(cfg,op,truebb,falsebb) do { \
         MonoInst *ins; \
         MONO_INST_NEW ((cfg), (ins), (op)); \
-               ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);      \
+               ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);   \
         ins->inst_true_bb = (truebb); \
         ins->inst_false_bb = (falsebb); \
         mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
@@ -784,8 +845,6 @@ static int ccount = 0;
 
 /* Loads/Stores which can fault are handled correctly by the LLVM mono branch */
 #define MONO_EMIT_NEW_IMPLICIT_EXCEPTION_LOAD_STORE(cfg) do { \
-       if (COMPILE_LLVM (cfg) && !IS_LLVM_MONO_BRANCH)                 \
-               MONO_EMIT_NEW_IMPLICIT_EXCEPTION ((cfg));                       \
     } while (0)
 
 /* Emit an explicit null check which doesn't depend on SIGSEGV signal handling */
@@ -813,8 +872,6 @@ static int ccount = 0;
                int __ins_flags = ins_flags; \
                if (__ins_flags & MONO_INST_FAULT) {                                                            \
                        MONO_EMIT_NULL_CHECK ((cfg), (base));                                           \
-                       if (cfg->explicit_null_checks)                                                          \
-                               __ins_flags &= ~MONO_INST_FAULT;                                                        \
                }                                                                                                                               \
                NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); \
                (dest)->flags = (__ins_flags);                                                                  \
@@ -825,8 +882,6 @@ static int ccount = 0;
                int __ins_flags = ins_flags; \
            if (__ins_flags & MONO_INST_FAULT) {                                                                        \
                        MONO_EMIT_NULL_CHECK ((cfg), (base));                                           \
-                       if (cfg->explicit_null_checks)                                                          \
-                               __ins_flags &= ~MONO_INST_FAULT;                                                        \
                }                                                                                                                               \
                NEW_LOAD_MEMBASE ((cfg), (inst), (op), (dr), (base), (offset)); \
                inst->flags = (__ins_flags); \
@@ -847,6 +902,12 @@ static int ccount = 0;
 
 #define MONO_EMIT_NEW_LOAD_MEMBASE_FAULT(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
 
+#define NEW_LOAD_MEMBASE_INVARIANT(cfg,dest,op,dr,base,offset) NEW_LOAD_MEMBASE_FLAGS ((cfg), (dest), (op), (dr), (base), (offset), MONO_INST_INVARIANT_LOAD)
+
+#define MONO_EMIT_NEW_LOAD_MEMBASE_OP_INVARIANT(cfg,op,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS ((cfg), (op), (dr), (base), (offset), MONO_INST_INVARIANT_LOAD)
+
+#define MONO_EMIT_NEW_LOAD_MEMBASE_INVARIANT(cfg,dr,base,offset) MONO_EMIT_NEW_LOAD_MEMBASE_OP_INVARIANT ((cfg), (OP_LOAD_MEMBASE), (dr), (base), (offset))
+
 /*Object Model related macros*/
 
 /* Default bounds check implementation for most architectures + llvm */
@@ -855,7 +916,7 @@ static int ccount = 0;
                        if (fault) \
                                MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset); \
                        else \
-                               MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset, MONO_INST_CONSTANT_LOAD); \
+                               MONO_EMIT_NEW_LOAD_MEMBASE_OP_FLAGS (cfg, OP_LOADI4_MEMBASE, _length_reg, array_reg, offset, MONO_INST_INVARIANT_LOAD); \
                        MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, _length_reg, index_reg); \
                        MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
        } while (0)
@@ -875,15 +936,15 @@ static int ccount = 0;
                if (!(cfg->opt & MONO_OPT_ABCREM)) {                                                    \
                        MONO_EMIT_NULL_CHECK (cfg, array_reg);                                          \
                        if (COMPILE_LLVM (cfg)) \
-                               MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg), TRUE); \
+                               MONO_EMIT_DEFAULT_BOUNDS_CHECK ((cfg), (array_reg), MONO_STRUCT_OFFSET (array_type, array_length_field), (index_reg), TRUE); \
                        else \
-                               MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), G_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
+                               MONO_ARCH_EMIT_BOUNDS_CHECK ((cfg), (array_reg), MONO_STRUCT_OFFSET (array_type, array_length_field), (index_reg)); \
                } else {                                                                                                                \
                        MonoInst *ins;                                                                                          \
                        MONO_INST_NEW ((cfg), ins, OP_BOUNDS_CHECK);                            \
                        ins->sreg1 = array_reg;                                                                         \
                        ins->sreg2 = index_reg;                                                                         \
-                       ins->inst_imm = G_STRUCT_OFFSET (array_type, array_length_field); \
+                       ins->inst_imm = MONO_STRUCT_OFFSET (array_type, array_length_field); \
                        ins->flags |= MONO_INST_FAULT; \
                        MONO_ADD_INS ((cfg)->cbb, ins);                                                         \
                        (cfg)->flags |= MONO_CFG_HAS_ARRAY_ACCESS;                                      \