Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / liveness.c
index a5cbabb53a53333dd2ecf2a2cfd1012bb780d507..eaa656e059c1b9c3894a321a9a4d6b19b24704e6 100644 (file)
@@ -1,14 +1,17 @@
-/*
- * liveness.c: liveness analysis
+/**
+ * \file
+ * liveness analysis
  *
  * Author:
  *   Dietmar Maurer (dietmar@ximian.com)
  *
  * (C) 2002 Ximian, Inc.
  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include <config.h>
+#include <mono/utils/mono-compiler.h>
 
 #ifndef DISABLE_JIT
 
@@ -20,6 +23,8 @@
 
 #define BITS_PER_CHUNK MONO_BITSET_BITS_PER_CHUNK
 
+#define BB_ID_SHIFT 18
+
 /* 
  * The liveness2 pass can't handle long vars on 32 bit platforms because the component
  * vars have the same 'idx'.
@@ -42,14 +47,14 @@ optimize_initlocals (MonoCompile *cfg);
 static inline MonoBitSet* 
 mono_bitset_mp_new (MonoMemPool *mp, guint32 size, guint32 max_size)
 {
-       guint8 *mem = mono_mempool_alloc0 (mp, size);
+       guint8 *mem = (guint8 *)mono_mempool_alloc0 (mp, size);
        return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
 }
 
 static inline MonoBitSet* 
 mono_bitset_mp_new_noinit (MonoMemPool *mp, guint32 size, guint32 max_size)
 {
-       guint8 *mem = mono_mempool_alloc (mp, size);
+       guint8 *mem = (guint8 *)mono_mempool_alloc (mp, size);
        return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
 }
 
@@ -101,8 +106,8 @@ visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
                        cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
                        if (SIZEOF_REGISTER == 4 && (var->type == STACK_I8 || (var->type == STACK_R8 && COMPILE_SOFT_FLOAT (cfg)))) {
                                /* Make the component vregs volatile as well (#612206) */
-                               get_vreg_to_inst (cfg, var->dreg + 1)->flags |= MONO_INST_VOLATILE;
-                               get_vreg_to_inst (cfg, var->dreg + 2)->flags |= MONO_INST_VOLATILE;
+                               get_vreg_to_inst (cfg, MONO_LVREG_LS (var->dreg))->flags |= MONO_INST_VOLATILE;
+                               get_vreg_to_inst (cfg, MONO_LVREG_MS (var->dreg))->flags |= MONO_INST_VOLATILE;
                        }
                }
                        
@@ -120,8 +125,8 @@ visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
                                cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
                                if (SIZEOF_REGISTER == 4 && (var->type == STACK_I8 || (var->type == STACK_R8 && COMPILE_SOFT_FLOAT (cfg)))) {
                                        /* Make the component vregs volatile as well (#612206) */
-                                       get_vreg_to_inst (cfg, var->dreg + 1)->flags |= MONO_INST_VOLATILE;
-                                       get_vreg_to_inst (cfg, var->dreg + 2)->flags |= MONO_INST_VOLATILE;
+                                       get_vreg_to_inst (cfg, MONO_LVREG_LS (var->dreg))->flags |= MONO_INST_VOLATILE;
+                                       get_vreg_to_inst (cfg, MONO_LVREG_MS (var->dreg))->flags |= MONO_INST_VOLATILE;
                                }
                        }
                }
@@ -152,7 +157,7 @@ mono_liveness_handle_exception_clauses (MonoCompile *cfg)
         * Determine which clauses are outer try clauses, i.e. they are not contained in any
         * other non-try clause.
         */
-       outer_try = mono_mempool_alloc0 (cfg->mempool, sizeof (gboolean) * header->num_clauses);
+       outer_try = (gboolean *)mono_mempool_alloc0 (cfg->mempool, sizeof (gboolean) * header->num_clauses);
        for (i = 0; i < header->num_clauses; ++i)
                outer_try [i] = TRUE;
        /* Iterate over the clauses backward, so outer clauses come first */
@@ -217,7 +222,7 @@ analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
        MonoInst *ins;
        int sreg, inst_num;
        MonoMethodVar *vars = cfg->vars;
-       guint32 abs_pos = (bb->dfn << 16);
+       guint32 abs_pos = (bb->dfn << BB_ID_SHIFT);
        
        /* Start inst_num from > 0, so last_use.abs_pos is only 0 for dead variables */
        for (inst_num = 2, ins = bb->code; ins; ins = ins->next, inst_num += 2) {
@@ -227,8 +232,7 @@ analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
 
 #ifdef DEBUG_LIVENESS
                if (cfg->verbose_level > 1) {
-                       printf ("\t");
-                       mono_print_ins (ins);
+                       mono_print_ins_index (1, ins);
                }
 #endif
 
@@ -236,7 +240,7 @@ analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
                        continue;
 
                if (ins->opcode == OP_LDADDR) {
-                       MonoInst *var = ins->inst_p0;
+                       MonoInst *var = (MonoInst *)ins->inst_p0;
                        int idx = var->inst_c0;
                        MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
 
@@ -492,7 +496,7 @@ mono_analyze_liveness (MonoCompile *cfg)
        for (i = 0; i < cfg->num_bblocks; ++i) {
                MonoBasicBlock *bb = cfg->bblocks [i];
                guint32 max;
-               guint32 abs_pos = (bb->dfn << 16);
+               guint32 abs_pos = (bb->dfn << BB_ID_SHIFT);
                MonoMethodVar *vars = cfg->vars;
 
                if (!bb->live_out_set)
@@ -512,7 +516,7 @@ mono_analyze_liveness (MonoCompile *cfg)
                                if (bits_in & 1)
                                        update_live_range (&vars [k], abs_pos + 0);
                                if (bits_out & 1)
-                                       update_live_range (&vars [k], abs_pos + 0xffff);
+                                       update_live_range (&vars [k], abs_pos + ((1 << BB_ID_SHIFT) - 1));
                                bits_in >>= 1;
                                bits_out >>= 1;
                                k ++;
@@ -538,7 +542,7 @@ mono_analyze_liveness (MonoCompile *cfg)
                                 * VOLATILE, since that would prevent it from being allocated to
                                 * registers.
                                 */
-                                if (!cfg->disable_deadce_vars && !(cfg->generic_sharing_context && mono_method_signature (cfg->method)->hasthis && cfg->varinfo [vi->idx] == cfg->args [0]))
+                                if (!cfg->disable_deadce_vars && !(cfg->gshared && mono_method_signature (cfg->method)->hasthis && cfg->varinfo [vi->idx] == cfg->args [0]))
                                         cfg->varinfo [vi->idx]->flags |= MONO_INST_IS_DEAD;
                        }
                        vi->range.first_use.abs_pos = 0;
@@ -614,7 +618,7 @@ optimize_initlocals (MonoCompile *cfg)
                                //printf ("DEAD: "); mono_print_ins (ins);
                                if (cfg->disable_initlocals_opt_refs && var->type == STACK_OBJ)
                                        continue;
-                               if ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST)) {
+                               if ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST) || (ins->opcode == OP_R4CONST)) {
                                        NULLIFY_INS (ins);
                                        MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;
                                        /* 
@@ -658,7 +662,7 @@ mono_linterval_add_range (MonoCompile *cfg, MonoLiveInterval *interval, int from
                next_range->from = from;
        } else {
                /* Insert it */
-               new_range = mono_mempool_alloc (cfg->mempool, sizeof (MonoLiveRange2));
+               new_range = (MonoLiveRange2 *)mono_mempool_alloc (cfg->mempool, sizeof (MonoLiveRange2));
                new_range->from = from;
                new_range->to = to;
                new_range->next = NULL;
@@ -751,8 +755,8 @@ mono_linterval_split (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInte
 
        g_assert (pos > interval->range->from && pos <= interval->last_range->to);
 
-       *i1 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
-       *i2 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
+       *i1 = (MonoLiveInterval *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
+       *i2 = (MonoLiveInterval *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
 
        for (r = interval->range; r; r = r->next) {
                if (pos > r->to) {
@@ -788,7 +792,7 @@ update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int in
 
        LIVENESS_DEBUG (printf ("\t%x: ", inst_num); mono_print_ins (ins));
 
-       if (ins->opcode == OP_NOP)
+       if (ins->opcode == OP_NOP || ins->opcode == OP_IL_SEQ_POINT)
                return;
 
        /* DREG */
@@ -810,16 +814,21 @@ update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int in
                        }
                        else {
                                /* Try dead code elimination */
-                               if ((var != cfg->ret) && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST)) && !(var->flags & MONO_INST_VOLATILE)) {
+                               if (!cfg->disable_deadce_vars && (var != cfg->ret) && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST)) && !(var->flags & MONO_INST_VOLATILE)) {
                                        LIVENESS_DEBUG (printf ("\tdead def of R%d, eliminated\n", ins->dreg));
-                                       ins->opcode = OP_NOP;
-                                       ins->dreg = -1;
-                                       MONO_INST_NULLIFY_SREGS (ins);
+                                       NULLIFY_INS (ins);
                                        return;
+                               } else {
+                                       int inst_num_add = 1;
+                                       MonoInst *next = ins->next;
+                                       while (next && next->opcode == OP_IL_SEQ_POINT) {
+                                               inst_num_add++;
+                                               next = next->next;
+                                       }
+
+                                       LIVENESS_DEBUG (printf ("\tdead def of R%d, add range to R%d: [%x, %x]\n", ins->dreg, ins->dreg, inst_num, inst_num + inst_num_add));
+                                       mono_linterval_add_range (cfg, vi->interval, inst_num, inst_num + inst_num_add);
                                }
-
-                               LIVENESS_DEBUG (printf ("\tdead def of R%d, add range to R%d: [%x, %x]\n", ins->dreg, ins->dreg, inst_num, inst_num + 1));
-                               mono_linterval_add_range (cfg, vi->interval, inst_num, inst_num + 1);
                        }
                }
        }
@@ -843,17 +852,33 @@ update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int in
 static void
 mono_analyze_liveness2 (MonoCompile *cfg)
 {
-       int bnum, idx, i, j, nins, max, max_vars, block_from, block_to, pos, reverse_len;
+       int bnum, idx, i, j, nins, max, max_vars, block_from, block_to, pos;
        gint32 *last_use;
        static guint32 disabled = -1;
-       MonoInst **reverse;
 
        if (disabled == -1)
-               disabled = g_getenv ("DISABLED") != NULL;
+               disabled = g_hasenv ("DISABLED");
 
        if (disabled)
                return;
 
+       if (cfg->num_bblocks >= (1 << (32 - BB_ID_SHIFT - 1)))
+               /* Ranges would overflow */
+               return;
+
+       for (bnum = cfg->num_bblocks - 1; bnum >= 0; --bnum) {
+               MonoBasicBlock *bb = cfg->bblocks [bnum];
+               MonoInst *ins;
+
+               nins = 0;
+               for (nins = 0, ins = bb->code; ins; ins = ins->next, ++nins)
+                       nins ++;
+
+               if (nins >= ((1 << BB_ID_SHIFT) - 1))
+                       /* Ranges would overflow */
+                       return;
+       }
+
        LIVENESS_DEBUG (printf ("LIVENESS 2 %s\n", mono_method_full_name (cfg->method, TRUE)));
 
        /*
@@ -864,13 +889,10 @@ mono_analyze_liveness2 (MonoCompile *cfg)
        max_vars = cfg->num_varinfo;
        last_use = g_new0 (gint32, max_vars);
 
-       reverse_len = 1024;
-       reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
-
        for (idx = 0; idx < max_vars; ++idx) {
                MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
 
-               vi->interval = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
+               vi->interval = (MonoLiveInterval *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
        }
 
        /*
@@ -881,12 +903,12 @@ mono_analyze_liveness2 (MonoCompile *cfg)
                MonoBasicBlock *bb = cfg->bblocks [bnum];
                MonoInst *ins;
 
-               block_from = (bb->dfn << 16) + 1; /* so pos > 0 */
+               block_from = (bb->dfn << BB_ID_SHIFT) + 1; /* so pos > 0 */
                if (bnum < cfg->num_bblocks - 1)
                        /* Beginning of the next bblock */
-                       block_to = (cfg->bblocks [bnum + 1]->dfn << 16) + 1;
+                       block_to = (cfg->bblocks [bnum + 1]->dfn << BB_ID_SHIFT) + 1;
                else
-                       block_to = (bb->dfn << 16) + 0xffff;
+                       block_to = (bb->dfn << BB_ID_SHIFT) + ((1 << BB_ID_SHIFT) - 1);
 
                LIVENESS_DEBUG (printf ("LIVENESS BLOCK BB%d:\n", bb->block_num));
 
@@ -914,25 +936,13 @@ mono_analyze_liveness2 (MonoCompile *cfg)
                if (cfg->ret)
                        last_use [cfg->ret->inst_c0] = block_to;
 
-               for (nins = 0, pos = block_from, ins = bb->code; ins; ins = ins->next, ++nins, ++pos) {
-                       if (nins >= reverse_len) {
-                               int new_reverse_len = reverse_len * 2;
-                               MonoInst **new_reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
-                               memcpy (new_reverse, reverse, sizeof (MonoInst*) * reverse_len);
-                               reverse = new_reverse;
-                               reverse_len = new_reverse_len;
-                       }
-
-                       reverse [nins] = ins;
-               }
+               pos = block_from + 1;
+               MONO_BB_FOR_EACH_INS (bb, ins) pos++;
 
                /* Process instructions backwards */
-               for (i = nins - 1; i >= 0; --i) {
-                       MonoInst *ins = (MonoInst*)reverse [i];
-
-                       update_liveness2 (cfg, ins, FALSE, pos, last_use);
-
-                       pos --;
+               MONO_BB_FOR_EACH_INS_REVERSE (bb, ins) {
+                       update_liveness2 (cfg, ins, FALSE, pos, last_use);
+                       pos--;
                }
 
                for (idx = 0; idx < max_vars; ++idx) {
@@ -1001,17 +1011,17 @@ update_liveness_gc (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, gint32
 
                /* Add it to the last callsite */
                g_assert (*callsites);
-               last = (*callsites)->data;
+               last = (GCCallSite *)(*callsites)->data;
                last->param_slots = g_slist_prepend_mempool (cfg->mempool, last->param_slots, ins);
        } else if (ins->flags & MONO_INST_GC_CALLSITE) {
-               GCCallSite *callsite = mono_mempool_alloc0 (cfg->mempool, sizeof (GCCallSite));
+               GCCallSite *callsite = (GCCallSite *)mono_mempool_alloc0 (cfg->mempool, sizeof (GCCallSite));
                int i;
 
                LIVENESS_DEBUG (printf ("\t%x: ", ins->backend.pc_offset); mono_print_ins (ins));
                LIVENESS_DEBUG (printf ("\t\tlive: "));
 
                callsite->bb = bb;
-               callsite->liveness = mono_mempool_alloc0 (cfg->mempool, ALIGN_TO (cfg->num_varinfo, 8) / 8);
+               callsite->liveness = (guint8 *)mono_mempool_alloc0 (cfg->mempool, ALIGN_TO (cfg->num_varinfo, 8) / 8);
                callsite->pc_offset = ins->backend.pc_offset;
                for (i = 0; i < cfg->num_varinfo; ++i) {
                        if (last_use [i] != 0) {
@@ -1067,7 +1077,7 @@ mono_analyze_liveness_gc (MonoCompile *cfg)
        }
 
        reverse_len = 1024;
-       reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
+       reverse = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
 
        for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
                MonoInst *ins;
@@ -1109,7 +1119,7 @@ mono_analyze_liveness_gc (MonoCompile *cfg)
                for (nins = 0, pos = block_from, ins = bb->code; ins; ins = ins->next, ++nins, ++pos) {
                        if (nins >= reverse_len) {
                                int new_reverse_len = reverse_len * 2;
-                               MonoInst **new_reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
+                               MonoInst **new_reverse = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
                                memcpy (new_reverse, reverse, sizeof (MonoInst*) * reverse_len);
                                reverse = new_reverse;
                                reverse_len = new_reverse_len;
@@ -1133,4 +1143,8 @@ mono_analyze_liveness_gc (MonoCompile *cfg)
        g_free (vreg_to_varinfo);
 }
 
-#endif /* DISABLE_JIT */
+#else /* !DISABLE_JIT */
+
+MONO_EMPTY_SOURCE_FILE (liveness);
+
+#endif /* !DISABLE_JIT */