[jit] Fix a regression introduced by 00a28a7dfd44a10c3bc4c3b57449c93aa5124092 which...
[mono.git] / mono / mini / liveness.c
index 12f96b3479b40dcff9b8549f2b95179cb134b5a1..4e316f6db021d3d0eec80b212950cedbc9472535 100644 (file)
@@ -5,8 +5,13 @@
  *   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))
@@ -15,6 +20,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'.
@@ -138,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
@@ -149,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);
@@ -173,9 +219,10 @@ 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];
@@ -446,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;
@@ -468,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 ++;
@@ -570,7 +616,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;
                                        /* 
@@ -744,7 +790,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 */
@@ -768,14 +814,19 @@ update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int in
                                /* 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)) {
                                        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);
                        }
                }
        }
@@ -799,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)))
+               /* 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)));
 
        /*
@@ -837,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));
 
@@ -850,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;
@@ -1001,7 +1068,7 @@ get_vreg_from_var (MonoCompile *cfg, MonoInst *var)
 void
 mono_analyze_liveness_gc (MonoCompile *cfg)
 {
-       int idx, i, j, nins, rem, max, max_vars, block_from, block_to, pos, reverse_len;
+       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;
@@ -1041,7 +1108,6 @@ mono_analyze_liveness_gc (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;
@@ -1091,3 +1157,4 @@ mono_analyze_liveness_gc (MonoCompile *cfg)
        g_free (vreg_to_varinfo);
 }
 
+#endif /* DISABLE_JIT */