-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
compiler.cpp
10607 lines (9067 loc) · 355 KB
/
compiler.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.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX Compiler XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif // _MSC_VER
#include "hostallocator.h"
#include "emit.h"
#include "ssabuilder.h"
#include "valuenum.h"
#include "rangecheck.h"
#include "lower.h"
#include "stacklevelsetter.h"
#include "jittelemetry.h"
#include "patchpointinfo.h"
#include "jitstd/algorithm.h"
extern ICorJitHost* g_jitHost;
unsigned Compiler::jitTotalMethodCompiled = 0;
#if defined(DEBUG)
LONG Compiler::jitNestingLevel = 0;
#endif // defined(DEBUG)
// static
bool Compiler::s_pAltJitExcludeAssembliesListInitialized = false;
AssemblyNamesList2* Compiler::s_pAltJitExcludeAssembliesList = nullptr;
#ifdef DEBUG
// static
bool Compiler::s_pJitDisasmIncludeAssembliesListInitialized = false;
AssemblyNamesList2* Compiler::s_pJitDisasmIncludeAssembliesList = nullptr;
// static
bool Compiler::s_pJitFunctionFileInitialized = false;
MethodSet* Compiler::s_pJitMethodSet = nullptr;
#endif // DEBUG
#ifdef CONFIGURABLE_ARM_ABI
// static
bool GlobalJitOptions::compFeatureHfa = false;
LONG GlobalJitOptions::compUseSoftFPConfigured = 0;
#endif // CONFIGURABLE_ARM_ABI
/*****************************************************************************
*
* Little helpers to grab the current cycle counter value; this is done
* differently based on target architecture, host toolchain, etc. The
* main thing is to keep the overhead absolutely minimal; in fact, on
* x86/x64 we use RDTSC even though it's not thread-safe; GetThreadCycles
* (which is monotonous) is just too expensive.
*/
#ifdef FEATURE_JIT_METHOD_PERF
#if defined(HOST_X86) || defined(HOST_AMD64)
#if defined(_MSC_VER)
#include <intrin.h>
inline bool _our_GetThreadCycles(unsigned __int64* cycleOut)
{
*cycleOut = __rdtsc();
return true;
}
#elif defined(__GNUC__)
inline bool _our_GetThreadCycles(unsigned __int64* cycleOut)
{
uint32_t hi, lo;
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
*cycleOut = (static_cast<unsigned __int64>(hi) << 32) | static_cast<unsigned __int64>(lo);
return true;
}
#else // neither _MSC_VER nor __GNUC__
// The following *might* work - might as well try.
#define _our_GetThreadCycles(cp) GetThreadCycles(cp)
#endif
#elif defined(HOST_ARM) || defined(HOST_ARM64)
// If this doesn't work please see ../gc/gc.cpp for additional ARM
// info (and possible solutions).
#define _our_GetThreadCycles(cp) GetThreadCycles(cp)
#else // not x86/x64 and not ARM
// Don't know what this target is, but let's give it a try; if
// someone really wants to make this work, please add the right
// code here.
#define _our_GetThreadCycles(cp) GetThreadCycles(cp)
#endif // which host OS
const BYTE genTypeSizes[] = {
#define DEF_TP(tn, nm, jitType, sz, sze, asze, st, al, regTyp, regFld, tf) sz,
#include "typelist.h"
#undef DEF_TP
};
const BYTE genTypeAlignments[] = {
#define DEF_TP(tn, nm, jitType, sz, sze, asze, st, al, regTyp, regFld, tf) al,
#include "typelist.h"
#undef DEF_TP
};
const BYTE genTypeStSzs[] = {
#define DEF_TP(tn, nm, jitType, sz, sze, asze, st, al, regTyp, regFld, tf) st,
#include "typelist.h"
#undef DEF_TP
};
const BYTE genActualTypes[] = {
#define DEF_TP(tn, nm, jitType, sz, sze, asze, st, al, regTyp, regFld, tf) jitType,
#include "typelist.h"
#undef DEF_TP
};
#endif // FEATURE_JIT_METHOD_PERF
/*****************************************************************************/
inline unsigned getCurTime()
{
SYSTEMTIME tim;
GetSystemTime(&tim);
return (((tim.wHour * 60) + tim.wMinute) * 60 + tim.wSecond) * 1000 + tim.wMilliseconds;
}
/*****************************************************************************/
#ifdef DEBUG
/*****************************************************************************/
static FILE* jitSrcFilePtr;
static unsigned jitCurSrcLine;
void Compiler::JitLogEE(unsigned level, const char* fmt, ...)
{
va_list args;
if (verbose)
{
va_start(args, fmt);
vflogf(jitstdout, fmt, args);
va_end(args);
}
va_start(args, fmt);
vlogf(level, fmt, args);
va_end(args);
}
#endif // DEBUG
/*****************************************************************************/
#if defined(DEBUG) || MEASURE_NODE_SIZE || MEASURE_BLOCK_SIZE || DISPLAY_SIZES || CALL_ARG_STATS
static unsigned genMethodCnt; // total number of methods JIT'ted
unsigned genMethodICnt; // number of interruptible methods
unsigned genMethodNCnt; // number of non-interruptible methods
static unsigned genSmallMethodsNeedingExtraMemoryCnt = 0;
#endif
/*****************************************************************************/
#if MEASURE_NODE_SIZE
NodeSizeStats genNodeSizeStats;
NodeSizeStats genNodeSizeStatsPerFunc;
unsigned genTreeNcntHistBuckets[] = {10, 20, 30, 40, 50, 100, 200, 300, 400, 500, 1000, 5000, 10000, 0};
Histogram genTreeNcntHist(genTreeNcntHistBuckets);
unsigned genTreeNsizHistBuckets[] = {1000, 5000, 10000, 50000, 100000, 500000, 1000000, 0};
Histogram genTreeNsizHist(genTreeNsizHistBuckets);
#endif // MEASURE_NODE_SIZE
/*****************************************************************************/
#if MEASURE_MEM_ALLOC
unsigned memAllocHistBuckets[] = {64, 128, 192, 256, 512, 1024, 4096, 8192, 0};
Histogram memAllocHist(memAllocHistBuckets);
unsigned memUsedHistBuckets[] = {16, 32, 64, 128, 192, 256, 512, 1024, 4096, 8192, 0};
Histogram memUsedHist(memUsedHistBuckets);
#endif // MEASURE_MEM_ALLOC
/*****************************************************************************
*
* Variables to keep track of total code amounts.
*/
#if DISPLAY_SIZES
size_t grossVMsize; // Total IL code size
size_t grossNCsize; // Native code + data size
size_t totalNCsize; // Native code + data + GC info size (TODO-Cleanup: GC info size only accurate for JIT32_GCENCODER)
size_t gcHeaderISize; // GC header size: interruptible methods
size_t gcPtrMapISize; // GC pointer map size: interruptible methods
size_t gcHeaderNSize; // GC header size: non-interruptible methods
size_t gcPtrMapNSize; // GC pointer map size: non-interruptible methods
#endif // DISPLAY_SIZES
/*****************************************************************************
*
* Variables to keep track of argument counts.
*/
#if CALL_ARG_STATS
unsigned argTotalCalls;
unsigned argHelperCalls;
unsigned argStaticCalls;
unsigned argNonVirtualCalls;
unsigned argVirtualCalls;
unsigned argTotalArgs; // total number of args for all calls (including objectPtr)
unsigned argTotalDWordArgs;
unsigned argTotalLongArgs;
unsigned argTotalFloatArgs;
unsigned argTotalDoubleArgs;
unsigned argTotalRegArgs;
unsigned argTotalTemps;
unsigned argTotalLclVar;
unsigned argTotalDeferred;
unsigned argTotalConst;
unsigned argTotalObjPtr;
unsigned argMaxTempsPerMethod;
unsigned argCntBuckets[] = {0, 1, 2, 3, 4, 5, 6, 10, 0};
Histogram argCntTable(argCntBuckets);
unsigned argDWordCntBuckets[] = {0, 1, 2, 3, 4, 5, 6, 10, 0};
Histogram argDWordCntTable(argDWordCntBuckets);
unsigned argDWordLngCntBuckets[] = {0, 1, 2, 3, 4, 5, 6, 10, 0};
Histogram argDWordLngCntTable(argDWordLngCntBuckets);
unsigned argTempsCntBuckets[] = {0, 1, 2, 3, 4, 5, 6, 10, 0};
Histogram argTempsCntTable(argTempsCntBuckets);
#endif // CALL_ARG_STATS
/*****************************************************************************
*
* Variables to keep track of basic block counts.
*/
#if COUNT_BASIC_BLOCKS
// --------------------------------------------------
// Basic block count frequency table:
// --------------------------------------------------
// <= 1 ===> 26872 count ( 56% of total)
// 2 .. 2 ===> 669 count ( 58% of total)
// 3 .. 3 ===> 4687 count ( 68% of total)
// 4 .. 5 ===> 5101 count ( 78% of total)
// 6 .. 10 ===> 5575 count ( 90% of total)
// 11 .. 20 ===> 3028 count ( 97% of total)
// 21 .. 50 ===> 1108 count ( 99% of total)
// 51 .. 100 ===> 182 count ( 99% of total)
// 101 .. 1000 ===> 34 count (100% of total)
// 1001 .. 10000 ===> 0 count (100% of total)
// --------------------------------------------------
unsigned bbCntBuckets[] = {1, 2, 3, 5, 10, 20, 50, 100, 1000, 10000, 0};
Histogram bbCntTable(bbCntBuckets);
/* Histogram for the IL opcode size of methods with a single basic block */
unsigned bbSizeBuckets[] = {1, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 0};
Histogram bbOneBBSizeTable(bbSizeBuckets);
unsigned domsChangedIterationBuckets[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0};
Histogram domsChangedIterationTable(domsChangedIterationBuckets);
unsigned computeReachabilitySetsIterationBuckets[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0};
Histogram computeReachabilitySetsIterationTable(computeReachabilitySetsIterationBuckets);
unsigned computeReachabilityIterationBuckets[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0};
Histogram computeReachabilityIterationTable(computeReachabilityIterationBuckets);
#endif // COUNT_BASIC_BLOCKS
/*****************************************************************************
*
* Used by optFindNaturalLoops to gather statistical information such as
* - total number of natural loops
* - number of loops with 1, 2, ... exit conditions
* - number of loops that have an iterator (for like)
* - number of loops that have a constant iterator
*/
#if COUNT_LOOPS
unsigned totalLoopMethods; // counts the total number of methods that have natural loops
unsigned maxLoopsPerMethod; // counts the maximum number of loops a method has
unsigned totalLoopOverflows; // # of methods that identified more loops than we can represent
unsigned totalLoopCount; // counts the total number of natural loops
unsigned totalUnnatLoopCount; // counts the total number of (not-necessarily natural) loops
unsigned totalUnnatLoopOverflows; // # of methods that identified more unnatural loops than we can represent
unsigned iterLoopCount; // counts the # of loops with an iterator (for like)
unsigned constIterLoopCount; // counts the # of loops with a constant iterator (for like)
bool hasMethodLoops; // flag to keep track if we already counted a method as having loops
unsigned loopsThisMethod; // counts the number of loops in the current method
bool loopOverflowThisMethod; // True if we exceeded the max # of loops in the method.
/* Histogram for number of loops in a method */
unsigned loopCountBuckets[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0};
Histogram loopCountTable(loopCountBuckets);
/* Histogram for number of loop exits */
unsigned loopExitCountBuckets[] = {0, 1, 2, 3, 4, 5, 6, 0};
Histogram loopExitCountTable(loopExitCountBuckets);
#endif // COUNT_LOOPS
//------------------------------------------------------------------------
// getJitGCType: Given the VM's CorInfoGCType convert it to the JIT's var_types
//
// Arguments:
// gcType - an enum value that originally came from an element
// of the BYTE[] returned from getClassGClayout()
//
// Return Value:
// The corresponding enum value from the JIT's var_types
//
// Notes:
// The gcLayout of each field of a struct is returned from getClassGClayout()
// as a BYTE[] but each BYTE element is actually a CorInfoGCType value
// Note when we 'know' that there is only one element in this array
// the JIT will often pass the address of a single BYTE, instead of a BYTE[]
//
var_types Compiler::getJitGCType(BYTE gcType)
{
var_types result = TYP_UNKNOWN;
CorInfoGCType corInfoType = (CorInfoGCType)gcType;
if (corInfoType == TYPE_GC_NONE)
{
result = TYP_I_IMPL;
}
else if (corInfoType == TYPE_GC_REF)
{
result = TYP_REF;
}
else if (corInfoType == TYPE_GC_BYREF)
{
result = TYP_BYREF;
}
else
{
noway_assert(!"Bad value of 'gcType'");
}
return result;
}
#ifdef TARGET_X86
//---------------------------------------------------------------------------
// isTrivialPointerSizedStruct:
// Check if the given struct type contains only one pointer-sized integer value type
//
// Arguments:
// clsHnd - the handle for the struct type.
//
// Return Value:
// true if the given struct type contains only one pointer-sized integer value type,
// false otherwise.
//
bool Compiler::isTrivialPointerSizedStruct(CORINFO_CLASS_HANDLE clsHnd) const
{
assert(info.compCompHnd->isValueClass(clsHnd));
if (info.compCompHnd->getClassSize(clsHnd) != TARGET_POINTER_SIZE)
{
return false;
}
for (;;)
{
// all of class chain must be of value type and must have only one field
if (!info.compCompHnd->isValueClass(clsHnd) || info.compCompHnd->getClassNumInstanceFields(clsHnd) != 1)
{
return false;
}
CORINFO_CLASS_HANDLE* pClsHnd = &clsHnd;
CORINFO_FIELD_HANDLE fldHnd = info.compCompHnd->getFieldInClass(clsHnd, 0);
CorInfoType fieldType = info.compCompHnd->getFieldType(fldHnd, pClsHnd);
var_types vt = JITtype2varType(fieldType);
if (fieldType == CORINFO_TYPE_VALUECLASS)
{
clsHnd = *pClsHnd;
}
else if (varTypeIsI(vt) && !varTypeIsGC(vt))
{
return true;
}
else
{
return false;
}
}
}
#endif // TARGET_X86
//---------------------------------------------------------------------------
// isNativePrimitiveStructType:
// Check if the given struct type is an intrinsic type that should be treated as though
// it is not a struct at the unmanaged ABI boundary.
//
// Arguments:
// clsHnd - the handle for the struct type.
//
// Return Value:
// true if the given struct type should be treated as a primitive for unmanaged calls,
// false otherwise.
//
bool Compiler::isNativePrimitiveStructType(CORINFO_CLASS_HANDLE clsHnd)
{
if (!isIntrinsicType(clsHnd))
{
return false;
}
const char* namespaceName = nullptr;
const char* typeName = getClassNameFromMetadata(clsHnd, &namespaceName);
if (strcmp(namespaceName, "System.Runtime.InteropServices") != 0)
{
return false;
}
return strcmp(typeName, "CLong") == 0 || strcmp(typeName, "CULong") == 0 || strcmp(typeName, "NFloat") == 0;
}
//-----------------------------------------------------------------------------
// getPrimitiveTypeForStruct:
// Get the "primitive" type that is used for a struct
// of size 'structSize'.
// We examine 'clsHnd' to check the GC layout of the struct and
// return TYP_REF for structs that simply wrap an object.
// If the struct is a one element HFA/HVA, we will return the
// proper floating point or vector type.
//
// Arguments:
// structSize - the size of the struct type, cannot be zero
// clsHnd - the handle for the struct type, used when may have
// an HFA or if we need the GC layout for an object ref.
//
// Return Value:
// The primitive type (i.e. byte, short, int, long, ref, float, double)
// used to pass or return structs of this size.
// If we shouldn't use a "primitive" type then TYP_UNKNOWN is returned.
// Notes:
// For 32-bit targets (X86/ARM32) the 64-bit TYP_LONG type is not
// considered a primitive type by this method.
// So a struct that wraps a 'long' is passed and returned in the
// same way as any other 8-byte struct
// For ARM32 if we have an HFA struct that wraps a 64-bit double
// we will return TYP_DOUBLE.
// For vector calling conventions, a vector is considered a "primitive"
// type, as it is passed in a single register.
//
var_types Compiler::getPrimitiveTypeForStruct(unsigned structSize, CORINFO_CLASS_HANDLE clsHnd, bool isVarArg)
{
assert(structSize != 0);
var_types useType = TYP_UNKNOWN;
// Start by determining if we have an HFA/HVA with a single element.
if (GlobalJitOptions::compFeatureHfa)
{
// Arm64 Windows VarArg methods arguments will not classify HFA types, they will need to be treated
// as if they are not HFA types.
if (!(TargetArchitecture::IsArm64 && TargetOS::IsWindows && isVarArg))
{
switch (structSize)
{
case 4:
case 8:
#ifdef TARGET_ARM64
case 16:
#endif // TARGET_ARM64
{
var_types hfaType = GetHfaType(clsHnd);
// We're only interested in the case where the struct size is equal to the size of the hfaType.
if (varTypeIsValidHfaType(hfaType))
{
if (genTypeSize(hfaType) == structSize)
{
useType = hfaType;
}
else
{
return TYP_UNKNOWN;
}
}
}
}
if (useType != TYP_UNKNOWN)
{
return useType;
}
}
}
// Now deal with non-HFA/HVA structs.
switch (structSize)
{
case 1:
useType = TYP_UBYTE;
break;
case 2:
useType = TYP_USHORT;
break;
#if !defined(TARGET_XARCH) || defined(UNIX_AMD64_ABI)
case 3:
useType = TYP_INT;
break;
#endif // !TARGET_XARCH || UNIX_AMD64_ABI
#ifdef TARGET_64BIT
case 4:
// We dealt with the one-float HFA above. All other 4-byte structs are handled as INT.
useType = TYP_INT;
break;
#if !defined(TARGET_XARCH) || defined(UNIX_AMD64_ABI)
case 5:
case 6:
case 7:
useType = TYP_I_IMPL;
break;
#endif // !TARGET_XARCH || UNIX_AMD64_ABI
#endif // TARGET_64BIT
case TARGET_POINTER_SIZE:
{
BYTE gcPtr = 0;
// Check if this pointer-sized struct is wrapping a GC object
info.compCompHnd->getClassGClayout(clsHnd, &gcPtr);
useType = getJitGCType(gcPtr);
}
break;
default:
useType = TYP_UNKNOWN;
break;
}
return useType;
}
//-----------------------------------------------------------------------------
// getArgTypeForStruct:
// Get the type that is used to pass values of the given struct type.
// If you have already retrieved the struct size then it should be
// passed as the optional fourth argument, as this allows us to avoid
// an extra call to getClassSize(clsHnd)
//
// Arguments:
// clsHnd - the handle for the struct type
// wbPassStruct - An "out" argument with information about how
// the struct is to be passed
// isVarArg - is vararg, used to ignore HFA types for Arm64 windows varargs
// structSize - the size of the struct type,
// or zero if we should call getClassSize(clsHnd)
//
// Return Value:
// For wbPassStruct you can pass a 'nullptr' and nothing will be written
// or returned for that out parameter.
// When *wbPassStruct is SPK_PrimitiveType this method's return value
// is the primitive type used to pass the struct.
// When *wbPassStruct is SPK_ByReference this method's return value
// is always TYP_UNKNOWN and the struct type is passed by reference to a copy
// When *wbPassStruct is SPK_ByValue or SPK_ByValueAsHfa this method's return value
// is always TYP_STRUCT and the struct type is passed by value either
// using multiple registers or on the stack.
//
// Assumptions:
// The size must be the size of the given type.
// The given class handle must be for a value type (struct).
//
// Notes:
// About HFA types:
// When the clsHnd is a one element HFA type we return the appropriate
// floating point primitive type and *wbPassStruct is SPK_PrimitiveType
// If there are two or more elements in the HFA type then the this method's
// return value is TYP_STRUCT and *wbPassStruct is SPK_ByValueAsHfa
//
var_types Compiler::getArgTypeForStruct(CORINFO_CLASS_HANDLE clsHnd,
structPassingKind* wbPassStruct,
bool isVarArg,
unsigned structSize)
{
var_types useType = TYP_UNKNOWN;
structPassingKind howToPassStruct = SPK_Unknown; // We must change this before we return
assert(structSize != 0);
// Determine if we can pass the struct as a primitive type.
// Note that on x86 we only pass specific pointer-sized structs that satisfy isTrivialPointerSizedStruct checks.
#ifndef TARGET_X86
#ifdef UNIX_AMD64_ABI
// An 8-byte struct may need to be passed in a floating point register
// So we always consult the struct "Classifier" routine
//
SYSTEMV_AMD64_CORINFO_STRUCT_REG_PASSING_DESCRIPTOR structDesc;
eeGetSystemVAmd64PassStructInRegisterDescriptor(clsHnd, &structDesc);
if (structDesc.passedInRegisters && (structDesc.eightByteCount != 1))
{
// We can't pass this as a primitive type.
}
else if (structDesc.eightByteClassifications[0] == SystemVClassificationTypeSSE)
{
// If this is passed as a floating type, use that.
// Otherwise, we'll use the general case - we don't want to use the "EightByteType"
// directly, because it returns `TYP_INT` for any integral type <= 4 bytes, and
// we need to preserve small types.
useType = GetEightByteType(structDesc, 0);
}
else
#endif // UNIX_AMD64_ABI
// The largest arg passed in a single register is MAX_PASS_SINGLEREG_BYTES,
// so we can skip calling getPrimitiveTypeForStruct when we
// have a struct that is larger than that.
//
if (structSize <= MAX_PASS_SINGLEREG_BYTES)
{
// We set the "primitive" useType based upon the structSize
// and also examine the clsHnd to see if it is an HFA of count one
useType = getPrimitiveTypeForStruct(structSize, clsHnd, isVarArg);
}
#else
if (isTrivialPointerSizedStruct(clsHnd))
{
useType = TYP_I_IMPL;
}
#endif // !TARGET_X86
// Did we change this struct type into a simple "primitive" type?
//
if (useType != TYP_UNKNOWN)
{
// Yes, we should use the "primitive" type in 'useType'
howToPassStruct = SPK_PrimitiveType;
}
else // We can't replace the struct with a "primitive" type
{
// See if we can pass this struct by value, possibly in multiple registers
// or if we should pass it by reference to a copy
//
if (structSize <= MAX_PASS_MULTIREG_BYTES)
{
// Structs that are HFA/HVA's are passed by value in multiple registers.
// Arm64 Windows VarArg methods arguments will not classify HFA/HVA types, they will need to be treated
// as if they are not HFA/HVA types.
var_types hfaType;
if (TargetArchitecture::IsArm64 && TargetOS::IsWindows && isVarArg)
{
hfaType = TYP_UNDEF;
}
else
{
hfaType = GetHfaType(clsHnd);
}
if (varTypeIsValidHfaType(hfaType))
{
// HFA's of count one should have been handled by getPrimitiveTypeForStruct
assert(GetHfaCount(clsHnd) >= 2);
// setup wbPassType and useType indicate that this is passed by value as an HFA
// using multiple registers
// (when all of the parameters registers are used, then the stack will be used)
howToPassStruct = SPK_ByValueAsHfa;
useType = TYP_STRUCT;
}
else // Not an HFA struct type
{
#ifdef UNIX_AMD64_ABI
// The case of (structDesc.eightByteCount == 1) should have already been handled
if ((structDesc.eightByteCount > 1) || !structDesc.passedInRegisters)
{
// setup wbPassType and useType indicate that this is passed by value in multiple registers
// (when all of the parameters registers are used, then the stack will be used)
howToPassStruct = SPK_ByValue;
useType = TYP_STRUCT;
}
else
{
assert(structDesc.eightByteCount == 0);
// Otherwise we pass this struct by reference to a copy
// setup wbPassType and useType indicate that this is passed using one register
// (by reference to a copy)
howToPassStruct = SPK_ByReference;
useType = TYP_UNKNOWN;
}
#elif defined(TARGET_ARM64)
// Structs that are pointer sized or smaller should have been handled by getPrimitiveTypeForStruct
assert(structSize > TARGET_POINTER_SIZE);
// On ARM64 structs that are 9-16 bytes are passed by value in multiple registers
//
if (structSize <= (TARGET_POINTER_SIZE * 2))
{
// setup wbPassType and useType indicate that this is passed by value in multiple registers
// (when all of the parameters registers are used, then the stack will be used)
howToPassStruct = SPK_ByValue;
useType = TYP_STRUCT;
}
else // a structSize that is 17-32 bytes in size
{
// Otherwise we pass this struct by reference to a copy
// setup wbPassType and useType indicate that this is passed using one register
// (by reference to a copy)
howToPassStruct = SPK_ByReference;
useType = TYP_UNKNOWN;
}
#elif defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
// Otherwise we pass this struct by value on the stack
// setup wbPassType and useType indicate that this is passed by value according to the X86/ARM32 ABI
// On LOONGARCH64 struct that is 1-16 bytes is passed by value in one/two register(s)
howToPassStruct = SPK_ByValue;
useType = TYP_STRUCT;
#else // TARGET_XXX
noway_assert(!"Unhandled TARGET in getArgTypeForStruct (with FEATURE_MULTIREG_ARGS=1)");
#endif // TARGET_XXX
}
}
else // (structSize > MAX_PASS_MULTIREG_BYTES)
{
// We have a (large) struct that can't be replaced with a "primitive" type
// and can't be passed in multiple registers
CLANG_FORMAT_COMMENT_ANCHOR;
#if defined(TARGET_X86) || defined(TARGET_ARM) || defined(UNIX_AMD64_ABI)
// Otherwise we pass this struct by value on the stack
// setup wbPassType and useType indicate that this is passed by value according to the X86/ARM32 ABI
howToPassStruct = SPK_ByValue;
useType = TYP_STRUCT;
#elif defined(TARGET_AMD64) || defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64) || defined(TARGET_RISCV64)
// Otherwise we pass this struct by reference to a copy
// setup wbPassType and useType indicate that this is passed using one register (by reference to a copy)
howToPassStruct = SPK_ByReference;
useType = TYP_UNKNOWN;
#else // TARGET_XXX
noway_assert(!"Unhandled TARGET in getArgTypeForStruct");
#endif // TARGET_XXX
}
}
// 'howToPassStruct' must be set to one of the valid values before we return
assert(howToPassStruct != SPK_Unknown);
if (wbPassStruct != nullptr)
{
*wbPassStruct = howToPassStruct;
}
return useType;
}
//-----------------------------------------------------------------------------
// getReturnTypeForStruct:
// Get the type that is used to return values of the given struct type.
// If you have already retrieved the struct size then it should be
// passed as the optional third argument, as this allows us to avoid
// an extra call to getClassSize(clsHnd)
//
// Arguments:
// clsHnd - the handle for the struct type
// callConv - the calling convention of the function
// that returns this struct.
// wbReturnStruct - An "out" argument with information about how
// the struct is to be returned
// structSize - the size of the struct type,
// or zero if we should call getClassSize(clsHnd)
//
// Return Value:
// For wbReturnStruct you can pass a 'nullptr' and nothing will be written
// or returned for that out parameter.
// When *wbReturnStruct is SPK_PrimitiveType this method's return value
// is the primitive type used to return the struct.
// When *wbReturnStruct is SPK_ByReference this method's return value
// is always TYP_UNKNOWN and the struct type is returned using a return buffer
// When *wbReturnStruct is SPK_ByValue or SPK_ByValueAsHfa this method's return value
// is always TYP_STRUCT and the struct type is returned using multiple registers.
//
// Assumptions:
// The size must be the size of the given type.
// The given class handle must be for a value type (struct).
//
// Notes:
// About HFA types:
// When the clsHnd is a one element HFA type then this method's return
// value is the appropriate floating point primitive type and
// *wbReturnStruct is SPK_PrimitiveType.
// If there are two or more elements in the HFA type and the target supports
// multireg return types then the return value is TYP_STRUCT and
// *wbReturnStruct is SPK_ByValueAsHfa.
// Additionally if there are two or more elements in the HFA type and
// the target doesn't support multreg return types then it is treated
// as if it wasn't an HFA type.
// About returning TYP_STRUCT:
// Whenever this method's return value is TYP_STRUCT it always means
// that multiple registers are used to return this struct.
//
var_types Compiler::getReturnTypeForStruct(CORINFO_CLASS_HANDLE clsHnd,
CorInfoCallConvExtension callConv,
structPassingKind* wbReturnStruct /* = nullptr */,
unsigned structSize /* = 0 */)
{
var_types useType = TYP_UNKNOWN;
structPassingKind howToReturnStruct = SPK_Unknown; // We must change this before we return
bool canReturnInRegister = true;
assert(clsHnd != NO_CLASS_HANDLE);
if (structSize == 0)
{
structSize = info.compCompHnd->getClassSize(clsHnd);
}
assert(structSize > 0);
#ifdef UNIX_AMD64_ABI
// An 8-byte struct may need to be returned in a floating point register
// So we always consult the struct "Classifier" routine
//
SYSTEMV_AMD64_CORINFO_STRUCT_REG_PASSING_DESCRIPTOR structDesc;
eeGetSystemVAmd64PassStructInRegisterDescriptor(clsHnd, &structDesc);
if (structDesc.eightByteCount == 1)
{
assert(structSize <= sizeof(double));
assert(structDesc.passedInRegisters);
if (structDesc.eightByteClassifications[0] == SystemVClassificationTypeSSE)
{
// If this is returned as a floating type, use that.
// Otherwise, leave as TYP_UNKNOWN and we'll sort things out below.
useType = GetEightByteType(structDesc, 0);
howToReturnStruct = SPK_PrimitiveType;
}
}
else
{
// Return classification is not always size based...
canReturnInRegister = structDesc.passedInRegisters;
if (!canReturnInRegister)
{
assert(structDesc.eightByteCount == 0);
howToReturnStruct = SPK_ByReference;
useType = TYP_UNKNOWN;
}
}
#elif UNIX_X86_ABI
if (callConv != CorInfoCallConvExtension::Managed && !isNativePrimitiveStructType(clsHnd))
{
canReturnInRegister = false;
howToReturnStruct = SPK_ByReference;
useType = TYP_UNKNOWN;
}
#elif defined(TARGET_LOONGARCH64)
if (structSize <= (TARGET_POINTER_SIZE * 2))
{
uint32_t floatFieldFlags = info.compCompHnd->getLoongArch64PassStructInRegisterFlags(clsHnd);
if ((floatFieldFlags & STRUCT_FLOAT_FIELD_ONLY_ONE) != 0)
{
howToReturnStruct = SPK_PrimitiveType;
useType = (structSize > 4) ? TYP_DOUBLE : TYP_FLOAT;
}
else if (floatFieldFlags & (STRUCT_HAS_FLOAT_FIELDS_MASK ^ STRUCT_FLOAT_FIELD_ONLY_ONE))
{
howToReturnStruct = SPK_ByValue;
useType = TYP_STRUCT;
}
}
#elif defined(TARGET_RISCV64)
if (structSize <= (TARGET_POINTER_SIZE * 2))
{
uint32_t floatFieldFlags = info.compCompHnd->getRISCV64PassStructInRegisterFlags(clsHnd);
if ((floatFieldFlags & STRUCT_FLOAT_FIELD_ONLY_ONE) != 0)
{
howToReturnStruct = SPK_PrimitiveType;
useType = (structSize > 4) ? TYP_DOUBLE : TYP_FLOAT;
}
else if (floatFieldFlags & (STRUCT_HAS_FLOAT_FIELDS_MASK ^ STRUCT_FLOAT_FIELD_ONLY_ONE))
{
howToReturnStruct = SPK_ByValue;
useType = TYP_STRUCT;
}
}
#endif
if (TargetOS::IsWindows && !TargetArchitecture::IsArm32 && callConvIsInstanceMethodCallConv(callConv) &&
!isNativePrimitiveStructType(clsHnd))
{
canReturnInRegister = false;
howToReturnStruct = SPK_ByReference;
useType = TYP_UNKNOWN;
}
// Check for cases where a small struct is returned in a register
// via a primitive type.
//
// The largest "primitive type" is MAX_PASS_SINGLEREG_BYTES
// so we can skip calling getPrimitiveTypeForStruct when we
// have a struct that is larger than that.
if (canReturnInRegister && (useType == TYP_UNKNOWN) && (structSize <= MAX_PASS_SINGLEREG_BYTES))
{
// We set the "primitive" useType based upon the structSize
// and also examine the clsHnd to see if it is an HFA of count one
//
// The ABI for struct returns in varArg methods, is same as the normal case,
// so pass false for isVararg
useType = getPrimitiveTypeForStruct(structSize, clsHnd, /*isVararg=*/false);
if (useType != TYP_UNKNOWN)
{
if (structSize == genTypeSize(useType))
{
// Currently: 1, 2, 4, or 8 byte structs
howToReturnStruct = SPK_PrimitiveType;
}
else
{
// Currently: 3, 5, 6, or 7 byte structs
assert(structSize < genTypeSize(useType));
howToReturnStruct = SPK_EnclosingType;
}
}
}
#ifdef TARGET_64BIT
// Note this handles an odd case when FEATURE_MULTIREG_RET is disabled and HFAs are enabled
//
// getPrimitiveTypeForStruct will return TYP_UNKNOWN for a struct that is an HFA of two floats
// because when HFA are enabled, normally we would use two FP registers to pass or return it
//
// But if we don't have support for multiple register return types, we have to change this.
// Since what we have is an 8-byte struct (float + float) we change useType to TYP_I_IMPL
// so that the struct is returned instead using an 8-byte integer register.
//
if ((FEATURE_MULTIREG_RET == 0) && (useType == TYP_UNKNOWN) && (structSize == (2 * sizeof(float))) && IsHfa(clsHnd))
{
useType = TYP_I_IMPL;
howToReturnStruct = SPK_PrimitiveType;
}
#endif
// Did we change this struct type into a simple "primitive" type?
if (useType != TYP_UNKNOWN)
{
// If so, we should have already set howToReturnStruct, too.
assert(howToReturnStruct != SPK_Unknown);
}
else if (canReturnInRegister) // We can't replace the struct with a "primitive" type