[threads] Use runtime flag to enable cooperative suspend
[mono.git] / mono / mini / liveness.c
index 5f304b390fd41f0c9fbc36ef9b3bc22315042b4a..355846bea67270805a5262f2d9518839fc2f33df 100644 (file)
@@ -20,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'.
@@ -217,7 +219,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) {
@@ -492,7 +494,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 +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 ++;
@@ -538,7 +540,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 +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;
                                        /* 
@@ -788,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 */
@@ -810,14 +812,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));
                                        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);
                        }
                }
        }
@@ -852,6 +861,23 @@ mono_analyze_liveness2 (MonoCompile *cfg)
        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)));
 
        /*
@@ -879,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));