This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
gcinfoencoder.cpp
2706 lines (2285 loc) · 93.1 KB
/
gcinfoencoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/*****************************************************************************
*
* GC Information Encoding API
*
*/
#include <stdint.h>
#include "gcinfoencoder.h"
#ifdef _DEBUG
#ifndef LOGGING
#define LOGGING
#endif
#endif
#ifndef STANDALONE_BUILD
#include "log.h"
#include "simplerhash.h"
#include "bitposition.h"
#endif
#ifdef MEASURE_GCINFO
#define GCINFO_WRITE(writer, val, numBits, counter) \
{ \
writer.Write(val, numBits); \
m_CurrentMethodSize.counter += numBits; \
m_CurrentMethodSize.TotalSize += numBits; \
}
#define GCINFO_WRITE_VARL_U(writer, val, base, counter) \
{ \
size_t __temp = \
writer.EncodeVarLengthUnsigned(val, base); \
m_CurrentMethodSize.counter += __temp; \
m_CurrentMethodSize.TotalSize += __temp; \
}
#define GCINFO_WRITE_VARL_S(writer, val, base, counter) \
{ \
size_t __temp = \
writer.EncodeVarLengthSigned(val, base); \
m_CurrentMethodSize.counter += __temp; \
m_CurrentMethodSize.TotalSize += __temp; \
}
#define GCINFO_WRITE_VECTOR(writer, vector, counter) \
{ \
WriteSlotStateVector(writer, vector); \
for(UINT32 i = 0; i < m_NumSlots; i++) \
{ \
if(!m_SlotTable[i].IsDeleted() && \
!m_SlotTable[i].IsUntracked()) \
{ \
m_CurrentMethodSize.counter++; \
m_CurrentMethodSize.TotalSize++; \
} \
} \
}
#define GCINFO_WRITE_VAR_VECTOR(writer, vector, baseSkip, baseRun, counter) \
{ \
size_t __temp = \
WriteSlotStateVarLengthVector(writer, vector, baseSkip, baseRun); \
m_CurrentMethodSize.counter += __temp; \
m_CurrentMethodSize.TotalSize += __temp; \
}
#else
#define GCINFO_WRITE(writer, val, numBits, counter) \
writer.Write(val, numBits);
#define GCINFO_WRITE_VARL_U(writer, val, base, counter) \
writer.EncodeVarLengthUnsigned(val, base);
#define GCINFO_WRITE_VARL_S(writer, val, base, counter) \
writer.EncodeVarLengthSigned(val, base);
#define GCINFO_WRITE_VECTOR(writer, vector, counter) \
WriteSlotStateVector(writer, vector);
#define GCINFO_WRITE_VAR_VECTOR(writer, vector, baseSkip, baseRun, counter) \
WriteSlotStateVarLengthVector(writer, vector, baseSkip, baseRun);
#endif
#define LOG_GCSLOTDESC_FMT "%s%c%d%s%s%s"
#define LOG_GCSLOTDESC_ARGS(pDesc) (pDesc)->IsRegister() ? "register" \
: GcStackSlotBaseNames[(pDesc)->Slot.Stack.Base], \
(pDesc)->IsRegister() ? ' ' : (pDesc)->Slot.Stack.SpOffset < 0 ? '-' : '+', \
(pDesc)->IsRegister() ? (pDesc)->Slot.RegisterNumber \
: ((pDesc)->Slot.Stack.SpOffset), \
(pDesc)->IsPinned() ? " pinned" : "", \
(pDesc)->IsInterior() ? " interior" : "", \
(pDesc)->IsUntracked() ? " untracked" : ""
#define LOG_REGTRANSITION_FMT "register %u%s%s"
#define LOG_REGTRANSITION_ARGS(RegisterNumber, Flags) \
RegisterNumber, \
(Flags & GC_SLOT_PINNED) ? " pinned" : "", \
(Flags & GC_SLOT_INTERIOR) ? " interior" : ""
#define LOG_STACKTRANSITION_FMT "%s%c%d%s%s%s"
#define LOG_STACKTRANSITION_ARGS(BaseRegister, StackOffset, Flags) \
GcStackSlotBaseNames[BaseRegister], \
((StackOffset) < 0) ? '-' : '+', \
((StackOffset) >= 0) ? (StackOffset) \
: -(StackOffset), \
(Flags & GC_SLOT_PINNED) ? " pinned" : "", \
(Flags & GC_SLOT_INTERIOR) ? " interior" : "", \
(Flags & GC_SLOT_UNTRACKED) ? " untracked" : ""
class BitArray
{
friend class BitArrayIterator;
typedef uint32_t ChunkType;
static constexpr size_t NumBitsPerChunk = sizeof(ChunkType) * CHAR_BIT;
public:
BitArray(IAllocator* pJitAllocator, size_t numBits)
{
const size_t numChunks = (numBits + NumBitsPerChunk - 1) / NumBitsPerChunk;
m_pData = (ChunkType*)pJitAllocator->Alloc(sizeof(ChunkType) * numChunks);
m_pEndData = m_pData + numChunks;
#ifdef MUST_CALL_IALLOCATOR_FREE
m_pJitAllocator = pJitAllocator;
#endif
}
inline void SetBit( size_t pos )
{
size_t element = pos / NumBitsPerChunk;
int bpos = (int)(pos % NumBitsPerChunk);
m_pData[element] |= ((ChunkType)1 << bpos);
}
inline void ClearBit( size_t pos )
{
size_t element = pos / NumBitsPerChunk;
int bpos = (int)(pos % NumBitsPerChunk);
m_pData[element] &= ~((ChunkType)1 << bpos);
}
inline void SetAll()
{
ChunkType* ptr = m_pData;
while(ptr < m_pEndData)
*(ptr++) = ~(ChunkType)0;
}
inline void ClearAll()
{
ChunkType* ptr = m_pData;
while(ptr < m_pEndData)
*(ptr++) = (ChunkType)0;
}
inline void WriteBit( size_t pos, BOOL val)
{
if(val)
SetBit(pos);
else
ClearBit(pos);
}
inline uint32_t ReadBit( size_t pos ) const
{
size_t element = pos / NumBitsPerChunk;
int bpos = (int)(pos % NumBitsPerChunk);
return (m_pData[element] & ((ChunkType)1 << bpos));
}
inline bool operator==(const BitArray &other) const
{
_ASSERTE(other.m_pEndData - other.m_pData == m_pEndData - m_pData);
ChunkType* dest = m_pData;
ChunkType* src = other.m_pData;
return 0 == memcmp(dest, src, (m_pEndData - m_pData) * sizeof(ChunkType));
}
inline int GetHashCode() const
{
const int* src = (const int*)m_pData;
int result = *src++;
while (src < (const int*)m_pEndData)
result = _rotr(result, 5) ^ *src++;
return result;
}
inline BitArray& operator=(const BitArray &other)
{
_ASSERTE(other.m_pEndData - other.m_pData == m_pEndData - m_pData);
ChunkType* dest = m_pData;
ChunkType* src = other.m_pData;
while(dest < m_pEndData)
*(dest++) = *(src++);
return *this;
}
inline BitArray& operator|=(const BitArray &other)
{
_ASSERTE(other.m_pEndData - other.m_pData == m_pEndData - m_pData);
ChunkType* dest = m_pData;
ChunkType* src = other.m_pData;
while(dest < m_pEndData)
*(dest++) |= *(src++);
return *this;
}
#ifdef MUST_CALL_IALLOCATOR_FREE
~BitArray()
{
m_pAllocator->Free( m_pData );
}
#endif
static void* operator new(size_t size, IAllocator* allocator)
{
return allocator->Alloc(size);
}
private:
ChunkType * m_pData;
ChunkType * m_pEndData;
#ifdef MUST_CALL_IALLOCATOR_FREE
IAllocator* m_pJitAllocator;
#endif
};
class BitArrayIterator
{
public:
BitArrayIterator(BitArray* bitArray)
{
m_pCurData = (unsigned *)bitArray->m_pData;
m_pEndData = (unsigned *)bitArray->m_pEndData;
m_curBits = *m_pCurData;
m_curBit = 0;
m_curBase = 0;
GetNext();
}
void operator++(int dummy) //int dummy is c++ for "this is postfix ++"
{
GetNext();
}
void operator++() // prefix ++
{
GetNext();
}
void GetNext()
{
m_curBits -= m_curBit;
while (m_curBits == 0)
{
m_pCurData++;
m_curBase += 32;
if (m_pCurData == m_pEndData)
break;
m_curBits = *m_pCurData;
}
m_curBit = (unsigned)((int)m_curBits & -(int)m_curBits);
}
unsigned operator*()
{
assert(!end() && (m_curBit != 0));
unsigned bitPosition = BitPosition(m_curBit);
return bitPosition + m_curBase;
}
bool end()
{
return (m_pCurData == m_pEndData);
}
private:
unsigned* m_pCurData;
unsigned* m_pEndData;
unsigned m_curBits;
unsigned m_curBit;
unsigned m_curBase;
};
class LiveStateFuncs
{
public:
static int GetHashCode(const BitArray * key)
{
return key->GetHashCode();
}
static bool Equals(const BitArray * k1, const BitArray * k2)
{
return *k1 == *k2;
}
};
class GcInfoNoMemoryException
{
};
class GcInfoHashBehavior
{
public:
static const unsigned s_growth_factor_numerator = 3;
static const unsigned s_growth_factor_denominator = 2;
static const unsigned s_density_factor_numerator = 3;
static const unsigned s_density_factor_denominator = 4;
static const unsigned s_minimum_allocation = 7;
inline static void DECLSPEC_NORETURN NoMemory()
{
throw GcInfoNoMemoryException();
}
};
typedef SimplerHashTable<const BitArray *, LiveStateFuncs, UINT32, GcInfoHashBehavior> LiveStateHashTable;
#ifdef MEASURE_GCINFO
// Fi = fully-interruptible; we count any method that has one or more interruptible ranges
// Pi = partially-interruptible; methods with zero fully-interruptible ranges
GcInfoSize g_FiGcInfoSize;
GcInfoSize g_PiGcInfoSize;
// Number of methods with GcInfo that have SlimHeader
size_t g_NumSlimHeaders = 0;
// Number of methods with GcInfo that have FatHeader
size_t g_NumFatHeaders = 0;
GcInfoSize::GcInfoSize()
{
memset(this, 0, sizeof(*this));
}
GcInfoSize& GcInfoSize::operator+=(const GcInfoSize& other)
{
TotalSize += other.TotalSize;
NumMethods += other.NumMethods;
NumCallSites += other.NumCallSites;
NumRanges += other.NumRanges;
NumRegs += other.NumRegs;
NumStack += other.NumStack;
NumUntracked += other.NumUntracked;
NumTransitions += other.NumTransitions;
SizeOfCode += other.SizeOfCode;
EncPreservedSlots += other.EncPreservedSlots;
UntrackedSlotSize += other.UntrackedSlotSize;
NumUntrackedSize += other.NumUntrackedSize;
FlagsSize += other.FlagsSize;
CodeLengthSize += other.CodeLengthSize;
ProEpilogSize += other.ProEpilogSize;
SecObjSize += other.SecObjSize;
GsCookieSize += other.GsCookieSize;
GenericsCtxSize += other.GenericsCtxSize;
PspSymSize += other.PspSymSize;
StackBaseSize += other.StackBaseSize;
ReversePInvokeFrameSize += other.ReversePInvokeFrameSize;
FixedAreaSize += other.FixedAreaSize;
NumCallSitesSize += other.NumCallSitesSize;
NumRangesSize += other.NumRangesSize;
CallSitePosSize += other.CallSitePosSize;
RangeSize += other.RangeSize;
NumRegsSize += other.NumRegsSize;
NumStackSize += other.NumStackSize;
RegSlotSize += other.RegSlotSize;
StackSlotSize += other.StackSlotSize;
CallSiteStateSize += other.CallSiteStateSize;
EhPosSize += other.EhPosSize;
EhStateSize += other.EhStateSize;
ChunkPtrSize += other.ChunkPtrSize;
ChunkMaskSize += other.ChunkMaskSize;
ChunkFinalStateSize += other.ChunkFinalStateSize;
ChunkTransitionSize += other.ChunkTransitionSize;
return *this;
}
void GcInfoSize::Log(DWORD level, const char * header)
{
if(LoggingOn(LF_GCINFO, level))
{
LogSpew(LF_GCINFO, level, header);
LogSpew(LF_GCINFO, level, "---COUNTS---\n");
LogSpew(LF_GCINFO, level, "NumMethods: %Iu\n", NumMethods);
LogSpew(LF_GCINFO, level, "NumCallSites: %Iu\n", NumCallSites);
LogSpew(LF_GCINFO, level, "NumRanges: %Iu\n", NumRanges);
LogSpew(LF_GCINFO, level, "NumRegs: %Iu\n", NumRegs);
LogSpew(LF_GCINFO, level, "NumStack: %Iu\n", NumStack);
LogSpew(LF_GCINFO, level, "NumUntracked: %Iu\n", NumUntracked);
LogSpew(LF_GCINFO, level, "NumTransitions: %Iu\n", NumTransitions);
LogSpew(LF_GCINFO, level, "SizeOfCode: %Iu\n", SizeOfCode);
LogSpew(LF_GCINFO, level, "EncPreservedSlots: %Iu\n", EncPreservedSlots);
LogSpew(LF_GCINFO, level, "---SIZES(bits)---\n");
LogSpew(LF_GCINFO, level, "Total: %Iu\n", TotalSize);
LogSpew(LF_GCINFO, level, "UntrackedSlot: %Iu\n", UntrackedSlotSize);
LogSpew(LF_GCINFO, level, "NumUntracked: %Iu\n", NumUntrackedSize);
LogSpew(LF_GCINFO, level, "Flags: %Iu\n", FlagsSize);
LogSpew(LF_GCINFO, level, "CodeLength: %Iu\n", CodeLengthSize);
LogSpew(LF_GCINFO, level, "Prolog/Epilog: %Iu\n", ProEpilogSize);
LogSpew(LF_GCINFO, level, "SecObj: %Iu\n", SecObjSize);
LogSpew(LF_GCINFO, level, "GsCookie: %Iu\n", GsCookieSize);
LogSpew(LF_GCINFO, level, "PspSym: %Iu\n", PspSymSize);
LogSpew(LF_GCINFO, level, "GenericsCtx: %Iu\n", GenericsCtxSize);
LogSpew(LF_GCINFO, level, "StackBase: %Iu\n", StackBaseSize);
LogSpew(LF_GCINFO, level, "FixedArea: %Iu\n", FixedAreaSize);
LogSpew(LF_GCINFO, level, "ReversePInvokeFrame: %Iu\n", ReversePInvokeFrameSize);
LogSpew(LF_GCINFO, level, "NumCallSites: %Iu\n", NumCallSitesSize);
LogSpew(LF_GCINFO, level, "NumRanges: %Iu\n", NumRangesSize);
LogSpew(LF_GCINFO, level, "CallSiteOffsets: %Iu\n", CallSitePosSize);
LogSpew(LF_GCINFO, level, "Ranges: %Iu\n", RangeSize);
LogSpew(LF_GCINFO, level, "NumRegs: %Iu\n", NumRegsSize);
LogSpew(LF_GCINFO, level, "NumStack: %Iu\n", NumStackSize);
LogSpew(LF_GCINFO, level, "RegSlots: %Iu\n", RegSlotSize);
LogSpew(LF_GCINFO, level, "StackSlots: %Iu\n", StackSlotSize);
LogSpew(LF_GCINFO, level, "CallSiteStates: %Iu\n", CallSiteStateSize);
LogSpew(LF_GCINFO, level, "EhOffsets: %Iu\n", EhPosSize);
LogSpew(LF_GCINFO, level, "EhStates: %Iu\n", EhStateSize);
LogSpew(LF_GCINFO, level, "ChunkPointers: %Iu\n", ChunkPtrSize);
LogSpew(LF_GCINFO, level, "ChunkMasks: %Iu\n", ChunkMaskSize);
LogSpew(LF_GCINFO, level, "ChunkFinalStates: %Iu\n", ChunkFinalStateSize);
LogSpew(LF_GCINFO, level, "Transitions: %Iu\n", ChunkTransitionSize);
}
}
#endif
GcInfoEncoder::GcInfoEncoder(
ICorJitInfo* pCorJitInfo,
CORINFO_METHOD_INFO* pMethodInfo,
IAllocator* pJitAllocator,
NoMemoryFunction pNoMem
)
: m_Info1( pJitAllocator ),
m_Info2( pJitAllocator ),
m_InterruptibleRanges( pJitAllocator ),
m_LifetimeTransitions( pJitAllocator )
{
#ifdef MEASURE_GCINFO
// This causes multiple complus.log files in JIT64. TODO: consider using ICorJitInfo::logMsg instead.
InitializeLogging();
#endif
_ASSERTE( pCorJitInfo != NULL );
_ASSERTE( pMethodInfo != NULL );
_ASSERTE( pJitAllocator != NULL );
_ASSERTE( pNoMem != NULL );
m_pCorJitInfo = pCorJitInfo;
m_pMethodInfo = pMethodInfo;
m_pAllocator = pJitAllocator;
m_pNoMem = pNoMem;
#ifdef _DEBUG
CORINFO_METHOD_HANDLE methodHandle = pMethodInfo->ftn;
// Get the name of the current method along with the enclosing class
// or module name.
m_MethodName =
pCorJitInfo->getMethodName(methodHandle, (const char **)&m_ModuleName);
#endif
m_SlotTableSize = m_SlotTableInitialSize;
m_SlotTable = (GcSlotDesc*) m_pAllocator->Alloc( m_SlotTableSize*sizeof(GcSlotDesc) );
m_NumSlots = 0;
#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
m_pCallSites = NULL;
m_pCallSiteSizes = NULL;
m_NumCallSites = 0;
#endif
m_SecurityObjectStackSlot = NO_SECURITY_OBJECT;
m_GSCookieStackSlot = NO_GS_COOKIE;
m_GSCookieValidRangeStart = 0;
_ASSERTE(sizeof(m_GSCookieValidRangeEnd) == sizeof(UINT32));
m_GSCookieValidRangeEnd = (UINT32) (-1); // == UINT32.MaxValue
m_PSPSymStackSlot = NO_PSP_SYM;
m_GenericsInstContextStackSlot = NO_GENERICS_INST_CONTEXT;
m_contextParamType = GENERIC_CONTEXTPARAM_NONE;
m_StackBaseRegister = NO_STACK_BASE_REGISTER;
m_SizeOfEditAndContinuePreservedArea = NO_SIZE_OF_EDIT_AND_CONTINUE_PRESERVED_AREA;
m_ReversePInvokeFrameSlot = NO_REVERSE_PINVOKE_FRAME;
#ifdef _TARGET_AMD64_
m_WantsReportOnlyLeaf = false;
#elif defined(_TARGET_ARM_) || defined(_TARGET_ARM64_)
m_HasTailCalls = false;
#endif // _TARGET_AMD64_
m_IsVarArg = false;
m_pLastInterruptibleRange = NULL;
#ifdef _DEBUG
m_IsSlotTableFrozen = FALSE;
#endif //_DEBUG
#ifndef _TARGET_X86_
// If the compiler doesn't set the GCInfo, report RT_Unset.
// This is used for compatibility with JITs that aren't updated to use the new API.
m_ReturnKind = RT_Unset;
#else
m_ReturnKind = RT_Illegal;
#endif // _TARGET_X86_
m_CodeLength = 0;
#ifdef FIXED_STACK_PARAMETER_SCRATCH_AREA
m_SizeOfStackOutgoingAndScratchArea = -1;
#endif // FIXED_STACK_PARAMETER_SCRATCH_AREA
}
#ifdef PARTIALLY_INTERRUPTIBLE_GC_SUPPORTED
void GcInfoEncoder::DefineCallSites(UINT32* pCallSites, BYTE* pCallSiteSizes, UINT32 numCallSites)
{
m_pCallSites = pCallSites;
m_pCallSiteSizes = pCallSiteSizes;
m_NumCallSites = numCallSites;
#ifdef _DEBUG
for(UINT32 i=0; i<numCallSites; i++)
{
_ASSERTE(pCallSiteSizes[i] > 0);
_ASSERTE(DENORMALIZE_CODE_OFFSET(NORMALIZE_CODE_OFFSET(pCallSites[i])) == pCallSites[i]);
if(i > 0)
{
UINT32 prevEnd = pCallSites[i-1] + pCallSiteSizes[i-1];
UINT32 curStart = pCallSites[i];
_ASSERTE(curStart >= prevEnd);
}
}
#endif
}
#endif
GcSlotId GcInfoEncoder::GetRegisterSlotId( UINT32 regNum, GcSlotFlags flags )
{
// We could lookup an existing identical slot in the slot table (via some hashtable mechanism).
// We just create duplicates for now.
#ifdef _DEBUG
_ASSERTE( !m_IsSlotTableFrozen );
#endif
if( m_NumSlots == m_SlotTableSize )
{
GrowSlotTable();
}
_ASSERTE( m_NumSlots < m_SlotTableSize );
_ASSERTE( (flags & (GC_SLOT_IS_REGISTER | GC_SLOT_IS_DELETED | GC_SLOT_UNTRACKED)) == 0 );
m_SlotTable[ m_NumSlots ].Slot.RegisterNumber = regNum;
m_SlotTable[ m_NumSlots ].Flags = (GcSlotFlags) (flags | GC_SLOT_IS_REGISTER);
GcSlotId newSlotId;
newSlotId = m_NumSlots++;
return newSlotId;
}
GcSlotId GcInfoEncoder::GetStackSlotId( INT32 spOffset, GcSlotFlags flags, GcStackSlotBase spBase )
{
// We could lookup an existing identical slot in the slot table (via some hashtable mechanism).
// We just create duplicates for now.
#ifdef _DEBUG
_ASSERTE( !m_IsSlotTableFrozen );
#endif
if( m_NumSlots == m_SlotTableSize )
{
GrowSlotTable();
}
_ASSERTE( m_NumSlots < m_SlotTableSize );
// Not valid to reference anything below the current stack pointer
_ASSERTE(GC_SP_REL != spBase || spOffset >= 0);
_ASSERTE( (flags & (GC_SLOT_IS_REGISTER | GC_SLOT_IS_DELETED)) == 0 );
// the spOffset for the stack slot is required to be pointer size aligned
_ASSERTE((spOffset % TARGET_POINTER_SIZE) == 0);
m_SlotTable[ m_NumSlots ].Slot.Stack.SpOffset = spOffset;
m_SlotTable[ m_NumSlots ].Slot.Stack.Base = spBase;
m_SlotTable[ m_NumSlots ].Flags = flags;
GcSlotId newSlotId;
newSlotId = m_NumSlots++;
return newSlotId;
}
void GcInfoEncoder::GrowSlotTable()
{
m_SlotTableSize *= 2;
GcSlotDesc* newSlotTable = (GcSlotDesc*) m_pAllocator->Alloc( m_SlotTableSize * sizeof(GcSlotDesc) );
memcpy( newSlotTable, m_SlotTable, m_NumSlots * sizeof(GcSlotDesc) );
#ifdef MUST_CALL_JITALLOCATOR_FREE
m_pAllocator->Free( m_SlotTable );
#endif
m_SlotTable = newSlotTable;
}
void GcInfoEncoder::WriteSlotStateVector(BitStreamWriter &writer, const BitArray& vector)
{
for(UINT32 i = 0; i < m_NumSlots && !m_SlotTable[i].IsUntracked(); i++)
{
if(!m_SlotTable[i].IsDeleted())
writer.Write(vector.ReadBit(i) ? 1 : 0, 1);
else
_ASSERTE(vector.ReadBit(i) == 0);
}
}
void GcInfoEncoder::DefineInterruptibleRange( UINT32 startInstructionOffset, UINT32 length )
{
UINT32 stopInstructionOffset = startInstructionOffset + length;
UINT32 normStartOffset = NORMALIZE_CODE_OFFSET(startInstructionOffset);
UINT32 normStopOffset = NORMALIZE_CODE_OFFSET(stopInstructionOffset);
// Ranges must not overlap and must be passed sorted by increasing offset
_ASSERTE(
m_pLastInterruptibleRange == NULL ||
normStartOffset >= m_pLastInterruptibleRange->NormStopOffset
);
// Ignore empty ranges
if(normStopOffset > normStartOffset)
{
if(m_pLastInterruptibleRange
&& normStartOffset == m_pLastInterruptibleRange->NormStopOffset)
{
// Merge adjacent ranges
m_pLastInterruptibleRange->NormStopOffset = normStopOffset;
}
else
{
InterruptibleRange range;
range.NormStartOffset = normStartOffset;
range.NormStopOffset = normStopOffset;
m_pLastInterruptibleRange = m_InterruptibleRanges.Append();
*m_pLastInterruptibleRange = range;
}
}
LOG((LF_GCINFO, LL_INFO1000000, "interruptible at %x length %x\n", startInstructionOffset, length));
}
//
// For inputs, pass zero as offset
//
void GcInfoEncoder::SetSlotState(
UINT32 instructionOffset,
GcSlotId slotId,
GcSlotState slotState
)
{
_ASSERTE( (m_SlotTable[ slotId ].Flags & GC_SLOT_UNTRACKED) == 0 );
LifetimeTransition transition;
transition.SlotId = slotId;
transition.CodeOffset = instructionOffset;
transition.BecomesLive = ( slotState == GC_SLOT_LIVE );
transition.IsDeleted = FALSE;
*( m_LifetimeTransitions.Append() ) = transition;
LOG((LF_GCINFO, LL_INFO1000000, LOG_GCSLOTDESC_FMT " %s at %x\n", LOG_GCSLOTDESC_ARGS(&m_SlotTable[slotId]), slotState == GC_SLOT_LIVE ? "live" : "dead", instructionOffset));
}
void GcInfoEncoder::SetIsVarArg()
{
m_IsVarArg = true;
}
void GcInfoEncoder::SetCodeLength( UINT32 length )
{
_ASSERTE( length > 0 );
_ASSERTE( m_CodeLength == 0 || m_CodeLength == length );
m_CodeLength = length;
}
void GcInfoEncoder::SetSecurityObjectStackSlot( INT32 spOffset )
{
_ASSERTE( spOffset != NO_SECURITY_OBJECT );
#if defined(_TARGET_AMD64_)
_ASSERTE( spOffset < 0x10 && "The security object cannot reside in an input variable!" );
#endif
_ASSERTE( m_SecurityObjectStackSlot == NO_SECURITY_OBJECT || m_SecurityObjectStackSlot == spOffset );
m_SecurityObjectStackSlot = spOffset;
}
void GcInfoEncoder::SetPrologSize( UINT32 prologSize )
{
_ASSERTE(prologSize != 0);
_ASSERTE(m_GSCookieValidRangeStart == 0 || m_GSCookieValidRangeStart == prologSize);
_ASSERTE(m_GSCookieValidRangeEnd == (UINT32)(-1) || m_GSCookieValidRangeEnd == prologSize+1);
m_GSCookieValidRangeStart = prologSize;
// satisfy asserts that assume m_GSCookieValidRangeStart != 0 ==> m_GSCookieValidRangeStart < m_GSCookieValidRangeEnd
m_GSCookieValidRangeEnd = prologSize+1;
}
void GcInfoEncoder::SetGSCookieStackSlot( INT32 spOffsetGSCookie, UINT32 validRangeStart, UINT32 validRangeEnd )
{
_ASSERTE( spOffsetGSCookie != NO_GS_COOKIE );
_ASSERTE( m_GSCookieStackSlot == NO_GS_COOKIE || m_GSCookieStackSlot == spOffsetGSCookie );
_ASSERTE( validRangeStart < validRangeEnd );
m_GSCookieStackSlot = spOffsetGSCookie;
m_GSCookieValidRangeStart = validRangeStart;
m_GSCookieValidRangeEnd = validRangeEnd;
}
void GcInfoEncoder::SetPSPSymStackSlot( INT32 spOffsetPSPSym )
{
_ASSERTE( spOffsetPSPSym != NO_PSP_SYM );
_ASSERTE( m_PSPSymStackSlot == NO_PSP_SYM || m_PSPSymStackSlot == spOffsetPSPSym );
m_PSPSymStackSlot = spOffsetPSPSym;
}
void GcInfoEncoder::SetGenericsInstContextStackSlot( INT32 spOffsetGenericsContext, GENERIC_CONTEXTPARAM_TYPE type)
{
_ASSERTE( spOffsetGenericsContext != NO_GENERICS_INST_CONTEXT);
_ASSERTE( m_GenericsInstContextStackSlot == NO_GENERICS_INST_CONTEXT || m_GenericsInstContextStackSlot == spOffsetGenericsContext );
m_GenericsInstContextStackSlot = spOffsetGenericsContext;
m_contextParamType = type;
}
void GcInfoEncoder::SetStackBaseRegister( UINT32 regNum )
{
_ASSERTE( regNum != NO_STACK_BASE_REGISTER );
_ASSERTE(DENORMALIZE_STACK_BASE_REGISTER(NORMALIZE_STACK_BASE_REGISTER(regNum)) == regNum);
_ASSERTE( m_StackBaseRegister == NO_STACK_BASE_REGISTER || m_StackBaseRegister == regNum );
m_StackBaseRegister = regNum;
}
void GcInfoEncoder::SetSizeOfEditAndContinuePreservedArea( UINT32 slots )
{
_ASSERTE( slots != NO_SIZE_OF_EDIT_AND_CONTINUE_PRESERVED_AREA );
_ASSERTE( m_SizeOfEditAndContinuePreservedArea == NO_SIZE_OF_EDIT_AND_CONTINUE_PRESERVED_AREA );
m_SizeOfEditAndContinuePreservedArea = slots;
}
#ifdef _TARGET_AMD64_
void GcInfoEncoder::SetWantsReportOnlyLeaf()
{
m_WantsReportOnlyLeaf = true;
}
#elif defined(_TARGET_ARM_) || defined(_TARGET_ARM64_)
void GcInfoEncoder::SetHasTailCalls()
{
m_HasTailCalls = true;
}
#endif // _TARGET_AMD64_
#ifdef FIXED_STACK_PARAMETER_SCRATCH_AREA
void GcInfoEncoder::SetSizeOfStackOutgoingAndScratchArea( UINT32 size )
{
_ASSERTE( size != (UINT32)-1 );
_ASSERTE( m_SizeOfStackOutgoingAndScratchArea == (UINT32)-1 || m_SizeOfStackOutgoingAndScratchArea == size );
m_SizeOfStackOutgoingAndScratchArea = size;
}
#endif // FIXED_STACK_PARAMETER_SCRATCH_AREA
void GcInfoEncoder::SetReversePInvokeFrameSlot(INT32 spOffset)
{
m_ReversePInvokeFrameSlot = spOffset;
}
void GcInfoEncoder::SetReturnKind(ReturnKind returnKind)
{
_ASSERTE(IsValidReturnKind(returnKind));
m_ReturnKind = returnKind;
}
struct GcSlotDescAndId
{
GcSlotDesc m_SlotDesc;
UINT32 m_SlotId;
};
int __cdecl CompareSlotDescAndIdBySlotDesc(const void* p1, const void* p2)
{
const GcSlotDesc* pFirst = &reinterpret_cast<const GcSlotDescAndId*>(p1)->m_SlotDesc;
const GcSlotDesc* pSecond = &reinterpret_cast<const GcSlotDescAndId*>(p2)->m_SlotDesc;
int firstFlags = pFirst->Flags ^ GC_SLOT_UNTRACKED;
int secondFlags = pSecond->Flags ^ GC_SLOT_UNTRACKED;
// All registers come before all stack slots
// All untracked come last
// Then sort them by flags, ensuring that the least-frequent interior/pinned flag combinations are first
// This is accomplished in the comparison of flags, since we encode IsRegister in the highest flag bit
// And we XOR the UNTRACKED flag to place them last in the second highest flag bit
if( firstFlags > secondFlags ) return -1;
if( firstFlags < secondFlags ) return 1;
// Then sort them by slot
if( pFirst->IsRegister() )
{
_ASSERTE( pSecond->IsRegister() );
if( pFirst->Slot.RegisterNumber < pSecond->Slot.RegisterNumber ) return -1;
if( pFirst->Slot.RegisterNumber > pSecond->Slot.RegisterNumber ) return 1;
}
else
{
_ASSERTE( !pSecond->IsRegister() );
if( pFirst->Slot.Stack.SpOffset < pSecond->Slot.Stack.SpOffset ) return -1;
if( pFirst->Slot.Stack.SpOffset > pSecond->Slot.Stack.SpOffset ) return 1;
// This is arbitrary, but we want to make sure they are considered separate slots
if( pFirst->Slot.Stack.Base < pSecond->Slot.Stack.Base ) return -1;
if( pFirst->Slot.Stack.Base > pSecond->Slot.Stack.Base ) return 1;
}
// If we get here, the slots are identical
_ASSERTE(!"Duplicate slots definitions found in GC information!");
return 0;
}
int __cdecl CompareLifetimeTransitionsByOffsetThenSlot(const void* p1, const void* p2)
{
const GcInfoEncoder::LifetimeTransition* pFirst = (const GcInfoEncoder::LifetimeTransition*) p1;
const GcInfoEncoder::LifetimeTransition* pSecond = (const GcInfoEncoder::LifetimeTransition*) p2;
UINT32 firstOffset = pFirst->CodeOffset;
UINT32 secondOffset = pSecond->CodeOffset;
if (firstOffset == secondOffset)
{
return pFirst->SlotId - pSecond->SlotId;
}
else
{
return firstOffset - secondOffset;
}
}
int __cdecl CompareLifetimeTransitionsBySlot(const void* p1, const void* p2)
{
const GcInfoEncoder::LifetimeTransition* pFirst = (const GcInfoEncoder::LifetimeTransition*) p1;
const GcInfoEncoder::LifetimeTransition* pSecond = (const GcInfoEncoder::LifetimeTransition*) p2;
UINT32 firstOffset = pFirst->CodeOffset;
UINT32 secondOffset = pSecond->CodeOffset;
_ASSERTE(GetNormCodeOffsetChunk(firstOffset) == GetNormCodeOffsetChunk(secondOffset));
// Sort them by slot
if( pFirst->SlotId < pSecond->SlotId ) return -1;
if( pFirst->SlotId > pSecond->SlotId ) return 1;
// Then sort them by code offset
if( firstOffset < secondOffset )
return -1;
else
{
_ASSERTE(( firstOffset > secondOffset ) && "Redundant transitions found in GC info!");
return 1;
}
}
BitStreamWriter::MemoryBlockList::MemoryBlockList()
: m_head(nullptr),
m_tail(nullptr)
{
}
BitStreamWriter::MemoryBlock* BitStreamWriter::MemoryBlockList::AppendNew(IAllocator* allocator, size_t bytes)
{
auto* memBlock = reinterpret_cast<MemoryBlock*>(allocator->Alloc(sizeof(MemoryBlock) + bytes));
memBlock->m_next = nullptr;
if (m_tail != nullptr)
{
_ASSERTE(m_head != nullptr);
m_tail->m_next = memBlock;
}
else
{
_ASSERTE(m_head == nullptr);
m_head = memBlock;
}
m_tail = memBlock;
return memBlock;
}
void BitStreamWriter::MemoryBlockList::Dispose(IAllocator* allocator)
{
#ifdef MUST_CALL_JITALLOCATOR_FREE
for (MemoryBlock* block = m_head, *next; block != nullptr; block = next)
{
next = block->m_next;
allocator->Free(block);
}
m_head = nullptr;
m_tail = nullptr;
#endif
}
void GcInfoEncoder::FinalizeSlotIds()
{
#ifdef _DEBUG
m_IsSlotTableFrozen = TRUE;
#endif
}
bool GcInfoEncoder::IsAlwaysScratch(GcSlotDesc &slotDesc)
{
#if defined(_TARGET_ARM_)
_ASSERTE( m_SizeOfStackOutgoingAndScratchArea != (UINT32)-1 );
if(slotDesc.IsRegister())
{
int regNum = (int) slotDesc.Slot.RegisterNumber;
_ASSERTE(regNum >= 0 && regNum <= 14);
_ASSERTE(regNum != 13); // sp
return ((regNum <= 3) || (regNum >= 12)); // R12 and R14/LR are both scratch registers
}
else if (!slotDesc.IsUntracked() && (slotDesc.Slot.Stack.Base == GC_SP_REL) &&
((UINT32)slotDesc.Slot.Stack.SpOffset < m_SizeOfStackOutgoingAndScratchArea))
{
return TRUE;
}
else
return FALSE;
#elif defined(_TARGET_AMD64_)
_ASSERTE( m_SizeOfStackOutgoingAndScratchArea != (UINT32)-1 );
if(slotDesc.IsRegister())
{
int regNum = (int) slotDesc.Slot.RegisterNumber;
_ASSERTE(regNum >= 0 && regNum <= 16);
_ASSERTE(regNum != 4); // rsp
UINT16 PreservedRegMask =
(1 << 3) // rbx
| (1 << 5) // rbp
#ifndef UNIX_AMD64_ABI
| (1 << 6) // rsi
| (1 << 7) // rdi
#endif // UNIX_AMD64_ABI
| (1 << 12) // r12
| (1 << 13) // r13
| (1 << 14) // r14
| (1 << 15); // r15
return !(PreservedRegMask & (1 << regNum));
}
else if (!slotDesc.IsUntracked() && (slotDesc.Slot.Stack.Base == GC_SP_REL) &&
((UINT32)slotDesc.Slot.Stack.SpOffset < m_SizeOfStackOutgoingAndScratchArea))
{
return TRUE;
}
else
return FALSE;
#else
return FALSE;
#endif
}
void GcInfoEncoder::Build()
{
#ifdef _DEBUG
_ASSERTE(m_IsSlotTableFrozen || m_NumSlots == 0);
#endif
_ASSERTE((1 << NUM_NORM_CODE_OFFSETS_PER_CHUNK_LOG2) == NUM_NORM_CODE_OFFSETS_PER_CHUNK);
LOG((LF_GCINFO, LL_INFO100,
"Entering GcInfoEncoder::Build() for method %s[%s]\n",
m_MethodName, m_ModuleName
));
///////////////////////////////////////////////////////////////////////
// Method header
///////////////////////////////////////////////////////////////////////
UINT32 hasSecurityObject = (m_SecurityObjectStackSlot != NO_SECURITY_OBJECT);