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)
8 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
13 #include <mono/metadata/gc-internal.h>
16 #if defined(MONO_ARCH_GC_MAPS_SUPPORTED)
18 #include <mono/metadata/sgen-conf.h>
19 #include <mono/metadata/gc-internal.h>
20 #include <mono/utils/mono-counters.h>
22 #define SIZEOF_SLOT ((int)sizeof (mgreg_t))
24 #define GC_BITS_PER_WORD (sizeof (mword) * 8)
26 /* Contains state needed by the GC Map construction code */
29 * This contains information about stack slots initialized in the prolog, encoded using
30 * (slot_index << 16) | slot_type. The slot_index is relative to the CFA, i.e. 0
31 * means cfa+0, 1 means cfa-4/8, etc.
33 GSList *stack_slots_from_cfa;
34 /* Same for stack slots relative to the frame pointer */
35 GSList *stack_slots_from_fp;
37 /* Number of slots in the map */
39 /* The number of registers in the map */
41 /* Min and Max offsets of the stack frame relative to fp */
42 int min_offset, max_offset;
43 /* Same for the locals area */
44 int locals_min_offset, locals_max_offset;
46 /* The call sites where this frame can be stopped during GC */
47 GCCallSite **callsites;
48 /* The number of call sites */
52 * The width of the stack bitmaps in bytes. This is not equal to the bitmap width at
53 * runtime, since it includes columns which are 0.
55 int stack_bitmap_width;
57 * A bitmap whose width equals nslots, and whose height equals ncallsites.
58 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
61 guint8 *stack_ref_bitmap;
62 /* Same for SLOT_PIN */
63 guint8 *stack_pin_bitmap;
66 * Similar bitmaps for registers. These have width MONO_MAX_IREGS in bits.
69 guint8 *reg_ref_bitmap;
70 guint8 *reg_pin_bitmap;
73 #define ALIGN_TO(val,align) ((((mgreg_t)val) + ((align) - 1)) & ~((align) - 1))
78 /* We don't support debug levels, its all-or-nothing */
79 #define DEBUG(s) do { s; fflush (logfile); } while (0)
80 #define DEBUG_ENABLED 1
87 #define DEBUG_PRECISE(s) do { s; } while (0)
88 #define DEBUG_PRECISE_ENABLED
90 #define DEBUG_PRECISE(s)
94 * Contains information collected during the conservative stack marking pass,
95 * used during the precise pass. This helps to avoid doing a stack walk twice, which
101 int frame_start_offset;
103 /* Relative to stack_start */
104 int reg_locations [MONO_MAX_IREGS];
105 #ifdef DEBUG_PRECISE_ENABLED
108 int regs [MONO_MAX_IREGS];
112 /* Max number of frames stored in the TLS data */
113 #define MAX_FRAMES 50
116 * Per-thread data kept by this module. This is stored in the GC and passed to us as
117 * parameters, instead of being stored in a TLS variable, since during a collection,
118 * only the collection thread is active.
121 MonoThreadUnwindState unwind_state;
122 MonoThreadInfo *info;
125 gpointer ref_to_track;
126 /* Number of frames collected during the !precise pass */
128 FrameInfo frames [MAX_FRAMES];
131 /* These are constant so don't store them in the GC Maps */
132 /* Number of registers stored in gc maps */
133 #define NREGS MONO_MAX_IREGS
137 * Contains information needed to mark a stack frame.
138 * This is a transient structure, created from a compressed representation on-demand.
142 * The offsets of the GC tracked area inside the stack frame relative to the frame pointer.
143 * This includes memory which is NOREF thus doesn't need GC maps.
148 * The offset relative to frame_offset where the the memory described by the GC maps
152 /* The number of stack slots in the map */
154 /* The frame pointer register */
156 /* The size of each callsite table entry */
157 guint8 callsite_entry_size;
158 guint has_pin_slots : 1;
159 guint has_ref_slots : 1;
160 guint has_ref_regs : 1;
161 guint has_pin_regs : 1;
163 /* The offsets below are into an external bitmaps array */
166 * A bitmap whose width is equal to bitmap_width, and whose height is equal to ncallsites.
167 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
170 guint32 stack_ref_bitmap_offset;
172 * Same for SLOT_PIN. It is possible that the same bit is set in both bitmaps at
173 * different callsites, if the slot starts out as PIN, and later changes to REF.
175 guint32 stack_pin_bitmap_offset;
178 * Corresponding bitmaps for registers
179 * These have width equal to the number of bits set in reg_ref_mask/reg_pin_mask.
180 * FIXME: Merge these with the normal bitmaps, i.e. reserve the first x slots for them ?
182 guint32 reg_pin_bitmap_offset;
183 guint32 reg_ref_bitmap_offset;
185 guint32 used_int_regs, reg_ref_mask, reg_pin_mask;
187 /* The number of bits set in the two masks above */
188 guint8 nref_regs, npin_regs;
191 * A bit array marking slots which contain refs.
192 * This is used only for debugging.
196 /* Callsite offsets */
197 /* These can take up a lot of space, so encode them compactly */
207 * A compressed version of GCMap. This is what gets stored in MonoJitInfo.
211 //guint8 encoded_size;
214 * The arrays below are embedded after the struct.
215 * Their address needs to be computed.
218 /* The fixed fields of the GCMap encoded using LEB128 */
219 guint8 encoded [MONO_ZERO_LEN_ARRAY];
221 /* An array of ncallsites entries, each entry is callsite_entry_size bytes long */
222 guint8 callsites [MONO_ZERO_LEN_ARRAY];
225 guint8 bitmaps [MONO_ZERO_LEN_ARRAY];
228 static int precise_frame_count [2], precise_frame_limit = -1;
229 static gboolean precise_frame_limit_inited;
235 int scanned_precisely;
236 int scanned_conservatively;
237 int scanned_registers;
247 int gc_callsites_size;
248 int gc_callsites8_size;
249 int gc_callsites16_size;
250 int gc_callsites32_size;
252 int gc_map_struct_size;
256 static JITGCStats stats;
258 static FILE *logfile;
260 static gboolean enable_gc_maps_for_aot;
263 mini_gc_enable_gc_maps_for_aot (void)
265 enable_gc_maps_for_aot = TRUE;
268 // FIXME: Move these to a shared place
271 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
276 guint8 b = value & 0x7f;
278 if (value != 0) /* more bytes to come */
286 static G_GNUC_UNUSED void
287 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
290 gboolean negative = (value < 0);
298 /* the following is unnecessary if the
299 * implementation of >>= uses an arithmetic rather
300 * than logical shift for a signed left operand
304 value |= - (1 <<(size - 7));
305 /* sign bit of byte is second high order bit (0x40) */
306 if ((value == 0 && !(byte & 0x40)) ||
307 (value == -1 && (byte & 0x40)))
317 static inline guint32
318 decode_uleb128 (guint8 *buf, guint8 **endbuf)
328 res = res | (((int)(b & 0x7f)) << shift);
340 decode_sleb128 (guint8 *buf, guint8 **endbuf)
350 res = res | (((int)(b & 0x7f)) << shift);
353 if (shift < 32 && (b & 0x40))
354 res |= - (1 << shift);
365 encode_frame_reg (int frame_reg)
368 if (frame_reg == AMD64_RSP)
370 else if (frame_reg == AMD64_RBP)
372 #elif defined(TARGET_X86)
373 if (frame_reg == X86_EBP)
375 else if (frame_reg == X86_ESP)
377 #elif defined(TARGET_ARM)
378 if (frame_reg == ARMREG_SP)
380 else if (frame_reg == ARMREG_FP)
382 #elif defined(TARGET_S390X)
383 if (frame_reg == S390_SP)
385 else if (frame_reg == S390_FP)
390 g_assert_not_reached ();
395 decode_frame_reg (int encoded)
400 else if (encoded == 1)
402 #elif defined(TARGET_X86)
405 else if (encoded == 1)
407 #elif defined(TARGET_ARM)
410 else if (encoded == 1)
412 #elif defined(TARGET_S390X)
415 else if (encoded == 1)
420 g_assert_not_reached ();
426 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15, AMD64_RDI, AMD64_RSI };
428 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15 };
430 #elif defined(TARGET_X86)
431 static int callee_saved_regs [] = { X86_EBX, X86_ESI, X86_EDI };
432 #elif defined(TARGET_ARM)
433 static int callee_saved_regs [] = { ARMREG_V1, ARMREG_V2, ARMREG_V3, ARMREG_V4, ARMREG_V5, ARMREG_V7, ARMREG_FP };
434 #elif defined(TARGET_ARM64)
436 static int callee_saved_regs [] = { };
437 #elif defined(TARGET_S390X)
438 static int callee_saved_regs [] = { s390_r6, s390_r7, s390_r8, s390_r9, s390_r10, s390_r11, s390_r12, s390_r13, s390_r14 };
439 #elif defined(TARGET_POWERPC)
440 static int callee_saved_regs [] = { ppc_r6, ppc_r7, ppc_r8, ppc_r9, ppc_r10, ppc_r11, ppc_r12, ppc_r13, ppc_r14 };
444 encode_regmask (guint32 regmask)
450 for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i) {
451 if (regmask & (1 << callee_saved_regs [i])) {
453 regmask -= (1 << callee_saved_regs [i]);
456 g_assert (regmask == 0);
461 decode_regmask (guint32 regmask)
467 for (i = 0; i < sizeof (callee_saved_regs) / sizeof (int); ++i)
468 if (regmask & (1 << i))
469 res |= (1 << callee_saved_regs [i]);
476 * Encode the fixed fields of MAP into a buffer pointed to by BUF.
479 encode_gc_map (GCMap *map, guint8 *buf, guint8 **endbuf)
483 encode_sleb128 (map->start_offset / SIZEOF_SLOT, buf, &buf);
484 encode_sleb128 (map->end_offset / SIZEOF_SLOT, buf, &buf);
485 encode_sleb128 (map->map_offset / SIZEOF_SLOT, buf, &buf);
486 encode_uleb128 (map->nslots, buf, &buf);
487 g_assert (map->callsite_entry_size <= 4);
488 freg = encode_frame_reg (map->frame_reg);
490 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);
491 encode_uleb128 (flags, buf, &buf);
492 encode_uleb128 (encode_regmask (map->used_int_regs), buf, &buf);
493 if (map->has_ref_regs)
494 encode_uleb128 (encode_regmask (map->reg_ref_mask), buf, &buf);
495 if (map->has_pin_regs)
496 encode_uleb128 (encode_regmask (map->reg_pin_mask), buf, &buf);
497 encode_uleb128 (map->ncallsites, buf, &buf);
505 * Decode the encoded GC map representation in BUF and store the result into MAP.
508 decode_gc_map (guint8 *buf, GCMap *map, guint8 **endbuf)
511 int stack_bitmap_size, reg_ref_bitmap_size, reg_pin_bitmap_size, offset, freg;
514 map->start_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
515 map->end_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
516 map->map_offset = decode_sleb128 (buf, &buf) * SIZEOF_SLOT;
517 map->nslots = decode_uleb128 (buf, &buf);
518 flags = decode_uleb128 (buf, &buf);
519 map->has_ref_slots = (flags & 1) ? 1 : 0;
520 map->has_pin_slots = (flags & 2) ? 1 : 0;
521 map->has_ref_regs = (flags & 4) ? 1 : 0;
522 map->has_pin_regs = (flags & 8) ? 1 : 0;
523 map->callsite_entry_size = ((flags >> 4) & 0x3) + 1;
525 map->frame_reg = decode_frame_reg (freg);
526 map->used_int_regs = decode_regmask (decode_uleb128 (buf, &buf));
527 if (map->has_ref_regs) {
528 map->reg_ref_mask = decode_regmask (decode_uleb128 (buf, &buf));
530 for (i = 0; i < NREGS; ++i)
531 if (map->reg_ref_mask & (1 << i))
535 if (map->has_pin_regs) {
536 map->reg_pin_mask = decode_regmask (decode_uleb128 (buf, &buf));
538 for (i = 0; i < NREGS; ++i)
539 if (map->reg_pin_mask & (1 << i))
543 map->ncallsites = decode_uleb128 (buf, &buf);
545 stack_bitmap_size = (ALIGN_TO (map->nslots, 8) / 8) * map->ncallsites;
546 reg_ref_bitmap_size = (ALIGN_TO (map->nref_regs, 8) / 8) * map->ncallsites;
547 reg_pin_bitmap_size = (ALIGN_TO (map->npin_regs, 8) / 8) * map->ncallsites;
549 map->stack_ref_bitmap_offset = offset;
550 if (map->has_ref_slots)
551 offset += stack_bitmap_size;
552 map->stack_pin_bitmap_offset = offset;
553 if (map->has_pin_slots)
554 offset += stack_bitmap_size;
555 map->reg_ref_bitmap_offset = offset;
556 if (map->has_ref_regs)
557 offset += reg_ref_bitmap_size;
558 map->reg_pin_bitmap_offset = offset;
559 if (map->has_pin_regs)
560 offset += reg_pin_bitmap_size;
566 thread_attach_func (void)
570 tls = g_new0 (TlsData, 1);
571 tls->tid = GetCurrentThreadId ();
572 tls->info = mono_thread_info_current ();
573 stats.tlsdata_size += sizeof (TlsData);
579 thread_detach_func (gpointer user_data)
581 TlsData *tls = user_data;
587 thread_suspend_func (gpointer user_data, void *sigctx, MonoContext *ctx)
589 TlsData *tls = user_data;
592 /* Happens during startup */
596 if (tls->tid != GetCurrentThreadId ()) {
597 /* Happens on osx because threads are not suspended using signals */
602 g_assert (tls->info);
606 res = mono_thread_state_init_from_handle (&tls->unwind_state, tls->info);
609 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
611 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
612 mono_arch_sigctx_to_monoctx (sigctx, &tls->unwind_state.ctx);
613 tls->unwind_state.valid = TRUE;
615 tls->unwind_state.valid = FALSE;
618 memcpy (&tls->unwind_state.ctx, ctx, sizeof (MonoContext));
619 tls->unwind_state.valid = TRUE;
621 tls->unwind_state.valid = FALSE;
623 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_JIT_TLS] = mono_native_tls_get_value (mono_jit_tls_id);
624 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
627 if (!tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN]) {
628 /* Happens during startup */
629 tls->unwind_state.valid = FALSE;
634 #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
637 set_bit (guint8 *bitmap, int width, int y, int x)
639 bitmap [(width * y) + (x / 8)] |= (1 << (x % 8));
643 clear_bit (guint8 *bitmap, int width, int y, int x)
645 bitmap [(width * y) + (x / 8)] &= ~(1 << (x % 8));
649 get_bit (guint8 *bitmap, int width, int y, int x)
651 return bitmap [(width * y) + (x / 8)] & (1 << (x % 8));
655 slot_type_to_string (GCSlotType type)
665 g_assert_not_reached ();
670 static inline mgreg_t
671 get_frame_pointer (MonoContext *ctx, int frame_reg)
673 #if defined(TARGET_AMD64)
674 if (frame_reg == AMD64_RSP)
676 else if (frame_reg == AMD64_RBP)
678 #elif defined(TARGET_X86)
679 if (frame_reg == X86_ESP)
681 else if (frame_reg == X86_EBP)
683 #elif defined(TARGET_ARM)
684 if (frame_reg == ARMREG_SP)
685 return (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
686 else if (frame_reg == ARMREG_FP)
687 return (mgreg_t)MONO_CONTEXT_GET_BP (ctx);
688 #elif defined(TARGET_S390X)
689 if (frame_reg == S390_SP)
690 return (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
691 else if (frame_reg == S390_FP)
692 return (mgreg_t)MONO_CONTEXT_GET_BP (ctx);
694 g_assert_not_reached ();
699 * conservatively_pass:
701 * Mark a thread stack conservatively and collect information needed by the precise pass.
704 conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
708 MonoContext ctx, new_ctx;
711 gboolean last = TRUE;
715 guint8* fp, *p, *real_frame_start, *frame_start, *frame_end;
716 int i, pc_offset, cindex, bitmap_width;
717 int scanned = 0, scanned_precisely, scanned_conservatively, scanned_registers;
719 StackFrameInfo frame;
720 mgreg_t *reg_locations [MONO_MAX_IREGS];
721 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
724 guint32 precise_regmask;
728 tls->ref_to_track = NULL;
731 /* tls == NULL can happen during startup */
732 if (mono_thread_internal_current () == NULL || !tls) {
733 mono_gc_conservatively_scan_area (stack_start, stack_end);
734 stats.scanned_stacks += stack_end - stack_start;
738 lmf = tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF];
741 /* Number of bytes scanned based on GC map data */
743 /* Number of bytes scanned precisely based on GC map data */
744 scanned_precisely = 0;
745 /* Number of bytes scanned conservatively based on GC map data */
746 scanned_conservatively = 0;
747 /* Number of bytes scanned conservatively in register save areas */
748 scanned_registers = 0;
750 /* This is one past the last address which we have scanned */
751 stack_limit = stack_start;
753 if (!tls->unwind_state.valid)
754 memset (&new_ctx, 0, sizeof (ctx));
756 memcpy (&new_ctx, &tls->unwind_state.ctx, sizeof (MonoContext));
758 memset (reg_locations, 0, sizeof (reg_locations));
759 memset (new_reg_locations, 0, sizeof (new_reg_locations));
762 if (!tls->unwind_state.valid)
765 memcpy (&ctx, &new_ctx, sizeof (ctx));
767 for (i = 0; i < MONO_MAX_IREGS; ++i) {
768 if (new_reg_locations [i]) {
770 * If the current frame saves the register, it means it might modify its
771 * value, thus the old location might not contain the same value, so
772 * we have to mark it conservatively.
774 if (reg_locations [i]) {
775 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
776 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
777 scanned_registers += SIZEOF_SLOT;
780 reg_locations [i] = new_reg_locations [i];
782 DEBUG (fprintf (logfile, "\treg %s is now at location %p.\n", mono_arch_regname (i), reg_locations [i]));
786 g_assert ((mgreg_t)stack_limit % SIZEOF_SLOT == 0);
788 res = mono_find_jit_info_ext (frame.domain ? frame.domain : tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN], tls->unwind_state.unwind_data [MONO_UNWIND_DATA_JIT_TLS], NULL, &ctx, &new_ctx, NULL, &lmf, new_reg_locations, &frame);
794 // FIXME: For skipped frames, scan the param area of the parent frame conservatively ?
796 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
798 * These frames are problematic for several reasons:
799 * - they are unwound through an LMF, and we have no precise register tracking for those.
800 * - the LMF might not contain a precise ip, so we can't compute the call site.
801 * - the LMF only unwinds to the wrapper frame, so we get these methods twice.
803 DEBUG (fprintf (logfile, "Mark(0): <Managed-to-native transition>\n"));
804 for (i = 0; i < MONO_MAX_IREGS; ++i) {
805 if (reg_locations [i]) {
806 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
807 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
808 scanned_registers += SIZEOF_SLOT;
810 reg_locations [i] = NULL;
811 new_reg_locations [i] = NULL;
818 method = jinfo_get_method (ji);
822 /* The last frame can be in any state so mark conservatively */
825 DEBUG (char *fname = mono_method_full_name (method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
827 DEBUG (fprintf (logfile, "\t <Last frame>\n"));
830 * new_reg_locations is not precise when a method is interrupted during its epilog, so clear it.
832 for (i = 0; i < MONO_MAX_IREGS; ++i) {
833 if (reg_locations [i]) {
834 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
835 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
836 scanned_registers += SIZEOF_SLOT;
838 if (new_reg_locations [i]) {
839 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), new_reg_locations [i]));
840 mono_gc_conservatively_scan_area (new_reg_locations [i], (char*)new_reg_locations [i] + SIZEOF_SLOT);
841 scanned_registers += SIZEOF_SLOT;
843 reg_locations [i] = NULL;
844 new_reg_locations [i] = NULL;
849 pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
851 /* These frames are very problematic */
852 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
853 DEBUG (char *fname = mono_method_full_name (method, TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
854 DEBUG (fprintf (logfile, "\tSkip.\n"));
858 /* All the other frames are at a call site */
860 if (tls->nframes == MAX_FRAMES) {
862 * Can't save information since the array is full. So scan the rest of the
863 * stack conservatively.
865 DEBUG (fprintf (logfile, "Mark (0): Frame stack full.\n"));
869 /* Scan the frame of this method */
872 * A frame contains the following:
877 * - localloc-ed memory
879 g_assert (pc_offset >= 0);
884 DEBUG (char *fname = mono_method_full_name (jinfo_get_method (ji), TRUE); fprintf (logfile, "Mark(0): %s+0x%x (%p)\n", fname, pc_offset, (gpointer)MONO_CONTEXT_GET_IP (&ctx)); g_free (fname));
885 DEBUG (fprintf (logfile, "\tNo GC Map.\n"));
889 /* The embedded callsite table requires this */
890 g_assert (((mgreg_t)emap % 4) == 0);
893 * Debugging aid to control the number of frames scanned precisely
895 if (!precise_frame_limit_inited) {
896 if (g_getenv ("MONO_PRECISE_COUNT"))
897 precise_frame_limit = atoi (g_getenv ("MONO_PRECISE_COUNT"));
898 precise_frame_limit_inited = TRUE;
901 if (precise_frame_limit != -1) {
902 if (precise_frame_count [FALSE] == precise_frame_limit)
903 printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (method, TRUE));
904 if (precise_frame_count [FALSE] > precise_frame_limit)
907 precise_frame_count [FALSE] ++;
909 /* Decode the encoded GC map */
911 memset (map, 0, sizeof (GCMap));
912 decode_gc_map (&emap->encoded [0], map, &p);
913 p = (guint8*)ALIGN_TO (p, map->callsite_entry_size);
914 map->callsites.offsets8 = p;
915 p += map->callsite_entry_size * map->ncallsites;
918 fp = (guint8*)get_frame_pointer (&ctx, map->frame_reg);
920 real_frame_start = fp + map->start_offset;
921 frame_start = fp + map->start_offset + map->map_offset;
922 frame_end = fp + map->end_offset;
924 DEBUG (char *fname = mono_method_full_name (jinfo_get_method (ji), 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));
926 /* Find the callsite index */
927 if (map->callsite_entry_size == 1) {
928 for (i = 0; i < map->ncallsites; ++i)
929 /* ip points inside the call instruction */
930 if (map->callsites.offsets8 [i] == pc_offset + 1)
932 } else if (map->callsite_entry_size == 2) {
933 // FIXME: Use a binary search
934 for (i = 0; i < map->ncallsites; ++i)
935 /* ip points inside the call instruction */
936 if (map->callsites.offsets16 [i] == pc_offset + 1)
939 // FIXME: Use a binary search
940 for (i = 0; i < map->ncallsites; ++i)
941 /* ip points inside the call instruction */
942 if (map->callsites.offsets32 [i] == pc_offset + 1)
945 if (i == map->ncallsites) {
946 printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (method, TRUE));
947 g_assert_not_reached ();
952 * This is not neccessary true on x86 because frames have a different size at each
955 //g_assert (real_frame_start >= stack_limit);
957 if (real_frame_start > stack_limit) {
958 /* This scans the previously skipped frames as well */
959 DEBUG (fprintf (logfile, "\tscan area %p-%p (%d).\n", stack_limit, real_frame_start, (int)(real_frame_start - stack_limit)));
960 mono_gc_conservatively_scan_area (stack_limit, real_frame_start);
961 stats.scanned_other += real_frame_start - stack_limit;
964 /* Mark stack slots */
965 if (map->has_pin_slots) {
966 int bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
967 guint8 *pin_bitmap = &bitmaps [map->stack_pin_bitmap_offset + (bitmap_width * cindex)];
972 for (i = 0; i < map->nslots; ++i) {
973 pinned = pin_bitmap [i / 8] & (1 << (i % 8));
975 DEBUG (fprintf (logfile, "\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
976 mono_gc_conservatively_scan_area (p, p + SIZEOF_SLOT);
977 scanned_conservatively += SIZEOF_SLOT;
979 scanned_precisely += SIZEOF_SLOT;
984 scanned_precisely += (map->nslots * SIZEOF_SLOT);
987 /* The area outside of start-end is NOREF */
988 scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * SIZEOF_SLOT);
991 precise_regmask = map->used_int_regs | (1 << map->frame_reg);
992 if (map->has_pin_regs) {
993 int bitmap_width = ALIGN_TO (map->npin_regs, 8) / 8;
994 guint8 *pin_bitmap = &bitmaps [map->reg_pin_bitmap_offset + (bitmap_width * cindex)];
996 for (i = 0; i < NREGS; ++i) {
997 if (!(map->used_int_regs & (1 << i)))
1000 if (!(map->reg_pin_mask & (1 << i)))
1003 if (pin_bitmap [bindex / 8] & (1 << (bindex % 8))) {
1004 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
1005 precise_regmask &= ~(1 << i);
1011 scanned += map->end_offset - map->start_offset;
1013 g_assert (scanned == scanned_precisely + scanned_conservatively);
1015 stack_limit = frame_end;
1017 /* Save information for the precise pass */
1018 fi = &tls->frames [tls->nframes];
1019 fi->nslots = map->nslots;
1020 bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
1021 if (map->has_ref_slots)
1022 fi->bitmap = &bitmaps [map->stack_ref_bitmap_offset + (bitmap_width * cindex)];
1025 fi->frame_start_offset = frame_start - stack_start;
1026 fi->nreg_locations = 0;
1027 DEBUG_PRECISE (fi->ji = ji);
1028 DEBUG_PRECISE (fi->fp = fp);
1030 if (map->has_ref_regs) {
1031 int bitmap_width = ALIGN_TO (map->nref_regs, 8) / 8;
1032 guint8 *ref_bitmap = &bitmaps [map->reg_ref_bitmap_offset + (bitmap_width * cindex)];
1034 for (i = 0; i < NREGS; ++i) {
1035 if (!(map->reg_ref_mask & (1 << i)))
1038 if (reg_locations [i] && (ref_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
1039 DEBUG_PRECISE (fi->regs [fi->nreg_locations] = i);
1040 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i), reg_locations [i]));
1041 fi->reg_locations [fi->nreg_locations] = (guint8*)reg_locations [i] - stack_start;
1042 fi->nreg_locations ++;
1049 * Clear locations of precisely tracked registers.
1051 if (precise_regmask) {
1052 for (i = 0; i < NREGS; ++i) {
1053 if (precise_regmask & (1 << i)) {
1055 * The method uses this register, and we have precise info for it.
1056 * This means the location will be scanned precisely.
1057 * Tell the code at the beginning of the loop that this location is
1060 if (reg_locations [i])
1061 DEBUG (fprintf (logfile, "\treg %s at location %p (==%p) is precise.\n", mono_arch_regname (i), reg_locations [i], (gpointer)*reg_locations [i]));
1062 reg_locations [i] = NULL;
1070 /* Scan the remaining register save locations */
1071 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1072 if (reg_locations [i]) {
1073 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", reg_locations [i]));
1074 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
1075 scanned_registers += SIZEOF_SLOT;
1077 if (new_reg_locations [i]) {
1078 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", new_reg_locations [i]));
1079 mono_gc_conservatively_scan_area (new_reg_locations [i], (char*)new_reg_locations [i] + SIZEOF_SLOT);
1080 scanned_registers += SIZEOF_SLOT;
1084 if (stack_limit < stack_end) {
1085 DEBUG (fprintf (logfile, "\tscan remaining stack %p-%p (%d).\n", stack_limit, stack_end, (int)(stack_end - stack_limit)));
1086 mono_gc_conservatively_scan_area (stack_limit, stack_end);
1087 stats.scanned_native += stack_end - stack_limit;
1090 DEBUG (fprintf (logfile, "Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
1092 stats.scanned_stacks += stack_end - stack_start;
1093 stats.scanned += scanned;
1094 stats.scanned_precisely += scanned_precisely;
1095 stats.scanned_conservatively += scanned_conservatively;
1096 stats.scanned_registers += scanned_registers;
1098 //mono_gc_conservatively_scan_area (stack_start, stack_end);
1104 * Mark a thread stack precisely based on information saved during the conservative
1108 precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end, void *gc_data)
1112 guint8 *frame_start;
1117 if (!tls->unwind_state.valid)
1120 for (findex = 0; findex < tls->nframes; findex ++) {
1121 /* Load information saved by the !precise pass */
1122 fi = &tls->frames [findex];
1123 frame_start = stack_start + fi->frame_start_offset;
1125 DEBUG (char *fname = mono_method_full_name (jinfo_get_method (fi->ji), TRUE); fprintf (logfile, "Mark(1): %s\n", fname); g_free (fname));
1128 * FIXME: Add a function to mark using a bitmap, to avoid doing a
1129 * call for each object.
1132 /* Mark stack slots */
1134 guint8 *ref_bitmap = fi->bitmap;
1137 for (i = 0; i < fi->nslots; ++i) {
1138 MonoObject **ptr = (MonoObject**)(frame_start + (i * SIZEOF_SLOT));
1140 live = ref_bitmap [i / 8] & (1 << (i % 8));
1143 MonoObject *obj = *ptr;
1145 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1146 *ptr = mono_gc_scan_object (obj, gc_data);
1147 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1149 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1154 * This is disabled because the pointer takes up a lot of space.
1155 * Stack slots might be shared between ref and non-ref variables ?
1157 if (map->ref_slots [i / 8] & (1 << (i % 8))) {
1158 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));
1160 * Fail fast if the live range is incorrect, and
1161 * the JITted code tries to access this object
1170 /* Mark registers */
1173 * Registers are different from stack slots, they have no address where they
1174 * are stored. Instead, some frame below this frame in the stack saves them
1175 * in its prolog to the stack. We can mark this location precisely.
1177 for (i = 0; i < fi->nreg_locations; ++i) {
1179 * reg_locations [i] contains the address of the stack slot where
1180 * a reg was last saved, so mark that slot.
1182 MonoObject **ptr = (MonoObject**)((guint8*)stack_start + fi->reg_locations [i]);
1183 MonoObject *obj = *ptr;
1186 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj));
1187 *ptr = mono_gc_scan_object (obj, gc_data);
1188 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1190 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p\n", mono_arch_regname (fi->regs [i]), ptr, obj));
1196 * Debugging aid to check for missed refs.
1198 if (tls->ref_to_track) {
1201 for (p = (mgreg_t*)stack_start; p < (mgreg_t*)stack_end; ++p)
1202 if (*p == (mgreg_t)tls->ref_to_track)
1203 printf ("REF AT %p.\n", p);
1210 * This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
1211 * call, and TRUE at the second. USER_DATA points to a TlsData
1212 * structure filled up by thread_suspend_func.
1215 thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise, void *gc_data)
1217 TlsData *tls = user_data;
1219 DEBUG (fprintf (logfile, "****************************************\n"));
1220 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));
1221 DEBUG (fprintf (logfile, "****************************************\n"));
1224 conservative_pass (tls, stack_start, stack_end);
1226 precise_pass (tls, stack_start, stack_end, gc_data);
1232 mini_gc_init_gc_map (MonoCompile *cfg)
1234 if (COMPILE_LLVM (cfg))
1237 if (!mono_gc_is_moving ())
1240 if (cfg->compile_aot) {
1241 if (!enable_gc_maps_for_aot)
1243 } else if (!mono_gc_precise_stack_mark_enabled ())
1247 /* Debugging support */
1249 static int precise_count;
1252 if (g_getenv ("MONO_GCMAP_COUNT")) {
1253 if (precise_count == atoi (g_getenv ("MONO_GCMAP_COUNT")))
1254 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1255 if (precise_count > atoi (g_getenv ("MONO_GCMAP_COUNT")))
1261 cfg->compute_gc_maps = TRUE;
1263 cfg->gc_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoCompileGC));
1267 * mini_gc_set_slot_type_from_fp:
1269 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1270 * relative to the frame pointer. By default, all stack slots are type PIN, so there is no
1271 * need to call this function for those slots.
1274 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
1276 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1278 if (!cfg->compute_gc_maps)
1281 g_assert (slot_offset % SIZEOF_SLOT == 0);
1283 gcfg->stack_slots_from_fp = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_fp, GINT_TO_POINTER (((slot_offset) << 16) | type));
1287 * mini_gc_set_slot_type_from_cfa:
1289 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1290 * relative to the DWARF CFA value. This should be called from mono_arch_emit_prolog ().
1291 * If type is STACK_REF, the slot is assumed to be live from the end of the prolog until
1292 * the end of the method. By default, all stack slots are type PIN, so there is no need to
1293 * call this function for those slots.
1296 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
1298 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1299 int slot = - (slot_offset / SIZEOF_SLOT);
1301 if (!cfg->compute_gc_maps)
1304 g_assert (slot_offset <= 0);
1305 g_assert (slot_offset % SIZEOF_SLOT == 0);
1307 gcfg->stack_slots_from_cfa = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_cfa, GUINT_TO_POINTER (((slot) << 16) | type));
1311 fp_offset_to_slot (MonoCompile *cfg, int offset)
1313 MonoCompileGC *gcfg = cfg->gc_info;
1315 return (offset - gcfg->min_offset) / SIZEOF_SLOT;
1319 slot_to_fp_offset (MonoCompile *cfg, int slot)
1321 MonoCompileGC *gcfg = cfg->gc_info;
1323 return (slot * SIZEOF_SLOT) + gcfg->min_offset;
1326 static inline MONO_ALWAYS_INLINE void
1327 set_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1329 g_assert (slot >= 0 && slot < gcfg->nslots);
1331 if (type == SLOT_PIN) {
1332 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1333 set_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1334 } else if (type == SLOT_REF) {
1335 set_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1336 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1337 } else if (type == SLOT_NOREF) {
1338 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1339 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1344 set_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1347 guint8 *ref_bitmap, *pin_bitmap;
1352 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1353 set_slot (gcfg, slot, cindex, type);
1355 ref_bitmap = gcfg->stack_ref_bitmap;
1356 pin_bitmap = gcfg->stack_pin_bitmap;
1357 width = gcfg->stack_bitmap_width;
1360 if (type == SLOT_PIN) {
1361 memset (ref_bitmap + pos, 0, width);
1362 memset (pin_bitmap + pos, 0xff, width);
1363 } else if (type == SLOT_REF) {
1364 memset (ref_bitmap + pos, 0xff, width);
1365 memset (pin_bitmap + pos, 0, width);
1366 } else if (type == SLOT_NOREF) {
1367 memset (ref_bitmap + pos, 0, width);
1368 memset (pin_bitmap + pos, 0, width);
1373 set_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1377 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1378 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1379 if (callsite_offset >= from && callsite_offset < to)
1380 set_slot (gcfg, slot, cindex, type);
1385 set_reg_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1387 g_assert (slot >= 0 && slot < gcfg->nregs);
1389 if (type == SLOT_PIN) {
1390 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1391 set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1392 } else if (type == SLOT_REF) {
1393 set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1394 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1395 } else if (type == SLOT_NOREF) {
1396 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1397 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1402 set_reg_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1406 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1407 set_reg_slot (gcfg, slot, cindex, type);
1411 set_reg_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1415 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1416 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1417 if (callsite_offset >= from && callsite_offset < to)
1418 set_reg_slot (gcfg, slot, cindex, type);
1423 process_spill_slots (MonoCompile *cfg)
1425 MonoCompileGC *gcfg = cfg->gc_info;
1430 /* Mark all ref/pin spill slots as NOREF by default outside of their live range */
1431 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1432 for (l = bb->spill_slot_defs; l; l = l->next) {
1433 MonoInst *def = l->data;
1434 int spill_slot = def->inst_c0;
1435 int bank = def->inst_c1;
1436 int offset = cfg->spill_info [bank][spill_slot].offset;
1437 int slot = fp_offset_to_slot (cfg, offset);
1439 if (bank == MONO_REG_INT_MP || bank == MONO_REG_INT_REF)
1440 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1444 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1445 for (l = bb->spill_slot_defs; l; l = l->next) {
1446 MonoInst *def = l->data;
1447 int spill_slot = def->inst_c0;
1448 int bank = def->inst_c1;
1449 int offset = cfg->spill_info [bank][spill_slot].offset;
1450 int slot = fp_offset_to_slot (cfg, offset);
1453 if (bank == MONO_REG_INT_MP)
1459 * Extend the live interval for the GC tracked spill slots
1460 * defined in this bblock.
1461 * FIXME: This is not needed.
1463 set_slot_in_range (gcfg, slot, def->backend.pc_offset, bb->native_offset + bb->native_length, type);
1465 if (cfg->verbose_level > 1)
1466 printf ("\t%s spill slot at %s0x%x(fp) (slot = %d)\n", slot_type_to_string (type), offset >= 0 ? "" : "-", ABS (offset), slot);
1470 /* Set fp spill slots to NOREF */
1471 for (i = 0; i < cfg->spill_info_len [MONO_REG_DOUBLE]; ++i) {
1472 int offset = cfg->spill_info [MONO_REG_DOUBLE][i].offset;
1478 slot = fp_offset_to_slot (cfg, offset);
1480 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1482 if (cfg->verbose_level > 1)
1483 printf ("\tfp spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1486 /* Set int spill slots to NOREF */
1487 for (i = 0; i < cfg->spill_info_len [MONO_REG_INT]; ++i) {
1488 int offset = cfg->spill_info [MONO_REG_INT][i].offset;
1494 slot = fp_offset_to_slot (cfg, offset);
1496 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1497 if (cfg->verbose_level > 1)
1498 printf ("\tint spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1503 * process_other_slots:
1505 * Process stack slots registered using mini_gc_set_slot_type_... ().
1508 process_other_slots (MonoCompile *cfg)
1510 MonoCompileGC *gcfg = cfg->gc_info;
1513 /* Relative to the CFA */
1514 for (l = gcfg->stack_slots_from_cfa; l; l = l->next) {
1515 guint data = GPOINTER_TO_UINT (l->data);
1516 int cfa_slot = data >> 16;
1517 GCSlotType type = data & 0xff;
1521 * Map the cfa relative slot to an fp relative slot.
1522 * slot_addr == cfa - <cfa_slot>*4/8
1523 * fp + cfa_offset == cfa
1524 * -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
1526 slot = (cfg->cfa_offset / SIZEOF_SLOT) - cfa_slot - (gcfg->min_offset / SIZEOF_SLOT);
1528 set_slot_everywhere (gcfg, slot, type);
1530 if (cfg->verbose_level > 1) {
1531 int fp_offset = slot_to_fp_offset (cfg, slot);
1532 if (type == SLOT_NOREF)
1533 printf ("\tnoref slot at %s0x%x(fp) (slot = %d) (cfa - 0x%x)\n", fp_offset >= 0 ? "" : "-", ABS (fp_offset), slot, (int)(cfa_slot * SIZEOF_SLOT));
1537 /* Relative to the FP */
1538 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
1539 gint data = GPOINTER_TO_INT (l->data);
1540 int offset = data >> 16;
1541 GCSlotType type = data & 0xff;
1544 slot = fp_offset_to_slot (cfg, offset);
1546 set_slot_everywhere (gcfg, slot, type);
1548 /* Liveness for these slots is handled by process_spill_slots () */
1550 if (cfg->verbose_level > 1) {
1551 if (type == SLOT_REF)
1552 printf ("\tref slot at fp+0x%x (slot = %d)\n", offset, slot);
1553 else if (type == SLOT_NOREF)
1554 printf ("\tnoref slot at 0x%x(fp) (slot = %d)\n", offset, slot);
1560 get_vtype_bitmap (MonoType *t, int *numbits)
1562 MonoClass *klass = mono_class_from_mono_type (t);
1564 if (klass->generic_container || mono_class_is_open_constructed_type (t)) {
1565 /* FIXME: Generic sharing */
1568 mono_class_compute_gc_descriptor (klass);
1570 return mono_gc_get_bitmap_for_descr (klass->gc_descr, numbits);
1574 static inline const char*
1575 get_offset_sign (int offset)
1577 return offset < 0 ? "-" : "+";
1581 get_offset_val (int offset)
1583 return offset < 0 ? (- offset) : offset;
1587 process_variables (MonoCompile *cfg)
1589 MonoCompileGC *gcfg = cfg->gc_info;
1590 MonoMethodSignature *sig = mono_method_signature (cfg->method);
1591 int i, locals_min_slot, locals_max_slot, cindex;
1595 int locals_min_offset = gcfg->locals_min_offset;
1596 int locals_max_offset = gcfg->locals_max_offset;
1598 /* Slots for locals are NOREF by default */
1599 locals_min_slot = (locals_min_offset - gcfg->min_offset) / SIZEOF_SLOT;
1600 locals_max_slot = (locals_max_offset - gcfg->min_offset) / SIZEOF_SLOT;
1601 for (i = locals_min_slot; i < locals_max_slot; ++i) {
1602 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1606 * Compute the offset where variables are initialized in the first bblock, if any.
1608 pc_offsets = g_new0 (int, cfg->next_vreg);
1610 bb = cfg->bb_entry->next_bb;
1611 MONO_BB_FOR_EACH_INS (bb, tmp) {
1612 if (tmp->opcode == OP_GC_LIVENESS_DEF) {
1613 int vreg = tmp->inst_c1;
1614 if (pc_offsets [vreg] == 0) {
1615 g_assert (tmp->backend.pc_offset > 0);
1616 pc_offsets [vreg] = tmp->backend.pc_offset;
1622 * Stack slots holding arguments are initialized in the prolog.
1623 * This means we can treat them alive for the whole method.
1625 for (i = 0; i < cfg->num_varinfo; i++) {
1626 MonoInst *ins = cfg->varinfo [i];
1627 MonoType *t = ins->inst_vtype;
1630 gboolean byref, is_this = FALSE;
1631 gboolean is_arg = i < cfg->locals_start;
1633 if (ins == cfg->ret) {
1634 if (!(ins->opcode == OP_REGOFFSET && MONO_TYPE_ISSTRUCT (t)))
1638 vmv = MONO_VARINFO (cfg, i);
1640 /* For some reason, 'this' is byref */
1641 if (sig->hasthis && ins == cfg->args [0] && !cfg->method->klass->valuetype) {
1642 t = &cfg->method->klass->byval_arg;
1648 if (ins->opcode == OP_REGVAR) {
1650 GCSlotType slot_type;
1652 t = mini_type_get_underlying_type (NULL, t);
1655 g_assert (hreg < MONO_MAX_IREGS);
1658 slot_type = SLOT_PIN;
1660 slot_type = mini_type_is_reference (cfg, t) ? SLOT_REF : SLOT_NOREF;
1662 if (slot_type == SLOT_PIN) {
1663 /* These have no live interval, be conservative */
1664 set_reg_slot_everywhere (gcfg, hreg, slot_type);
1667 * Unlike variables allocated to the stack, we generate liveness info
1668 * for noref vars in registers in mono_spill_global_vars (), because
1669 * knowing that a register doesn't contain a ref allows us to mark its save
1670 * locations precisely.
1672 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1673 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1674 set_reg_slot (gcfg, hreg, cindex, slot_type);
1677 if (cfg->verbose_level > 1) {
1678 printf ("\t%s %sreg %s(R%d)\n", slot_type_to_string (slot_type), is_arg ? "arg " : "", mono_arch_regname (hreg), vmv->vreg);
1684 if (ins->opcode != OP_REGOFFSET)
1687 if (ins->inst_offset % SIZEOF_SLOT != 0)
1690 pos = fp_offset_to_slot (cfg, ins->inst_offset);
1692 if (is_arg && ins->flags & MONO_INST_IS_DEAD) {
1693 /* These do not get stored in the prolog */
1694 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1696 if (cfg->verbose_level > 1) {
1697 printf ("\tdead arg at fp%s0x%x (slot = %d): %s\n", get_offset_sign (ins->inst_offset), get_offset_val (ins->inst_offset), pos, mono_type_full_name (ins->inst_vtype));
1702 if (MONO_TYPE_ISSTRUCT (t)) {
1704 gsize *bitmap = NULL;
1705 gboolean pin = FALSE;
1709 if (ins->backend.is_pinvoke)
1710 size = mono_class_native_size (ins->klass, NULL);
1712 size = mono_class_value_size (ins->klass, NULL);
1713 size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
1715 if (cfg->verbose_level > 1)
1716 printf ("\tvtype R%d at %s0x%x(fp)-%s0x%x(fp) (slot %d-%d): %s\n", vmv->vreg, get_offset_sign (ins->inst_offset), get_offset_val (ins->inst_offset), get_offset_sign (ins->inst_offset), get_offset_val (ins->inst_offset + (size_in_slots * SIZEOF_SLOT)), pos, pos + size_in_slots, mono_type_full_name (ins->inst_vtype));
1718 if (!ins->klass->has_references) {
1720 for (j = 0; j < size_in_slots; ++j)
1721 set_slot_everywhere (gcfg, pos + j, SLOT_NOREF);
1726 bitmap = get_vtype_bitmap (t, &numbits);
1731 * Most vtypes are marked volatile because of the LDADDR instructions,
1732 * and they have no liveness information since they are decomposed
1733 * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
1734 * them during VZERO decomposition.
1737 if (!pc_offsets [vmv->vreg])
1740 if (ins->backend.is_pinvoke)
1745 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1746 if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
1747 for (j = 0; j < numbits; ++j) {
1748 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
1749 /* The descriptor is for the boxed object */
1750 set_slot (gcfg, (pos + j - (sizeof (MonoObject) / SIZEOF_SLOT)), cindex, pin ? SLOT_PIN : SLOT_REF);
1756 if (cfg->verbose_level > 1) {
1757 for (j = 0; j < numbits; ++j) {
1758 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD)))
1759 printf ("\t\t%s slot at 0x%x(fp) (slot = %d)\n", pin ? "pin" : "ref", (int)(ins->inst_offset + (j * SIZEOF_SLOT)), (int)(pos + j - (sizeof (MonoObject) / SIZEOF_SLOT)));
1763 if (cfg->verbose_level > 1)
1764 printf ("\t\tpinned\n");
1765 for (j = 0; j < size_in_slots; ++j) {
1766 set_slot_everywhere (gcfg, pos + j, SLOT_PIN);
1775 if (!is_arg && (ins->inst_offset < gcfg->min_offset || ins->inst_offset >= gcfg->max_offset))
1776 /* Vret addr etc. */
1781 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1783 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1784 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1785 set_slot (gcfg, pos, cindex, SLOT_PIN);
1787 if (cfg->verbose_level > 1)
1788 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));
1793 * This is currently disabled, but could be enabled to debug crashes.
1796 if (t->type == MONO_TYPE_I) {
1798 * Variables created in mono_handle_global_vregs have type I, but they
1799 * could hold GC refs since the vregs they were created from might not been
1800 * marked as holding a GC ref. So be conservative.
1802 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1807 t = mini_type_get_underlying_type (NULL, t);
1809 if (!mini_type_is_reference (cfg, t)) {
1810 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1811 if (cfg->verbose_level > 1)
1812 printf ("\tnoref%s at %s0x%x(fp) (R%d, slot = %d): %s\n", (is_arg ? " arg" : ""), 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));
1813 if (!t->byref && sizeof (mgreg_t) == 4 && (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_R8)) {
1814 set_slot_everywhere (gcfg, pos + 1, SLOT_NOREF);
1815 if (cfg->verbose_level > 1)
1816 printf ("\tnoref at %s0x%x(fp) (R%d, slot = %d): %s\n", ins->inst_offset < 0 ? "-" : "", (ins->inst_offset < 0) ? -(int)(ins->inst_offset + 4) : (int)ins->inst_offset + 4, vmv->vreg, pos + 1, mono_type_full_name (ins->inst_vtype));
1821 /* 'this' is marked INDIRECT for gshared methods */
1822 if (ins->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT) && !is_this) {
1824 * For volatile variables, treat them alive from the point they are
1825 * initialized in the first bblock until the end of the method.
1828 set_slot_everywhere (gcfg, pos, SLOT_REF);
1829 } else if (pc_offsets [vmv->vreg]) {
1830 set_slot_in_range (gcfg, pos, 0, pc_offsets [vmv->vreg], SLOT_PIN);
1831 set_slot_in_range (gcfg, pos, pc_offsets [vmv->vreg], cfg->code_size, SLOT_REF);
1833 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1835 if (cfg->verbose_level > 1)
1836 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));
1841 /* Live for the whole method */
1842 set_slot_everywhere (gcfg, pos, SLOT_REF);
1844 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1845 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1846 set_slot (gcfg, pos, cindex, SLOT_REF);
1849 if (cfg->verbose_level > 1) {
1850 printf ("\tref%s at %s0x%x(fp) (R%d, slot = %d): %s\n", (is_arg ? " arg" : ""), 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));
1854 g_free (pc_offsets);
1858 sp_offset_to_fp_offset (MonoCompile *cfg, int sp_offset)
1861 * Convert a sp relative offset to a slot index. This is
1862 * platform specific.
1865 /* fp = sp + offset */
1866 g_assert (cfg->frame_reg == AMD64_RBP);
1867 return (- cfg->arch.sp_fp_offset + sp_offset);
1868 #elif defined(TARGET_X86)
1869 /* The offset is computed from the sp at the start of the call sequence */
1870 g_assert (cfg->frame_reg == X86_EBP);
1871 #ifdef MONO_X86_NO_PUSHES
1872 return (- cfg->arch.sp_fp_offset + sp_offset);
1874 return (- cfg->arch.sp_fp_offset - sp_offset);
1883 process_param_area_slots (MonoCompile *cfg)
1885 MonoCompileGC *gcfg = cfg->gc_info;
1890 * These slots are used for passing parameters during calls. They are sp relative, not
1891 * fp relative, so they are harder to handle.
1893 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1894 /* The distance between fp and sp is not constant */
1897 is_param = mono_mempool_alloc0 (cfg->mempool, gcfg->nslots * sizeof (gboolean));
1899 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1900 GCCallSite *callsite = gcfg->callsites [cindex];
1903 for (l = callsite->param_slots; l; l = l->next) {
1904 MonoInst *def = l->data;
1905 MonoType *t = def->inst_vtype;
1906 int sp_offset = def->inst_offset;
1907 int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1908 int slot = fp_offset_to_slot (cfg, fp_offset);
1912 if (MONO_TYPE_ISSTRUCT (t)) {
1913 size = mini_type_stack_size_full (cfg->generic_sharing_context, t, &align, FALSE);
1915 size = sizeof (mgreg_t);
1918 for (i = 0; i < size / sizeof (mgreg_t); ++i) {
1919 g_assert (slot + i >= 0 && slot + i < gcfg->nslots);
1920 is_param [slot + i] = TRUE;
1925 /* All param area slots are noref by default */
1926 for (i = 0; i < gcfg->nslots; ++i) {
1928 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1932 * We treat param area slots as being part of the callee's frame, to be able to handle tail calls which overwrite
1933 * the argument area of the caller.
1938 process_finally_clauses (MonoCompile *cfg)
1940 MonoCompileGC *gcfg = cfg->gc_info;
1941 GCCallSite **callsites;
1943 gboolean has_finally;
1944 int i, j, nslots, nregs;
1946 ncallsites = gcfg->ncallsites;
1947 nslots = gcfg->nslots;
1948 nregs = gcfg->nregs;
1949 callsites = gcfg->callsites;
1952 * The calls to the finally clauses don't show up in the cfg. See
1953 * test_0_liveness_8 ().
1954 * Variables accessed inside the finally clause are already marked VOLATILE by
1955 * mono_liveness_handle_exception_clauses (). Variables not accessed inside the finally clause have
1956 * correct liveness outside the finally clause. So mark them PIN inside the finally clauses.
1958 has_finally = FALSE;
1959 for (i = 0; i < cfg->header->num_clauses; ++i) {
1960 MonoExceptionClause *clause = &cfg->header->clauses [i];
1962 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1967 if (cfg->verbose_level > 1)
1968 printf ("\tMethod has finally clauses, pessimizing live ranges.\n");
1969 for (j = 0; j < ncallsites; ++j) {
1970 MonoBasicBlock *bb = callsites [j]->bb;
1971 MonoExceptionClause *clause;
1972 gboolean is_in_finally = FALSE;
1974 for (i = 0; i < cfg->header->num_clauses; ++i) {
1975 clause = &cfg->header->clauses [i];
1977 if (MONO_OFFSET_IN_HANDLER (clause, bb->real_offset)) {
1978 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1979 is_in_finally = TRUE;
1985 if (is_in_finally) {
1986 for (i = 0; i < nslots; ++i)
1987 set_slot (gcfg, i, j, SLOT_PIN);
1988 for (i = 0; i < nregs; ++i)
1989 set_reg_slot (gcfg, i, j, SLOT_PIN);
1996 compute_frame_size (MonoCompile *cfg)
1998 int i, locals_min_offset, locals_max_offset, cfa_min_offset, cfa_max_offset;
1999 int min_offset, max_offset;
2000 MonoCompileGC *gcfg = cfg->gc_info;
2001 MonoMethodSignature *sig = mono_method_signature (cfg->method);
2004 /* Compute min/max offsets from the fp */
2007 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_S390X)
2008 locals_min_offset = ALIGN_TO (cfg->locals_min_stack_offset, SIZEOF_SLOT);
2009 locals_max_offset = cfg->locals_max_stack_offset;
2011 /* min/max stack offset needs to be computed in mono_arch_allocate_vars () */
2015 locals_min_offset = ALIGN_TO (locals_min_offset, SIZEOF_SLOT);
2016 locals_max_offset = ALIGN_TO (locals_max_offset, SIZEOF_SLOT);
2018 min_offset = locals_min_offset;
2019 max_offset = locals_max_offset;
2022 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2023 MonoInst *ins = cfg->args [i];
2025 if (ins->opcode == OP_REGOFFSET) {
2026 int size, size_in_slots;
2027 size = mini_type_stack_size_full (cfg->generic_sharing_context, ins->inst_vtype, NULL, ins->backend.is_pinvoke);
2028 size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
2030 min_offset = MIN (min_offset, ins->inst_offset);
2031 max_offset = MAX ((int)max_offset, (int)(ins->inst_offset + (size_in_slots * SIZEOF_SLOT)));
2036 g_assert (cfg->frame_reg == cfg->cfa_reg);
2037 g_assert (cfg->cfa_offset > 0);
2039 cfa_max_offset = cfg->cfa_offset;
2041 min_offset = MIN (min_offset, cfa_min_offset);
2042 max_offset = MAX (max_offset, cfa_max_offset);
2044 /* Fp relative slots */
2045 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
2046 gint data = GPOINTER_TO_INT (l->data);
2047 int offset = data >> 16;
2049 min_offset = MIN (min_offset, offset);
2053 if (!(cfg->flags & MONO_CFG_HAS_SPILLUP)) {
2054 int stack_offset = ALIGN_TO (cfg->stack_offset, SIZEOF_SLOT);
2055 min_offset = MIN (min_offset, (-stack_offset));
2058 /* Param area slots */
2060 min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
2061 #elif defined(TARGET_X86)
2062 #ifdef MONO_X86_NO_PUSHES
2063 min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
2065 min_offset = MIN (min_offset, - (cfg->arch.sp_fp_offset + cfg->arch.param_area_size));
2067 #elif defined(TARGET_ARM)
2069 #elif defined(TARGET_s390X)
2075 gcfg->min_offset = min_offset;
2076 gcfg->max_offset = max_offset;
2077 gcfg->locals_min_offset = locals_min_offset;
2078 gcfg->locals_max_offset = locals_max_offset;
2082 init_gcfg (MonoCompile *cfg)
2084 int i, nregs, nslots;
2085 MonoCompileGC *gcfg = cfg->gc_info;
2086 GCCallSite **callsites;
2095 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2096 ncallsites += g_slist_length (bb->gc_callsites);
2098 callsites = mono_mempool_alloc0 (cfg->mempool, ncallsites * sizeof (GCCallSite*));
2100 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2101 for (l = bb->gc_callsites; l; l = l->next)
2102 callsites [i++] = l->data;
2105 /* The callsites should already be ordered by pc offset */
2106 for (i = 1; i < ncallsites; ++i)
2107 g_assert (callsites [i - 1]->pc_offset < callsites [i]->pc_offset);
2110 * The stack frame looks like this:
2112 * <fp + max_offset> == cfa -> <end of previous frame>
2113 * <other stack slots>
2115 * <other stack slots>
2116 * fp + min_offset ->
2121 if (cfg->verbose_level > 1)
2122 printf ("GC Map for %s: 0x%x-0x%x\n", mono_method_full_name (cfg->method, TRUE), gcfg->min_offset, gcfg->max_offset);
2124 nslots = (gcfg->max_offset - gcfg->min_offset) / SIZEOF_SLOT;
2127 gcfg->nslots = nslots;
2128 gcfg->nregs = nregs;
2129 gcfg->callsites = callsites;
2130 gcfg->ncallsites = ncallsites;
2131 gcfg->stack_bitmap_width = ALIGN_TO (ncallsites, 8) / 8;
2132 gcfg->reg_bitmap_width = ALIGN_TO (ncallsites, 8) / 8;
2133 gcfg->stack_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * nslots);
2134 gcfg->stack_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * nslots);
2135 gcfg->reg_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * nregs);
2136 gcfg->reg_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * nregs);
2138 /* All slots start out as PIN */
2139 memset (gcfg->stack_pin_bitmap, 0xff, gcfg->stack_bitmap_width * nregs);
2140 for (i = 0; i < nregs; ++i) {
2142 * By default, registers are NOREF.
2143 * It is possible for a callee to save them before being defined in this method,
2144 * but the saved value is dead too, so it doesn't need to be marked.
2146 if ((cfg->used_int_regs & (1 << i)))
2147 set_reg_slot_everywhere (gcfg, i, SLOT_NOREF);
2151 static inline gboolean
2152 has_bit_set (guint8 *bitmap, int width, int slot)
2155 int pos = width * slot;
2157 for (i = 0; i < width; ++i) {
2158 if (bitmap [pos + i])
2165 create_map (MonoCompile *cfg)
2168 int i, j, nregs, nslots, nref_regs, npin_regs, alloc_size, bitmaps_size, bitmaps_offset;
2170 int stack_bitmap_width, stack_bitmap_size, reg_ref_bitmap_width, reg_ref_bitmap_size;
2171 int reg_pin_bitmap_width, reg_pin_bitmap_size, bindex;
2173 gboolean has_ref_slots, has_pin_slots, has_ref_regs, has_pin_regs;
2174 MonoCompileGC *gcfg = cfg->gc_info;
2175 GCCallSite **callsites;
2177 guint8 *bitmap, *bitmaps;
2178 guint32 reg_ref_mask, reg_pin_mask;
2180 ncallsites = gcfg->ncallsites;
2181 nslots = gcfg->nslots;
2182 nregs = gcfg->nregs;
2183 callsites = gcfg->callsites;
2186 * Compute the real size of the bitmap i.e. ignore NOREF columns at the beginning and at
2187 * the end. Also, compute whenever the map needs ref/pin bitmaps, and collect stats.
2189 has_ref_slots = FALSE;
2190 has_pin_slots = FALSE;
2193 memset (ntypes, 0, sizeof (ntypes));
2194 for (i = 0; i < nslots; ++i) {
2195 gboolean has_ref = FALSE;
2196 gboolean has_pin = FALSE;
2198 if (has_bit_set (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, i))
2200 if (has_bit_set (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, i))
2204 has_ref_slots = TRUE;
2206 has_pin_slots = TRUE;
2209 ntypes [SLOT_REF] ++;
2211 ntypes [SLOT_PIN] ++;
2213 ntypes [SLOT_NOREF] ++;
2215 if (has_ref || has_pin) {
2222 start = end = nslots;
2224 g_assert (start != -1);
2225 g_assert (start < end);
2228 has_ref_regs = FALSE;
2229 has_pin_regs = FALSE;
2234 for (i = 0; i < nregs; ++i) {
2235 gboolean has_ref = FALSE;
2236 gboolean has_pin = FALSE;
2238 if (!(cfg->used_int_regs & (1 << i)))
2241 if (has_bit_set (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, i))
2243 if (has_bit_set (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, i))
2247 reg_ref_mask |= (1 << i);
2248 has_ref_regs = TRUE;
2252 reg_pin_mask |= (1 << i);
2253 has_pin_regs = TRUE;
2258 if (cfg->verbose_level > 1)
2259 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);
2261 /* Create the GC Map */
2263 /* The work bitmaps have one row for each slot, since this is how we access them during construction */
2264 stack_bitmap_width = ALIGN_TO (end - start, 8) / 8;
2265 stack_bitmap_size = stack_bitmap_width * ncallsites;
2266 reg_ref_bitmap_width = ALIGN_TO (nref_regs, 8) / 8;
2267 reg_ref_bitmap_size = reg_ref_bitmap_width * ncallsites;
2268 reg_pin_bitmap_width = ALIGN_TO (npin_regs, 8) / 8;
2269 reg_pin_bitmap_size = reg_pin_bitmap_width * ncallsites;
2270 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);
2272 map = mono_mempool_alloc0 (cfg->mempool, sizeof (GCMap));
2274 map->frame_reg = cfg->frame_reg;
2275 map->start_offset = gcfg->min_offset;
2276 map->end_offset = gcfg->min_offset + (nslots * SIZEOF_SLOT);
2277 map->map_offset = start * SIZEOF_SLOT;
2278 map->nslots = end - start;
2279 map->has_ref_slots = has_ref_slots;
2280 map->has_pin_slots = has_pin_slots;
2281 map->has_ref_regs = has_ref_regs;
2282 map->has_pin_regs = has_pin_regs;
2283 g_assert (nregs < 32);
2284 map->used_int_regs = cfg->used_int_regs;
2285 map->reg_ref_mask = reg_ref_mask;
2286 map->reg_pin_mask = reg_pin_mask;
2287 map->nref_regs = nref_regs;
2288 map->npin_regs = npin_regs;
2290 bitmaps = mono_mempool_alloc0 (cfg->mempool, bitmaps_size);
2293 if (has_ref_slots) {
2294 map->stack_ref_bitmap_offset = bitmaps_offset;
2295 bitmaps_offset += stack_bitmap_size;
2297 bitmap = &bitmaps [map->stack_ref_bitmap_offset];
2298 for (i = 0; i < nslots; ++i) {
2299 for (j = 0; j < ncallsites; ++j) {
2300 if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, i, j))
2301 set_bit (bitmap, stack_bitmap_width, j, i - start);
2305 if (has_pin_slots) {
2306 map->stack_pin_bitmap_offset = bitmaps_offset;
2307 bitmaps_offset += stack_bitmap_size;
2309 bitmap = &bitmaps [map->stack_pin_bitmap_offset];
2310 for (i = 0; i < nslots; ++i) {
2311 for (j = 0; j < ncallsites; ++j) {
2312 if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, i, j))
2313 set_bit (bitmap, stack_bitmap_width, j, i - start);
2318 map->reg_ref_bitmap_offset = bitmaps_offset;
2319 bitmaps_offset += reg_ref_bitmap_size;
2321 bitmap = &bitmaps [map->reg_ref_bitmap_offset];
2323 for (i = 0; i < nregs; ++i) {
2324 if (reg_ref_mask & (1 << i)) {
2325 for (j = 0; j < ncallsites; ++j) {
2326 if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, i, j))
2327 set_bit (bitmap, reg_ref_bitmap_width, j, bindex);
2334 map->reg_pin_bitmap_offset = bitmaps_offset;
2335 bitmaps_offset += reg_pin_bitmap_size;
2337 bitmap = &bitmaps [map->reg_pin_bitmap_offset];
2339 for (i = 0; i < nregs; ++i) {
2340 if (reg_pin_mask & (1 << i)) {
2341 for (j = 0; j < ncallsites; ++j) {
2342 if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, i, j))
2343 set_bit (bitmap, reg_pin_bitmap_width, j, bindex);
2351 map->ncallsites = ncallsites;
2352 if (cfg->code_len < 256)
2353 map->callsite_entry_size = 1;
2354 else if (cfg->code_len < 65536)
2355 map->callsite_entry_size = 2;
2357 map->callsite_entry_size = 4;
2359 /* Encode the GC Map */
2367 encode_gc_map (map, buf, &endbuf);
2368 g_assert (endbuf - buf < 256);
2370 encoded_size = endbuf - buf;
2371 alloc_size = sizeof (GCEncodedMap) + ALIGN_TO (encoded_size, map->callsite_entry_size) + (map->callsite_entry_size * map->ncallsites) + bitmaps_size;
2373 emap = mono_domain_alloc0 (cfg->domain, alloc_size);
2374 //emap->ref_slots = map->ref_slots;
2376 /* Encoded fixed fields */
2377 p = &emap->encoded [0];
2378 //emap->encoded_size = encoded_size;
2379 memcpy (p, buf, encoded_size);
2382 /* Callsite table */
2383 p = (guint8*)ALIGN_TO ((mgreg_t)p, map->callsite_entry_size);
2384 if (map->callsite_entry_size == 1) {
2385 guint8 *offsets = p;
2386 for (i = 0; i < ncallsites; ++i)
2387 offsets [i] = callsites [i]->pc_offset;
2388 stats.gc_callsites8_size += ncallsites * sizeof (guint8);
2389 } else if (map->callsite_entry_size == 2) {
2390 guint16 *offsets = (guint16*)p;
2391 for (i = 0; i < ncallsites; ++i)
2392 offsets [i] = callsites [i]->pc_offset;
2393 stats.gc_callsites16_size += ncallsites * sizeof (guint16);
2395 guint32 *offsets = (guint32*)p;
2396 for (i = 0; i < ncallsites; ++i)
2397 offsets [i] = callsites [i]->pc_offset;
2398 stats.gc_callsites32_size += ncallsites * sizeof (guint32);
2400 p += ncallsites * map->callsite_entry_size;
2403 memcpy (p, bitmaps, bitmaps_size);
2406 g_assert ((guint8*)p - (guint8*)emap <= alloc_size);
2408 stats.gc_maps_size += alloc_size;
2409 stats.gc_callsites_size += ncallsites * map->callsite_entry_size;
2410 stats.gc_bitmaps_size += bitmaps_size;
2411 stats.gc_map_struct_size += sizeof (GCEncodedMap) + encoded_size;
2413 cfg->jit_info->gc_info = emap;
2415 cfg->gc_map = (guint8*)emap;
2416 cfg->gc_map_size = alloc_size;
2419 stats.all_slots += nslots;
2420 stats.ref_slots += ntypes [SLOT_REF];
2421 stats.noref_slots += ntypes [SLOT_NOREF];
2422 stats.pin_slots += ntypes [SLOT_PIN];
2426 mini_gc_create_gc_map (MonoCompile *cfg)
2428 if (!cfg->compute_gc_maps)
2432 * During marking, all frames except the top frame are at a call site, and we mark the
2433 * top frame conservatively. This means that we only need to compute and record
2434 * GC maps for call sites.
2437 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
2438 /* Without liveness info, the live ranges are not precise enough */
2441 mono_analyze_liveness_gc (cfg);
2443 compute_frame_size (cfg);
2447 process_spill_slots (cfg);
2448 process_other_slots (cfg);
2449 process_param_area_slots (cfg);
2450 process_variables (cfg);
2451 process_finally_clauses (cfg);
2456 #endif /* DISABLE_JIT */
2459 parse_debug_options (void)
2464 env = g_getenv ("MONO_GCMAP_DEBUG");
2468 opts = g_strsplit (env, ",", -1);
2469 for (ptr = opts; ptr && *ptr; ptr ++) {
2470 /* No options yet */
2471 fprintf (stderr, "Invalid format for the MONO_GCMAP_DEBUG env variable: '%s'\n", env);
2482 memset (&cb, 0, sizeof (cb));
2483 cb.thread_attach_func = thread_attach_func;
2484 cb.thread_detach_func = thread_detach_func;
2485 cb.thread_suspend_func = thread_suspend_func;
2486 /* Comment this out to disable precise stack marking */
2487 cb.thread_mark_func = thread_mark_func;
2488 mono_gc_set_gc_callbacks (&cb);
2490 logfile = mono_gc_get_logfile ();
2492 parse_debug_options ();
2494 mono_counters_register ("GC Maps size",
2495 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_maps_size);
2496 mono_counters_register ("GC Call Sites size",
2497 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites_size);
2498 mono_counters_register ("GC Bitmaps size",
2499 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_bitmaps_size);
2500 mono_counters_register ("GC Map struct size",
2501 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_map_struct_size);
2502 mono_counters_register ("GC Call Sites encoded using 8 bits",
2503 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites8_size);
2504 mono_counters_register ("GC Call Sites encoded using 16 bits",
2505 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites16_size);
2506 mono_counters_register ("GC Call Sites encoded using 32 bits",
2507 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites32_size);
2509 mono_counters_register ("GC Map slots (all)",
2510 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.all_slots);
2511 mono_counters_register ("GC Map slots (ref)",
2512 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.ref_slots);
2513 mono_counters_register ("GC Map slots (noref)",
2514 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.noref_slots);
2515 mono_counters_register ("GC Map slots (pin)",
2516 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.pin_slots);
2518 mono_counters_register ("GC TLS Data size",
2519 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.tlsdata_size);
2521 mono_counters_register ("Stack space scanned (all)",
2522 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_stacks);
2523 mono_counters_register ("Stack space scanned (native)",
2524 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_native);
2525 mono_counters_register ("Stack space scanned (other)",
2526 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_other);
2527 mono_counters_register ("Stack space scanned (using GC Maps)",
2528 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned);
2529 mono_counters_register ("Stack space scanned (precise)",
2530 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_precisely);
2531 mono_counters_register ("Stack space scanned (pin)",
2532 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_conservatively);
2533 mono_counters_register ("Stack space scanned (pin registers)",
2534 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_registers);
2540 mini_gc_enable_gc_maps_for_aot (void)
2552 mini_gc_init_gc_map (MonoCompile *cfg)
2557 mini_gc_create_gc_map (MonoCompile *cfg)
2562 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
2567 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
2571 #endif /* DISABLE_JIT */
2580 * Set GC specific options in CFG.
2583 mini_gc_init_cfg (MonoCompile *cfg)
2585 if (mono_gc_is_moving ()) {
2586 cfg->disable_ref_noref_stack_slot_share = TRUE;
2587 cfg->gen_write_barriers = TRUE;
2590 mini_gc_init_gc_map (cfg);
2593 #endif /* DISABLE_JIT */
2596 * Problems with the current code:
2597 * - the stack walk is slow
2598 * - vtypes/refs used in EH regions are treated conservatively
2599 * - if the code is finished, less pinning will be done, causing problems because
2600 * we promote all surviving objects to old-gen.
2601 * - the unwind code can't handle a method stopped inside a finally region, it thinks the caller is
2602 * another method, but in reality it is either the exception handling code or the CALL_HANDLER opcode.
2603 * This manifests in "Unable to find ip offset x in callsite list" assertions.
2604 * - the unwind code also can't handle frames which are in the epilog, since the unwind info is not
2609 * Ideas for creating smaller GC maps:
2610 * - remove empty columns from the bitmaps. This requires adding a mask bit array for
2612 * - merge reg and stack slot bitmaps, so the unused bits at the end of the reg bitmap are
2614 * - if the bitmap width is not a multiple of 8, the remaining bits are wasted.
2615 * - group ref and non-ref stack slots together in mono_allocate_stack_slots ().
2616 * - add an index for the callsite table so that each entry can be encoded as a 1 byte difference
2617 * from an index entry.