-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathlayout.cpp
1098 lines (953 loc) · 32.2 KB
/
layout.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.
#include "jitpch.h"
#include "layout.h"
#include "compiler.h"
// Key used in ClassLayoutTable's hash table for custom layouts.
struct CustomLayoutKey
{
unsigned Size;
const BYTE* GCPtrTypes;
CustomLayoutKey(ClassLayout* layout)
: Size(layout->GetSize())
, GCPtrTypes(layout->m_gcPtrCount > 0 ? layout->GetGCPtrs() : nullptr)
{
assert(layout->IsCustomLayout());
}
CustomLayoutKey(const ClassLayoutBuilder& builder)
: Size(builder.m_size)
, GCPtrTypes(builder.m_gcPtrCount > 0 ? builder.m_gcPtrs : nullptr)
{
}
static bool Equals(const CustomLayoutKey& l, const CustomLayoutKey& r)
{
if (l.Size != r.Size)
{
return false;
}
if ((l.GCPtrTypes == nullptr) != (r.GCPtrTypes == nullptr))
{
return false;
}
if ((l.GCPtrTypes != nullptr) && (memcmp(l.GCPtrTypes, r.GCPtrTypes, l.Size / TARGET_POINTER_SIZE) != 0))
{
return false;
}
return true;
}
static unsigned GetHashCode(const CustomLayoutKey& key)
{
unsigned hash = key.Size;
if (key.GCPtrTypes != nullptr)
{
hash ^= 0xc4cfbb2a + (hash << 19) + (hash >> 13);
for (unsigned i = 0; i < key.Size / TARGET_POINTER_SIZE; i++)
{
hash ^= key.GCPtrTypes[i] + 0x9e3779b9 + (hash << 19) + (hash >> 13);
}
}
else
{
hash ^= 0x324ba6da + (hash << 19) + (hash >> 13);
}
return hash;
}
};
// Keeps track of layout objects associated to class handles or block sizes. A layout is usually
// referenced by a pointer (ClassLayout*) but can also be referenced by a number (unsigned,
// FirstLayoutNum-based), when space constraints or other needs make numbers more appealing.
// Layout objects are immutable and there's always a 1:1 mapping between class handles/block sizes,
// pointers and numbers (e.g. class handle equality implies ClassLayout pointer equality).
class ClassLayoutTable
{
// Each layout is assigned a number, starting with TYP_UNKNOWN + 1. This way one could use a single
// unsigned value to represent the notion of type - values below TYP_UNKNOWN are var_types and values
// above it are struct layouts.
static constexpr unsigned ZeroSizedBlockLayoutNum = TYP_UNKNOWN + 1;
static constexpr unsigned FirstLayoutNum = TYP_UNKNOWN + 2;
typedef JitHashTable<CustomLayoutKey, CustomLayoutKey, unsigned> CustomLayoutIndexMap;
typedef JitHashTable<CORINFO_CLASS_HANDLE, JitPtrKeyFuncs<CORINFO_CLASS_STRUCT_>, unsigned> ObjLayoutIndexMap;
union
{
// Up to 3 layouts can be stored "inline" and finding a layout by handle/size can be done using linear search.
// Most methods need no more than 2 layouts.
ClassLayout* m_layoutArray[3];
// Otherwise a dynamic array is allocated and hashtables are used to map from handle/size to layout array index.
struct
{
ClassLayout** m_layoutLargeArray;
CustomLayoutIndexMap* m_customLayoutMap;
ObjLayoutIndexMap* m_objLayoutMap;
};
};
// The number of layout objects stored in this table.
unsigned m_layoutCount = 0;
// The capacity of m_layoutLargeArray (when more than 3 layouts are stored).
unsigned m_layoutLargeCapacity = 0;
// We furthermore fast-path the 0-sized block layout which is used for
// block locals that may grow (e.g. the outgoing arg area in every non-x86
// compilation).
ClassLayout m_zeroSizedBlockLayout;
public:
ClassLayoutTable()
: m_zeroSizedBlockLayout(0)
{
}
// Get a number that uniquely identifies the specified layout.
unsigned GetLayoutNum(ClassLayout* layout) const
{
if (layout == &m_zeroSizedBlockLayout)
{
return ZeroSizedBlockLayoutNum;
}
return GetLayoutIndex(layout) + FirstLayoutNum;
}
// Get the layout that corresponds to the specified identifier number.
ClassLayout* GetLayoutByNum(unsigned num) const
{
if (num == ZeroSizedBlockLayoutNum)
{
// Fine to cast away const as ClassLayout is immutable
return const_cast<ClassLayout*>(&m_zeroSizedBlockLayout);
}
assert(num >= FirstLayoutNum);
return GetLayoutByIndex(num - FirstLayoutNum);
}
// Get the layout having the specified size but no class handle.
ClassLayout* GetCustomLayout(Compiler* compiler, const ClassLayoutBuilder& builder)
{
if (builder.m_size == 0)
{
return &m_zeroSizedBlockLayout;
}
return GetLayoutByIndex(GetCustomLayoutIndex(compiler, builder));
}
// Get a number that uniquely identifies a layout having the specified size but no class handle.
unsigned GetCustomLayoutNum(Compiler* compiler, const ClassLayoutBuilder& builder)
{
if (builder.m_size == 0)
{
return ZeroSizedBlockLayoutNum;
}
return GetCustomLayoutIndex(compiler, builder) + FirstLayoutNum;
}
// Get the layout for the specified class handle.
ClassLayout* GetObjLayout(Compiler* compiler, CORINFO_CLASS_HANDLE classHandle)
{
return GetLayoutByIndex(GetObjLayoutIndex(compiler, classHandle));
}
// Get a number that uniquely identifies a layout for the specified class handle.
unsigned GetObjLayoutNum(Compiler* compiler, CORINFO_CLASS_HANDLE classHandle)
{
return GetObjLayoutIndex(compiler, classHandle) + FirstLayoutNum;
}
private:
bool HasSmallCapacity() const
{
return m_layoutCount <= ArrLen(m_layoutArray);
}
ClassLayout* GetLayoutByIndex(unsigned index) const
{
assert(index < m_layoutCount);
if (HasSmallCapacity())
{
return m_layoutArray[index];
}
else
{
return m_layoutLargeArray[index];
}
}
unsigned GetLayoutIndex(ClassLayout* layout) const
{
assert(layout != nullptr);
assert(layout != &m_zeroSizedBlockLayout);
if (HasSmallCapacity())
{
for (unsigned i = 0; i < m_layoutCount; i++)
{
if (m_layoutArray[i] == layout)
{
return i;
}
}
}
else
{
unsigned index = 0;
if (layout->IsCustomLayout() ? m_customLayoutMap->Lookup(CustomLayoutKey(layout), &index)
: m_objLayoutMap->Lookup(layout->GetClassHandle(), &index))
{
return index;
}
}
unreached();
}
unsigned GetCustomLayoutIndex(Compiler* compiler, const ClassLayoutBuilder& builder)
{
// The 0-sized layout has its own fast path.
assert(builder.m_size != 0);
CustomLayoutKey key(builder);
if (HasSmallCapacity())
{
for (unsigned i = 0; i < m_layoutCount; i++)
{
if (m_layoutArray[i]->IsCustomLayout() &&
CustomLayoutKey::Equals(key, CustomLayoutKey(m_layoutArray[i])))
{
return i;
}
}
}
else
{
unsigned index;
if (m_customLayoutMap->Lookup(key, &index))
{
return index;
}
}
return AddCustomLayout(compiler, ClassLayout::Create(compiler, builder));
}
unsigned AddCustomLayout(Compiler* compiler, ClassLayout* layout)
{
if (m_layoutCount < ArrLen(m_layoutArray))
{
m_layoutArray[m_layoutCount] = layout;
return m_layoutCount++;
}
unsigned index = AddLayoutLarge(compiler, layout);
m_customLayoutMap->Set(CustomLayoutKey(layout), index);
return index;
}
unsigned GetObjLayoutIndex(Compiler* compiler, CORINFO_CLASS_HANDLE classHandle)
{
assert(classHandle != NO_CLASS_HANDLE);
if (HasSmallCapacity())
{
for (unsigned i = 0; i < m_layoutCount; i++)
{
if (m_layoutArray[i]->GetClassHandle() == classHandle)
{
return i;
}
}
}
else
{
unsigned index;
if (m_objLayoutMap->Lookup(classHandle, &index))
{
return index;
}
}
return AddObjLayout(compiler, ClassLayout::Create(compiler, classHandle));
}
unsigned AddObjLayout(Compiler* compiler, ClassLayout* layout)
{
if (m_layoutCount < ArrLen(m_layoutArray))
{
m_layoutArray[m_layoutCount] = layout;
return m_layoutCount++;
}
unsigned index = AddLayoutLarge(compiler, layout);
m_objLayoutMap->Set(layout->GetClassHandle(), index);
return index;
}
unsigned AddLayoutLarge(Compiler* compiler, ClassLayout* layout)
{
if (m_layoutCount >= m_layoutLargeCapacity)
{
CompAllocator alloc = compiler->getAllocator(CMK_ClassLayout);
unsigned newCapacity = m_layoutCount * 2;
ClassLayout** newArray = alloc.allocate<ClassLayout*>(newCapacity);
if (m_layoutCount <= ArrLen(m_layoutArray))
{
CustomLayoutIndexMap* customLayoutMap = new (alloc) CustomLayoutIndexMap(alloc);
ObjLayoutIndexMap* objLayoutMap = new (alloc) ObjLayoutIndexMap(alloc);
for (unsigned i = 0; i < m_layoutCount; i++)
{
ClassLayout* l = m_layoutArray[i];
newArray[i] = l;
if (l->IsCustomLayout())
{
customLayoutMap->Set(CustomLayoutKey(l), i);
}
else
{
objLayoutMap->Set(l->GetClassHandle(), i);
}
}
m_customLayoutMap = customLayoutMap;
m_objLayoutMap = objLayoutMap;
}
else
{
memcpy(newArray, m_layoutLargeArray, m_layoutCount * sizeof(newArray[0]));
}
m_layoutLargeArray = newArray;
m_layoutLargeCapacity = newCapacity;
}
m_layoutLargeArray[m_layoutCount] = layout;
return m_layoutCount++;
}
};
ClassLayoutTable* Compiler::typCreateClassLayoutTable()
{
assert(m_classLayoutTable == nullptr);
if (compIsForInlining())
{
m_classLayoutTable = impInlineInfo->InlinerCompiler->m_classLayoutTable;
if (m_classLayoutTable == nullptr)
{
m_classLayoutTable = new (this, CMK_ClassLayout) ClassLayoutTable();
impInlineInfo->InlinerCompiler->m_classLayoutTable = m_classLayoutTable;
}
}
else
{
m_classLayoutTable = new (this, CMK_ClassLayout) ClassLayoutTable();
}
return m_classLayoutTable;
}
ClassLayoutTable* Compiler::typGetClassLayoutTable()
{
if (m_classLayoutTable == nullptr)
{
return typCreateClassLayoutTable();
}
return m_classLayoutTable;
}
ClassLayout* Compiler::typGetLayoutByNum(unsigned layoutNum)
{
return typGetClassLayoutTable()->GetLayoutByNum(layoutNum);
}
unsigned Compiler::typGetLayoutNum(ClassLayout* layout)
{
return typGetClassLayoutTable()->GetLayoutNum(layout);
}
unsigned Compiler::typGetObjLayoutNum(CORINFO_CLASS_HANDLE classHandle)
{
return typGetClassLayoutTable()->GetObjLayoutNum(this, classHandle);
}
ClassLayout* Compiler::typGetObjLayout(CORINFO_CLASS_HANDLE classHandle)
{
return typGetClassLayoutTable()->GetObjLayout(this, classHandle);
}
unsigned Compiler::typGetCustomLayoutNum(const ClassLayoutBuilder& builder)
{
return typGetClassLayoutTable()->GetCustomLayoutNum(this, builder);
}
ClassLayout* Compiler::typGetCustomLayout(const ClassLayoutBuilder& builder)
{
return typGetClassLayoutTable()->GetCustomLayout(this, builder);
}
unsigned Compiler::typGetBlkLayoutNum(unsigned blockSize)
{
return typGetCustomLayoutNum(ClassLayoutBuilder(this, blockSize));
}
ClassLayout* Compiler::typGetBlkLayout(unsigned blockSize)
{
return typGetCustomLayout(ClassLayoutBuilder(this, blockSize));
}
ClassLayout* Compiler::typGetArrayLayout(CORINFO_CLASS_HANDLE classHandle, unsigned length)
{
ClassLayoutBuilder b = ClassLayoutBuilder::BuildArray(this, classHandle, length);
return typGetCustomLayout(b);
}
ClassLayout* Compiler::typGetNonGCLayout(ClassLayout* layout)
{
assert(layout->HasGCPtr());
ClassLayoutBuilder b(this, layout->GetSize());
b.CopyPaddingFrom(0, layout);
#ifdef DEBUG
b.CopyNameFrom(layout, "[nongc] ");
#endif
return typGetCustomLayout(b);
}
ClassLayout* Compiler::typGetByrefLayout(ClassLayout* layout)
{
assert(layout->HasGCPtr());
ClassLayoutBuilder b(this, layout->GetSize());
b.CopyPaddingFrom(0, layout);
b.CopyGCInfoFromMakeByref(0, layout);
#ifdef DEBUG
b.CopyNameFrom(layout, "[byref] ");
#endif
return typGetCustomLayout(b);
}
#ifdef DEBUG
//------------------------------------------------------------------------
// CopyNameFrom: Copy layout names, with optional prefix.
//
// Parameters:
// layout - layout to copy from
// prefix - prefix to add (or nullptr)
//
void ClassLayoutBuilder::CopyNameFrom(ClassLayout* layout, const char* prefix)
{
const char* layoutName = layout->GetClassName();
const char* layoutShortName = layout->GetShortClassName();
if (prefix != nullptr)
{
char* newName = nullptr;
char* newShortName = nullptr;
if (layoutName != nullptr)
{
size_t len = strlen(prefix) + strlen(layoutName) + 1;
newName = m_compiler->getAllocator(CMK_DebugOnly).allocate<char>(len);
sprintf_s(newName, len, "%s%s", prefix, layoutShortName);
}
if (layoutShortName != nullptr)
{
size_t len = strlen(prefix) + strlen(layoutName) + 1;
newShortName = m_compiler->getAllocator(CMK_DebugOnly).allocate<char>(len);
sprintf_s(newShortName, len, "%s%s", prefix, layoutShortName);
}
SetName(newName, newShortName);
}
else
{
SetName(layoutName, layoutShortName);
}
}
#endif // DEBUG
//------------------------------------------------------------------------
// Create: Create a ClassLayout from an EE side class handle.
//
// Parameters:
// compiler - The Compiler object
// classHandle - The class handle
//
// Return value:
// New layout representing an EE side class.
//
ClassLayout* ClassLayout::Create(Compiler* compiler, CORINFO_CLASS_HANDLE classHandle)
{
bool isValueClass = compiler->eeIsValueClass(classHandle);
unsigned size;
if (isValueClass)
{
size = compiler->info.compCompHnd->getClassSize(classHandle);
}
else
{
size = compiler->info.compCompHnd->getHeapClassSize(classHandle);
}
var_types type = compiler->impNormStructType(classHandle);
INDEBUG(const char* className = compiler->eeGetClassName(classHandle);)
INDEBUG(const char* shortClassName = compiler->eeGetShortClassName(classHandle);)
ClassLayout* layout = new (compiler, CMK_ClassLayout)
ClassLayout(classHandle, isValueClass, size, type DEBUGARG(className) DEBUGARG(shortClassName));
if (layout->m_size < TARGET_POINTER_SIZE)
{
assert(layout->GetSlotCount() == 1);
assert(layout->m_gcPtrCount == 0);
layout->m_gcPtrsArray[0] = TYPE_GC_NONE;
}
else
{
BYTE* gcPtrs;
if (layout->GetSlotCount() <= sizeof(m_gcPtrsArray))
{
gcPtrs = layout->m_gcPtrsArray;
}
else
{
layout->m_gcPtrs = gcPtrs = new (compiler, CMK_ClassLayout) BYTE[layout->GetSlotCount()];
}
unsigned gcPtrCount = compiler->info.compCompHnd->getClassGClayout(classHandle, gcPtrs);
assert((gcPtrCount == 0) || ((compiler->info.compCompHnd->getClassAttribs(classHandle) &
(CORINFO_FLG_CONTAINS_GC_PTR | CORINFO_FLG_BYREF_LIKE)) != 0));
// Since class size is unsigned there's no way we could have more than 2^30 slots
// so it should be safe to fit this into a 30 bits bit field.
assert(gcPtrCount < (1 << 30));
layout->m_gcPtrCount = gcPtrCount;
}
return layout;
}
//------------------------------------------------------------------------
// Create: Create a ClassLayout from a ClassLayoutBuilder.
//
// Parameters:
// compiler - The Compiler object
// builder - Builder representing the layout
//
// Return value:
// New layout representing a custom (JIT internal) class layout.
//
ClassLayout* ClassLayout::Create(Compiler* compiler, const ClassLayoutBuilder& builder)
{
ClassLayout* newLayout = new (compiler, CMK_ClassLayout) ClassLayout(builder.m_size);
newLayout->m_gcPtrCount = builder.m_gcPtrCount;
newLayout->m_nonPadding = builder.m_nonPadding;
#ifdef DEBUG
newLayout->m_name = builder.m_name;
newLayout->m_shortName = builder.m_shortName;
#endif
if (newLayout->GetSlotCount() <= sizeof(newLayout->m_gcPtrsArray))
{
if (builder.m_gcPtrCount > 0)
{
memcpy(newLayout->m_gcPtrsArray, builder.m_gcPtrs, newLayout->GetSlotCount());
}
else
{
memset(newLayout->m_gcPtrsArray, TYPE_GC_NONE, newLayout->GetSlotCount());
}
}
else if (builder.m_gcPtrCount > 0)
{
newLayout->m_gcPtrs = builder.m_gcPtrs;
}
else
{
newLayout->m_gcPtrs = new (compiler, CMK_ClassLayout) BYTE[newLayout->GetSlotCount()]{};
}
return newLayout;
}
//------------------------------------------------------------------------
// IsStackOnly: does the layout represent a block that can never be on the heap?
//
// Parameters:
// comp - The Compiler object
//
// Return value:
// true if the block is stack only
//
bool ClassLayout::IsStackOnly(Compiler* comp) const
{
// Byref-like structs are stack only
if ((m_classHandle != NO_CLASS_HANDLE) && comp->eeIsByrefLike(m_classHandle))
{
return true;
}
return false;
}
//------------------------------------------------------------------------
// IntersectsGCPtr: check if the specified interval intersects with a GC
// pointer.
//
// Parameters:
// offset - The start offset of the interval
// size - The size of the interval
//
// Return value:
// True if it does.
//
bool ClassLayout::IntersectsGCPtr(unsigned offset, unsigned size) const
{
if (!HasGCPtr())
{
return false;
}
unsigned startSlot = offset / TARGET_POINTER_SIZE;
unsigned endSlot = (offset + size - 1) / TARGET_POINTER_SIZE;
assert((startSlot < GetSlotCount()) && (endSlot < GetSlotCount()));
for (unsigned i = startSlot; i <= endSlot; i++)
{
if (IsGCPtr(i))
{
return true;
}
}
return false;
}
//------------------------------------------------------------------------
// GetNonPadding:
// Get a SegmentList containing segments for all the non-padding in the
// layout. This is generally the areas of the layout covered by fields, but
// in some cases may also include other parts.
//
// Parameters:
// comp - Compiler instance
//
// Return value:
// A segment list.
//
const SegmentList& ClassLayout::GetNonPadding(Compiler* comp)
{
if (m_nonPadding != nullptr)
{
return *m_nonPadding;
}
m_nonPadding = new (comp, CMK_ClassLayout) SegmentList(comp->getAllocator(CMK_ClassLayout));
if (IsCustomLayout())
{
if (m_size > 0)
{
m_nonPadding->Add(SegmentList::Segment(0, GetSize()));
}
return *m_nonPadding;
}
CORINFO_TYPE_LAYOUT_NODE nodes[256];
size_t numNodes = ArrLen(nodes);
GetTypeLayoutResult result = comp->info.compCompHnd->getTypeLayout(GetClassHandle(), nodes, &numNodes);
if (result != GetTypeLayoutResult::Success)
{
m_nonPadding->Add(SegmentList::Segment(0, GetSize()));
}
else
{
for (size_t i = 0; i < numNodes; i++)
{
const CORINFO_TYPE_LAYOUT_NODE& node = nodes[i];
if ((node.type != CORINFO_TYPE_VALUECLASS) || (node.simdTypeHnd != NO_CLASS_HANDLE) ||
node.hasSignificantPadding)
{
m_nonPadding->Add(SegmentList::Segment(node.offset, node.offset + node.size));
}
}
}
return *m_nonPadding;
}
//------------------------------------------------------------------------
// AreCompatible: check if 2 layouts are the same for copying.
//
// Arguments:
// layout1 - the first layout (copy destination)
// layout2 - the second layout (copy source)
//
// Return value:
// true if compatible, false otherwise.
//
// Notes:
// Layouts are called compatible if they are equal or if
// they have the same size and the same GC slots.
//
// static
bool ClassLayout::AreCompatible(const ClassLayout* layout1, const ClassLayout* layout2)
{
if ((layout1 == nullptr) || (layout2 == nullptr))
{
return false;
}
CORINFO_CLASS_HANDLE clsHnd1 = layout1->GetClassHandle();
CORINFO_CLASS_HANDLE clsHnd2 = layout2->GetClassHandle();
if ((clsHnd1 != NO_CLASS_HANDLE) == (clsHnd2 != NO_CLASS_HANDLE))
{
// Either both are class-based layout or both are custom layouts.
// Custom layouts only match each other if they are the same pointer.
if (clsHnd1 == NO_CLASS_HANDLE)
{
return layout1 == layout2;
}
// For class-based layouts they are definitely compatible for the same
// handle
if (clsHnd1 == clsHnd2)
{
return true;
}
// But they may still be compatible for different handles.
}
if (layout1->GetSize() != layout2->GetSize())
{
return false;
}
if (layout1->HasGCPtr() != layout2->HasGCPtr())
{
return false;
}
if (layout1->GetType() != layout2->GetType())
{
return false;
}
if (!layout1->HasGCPtr() && !layout2->HasGCPtr())
{
return true;
}
// assert(clsHnd1 != NO_CLASS_HANDLE);
// assert(clsHnd2 != NO_CLASS_HANDLE);
assert(layout1->HasGCPtr() && layout2->HasGCPtr());
if (layout1->GetGCPtrCount() != layout2->GetGCPtrCount())
{
return false;
}
assert(layout1->GetSlotCount() == layout2->GetSlotCount());
unsigned slotsCount = layout1->GetSlotCount();
for (unsigned i = 0; i < slotsCount; ++i)
{
if (layout1->GetGCPtrType(i) != layout2->GetGCPtrType(i))
{
// Allow a source GC_REF to match a custom GC_BYREF
//
if ((layout2->GetGCPtrType(i) == TYP_REF) && (layout1->GetGCPtrType(i) == TYP_BYREF) &&
layout1->IsCustomLayout())
{
continue;
}
return false;
}
}
return true;
}
//------------------------------------------------------------------------
// ClassLayoutBuilder: Construct a new builder for a class layout of the
// specified size.
//
// Arguments:
// compiler - Compiler instance
// size - Size of the layout
//
ClassLayoutBuilder::ClassLayoutBuilder(Compiler* compiler, unsigned size)
: m_compiler(compiler)
, m_size(size)
{
}
//------------------------------------------------------------------------
// BuildArray: Construct a builder for an array layout
//
// Arguments:
// compiler - Compiler instance
// arrayHandle - class handle for array
// length - array length (in elements)
//
// Note:
// For arrays of structs we currently do not copy any struct padding,
// with the presumption that it is unlikely we will ever promote array elements.
//
ClassLayoutBuilder ClassLayoutBuilder::BuildArray(Compiler* compiler, CORINFO_CLASS_HANDLE arrayHandle, unsigned length)
{
assert(length <= CORINFO_Array_MaxLength);
assert(arrayHandle != NO_CLASS_HANDLE);
CORINFO_CLASS_HANDLE elemClsHnd = NO_CLASS_HANDLE;
CorInfoType corType = compiler->info.compCompHnd->getChildType(arrayHandle, &elemClsHnd);
var_types type = JITtype2varType(corType);
ClassLayout* elementLayout = nullptr;
unsigned elementSize = 0;
if (type == TYP_STRUCT)
{
elementLayout = compiler->typGetObjLayout(elemClsHnd);
elementSize = elementLayout->GetSize();
}
else
{
elementSize = genTypeSize(type);
}
ClrSafeInt<unsigned> totalSize(elementSize);
totalSize *= static_cast<unsigned>(length);
totalSize.AlignUp(TARGET_POINTER_SIZE);
totalSize += static_cast<unsigned>(OFFSETOF__CORINFO_Array__data);
assert(!totalSize.IsOverflow());
ClassLayoutBuilder builder(compiler, totalSize.Value());
if (elementLayout != nullptr)
{
if (elementLayout->HasGCPtr())
{
unsigned offset = OFFSETOF__CORINFO_Array__data;
for (unsigned i = 0; i < length; i++)
{
builder.CopyGCInfoFrom(offset, elementLayout);
offset += elementSize;
}
}
}
else if (varTypeIsGC(type))
{
unsigned offset = OFFSETOF__CORINFO_Array__data;
for (unsigned i = 0; i < length; i++)
{
assert((offset % TARGET_POINTER_SIZE) == 0);
unsigned const slot = offset / TARGET_POINTER_SIZE;
builder.SetGCPtrType(slot, type);
offset += elementSize;
}
}
#ifdef DEBUG
const char* className = compiler->eeGetClassName(arrayHandle);
const char* shortClassName = compiler->eeGetShortClassName(arrayHandle);
builder.SetName(className, shortClassName);
#endif
return builder;
}
//------------------------------------------------------------------------
// GetOrCreateGCPtrs: Get or create the array indicating GC pointer types.
//
// Returns:
// The array of CorInfoGCType.
//
BYTE* ClassLayoutBuilder::GetOrCreateGCPtrs()
{
assert(m_size % TARGET_POINTER_SIZE == 0);
if (m_gcPtrs == nullptr)
{
m_gcPtrs = new (m_compiler, CMK_ClassLayout) BYTE[m_size / TARGET_POINTER_SIZE]{};
}
return m_gcPtrs;
}
//------------------------------------------------------------------------
// SetGCPtr: Set a slot to have specified GC pointer type.
//
// Arguments:
// slot - The GC pointer slot. The slot number corresponds to offset slot * TARGET_POINTER_SIZE.
// type - Type of GC pointer that this slot contains.
//
// Remarks:
// GC pointer information can only be set in layouts of size divisible by
// TARGET_POINTER_SIZE.
//
void ClassLayoutBuilder::SetGCPtr(unsigned slot, CorInfoGCType type)
{
BYTE* ptrs = GetOrCreateGCPtrs();
assert(slot * TARGET_POINTER_SIZE < m_size);
if (ptrs[slot] != TYPE_GC_NONE)
{
m_gcPtrCount--;
}
ptrs[slot] = static_cast<BYTE>(type);
if (type != TYPE_GC_NONE)
{
m_gcPtrCount++;
}
}
//------------------------------------------------------------------------
// SetGCPtrType: Set a slot to have specified type.
//
// Arguments:
// slot - The GC pointer slot. The slot number corresponds to offset slot * TARGET_POINTER_SIZE.
// type - Type that this slot contains. Must be TYP_REF, TYP_BYREF or TYP_I_IMPL.
//
// Remarks:
// GC pointer information can only be set in layouts of size divisible by
// TARGET_POINTER_SIZE.
//
void ClassLayoutBuilder::SetGCPtrType(unsigned slot, var_types type)
{
switch (type)
{
case TYP_REF:
SetGCPtr(slot, TYPE_GC_REF);
break;
case TYP_BYREF:
SetGCPtr(slot, TYPE_GC_BYREF);
break;
case TYP_I_IMPL:
SetGCPtr(slot, TYPE_GC_NONE);
break;
default:
assert(!"Invalid type passed to ClassLayoutBuilder::SetGCPtrType");
break;
}
}
//------------------------------------------------------------------------
// CopyInfoGCFrom: Copy GC pointers from another layout.
//
// Arguments:
// offset - Offset in this builder to start copy information into.
// layout - Layout to get information from.
//
void ClassLayoutBuilder::CopyGCInfoFrom(unsigned offset, ClassLayout* layout)
{
assert(offset + layout->GetSize() <= m_size);
if (layout->GetGCPtrCount() > 0)
{
assert(offset % TARGET_POINTER_SIZE == 0);
unsigned startSlot = offset / TARGET_POINTER_SIZE;
for (unsigned slot = 0; slot < layout->GetSlotCount(); slot++)
{
SetGCPtr(startSlot + slot, layout->GetGCPtr(slot));
}
}
}
//------------------------------------------------------------------------
// CopyInfoGCFromMakeByref: Copy GC pointers from another layout,and change
// all gc references to be TYP_BYREF (TYPE_GC_BYREF)
//
// Arguments:
// offset - Offset in this builder to start copy information into.
// layout - Layout to get information from.
//
void ClassLayoutBuilder::CopyGCInfoFromMakeByref(unsigned offset, ClassLayout* layout)
{
assert(offset + layout->GetSize() <= m_size);