-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathlsraxarch.cpp
3139 lines (2767 loc) · 106 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 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;
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 (!varTypeUsesIntReg(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
if (tree->TypeIs(TYP_SIMD12) && tree->OperIs(GT_STORE_LCL_FLD))
{
if (!tree->AsLclFld()->Data()->IsVectorZero())
{
// GT_STORE_LCL_FLD needs an internal register, when the
// data is not zero, so the upper 4 bytes can be extracted
buildInternalFloatRegisterDefForNode(tree);
buildInternalRegisterUses();
}
}
#endif // FEATURE_SIMD
BuildDef(tree);
}
break;
case GT_STORE_LCL_FLD:
case GT_STORE_LCL_VAR:
if (tree->IsMultiRegLclVar() && isCandidateMultiRegLclVar(tree->AsLclVar()))
{
dstCount = compiler->lvaGetDesc(tree->AsLclVar())->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_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);
BuildKills(tree, RBM_NONE);
break;
case GT_PROF_HOOK:
srcCount = 0;
assert(dstCount == 0);
killMask = getKillSetForProfilerHook();
BuildKills(tree, killMask);
break;
case GT_CNS_INT:
case GT_CNS_LNG:
case GT_CNS_DBL:
#if defined(FEATURE_SIMD)
case GT_CNS_VEC:
#endif // FEATURE_SIMD
#if defined(FEATURE_MASKED_HW_INTRINSICS)
case GT_CNS_MSK:
#endif // FEATURE_MASKED_HW_INTRINSICS
{
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();
BuildKills(tree, killMask);
break;
#ifdef SWIFT_SUPPORT
case GT_SWIFT_ERROR_RET:
BuildUse(tree->gtGetOp1(), RBM_SWIFT_ERROR.GetIntRegSet());
// Plus one for error register
srcCount = BuildReturn(tree) + 1;
killMask = getKillSetForReturn();
BuildKills(tree, killMask);
break;
#endif // SWIFT_SUPPORT
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.GetIntRegSet());
}
break;
case GT_NOP:
srcCount = 0;
assert(tree->TypeIs(TYP_VOID));
assert(dstCount == 0);
break;
case GT_KEEPALIVE:
assert(dstCount == 0);
srcCount = BuildOperandUses(tree->gtGetOp1());
break;
case GT_JTRUE:
BuildOperandUses(tree->gtGetOp1(), RBM_NONE);
srcCount = 1;
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_SELECT:
assert(dstCount == 1);
srcCount = BuildSelect(tree->AsConditional());
break;
case GT_SELECTCC:
assert(dstCount == 1);
srcCount = BuildSelect(tree->AsOp());
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;
#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_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);
BuildKills(tree, 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_HW_INTRINSICS
case GT_HWINTRINSIC:
srcCount = BuildHWIntrinsic(tree->AsHWIntrinsic(), &dstCount);
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_BITTEST_EQ:
case GT_BITTEST_NE:
case GT_CMP:
case GT_TEST:
case GT_BT:
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);
GenTree* addr = tree->AsCmpXchg()->Addr();
GenTree* data = tree->AsCmpXchg()->Data();
GenTree* comparand = tree->AsCmpXchg()->Comparand();
// Comparand is preferenced to RAX.
// The remaining two operands can be in any reg other than RAX.
SingleTypeRegSet nonRaxCandidates = availableIntRegs & ~SRBM_RAX;
BuildUse(addr, nonRaxCandidates);
BuildUse(data, varTypeIsByte(tree) ? (nonRaxCandidates & RBM_BYTE_REGS.GetIntRegSet()) : nonRaxCandidates);
BuildUse(comparand, SRBM_RAX);
BuildDef(tree, SRBM_RAX);
}
break;
case GT_XORR:
case GT_XAND:
if (!tree->IsUnusedValue())
{
GenTree* addr = tree->gtGetOp1();
GenTree* data = tree->gtGetOp2();
// These don't support byte operands.
assert(!varTypeIsByte(data));
// if tree's value is used, we'll emit a cmpxchg-loop idiom (requires RAX)
buildInternalIntRegisterDefForNode(tree, availableIntRegs & ~SRBM_RAX);
BuildUse(addr, availableIntRegs & ~SRBM_RAX);
BuildUse(data, availableIntRegs & ~SRBM_RAX);
BuildDef(tree, SRBM_RAX);
buildInternalRegisterUses();
srcCount = 2;
assert(dstCount == 1);
break;
}
FALLTHROUGH;
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, varTypeIsByte(tree) ? RBM_BYTE_REGS.GetIntRegSet() : RBM_NONE);
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_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:
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_BOUNDS_CHECK:
// Consumes arrLen & index - has no result
assert(dstCount == 0);
srcCount = BuildOperandUses(tree->AsBoundsChk()->GetIndex());
srcCount += BuildOperandUses(tree->AsBoundsChk()->GetArrayLength());
break;
case GT_ARR_ELEM:
// These must have been lowered
noway_assert(!"We should never see a GT_ARR_ELEM after Lowering.");
srcCount = 0;
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->AsStoreInd()))
{
srcCount = BuildGCWriteBarrier(tree);
break;
}
srcCount = BuildIndir(tree->AsIndir());
break;
case GT_NULLCHECK:
{
assert(dstCount == 0);
#ifdef TARGET_X86
if (varTypeIsByte(tree))
{
// on X86 we have to use byte-able regs for byte-wide loads
BuildUse(tree->gtGetOp1(), RBM_BYTE_REGS.GetIntRegSet());
srcCount = 1;
break;
}
#endif
// 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.GetIntRegSet());
break;
#if defined(FEATURE_EH_WINDOWS_X86)
case GT_END_LFIN:
srcCount = 0;
assert(dstCount == 0);
break;
#endif
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 perform 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;
#ifdef SWIFT_SUPPORT
case GT_SWIFT_ERROR:
srcCount = 0;
assert(dstCount == 1);
// Any register should do here, but the error register value should immediately
// be moved from GT_SWIFT_ERROR's destination register to the SwiftError struct,
// and we know REG_SWIFT_ERROR should be busy up to this point, anyway.
// By forcing LSRA to use REG_SWIFT_ERROR as both the source and destination register,
// we can ensure the redundant move is elided.
BuildDef(tree, RBM_SWIFT_ERROR.GetIntRegSet());
break;
#endif // SWIFT_SUPPORT
} // end switch (tree->OperGet())
// We need to be sure that we've set srcCount and dstCount appropriately.
assert((dstCount < 2) || tree->IsMultiRegNode());
assert(isLocalDefUse == (tree->IsValue() && tree->IsUnusedValue()));
assert(!tree->IsValue() || (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.
// op1 - its first operand
// op2 - its second operand
// 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(GenTree* tree, GenTree* op1, GenTree* op2, 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 (isRMWRegOper(tree))
{
// If we have a read-modify-write operation, we want to preference op1 to the target,
// if it is not contained.
if (!op1->isContained())
{
*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.
#ifdef FEATURE_HW_INTRINSICS
assert(tree->OperIsBinary() || (tree->OperIsMultiOp() && (tree->AsMultiOp()->GetOperandCount() <= 2)));
#else
assert(tree->OperIsBinary());
#endif
if (tree->OperIsCompare() || tree->OperIs(GT_CMP, GT_TEST, 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_STORE_BLK:
case GT_SWITCH_TABLE:
case GT_LOCKADD:
#ifdef TARGET_X86
case GT_LONG:
#endif
case GT_SWIFT_ERROR_RET:
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:
#ifdef TARGET_X86
case GT_SUB_HI:
case GT_LSH_HI:
#endif
{
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(GenTree* node, GenTree* op1, GenTree* op2, SingleTypeRegSet candidates)
{
int srcCount = 0;
SingleTypeRegSet op1Candidates = candidates;
SingleTypeRegSet op2Candidates = candidates;
#ifdef TARGET_X86
if (varTypeIsByte(node))
{
SingleTypeRegSet 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, op1, op2, &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;
}
//------------------------------------------------------------------------
// BuildSelect: Build RefPositions for a GT_SELECT/GT_SELECT_HI node.
//
// Arguments:
// select - The GT_SELECT/GT_SELECT_HI node
//
// Return Value:
// The number of sources consumed by this node.
//
int LinearScan::BuildSelect(GenTreeOp* select)
{
int srcCount = 0;
if (select->OperIs(GT_SELECT))
{
GenTree* cond = select->AsConditional()->gtCond;
BuildUse(cond);
srcCount++;
}
GenTree* trueVal = select->gtOp1;
GenTree* falseVal = select->gtOp2;
RefPositionIterator op1UsesPrev = refPositions.backPosition();
assert(op1UsesPrev != refPositions.end());
RefPosition* uncontainedTrueRP = nullptr;
if (trueVal->isContained())
{
srcCount += BuildOperandUses(trueVal);
}
else
{
tgtPrefUse = uncontainedTrueRP = BuildUse(trueVal);
srcCount++;
}
RefPositionIterator op2UsesPrev = refPositions.backPosition();
RefPosition* uncontainedFalseRP = nullptr;
if (falseVal->isContained())
{
srcCount += BuildOperandUses(falseVal);
}
else
{
tgtPrefUse2 = uncontainedFalseRP = BuildUse(falseVal);
srcCount++;
}
if ((tgtPrefUse != nullptr) && (tgtPrefUse2 != nullptr))
{
// CQ analysis shows that it's best to always prefer only the 'true'
// val here.
tgtPrefUse2 = nullptr;
}
// Codegen will emit something like:
//
// mov dstReg, falseVal
// cmov dstReg, trueVal
//
// We need to ensure that dstReg does not interfere with any register that
// appears in the second instruction. At the same time we want to
// preference the dstReg to be the same register as either falseVal/trueVal
// to be able to elide the mov whenever possible.
//
// While we could resolve the situation with either an internal register or
// by marking the uses as delay free unconditionally, this is a node used
// for very basic code patterns, so the logic here tries to be smarter to
// avoid the extra register pressure/potential copies.
//
// We have some flexibility as codegen can swap falseVal/trueVal as needed
// to avoid the conflict by reversing the sense of the cmov. If we can
// guarantee that the dstReg is used only in one of falseVal/trueVal, then
// we are good.
//
// To ensure the above we have some bespoke interference logic here on
// intervals for the ref positions we built above. It marks one of the uses
// as delay freed when it finds interference (almost never).
//
RefPositionIterator op1Use = op1UsesPrev;
while (op1Use != op2UsesPrev)
{
++op1Use;
if (op1Use->refType != RefTypeUse)
{
continue;
}
RefPositionIterator op2Use = op2UsesPrev;
++op2Use;
while (op2Use != refPositions.end())
{
if (op2Use->refType == RefTypeUse)
{
if (op1Use->getInterval() == op2Use->getInterval())
{
setDelayFree(&*op1Use);
break;
}
++op2Use;
}
}
}
// Certain FP conditions are special and require multiple cmovs. These may
// introduce additional uses of either trueVal or falseVal after the first
// mov. In these cases we need additional delay-free marking. We do not
// support any containment for these currently (we do not want to incur
// multiple memory accesses, but we could contain the operand in the 'mov'
// instruction with some more care taken for marking things delay reg freed
// correctly).