-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathlsraxarch.cpp
2814 lines (2499 loc) · 92.9 KB
/
lsraxarch.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.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX Register Requirements for AMD64 XX
XX XX
XX This encapsulates all the logic for setting register requirements for XX
XX the AMD64 architecture. XX
XX XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#ifdef TARGET_XARCH
#include "jit.h"
#include "sideeffects.h"
#include "lower.h"
//------------------------------------------------------------------------
// BuildNode: Build the RefPositions for for a node
//
// Arguments:
// treeNode - the node of interest
//
// Return Value:
// The number of sources consumed by this node.
//
// Notes:
// Preconditions:
// LSRA Has been initialized.
//
// Postconditions:
// RefPositions have been built for all the register defs and uses required
// for this node.
//
int LinearScan::BuildNode(GenTree* tree)
{
assert(!tree->isContained());
int srcCount;
int dstCount = 0;
regMaskTP killMask = RBM_NONE;
bool isLocalDefUse = false;
// Reset the build-related members of LinearScan.
clearBuildState();
// Set the default dstCount. This may be modified below.
if (tree->IsValue())
{
dstCount = 1;
if (tree->IsUnusedValue())
{
isLocalDefUse = true;
}
}
else
{
dstCount = 0;
}
// floating type generates AVX instruction (vmovss etc.), set the flag
if (varTypeUsesFloatReg(tree->TypeGet()))
{
SetContainsAVXFlags();
}
switch (tree->OperGet())
{
default:
srcCount = BuildSimple(tree);
break;
case GT_LCL_VAR:
// We make a final determination about whether a GT_LCL_VAR is a candidate or contained
// after liveness. In either case we don't build any uses or defs. Otherwise, this is a
// load of a stack-based local into a register and we'll fall through to the general
// local case below.
if (checkContainedOrCandidateLclVar(tree->AsLclVar()))
{
return 0;
}
FALLTHROUGH;
case GT_LCL_FLD:
{
srcCount = 0;
#ifdef FEATURE_SIMD
// Need an additional register to read upper 4 bytes of Vector3.
if (tree->TypeGet() == TYP_SIMD12)
{
// We need an internal register different from targetReg in which 'tree' produces its result
// because both targetReg and internal reg will be in use at the same time.
buildInternalFloatRegisterDefForNode(tree, allSIMDRegs());
setInternalRegsDelayFree = true;
buildInternalRegisterUses();
}
#endif
BuildDef(tree);
}
break;
case GT_STORE_LCL_FLD:
case GT_STORE_LCL_VAR:
if (tree->IsMultiRegLclVar() && isCandidateMultiRegLclVar(tree->AsLclVar()))
{
dstCount = compiler->lvaGetDesc(tree->AsLclVar()->GetLclNum())->lvFieldCnt;
}
srcCount = BuildStoreLoc(tree->AsLclVarCommon());
break;
case GT_FIELD_LIST:
// These should always be contained. We don't correctly allocate or
// generate code for a non-contained GT_FIELD_LIST.
noway_assert(!"Non-contained GT_FIELD_LIST");
srcCount = 0;
break;
case GT_LIST:
case GT_ARGPLACE:
case GT_NO_OP:
case GT_START_NONGC:
srcCount = 0;
assert(dstCount == 0);
break;
case GT_START_PREEMPTGC:
// This kills GC refs in callee save regs
srcCount = 0;
assert(dstCount == 0);
BuildDefsWithKills(tree, 0, RBM_NONE, RBM_NONE);
break;
case GT_PROF_HOOK:
srcCount = 0;
assert(dstCount == 0);
killMask = getKillSetForProfilerHook();
BuildDefsWithKills(tree, 0, RBM_NONE, killMask);
break;
case GT_CNS_INT:
case GT_CNS_LNG:
case GT_CNS_DBL:
{
srcCount = 0;
assert(dstCount == 1);
assert(!tree->IsReuseRegVal());
RefPosition* def = BuildDef(tree);
def->getInterval()->isConstant = true;
}
break;
#if !defined(TARGET_64BIT)
case GT_LONG:
assert(tree->IsUnusedValue()); // Contained nodes are already processed, only unused GT_LONG can reach here.
// An unused GT_LONG node needs to consume its sources, but need not produce a register.
tree->gtType = TYP_VOID;
tree->ClearUnusedValue();
isLocalDefUse = false;
srcCount = 2;
dstCount = 0;
BuildUse(tree->gtGetOp1());
BuildUse(tree->gtGetOp2());
break;
#endif // !defined(TARGET_64BIT)
case GT_BOX:
case GT_COMMA:
case GT_QMARK:
case GT_COLON:
srcCount = 0;
unreached();
break;
case GT_RETURN:
srcCount = BuildReturn(tree);
killMask = getKillSetForReturn();
BuildDefsWithKills(tree, 0, RBM_NONE, killMask);
break;
case GT_RETFILT:
assert(dstCount == 0);
if (tree->TypeGet() == TYP_VOID)
{
srcCount = 0;
}
else
{
assert(tree->TypeGet() == TYP_INT);
srcCount = 1;
BuildUse(tree->gtGetOp1(), RBM_INTRET);
}
break;
// A GT_NOP is either a passthrough (if it is void, or if it has
// a child), but must be considered to produce a dummy value if it
// has a type but no child
case GT_NOP:
srcCount = 0;
assert((tree->gtGetOp1() == nullptr) || tree->isContained());
if (tree->TypeGet() != TYP_VOID && tree->gtGetOp1() == nullptr)
{
assert(dstCount == 1);
BuildUse(tree->gtGetOp1());
BuildDef(tree);
}
else
{
assert(dstCount == 0);
}
break;
case GT_KEEPALIVE:
assert(dstCount == 0);
srcCount = BuildOperandUses(tree->gtGetOp1());
break;
case GT_JTRUE:
{
srcCount = 0;
assert(dstCount == 0);
GenTree* cmp = tree->gtGetOp1();
assert(!cmp->IsValue());
}
break;
case GT_JCC:
srcCount = 0;
assert(dstCount == 0);
break;
case GT_SETCC:
srcCount = 0;
assert(dstCount == 1);
// This defines a byte value (note that on x64 allByteRegs() is defined as RBM_ALLINT).
BuildDef(tree, allByteRegs());
break;
case GT_JMP:
srcCount = 0;
assert(dstCount == 0);
break;
case GT_SWITCH:
// This should never occur since switch nodes must not be visible at this
// point in the JIT.
srcCount = 0;
noway_assert(!"Switch must be lowered at this point");
break;
case GT_JMPTABLE:
srcCount = 0;
assert(dstCount == 1);
BuildDef(tree);
break;
case GT_SWITCH_TABLE:
{
assert(dstCount == 0);
buildInternalIntRegisterDefForNode(tree);
srcCount = BuildBinaryUses(tree->AsOp());
buildInternalRegisterUses();
assert(srcCount == 2);
}
break;
case GT_ASG:
noway_assert(!"We should never hit any assignment operator in lowering");
srcCount = 0;
break;
#if !defined(TARGET_64BIT)
case GT_ADD_LO:
case GT_ADD_HI:
case GT_SUB_LO:
case GT_SUB_HI:
#endif
case GT_ADD:
case GT_SUB:
case GT_AND:
case GT_OR:
case GT_XOR:
srcCount = BuildBinaryUses(tree->AsOp());
assert(dstCount == 1);
BuildDef(tree);
break;
case GT_BT:
srcCount = BuildBinaryUses(tree->AsOp());
assert(dstCount == 0);
break;
case GT_RETURNTRAP:
{
// This just turns into a compare of its child with an int + a conditional call.
RefPosition* internalDef = buildInternalIntRegisterDefForNode(tree);
srcCount = BuildOperandUses(tree->gtGetOp1());
buildInternalRegisterUses();
killMask = compiler->compHelperCallKillSet(CORINFO_HELP_STOP_FOR_GC);
BuildDefsWithKills(tree, 0, RBM_NONE, killMask);
}
break;
case GT_MOD:
case GT_DIV:
case GT_UMOD:
case GT_UDIV:
srcCount = BuildModDiv(tree->AsOp());
break;
#if defined(TARGET_X86)
case GT_MUL_LONG:
dstCount = 2;
FALLTHROUGH;
#endif
case GT_MUL:
case GT_MULHI:
srcCount = BuildMul(tree->AsOp());
break;
case GT_INTRINSIC:
srcCount = BuildIntrinsic(tree->AsOp());
break;
#ifdef FEATURE_SIMD
case GT_SIMD:
srcCount = BuildSIMD(tree->AsSIMD());
break;
#endif // FEATURE_SIMD
#ifdef FEATURE_HW_INTRINSICS
case GT_HWINTRINSIC:
srcCount = BuildHWIntrinsic(tree->AsHWIntrinsic());
break;
#endif // FEATURE_HW_INTRINSICS
case GT_CAST:
assert(dstCount == 1);
srcCount = BuildCast(tree->AsCast());
break;
case GT_BITCAST:
assert(dstCount == 1);
if (!tree->gtGetOp1()->isContained())
{
BuildUse(tree->gtGetOp1());
srcCount = 1;
}
else
{
srcCount = 0;
}
BuildDef(tree);
break;
case GT_NEG:
// TODO-XArch-CQ:
// SSE instruction set doesn't have an instruction to negate a number.
// The recommended way is to xor the float/double number with a bitmask.
// The only way to xor is using xorps or xorpd both of which operate on
// 128-bit operands. To hold the bit-mask we would need another xmm
// register or a 16-byte aligned 128-bit data constant. Right now emitter
// lacks the support for emitting such constants or instruction with mem
// addressing mode referring to a 128-bit operand. For now we use an
// internal xmm register to load 32/64-bit bitmask from data section.
// Note that by trading additional data section memory (128-bit) we can
// save on the need for an internal register and also a memory-to-reg
// move.
//
// Note: another option to avoid internal register requirement is by
// lowering as GT_SUB(0, src). This will generate code different from
// Jit64 and could possibly result in compat issues (?).
if (varTypeIsFloating(tree))
{
RefPosition* internalDef = buildInternalFloatRegisterDefForNode(tree, internalFloatRegCandidates());
srcCount = BuildOperandUses(tree->gtGetOp1());
buildInternalRegisterUses();
}
else
{
srcCount = BuildOperandUses(tree->gtGetOp1());
}
BuildDef(tree);
break;
case GT_NOT:
srcCount = BuildOperandUses(tree->gtGetOp1());
BuildDef(tree);
break;
case GT_LSH:
case GT_RSH:
case GT_RSZ:
case GT_ROL:
case GT_ROR:
#ifdef TARGET_X86
case GT_LSH_HI:
case GT_RSH_LO:
#endif
srcCount = BuildShiftRotate(tree);
break;
case GT_EQ:
case GT_NE:
case GT_LT:
case GT_LE:
case GT_GE:
case GT_GT:
case GT_TEST_EQ:
case GT_TEST_NE:
case GT_CMP:
srcCount = BuildCmp(tree);
break;
case GT_CKFINITE:
{
assert(dstCount == 1);
RefPosition* internalDef = buildInternalIntRegisterDefForNode(tree);
srcCount = BuildOperandUses(tree->gtGetOp1());
buildInternalRegisterUses();
BuildDef(tree);
}
break;
case GT_CMPXCHG:
{
srcCount = 3;
assert(dstCount == 1);
// Comparand is preferenced to RAX.
// The remaining two operands can be in any reg other than RAX.
BuildUse(tree->AsCmpXchg()->gtOpLocation, allRegs(TYP_INT) & ~RBM_RAX);
BuildUse(tree->AsCmpXchg()->gtOpValue, allRegs(TYP_INT) & ~RBM_RAX);
BuildUse(tree->AsCmpXchg()->gtOpComparand, RBM_RAX);
BuildDef(tree, RBM_RAX);
}
break;
case GT_XORR:
case GT_XAND:
case GT_XADD:
case GT_XCHG:
{
// TODO-XArch-Cleanup: We should make the indirection explicit on these nodes so that we don't have
// to special case them.
// These tree nodes will have their op1 marked as isDelayFree=true.
// That is, op1's reg remains in use until the subsequent instruction.
GenTree* addr = tree->gtGetOp1();
GenTree* data = tree->gtGetOp2();
assert(!addr->isContained());
RefPosition* addrUse = BuildUse(addr);
setDelayFree(addrUse);
tgtPrefUse = addrUse;
assert(!data->isContained());
BuildUse(data);
srcCount = 2;
assert(dstCount == 1);
BuildDef(tree);
}
break;
case GT_PUTARG_REG:
srcCount = BuildPutArgReg(tree->AsUnOp());
break;
case GT_CALL:
srcCount = BuildCall(tree->AsCall());
if (tree->AsCall()->HasMultiRegRetVal())
{
dstCount = tree->AsCall()->GetReturnTypeDesc()->GetReturnRegCount();
}
break;
case GT_ADDR:
{
// For a GT_ADDR, the child node should not be evaluated into a register
GenTree* child = tree->gtGetOp1();
assert(!isCandidateLocalRef(child));
assert(child->isContained());
assert(dstCount == 1);
srcCount = 0;
}
break;
#if !defined(FEATURE_PUT_STRUCT_ARG_STK)
case GT_OBJ:
#endif
case GT_BLK:
case GT_DYN_BLK:
// These should all be eliminated prior to Lowering.
assert(!"Non-store block node in Lowering");
srcCount = 0;
break;
#ifdef FEATURE_PUT_STRUCT_ARG_STK
case GT_PUTARG_STK:
srcCount = BuildPutArgStk(tree->AsPutArgStk());
break;
#endif // FEATURE_PUT_STRUCT_ARG_STK
case GT_STORE_BLK:
case GT_STORE_OBJ:
case GT_STORE_DYN_BLK:
srcCount = BuildBlockStore(tree->AsBlk());
break;
case GT_INIT_VAL:
// Always a passthrough of its child's value.
assert(!"INIT_VAL should always be contained");
srcCount = 0;
break;
case GT_LCLHEAP:
srcCount = BuildLclHeap(tree);
break;
case GT_ARR_BOUNDS_CHECK:
#ifdef FEATURE_SIMD
case GT_SIMD_CHK:
#endif // FEATURE_SIMD
#ifdef FEATURE_HW_INTRINSICS
case GT_HW_INTRINSIC_CHK:
#endif // FEATURE_HW_INTRINSICS
// Consumes arrLen & index - has no result
assert(dstCount == 0);
srcCount = BuildOperandUses(tree->AsBoundsChk()->gtIndex);
srcCount += BuildOperandUses(tree->AsBoundsChk()->gtArrLen);
break;
case GT_ARR_ELEM:
// These must have been lowered to GT_ARR_INDEX
noway_assert(!"We should never see a GT_ARR_ELEM after Lowering.");
srcCount = 0;
break;
case GT_ARR_INDEX:
{
srcCount = 2;
assert(dstCount == 1);
assert(!tree->AsArrIndex()->ArrObj()->isContained());
assert(!tree->AsArrIndex()->IndexExpr()->isContained());
// For GT_ARR_INDEX, the lifetime of the arrObj must be extended because it is actually used multiple
// times while the result is being computed.
RefPosition* arrObjUse = BuildUse(tree->AsArrIndex()->ArrObj());
setDelayFree(arrObjUse);
BuildUse(tree->AsArrIndex()->IndexExpr());
BuildDef(tree);
}
break;
case GT_ARR_OFFSET:
{
// This consumes the offset, if any, the arrObj and the effective index,
// and produces the flattened offset for this dimension.
assert(dstCount == 1);
srcCount = 0;
RefPosition* internalDef = nullptr;
if (tree->AsArrOffs()->gtOffset->isContained())
{
srcCount = 2;
}
else
{
// Here we simply need an internal register, which must be different
// from any of the operand's registers, but may be the same as targetReg.
srcCount = 3;
internalDef = buildInternalIntRegisterDefForNode(tree);
BuildUse(tree->AsArrOffs()->gtOffset);
}
BuildUse(tree->AsArrOffs()->gtIndex);
BuildUse(tree->AsArrOffs()->gtArrObj);
if (internalDef != nullptr)
{
buildInternalRegisterUses();
}
BuildDef(tree);
}
break;
case GT_LEA:
// The LEA usually passes its operands through to the GT_IND, in which case it will
// be contained, but we may be instantiating an address, in which case we set them here.
srcCount = 0;
assert(dstCount == 1);
if (tree->AsAddrMode()->HasBase())
{
srcCount++;
BuildUse(tree->AsAddrMode()->Base());
}
if (tree->AsAddrMode()->HasIndex())
{
srcCount++;
BuildUse(tree->AsAddrMode()->Index());
}
BuildDef(tree);
break;
case GT_STOREIND:
if (compiler->codeGen->gcInfo.gcIsWriteBarrierStoreIndNode(tree))
{
srcCount = BuildGCWriteBarrier(tree);
break;
}
srcCount = BuildIndir(tree->AsIndir());
break;
case GT_NULLCHECK:
{
assert(dstCount == 0);
// If we have a contained address on a nullcheck, we transform it to
// an unused GT_IND, since we require a target register.
BuildUse(tree->gtGetOp1());
srcCount = 1;
break;
}
case GT_IND:
srcCount = BuildIndir(tree->AsIndir());
assert(dstCount == 1);
break;
case GT_CATCH_ARG:
srcCount = 0;
assert(dstCount == 1);
BuildDef(tree, RBM_EXCEPTION_OBJECT);
break;
#if !defined(FEATURE_EH_FUNCLETS)
case GT_END_LFIN:
srcCount = 0;
assert(dstCount == 0);
break;
#endif
case GT_CLS_VAR:
// These nodes are eliminated by rationalizer.
JITDUMP("Unexpected node %s in Lower.\n", GenTree::OpName(tree->OperGet()));
unreached();
break;
case GT_INDEX_ADDR:
{
assert(dstCount == 1);
RefPosition* internalDef = nullptr;
#ifdef TARGET_64BIT
// On 64-bit we always need a temporary register:
// - if the index is `native int` then we need to load the array
// length into a register to widen it to `native int`
// - if the index is `int` (or smaller) then we need to widen
// it to `long` to peform the address calculation
internalDef = buildInternalIntRegisterDefForNode(tree);
#else // !TARGET_64BIT
assert(!varTypeIsLong(tree->AsIndexAddr()->Index()->TypeGet()));
switch (tree->AsIndexAddr()->gtElemSize)
{
case 1:
case 2:
case 4:
case 8:
break;
default:
internalDef = buildInternalIntRegisterDefForNode(tree);
break;
}
#endif // !TARGET_64BIT
srcCount = BuildBinaryUses(tree->AsOp());
if (internalDef != nullptr)
{
buildInternalRegisterUses();
}
BuildDef(tree);
}
break;
} // end switch (tree->OperGet())
// We need to be sure that we've set srcCount and dstCount appropriately.
// Not that for XARCH, the maximum number of registers defined is 2.
assert((dstCount < 2) || ((dstCount == 2) && tree->IsMultiRegNode()));
assert(isLocalDefUse == (tree->IsValue() && tree->IsUnusedValue()));
assert(!tree->IsUnusedValue() || (dstCount != 0));
assert(dstCount == tree->GetRegisterDstCount(compiler));
return srcCount;
}
//------------------------------------------------------------------------
// getTgtPrefOperands: Identify whether the operands of an Op should be preferenced to the target.
//
// Arguments:
// tree - the node of interest.
// prefOp1 - a bool "out" parameter indicating, on return, whether op1 should be preferenced to the target.
// prefOp2 - a bool "out" parameter indicating, on return, whether op2 should be preferenced to the target.
//
// Return Value:
// This has two "out" parameters for returning the results (see above).
//
// Notes:
// The caller is responsible for initializing the two "out" parameters to false.
//
void LinearScan::getTgtPrefOperands(GenTreeOp* tree, bool& prefOp1, bool& prefOp2)
{
// If op2 of a binary-op gets marked as contained, then binary-op srcCount will be 1.
// Even then we would like to set isTgtPref on Op1.
if (tree->OperIsBinary() && isRMWRegOper(tree))
{
GenTree* op1 = tree->gtGetOp1();
GenTree* op2 = tree->gtGetOp2();
// If we have a read-modify-write operation, we want to preference op1 to the target,
// if it is not contained.
if (!op1->isContained() && !op1->OperIs(GT_LIST))
{
prefOp1 = true;
}
// Commutative opers like add/mul/and/or/xor could reverse the order of operands if it is safe to do so.
// In that case we will preference both, to increase the chance of getting a match.
if (tree->OperIsCommutative() && op2 != nullptr && !op2->isContained())
{
prefOp2 = true;
}
}
}
//------------------------------------------------------------------------------
// isRMWRegOper: Can this binary tree node be used in a Read-Modify-Write format
//
// Arguments:
// tree - a binary tree node
//
// Return Value:
// Returns true if we can use the read-modify-write instruction form
//
// Notes:
// This is used to determine whether to preference the source to the destination register.
//
bool LinearScan::isRMWRegOper(GenTree* tree)
{
// TODO-XArch-CQ: Make this more accurate.
// For now, We assume that most binary operators are of the RMW form.
assert(tree->OperIsBinary());
if (tree->OperIsCompare() || tree->OperIs(GT_CMP) || tree->OperIs(GT_BT))
{
return false;
}
switch (tree->OperGet())
{
// These Opers either support a three op form (i.e. GT_LEA), or do not read/write their first operand
case GT_LEA:
case GT_STOREIND:
case GT_ARR_INDEX:
case GT_STORE_BLK:
case GT_STORE_OBJ:
case GT_SWITCH_TABLE:
case GT_LOCKADD:
#ifdef TARGET_X86
case GT_LONG:
#endif
return false;
case GT_ADD:
case GT_SUB:
case GT_DIV:
{
return !varTypeIsFloating(tree->TypeGet()) || !compiler->canUseVexEncoding();
}
// x86/x64 does support a three op multiply when op2|op1 is a contained immediate
case GT_MUL:
{
if (varTypeIsFloating(tree->TypeGet()))
{
return !compiler->canUseVexEncoding();
}
return (!tree->gtGetOp2()->isContainedIntOrIImmed() && !tree->gtGetOp1()->isContainedIntOrIImmed());
}
#ifdef FEATURE_HW_INTRINSICS
case GT_HWINTRINSIC:
return tree->isRMWHWIntrinsic(compiler);
#endif // FEATURE_HW_INTRINSICS
default:
return true;
}
}
// Support for building RefPositions for RMW nodes.
int LinearScan::BuildRMWUses(GenTreeOp* node, regMaskTP candidates)
{
int srcCount = 0;
GenTree* op1 = node->gtOp1;
GenTree* op2 = node->gtGetOp2IfPresent();
regMaskTP op1Candidates = candidates;
regMaskTP op2Candidates = candidates;
#ifdef TARGET_X86
if (varTypeIsByte(node))
{
regMaskTP byteCandidates = (candidates == RBM_NONE) ? allByteRegs() : (candidates & allByteRegs());
if (!op1->isContained())
{
assert(byteCandidates != RBM_NONE);
op1Candidates = byteCandidates;
}
if (node->OperIsCommutative() && !op2->isContained())
{
assert(byteCandidates != RBM_NONE);
op2Candidates = byteCandidates;
}
}
#endif // TARGET_X86
bool prefOp1 = false;
bool prefOp2 = false;
getTgtPrefOperands(node, prefOp1, prefOp2);
assert(!prefOp2 || node->OperIsCommutative());
// Determine which operand, if any, should be delayRegFree. Normally, this would be op2,
// but if we have a commutative operator and op1 is a contained memory op, it would be op1.
// We need to make the delayRegFree operand remain live until the op is complete, by marking
// the source(s) associated with op2 as "delayFree".
// Note that if op2 of a binary RMW operator is a memory op, even if the operator
// is commutative, codegen cannot reverse them.
// TODO-XArch-CQ: This is not actually the case for all RMW binary operators, but there's
// more work to be done to correctly reverse the operands if they involve memory
// operands. Also, we may need to handle more cases than GT_IND, especially once
// we've modified the register allocator to not require all nodes to be assigned
// a register (e.g. a spilled lclVar can often be referenced directly from memory).
// Note that we may have a null op2, even with 2 sources, if op1 is a base/index memory op.
GenTree* delayUseOperand = op2;
if (node->OperIsCommutative())
{
if (op1->isContained() && op2 != nullptr)
{
delayUseOperand = op1;
}
else if (!op2->isContained() || op2->IsCnsIntOrI())
{
// If we have a commutative operator and op2 is not a memory op, we don't need
// to set delayRegFree on either operand because codegen can swap them.
delayUseOperand = nullptr;
}
}
else if (op1->isContained())
{
delayUseOperand = nullptr;
}
if (delayUseOperand != nullptr)
{
assert(!prefOp1 || delayUseOperand != op1);
assert(!prefOp2 || delayUseOperand != op2);
}
// Build first use
if (prefOp1)
{
assert(!op1->isContained());
tgtPrefUse = BuildUse(op1, op1Candidates);
srcCount++;
}
else if (delayUseOperand == op1)
{
srcCount += BuildDelayFreeUses(op1, op2, op1Candidates);
}
else
{
srcCount += BuildOperandUses(op1, op1Candidates);
}
// Build second use
if (op2 != nullptr)
{
if (prefOp2)
{
assert(!op2->isContained());
tgtPrefUse2 = BuildUse(op2, op2Candidates);
srcCount++;
}
else if (delayUseOperand == op2)
{
srcCount += BuildDelayFreeUses(op2, op1, op2Candidates);
}
else
{
srcCount += BuildOperandUses(op2, op2Candidates);
}
}
return srcCount;
}
//------------------------------------------------------------------------
// BuildShiftRotate: Set the NodeInfo for a shift or rotate.
//
// Arguments:
// tree - The node of interest
//
// Return Value:
// The number of sources consumed by this node.
//
int LinearScan::BuildShiftRotate(GenTree* tree)
{
// For shift operations, we need that the number
// of bits moved gets stored in CL in case
// the number of bits to shift is not a constant.
int srcCount = 0;
GenTree* shiftBy = tree->gtGetOp2();
GenTree* source = tree->gtGetOp1();
regMaskTP srcCandidates = RBM_NONE;
regMaskTP dstCandidates = RBM_NONE;
// x64 can encode 8 bits of shift and it will use 5 or 6. (the others are masked off)
// We will allow whatever can be encoded - hope you know what you are doing.
if (shiftBy->isContained())
{
assert(shiftBy->OperIsConst());
}
else
{
srcCandidates = allRegs(TYP_INT) & ~RBM_RCX;
dstCandidates = allRegs(TYP_INT) & ~RBM_RCX;
}
// Note that Rotate Left/Right instructions don't set ZF and SF flags.
//
// If the operand being shifted is 32-bits then upper three bits are masked
// by hardware to get actual shift count. Similarly for 64-bit operands
// shift count is narrowed to [0..63]. If the resulting shift count is zero,
// then shift operation won't modify flags.
//
// TODO-CQ-XARCH: We can optimize generating 'test' instruction for GT_EQ/NE(shift, 0)
// if the shift count is known to be non-zero and in the range depending on the
// operand size.
CLANG_FORMAT_COMMENT_ANCHOR;
#ifdef TARGET_X86
// The first operand of a GT_LSH_HI and GT_RSH_LO oper is a GT_LONG so that
// we can have a three operand form.
if (tree->OperGet() == GT_LSH_HI || tree->OperGet() == GT_RSH_LO)
{
assert((source->OperGet() == GT_LONG) && source->isContained());
GenTree* sourceLo = source->gtGetOp1();
GenTree* sourceHi = source->gtGetOp2();
assert(!sourceLo->isContained() && !sourceHi->isContained());
RefPosition* sourceLoUse = BuildUse(sourceLo, srcCandidates);
RefPosition* sourceHiUse = BuildUse(sourceHi, srcCandidates);
if (!tree->isContained())
{
if (tree->OperGet() == GT_LSH_HI)
{
setDelayFree(sourceLoUse);
}
else
{
setDelayFree(sourceHiUse);
}
}
}
else
#endif
if (!source->isContained())
{
tgtPrefUse = BuildUse(source, srcCandidates);
srcCount++;
}
else
{
srcCount += BuildOperandUses(source, srcCandidates);
}
if (!tree->isContained())
{
if (!shiftBy->isContained())
{
srcCount += BuildDelayFreeUses(shiftBy, source, RBM_RCX);
buildKillPositionsForNode(tree, currentLoc + 1, RBM_RCX);
}
BuildDef(tree, dstCandidates);
}
else
{
if (!shiftBy->isContained())
{
srcCount += BuildOperandUses(shiftBy, RBM_RCX);
buildKillPositionsForNode(tree, currentLoc + 1, RBM_RCX);