forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.cpp
542 lines (457 loc) · 15.5 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
// 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"
// 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<unsigned, JitSmallPrimitiveKeyFuncs<unsigned>, unsigned> BlkLayoutIndexMap;
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;
BlkLayoutIndexMap* m_blkLayoutMap;
ObjLayoutIndexMap* m_objLayoutMap;
};
};
// The number of layout objects stored in this table.
unsigned m_layoutCount;
// The capacity of m_layoutLargeArray (when more than 3 layouts are stored).
unsigned m_layoutLargeCapacity;
// 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_layoutCount(0), m_layoutLargeCapacity(0), 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* GetBlkLayout(Compiler* compiler, unsigned blockSize)
{
if (blockSize == 0)
{
return &m_zeroSizedBlockLayout;
}
return GetLayoutByIndex(GetBlkLayoutIndex(compiler, blockSize));
}
// Get a number that uniquely identifies a layout having the specified size but no class handle.
unsigned GetBlkLayoutNum(Compiler* compiler, unsigned blockSize)
{
if (blockSize == 0)
{
return ZeroSizedBlockLayoutNum;
}
return GetBlkLayoutIndex(compiler, blockSize) + 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->IsBlockLayout() && m_blkLayoutMap->Lookup(layout->GetSize(), &index)) ||
m_objLayoutMap->Lookup(layout->GetClassHandle(), &index))
{
return index;
}
}
unreached();
}
unsigned GetBlkLayoutIndex(Compiler* compiler, unsigned blockSize)
{
// The 0-sized block layout has its own fast path.
assert(blockSize != 0);
if (HasSmallCapacity())
{
for (unsigned i = 0; i < m_layoutCount; i++)
{
if (m_layoutArray[i]->IsBlockLayout() && (m_layoutArray[i]->GetSize() == blockSize))
{
return i;
}
}
}
else
{
unsigned index;
if (m_blkLayoutMap->Lookup(blockSize, &index))
{
return index;
}
}
return AddBlkLayout(compiler, CreateBlkLayout(compiler, blockSize));
}
ClassLayout* CreateBlkLayout(Compiler* compiler, unsigned blockSize)
{
return new (compiler, CMK_ClassLayout) ClassLayout(blockSize);
}
unsigned AddBlkLayout(Compiler* compiler, ClassLayout* layout)
{
if (m_layoutCount < ArrLen(m_layoutArray))
{
m_layoutArray[m_layoutCount] = layout;
return m_layoutCount++;
}
unsigned index = AddLayoutLarge(compiler, layout);
m_blkLayoutMap->Set(layout->GetSize(), 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, CreateObjLayout(compiler, classHandle));
}
ClassLayout* CreateObjLayout(Compiler* compiler, CORINFO_CLASS_HANDLE classHandle)
{
return 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))
{
BlkLayoutIndexMap* blkLayoutMap = new (alloc) BlkLayoutIndexMap(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->IsBlockLayout())
{
blkLayoutMap->Set(l->GetSize(), i);
}
else
{
objLayoutMap->Set(l->GetClassHandle(), i);
}
}
m_blkLayoutMap = blkLayoutMap;
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::typGetBlkLayoutNum(unsigned blockSize)
{
return typGetClassLayoutTable()->GetBlkLayoutNum(this, blockSize);
}
ClassLayout* Compiler::typGetBlkLayout(unsigned blockSize)
{
return typGetClassLayoutTable()->GetBlkLayout(this, blockSize);
}
unsigned Compiler::typGetObjLayoutNum(CORINFO_CLASS_HANDLE classHandle)
{
return typGetClassLayoutTable()->GetObjLayoutNum(this, classHandle);
}
ClassLayout* Compiler::typGetObjLayout(CORINFO_CLASS_HANDLE classHandle)
{
return typGetClassLayoutTable()->GetObjLayout(this, classHandle);
}
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));
layout->InitializeGCPtrs(compiler);
return layout;
}
void ClassLayout::InitializeGCPtrs(Compiler* compiler)
{
assert(!m_gcPtrsInitialized);
assert(!IsBlockLayout());
if (m_size < TARGET_POINTER_SIZE)
{
assert(GetSlotCount() == 1);
assert(m_gcPtrCount == 0);
m_gcPtrsArray[0] = TYPE_GC_NONE;
}
else
{
BYTE* gcPtrs;
if (GetSlotCount() > sizeof(m_gcPtrsArray))
{
gcPtrs = m_gcPtrs = new (compiler, CMK_ClassLayout) BYTE[GetSlotCount()];
}
else
{
gcPtrs = m_gcPtrsArray;
}
unsigned gcPtrCount = compiler->info.compCompHnd->getClassGClayout(m_classHandle, gcPtrs);
assert((gcPtrCount == 0) || ((compiler->info.compCompHnd->getClassAttribs(m_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));
m_gcPtrCount = gcPtrCount;
}
INDEBUG(m_gcPtrsInitialized = true;)
}
//------------------------------------------------------------------------
// HasGCByRef: does the layout contain at least one GC ByRef
//
// Return value:
// true if at least one GC ByRef, false otherwise.
//
bool ClassLayout::HasGCByRef() const
{
unsigned slots = GetSlotCount();
for (unsigned i = 0; i < slots; i++)
{
if (IsGCByRef(i))
{
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;
}
//------------------------------------------------------------------------
// AreCompatible: check if 2 layouts are the same for copying.
//
// Arguments:
// layout1 - the first layout;
// layout2 - the second layout.
//
// 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) && (clsHnd1 == clsHnd2))
{
return true;
}
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))
{
return false;
}
}
return true;
}