[threads] Use runtime flag to enable cooperative suspend
[mono.git] / mono / mini / liveness.c
index 078461ea3fac551dc1895b238484b99106a50a9a..355846bea67270805a5262f2d9518839fc2f33df 100644 (file)
@@ -5,25 +5,28 @@
  *   Dietmar Maurer (dietmar@ximian.com)
  *
  * (C) 2002 Ximian, Inc.
+ * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
  */
 
+#include <config.h>
+
+#ifndef DISABLE_JIT
+
 #include "mini.h"
 
 #define SPILL_COST_INCREMENT (1 << (bb->nesting << 1))
 
-//#define DEBUG_LIVENESS
+#define DEBUG_LIVENESS
 
-#if SIZEOF_VOID_P == 8
-#define BITS_PER_CHUNK 64
-#else
-#define BITS_PER_CHUNK 32
-#endif
+#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'.
  */
-#if SIZEOF_VOID_P == 8
+#if SIZEOF_REGISTER == 8
 #define ENABLE_LIVENESS2
 #endif
 
@@ -56,13 +59,17 @@ G_GNUC_UNUSED static void
 mono_bitset_print (MonoBitSet *set)
 {
        int i;
+       gboolean first = TRUE;
 
        printf ("{");
        for (i = 0; i < mono_bitset_size (set); i++) {
 
-               if (mono_bitset_test (set, i))
-                       printf ("%d, ", i);
-
+               if (mono_bitset_test (set, i)) {
+                       if (!first)
+                               printf (", ");
+                       printf ("%d", i);
+                       first = FALSE;
+               }
        }
        printf ("}\n");
 }
@@ -78,7 +85,8 @@ visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
 
        for (ins = bb->code; ins; ins = ins->next) {
                const char *spec = INS_INFO (ins->opcode);
-               int regtype, srcindex, sreg;
+               int regtype, srcindex, sreg, num_sregs;
+               int sregs [MONO_MAX_SRC_REGS];
 
                if (ins->opcode == OP_NOP)
                        continue;
@@ -93,20 +101,30 @@ visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
                        MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
 
                        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;
+                       }
                }
                        
                /* SREGS */
-               for (srcindex = 0; srcindex < 2; ++srcindex) {
-                       regtype = spec [(srcindex == 0) ? MONO_INST_SRC1 : MONO_INST_SRC2];
-                       sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
-                       
-                       g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
-                       if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
+               num_sregs = mono_inst_get_src_registers (ins, sregs);
+               for (srcindex = 0; srcindex < num_sregs; ++srcindex) {
+                       sreg = sregs [srcindex];
+
+                       g_assert (sreg != -1);
+                       if (get_vreg_to_inst (cfg, sreg)) {
                                MonoInst *var = get_vreg_to_inst (cfg, sreg);
                                int idx = var->inst_c0;
                                MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
 
                                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;
+                               }
                        }
                }
        }
@@ -127,6 +145,39 @@ mono_liveness_handle_exception_clauses (MonoCompile *cfg)
 {
        MonoBasicBlock *bb;
        GSList *visited = NULL;
+       MonoMethodHeader *header = cfg->header;
+       MonoExceptionClause *clause, *clause2;
+       int i, j;
+       gboolean *outer_try;
+
+       /* 
+        * 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);
+       for (i = 0; i < header->num_clauses; ++i)
+               outer_try [i] = TRUE;
+       /* Iterate over the clauses backward, so outer clauses come first */
+       /* This avoids doing an O(2) search, since we can determine when inner clauses end */
+       for (i = header->num_clauses - 1; i >= 0; --i) {
+               clause = &header->clauses [i];
+
+               if (clause->flags != 0) {
+                       outer_try [i] = TRUE;
+                       /* Iterate over inner clauses */
+                       for (j = i - 1; j >= 0; --j) {
+                               clause2 = &header->clauses [j];
+
+                               if (clause2->flags == 0 && MONO_OFFSET_IN_HANDLER (clause, clause2->try_offset)) {
+                                       outer_try [j] = FALSE;
+                                       break;
+                               }
+                               if (clause2->try_offset < clause->try_offset)
+                                       /* End of inner clauses */
+                                       break;
+                       }
+               }
+       }
 
        /*
         * Variables in exception handler register cannot be allocated to registers
@@ -138,9 +189,15 @@ mono_liveness_handle_exception_clauses (MonoCompile *cfg)
         */
        for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
 
-               if (bb->region == -1 || MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
+               if (bb->region == -1)
+                       continue;
+
+               if (MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY) && outer_try [MONO_REGION_CLAUSE_INDEX (bb->region)])
                        continue;
 
+               if (cfg->verbose_level > 2)
+                       printf ("pessimize variables in bb %d.\n", bb->block_num);
+
                visit_bb (cfg, bb, &visited);
        }
        g_slist_free (visited);
@@ -162,13 +219,19 @@ 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);
        
-       for (inst_num = 0, ins = bb->code; ins; ins = ins->next, inst_num += 2) {
+       /* 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) {
                const char *spec = INS_INFO (ins->opcode);
+               int num_sregs, i;
+               int sregs [MONO_MAX_SRC_REGS];
 
 #ifdef DEBUG_LIVENESS
-                       printf ("\t"); mono_print_ins (ins);
+               if (cfg->verbose_level > 1) {
+                       printf ("\t");
+                       mono_print_ins (ins);
+               }
 #endif
 
                if (ins->opcode == OP_NOP)
@@ -180,7 +243,8 @@ analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
                        MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
 
 #ifdef DEBUG_LIVENESS
-                       printf ("\tGEN: R%d(%d)\n", var->dreg, idx);
+                       if (cfg->verbose_level > 1)
+                               printf ("\tGEN: R%d(%d)\n", var->dreg, idx);
 #endif
                        update_live_range (&vars [idx], abs_pos + inst_num); 
                        if (!mono_bitset_test_fast (bb->kill_set, idx))
@@ -189,37 +253,23 @@ analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
                }                               
 
                /* SREGs must come first, so MOVE r <- r is handled correctly */
-
-               /* SREG1 */
-               sreg = ins->sreg1;
-               if ((spec [MONO_INST_SRC1] != ' ') && get_vreg_to_inst (cfg, sreg)) {
-                       MonoInst *var = get_vreg_to_inst (cfg, sreg);
-                       int idx = var->inst_c0;
-                       MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
-
-#ifdef DEBUG_LIVENESS
-                       printf ("\tGEN: R%d(%d)\n", sreg, idx);
-#endif
-                       update_live_range (&vars [idx], abs_pos + inst_num); 
-                       if (!mono_bitset_test_fast (bb->kill_set, idx))
-                               mono_bitset_set_fast (bb->gen_set, idx);
-                       vi->spill_costs += SPILL_COST_INCREMENT;
-               }
-
-               /* SREG2 */
-               sreg = ins->sreg2;
-               if ((spec [MONO_INST_SRC2] != ' ') && get_vreg_to_inst (cfg, sreg)) {
-                       MonoInst *var = get_vreg_to_inst (cfg, sreg);
-                       int idx = var->inst_c0;
-                       MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
+               num_sregs = mono_inst_get_src_registers (ins, sregs);
+               for (i = 0; i < num_sregs; ++i) {
+                       sreg = sregs [i];
+                       if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
+                               MonoInst *var = get_vreg_to_inst (cfg, sreg);
+                               int idx = var->inst_c0;
+                               MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
 
 #ifdef DEBUG_LIVENESS
-                       printf ("\tGEN: R%d(%d)\n", sreg, idx);
+                               if (cfg->verbose_level > 1)
+                                       printf ("\tGEN: R%d(%d)\n", sreg, idx);
 #endif
-                       update_live_range (&vars [idx], abs_pos + inst_num); 
-                       if (!mono_bitset_test_fast (bb->kill_set, idx))
-                               mono_bitset_set_fast (bb->gen_set, idx);
-                       vi->spill_costs += SPILL_COST_INCREMENT;
+                               update_live_range (&vars [idx], abs_pos + inst_num); 
+                               if (!mono_bitset_test_fast (bb->kill_set, idx))
+                                       mono_bitset_set_fast (bb->gen_set, idx);
+                               vi->spill_costs += SPILL_COST_INCREMENT;
+                       }
                }
 
                /* DREG */
@@ -235,7 +285,8 @@ analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
                                vi->spill_costs += SPILL_COST_INCREMENT;
                        } else {
 #ifdef DEBUG_LIVENESS
-                               printf ("\tKILL: R%d(%d)\n", ins->dreg, idx);
+                               if (cfg->verbose_level > 1)
+                                       printf ("\tKILL: R%d(%d)\n", ins->dreg, idx);
 #endif
                                update_live_range (&vars [idx], abs_pos + inst_num + 1); 
                                mono_bitset_set_fast (bb->kill_set, idx);
@@ -260,7 +311,8 @@ mono_analyze_liveness (MonoCompile *cfg)
        int bitsize;
 
 #ifdef DEBUG_LIVENESS
-       printf ("LIVENESS %s\n", mono_method_full_name (cfg->method, TRUE));
+       if (cfg->verbose_level > 1)
+               printf ("\nLIVENESS:\n");
 #endif
 
        g_assert (!(cfg->comp_done & MONO_COMP_LIVENESS));
@@ -284,16 +336,23 @@ mono_analyze_liveness (MonoCompile *cfg)
                bb->gen_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
                bb->kill_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
 
+#ifdef DEBUG_LIVENESS
+               if (cfg->verbose_level > 1) {
+                       printf ("BLOCK BB%d (", bb->block_num);
+                       for (j = 0; j < bb->out_count; j++) 
+                               printf ("BB%d, ", bb->out_bb [j]->block_num);
+               
+                       printf ("):\n");
+               }
+#endif
+
                analyze_liveness_bb (cfg, bb);
 
 #ifdef DEBUG_LIVENESS
-               printf ("BLOCK BB%d (", bb->block_num);
-               for (j = 0; j < bb->out_count; j++) 
-                       printf ("BB%d, ", bb->out_bb [j]->block_num);
-               
-               printf (")\n");
-               printf ("GEN  BB%d: ", bb->block_num); mono_bitset_print (bb->gen_set);
-               printf ("KILL BB%d: ", bb->block_num); mono_bitset_print (bb->kill_set);
+               if (cfg->verbose_level > 1) {
+                       printf ("GEN  BB%d: ", bb->block_num); mono_bitset_print (bb->gen_set);
+                       printf ("KILL BB%d: ", bb->block_num); mono_bitset_print (bb->kill_set);
+               }
 #endif
        }
 
@@ -320,6 +379,9 @@ mono_analyze_liveness (MonoCompile *cfg)
 
        out_iter = 0;
 
+       if (cfg->verbose_level > 1)
+               printf ("\nITERATION:\n");
+
        while (l_end != 0) {
                MonoBasicBlock *bb = worklist [--l_end];
                MonoBasicBlock *out_bb;
@@ -328,13 +390,15 @@ mono_analyze_liveness (MonoCompile *cfg)
                in_worklist [bb->dfn] = FALSE;
 
 #ifdef DEBUG_LIVENESS
-               printf ("P: %d(%d): IN: ", bb->block_num, bb->dfn);
-               for (j = 0; j < bb->in_count; ++j) 
-                       printf ("BB%d ", bb->in_bb [j]->block_num);
-               printf ("OUT:");
-               for (j = 0; j < bb->out_count; ++j) 
-                       printf ("BB%d ", bb->out_bb [j]->block_num);
-               printf ("\n");
+               if (cfg->verbose_level > 1) {
+                       printf ("P: BB%d(%d): IN: ", bb->block_num, bb->dfn);
+                       for (j = 0; j < bb->in_count; ++j) 
+                               printf ("BB%d ", bb->in_bb [j]->block_num);
+                       printf ("OUT:");
+                       for (j = 0; j < bb->out_count; ++j) 
+                               printf ("BB%d ", bb->out_bb [j]->block_num);
+                       printf ("\n");
+               }
 #endif
 
 
@@ -363,7 +427,11 @@ mono_analyze_liveness (MonoCompile *cfg)
                                mono_bitset_union_fast (out_bb->live_in_set, out_bb->gen_set);
                        }
 
-                       mono_bitset_union_fast (bb->live_out_set, out_bb->live_in_set);
+                       // FIXME: Do this somewhere else
+                       if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
+                       } else {
+                               mono_bitset_union_fast (bb->live_out_set, out_bb->live_in_set);
+                       }
                }
                                
                if (changed || !mono_bitset_equal (old_live_out_set, bb->live_out_set)) {
@@ -381,7 +449,8 @@ mono_analyze_liveness (MonoCompile *cfg)
                                 */
                                if (in_bb->gen_set && !in_worklist [in_bb->dfn]) {
 #ifdef DEBUG_LIVENESS
-                                       printf ("\tADD: %d\n", in_bb->block_num);
+                                       if (cfg->verbose_level > 1)
+                                               printf ("\tADD: %d\n", in_bb->block_num);
 #endif
                                        /*
                                         * Put the block at the top of the stack, so it
@@ -392,9 +461,15 @@ mono_analyze_liveness (MonoCompile *cfg)
                                }
                        }
                }
+
+               if (G_UNLIKELY (cfg->verbose_level > 1)) {
+                       printf ("\tLIVE IN  BB%d: ", bb->block_num); 
+                       mono_bitset_print (bb->live_in_set); 
+               }                       
        }
 
 #ifdef DEBUG_LIVENESS
+       if (cfg->verbose_level > 1)
                printf ("IT: %d %d.\n", cfg->num_bblocks, out_iter);
 #endif
 
@@ -418,14 +493,13 @@ mono_analyze_liveness (MonoCompile *cfg)
 
        for (i = 0; i < cfg->num_bblocks; ++i) {
                MonoBasicBlock *bb = cfg->bblocks [i];
-               guint32 rem, max;
-               guint32 abs_pos = (bb->dfn << 16);
+               guint32 max;
+               guint32 abs_pos = (bb->dfn << BB_ID_SHIFT);
                MonoMethodVar *vars = cfg->vars;
 
                if (!bb->live_out_set)
                        continue;
 
-               rem = max_vars % BITS_PER_CHUNK;
                max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
                for (j = 0; j < max; ++j) {
                        gsize bits_in;
@@ -440,7 +514,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 ++;
@@ -457,20 +531,38 @@ mono_analyze_liveness (MonoCompile *cfg)
        for (i = 0; i < max_vars; i ++) {
                MonoMethodVar *vi = MONO_VARINFO (cfg, i);
                if (cfg->varinfo [vi->idx]->opcode == OP_ARG) {
-                       if (vi->range.last_use.abs_pos == 0 && !(cfg->varinfo [vi->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
-                               cfg->varinfo [vi->idx]->flags |= MONO_INST_IS_DEAD;
+                       if (vi->range.last_use.abs_pos == 0 && !(cfg->varinfo [vi->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
+                               /* 
+                                * Can't eliminate the this variable in gshared code, since
+                                * it is needed during exception handling to identify the
+                                * method.
+                                * It is better to check for this here instead of marking the variable
+                                * VOLATILE, since that would prevent it from being allocated to
+                                * registers.
+                                */
+                                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;
                }
        }
 
 #ifdef DEBUG_LIVENESS
-       for (i = cfg->num_bblocks - 1; i >= 0; i--) {
-               MonoBasicBlock *bb = cfg->bblocks [i];
+       if (cfg->verbose_level > 1) {
+               for (i = cfg->num_bblocks - 1; i >= 0; i--) {
+                       MonoBasicBlock *bb = cfg->bblocks [i];
                
-               printf ("LIVE IN  BB%d: ", bb->block_num); 
-               mono_bitset_print (bb->live_in_set); 
-               printf ("LIVE OUT BB%d: ", bb->block_num); 
-               mono_bitset_print (bb->live_out_set); 
+                       printf ("LIVE IN  BB%d: ", bb->block_num); 
+                       mono_bitset_print (bb->live_in_set); 
+                       printf ("LIVE OUT BB%d: ", bb->block_num); 
+                       mono_bitset_print (bb->live_out_set); 
+               }
+
+               for (i = 0; i < max_vars; i ++) {
+                       MonoMethodVar *vi = MONO_VARINFO (cfg, i);
+
+                       printf ("V%d: [0x%x - 0x%x]\n", i, vi->range.first_use.abs_pos, vi->range.last_use.abs_pos);
+               }
        }
 #endif
 
@@ -502,12 +594,13 @@ optimize_initlocals (MonoCompile *cfg)
        mono_bitset_clear_all (used);
        initlocals_bb = cfg->bb_entry->next_bb;
        for (ins = initlocals_bb->code; ins; ins = ins->next) {
-               const char *spec = INS_INFO (ins->opcode);
+               int num_sregs, i;
+               int sregs [MONO_MAX_SRC_REGS];
+
+               num_sregs = mono_inst_get_src_registers (ins, sregs);
+               for (i = 0; i < num_sregs; ++i)
+                       mono_bitset_set_fast (used, sregs [i]);
 
-               if (spec [MONO_INST_SRC1] != ' ')
-                       mono_bitset_set_fast (used, ins->sreg1);
-               if (spec [MONO_INST_SRC2] != ' ')
-                       mono_bitset_set_fast (used, ins->sreg2);
                if (MONO_IS_STORE_MEMBASE (ins))
                        mono_bitset_set_fast (used, ins->dreg);
        }
@@ -521,7 +614,9 @@ optimize_initlocals (MonoCompile *cfg)
 
                        if (var && !mono_bitset_test_fast (used, ins->dreg) && !mono_bitset_test_fast (initlocals_bb->live_out_set, var->inst_c0) && (var != cfg->ret) && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
                                //printf ("DEAD: "); mono_print_ins (ins);
-                               if ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST)) {
+                               if (cfg->disable_initlocals_opt_refs && var->type == STACK_OBJ)
+                                       continue;
+                               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;
                                        /* 
@@ -676,23 +771,26 @@ mono_linterval_split (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInte
        }
 }
 
-#ifdef ENABLE_LIVENESS2
-
-#if 0
-#define LIVENESS_DEBUG(a) do { a; } while (0)
+#if 1
+#define LIVENESS_DEBUG(a) do { if (cfg->verbose_level > 1) do { a; } while (0); } while (0)
+#define ENABLE_LIVENESS_DEBUG 1
 #else
 #define LIVENESS_DEBUG(a)
 #endif
 
+#ifdef ENABLE_LIVENESS2
+
 static inline void
 update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int inst_num, gint32 *last_use)
 {
        const char *spec = INS_INFO (ins->opcode);
        int sreg;
+       int num_sregs, i;
+       int sregs [MONO_MAX_SRC_REGS];
 
        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 */
@@ -714,40 +812,37 @@ 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 = ins->sreg1 = ins->sreg2 = -1;
+                                       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);
                        }
                }
        }
 
-       /* SREG1 */
-       sreg = ins->sreg1;
-       if ((spec [MONO_INST_SRC1] != ' ') && get_vreg_to_inst (cfg, sreg)) {
-               MonoInst *var = get_vreg_to_inst (cfg, sreg);
-               int idx = var->inst_c0;
-
-               if (last_use [idx] == 0) {
-                       LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
-                       last_use [idx] = inst_num;
-               }
-       }
-
-       /* SREG2 */
-       sreg = ins->sreg2;
-       if ((spec [MONO_INST_SRC2] != ' ') && get_vreg_to_inst (cfg, sreg)) {
-               MonoInst *var = get_vreg_to_inst (cfg, sreg);
-               int idx = var->inst_c0;
+       /* SREGs */
+       num_sregs = mono_inst_get_src_registers (ins, sregs);
+       for (i = 0; i < num_sregs; ++i) {
+               sreg = sregs [i];
+               if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
+                       MonoInst *var = get_vreg_to_inst (cfg, sreg);
+                       int idx = var->inst_c0;
 
-               if (last_use [idx] == 0) {
-                       LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
-                       last_use [idx] = inst_num;
+                       if (last_use [idx] == 0) {
+                               LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
+                               last_use [idx] = inst_num;
+                       }
                }
        }
 }
@@ -755,17 +850,34 @@ update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int in
 static void
 mono_analyze_liveness2 (MonoCompile *cfg)
 {
-       int bnum, idx, i, j, nins, rem, max, max_vars, block_from, block_to, pos, reverse_len;
+       int bnum, idx, i, j, nins, max, max_vars, block_from, block_to, pos, reverse_len;
        gint32 *last_use;
        static guint32 disabled = -1;
        MonoInst **reverse;
 
        if (disabled == -1)
-               disabled = getenv ("DISABLED") != NULL;
+               disabled = g_getenv ("DISABLED") != NULL;
 
        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)));
 
        /*
@@ -793,12 +905,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));
 
@@ -806,7 +918,6 @@ mono_analyze_liveness2 (MonoCompile *cfg)
                
                /* For variables in bb->live_out, set last_use to block_to */
 
-               rem = max_vars % BITS_PER_CHUNK;
                max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
                for (j = 0; j < max; ++j) {
                        gsize bits_out;
@@ -884,3 +995,166 @@ mono_analyze_liveness2 (MonoCompile *cfg)
 }
 
 #endif
+
+#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
+
+static inline void
+update_liveness_gc (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, gint32 *last_use, MonoMethodVar **vreg_to_varinfo, GSList **callsites)
+{
+       if (ins->opcode == OP_GC_LIVENESS_DEF || ins->opcode == OP_GC_LIVENESS_USE) {
+               int vreg = ins->inst_c1;
+               MonoMethodVar *vi = vreg_to_varinfo [vreg];
+               int idx = vi->idx;
+               int pc_offset = ins->backend.pc_offset;
+
+               LIVENESS_DEBUG (printf ("\t%x: ", pc_offset); mono_print_ins (ins));
+
+               if (ins->opcode == OP_GC_LIVENESS_DEF) {
+                       if (last_use [idx] > 0) {
+                               LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", vreg, pc_offset, last_use [idx]));
+                               last_use [idx] = 0;
+                       }
+               } else {
+                       if (last_use [idx] == 0) {
+                               LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", vreg, pc_offset));
+                               last_use [idx] = pc_offset;
+                       }
+               }
+       } else if (ins->opcode == OP_GC_PARAM_SLOT_LIVENESS_DEF) {
+               GCCallSite *last;
+
+               /* Add it to the last callsite */
+               g_assert (*callsites);
+               last = (*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));
+               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->pc_offset = ins->backend.pc_offset;
+               for (i = 0; i < cfg->num_varinfo; ++i) {
+                       if (last_use [i] != 0) {
+                               LIVENESS_DEBUG (printf ("R%d", MONO_VARINFO (cfg, i)->vreg));
+                               callsite->liveness [i / 8] |= (1 << (i % 8));
+                       }
+               }
+               LIVENESS_DEBUG (printf ("\n"));
+               *callsites = g_slist_prepend_mempool (cfg->mempool, *callsites, callsite);
+       }
+}
+
+static inline int
+get_vreg_from_var (MonoCompile *cfg, MonoInst *var)
+{
+       if (var->opcode == OP_REGVAR)
+               /* dreg contains a hreg, but inst_c0 still contains the var index */
+               return MONO_VARINFO (cfg, var->inst_c0)->vreg;
+       else
+               /* dreg still contains the vreg */
+               return var->dreg;
+}
+
+/*
+ * mono_analyze_liveness_gc:
+ *
+ *   Compute liveness bitmaps for each call site.
+ * This function is a modified version of mono_analyze_liveness2 ().
+ */
+void
+mono_analyze_liveness_gc (MonoCompile *cfg)
+{
+       int idx, i, j, nins, max, max_vars, block_from, block_to, pos, reverse_len;
+       gint32 *last_use;
+       MonoInst **reverse;
+       MonoMethodVar **vreg_to_varinfo = NULL;
+       MonoBasicBlock *bb;
+       GSList *callsites;
+
+       LIVENESS_DEBUG (printf ("\n------------ GC LIVENESS: ----------\n"));
+
+       max_vars = cfg->num_varinfo;
+       last_use = g_new0 (gint32, max_vars);
+
+       /*
+        * var->inst_c0 no longer contains the variable index, so compute a mapping now.
+        */
+       vreg_to_varinfo = g_new0 (MonoMethodVar*, cfg->next_vreg);
+       for (idx = 0; idx < max_vars; ++idx) {
+               MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
+
+               vreg_to_varinfo [vi->vreg] = vi;
+       }
+
+       reverse_len = 1024;
+       reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
+
+       for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+               MonoInst *ins;
+
+               block_from = bb->real_native_offset;
+               block_to = bb->native_offset + bb->native_length;
+
+               LIVENESS_DEBUG (printf ("GC LIVENESS BB%d:\n", bb->block_num));
+
+               if (!bb->code)
+                       continue;
+
+               memset (last_use, 0, max_vars * sizeof (gint32));
+               
+               /* For variables in bb->live_out, set last_use to block_to */
+
+               max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
+               for (j = 0; j < max; ++j) {
+                       gsize bits_out;
+                       int k;
+
+                       if (!bb->live_out_set)
+                               /* The variables used in this bblock are volatile anyway */
+                               continue;
+
+                       bits_out = mono_bitset_get_fast (bb->live_out_set, j);
+                       k = (j * BITS_PER_CHUNK);       
+                       while (bits_out) {
+                               if ((bits_out & 1) && cfg->varinfo [k]->flags & MONO_INST_GC_TRACK) {
+                                       int vreg = get_vreg_from_var (cfg, cfg->varinfo [k]);
+                                       LIVENESS_DEBUG (printf ("Var R%d live at exit, last_use set to %x.\n", vreg, block_to));
+                                       last_use [k] = block_to;
+                               }
+                               bits_out >>= 1;
+                               k ++;
+                       }
+               }
+
+               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;
+               }
+
+               /* Process instructions backwards */
+               callsites = NULL;
+               for (i = nins - 1; i >= 0; --i) {
+                       MonoInst *ins = (MonoInst*)reverse [i];
+
+                       update_liveness_gc (cfg, bb, ins, last_use, vreg_to_varinfo, &callsites);
+               }
+               /* The callsites should already be sorted by pc offset because we added them backwards */
+               bb->gc_callsites = callsites;
+       }
+
+       g_free (last_use);
+       g_free (vreg_to_varinfo);
+}
+
+#endif /* DISABLE_JIT */