[xbuild] Vbc task - make error column check a little non-specific.
[mono.git] / mono / mini / mini-gc.c
index f950df14d89ef149df9fce1656803b3b7040f011..3972f5730972f70359077abf0ee3aa51bedd0ed5 100644 (file)
 #include "mini-gc.h"
 #include <mono/metadata/gc-internal.h>
 
-/*
- * The code below does not work yet, and probably needs to be thrown out if we move
- * to GC safe points.
- */
-
 //#if 0
-#ifdef HAVE_SGEN_GC
+#if defined(MONO_ARCH_GC_MAPS_SUPPORTED)
 
 #include <mono/metadata/gc-internal.h>
 #include <mono/utils/mono-counters.h>
 
+#if SIZEOF_VOID_P == 4
+typedef guint32 mword;
+#else
+typedef guint64 mword;
+#endif
+
+#define GC_BITS_PER_WORD (sizeof (mword) * 8)
+
 /* Contains state needed by the GC Map construction code */
 typedef struct {
        /*
@@ -35,49 +38,81 @@ typedef struct {
 
        /* Number of slots in the map */
        int nslots;
-       /* The type of the slots */
-       StackSlotType *slots;
-       /* Live intervals for every slot */
-       GSList **live_intervals;
-       /* Whenever the slot starts out as SLOT_PIN, then changes to SLOT_REF */
-       gboolean *starts_pinned;
        /* The number of registers in the map */
        int nregs;
-       /*
-        * GC Type of registers.
-        * Registers might be shared between refs and non-refs, so we store a GC type
-        * for each live interval.
-        * FIXME: Do the same for slots too, i.e. make 'slots' a list.
-        * FIXME: Add a struct for the type + interval pair.
-        */
-       GSList **reg_types;
-       /* 
-        * Live intervals for registers.
-        * This has width MONO_MAX_IREGS.
-        * FIXME: Only store callee saved regs.
-        */
-       GSList **reg_live_intervals;
        /* Min and Max offsets of the stack frame relative to fp */
        int min_offset, max_offset;
        /* Same for the locals area */
        int locals_min_offset, locals_max_offset;
+
+       /* The call sites where this frame can be stopped during GC */
+       GCCallSite **callsites;
+       /* The number of call sites */
+       int ncallsites;
+
+       /*
+        * The width of the stack bitmaps in bytes. This is not equal to the bitmap width at
+     * runtime, since it includes columns which are 0.
+        */
+       int stack_bitmap_width;
+       /* 
+        * A bitmap whose width equals nslots, and whose height equals ncallsites.
+        * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
+        * given callsite.
+        */
+       guint8 *stack_ref_bitmap;
+       /* Same for SLOT_PIN */
+       guint8 *stack_pin_bitmap;
+
+       /*
+        * Similar bitmaps for registers. These have width MONO_MAX_IREGS in bits.
+        */
+       int reg_bitmap_width;
+       guint8 *reg_ref_bitmap;
+       guint8 *reg_pin_bitmap;
 } MonoCompileGC;
 
-#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
+#define ALIGN_TO(val,align) ((((mgreg_t)val) + ((align) - 1)) & ~((align) - 1))
+
+#undef DEBUG
 
 #if 0
-#define DEBUG(s) do { s; } while (0)
+/* We don't support debug levels, its all-or-nothing */
+#define DEBUG(s) do { s; fflush (logfile); } while (0)
+#define DEBUG_ENABLED 1
 #else
 #define DEBUG(s)
 #endif
 
-#if 1
-#define DEBUG_GC_MAP(s) do { s; fflush (stdout); } while (0)
+#ifdef DEBUG_ENABLED
+//#if 1
+#define DEBUG_PRECISE(s) do { s; } while (0)
+#define DEBUG_PRECISE_ENABLED
 #else
-#define DEBUG_GC_MAP(s)
+#define DEBUG_PRECISE(s)
+#endif
+
+/*
+ * Contains information collected during the conservative stack marking pass,
+ * used during the precise pass. This helps to avoid doing a stack walk twice, which
+ * is expensive.
+ */
+typedef struct {
+       guint8 *bitmap;
+       int nslots;
+    int frame_start_offset;
+       int nreg_locations;
+       /* Relative to stack_start */
+       int reg_locations [MONO_MAX_IREGS];
+#ifdef DEBUG_PRECISE_ENABLED
+       MonoJitInfo *ji;
+       gpointer fp;
+       int regs [MONO_MAX_IREGS];
 #endif
+} FrameInfo;
 
-#define GC_BITS_PER_WORD (sizeof (gsize) * 8)
+/* Max number of frames stored in the TLS data */
+#define MAX_FRAMES 50
 
 /*
  * Per-thread data kept by this module. This is stored in the GC and passed to us as
@@ -89,69 +124,429 @@ typedef struct {
        MonoContext ctx;
        gboolean has_context;
        MonoJitTlsData *jit_tls;
+       /* For debugging */
+       mgreg_t tid;
+       gpointer ref_to_track;
+       /* Number of frames collected during the !precise pass */
+       int nframes;
+       FrameInfo frames [MAX_FRAMES];
 } TlsData;
 
+/* These are constant so don't store them in the GC Maps */
+/* Number of registers stored in gc maps */
+#define NREGS MONO_MAX_IREGS
+
 /* 
+ * The GC Map itself.
  * Contains information needed to mark a stack frame.
- * FIXME: Optimize the memory usage.
+ * This is a transient structure, created from a compressed representation on-demand.
  */
 typedef struct {
-       /* The frame pointer register */
-       int frame_reg;
-       /* The offset of the GC tracked area inside the stack frame relative to the frame pointer */
-       int frame_offset;
-       /* The number of stack slots */
+       /*
+        * The offsets of the GC tracked area inside the stack frame relative to the frame pointer.
+        * This includes memory which is NOREF thus doesn't need GC maps.
+        */
+       int start_offset;
+       int end_offset;
+       /*
+        * The offset relative to frame_offset where the the memory described by the GC maps
+        * begins.
+        */
+       int map_offset;
+       /* The number of stack slots in the map */
        int nslots;
-       /* The number of registers in the map */
-       int nregs;
-       /* Thw width of the stack bitmap in bytes */
-       int bitmap_width;
-       /* Thw width of the register bitmap in bytes */
-       int reg_bitmap_width;
+       /* The frame pointer register */
+       guint8 frame_reg;
+       /* The size of each callsite table entry */
+       guint8 callsite_entry_size;
        guint has_pin_slots : 1;
        guint has_ref_slots : 1;
        guint has_ref_regs : 1;
        guint has_pin_regs : 1;
+
+       /* The offsets below are into an external bitmaps array */
+
        /* 
-        * A bitmap whose width is equal to bitmap_width, and whose
-        * height is equal to the number of possible PC offsets.
+        * A bitmap whose width is equal to bitmap_width, and whose height is equal to ncallsites.
         * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
-        * given pc offset.
-        * FIXME: Compress this.
-        * FIXME: Embed this after the structure.
+        * given callsite.
         */
-       guint8 *ref_bitmap;
+       guint32 stack_ref_bitmap_offset;
        /*
         * Same for SLOT_PIN. It is possible that the same bit is set in both bitmaps at
-     * different pc offsets, if the slot starts out as PIN, and later changes to REF.
+     * different callsites, if the slot starts out as PIN, and later changes to REF.
         */
-       guint8 *pin_bitmap;
+       guint32 stack_pin_bitmap_offset;
 
        /*
         * Corresponding bitmaps for registers
-        * These have width MONO_MAX_IREGS in bits.
+        * These have width equal to the number of bits set in reg_ref_mask/reg_pin_mask.
         * FIXME: Merge these with the normal bitmaps, i.e. reserve the first x slots for them ?
         */
-       guint8 *reg_pin_bitmap;
-       guint8 *reg_ref_bitmap;
+       guint32 reg_pin_bitmap_offset;
+       guint32 reg_ref_bitmap_offset;
+
+       guint32 used_int_regs, reg_ref_mask, reg_pin_mask;
 
-       /* The registers used by the method */
-       guint64 used_regs;
+       /* The number of bits set in the two masks above */
+       guint8 nref_regs, npin_regs;
 
        /*
         * A bit array marking slots which contain refs.
         * This is used only for debugging.
         */
-       guint8 *ref_slots;
+       //guint8 *ref_slots;
+
+       /* Callsite offsets */
+       /* These can take up a lot of space, so encode them compactly */
+       union {
+               guint8 *offsets8;
+               guint16 *offsets16;
+               guint32 *offsets32;
+       } callsites;
+       int ncallsites;
 } GCMap;
 
-/* Statistics */
-static guint32 gc_maps_size;
+/*
+ * A compressed version of GCMap. This is what gets stored in MonoJitInfo.
+ */
+typedef struct {
+       //guint8 *ref_slots;
+       //guint8 encoded_size;
+
+       /*
+        * The arrays below are embedded after the struct.
+        * Their address needs to be computed.
+        */
+
+       /* The fixed fields of the GCMap encoded using LEB128 */
+       guint8 encoded [MONO_ZERO_LEN_ARRAY];
+
+       /* An array of ncallsites entries, each entry is callsite_entry_size bytes long */
+       guint8 callsites [MONO_ZERO_LEN_ARRAY];
+
+       /* The GC bitmaps */
+       guint8 bitmaps [MONO_ZERO_LEN_ARRAY];
+} GCEncodedMap;
+
+static int precise_frame_count [2], precise_frame_limit = -1;
+static gboolean precise_frame_limit_inited;
+
+/* Stats */
+typedef struct {
+       int scanned_stacks;
+       int scanned;
+       int scanned_precisely;
+       int scanned_conservatively;
+       int scanned_registers;
+       int scanned_native;
+       int scanned_other;
+       
+       int all_slots;
+       int noref_slots;
+       int ref_slots;
+       int pin_slots;
+
+       int gc_maps_size;
+       int gc_callsites_size;
+       int gc_callsites8_size;
+       int gc_callsites16_size;
+       int gc_callsites32_size;
+       int gc_bitmaps_size;
+       int gc_map_struct_size;
+       int tlsdata_size;
+} JITGCStats;
+
+static JITGCStats stats;
+
+static FILE *logfile;
+
+// FIXME: Move these to a shared place
+
+static inline void
+encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
+{
+       guint8 *p = buf;
+
+       do {
+               guint8 b = value & 0x7f;
+               value >>= 7;
+               if (value != 0) /* more bytes to come */
+                       b |= 0x80;
+               *p ++ = b;
+       } while (value);
+
+       *endbuf = p;
+}
+
+static G_GNUC_UNUSED void
+encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
+{
+       gboolean more = 1;
+       gboolean negative = (value < 0);
+       guint32 size = 32;
+       guint8 byte;
+       guint8 *p = buf;
+
+       while (more) {
+               byte = value & 0x7f;
+               value >>= 7;
+               /* the following is unnecessary if the
+                * implementation of >>= uses an arithmetic rather
+                * than logical shift for a signed left operand
+                */
+               if (negative)
+                       /* sign extend */
+                       value |= - (1 <<(size - 7));
+               /* sign bit of byte is second high order bit (0x40) */
+               if ((value == 0 && !(byte & 0x40)) ||
+                       (value == -1 && (byte & 0x40)))
+                       more = 0;
+               else
+                       byte |= 0x80;
+               *p ++= byte;
+       }
+
+       *endbuf = p;
+}
+
+static inline guint32
+decode_uleb128 (guint8 *buf, guint8 **endbuf)
+{
+       guint8 *p = buf;
+       guint32 res = 0;
+       int shift = 0;
+
+       while (TRUE) {
+               guint8 b = *p;
+               p ++;
+
+               res = res | (((int)(b & 0x7f)) << shift);
+               if (!(b & 0x80))
+                       break;
+               shift += 7;
+       }
+
+       *endbuf = p;
+
+       return res;
+}
+
+static inline gint32
+decode_sleb128 (guint8 *buf, guint8 **endbuf)
+{
+       guint8 *p = buf;
+       gint32 res = 0;
+       int shift = 0;
+
+       while (TRUE) {
+               guint8 b = *p;
+               p ++;
+
+               res = res | (((int)(b & 0x7f)) << shift);
+               shift += 7;
+               if (!(b & 0x80)) {
+                       if (shift < 32 && (b & 0x40))
+                               res |= - (1 << shift);
+                       break;
+               }
+       }
+
+       *endbuf = p;
+
+       return res;
+}
+
+static int
+encode_frame_reg (int frame_reg)
+{
+#ifdef TARGET_AMD64
+       if (frame_reg == AMD64_RSP)
+               return 0;
+       else if (frame_reg == AMD64_RBP)
+               return 1;
+#elif defined(TARGET_X86)
+       if (frame_reg == X86_EBP)
+               return 0;
+       else if (frame_reg == X86_ESP)
+               return 1;
+#else
+       NOT_IMPLEMENTED;
+#endif
+       g_assert_not_reached ();
+       return -1;
+}
+
+static int
+decode_frame_reg (int encoded)
+{
+#ifdef TARGET_AMD64
+       if (encoded == 0)
+               return AMD64_RSP;
+       else if (encoded == 1)
+               return AMD64_RBP;
+#elif defined(TARGET_X86)
+       if (encoded == 0)
+               return X86_EBP;
+       else if (encoded == 1)
+               return X86_ESP;
+#else
+       NOT_IMPLEMENTED;
+#endif
+       g_assert_not_reached ();
+       return -1;
+}
+
+#ifdef TARGET_AMD64
+#ifdef HOST_WIN32
+static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15, AMD64_RDI, AMD64_RSI };
+#else
+static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15 };
+#endif
+#elif defined(TARGET_X86)
+static int callee_saved_regs [] = { X86_EBX, X86_ESI, X86_EDI };
+#endif
+
+static guint32
+encode_regmask (guint32 regmask)
+{
+       int i;
+       guint32 res;
+
+       res = 0;
+       for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i) {
+               if (regmask & (1 << callee_saved_regs [i])) {
+                       res |= (1 << i);
+                       regmask -= (1 << callee_saved_regs [i]);
+               }
+       }
+       g_assert (regmask == 0);
+       return res;
+}
+
+static guint32
+decode_regmask (guint32 regmask)
+{
+       int i;
+       guint32 res;
+
+       res = 0;
+       for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i)
+               if (regmask & (1 << i))
+                       res |= (1 << callee_saved_regs [i]);
+       return res;
+}
+
+/*
+ * encode_gc_map:
+ *
+ *   Encode the fixed fields of MAP into a buffer pointed to by BUF.
+ */
+static void
+encode_gc_map (GCMap *map, guint8 *buf, guint8 **endbuf)
+{
+       guint32 flags, freg;
+
+       encode_sleb128 (map->start_offset / sizeof (mgreg_t), buf, &buf);
+       encode_sleb128 (map->end_offset / sizeof (mgreg_t), buf, &buf);
+       encode_sleb128 (map->map_offset / sizeof (mgreg_t), buf, &buf);
+       encode_uleb128 (map->nslots, buf, &buf);
+       g_assert (map->callsite_entry_size <= 4);
+       freg = encode_frame_reg (map->frame_reg);
+       g_assert (freg < 2);
+       flags = (map->has_ref_slots ? 1 : 0) | (map->has_pin_slots ? 2 : 0) | (map->has_ref_regs ? 4 : 0) | (map->has_pin_regs ? 8 : 0) | ((map->callsite_entry_size - 1) << 4) | (freg << 6);
+       encode_uleb128 (flags, buf, &buf);
+       encode_uleb128 (encode_regmask (map->used_int_regs), buf, &buf);
+       if (map->has_ref_regs)
+               encode_uleb128 (encode_regmask (map->reg_ref_mask), buf, &buf);
+       if (map->has_pin_regs)
+               encode_uleb128 (encode_regmask (map->reg_pin_mask), buf, &buf);
+       encode_uleb128 (map->ncallsites, buf, &buf);
+
+       *endbuf = buf;
+}      
+
+/*
+ * decode_gc_map:
+ *
+ *   Decode the encoded GC map representation in BUF and store the result into MAP.
+ */
+static void
+decode_gc_map (guint8 *buf, GCMap *map, guint8 **endbuf)
+{
+       guint32 flags;
+       int stack_bitmap_size, reg_ref_bitmap_size, reg_pin_bitmap_size, offset, freg;
+       int i, n;
+
+       map->start_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
+       map->end_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
+       map->map_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
+       map->nslots = decode_uleb128 (buf, &buf);
+       flags = decode_uleb128 (buf, &buf);
+       map->has_ref_slots = (flags & 1) ? 1 : 0;
+       map->has_pin_slots = (flags & 2) ? 1 : 0;
+       map->has_ref_regs = (flags & 4) ? 1 : 0;
+       map->has_pin_regs = (flags & 8) ? 1 : 0;
+       map->callsite_entry_size = ((flags >> 4) & 0x3) + 1;
+       freg = flags >> 6;
+       map->frame_reg = decode_frame_reg (freg);
+       map->used_int_regs = decode_regmask (decode_uleb128 (buf, &buf));
+       if (map->has_ref_regs) {
+               map->reg_ref_mask = decode_regmask (decode_uleb128 (buf, &buf));
+               n = 0;
+               for (i = 0; i < NREGS; ++i)
+                       if (map->reg_ref_mask & (1 << i))
+                               n ++;
+               map->nref_regs = n;
+       }
+       if (map->has_pin_regs) {
+               map->reg_pin_mask = decode_regmask (decode_uleb128 (buf, &buf));
+               n = 0;
+               for (i = 0; i < NREGS; ++i)
+                       if (map->reg_pin_mask & (1 << i))
+                               n ++;
+               map->npin_regs = n;
+       }
+       map->ncallsites = decode_uleb128 (buf, &buf);
+
+       stack_bitmap_size = (ALIGN_TO (map->nslots, 8) / 8) * map->ncallsites;
+       reg_ref_bitmap_size = (ALIGN_TO (map->nref_regs, 8) / 8) * map->ncallsites;
+       reg_pin_bitmap_size = (ALIGN_TO (map->npin_regs, 8) / 8) * map->ncallsites;
+       offset = 0;
+       map->stack_ref_bitmap_offset = offset;
+       if (map->has_ref_slots)
+               offset += stack_bitmap_size;
+       map->stack_pin_bitmap_offset = offset;
+       if (map->has_pin_slots)
+               offset += stack_bitmap_size;
+       map->reg_ref_bitmap_offset = offset;
+       if (map->has_ref_regs)
+               offset += reg_ref_bitmap_size;
+       map->reg_pin_bitmap_offset = offset;
+       if (map->has_pin_regs)
+               offset += reg_pin_bitmap_size;
+
+       *endbuf = buf;
+}
 
 static gpointer
 thread_attach_func (void)
 {
-       return g_new0 (TlsData, 1);
+       TlsData *tls;
+
+       tls = g_new0 (TlsData, 1);
+       tls->tid = GetCurrentThreadId ();
+       stats.tlsdata_size += sizeof (TlsData);
+
+       return tls;
+}
+
+static void
+thread_detach_func (gpointer user_data)
+{
+       TlsData *tls = user_data;
+
+       g_free (tls);
 }
 
 static void
@@ -173,25 +568,6 @@ thread_suspend_func (gpointer user_data, void *sigctx)
        tls->jit_tls = TlsGetValue (mono_jit_tls_id);
 }
 
-static int precise_frame_count [2], precise_frame_limit = -1;
-static gboolean precise_frame_limit_inited;
-
-/* Stats */
-typedef struct {
-       int scanned_stacks;
-       int scanned;
-       int scanned_precisely;
-       int scanned_conservatively;
-       int scanned_registers;
-
-       int all_slots;
-       int noref_slots;
-       int ref_slots;
-       int pin_slots;
-} JITGCStats;
-
-static JITGCStats stats;
-
 #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
 
 static inline void
@@ -213,7 +589,7 @@ get_bit (guint8 *bitmap, int width, int y, int x)
 }
 
 static const char*
-slot_type_to_string (StackSlotType type)
+slot_type_to_string (GCSlotType type)
 {
        switch (type) {
        case SLOT_REF:
@@ -228,37 +604,60 @@ slot_type_to_string (StackSlotType type)
        }
 }
 
+static inline mgreg_t
+get_frame_pointer (MonoContext *ctx, int frame_reg)
+{
+#if defined(TARGET_AMD64)
+               if (frame_reg == AMD64_RSP)
+                       return ctx->rsp;
+               else if (frame_reg == AMD64_RBP)
+                       return ctx->rbp;
+#elif defined(TARGET_X86)
+               if (frame_reg == X86_ESP)
+                       return ctx->esp;
+               else if (frame_reg == X86_EBP)
+                       return ctx->ebp;
+#endif
+               g_assert_not_reached ();
+               return 0;
+}
+
 /*
- * thread_mark_func:
+ * conservatively_pass:
  *
- *   This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
- * call, and TRUE at the second. USER_DATA points to a TlsData
- * structure filled up by thread_suspend_func. 
+ *   Mark a thread stack conservatively and collect information needed by the precise pass.
  */
 static void
-thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise)
+conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
 {
-       TlsData *tls = user_data;
        MonoJitInfo *ji;
        MonoContext ctx, new_ctx;
        MonoLMF *lmf;
        guint8 *stack_limit;
        gboolean last = TRUE;
        GCMap *map;
-       guint8* fp, *frame_start, *frame_end;
-       int i, pc_offset;
+       GCMap map_tmp;
+       GCEncodedMap *emap;
+       guint8* fp, *p, *real_frame_start, *frame_start, *frame_end;
+       int i, pc_offset, cindex, bitmap_width;
        int scanned = 0, scanned_precisely, scanned_conservatively, scanned_registers;
        gboolean res;
        StackFrameInfo frame;
        mgreg_t *reg_locations [MONO_MAX_IREGS];
        mgreg_t *new_reg_locations [MONO_MAX_IREGS];
+       guint8 *bitmaps;
+       FrameInfo *fi;
+       guint32 precise_regmask;
+
+       if (tls) {
+               tls->nframes = 0;
+               tls->ref_to_track = NULL;
+       }
 
        /* tls == NULL can happen during startup */
        if (mono_thread_internal_current () == NULL || !tls) {
-               if (!precise) {
-                       mono_gc_conservatively_scan_area (stack_start, stack_end);
-                       stats.scanned_stacks += stack_end - stack_start;
-               }
+               mono_gc_conservatively_scan_area (stack_start, stack_end);
+               stats.scanned_stacks += stack_end - stack_start;
                return;
        }
 
@@ -277,8 +676,6 @@ thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gb
        /* This is one past the last address which we have scanned */
        stack_limit = stack_start;
 
-       DEBUG (printf ("*** %s stack marking %p-%p ***\n", precise ? "Precise" : "Conservative", stack_start, stack_end));
-
        if (!tls->has_context)
                memset (&new_ctx, 0, sizeof (ctx));
        else
@@ -295,49 +692,79 @@ thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gb
                                /*
                                 * If the current frame saves the register, it means it might modify its
                                 * value, thus the old location might not contain the same value, so
-                                * we have to mark it conservatively. If we have precise info about the
-                                * register, reg_locations [i] is already cleared.
+                                * we have to mark it conservatively.
                                 */
-                               if (!precise && reg_locations [i]) {
-                                       DEBUG (printf ("\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
+                               if (reg_locations [i]) {
+                                       DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
                                        mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
-                                       // FIXME: This is not correct because the location might be in a frame
-                                       // without a GC map
-                                       // Use a separate stat for now
-                                       //scanned_conservatively += sizeof (mgreg_t);
                                        scanned_registers += sizeof (mgreg_t);
                                }
 
                                reg_locations [i] = new_reg_locations [i];
 
-                               if (!precise) {
-                                       DEBUG (printf ("\treg %s is at location %p.\n", mono_arch_regname (i), reg_locations [i]));
-                               }
+                               DEBUG (fprintf (logfile, "\treg %s is now at location %p.\n", mono_arch_regname (i), reg_locations [i]));
                        }
                }
 
-               g_assert ((guint64)stack_limit % sizeof (mgreg_t) == 0);
+               g_assert ((mgreg_t)stack_limit % sizeof (mgreg_t) == 0);
 
-#ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
                res = mono_find_jit_info_ext (frame.domain ? frame.domain : mono_domain_get (), tls->jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, new_reg_locations, &frame);
                if (!res)
                        break;
-#else
-               break;
-#endif
+
+               ji = frame.ji;
+
+               if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
+                       /*
+                        * These frames are problematic for several reasons:
+                        * - they are unwound through an LMF, and we have no precise register tracking for those.
+                        * - the LMF might not contain a precise ip, so we can't compute the call site.
+                        * - the LMF only unwinds to the wrapper frame, so we get these methods twice.
+                        */
+                       DEBUG (fprintf (logfile, "Mark(0): <Managed-to-native transition>\n"));
+                       for (i = 0; i < MONO_MAX_IREGS; ++i) {
+                               if (reg_locations [i]) {
+                                       DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
+                                       mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
+                                       scanned_registers += sizeof (mgreg_t);
+                               }
+                               reg_locations [i] = NULL;
+                               new_reg_locations [i] = NULL;
+                       }
+                       ctx = new_ctx;
+                       continue;
+               }
 
                /* The last frame can be in any state so mark conservatively */
                if (last) {
+                       if (ji) {
+                               DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
+                       }
+                       DEBUG (fprintf (logfile, "\t <Last frame>\n"));
                        last = FALSE;
                        continue;
                }
 
-               /* These frames are returned by mono_find_jit_info () two times */
-               if (!frame.managed)
+               pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
+
+               /* These frames are very problematic */
+               if (ji->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
+                       DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
+                       DEBUG (fprintf (logfile, "\tSkip.\n"));
                        continue;
+               }
 
                /* All the other frames are at a call site */
 
+               if (tls->nframes == MAX_FRAMES) {
+                       /* 
+                        * Can't save information since the array is full. So scan the rest of the
+                        * stack conservatively.
+                        */
+                       DEBUG (fprintf (logfile, "Mark (0): Frame stack full.\n"));
+                       break;
+               }
+
                /* Scan the frame of this method */
 
                /*
@@ -347,18 +774,20 @@ thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gb
                 * - locals
                 * - spill area
                 * - localloc-ed memory
-                * Currently, only the locals/args are scanned precisely.
                 */
+               g_assert (pc_offset >= 0);
 
-               ji = frame.ji;
-               map = ji->gc_info;
-
-               if (!map) {
-                       DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); printf ("Mark(%d): No GC map for %s\n", precise, fname); g_free (fname));
+               emap = ji->gc_info;
 
+               if (!emap) {
+                       DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
+                       DEBUG (fprintf (logfile, "\tNo GC Map.\n"));
                        continue;
                }
 
+               /* The embedded callsite table requires this */
+               g_assert (((mgreg_t)emap % 4) == 0);
+
                /*
                 * Debugging aid to control the number of frames scanned precisely
                 */
@@ -369,207 +798,324 @@ thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gb
                }
                                
                if (precise_frame_limit != -1) {
-                       if (precise_frame_count [precise] == precise_frame_limit)
+                       if (precise_frame_count [FALSE] == precise_frame_limit)
                                printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (ji->method, TRUE));
-                       if (precise_frame_count [precise] > precise_frame_limit)
+                       if (precise_frame_count [FALSE] > precise_frame_limit)
                                continue;
                }
-               precise_frame_count [precise] ++;
-
-#ifdef __x86_64__
-               if (map->frame_reg == AMD64_RSP)
-                       fp = (guint8*)ctx.rsp;
-               else if (map->frame_reg == AMD64_RBP)
-                       fp = (guint8*)ctx.rbp;
-               else
+               precise_frame_count [FALSE] ++;
+
+               /* Decode the encoded GC map */
+               map = &map_tmp;
+               memset (map, 0, sizeof (GCMap));
+               decode_gc_map (&emap->encoded [0], map, &p);
+               p = (guint8*)ALIGN_TO (p, map->callsite_entry_size);
+               map->callsites.offsets8 = p;
+               p += map->callsite_entry_size * map->ncallsites;
+               bitmaps = p;
+
+               fp = (guint8*)get_frame_pointer (&ctx, map->frame_reg);
+
+               real_frame_start = fp + map->start_offset;
+               frame_start = fp + map->start_offset + map->map_offset;
+               frame_end = fp + map->end_offset;
+
+               DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p) limit=%p fp=%p frame=%p-%p (%d)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx), stack_limit, fp, frame_start, frame_end, (int)(frame_end - frame_start)); g_free (fname));
+
+               /* Find the callsite index */
+               if (map->callsite_entry_size == 1) {
+                       for (i = 0; i < map->ncallsites; ++i)
+                               /* ip points inside the call instruction */
+                               if (map->callsites.offsets8 [i] == pc_offset + 1)
+                                       break;
+               } else if (map->callsite_entry_size == 2) {
+                       // FIXME: Use a binary search
+                       for (i = 0; i < map->ncallsites; ++i)
+                               /* ip points inside the call instruction */
+                               if (map->callsites.offsets16 [i] == pc_offset + 1)
+                                       break;
+               } else {
+                       // FIXME: Use a binary search
+                       for (i = 0; i < map->ncallsites; ++i)
+                               /* ip points inside the call instruction */
+                               if (map->callsites.offsets32 [i] == pc_offset + 1)
+                                       break;
+               }
+               if (i == map->ncallsites) {
+                       printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (ji->method, TRUE));
                        g_assert_not_reached ();
-#else
-               fp = NULL;
-               g_assert_not_reached ();
-#endif
+               }
+               cindex = i;
 
-               frame_start = fp + map->frame_offset;
-               frame_end = frame_start + (map->nslots * sizeof (mgreg_t));
+               g_assert (real_frame_start >= stack_limit);
 
-               pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
-               g_assert (pc_offset >= 0);
+               if (real_frame_start > stack_limit) {
+                       /* This scans the previously skipped frames as well */
+                       DEBUG (fprintf (logfile, "\tscan area %p-%p (%d).\n", stack_limit, real_frame_start, (int)(real_frame_start - stack_limit)));
+                       mono_gc_conservatively_scan_area (stack_limit, real_frame_start);
+                       stats.scanned_other += real_frame_start - stack_limit;
+               }
 
-               DEBUG (char *fname = mono_method_full_name (ji->method, TRUE); printf ("Mark(%d): %s+0x%x (%p) limit=%p fp=%p frame=%p-%p (%d)\n", precise, fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx), stack_limit, fp, frame_start, frame_end, (int)(frame_end - frame_start)); g_free (fname));
+               /* Mark stack slots */
+               if (map->has_pin_slots) {
+                       int bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
+                       guint8 *pin_bitmap = &bitmaps [map->stack_pin_bitmap_offset + (bitmap_width * cindex)];
+                       guint8 *p;
+                       gboolean pinned;
+
+                       p = frame_start;
+                       for (i = 0; i < map->nslots; ++i) {
+                               pinned = pin_bitmap [i / 8] & (1 << (i % 8));
+                               if (pinned) {
+                                       DEBUG (fprintf (logfile, "\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
+                                       mono_gc_conservatively_scan_area (p, p + sizeof (mgreg_t));
+                                       scanned_conservatively += sizeof (mgreg_t);
+                               } else {
+                                       scanned_precisely += sizeof (mgreg_t);
+                               }
+                               p += sizeof (mgreg_t);
+                       }
+               } else {
+                       scanned_precisely += (map->nslots * sizeof (mgreg_t));
+               }
 
-               /* 
-                * FIXME: Add a function to mark using a bitmap, to avoid doing a 
-                * call for each object.
-                */
+               /* The area outside of start-end is NOREF */
+               scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * sizeof (mgreg_t));
+
+               /* Mark registers */
+               precise_regmask = map->used_int_regs | (1 << map->frame_reg);
+               if (map->has_pin_regs) {
+                       int bitmap_width = ALIGN_TO (map->npin_regs, 8) / 8;
+                       guint8 *pin_bitmap = &bitmaps [map->reg_pin_bitmap_offset + (bitmap_width * cindex)];
+                       int bindex = 0;
+                       for (i = 0; i < NREGS; ++i) {
+                               if (!(map->used_int_regs & (1 << i)))
+                                       continue;
+                               
+                               if (!(map->reg_pin_mask & (1 << i)))
+                                       continue;
 
-               /* Pinning needs to be done first, then the precise scan later */
+                               if (pin_bitmap [bindex / 8] & (1 << (bindex % 8))) {
+                                       DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
+                                       precise_regmask &= ~(1 << i);
+                               }
+                               bindex ++;
+                       }
+               }
 
-               if (!precise) {
-                       g_assert (frame_start >= stack_limit);
+               scanned += map->end_offset - map->start_offset;
 
-                       if (frame_start > stack_limit) {
-                               /* This scans the previously skipped frames as well */
-                               DEBUG (printf ("\tscan area %p-%p.\n", stack_limit, frame_start));
-                               mono_gc_conservatively_scan_area (stack_limit, frame_start);
-                       }
+               g_assert (scanned == scanned_precisely + scanned_conservatively);
 
-                       /* Mark stack slots */
-                       if (map->has_pin_slots) {
-                               guint8 *pin_bitmap = &map->pin_bitmap [(map->bitmap_width * pc_offset)];
-                               guint8 *p;
-                               gboolean pinned;
-
-                               p = frame_start;
-                               for (i = 0; i < map->nslots; ++i) {
-                                       pinned = pin_bitmap [i / 8] & (1 << (i % 8));
-                                       if (pinned) {
-                                               DEBUG (printf ("\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
-                                               mono_gc_conservatively_scan_area (p, p + sizeof (mgreg_t));
-                                               scanned_conservatively += sizeof (mgreg_t);
-                                       } else {
-                                               scanned_precisely += sizeof (mgreg_t);
-                                       }
-                                       p += sizeof (mgreg_t);
+               stack_limit = frame_end;
+
+               /* Save information for the precise pass */
+               fi = &tls->frames [tls->nframes];
+               fi->nslots = map->nslots;
+               bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
+               if (map->has_ref_slots)
+                       fi->bitmap = &bitmaps [map->stack_ref_bitmap_offset + (bitmap_width * cindex)];
+               else
+                       fi->bitmap = NULL;
+               fi->frame_start_offset = frame_start - stack_start;
+               fi->nreg_locations = 0;
+               DEBUG_PRECISE (fi->ji = ji);
+               DEBUG_PRECISE (fi->fp = fp);
+
+               if (map->has_ref_regs) {
+                       int bitmap_width = ALIGN_TO (map->nref_regs, 8) / 8;
+                       guint8 *ref_bitmap = &bitmaps [map->reg_ref_bitmap_offset + (bitmap_width * cindex)];
+                       int bindex = 0;
+                       for (i = 0; i < NREGS; ++i) {
+                               if (!(map->reg_ref_mask & (1 << i)))
+                                       continue;
+
+                               if (reg_locations [i] && (ref_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
+                                       DEBUG_PRECISE (fi->regs [fi->nreg_locations] = i);
+                                       DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i), reg_locations [i]));
+                                       fi->reg_locations [fi->nreg_locations] = (guint8*)reg_locations [i] - stack_start;
+                                       fi->nreg_locations ++;
                                }
-                       } else {
-                               scanned_precisely += map->nslots * sizeof (mgreg_t);
+                               bindex ++;
                        }
+               }
 
-                       /* Mark registers */
-                       if (map->has_pin_regs) {
-                               guint8 *pin_bitmap = &map->reg_pin_bitmap [(map->reg_bitmap_width * pc_offset)];
-                               for (i = 0; i < map->nregs; ++i) {
-                                       if (!(map->used_regs & (1 << i)))
-                                               continue;
+               /*
+                * Clear locations of precisely stacked registers.
+                */
+               if (precise_regmask) {
+                       for (i = 0; i < NREGS; ++i) {
+                               if (precise_regmask & (1 << i)) {
+                                       /*
+                                        * The method uses this register, and we have precise info for it.
+                                        * This means the location will be scanned precisely.
+                                        * Tell the code at the beginning of the loop that this location is
+                                        * processed.
+                                        */
+                                       if (reg_locations [i])
+                                               DEBUG (fprintf (logfile, "\treg %s at location %p (==%p) is precise.\n", mono_arch_regname (i), reg_locations [i], (gpointer)*reg_locations [i]));
+                                       reg_locations [i] = NULL;
+                               }
+                       }
+               }
 
-                                       /* We treated the save slots as precise above */
-                                       scanned_precisely -= sizeof (mgreg_t);
+               tls->nframes ++;
+       }
 
-                                       if (!reg_locations [i])
-                                               continue;
+       /* Scan the remaining register save locations */
+       for (i = 0; i < MONO_MAX_IREGS; ++i) {
+               if (reg_locations [i]) {
+                       DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", reg_locations [i]));
+                       mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
+                       scanned_registers += sizeof (mgreg_t);
+               }
+               if (new_reg_locations [i]) {
+                       DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", new_reg_locations [i]));
+                       mono_gc_conservatively_scan_area (new_reg_locations [i], new_reg_locations [i] + sizeof (mgreg_t));
+                       scanned_registers += sizeof (mgreg_t);
+               }
+       }
 
-                                       if (!(pin_bitmap [i / 8] & (1 << (i % 8)))) {
-                                               /*
-                                                * The method uses this register, and we have precise info for it.
-                                                * This means the location will be scanned precisely.
-                                                * Tell the code at the beginning of the loop that this location is
-                                                * processed.
-                                                */
-                                               DEBUG (printf ("\treg %s at location %p is precise.\n", mono_arch_regname (i), reg_locations [i]));
-                                               reg_locations [i] = NULL;
-                                               scanned_precisely += sizeof (mgreg_t);
-                                       }
-                               }
-                       }
+       if (stack_limit < stack_end) {
+               DEBUG (fprintf (logfile, "\tscan remaining stack %p-%p (%d).\n", stack_limit, stack_end, (int)(stack_end - stack_limit)));
+               mono_gc_conservatively_scan_area (stack_limit, stack_end);
+               stats.scanned_native += stack_end - stack_limit;
+       }
 
-                       scanned += frame_end - frame_start;
+       DEBUG (fprintf (logfile, "Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
 
-                       /* Not == because registers are marked in a later frame */
-                       g_assert (scanned >= scanned_precisely + scanned_conservatively);
+       stats.scanned_stacks += stack_end - stack_start;
+       stats.scanned += scanned;
+       stats.scanned_precisely += scanned_precisely;
+       stats.scanned_conservatively += scanned_conservatively;
+       stats.scanned_registers += scanned_registers;
 
-                       stack_limit = frame_end;
-               } else {
-                       /* Mark stack slots */
-                       if (map->has_ref_slots) {
-                               guint8 *ref_bitmap = &map->ref_bitmap [(map->bitmap_width * pc_offset)];
-                               gboolean live;
-
-                               for (i = 0; i < map->nslots; ++i) {
-                                       MonoObject **ptr = (MonoObject**)(frame_start + (i * sizeof (mgreg_t)));
-
-                                       live = ref_bitmap [i / 8] & (1 << (i % 8));
-
-                                       if (live) {
-                                               MonoObject *obj = *ptr;
-                                               if (obj) {
-                                                       DEBUG (printf ("\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fp)), ptr, obj));
-                                                       *ptr = mono_gc_scan_object (obj);
-                                                       DEBUG (printf (" %p.\n", *ptr));
-                                               } else {
-                                                       DEBUG (printf ("\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fp)), ptr, obj));
-                                               }
-                                       } else {
-                                               if (map->ref_slots [i / 8] & (1 << (i % 8))) {
-                                                       DEBUG (printf ("\tref %s0x%x(fp)=%p: dead (%p)\n", (guint8*)ptr >= (guint8*)fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fp)), ptr, *ptr));
-                                                       /*
-                                                        * Fail fast if the live range is incorrect, and
-                                                        * the JITted code tries to access this object
-                                                        */
-                                                       *ptr = DEAD_REF;
-                                               }
-                                       }
-                               }
-                       }
+       //mono_gc_conservatively_scan_area (stack_start, stack_end);
+}
 
-                       /* Mark registers */
+/*
+ * precise_pass:
+ *
+ *   Mark a thread stack precisely based on information saved during the conservative
+ * pass.
+ */
+static void
+precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
+{
+       int findex, i;
+       FrameInfo *fi;
+       guint8 *frame_start;
 
-                       /*
-                        * Registers are different from stack slots, they have no address where they
-                        * are stored. Instead, some frame below this frame in the stack saves them
-                        * in its prolog to the stack. We can mark this location precisely.
-                        */
-                       if (map->has_ref_regs) {
-                               guint8 *ref_bitmap = &map->reg_ref_bitmap [(map->reg_bitmap_width * pc_offset)];
-                               for (i = 0; i < map->nregs; ++i) {
-                                       if (!reg_locations [i])
-                                               continue;
+       if (!tls)
+               return;
+
+       for (findex = 0; findex < tls->nframes; findex ++) {
+               /* Load information saved by the !precise pass */
+               fi = &tls->frames [findex];
+               frame_start = stack_start + fi->frame_start_offset;
+
+               DEBUG (char *fname = mono_method_full_name (fi->ji->method, TRUE); fprintf (logfile, "Mark(1): %s\n", fname); g_free (fname));
+
+               /* 
+                * FIXME: Add a function to mark using a bitmap, to avoid doing a 
+                * call for each object.
+                */
+
+               /* Mark stack slots */
+               if (fi->bitmap) {
+                       guint8 *ref_bitmap = fi->bitmap;
+                       gboolean live;
+
+                       for (i = 0; i < fi->nslots; ++i) {
+                               MonoObject **ptr = (MonoObject**)(frame_start + (i * sizeof (mgreg_t)));
 
-                                       if (ref_bitmap [i / 8] & (1 << (i % 8))) {
+                               live = ref_bitmap [i / 8] & (1 << (i % 8));
+
+                               if (live) {
+                                       MonoObject *obj = *ptr;
+                                       if (obj) {
+                                               DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
+                                               *ptr = mono_gc_scan_object (obj);
+                                               DEBUG (fprintf (logfile, " %p.\n", *ptr));
+                                       } else {
+                                               DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
+                                       }
+                               } else {
+#if 0
+                                       /*
+                                        * This is disabled because the pointer takes up a lot of space.
+                                        * Stack slots might be shared between ref and non-ref variables ?
+                                        */
+                                       if (map->ref_slots [i / 8] & (1 << (i % 8))) {
+                                               DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: dead (%p)\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, *ptr));
                                                /*
-                                                * reg_locations [i] contains the address of the stack slot where
-                                                * i was last saved, so mark that slot.
+                                                * Fail fast if the live range is incorrect, and
+                                                * the JITted code tries to access this object
                                                 */
-                                               MonoObject **ptr = (MonoObject**)reg_locations [i];
-                                               MonoObject *obj = *ptr;
-
-                                               if (obj) {
-                                                       DEBUG (printf ("\treg %s saved at %p: %p ->", mono_arch_regname (i), reg_locations [i], obj));
-                                                       *ptr = mono_gc_scan_object (obj);
-                                                       DEBUG (printf (" %p.\n", *ptr));
-                                               } else {
-                                                       DEBUG (printf ("\treg %s saved at %p: %p", mono_arch_regname (i), reg_locations [i], obj));
-                                               }
+                                               *ptr = DEAD_REF;
                                        }
-
-                                       /* Mark the save slot as processed */
-                                       reg_locations [i] = NULL;
+#endif
                                }
-                       }       
+                       }
                }
-       }
 
-       if (!precise) {
-               /* Scan the remaining register save locations */
-               for (i = 0; i < MONO_MAX_IREGS; ++i) {
-                       if (reg_locations [i]) {
-                               DEBUG (printf ("\tscan saved reg location %p.\n", reg_locations [i]));
-                               mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
-                               scanned_conservatively += sizeof (mgreg_t);
-                       }
-                       // FIXME: Is this needed ?
-                       if (new_reg_locations [i]) {
-                               DEBUG (printf ("\tscan saved reg location %p.\n", new_reg_locations [i]));
-                               mono_gc_conservatively_scan_area (new_reg_locations [i], new_reg_locations [i] + sizeof (mgreg_t));
-                               scanned_conservatively += sizeof (mgreg_t);
+               /* Mark registers */
+
+               /*
+                * Registers are different from stack slots, they have no address where they
+                * are stored. Instead, some frame below this frame in the stack saves them
+                * in its prolog to the stack. We can mark this location precisely.
+                */
+               for (i = 0; i < fi->nreg_locations; ++i) {
+                       /*
+                        * reg_locations [i] contains the address of the stack slot where
+                        * a reg was last saved, so mark that slot.
+                        */
+                       MonoObject **ptr = (MonoObject**)((guint8*)stack_start + fi->reg_locations [i]);
+                       MonoObject *obj = *ptr;
+
+                       if (obj) {
+                               DEBUG (fprintf (logfile, "\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj));
+                               *ptr = mono_gc_scan_object (obj);
+                               DEBUG (fprintf (logfile, " %p.\n", *ptr));
+                       } else {
+                               DEBUG (fprintf (logfile, "\treg %s saved at %p: %p\n", mono_arch_regname (fi->regs [i]), ptr, obj));
                        }
-               }
+               }       
        }
 
-       if (stack_limit < stack_end && !precise) {
-               DEBUG (printf ("\tscan area %p-%p.\n", stack_limit, stack_end));
-               mono_gc_conservatively_scan_area (stack_limit, stack_end);
+       /*
+        * Debugging aid to check for missed refs.
+        */
+       if (tls->ref_to_track) {
+               mgreg_t *p;
+
+               for (p = (mgreg_t*)stack_start; p < (mgreg_t*)stack_end; ++p)
+                       if (*p == (mgreg_t)tls->ref_to_track)
+                               printf ("REF AT %p.\n", p);
        }
+}
 
-       DEBUG (printf ("Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
+/*
+ * thread_mark_func:
+ *
+ *   This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
+ * call, and TRUE at the second. USER_DATA points to a TlsData
+ * structure filled up by thread_suspend_func. 
+ */
+static void
+thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise)
+{
+       TlsData *tls = user_data;
 
-       if (!precise) {
-               stats.scanned_stacks += stack_end - stack_start;
-               stats.scanned += scanned;
-               stats.scanned_precisely += scanned_precisely;
-               stats.scanned_conservatively += scanned_conservatively;
-               stats.scanned_registers += scanned_registers;
-       }
+       DEBUG (fprintf (logfile, "****************************************\n"));
+       DEBUG (fprintf (logfile, "*** %s stack marking for thread %p (%p-%p) ***\n", precise ? "Precise" : "Conservative", tls ? GUINT_TO_POINTER (tls->tid) : NULL, stack_start, stack_end));
+       DEBUG (fprintf (logfile, "****************************************\n"));
 
-       //mono_gc_conservatively_scan_area (stack_start, stack_end);
+       if (!precise)
+               conservative_pass (tls, stack_start, stack_end);
+       else
+               precise_pass (tls, stack_start, stack_end);
 }
 
 static void
@@ -578,6 +1124,12 @@ mini_gc_init_gc_map (MonoCompile *cfg)
        if (COMPILE_LLVM (cfg))
                return;
 
+       if (!mono_gc_is_moving ())
+               return;
+
+       if (!cfg->compile_aot && !mono_gc_precise_stack_mark_enabled ())
+               return;
+
 #if 1
        /* Debugging support */
        {
@@ -606,7 +1158,7 @@ mini_gc_init_gc_map (MonoCompile *cfg)
  * need to call this function for those slots.
  */
 void
-mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, StackSlotType type)
+mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
 {
        MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
 
@@ -628,7 +1180,7 @@ mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, StackSlotType
  * call this function for those slots.
  */
 void
-mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, StackSlotType type)
+mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
 {
        MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
        int slot = - (slot_offset / sizeof (mgreg_t));
@@ -642,19 +1194,96 @@ mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, StackSlotType
        gcfg->stack_slots_from_cfa = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_cfa, GUINT_TO_POINTER (((slot) << 16) | type));
 }
 
-static inline void
-set_slot (MonoCompileGC *gcfg, int pos, StackSlotType val)
+static inline int
+fp_offset_to_slot (MonoCompile *cfg, int offset)
 {
-       g_assert (pos >= 0 && pos < gcfg->nslots);
-       gcfg->slots [pos] = val;
+       MonoCompileGC *gcfg = cfg->gc_info;
+
+       return (offset - gcfg->min_offset) / sizeof (mgreg_t);
 }
 
 static inline int
-fp_offset_to_slot (MonoCompile *cfg, int offset)
+slot_to_fp_offset (MonoCompile *cfg, int slot)
 {
        MonoCompileGC *gcfg = cfg->gc_info;
 
-       return (offset - gcfg->min_offset) / sizeof (mgreg_t);
+       return (slot * sizeof (mgreg_t)) + gcfg->min_offset;
+}
+
+static inline void
+set_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
+{
+       g_assert (slot >= 0 && slot < gcfg->nslots);
+
+       if (type == SLOT_PIN) {
+               clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+               set_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+       } else if (type == SLOT_REF) {
+               set_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+               clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+       } else if (type == SLOT_NOREF) {
+               clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+               clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
+       }
+}
+
+static inline void
+set_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
+{
+       int cindex;
+
+       for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
+               set_slot (gcfg, slot, cindex, type);
+}
+
+static inline void
+set_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
+{
+       int cindex;
+
+       for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
+               int callsite_offset = gcfg->callsites [cindex]->pc_offset;
+               if (callsite_offset >= from && callsite_offset < to)
+                       set_slot (gcfg, slot, cindex, type);
+       }
+}
+
+static inline void
+set_reg_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
+{
+       g_assert (slot >= 0 && slot < gcfg->nregs);
+
+       if (type == SLOT_PIN) {
+               clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+               set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+       } else if (type == SLOT_REF) {
+               set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+               clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+       } else if (type == SLOT_NOREF) {
+               clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+               clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
+       }
+}
+
+static inline void
+set_reg_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
+{
+       int cindex;
+
+       for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
+               set_reg_slot (gcfg, slot, cindex, type);
+}
+
+static inline void
+set_reg_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
+{
+       int cindex;
+
+       for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
+               int callsite_offset = gcfg->callsites [cindex]->pc_offset;
+               if (callsite_offset >= from && callsite_offset < to)
+                       set_reg_slot (gcfg, slot, cindex, type);
+       }
 }
 
 static void
@@ -665,26 +1294,43 @@ process_spill_slots (MonoCompile *cfg)
        GSList *l;
        int i;
 
+       /* Mark all ref/pin spill slots as NOREF by default outside of their live range */
        for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
-               /*
-                * Extend the live interval for the GC tracked spill slots
-                * defined in this bblock.
-                */
                for (l = bb->spill_slot_defs; l; l = l->next) {
                        MonoInst *def = l->data;
                        int spill_slot = def->inst_c0;
-                       int offset = cfg->spill_info [MONO_REG_INT_REF][spill_slot].offset;
+                       int bank = def->inst_c1;
+                       int offset = cfg->spill_info [bank][spill_slot].offset;
                        int slot = fp_offset_to_slot (cfg, offset);
-                       MonoLiveInterval *interval;
 
-                       set_slot (gcfg, slot, SLOT_REF);
+                       if (bank == MONO_REG_INT_MP || bank == MONO_REG_INT_REF)
+                               set_slot_everywhere (gcfg, slot, SLOT_NOREF);
+               }
+       }
 
-                       interval = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
-                       mono_linterval_add_range (cfg, interval, def->backend.pc_offset, bb->native_offset + bb->native_length);
-                       gcfg->live_intervals [slot] = g_slist_prepend_mempool (cfg->mempool, gcfg->live_intervals [slot], interval);
+       for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+               for (l = bb->spill_slot_defs; l; l = l->next) {
+                       MonoInst *def = l->data;
+                       int spill_slot = def->inst_c0;
+                       int bank = def->inst_c1;
+                       int offset = cfg->spill_info [bank][spill_slot].offset;
+                       int slot = fp_offset_to_slot (cfg, offset);
+                       GCSlotType type;
+
+                       if (bank == MONO_REG_INT_MP)
+                               type = SLOT_PIN;
+                       else
+                               type = SLOT_REF;
+
+                       /*
+                        * Extend the live interval for the GC tracked spill slots
+                        * defined in this bblock.
+                        * FIXME: This is not needed.
+                        */
+                       set_slot_in_range (gcfg, slot, def->backend.pc_offset, bb->native_offset + bb->native_length, type);
 
                        if (cfg->verbose_level > 1)
-                               printf ("\tref spill slot at fp+0x%x (slot = %d)\n", offset, slot);
+                               printf ("\t%s spill slot at %s0x%x(fp) (slot = %d)\n", slot_type_to_string (type), offset >= 0 ? "" : "-", ABS (offset), slot);
                }
        }
 
@@ -698,10 +1344,10 @@ process_spill_slots (MonoCompile *cfg)
 
                slot = fp_offset_to_slot (cfg, offset);
 
-               set_slot (gcfg, slot, SLOT_NOREF);
+               set_slot_everywhere (gcfg, slot, SLOT_NOREF);
                /* FIXME: 32 bit */
                if (cfg->verbose_level > 1)
-                       printf ("\tfp spill slot at fp+0x%x (slot = %d)\n", offset, slot);
+                       printf ("\tfp spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
        }
 
        /* Set int spill slots to NOREF */
@@ -714,9 +1360,9 @@ process_spill_slots (MonoCompile *cfg)
 
                slot = fp_offset_to_slot (cfg, offset);
 
-               set_slot (gcfg, slot, SLOT_NOREF);
+               set_slot_everywhere (gcfg, slot, SLOT_NOREF);
                if (cfg->verbose_level > 1)
-                       printf ("\tint spill slot at fp+0x%x (slot = %d)\n", offset, slot);
+                       printf ("\tint spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
        }
 }
 
@@ -734,23 +1380,24 @@ process_other_slots (MonoCompile *cfg)
        /* Relative to the CFA */
        for (l = gcfg->stack_slots_from_cfa; l; l = l->next) {
                guint data = GPOINTER_TO_UINT (l->data);
-               int slot = data >> 16;
-               StackSlotType type = data & 0xff;
-               int fp_slot;
+               int cfa_slot = data >> 16;
+               GCSlotType type = data & 0xff;
+               int slot;
                
                /*
                 * Map the cfa relative slot to an fp relative slot.
-                * fp_slot_addr == cfa - <slot>*4/8
+                * slot_addr == cfa - <cfa_slot>*4/8
                 * fp + cfa_offset == cfa
-                * -> fp_slot_addr == fp + (cfa_offset - <slot>*4/8)
+                * -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
                 */
-               fp_slot = (cfg->cfa_offset / sizeof (mgreg_t)) - slot - (gcfg->min_offset / sizeof (mgreg_t));
+               slot = (cfg->cfa_offset / sizeof (mgreg_t)) - cfa_slot - (gcfg->min_offset / sizeof (mgreg_t));
 
-               set_slot (gcfg, fp_slot, type);
+               set_slot_everywhere (gcfg, slot, type);
 
                if (cfg->verbose_level > 1) {
+                       int fp_offset = slot_to_fp_offset (cfg, slot);
                        if (type == SLOT_NOREF)
-                               printf ("\tnoref slot at fp+0x%x (slot = %d) (cfa - 0x%x)\n", (int)(fp_slot * sizeof (mgreg_t)), fp_slot, (int)(slot * sizeof (mgreg_t)));
+                               printf ("\tnoref slot at %s0x%x(fp) (slot = %d) (cfa - 0x%x)\n", fp_offset >= 0 ? "" : "-", ABS (fp_offset), slot, (int)(cfa_slot * sizeof (mgreg_t)));
                }
        }
 
@@ -758,12 +1405,12 @@ process_other_slots (MonoCompile *cfg)
        for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
                gint data = GPOINTER_TO_INT (l->data);
                int offset = data >> 16;
-               StackSlotType type = data & 0xff;
+               GCSlotType type = data & 0xff;
                int slot;
                
                slot = fp_offset_to_slot (cfg, offset);
 
-               set_slot (gcfg, slot, type);
+               set_slot_everywhere (gcfg, slot, type);
 
                /* Liveness for these slots is handled by process_spill_slots () */
 
@@ -775,23 +1422,23 @@ process_other_slots (MonoCompile *cfg)
 }
 
 static void
-process_locals (MonoCompile *cfg)
+process_variables (MonoCompile *cfg)
 {
-       int i, locals_min_slot, locals_max_slot;
+       MonoCompileGC *gcfg = cfg->gc_info;
+       MonoMethodSignature *sig = mono_method_signature (cfg->method);
+       int i, locals_min_slot, locals_max_slot, cindex;
        MonoBasicBlock *bb;
        MonoInst *tmp;
        int *pc_offsets;
-       MonoCompileGC *gcfg = cfg->gc_info;
-       GSList **live_intervals = gcfg->live_intervals;
-       gboolean *starts_pinned = gcfg->starts_pinned;
        int locals_min_offset = gcfg->locals_min_offset;
        int locals_max_offset = gcfg->locals_max_offset;
 
        /* Slots for locals are NOREF by default */
        locals_min_slot = (locals_min_offset - gcfg->min_offset) / sizeof (mgreg_t);
        locals_max_slot = (locals_max_offset - gcfg->min_offset) / sizeof (mgreg_t);
-       for (i = locals_min_slot; i < locals_max_slot; ++i)
-               set_slot (gcfg, i, SLOT_NOREF);
+       for (i = locals_min_slot; i < locals_max_slot; ++i) {
+               set_slot_everywhere (gcfg, i, SLOT_NOREF);
+       }
 
        /*
         * Compute the offset where variables are initialized in the first bblock, if any.
@@ -806,49 +1453,65 @@ process_locals (MonoCompile *cfg)
                                g_assert (tmp->backend.pc_offset > 0);
                                pc_offsets [vreg] = tmp->backend.pc_offset;
                        }
-                       break;
                }
        }
 
-       for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
+       /*
+        * Stack slots holding arguments are initialized in the prolog.
+        * This means we can treat them alive for the whole method.
+        */
+       for (i = 0; i < cfg->num_varinfo; i++) {
                MonoInst *ins = cfg->varinfo [i];
                MonoType *t = ins->inst_vtype;
                MonoMethodVar *vmv;
                guint32 pos;
-               gboolean byref = t->byref;
-               MonoLiveInterval *li;
+               gboolean byref, is_this = FALSE;
+               gboolean is_arg = i < cfg->locals_start;
+
+               if (ins == cfg->ret)
+                       continue;
 
                vmv = MONO_VARINFO (cfg, i);
 
+               /* For some reason, 'this' is byref */
+               if (sig->hasthis && ins == cfg->args [0] && !cfg->method->klass->valuetype) {
+                       t = &cfg->method->klass->byval_arg;
+                       is_this = TRUE;
+               }
+
+               byref = t->byref;
+
                if (ins->opcode == OP_REGVAR) {
                        int hreg;
-                       StackSlotType slot_type;
+                       GCSlotType slot_type;
 
                        t = mini_type_get_underlying_type (NULL, t);
 
                        hreg = ins->dreg;
                        g_assert (hreg < MONO_MAX_IREGS);
 
-                       if (byref) {
-                               /* These have no live interval, be conservative */
+                       if (byref)
                                slot_type = SLOT_PIN;
-                               li = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
-                               mono_linterval_add_range (cfg, li, 0, cfg->code_size);
-                       } else if (!MONO_TYPE_IS_REFERENCE (t)) {
-                               // FIXME: These have no gc live interval
-                               continue;
+                       else
+                               slot_type = MONO_TYPE_IS_REFERENCE (t) ? SLOT_REF : SLOT_NOREF;
+
+                       if (slot_type == SLOT_PIN) {
+                               /* These have no live interval, be conservative */
+                               set_reg_slot_everywhere (gcfg, hreg, slot_type);
                        } else {
-                               slot_type = SLOT_REF;
-                               li = vmv->gc_interval;
+                               /*
+                                * Unlike variables allocated to the stack, we generate liveness info
+                                * for noref vars in registers in mono_spill_global_vars (), because
+                                * knowing that a register doesn't contain a ref allows us to mark its save
+                                * locations precisely.
+                                */
+                               for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
+                                       if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
+                                               set_reg_slot (gcfg, hreg, cindex, slot_type);
                        }
 
-                       gcfg->reg_types [hreg] = g_slist_prepend_mempool (cfg->mempool, gcfg->reg_types [hreg], GINT_TO_POINTER (slot_type));
-                       gcfg->reg_live_intervals [hreg] = g_slist_prepend_mempool (cfg->mempool, gcfg->reg_live_intervals [hreg], li);
-
                        if (cfg->verbose_level > 1) {
-                               printf ("\t%s reg %s: ", slot_type_to_string (slot_type), mono_arch_regname (hreg));
-                               mono_linterval_print (li);
-                               printf ("\n");
+                               printf ("\t%s %sreg %s(R%d)\n", slot_type_to_string (slot_type), is_arg ? "arg " : "", mono_arch_regname (hreg), vmv->vreg);
                        }
 
                        continue;
@@ -860,17 +1523,42 @@ process_locals (MonoCompile *cfg)
                if (ins->inst_offset % sizeof (mgreg_t) != 0)
                        continue;
 
+               if (is_arg && ins->inst_offset >= gcfg->max_offset)
+                       /* In parent frame */
+                       continue;
+
                pos = fp_offset_to_slot (cfg, ins->inst_offset);
 
+               if (is_arg && ins->flags & MONO_INST_IS_DEAD) {
+                       /* These do not get stored in the prolog */
+                       set_slot_everywhere (gcfg, pos, SLOT_NOREF);
+
+                       if (cfg->verbose_level > 1) {
+                               printf ("\tdead arg at fp%s0x%x (slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "+", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, pos, mono_type_full_name (ins->inst_vtype));
+                       }
+                       continue;
+               }
+
                if (MONO_TYPE_ISSTRUCT (t)) {
                        int numbits = 0, j;
                        gsize *bitmap = NULL;
                        gboolean pin = FALSE;
-                       MonoLiveInterval *interval = NULL;
                        int size;
+                       int size_in_slots;
+                       
+                       if (ins->backend.is_pinvoke)
+                               size = mono_class_native_size (ins->klass, NULL);
+                       else
+                               size = mono_class_value_size (ins->klass, NULL);
+                       size_in_slots = ALIGN_TO (size, sizeof (mgreg_t)) / sizeof (mgreg_t);
 
-                       if (!ins->klass->has_references)
+                       if (!ins->klass->has_references) {
+                               if (is_arg) {
+                                       for (j = 0; j < size_in_slots; ++j)
+                                               set_slot_everywhere (gcfg, pos + j, SLOT_NOREF);
+                               }
                                continue;
+                       }
 
                        if (ins->klass->generic_container || mono_class_is_open_constructed_type (t)) {
                                /* FIXME: Generic sharing */
@@ -879,9 +1567,7 @@ process_locals (MonoCompile *cfg)
                                mono_class_compute_gc_descriptor (ins->klass);
 
                                bitmap = mono_gc_get_bitmap_for_descr (ins->klass->gc_descr, &numbits);
-
                                if (!bitmap)
-                                       // FIXME:
                                        pin = TRUE;
 
                                /*
@@ -890,61 +1576,61 @@ process_locals (MonoCompile *cfg)
                                 * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
                                 * them during VZERO decomposition.
                                 */
-                               if (pc_offsets [vmv->vreg]) {
-                                       interval = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
-                                       mono_linterval_add_range (cfg, interval, pc_offsets [vmv->vreg], cfg->code_size);
-                               } else {
+                               if (!pc_offsets [vmv->vreg])
                                        pin = TRUE;
-                               }
                        }
 
                        if (ins->backend.is_pinvoke)
                                pin = TRUE;
 
-                       if (ins->backend.is_pinvoke)
-                               size = mono_class_native_size (ins->klass, NULL);
-                       else
-                               size = mono_class_value_size (ins->klass, NULL);
+                       if (cfg->verbose_level > 1)
+                               printf ("\tvtype R%d at fp+0x%x-0x%x: %s\n", vmv->vreg, (int)ins->inst_offset, (int)(ins->inst_offset + (size / sizeof (mgreg_t))), mono_type_full_name (ins->inst_vtype));
 
                        if (bitmap) {
-                               for (j = 0; j < numbits; ++j) {
-                                       if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
-                                               /* The descriptor is for the boxed object */
-                                               set_slot (gcfg, (pos + j - (sizeof (MonoObject) / sizeof (gpointer))), pin ? SLOT_PIN : SLOT_REF);
+                               for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
+                                       if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
+                                               for (j = 0; j < numbits; ++j) {
+                                                       if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
+                                                               /* The descriptor is for the boxed object */
+                                                               set_slot (gcfg, (pos + j - (sizeof (MonoObject) / sizeof (mgreg_t))), cindex, pin ? SLOT_PIN : SLOT_REF);
+                                                       }
+                                               }
                                        }
                                }
-                       } else if (pin) {
-                               for (j = 0; j < size / sizeof (mgreg_t); ++j)
-                                       set_slot (gcfg, pos + j, SLOT_PIN);
-                       }
 
-                       if (!pin) {
-                               for (j = 0; j < size / sizeof (mgreg_t); ++j) {
-                                       live_intervals [pos + j] = g_slist_prepend_mempool (cfg->mempool, live_intervals [pos + j], interval);
-                                       starts_pinned [pos + j] = TRUE;
+                               if (cfg->verbose_level > 1) {
+                                       for (j = 0; j < numbits; ++j) {
+                                               if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD)))
+                                                       printf ("\t\t%s slot at 0x%x(fp) (slot = %d)\n", pin ? "pin" : "ref", (int)(ins->inst_offset + (j * sizeof (mgreg_t))), (int)(pos + j - (sizeof (MonoObject) / sizeof (mgreg_t))));
+                                       }
+                               }
+                       } else {
+                               if (cfg->verbose_level > 1)
+                                       printf ("\t\tpinned\n");
+                               for (j = 0; j < size_in_slots; ++j) {
+                                       set_slot_everywhere (gcfg, pos + j, SLOT_PIN);
                                }
                        }
 
                        g_free (bitmap);
 
-                       if (cfg->verbose_level > 1) {
-                               printf ("\tvtype R%d at fp+0x%x-0x%x: %s ", vmv->vreg, (int)ins->inst_offset, (int)(ins->inst_offset + (size / sizeof (mgreg_t))), mono_type_full_name (ins->inst_vtype));
-                               if (interval)
-                                       mono_linterval_print (interval);
-                               else
-                                       printf ("(pinned)");
-                               printf ("\n");
-                       }
-
                        continue;
                }
 
-               if (ins->inst_offset < gcfg->min_offset || ins->inst_offset >= gcfg->max_offset)
+               if (!is_arg && (ins->inst_offset < gcfg->min_offset || ins->inst_offset >= gcfg->max_offset))
                        /* Vret addr etc. */
                        continue;
 
                if (t->byref) {
-                       set_slot (gcfg, pos, SLOT_PIN);
+                       if (is_arg) {
+                               set_slot_everywhere (gcfg, pos, SLOT_PIN);
+                       } else {
+                               for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
+                                       if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
+                                               set_slot (gcfg, pos, cindex, SLOT_PIN);
+                       }
+                       if (cfg->verbose_level > 1)
+                               printf ("\tbyref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
                        continue;
                }
 
@@ -958,172 +1644,207 @@ process_locals (MonoCompile *cfg)
                         * could hold GC refs since the vregs they were created from might not been
                         * marked as holding a GC ref. So be conservative.
                         */
-                       set_slot (gcfg, pos, SLOT_PIN);
+                       set_slot_everywhere (gcfg, pos, SLOT_PIN);
                        continue;
                }
 #endif
 
                t = mini_type_get_underlying_type (NULL, t);
 
-               if (MONO_TYPE_IS_REFERENCE (ins->inst_vtype)) {
-                       if (vmv && !vmv->gc_interval) {
-                               set_slot (gcfg, pos, SLOT_PIN);
-                               continue;
-                       }
+               if (!MONO_TYPE_IS_REFERENCE (t)) {
+                       set_slot_everywhere (gcfg, pos, SLOT_NOREF);
+                       if (cfg->verbose_level > 1)
+                               printf ("\tnoref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
+                       continue;
+               }
 
-                       if (ins->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT)) {
-                               /*
-                                * For volatile variables, treat them alive from the point they are
-                                * initialized in the first bblock until the end of the method.
-                                */
-                               if (pc_offsets [vmv->vreg]) {
-                                       vmv->gc_interval = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
-                                       mono_linterval_add_range (cfg, vmv->gc_interval, pc_offsets [vmv->vreg], cfg->code_size);
-                                       starts_pinned [pos] = TRUE;
-                               } else {
-                                       set_slot (gcfg, pos, SLOT_PIN);
-                                       continue;
-                               }
+               /* 'this' is marked INDIRECT for gshared methods */
+               if (ins->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT) && !is_this) {
+                       /*
+                        * For volatile variables, treat them alive from the point they are
+                        * initialized in the first bblock until the end of the method.
+                        */
+                       if (is_arg) {
+                               set_slot_everywhere (gcfg, pos, SLOT_REF);
+                       } else if (pc_offsets [vmv->vreg]) {
+                               set_slot_in_range (gcfg, pos, 0, pc_offsets [vmv->vreg], SLOT_PIN);
+                               set_slot_in_range (gcfg, pos, pc_offsets [vmv->vreg], cfg->code_size, SLOT_REF);
+                       } else {
+                               set_slot_everywhere (gcfg, pos, SLOT_PIN);
                        }
+                       if (cfg->verbose_level > 1)
+                               printf ("\tvolatile ref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
+                       continue;
+               }
 
-                       set_slot (gcfg, pos, SLOT_REF);
-
-                       live_intervals [pos] = g_slist_prepend_mempool (cfg->mempool, live_intervals [pos], vmv->gc_interval);
+               if (is_arg) {
+                       /* Live for the whole method */
+                       set_slot_everywhere (gcfg, pos, SLOT_REF);
+               } else {
+                       for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
+                               if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
+                                       set_slot (gcfg, pos, cindex, SLOT_REF);
+               }
 
-                       if (cfg->verbose_level > 1) {
-                               printf ("\tref at %s0x%x(fp) (slot=%d): %s ", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, pos, mono_type_full_name (ins->inst_vtype));
-                               mono_linterval_print (vmv->gc_interval);
-                               printf ("\n");
-                       }
+               if (cfg->verbose_level > 1) {
+                       printf ("\tref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, vmv->vreg, pos, mono_type_full_name (ins->inst_vtype));
                }
        }
 
        g_free (pc_offsets);
 }
 
-// FIXME: Merge this with process_locals ()
-static void
-process_arguments (MonoCompile *cfg)
+static int
+sp_offset_to_fp_offset (MonoCompile *cfg, int sp_offset)
 {
-       int i, j;
-       MonoCompileGC *gcfg = cfg->gc_info;
-       MonoMethodSignature *sig = mono_method_signature (cfg->method);
-       MonoLiveInterval *li;
-
-       /*
-        * Stack slots holding arguments are initialized in the prolog.
-        * This means we can treat them alive for the whole method.
+       /* 
+        * Convert a sp relative offset to a slot index. This is
+        * platform specific.
         */
-       for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
-               MonoInst *ins = cfg->args [i];
-               MonoType *t = ins->inst_vtype;
-               int pos;
-               gboolean byref = t->byref;
-
-               /* For some reason, 'this' is byref */
-               if (sig->hasthis && i == 0 && !cfg->method->klass->valuetype)
-                       t = &cfg->method->klass->byval_arg;
-
-               if (ins->opcode == OP_REGVAR) {
-                       int hreg;
-                       StackSlotType slot_type;
-
-                       t = mini_type_get_underlying_type (NULL, t);
-
-                       if (byref)
-                               slot_type = SLOT_PIN;
-                       else
-                               slot_type = MONO_TYPE_IS_REFERENCE (t) ? SLOT_REF : SLOT_NOREF;
-
-                       hreg = ins->dreg;
-                       g_assert (hreg < MONO_MAX_IREGS);
-
-                       if (gcfg->reg_live_intervals [hreg]) {
-                               /* 
-                                * FIXME: This argument shares a hreg with a local, we can't add the whole
-                                * method as a live interval, since it would overlap with the locals
-                                * live interval.
-                                */
-                               continue;
-                       }
-
-                       gcfg->reg_types [hreg] = g_slist_prepend_mempool (cfg->mempool, gcfg->reg_types [hreg], GINT_TO_POINTER (slot_type));
-
-                       /* Live for the whole method */
-                       li = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
-                       mono_linterval_add_range (cfg, li, 0, cfg->code_size);
-                       gcfg->reg_live_intervals [hreg] = g_slist_prepend_mempool (cfg->mempool, gcfg->reg_live_intervals [hreg], li);
+#ifdef TARGET_AMD64
+       /* fp = sp + offset */
+       g_assert (cfg->frame_reg == AMD64_RBP);
+       return (- cfg->arch.sp_fp_offset + sp_offset);
+#else
+       NOT_IMPLEMENTED;
+       return -1;
+#endif
+}
 
-                       if (cfg->verbose_level > 1) {
-                               printf ("\t%s arg reg %s: ", slot_type_to_string (slot_type), mono_arch_regname (hreg));
-                               mono_linterval_print (li);
-                               printf ("\n");
+static GCSlotType
+type_to_gc_slot_type (MonoType *t)
+{
+       if (t->byref)
+               return SLOT_PIN;
+       t = mini_type_get_underlying_type (NULL, t);
+       if (MONO_TYPE_IS_REFERENCE (t))
+               return SLOT_REF;
+       else {
+               if (MONO_TYPE_ISSTRUCT (t)) {
+                       MonoClass *klass = mono_class_from_mono_type (t);
+                       if (!klass->has_references) {
+                               return SLOT_NOREF;
+                       } else {
+                               // FIXME:
+                               return SLOT_PIN;
                        }
-
-                       continue;
                }
+               return SLOT_NOREF;
+       }
+}
 
-               if (ins->opcode != OP_REGOFFSET)
-                       continue;
+static void
+process_param_area_slots (MonoCompile *cfg)
+{
+       MonoCompileGC *gcfg = cfg->gc_info;
+       int i;
+       gboolean *is_param;
 
-               g_assert (ins->inst_offset % sizeof (mgreg_t) == 0);
+       /*
+        * These slots are used for passing parameters during calls. They are sp relative, not
+        * fp relative, so they are harder to handle.
+        */
+       if (cfg->flags & MONO_CFG_HAS_ALLOCA)
+               /* The distance between fp and sp is not constant */
+               return;
 
-               pos = (ins->inst_offset - gcfg->min_offset) / sizeof (mgreg_t);
+       is_param = mono_mempool_alloc0 (cfg->mempool, gcfg->nslots * sizeof (gboolean));
 
-               if (ins->inst_offset >= gcfg->max_offset)
-                       /* In parent frame */
-                       continue;
+       for (i = 0; i < gcfg->ncallsites; ++i) {
+               GCCallSite *callsite = gcfg->callsites [i];
+               GSList *l;
 
-               if (ins->flags & MONO_INST_IS_DEAD) {
-                       /* These do not get stored in the prolog */
-                       set_slot (gcfg, pos, SLOT_NOREF);
+               for (l = callsite->param_slots; l; l = l->next) {
+                       MonoInst *def = l->data;
+                       int sp_offset = def->inst_offset;
+                       int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
+                       int slot = fp_offset_to_slot (cfg, fp_offset);
 
-                       if (cfg->verbose_level > 1) {
-                               printf ("\tdead arg at fp%s0x%x (slot=%d): %s\n", ins->inst_offset < 0 ? "-" : "+", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, pos, mono_type_full_name (ins->inst_vtype));
-                       }
-                       continue;
+                       g_assert (slot >= 0 && slot < gcfg->nslots);
+                       is_param [slot] = TRUE;
                }
+       }
 
-               if (MONO_TYPE_ISSTRUCT (t)) {
-                       int size;
-
-                       if (ins->backend.is_pinvoke)
-                               size = mono_class_native_size (ins->klass, NULL);
-                       else
-                               size = mono_class_value_size (ins->klass, NULL);
+       /* All param area slots are noref by default */
+       for (i = 0; i < gcfg->nslots; ++i) {
+               if (is_param [i])
+                       set_slot_everywhere (gcfg, i, SLOT_NOREF);
+       }
 
-                       if (!ins->klass->has_references) {
-                               for (j = 0; j < size / sizeof (mgreg_t); ++j)
-                                       set_slot (gcfg, pos + j, SLOT_NOREF);
-                               continue;
-                       }
+       for (i = 0; i < gcfg->ncallsites; ++i) {
+               GCCallSite *callsite = gcfg->callsites [i];
+               GSList *l;
 
-                       // FIXME:
-                       continue;
+               for (l = callsite->param_slots; l; l = l->next) {
+                       MonoInst *def = l->data;
+                       MonoType *t = def->inst_vtype;
+                       int sp_offset = def->inst_offset;
+                       int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
+                       int slot = fp_offset_to_slot (cfg, fp_offset);
+                       GCSlotType type = type_to_gc_slot_type (t);
+
+                       /* The slot is live between the def instruction and the call */
+                       set_slot_in_range (gcfg, slot, def->backend.pc_offset, callsite->pc_offset + 1, type);
+                       if (cfg->verbose_level > 1)
+                               printf ("\t%s param area slot at %s0x%x(fp)=0x%x(sp) (slot = %d) [0x%x-0x%x]\n", slot_type_to_string (type), fp_offset >= 0 ? "+" : "-", ABS (fp_offset), sp_offset, slot, def->backend.pc_offset, callsite->pc_offset + 1);
                }
+       }
+}
 
-               if (t->byref)
-                       continue;
-
-               t = mini_type_get_underlying_type (NULL, t);
-
-               if (!MONO_TYPE_IS_REFERENCE (t)) {
-                       set_slot (gcfg, pos, SLOT_NOREF);
-                       continue;
-               }
+static void
+process_finally_clauses (MonoCompile *cfg)
+{
+       MonoCompileGC *gcfg = cfg->gc_info;
+       GCCallSite **callsites;
+       int ncallsites;
+       gboolean has_finally;
+       int i, j, nslots, nregs;
 
-               set_slot (gcfg, pos, SLOT_REF);
+       ncallsites = gcfg->ncallsites;
+       nslots = gcfg->nslots;
+       nregs = gcfg->nregs;
+       callsites = gcfg->callsites;
 
-               /* Live for the whole method */
-               li = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
-               mono_linterval_add_range (cfg, li, 0, cfg->code_size);
+       /*
+        * The calls to the finally clauses don't show up in the cfg. See
+        * test_0_liveness_8 ().
+        * Variables accessed inside the finally clause are already marked VOLATILE by
+        * mono_liveness_handle_exception_clauses (). Variables not accessed inside the finally clause have
+        * correct liveness outside the finally clause. So mark them PIN inside the finally clauses.
+        */
+       has_finally = FALSE;
+       for (i = 0; i < cfg->header->num_clauses; ++i) {
+               MonoExceptionClause *clause = &cfg->header->clauses [i];
 
-               gcfg->live_intervals [pos] = g_slist_prepend_mempool (cfg->mempool, gcfg->live_intervals [pos], li);
+               if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
+                       has_finally = TRUE;
+               }
+       }
+       if (has_finally) {
+               if (cfg->verbose_level > 1)
+                       printf ("\tMethod has finally clauses, pessimizing live ranges.\n");
+               for (j = 0; j < ncallsites; ++j) {
+                       MonoBasicBlock *bb = callsites [j]->bb;
+                       MonoExceptionClause *clause;
+                       gboolean is_in_finally = FALSE;
+
+                       for (i = 0; i < cfg->header->num_clauses; ++i) {
+                               clause = &cfg->header->clauses [i];
+                          
+                               if (MONO_OFFSET_IN_HANDLER (clause, bb->real_offset)) {
+                                       if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
+                                               is_in_finally = TRUE;
+                                               break;
+                                       }
+                               }
+                       }
 
-               if (cfg->verbose_level > 1) {
-                       printf ("\tref at %s0x%x(fp) (slot=%d): %s ", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)ins->inst_offset : (int)ins->inst_offset, pos, mono_type_full_name (ins->inst_vtype));
-                       mono_linterval_print (li);
-                       printf ("\n");
+                       if (is_in_finally) {
+                               for (i = 0; i < nslots; ++i)
+                                       set_slot (gcfg, i, j, SLOT_PIN);
+                               for (i = 0; i < nregs; ++i)
+                                       set_reg_slot (gcfg, i, j, SLOT_PIN);
+                       }
                }
        }
 }
@@ -1135,11 +1856,12 @@ compute_frame_size (MonoCompile *cfg)
        int min_offset, max_offset;
        MonoCompileGC *gcfg = cfg->gc_info;
        MonoMethodSignature *sig = mono_method_signature (cfg->method);
+       GSList *l;
 
        /* Compute min/max offsets from the fp */
 
        /* Locals */
-#ifdef TARGET_AMD64
+#if defined(TARGET_AMD64) || defined(TARGET_X86)
        locals_min_offset = ALIGN_TO (cfg->locals_min_stack_offset, sizeof (mgreg_t));
        locals_max_offset = cfg->locals_max_stack_offset;
 #else
@@ -1170,68 +1892,58 @@ compute_frame_size (MonoCompile *cfg)
        min_offset = MIN (min_offset, cfa_min_offset);
        max_offset = MAX (max_offset, cfa_max_offset);
 
+       /* Fp relative slots */
+       for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
+               gint data = GPOINTER_TO_INT (l->data);
+               int offset = data >> 16;
+
+               min_offset = MIN (min_offset, offset);
+       }
+
        /* Spill slots */
        if (!(cfg->flags & MONO_CFG_HAS_SPILLUP)) {
                int stack_offset = ALIGN_TO (cfg->stack_offset, sizeof (mgreg_t));
                min_offset = MIN (min_offset, (-stack_offset));
        }
 
+       /* Param area slots */
+#ifdef TARGET_AMD64
+       min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
+#endif
+
        gcfg->min_offset = min_offset;
        gcfg->max_offset = max_offset;
        gcfg->locals_min_offset = locals_min_offset;
        gcfg->locals_max_offset = locals_max_offset;
 }
 
-void
-mini_gc_create_gc_map (MonoCompile *cfg)
+static void
+init_gcfg (MonoCompile *cfg)
 {
-       GCMap *map;
-       int i, nregs, nslots, alloc_size;
-       int ntypes [16];
-       StackSlotType *slots = NULL;
-       GSList **live_intervals;
-       int bitmap_width, bitmap_size, reg_bitmap_width, reg_bitmap_size;
-       gboolean *starts_pinned;
-       gboolean has_ref_slots, has_pin_slots, has_ref_regs, has_pin_regs;
+       int i, nregs, nslots;
        MonoCompileGC *gcfg = cfg->gc_info;
-
-       if (!cfg->compute_gc_maps)
-               return;
+       GCCallSite **callsites;
+       int ncallsites;
+       MonoBasicBlock *bb;
+       GSList *l;
 
        /*
-        * Since we currently don't use GC safe points, we need to create GC maps which
-        * are precise at every instruction within a method. The live ranges calculated by
-        * the liveness pass are not usable for this, since they contain abstract positions, not
-        * pc offsets. The live ranges calculated by mono_spill_global_vars () are not usable
-        * either, since they can't model holes. Instead of these, we implement our own
-        * liveness analysis which is precise, and works with PC offsets. It calculates live
-        * intervals, which are unions of live ranges.
-        * FIXME:
-        * - it would simplify things if we extended live ranges to the end of basic blocks
-        * instead of computing them precisely.
-        * - maybe mark loads+stores as needing GC tracking, instead of using DEF/USE
-        * instructions ?
-        * - group ref and no-ref slots together on the stack, to speed up marking, and
-        * to make the gc bitmaps better compressable.
-        * During marking, all frames except the top frame are at a call site, and we mark the
-        * top frame conservatively. This means that stack slots initialized in the prolog can
-        * be assumed to be valid/live through the whole method.
+        * Collect callsites
         */
+       ncallsites = 0;
+       for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+               ncallsites += g_slist_length (bb->gc_callsites);
+       }
+       callsites = mono_mempool_alloc0 (cfg->mempool, ncallsites * sizeof (GCCallSite*));
+       i = 0;
+       for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+               for (l = bb->gc_callsites; l; l = l->next)
+                       callsites [i++] = l->data;
+       }
 
-       if (!(cfg->comp_done & MONO_COMP_LIVENESS))
-               /* Without liveness info, the live ranges are not precise enough */
-               return;
-
-       if (cfg->header->num_clauses)
-               /*
-                * The calls to the finally clauses don't show up in the cfg. See
-                * test_0_liveness_8 ().
-                */
-               return;
-
-       mono_analyze_liveness_gc (cfg);
-       
-       compute_frame_size (cfg);
+       /* The callsites should already be ordered by pc offset */
+       for (i = 1; i < ncallsites; ++i)
+               g_assert (callsites [i - 1]->pc_offset < callsites [i]->pc_offset);
 
        /*
         * The stack frame looks like this:
@@ -1249,196 +1961,350 @@ mini_gc_create_gc_map (MonoCompile *cfg)
                printf ("GC Map for %s: 0x%x-0x%x\n", mono_method_full_name (cfg->method, TRUE), gcfg->min_offset, gcfg->max_offset);
 
        nslots = (gcfg->max_offset - gcfg->min_offset) / sizeof (mgreg_t);
-       nregs = MONO_MAX_IREGS;
-       /* slot [i] == type of slot at fp - <min_offset> + i*4/8 */
-       slots = g_new0 (StackSlotType, nslots);
-       live_intervals = g_new0 (GSList*, nslots);
-       starts_pinned = g_new0 (gboolean, nslots);
+       nregs = NREGS;
 
-       gcfg->slots = slots;
        gcfg->nslots = nslots;
-       gcfg->live_intervals = live_intervals;
-       gcfg->starts_pinned = starts_pinned;
-       gcfg->reg_types = mono_mempool_alloc0 (cfg->mempool, sizeof (GSList*) * nregs);
-       gcfg->reg_live_intervals = mono_mempool_alloc0 (cfg->mempool, sizeof (GSList*) * nregs);
+       gcfg->nregs = nregs;
+       gcfg->callsites = callsites;
+       gcfg->ncallsites = ncallsites;
+       gcfg->stack_bitmap_width = ALIGN_TO (nslots, 8) / 8;
+       gcfg->reg_bitmap_width = ALIGN_TO (nregs, 8) / 8;
+       gcfg->stack_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * ncallsites);
+       gcfg->stack_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * ncallsites);
+       gcfg->reg_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
+       gcfg->reg_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
 
        /* All slots start out as PIN */
-       for (i = 0; i < nslots; ++i)
-               set_slot (gcfg, i, SLOT_PIN);
+       memset (gcfg->stack_pin_bitmap, 0xff, gcfg->stack_bitmap_width * ncallsites);
+       for (i = 0; i < nregs; ++i) {
+               /*
+                * By default, registers are NOREF.
+                * It is possible for a callee to save them before being defined in this method,
+                * but the saved value is dead too, so it doesn't need to be marked.
+                */
+               if ((cfg->used_int_regs & (1 << i)))
+                       set_reg_slot_everywhere (gcfg, i, SLOT_NOREF);
+       }
+}
 
-       process_spill_slots (cfg);
-       process_other_slots (cfg);
-       process_locals (cfg);
-       process_arguments (cfg);
+static void
+create_map (MonoCompile *cfg)
+{
+       GCMap *map;
+       int i, j, nregs, nslots, nref_regs, npin_regs, alloc_size, bitmaps_size, bitmaps_offset;
+       int ntypes [16];
+       int stack_bitmap_width, stack_bitmap_size, reg_ref_bitmap_width, reg_ref_bitmap_size;
+       int reg_pin_bitmap_width, reg_pin_bitmap_size, bindex;
+       int start, end;
+       gboolean has_ref_slots, has_pin_slots, has_ref_regs, has_pin_regs;
+       MonoCompileGC *gcfg = cfg->gc_info;
+       GCCallSite **callsites;
+       int ncallsites;
+       guint8 *bitmap, *bitmaps;
+       guint32 reg_ref_mask, reg_pin_mask;
 
-       /* Create the GC Map */
+       ncallsites = gcfg->ncallsites;
+       nslots = gcfg->nslots;
+       nregs = gcfg->nregs;
+       callsites = gcfg->callsites;
 
+       /* 
+        * Compute the real size of the bitmap i.e. ignore NOREF columns at the beginning and at
+        * the end. Also, compute whenever the map needs ref/pin bitmaps, and collect stats.
+        */
        has_ref_slots = FALSE;
        has_pin_slots = FALSE;
+       start = -1;
+       end = -1;
        memset (ntypes, 0, sizeof (ntypes));
        for (i = 0; i < nslots; ++i) {
-               ntypes [slots [i]] ++;
-               if (slots [i] == SLOT_REF)
+               gboolean has_ref = FALSE;
+               gboolean has_pin = FALSE;
+
+               for (j = 0; j < ncallsites; ++j) {
+                       if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, j, i))
+                               has_pin = TRUE;
+                       if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, j, i))
+                               has_ref = TRUE;
+               }
+
+               if (has_ref)
                        has_ref_slots = TRUE;
-               if (slots [i] == SLOT_PIN || (slots [i] == SLOT_REF && starts_pinned [i]))
+               if (has_pin)
                        has_pin_slots = TRUE;
+
+               if (has_ref)
+                       ntypes [SLOT_REF] ++;
+               else if (has_pin)
+                       ntypes [SLOT_PIN] ++;
+               else
+                       ntypes [SLOT_NOREF] ++;
+
+               if (has_ref || has_pin) {
+                       if (start == -1)
+                               start = i;
+                       end = i + 1;
+               }
+       }
+       if (start == -1) {
+               start = end = nslots;
+       } else {
+               g_assert (start != -1);
+               g_assert (start < end);
        }
+
        has_ref_regs = FALSE;
-       has_pin_regs = TRUE;
+       has_pin_regs = FALSE;
+       reg_ref_mask = 0;
+       reg_pin_mask = 0;
+       nref_regs = 0;
+       npin_regs = 0;
        for (i = 0; i < nregs; ++i) {
-               GSList *l;
-               for (l = gcfg->reg_types [i]; l; l = l->next)
-                       if (GPOINTER_TO_UINT (l->data) == SLOT_REF)
-                               has_ref_regs = TRUE;
+               gboolean has_ref = FALSE;
+               gboolean has_pin = FALSE;
+
+               if (!(cfg->used_int_regs & (1 << i)))
+                       continue;
+
+               for (j = 0; j < ncallsites; ++j) {
+                       if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i)) {
+                               has_ref = TRUE;
+                               break;
+                       }
+               }
+               for (j = 0; j < ncallsites; ++j) {
+                       if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i)) {
+                               has_pin = TRUE;
+                               break;
+                       }
+               }
+
+               if (has_ref) {
+                       reg_ref_mask |= (1 << i);
+                       has_ref_regs = TRUE;
+                       nref_regs ++;
+               }
+               if (has_pin) {
+                       reg_pin_mask |= (1 << i);
+                       has_pin_regs = TRUE;
+                       npin_regs ++;
+               }
        }
 
        if (cfg->verbose_level > 1)
-               printf ("Slots: %d Refs: %d NoRefs: %d Pin: %d\n", nslots, ntypes [SLOT_REF], ntypes [SLOT_NOREF], ntypes [SLOT_PIN]);
+               printf ("Slots: %d Start: %d End: %d Refs: %d NoRefs: %d Pin: %d Callsites: %d\n", nslots, start, end, ntypes [SLOT_REF], ntypes [SLOT_NOREF], ntypes [SLOT_PIN], ncallsites);
+
+       /* Create the GC Map */
 
-       bitmap_width = ALIGN_TO (nslots, 8) / 8;
-       bitmap_size = bitmap_width * cfg->code_len;
-       reg_bitmap_width = ALIGN_TO (nregs, 8) / 8;
-       reg_bitmap_size = reg_bitmap_width * cfg->code_len;
-       alloc_size = sizeof (GCMap);
-       map = mono_domain_alloc0 (cfg->domain, alloc_size);
-       gc_maps_size += alloc_size;
+       stack_bitmap_width = ALIGN_TO (end - start, 8) / 8;
+       stack_bitmap_size = stack_bitmap_width * ncallsites;
+       reg_ref_bitmap_width = ALIGN_TO (nref_regs, 8) / 8;
+       reg_ref_bitmap_size = reg_ref_bitmap_width * ncallsites;
+       reg_pin_bitmap_width = ALIGN_TO (npin_regs, 8) / 8;
+       reg_pin_bitmap_size = reg_pin_bitmap_width * ncallsites;
+       bitmaps_size = (has_ref_slots ? stack_bitmap_size : 0) + (has_pin_slots ? stack_bitmap_size : 0) + (has_ref_regs ? reg_ref_bitmap_size : 0) + (has_pin_regs ? reg_pin_bitmap_size : 0);
+       
+       map = mono_mempool_alloc0 (cfg->mempool, sizeof (GCMap));
 
        map->frame_reg = cfg->frame_reg;
-       map->frame_offset = gcfg->min_offset;
-       map->nslots = nslots;
-       map->nregs = nregs;
-       map->bitmap_width = bitmap_width;
-       map->reg_bitmap_width = reg_bitmap_width;
+       map->start_offset = gcfg->min_offset;
+       map->end_offset = gcfg->min_offset + (nslots * sizeof (mgreg_t));
+       map->map_offset = start * sizeof (mgreg_t);
+       map->nslots = end - start;
        map->has_ref_slots = has_ref_slots;
        map->has_pin_slots = has_pin_slots;
        map->has_ref_regs = has_ref_regs;
        map->has_pin_regs = has_pin_regs;
-       map->ref_slots = mono_domain_alloc0 (cfg->domain, bitmap_width);
-       gc_maps_size += bitmap_width;
+       g_assert (nregs < 32);
+       map->used_int_regs = cfg->used_int_regs;
+       map->reg_ref_mask = reg_ref_mask;
+       map->reg_pin_mask = reg_pin_mask;
+       map->nref_regs = nref_regs;
+       map->npin_regs = npin_regs;
+
+       bitmaps = mono_mempool_alloc0 (cfg->mempool, bitmaps_size);
 
+       bitmaps_offset = 0;
        if (has_ref_slots) {
-               map->ref_bitmap = mono_domain_alloc0 (cfg->domain, bitmap_size);
-               gc_maps_size += bitmap_size;
+               map->stack_ref_bitmap_offset = bitmaps_offset;
+               bitmaps_offset += stack_bitmap_size;
+
+               bitmap = &bitmaps [map->stack_ref_bitmap_offset];
+               for (i = 0; i < nslots; ++i) {
+                       for (j = 0; j < ncallsites; ++j) {
+                               if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, j, i))
+                                       set_bit (bitmap, stack_bitmap_width, j, i - start);
+                       }
+               }
        }
        if (has_pin_slots) {
-               map->pin_bitmap = mono_domain_alloc0 (cfg->domain, bitmap_size);
-               gc_maps_size += bitmap_size;
+               map->stack_pin_bitmap_offset = bitmaps_offset;
+               bitmaps_offset += stack_bitmap_size;
+
+               bitmap = &bitmaps [map->stack_pin_bitmap_offset];
+               for (i = 0; i < nslots; ++i) {
+                       for (j = 0; j < ncallsites; ++j) {
+                               if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, j, i))
+                                       set_bit (bitmap, stack_bitmap_width, j, i - start);
+                       }
+               }
        }
        if (has_ref_regs) {
-               map->reg_ref_bitmap = mono_domain_alloc0 (cfg->domain, reg_bitmap_size);
-               gc_maps_size += reg_bitmap_size;
+               map->reg_ref_bitmap_offset = bitmaps_offset;
+               bitmaps_offset += reg_ref_bitmap_size;
+
+               bitmap = &bitmaps [map->reg_ref_bitmap_offset];
+               bindex = 0;
+               for (i = 0; i < nregs; ++i) {
+                       if (reg_ref_mask & (1 << i)) {
+                               for (j = 0; j < ncallsites; ++j) {
+                                       if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i))
+                                               set_bit (bitmap, reg_ref_bitmap_width, j, bindex);
+                               }
+                               bindex ++;
+                       }
+               }
        }
        if (has_pin_regs) {
-               map->reg_pin_bitmap = mono_domain_alloc0 (cfg->domain, reg_bitmap_size);
-               gc_maps_size += reg_bitmap_size;
+               map->reg_pin_bitmap_offset = bitmaps_offset;
+               bitmaps_offset += reg_pin_bitmap_size;
+
+               bitmap = &bitmaps [map->reg_pin_bitmap_offset];
+               bindex = 0;
+               for (i = 0; i < nregs; ++i) {
+                       if (reg_pin_mask & (1 << i)) {
+                               for (j = 0; j < ncallsites; ++j) {
+                                       if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i))
+                                               set_bit (bitmap, reg_pin_bitmap_width, j, bindex);
+                               }
+                               bindex ++;
+                       }
+               }
        }
 
-       /* Create liveness bitmaps */
+       /* Call sites */
+       map->ncallsites = ncallsites;
+       if (cfg->code_len < 256)
+               map->callsite_entry_size = 1;
+       else if (cfg->code_len < 65536)
+               map->callsite_entry_size = 2;
+       else
+               map->callsite_entry_size = 4;
 
-       /* Stack slots */
-       for (i = 0; i < nslots; ++i) {
-               int pc_offset;
+       /* Encode the GC Map */
+       {
+               guint8 buf [256];
+               guint8 *endbuf;
+               GCEncodedMap *emap;
+               int encoded_size;
+               guint8 *p;
+
+               encode_gc_map (map, buf, &endbuf);
+               g_assert (endbuf - buf < 256);
+
+               encoded_size = endbuf - buf;
+               alloc_size = sizeof (GCEncodedMap) + ALIGN_TO (encoded_size, map->callsite_entry_size) + (map->callsite_entry_size * map->ncallsites) + bitmaps_size;
+
+               emap = mono_domain_alloc0 (cfg->domain, alloc_size);
+               //emap->ref_slots = map->ref_slots;
+
+               /* Encoded fixed fields */
+               p = &emap->encoded [0];
+               //emap->encoded_size = encoded_size;
+               memcpy (p, buf, encoded_size);
+               p += encoded_size;
+
+               /* Callsite table */
+               p = (guint8*)ALIGN_TO ((mgreg_t)p, map->callsite_entry_size);
+               if (map->callsite_entry_size == 1) {
+                       guint8 *offsets = p;
+                       for (i = 0; i < ncallsites; ++i)
+                               offsets [i] = callsites [i]->pc_offset;
+                       stats.gc_callsites8_size += ncallsites * sizeof (guint8);
+               } else if (map->callsite_entry_size == 2) {
+                       guint16 *offsets = (guint16*)p;
+                       for (i = 0; i < ncallsites; ++i)
+                               offsets [i] = callsites [i]->pc_offset;
+                       stats.gc_callsites16_size += ncallsites * sizeof (guint16);
+               } else {
+                       guint32 *offsets = (guint32*)p;
+                       for (i = 0; i < ncallsites; ++i)
+                               offsets [i] = callsites [i]->pc_offset;
+                       stats.gc_callsites32_size += ncallsites * sizeof (guint32);
+               }
+               p += ncallsites * map->callsite_entry_size;
 
-               if (slots [i] == SLOT_REF) {
-                       MonoLiveInterval *iv;
-                       GSList *l;
-                       MonoLiveRange2 *r;
+               /* Bitmaps */
+               memcpy (p, bitmaps, bitmaps_size);
+               p += bitmaps_size;
 
-                       map->ref_slots [i / 8] |= (1 << i);
+               g_assert ((guint8*)p - (guint8*)emap <= alloc_size);
 
-                       if (starts_pinned [i]) {
-                               /* The slots start out as pinned until they are first defined */
-                               g_assert (live_intervals [i]);
-                               g_assert (!live_intervals [i]->next);
+               stats.gc_maps_size += alloc_size;
+               stats.gc_callsites_size += ncallsites * map->callsite_entry_size;
+               stats.gc_bitmaps_size += bitmaps_size;
+               stats.gc_map_struct_size += sizeof (GCEncodedMap) + encoded_size;
 
-                               iv = live_intervals [i]->data;
-                               for (pc_offset = 0; pc_offset < iv->range->from; ++pc_offset)
-                                       set_bit (map->pin_bitmap, bitmap_width, pc_offset, i);
-                       }
+               cfg->jit_info->gc_info = emap;
 
-                       for (l = live_intervals [i]; l; l = l->next) {
-                               iv = l->data;
-                               for (r = iv->range; r; r = r->next) {
-                                       for (pc_offset = r->from; pc_offset < r->to; ++pc_offset)
-                                               set_bit (map->ref_bitmap, bitmap_width, pc_offset, i);
-                               }
-                       }
-               } else if (slots [i] == SLOT_PIN) {
-                       for (pc_offset = 0; pc_offset < cfg->code_len; ++pc_offset)
-                               set_bit (map->pin_bitmap, bitmap_width, pc_offset, i);
-               }
+               cfg->gc_map = (guint8*)emap;
+               cfg->gc_map_size = alloc_size;
        }
 
-       /* Registers */
-       for (i = 0; i < nregs; ++i) {
-               MonoLiveInterval *iv;
-               GSList *l, *l2;
-               MonoLiveRange2 *r;
-               int pc_offset;
-               StackSlotType type;
-
-               if (!(cfg->used_int_regs & (1 << i))) {
-                       g_assert (!gcfg->reg_types [i]);
-                       continue;
-               }
+       stats.all_slots += nslots;
+       stats.ref_slots += ntypes [SLOT_REF];
+       stats.noref_slots += ntypes [SLOT_NOREF];
+       stats.pin_slots += ntypes [SLOT_PIN];
+}
 
-               g_assert (i < 64);
+void
+mini_gc_create_gc_map (MonoCompile *cfg)
+{
+       if (!cfg->compute_gc_maps)
+               return;
 
-               map->used_regs |= (1 << i);
+       /*
+        * During marking, all frames except the top frame are at a call site, and we mark the
+        * top frame conservatively. This means that we only need to compute and record
+        * GC maps for call sites.
+        */
 
-               /*
-                * By default, registers are PIN.
-                * This is because we don't know their type outside their live range, since
-                * they could have the same value as in the caller, or a value set by the
-                * current method etc.
-                */
-               for (pc_offset = 0; pc_offset < cfg->code_len; ++pc_offset)
-                       set_bit (map->reg_pin_bitmap, reg_bitmap_width, pc_offset, i);
-
-               l2 = gcfg->reg_types [i];
-               for (l = gcfg->reg_live_intervals [i]; l; l = l->next) {
-                       iv = l->data;
-                       type = GPOINTER_TO_UINT (l2->data);
-
-                       if (type == SLOT_REF) {
-                               for (r = iv->range; r; r = r->next) {
-                                       for (pc_offset = r->from; pc_offset < r->to; ++pc_offset) {
-                                               set_bit (map->reg_ref_bitmap, reg_bitmap_width, pc_offset, i);
-                                       }
-                               }
-                       } else if (type == SLOT_PIN) {
-                               for (r = iv->range; r; r = r->next) {
-                                       for (pc_offset = r->from; pc_offset < r->to; ++pc_offset) {
-                                               set_bit (map->reg_pin_bitmap, reg_bitmap_width, pc_offset, i);
-                                       }
-                               }
-                       } else if (type == SLOT_NOREF) {
-                               for (r = iv->range; r; r = r->next) {
-                                       for (pc_offset = r->from; pc_offset < r->to; ++pc_offset) {
-                                               clear_bit (map->reg_pin_bitmap, reg_bitmap_width, pc_offset, i);
-                                       }
-                               }
-                       }
+       if (!(cfg->comp_done & MONO_COMP_LIVENESS))
+               /* Without liveness info, the live ranges are not precise enough */
+               return;
 
-                       l2 = l2->next;
-               }
-       }
+       mono_analyze_liveness_gc (cfg);
 
-       stats.all_slots += nslots;
-       for (i = 0; i < nslots; ++i) {
-               if (slots [i] == SLOT_REF)
-                       stats.ref_slots ++;
-               else if (slots [i] == SLOT_NOREF)
-                       stats.noref_slots ++;
-               else
-                       stats.pin_slots ++;
-       }
+       compute_frame_size (cfg);
+
+       init_gcfg (cfg);
+
+       process_spill_slots (cfg);
+       process_other_slots (cfg);
+       process_param_area_slots (cfg);
+       process_variables (cfg);
+       process_finally_clauses (cfg);
+
+       create_map (cfg);
+}
 
-       cfg->jit_info->gc_info = map;
+static void
+parse_debug_options (void)
+{
+       char **opts, **ptr;
+       char *env;
+
+       env = getenv ("MONO_GCMAP_DEBUG");
+       if (!env)
+               return;
 
-       g_free (live_intervals);
-       g_free (slots);
-       g_free (starts_pinned);
+       opts = g_strsplit (env, ",", -1);
+       for (ptr = opts; ptr && *ptr; ptr ++) {
+               /* No options yet */
+               fprintf (stderr, "Invalid format for the MONO_GCMAP_DEBUG env variable: '%s'\n", env);
+               exit (1);
+       }
+       g_strfreev (opts);
 }
 
 void
@@ -1448,13 +2314,30 @@ mini_gc_init (void)
 
        memset (&cb, 0, sizeof (cb));
        cb.thread_attach_func = thread_attach_func;
+       cb.thread_detach_func = thread_detach_func;
        cb.thread_suspend_func = thread_suspend_func;
        /* Comment this out to disable precise stack marking */
        cb.thread_mark_func = thread_mark_func;
        mono_gc_set_gc_callbacks (&cb);
 
+       logfile = mono_gc_get_logfile ();
+
+       parse_debug_options ();
+
        mono_counters_register ("GC Maps size",
-                                                       MONO_COUNTER_GC | MONO_COUNTER_INT, &gc_maps_size);
+                                                       MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_maps_size);
+       mono_counters_register ("GC Call Sites size",
+                                                       MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites_size);
+       mono_counters_register ("GC Bitmaps size",
+                                                       MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_bitmaps_size);
+       mono_counters_register ("GC Map struct size",
+                                                       MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_map_struct_size);
+       mono_counters_register ("GC Call Sites encoded using 8 bits",
+                                                       MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites8_size);
+       mono_counters_register ("GC Call Sites encoded using 16 bits",
+                                                       MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites16_size);
+       mono_counters_register ("GC Call Sites encoded using 32 bits",
+                                                       MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites32_size);
 
        mono_counters_register ("GC Map slots (all)",
                                                        MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.all_slots);
@@ -1465,8 +2348,15 @@ mini_gc_init (void)
        mono_counters_register ("GC Map slots (pin)",
                                                        MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.pin_slots);
 
+       mono_counters_register ("GC TLS Data size",
+                                                       MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.tlsdata_size);
+
        mono_counters_register ("Stack space scanned (all)",
                                                        MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_stacks);
+       mono_counters_register ("Stack space scanned (native)",
+                                                       MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_native);
+       mono_counters_register ("Stack space scanned (other)",
+                                                       MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_other);
        mono_counters_register ("Stack space scanned (using GC Maps)",
                                                        MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned);
        mono_counters_register ("Stack space scanned (precise)",
@@ -1495,12 +2385,12 @@ mini_gc_create_gc_map (MonoCompile *cfg)
 }
 
 void
-mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, StackSlotType type)
+mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
 {
 }
 
 void
-mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, StackSlotType type)
+mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
 {
 }
 
@@ -1524,12 +2414,25 @@ mini_gc_init_cfg (MonoCompile *cfg)
 
 /*
  * Problems with the current code:
- * - it makes two passes over the stack
  * - the stack walk is slow
- * - only the locals are scanned precisely
  * - vtypes/refs used in EH regions are treated conservatively
- * - the computation of the GC maps is slow since it involves a liveness analysis pass
- * - the GC maps are uncompressed and take up a lot of memory.
  * - if the code is finished, less pinning will be done, causing problems because
  *   we promote all surviving objects to old-gen.
+ * - the unwind code can't handle a method stopped inside a finally region, it thinks the caller is
+ *   another method, but in reality it is either the exception handling code or the CALL_HANDLER opcode.
+ *   This manifests in "Unable to find ip offset x in callsite list" assertions.
+ * - the unwind code also can't handle frames which are in the epilog, since the unwind info is not
+ *   precise there.
+ */
+
+/*
+ * Ideas for creating smaller GC maps:
+ * - remove empty columns from the bitmaps. This requires adding a mask bit array for
+ *   each bitmap.
+ * - merge reg and stack slot bitmaps, so the unused bits at the end of the reg bitmap are
+ *   not wasted.
+ * - if the bitmap width is not a multiple of 8, the remaining bits are wasted.
+ * - group ref and non-ref stack slots together in mono_allocate_stack_slots ().
+ * - add an index for the callsite table so that each entry can be encoded as a 1 byte difference
+ *   from an index entry.
  */