-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathlclmorph.cpp
1320 lines (1162 loc) · 48.3 KB
/
lclmorph.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"
class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
{
// During tree traversal every GenTree node produces a "value" that represents:
// - the memory location associated with a local variable, including an offset
// accumulated from GT_LCL_FLD and GT_FIELD nodes.
// - the address of local variable memory location, including an offset as well.
// - an unknown value - the result of a node we don't know how to process. This
// also includes the result of TYP_VOID nodes (or any other nodes that don't
// actually produce values in IR) in order to support the invariant that every
// node produces a value.
//
// The existence of GT_ADDR nodes and their use together with GT_FIELD to form
// FIELD/ADDR/FIELD/ADDR/LCL_VAR sequences complicate things a bit. A typical
// GT_FIELD node acts like an indirection and should produce an unknown value,
// local address analysis doesn't know or care what value the field stores.
// But a GT_FIELD can also be used as an operand for a GT_ADDR node and then
// the GT_FIELD node does not perform an indirection, it's just represents a
// location, similar to GT_LCL_VAR and GT_LCL_FLD.
//
// To avoid this issue, the semantics of GT_FIELD (and for simplicity's sake any other
// indirection) nodes slightly deviates from the IR semantics - an indirection does not
// actually produce an unknown value but a location value, if the indirection address
// operand is an address value.
//
// The actual indirection is performed when the indirection's user node is processed:
// - A GT_ADDR user turns the location value produced by the indirection back
// into an address value.
// - Any other user node performs the indirection and produces an unknown value.
//
class Value
{
GenTree* m_node;
FieldSeqNode* m_fieldSeq;
unsigned m_lclNum;
unsigned m_offset;
bool m_address;
INDEBUG(bool m_consumed;)
public:
// Produce an unknown value associated with the specified node.
Value(GenTree* node)
: m_node(node)
, m_fieldSeq(nullptr)
, m_lclNum(BAD_VAR_NUM)
, m_offset(0)
, m_address(false)
#ifdef DEBUG
, m_consumed(false)
#endif // DEBUG
{
}
// Get the node that produced this value.
GenTree* Node() const
{
return m_node;
}
// Does this value represent a location?
bool IsLocation() const
{
return (m_lclNum != BAD_VAR_NUM) && !m_address;
}
// Does this value represent the address of a location?
bool IsAddress() const
{
assert((m_lclNum != BAD_VAR_NUM) || !m_address);
return m_address;
}
// Get the location's variable number.
unsigned LclNum() const
{
assert(IsLocation() || IsAddress());
return m_lclNum;
}
// Get the location's byte offset.
unsigned Offset() const
{
assert(IsLocation() || IsAddress());
return m_offset;
}
// Get the location's field sequence.
FieldSeqNode* FieldSeq() const
{
return m_fieldSeq;
}
//------------------------------------------------------------------------
// Location: Produce a location value.
//
// Arguments:
// lclVar - a GT_LCL_VAR node that defines the location
//
// Notes:
// - (lclnum) => LOCATION(lclNum, 0)
//
void Location(GenTreeLclVar* lclVar)
{
assert(lclVar->OperIs(GT_LCL_VAR));
assert(!IsLocation() && !IsAddress());
m_lclNum = lclVar->GetLclNum();
assert(m_offset == 0);
assert(m_fieldSeq == nullptr);
}
//------------------------------------------------------------------------
// Address: Produce an address value from a GT_LCL_VAR_ADDR node.
//
// Arguments:
// lclVar - a GT_LCL_VAR_ADDR node that defines the address
//
// Notes:
// - (lclnum) => ADDRESS(lclNum, 0)
//
void Address(GenTreeLclVar* lclVar)
{
assert(lclVar->OperIs(GT_LCL_VAR_ADDR));
assert(!IsLocation() && !IsAddress());
m_lclNum = lclVar->GetLclNum();
m_address = true;
assert(m_offset == 0);
assert(m_fieldSeq == nullptr);
}
//------------------------------------------------------------------------
// Location: Produce a location value.
//
// Arguments:
// lclFld - a GT_LCL_FLD node that defines the location
//
// Notes:
// - (lclnum, lclOffs) => LOCATION(lclNum, offset)
//
void Location(GenTreeLclFld* lclFld)
{
assert(lclFld->OperIs(GT_LCL_FLD));
assert(!IsLocation() && !IsAddress());
m_lclNum = lclFld->GetLclNum();
m_offset = lclFld->GetLclOffs();
m_fieldSeq = lclFld->GetFieldSeq();
}
//------------------------------------------------------------------------
// Address: Produce an address value from a LCL_FLD_ADDR node.
//
// Arguments:
// lclFld - a GT_LCL_FLD_ADDR node that defines the address
//
// Notes:
// - (lclnum, lclOffs) => ADDRESS(lclNum, offset)
//
void Address(GenTreeLclFld* lclFld)
{
assert(lclFld->OperIs(GT_LCL_FLD_ADDR));
assert(!IsLocation() && !IsAddress());
m_lclNum = lclFld->GetLclNum();
m_offset = lclFld->GetLclOffs();
m_fieldSeq = lclFld->GetFieldSeq();
m_address = true;
}
//------------------------------------------------------------------------
// Address: Produce an address value from a location value.
//
// Arguments:
// val - the input value
//
// Notes:
// - LOCATION(lclNum, offset) => ADDRESS(lclNum, offset)
// - ADDRESS(lclNum, offset) => invalid, we should never encounter something like ADDR(ADDR(...))
// - UNKNOWN => UNKNOWN
//
void Address(Value& val)
{
assert(!IsLocation() && !IsAddress());
assert(!val.IsAddress());
if (val.IsLocation())
{
m_address = true;
m_lclNum = val.m_lclNum;
m_offset = val.m_offset;
m_fieldSeq = val.m_fieldSeq;
}
INDEBUG(val.Consume();)
}
//------------------------------------------------------------------------
// Field: Produce a location value from an address value.
//
// Arguments:
// val - the input value
// field - the FIELD node that uses the input address value
// compiler - the compiler instance
//
// Return Value:
// `true` if the value was consumed. `false` if the input value
// cannot be consumed because it is itsef a location or because
// the offset overflowed. In this case the caller is expected
// to escape the input value.
//
// Notes:
// - LOCATION(lclNum, offset) => not representable, must escape
// - ADDRESS(lclNum, offset) => LOCATION(lclNum, offset + field.Offset)
// if the offset overflows then location is not representable, must escape
// - UNKNOWN => UNKNOWN
//
bool Field(Value& val, GenTreeField* field, Compiler* compiler)
{
assert(!IsLocation() && !IsAddress());
if (val.IsLocation())
{
return false;
}
if (val.IsAddress())
{
ClrSafeInt<unsigned> newOffset =
ClrSafeInt<unsigned>(val.m_offset) + ClrSafeInt<unsigned>(field->gtFldOffset);
if (newOffset.IsOverflow())
{
return false;
}
m_lclNum = val.m_lclNum;
m_offset = newOffset.Value();
bool haveCorrectFieldForVN;
if (field->gtFldMayOverlap)
{
haveCorrectFieldForVN = false;
}
else
{
LclVarDsc* varDsc = compiler->lvaGetDesc(m_lclNum);
if (!varTypeIsStruct(varDsc))
{
haveCorrectFieldForVN = false;
}
else if (val.m_fieldSeq == nullptr)
{
CORINFO_CLASS_HANDLE clsHnd = varDsc->GetStructHnd();
// If the answer is no we are probably accessing a canon type with a non-canon fldHnd,
// currently it could happen in crossgen2 scenario where VM distinguishes class<canon>._field
// from class<not-canon-ref-type>._field.
haveCorrectFieldForVN =
compiler->info.compCompHnd->doesFieldBelongToClass(field->gtFldHnd, clsHnd);
}
else
{
FieldSeqNode* lastSeqNode = val.m_fieldSeq->GetTail();
assert(lastSeqNode != nullptr);
if (lastSeqNode == FieldSeqStore::NotAField())
{
haveCorrectFieldForVN = false;
}
else
{
CORINFO_FIELD_HANDLE lastFieldBeforeTheCurrent = lastSeqNode->GetFieldHandle();
CORINFO_CLASS_HANDLE clsHnd;
CorInfoType fieldCorType =
compiler->info.compCompHnd->getFieldType(lastFieldBeforeTheCurrent, &clsHnd);
if (fieldCorType != CORINFO_TYPE_VALUECLASS)
{
// For example, System.IntPtr:ToInt64, when inlined, creates trees like
// * FIELD long _value
// \--* ADDR byref
// \--* FIELD long Information
// \--* ADDR byref
// \--* LCL_VAR struct<Interop+NtDll+IO_STATUS_BLOCK, 16> V08 tmp7
haveCorrectFieldForVN = false;
}
else
{
haveCorrectFieldForVN =
compiler->info.compCompHnd->doesFieldBelongToClass(field->gtFldHnd, clsHnd);
noway_assert(haveCorrectFieldForVN);
}
}
}
}
if (haveCorrectFieldForVN)
{
FieldSeqStore* fieldSeqStore = compiler->GetFieldSeqStore();
m_fieldSeq = fieldSeqStore->Append(val.m_fieldSeq, fieldSeqStore->CreateSingleton(field->gtFldHnd));
}
else
{
m_fieldSeq = FieldSeqStore::NotAField();
JITDUMP("Setting NotAField for [%06u],\n", compiler->dspTreeID(field));
}
}
INDEBUG(val.Consume();)
return true;
}
//------------------------------------------------------------------------
// Indir: Produce a location value from an address value.
//
// Arguments:
// val - the input value
//
// Return Value:
// `true` if the value was consumed. `false` if the input value
// cannot be consumed because it is itsef a location. In this
// case the caller is expected to escape the input value.
//
// Notes:
// - LOCATION(lclNum, offset) => not representable, must escape
// - ADDRESS(lclNum, offset) => LOCATION(lclNum, offset)
// - UNKNOWN => UNKNOWN
//
bool Indir(Value& val)
{
assert(!IsLocation() && !IsAddress());
if (val.IsLocation())
{
return false;
}
if (val.IsAddress())
{
m_lclNum = val.m_lclNum;
m_offset = val.m_offset;
m_fieldSeq = val.m_fieldSeq;
}
INDEBUG(val.Consume();)
return true;
}
#ifdef DEBUG
void Consume()
{
assert(!m_consumed);
// Mark the value as consumed so that PopValue can ensure that values
// aren't popped from the stack without being processed appropriately.
m_consumed = true;
}
bool IsConsumed()
{
return m_consumed;
}
#endif // DEBUG
};
ArrayStack<Value> m_valueStack;
INDEBUG(bool m_stmtModified;)
public:
enum
{
DoPreOrder = true,
DoPostOrder = true,
ComputeStack = true,
DoLclVarsOnly = false,
UseExecutionOrder = false,
};
LocalAddressVisitor(Compiler* comp)
: GenTreeVisitor<LocalAddressVisitor>(comp), m_valueStack(comp->getAllocator(CMK_LocalAddressVisitor))
{
}
void VisitStmt(Statement* stmt)
{
#ifdef DEBUG
if (m_compiler->verbose)
{
printf("LocalAddressVisitor visiting statement:\n");
m_compiler->gtDispStmt(stmt);
m_stmtModified = false;
}
#endif // DEBUG
WalkTree(stmt->GetRootNodePointer(), nullptr);
// We could have something a statement like IND(ADDR(LCL_VAR)) so we need to escape
// the location here. This doesn't seem to happen often, if ever. The importer
// tends to wrap such a tree in a COMMA.
if (TopValue(0).IsLocation())
{
EscapeLocation(TopValue(0), nullptr);
}
else
{
// If we have an address on the stack then we don't need to do anything.
// The address tree isn't actually used and it will be discarded during
// morphing. So just mark any value as consumed to keep PopValue happy.
INDEBUG(TopValue(0).Consume();)
}
PopValue();
assert(m_valueStack.Empty());
#ifdef DEBUG
if (m_compiler->verbose)
{
if (m_stmtModified)
{
printf("LocalAddressVisitor modified statement:\n");
m_compiler->gtDispStmt(stmt);
}
printf("\n");
}
#endif // DEBUG
}
// Morph promoted struct fields and count local occurrences.
//
// Also create and push the value produced by the visited node. This is done here
// rather than in PostOrderVisit because it makes it easy to handle nodes with an
// arbitrary number of operands - just pop values until the value corresponding
// to the visited node is encountered.
fgWalkResult PreOrderVisit(GenTree** use, GenTree* user)
{
GenTree* const node = *use;
if (node->OperIs(GT_FIELD))
{
MorphStructField(node, user);
}
else if (node->OperIs(GT_LCL_FLD))
{
MorphLocalField(node, user);
}
if (node->OperIsLocal())
{
unsigned const lclNum = node->AsLclVarCommon()->GetLclNum();
LclVarDsc* const varDsc = m_compiler->lvaGetDesc(lclNum);
UpdateEarlyRefCount(lclNum);
if (varDsc->lvIsStructField)
{
// Promoted field, increase count for the parent lclVar.
//
assert(!m_compiler->lvaIsImplicitByRefLocal(lclNum));
unsigned parentLclNum = varDsc->lvParentLcl;
UpdateEarlyRefCount(parentLclNum);
}
if (varDsc->lvPromoted)
{
// Promoted struct, increase count for each promoted field.
//
for (unsigned childLclNum = varDsc->lvFieldLclStart;
childLclNum < varDsc->lvFieldLclStart + varDsc->lvFieldCnt; ++childLclNum)
{
UpdateEarlyRefCount(childLclNum);
}
}
}
PushValue(node);
return Compiler::WALK_CONTINUE;
}
// Evaluate a node. Since this is done in postorder, the node's operands have already been
// evaluated and are available on the value stack. The value produced by the visited node
// is left on the top of the evaluation stack.
fgWalkResult PostOrderVisit(GenTree** use, GenTree* user)
{
GenTree* node = *use;
switch (node->OperGet())
{
case GT_LCL_VAR:
assert(TopValue(0).Node() == node);
TopValue(0).Location(node->AsLclVar());
break;
case GT_LCL_VAR_ADDR:
assert(TopValue(0).Node() == node);
TopValue(0).Address(node->AsLclVar());
break;
case GT_LCL_FLD:
assert(TopValue(0).Node() == node);
TopValue(0).Location(node->AsLclFld());
break;
case GT_LCL_FLD_ADDR:
assert(TopValue(0).Node() == node);
TopValue(0).Address(node->AsLclFld());
break;
case GT_ADDR:
assert(TopValue(1).Node() == node);
assert(TopValue(0).Node() == node->gtGetOp1());
TopValue(1).Address(TopValue(0));
PopValue();
break;
case GT_FIELD:
if (node->AsField()->GetFldObj() != nullptr)
{
assert(TopValue(1).Node() == node);
assert(TopValue(0).Node() == node->AsField()->GetFldObj());
if (!TopValue(1).Field(TopValue(0), node->AsField(), m_compiler))
{
// Either the address comes from a location value (e.g. FIELD(IND(...)))
// or the field offset has overflowed.
EscapeValue(TopValue(0), node);
}
PopValue();
}
else
{
assert(TopValue(0).Node() == node);
}
break;
case GT_OBJ:
case GT_BLK:
case GT_IND:
assert(TopValue(1).Node() == node);
assert(TopValue(0).Node() == node->gtGetOp1());
if ((node->gtFlags & GTF_IND_VOLATILE) != 0)
{
// Volatile indirections must not be removed so the address,
// if any, must be escaped.
EscapeValue(TopValue(0), node);
}
else if (!TopValue(1).Indir(TopValue(0)))
{
// If the address comes from another indirection (e.g. IND(IND(...))
// then we need to escape the location.
EscapeLocation(TopValue(0), node);
}
PopValue();
break;
case GT_RETURN:
if (TopValue(0).Node() != node)
{
assert(TopValue(1).Node() == node);
assert(TopValue(0).Node() == node->gtGetOp1());
GenTreeUnOp* ret = node->AsUnOp();
GenTree* retVal = ret->gtGetOp1();
if (retVal->OperIs(GT_LCL_VAR))
{
// TODO-1stClassStructs: this block is a temporary workaround to keep diffs small,
// having `doNotEnreg` affect block init and copy transformations that affect many methods.
// I have a change that introduces more precise and effective solution for that, but it would
// be merged separatly.
GenTreeLclVar* lclVar = retVal->AsLclVar();
unsigned lclNum = lclVar->GetLclNum();
if (!m_compiler->compMethodReturnsMultiRegRegTypeAlternate() &&
!m_compiler->lvaIsImplicitByRefLocal(lclVar->GetLclNum()))
{
LclVarDsc* varDsc = m_compiler->lvaGetDesc(lclNum);
if (varDsc->lvFieldCnt > 1)
{
m_compiler->lvaSetVarDoNotEnregister(
lclNum DEBUGARG(DoNotEnregisterReason::BlockOpRet));
}
}
}
EscapeValue(TopValue(0), node);
PopValue();
}
break;
default:
while (TopValue(0).Node() != node)
{
EscapeValue(TopValue(0), node);
PopValue();
}
break;
}
assert(TopValue(0).Node() == node);
return Compiler::WALK_CONTINUE;
}
private:
void PushValue(GenTree* node)
{
m_valueStack.Push(node);
}
Value& TopValue(unsigned index)
{
return m_valueStack.TopRef(index);
}
void PopValue()
{
assert(TopValue(0).IsConsumed());
m_valueStack.Pop();
}
//------------------------------------------------------------------------
// EscapeValue: Process an escaped value
//
// Arguments:
// val - the escaped address value
// user - the node that uses the escaped value
//
void EscapeValue(Value& val, GenTree* user)
{
if (val.IsLocation())
{
EscapeLocation(val, user);
}
else if (val.IsAddress())
{
EscapeAddress(val, user);
}
else
{
INDEBUG(val.Consume();)
}
}
//------------------------------------------------------------------------
// EscapeAddress: Process an escaped address value
//
// Arguments:
// val - the escaped address value
// user - the node that uses the address value
//
void EscapeAddress(Value& val, GenTree* user)
{
assert(val.IsAddress());
LclVarDsc* varDsc = m_compiler->lvaGetDesc(val.LclNum());
// In general we don't know how an exposed struct field address will be used - it may be used to
// access only that specific field or it may be used to access other fields in the same struct
// by using pointer/ref arithmetic. It seems reasonable to make an exception for the "this" arg
// of calls - it would be highly unusual for a struct member method to attempt to access memory
// beyond "this" instance. And calling struct member methods is common enough that attempting to
// mark the entire struct as address exposed results in CQ regressions.
GenTreeCall* callTree = user->IsCall() ? user->AsCall() : nullptr;
bool isThisArg = (callTree != nullptr) && callTree->gtArgs.HasThisPointer() &&
(val.Node() == callTree->gtArgs.GetThisArg()->GetNode());
bool exposeParentLcl = varDsc->lvIsStructField && !isThisArg;
bool hasHiddenStructArg = false;
if (m_compiler->opts.compJitOptimizeStructHiddenBuffer)
{
if (varTypeIsStruct(varDsc) && varDsc->lvIsTemp)
{
if ((callTree != nullptr) && callTree->gtArgs.HasRetBuffer() &&
(val.Node() == callTree->gtArgs.GetRetBufferArg()->GetNode()))
{
assert(!exposeParentLcl);
m_compiler->lvaSetHiddenBufferStructArg(val.LclNum());
hasHiddenStructArg = true;
callTree->gtCallMoreFlags |= GTF_CALL_M_RETBUFFARG_LCLOPT;
}
}
}
if (!hasHiddenStructArg)
{
m_compiler->lvaSetVarAddrExposed(
exposeParentLcl ? varDsc->lvParentLcl : val.LclNum() DEBUGARG(AddressExposedReason::ESCAPE_ADDRESS));
}
#ifdef TARGET_64BIT
// If the address of a variable is passed in a call and the allocation size of the variable
// is 32 bits we will quirk the size to 64 bits. Some PInvoke signatures incorrectly specify
// a ByRef to an INT32 when they actually write a SIZE_T or INT64. There are cases where
// overwriting these extra 4 bytes corrupts some data (such as a saved register) that leads
// to A/V. Wheras previously the JIT64 codegen did not lead to an A/V.
if (!varDsc->lvIsParam && !varDsc->lvIsStructField && (genActualType(varDsc->TypeGet()) == TYP_INT))
{
// TODO-Cleanup: This should simply check if the user is a call node, not if a call ancestor exists.
if (Compiler::gtHasCallOnStack(&m_ancestors))
{
varDsc->lvQuirkToLong = true;
JITDUMP("Adding a quirk for the storage size of V%02u of type %s\n", val.LclNum(),
varTypeName(varDsc->TypeGet()));
}
}
#endif // TARGET_64BIT
// TODO-ADDR: For now use LCL_VAR_ADDR and LCL_FLD_ADDR only as call arguments and assignment sources.
// Other usages require more changes. For example, a tree like OBJ(ADD(ADDR(LCL_VAR), 4))
// could be changed to OBJ(LCL_FLD_ADDR) but then DefinesLocalAddr does not recognize
// LCL_FLD_ADDR (even though it does recognize LCL_VAR_ADDR).
if (user->OperIs(GT_CALL, GT_ASG) && !hasHiddenStructArg)
{
MorphLocalAddress(val);
}
INDEBUG(val.Consume();)
}
//------------------------------------------------------------------------
// EscapeLocation: Process an escaped location value
//
// Arguments:
// val - the escaped location value
// user - the node that uses the location value
//
// Notes:
// Unlike EscapeAddress, this does not necessarily mark the lclvar associated
// with the value as address exposed. This is needed only if the indirection
// is wider than the lclvar.
//
void EscapeLocation(Value& val, GenTree* user)
{
assert(val.IsLocation());
GenTree* node = val.Node();
if (node->OperIs(GT_LCL_VAR, GT_LCL_FLD))
{
// If the location is accessed directly then we don't need to do anything.
assert(node->AsLclVarCommon()->GetLclNum() == val.LclNum());
}
else
{
// Otherwise it must be accessed through some kind of indirection. Usually this is
// something like IND(ADDR(LCL_VAR)), global morph will change it to GT_LCL_VAR or
// GT_LCL_FLD so the lclvar does not need to be address exposed.
//
// However, it is possible for the indirection to be wider than the lclvar
// (e.g. *(long*)&int32Var) or to have a field offset that pushes the indirection
// past the end of the lclvar memory location. In such cases morph doesn't do
// anything so the lclvar needs to be address exposed.
//
// More importantly, if the lclvar is a promoted struct field then the parent lclvar
// also needs to be address exposed so we get dependent struct promotion. Code like
// *(long*)&int32Var has undefined behavior and it's practically useless but reading,
// say, 2 consecutive Int32 struct fields as Int64 has more practical value.
LclVarDsc* varDsc = m_compiler->lvaGetDesc(val.LclNum());
unsigned indirSize = GetIndirSize(node, user);
bool isWide;
if (indirSize == 0)
{
// If we can't figure out the indirection size then treat it as a wide indirection.
isWide = true;
}
else
{
ClrSafeInt<unsigned> endOffset = ClrSafeInt<unsigned>(val.Offset()) + ClrSafeInt<unsigned>(indirSize);
if (endOffset.IsOverflow())
{
isWide = true;
}
else if (varDsc->TypeGet() == TYP_STRUCT)
{
isWide = (endOffset.Value() > varDsc->lvExactSize);
}
else
{
// For small int types use the real type size, not the stack slot size.
// Morph does manage to transform `*(int*)&byteVar` into just byteVar where
// the LCL_VAR node has type TYP_INT. But such code is simply bogus and
// there's no reason to attempt to optimize it. It makes more sense to
// mark the variable address exposed in such circumstances.
//
// Same for "small" SIMD types - SIMD8/12 have 8/12 bytes, even if the
// stack location may have 16 bytes.
//
// For TYP_BLK variables the type size is 0 so they're always address
// exposed.
isWide = (endOffset.Value() > genTypeSize(varDsc->TypeGet()));
}
}
if (isWide)
{
m_compiler->lvaSetVarAddrExposed(varDsc->lvIsStructField
? varDsc->lvParentLcl
: val.LclNum() DEBUGARG(AddressExposedReason::WIDE_INDIR));
}
else
{
MorphLocalIndir(val, user);
}
}
INDEBUG(val.Consume();)
}
//------------------------------------------------------------------------
// GetIndirSize: Return the size (in bytes) of an indirection node.
//
// Arguments:
// indir - the indirection node
// user - the node that uses the indirection
//
// Notes:
// This returns 0 for indirection of unknown size. GT_IND nodes that have type
// TYP_STRUCT are expected to only appears on the RHS of an assignment, in which
// case the LHS size will be used instead. Otherwise 0 is returned as well.
//
unsigned GetIndirSize(GenTree* indir, GenTree* user)
{
assert(indir->OperIs(GT_IND, GT_OBJ, GT_BLK, GT_FIELD));
if (indir->TypeGet() != TYP_STRUCT)
{
return genTypeSize(indir->TypeGet());
}
// A struct indir that is the RHS of an assignment needs special casing:
// - It can be a GT_IND of type TYP_STRUCT, in which case the size is given by the LHS.
// - It can be a GT_OBJ that has a correct size, but different than the size of the LHS.
// The LHS size takes precedence.
// Just take the LHS size in all cases.
if (user != nullptr && user->OperIs(GT_ASG) && (indir == user->gtGetOp2()))
{
indir = user->gtGetOp1();
if (indir->TypeGet() != TYP_STRUCT)
{
return genTypeSize(indir->TypeGet());
}
// The LHS may be a LCL_VAR/LCL_FLD, these are not indirections so we need to handle them here.
// It can also be a GT_INDEX, this is an indirection but it never applies to lclvar addresses
// so it needs to be handled here as well.
switch (indir->OperGet())
{
case GT_LCL_VAR:
return m_compiler->lvaGetDesc(indir->AsLclVar())->lvExactSize;
case GT_LCL_FLD:
return genTypeSize(indir->TypeGet());
case GT_INDEX:
return indir->AsIndex()->gtIndElemSize;
default:
break;
}
}
switch (indir->OperGet())
{
case GT_FIELD:
return m_compiler->info.compCompHnd->getClassSize(
m_compiler->info.compCompHnd->getFieldClass(indir->AsField()->gtFldHnd));
case GT_BLK:
case GT_OBJ:
return indir->AsBlk()->GetLayout()->GetSize();
default:
assert(indir->OperIs(GT_IND));
return 0;
}
}
//------------------------------------------------------------------------
// MorphLocalAddress: Change a tree that represents a local variable address
// to a single LCL_VAR_ADDR or LCL_FLD_ADDR node.
//
// Arguments:
// val - a value that represents the local address
//
void MorphLocalAddress(const Value& val)
{
assert(val.IsAddress());
assert(val.Node()->TypeIs(TYP_BYREF, TYP_I_IMPL));
assert(m_compiler->lvaVarAddrExposed(val.LclNum()));
LclVarDsc* varDsc = m_compiler->lvaGetDesc(val.LclNum());
if (varDsc->lvPromoted || varDsc->lvIsStructField || m_compiler->lvaIsImplicitByRefLocal(val.LclNum()))
{
// TODO-ADDR: For now we ignore promoted and "implicit by ref" variables,
// they require additional changes in subsequent phases.
return;
}
#ifdef TARGET_X86
if (m_compiler->info.compIsVarArgs && varDsc->lvIsParam && !varDsc->lvIsRegArg)
{
// TODO-ADDR: For now we ignore all stack parameters of varargs methods,
// fgMorphStackArgForVarArgs does not handle LCL_VAR|FLD_ADDR nodes.
return;
}
#endif
GenTree* addr = val.Node();
if (val.Offset() > UINT16_MAX)
{
// The offset is too large to store in a LCL_FLD_ADDR node,
// use ADD(LCL_VAR_ADDR, offset) instead.
addr->ChangeOper(GT_ADD);
addr->AsOp()->gtOp1 = m_compiler->gtNewLclVarAddrNode(val.LclNum());
addr->AsOp()->gtOp2 = m_compiler->gtNewIconNode(val.Offset(), val.FieldSeq());
}
else if ((val.Offset() != 0) || (val.FieldSeq() != nullptr))
{
addr->ChangeOper(GT_LCL_FLD_ADDR);
addr->AsLclFld()->SetLclNum(val.LclNum());
addr->AsLclFld()->SetLclOffs(val.Offset());
addr->AsLclFld()->SetFieldSeq(val.FieldSeq());
}
else
{
addr->ChangeOper(GT_LCL_VAR_ADDR);
addr->AsLclVar()->SetLclNum(val.LclNum());
}
// Local address nodes never have side effects (nor any other flags, at least at this point).
addr->gtFlags = GTF_EMPTY;
INDEBUG(m_stmtModified = true;)
}
//------------------------------------------------------------------------
// MorphLocalIndir: Change a tree that represents an indirect access to a struct
// variable to a single LCL_VAR or LCL_FLD node.
//
// Arguments:
// val - a value that represents the local indirection
// user - the indirection's user node
//
void MorphLocalIndir(const Value& val, GenTree* user)
{
assert(val.IsLocation());
GenTree* indir = val.Node();
assert(indir->OperIs(GT_IND, GT_OBJ, GT_BLK, GT_FIELD));
if (val.Offset() > UINT16_MAX)
{
// TODO-ADDR: We can't use LCL_FLD because the offset is too large but we should
// transform the tree into IND(ADD(LCL_VAR_ADDR, offset)) instead of leaving this
// this to fgMorphField.
return;
}
if (indir->OperIs(GT_FIELD) ? indir->AsField()->IsVolatile() : indir->AsIndir()->IsVolatile())
{
// TODO-ADDR: We shouldn't remove the indir because it's volatile but we should
// transform the tree into IND(LCL_VAR|FLD_ADDR) instead of leaving this to
// fgMorphField.
return;
}
LclVarDsc* varDsc = m_compiler->lvaGetDesc(val.LclNum());
if (varDsc->TypeGet() != TYP_STRUCT)
{
// TODO-ADDR: Skip integral/floating point variables for now, they're more
// complicated to transform. We can always turn an indirect access of such
// a variable into a LCL_FLD but that blocks enregistration so we need to
// detect those case where we can use LCL_VAR instead, perhaps in conjuction
// with CAST and/or BITCAST.
// Also skip SIMD variables for now, fgMorphFieldAssignToSimdSetElement and
// others need to be updated to recognize LCL_FLDs.
return;
}
if (varDsc->lvPromoted || varDsc->lvIsStructField || m_compiler->lvaIsImplicitByRefLocal(val.LclNum()))