2 * mini-gc.c: GC interface for the mono JIT
5 * Zoltan Varga (vargaz@gmail.com)
7 * Copyright 2009 Novell, Inc (http://www.novell.com)
12 #include <mono/metadata/gc-internal.h>
15 #if defined(HAVE_SGEN_GC) && defined(MONO_ARCH_GC_MAPS_SUPPORTED)
17 #include <mono/metadata/sgen-gc.h>
18 #include <mono/metadata/gc-internal.h>
19 #include <mono/utils/mono-counters.h>
21 /* Contains state needed by the GC Map construction code */
24 * This contains information about stack slots initialized in the prolog, encoded using
25 * (slot_index << 16) | slot_type. The slot_index is relative to the CFA, i.e. 0
26 * means cfa+0, 1 means cfa-4/8, etc.
28 GSList *stack_slots_from_cfa;
29 /* Same for stack slots relative to the frame pointer */
30 GSList *stack_slots_from_fp;
32 /* Number of slots in the map */
34 /* The number of registers in the map */
36 /* Min and Max offsets of the stack frame relative to fp */
37 int min_offset, max_offset;
38 /* Same for the locals area */
39 int locals_min_offset, locals_max_offset;
41 /* The call sites where this frame can be stopped during GC */
42 GCCallSite **callsites;
43 /* The number of call sites */
47 * The width of the stack bitmaps in bytes. This is not equal to the bitmap width at
48 * runtime, since it includes columns which are 0.
50 int stack_bitmap_width;
52 * A bitmap whose width equals nslots, and whose height equals ncallsites.
53 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
56 guint8 *stack_ref_bitmap;
57 /* Same for SLOT_PIN */
58 guint8 *stack_pin_bitmap;
61 * Similar bitmaps for registers. These have width MONO_MAX_IREGS in bits.
64 guint8 *reg_ref_bitmap;
65 guint8 *reg_pin_bitmap;
68 #define ALIGN_TO(val,align) ((((mgreg_t)val) + ((align) - 1)) & ~((align) - 1))
71 /* We don't support debug levels, its all-or-nothing */
72 #define DEBUG(s) do { s; fflush (logfile); } while (0)
73 #define DEBUG_ENABLED 1
80 #define DEBUG_PRECISE(s) do { s; } while (0)
81 #define DEBUG_PRECISE_ENABLED
83 #define DEBUG_PRECISE(s)
87 * Contains information collected during the conservative stack marking pass,
88 * used during the precise pass. This helps to avoid doing a stack walk twice, which
94 int frame_start_offset;
96 /* Relative to stack_start */
97 int reg_locations [MONO_MAX_IREGS];
98 #ifdef DEBUG_PRECISE_ENABLED
101 int regs [MONO_MAX_IREGS];
105 /* Max number of frames stored in the TLS data */
106 #define MAX_FRAMES 50
109 * Per-thread data kept by this module. This is stored in the GC and passed to us as
110 * parameters, instead of being stored in a TLS variable, since during a collection,
111 * only the collection thread is active.
116 gboolean has_context;
117 MonoJitTlsData *jit_tls;
120 gpointer ref_to_track;
121 /* Number of frames collected during the !precise pass */
123 FrameInfo frames [MAX_FRAMES];
126 /* These are constant so don't store them in the GC Maps */
127 /* Number of registers stored in gc maps */
128 #define NREGS MONO_MAX_IREGS
132 * Contains information needed to mark a stack frame.
133 * This is a transient structure, created from a compressed representation on-demand.
137 * The offsets of the GC tracked area inside the stack frame relative to the frame pointer.
138 * This includes memory which is NOREF thus doesn't need GC maps.
143 * The offset relative to frame_offset where the the memory described by the GC maps
147 /* The number of stack slots in the map */
149 /* The frame pointer register */
151 /* The size of each callsite table entry */
152 guint8 callsite_entry_size;
153 guint has_pin_slots : 1;
154 guint has_ref_slots : 1;
155 guint has_ref_regs : 1;
156 guint has_pin_regs : 1;
158 /* The offsets below are into an external bitmaps array */
161 * A bitmap whose width is equal to bitmap_width, and whose height is equal to ncallsites.
162 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
165 guint32 stack_ref_bitmap_offset;
167 * Same for SLOT_PIN. It is possible that the same bit is set in both bitmaps at
168 * different callsites, if the slot starts out as PIN, and later changes to REF.
170 guint32 stack_pin_bitmap_offset;
173 * Corresponding bitmaps for registers
174 * These have width equal to the number of bits set in reg_ref_mask/reg_pin_mask.
175 * FIXME: Merge these with the normal bitmaps, i.e. reserve the first x slots for them ?
177 guint32 reg_pin_bitmap_offset;
178 guint32 reg_ref_bitmap_offset;
180 guint32 used_int_regs, reg_ref_mask, reg_pin_mask;
182 /* The number of bits set in the two masks above */
183 guint8 nref_regs, npin_regs;
186 * A bit array marking slots which contain refs.
187 * This is used only for debugging.
191 /* Callsite offsets */
192 /* These can take up a lot of space, so encode them compactly */
202 * A compressed version of GCMap. This is what gets stored in MonoJitInfo.
206 //guint8 encoded_size;
209 * The arrays below are embedded after the struct.
210 * Their address needs to be computed.
213 /* The fixed fields of the GCMap encoded using LEB128 */
214 guint8 encoded [MONO_ZERO_LEN_ARRAY];
216 /* An array of ncallsites entries, each entry is callsite_entry_size bytes long */
217 guint8 callsites [MONO_ZERO_LEN_ARRAY];
220 guint8 bitmaps [MONO_ZERO_LEN_ARRAY];
223 static int precise_frame_count [2], precise_frame_limit = -1;
224 static gboolean precise_frame_limit_inited;
230 int scanned_precisely;
231 int scanned_conservatively;
232 int scanned_registers;
242 int gc_callsites_size;
243 int gc_callsites8_size;
244 int gc_callsites16_size;
245 int gc_callsites32_size;
247 int gc_map_struct_size;
251 static JITGCStats stats;
253 static FILE *logfile;
255 // FIXME: Move these to a shared place
258 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
263 guint8 b = value & 0x7f;
265 if (value != 0) /* more bytes to come */
273 static G_GNUC_UNUSED void
274 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
277 gboolean negative = (value < 0);
285 /* the following is unnecessary if the
286 * implementation of >>= uses an arithmetic rather
287 * than logical shift for a signed left operand
291 value |= - (1 <<(size - 7));
292 /* sign bit of byte is second high order bit (0x40) */
293 if ((value == 0 && !(byte & 0x40)) ||
294 (value == -1 && (byte & 0x40)))
304 static inline guint32
305 decode_uleb128 (guint8 *buf, guint8 **endbuf)
315 res = res | (((int)(b & 0x7f)) << shift);
327 decode_sleb128 (guint8 *buf, guint8 **endbuf)
337 res = res | (((int)(b & 0x7f)) << shift);
340 if (shift < 32 && (b & 0x40))
341 res |= - (1 << shift);
352 encode_frame_reg (int frame_reg)
355 if (frame_reg == AMD64_RSP)
357 else if (frame_reg == AMD64_RBP)
359 #elif defined(TARGET_X86)
360 if (frame_reg == X86_EBP)
362 else if (frame_reg == X86_ESP)
367 g_assert_not_reached ();
372 decode_frame_reg (int encoded)
377 else if (encoded == 1)
379 #elif defined(TARGET_X86)
382 else if (encoded == 1)
387 g_assert_not_reached ();
393 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15, AMD64_RDI, AMD64_RSI };
395 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15 };
397 #elif defined(TARGET_X86)
398 static int callee_saved_regs [] = { X86_EBX, X86_ESI, X86_EDI };
402 encode_regmask (guint32 regmask)
408 for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i) {
409 if (regmask & (1 << callee_saved_regs [i])) {
411 regmask -= (1 << callee_saved_regs [i]);
414 g_assert (regmask == 0);
419 decode_regmask (guint32 regmask)
425 for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i)
426 if (regmask & (1 << i))
427 res |= (1 << callee_saved_regs [i]);
434 * Encode the fixed fields of MAP into a buffer pointed to by BUF.
437 encode_gc_map (GCMap *map, guint8 *buf, guint8 **endbuf)
441 encode_sleb128 (map->start_offset / sizeof (mgreg_t), buf, &buf);
442 encode_sleb128 (map->end_offset / sizeof (mgreg_t), buf, &buf);
443 encode_sleb128 (map->map_offset / sizeof (mgreg_t), buf, &buf);
444 encode_uleb128 (map->nslots, buf, &buf);
445 g_assert (map->callsite_entry_size <= 4);
446 freg = encode_frame_reg (map->frame_reg);
448 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);
449 encode_uleb128 (flags, buf, &buf);
450 encode_uleb128 (encode_regmask (map->used_int_regs), buf, &buf);
451 if (map->has_ref_regs)
452 encode_uleb128 (encode_regmask (map->reg_ref_mask), buf, &buf);
453 if (map->has_pin_regs)
454 encode_uleb128 (encode_regmask (map->reg_pin_mask), buf, &buf);
455 encode_uleb128 (map->ncallsites, buf, &buf);
463 * Decode the encoded GC map representation in BUF and store the result into MAP.
466 decode_gc_map (guint8 *buf, GCMap *map, guint8 **endbuf)
469 int stack_bitmap_size, reg_ref_bitmap_size, reg_pin_bitmap_size, offset, freg;
472 map->start_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
473 map->end_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
474 map->map_offset = decode_sleb128 (buf, &buf) * sizeof (mgreg_t);
475 map->nslots = decode_uleb128 (buf, &buf);
476 flags = decode_uleb128 (buf, &buf);
477 map->has_ref_slots = (flags & 1) ? 1 : 0;
478 map->has_pin_slots = (flags & 2) ? 1 : 0;
479 map->has_ref_regs = (flags & 4) ? 1 : 0;
480 map->has_pin_regs = (flags & 8) ? 1 : 0;
481 map->callsite_entry_size = ((flags >> 4) & 0x3) + 1;
483 map->frame_reg = decode_frame_reg (freg);
484 map->used_int_regs = decode_regmask (decode_uleb128 (buf, &buf));
485 if (map->has_ref_regs) {
486 map->reg_ref_mask = decode_regmask (decode_uleb128 (buf, &buf));
488 for (i = 0; i < NREGS; ++i)
489 if (map->reg_ref_mask & (1 << i))
493 if (map->has_pin_regs) {
494 map->reg_pin_mask = decode_regmask (decode_uleb128 (buf, &buf));
496 for (i = 0; i < NREGS; ++i)
497 if (map->reg_pin_mask & (1 << i))
501 map->ncallsites = decode_uleb128 (buf, &buf);
503 stack_bitmap_size = (ALIGN_TO (map->nslots, 8) / 8) * map->ncallsites;
504 reg_ref_bitmap_size = (ALIGN_TO (map->nref_regs, 8) / 8) * map->ncallsites;
505 reg_pin_bitmap_size = (ALIGN_TO (map->npin_regs, 8) / 8) * map->ncallsites;
507 map->stack_ref_bitmap_offset = offset;
508 if (map->has_ref_slots)
509 offset += stack_bitmap_size;
510 map->stack_pin_bitmap_offset = offset;
511 if (map->has_pin_slots)
512 offset += stack_bitmap_size;
513 map->reg_ref_bitmap_offset = offset;
514 if (map->has_ref_regs)
515 offset += reg_ref_bitmap_size;
516 map->reg_pin_bitmap_offset = offset;
517 if (map->has_pin_regs)
518 offset += reg_pin_bitmap_size;
524 thread_attach_func (void)
528 tls = g_new0 (TlsData, 1);
529 tls->tid = GetCurrentThreadId ();
530 stats.tlsdata_size += sizeof (TlsData);
536 thread_detach_func (gpointer user_data)
538 TlsData *tls = user_data;
544 thread_suspend_func (gpointer user_data, void *sigctx)
546 TlsData *tls = user_data;
549 /* Happens during startup */
552 tls->lmf = mono_get_lmf ();
554 mono_arch_sigctx_to_monoctx (sigctx, &tls->ctx);
555 tls->has_context = TRUE;
557 tls->has_context = FALSE;
559 tls->jit_tls = TlsGetValue (mono_jit_tls_id);
562 #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
565 set_bit (guint8 *bitmap, int width, int y, int x)
567 bitmap [(width * y) + (x / 8)] |= (1 << (x % 8));
571 clear_bit (guint8 *bitmap, int width, int y, int x)
573 bitmap [(width * y) + (x / 8)] &= ~(1 << (x % 8));
577 get_bit (guint8 *bitmap, int width, int y, int x)
579 return bitmap [(width * y) + (x / 8)] & (1 << (x % 8));
583 slot_type_to_string (GCSlotType type)
593 g_assert_not_reached ();
598 static inline mgreg_t
599 get_frame_pointer (MonoContext *ctx, int frame_reg)
601 #if defined(TARGET_AMD64)
602 if (frame_reg == AMD64_RSP)
604 else if (frame_reg == AMD64_RBP)
606 #elif defined(TARGET_X86)
607 if (frame_reg == X86_ESP)
609 else if (frame_reg == X86_EBP)
612 g_assert_not_reached ();
617 * conservatively_pass:
619 * Mark a thread stack conservatively and collect information needed by the precise pass.
622 conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
625 MonoContext ctx, new_ctx;
628 gboolean last = TRUE;
632 guint8* fp, *p, *real_frame_start, *frame_start, *frame_end;
633 int i, pc_offset, cindex, bitmap_width;
634 int scanned = 0, scanned_precisely, scanned_conservatively, scanned_registers;
636 StackFrameInfo frame;
637 mgreg_t *reg_locations [MONO_MAX_IREGS];
638 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
641 guint32 precise_regmask;
645 tls->ref_to_track = NULL;
648 /* tls == NULL can happen during startup */
649 if (mono_thread_internal_current () == NULL || !tls) {
650 mono_gc_conservatively_scan_area (stack_start, stack_end);
651 stats.scanned_stacks += stack_end - stack_start;
658 /* Number of bytes scanned based on GC map data */
660 /* Number of bytes scanned precisely based on GC map data */
661 scanned_precisely = 0;
662 /* Number of bytes scanned conservatively based on GC map data */
663 scanned_conservatively = 0;
664 /* Number of bytes scanned conservatively in register save areas */
665 scanned_registers = 0;
667 /* This is one past the last address which we have scanned */
668 stack_limit = stack_start;
670 if (!tls->has_context)
671 memset (&new_ctx, 0, sizeof (ctx));
673 memcpy (&new_ctx, &tls->ctx, sizeof (MonoContext));
675 memset (reg_locations, 0, sizeof (reg_locations));
676 memset (new_reg_locations, 0, sizeof (new_reg_locations));
679 memcpy (&ctx, &new_ctx, sizeof (ctx));
681 for (i = 0; i < MONO_MAX_IREGS; ++i) {
682 if (new_reg_locations [i]) {
684 * If the current frame saves the register, it means it might modify its
685 * value, thus the old location might not contain the same value, so
686 * we have to mark it conservatively.
688 if (reg_locations [i]) {
689 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
690 mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
691 scanned_registers += sizeof (mgreg_t);
694 reg_locations [i] = new_reg_locations [i];
696 DEBUG (fprintf (logfile, "\treg %s is now at location %p.\n", mono_arch_regname (i), reg_locations [i]));
700 g_assert ((mgreg_t)stack_limit % sizeof (mgreg_t) == 0);
702 #ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
703 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);
712 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
714 * These frames are problematic for several reasons:
715 * - they are unwound through an LMF, and we have no precise register tracking for those.
716 * - the LMF might not contain a precise ip, so we can't compute the call site.
717 * - the LMF only unwinds to the wrapper frame, so we get these methods twice.
719 DEBUG (fprintf (logfile, "Mark(0): <Managed-to-native transition>\n"));
720 for (i = 0; i < MONO_MAX_IREGS; ++i) {
721 if (reg_locations [i]) {
722 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
723 mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
724 scanned_registers += sizeof (mgreg_t);
726 reg_locations [i] = NULL;
727 new_reg_locations [i] = NULL;
732 /* The last frame can be in any state so mark conservatively */
735 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));
737 DEBUG (fprintf (logfile, "\t <Last frame>\n"));
742 pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
744 /* These frames are very problematic */
745 if (ji->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
746 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));
747 DEBUG (fprintf (logfile, "\tSkip.\n"));
751 /* All the other frames are at a call site */
753 if (tls->nframes == MAX_FRAMES) {
755 * Can't save information since the array is full. So scan the rest of the
756 * stack conservatively.
758 DEBUG (fprintf (logfile, "Mark (0): Frame stack full.\n"));
762 /* Scan the frame of this method */
765 * A frame contains the following:
770 * - localloc-ed memory
772 g_assert (pc_offset >= 0);
777 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));
778 DEBUG (fprintf (logfile, "\tNo GC Map.\n"));
783 * Debugging aid to control the number of frames scanned precisely
785 if (!precise_frame_limit_inited) {
786 if (getenv ("MONO_PRECISE_COUNT"))
787 precise_frame_limit = atoi (getenv ("MONO_PRECISE_COUNT"));
788 precise_frame_limit_inited = TRUE;
791 if (precise_frame_limit != -1) {
792 if (precise_frame_count [FALSE] == precise_frame_limit)
793 printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (ji->method, TRUE));
794 if (precise_frame_count [FALSE] > precise_frame_limit)
797 precise_frame_count [FALSE] ++;
799 /* Decode the encoded GC map */
801 memset (map, 0, sizeof (GCMap));
802 decode_gc_map (&emap->encoded [0], map, &p);
803 p = (guint8*)ALIGN_TO (p, map->callsite_entry_size);
804 map->callsites.offsets8 = p;
805 p += map->callsite_entry_size * map->ncallsites;
808 fp = (guint8*)get_frame_pointer (&ctx, map->frame_reg);
810 real_frame_start = fp + map->start_offset;
811 frame_start = fp + map->start_offset + map->map_offset;
812 frame_end = fp + map->end_offset;
814 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));
816 /* Find the callsite index */
817 if (ji->code_size < 256) {
818 for (i = 0; i < map->ncallsites; ++i)
819 /* ip points inside the call instruction */
820 if (map->callsites.offsets8 [i] == pc_offset + 1)
822 } else if (ji->code_size < 65536) {
823 // FIXME: Use a binary search
824 for (i = 0; i < map->ncallsites; ++i)
825 /* ip points inside the call instruction */
826 if (map->callsites.offsets16 [i] == pc_offset + 1)
829 // FIXME: Use a binary search
830 for (i = 0; i < map->ncallsites; ++i)
831 /* ip points inside the call instruction */
832 if (map->callsites.offsets32 [i] == pc_offset + 1)
835 if (i == map->ncallsites) {
836 printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (ji->method, TRUE));
837 g_assert_not_reached ();
841 g_assert (real_frame_start >= stack_limit);
843 if (real_frame_start > stack_limit) {
844 /* This scans the previously skipped frames as well */
845 DEBUG (fprintf (logfile, "\tscan area %p-%p (%d).\n", stack_limit, real_frame_start, (int)(real_frame_start - stack_limit)));
846 mono_gc_conservatively_scan_area (stack_limit, real_frame_start);
847 stats.scanned_other += real_frame_start - stack_limit;
850 /* Mark stack slots */
851 if (map->has_pin_slots) {
852 int bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
853 guint8 *pin_bitmap = &bitmaps [map->stack_pin_bitmap_offset + (bitmap_width * cindex)];
858 for (i = 0; i < map->nslots; ++i) {
859 pinned = pin_bitmap [i / 8] & (1 << (i % 8));
861 DEBUG (fprintf (logfile, "\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
862 mono_gc_conservatively_scan_area (p, p + sizeof (mgreg_t));
863 scanned_conservatively += sizeof (mgreg_t);
865 scanned_precisely += sizeof (mgreg_t);
867 p += sizeof (mgreg_t);
870 scanned_precisely += (map->nslots * sizeof (mgreg_t));
873 /* The area outside of start-end is NOREF */
874 scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * sizeof (mgreg_t));
877 precise_regmask = map->used_int_regs | (1 << map->frame_reg);
878 if (map->has_pin_regs) {
879 int bitmap_width = ALIGN_TO (map->npin_regs, 8) / 8;
880 guint8 *pin_bitmap = &bitmaps [map->reg_pin_bitmap_offset + (bitmap_width * cindex)];
882 for (i = 0; i < NREGS; ++i) {
883 if (!(map->used_int_regs & (1 << i)))
886 if (!(map->reg_pin_mask & (1 << i)))
889 if (pin_bitmap [bindex / 8] & (1 << (bindex % 8))) {
890 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
891 precise_regmask &= ~(1 << i);
897 scanned += map->end_offset - map->start_offset;
899 g_assert (scanned == scanned_precisely + scanned_conservatively);
901 stack_limit = frame_end;
903 /* Save information for the precise pass */
904 fi = &tls->frames [tls->nframes];
905 fi->nslots = map->nslots;
906 bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
907 if (map->has_ref_slots)
908 fi->bitmap = &bitmaps [map->stack_ref_bitmap_offset + (bitmap_width * cindex)];
911 fi->frame_start_offset = frame_start - stack_start;
912 fi->nreg_locations = 0;
913 DEBUG_PRECISE (fi->ji = ji);
914 DEBUG_PRECISE (fi->fp = fp);
916 if (map->has_ref_regs) {
917 int bitmap_width = ALIGN_TO (map->nref_regs, 8) / 8;
918 guint8 *ref_bitmap = &bitmaps [map->reg_ref_bitmap_offset + (bitmap_width * cindex)];
920 for (i = 0; i < NREGS; ++i) {
921 if (!(map->reg_ref_mask & (1 << i)))
924 if (reg_locations [i] && (ref_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
925 DEBUG_PRECISE (fi->regs [fi->nreg_locations] = i);
926 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i), reg_locations [i]));
927 fi->reg_locations [fi->nreg_locations] = (guint8*)reg_locations [i] - stack_start;
928 fi->nreg_locations ++;
935 * Clear locations of precisely stacked registers.
937 if (precise_regmask) {
938 for (i = 0; i < NREGS; ++i) {
939 if (precise_regmask & (1 << i)) {
941 * The method uses this register, and we have precise info for it.
942 * This means the location will be scanned precisely.
943 * Tell the code at the beginning of the loop that this location is
946 if (reg_locations [i])
947 DEBUG (fprintf (logfile, "\treg %s at location %p (==%p) is precise.\n", mono_arch_regname (i), reg_locations [i], (gpointer)*reg_locations [i]));
948 reg_locations [i] = NULL;
956 /* Scan the remaining register save locations */
957 for (i = 0; i < MONO_MAX_IREGS; ++i) {
958 if (reg_locations [i]) {
959 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", reg_locations [i]));
960 mono_gc_conservatively_scan_area (reg_locations [i], reg_locations [i] + sizeof (mgreg_t));
961 scanned_registers += sizeof (mgreg_t);
963 if (new_reg_locations [i]) {
964 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", new_reg_locations [i]));
965 mono_gc_conservatively_scan_area (new_reg_locations [i], new_reg_locations [i] + sizeof (mgreg_t));
966 scanned_registers += sizeof (mgreg_t);
970 if (stack_limit < stack_end) {
971 DEBUG (fprintf (logfile, "\tscan remaining stack %p-%p (%d).\n", stack_limit, stack_end, (int)(stack_end - stack_limit)));
972 mono_gc_conservatively_scan_area (stack_limit, stack_end);
973 stats.scanned_native += stack_end - stack_limit;
976 DEBUG (fprintf (logfile, "Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
978 stats.scanned_stacks += stack_end - stack_start;
979 stats.scanned += scanned;
980 stats.scanned_precisely += scanned_precisely;
981 stats.scanned_conservatively += scanned_conservatively;
982 stats.scanned_registers += scanned_registers;
984 //mono_gc_conservatively_scan_area (stack_start, stack_end);
990 * Mark a thread stack precisely based on information saved during the conservative
994 precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
1003 for (findex = 0; findex < tls->nframes; findex ++) {
1004 /* Load information saved by the !precise pass */
1005 fi = &tls->frames [findex];
1006 frame_start = stack_start + fi->frame_start_offset;
1008 DEBUG (char *fname = mono_method_full_name (fi->ji->method, TRUE); fprintf (logfile, "Mark(1): %s\n", fname); g_free (fname));
1011 * FIXME: Add a function to mark using a bitmap, to avoid doing a
1012 * call for each object.
1015 /* Mark stack slots */
1017 guint8 *ref_bitmap = fi->bitmap;
1020 for (i = 0; i < fi->nslots; ++i) {
1021 MonoObject **ptr = (MonoObject**)(frame_start + (i * sizeof (mgreg_t)));
1023 live = ref_bitmap [i / 8] & (1 << (i % 8));
1026 MonoObject *obj = *ptr;
1028 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1029 *ptr = mono_gc_scan_object (obj);
1030 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1032 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1037 * This is disabled because the pointer takes up a lot of space.
1038 * Stack slots might be shared between ref and non-ref variables ?
1040 if (map->ref_slots [i / 8] & (1 << (i % 8))) {
1041 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));
1043 * Fail fast if the live range is incorrect, and
1044 * the JITted code tries to access this object
1053 /* Mark registers */
1056 * Registers are different from stack slots, they have no address where they
1057 * are stored. Instead, some frame below this frame in the stack saves them
1058 * in its prolog to the stack. We can mark this location precisely.
1060 for (i = 0; i < fi->nreg_locations; ++i) {
1062 * reg_locations [i] contains the address of the stack slot where
1063 * a reg was last saved, so mark that slot.
1065 MonoObject **ptr = (MonoObject**)((guint8*)stack_start + fi->reg_locations [i]);
1066 MonoObject *obj = *ptr;
1069 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj));
1070 *ptr = mono_gc_scan_object (obj);
1071 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1073 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p\n", mono_arch_regname (fi->regs [i]), ptr, obj));
1079 * Debugging aid to check for missed refs.
1081 if (tls->ref_to_track) {
1084 for (p = (mgreg_t*)stack_start; p < (mgreg_t*)stack_end; ++p)
1085 if (*p == (mgreg_t)tls->ref_to_track)
1086 printf ("REF AT %p.\n", p);
1093 * This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
1094 * call, and TRUE at the second. USER_DATA points to a TlsData
1095 * structure filled up by thread_suspend_func.
1098 thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise)
1100 TlsData *tls = user_data;
1102 DEBUG (fprintf (logfile, "****************************************\n"));
1103 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));
1104 DEBUG (fprintf (logfile, "****************************************\n"));
1107 conservative_pass (tls, stack_start, stack_end);
1109 precise_pass (tls, stack_start, stack_end);
1113 mini_gc_init_gc_map (MonoCompile *cfg)
1115 if (COMPILE_LLVM (cfg))
1119 /* Debugging support */
1121 static int precise_count;
1124 if (getenv ("MONO_GCMAP_COUNT")) {
1125 if (precise_count == atoi (getenv ("MONO_GCMAP_COUNT")))
1126 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1127 if (precise_count > atoi (getenv ("MONO_GCMAP_COUNT")))
1133 cfg->compute_gc_maps = TRUE;
1135 cfg->gc_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoCompileGC));
1139 * mini_gc_set_slot_type_from_fp:
1141 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1142 * relative to the frame pointer. By default, all stack slots are type PIN, so there is no
1143 * need to call this function for those slots.
1146 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
1148 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1150 if (!cfg->compute_gc_maps)
1153 g_assert (slot_offset % sizeof (mgreg_t) == 0);
1155 gcfg->stack_slots_from_fp = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_fp, GINT_TO_POINTER (((slot_offset) << 16) | type));
1159 * mini_gc_set_slot_type_from_cfa:
1161 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1162 * relative to the DWARF CFA value. This should be called from mono_arch_emit_prolog ().
1163 * If type is STACK_REF, the slot is assumed to be live from the end of the prolog until
1164 * the end of the method. By default, all stack slots are type PIN, so there is no need to
1165 * call this function for those slots.
1168 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
1170 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1171 int slot = - (slot_offset / sizeof (mgreg_t));
1173 if (!cfg->compute_gc_maps)
1176 g_assert (slot_offset <= 0);
1177 g_assert (slot_offset % sizeof (mgreg_t) == 0);
1179 gcfg->stack_slots_from_cfa = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_cfa, GUINT_TO_POINTER (((slot) << 16) | type));
1183 fp_offset_to_slot (MonoCompile *cfg, int offset)
1185 MonoCompileGC *gcfg = cfg->gc_info;
1187 return (offset - gcfg->min_offset) / sizeof (mgreg_t);
1191 slot_to_fp_offset (MonoCompile *cfg, int slot)
1193 MonoCompileGC *gcfg = cfg->gc_info;
1195 return (slot * sizeof (mgreg_t)) + gcfg->min_offset;
1199 set_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1201 g_assert (slot >= 0 && slot < gcfg->nslots);
1203 if (type == SLOT_PIN) {
1204 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1205 set_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1206 } else if (type == SLOT_REF) {
1207 set_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1208 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1209 } else if (type == SLOT_NOREF) {
1210 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1211 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, callsite_index, slot);
1216 set_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1220 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1221 set_slot (gcfg, slot, cindex, type);
1225 set_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1229 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1230 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1231 if (callsite_offset >= from && callsite_offset < to)
1232 set_slot (gcfg, slot, cindex, type);
1237 set_reg_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1239 g_assert (slot >= 0 && slot < gcfg->nregs);
1241 if (type == SLOT_PIN) {
1242 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1243 set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1244 } else if (type == SLOT_REF) {
1245 set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1246 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1247 } else if (type == SLOT_NOREF) {
1248 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1249 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, callsite_index, slot);
1254 set_reg_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1258 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1259 set_reg_slot (gcfg, slot, cindex, type);
1263 set_reg_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1267 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1268 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1269 if (callsite_offset >= from && callsite_offset < to)
1270 set_reg_slot (gcfg, slot, cindex, type);
1275 process_spill_slots (MonoCompile *cfg)
1277 MonoCompileGC *gcfg = cfg->gc_info;
1282 /* Mark all ref/pin spill slots as NOREF by default outside of their live range */
1283 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1284 for (l = bb->spill_slot_defs; l; l = l->next) {
1285 MonoInst *def = l->data;
1286 int spill_slot = def->inst_c0;
1287 int bank = def->inst_c1;
1288 int offset = cfg->spill_info [bank][spill_slot].offset;
1289 int slot = fp_offset_to_slot (cfg, offset);
1291 if (bank == MONO_REG_INT_MP || bank == MONO_REG_INT_REF)
1292 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1296 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1297 for (l = bb->spill_slot_defs; l; l = l->next) {
1298 MonoInst *def = l->data;
1299 int spill_slot = def->inst_c0;
1300 int bank = def->inst_c1;
1301 int offset = cfg->spill_info [bank][spill_slot].offset;
1302 int slot = fp_offset_to_slot (cfg, offset);
1305 if (bank == MONO_REG_INT_MP)
1311 * Extend the live interval for the GC tracked spill slots
1312 * defined in this bblock.
1313 * FIXME: This is not needed.
1315 set_slot_in_range (gcfg, slot, def->backend.pc_offset, bb->native_offset + bb->native_length, type);
1317 if (cfg->verbose_level > 1)
1318 printf ("\t%s spill slot at %s0x%x(fp) (slot = %d)\n", slot_type_to_string (type), offset >= 0 ? "" : "-", ABS (offset), slot);
1322 /* Set fp spill slots to NOREF */
1323 for (i = 0; i < cfg->spill_info_len [MONO_REG_DOUBLE]; ++i) {
1324 int offset = cfg->spill_info [MONO_REG_DOUBLE][i].offset;
1330 slot = fp_offset_to_slot (cfg, offset);
1332 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1334 if (cfg->verbose_level > 1)
1335 printf ("\tfp spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1338 /* Set int spill slots to NOREF */
1339 for (i = 0; i < cfg->spill_info_len [MONO_REG_INT]; ++i) {
1340 int offset = cfg->spill_info [MONO_REG_INT][i].offset;
1346 slot = fp_offset_to_slot (cfg, offset);
1348 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1349 if (cfg->verbose_level > 1)
1350 printf ("\tint spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1355 * process_other_slots:
1357 * Process stack slots registered using mini_gc_set_slot_type_... ().
1360 process_other_slots (MonoCompile *cfg)
1362 MonoCompileGC *gcfg = cfg->gc_info;
1365 /* Relative to the CFA */
1366 for (l = gcfg->stack_slots_from_cfa; l; l = l->next) {
1367 guint data = GPOINTER_TO_UINT (l->data);
1368 int cfa_slot = data >> 16;
1369 GCSlotType type = data & 0xff;
1373 * Map the cfa relative slot to an fp relative slot.
1374 * slot_addr == cfa - <cfa_slot>*4/8
1375 * fp + cfa_offset == cfa
1376 * -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
1378 slot = (cfg->cfa_offset / sizeof (mgreg_t)) - cfa_slot - (gcfg->min_offset / sizeof (mgreg_t));
1380 set_slot_everywhere (gcfg, slot, type);
1382 if (cfg->verbose_level > 1) {
1383 int fp_offset = slot_to_fp_offset (cfg, slot);
1384 if (type == SLOT_NOREF)
1385 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)));
1389 /* Relative to the FP */
1390 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
1391 gint data = GPOINTER_TO_INT (l->data);
1392 int offset = data >> 16;
1393 GCSlotType type = data & 0xff;
1396 slot = fp_offset_to_slot (cfg, offset);
1398 set_slot_everywhere (gcfg, slot, type);
1400 /* Liveness for these slots is handled by process_spill_slots () */
1402 if (cfg->verbose_level > 1) {
1403 if (type == SLOT_REF)
1404 printf ("\tref slot at fp+0x%x (slot = %d)\n", offset, slot);
1410 process_variables (MonoCompile *cfg)
1412 MonoCompileGC *gcfg = cfg->gc_info;
1413 MonoMethodSignature *sig = mono_method_signature (cfg->method);
1414 int i, locals_min_slot, locals_max_slot, cindex;
1418 int locals_min_offset = gcfg->locals_min_offset;
1419 int locals_max_offset = gcfg->locals_max_offset;
1421 /* Slots for locals are NOREF by default */
1422 locals_min_slot = (locals_min_offset - gcfg->min_offset) / sizeof (mgreg_t);
1423 locals_max_slot = (locals_max_offset - gcfg->min_offset) / sizeof (mgreg_t);
1424 for (i = locals_min_slot; i < locals_max_slot; ++i) {
1425 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1429 * Compute the offset where variables are initialized in the first bblock, if any.
1431 pc_offsets = g_new0 (int, cfg->next_vreg);
1433 bb = cfg->bb_entry->next_bb;
1434 MONO_BB_FOR_EACH_INS (bb, tmp) {
1435 if (tmp->opcode == OP_GC_LIVENESS_DEF) {
1436 int vreg = tmp->inst_c1;
1437 if (pc_offsets [vreg] == 0) {
1438 g_assert (tmp->backend.pc_offset > 0);
1439 pc_offsets [vreg] = tmp->backend.pc_offset;
1445 * Stack slots holding arguments are initialized in the prolog.
1446 * This means we can treat them alive for the whole method.
1448 for (i = 0; i < cfg->num_varinfo; i++) {
1449 MonoInst *ins = cfg->varinfo [i];
1450 MonoType *t = ins->inst_vtype;
1453 gboolean byref, is_this = FALSE;
1454 gboolean is_arg = i < cfg->locals_start;
1456 if (ins == cfg->ret)
1459 vmv = MONO_VARINFO (cfg, i);
1461 /* For some reason, 'this' is byref */
1462 if (sig->hasthis && ins == cfg->args [0] && !cfg->method->klass->valuetype) {
1463 t = &cfg->method->klass->byval_arg;
1469 if (ins->opcode == OP_REGVAR) {
1471 GCSlotType slot_type;
1473 t = mini_type_get_underlying_type (NULL, t);
1476 g_assert (hreg < MONO_MAX_IREGS);
1479 slot_type = SLOT_PIN;
1481 slot_type = MONO_TYPE_IS_REFERENCE (t) ? SLOT_REF : SLOT_NOREF;
1483 if (slot_type == SLOT_PIN) {
1484 /* These have no live interval, be conservative */
1485 set_reg_slot_everywhere (gcfg, hreg, slot_type);
1488 * Unlike variables allocated to the stack, we generate liveness info
1489 * for noref vars in registers in mono_spill_global_vars (), because
1490 * knowing that a register doesn't contain a ref allows us to mark its save
1491 * locations precisely.
1493 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1494 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1495 set_reg_slot (gcfg, hreg, cindex, slot_type);
1498 if (cfg->verbose_level > 1) {
1499 printf ("\t%s %sreg %s(R%d)\n", slot_type_to_string (slot_type), is_arg ? "arg " : "", mono_arch_regname (hreg), vmv->vreg);
1505 if (ins->opcode != OP_REGOFFSET)
1508 if (ins->inst_offset % sizeof (mgreg_t) != 0)
1511 if (is_arg && ins->inst_offset >= gcfg->max_offset)
1512 /* In parent frame */
1515 pos = fp_offset_to_slot (cfg, ins->inst_offset);
1517 if (is_arg && ins->flags & MONO_INST_IS_DEAD) {
1518 /* These do not get stored in the prolog */
1519 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1521 if (cfg->verbose_level > 1) {
1522 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));
1527 if (MONO_TYPE_ISSTRUCT (t)) {
1529 gsize *bitmap = NULL;
1530 gboolean pin = FALSE;
1534 if (ins->backend.is_pinvoke)
1535 size = mono_class_native_size (ins->klass, NULL);
1537 size = mono_class_value_size (ins->klass, NULL);
1538 size_in_slots = ALIGN_TO (size, sizeof (mgreg_t)) / sizeof (mgreg_t);
1540 if (!ins->klass->has_references) {
1542 for (j = 0; j < size_in_slots; ++j)
1543 set_slot_everywhere (gcfg, pos + j, SLOT_NOREF);
1548 if (ins->klass->generic_container || mono_class_is_open_constructed_type (t)) {
1549 /* FIXME: Generic sharing */
1552 mono_class_compute_gc_descriptor (ins->klass);
1554 bitmap = mono_gc_get_bitmap_for_descr (ins->klass->gc_descr, &numbits);
1558 * Most vtypes are marked volatile because of the LDADDR instructions,
1559 * and they have no liveness information since they are decomposed
1560 * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
1561 * them during VZERO decomposition.
1563 if (!pc_offsets [vmv->vreg])
1567 if (ins->backend.is_pinvoke)
1570 if (cfg->verbose_level > 1)
1571 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));
1574 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1575 if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
1576 for (j = 0; j < numbits; ++j) {
1577 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
1578 /* The descriptor is for the boxed object */
1579 set_slot (gcfg, (pos + j - (sizeof (MonoObject) / sizeof (mgreg_t))), cindex, pin ? SLOT_PIN : SLOT_REF);
1585 if (cfg->verbose_level > 1) {
1586 for (j = 0; j < numbits; ++j) {
1587 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD)))
1588 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))));
1592 if (cfg->verbose_level > 1)
1593 printf ("\t\tpinned\n");
1594 for (j = 0; j < size_in_slots; ++j) {
1595 set_slot_everywhere (gcfg, pos + j, SLOT_PIN);
1604 if (!is_arg && (ins->inst_offset < gcfg->min_offset || ins->inst_offset >= gcfg->max_offset))
1605 /* Vret addr etc. */
1610 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1612 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1613 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1614 set_slot (gcfg, pos, cindex, SLOT_PIN);
1616 if (cfg->verbose_level > 1)
1617 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));
1622 * This is currently disabled, but could be enabled to debug crashes.
1625 if (t->type == MONO_TYPE_I) {
1627 * Variables created in mono_handle_global_vregs have type I, but they
1628 * could hold GC refs since the vregs they were created from might not been
1629 * marked as holding a GC ref. So be conservative.
1631 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1636 t = mini_type_get_underlying_type (NULL, t);
1638 if (!MONO_TYPE_IS_REFERENCE (t)) {
1639 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1640 if (cfg->verbose_level > 1)
1641 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));
1645 /* 'this' is marked INDIRECT for gshared methods */
1646 if (ins->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT) && !is_this) {
1648 * For volatile variables, treat them alive from the point they are
1649 * initialized in the first bblock until the end of the method.
1652 set_slot_everywhere (gcfg, pos, SLOT_REF);
1653 } else if (pc_offsets [vmv->vreg]) {
1654 set_slot_in_range (gcfg, pos, 0, pc_offsets [vmv->vreg], SLOT_PIN);
1655 set_slot_in_range (gcfg, pos, pc_offsets [vmv->vreg], cfg->code_size, SLOT_REF);
1657 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1659 if (cfg->verbose_level > 1)
1660 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));
1665 /* Live for the whole method */
1666 set_slot_everywhere (gcfg, pos, SLOT_REF);
1668 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1669 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1670 set_slot (gcfg, pos, cindex, SLOT_REF);
1673 if (cfg->verbose_level > 1) {
1674 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));
1678 g_free (pc_offsets);
1682 sp_offset_to_fp_offset (MonoCompile *cfg, int sp_offset)
1685 * Convert a sp relative offset to a slot index. This is
1686 * platform specific.
1689 /* fp = sp + offset */
1690 g_assert (cfg->frame_reg == AMD64_RBP);
1691 return (- cfg->arch.sp_fp_offset + sp_offset);
1699 type_to_gc_slot_type (MonoType *t)
1703 t = mini_type_get_underlying_type (NULL, t);
1704 if (MONO_TYPE_IS_REFERENCE (t))
1707 if (MONO_TYPE_ISSTRUCT (t)) {
1708 MonoClass *klass = mono_class_from_mono_type (t);
1709 if (!klass->has_references) {
1721 process_param_area_slots (MonoCompile *cfg)
1723 MonoCompileGC *gcfg = cfg->gc_info;
1728 * These slots are used for passing parameters during calls. They are sp relative, not
1729 * fp relative, so they are harder to handle.
1731 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1732 /* The distance between fp and sp is not constant */
1735 is_param = mono_mempool_alloc0 (cfg->mempool, gcfg->nslots * sizeof (gboolean));
1737 for (i = 0; i < gcfg->ncallsites; ++i) {
1738 GCCallSite *callsite = gcfg->callsites [i];
1741 for (l = callsite->param_slots; l; l = l->next) {
1742 MonoInst *def = l->data;
1743 int sp_offset = def->inst_offset;
1744 int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1745 int slot = fp_offset_to_slot (cfg, fp_offset);
1747 g_assert (slot >= 0 && slot < gcfg->nslots);
1748 is_param [slot] = TRUE;
1752 /* All param area slots are noref by default */
1753 for (i = 0; i < gcfg->nslots; ++i) {
1755 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1758 for (i = 0; i < gcfg->ncallsites; ++i) {
1759 GCCallSite *callsite = gcfg->callsites [i];
1762 for (l = callsite->param_slots; l; l = l->next) {
1763 MonoInst *def = l->data;
1764 MonoType *t = def->inst_vtype;
1765 int sp_offset = def->inst_offset;
1766 int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1767 int slot = fp_offset_to_slot (cfg, fp_offset);
1768 GCSlotType type = type_to_gc_slot_type (t);
1770 /* The slot is live between the def instruction and the call */
1771 set_slot_in_range (gcfg, slot, def->backend.pc_offset, callsite->pc_offset + 1, type);
1772 if (cfg->verbose_level > 1)
1773 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);
1779 process_finally_clauses (MonoCompile *cfg)
1781 MonoCompileGC *gcfg = cfg->gc_info;
1782 GCCallSite **callsites;
1784 gboolean has_finally;
1785 int i, j, nslots, nregs;
1787 ncallsites = gcfg->ncallsites;
1788 nslots = gcfg->nslots;
1789 nregs = gcfg->nregs;
1790 callsites = gcfg->callsites;
1793 * The calls to the finally clauses don't show up in the cfg. See
1794 * test_0_liveness_8 ().
1795 * Variables accessed inside the finally clause are already marked VOLATILE by
1796 * mono_liveness_handle_exception_clauses (). Variables not accessed inside the finally clause have
1797 * correct liveness outside the finally clause. So mark them PIN inside the finally clauses.
1799 has_finally = FALSE;
1800 for (i = 0; i < cfg->header->num_clauses; ++i) {
1801 MonoExceptionClause *clause = &cfg->header->clauses [i];
1803 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1808 if (cfg->verbose_level > 1)
1809 printf ("\tMethod has finally clauses, pessimizing live ranges.\n");
1810 for (j = 0; j < ncallsites; ++j) {
1811 MonoBasicBlock *bb = callsites [j]->bb;
1812 MonoExceptionClause *clause;
1813 gboolean is_in_finally = FALSE;
1815 for (i = 0; i < cfg->header->num_clauses; ++i) {
1816 clause = &cfg->header->clauses [i];
1818 if (MONO_OFFSET_IN_HANDLER (clause, bb->real_offset)) {
1819 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1820 is_in_finally = TRUE;
1826 if (is_in_finally) {
1827 for (i = 0; i < nslots; ++i)
1828 set_slot (gcfg, i, j, SLOT_PIN);
1829 for (i = 0; i < nregs; ++i)
1830 set_reg_slot (gcfg, i, j, SLOT_PIN);
1837 compute_frame_size (MonoCompile *cfg)
1839 int i, locals_min_offset, locals_max_offset, cfa_min_offset, cfa_max_offset;
1840 int min_offset, max_offset;
1841 MonoCompileGC *gcfg = cfg->gc_info;
1842 MonoMethodSignature *sig = mono_method_signature (cfg->method);
1845 /* Compute min/max offsets from the fp */
1848 #if defined(TARGET_AMD64) || defined(TARGET_X86)
1849 locals_min_offset = ALIGN_TO (cfg->locals_min_stack_offset, sizeof (mgreg_t));
1850 locals_max_offset = cfg->locals_max_stack_offset;
1852 /* min/max stack offset needs to be computed in mono_arch_allocate_vars () */
1856 locals_min_offset = ALIGN_TO (locals_min_offset, sizeof (mgreg_t));
1857 locals_max_offset = ALIGN_TO (locals_max_offset, sizeof (mgreg_t));
1859 min_offset = locals_min_offset;
1860 max_offset = locals_max_offset;
1863 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1864 MonoInst *ins = cfg->args [i];
1866 if (ins->opcode == OP_REGOFFSET)
1867 min_offset = MIN (min_offset, ins->inst_offset);
1871 g_assert (cfg->frame_reg == cfg->cfa_reg);
1872 g_assert (cfg->cfa_offset > 0);
1874 cfa_max_offset = cfg->cfa_offset;
1876 min_offset = MIN (min_offset, cfa_min_offset);
1877 max_offset = MAX (max_offset, cfa_max_offset);
1879 /* Fp relative slots */
1880 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
1881 gint data = GPOINTER_TO_INT (l->data);
1882 int offset = data >> 16;
1884 min_offset = MIN (min_offset, offset);
1888 if (!(cfg->flags & MONO_CFG_HAS_SPILLUP)) {
1889 int stack_offset = ALIGN_TO (cfg->stack_offset, sizeof (mgreg_t));
1890 min_offset = MIN (min_offset, (-stack_offset));
1893 /* Param area slots */
1895 min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
1898 gcfg->min_offset = min_offset;
1899 gcfg->max_offset = max_offset;
1900 gcfg->locals_min_offset = locals_min_offset;
1901 gcfg->locals_max_offset = locals_max_offset;
1905 init_gcfg (MonoCompile *cfg)
1907 int i, nregs, nslots;
1908 MonoCompileGC *gcfg = cfg->gc_info;
1909 GCCallSite **callsites;
1918 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1919 ncallsites += g_slist_length (bb->gc_callsites);
1921 callsites = mono_mempool_alloc0 (cfg->mempool, ncallsites * sizeof (GCCallSite*));
1923 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1924 for (l = bb->gc_callsites; l; l = l->next)
1925 callsites [i++] = l->data;
1928 /* The callsites should already be ordered by pc offset */
1929 for (i = 1; i < ncallsites; ++i)
1930 g_assert (callsites [i - 1]->pc_offset < callsites [i]->pc_offset);
1933 * The stack frame looks like this:
1935 * <fp + max_offset> == cfa -> <end of previous frame>
1936 * <other stack slots>
1938 * <other stack slots>
1939 * fp + min_offset ->
1944 if (cfg->verbose_level > 1)
1945 printf ("GC Map for %s: 0x%x-0x%x\n", mono_method_full_name (cfg->method, TRUE), gcfg->min_offset, gcfg->max_offset);
1947 nslots = (gcfg->max_offset - gcfg->min_offset) / sizeof (mgreg_t);
1950 gcfg->nslots = nslots;
1951 gcfg->nregs = nregs;
1952 gcfg->callsites = callsites;
1953 gcfg->ncallsites = ncallsites;
1954 gcfg->stack_bitmap_width = ALIGN_TO (nslots, 8) / 8;
1955 gcfg->reg_bitmap_width = ALIGN_TO (nregs, 8) / 8;
1956 gcfg->stack_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * ncallsites);
1957 gcfg->stack_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * ncallsites);
1958 gcfg->reg_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
1959 gcfg->reg_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * ncallsites);
1961 /* All slots start out as PIN */
1962 memset (gcfg->stack_pin_bitmap, 0xff, gcfg->stack_bitmap_width * ncallsites);
1963 for (i = 0; i < nregs; ++i) {
1965 * By default, registers are NOREF.
1966 * It is possible for a callee to save them before being defined in this method,
1967 * but the saved value is dead too, so it doesn't need to be marked.
1969 if ((cfg->used_int_regs & (1 << i)))
1970 set_reg_slot_everywhere (gcfg, i, SLOT_NOREF);
1975 create_map (MonoCompile *cfg)
1978 int i, j, nregs, nslots, nref_regs, npin_regs, alloc_size, bitmaps_size, bitmaps_offset;
1980 int stack_bitmap_width, stack_bitmap_size, reg_ref_bitmap_width, reg_ref_bitmap_size;
1981 int reg_pin_bitmap_width, reg_pin_bitmap_size, bindex;
1983 gboolean has_ref_slots, has_pin_slots, has_ref_regs, has_pin_regs;
1984 MonoCompileGC *gcfg = cfg->gc_info;
1985 GCCallSite **callsites;
1987 guint8 *bitmap, *bitmaps;
1988 guint32 reg_ref_mask, reg_pin_mask;
1990 ncallsites = gcfg->ncallsites;
1991 nslots = gcfg->nslots;
1992 nregs = gcfg->nregs;
1993 callsites = gcfg->callsites;
1996 * Compute the real size of the bitmap i.e. ignore NOREF columns at the beginning and at
1997 * the end. Also, compute whenever the map needs ref/pin bitmaps, and collect stats.
1999 has_ref_slots = FALSE;
2000 has_pin_slots = FALSE;
2003 memset (ntypes, 0, sizeof (ntypes));
2004 for (i = 0; i < nslots; ++i) {
2005 gboolean has_ref = FALSE;
2006 gboolean has_pin = FALSE;
2008 for (j = 0; j < ncallsites; ++j) {
2009 if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, j, i))
2011 if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, j, i))
2016 has_ref_slots = TRUE;
2018 has_pin_slots = TRUE;
2021 ntypes [SLOT_REF] ++;
2023 ntypes [SLOT_PIN] ++;
2025 ntypes [SLOT_NOREF] ++;
2027 if (has_ref || has_pin) {
2034 start = end = nslots;
2036 g_assert (start != -1);
2037 g_assert (start < end);
2040 has_ref_regs = FALSE;
2041 has_pin_regs = FALSE;
2046 for (i = 0; i < nregs; ++i) {
2047 gboolean has_ref = FALSE;
2048 gboolean has_pin = FALSE;
2050 if (!(cfg->used_int_regs & (1 << i)))
2053 for (j = 0; j < ncallsites; ++j) {
2054 if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i)) {
2059 for (j = 0; j < ncallsites; ++j) {
2060 if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i)) {
2067 reg_ref_mask |= (1 << i);
2068 has_ref_regs = TRUE;
2072 reg_pin_mask |= (1 << i);
2073 has_pin_regs = TRUE;
2078 if (cfg->verbose_level > 1)
2079 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);
2081 /* Create the GC Map */
2083 stack_bitmap_width = ALIGN_TO (end - start, 8) / 8;
2084 stack_bitmap_size = stack_bitmap_width * ncallsites;
2085 reg_ref_bitmap_width = ALIGN_TO (nref_regs, 8) / 8;
2086 reg_ref_bitmap_size = reg_ref_bitmap_width * ncallsites;
2087 reg_pin_bitmap_width = ALIGN_TO (npin_regs, 8) / 8;
2088 reg_pin_bitmap_size = reg_pin_bitmap_width * ncallsites;
2089 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);
2091 map = mono_mempool_alloc0 (cfg->mempool, sizeof (GCMap));
2093 map->frame_reg = cfg->frame_reg;
2094 map->start_offset = gcfg->min_offset;
2095 map->end_offset = gcfg->min_offset + (nslots * sizeof (mgreg_t));
2096 map->map_offset = start * sizeof (mgreg_t);
2097 map->nslots = end - start;
2098 map->has_ref_slots = has_ref_slots;
2099 map->has_pin_slots = has_pin_slots;
2100 map->has_ref_regs = has_ref_regs;
2101 map->has_pin_regs = has_pin_regs;
2102 g_assert (nregs < 32);
2103 map->used_int_regs = cfg->used_int_regs;
2104 map->reg_ref_mask = reg_ref_mask;
2105 map->reg_pin_mask = reg_pin_mask;
2106 map->nref_regs = nref_regs;
2107 map->npin_regs = npin_regs;
2109 bitmaps = mono_mempool_alloc0 (cfg->mempool, bitmaps_size);
2112 if (has_ref_slots) {
2113 map->stack_ref_bitmap_offset = bitmaps_offset;
2114 bitmaps_offset += stack_bitmap_size;
2116 bitmap = &bitmaps [map->stack_ref_bitmap_offset];
2117 for (i = 0; i < nslots; ++i) {
2118 for (j = 0; j < ncallsites; ++j) {
2119 if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, j, i))
2120 set_bit (bitmap, stack_bitmap_width, j, i - start);
2124 if (has_pin_slots) {
2125 map->stack_pin_bitmap_offset = bitmaps_offset;
2126 bitmaps_offset += stack_bitmap_size;
2128 bitmap = &bitmaps [map->stack_pin_bitmap_offset];
2129 for (i = 0; i < nslots; ++i) {
2130 for (j = 0; j < ncallsites; ++j) {
2131 if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, j, i))
2132 set_bit (bitmap, stack_bitmap_width, j, i - start);
2137 map->reg_ref_bitmap_offset = bitmaps_offset;
2138 bitmaps_offset += reg_ref_bitmap_size;
2140 bitmap = &bitmaps [map->reg_ref_bitmap_offset];
2142 for (i = 0; i < nregs; ++i) {
2143 if (reg_ref_mask & (1 << i)) {
2144 for (j = 0; j < ncallsites; ++j) {
2145 if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, j, i))
2146 set_bit (bitmap, reg_ref_bitmap_width, j, bindex);
2153 map->reg_pin_bitmap_offset = bitmaps_offset;
2154 bitmaps_offset += reg_pin_bitmap_size;
2156 bitmap = &bitmaps [map->reg_pin_bitmap_offset];
2158 for (i = 0; i < nregs; ++i) {
2159 if (reg_pin_mask & (1 << i)) {
2160 for (j = 0; j < ncallsites; ++j) {
2161 if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, j, i))
2162 set_bit (bitmap, reg_pin_bitmap_width, j, bindex);
2170 map->ncallsites = ncallsites;
2171 if (cfg->code_len < 256)
2172 map->callsite_entry_size = 1;
2173 else if (cfg->code_len < 65536)
2174 map->callsite_entry_size = 2;
2176 map->callsite_entry_size = 4;
2178 /* Encode the GC Map */
2186 encode_gc_map (map, buf, &endbuf);
2187 g_assert (endbuf - buf < 256);
2189 encoded_size = endbuf - buf;
2190 alloc_size = sizeof (GCEncodedMap) + ALIGN_TO (encoded_size, map->callsite_entry_size) + (map->callsite_entry_size * map->ncallsites) + bitmaps_size;
2192 emap = mono_domain_alloc0 (cfg->domain, alloc_size);
2193 //emap->ref_slots = map->ref_slots;
2195 /* Encoded fixed fields */
2196 p = &emap->encoded [0];
2197 //emap->encoded_size = encoded_size;
2198 memcpy (p, buf, encoded_size);
2201 /* Callsite table */
2202 p = (guint8*)ALIGN_TO ((mgreg_t)p, map->callsite_entry_size);
2203 if (map->callsite_entry_size == 1) {
2204 guint8 *offsets = p;
2205 for (i = 0; i < ncallsites; ++i)
2206 offsets [i] = callsites [i]->pc_offset;
2207 stats.gc_callsites8_size += ncallsites * sizeof (guint8);
2208 } else if (map->callsite_entry_size == 2) {
2209 guint16 *offsets = (guint16*)p;
2210 for (i = 0; i < ncallsites; ++i)
2211 offsets [i] = callsites [i]->pc_offset;
2212 stats.gc_callsites16_size += ncallsites * sizeof (guint16);
2214 guint32 *offsets = (guint32*)p;
2215 for (i = 0; i < ncallsites; ++i)
2216 offsets [i] = callsites [i]->pc_offset;
2217 stats.gc_callsites32_size += ncallsites * sizeof (guint32);
2219 p += ncallsites * map->callsite_entry_size;
2222 memcpy (p, bitmaps, bitmaps_size);
2224 stats.gc_maps_size += alloc_size;
2225 stats.gc_callsites_size += ncallsites * map->callsite_entry_size;
2226 stats.gc_bitmaps_size += bitmaps_size;
2227 stats.gc_map_struct_size += sizeof (GCEncodedMap) + encoded_size;
2229 cfg->jit_info->gc_info = emap;
2232 stats.all_slots += nslots;
2233 stats.ref_slots += ntypes [SLOT_REF];
2234 stats.noref_slots += ntypes [SLOT_NOREF];
2235 stats.pin_slots += ntypes [SLOT_PIN];
2239 mini_gc_create_gc_map (MonoCompile *cfg)
2241 if (!cfg->compute_gc_maps)
2245 * During marking, all frames except the top frame are at a call site, and we mark the
2246 * top frame conservatively. This means that we only need to compute and record
2247 * GC maps for call sites.
2250 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
2251 /* Without liveness info, the live ranges are not precise enough */
2254 mono_analyze_liveness_gc (cfg);
2256 compute_frame_size (cfg);
2260 process_spill_slots (cfg);
2261 process_other_slots (cfg);
2262 process_param_area_slots (cfg);
2263 process_variables (cfg);
2264 process_finally_clauses (cfg);
2270 parse_debug_options (void)
2275 env = getenv ("MONO_GCMAP_DEBUG");
2279 opts = g_strsplit (env, ",", -1);
2280 for (ptr = opts; ptr && *ptr; ptr ++) {
2281 /* No options yet */
2282 fprintf (stderr, "Invalid format for the MONO_GCMAP_DEBUG env variable: '%s'\n", env);
2293 memset (&cb, 0, sizeof (cb));
2294 cb.thread_attach_func = thread_attach_func;
2295 cb.thread_detach_func = thread_detach_func;
2296 cb.thread_suspend_func = thread_suspend_func;
2297 /* Comment this out to disable precise stack marking */
2298 cb.thread_mark_func = thread_mark_func;
2299 mono_gc_set_gc_callbacks (&cb);
2301 logfile = mono_sgen_get_logfile ();
2303 parse_debug_options ();
2305 mono_counters_register ("GC Maps size",
2306 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_maps_size);
2307 mono_counters_register ("GC Call Sites size",
2308 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites_size);
2309 mono_counters_register ("GC Bitmaps size",
2310 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_bitmaps_size);
2311 mono_counters_register ("GC Map struct size",
2312 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_map_struct_size);
2313 mono_counters_register ("GC Call Sites encoded using 8 bits",
2314 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites8_size);
2315 mono_counters_register ("GC Call Sites encoded using 16 bits",
2316 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites16_size);
2317 mono_counters_register ("GC Call Sites encoded using 32 bits",
2318 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites32_size);
2320 mono_counters_register ("GC Map slots (all)",
2321 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.all_slots);
2322 mono_counters_register ("GC Map slots (ref)",
2323 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.ref_slots);
2324 mono_counters_register ("GC Map slots (noref)",
2325 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.noref_slots);
2326 mono_counters_register ("GC Map slots (pin)",
2327 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.pin_slots);
2329 mono_counters_register ("GC TLS Data size",
2330 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.tlsdata_size);
2332 mono_counters_register ("Stack space scanned (all)",
2333 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_stacks);
2334 mono_counters_register ("Stack space scanned (native)",
2335 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_native);
2336 mono_counters_register ("Stack space scanned (other)",
2337 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_other);
2338 mono_counters_register ("Stack space scanned (using GC Maps)",
2339 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned);
2340 mono_counters_register ("Stack space scanned (precise)",
2341 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_precisely);
2342 mono_counters_register ("Stack space scanned (pin)",
2343 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_conservatively);
2344 mono_counters_register ("Stack space scanned (pin registers)",
2345 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_registers);
2356 mini_gc_init_gc_map (MonoCompile *cfg)
2361 mini_gc_create_gc_map (MonoCompile *cfg)
2366 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
2371 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
2380 * Set GC specific options in CFG.
2383 mini_gc_init_cfg (MonoCompile *cfg)
2385 if (mono_gc_is_moving ()) {
2386 cfg->disable_ref_noref_stack_slot_share = TRUE;
2387 cfg->gen_write_barriers = TRUE;
2390 mini_gc_init_gc_map (cfg);
2394 * Problems with the current code:
2395 * - the stack walk is slow
2396 * - vtypes/refs used in EH regions are treated conservatively
2397 * - if the code is finished, less pinning will be done, causing problems because
2398 * we promote all surviving objects to old-gen.
2399 * - the unwind code can't handle a method stopped inside a finally region, it thinks the caller is
2400 * another method, but in reality it is either the exception handling code or the CALL_HANDLER opcode.
2401 * This manifests in "Unable to find ip offset x in callsite list" assertions.
2402 * - the unwind code also can't handle frames which are in the epilog, since the unwind info is not
2407 * Ideas for creating smaller GC maps:
2408 * - remove empty columns from the bitmaps. This requires adding a mask bit array for
2410 * - merge reg and stack slot bitmaps, so the unused bits at the end of the reg bitmap are
2412 * - if the bitmap width is not a multiple of 8, the remaining bits are wasted.
2413 * - group ref and non-ref stack slots together in mono_allocate_stack_slots ().
2414 * - add an index for the callsite table so that each entry can be encoded as a 1 byte difference
2415 * from an index entry.