Use g_assertion_message instead of exit to kill an iOS app.
[mono.git] / mono / mini / liveness.c
index f8d073ff433ae5e08faa998024c0c3b6371b461e..aabae662bf7db5e9e42f47eb71972cae26df8b8a 100644 (file)
@@ -5,6 +5,7 @@
  *   Dietmar Maurer (dietmar@ximian.com)
  *
  * (C) 2002 Ximian, Inc.
+ * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
  */
 
 #include "mini.h"
@@ -138,6 +139,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 +183,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);
@@ -446,14 +486,13 @@ mono_analyze_liveness (MonoCompile *cfg)
 
        for (i = 0; i < cfg->num_bblocks; ++i) {
                MonoBasicBlock *bb = cfg->bblocks [i];
-               guint32 rem, max;
+               guint32 max;
                guint32 abs_pos = (bb->dfn << 16);
                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;
@@ -799,7 +838,7 @@ 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;
@@ -850,7 +889,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;
@@ -929,10 +967,10 @@ mono_analyze_liveness2 (MonoCompile *cfg)
 
 #endif
 
-#ifdef HAVE_SGEN_GC
+#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
 
 static inline void
-update_liveness2_gc (MonoCompile *cfg, MonoInst *ins, gint32 *last_use, MonoMethodVar **vreg_to_varinfo)
+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;
@@ -945,7 +983,6 @@ update_liveness2_gc (MonoCompile *cfg, MonoInst *ins, gint32 *last_use, MonoMeth
                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]));
-                               mono_linterval_add_range (cfg, vi->gc_interval, pc_offset, last_use [idx]);
                                last_use [idx] = 0;
                        }
                } else {
@@ -954,26 +991,60 @@ update_liveness2_gc (MonoCompile *cfg, MonoInst *ins, gint32 *last_use, MonoMeth
                                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 precise liveness intervals (unions for ranges) for variables which need GC
- * tracking.
- * The computed intervals are stored into MonoMethodVar->gc_interval.
+ *   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, 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;
        MonoBasicBlock *bb;
+       GSList *callsites;
 
        LIVENESS_DEBUG (printf ("\n------------ GC LIVENESS: ----------\n"));
 
@@ -993,16 +1064,6 @@ mono_analyze_liveness_gc (MonoCompile *cfg)
        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->gc_interval = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
-       }
-
-       /*
-        * FIXME: Process bblocks in reverse order, so the addition of new live ranges
-        * to the intervals is faster.
-        */
        for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
                MonoInst *ins;
 
@@ -1018,7 +1079,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;
@@ -1032,7 +1092,8 @@ mono_analyze_liveness_gc (MonoCompile *cfg)
                        k = (j * BITS_PER_CHUNK);       
                        while (bits_out) {
                                if ((bits_out & 1) && cfg->varinfo [k]->flags & MONO_INST_GC_TRACK) {
-                                       LIVENESS_DEBUG (printf ("Var R%d live at exit, last_use set to %x.\n", cfg->varinfo [j]->dreg, block_to));
+                                       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;
@@ -1053,41 +1114,17 @@ mono_analyze_liveness_gc (MonoCompile *cfg)
                }
 
                /* Process instructions backwards */
+               callsites = NULL;
                for (i = nins - 1; i >= 0; --i) {
                        MonoInst *ins = (MonoInst*)reverse [i];
 
-                       update_liveness2_gc (cfg, ins, last_use, vreg_to_varinfo);
-               }
-
-               for (idx = 0; idx < max_vars; ++idx) {
-                       MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
-
-                       if (last_use [idx] != 0 && cfg->varinfo [idx]->flags & MONO_INST_GC_TRACK) {
-                               /* Live at exit, not written -> live on enter */
-                               LIVENESS_DEBUG (printf ("Var R%d live at enter, add range to R%d: [%x, %x)\n", cfg->varinfo [idx]->dreg, cfg->varinfo [idx]->dreg, block_from, last_use [idx]));
-                               mono_linterval_add_range (cfg, vi->gc_interval, block_from, last_use [idx]);
-                       }
-               }
-       }
-
-       /* FIXME: Arguments, these are stored to in the prolog */
-
-#ifdef ENABLE_LIVENESS_DEBUG
-       LIVENESS_DEBUG (printf ("\nGC LIVENESS RESULT:\n"));
-       for (idx = 0; idx < max_vars; ++idx) {
-               MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
-
-               if (cfg->varinfo [idx]->opcode == OP_REGOFFSET && (cfg->varinfo [idx]->flags & MONO_INST_GC_TRACK)) {
-                       LIVENESS_DEBUG (printf ("\tR%d (%s+0x%x): ", cfg->varinfo [idx]->dreg, mono_arch_regname (cfg->varinfo [idx]->inst_basereg), (int)cfg->varinfo [idx]->inst_offset));
-                       LIVENESS_DEBUG (mono_linterval_print (vi->gc_interval));
-                       LIVENESS_DEBUG (printf ("\n"));
+                       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;
        }
-       LIVENESS_DEBUG (printf ("-------------------------------------\n\n"));
-#endif
 
        g_free (last_use);
        g_free (vreg_to_varinfo);
 }
 
-#endif