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
/
gcinfo.cpp
838 lines (705 loc) · 26 KB
/
gcinfo.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
// 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.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX GCInfo XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "gcinfo.h"
#include "emit.h"
#include "jitgcinfo.h"
#ifdef _TARGET_AMD64_
#include "gcinfoencoder.h" //this includes a LOT of other files too
#endif
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
extern int JITGcBarrierCall;
/*****************************************************************************/
#if MEASURE_PTRTAB_SIZE
/* static */ size_t GCInfo::s_gcRegPtrDscSize = 0;
/* static */ size_t GCInfo::s_gcTotalPtrTabSize = 0;
#endif // MEASURE_PTRTAB_SIZE
/*
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX GCInfo XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
GCInfo::GCInfo(Compiler* theCompiler) : compiler(theCompiler)
{
regSet = nullptr;
gcVarPtrList = nullptr;
gcVarPtrLast = nullptr;
gcRegPtrList = nullptr;
gcRegPtrLast = nullptr;
gcPtrArgCnt = 0;
gcCallDescList = nullptr;
gcCallDescLast = nullptr;
#ifdef JIT32_GCENCODER
gcEpilogTable = nullptr;
#else // !JIT32_GCENCODER
m_regSlotMap = nullptr;
m_stackSlotMap = nullptr;
#endif // JIT32_GCENCODER
}
/*****************************************************************************/
/*****************************************************************************
* Reset tracking info at the start of a basic block.
*/
void GCInfo::gcResetForBB()
{
gcRegGCrefSetCur = RBM_NONE;
gcRegByrefSetCur = RBM_NONE;
VarSetOps::AssignNoCopy(compiler, gcVarPtrSetCur, VarSetOps::MakeEmpty(compiler));
}
#ifdef DEBUG
/*****************************************************************************
*
* Print the changes in the gcRegGCrefSetCur sets.
*/
void GCInfo::gcDspGCrefSetChanges(regMaskTP gcRegGCrefSetNew DEBUGARG(bool forceOutput))
{
if (compiler->verbose)
{
if (forceOutput || (gcRegGCrefSetCur != gcRegGCrefSetNew))
{
printf("\t\t\t\t\t\t\tGC regs: ");
if (gcRegGCrefSetCur == gcRegGCrefSetNew)
{
printf("(unchanged) ");
}
else
{
printRegMaskInt(gcRegGCrefSetCur);
compiler->GetEmitter()->emitDispRegSet(gcRegGCrefSetCur);
printf(" => ");
}
printRegMaskInt(gcRegGCrefSetNew);
compiler->GetEmitter()->emitDispRegSet(gcRegGCrefSetNew);
printf("\n");
}
}
}
/*****************************************************************************
*
* Print the changes in the gcRegByrefSetCur sets.
*/
void GCInfo::gcDspByrefSetChanges(regMaskTP gcRegByrefSetNew DEBUGARG(bool forceOutput))
{
if (compiler->verbose)
{
if (forceOutput || (gcRegByrefSetCur != gcRegByrefSetNew))
{
printf("\t\t\t\t\t\t\tByref regs: ");
if (gcRegByrefSetCur == gcRegByrefSetNew)
{
printf("(unchanged) ");
}
else
{
printRegMaskInt(gcRegByrefSetCur);
compiler->GetEmitter()->emitDispRegSet(gcRegByrefSetCur);
printf(" => ");
}
printRegMaskInt(gcRegByrefSetNew);
compiler->GetEmitter()->emitDispRegSet(gcRegByrefSetNew);
printf("\n");
}
}
}
#endif // DEBUG
/*****************************************************************************
*
* Mark the set of registers given by the specified mask as holding
* GCref pointer values.
*/
void GCInfo::gcMarkRegSetGCref(regMaskTP regMask DEBUGARG(bool forceOutput))
{
#ifdef DEBUG
if (compiler->compRegSetCheckLevel == 0)
{
// This set of registers are going to hold REFs.
// Make sure they were not holding BYREFs.
assert((gcRegByrefSetCur & regMask) == 0);
}
#endif
regMaskTP gcRegByrefSetNew = gcRegByrefSetCur & ~regMask; // Clear it if set in Byref mask
regMaskTP gcRegGCrefSetNew = gcRegGCrefSetCur | regMask; // Set it in GCref mask
INDEBUG(gcDspGCrefSetChanges(gcRegGCrefSetNew, forceOutput));
INDEBUG(gcDspByrefSetChanges(gcRegByrefSetNew));
gcRegByrefSetCur = gcRegByrefSetNew;
gcRegGCrefSetCur = gcRegGCrefSetNew;
}
/*****************************************************************************
*
* Mark the set of registers given by the specified mask as holding
* Byref pointer values.
*/
void GCInfo::gcMarkRegSetByref(regMaskTP regMask DEBUGARG(bool forceOutput))
{
regMaskTP gcRegByrefSetNew = gcRegByrefSetCur | regMask; // Set it in Byref mask
regMaskTP gcRegGCrefSetNew = gcRegGCrefSetCur & ~regMask; // Clear it if set in GCref mask
INDEBUG(gcDspGCrefSetChanges(gcRegGCrefSetNew));
INDEBUG(gcDspByrefSetChanges(gcRegByrefSetNew, forceOutput));
gcRegByrefSetCur = gcRegByrefSetNew;
gcRegGCrefSetCur = gcRegGCrefSetNew;
}
/*****************************************************************************
*
* Mark the set of registers given by the specified mask as holding
* non-pointer values.
*/
void GCInfo::gcMarkRegSetNpt(regMaskTP regMask DEBUGARG(bool forceOutput))
{
/* NOTE: don't unmark any live register variables */
regMaskTP gcRegByrefSetNew = gcRegByrefSetCur & ~(regMask & ~regSet->GetMaskVars());
regMaskTP gcRegGCrefSetNew = gcRegGCrefSetCur & ~(regMask & ~regSet->GetMaskVars());
INDEBUG(gcDspGCrefSetChanges(gcRegGCrefSetNew, forceOutput));
INDEBUG(gcDspByrefSetChanges(gcRegByrefSetNew, forceOutput));
gcRegByrefSetCur = gcRegByrefSetNew;
gcRegGCrefSetCur = gcRegGCrefSetNew;
}
/*****************************************************************************
*
* Mark the specified register as now holding a value of the given type.
*/
void GCInfo::gcMarkRegPtrVal(regNumber reg, var_types type)
{
regMaskTP regMask = genRegMask(reg);
switch (type)
{
case TYP_REF:
gcMarkRegSetGCref(regMask);
break;
case TYP_BYREF:
gcMarkRegSetByref(regMask);
break;
default:
gcMarkRegSetNpt(regMask);
break;
}
}
/*****************************************************************************/
GCInfo::WriteBarrierForm GCInfo::gcIsWriteBarrierCandidate(GenTree* tgt, GenTree* assignVal)
{
/* Are we storing a GC ptr? */
if (!varTypeIsGC(tgt->TypeGet()))
{
return WBF_NoBarrier;
}
/* Ignore any assignments of NULL */
// 'assignVal' can be the constant Null or something else (LclVar, etc..)
// that is known to be null via Value Numbering.
if (assignVal->GetVN(VNK_Liberal) == ValueNumStore::VNForNull())
{
return WBF_NoBarrier;
}
if (assignVal->gtOper == GT_CNS_INT && assignVal->AsIntCon()->gtIconVal == 0)
{
return WBF_NoBarrier;
}
/* Where are we storing into? */
tgt = tgt->gtEffectiveVal();
switch (tgt->gtOper)
{
case GT_STOREIND:
case GT_IND: /* Could be the managed heap */
if (tgt->TypeGet() == TYP_BYREF)
{
// Byref values cannot be in managed heap.
// This case occurs for Span<T>.
return WBF_NoBarrier;
}
if (tgt->gtFlags & GTF_IND_TGT_NOT_HEAP)
{
// This indirection is not from to the heap.
// This case occurs for stack-allocated objects.
return WBF_NoBarrier;
}
return gcWriteBarrierFormFromTargetAddress(tgt->AsOp()->gtOp1);
case GT_LEA:
return gcWriteBarrierFormFromTargetAddress(tgt->AsAddrMode()->Base());
case GT_ARR_ELEM: /* Definitely in the managed heap */
case GT_CLS_VAR:
return WBF_BarrierUnchecked;
case GT_LCL_VAR: /* Definitely not in the managed heap */
case GT_LCL_FLD:
case GT_STORE_LCL_VAR:
case GT_STORE_LCL_FLD:
return WBF_NoBarrier;
default:
break;
}
assert(!"Missing case in gcIsWriteBarrierCandidate");
return WBF_NoBarrier;
}
bool GCInfo::gcIsWriteBarrierStoreIndNode(GenTree* op)
{
assert(op->OperIs(GT_STOREIND));
return gcIsWriteBarrierCandidate(op, op->AsOp()->gtOp2) != WBF_NoBarrier;
}
/*****************************************************************************/
/*****************************************************************************
*
* Initialize the non-register pointer variable tracking logic.
*/
void GCInfo::gcVarPtrSetInit()
{
VarSetOps::AssignNoCopy(compiler, gcVarPtrSetCur, VarSetOps::MakeEmpty(compiler));
/* Initialize the list of lifetime entries */
gcVarPtrList = gcVarPtrLast = nullptr;
}
/*****************************************************************************
*
* Allocate a new pointer register set / pointer argument entry and append
* it to the list.
*/
GCInfo::regPtrDsc* GCInfo::gcRegPtrAllocDsc()
{
regPtrDsc* regPtrNext;
assert(compiler->IsFullPtrRegMapRequired());
/* Allocate a new entry and initialize it */
regPtrNext = new (compiler, CMK_GC) regPtrDsc;
regPtrNext->rpdIsThis = FALSE;
regPtrNext->rpdOffs = 0;
regPtrNext->rpdNext = nullptr;
// Append the entry to the end of the list.
if (gcRegPtrLast == nullptr)
{
assert(gcRegPtrList == nullptr);
gcRegPtrList = gcRegPtrLast = regPtrNext;
}
else
{
assert(gcRegPtrList != nullptr);
gcRegPtrLast->rpdNext = regPtrNext;
gcRegPtrLast = regPtrNext;
}
#if MEASURE_PTRTAB_SIZE
s_gcRegPtrDscSize += sizeof(*regPtrNext);
#endif
return regPtrNext;
}
#ifdef JIT32_GCENCODER
/*****************************************************************************
*
* Compute the various counts that get stored in the info block header.
*/
void GCInfo::gcCountForHeader(UNALIGNED unsigned int* pUntrackedCount, UNALIGNED unsigned int* pVarPtrTableSize)
{
unsigned varNum;
LclVarDsc* varDsc;
bool keepThisAlive = false; // did we track "this" in a synchronized method?
unsigned int untrackedCount = 0;
// Count the untracked locals and non-enregistered args.
for (varNum = 0, varDsc = compiler->lvaTable; varNum < compiler->lvaCount; varNum++, varDsc++)
{
if (compiler->lvaIsFieldOfDependentlyPromotedStruct(varDsc))
{
// Field local of a PROMOTION_TYPE_DEPENDENT struct must have been
// reported through its parent local
continue;
}
if (varTypeIsGC(varDsc->TypeGet()))
{
if (!gcIsUntrackedLocalOrNonEnregisteredArg(varNum, &keepThisAlive))
{
continue;
}
#ifdef DEBUG
if (compiler->verbose)
{
int offs = varDsc->lvStkOffs;
printf("GCINFO: untrckd %s lcl at [%s", varTypeGCstring(varDsc->TypeGet()),
compiler->GetEmitter()->emitGetFrameReg());
if (offs < 0)
{
printf("-%02XH", -offs);
}
else if (offs > 0)
{
printf("+%02XH", +offs);
}
printf("]\n");
}
#endif
untrackedCount++;
}
else if ((varDsc->TypeGet() == TYP_STRUCT) && varDsc->lvOnFrame)
{
untrackedCount += varDsc->GetLayout()->GetGCPtrCount();
}
}
// Also count spill temps that hold pointers.
assert(regSet->tmpAllFree());
for (TempDsc* tempThis = regSet->tmpListBeg(); tempThis != nullptr; tempThis = regSet->tmpListNxt(tempThis))
{
if (varTypeIsGC(tempThis->tdTempType()) == false)
{
continue;
}
#ifdef DEBUG
if (compiler->verbose)
{
int offs = tempThis->tdTempOffs();
printf("GCINFO: untrck %s Temp at [%s", varTypeGCstring(varDsc->TypeGet()),
compiler->GetEmitter()->emitGetFrameReg());
if (offs < 0)
{
printf("-%02XH", -offs);
}
else if (offs > 0)
{
printf("+%02XH", +offs);
}
printf("]\n");
}
#endif
untrackedCount++;
}
#ifdef DEBUG
if (compiler->verbose)
{
printf("GCINFO: untrckVars = %u\n", untrackedCount);
}
#endif
*pUntrackedCount = untrackedCount;
// Count the number of entries in the table of non-register pointer variable lifetimes.
unsigned int varPtrTableSize = 0;
if (keepThisAlive)
{
varPtrTableSize++;
}
if (gcVarPtrList != nullptr)
{
// We'll use a delta encoding for the lifetime offsets.
for (varPtrDsc* varTmp = gcVarPtrList; varTmp != nullptr; varTmp = varTmp->vpdNext)
{
// Special case: skip any 0-length lifetimes.
if (varTmp->vpdBegOfs == varTmp->vpdEndOfs)
{
continue;
}
varPtrTableSize++;
}
}
#ifdef DEBUG
if (compiler->verbose)
{
printf("GCINFO: trackdLcls = %u\n", varPtrTableSize);
}
#endif
*pVarPtrTableSize = varPtrTableSize;
}
//------------------------------------------------------------------------
// gcIsUntrackedLocalOrNonEnregisteredArg: Check if this varNum with GC type
// corresponds to an untracked local or argument that was not fully enregistered.
//
//
// Arguments:
// varNum - the variable number to check;
// pKeepThisAlive - if !FEATURE_EH_FUNCLETS and the argument != nullptr remember
// if `this` should be kept alive and considered tracked.
//
// Return value:
// true if it an untracked pointer value.
//
bool GCInfo::gcIsUntrackedLocalOrNonEnregisteredArg(unsigned varNum, bool* pKeepThisAlive)
{
LclVarDsc* varDsc = compiler->lvaGetDesc(varNum);
assert(!compiler->lvaIsFieldOfDependentlyPromotedStruct(varDsc));
assert(varTypeIsGC(varDsc->TypeGet()));
// Do we have an argument or local variable?
if (!varDsc->lvIsParam)
{
// If is pinned, it must be an untracked local.
assert(!varDsc->lvPinned || !varDsc->lvTracked);
if (varDsc->lvTracked || !varDsc->lvOnFrame)
{
return false;
}
}
else
{
// Stack-passed arguments which are not enregistered are always reported in this "untracked stack pointers"
// section of the GC info even if lvTracked==true.
// Has this argument been fully enregistered?
if (!varDsc->lvOnFrame)
{
// If a CEE_JMP has been used, then we need to report all the arguments even if they are enregistered, since
// we will be using this value in JMP call. Note that this is subtle as we require that argument offsets
// are always fixed up properly even if lvRegister is set .
if (!compiler->compJmpOpUsed)
{
return false;
}
}
else if (varDsc->lvIsRegArg && varDsc->lvTracked)
{
// If this register-passed arg is tracked, then it has been allocated space near the other pointer variables
// and we have accurate life-time info. It will be reported with gcVarPtrList in the "tracked-pointer"
// section.
return false;
}
}
#if !defined(FEATURE_EH_FUNCLETS)
if (compiler->lvaIsOriginalThisArg(varNum) && compiler->lvaKeepAliveAndReportThis())
{
// "this" is in the untracked variable area, but encoding of untracked variables does not support reporting
// "this". So report it as a tracked variable with a liveness extending over the entire method.
//
// TODO-x86-Cleanup: the semantic here is not clear, it would be useful to check different cases and
// add a description where "this" is saved and how it is tracked in each of them:
// 1) when FEATURE_EH_FUNCLETS defined (x86 Linux);
// 2) when FEATURE_EH_FUNCLETS not defined, lvaKeepAliveAndReportThis == true, compJmpOpUsed == true;
// 3) when there is regPtrDsc for "this", but keepThisAlive == true;
// etc.
if (pKeepThisAlive != nullptr)
{
*pKeepThisAlive = true;
}
return false;
}
#endif // !FEATURE_EH_FUNCLETS
return true;
}
/*****************************************************************************
*
* Shutdown the 'pointer value' register tracking logic and save the necessary
* info (which will be used at runtime to locate all pointers) at the specified
* address. The number of bytes written to 'destPtr' must be identical to that
* returned from gcPtrTableSize().
*/
BYTE* GCInfo::gcPtrTableSave(BYTE* destPtr, const InfoHdr& header, unsigned codeSize, size_t* pArgTabOffset)
{
/* Write the tables to the info block */
return destPtr + gcMakeRegPtrTable(destPtr, -1, header, codeSize, pArgTabOffset);
}
#endif // JIT32_GCENCODER
/*****************************************************************************
*
* Initialize the 'pointer value' register/argument tracking logic.
*/
void GCInfo::gcRegPtrSetInit()
{
gcRegGCrefSetCur = gcRegByrefSetCur = 0;
if (compiler->IsFullPtrRegMapRequired())
{
gcRegPtrList = gcRegPtrLast = nullptr;
}
else
{
/* Initialize the 'call descriptor' list */
gcCallDescList = gcCallDescLast = nullptr;
}
}
#ifdef JIT32_GCENCODER
/*****************************************************************************
*
* Helper passed to genEmitter.emitGenEpilogLst() to generate
* the table of epilogs.
*/
/* static */ size_t GCInfo::gcRecordEpilog(void* pCallBackData, unsigned offset)
{
GCInfo* gcInfo = (GCInfo*)pCallBackData;
assert(gcInfo);
size_t result = encodeUDelta(gcInfo->gcEpilogTable, offset, gcInfo->gcEpilogPrevOffset);
if (gcInfo->gcEpilogTable)
gcInfo->gcEpilogTable += result;
gcInfo->gcEpilogPrevOffset = offset;
return result;
}
#endif // JIT32_GCENCODER
GCInfo::WriteBarrierForm GCInfo::gcWriteBarrierFormFromTargetAddress(GenTree* tgtAddr)
{
// If we store through an int to a GC_REF field, we'll assume that needs to use a checked barriers.
if (tgtAddr->TypeGet() == TYP_I_IMPL)
{
return GCInfo::WBF_BarrierChecked; // Why isn't this GCInfo::WBF_BarrierUnknown?
}
// Otherwise...
assert(tgtAddr->TypeGet() == TYP_BYREF);
bool simplifiedExpr = true;
while (simplifiedExpr)
{
simplifiedExpr = false;
tgtAddr = tgtAddr->gtSkipReloadOrCopy();
while (tgtAddr->OperGet() == GT_ADDR && tgtAddr->AsOp()->gtOp1->OperGet() == GT_IND)
{
tgtAddr = tgtAddr->AsOp()->gtOp1->AsOp()->gtOp1;
simplifiedExpr = true;
assert(tgtAddr->TypeGet() == TYP_BYREF);
}
// For additions, one of the operands is a byref or a ref (and the other is not). Follow this down to its
// source.
while (tgtAddr->OperIs(GT_ADD, GT_LEA))
{
if (tgtAddr->OperGet() == GT_ADD)
{
GenTree* addOp1 = tgtAddr->AsOp()->gtGetOp1();
GenTree* addOp2 = tgtAddr->AsOp()->gtGetOp2();
var_types addOp1Type = addOp1->TypeGet();
var_types addOp2Type = addOp2->TypeGet();
if (addOp1Type == TYP_BYREF || addOp1Type == TYP_REF)
{
assert(addOp2Type != TYP_BYREF && addOp2Type != TYP_REF);
tgtAddr = addOp1;
simplifiedExpr = true;
}
else if (addOp2Type == TYP_BYREF || addOp2Type == TYP_REF)
{
tgtAddr = addOp2;
simplifiedExpr = true;
}
else
{
// We might have a native int. For example:
// const int 0
// + byref
// lclVar int V06 loc5 // this is a local declared "valuetype VType*"
return GCInfo::WBF_BarrierUnknown;
}
}
else
{
// Must be an LEA (i.e., an AddrMode)
assert(tgtAddr->OperGet() == GT_LEA);
tgtAddr = tgtAddr->AsAddrMode()->Base();
if (tgtAddr->TypeGet() == TYP_BYREF || tgtAddr->TypeGet() == TYP_REF)
{
simplifiedExpr = true;
}
else
{
// We might have a native int.
return GCInfo::WBF_BarrierUnknown;
}
}
}
}
if (tgtAddr->IsLocalAddrExpr() != nullptr)
{
// No need for a GC barrier when writing to a local variable.
return GCInfo::WBF_NoBarrier;
}
if (tgtAddr->OperGet() == GT_LCL_VAR)
{
unsigned lclNum = tgtAddr->AsLclVar()->GetLclNum();
LclVarDsc* varDsc = &compiler->lvaTable[lclNum];
// Instead of marking LclVar with 'lvStackByref',
// Consider decomposing the Value Number given to this LclVar to see if it was
// created using a GT_ADDR(GT_LCLVAR) or a GT_ADD( GT_ADDR(GT_LCLVAR), Constant)
// We may have an internal compiler temp created in fgMorphCopyBlock() that we know
// points at one of our stack local variables, it will have lvStackByref set to true.
//
if (varDsc->lvStackByref)
{
assert(varDsc->TypeGet() == TYP_BYREF);
return GCInfo::WBF_NoBarrier;
}
// We don't eliminate for inlined methods, where we (can) know where the "retBuff" points.
if (!compiler->compIsForInlining() && lclNum == compiler->info.compRetBuffArg)
{
assert(compiler->info.compRetType == TYP_STRUCT); // Else shouldn't have a ret buff.
// Are we assured that the ret buff pointer points into the stack of a caller?
if (compiler->info.compRetBuffDefStack)
{
#if 0
// This is an optional debugging mode. If the #if 0 above is changed to #if 1,
// every barrier we remove for stores to GC ref fields of a retbuff use a special
// helper that asserts that the target is not in the heap.
#ifdef DEBUG
return WBF_NoBarrier_CheckNotHeapInDebug;
#else
return WBF_NoBarrier;
#endif
#else // 0
return GCInfo::WBF_NoBarrier;
#endif // 0
}
}
}
if (tgtAddr->TypeGet() == TYP_REF)
{
return GCInfo::WBF_BarrierUnchecked;
}
// Otherwise, we have no information.
return GCInfo::WBF_BarrierUnknown;
}
//------------------------------------------------------------------------
// gcUpdateForRegVarMove: Update the masks when a variable is moved
//
// Arguments:
// srcMask - The register mask for the register(s) from which it is being moved
// dstMask - The register mask for the register(s) to which it is being moved
// type - The type of the variable
//
// Return Value:
// None
//
// Notes:
// This is called during codegen when a var is moved due to an LSRA_ASG.
// It is also called by LinearScan::recordVarLocationAtStartOfBB() which is in turn called by
// CodeGen::genCodeForBBList() at the block boundary.
void GCInfo::gcUpdateForRegVarMove(regMaskTP srcMask, regMaskTP dstMask, LclVarDsc* varDsc)
{
var_types type = varDsc->TypeGet();
bool isGCRef = (type == TYP_REF);
bool isByRef = (type == TYP_BYREF);
if (srcMask != RBM_NONE)
{
regSet->RemoveMaskVars(srcMask);
if (isGCRef)
{
assert((gcRegByrefSetCur & srcMask) == 0);
gcRegGCrefSetCur &= ~srcMask;
gcRegGCrefSetCur |= dstMask; // safe if no dst, i.e. RBM_NONE
}
else if (isByRef)
{
assert((gcRegGCrefSetCur & srcMask) == 0);
gcRegByrefSetCur &= ~srcMask;
gcRegByrefSetCur |= dstMask; // safe if no dst, i.e. RBM_NONE
}
}
else if (isGCRef || isByRef)
{
// In this case, we are moving it from the stack to a register,
// so remove it from the set of live stack gc refs
VarSetOps::RemoveElemD(compiler, gcVarPtrSetCur, varDsc->lvVarIndex);
}
if (dstMask != RBM_NONE)
{
regSet->AddMaskVars(dstMask);
// If the source is a reg, then the gc sets have been set appropriately
// Otherwise, we have to determine whether to set them
if (srcMask == RBM_NONE)
{
if (isGCRef)
{
gcRegGCrefSetCur |= dstMask;
}
else if (isByRef)
{
gcRegByrefSetCur |= dstMask;
}
}
}
else if (isGCRef || isByRef)
{
VarSetOps::AddElemD(compiler, gcVarPtrSetCur, varDsc->lvVarIndex);
}
}
/*****************************************************************************/
/*****************************************************************************/