forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorphblock.cpp
1581 lines (1401 loc) · 53.4 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* store, bool initBlock);
GenTree* Morph();
void PrepareDst();
virtual void PrepareSrc();
virtual void TrySpecialCases();
virtual void MorphStructCases();
void PropagateBlockAssertions();
void PropagateExpansionAssertions();
virtual const char* GetHelperName() const
{
return "MorphInitBlock";
}
private:
void TryInitFieldByField();
void TryPrimitiveInit();
GenTree* EliminateCommas(GenTree** commaPool);
protected:
Compiler* m_comp;
bool m_initBlock;
GenTree* m_store = nullptr;
GenTree* m_src = nullptr;
unsigned m_blockSize = 0;
ClassLayout* m_blockLayout = nullptr;
unsigned m_dstLclNum = BAD_VAR_NUM;
GenTreeLclVarCommon* m_dstLclNode = nullptr;
LclVarDsc* m_dstVarDsc = nullptr;
unsigned m_dstLclOffset = 0;
bool m_dstUseLclFld = false;
bool m_dstSingleStoreLclVar = false;
enum class BlockTransformation
{
Undefined,
FieldByField,
OneStoreBlock,
StructBlock,
SkipMultiRegSrc,
SkipSingleRegCallSrc,
Nop
};
BlockTransformation m_transformationDecision = BlockTransformation::Undefined;
GenTree* m_result = nullptr;
};
//------------------------------------------------------------------------
// MorphInitBlock: Morph a block initialization assignment tree.
//
// Arguments:
// comp - a compiler instance;
// tree - A store tree that performs block initialization.
//
// Return Value:
// A possibly modified tree to perform the initialization.
//
// 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;
// store - store node to morph.
//
// Notes:
// Most class members are initialized via in-class member initializers.
//
MorphInitBlockHelper::MorphInitBlockHelper(Compiler* comp, GenTree* store, bool initBlock = true)
: m_comp(comp), m_initBlock(initBlock)
{
assert(store->OperIsStore());
assert((m_initBlock == store->OperIsInitBlkOp()) && (!m_initBlock == store->OperIsCopyBlkOp()));
m_store = store;
}
//------------------------------------------------------------------------
// Morph: transform the store to a possible better form and changes its
// operands to an appropriate form for later phases.
//
// Return Value:
// A possibly modified tree to perform the block operation.
//
// Notes:
// It is used for both init and copy block.
//
GenTree* MorphInitBlockHelper::Morph()
{
JITDUMP("%s:\n", GetHelperName());
GenTree* commaPool;
GenTree* sideEffects = EliminateCommas(&commaPool);
PrepareDst();
PrepareSrc();
PropagateBlockAssertions();
TrySpecialCases();
if (m_transformationDecision == BlockTransformation::Undefined)
{
MorphStructCases();
}
PropagateExpansionAssertions();
assert(m_transformationDecision != BlockTransformation::Undefined);
assert(m_result != nullptr);
#ifdef DEBUG
// If we are going to return a different node than the input then morph
// expects us to have set GTF_DEBUG_NODE_MORPHED.
if ((m_result != m_store) || (sideEffects != nullptr))
{
m_result->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED;
}
#endif
while (sideEffects != nullptr)
{
if (commaPool != nullptr)
{
GenTree* comma = commaPool;
commaPool = commaPool->gtNext;
assert(comma->OperIs(GT_COMMA));
comma->gtType = TYP_VOID;
comma->AsOp()->gtOp1 = sideEffects;
comma->AsOp()->gtOp2 = m_result;
comma->gtFlags = (sideEffects->gtFlags | m_result->gtFlags) & GTF_ALL_EFFECT;
m_result = comma;
}
else
{
m_result = m_comp->gtNewOperNode(GT_COMMA, TYP_VOID, sideEffects, m_result);
}
INDEBUG(m_result->gtDebugFlags |= GTF_DEBUG_NODE_MORPHED);
sideEffects = sideEffects->gtNext;
}
JITDUMP("%s (after):\n", GetHelperName());
DISPTREE(m_result);
return m_result;
}
//------------------------------------------------------------------------
// PrepareDst: Initialize member fields with information about the store's
// destination.
//
void MorphInitBlockHelper::PrepareDst()
{
if (m_store->OperIsLocalStore())
{
m_dstLclNode = m_store->AsLclVarCommon();
m_dstLclOffset = m_dstLclNode->GetLclOffs();
m_dstLclNum = m_dstLclNode->GetLclNum();
m_dstVarDsc = m_comp->lvaGetDesc(m_dstLclNum);
// Kill everything about m_dstLclNum (and its field locals)
if (m_comp->optLocalAssertionProp && (m_comp->optAssertionCount > 0))
{
m_comp->fgKillDependentAssertions(m_dstLclNum DEBUGARG(m_store));
}
}
else
{
assert(m_store->OperIs(GT_STOREIND, GT_STORE_BLK));
}
if (m_store->TypeIs(TYP_STRUCT))
{
m_blockLayout = m_store->GetLayout(m_comp);
m_blockSize = m_blockLayout->GetSize();
}
else
{
m_blockSize = genTypeSize(m_store);
}
assert(m_blockSize != 0);
#if defined(DEBUG)
if (m_comp->verbose)
{
printf("PrepareDst for [%06u] ", m_comp->dspTreeID(m_store));
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
}
//------------------------------------------------------------------------
// PropagateBlockAssertions: propagate assertions based on the original tree
//
// Notes:
// Once the init or copy tree is morphed, assertion gen can no
// longer recognize what it means.
//
// So we generate assertions based on the original tree.
//
void MorphInitBlockHelper::PropagateBlockAssertions()
{
if (m_comp->optLocalAssertionProp)
{
m_comp->fgAssertionGen(m_store);
}
}
//------------------------------------------------------------------------
// PropagateExpansionAssertions: propagate assertions based on the
// expanded tree
//
// Notes:
// After the copy/init is expanded, we may see additional assertions
// to generate.
//
void MorphInitBlockHelper::PropagateExpansionAssertions()
{
// Consider doing this for FieldByField as well
//
if (m_comp->optLocalAssertionProp && (m_transformationDecision == BlockTransformation::OneStoreBlock))
{
m_comp->fgAssertionGen(m_store);
}
}
//------------------------------------------------------------------------
// PrepareSrc: Initialize member fields with information about the store's
// source value.
//
void MorphInitBlockHelper::PrepareSrc()
{
m_src = m_store->Data();
}
//------------------------------------------------------------------------
// TrySpecialCases: check special cases that require special transformations.
// We don't have any for init block.
//
void MorphInitBlockHelper::TrySpecialCases()
{
return;
}
//------------------------------------------------------------------------
// MorphStructCases: transforms the store 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.
//
void MorphInitBlockHelper::MorphStructCases()
{
// See if we can use the promoted fields to initialize the local: if we have already determined
// that a promoted local will not be enregistered, we are better off doing a block init.
if ((m_dstVarDsc != nullptr) && m_dstVarDsc->lvPromoted && !m_dstVarDsc->lvDoNotEnregister)
{
TryInitFieldByField();
}
if (m_transformationDecision == BlockTransformation::Undefined)
{
TryPrimitiveInit();
}
if (m_transformationDecision == BlockTransformation::Undefined)
{
m_result = m_store;
m_transformationDecision = BlockTransformation::StructBlock;
if (m_dstVarDsc != nullptr)
{
if (m_store->OperIs(GT_STORE_LCL_FLD))
{
m_comp->lvaSetVarDoNotEnregister(m_dstLclNum DEBUGARG(DoNotEnregisterReason::LocalField));
}
else if (m_dstVarDsc->lvPromoted)
{
m_comp->lvaSetVarDoNotEnregister(m_dstLclNum DEBUGARG(DoNotEnregisterReason::BlockOp));
}
}
}
}
//------------------------------------------------------------------------
// InitFieldByField: Attempts to promote a local block init tree to a tree
// of promoted field initialization assignments.
//
// If successful, will set "m_transformationDecision" to "FieldByField" and
// "m_result" to the final tree.
//
// Notes:
// This transforms a single block initialization assignment like:
//
// * STORE_BLK struct<12> (init)
// +--* LCL_ADDR byref V02 loc0
// | \-- int V02.a (offs=0x00) -> V06 tmp3
// | \-- ubyte V02.c (offs=0x04) -> V07 tmp4
// | \-- float V02.d (offs=0x08) -> V08 tmp5
// \--* INIT_VAL int
// \--* CNS_INT int 42
//
// into a COMMA tree of stores that initialize each promoted struct
// field:
//
// * COMMA void
// +--* COMMA void
// | +--* STORE_LCL_VAR int V06 tmp3
// | | \--* CNS_INT int 0x2A2A2A2A
// | \--* STORE_LCL_VAR ubyte V07 tmp4
// | \--* CNS_INT int 42
// \--* STORE_LCL_VAR float V08 tmp5
// \--* CNS_DBL float 1.5113661732714390e-13
//
void MorphInitBlockHelper::TryInitFieldByField()
{
assert((m_dstVarDsc != nullptr) && (m_dstVarDsc->lvPromoted));
LclVarDsc* destLclVar = m_dstVarDsc;
unsigned blockSize = m_blockSize;
if (destLclVar->IsAddressExposed() && destLclVar->lvContainsHoles)
{
JITDUMP(" dest is address exposed and contains holes.\n");
return;
}
if (destLclVar->lvAnySignificantPadding)
{
JITDUMP(" dest has significant padding.\n");
return;
}
if (m_dstLclOffset != 0)
{
JITDUMP(" dest not at a zero offset.\n");
return;
}
if (destLclVar->lvExactSize() != blockSize)
{
JITDUMP(" dest size mismatch.\n");
return;
}
GenTree* initVal = m_src->OperIsInitVal() ? m_src->gtGetOp1() : m_src;
if (!initVal->OperIs(GT_CNS_INT))
{
JITDUMP(" source is not constant.\n");
return;
}
const uint8_t initPattern = (uint8_t)(initVal->AsIntCon()->IconValue() & 0xFF);
if (initPattern != 0)
{
for (unsigned i = 0; i < destLclVar->lvFieldCnt; ++i)
{
LclVarDsc* fieldDesc = m_comp->lvaGetDesc(destLclVar->lvFieldLclStart + i);
if (varTypeIsGC(fieldDesc))
{
// Cannot initialize GC types with a non-zero constant. The
// former is completely bogus.
JITDUMP(" dest contains GC fields and source constant is not 0.\n");
return;
}
}
}
JITDUMP(" using field by field initialization.\n");
GenTree* tree = nullptr;
for (unsigned i = 0; i < destLclVar->lvFieldCnt; ++i)
{
unsigned fieldLclNum = destLclVar->lvFieldLclStart + i;
if (m_comp->fgGlobalMorph && m_dstLclNode->IsLastUse(i))
{
JITDUMP("Field-by-field init skipping write to dead field V%02u\n", fieldLclNum);
continue;
}
LclVarDsc* fieldDesc = m_comp->lvaGetDesc(fieldLclNum);
var_types fieldType = fieldDesc->TypeGet();
GenTree* src = m_comp->gtNewConWithPattern(fieldType, initPattern);
GenTree* store = m_comp->gtNewTempStore(fieldLclNum, src);
if (m_comp->optLocalAssertionProp)
{
m_comp->fgAssertionGen(store);
}
if (tree != nullptr)
{
tree = m_comp->gtNewOperNode(GT_COMMA, TYP_VOID, tree, store);
}
else
{
tree = store;
}
}
if (tree == nullptr)
{
tree = m_comp->gtNewNothingNode();
}
m_result = tree;
m_transformationDecision = BlockTransformation::FieldByField;
}
//------------------------------------------------------------------------
// TryPrimitiveInit: Replace block zero-initialization with a primitive store.
//
// Transforms patterns like "STORE_BLK(LCL_VAR_ADDR, 0)" into simple
// stores: "STORE_LCL_VAR<int>(0)".
//
// If successful, will set "m_transformationDecision" to "OneStoreBlock".
//
void MorphInitBlockHelper::TryPrimitiveInit()
{
if (m_src->IsIntegralConst(0) && (m_dstVarDsc != nullptr) && (genTypeSize(m_dstVarDsc) == m_blockSize))
{
var_types lclVarType = m_dstVarDsc->TypeGet();
if (varTypeIsSIMD(lclVarType))
{
m_src = m_comp->gtNewZeroConNode(lclVarType);
}
else
{
m_src->BashToZeroConst(lclVarType);
}
m_store->ChangeType(m_dstVarDsc->lvNormalizeOnLoad() ? lclVarType : genActualType(lclVarType));
m_store->ChangeOper(GT_STORE_LCL_VAR);
m_store->AsLclVar()->SetLclNum(m_dstLclNum);
m_store->gtFlags |= GTF_VAR_DEF;
m_result = m_store;
m_transformationDecision = BlockTransformation::OneStoreBlock;
}
}
//------------------------------------------------------------------------
// EliminateCommas: Prepare for block morphing by removing commas from the
// source operand of the assignment.
//
// Parameters:
// commaPool - [out] Pool of GT_COMMA nodes linked by their gtNext nodes that
// can be used by the caller to avoid unnecessarily creating
// new commas.
//
// Returns:
// Extracted side effects, in reverse order, linked via the gtNext fields of
// the nodes.
//
// Notes:
// We have a tree like the following:
//
// STOREIND
// / \.
// B COMMA
// / \.
// C D
//
// We'd like downstream code to just see and expand STOREIND(B, D).
// We will produce:
//
// COMMA
// / \.
// STORE_LCL_VAR<tmp> COMMA
// / / \.
// B C STOREIND
// / \.
// tmp D
//
// If the store has GTF_REVERSE_OPS then we will produce:
//
// COMMA
// / \.
// C STOREIND
// / \.
// B D
//
// While keeping the GTF_REVERSE_OPS.
//
// Note that the final resulting tree is created in the caller since it also
// needs to propagate side effect flags from the decomposed store to all the
// created commas. Therefore this function just returns a linked list of the
// side effects to be used for that purpose.
//
GenTree* MorphInitBlockHelper::EliminateCommas(GenTree** commaPool)
{
*commaPool = nullptr;
GenTree* sideEffects = nullptr;
auto addSideEffect = [&sideEffects](GenTree* sideEff) {
sideEff->gtNext = sideEffects;
sideEffects = sideEff;
};
auto addComma = [commaPool, &addSideEffect](GenTree* comma) {
addSideEffect(comma->gtGetOp1());
comma->gtNext = *commaPool;
*commaPool = comma;
};
GenTree* src = m_store->Data();
if (m_store->IsReverseOp())
{
while (src->OperIs(GT_COMMA))
{
addComma(src);
src = src->gtGetOp2();
}
}
else
{
if (m_store->OperIsIndir() && src->OperIs(GT_COMMA))
{
GenTree* addr = m_store->AsIndir()->Addr();
if (((addr->gtFlags & GTF_ALL_EFFECT) != 0) || (((src->gtFlags & GTF_ASG) != 0) && !addr->IsInvariant()))
{
unsigned lhsAddrLclNum = m_comp->lvaGrabTemp(true DEBUGARG("Block morph LHS addr"));
addSideEffect(m_comp->gtNewTempStore(lhsAddrLclNum, addr));
m_store->AsUnOp()->gtOp1 = m_comp->gtNewLclvNode(lhsAddrLclNum, genActualType(addr));
m_comp->gtUpdateNodeSideEffects(m_store);
}
}
while (src->OperIs(GT_COMMA))
{
addComma(src);
src = src->gtGetOp2();
}
}
if (sideEffects != nullptr)
{
m_store->Data() = src;
m_comp->gtUpdateNodeSideEffects(m_store);
}
return sideEffects;
}
class MorphCopyBlockHelper : public MorphInitBlockHelper
{
public:
static GenTree* MorphCopyBlock(Compiler* comp, GenTree* tree);
protected:
MorphCopyBlockHelper(Compiler* comp, GenTree* store);
void PrepareSrc() override;
void TrySpecialCases() override;
void MorphStructCases() override;
void TryPrimitiveCopy();
GenTree* CopyFieldByField();
const char* GetHelperName() const override
{
return "MorphCopyBlock";
}
protected:
unsigned m_srcLclNum = BAD_VAR_NUM;
LclVarDsc* m_srcVarDsc = nullptr;
GenTreeLclVarCommon* m_srcLclNode = nullptr;
bool m_srcUseLclFld = false;
unsigned m_srcLclOffset = 0;
bool m_srcSingleStoreLclVar = false;
bool m_dstDoFldStore = false;
bool m_srcDoFldStore = false;
private:
bool CanReuseAddressForDecomposedStore(GenTree* addr);
};
//------------------------------------------------------------------------
// MorphCopyBlock: Morph a block copy assignment tree.
//
// Arguments:
// comp - a compiler instance;
// tree - A store tree that performs block copy.
//
// Return Value:
// A possibly modified tree to perform 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;
// store - store node to morph.
//
// Notes:
// Most class members are initialized via in-class member initializers.
//
MorphCopyBlockHelper::MorphCopyBlockHelper(Compiler* comp, GenTree* store) : MorphInitBlockHelper(comp, store, false)
{
}
//------------------------------------------------------------------------
// PrepareSrc: Initialize member fields with information about the store's
// source value.
//
void MorphCopyBlockHelper::PrepareSrc()
{
m_src = m_store->Data();
if (m_src->IsLocal())
{
m_srcLclNode = m_src->AsLclVarCommon();
m_srcLclOffset = m_srcLclNode->GetLclOffs();
m_srcLclNum = m_srcLclNode->GetLclNum();
m_srcVarDsc = m_comp->lvaGetDesc(m_srcLclNum);
}
// Verify that the types of the store and data match.
assert(m_store->TypeGet() == m_src->TypeGet());
if (m_store->TypeIs(TYP_STRUCT))
{
assert(ClassLayout::AreCompatible(m_blockLayout, m_src->GetLayout(m_comp)));
}
}
// TrySpecialCases: check special cases that require special transformations.
// The current special cases include assignments with calls in RHS.
//
void MorphCopyBlockHelper::TrySpecialCases()
{
if (m_src->IsMultiRegNode())
{
assert(m_store->OperIs(GT_STORE_LCL_VAR));
m_dstVarDsc->lvIsMultiRegRet = true;
JITDUMP("Not morphing a multireg node return\n");
m_transformationDecision = BlockTransformation::SkipMultiRegSrc;
m_result = m_store;
}
else if (m_src->IsCall() && m_store->OperIs(GT_STORE_LCL_VAR) && m_dstVarDsc->CanBeReplacedWithItsField(m_comp))
{
JITDUMP("Not morphing a single reg call return\n");
m_transformationDecision = BlockTransformation::SkipSingleRegCallSrc;
m_result = m_store;
}
}
//------------------------------------------------------------------------
// MorphStructCases: transforms the store 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.
//
void MorphCopyBlockHelper::MorphStructCases()
{
JITDUMP("block assignment to morph:\n");
DISPTREE(m_store);
if (m_dstVarDsc != nullptr)
{
if (m_dstVarDsc->lvPromoted)
{
noway_assert(varTypeIsStruct(m_dstVarDsc));
noway_assert(!m_comp->opts.MinOpts());
if (m_blockSize == m_dstVarDsc->lvExactSize())
{
JITDUMP(" (m_dstDoFldStore=true)");
// We may decide later that a copyblk is required when this struct has holes
m_dstDoFldStore = true;
}
else
{
JITDUMP(" with mismatched dest size");
}
}
}
if (m_srcVarDsc != nullptr)
{
if (m_srcVarDsc->lvPromoted)
{
noway_assert(varTypeIsStruct(m_srcVarDsc));
noway_assert(!m_comp->opts.MinOpts());
if (m_blockSize == m_srcVarDsc->lvExactSize())
{
JITDUMP(" (m_srcDoFldStore=true)");
// We may decide later that a copyblk is required when this struct has holes
m_srcDoFldStore = 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.
// Don't do this for SSA definitions as we have no way to update downstream uses.
if ((m_dstVarDsc != nullptr) && (m_srcVarDsc == m_dstVarDsc) && (m_dstLclOffset == m_srcLclOffset) &&
!m_store->AsLclVarCommon()->HasSsaIdentity())
{
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;
// this will avoid having to DNER the enregisterable local when creating LCL_FLD nodes.
if ((m_store->OperIs(GT_STORE_LCL_VAR) && m_dstVarDsc->lvRegStruct) ||
(m_src->OperIs(GT_LCL_VAR) && m_srcVarDsc->lvRegStruct))
{
requiresCopyBlock = true;
}
// Can we use field by field assignment for the dest?
if (m_dstDoFldStore && m_dstVarDsc->lvAnySignificantPadding)
{
JITDUMP(" dest has significant padding");
// C++ style CopyBlock with holes
requiresCopyBlock = true;
}
// Can we use field by field assignment for the src?
if (m_srcDoFldStore && m_srcVarDsc->lvAnySignificantPadding)
{
JITDUMP(" src has significant padding");
// C++ style CopyBlock with holes
requiresCopyBlock = true;
}
#if defined(TARGET_ARM)
if (m_store->OperIsIndir() && m_store->AsIndir()->IsUnaligned())
{
JITDUMP(" store is unaligned");
requiresCopyBlock = true;
}
if (m_src->OperIsIndir() && m_src->AsIndir()->IsUnaligned())
{
JITDUMP(" src 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.
// For HWI/SIMD/CNS_VEC, we don't expect promoted destinations - we purposefully
// mark SIMDs used in such copies as "used in a SIMD intrinsic", to prevent their
// promotion.
if ((m_srcVarDsc == nullptr) && !m_src->OperIsIndir())
{
JITDUMP(" src is not an L-value");
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 preferred:
// - if fields can be enregistered;
// - if the struct has only one field.
// - if the copy involves GC pointers and the destination isn't stack (backend uses a helper for these block
// copies)
bool dstFldIsProfitable =
((m_dstVarDsc != nullptr) && (!m_dstVarDsc->lvDoNotEnregister || (m_dstVarDsc->lvFieldCnt == 1)));
bool srcFldIsProfitable = ((m_srcVarDsc != nullptr) && (!m_srcVarDsc->lvDoNotEnregister ||
(m_srcVarDsc->HasGCPtr() && (m_dstVarDsc == nullptr)) ||
(m_srcVarDsc->lvFieldCnt == 1)));
// Are both dest and src promoted structs?
if (m_dstDoFldStore && m_srcDoFldStore && (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->GetLayout() != m_srcVarDsc->GetLayout())
{
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_dstDoFldStore && 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_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_srcSingleStoreLclVar = true;
}
}
}
else if (m_srcDoFldStore && srcFldIsProfitable)
{
// Check for the symmetric case (which happens for the _reference 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_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_dstSingleStoreLclVar = true;
}
}
}
// Are neither dest or src promoted structs?
else
{
assert(!(m_dstDoFldStore && dstFldIsProfitable) && !(m_srcDoFldStore && 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_dstDoFldStore = false;
m_srcDoFldStore = false;
}
JITDUMP(requiresCopyBlock ? " this requires a CopyBlock.\n" : " using field by field assignments.\n");
if (requiresCopyBlock)
{
TryPrimitiveCopy();
if (m_transformationDecision == BlockTransformation::Undefined)
{
m_result = m_store;
m_transformationDecision = BlockTransformation::StructBlock;
}
}
else
{
m_result = CopyFieldByField();
m_transformationDecision = BlockTransformation::FieldByField;
}
// Mark the dest/src structs as DoNotEnreg when they are not being fully referenced as the same type.
//
if (!m_dstDoFldStore && (m_dstVarDsc != nullptr) && !m_dstSingleStoreLclVar)
{
if (m_store->OperIs(GT_STORE_LCL_FLD))
{
m_comp->lvaSetVarDoNotEnregister(m_dstLclNum DEBUGARG(DoNotEnregisterReason::LocalField));
}
else if (m_dstVarDsc->lvPromoted)
{
// Mark it as DoNotEnregister.
m_comp->lvaSetVarDoNotEnregister(m_dstLclNum DEBUGARG(DoNotEnregisterReason::BlockOp));
}
}
if (!m_srcDoFldStore && (m_srcVarDsc != nullptr) && !m_srcSingleStoreLclVar)
{
if (m_src->OperIs(GT_LCL_FLD))
{
m_comp->lvaSetVarDoNotEnregister(m_srcLclNum DEBUGARG(DoNotEnregisterReason::LocalField));
}
else if (m_srcVarDsc->lvPromoted)
{
m_comp->lvaSetVarDoNotEnregister(m_srcLclNum DEBUGARG(DoNotEnregisterReason::BlockOp));
}
}
}
//------------------------------------------------------------------------
// TryPrimitiveCopy: Attempt to replace a block assignment with a scalar assignment.
//