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/gc-internal.h>
19 #include <mono/utils/mono-counters.h>
21 #if SIZEOF_VOID_P == 4
22 typedef guint32 mword;
24 typedef guint64 mword;
27 #define SIZEOF_SLOT ((int)sizeof (mgreg_t))
29 #define GC_BITS_PER_WORD (sizeof (mword) * 8)
31 /* Contains state needed by the GC Map construction code */
34 * This contains information about stack slots initialized in the prolog, encoded using
35 * (slot_index << 16) | slot_type. The slot_index is relative to the CFA, i.e. 0
36 * means cfa+0, 1 means cfa-4/8, etc.
38 GSList *stack_slots_from_cfa;
39 /* Same for stack slots relative to the frame pointer */
40 GSList *stack_slots_from_fp;
42 /* Number of slots in the map */
44 /* The number of registers in the map */
46 /* Min and Max offsets of the stack frame relative to fp */
47 int min_offset, max_offset;
48 /* Same for the locals area */
49 int locals_min_offset, locals_max_offset;
51 /* The call sites where this frame can be stopped during GC */
52 GCCallSite **callsites;
53 /* The number of call sites */
57 * The width of the stack bitmaps in bytes. This is not equal to the bitmap width at
58 * runtime, since it includes columns which are 0.
60 int stack_bitmap_width;
62 * A bitmap whose width equals nslots, and whose height equals ncallsites.
63 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
66 guint8 *stack_ref_bitmap;
67 /* Same for SLOT_PIN */
68 guint8 *stack_pin_bitmap;
71 * Similar bitmaps for registers. These have width MONO_MAX_IREGS in bits.
74 guint8 *reg_ref_bitmap;
75 guint8 *reg_pin_bitmap;
78 #define ALIGN_TO(val,align) ((((mgreg_t)val) + ((align) - 1)) & ~((align) - 1))
83 /* We don't support debug levels, its all-or-nothing */
84 #define DEBUG(s) do { s; fflush (logfile); } while (0)
85 #define DEBUG_ENABLED 1
92 #define DEBUG_PRECISE(s) do { s; } while (0)
93 #define DEBUG_PRECISE_ENABLED
95 #define DEBUG_PRECISE(s)
99 * Contains information collected during the conservative stack marking pass,
100 * used during the precise pass. This helps to avoid doing a stack walk twice, which
106 int frame_start_offset;
108 /* Relative to stack_start */
109 int reg_locations [MONO_MAX_IREGS];
110 #ifdef DEBUG_PRECISE_ENABLED
113 int regs [MONO_MAX_IREGS];
117 /* Max number of frames stored in the TLS data */
118 #define MAX_FRAMES 50
121 * Per-thread data kept by this module. This is stored in the GC and passed to us as
122 * parameters, instead of being stored in a TLS variable, since during a collection,
123 * only the collection thread is active.
126 MonoThreadUnwindState unwind_state;
127 MonoThreadInfo *info;
130 gpointer ref_to_track;
131 /* Number of frames collected during the !precise pass */
133 FrameInfo frames [MAX_FRAMES];
136 /* These are constant so don't store them in the GC Maps */
137 /* Number of registers stored in gc maps */
138 #define NREGS MONO_MAX_IREGS
142 * Contains information needed to mark a stack frame.
143 * This is a transient structure, created from a compressed representation on-demand.
147 * The offsets of the GC tracked area inside the stack frame relative to the frame pointer.
148 * This includes memory which is NOREF thus doesn't need GC maps.
153 * The offset relative to frame_offset where the the memory described by the GC maps
157 /* The number of stack slots in the map */
159 /* The frame pointer register */
161 /* The size of each callsite table entry */
162 guint8 callsite_entry_size;
163 guint has_pin_slots : 1;
164 guint has_ref_slots : 1;
165 guint has_ref_regs : 1;
166 guint has_pin_regs : 1;
168 /* The offsets below are into an external bitmaps array */
171 * A bitmap whose width is equal to bitmap_width, and whose height is equal to ncallsites.
172 * The bitmap contains a 1 if the corresponding stack slot has type SLOT_REF at the
175 guint32 stack_ref_bitmap_offset;
177 * Same for SLOT_PIN. It is possible that the same bit is set in both bitmaps at
178 * different callsites, if the slot starts out as PIN, and later changes to REF.
180 guint32 stack_pin_bitmap_offset;
183 * Corresponding bitmaps for registers
184 * These have width equal to the number of bits set in reg_ref_mask/reg_pin_mask.
185 * FIXME: Merge these with the normal bitmaps, i.e. reserve the first x slots for them ?
187 guint32 reg_pin_bitmap_offset;
188 guint32 reg_ref_bitmap_offset;
190 guint32 used_int_regs, reg_ref_mask, reg_pin_mask;
192 /* The number of bits set in the two masks above */
193 guint8 nref_regs, npin_regs;
196 * A bit array marking slots which contain refs.
197 * This is used only for debugging.
201 /* Callsite offsets */
202 /* These can take up a lot of space, so encode them compactly */
212 * A compressed version of GCMap. This is what gets stored in MonoJitInfo.
216 //guint8 encoded_size;
219 * The arrays below are embedded after the struct.
220 * Their address needs to be computed.
223 /* The fixed fields of the GCMap encoded using LEB128 */
224 guint8 encoded [MONO_ZERO_LEN_ARRAY];
226 /* An array of ncallsites entries, each entry is callsite_entry_size bytes long */
227 guint8 callsites [MONO_ZERO_LEN_ARRAY];
230 guint8 bitmaps [MONO_ZERO_LEN_ARRAY];
233 static int precise_frame_count [2], precise_frame_limit = -1;
234 static gboolean precise_frame_limit_inited;
240 int scanned_precisely;
241 int scanned_conservatively;
242 int scanned_registers;
252 int gc_callsites_size;
253 int gc_callsites8_size;
254 int gc_callsites16_size;
255 int gc_callsites32_size;
257 int gc_map_struct_size;
261 static JITGCStats stats;
263 static FILE *logfile;
265 static gboolean enable_gc_maps_for_aot;
268 mini_gc_enable_gc_maps_for_aot (void)
270 enable_gc_maps_for_aot = TRUE;
273 // FIXME: Move these to a shared place
276 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
281 guint8 b = value & 0x7f;
283 if (value != 0) /* more bytes to come */
291 static G_GNUC_UNUSED void
292 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
295 gboolean negative = (value < 0);
303 /* the following is unnecessary if the
304 * implementation of >>= uses an arithmetic rather
305 * than logical shift for a signed left operand
309 value |= - (1 <<(size - 7));
310 /* sign bit of byte is second high order bit (0x40) */
311 if ((value == 0 && !(byte & 0x40)) ||
312 (value == -1 && (byte & 0x40)))
322 static inline guint32
323 decode_uleb128 (guint8 *buf, guint8 **endbuf)
333 res = res | (((int)(b & 0x7f)) << shift);
345 decode_sleb128 (guint8 *buf, guint8 **endbuf)
355 res = res | (((int)(b & 0x7f)) << shift);
358 if (shift < 32 && (b & 0x40))
359 res |= - (1 << shift);
370 encode_frame_reg (int frame_reg)
373 if (frame_reg == AMD64_RSP)
375 else if (frame_reg == AMD64_RBP)
377 #elif defined(TARGET_X86)
378 if (frame_reg == X86_EBP)
380 else if (frame_reg == X86_ESP)
382 #elif defined(TARGET_ARM)
383 if (frame_reg == ARMREG_SP)
385 else if (frame_reg == ARMREG_FP)
387 #elif defined(TARGET_S390X)
388 if (frame_reg == S390_SP)
390 else if (frame_reg == S390_FP)
395 g_assert_not_reached ();
400 decode_frame_reg (int encoded)
405 else if (encoded == 1)
407 #elif defined(TARGET_X86)
410 else if (encoded == 1)
412 #elif defined(TARGET_ARM)
415 else if (encoded == 1)
417 #elif defined(TARGET_S390X)
420 else if (encoded == 1)
425 g_assert_not_reached ();
431 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15, AMD64_RDI, AMD64_RSI };
433 static int callee_saved_regs [] = { AMD64_RBP, AMD64_RBX, AMD64_R12, AMD64_R13, AMD64_R14, AMD64_R15 };
435 #elif defined(TARGET_X86)
436 static int callee_saved_regs [] = { X86_EBX, X86_ESI, X86_EDI };
437 #elif defined(TARGET_ARM)
438 static int callee_saved_regs [] = { ARMREG_V1, ARMREG_V2, ARMREG_V3, ARMREG_V4, ARMREG_V5, ARMREG_V7, ARMREG_FP };
439 #elif defined(TARGET_S390X)
440 static int callee_saved_regs [] = { s390_r6, s390_r7, s390_r8, s390_r9, s390_r10, s390_r11, s390_r12, s390_r13, s390_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 */
593 tls->unwind_state.valid = FALSE;
597 if (tls->tid != GetCurrentThreadId ()) {
598 /* Happens on osx because threads are not suspended using signals */
601 g_assert (tls->info);
602 res = mono_thread_state_init_from_handle (&tls->unwind_state, (MonoNativeThreadId)tls->tid, tls->info->native_handle);
604 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
606 mono_arch_sigctx_to_monoctx (sigctx, &tls->unwind_state.ctx);
607 tls->unwind_state.valid = TRUE;
609 memcpy (&tls->unwind_state.ctx, ctx, sizeof (MonoContext));
610 tls->unwind_state.valid = TRUE;
612 tls->unwind_state.valid = FALSE;
614 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_JIT_TLS] = mono_native_tls_get_value (mono_jit_tls_id);
615 tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
618 if (!tls->unwind_state.unwind_data [MONO_UNWIND_DATA_DOMAIN]) {
619 /* Happens during startup */
620 tls->unwind_state.valid = FALSE;
625 #define DEAD_REF ((gpointer)(gssize)0x2a2a2a2a2a2a2a2aULL)
628 set_bit (guint8 *bitmap, int width, int y, int x)
630 bitmap [(width * y) + (x / 8)] |= (1 << (x % 8));
634 clear_bit (guint8 *bitmap, int width, int y, int x)
636 bitmap [(width * y) + (x / 8)] &= ~(1 << (x % 8));
640 get_bit (guint8 *bitmap, int width, int y, int x)
642 return bitmap [(width * y) + (x / 8)] & (1 << (x % 8));
646 slot_type_to_string (GCSlotType type)
656 g_assert_not_reached ();
661 static inline mgreg_t
662 get_frame_pointer (MonoContext *ctx, int frame_reg)
664 #if defined(TARGET_AMD64)
665 if (frame_reg == AMD64_RSP)
667 else if (frame_reg == AMD64_RBP)
669 #elif defined(TARGET_X86)
670 if (frame_reg == X86_ESP)
672 else if (frame_reg == X86_EBP)
674 #elif defined(TARGET_ARM)
675 if (frame_reg == ARMREG_SP)
676 return (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
677 else if (frame_reg == ARMREG_FP)
678 return (mgreg_t)MONO_CONTEXT_GET_BP (ctx);
679 #elif defined(TARGET_S390X)
680 if (frame_reg == S390_SP)
681 return (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
682 else if (frame_reg == S390_FP)
683 return (mgreg_t)MONO_CONTEXT_GET_BP (ctx);
685 g_assert_not_reached ();
690 * conservatively_pass:
692 * Mark a thread stack conservatively and collect information needed by the precise pass.
695 conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
698 MonoContext ctx, new_ctx;
701 gboolean last = TRUE;
705 guint8* fp, *p, *real_frame_start, *frame_start, *frame_end;
706 int i, pc_offset, cindex, bitmap_width;
707 int scanned = 0, scanned_precisely, scanned_conservatively, scanned_registers;
709 StackFrameInfo frame;
710 mgreg_t *reg_locations [MONO_MAX_IREGS];
711 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
714 guint32 precise_regmask;
718 tls->ref_to_track = NULL;
721 /* tls == NULL can happen during startup */
722 if (mono_thread_internal_current () == NULL || !tls) {
723 mono_gc_conservatively_scan_area (stack_start, stack_end);
724 stats.scanned_stacks += stack_end - stack_start;
728 lmf = tls->unwind_state.unwind_data [MONO_UNWIND_DATA_LMF];
731 /* Number of bytes scanned based on GC map data */
733 /* Number of bytes scanned precisely based on GC map data */
734 scanned_precisely = 0;
735 /* Number of bytes scanned conservatively based on GC map data */
736 scanned_conservatively = 0;
737 /* Number of bytes scanned conservatively in register save areas */
738 scanned_registers = 0;
740 /* This is one past the last address which we have scanned */
741 stack_limit = stack_start;
743 if (!tls->unwind_state.valid)
744 memset (&new_ctx, 0, sizeof (ctx));
746 memcpy (&new_ctx, &tls->unwind_state.ctx, sizeof (MonoContext));
748 memset (reg_locations, 0, sizeof (reg_locations));
749 memset (new_reg_locations, 0, sizeof (new_reg_locations));
752 if (!tls->unwind_state.valid)
755 memcpy (&ctx, &new_ctx, sizeof (ctx));
757 for (i = 0; i < MONO_MAX_IREGS; ++i) {
758 if (new_reg_locations [i]) {
760 * If the current frame saves the register, it means it might modify its
761 * value, thus the old location might not contain the same value, so
762 * we have to mark it conservatively.
764 if (reg_locations [i]) {
765 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
766 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
767 scanned_registers += SIZEOF_SLOT;
770 reg_locations [i] = new_reg_locations [i];
772 DEBUG (fprintf (logfile, "\treg %s is now at location %p.\n", mono_arch_regname (i), reg_locations [i]));
776 g_assert ((mgreg_t)stack_limit % SIZEOF_SLOT == 0);
778 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);
784 // FIXME: For skipped frames, scan the param area of the parent frame conservatively ?
786 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
788 * These frames are problematic for several reasons:
789 * - they are unwound through an LMF, and we have no precise register tracking for those.
790 * - the LMF might not contain a precise ip, so we can't compute the call site.
791 * - the LMF only unwinds to the wrapper frame, so we get these methods twice.
793 DEBUG (fprintf (logfile, "Mark(0): <Managed-to-native transition>\n"));
794 for (i = 0; i < MONO_MAX_IREGS; ++i) {
795 if (reg_locations [i]) {
796 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
797 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
798 scanned_registers += SIZEOF_SLOT;
800 reg_locations [i] = NULL;
801 new_reg_locations [i] = NULL;
807 /* The last frame can be in any state so mark conservatively */
810 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));
812 DEBUG (fprintf (logfile, "\t <Last frame>\n"));
815 * new_reg_locations is not precise when a method is interrupted during its epilog, so clear it.
817 for (i = 0; i < MONO_MAX_IREGS; ++i) {
818 if (reg_locations [i]) {
819 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), reg_locations [i]));
820 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
821 scanned_registers += SIZEOF_SLOT;
823 if (new_reg_locations [i]) {
824 DEBUG (fprintf (logfile, "\tscan saved reg %s location %p.\n", mono_arch_regname (i), new_reg_locations [i]));
825 mono_gc_conservatively_scan_area (new_reg_locations [i], (char*)new_reg_locations [i] + SIZEOF_SLOT);
826 scanned_registers += SIZEOF_SLOT;
828 reg_locations [i] = NULL;
829 new_reg_locations [i] = NULL;
834 pc_offset = (guint8*)MONO_CONTEXT_GET_IP (&ctx) - (guint8*)ji->code_start;
836 /* These frames are very problematic */
837 if (ji->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
838 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));
839 DEBUG (fprintf (logfile, "\tSkip.\n"));
843 /* All the other frames are at a call site */
845 if (tls->nframes == MAX_FRAMES) {
847 * Can't save information since the array is full. So scan the rest of the
848 * stack conservatively.
850 DEBUG (fprintf (logfile, "Mark (0): Frame stack full.\n"));
854 /* Scan the frame of this method */
857 * A frame contains the following:
862 * - localloc-ed memory
864 g_assert (pc_offset >= 0);
869 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));
870 DEBUG (fprintf (logfile, "\tNo GC Map.\n"));
874 /* The embedded callsite table requires this */
875 g_assert (((mgreg_t)emap % 4) == 0);
878 * Debugging aid to control the number of frames scanned precisely
880 if (!precise_frame_limit_inited) {
881 if (getenv ("MONO_PRECISE_COUNT"))
882 precise_frame_limit = atoi (getenv ("MONO_PRECISE_COUNT"));
883 precise_frame_limit_inited = TRUE;
886 if (precise_frame_limit != -1) {
887 if (precise_frame_count [FALSE] == precise_frame_limit)
888 printf ("LAST PRECISE FRAME: %s\n", mono_method_full_name (ji->method, TRUE));
889 if (precise_frame_count [FALSE] > precise_frame_limit)
892 precise_frame_count [FALSE] ++;
894 /* Decode the encoded GC map */
896 memset (map, 0, sizeof (GCMap));
897 decode_gc_map (&emap->encoded [0], map, &p);
898 p = (guint8*)ALIGN_TO (p, map->callsite_entry_size);
899 map->callsites.offsets8 = p;
900 p += map->callsite_entry_size * map->ncallsites;
903 fp = (guint8*)get_frame_pointer (&ctx, map->frame_reg);
905 real_frame_start = fp + map->start_offset;
906 frame_start = fp + map->start_offset + map->map_offset;
907 frame_end = fp + map->end_offset;
909 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));
911 /* Find the callsite index */
912 if (map->callsite_entry_size == 1) {
913 for (i = 0; i < map->ncallsites; ++i)
914 /* ip points inside the call instruction */
915 if (map->callsites.offsets8 [i] == pc_offset + 1)
917 } else if (map->callsite_entry_size == 2) {
918 // FIXME: Use a binary search
919 for (i = 0; i < map->ncallsites; ++i)
920 /* ip points inside the call instruction */
921 if (map->callsites.offsets16 [i] == pc_offset + 1)
924 // FIXME: Use a binary search
925 for (i = 0; i < map->ncallsites; ++i)
926 /* ip points inside the call instruction */
927 if (map->callsites.offsets32 [i] == pc_offset + 1)
930 if (i == map->ncallsites) {
931 printf ("Unable to find ip offset 0x%x in callsite list of %s.\n", pc_offset + 1, mono_method_full_name (ji->method, TRUE));
932 g_assert_not_reached ();
937 * This is not neccessary true on x86 because frames have a different size at each
940 //g_assert (real_frame_start >= stack_limit);
942 if (real_frame_start > stack_limit) {
943 /* This scans the previously skipped frames as well */
944 DEBUG (fprintf (logfile, "\tscan area %p-%p (%d).\n", stack_limit, real_frame_start, (int)(real_frame_start - stack_limit)));
945 mono_gc_conservatively_scan_area (stack_limit, real_frame_start);
946 stats.scanned_other += real_frame_start - stack_limit;
949 /* Mark stack slots */
950 if (map->has_pin_slots) {
951 int bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
952 guint8 *pin_bitmap = &bitmaps [map->stack_pin_bitmap_offset + (bitmap_width * cindex)];
957 for (i = 0; i < map->nslots; ++i) {
958 pinned = pin_bitmap [i / 8] & (1 << (i % 8));
960 DEBUG (fprintf (logfile, "\tscan slot %s0x%x(fp)=%p.\n", (guint8*)p > (guint8*)fp ? "" : "-", ABS ((int)((gssize)p - (gssize)fp)), p));
961 mono_gc_conservatively_scan_area (p, p + SIZEOF_SLOT);
962 scanned_conservatively += SIZEOF_SLOT;
964 scanned_precisely += SIZEOF_SLOT;
969 scanned_precisely += (map->nslots * SIZEOF_SLOT);
972 /* The area outside of start-end is NOREF */
973 scanned_precisely += (map->end_offset - map->start_offset) - (map->nslots * SIZEOF_SLOT);
976 precise_regmask = map->used_int_regs | (1 << map->frame_reg);
977 if (map->has_pin_regs) {
978 int bitmap_width = ALIGN_TO (map->npin_regs, 8) / 8;
979 guint8 *pin_bitmap = &bitmaps [map->reg_pin_bitmap_offset + (bitmap_width * cindex)];
981 for (i = 0; i < NREGS; ++i) {
982 if (!(map->used_int_regs & (1 << i)))
985 if (!(map->reg_pin_mask & (1 << i)))
988 if (pin_bitmap [bindex / 8] & (1 << (bindex % 8))) {
989 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is pinning.\n", mono_arch_regname (i), reg_locations [i]));
990 precise_regmask &= ~(1 << i);
996 scanned += map->end_offset - map->start_offset;
998 g_assert (scanned == scanned_precisely + scanned_conservatively);
1000 stack_limit = frame_end;
1002 /* Save information for the precise pass */
1003 fi = &tls->frames [tls->nframes];
1004 fi->nslots = map->nslots;
1005 bitmap_width = ALIGN_TO (map->nslots, 8) / 8;
1006 if (map->has_ref_slots)
1007 fi->bitmap = &bitmaps [map->stack_ref_bitmap_offset + (bitmap_width * cindex)];
1010 fi->frame_start_offset = frame_start - stack_start;
1011 fi->nreg_locations = 0;
1012 DEBUG_PRECISE (fi->ji = ji);
1013 DEBUG_PRECISE (fi->fp = fp);
1015 if (map->has_ref_regs) {
1016 int bitmap_width = ALIGN_TO (map->nref_regs, 8) / 8;
1017 guint8 *ref_bitmap = &bitmaps [map->reg_ref_bitmap_offset + (bitmap_width * cindex)];
1019 for (i = 0; i < NREGS; ++i) {
1020 if (!(map->reg_ref_mask & (1 << i)))
1023 if (reg_locations [i] && (ref_bitmap [bindex / 8] & (1 << (bindex % 8)))) {
1024 DEBUG_PRECISE (fi->regs [fi->nreg_locations] = i);
1025 DEBUG (fprintf (logfile, "\treg %s saved at 0x%p is ref.\n", mono_arch_regname (i), reg_locations [i]));
1026 fi->reg_locations [fi->nreg_locations] = (guint8*)reg_locations [i] - stack_start;
1027 fi->nreg_locations ++;
1034 * Clear locations of precisely tracked registers.
1036 if (precise_regmask) {
1037 for (i = 0; i < NREGS; ++i) {
1038 if (precise_regmask & (1 << i)) {
1040 * The method uses this register, and we have precise info for it.
1041 * This means the location will be scanned precisely.
1042 * Tell the code at the beginning of the loop that this location is
1045 if (reg_locations [i])
1046 DEBUG (fprintf (logfile, "\treg %s at location %p (==%p) is precise.\n", mono_arch_regname (i), reg_locations [i], (gpointer)*reg_locations [i]));
1047 reg_locations [i] = NULL;
1055 /* Scan the remaining register save locations */
1056 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1057 if (reg_locations [i]) {
1058 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", reg_locations [i]));
1059 mono_gc_conservatively_scan_area (reg_locations [i], (char*)reg_locations [i] + SIZEOF_SLOT);
1060 scanned_registers += SIZEOF_SLOT;
1062 if (new_reg_locations [i]) {
1063 DEBUG (fprintf (logfile, "\tscan saved reg location %p.\n", new_reg_locations [i]));
1064 mono_gc_conservatively_scan_area (new_reg_locations [i], (char*)new_reg_locations [i] + SIZEOF_SLOT);
1065 scanned_registers += SIZEOF_SLOT;
1069 if (stack_limit < stack_end) {
1070 DEBUG (fprintf (logfile, "\tscan remaining stack %p-%p (%d).\n", stack_limit, stack_end, (int)(stack_end - stack_limit)));
1071 mono_gc_conservatively_scan_area (stack_limit, stack_end);
1072 stats.scanned_native += stack_end - stack_limit;
1075 DEBUG (fprintf (logfile, "Marked %d bytes, p=%d,c=%d out of %d.\n", scanned, scanned_precisely, scanned_conservatively, (int)(stack_end - stack_start)));
1077 stats.scanned_stacks += stack_end - stack_start;
1078 stats.scanned += scanned;
1079 stats.scanned_precisely += scanned_precisely;
1080 stats.scanned_conservatively += scanned_conservatively;
1081 stats.scanned_registers += scanned_registers;
1083 //mono_gc_conservatively_scan_area (stack_start, stack_end);
1089 * Mark a thread stack precisely based on information saved during the conservative
1093 precise_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
1097 guint8 *frame_start;
1102 if (!tls->unwind_state.valid)
1105 for (findex = 0; findex < tls->nframes; findex ++) {
1106 /* Load information saved by the !precise pass */
1107 fi = &tls->frames [findex];
1108 frame_start = stack_start + fi->frame_start_offset;
1110 DEBUG (char *fname = mono_method_full_name (fi->ji->method, TRUE); fprintf (logfile, "Mark(1): %s\n", fname); g_free (fname));
1113 * FIXME: Add a function to mark using a bitmap, to avoid doing a
1114 * call for each object.
1117 /* Mark stack slots */
1119 guint8 *ref_bitmap = fi->bitmap;
1122 for (i = 0; i < fi->nslots; ++i) {
1123 MonoObject **ptr = (MonoObject**)(frame_start + (i * SIZEOF_SLOT));
1125 live = ref_bitmap [i / 8] & (1 << (i % 8));
1128 MonoObject *obj = *ptr;
1130 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p ->", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1131 *ptr = mono_gc_scan_object (obj);
1132 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1134 DEBUG (fprintf (logfile, "\tref %s0x%x(fp)=%p: %p.\n", (guint8*)ptr >= (guint8*)fi->fp ? "" : "-", ABS ((int)((gssize)ptr - (gssize)fi->fp)), ptr, obj));
1139 * This is disabled because the pointer takes up a lot of space.
1140 * Stack slots might be shared between ref and non-ref variables ?
1142 if (map->ref_slots [i / 8] & (1 << (i % 8))) {
1143 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));
1145 * Fail fast if the live range is incorrect, and
1146 * the JITted code tries to access this object
1155 /* Mark registers */
1158 * Registers are different from stack slots, they have no address where they
1159 * are stored. Instead, some frame below this frame in the stack saves them
1160 * in its prolog to the stack. We can mark this location precisely.
1162 for (i = 0; i < fi->nreg_locations; ++i) {
1164 * reg_locations [i] contains the address of the stack slot where
1165 * a reg was last saved, so mark that slot.
1167 MonoObject **ptr = (MonoObject**)((guint8*)stack_start + fi->reg_locations [i]);
1168 MonoObject *obj = *ptr;
1171 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p ->", mono_arch_regname (fi->regs [i]), ptr, obj));
1172 *ptr = mono_gc_scan_object (obj);
1173 DEBUG (fprintf (logfile, " %p.\n", *ptr));
1175 DEBUG (fprintf (logfile, "\treg %s saved at %p: %p\n", mono_arch_regname (fi->regs [i]), ptr, obj));
1181 * Debugging aid to check for missed refs.
1183 if (tls->ref_to_track) {
1186 for (p = (mgreg_t*)stack_start; p < (mgreg_t*)stack_end; ++p)
1187 if (*p == (mgreg_t)tls->ref_to_track)
1188 printf ("REF AT %p.\n", p);
1195 * This is called by the GC twice to mark a thread stack. PRECISE is FALSE at the first
1196 * call, and TRUE at the second. USER_DATA points to a TlsData
1197 * structure filled up by thread_suspend_func.
1200 thread_mark_func (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise)
1202 TlsData *tls = user_data;
1204 DEBUG (fprintf (logfile, "****************************************\n"));
1205 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));
1206 DEBUG (fprintf (logfile, "****************************************\n"));
1209 conservative_pass (tls, stack_start, stack_end);
1211 precise_pass (tls, stack_start, stack_end);
1215 mini_gc_init_gc_map (MonoCompile *cfg)
1217 if (COMPILE_LLVM (cfg))
1220 if (!mono_gc_is_moving ())
1223 if (cfg->compile_aot) {
1224 if (!enable_gc_maps_for_aot)
1226 } else if (!mono_gc_precise_stack_mark_enabled ())
1230 /* Debugging support */
1232 static int precise_count;
1235 if (getenv ("MONO_GCMAP_COUNT")) {
1236 if (precise_count == atoi (getenv ("MONO_GCMAP_COUNT")))
1237 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
1238 if (precise_count > atoi (getenv ("MONO_GCMAP_COUNT")))
1244 cfg->compute_gc_maps = TRUE;
1246 cfg->gc_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoCompileGC));
1250 * mini_gc_set_slot_type_from_fp:
1252 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1253 * relative to the frame pointer. By default, all stack slots are type PIN, so there is no
1254 * need to call this function for those slots.
1257 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
1259 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1261 if (!cfg->compute_gc_maps)
1264 g_assert (slot_offset % SIZEOF_SLOT == 0);
1266 gcfg->stack_slots_from_fp = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_fp, GINT_TO_POINTER (((slot_offset) << 16) | type));
1270 * mini_gc_set_slot_type_from_cfa:
1272 * Set the GC slot type of the stack slot identified by SLOT_OFFSET, which should be
1273 * relative to the DWARF CFA value. This should be called from mono_arch_emit_prolog ().
1274 * If type is STACK_REF, the slot is assumed to be live from the end of the prolog until
1275 * the end of the method. By default, all stack slots are type PIN, so there is no need to
1276 * call this function for those slots.
1279 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
1281 MonoCompileGC *gcfg = (MonoCompileGC*)cfg->gc_info;
1282 int slot = - (slot_offset / SIZEOF_SLOT);
1284 if (!cfg->compute_gc_maps)
1287 g_assert (slot_offset <= 0);
1288 g_assert (slot_offset % SIZEOF_SLOT == 0);
1290 gcfg->stack_slots_from_cfa = g_slist_prepend_mempool (cfg->mempool, gcfg->stack_slots_from_cfa, GUINT_TO_POINTER (((slot) << 16) | type));
1294 fp_offset_to_slot (MonoCompile *cfg, int offset)
1296 MonoCompileGC *gcfg = cfg->gc_info;
1298 return (offset - gcfg->min_offset) / SIZEOF_SLOT;
1302 slot_to_fp_offset (MonoCompile *cfg, int slot)
1304 MonoCompileGC *gcfg = cfg->gc_info;
1306 return (slot * SIZEOF_SLOT) + gcfg->min_offset;
1309 static inline MONO_ALWAYS_INLINE void
1310 set_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1312 g_assert (slot >= 0 && slot < gcfg->nslots);
1314 if (type == SLOT_PIN) {
1315 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1316 set_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1317 } else if (type == SLOT_REF) {
1318 set_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1319 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1320 } else if (type == SLOT_NOREF) {
1321 clear_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1322 clear_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, slot, callsite_index);
1327 set_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1330 guint8 *ref_bitmap, *pin_bitmap;
1335 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1336 set_slot (gcfg, slot, cindex, type);
1338 ref_bitmap = gcfg->stack_ref_bitmap;
1339 pin_bitmap = gcfg->stack_pin_bitmap;
1340 width = gcfg->stack_bitmap_width;
1343 if (type == SLOT_PIN) {
1344 memset (ref_bitmap + pos, 0, width);
1345 memset (pin_bitmap + pos, 0xff, width);
1346 } else if (type == SLOT_REF) {
1347 memset (ref_bitmap + pos, 0xff, width);
1348 memset (pin_bitmap + pos, 0, width);
1349 } else if (type == SLOT_NOREF) {
1350 memset (ref_bitmap + pos, 0, width);
1351 memset (pin_bitmap + pos, 0, width);
1356 set_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1360 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1361 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1362 if (callsite_offset >= from && callsite_offset < to)
1363 set_slot (gcfg, slot, cindex, type);
1368 set_reg_slot (MonoCompileGC *gcfg, int slot, int callsite_index, GCSlotType type)
1370 g_assert (slot >= 0 && slot < gcfg->nregs);
1372 if (type == SLOT_PIN) {
1373 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1374 set_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1375 } else if (type == SLOT_REF) {
1376 set_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1377 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1378 } else if (type == SLOT_NOREF) {
1379 clear_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1380 clear_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, slot, callsite_index);
1385 set_reg_slot_everywhere (MonoCompileGC *gcfg, int slot, GCSlotType type)
1389 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1390 set_reg_slot (gcfg, slot, cindex, type);
1394 set_reg_slot_in_range (MonoCompileGC *gcfg, int slot, int from, int to, GCSlotType type)
1398 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1399 int callsite_offset = gcfg->callsites [cindex]->pc_offset;
1400 if (callsite_offset >= from && callsite_offset < to)
1401 set_reg_slot (gcfg, slot, cindex, type);
1406 process_spill_slots (MonoCompile *cfg)
1408 MonoCompileGC *gcfg = cfg->gc_info;
1413 /* Mark all ref/pin spill slots as NOREF by default outside of their live range */
1414 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1415 for (l = bb->spill_slot_defs; l; l = l->next) {
1416 MonoInst *def = l->data;
1417 int spill_slot = def->inst_c0;
1418 int bank = def->inst_c1;
1419 int offset = cfg->spill_info [bank][spill_slot].offset;
1420 int slot = fp_offset_to_slot (cfg, offset);
1422 if (bank == MONO_REG_INT_MP || bank == MONO_REG_INT_REF)
1423 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1427 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1428 for (l = bb->spill_slot_defs; l; l = l->next) {
1429 MonoInst *def = l->data;
1430 int spill_slot = def->inst_c0;
1431 int bank = def->inst_c1;
1432 int offset = cfg->spill_info [bank][spill_slot].offset;
1433 int slot = fp_offset_to_slot (cfg, offset);
1436 if (bank == MONO_REG_INT_MP)
1442 * Extend the live interval for the GC tracked spill slots
1443 * defined in this bblock.
1444 * FIXME: This is not needed.
1446 set_slot_in_range (gcfg, slot, def->backend.pc_offset, bb->native_offset + bb->native_length, type);
1448 if (cfg->verbose_level > 1)
1449 printf ("\t%s spill slot at %s0x%x(fp) (slot = %d)\n", slot_type_to_string (type), offset >= 0 ? "" : "-", ABS (offset), slot);
1453 /* Set fp spill slots to NOREF */
1454 for (i = 0; i < cfg->spill_info_len [MONO_REG_DOUBLE]; ++i) {
1455 int offset = cfg->spill_info [MONO_REG_DOUBLE][i].offset;
1461 slot = fp_offset_to_slot (cfg, offset);
1463 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1465 if (cfg->verbose_level > 1)
1466 printf ("\tfp spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1469 /* Set int spill slots to NOREF */
1470 for (i = 0; i < cfg->spill_info_len [MONO_REG_INT]; ++i) {
1471 int offset = cfg->spill_info [MONO_REG_INT][i].offset;
1477 slot = fp_offset_to_slot (cfg, offset);
1479 set_slot_everywhere (gcfg, slot, SLOT_NOREF);
1480 if (cfg->verbose_level > 1)
1481 printf ("\tint spill slot at %s0x%x(fp) (slot = %d)\n", offset >= 0 ? "" : "-", ABS (offset), slot);
1486 * process_other_slots:
1488 * Process stack slots registered using mini_gc_set_slot_type_... ().
1491 process_other_slots (MonoCompile *cfg)
1493 MonoCompileGC *gcfg = cfg->gc_info;
1496 /* Relative to the CFA */
1497 for (l = gcfg->stack_slots_from_cfa; l; l = l->next) {
1498 guint data = GPOINTER_TO_UINT (l->data);
1499 int cfa_slot = data >> 16;
1500 GCSlotType type = data & 0xff;
1504 * Map the cfa relative slot to an fp relative slot.
1505 * slot_addr == cfa - <cfa_slot>*4/8
1506 * fp + cfa_offset == cfa
1507 * -> slot_addr == fp + (cfa_offset - <cfa_slot>*4/8)
1509 slot = (cfg->cfa_offset / SIZEOF_SLOT) - cfa_slot - (gcfg->min_offset / SIZEOF_SLOT);
1511 set_slot_everywhere (gcfg, slot, type);
1513 if (cfg->verbose_level > 1) {
1514 int fp_offset = slot_to_fp_offset (cfg, slot);
1515 if (type == SLOT_NOREF)
1516 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));
1520 /* Relative to the FP */
1521 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
1522 gint data = GPOINTER_TO_INT (l->data);
1523 int offset = data >> 16;
1524 GCSlotType type = data & 0xff;
1527 slot = fp_offset_to_slot (cfg, offset);
1529 set_slot_everywhere (gcfg, slot, type);
1531 /* Liveness for these slots is handled by process_spill_slots () */
1533 if (cfg->verbose_level > 1) {
1534 if (type == SLOT_REF)
1535 printf ("\tref slot at fp+0x%x (slot = %d)\n", offset, slot);
1536 else if (type == SLOT_NOREF)
1537 printf ("\tnoref slot at 0x%x(fp) (slot = %d)\n", offset, slot);
1543 get_vtype_bitmap (MonoType *t, int *numbits)
1545 MonoClass *klass = mono_class_from_mono_type (t);
1547 if (klass->generic_container || mono_class_is_open_constructed_type (t)) {
1548 /* FIXME: Generic sharing */
1551 mono_class_compute_gc_descriptor (klass);
1553 return mono_gc_get_bitmap_for_descr (klass->gc_descr, numbits);
1557 static inline const char*
1558 get_offset_sign (int offset)
1560 return offset < 0 ? "-" : "+";
1564 get_offset_val (int offset)
1566 return offset < 0 ? (- offset) : offset;
1570 process_variables (MonoCompile *cfg)
1572 MonoCompileGC *gcfg = cfg->gc_info;
1573 MonoMethodSignature *sig = mono_method_signature (cfg->method);
1574 int i, locals_min_slot, locals_max_slot, cindex;
1578 int locals_min_offset = gcfg->locals_min_offset;
1579 int locals_max_offset = gcfg->locals_max_offset;
1581 /* Slots for locals are NOREF by default */
1582 locals_min_slot = (locals_min_offset - gcfg->min_offset) / SIZEOF_SLOT;
1583 locals_max_slot = (locals_max_offset - gcfg->min_offset) / SIZEOF_SLOT;
1584 for (i = locals_min_slot; i < locals_max_slot; ++i) {
1585 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1589 * Compute the offset where variables are initialized in the first bblock, if any.
1591 pc_offsets = g_new0 (int, cfg->next_vreg);
1593 bb = cfg->bb_entry->next_bb;
1594 MONO_BB_FOR_EACH_INS (bb, tmp) {
1595 if (tmp->opcode == OP_GC_LIVENESS_DEF) {
1596 int vreg = tmp->inst_c1;
1597 if (pc_offsets [vreg] == 0) {
1598 g_assert (tmp->backend.pc_offset > 0);
1599 pc_offsets [vreg] = tmp->backend.pc_offset;
1605 * Stack slots holding arguments are initialized in the prolog.
1606 * This means we can treat them alive for the whole method.
1608 for (i = 0; i < cfg->num_varinfo; i++) {
1609 MonoInst *ins = cfg->varinfo [i];
1610 MonoType *t = ins->inst_vtype;
1613 gboolean byref, is_this = FALSE;
1614 gboolean is_arg = i < cfg->locals_start;
1616 if (ins == cfg->ret) {
1617 if (!(ins->opcode == OP_REGOFFSET && MONO_TYPE_ISSTRUCT (t)))
1621 vmv = MONO_VARINFO (cfg, i);
1623 /* For some reason, 'this' is byref */
1624 if (sig->hasthis && ins == cfg->args [0] && !cfg->method->klass->valuetype) {
1625 t = &cfg->method->klass->byval_arg;
1631 if (ins->opcode == OP_REGVAR) {
1633 GCSlotType slot_type;
1635 t = mini_type_get_underlying_type (NULL, t);
1638 g_assert (hreg < MONO_MAX_IREGS);
1641 slot_type = SLOT_PIN;
1643 slot_type = mini_type_is_reference (cfg, t) ? SLOT_REF : SLOT_NOREF;
1645 if (slot_type == SLOT_PIN) {
1646 /* These have no live interval, be conservative */
1647 set_reg_slot_everywhere (gcfg, hreg, slot_type);
1650 * Unlike variables allocated to the stack, we generate liveness info
1651 * for noref vars in registers in mono_spill_global_vars (), because
1652 * knowing that a register doesn't contain a ref allows us to mark its save
1653 * locations precisely.
1655 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1656 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1657 set_reg_slot (gcfg, hreg, cindex, slot_type);
1660 if (cfg->verbose_level > 1) {
1661 printf ("\t%s %sreg %s(R%d)\n", slot_type_to_string (slot_type), is_arg ? "arg " : "", mono_arch_regname (hreg), vmv->vreg);
1667 if (ins->opcode != OP_REGOFFSET)
1670 if (ins->inst_offset % SIZEOF_SLOT != 0)
1673 pos = fp_offset_to_slot (cfg, ins->inst_offset);
1675 if (is_arg && ins->flags & MONO_INST_IS_DEAD) {
1676 /* These do not get stored in the prolog */
1677 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1679 if (cfg->verbose_level > 1) {
1680 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));
1685 if (MONO_TYPE_ISSTRUCT (t)) {
1687 gsize *bitmap = NULL;
1688 gboolean pin = FALSE;
1692 if (ins->backend.is_pinvoke)
1693 size = mono_class_native_size (ins->klass, NULL);
1695 size = mono_class_value_size (ins->klass, NULL);
1696 size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
1698 if (cfg->verbose_level > 1)
1699 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));
1701 if (!ins->klass->has_references) {
1703 for (j = 0; j < size_in_slots; ++j)
1704 set_slot_everywhere (gcfg, pos + j, SLOT_NOREF);
1709 bitmap = get_vtype_bitmap (t, &numbits);
1714 * Most vtypes are marked volatile because of the LDADDR instructions,
1715 * and they have no liveness information since they are decomposed
1716 * before the liveness pass. We emit OP_GC_LIVENESS_DEF instructions for
1717 * them during VZERO decomposition.
1720 if (!pc_offsets [vmv->vreg])
1723 if (ins->backend.is_pinvoke)
1728 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1729 if (gcfg->callsites [cindex]->pc_offset > pc_offsets [vmv->vreg]) {
1730 for (j = 0; j < numbits; ++j) {
1731 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD))) {
1732 /* The descriptor is for the boxed object */
1733 set_slot (gcfg, (pos + j - (sizeof (MonoObject) / SIZEOF_SLOT)), cindex, pin ? SLOT_PIN : SLOT_REF);
1739 if (cfg->verbose_level > 1) {
1740 for (j = 0; j < numbits; ++j) {
1741 if (bitmap [j / GC_BITS_PER_WORD] & ((gsize)1 << (j % GC_BITS_PER_WORD)))
1742 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)));
1746 if (cfg->verbose_level > 1)
1747 printf ("\t\tpinned\n");
1748 for (j = 0; j < size_in_slots; ++j) {
1749 set_slot_everywhere (gcfg, pos + j, SLOT_PIN);
1758 if (!is_arg && (ins->inst_offset < gcfg->min_offset || ins->inst_offset >= gcfg->max_offset))
1759 /* Vret addr etc. */
1764 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1766 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1767 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1768 set_slot (gcfg, pos, cindex, SLOT_PIN);
1770 if (cfg->verbose_level > 1)
1771 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));
1776 * This is currently disabled, but could be enabled to debug crashes.
1779 if (t->type == MONO_TYPE_I) {
1781 * Variables created in mono_handle_global_vregs have type I, but they
1782 * could hold GC refs since the vregs they were created from might not been
1783 * marked as holding a GC ref. So be conservative.
1785 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1790 t = mini_type_get_underlying_type (NULL, t);
1792 if (!mini_type_is_reference (cfg, t)) {
1793 set_slot_everywhere (gcfg, pos, SLOT_NOREF);
1794 if (cfg->verbose_level > 1)
1795 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));
1796 if (!t->byref && sizeof (mgreg_t) == 4 && (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_R8)) {
1797 set_slot_everywhere (gcfg, pos + 1, SLOT_NOREF);
1798 if (cfg->verbose_level > 1)
1799 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));
1804 /* 'this' is marked INDIRECT for gshared methods */
1805 if (ins->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT) && !is_this) {
1807 * For volatile variables, treat them alive from the point they are
1808 * initialized in the first bblock until the end of the method.
1811 set_slot_everywhere (gcfg, pos, SLOT_REF);
1812 } else if (pc_offsets [vmv->vreg]) {
1813 set_slot_in_range (gcfg, pos, 0, pc_offsets [vmv->vreg], SLOT_PIN);
1814 set_slot_in_range (gcfg, pos, pc_offsets [vmv->vreg], cfg->code_size, SLOT_REF);
1816 set_slot_everywhere (gcfg, pos, SLOT_PIN);
1818 if (cfg->verbose_level > 1)
1819 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));
1824 /* Live for the whole method */
1825 set_slot_everywhere (gcfg, pos, SLOT_REF);
1827 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex)
1828 if (gcfg->callsites [cindex]->liveness [i / 8] & (1 << (i % 8)))
1829 set_slot (gcfg, pos, cindex, SLOT_REF);
1832 if (cfg->verbose_level > 1) {
1833 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));
1837 g_free (pc_offsets);
1841 sp_offset_to_fp_offset (MonoCompile *cfg, int sp_offset)
1844 * Convert a sp relative offset to a slot index. This is
1845 * platform specific.
1848 /* fp = sp + offset */
1849 g_assert (cfg->frame_reg == AMD64_RBP);
1850 return (- cfg->arch.sp_fp_offset + sp_offset);
1851 #elif defined(TARGET_X86)
1852 /* The offset is computed from the sp at the start of the call sequence */
1853 g_assert (cfg->frame_reg == X86_EBP);
1854 return (- cfg->arch.sp_fp_offset - sp_offset);
1862 process_param_area_slots (MonoCompile *cfg)
1864 MonoCompileGC *gcfg = cfg->gc_info;
1869 * These slots are used for passing parameters during calls. They are sp relative, not
1870 * fp relative, so they are harder to handle.
1872 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1873 /* The distance between fp and sp is not constant */
1876 is_param = mono_mempool_alloc0 (cfg->mempool, gcfg->nslots * sizeof (gboolean));
1878 for (cindex = 0; cindex < gcfg->ncallsites; ++cindex) {
1879 GCCallSite *callsite = gcfg->callsites [cindex];
1882 for (l = callsite->param_slots; l; l = l->next) {
1883 MonoInst *def = l->data;
1884 MonoType *t = def->inst_vtype;
1885 int sp_offset = def->inst_offset;
1886 int fp_offset = sp_offset_to_fp_offset (cfg, sp_offset);
1887 int slot = fp_offset_to_slot (cfg, fp_offset);
1891 if (MONO_TYPE_ISSTRUCT (t)) {
1892 size = mini_type_stack_size_full (cfg->generic_sharing_context, t, &align, FALSE);
1894 size = sizeof (mgreg_t);
1897 for (i = 0; i < size / sizeof (mgreg_t); ++i) {
1898 g_assert (slot + i >= 0 && slot + i < gcfg->nslots);
1899 is_param [slot + i] = TRUE;
1904 /* All param area slots are noref by default */
1905 for (i = 0; i < gcfg->nslots; ++i) {
1907 set_slot_everywhere (gcfg, i, SLOT_NOREF);
1911 * We treat param area slots as being part of the callee's frame, to be able to handle tail calls which overwrite
1912 * the argument area of the caller.
1917 process_finally_clauses (MonoCompile *cfg)
1919 MonoCompileGC *gcfg = cfg->gc_info;
1920 GCCallSite **callsites;
1922 gboolean has_finally;
1923 int i, j, nslots, nregs;
1925 ncallsites = gcfg->ncallsites;
1926 nslots = gcfg->nslots;
1927 nregs = gcfg->nregs;
1928 callsites = gcfg->callsites;
1931 * The calls to the finally clauses don't show up in the cfg. See
1932 * test_0_liveness_8 ().
1933 * Variables accessed inside the finally clause are already marked VOLATILE by
1934 * mono_liveness_handle_exception_clauses (). Variables not accessed inside the finally clause have
1935 * correct liveness outside the finally clause. So mark them PIN inside the finally clauses.
1937 has_finally = FALSE;
1938 for (i = 0; i < cfg->header->num_clauses; ++i) {
1939 MonoExceptionClause *clause = &cfg->header->clauses [i];
1941 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1946 if (cfg->verbose_level > 1)
1947 printf ("\tMethod has finally clauses, pessimizing live ranges.\n");
1948 for (j = 0; j < ncallsites; ++j) {
1949 MonoBasicBlock *bb = callsites [j]->bb;
1950 MonoExceptionClause *clause;
1951 gboolean is_in_finally = FALSE;
1953 for (i = 0; i < cfg->header->num_clauses; ++i) {
1954 clause = &cfg->header->clauses [i];
1956 if (MONO_OFFSET_IN_HANDLER (clause, bb->real_offset)) {
1957 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1958 is_in_finally = TRUE;
1964 if (is_in_finally) {
1965 for (i = 0; i < nslots; ++i)
1966 set_slot (gcfg, i, j, SLOT_PIN);
1967 for (i = 0; i < nregs; ++i)
1968 set_reg_slot (gcfg, i, j, SLOT_PIN);
1975 compute_frame_size (MonoCompile *cfg)
1977 int i, locals_min_offset, locals_max_offset, cfa_min_offset, cfa_max_offset;
1978 int min_offset, max_offset;
1979 MonoCompileGC *gcfg = cfg->gc_info;
1980 MonoMethodSignature *sig = mono_method_signature (cfg->method);
1983 /* Compute min/max offsets from the fp */
1986 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_S390X)
1987 locals_min_offset = ALIGN_TO (cfg->locals_min_stack_offset, SIZEOF_SLOT);
1988 locals_max_offset = cfg->locals_max_stack_offset;
1990 /* min/max stack offset needs to be computed in mono_arch_allocate_vars () */
1994 locals_min_offset = ALIGN_TO (locals_min_offset, SIZEOF_SLOT);
1995 locals_max_offset = ALIGN_TO (locals_max_offset, SIZEOF_SLOT);
1997 min_offset = locals_min_offset;
1998 max_offset = locals_max_offset;
2001 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2002 MonoInst *ins = cfg->args [i];
2004 if (ins->opcode == OP_REGOFFSET) {
2005 int size, size_in_slots;
2006 size = mini_type_stack_size_full (cfg->generic_sharing_context, ins->inst_vtype, NULL, ins->backend.is_pinvoke);
2007 size_in_slots = ALIGN_TO (size, SIZEOF_SLOT) / SIZEOF_SLOT;
2009 min_offset = MIN (min_offset, ins->inst_offset);
2010 max_offset = MAX ((int)max_offset, (int)(ins->inst_offset + (size_in_slots * SIZEOF_SLOT)));
2015 g_assert (cfg->frame_reg == cfg->cfa_reg);
2016 g_assert (cfg->cfa_offset > 0);
2018 cfa_max_offset = cfg->cfa_offset;
2020 min_offset = MIN (min_offset, cfa_min_offset);
2021 max_offset = MAX (max_offset, cfa_max_offset);
2023 /* Fp relative slots */
2024 for (l = gcfg->stack_slots_from_fp; l; l = l->next) {
2025 gint data = GPOINTER_TO_INT (l->data);
2026 int offset = data >> 16;
2028 min_offset = MIN (min_offset, offset);
2032 if (!(cfg->flags & MONO_CFG_HAS_SPILLUP)) {
2033 int stack_offset = ALIGN_TO (cfg->stack_offset, SIZEOF_SLOT);
2034 min_offset = MIN (min_offset, (-stack_offset));
2037 /* Param area slots */
2039 min_offset = MIN (min_offset, -cfg->arch.sp_fp_offset);
2040 #elif defined(TARGET_X86)
2041 min_offset = MIN (min_offset, - (cfg->arch.sp_fp_offset + cfg->arch.param_area_size));
2042 #elif defined(TARGET_ARM)
2044 #elif defined(TARGET_s390X)
2050 gcfg->min_offset = min_offset;
2051 gcfg->max_offset = max_offset;
2052 gcfg->locals_min_offset = locals_min_offset;
2053 gcfg->locals_max_offset = locals_max_offset;
2057 init_gcfg (MonoCompile *cfg)
2059 int i, nregs, nslots;
2060 MonoCompileGC *gcfg = cfg->gc_info;
2061 GCCallSite **callsites;
2070 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2071 ncallsites += g_slist_length (bb->gc_callsites);
2073 callsites = mono_mempool_alloc0 (cfg->mempool, ncallsites * sizeof (GCCallSite*));
2075 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2076 for (l = bb->gc_callsites; l; l = l->next)
2077 callsites [i++] = l->data;
2080 /* The callsites should already be ordered by pc offset */
2081 for (i = 1; i < ncallsites; ++i)
2082 g_assert (callsites [i - 1]->pc_offset < callsites [i]->pc_offset);
2085 * The stack frame looks like this:
2087 * <fp + max_offset> == cfa -> <end of previous frame>
2088 * <other stack slots>
2090 * <other stack slots>
2091 * fp + min_offset ->
2096 if (cfg->verbose_level > 1)
2097 printf ("GC Map for %s: 0x%x-0x%x\n", mono_method_full_name (cfg->method, TRUE), gcfg->min_offset, gcfg->max_offset);
2099 nslots = (gcfg->max_offset - gcfg->min_offset) / SIZEOF_SLOT;
2102 gcfg->nslots = nslots;
2103 gcfg->nregs = nregs;
2104 gcfg->callsites = callsites;
2105 gcfg->ncallsites = ncallsites;
2106 gcfg->stack_bitmap_width = ALIGN_TO (ncallsites, 8) / 8;
2107 gcfg->reg_bitmap_width = ALIGN_TO (ncallsites, 8) / 8;
2108 gcfg->stack_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * nslots);
2109 gcfg->stack_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->stack_bitmap_width * nslots);
2110 gcfg->reg_ref_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * nregs);
2111 gcfg->reg_pin_bitmap = mono_mempool_alloc0 (cfg->mempool, gcfg->reg_bitmap_width * nregs);
2113 /* All slots start out as PIN */
2114 memset (gcfg->stack_pin_bitmap, 0xff, gcfg->stack_bitmap_width * nregs);
2115 for (i = 0; i < nregs; ++i) {
2117 * By default, registers are NOREF.
2118 * It is possible for a callee to save them before being defined in this method,
2119 * but the saved value is dead too, so it doesn't need to be marked.
2121 if ((cfg->used_int_regs & (1 << i)))
2122 set_reg_slot_everywhere (gcfg, i, SLOT_NOREF);
2126 static inline gboolean
2127 has_bit_set (guint8 *bitmap, int width, int slot)
2130 int pos = width * slot;
2132 for (i = 0; i < width; ++i) {
2133 if (bitmap [pos + i])
2140 create_map (MonoCompile *cfg)
2143 int i, j, nregs, nslots, nref_regs, npin_regs, alloc_size, bitmaps_size, bitmaps_offset;
2145 int stack_bitmap_width, stack_bitmap_size, reg_ref_bitmap_width, reg_ref_bitmap_size;
2146 int reg_pin_bitmap_width, reg_pin_bitmap_size, bindex;
2148 gboolean has_ref_slots, has_pin_slots, has_ref_regs, has_pin_regs;
2149 MonoCompileGC *gcfg = cfg->gc_info;
2150 GCCallSite **callsites;
2152 guint8 *bitmap, *bitmaps;
2153 guint32 reg_ref_mask, reg_pin_mask;
2155 ncallsites = gcfg->ncallsites;
2156 nslots = gcfg->nslots;
2157 nregs = gcfg->nregs;
2158 callsites = gcfg->callsites;
2161 * Compute the real size of the bitmap i.e. ignore NOREF columns at the beginning and at
2162 * the end. Also, compute whenever the map needs ref/pin bitmaps, and collect stats.
2164 has_ref_slots = FALSE;
2165 has_pin_slots = FALSE;
2168 memset (ntypes, 0, sizeof (ntypes));
2169 for (i = 0; i < nslots; ++i) {
2170 gboolean has_ref = FALSE;
2171 gboolean has_pin = FALSE;
2173 if (has_bit_set (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, i))
2175 if (has_bit_set (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, i))
2179 has_ref_slots = TRUE;
2181 has_pin_slots = TRUE;
2184 ntypes [SLOT_REF] ++;
2186 ntypes [SLOT_PIN] ++;
2188 ntypes [SLOT_NOREF] ++;
2190 if (has_ref || has_pin) {
2197 start = end = nslots;
2199 g_assert (start != -1);
2200 g_assert (start < end);
2203 has_ref_regs = FALSE;
2204 has_pin_regs = FALSE;
2209 for (i = 0; i < nregs; ++i) {
2210 gboolean has_ref = FALSE;
2211 gboolean has_pin = FALSE;
2213 if (!(cfg->used_int_regs & (1 << i)))
2216 if (has_bit_set (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, i))
2218 if (has_bit_set (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, i))
2222 reg_ref_mask |= (1 << i);
2223 has_ref_regs = TRUE;
2227 reg_pin_mask |= (1 << i);
2228 has_pin_regs = TRUE;
2233 if (cfg->verbose_level > 1)
2234 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);
2236 /* Create the GC Map */
2238 /* The work bitmaps have one row for each slot, since this is how we access them during construction */
2239 stack_bitmap_width = ALIGN_TO (end - start, 8) / 8;
2240 stack_bitmap_size = stack_bitmap_width * ncallsites;
2241 reg_ref_bitmap_width = ALIGN_TO (nref_regs, 8) / 8;
2242 reg_ref_bitmap_size = reg_ref_bitmap_width * ncallsites;
2243 reg_pin_bitmap_width = ALIGN_TO (npin_regs, 8) / 8;
2244 reg_pin_bitmap_size = reg_pin_bitmap_width * ncallsites;
2245 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);
2247 map = mono_mempool_alloc0 (cfg->mempool, sizeof (GCMap));
2249 map->frame_reg = cfg->frame_reg;
2250 map->start_offset = gcfg->min_offset;
2251 map->end_offset = gcfg->min_offset + (nslots * SIZEOF_SLOT);
2252 map->map_offset = start * SIZEOF_SLOT;
2253 map->nslots = end - start;
2254 map->has_ref_slots = has_ref_slots;
2255 map->has_pin_slots = has_pin_slots;
2256 map->has_ref_regs = has_ref_regs;
2257 map->has_pin_regs = has_pin_regs;
2258 g_assert (nregs < 32);
2259 map->used_int_regs = cfg->used_int_regs;
2260 map->reg_ref_mask = reg_ref_mask;
2261 map->reg_pin_mask = reg_pin_mask;
2262 map->nref_regs = nref_regs;
2263 map->npin_regs = npin_regs;
2265 bitmaps = mono_mempool_alloc0 (cfg->mempool, bitmaps_size);
2268 if (has_ref_slots) {
2269 map->stack_ref_bitmap_offset = bitmaps_offset;
2270 bitmaps_offset += stack_bitmap_size;
2272 bitmap = &bitmaps [map->stack_ref_bitmap_offset];
2273 for (i = 0; i < nslots; ++i) {
2274 for (j = 0; j < ncallsites; ++j) {
2275 if (get_bit (gcfg->stack_ref_bitmap, gcfg->stack_bitmap_width, i, j))
2276 set_bit (bitmap, stack_bitmap_width, j, i - start);
2280 if (has_pin_slots) {
2281 map->stack_pin_bitmap_offset = bitmaps_offset;
2282 bitmaps_offset += stack_bitmap_size;
2284 bitmap = &bitmaps [map->stack_pin_bitmap_offset];
2285 for (i = 0; i < nslots; ++i) {
2286 for (j = 0; j < ncallsites; ++j) {
2287 if (get_bit (gcfg->stack_pin_bitmap, gcfg->stack_bitmap_width, i, j))
2288 set_bit (bitmap, stack_bitmap_width, j, i - start);
2293 map->reg_ref_bitmap_offset = bitmaps_offset;
2294 bitmaps_offset += reg_ref_bitmap_size;
2296 bitmap = &bitmaps [map->reg_ref_bitmap_offset];
2298 for (i = 0; i < nregs; ++i) {
2299 if (reg_ref_mask & (1 << i)) {
2300 for (j = 0; j < ncallsites; ++j) {
2301 if (get_bit (gcfg->reg_ref_bitmap, gcfg->reg_bitmap_width, i, j))
2302 set_bit (bitmap, reg_ref_bitmap_width, j, bindex);
2309 map->reg_pin_bitmap_offset = bitmaps_offset;
2310 bitmaps_offset += reg_pin_bitmap_size;
2312 bitmap = &bitmaps [map->reg_pin_bitmap_offset];
2314 for (i = 0; i < nregs; ++i) {
2315 if (reg_pin_mask & (1 << i)) {
2316 for (j = 0; j < ncallsites; ++j) {
2317 if (get_bit (gcfg->reg_pin_bitmap, gcfg->reg_bitmap_width, i, j))
2318 set_bit (bitmap, reg_pin_bitmap_width, j, bindex);
2326 map->ncallsites = ncallsites;
2327 if (cfg->code_len < 256)
2328 map->callsite_entry_size = 1;
2329 else if (cfg->code_len < 65536)
2330 map->callsite_entry_size = 2;
2332 map->callsite_entry_size = 4;
2334 /* Encode the GC Map */
2342 encode_gc_map (map, buf, &endbuf);
2343 g_assert (endbuf - buf < 256);
2345 encoded_size = endbuf - buf;
2346 alloc_size = sizeof (GCEncodedMap) + ALIGN_TO (encoded_size, map->callsite_entry_size) + (map->callsite_entry_size * map->ncallsites) + bitmaps_size;
2348 emap = mono_domain_alloc0 (cfg->domain, alloc_size);
2349 //emap->ref_slots = map->ref_slots;
2351 /* Encoded fixed fields */
2352 p = &emap->encoded [0];
2353 //emap->encoded_size = encoded_size;
2354 memcpy (p, buf, encoded_size);
2357 /* Callsite table */
2358 p = (guint8*)ALIGN_TO ((mgreg_t)p, map->callsite_entry_size);
2359 if (map->callsite_entry_size == 1) {
2360 guint8 *offsets = p;
2361 for (i = 0; i < ncallsites; ++i)
2362 offsets [i] = callsites [i]->pc_offset;
2363 stats.gc_callsites8_size += ncallsites * sizeof (guint8);
2364 } else if (map->callsite_entry_size == 2) {
2365 guint16 *offsets = (guint16*)p;
2366 for (i = 0; i < ncallsites; ++i)
2367 offsets [i] = callsites [i]->pc_offset;
2368 stats.gc_callsites16_size += ncallsites * sizeof (guint16);
2370 guint32 *offsets = (guint32*)p;
2371 for (i = 0; i < ncallsites; ++i)
2372 offsets [i] = callsites [i]->pc_offset;
2373 stats.gc_callsites32_size += ncallsites * sizeof (guint32);
2375 p += ncallsites * map->callsite_entry_size;
2378 memcpy (p, bitmaps, bitmaps_size);
2381 g_assert ((guint8*)p - (guint8*)emap <= alloc_size);
2383 stats.gc_maps_size += alloc_size;
2384 stats.gc_callsites_size += ncallsites * map->callsite_entry_size;
2385 stats.gc_bitmaps_size += bitmaps_size;
2386 stats.gc_map_struct_size += sizeof (GCEncodedMap) + encoded_size;
2388 cfg->jit_info->gc_info = emap;
2390 cfg->gc_map = (guint8*)emap;
2391 cfg->gc_map_size = alloc_size;
2394 stats.all_slots += nslots;
2395 stats.ref_slots += ntypes [SLOT_REF];
2396 stats.noref_slots += ntypes [SLOT_NOREF];
2397 stats.pin_slots += ntypes [SLOT_PIN];
2401 mini_gc_create_gc_map (MonoCompile *cfg)
2403 if (!cfg->compute_gc_maps)
2407 * During marking, all frames except the top frame are at a call site, and we mark the
2408 * top frame conservatively. This means that we only need to compute and record
2409 * GC maps for call sites.
2412 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
2413 /* Without liveness info, the live ranges are not precise enough */
2416 mono_analyze_liveness_gc (cfg);
2418 compute_frame_size (cfg);
2422 process_spill_slots (cfg);
2423 process_other_slots (cfg);
2424 process_param_area_slots (cfg);
2425 process_variables (cfg);
2426 process_finally_clauses (cfg);
2432 parse_debug_options (void)
2437 env = getenv ("MONO_GCMAP_DEBUG");
2441 opts = g_strsplit (env, ",", -1);
2442 for (ptr = opts; ptr && *ptr; ptr ++) {
2443 /* No options yet */
2444 fprintf (stderr, "Invalid format for the MONO_GCMAP_DEBUG env variable: '%s'\n", env);
2455 memset (&cb, 0, sizeof (cb));
2456 cb.thread_attach_func = thread_attach_func;
2457 cb.thread_detach_func = thread_detach_func;
2458 cb.thread_suspend_func = thread_suspend_func;
2459 /* Comment this out to disable precise stack marking */
2460 cb.thread_mark_func = thread_mark_func;
2461 mono_gc_set_gc_callbacks (&cb);
2463 logfile = mono_gc_get_logfile ();
2465 parse_debug_options ();
2467 mono_counters_register ("GC Maps size",
2468 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_maps_size);
2469 mono_counters_register ("GC Call Sites size",
2470 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites_size);
2471 mono_counters_register ("GC Bitmaps size",
2472 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_bitmaps_size);
2473 mono_counters_register ("GC Map struct size",
2474 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_map_struct_size);
2475 mono_counters_register ("GC Call Sites encoded using 8 bits",
2476 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites8_size);
2477 mono_counters_register ("GC Call Sites encoded using 16 bits",
2478 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites16_size);
2479 mono_counters_register ("GC Call Sites encoded using 32 bits",
2480 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.gc_callsites32_size);
2482 mono_counters_register ("GC Map slots (all)",
2483 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.all_slots);
2484 mono_counters_register ("GC Map slots (ref)",
2485 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.ref_slots);
2486 mono_counters_register ("GC Map slots (noref)",
2487 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.noref_slots);
2488 mono_counters_register ("GC Map slots (pin)",
2489 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.pin_slots);
2491 mono_counters_register ("GC TLS Data size",
2492 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.tlsdata_size);
2494 mono_counters_register ("Stack space scanned (all)",
2495 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_stacks);
2496 mono_counters_register ("Stack space scanned (native)",
2497 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_native);
2498 mono_counters_register ("Stack space scanned (other)",
2499 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_other);
2500 mono_counters_register ("Stack space scanned (using GC Maps)",
2501 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned);
2502 mono_counters_register ("Stack space scanned (precise)",
2503 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_precisely);
2504 mono_counters_register ("Stack space scanned (pin)",
2505 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_conservatively);
2506 mono_counters_register ("Stack space scanned (pin registers)",
2507 MONO_COUNTER_GC | MONO_COUNTER_INT, &stats.scanned_registers);
2518 mini_gc_init_gc_map (MonoCompile *cfg)
2523 mini_gc_create_gc_map (MonoCompile *cfg)
2528 mini_gc_set_slot_type_from_fp (MonoCompile *cfg, int slot_offset, GCSlotType type)
2533 mini_gc_set_slot_type_from_cfa (MonoCompile *cfg, int slot_offset, GCSlotType type)
2542 * Set GC specific options in CFG.
2545 mini_gc_init_cfg (MonoCompile *cfg)
2547 if (mono_gc_is_moving ()) {
2548 cfg->disable_ref_noref_stack_slot_share = TRUE;
2549 cfg->gen_write_barriers = TRUE;
2552 mini_gc_init_gc_map (cfg);
2556 * Problems with the current code:
2557 * - the stack walk is slow
2558 * - vtypes/refs used in EH regions are treated conservatively
2559 * - if the code is finished, less pinning will be done, causing problems because
2560 * we promote all surviving objects to old-gen.
2561 * - the unwind code can't handle a method stopped inside a finally region, it thinks the caller is
2562 * another method, but in reality it is either the exception handling code or the CALL_HANDLER opcode.
2563 * This manifests in "Unable to find ip offset x in callsite list" assertions.
2564 * - the unwind code also can't handle frames which are in the epilog, since the unwind info is not
2569 * Ideas for creating smaller GC maps:
2570 * - remove empty columns from the bitmaps. This requires adding a mask bit array for
2572 * - merge reg and stack slot bitmaps, so the unused bits at the end of the reg bitmap are
2574 * - if the bitmap width is not a multiple of 8, the remaining bits are wasted.
2575 * - group ref and non-ref stack slots together in mono_allocate_stack_slots ().
2576 * - add an index for the callsite table so that each entry can be encoded as a 1 byte difference
2577 * from an index entry.