-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
morphblock.cpp
1559 lines (1403 loc) · 58.3 KB
/
morphblock.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 MorphInitBlockHelper
{
public:
static GenTree* MorphInitBlock(Compiler* comp, GenTree* tree);
protected:
MorphInitBlockHelper(Compiler* comp, GenTree* asg, bool initBlock);
GenTree* Morph();
void PrepareDst();
virtual void PrepareSrc();
virtual void TrySpecialCases();
virtual void MorphStructCases();
virtual const char* GetHelperName() const
{
return "MorphInitBlock";
}
static GenTree* MorphBlock(Compiler* comp, GenTree* tree, bool isDest);
static GenTree* MorphCommaBlock(Compiler* comp, GenTreeOp* firstComma);
static GenTreeBlk* MorphDynamicBlock(Compiler* comp, GenTreeDynBlk* dynBlock);
protected:
Compiler* m_comp;
bool m_initBlock;
GenTreeOp* m_asg = nullptr;
GenTree* m_dst = nullptr;
GenTree* m_src = nullptr;
unsigned m_blockSize = 0;
bool m_blockSizeIsConst = false;
unsigned m_dstLclNum = BAD_VAR_NUM;
GenTreeLclVarCommon* m_dstLclNode = nullptr;
LclVarDsc* m_dstVarDsc = nullptr;
FieldSeqNode* m_dstFldSeq = nullptr;
unsigned m_dstLclOffset = 0;
bool m_dstUseLclFld = false;
bool m_dstSingleLclVarAsg = false;
GenTree* m_dstAddr = nullptr;
ssize_t m_dstAddOff = 0;
#if defined(DEBUG)
bool m_isLateArg = false;
#endif // DEBUG
enum class BlockTransformation
{
Undefined,
FieldByField,
OneAsgBlock,
StructBlock,
SkipCallSrc,
Nop
};
BlockTransformation m_transformationDecision = BlockTransformation::Undefined;
GenTree* m_result = nullptr;
};
//------------------------------------------------------------------------
// MorphInitBlock: Morph a block initialization assignment tree.
//
// Arguments:
// comp - a compiler instance;
// tree - A GT_ASG tree that performs block initialization.
//
// Return Value:
// A possibly modified tree to perfom the initializetion.
//
// static
GenTree* MorphInitBlockHelper::MorphInitBlock(Compiler* comp, GenTree* tree)
{
const bool initBlock = true;
MorphInitBlockHelper helper(comp, tree, initBlock);
return helper.Morph();
}
//------------------------------------------------------------------------
// MorphInitBlockHelper: helper's constructor.
//
// Arguments:
// comp - a compiler instance;
// initBlock - true if this is init block op, false if it is a copy block;
// asg - GT_ASG node to morph.
//
// Notes:
// Most class members are initialized via in-class member initializers.
//
MorphInitBlockHelper::MorphInitBlockHelper(Compiler* comp, GenTree* asg, bool initBlock = true)
: m_comp(comp), m_initBlock(initBlock)
{
assert(asg->OperIs(GT_ASG));
#if defined(DEBUG)
if (m_initBlock)
{
assert(asg->OperIsInitBlkOp());
}
else
{
assert(asg->OperIsCopyBlkOp());
}
#endif // DEBUG
m_asg = asg->AsOp();
}
//------------------------------------------------------------------------
// Morph: transform the asg to a possible better form and changes its children
// to an appropriate form for later phases, for example, adds SIMD_INIT nodes
// or sets lvDoNotEnregister on locals.
//
// Return Value:
// A possibly modified tree to perfom the block operation.
//
// Notes:
// It is used for both init and copy block.
//
GenTree* MorphInitBlockHelper::Morph()
{
JITDUMP("%s:\n", GetHelperName());
PrepareDst();
PrepareSrc();
INDEBUG(m_isLateArg = (m_asg->gtFlags & GTF_LATE_ARG) != 0);
TrySpecialCases();
if (m_transformationDecision == BlockTransformation::Undefined)
{
GenTree* oneAsgTree = nullptr;
if (m_dst != m_dstLclNode)
{
oneAsgTree = m_comp->fgMorphOneAsgBlockOp(m_asg);
}
if (oneAsgTree != nullptr)
{
assert((m_asg == oneAsgTree) && "fgMorphOneAsgBlock must return the incoming tree.");
m_transformationDecision = BlockTransformation::OneAsgBlock;
m_result = oneAsgTree;
}
else
{
MorphStructCases();
}
}
assert(m_transformationDecision != BlockTransformation::Undefined);
assert(m_result != nullptr);
if (m_result != m_asg)
{
const bool isLateArg = ((m_asg->gtFlags & GTF_LATE_ARG) != 0);
assert(m_isLateArg == isLateArg);
if (isLateArg)
{
assert(!m_initBlock && "do not expect a block init as a late arg.");
m_result->gtFlags |= GTF_LATE_ARG;
}
}
#ifdef DEBUG
if (m_result != m_asg)
{
m_result->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED;
}
if (m_comp->verbose)
{
printf("%s (after):\n", GetHelperName());
m_comp->gtDispTree(m_result);
}
#endif // DEBUG
return m_result;
}
//------------------------------------------------------------------------
// PrepareDst: Transform the asg destination to an appropriate form and initialize member fields
// with information about it.
//
// Notes:
// When assertion propagation is enabled this method kills assertions about the dst local,
// so the correctness depends on `IsLocalAddrExpr` recognizing all patterns.
//
void MorphInitBlockHelper::PrepareDst()
{
GenTree* origDst = m_asg->gtGetOp1();
m_dst = MorphBlock(m_comp, origDst, true);
if (m_dst != origDst)
{
m_asg->gtOp1 = m_dst;
}
if (m_asg->TypeGet() != m_dst->TypeGet())
{
assert(!m_initBlock && "the asg type should be final for an init block.");
JITDUMP("changing type of asignment from %-6s to %-6s\n", varTypeName(m_asg->TypeGet()),
varTypeName(m_dst->TypeGet()));
m_asg->ChangeType(m_dst->TypeGet());
}
if (m_dst->IsLocal())
{
m_dstLclNode = m_dst->AsLclVarCommon();
m_dstVarDsc = m_comp->lvaGetDesc(m_dstLclNode);
m_blockSizeIsConst = true;
if (m_dst->OperIs(GT_LCL_VAR))
{
if (m_dstVarDsc->TypeGet() == TYP_STRUCT)
{
#ifdef DEBUG
const bool isSizeMistmatch =
(m_dstVarDsc->lvExactSize != m_comp->info.compCompHnd->getClassSize(m_dstVarDsc->GetStructHnd()));
const bool isStackAllocCandidate =
m_comp->compObjectStackAllocation() && !m_dstVarDsc->GetLayout()->IsValueClass();
// There were cases where for temps lvExactSize did not correspond to the struct size
// so we were using `getClassSize` result here, however, now this cases are gone and the only
// scenario when `getClassSize` != `lvExactSize` it is a class object optimized to be on stack
assert(!isSizeMistmatch || isStackAllocCandidate);
#endif // DEBUG
m_blockSize = m_dstVarDsc->lvExactSize;
}
else
{
m_blockSize = genTypeSize(m_dstVarDsc);
}
}
else
{
assert(m_dst->OperIs(GT_LCL_FLD) && !m_dst->TypeIs(TYP_STRUCT));
GenTreeLclFld* destFld = m_dst->AsLclFld();
m_blockSize = genTypeSize(destFld->TypeGet());
m_dstFldSeq = destFld->GetFieldSeq();
}
}
else
{
assert(m_dst == m_dst->gtEffectiveVal() && "the commas were skipped in MorphBlock");
assert(m_dst->OperIs(GT_IND, GT_BLK, GT_OBJ, GT_DYN_BLK));
GenTree* dstAddr = m_dst->AsIndir()->Addr();
if (m_dst->OperGet() == GT_IND)
{
assert(m_dst->TypeGet() != TYP_STRUCT);
m_blockSize = genTypeSize(m_dst);
m_blockSizeIsConst = true;
}
else
{
assert(m_dst->OperIsBlk());
GenTreeBlk* blk = m_dst->AsBlk();
m_blockSize = blk->Size();
m_blockSizeIsConst = !blk->OperIs(GT_DYN_BLK);
}
noway_assert(dstAddr->TypeIs(TYP_BYREF, TYP_I_IMPL));
if (dstAddr->IsLocalAddrExpr(m_comp, &m_dstLclNode, &m_dstFldSeq, &m_dstAddOff))
{
// Don't expect `IsLocalAddrExpr` to pass `INDEX_ADDR`.
assert((m_dst->gtFlags & GTF_IND_ARR_INDEX) == 0);
// Note that lclNode can be a field, like `BLK<4> struct(ADD(ADDR(LCL_FLD int), CNST_INT))`.
m_dstVarDsc = m_comp->lvaGetDesc(m_dstLclNode);
}
}
if (m_dstLclNode != nullptr)
{
m_dstLclNum = m_dstLclNode->GetLclNum();
m_dstLclOffset = m_dstLclNode->GetLclOffs();
#if LOCAL_ASSERTION_PROP
// Kill everything about m_dstLclNum (and its field locals)
if (m_comp->optLocalAssertionProp && (m_comp->optAssertionCount > 0))
{
m_comp->fgKillDependentAssertions(m_dstLclNum DEBUGARG(m_asg));
}
#endif // LOCAL_ASSERTION_PROP
}
#if defined(DEBUG)
if (m_comp->verbose)
{
printf("PrepareDst for [%06u] ", m_comp->dspTreeID(origDst));
if (m_dstLclNode != nullptr)
{
printf("have found a local var V%02u.\n", m_dstLclNum);
}
else
{
printf("have not found a local var.\n");
}
}
#endif // DEBUG
}
//------------------------------------------------------------------------
// PrepareSrc: Transform the asg src to an appropriate form and initialize member fields
// with information about it.
//
void MorphInitBlockHelper::PrepareSrc()
{
m_src = m_asg->gtGetOp2();
}
//------------------------------------------------------------------------
// TrySpecialCases: check special cases that require special transformations.
// We don't have any for for init block.
//
void MorphInitBlockHelper::TrySpecialCases()
{
return;
}
//------------------------------------------------------------------------
// MorphStructCases: transforms the asg as field by field init or keeps it as a block init
// but sets appropriate flags for the involved lclVars.
//
// Assumptions:
// we have already checked that it is not a special case and can't be transformed as OneAsgBlock.
//
void MorphInitBlockHelper::MorphStructCases()
{
GenTree* initVal = m_src->OperIsInitVal() ? m_src->gtGetOp1() : m_src;
if (m_dstLclNum != BAD_VAR_NUM)
{
// If we have already determined that a promoted TYP_STRUCT lclVar will not be enregistered,
// we are better off doing a block init.
bool tryFieldByField = false;
if (m_dstVarDsc->lvPromoted)
{
assert(m_dstLclNode->OperIs(GT_LCL_VAR)); // Don't expect a promoted LCL_VAR with a field reference.
if (m_dstVarDsc->lvDoNotEnregister)
{
tryFieldByField = false;
}
else
{
tryFieldByField = true;
}
}
if (tryFieldByField)
{
GenTreeLclVar* dstLclVar = m_dstLclNode->AsLclVar();
GenTree* newTree = m_comp->fgMorphPromoteLocalInitBlock(dstLclVar, initVal, m_blockSize);
if (newTree != nullptr)
{
m_transformationDecision = BlockTransformation::FieldByField;
m_result = newTree;
}
}
}
if (m_transformationDecision == BlockTransformation::Undefined)
{
// For an InitBlock we always require a block operand.
m_dst = m_comp->fgMorphBlockOperand(m_dst, m_dst->TypeGet(), m_blockSize, true /*isBlkReqd*/);
m_transformationDecision = BlockTransformation::StructBlock;
m_dst->gtFlags |= GTF_DONT_CSE;
m_result = m_asg;
m_result->AsOp()->gtOp1 = m_dst;
m_result->gtFlags |= (m_dst->gtFlags & GTF_ALL_EFFECT);
#if FEATURE_SIMD
if (varTypeIsSIMD(m_asg) && (m_dst == m_dstLclNode))
{
// For a SIMD local init we need to call SIMDIntrinsic init.
// We need this block becuase morph does not create SIMD init for promoted lclVars.
assert(m_src->IsIntegralConst(0) || m_src->IsFPZero());
assert(m_dstVarDsc != nullptr);
const var_types asgType = m_asg->TypeGet();
CorInfoType simdBaseJitType = m_dstVarDsc->GetSimdBaseJitType();
m_src = m_comp->gtNewSIMDNode(asgType, m_src, SIMDIntrinsicInit, simdBaseJitType, m_blockSize);
m_result->AsOp()->gtOp2 = m_src;
}
#endif // FEATURE_SIMD
if (m_dstVarDsc != nullptr)
{
if (m_dst != m_dstLclNode)
{
// If we access the dst as a whole but not directly, for example, with OBJ(ADDR(LCL_VAR))
// then set doNotEnreg.
// TODO-1stClassStructs: remove it when we can represent narowing struct cast
// without taking address of the lcl.
m_comp->lvaSetVarDoNotEnregister(m_dstLclNum DEBUGARG(DoNotEnregisterReason::CastTakesAddr));
}
else if (m_dstVarDsc->lvPromoted)
{
m_comp->lvaSetVarDoNotEnregister(m_dstLclNum DEBUGARG(DoNotEnregisterReason::BlockOp));
}
}
}
}
//------------------------------------------------------------------------
// MorphBlock: Morph a block node preparatory to morphing a block assignment.
//
// Arguments:
// comp - a compiler instance;
// tree - a struct type node;
// isDest - true if this is the destination of an assignment;
//
// Return Value:
// Returns the possibly-morphed node. The caller is responsible for updating
// the parent of this node.
//
// static
GenTree* MorphInitBlockHelper::MorphBlock(Compiler* comp, GenTree* tree, bool isDest)
{
JITDUMP("MorphBlock for %s tree, before:\n", (isDest ? "dst" : "src"));
DISPTREE(tree);
// Src can be a primitive type.
assert(!isDest || varTypeIsStruct(tree));
GenTree* handleTree = nullptr;
GenTree* addr = nullptr;
if (tree->OperIs(GT_COMMA))
{
// TODO-Cleanup: this block is not needed for not struct nodes, but
// fgMorphOneAsgBlockOp works wrong without this transformation.
tree = MorphCommaBlock(comp, tree->AsOp());
}
if (!tree->OperIsBlk())
{
JITDUMP("MorphBlock after:\n");
DISPTREE(tree);
return tree;
}
GenTreeBlk* blkNode = tree->AsBlk();
if (blkNode->OperIs(GT_DYN_BLK))
{
blkNode = MorphDynamicBlock(comp, blkNode->AsDynBlk());
if (blkNode->OperIs(GT_DYN_BLK))
{
JITDUMP("MorphBlock after:\n");
DISPTREE(blkNode);
return blkNode;
}
}
GenTree* blkAddr = blkNode->Addr();
assert(blkAddr != nullptr);
assert(blkAddr->TypeIs(TYP_I_IMPL, TYP_BYREF, TYP_REF));
// GT_ADDR, GT_LCL_VAR/FLD, GT_ADD, GT_COMMA, GT_CALL, GT_CNST_INT, GT_LCL_VAR/FLD_ADDR
JITDUMP("MorphBlock after:\n");
DISPTREE(tree);
return tree;
}
//------------------------------------------------------------------------
// MorphCommaBlock: transform COMMA<struct>(X) as OBJ<STRUCT>(COMMA byref(ADDR(X)).
//
// Notes:
// In order to CSE and value number array index expressions and bounds checks,
// the commas in which they are contained need to match.
// The pattern is that the COMMA should be the address expression.
// Therefore, we insert a GT_ADDR just above the node, and wrap it in an obj or ind.
// TODO-1stClassStructs: Consider whether this can be improved.
// Example:
// before: [3] comma struct <- [2] comma struct <- [1] LCL_VAR struct
// after: [5] obj <- [3] comma byref <- [2] comma byref <- [4] addr byref <- [1] LCL_VAR struct
//
// static
GenTree* MorphInitBlockHelper::MorphCommaBlock(Compiler* comp, GenTreeOp* firstComma)
{
assert(firstComma->OperIs(GT_COMMA));
Compiler::GenTreePtrStack commas(comp->getAllocator(CMK_ArrayStack));
for (GenTree* currComma = firstComma; currComma != nullptr && currComma->OperIs(GT_COMMA);
currComma = currComma->gtGetOp2())
{
commas.Push(currComma);
}
GenTree* lastComma = commas.Top();
GenTree* effectiveVal = lastComma->gtGetOp2();
assert(effectiveVal == firstComma->gtEffectiveVal());
GenTree* effectiveValAddr = comp->gtNewOperNode(GT_ADDR, TYP_BYREF, effectiveVal);
INDEBUG(effectiveValAddr->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED);
lastComma->AsOp()->gtOp2 = effectiveValAddr;
while (!commas.Empty())
{
GenTree* comma = commas.Pop();
comma->gtType = TYP_BYREF;
comp->gtUpdateNodeSideEffects(comma);
}
const var_types blockType = effectiveVal->TypeGet();
GenTree* addr = firstComma;
GenTree* res;
if (blockType == TYP_STRUCT)
{
CORINFO_CLASS_HANDLE structHnd = comp->gtGetStructHandleIfPresent(effectiveVal);
if (structHnd == NO_CLASS_HANDLE)
{
// TODO-1stClassStructs: get rid of all such cases.
res = comp->gtNewIndir(blockType, addr);
}
else
{
res = comp->gtNewObjNode(structHnd, addr);
comp->gtSetObjGcInfo(res->AsObj());
}
}
else
{
res = comp->gtNewIndir(blockType, addr);
}
comp->gtUpdateNodeSideEffects(res);
INDEBUG(res->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED);
return res;
}
//------------------------------------------------------------------------
// MorphDynamicBlock: tries to transform a dynamic block as a const block.
//
// static
GenTreeBlk* MorphInitBlockHelper::MorphDynamicBlock(Compiler* comp, GenTreeDynBlk* dynBlock)
{
if (dynBlock->gtDynamicSize->IsCnsIntOrI())
{
GenTreeIntCon* dynSize = dynBlock->gtDynamicSize->AsIntCon();
assert(dynSize->FitsInI32());
unsigned size = static_cast<unsigned>(dynSize->IconValue());
// A GT_BLK with size of zero is not supported,
// so if we encounter such a thing we just leave it as a GT_DYN_BLK
if (size != 0)
{
dynBlock->gtDynamicSize = nullptr;
GenTreeBlk* blkNode = dynBlock;
blkNode->ChangeOper(GT_BLK);
blkNode->SetLayout(comp->typGetBlkLayout(size));
JITDUMP("MorphDynamicBlock: DYN_BLK was morphed into BLK:\n");
return blkNode;
}
else
{
JITDUMP("MorphDynamicBlock: DYN_BLK with zero size can't be morphed:\n");
return dynBlock;
}
}
else
{
JITDUMP("MorphDynamicBlock: DYN_BLK with non-const size can't be morphed:\n");
return dynBlock;
}
}
class MorphCopyBlockHelper : public MorphInitBlockHelper
{
public:
static GenTree* MorphCopyBlock(Compiler* comp, GenTree* tree);
protected:
MorphCopyBlockHelper(Compiler* comp, GenTree* asg);
void PrepareSrc() override;
void TrySpecialCases() override;
void MorphStructCases() override;
GenTree* CopyFieldByField();
const char* GetHelperName() const override
{
return "MorphCopyBlock";
}
protected:
unsigned m_srcLclNum = BAD_VAR_NUM;
LclVarDsc* m_srcVarDsc = nullptr;
GenTreeLclVarCommon* m_srcLclNode = nullptr;
FieldSeqNode* m_srcFldSeq = nullptr;
bool m_srcUseLclFld = false;
unsigned m_srcLclOffset = 0;
bool m_srcSingleLclVarAsg = false;
GenTree* m_srcAddr = nullptr;
ssize_t m_srcAddOff = 0;
bool m_dstDoFldAsg = false;
bool m_srcDoFldAsg = false;
};
//------------------------------------------------------------------------
// MorphCopyBlock: Morph a block copy assignment tree.
//
// Arguments:
// comp - a compiler instance;
// tree - A GT_ASG tree that performs block copy.
//
// Return Value:
// A possibly modified tree to perfom the copy.
//
// static
GenTree* MorphCopyBlockHelper::MorphCopyBlock(Compiler* comp, GenTree* tree)
{
MorphCopyBlockHelper helper(comp, tree);
return helper.Morph();
}
//------------------------------------------------------------------------
// MorphCopyBlockHelper: helper's constructor.
//
// Arguments:
// comp - a compiler instance;
// asg - GT_ASG node to morph.
//
// Notes:
// Most class members are initialized via in-class member initializers.
//
MorphCopyBlockHelper::MorphCopyBlockHelper(Compiler* comp, GenTree* asg) : MorphInitBlockHelper(comp, asg, false)
{
}
//------------------------------------------------------------------------
// PrepareSrc: Transform the asg src to an appropriate form and initialize member fields
// with information about it.
//
void MorphCopyBlockHelper::PrepareSrc()
{
GenTree* origSrc = m_asg->gtGetOp2();
m_src = MorphBlock(m_comp, origSrc, false);
if (m_src != origSrc)
{
m_asg->gtOp2 = m_src;
}
if (m_src->IsLocal())
{
m_srcLclNode = m_src->AsLclVarCommon();
m_srcLclNum = m_srcLclNode->GetLclNum();
if (m_src->OperGet() == GT_LCL_FLD)
{
m_srcFldSeq = m_src->AsLclFld()->GetFieldSeq();
}
}
else if (m_src->OperIsIndir())
{
if (m_src->AsOp()->gtOp1->IsLocalAddrExpr(m_comp, &m_srcLclNode, &m_srcFldSeq, &m_srcAddOff))
{
m_srcLclNum = m_srcLclNode->GetLclNum();
}
else
{
m_srcAddr = m_src->AsOp()->gtOp1;
}
}
if (m_srcLclNum != BAD_VAR_NUM)
{
m_srcLclOffset = m_srcLclNode->GetLclOffs();
m_srcVarDsc = m_comp->lvaGetDesc(m_srcLclNum);
}
}
// TrySpecialCases: check special cases that require special transformations.
// The current special cases include asignments with calls in RHS.
//
// Notes:
// It could change multiReg flags or change m_dst node.
//
void MorphCopyBlockHelper::TrySpecialCases()
{
#if FEATURE_MULTIREG_RET
// If this is a multi-reg return, we will not do any morphing of this node.
if (m_src->IsMultiRegCall())
{
assert(m_dst->OperGet() == GT_LCL_VAR);
JITDUMP("Not morphing a multireg call return\n");
m_transformationDecision = BlockTransformation::SkipCallSrc;
m_result = m_asg;
}
else if (m_dst->IsMultiRegLclVar() && !m_src->IsMultiRegNode())
{
m_dst->AsLclVar()->ClearMultiReg();
}
#endif // FEATURE_MULTIREG_RET
if (m_transformationDecision == BlockTransformation::Undefined)
{
if (m_src->IsCall())
{
if (m_dst->OperIs(GT_OBJ))
{
GenTreeLclVar* lclVar = m_comp->fgMorphTryFoldObjAsLclVar(m_dst->AsObj());
if (lclVar != nullptr)
{
m_dst = lclVar;
m_asg->gtOp1 = lclVar;
}
}
if (m_dst->OperIs(GT_LCL_VAR))
{
LclVarDsc* varDsc = m_comp->lvaGetDesc(m_dst->AsLclVar());
if (varTypeIsStruct(varDsc) && varDsc->CanBeReplacedWithItsField(m_comp))
{
m_dst->gtFlags |= GTF_DONT_CSE;
JITDUMP("Not morphing a single reg call return\n");
m_transformationDecision = BlockTransformation::SkipCallSrc;
m_result = m_asg;
}
}
}
}
}
//------------------------------------------------------------------------
// MorphStructCases: transforms the asg as field by field copy or keeps it as a block init
// but sets appropriate flags for the involved lclVars.
//
// Assumptions:
// we have already checked that it is not a special case and can't be transformed as OneAsgBlock.
//
void MorphCopyBlockHelper::MorphStructCases()
{
JITDUMP("block assignment to morph:\n");
DISPTREE(m_asg);
if (m_dstVarDsc != nullptr)
{
if (m_dstVarDsc->lvPromoted && m_blockSizeIsConst)
{
noway_assert(varTypeIsStruct(m_dstVarDsc));
noway_assert(!m_comp->opts.MinOpts());
if (m_blockSize == m_dstVarDsc->lvExactSize)
{
JITDUMP(" (m_dstDoFldAsg=true)");
// We may decide later that a copyblk is required when this struct has holes
m_dstDoFldAsg = true;
}
else
{
JITDUMP(" with mismatched dest size");
}
}
}
if (m_srcVarDsc != nullptr)
{
if (m_srcVarDsc->lvPromoted && m_blockSizeIsConst)
{
noway_assert(varTypeIsStruct(m_srcVarDsc));
noway_assert(!m_comp->opts.MinOpts());
if (m_blockSize == m_srcVarDsc->lvExactSize)
{
JITDUMP(" (m_srcDoFldAsg=true)");
// We may decide later that a copyblk is required when this struct has holes
m_srcDoFldAsg = true;
}
else
{
JITDUMP(" with mismatched src size");
}
}
}
// Check to see if we are doing a copy to/from the same local block.
// If so, morph it to a nop.
if ((m_dstVarDsc != nullptr) && (m_srcVarDsc == m_dstVarDsc) && (m_dstFldSeq == m_srcFldSeq) &&
m_dstFldSeq != FieldSeqStore::NotAField())
{
JITDUMP("Self-copy; replaced with a NOP.\n");
m_transformationDecision = BlockTransformation::Nop;
GenTree* nop = m_comp->gtNewNothingNode();
m_result = nop;
return;
}
// Check to see if we are required to do a copy block because the struct contains holes
// and either the src or dest is externally visible
//
bool requiresCopyBlock = false;
// If either src or dest is a reg-sized non-field-addressed struct, keep the copyBlock.
if ((m_dstVarDsc != nullptr && m_dstVarDsc->lvRegStruct) || (m_srcVarDsc != nullptr && m_srcVarDsc->lvRegStruct))
{
requiresCopyBlock = true;
}
// Can we use field by field assignment for the dest?
if (m_dstDoFldAsg && m_dstVarDsc->lvCustomLayout && m_dstVarDsc->lvContainsHoles)
{
JITDUMP(" dest contains custom layout and contains holes");
// C++ style CopyBlock with holes
requiresCopyBlock = true;
}
// Can we use field by field assignment for the src?
if (m_srcDoFldAsg && m_srcVarDsc->lvCustomLayout && m_srcVarDsc->lvContainsHoles)
{
JITDUMP(" src contains custom layout and contains holes");
// C++ style CopyBlock with holes
requiresCopyBlock = true;
}
#if defined(TARGET_ARM)
if ((m_src->OperIsIndir()) && (m_src->gtFlags & GTF_IND_UNALIGNED))
{
JITDUMP(" src is unaligned");
requiresCopyBlock = true;
}
if (m_asg->gtFlags & GTF_BLK_UNALIGNED)
{
JITDUMP(" m_asg is unaligned");
requiresCopyBlock = true;
}
#endif // TARGET_ARM
// Don't use field by field assignment if the src is a call,
// lowering will handle it without spilling the call result into memory
// to access the individual fields.
//
if (m_src->OperGet() == GT_CALL)
{
JITDUMP(" src is a call");
requiresCopyBlock = true;
}
// If we passed the above checks, then we will check these two
if (!requiresCopyBlock)
{
// It is not always profitable to do field by field init for structs that are allocated to memory.
// A struct with 8 bool fields will require 8 moves instead of one if we do this transformation.
// A simple heuristic when field by field copy is prefered:
// - if fields can be enregistered;
// - if the struct has GCPtrs (block copy would be done via helper that is expensive);
// - if the struct has only one field.
bool dstFldIsProfitable =
((m_dstVarDsc != nullptr) &&
(!m_dstVarDsc->lvDoNotEnregister || m_dstVarDsc->HasGCPtr() || (m_dstVarDsc->lvFieldCnt == 1)));
bool srcFldIsProfitable =
((m_srcVarDsc != nullptr) &&
(!m_srcVarDsc->lvDoNotEnregister || m_srcVarDsc->HasGCPtr() || (m_srcVarDsc->lvFieldCnt == 1)));
// Are both dest and src promoted structs?
if (m_dstDoFldAsg && m_srcDoFldAsg && (dstFldIsProfitable || srcFldIsProfitable))
{
// Both structs should be of the same type, or have the same number of fields of the same type.
// If not we will use a copy block.
bool misMatchedTypes = false;
if (m_dstVarDsc->GetStructHnd() != m_srcVarDsc->GetStructHnd())
{
if (m_dstVarDsc->lvFieldCnt != m_srcVarDsc->lvFieldCnt)
{
misMatchedTypes = true;
}
else
{
for (int i = 0; i < m_dstVarDsc->lvFieldCnt; i++)
{
LclVarDsc* destFieldVarDsc = m_comp->lvaGetDesc(m_dstVarDsc->lvFieldLclStart + i);
LclVarDsc* srcFieldVarDsc = m_comp->lvaGetDesc(m_srcVarDsc->lvFieldLclStart + i);
if ((destFieldVarDsc->lvType != srcFieldVarDsc->lvType) ||
(destFieldVarDsc->lvFldOffset != srcFieldVarDsc->lvFldOffset))
{
misMatchedTypes = true;
break;
}
}
}
if (misMatchedTypes)
{
requiresCopyBlock = true; // Mismatched types, leave as a CopyBlock
JITDUMP(" with mismatched types");
}
}
}
else if (m_dstDoFldAsg && dstFldIsProfitable)
{
// Match the following kinds of trees:
// fgMorphTree BB01, stmt 9 (before)
// [000052] ------------ const int 8
// [000053] -A--G------- copyBlk void
// [000051] ------------ addr byref
// [000050] ------------ lclVar long V07 loc5
// [000054] --------R--- <list> void
// [000049] ------------ addr byref
// [000048] ------------ lclVar struct(P) V06 loc4
// long V06.h (offs=0x00) -> V17 tmp9
// Yields this transformation
// fgMorphCopyBlock (after):
// [000050] ------------ lclVar long V07 loc5
// [000085] -A---------- = long
// [000083] D------N---- lclVar long V17 tmp9
//
if (m_blockSizeIsConst && (m_dstVarDsc->lvFieldCnt == 1) && (m_srcVarDsc != nullptr) &&
(m_blockSize == genTypeSize(m_srcVarDsc->TypeGet())))
{
// Reject the following tree:
// - seen on x86chk jit\jit64\hfa\main\hfa_sf3E_r.exe
//
// fgMorphTree BB01, stmt 6 (before)
// [000038] ------------- const int 4
// [000039] -A--G-------- copyBlk void
// [000037] ------------- addr byref
// [000036] ------------- lclVar int V05 loc3
// [000040] --------R---- <list> void
// [000035] ------------- addr byref
// [000034] ------------- lclVar struct(P) V04 loc2
// float V04.f1 (offs=0x00) -> V13 tmp6
// As this would framsform into
// float V13 = int V05
//
unsigned fieldLclNum = m_comp->lvaGetDesc(m_dstLclNum)->lvFieldLclStart;
var_types destType = m_comp->lvaGetDesc(fieldLclNum)->TypeGet();
if (m_srcVarDsc->TypeGet() == destType)
{
m_srcSingleLclVarAsg = true;
}
}
}
else if (m_srcDoFldAsg && srcFldIsProfitable)
{
// Check for the symmetric case (which happens for the _pointer field of promoted spans):
//
// [000240] -----+------ /--* lclVar struct(P) V18 tmp9
// /--* byref V18._value (offs=0x00) -> V30
// tmp21
// [000245] -A------R--- * = struct (copy)
// [000244] -----+------ \--* obj(8) struct
// [000243] -----+------ \--* addr byref
// [000242] D----+-N---- \--* lclVar byref V28 tmp19
//
if (m_blockSizeIsConst && (m_srcVarDsc->lvFieldCnt == 1) && (m_dstVarDsc != nullptr) &&
(m_blockSize == genTypeSize(m_dstVarDsc->TypeGet())))
{
// Check for type agreement
unsigned fieldLclNum = m_comp->lvaGetDesc(m_srcLclNum)->lvFieldLclStart;
var_types srcType = m_comp->lvaGetDesc(fieldLclNum)->TypeGet();
if (m_dstVarDsc->TypeGet() == srcType)
{
m_dstSingleLclVarAsg = true;
}
}
}
// Are neither dest or src promoted structs?
else
{
assert(!(m_dstDoFldAsg && dstFldIsProfitable) && !(m_srcDoFldAsg && srcFldIsProfitable));
requiresCopyBlock = true; // Leave as a CopyBlock
JITDUMP(" with no promoted structs");
}
}
// If we require a copy block the set both of the field assign bools to false
if (requiresCopyBlock)
{
// If a copy block is required then we won't do field by field assignments
m_dstDoFldAsg = false;
m_srcDoFldAsg = false;
}
JITDUMP(requiresCopyBlock ? " this requires a CopyBlock.\n" : " using field by field assignments.\n");
if (requiresCopyBlock)
{
const var_types asgType = m_dst->TypeGet();
bool isBlkReqd = (asgType == TYP_STRUCT);
m_dst = m_comp->fgMorphBlockOperand(m_dst, asgType, m_blockSize, isBlkReqd);
m_dst->gtFlags |= GTF_DONT_CSE;
m_asg->gtOp1 = m_dst;
m_asg->gtFlags |= (m_dst->gtFlags & GTF_ALL_EFFECT);
// Eliminate the "OBJ or BLK" node on the src.
m_src = m_comp->fgMorphBlockOperand(m_src, asgType, m_blockSize, false /*!isBlkReqd*/);
m_asg->gtOp2 = m_src;