-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathsimdcodegenxarch.cpp
2433 lines (2158 loc) · 92.5 KB
/
simdcodegenxarch.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 Amd64 SIMD Code Generator XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#pragma warning(disable : 4310) // cast truncates constant value - happens for (int8_t)SHUFFLE_ZXXX
#endif
#ifdef TARGET_XARCH
#ifdef FEATURE_SIMD
#include "emit.h"
#include "codegen.h"
#include "sideeffects.h"
#include "lower.h"
#include "gcinfo.h"
#include "gcinfoencoder.h"
// Instruction immediates
// Insertps:
// - bits 6 and 7 of the immediate indicate which source item to select (0..3)
// - bits 4 and 5 of the immediate indicate which target item to insert into (0..3)
// - bits 0 to 3 of the immediate indicate which target item to zero
#define INSERTPS_SOURCE_SELECT(i) ((i) << 6)
#define INSERTPS_TARGET_SELECT(i) ((i) << 4)
#define INSERTPS_ZERO(i) (1 << (i))
// ROUNDPS/PD:
// - Bit 0 through 1 - Rounding mode
// * 0b00 - Round to nearest (even)
// * 0b01 - Round toward Neg. Infinity
// * 0b10 - Round toward Pos. Infinity
// * 0b11 - Round toward zero (Truncate)
// - Bit 2 - Source of rounding control, 0b0 for immediate.
// - Bit 3 - Precision exception, 0b1 to ignore. (We don't raise FP exceptions)
#define ROUNDPS_TO_NEAREST_IMM 0b1000
#define ROUNDPS_TOWARD_NEGATIVE_INFINITY_IMM 0b1001
#define ROUNDPS_TOWARD_POSITIVE_INFINITY_IMM 0b1010
#define ROUNDPS_TOWARD_ZERO_IMM 0b1011
// getOpForSIMDIntrinsic: return the opcode for the given SIMD Intrinsic
//
// Arguments:
// intrinsicId - SIMD intrinsic Id
// baseType - Base type of the SIMD vector
// ival - Out param. Any immediate byte operand that needs to be passed to SSE2 opcode
//
//
// Return Value:
// Instruction (op) to be used, and ival is set if instruction requires an immediate operand.
//
instruction CodeGen::getOpForSIMDIntrinsic(SIMDIntrinsicID intrinsicId, var_types baseType, unsigned* ival /*=nullptr*/)
{
// Minimal required instruction set is SSE2.
assert(compiler->getSIMDSupportLevel() >= SIMD_SSE2_Supported);
instruction result = INS_invalid;
switch (intrinsicId)
{
case SIMDIntrinsicInit:
if (compiler->getSIMDSupportLevel() == SIMD_AVX2_Supported)
{
// AVX supports broadcast instructions to populate YMM reg with a single float/double value from memory.
// AVX2 supports broadcast instructions to populate YMM reg with a single value from memory or mm reg.
switch (baseType)
{
case TYP_FLOAT:
result = INS_vbroadcastss;
break;
case TYP_DOUBLE:
result = INS_vbroadcastsd;
break;
case TYP_ULONG:
case TYP_LONG:
// NOTE: for x86, this instruction is valid if the src is xmm2/m64, but NOT if it is supposed
// to be TYP_LONG reg.
result = INS_vpbroadcastq;
break;
case TYP_UINT:
case TYP_INT:
result = INS_vpbroadcastd;
break;
case TYP_USHORT:
case TYP_SHORT:
result = INS_vpbroadcastw;
break;
case TYP_UBYTE:
case TYP_BYTE:
result = INS_vpbroadcastb;
break;
default:
unreached();
}
break;
}
// For SSE, SIMDIntrinsicInit uses the same instruction as the SIMDIntrinsicShuffleSSE2 intrinsic.
FALLTHROUGH;
case SIMDIntrinsicShuffleSSE2:
if (baseType == TYP_FLOAT)
{
result = INS_shufps;
}
else if (baseType == TYP_DOUBLE)
{
result = INS_shufpd;
}
else if (baseType == TYP_INT || baseType == TYP_UINT)
{
result = INS_pshufd;
}
else if (baseType == TYP_LONG || baseType == TYP_ULONG)
{
// We don't have a separate SSE2 instruction and will
// use the instruction meant for doubles since it is
// of the same size as a long.
result = INS_shufpd;
}
break;
case SIMDIntrinsicSub:
if (baseType == TYP_FLOAT)
{
result = INS_subps;
}
else if (baseType == TYP_DOUBLE)
{
result = INS_subpd;
}
else if (baseType == TYP_INT || baseType == TYP_UINT)
{
result = INS_psubd;
}
else if (baseType == TYP_USHORT || baseType == TYP_SHORT)
{
result = INS_psubw;
}
else if (baseType == TYP_UBYTE || baseType == TYP_BYTE)
{
result = INS_psubb;
}
else if (baseType == TYP_LONG || baseType == TYP_ULONG)
{
result = INS_psubq;
}
break;
case SIMDIntrinsicEqual:
if (baseType == TYP_FLOAT)
{
result = INS_cmpps;
assert(ival != nullptr);
*ival = 0;
}
else if (baseType == TYP_DOUBLE)
{
result = INS_cmppd;
assert(ival != nullptr);
*ival = 0;
}
else if (baseType == TYP_INT || baseType == TYP_UINT)
{
result = INS_pcmpeqd;
}
else if (baseType == TYP_USHORT || baseType == TYP_SHORT)
{
result = INS_pcmpeqw;
}
else if (baseType == TYP_UBYTE || baseType == TYP_BYTE)
{
result = INS_pcmpeqb;
}
else if ((baseType == TYP_ULONG || baseType == TYP_LONG) &&
(compiler->getSIMDSupportLevel() >= SIMD_SSE4_Supported))
{
result = INS_pcmpeqq;
}
break;
case SIMDIntrinsicBitwiseAnd:
if (baseType == TYP_FLOAT)
{
result = INS_andps;
}
else if (baseType == TYP_DOUBLE)
{
result = INS_andpd;
}
else if (varTypeIsIntegral(baseType))
{
result = INS_pand;
}
break;
case SIMDIntrinsicBitwiseOr:
if (baseType == TYP_FLOAT)
{
result = INS_orps;
}
else if (baseType == TYP_DOUBLE)
{
result = INS_orpd;
}
else if (varTypeIsIntegral(baseType))
{
result = INS_por;
}
break;
case SIMDIntrinsicCast:
result = INS_movaps;
break;
case SIMDIntrinsicConvertToSingle:
result = INS_cvtdq2ps;
break;
case SIMDIntrinsicConvertToDouble:
assert(baseType == TYP_LONG);
result = INS_cvtsi2sd;
break;
case SIMDIntrinsicConvertToInt32:
assert(baseType == TYP_FLOAT);
result = INS_cvttps2dq;
break;
case SIMDIntrinsicConvertToInt64:
assert(baseType == TYP_DOUBLE);
result = INS_cvttsd2si;
break;
case SIMDIntrinsicNarrow:
// Note that for the integer types the caller must zero the upper bits of
// each source element, since the instructions saturate.
switch (baseType)
{
case TYP_INT:
case TYP_UINT:
if (compiler->getSIMDSupportLevel() >= SIMD_SSE4_Supported)
{
result = INS_packusdw;
}
else
{
result = INS_packssdw;
}
break;
case TYP_SHORT:
case TYP_USHORT:
result = INS_packuswb;
break;
default:
assert(!"Invalid baseType for SIMDIntrinsicNarrow");
result = INS_invalid;
break;
}
break;
case SIMDIntrinsicWidenLo:
// Some of these have multiple instruction implementations, with one instruction to widen the lo half,
// and another to widen the hi half.
switch (baseType)
{
case TYP_FLOAT:
result = INS_cvtps2pd;
break;
case TYP_INT:
case TYP_UINT:
result = INS_punpckldq;
break;
case TYP_SHORT:
case TYP_USHORT:
result = INS_punpcklwd;
break;
case TYP_BYTE:
case TYP_UBYTE:
result = INS_punpcklbw;
break;
default:
assert(!"Invalid baseType for SIMDIntrinsicWidenLo");
result = INS_invalid;
break;
}
break;
case SIMDIntrinsicWidenHi:
switch (baseType)
{
case TYP_FLOAT:
// For this case, we actually use the same instruction.
result = INS_cvtps2pd;
break;
case TYP_INT:
case TYP_UINT:
result = INS_punpckhdq;
break;
case TYP_SHORT:
case TYP_USHORT:
result = INS_punpckhwd;
break;
case TYP_BYTE:
case TYP_UBYTE:
result = INS_punpckhbw;
break;
default:
assert(!"Invalid baseType for SIMDIntrinsicWidenHi");
result = INS_invalid;
break;
}
break;
case SIMDIntrinsicShiftLeftInternal:
switch (baseType)
{
case TYP_SIMD16:
// For SSE2, entire vector is shifted, for AVX2, 16-byte chunks are shifted.
result = INS_pslldq;
break;
case TYP_UINT:
case TYP_INT:
result = INS_pslld;
break;
case TYP_SHORT:
case TYP_USHORT:
result = INS_psllw;
break;
default:
assert(!"Invalid baseType for SIMDIntrinsicShiftLeftInternal");
result = INS_invalid;
break;
}
break;
case SIMDIntrinsicShiftRightInternal:
switch (baseType)
{
case TYP_SIMD16:
// For SSE2, entire vector is shifted, for AVX2, 16-byte chunks are shifted.
result = INS_psrldq;
break;
case TYP_UINT:
case TYP_INT:
result = INS_psrld;
break;
case TYP_SHORT:
case TYP_USHORT:
result = INS_psrlw;
break;
default:
assert(!"Invalid baseType for SIMDIntrinsicShiftRightInternal");
result = INS_invalid;
break;
}
break;
case SIMDIntrinsicUpperSave:
result = INS_vextractf128;
break;
case SIMDIntrinsicUpperRestore:
result = INS_insertps;
break;
default:
assert(!"Unsupported SIMD intrinsic");
unreached();
}
noway_assert(result != INS_invalid);
return result;
}
// genSIMDScalarMove: Generate code to move a value of type "type" from src mm reg
// to target mm reg, zeroing out the upper bits if and only if specified.
//
// Arguments:
// targetType the target type
// baseType the base type of value to be moved
// targetReg the target reg
// srcReg the src reg
// moveType action to be performed on target upper bits
//
// Return Value:
// None
//
// Notes:
// This is currently only supported for floating point types.
//
void CodeGen::genSIMDScalarMove(
var_types targetType, var_types baseType, regNumber targetReg, regNumber srcReg, SIMDScalarMoveType moveType)
{
assert(varTypeIsFloating(baseType));
switch (moveType)
{
case SMT_PreserveUpper:
if (srcReg != targetReg)
{
instruction ins = ins_Store(baseType);
if (GetEmitter()->IsDstSrcSrcAVXInstruction(ins))
{
// In general, when we use a three-operands move instruction, we want to merge the src with
// itself. This is an exception in that we actually want the "merge" behavior, so we must
// specify it with all 3 operands.
inst_RV_RV_RV(ins, targetReg, targetReg, srcReg, emitTypeSize(baseType));
}
else
{
inst_RV_RV(ins, targetReg, srcReg, baseType, emitTypeSize(baseType));
}
}
break;
case SMT_ZeroInitUpper:
if (compiler->canUseVexEncoding())
{
// insertps is a 128-bit only instruction, and clears the upper 128 bits, which is what we want.
// The insertpsImm selects which fields are copied and zero'd of the lower 128 bits, so we choose
// to zero all but the lower bits.
unsigned int insertpsImm =
(INSERTPS_TARGET_SELECT(0) | INSERTPS_ZERO(1) | INSERTPS_ZERO(2) | INSERTPS_ZERO(3));
assert((insertpsImm >= 0) && (insertpsImm <= 255));
inst_RV_RV_IV(INS_insertps, EA_16BYTE, targetReg, srcReg, (int8_t)insertpsImm);
}
else
{
if (srcReg == targetReg)
{
// There is no guarantee that upper bits of op1Reg are zero.
// We achieve this by using left logical shift 12-bytes and right logical shift 12 bytes.
instruction ins = getOpForSIMDIntrinsic(SIMDIntrinsicShiftLeftInternal, TYP_SIMD16);
GetEmitter()->emitIns_R_I(ins, EA_16BYTE, srcReg, 12);
ins = getOpForSIMDIntrinsic(SIMDIntrinsicShiftRightInternal, TYP_SIMD16);
GetEmitter()->emitIns_R_I(ins, EA_16BYTE, srcReg, 12);
}
else
{
genSIMDZero(targetType, TYP_FLOAT, targetReg);
inst_RV_RV(ins_Store(baseType), targetReg, srcReg);
}
}
break;
case SMT_ZeroInitUpper_SrcHasUpperZeros:
if (srcReg != targetReg)
{
instruction ins = ins_Copy(baseType);
assert(!GetEmitter()->IsDstSrcSrcAVXInstruction(ins));
inst_RV_RV(ins, targetReg, srcReg, baseType, emitTypeSize(baseType));
}
break;
default:
unreached();
}
}
void CodeGen::genSIMDZero(var_types targetType, var_types baseType, regNumber targetReg)
{
// We just use `INS_xorps` since `genSIMDZero` is used for both `System.Numerics.Vectors` and
// HardwareIntrinsics. Modern CPUs handle this specially in the renamer and it never hits the
// execution pipeline, additionally `INS_xorps` is always available (when using either the
// legacy or VEX encoding).
inst_RV_RV(INS_xorps, targetReg, targetReg, targetType, emitActualTypeSize(targetType));
}
//------------------------------------------------------------------------
// genSIMDIntrinsicInit: Generate code for SIMD Intrinsic Initialize.
//
// Arguments:
// simdNode - The GT_SIMD node
//
// Return Value:
// None.
//
void CodeGen::genSIMDIntrinsicInit(GenTreeSIMD* simdNode)
{
assert(simdNode->gtSIMDIntrinsicID == SIMDIntrinsicInit);
GenTree* op1 = simdNode->gtGetOp1();
var_types baseType = simdNode->GetSimdBaseType();
regNumber targetReg = simdNode->GetRegNum();
assert(targetReg != REG_NA);
var_types targetType = simdNode->TypeGet();
SIMDLevel level = compiler->getSIMDSupportLevel();
unsigned size = simdNode->GetSimdSize();
// Should never see small int base type vectors except for zero initialization.
noway_assert(!varTypeIsSmallInt(baseType) || op1->IsIntegralConst(0));
instruction ins = INS_invalid;
#if !defined(TARGET_64BIT)
if (op1->OperGet() == GT_LONG)
{
assert(varTypeIsLong(baseType));
GenTree* op1lo = op1->gtGetOp1();
GenTree* op1hi = op1->gtGetOp2();
if (op1lo->IsIntegralConst(0) && op1hi->IsIntegralConst(0))
{
genSIMDZero(targetType, baseType, targetReg);
}
else if (op1lo->IsIntegralConst(-1) && op1hi->IsIntegralConst(-1))
{
// Initialize elements of vector with all 1's: generate pcmpeqd reg, reg.
ins = getOpForSIMDIntrinsic(SIMDIntrinsicEqual, TYP_INT);
inst_RV_RV(ins, targetReg, targetReg, targetType, emitActualTypeSize(targetType));
}
else
{
// Generate:
// mov_i2xmm targetReg, op1lo
// mov_i2xmm xmmtmp, op1hi
// shl xmmtmp, 4 bytes
// por targetReg, xmmtmp
// Now, targetReg has the long in the low 64 bits. For SSE2, move it to the high 64 bits using:
// shufpd targetReg, targetReg, 0 // move the long to all the lanes
// For AVX2, move it to all 4 of the 64-bit lanes using:
// vpbroadcastq targetReg, targetReg
regNumber op1loReg = genConsumeReg(op1lo);
inst_RV_RV(ins_Copy(op1loReg, TYP_FLOAT), targetReg, op1loReg, TYP_INT);
regNumber tmpReg = simdNode->GetSingleTempReg();
regNumber op1hiReg = genConsumeReg(op1hi);
inst_RV_RV(ins_Copy(op1loReg, TYP_FLOAT), tmpReg, op1hiReg, TYP_INT);
ins = getOpForSIMDIntrinsic(SIMDIntrinsicShiftLeftInternal, TYP_SIMD16);
GetEmitter()->emitIns_R_I(ins, EA_16BYTE, tmpReg, 4); // shift left by 4 bytes
ins = getOpForSIMDIntrinsic(SIMDIntrinsicBitwiseOr, baseType);
inst_RV_RV(ins, targetReg, tmpReg, targetType, emitActualTypeSize(targetType));
if (compiler->getSIMDSupportLevel() == SIMD_AVX2_Supported)
{
inst_RV_RV(INS_vpbroadcastq, targetReg, targetReg, TYP_SIMD32, emitTypeSize(TYP_SIMD32));
}
else
{
ins = getOpForSIMDIntrinsic(SIMDIntrinsicShuffleSSE2, baseType);
GetEmitter()->emitIns_R_R_I(ins, emitActualTypeSize(targetType), targetReg, targetReg, 0);
}
}
}
else
#endif // !defined(TARGET_64BIT)
if (op1->isContained())
{
if (op1->IsIntegralConst(0) || op1->IsFPZero())
{
genSIMDZero(targetType, baseType, targetReg);
}
else if (varTypeIsIntegral(baseType) && op1->IsIntegralConst(-1))
{
// case of initializing elements of vector with all 1's
// generate pcmpeqd reg, reg
ins = getOpForSIMDIntrinsic(SIMDIntrinsicEqual, TYP_INT);
inst_RV_RV(ins, targetReg, targetReg, targetType, emitActualTypeSize(targetType));
}
else
{
assert(level == SIMD_AVX2_Supported);
ins = getOpForSIMDIntrinsic(SIMDIntrinsicInit, baseType);
if (op1->IsCnsFltOrDbl())
{
GetEmitter()->emitInsBinary(ins, emitTypeSize(targetType), simdNode, op1);
}
else if (op1->OperIsLocalAddr())
{
const GenTreeLclVarCommon* lclVar = op1->AsLclVarCommon();
unsigned offset = lclVar->GetLclOffs();
GetEmitter()->emitIns_R_S(ins, emitTypeSize(targetType), targetReg, lclVar->GetLclNum(), offset);
}
else
{
unreached();
}
}
}
else if (level == SIMD_AVX2_Supported && ((size == 32) || (size == 16)))
{
regNumber srcReg = genConsumeReg(op1);
if (baseType == TYP_INT || baseType == TYP_UINT || baseType == TYP_LONG || baseType == TYP_ULONG)
{
inst_RV_RV(ins_Copy(srcReg, TYP_FLOAT), targetReg, srcReg, baseType, emitTypeSize(baseType));
srcReg = targetReg;
}
ins = getOpForSIMDIntrinsic(simdNode->gtSIMDIntrinsicID, baseType);
GetEmitter()->emitIns_R_R(ins, emitActualTypeSize(targetType), targetReg, srcReg);
}
else
{
// If we reach here, op1 is not contained and we are using SSE or it is a SubRegisterSIMDType.
// In either case we are going to use the SSE2 shuffle instruction.
regNumber op1Reg = genConsumeReg(op1);
unsigned shuffleControl = 0;
if (compiler->isSubRegisterSIMDType(simdNode))
{
assert(baseType == TYP_FLOAT);
// We cannot assume that upper bits of op1Reg or targetReg be zero.
// Therefore we need to explicitly zero out upper bits. This is
// essential for the shuffle operation performed below.
//
// If op1 is a float/double constant, we would have loaded it from
// data section using movss/sd. Similarly if op1 is a memory op we
// would have loaded it using movss/sd. Movss/sd when loading a xmm reg
// from memory would zero-out upper bits. In these cases we can
// avoid explicitly zero'ing out targetReg if targetReg and op1Reg are the same or do it more efficiently
// if they are not the same.
SIMDScalarMoveType moveType =
op1->IsCnsFltOrDbl() || op1->isMemoryOp() ? SMT_ZeroInitUpper_SrcHasUpperZeros : SMT_ZeroInitUpper;
genSIMDScalarMove(targetType, TYP_FLOAT, targetReg, op1Reg, moveType);
if (size == 8)
{
shuffleControl = 0x50;
}
else if (size == 12)
{
shuffleControl = 0x40;
}
else
{
noway_assert(!"Unexpected size for SIMD type");
}
}
else // Vector<T>
{
if (op1Reg != targetReg)
{
inst_RV_RV(ins_Copy(op1Reg, TYP_FLOAT), targetReg, op1Reg, baseType, emitTypeSize(baseType));
}
}
ins = getOpForSIMDIntrinsic(SIMDIntrinsicShuffleSSE2, baseType);
assert((shuffleControl >= 0) && (shuffleControl <= 255));
GetEmitter()->emitIns_R_R_I(ins, emitActualTypeSize(targetType), targetReg, targetReg, (int8_t)shuffleControl);
}
genProduceReg(simdNode);
}
//-------------------------------------------------------------------------------------------
// genSIMDIntrinsicInitN: Generate code for SIMD Intrinsic Initialize for the form that takes
// a number of arguments equal to the length of the Vector.
//
// Arguments:
// simdNode - The GT_SIMD node
//
// Return Value:
// None.
//
void CodeGen::genSIMDIntrinsicInitN(GenTreeSIMD* simdNode)
{
assert(simdNode->gtSIMDIntrinsicID == SIMDIntrinsicInitN);
// Right now this intrinsic is supported only on TYP_FLOAT vectors
var_types baseType = simdNode->GetSimdBaseType();
noway_assert(baseType == TYP_FLOAT);
regNumber targetReg = simdNode->GetRegNum();
assert(targetReg != REG_NA);
var_types targetType = simdNode->TypeGet();
// Note that we cannot use targetReg before consumed all source operands. Therefore,
// Need an internal register to stitch together all the values into a single vector
// in an XMM reg.
regNumber vectorReg = simdNode->GetSingleTempReg();
// Zero out vectorReg if we are constructing a vector whose size is not equal to targetType vector size.
// For example in case of Vector4f we don't need to zero when using SSE2.
if (compiler->isSubRegisterSIMDType(simdNode))
{
genSIMDZero(targetType, baseType, vectorReg);
}
unsigned int baseTypeSize = genTypeSize(baseType);
instruction insLeftShift = getOpForSIMDIntrinsic(SIMDIntrinsicShiftLeftInternal, TYP_SIMD16);
// We will first consume the list items in execution (left to right) order,
// and record the registers.
regNumber operandRegs[SIMD_INTRINSIC_MAX_PARAM_COUNT];
unsigned initCount = 0;
for (GenTree* list = simdNode->gtGetOp1(); list != nullptr; list = list->gtGetOp2())
{
assert(list->OperGet() == GT_LIST);
GenTree* listItem = list->gtGetOp1();
assert(listItem->TypeGet() == baseType);
assert(!listItem->isContained());
regNumber operandReg = genConsumeReg(listItem);
operandRegs[initCount] = operandReg;
initCount++;
}
unsigned int offset = 0;
for (unsigned i = 0; i < initCount; i++)
{
// We will now construct the vector from the list items in reverse order.
// This allows us to efficiently stitch together a vector as follows:
// vectorReg = (vectorReg << offset)
// VectorReg[0] = listItemReg
// Use genSIMDScalarMove with SMT_PreserveUpper in order to ensure that the upper
// bits of vectorReg are not modified.
regNumber operandReg = operandRegs[initCount - i - 1];
if (offset != 0)
{
assert((baseTypeSize >= 0) && (baseTypeSize <= 255));
GetEmitter()->emitIns_R_I(insLeftShift, EA_16BYTE, vectorReg, (int8_t)baseTypeSize);
}
genSIMDScalarMove(targetType, baseType, vectorReg, operandReg, SMT_PreserveUpper);
offset += baseTypeSize;
}
noway_assert(offset == simdNode->GetSimdSize());
// Load the initialized value.
if (targetReg != vectorReg)
{
inst_RV_RV(ins_Copy(targetType), targetReg, vectorReg, targetType, emitActualTypeSize(targetType));
}
genProduceReg(simdNode);
}
//----------------------------------------------------------------------------------
// genSIMDIntrinsicUnOp: Generate code for SIMD Intrinsic unary operations like sqrt.
//
// Arguments:
// simdNode - The GT_SIMD node
//
// Return Value:
// None.
//
void CodeGen::genSIMDIntrinsicUnOp(GenTreeSIMD* simdNode)
{
assert(simdNode->gtSIMDIntrinsicID == SIMDIntrinsicCast);
GenTree* op1 = simdNode->gtGetOp1();
var_types baseType = simdNode->GetSimdBaseType();
regNumber targetReg = simdNode->GetRegNum();
assert(targetReg != REG_NA);
var_types targetType = simdNode->TypeGet();
regNumber op1Reg = genConsumeReg(op1);
instruction ins = getOpForSIMDIntrinsic(simdNode->gtSIMDIntrinsicID, baseType);
if (simdNode->gtSIMDIntrinsicID != SIMDIntrinsicCast || targetReg != op1Reg)
{
inst_RV_RV(ins, targetReg, op1Reg, targetType, emitActualTypeSize(targetType));
}
genProduceReg(simdNode);
}
//----------------------------------------------------------------------------------
// genSIMDIntrinsic32BitConvert: Generate code for 32-bit SIMD Convert (int/uint <-> float)
//
// Arguments:
// simdNode - The GT_SIMD node
//
// Return Value:
// None.
//
void CodeGen::genSIMDIntrinsic32BitConvert(GenTreeSIMD* simdNode)
{
SIMDIntrinsicID intrinsicID = simdNode->gtSIMDIntrinsicID;
assert((intrinsicID == SIMDIntrinsicConvertToSingle) || (intrinsicID == SIMDIntrinsicConvertToInt32));
GenTree* op1 = simdNode->gtGetOp1();
var_types baseType = simdNode->GetSimdBaseType();
regNumber targetReg = simdNode->GetRegNum();
assert(targetReg != REG_NA);
var_types targetType = simdNode->TypeGet();
regNumber op1Reg = genConsumeReg(op1);
instruction ins = getOpForSIMDIntrinsic(simdNode->gtSIMDIntrinsicID, baseType);
if (intrinsicID == SIMDIntrinsicConvertToSingle && baseType == TYP_UINT)
{
regNumber tmpIntReg = simdNode->GetSingleTempReg(RBM_ALLINT);
regNumber tmpReg = simdNode->ExtractTempReg(RBM_ALLFLOAT);
regNumber tmpReg2 = simdNode->GetSingleTempReg(RBM_ALLFLOAT);
assert(tmpReg != op1Reg && tmpReg2 != op1Reg);
// We will generate the following:
// vmovdqu tmpReg2, op1Reg (copy the src and put it into tmpReg2)
// vmovdqu targetReg, op1Reg (copy the src and put it into targetReg)
// vpsrld targetReg, 16 (get upper 16 bits of src and put it into targetReg)
// vpslld tmpReg2, 16
// vpsrld tmpReg2, 16 (get lower 16 bits of src and put it into tmpReg2)
// mov tmpIntReg, 0x5300000053000000
// vmovd tmpReg, tmpIntReg
// vpbroadcastd tmpReg, tmpReg (build mask for converting upper 16 bits of src)
// vorps targetReg, tmpReg
// vsubps targetReg, tmpReg (convert upper 16 bits of src and put it into targetReg)
// vcvtdq2ps tmpReg2, tmpReg2 (convert lower 16 bits of src and put it into tmpReg2)
// vaddps targetReg, tmpReg2 (add upper 16 bits and lower 16 bits)
inst_RV_RV(INS_movdqu, tmpReg2, op1Reg, baseType, emitActualTypeSize(targetType));
if (targetReg != op1Reg)
{
inst_RV_RV(INS_movdqu, targetReg, op1Reg, baseType, emitActualTypeSize(targetType));
}
// prepare upper 16 bits
GetEmitter()->emitIns_R_I(INS_psrld, emitActualTypeSize(targetType), targetReg, 16);
// prepare lower 16 bits
GetEmitter()->emitIns_R_I(INS_pslld, emitActualTypeSize(targetType), tmpReg2, 16);
GetEmitter()->emitIns_R_I(INS_psrld, emitActualTypeSize(targetType), tmpReg2, 16);
// prepare mask
#ifdef TARGET_AMD64
GetEmitter()->emitIns_R_I(INS_mov, EA_8BYTE, tmpIntReg, (ssize_t)0X5300000053000000);
inst_RV_RV(INS_movd, tmpReg, tmpIntReg, TYP_ULONG);
#else
if (compiler->getSIMDSupportLevel() == SIMD_AVX2_Supported)
{
GetEmitter()->emitIns_R_I(INS_mov, EA_4BYTE, tmpIntReg, (ssize_t)0X53000000);
inst_RV_RV(INS_movd, tmpReg, tmpIntReg, TYP_UINT);
}
else
{
GetEmitter()->emitIns_R_I(INS_mov, EA_4BYTE, tmpIntReg, (ssize_t)0X00005300);
inst_RV_RV(INS_pxor, tmpReg, tmpReg, targetType, emitActualTypeSize(targetType));
GetEmitter()->emitIns_R_R_I(INS_pinsrw, emitTypeSize(TYP_INT), tmpReg, tmpIntReg, 1);
GetEmitter()->emitIns_R_R_I(INS_pinsrw, emitTypeSize(TYP_INT), tmpReg, tmpIntReg, 3);
}
#endif
if (compiler->getSIMDSupportLevel() == SIMD_AVX2_Supported)
{
inst_RV_RV(INS_vpbroadcastd, tmpReg, tmpReg, targetType, emitActualTypeSize(targetType));
}
else
{
inst_RV_RV(INS_movlhps, tmpReg, tmpReg, targetType, emitActualTypeSize(targetType));
}
// convert upper 16 bits
inst_RV_RV(INS_orps, targetReg, tmpReg, targetType, emitActualTypeSize(targetType));
inst_RV_RV(INS_subps, targetReg, tmpReg, targetType, emitActualTypeSize(targetType));
// convert lower 16 bits
inst_RV_RV(ins, tmpReg2, tmpReg2, targetType, emitActualTypeSize(targetType));
// add lower 16 bits and upper 16 bits
inst_RV_RV(INS_addps, targetReg, tmpReg2, targetType, emitActualTypeSize(targetType));
}
else
{
inst_RV_RV(ins, targetReg, op1Reg, targetType, emitActualTypeSize(targetType));
}
genProduceReg(simdNode);
}
//----------------------------------------------------------------------------------
// genSIMDLo64BitConvert: Generate code to convert lower-most 64-bit item (long <--> double)
//
// Arguments:
// intrinsicID the SIMD intrinsic ID
// simdType the SIMD node type
// baseType the base type of value to be converted
// tmpReg the tmp reg
// tmpIntReg the tmp integer reg
// targetReg the target reg
//
// Return Value:
// None.
//
void CodeGen::genSIMDLo64BitConvert(SIMDIntrinsicID intrinsicID,
var_types simdType,
var_types baseType,
regNumber tmpReg,
regNumber tmpIntReg,
regNumber targetReg)
{
instruction ins = getOpForSIMDIntrinsic(intrinsicID, baseType);
if (intrinsicID == SIMDIntrinsicConvertToDouble)
{
inst_RV_RV(INS_movd, tmpIntReg, tmpReg, TYP_LONG);
inst_RV_RV(ins, targetReg, tmpIntReg, baseType, emitActualTypeSize(baseType));
}
else
{
inst_RV_RV(ins, tmpIntReg, tmpReg, baseType, emitActualTypeSize(baseType));
inst_RV_RV(INS_movd, targetReg, tmpIntReg, TYP_LONG);
}
}
//----------------------------------------------------------------------------------
// genSIMDIntrinsic64BitConvert: Generate code for 64-bit SIMD Convert (long/ulong <-> double)
//
// Arguments:
// simdNode - The GT_SIMD node
//
// Notes:
// There are no instructions for converting to/from 64-bit integers, so for these we
// do the conversion an element at a time.
//
void CodeGen::genSIMDIntrinsic64BitConvert(GenTreeSIMD* simdNode)
{
SIMDIntrinsicID intrinsicID = simdNode->gtSIMDIntrinsicID;
assert((intrinsicID == SIMDIntrinsicConvertToDouble) || (intrinsicID == SIMDIntrinsicConvertToInt64));
GenTree* op1 = simdNode->gtGetOp1();
var_types baseType = simdNode->GetSimdBaseType();
regNumber targetReg = simdNode->GetRegNum();
assert(targetReg != REG_NA);
var_types simdType = simdNode->TypeGet();
regNumber op1Reg = genConsumeReg(op1);
regNumber tmpIntReg = simdNode->GetSingleTempReg(RBM_ALLINT);
regNumber tmpReg;
regNumber tmpReg2;
regNumber tmpReg3;
SIMDLevel level = compiler->getSIMDSupportLevel();
#ifdef TARGET_X86
if (baseType == TYP_LONG)
{
tmpReg = simdNode->ExtractTempReg(RBM_ALLFLOAT);
tmpReg2 = simdNode->ExtractTempReg(RBM_ALLFLOAT);
tmpReg3 = simdNode->GetSingleTempReg(RBM_ALLFLOAT);
assert(tmpReg != op1Reg && tmpReg2 != op1Reg && tmpReg3 != op1Reg);
}
else
#endif
if (level == SIMD_AVX2_Supported || (baseType == TYP_ULONG))
{
tmpReg = simdNode->ExtractTempReg(RBM_ALLFLOAT);
tmpReg2 = simdNode->GetSingleTempReg(RBM_ALLFLOAT);
tmpReg3 = REG_NA;
assert(tmpReg != op1Reg && tmpReg2 != op1Reg);
}
else
{
tmpReg = simdNode->GetSingleTempReg(RBM_ALLFLOAT);
assert(tmpReg != op1Reg);
tmpReg2 = REG_NA;
tmpReg3 = REG_NA;
}
if ((intrinsicID == SIMDIntrinsicConvertToDouble) && (baseType == TYP_ULONG))
{
// We will generate the following
// vmovdqu tmpReg2, op1Reg (copy the src and put it into tmpReg2)
// vmovdqu targetReg, op1Reg (copy the src and put it into targetReg)
// vpsrlq targetReg, 32 (get upper 32 bits of src and put it into targetReg)
// vpsllq tmpReg2, 32
// vpsrlq tmpReg2, 32 (get lower 32 bits of src and put it into tmpReg2)
// mov tmpIntReg, 0x4530000000000000
// vmovd tmpReg, tmpIntReg
// vpbroadcastq tmpReg, tmpReg (build mask for upper 32 bits of src)
// vorpd targetReg, tmpReg
// vsubpd targetReg, tmpReg (convert upper 32 bits of src and put it into targetReg)
// mov tmpIntReg, 0x4330000000000000
// vmovd tmpReg, tmpIntReg
// vpbroadcastq tmpReg, tmpReg (build mask for lower 32 bits of src)
// vorpd tmpReg2, tmpReg
// vsubpd tmpReg2, tmpReg (convert lower 32 bits of src and put it into tmpReg2)
// vaddpd targetReg, tmpReg2 (add upper 32 bits and lower 32 bits together)
inst_RV_RV(INS_movdqu, tmpReg2, op1Reg, baseType, emitActualTypeSize(simdType));
if (targetReg != op1Reg)
{
inst_RV_RV(INS_movdqu, targetReg, op1Reg, baseType, emitActualTypeSize(simdType));
}
// prepare upper 32 bits
GetEmitter()->emitIns_R_I(INS_psrlq, emitActualTypeSize(simdType), targetReg, 32);
// prepare lower 32 bits
GetEmitter()->emitIns_R_I(INS_psllq, emitActualTypeSize(simdType), tmpReg2, 32);
GetEmitter()->emitIns_R_I(INS_psrlq, emitActualTypeSize(simdType), tmpReg2, 32);
// prepare mask for converting upper 32 bits
#ifdef TARGET_AMD64
GetEmitter()->emitIns_R_I(INS_mov, EA_8BYTE, tmpIntReg, (ssize_t)0X4530000000000000);
inst_RV_RV(INS_movd, tmpReg, tmpIntReg, TYP_ULONG);
#else
GetEmitter()->emitIns_R_I(INS_mov, EA_4BYTE, tmpIntReg, (ssize_t)0X45300000);
inst_RV_RV(INS_movd, tmpReg, tmpIntReg, TYP_UINT);
GetEmitter()->emitIns_R_I(INS_pslldq, EA_16BYTE, tmpReg, 4);
#endif
if (level == SIMD_AVX2_Supported)