1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
8 // IMPORTANT NOTES AND CAVEATS:
10 // This implementation is preliminary, and may change dramatically.
12 // New JIT types, TYP_SIMDxx, are introduced, and the SIMD intrinsics are created as GT_SIMD nodes.
13 // Nodes of SIMD types will be typed as TYP_SIMD* (e.g. TYP_SIMD8, TYP_SIMD16, etc.).
15 // Note that currently the "reference implementation" is the same as the runtime dll. As such, it is currently
16 // providing implementations for those methods not currently supported by the JIT as intrinsics.
18 // These are currently recognized using string compares, in order to provide an implementation in the JIT
19 // without taking a dependency on the VM.
20 // Furthermore, in the CTP, in order to limit the impact of doing these string compares
21 // against assembly names, we only look for the SIMDVector assembly if we are compiling a class constructor. This
22 // makes it somewhat more "pay for play" but is a significant usability compromise.
23 // This has been addressed for RTM by doing the assembly recognition in the VM.
24 // --------------------------------------------------------------------------------------
35 // Intrinsic Id to intrinsic info map
36 const SIMDIntrinsicInfo simdIntrinsicInfoArray[] = {
37 #define SIMD_INTRINSIC(mname, inst, id, name, retType, argCount, arg1, arg2, arg3, t1, t2, t3, t4, t5, t6, t7, t8, t9, \
39 {SIMDIntrinsic##id, mname, inst, retType, argCount, arg1, arg2, arg3, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10},
40 #include "simdintrinsiclist.h"
43 //------------------------------------------------------------------------
44 // getSIMDVectorLength: Get the length (number of elements of base type) of
45 // SIMD Vector given its size and base (element) type.
48 // simdSize - size of the SIMD vector
49 // baseType - type of the elements of the SIMD vector
52 int Compiler::getSIMDVectorLength(unsigned simdSize, var_types baseType)
54 return simdSize / genTypeSize(baseType);
57 //------------------------------------------------------------------------
58 // Get the length (number of elements of base type) of SIMD Vector given by typeHnd.
61 // typeHnd - type handle of the SIMD vector
63 int Compiler::getSIMDVectorLength(CORINFO_CLASS_HANDLE typeHnd)
65 unsigned sizeBytes = 0;
66 var_types baseType = getBaseTypeAndSizeOfSIMDType(typeHnd, &sizeBytes);
67 return getSIMDVectorLength(sizeBytes, baseType);
70 //------------------------------------------------------------------------
71 // Get the preferred alignment of SIMD vector type for better performance.
74 // typeHnd - type handle of the SIMD vector
76 int Compiler::getSIMDTypeAlignment(var_types simdType)
79 // Fixed length vectors have the following alignment preference
80 // Vector2 = 8 byte alignment
81 // Vector3/4 = 16-byte alignment
82 unsigned size = genTypeSize(simdType);
84 // preferred alignment for SSE2 128-bit vectors is 16-bytes
91 assert((size == 12) || (size == 16));
99 #elif defined(_TARGET_ARM64_)
102 assert(!"getSIMDTypeAlignment() unimplemented on target arch");
107 //----------------------------------------------------------------------------------
108 // Return the base type and size of SIMD vector type given its type handle.
111 // typeHnd - The handle of the type we're interested in.
112 // sizeBytes - out param
115 // base type of SIMD vector.
116 // sizeBytes if non-null is set to size in bytes.
118 // TODO-Throughput: current implementation parses class name to find base type. Change
119 // this when we implement SIMD intrinsic identification for the final
122 var_types Compiler::getBaseTypeAndSizeOfSIMDType(CORINFO_CLASS_HANDLE typeHnd, unsigned* sizeBytes /*= nullptr */)
125 if (typeHnd == nullptr)
130 // fast path search using cached type handles of important types
131 var_types simdBaseType = TYP_UNKNOWN;
134 // TODO - Optimize SIMD type recognition by IntrinsicAttribute
135 if (isSIMDClass(typeHnd))
137 // The most likely to be used type handles are looked up first followed by
138 // less likely to be used type handles
139 if (typeHnd == SIMDFloatHandle)
141 simdBaseType = TYP_FLOAT;
142 JITDUMP(" Known type SIMD Vector<Float>\n");
144 else if (typeHnd == SIMDIntHandle)
146 simdBaseType = TYP_INT;
147 JITDUMP(" Known type SIMD Vector<Int>\n");
149 else if (typeHnd == SIMDVector2Handle)
151 simdBaseType = TYP_FLOAT;
152 size = 2 * genTypeSize(TYP_FLOAT);
153 assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
154 JITDUMP(" Known type Vector2\n");
156 else if (typeHnd == SIMDVector3Handle)
158 simdBaseType = TYP_FLOAT;
159 size = 3 * genTypeSize(TYP_FLOAT);
160 assert(size == info.compCompHnd->getClassSize(typeHnd));
161 JITDUMP(" Known type Vector3\n");
163 else if (typeHnd == SIMDVector4Handle)
165 simdBaseType = TYP_FLOAT;
166 size = 4 * genTypeSize(TYP_FLOAT);
167 assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
168 JITDUMP(" Known type Vector4\n");
170 else if (typeHnd == SIMDVectorHandle)
172 JITDUMP(" Known type Vector\n");
174 else if (typeHnd == SIMDUShortHandle)
176 simdBaseType = TYP_USHORT;
177 JITDUMP(" Known type SIMD Vector<ushort>\n");
179 else if (typeHnd == SIMDUByteHandle)
181 simdBaseType = TYP_UBYTE;
182 JITDUMP(" Known type SIMD Vector<ubyte>\n");
184 else if (typeHnd == SIMDDoubleHandle)
186 simdBaseType = TYP_DOUBLE;
187 JITDUMP(" Known type SIMD Vector<Double>\n");
189 else if (typeHnd == SIMDLongHandle)
191 simdBaseType = TYP_LONG;
192 JITDUMP(" Known type SIMD Vector<Long>\n");
194 else if (typeHnd == SIMDShortHandle)
196 simdBaseType = TYP_SHORT;
197 JITDUMP(" Known type SIMD Vector<short>\n");
199 else if (typeHnd == SIMDByteHandle)
201 simdBaseType = TYP_BYTE;
202 JITDUMP(" Known type SIMD Vector<byte>\n");
204 else if (typeHnd == SIMDUIntHandle)
206 simdBaseType = TYP_UINT;
207 JITDUMP(" Known type SIMD Vector<uint>\n");
209 else if (typeHnd == SIMDULongHandle)
211 simdBaseType = TYP_ULONG;
212 JITDUMP(" Known type SIMD Vector<ulong>\n");
216 if (simdBaseType == TYP_UNKNOWN)
218 // Doesn't match with any of the cached type handles.
219 // Obtain base type by parsing fully qualified class name.
221 // TODO-Throughput: implement product shipping solution to query base type.
222 WCHAR className[256] = {0};
223 WCHAR* pbuf = &className[0];
224 int len = _countof(className);
225 info.compCompHnd->appendClassName(&pbuf, &len, typeHnd, TRUE, FALSE, FALSE);
226 noway_assert(pbuf < &className[256]);
227 JITDUMP("SIMD Candidate Type %S\n", className);
229 if (wcsncmp(className, W("System.Numerics."), 16) == 0)
231 if (wcsncmp(&(className[16]), W("Vector`1["), 9) == 0)
233 if (wcsncmp(&(className[25]), W("System.Single"), 13) == 0)
235 SIMDFloatHandle = typeHnd;
236 simdBaseType = TYP_FLOAT;
237 JITDUMP(" Found type SIMD Vector<Float>\n");
239 else if (wcsncmp(&(className[25]), W("System.Int32"), 12) == 0)
241 SIMDIntHandle = typeHnd;
242 simdBaseType = TYP_INT;
243 JITDUMP(" Found type SIMD Vector<Int>\n");
245 else if (wcsncmp(&(className[25]), W("System.UInt16"), 13) == 0)
247 SIMDUShortHandle = typeHnd;
248 simdBaseType = TYP_USHORT;
249 JITDUMP(" Found type SIMD Vector<ushort>\n");
251 else if (wcsncmp(&(className[25]), W("System.Byte"), 11) == 0)
253 SIMDUByteHandle = typeHnd;
254 simdBaseType = TYP_UBYTE;
255 JITDUMP(" Found type SIMD Vector<ubyte>\n");
257 else if (wcsncmp(&(className[25]), W("System.Double"), 13) == 0)
259 SIMDDoubleHandle = typeHnd;
260 simdBaseType = TYP_DOUBLE;
261 JITDUMP(" Found type SIMD Vector<Double>\n");
263 else if (wcsncmp(&(className[25]), W("System.Int64"), 12) == 0)
265 SIMDLongHandle = typeHnd;
266 simdBaseType = TYP_LONG;
267 JITDUMP(" Found type SIMD Vector<Long>\n");
269 else if (wcsncmp(&(className[25]), W("System.Int16"), 12) == 0)
271 SIMDShortHandle = typeHnd;
272 simdBaseType = TYP_SHORT;
273 JITDUMP(" Found type SIMD Vector<short>\n");
275 else if (wcsncmp(&(className[25]), W("System.SByte"), 12) == 0)
277 SIMDByteHandle = typeHnd;
278 simdBaseType = TYP_BYTE;
279 JITDUMP(" Found type SIMD Vector<byte>\n");
281 else if (wcsncmp(&(className[25]), W("System.UInt32"), 13) == 0)
283 SIMDUIntHandle = typeHnd;
284 simdBaseType = TYP_UINT;
285 JITDUMP(" Found type SIMD Vector<uint>\n");
287 else if (wcsncmp(&(className[25]), W("System.UInt64"), 13) == 0)
289 SIMDULongHandle = typeHnd;
290 simdBaseType = TYP_ULONG;
291 JITDUMP(" Found type SIMD Vector<ulong>\n");
295 JITDUMP(" Unknown SIMD Vector<T>\n");
298 else if (wcsncmp(&(className[16]), W("Vector2"), 8) == 0)
300 SIMDVector2Handle = typeHnd;
302 simdBaseType = TYP_FLOAT;
303 size = 2 * genTypeSize(TYP_FLOAT);
304 assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
305 JITDUMP(" Found Vector2\n");
307 else if (wcsncmp(&(className[16]), W("Vector3"), 8) == 0)
309 SIMDVector3Handle = typeHnd;
311 simdBaseType = TYP_FLOAT;
312 size = 3 * genTypeSize(TYP_FLOAT);
313 assert(size == info.compCompHnd->getClassSize(typeHnd));
314 JITDUMP(" Found Vector3\n");
316 else if (wcsncmp(&(className[16]), W("Vector4"), 8) == 0)
318 SIMDVector4Handle = typeHnd;
320 simdBaseType = TYP_FLOAT;
321 size = 4 * genTypeSize(TYP_FLOAT);
322 assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
323 JITDUMP(" Found Vector4\n");
325 else if (wcsncmp(&(className[16]), W("Vector"), 6) == 0)
327 SIMDVectorHandle = typeHnd;
328 JITDUMP(" Found type Vector\n");
332 JITDUMP(" Unknown SIMD Type\n");
336 if (simdBaseType != TYP_UNKNOWN && sizeBytes != nullptr)
338 // If not a fixed size vector then its size is same as SIMD vector
339 // register length in bytes
342 size = getSIMDVectorRegisterByteLength();
346 setUsesSIMDTypes(true);
349 #ifdef FEATURE_HW_INTRINSICS
350 else if (isIntrinsicType(typeHnd))
352 const size_t Vector64SizeBytes = 64 / 8;
353 const size_t Vector128SizeBytes = 128 / 8;
354 const size_t Vector256SizeBytes = 256 / 8;
356 #if defined(_TARGET_XARCH_)
357 static_assert_no_msg(YMM_REGSIZE_BYTES == Vector256SizeBytes);
358 static_assert_no_msg(XMM_REGSIZE_BYTES == Vector128SizeBytes);
360 if (typeHnd == Vector256FloatHandle)
362 simdBaseType = TYP_FLOAT;
363 size = Vector256SizeBytes;
364 JITDUMP(" Known type Vector256<float>\n");
366 else if (typeHnd == Vector256DoubleHandle)
368 simdBaseType = TYP_DOUBLE;
369 size = Vector256SizeBytes;
370 JITDUMP(" Known type Vector256<double>\n");
372 else if (typeHnd == Vector256IntHandle)
374 simdBaseType = TYP_INT;
375 size = Vector256SizeBytes;
376 JITDUMP(" Known type Vector256<int>\n");
378 else if (typeHnd == Vector256UIntHandle)
380 simdBaseType = TYP_UINT;
381 size = Vector256SizeBytes;
382 JITDUMP(" Known type Vector256<uint>\n");
384 else if (typeHnd == Vector256ShortHandle)
386 simdBaseType = TYP_SHORT;
387 size = Vector256SizeBytes;
388 JITDUMP(" Known type Vector256<short>\n");
390 else if (typeHnd == Vector256UShortHandle)
392 simdBaseType = TYP_USHORT;
393 size = Vector256SizeBytes;
394 JITDUMP(" Known type Vector256<ushort>\n");
396 else if (typeHnd == Vector256ByteHandle)
398 simdBaseType = TYP_BYTE;
399 size = Vector256SizeBytes;
400 JITDUMP(" Known type Vector256<sbyte>\n");
402 else if (typeHnd == Vector256UByteHandle)
404 simdBaseType = TYP_UBYTE;
405 size = Vector256SizeBytes;
406 JITDUMP(" Known type Vector256<byte>\n");
408 else if (typeHnd == Vector256LongHandle)
410 simdBaseType = TYP_LONG;
411 size = Vector256SizeBytes;
412 JITDUMP(" Known type Vector256<long>\n");
414 else if (typeHnd == Vector256ULongHandle)
416 simdBaseType = TYP_ULONG;
417 size = Vector256SizeBytes;
418 JITDUMP(" Known type Vector256<ulong>\n");
420 else if (typeHnd == Vector256FloatHandle)
422 simdBaseType = TYP_FLOAT;
423 size = Vector256SizeBytes;
424 JITDUMP(" Known type Vector256<float>\n");
427 #endif // defined(_TARGET_XARCH)
428 if (typeHnd == Vector128DoubleHandle)
430 simdBaseType = TYP_DOUBLE;
431 size = Vector128SizeBytes;
432 JITDUMP(" Known type Vector128<double>\n");
434 else if (typeHnd == Vector128IntHandle)
436 simdBaseType = TYP_INT;
437 size = Vector128SizeBytes;
438 JITDUMP(" Known type Vector128<int>\n");
440 else if (typeHnd == Vector128UIntHandle)
442 simdBaseType = TYP_UINT;
443 size = Vector128SizeBytes;
444 JITDUMP(" Known type Vector128<uint>\n");
446 else if (typeHnd == Vector128ShortHandle)
448 simdBaseType = TYP_SHORT;
449 size = Vector128SizeBytes;
450 JITDUMP(" Known type Vector128<short>\n");
452 else if (typeHnd == Vector128UShortHandle)
454 simdBaseType = TYP_USHORT;
455 size = Vector128SizeBytes;
456 JITDUMP(" Known type Vector128<ushort>\n");
458 else if (typeHnd == Vector128ByteHandle)
460 simdBaseType = TYP_BYTE;
461 size = Vector128SizeBytes;
462 JITDUMP(" Known type Vector128<sbyte>\n");
464 else if (typeHnd == Vector128UByteHandle)
466 simdBaseType = TYP_UBYTE;
467 size = Vector128SizeBytes;
468 JITDUMP(" Known type Vector128<byte>\n");
470 else if (typeHnd == Vector128LongHandle)
472 simdBaseType = TYP_LONG;
473 size = Vector128SizeBytes;
474 JITDUMP(" Known type Vector128<long>\n");
476 else if (typeHnd == Vector128ULongHandle)
478 simdBaseType = TYP_ULONG;
479 size = Vector128SizeBytes;
480 JITDUMP(" Known type Vector128<ulong>\n");
482 #if defined(_TARGET_ARM64_)
483 else if (typeHnd == Vector64IntHandle)
485 simdBaseType = TYP_INT;
486 size = Vector64SizeBytes;
487 JITDUMP(" Known type Vector64<int>\n");
489 else if (typeHnd == Vector64UIntHandle)
491 simdBaseType = TYP_UINT;
492 size = Vector64SizeBytes;
493 JITDUMP(" Known type Vector64<uint>\n");
495 else if (typeHnd == Vector64ShortHandle)
497 simdBaseType = TYP_SHORT;
498 size = Vector64SizeBytes;
499 JITDUMP(" Known type Vector64<short>\n");
501 else if (typeHnd == Vector64UShortHandle)
503 simdBaseType = TYP_USHORT;
504 size = Vector64SizeBytes;
505 JITDUMP(" Known type Vector64<ushort>\n");
507 else if (typeHnd == Vector64ByteHandle)
509 simdBaseType = TYP_BYTE;
510 size = Vector64SizeBytes;
511 JITDUMP(" Known type Vector64<sbyte>\n");
513 else if (typeHnd == Vector64UByteHandle)
515 simdBaseType = TYP_UBYTE;
516 size = Vector64SizeBytes;
517 JITDUMP(" Known type Vector64<byte>\n");
519 #endif // defined(_TARGET_ARM64_)
522 if (simdBaseType == TYP_UNKNOWN)
524 // Doesn't match with any of the cached type handles.
525 const char* className = getClassNameFromMetadata(typeHnd, nullptr);
526 CORINFO_CLASS_HANDLE baseTypeHnd = getTypeInstantiationArgument(typeHnd, 0);
528 if (baseTypeHnd != nullptr)
530 CorInfoType type = info.compCompHnd->getTypeForPrimitiveNumericClass(baseTypeHnd);
532 JITDUMP("HW Intrinsic SIMD Candidate Type %s with Base Type %s\n", className,
533 getClassNameFromMetadata(baseTypeHnd, nullptr));
535 #if defined(_TARGET_XARCH_)
536 if (strcmp(className, "Vector256`1") == 0)
538 size = Vector256SizeBytes;
541 case CORINFO_TYPE_FLOAT:
542 Vector256FloatHandle = typeHnd;
543 simdBaseType = TYP_FLOAT;
544 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<float>\n");
546 case CORINFO_TYPE_DOUBLE:
547 Vector256DoubleHandle = typeHnd;
548 simdBaseType = TYP_DOUBLE;
549 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<double>\n");
551 case CORINFO_TYPE_INT:
552 Vector256IntHandle = typeHnd;
553 simdBaseType = TYP_INT;
554 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<int>\n");
556 case CORINFO_TYPE_UINT:
557 Vector256UIntHandle = typeHnd;
558 simdBaseType = TYP_UINT;
559 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<uint>\n");
561 case CORINFO_TYPE_SHORT:
562 Vector256ShortHandle = typeHnd;
563 simdBaseType = TYP_SHORT;
564 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<short>\n");
566 case CORINFO_TYPE_USHORT:
567 Vector256UShortHandle = typeHnd;
568 simdBaseType = TYP_USHORT;
569 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<ushort>\n");
571 case CORINFO_TYPE_LONG:
572 Vector256LongHandle = typeHnd;
573 simdBaseType = TYP_LONG;
574 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<long>\n");
576 case CORINFO_TYPE_ULONG:
577 Vector256ULongHandle = typeHnd;
578 simdBaseType = TYP_ULONG;
579 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<ulong>\n");
581 case CORINFO_TYPE_UBYTE:
582 Vector256UByteHandle = typeHnd;
583 simdBaseType = TYP_UBYTE;
584 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<byte>\n");
586 case CORINFO_TYPE_BYTE:
587 Vector256ByteHandle = typeHnd;
588 simdBaseType = TYP_BYTE;
589 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<sbyte>\n");
593 JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector256<T>\n");
597 #endif // defined(_TARGET_XARCH_)
598 if (strcmp(className, "Vector128`1") == 0)
600 size = Vector128SizeBytes;
603 case CORINFO_TYPE_FLOAT:
604 Vector128FloatHandle = typeHnd;
605 simdBaseType = TYP_FLOAT;
606 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<float>\n");
608 case CORINFO_TYPE_DOUBLE:
609 Vector128DoubleHandle = typeHnd;
610 simdBaseType = TYP_DOUBLE;
611 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<double>\n");
613 case CORINFO_TYPE_INT:
614 Vector128IntHandle = typeHnd;
615 simdBaseType = TYP_INT;
616 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<int>\n");
618 case CORINFO_TYPE_UINT:
619 Vector128UIntHandle = typeHnd;
620 simdBaseType = TYP_UINT;
621 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<uint>\n");
623 case CORINFO_TYPE_SHORT:
624 Vector128ShortHandle = typeHnd;
625 simdBaseType = TYP_SHORT;
626 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<short>\n");
628 case CORINFO_TYPE_USHORT:
629 Vector128UShortHandle = typeHnd;
630 simdBaseType = TYP_USHORT;
631 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<ushort>\n");
633 case CORINFO_TYPE_LONG:
634 Vector128LongHandle = typeHnd;
635 simdBaseType = TYP_LONG;
636 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<long>\n");
638 case CORINFO_TYPE_ULONG:
639 Vector128ULongHandle = typeHnd;
640 simdBaseType = TYP_ULONG;
641 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<ulong>\n");
643 case CORINFO_TYPE_UBYTE:
644 Vector128UByteHandle = typeHnd;
645 simdBaseType = TYP_UBYTE;
646 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<byte>\n");
648 case CORINFO_TYPE_BYTE:
649 Vector128ByteHandle = typeHnd;
650 simdBaseType = TYP_BYTE;
651 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<sbyte>\n");
655 JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector128<T>\n");
658 #if defined(_TARGET_ARM64_)
659 else if (strcmp(className, "Vector64`1") == 0)
661 size = Vector64SizeBytes;
664 case CORINFO_TYPE_FLOAT:
665 Vector64FloatHandle = typeHnd;
666 simdBaseType = TYP_FLOAT;
667 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<float>\n");
669 case CORINFO_TYPE_INT:
670 Vector64IntHandle = typeHnd;
671 simdBaseType = TYP_INT;
672 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<int>\n");
674 case CORINFO_TYPE_UINT:
675 Vector64UIntHandle = typeHnd;
676 simdBaseType = TYP_UINT;
677 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<uint>\n");
679 case CORINFO_TYPE_SHORT:
680 Vector64ShortHandle = typeHnd;
681 simdBaseType = TYP_SHORT;
682 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<short>\n");
684 case CORINFO_TYPE_USHORT:
685 Vector64UShortHandle = typeHnd;
686 simdBaseType = TYP_USHORT;
687 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<ushort>\n");
689 case CORINFO_TYPE_UBYTE:
690 Vector64UByteHandle = typeHnd;
691 simdBaseType = TYP_UBYTE;
692 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<byte>\n");
694 case CORINFO_TYPE_BYTE:
695 Vector64ByteHandle = typeHnd;
696 simdBaseType = TYP_BYTE;
697 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<sbyte>\n");
701 JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector64<T>\n");
704 #endif // defined(_TARGET_ARM64_)
708 if (sizeBytes != nullptr)
713 if (simdBaseType != TYP_UNKNOWN)
715 setUsesSIMDTypes(true);
718 #endif // FEATURE_HW_INTRINSICS
723 //--------------------------------------------------------------------------------------
724 // getSIMDIntrinsicInfo: get SIMD intrinsic info given the method handle.
727 // inOutTypeHnd - The handle of the type on which the method is invoked. This is an in-out param.
728 // methodHnd - The handle of the method we're interested in.
729 // sig - method signature info
730 // isNewObj - whether this call represents a newboj constructor call
731 // argCount - argument count - out pram
732 // baseType - base type of the intrinsic - out param
733 // sizeBytes - size of SIMD vector type on which the method is invoked - out param
736 // SIMDIntrinsicInfo struct initialized corresponding to methodHnd.
737 // Sets SIMDIntrinsicInfo.id to SIMDIntrinsicInvalid if methodHnd doesn't correspond
738 // to any SIMD intrinsic. Also, sets the out params inOutTypeHnd, argCount, baseType and
741 // Note that VectorMath class doesn't have a base type and first argument of the method
742 // determines the SIMD vector type on which intrinsic is invoked. In such a case inOutTypeHnd
743 // is modified by this routine.
745 // TODO-Throughput: The current implementation is based on method name string parsing.
746 // Although we now have type identification from the VM, the parsing of intrinsic names
747 // could be made more efficient.
749 const SIMDIntrinsicInfo* Compiler::getSIMDIntrinsicInfo(CORINFO_CLASS_HANDLE* inOutTypeHnd,
750 CORINFO_METHOD_HANDLE methodHnd,
751 CORINFO_SIG_INFO* sig,
758 assert(baseType != nullptr);
759 assert(sizeBytes != nullptr);
761 // get baseType and size of the type
762 CORINFO_CLASS_HANDLE typeHnd = *inOutTypeHnd;
763 *baseType = getBaseTypeAndSizeOfSIMDType(typeHnd, sizeBytes);
765 if (typeHnd == SIMDVectorHandle)
767 // All of the supported intrinsics on this static class take a first argument that's a vector,
768 // which determines the baseType.
769 // The exception is the IsHardwareAccelerated property, which is handled as a special case.
770 assert(*baseType == TYP_UNKNOWN);
771 if (sig->numArgs == 0)
773 const SIMDIntrinsicInfo* hwAccelIntrinsicInfo = &(simdIntrinsicInfoArray[SIMDIntrinsicHWAccel]);
774 if ((strcmp(eeGetMethodName(methodHnd, nullptr), hwAccelIntrinsicInfo->methodName) == 0) &&
775 JITtype2varType(sig->retType) == hwAccelIntrinsicInfo->retType)
778 assert(hwAccelIntrinsicInfo->argCount == 0 && hwAccelIntrinsicInfo->isInstMethod == false);
779 return hwAccelIntrinsicInfo;
785 typeHnd = info.compCompHnd->getArgClass(sig, sig->args);
786 *inOutTypeHnd = typeHnd;
787 *baseType = getBaseTypeAndSizeOfSIMDType(typeHnd, sizeBytes);
791 if (*baseType == TYP_UNKNOWN)
793 JITDUMP("NOT a SIMD Intrinsic: unsupported baseType\n");
797 // account for implicit "this" arg
798 *argCount = sig->numArgs;
804 // Get the Intrinsic Id by parsing method name.
806 // TODO-Throughput: replace sequential search by binary search by arranging entries
807 // sorted by method name.
808 SIMDIntrinsicID intrinsicId = SIMDIntrinsicInvalid;
809 const char* methodName = eeGetMethodName(methodHnd, nullptr);
810 for (int i = SIMDIntrinsicNone + 1; i < SIMDIntrinsicInvalid; ++i)
812 if (strcmp(methodName, simdIntrinsicInfoArray[i].methodName) == 0)
814 // Found an entry for the method; further check whether it is one of
815 // the supported base types.
817 for (int j = 0; j < SIMD_INTRINSIC_MAX_BASETYPE_COUNT; ++j)
819 // Convention: if there are fewer base types supported than MAX_BASETYPE_COUNT,
820 // the end of the list is marked by TYP_UNDEF.
821 if (simdIntrinsicInfoArray[i].supportedBaseTypes[j] == TYP_UNDEF)
826 if (simdIntrinsicInfoArray[i].supportedBaseTypes[j] == *baseType)
838 // Now, check the arguments.
839 unsigned int fixedArgCnt = simdIntrinsicInfoArray[i].argCount;
840 unsigned int expectedArgCnt = fixedArgCnt;
842 // First handle SIMDIntrinsicInitN, where the arg count depends on the type.
843 // The listed arg types include the vector and the first two init values, which is the expected number
844 // for Vector2. For other cases, we'll check their types here.
845 if (*argCount > expectedArgCnt)
847 if (i == SIMDIntrinsicInitN)
849 if (*argCount == 3 && typeHnd == SIMDVector2Handle)
853 else if (*argCount == 4 && typeHnd == SIMDVector3Handle)
857 else if (*argCount == 5 && typeHnd == SIMDVector4Handle)
862 else if (i == SIMDIntrinsicInitFixed)
864 if (*argCount == 4 && typeHnd == SIMDVector4Handle)
870 if (*argCount != expectedArgCnt)
875 // Validate the types of individual args passed are what is expected of.
876 // If any of the types don't match with what is expected, don't consider
877 // as an intrinsic. This will make an older JIT with SIMD capabilities
878 // resilient to breaking changes to SIMD managed API.
880 // Note that from IL type stack, args get popped in right to left order
881 // whereas args get listed in method signatures in left to right order.
883 int stackIndex = (expectedArgCnt - 1);
885 // Track the arguments from the signature - we currently only use this to distinguish
886 // integral and pointer types, both of which will by TYP_I_IMPL on the importer stack.
887 CORINFO_ARG_LIST_HANDLE argLst = sig->args;
889 CORINFO_CLASS_HANDLE argClass;
890 for (unsigned int argIndex = 0; found == true && argIndex < expectedArgCnt; argIndex++)
892 bool isThisPtr = ((argIndex == 0) && sig->hasThis());
894 // In case of "newobj SIMDVector<T>(T val)", thisPtr won't be present on type stack.
895 // We don't check anything in that case.
896 if (!isThisPtr || !isNewObj)
898 GenTree* arg = impStackTop(stackIndex).val;
899 var_types argType = arg->TypeGet();
901 var_types expectedArgType;
902 if (argIndex < fixedArgCnt)
905 // - intrinsicInfo.argType[i] == TYP_UNDEF - intrinsic doesn't have a valid arg at position i
906 // - intrinsicInfo.argType[i] == TYP_UNKNOWN - arg type should be same as basetype
907 // Note that we pop the args off in reverse order.
908 expectedArgType = simdIntrinsicInfoArray[i].argType[argIndex];
909 assert(expectedArgType != TYP_UNDEF);
910 if (expectedArgType == TYP_UNKNOWN)
912 // The type of the argument will be genActualType(*baseType).
913 expectedArgType = genActualType(*baseType);
914 argType = genActualType(argType);
919 expectedArgType = *baseType;
922 if (!isThisPtr && argType == TYP_I_IMPL)
924 // The reference implementation has a constructor that takes a pointer.
925 // We don't want to recognize that one. This requires us to look at the CorInfoType
926 // in order to distinguish a signature with a pointer argument from one with an
927 // integer argument of pointer size, both of which will be TYP_I_IMPL on the stack.
928 // TODO-Review: This seems quite fragile. We should consider beefing up the checking
930 CorInfoType corType = strip(info.compCompHnd->getArgType(sig, argLst, &argClass));
931 if (corType == CORINFO_TYPE_PTR)
937 if (varTypeIsSIMD(argType))
939 argType = TYP_STRUCT;
941 if (argType != expectedArgType)
946 if (argIndex != 0 || !sig->hasThis())
948 argLst = info.compCompHnd->getArgNext(argLst);
953 // Cross check return type and static vs. instance is what we are expecting.
954 // If not, don't consider it as an intrinsic.
955 // Note that ret type of TYP_UNKNOWN means that it is not known apriori and must be same as baseType
958 var_types expectedRetType = simdIntrinsicInfoArray[i].retType;
959 if (expectedRetType == TYP_UNKNOWN)
961 // JIT maps uint/ulong type vars to TYP_INT/TYP_LONG.
963 (*baseType == TYP_UINT || *baseType == TYP_ULONG) ? genActualType(*baseType) : *baseType;
966 if (JITtype2varType(sig->retType) != expectedRetType ||
967 sig->hasThis() != simdIntrinsicInfoArray[i].isInstMethod)
975 intrinsicId = (SIMDIntrinsicID)i;
981 if (intrinsicId != SIMDIntrinsicInvalid)
983 JITDUMP("Method %s maps to SIMD intrinsic %s\n", methodName, simdIntrinsicNames[intrinsicId]);
984 return &simdIntrinsicInfoArray[intrinsicId];
988 JITDUMP("Method %s is NOT a SIMD intrinsic\n", methodName);
994 // Pops and returns GenTree node from importer's type stack.
995 // Normalizes TYP_STRUCT value in case of GT_CALL, GT_RET_EXPR and arg nodes.
998 // type - the type of value that the caller expects to be popped off the stack.
999 // expectAddr - if true indicates we are expecting type stack entry to be a TYP_BYREF.
1000 // structType - the class handle to use when normalizing if it is not the same as the stack entry class handle;
1001 // this can happen for certain scenarios, such as folding away a static cast, where we want the
1002 // value popped to have the type that would have been returned.
1005 // If the popped value is a struct, and the expected type is a simd type, it will be set
1006 // to that type, otherwise it will assert if the type being popped is not the expected type.
1008 GenTree* Compiler::impSIMDPopStack(var_types type, bool expectAddr, CORINFO_CLASS_HANDLE structType)
1010 StackEntry se = impPopStack();
1011 typeInfo ti = se.seTypeInfo;
1012 GenTree* tree = se.val;
1014 // If expectAddr is true implies what we have on stack is address and we need
1015 // SIMD type struct that it points to.
1018 assert(tree->TypeGet() == TYP_BYREF);
1019 if (tree->OperGet() == GT_ADDR)
1021 tree = tree->gtGetOp1();
1025 tree = gtNewOperNode(GT_IND, type, tree);
1029 bool isParam = false;
1031 // If we have a ldobj of a SIMD local we need to transform it.
1032 if (tree->OperGet() == GT_OBJ)
1034 GenTree* addr = tree->gtOp.gtOp1;
1035 if ((addr->OperGet() == GT_ADDR) && isSIMDTypeLocal(addr->gtOp.gtOp1))
1037 tree = addr->gtOp.gtOp1;
1041 if (tree->OperGet() == GT_LCL_VAR)
1043 unsigned lclNum = tree->AsLclVarCommon()->GetLclNum();
1044 LclVarDsc* lclVarDsc = &lvaTable[lclNum];
1045 isParam = lclVarDsc->lvIsParam;
1048 // normalize TYP_STRUCT value
1049 if (varTypeIsStruct(tree) && ((tree->OperGet() == GT_RET_EXPR) || (tree->OperGet() == GT_CALL) || isParam))
1051 assert(ti.IsType(TI_STRUCT));
1053 if (structType == nullptr)
1055 structType = ti.GetClassHandleForValueClass();
1058 tree = impNormStructVal(tree, structType, (unsigned)CHECK_SPILL_ALL);
1061 // Now set the type of the tree to the specialized SIMD struct type, if applicable.
1062 if (genActualType(tree->gtType) != genActualType(type))
1064 assert(tree->gtType == TYP_STRUCT);
1065 tree->gtType = type;
1067 else if (tree->gtType == TYP_BYREF)
1069 assert(tree->IsLocal() || (tree->OperGet() == GT_RET_EXPR) || (tree->OperGet() == GT_CALL) ||
1070 ((tree->gtOper == GT_ADDR) && varTypeIsSIMD(tree->gtGetOp1())));
1076 // impSIMDGetFixed: Create a GT_SIMD tree for a Get property of SIMD vector with a fixed index.
1079 // baseType - The base (element) type of the SIMD vector.
1080 // simdSize - The total size in bytes of the SIMD vector.
1081 // index - The index of the field to get.
1084 // Returns a GT_SIMD node with the SIMDIntrinsicGetItem intrinsic id.
1086 GenTreeSIMD* Compiler::impSIMDGetFixed(var_types simdType, var_types baseType, unsigned simdSize, int index)
1088 assert(simdSize >= ((index + 1) * genTypeSize(baseType)));
1090 // op1 is a SIMD source.
1091 GenTree* op1 = impSIMDPopStack(simdType, true);
1093 GenTree* op2 = gtNewIconNode(index);
1094 GenTreeSIMD* simdTree = gtNewSIMDNode(baseType, op1, op2, SIMDIntrinsicGetItem, baseType, simdSize);
1098 #ifdef _TARGET_XARCH_
1099 // impSIMDLongRelOpEqual: transforms operands and returns the SIMD intrinsic to be applied on
1100 // transformed operands to obtain == comparison result.
1103 // typeHnd - type handle of SIMD vector
1104 // size - SIMD vector size
1105 // op1 - in-out parameter; first operand
1106 // op2 - in-out parameter; second operand
1109 // Modifies in-out params op1, op2 and returns intrinsic ID to be applied to modified operands
1111 SIMDIntrinsicID Compiler::impSIMDLongRelOpEqual(CORINFO_CLASS_HANDLE typeHnd,
1116 var_types simdType = (*pOp1)->TypeGet();
1117 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1119 // There is no direct SSE2 support for comparing TYP_LONG vectors.
1120 // These have to be implemented in terms of TYP_INT vector comparison operations.
1122 // Equality(v1, v2):
1123 // tmp = (v1 == v2) i.e. compare for equality as if v1 and v2 are vector<int>
1124 // result = BitwiseAnd(t, shuffle(t, (2, 3, 0, 1)))
1125 // Shuffle is meant to swap the comparison results of low-32-bits and high 32-bits of respective long elements.
1127 // Compare vector<long> as if they were vector<int> and assign the result to a temp
1128 GenTree* compResult = gtNewSIMDNode(simdType, *pOp1, *pOp2, SIMDIntrinsicEqual, TYP_INT, size);
1129 unsigned lclNum = lvaGrabTemp(true DEBUGARG("SIMD Long =="));
1130 lvaSetStruct(lclNum, typeHnd, false);
1131 GenTree* tmp = gtNewLclvNode(lclNum, simdType);
1132 GenTree* asg = gtNewTempAssign(lclNum, compResult);
1134 // op1 = GT_COMMA(tmp=compResult, tmp)
1135 // op2 = Shuffle(tmp, 0xB1)
1136 // IntrinsicId = BitwiseAnd
1137 *pOp1 = gtNewOperNode(GT_COMMA, simdType, asg, tmp);
1138 *pOp2 = gtNewSIMDNode(simdType, gtNewLclvNode(lclNum, simdType), gtNewIconNode(SHUFFLE_ZWXY, TYP_INT),
1139 SIMDIntrinsicShuffleSSE2, TYP_INT, size);
1140 return SIMDIntrinsicBitwiseAnd;
1143 // impSIMDLongRelOpGreaterThan: transforms operands and returns the SIMD intrinsic to be applied on
1144 // transformed operands to obtain > comparison result.
1147 // typeHnd - type handle of SIMD vector
1148 // size - SIMD vector size
1149 // pOp1 - in-out parameter; first operand
1150 // pOp2 - in-out parameter; second operand
1153 // Modifies in-out params pOp1, pOp2 and returns intrinsic ID to be applied to modified operands
1155 SIMDIntrinsicID Compiler::impSIMDLongRelOpGreaterThan(CORINFO_CLASS_HANDLE typeHnd,
1160 var_types simdType = (*pOp1)->TypeGet();
1161 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1163 // GreaterThan(v1, v2) where v1 and v2 are vector long.
1164 // Let us consider the case of single long element comparison.
1165 // say L1 = (x1, y1) and L2 = (x2, y2) where x1, y1, x2, and y2 are 32-bit integers that comprise the longs L1 and
1168 // GreaterThan(L1, L2) can be expressed in terms of > relationship between 32-bit integers that comprise L1 and L2
1170 // = (x1, y1) > (x2, y2)
1171 // = (x1 > x2) || [(x1 == x2) && (y1 > y2)] - eq (1)
1173 // t = (v1 > v2) 32-bit signed comparison
1174 // u = (v1 == v2) 32-bit sized element equality
1175 // v = (v1 > v2) 32-bit unsigned comparison
1177 // z = shuffle(t, (3, 3, 1, 1)) - This corresponds to (x1 > x2) in eq(1) above
1178 // t1 = Shuffle(v, (2, 2, 0, 0)) - This corresponds to (y1 > y2) in eq(1) above
1179 // u1 = Shuffle(u, (3, 3, 1, 1)) - This corresponds to (x1 == x2) in eq(1) above
1180 // w = And(t1, u1) - This corresponds to [(x1 == x2) && (y1 > y2)] in eq(1) above
1181 // Result = BitwiseOr(z, w)
1183 // Since op1 and op2 gets used multiple times, make sure side effects are computed.
1184 GenTree* dupOp1 = nullptr;
1185 GenTree* dupOp2 = nullptr;
1186 GenTree* dupDupOp1 = nullptr;
1187 GenTree* dupDupOp2 = nullptr;
1189 if (((*pOp1)->gtFlags & GTF_SIDE_EFFECT) != 0)
1191 dupOp1 = fgInsertCommaFormTemp(pOp1, typeHnd);
1192 dupDupOp1 = gtNewLclvNode(dupOp1->AsLclVarCommon()->GetLclNum(), simdType);
1196 dupOp1 = gtCloneExpr(*pOp1);
1197 dupDupOp1 = gtCloneExpr(*pOp1);
1200 if (((*pOp2)->gtFlags & GTF_SIDE_EFFECT) != 0)
1202 dupOp2 = fgInsertCommaFormTemp(pOp2, typeHnd);
1203 dupDupOp2 = gtNewLclvNode(dupOp2->AsLclVarCommon()->GetLclNum(), simdType);
1207 dupOp2 = gtCloneExpr(*pOp2);
1208 dupDupOp2 = gtCloneExpr(*pOp2);
1211 assert(dupDupOp1 != nullptr && dupDupOp2 != nullptr);
1212 assert(dupOp1 != nullptr && dupOp2 != nullptr);
1213 assert(*pOp1 != nullptr && *pOp2 != nullptr);
1215 // v1GreaterThanv2Signed - signed 32-bit comparison
1216 GenTree* v1GreaterThanv2Signed = gtNewSIMDNode(simdType, *pOp1, *pOp2, SIMDIntrinsicGreaterThan, TYP_INT, size);
1218 // v1Equalsv2 - 32-bit equality
1219 GenTree* v1Equalsv2 = gtNewSIMDNode(simdType, dupOp1, dupOp2, SIMDIntrinsicEqual, TYP_INT, size);
1221 // v1GreaterThanv2Unsigned - unsigned 32-bit comparison
1222 var_types tempBaseType = TYP_UINT;
1223 SIMDIntrinsicID sid = impSIMDRelOp(SIMDIntrinsicGreaterThan, typeHnd, size, &tempBaseType, &dupDupOp1, &dupDupOp2);
1224 GenTree* v1GreaterThanv2Unsigned = gtNewSIMDNode(simdType, dupDupOp1, dupDupOp2, sid, tempBaseType, size);
1226 GenTree* z = gtNewSIMDNode(simdType, v1GreaterThanv2Signed, gtNewIconNode(SHUFFLE_WWYY, TYP_INT),
1227 SIMDIntrinsicShuffleSSE2, TYP_FLOAT, size);
1228 GenTree* t1 = gtNewSIMDNode(simdType, v1GreaterThanv2Unsigned, gtNewIconNode(SHUFFLE_ZZXX, TYP_INT),
1229 SIMDIntrinsicShuffleSSE2, TYP_FLOAT, size);
1230 GenTree* u1 = gtNewSIMDNode(simdType, v1Equalsv2, gtNewIconNode(SHUFFLE_WWYY, TYP_INT), SIMDIntrinsicShuffleSSE2,
1232 GenTree* w = gtNewSIMDNode(simdType, u1, t1, SIMDIntrinsicBitwiseAnd, TYP_INT, size);
1236 return SIMDIntrinsicBitwiseOr;
1239 // impSIMDLongRelOpGreaterThanOrEqual: transforms operands and returns the SIMD intrinsic to be applied on
1240 // transformed operands to obtain >= comparison result.
1243 // typeHnd - type handle of SIMD vector
1244 // size - SIMD vector size
1245 // pOp1 - in-out parameter; first operand
1246 // pOp2 - in-out parameter; second operand
1249 // Modifies in-out params pOp1, pOp2 and returns intrinsic ID to be applied to modified operands
1251 SIMDIntrinsicID Compiler::impSIMDLongRelOpGreaterThanOrEqual(CORINFO_CLASS_HANDLE typeHnd,
1256 var_types simdType = (*pOp1)->TypeGet();
1257 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1259 // expand this to (a == b) | (a > b)
1260 GenTree* dupOp1 = nullptr;
1261 GenTree* dupOp2 = nullptr;
1263 if (((*pOp1)->gtFlags & GTF_SIDE_EFFECT) != 0)
1265 dupOp1 = fgInsertCommaFormTemp(pOp1, typeHnd);
1269 dupOp1 = gtCloneExpr(*pOp1);
1272 if (((*pOp2)->gtFlags & GTF_SIDE_EFFECT) != 0)
1274 dupOp2 = fgInsertCommaFormTemp(pOp2, typeHnd);
1278 dupOp2 = gtCloneExpr(*pOp2);
1281 assert(dupOp1 != nullptr && dupOp2 != nullptr);
1282 assert(*pOp1 != nullptr && *pOp2 != nullptr);
1285 SIMDIntrinsicID id = impSIMDLongRelOpEqual(typeHnd, size, pOp1, pOp2);
1286 *pOp1 = gtNewSIMDNode(simdType, *pOp1, *pOp2, id, TYP_LONG, size);
1289 id = impSIMDLongRelOpGreaterThan(typeHnd, size, &dupOp1, &dupOp2);
1290 *pOp2 = gtNewSIMDNode(simdType, dupOp1, dupOp2, id, TYP_LONG, size);
1292 return SIMDIntrinsicBitwiseOr;
1295 // impSIMDInt32OrSmallIntRelOpGreaterThanOrEqual: transforms operands and returns the SIMD intrinsic to be applied on
1296 // transformed operands to obtain >= comparison result in case of integer base type vectors
1299 // typeHnd - type handle of SIMD vector
1300 // size - SIMD vector size
1301 // baseType - base type of SIMD vector
1302 // pOp1 - in-out parameter; first operand
1303 // pOp2 - in-out parameter; second operand
1306 // Modifies in-out params pOp1, pOp2 and returns intrinsic ID to be applied to modified operands
1308 SIMDIntrinsicID Compiler::impSIMDIntegralRelOpGreaterThanOrEqual(
1309 CORINFO_CLASS_HANDLE typeHnd, unsigned size, var_types baseType, GenTree** pOp1, GenTree** pOp2)
1311 var_types simdType = (*pOp1)->TypeGet();
1312 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1314 // This routine should be used only for integer base type vectors
1315 assert(varTypeIsIntegral(baseType));
1316 if ((getSIMDSupportLevel() == SIMD_SSE2_Supported) && ((baseType == TYP_LONG) || baseType == TYP_UBYTE))
1318 return impSIMDLongRelOpGreaterThanOrEqual(typeHnd, size, pOp1, pOp2);
1321 // expand this to (a == b) | (a > b)
1322 GenTree* dupOp1 = nullptr;
1323 GenTree* dupOp2 = nullptr;
1325 if (((*pOp1)->gtFlags & GTF_SIDE_EFFECT) != 0)
1327 dupOp1 = fgInsertCommaFormTemp(pOp1, typeHnd);
1331 dupOp1 = gtCloneExpr(*pOp1);
1334 if (((*pOp2)->gtFlags & GTF_SIDE_EFFECT) != 0)
1336 dupOp2 = fgInsertCommaFormTemp(pOp2, typeHnd);
1340 dupOp2 = gtCloneExpr(*pOp2);
1343 assert(dupOp1 != nullptr && dupOp2 != nullptr);
1344 assert(*pOp1 != nullptr && *pOp2 != nullptr);
1347 *pOp1 = gtNewSIMDNode(simdType, *pOp1, *pOp2, SIMDIntrinsicEqual, baseType, size);
1350 *pOp2 = gtNewSIMDNode(simdType, dupOp1, dupOp2, SIMDIntrinsicGreaterThan, baseType, size);
1352 return SIMDIntrinsicBitwiseOr;
1354 #endif // _TARGET_XARCH_
1356 // Transforms operands and returns the SIMD intrinsic to be applied on
1357 // transformed operands to obtain given relop result.
1360 // relOpIntrinsicId - Relational operator SIMD intrinsic
1361 // typeHnd - type handle of SIMD vector
1362 // size - SIMD vector size
1363 // inOutBaseType - base type of SIMD vector
1364 // pOp1 - in-out parameter; first operand
1365 // pOp2 - in-out parameter; second operand
1368 // Modifies in-out params pOp1, pOp2, inOutBaseType and returns intrinsic ID to be applied to modified operands
1370 SIMDIntrinsicID Compiler::impSIMDRelOp(SIMDIntrinsicID relOpIntrinsicId,
1371 CORINFO_CLASS_HANDLE typeHnd,
1373 var_types* inOutBaseType,
1377 var_types simdType = (*pOp1)->TypeGet();
1378 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1380 assert(isRelOpSIMDIntrinsic(relOpIntrinsicId));
1382 SIMDIntrinsicID intrinsicID = relOpIntrinsicId;
1383 #ifdef _TARGET_XARCH_
1384 var_types baseType = *inOutBaseType;
1386 if (varTypeIsFloating(baseType))
1388 // SSE2/AVX doesn't support > and >= on vector float/double.
1389 // Therefore, we need to use < and <= with swapped operands
1390 if (relOpIntrinsicId == SIMDIntrinsicGreaterThan || relOpIntrinsicId == SIMDIntrinsicGreaterThanOrEqual)
1392 GenTree* tmp = *pOp1;
1397 (relOpIntrinsicId == SIMDIntrinsicGreaterThan) ? SIMDIntrinsicLessThan : SIMDIntrinsicLessThanOrEqual;
1400 else if (varTypeIsIntegral(baseType))
1402 // SSE/AVX doesn't support < and <= on integer base type vectors.
1403 // Therefore, we need to use > and >= with swapped operands.
1404 if (intrinsicID == SIMDIntrinsicLessThan || intrinsicID == SIMDIntrinsicLessThanOrEqual)
1406 GenTree* tmp = *pOp1;
1410 intrinsicID = (relOpIntrinsicId == SIMDIntrinsicLessThan) ? SIMDIntrinsicGreaterThan
1411 : SIMDIntrinsicGreaterThanOrEqual;
1414 if ((getSIMDSupportLevel() == SIMD_SSE2_Supported) && baseType == TYP_LONG)
1416 // There is no direct SSE2 support for comparing TYP_LONG vectors.
1417 // These have to be implemented interms of TYP_INT vector comparison operations.
1418 if (intrinsicID == SIMDIntrinsicEqual)
1420 intrinsicID = impSIMDLongRelOpEqual(typeHnd, size, pOp1, pOp2);
1422 else if (intrinsicID == SIMDIntrinsicGreaterThan)
1424 intrinsicID = impSIMDLongRelOpGreaterThan(typeHnd, size, pOp1, pOp2);
1426 else if (intrinsicID == SIMDIntrinsicGreaterThanOrEqual)
1428 intrinsicID = impSIMDLongRelOpGreaterThanOrEqual(typeHnd, size, pOp1, pOp2);
1435 // SSE2 and AVX direct support for signed comparison of int32, int16 and int8 types
1436 else if (!varTypeIsUnsigned(baseType))
1438 if (intrinsicID == SIMDIntrinsicGreaterThanOrEqual)
1440 intrinsicID = impSIMDIntegralRelOpGreaterThanOrEqual(typeHnd, size, baseType, pOp1, pOp2);
1445 // Vector<byte>, Vector<ushort>, Vector<uint> and Vector<ulong>:
1446 // SSE2 supports > for signed comparison. Therefore, to use it for
1447 // comparing unsigned numbers, we subtract a constant from both the
1448 // operands such that the result fits within the corresponding signed
1449 // type. The resulting signed numbers are compared using SSE2 signed
1452 // Vector<byte>: constant to be subtracted is 2^7
1453 // Vector<ushort> constant to be subtracted is 2^15
1454 // Vector<uint> constant to be subtracted is 2^31
1455 // Vector<ulong> constant to be subtracted is 2^63
1457 // We need to treat op1 and op2 as signed for comparison purpose after
1458 // the transformation.
1459 __int64 constVal = 0;
1463 constVal = 0x80808080;
1464 *inOutBaseType = TYP_BYTE;
1467 constVal = 0x80008000;
1468 *inOutBaseType = TYP_SHORT;
1471 constVal = 0x80000000;
1472 *inOutBaseType = TYP_INT;
1475 constVal = 0x8000000000000000LL;
1476 *inOutBaseType = TYP_LONG;
1482 assert(constVal != 0);
1484 // This transformation is not required for equality.
1485 if (intrinsicID != SIMDIntrinsicEqual)
1487 // For constructing const vector use either long or int base type.
1488 var_types tempBaseType;
1490 if (baseType == TYP_ULONG)
1492 tempBaseType = TYP_LONG;
1493 initVal = gtNewLconNode(constVal);
1497 tempBaseType = TYP_INT;
1498 initVal = gtNewIconNode((ssize_t)constVal);
1500 initVal->gtType = tempBaseType;
1501 GenTree* constVector = gtNewSIMDNode(simdType, initVal, nullptr, SIMDIntrinsicInit, tempBaseType, size);
1503 // Assign constVector to a temp, since we intend to use it more than once
1504 // TODO-CQ: We have quite a few such constant vectors constructed during
1505 // the importation of SIMD intrinsics. Make sure that we have a single
1506 // temp per distinct constant per method.
1507 GenTree* tmp = fgInsertCommaFormTemp(&constVector, typeHnd);
1509 // op1 = op1 - constVector
1510 // op2 = op2 - constVector
1511 *pOp1 = gtNewSIMDNode(simdType, *pOp1, constVector, SIMDIntrinsicSub, baseType, size);
1512 *pOp2 = gtNewSIMDNode(simdType, *pOp2, tmp, SIMDIntrinsicSub, baseType, size);
1515 return impSIMDRelOp(intrinsicID, typeHnd, size, inOutBaseType, pOp1, pOp2);
1518 #elif defined(_TARGET_ARM64_)
1519 // TODO-ARM64-CQ handle comparisons against zero
1521 // _TARGET_ARM64_ doesn't support < and <= on register register comparisons
1522 // Therefore, we need to use > and >= with swapped operands.
1523 if (intrinsicID == SIMDIntrinsicLessThan || intrinsicID == SIMDIntrinsicLessThanOrEqual)
1525 GenTree* tmp = *pOp1;
1530 (intrinsicID == SIMDIntrinsicLessThan) ? SIMDIntrinsicGreaterThan : SIMDIntrinsicGreaterThanOrEqual;
1532 #else // !_TARGET_XARCH_
1533 assert(!"impSIMDRelOp() unimplemented on target arch");
1535 #endif // !_TARGET_XARCH_
1540 //-------------------------------------------------------------------------
1541 // impSIMDAbs: creates GT_SIMD node to compute Abs value of a given vector.
1544 // typeHnd - type handle of SIMD vector
1545 // baseType - base type of vector
1546 // size - vector size in bytes
1547 // op1 - operand of Abs intrinsic
1549 GenTree* Compiler::impSIMDAbs(CORINFO_CLASS_HANDLE typeHnd, var_types baseType, unsigned size, GenTree* op1)
1551 assert(varTypeIsSIMD(op1));
1553 var_types simdType = op1->TypeGet();
1554 GenTree* retVal = nullptr;
1556 #ifdef _TARGET_XARCH_
1557 // When there is no direct support, Abs(v) could be computed
1558 // on integer vectors as follows:
1559 // BitVector = v < vector.Zero
1560 // result = ConditionalSelect(BitVector, vector.Zero - v, v)
1562 bool useConditionalSelect = false;
1563 if (getSIMDSupportLevel() == SIMD_SSE2_Supported)
1565 // SSE2 doesn't support abs on signed integer type vectors.
1566 if (baseType == TYP_LONG || baseType == TYP_INT || baseType == TYP_SHORT || baseType == TYP_BYTE)
1568 useConditionalSelect = true;
1573 assert(getSIMDSupportLevel() >= SIMD_SSE4_Supported);
1574 if (baseType == TYP_LONG)
1576 // SSE4/AVX2 don't support abs on long type vector.
1577 useConditionalSelect = true;
1581 if (useConditionalSelect)
1583 // This works only on integer vectors not on float/double vectors.
1584 assert(varTypeIsIntegral(baseType));
1589 if (op1->OperGet() == GT_LCL_VAR)
1591 op1LclNum = op1->gtLclVarCommon.gtLclNum;
1592 op1Assign = nullptr;
1596 op1LclNum = lvaGrabTemp(true DEBUGARG("SIMD Abs op1"));
1597 lvaSetStruct(op1LclNum, typeHnd, false);
1598 op1Assign = gtNewTempAssign(op1LclNum, op1);
1599 op1 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1602 // Assign Vector.Zero to a temp since it is needed more than once
1603 GenTree* vecZero = gtNewSIMDVectorZero(simdType, baseType, size);
1604 unsigned vecZeroLclNum = lvaGrabTemp(true DEBUGARG("SIMD Abs VecZero"));
1605 lvaSetStruct(vecZeroLclNum, typeHnd, false);
1606 GenTree* vecZeroAssign = gtNewTempAssign(vecZeroLclNum, vecZero);
1608 // Construct BitVector = v < vector.Zero
1609 GenTree* bitVecOp1 = op1;
1610 GenTree* bitVecOp2 = gtNewLclvNode(vecZeroLclNum, vecZero->TypeGet());
1611 var_types relOpBaseType = baseType;
1612 SIMDIntrinsicID relOpIntrinsic =
1613 impSIMDRelOp(SIMDIntrinsicLessThan, typeHnd, size, &relOpBaseType, &bitVecOp1, &bitVecOp2);
1614 GenTree* bitVec = gtNewSIMDNode(simdType, bitVecOp1, bitVecOp2, relOpIntrinsic, relOpBaseType, size);
1615 unsigned bitVecLclNum = lvaGrabTemp(true DEBUGARG("SIMD Abs bitVec"));
1616 lvaSetStruct(bitVecLclNum, typeHnd, false);
1617 GenTree* bitVecAssign = gtNewTempAssign(bitVecLclNum, bitVec);
1618 bitVec = gtNewLclvNode(bitVecLclNum, bitVec->TypeGet());
1620 // Construct condSelectOp1 = vector.Zero - v
1621 GenTree* subOp1 = gtNewLclvNode(vecZeroLclNum, vecZero->TypeGet());
1622 GenTree* subOp2 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1623 GenTree* negVec = gtNewSIMDNode(simdType, subOp1, subOp2, SIMDIntrinsicSub, baseType, size);
1625 // Construct ConditionalSelect(bitVec, vector.Zero - v, v)
1626 GenTree* vec = gtNewLclvNode(op1LclNum, op1->TypeGet());
1627 retVal = impSIMDSelect(typeHnd, baseType, size, bitVec, negVec, vec);
1629 // Prepend bitVec assignment to retVal.
1630 // retVal = (tmp2 = v < tmp1), CondSelect(tmp2, tmp1 - v, v)
1631 retVal = gtNewOperNode(GT_COMMA, simdType, bitVecAssign, retVal);
1633 // Prepend vecZero assignment to retVal.
1634 // retVal = (tmp1 = vector.Zero), (tmp2 = v < tmp1), CondSelect(tmp2, tmp1 - v, v)
1635 retVal = gtNewOperNode(GT_COMMA, simdType, vecZeroAssign, retVal);
1637 // If op1 was assigned to a temp, prepend that to retVal.
1638 if (op1Assign != nullptr)
1640 // retVal = (v=op1), (tmp1 = vector.Zero), (tmp2 = v < tmp1), CondSelect(tmp2, tmp1 - v, v)
1641 retVal = gtNewOperNode(GT_COMMA, simdType, op1Assign, retVal);
1644 else if (varTypeIsFloating(baseType))
1646 // Abs(vf) = vf & new SIMDVector<float>(0x7fffffff);
1647 // Abs(vd) = vf & new SIMDVector<double>(0x7fffffffffffffff);
1648 GenTree* bitMask = nullptr;
1649 if (baseType == TYP_FLOAT)
1652 static_assert_no_msg(sizeof(float) == sizeof(int));
1653 *((int*)&f) = 0x7fffffff;
1654 bitMask = gtNewDconNode(f);
1656 else if (baseType == TYP_DOUBLE)
1659 static_assert_no_msg(sizeof(double) == sizeof(__int64));
1660 *((__int64*)&d) = 0x7fffffffffffffffLL;
1661 bitMask = gtNewDconNode(d);
1664 assert(bitMask != nullptr);
1665 bitMask->gtType = baseType;
1666 GenTree* bitMaskVector = gtNewSIMDNode(simdType, bitMask, SIMDIntrinsicInit, baseType, size);
1667 retVal = gtNewSIMDNode(simdType, op1, bitMaskVector, SIMDIntrinsicBitwiseAnd, baseType, size);
1669 else if (baseType == TYP_USHORT || baseType == TYP_UBYTE || baseType == TYP_UINT || baseType == TYP_ULONG)
1671 // Abs is a no-op on unsigned integer type vectors
1676 assert(getSIMDSupportLevel() >= SIMD_SSE4_Supported);
1677 assert(baseType != TYP_LONG);
1679 retVal = gtNewSIMDNode(simdType, op1, SIMDIntrinsicAbs, baseType, size);
1681 #elif defined(_TARGET_ARM64_)
1682 if (varTypeIsUnsigned(baseType))
1684 // Abs is a no-op on unsigned integer type vectors
1689 retVal = gtNewSIMDNode(simdType, op1, SIMDIntrinsicAbs, baseType, size);
1691 #else // !defined(_TARGET_XARCH)_ && !defined(_TARGET_ARM64_)
1692 assert(!"Abs intrinsic on non-xarch target not implemented");
1693 #endif // !_TARGET_XARCH_
1698 // Creates a GT_SIMD tree for Select operation
1701 // typeHnd - type handle of SIMD vector
1702 // baseType - base type of SIMD vector
1703 // size - SIMD vector size
1704 // op1 - first operand = Condition vector vc
1705 // op2 - second operand = va
1706 // op3 - third operand = vb
1709 // Returns GT_SIMD tree that computes Select(vc, va, vb)
1711 GenTree* Compiler::impSIMDSelect(
1712 CORINFO_CLASS_HANDLE typeHnd, var_types baseType, unsigned size, GenTree* op1, GenTree* op2, GenTree* op3)
1714 assert(varTypeIsSIMD(op1));
1715 var_types simdType = op1->TypeGet();
1716 assert(op2->TypeGet() == simdType);
1717 assert(op3->TypeGet() == simdType);
1719 // TODO-ARM64-CQ Support generating select instruction for SIMD
1721 // Select(BitVector vc, va, vb) = (va & vc) | (vb & !vc)
1722 // Select(op1, op2, op3) = (op2 & op1) | (op3 & !op1)
1723 // = SIMDIntrinsicBitwiseOr(SIMDIntrinsicBitwiseAnd(op2, op1),
1724 // SIMDIntrinsicBitwiseAndNot(op3, op1))
1726 // If Op1 has side effect, create an assignment to a temp
1728 GenTree* asg = nullptr;
1729 if ((op1->gtFlags & GTF_SIDE_EFFECT) != 0)
1731 unsigned lclNum = lvaGrabTemp(true DEBUGARG("SIMD Select"));
1732 lvaSetStruct(lclNum, typeHnd, false);
1733 tmp = gtNewLclvNode(lclNum, op1->TypeGet());
1734 asg = gtNewTempAssign(lclNum, op1);
1737 GenTree* andExpr = gtNewSIMDNode(simdType, op2, tmp, SIMDIntrinsicBitwiseAnd, baseType, size);
1738 GenTree* dupOp1 = gtCloneExpr(tmp);
1739 assert(dupOp1 != nullptr);
1740 #ifdef _TARGET_ARM64_
1741 // ARM64 implements SIMDIntrinsicBitwiseAndNot as Left & ~Right
1742 GenTree* andNotExpr = gtNewSIMDNode(simdType, op3, dupOp1, SIMDIntrinsicBitwiseAndNot, baseType, size);
1744 // XARCH implements SIMDIntrinsicBitwiseAndNot as ~Left & Right
1745 GenTree* andNotExpr = gtNewSIMDNode(simdType, dupOp1, op3, SIMDIntrinsicBitwiseAndNot, baseType, size);
1747 GenTree* simdTree = gtNewSIMDNode(simdType, andExpr, andNotExpr, SIMDIntrinsicBitwiseOr, baseType, size);
1749 // If asg not null, create a GT_COMMA tree.
1752 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), asg, simdTree);
1758 // Creates a GT_SIMD tree for Min/Max operation
1761 // IntrinsicId - SIMD intrinsic Id, either Min or Max
1762 // typeHnd - type handle of SIMD vector
1763 // baseType - base type of SIMD vector
1764 // size - SIMD vector size
1765 // op1 - first operand = va
1766 // op2 - second operand = vb
1769 // Returns GT_SIMD tree that computes Max(va, vb)
1771 GenTree* Compiler::impSIMDMinMax(SIMDIntrinsicID intrinsicId,
1772 CORINFO_CLASS_HANDLE typeHnd,
1778 assert(intrinsicId == SIMDIntrinsicMin || intrinsicId == SIMDIntrinsicMax);
1779 assert(varTypeIsSIMD(op1));
1780 var_types simdType = op1->TypeGet();
1781 assert(op2->TypeGet() == simdType);
1783 #if defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
1784 GenTree* simdTree = nullptr;
1786 #ifdef _TARGET_XARCH_
1787 // SSE2 has direct support for float/double/signed word/unsigned byte.
1788 // SSE4.1 has direct support for int32/uint32/signed byte/unsigned word.
1789 // For other integer types we compute min/max as follows
1791 // int32/uint32 (SSE2)
1792 // int64/uint64 (SSE2&SSE4):
1793 // compResult = (op1 < op2) in case of Min
1794 // (op1 > op2) in case of Max
1795 // Min/Max(op1, op2) = Select(compResult, op1, op2)
1797 // unsigned word (SSE2):
1798 // op1 = op1 - 2^15 ; to make it fit within a signed word
1799 // op2 = op2 - 2^15 ; to make it fit within a signed word
1800 // result = SSE2 signed word Min/Max(op1, op2)
1801 // result = result + 2^15 ; readjust it back
1803 // signed byte (SSE2):
1804 // op1 = op1 + 2^7 ; to make it unsigned
1805 // op1 = op1 + 2^7 ; to make it unsigned
1806 // result = SSE2 unsigned byte Min/Max(op1, op2)
1807 // result = result - 2^15 ; readjust it back
1809 if (varTypeIsFloating(baseType) || baseType == TYP_SHORT || baseType == TYP_UBYTE ||
1810 (getSIMDSupportLevel() >= SIMD_SSE4_Supported &&
1811 (baseType == TYP_BYTE || baseType == TYP_INT || baseType == TYP_UINT || baseType == TYP_USHORT)))
1813 // SSE2 or SSE4.1 has direct support
1814 simdTree = gtNewSIMDNode(simdType, op1, op2, intrinsicId, baseType, size);
1816 else if (baseType == TYP_USHORT || baseType == TYP_BYTE)
1818 assert(getSIMDSupportLevel() == SIMD_SSE2_Supported);
1820 SIMDIntrinsicID operIntrinsic;
1821 SIMDIntrinsicID adjustIntrinsic;
1822 var_types minMaxOperBaseType;
1823 if (baseType == TYP_USHORT)
1825 constVal = 0x80008000;
1826 operIntrinsic = SIMDIntrinsicSub;
1827 adjustIntrinsic = SIMDIntrinsicAdd;
1828 minMaxOperBaseType = TYP_SHORT;
1832 assert(baseType == TYP_BYTE);
1833 constVal = 0x80808080;
1834 operIntrinsic = SIMDIntrinsicAdd;
1835 adjustIntrinsic = SIMDIntrinsicSub;
1836 minMaxOperBaseType = TYP_UBYTE;
1839 GenTree* initVal = gtNewIconNode(constVal);
1840 GenTree* constVector = gtNewSIMDNode(simdType, initVal, nullptr, SIMDIntrinsicInit, TYP_INT, size);
1842 // Assign constVector to a temp, since we intend to use it more than once
1843 // TODO-CQ: We have quite a few such constant vectors constructed during
1844 // the importation of SIMD intrinsics. Make sure that we have a single
1845 // temp per distinct constant per method.
1846 GenTree* tmp = fgInsertCommaFormTemp(&constVector, typeHnd);
1848 // op1 = op1 - constVector
1849 // op2 = op2 - constVector
1850 op1 = gtNewSIMDNode(simdType, op1, constVector, operIntrinsic, baseType, size);
1851 op2 = gtNewSIMDNode(simdType, op2, tmp, operIntrinsic, baseType, size);
1853 // compute min/max of op1 and op2 considering them as if minMaxOperBaseType
1854 simdTree = gtNewSIMDNode(simdType, op1, op2, intrinsicId, minMaxOperBaseType, size);
1856 // re-adjust the value by adding or subtracting constVector
1857 tmp = gtNewLclvNode(tmp->AsLclVarCommon()->GetLclNum(), tmp->TypeGet());
1858 simdTree = gtNewSIMDNode(simdType, simdTree, tmp, adjustIntrinsic, baseType, size);
1860 #elif defined(_TARGET_ARM64_)
1861 // Arm64 has direct support for all types except int64/uint64
1862 // For which we compute min/max as follows
1865 // compResult = (op1 < op2) in case of Min
1866 // (op1 > op2) in case of Max
1867 // Min/Max(op1, op2) = Select(compResult, op1, op2)
1868 if (baseType != TYP_ULONG && baseType != TYP_LONG)
1870 simdTree = gtNewSIMDNode(simdType, op1, op2, intrinsicId, baseType, size);
1875 GenTree* dupOp1 = nullptr;
1876 GenTree* dupOp2 = nullptr;
1877 GenTree* op1Assign = nullptr;
1878 GenTree* op2Assign = nullptr;
1882 if ((op1->gtFlags & GTF_SIDE_EFFECT) != 0)
1884 op1LclNum = lvaGrabTemp(true DEBUGARG("SIMD Min/Max"));
1885 dupOp1 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1886 lvaSetStruct(op1LclNum, typeHnd, false);
1887 op1Assign = gtNewTempAssign(op1LclNum, op1);
1888 op1 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1892 dupOp1 = gtCloneExpr(op1);
1895 if ((op2->gtFlags & GTF_SIDE_EFFECT) != 0)
1897 op2LclNum = lvaGrabTemp(true DEBUGARG("SIMD Min/Max"));
1898 dupOp2 = gtNewLclvNode(op2LclNum, op2->TypeGet());
1899 lvaSetStruct(op2LclNum, typeHnd, false);
1900 op2Assign = gtNewTempAssign(op2LclNum, op2);
1901 op2 = gtNewLclvNode(op2LclNum, op2->TypeGet());
1905 dupOp2 = gtCloneExpr(op2);
1908 SIMDIntrinsicID relOpIntrinsic =
1909 (intrinsicId == SIMDIntrinsicMin) ? SIMDIntrinsicLessThan : SIMDIntrinsicGreaterThan;
1910 var_types relOpBaseType = baseType;
1912 // compResult = op1 relOp op2
1913 // simdTree = Select(compResult, op1, op2);
1914 assert(dupOp1 != nullptr);
1915 assert(dupOp2 != nullptr);
1916 relOpIntrinsic = impSIMDRelOp(relOpIntrinsic, typeHnd, size, &relOpBaseType, &dupOp1, &dupOp2);
1917 GenTree* compResult = gtNewSIMDNode(simdType, dupOp1, dupOp2, relOpIntrinsic, relOpBaseType, size);
1918 unsigned compResultLclNum = lvaGrabTemp(true DEBUGARG("SIMD Min/Max"));
1919 lvaSetStruct(compResultLclNum, typeHnd, false);
1920 GenTree* compResultAssign = gtNewTempAssign(compResultLclNum, compResult);
1921 compResult = gtNewLclvNode(compResultLclNum, compResult->TypeGet());
1922 simdTree = impSIMDSelect(typeHnd, baseType, size, compResult, op1, op2);
1923 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), compResultAssign, simdTree);
1925 // Now create comma trees if we have created assignments of op1/op2 to temps
1926 if (op2Assign != nullptr)
1928 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), op2Assign, simdTree);
1931 if (op1Assign != nullptr)
1933 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), op1Assign, simdTree);
1937 assert(simdTree != nullptr);
1939 #else // !(defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_))
1940 assert(!"impSIMDMinMax() unimplemented on target arch");
1942 #endif // !(defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_))
1945 //------------------------------------------------------------------------
1946 // getOp1ForConstructor: Get the op1 for a constructor call.
1949 // opcode - the opcode being handled (needed to identify the CEE_NEWOBJ case)
1950 // newobjThis - For CEE_NEWOBJ, this is the temp grabbed for the allocated uninitalized object.
1951 // clsHnd - The handle of the class of the method.
1954 // The tree node representing the object to be initialized with the constructor.
1957 // This method handles the differences between the CEE_NEWOBJ and constructor cases.
1959 GenTree* Compiler::getOp1ForConstructor(OPCODE opcode, GenTree* newobjThis, CORINFO_CLASS_HANDLE clsHnd)
1962 if (opcode == CEE_NEWOBJ)
1965 assert(newobjThis->gtOper == GT_ADDR && newobjThis->gtOp.gtOp1->gtOper == GT_LCL_VAR);
1967 // push newobj result on type stack
1968 unsigned tmp = op1->gtOp.gtOp1->gtLclVarCommon.gtLclNum;
1969 impPushOnStack(gtNewLclvNode(tmp, lvaGetRealType(tmp)), verMakeTypeInfo(clsHnd).NormaliseForStack());
1973 op1 = impSIMDPopStack(TYP_BYREF);
1975 assert(op1->TypeGet() == TYP_BYREF);
1979 //-------------------------------------------------------------------
1980 // Set the flag that indicates that the lclVar referenced by this tree
1981 // is used in a SIMD intrinsic.
1985 void Compiler::setLclRelatedToSIMDIntrinsic(GenTree* tree)
1987 assert(tree->OperIsLocal());
1988 unsigned lclNum = tree->AsLclVarCommon()->GetLclNum();
1989 LclVarDsc* lclVarDsc = &lvaTable[lclNum];
1990 lclVarDsc->lvUsedInSIMDIntrinsic = true;
1993 //-------------------------------------------------------------
1994 // Check if two field nodes reference at the same memory location.
1995 // Notice that this check is just based on pattern matching.
2000 // If op1's parents node and op2's parents node are at the same location, return true. Otherwise, return false
2002 bool areFieldsParentsLocatedSame(GenTree* op1, GenTree* op2)
2004 assert(op1->OperGet() == GT_FIELD);
2005 assert(op2->OperGet() == GT_FIELD);
2007 GenTree* op1ObjRef = op1->gtField.gtFldObj;
2008 GenTree* op2ObjRef = op2->gtField.gtFldObj;
2009 while (op1ObjRef != nullptr && op2ObjRef != nullptr)
2012 if (op1ObjRef->OperGet() != op2ObjRef->OperGet())
2016 else if (op1ObjRef->OperGet() == GT_ADDR)
2018 op1ObjRef = op1ObjRef->gtOp.gtOp1;
2019 op2ObjRef = op2ObjRef->gtOp.gtOp1;
2022 if (op1ObjRef->OperIsLocal() && op2ObjRef->OperIsLocal() &&
2023 op1ObjRef->AsLclVarCommon()->GetLclNum() == op2ObjRef->AsLclVarCommon()->GetLclNum())
2027 else if (op1ObjRef->OperGet() == GT_FIELD && op2ObjRef->OperGet() == GT_FIELD &&
2028 op1ObjRef->gtField.gtFldHnd == op2ObjRef->gtField.gtFldHnd)
2030 op1ObjRef = op1ObjRef->gtField.gtFldObj;
2031 op2ObjRef = op2ObjRef->gtField.gtFldObj;
2043 //----------------------------------------------------------------------
2044 // Check whether two field are contiguous
2046 // first - GenTree*. The Type of the node should be TYP_FLOAT
2047 // second - GenTree*. The Type of the node should be TYP_FLOAT
2049 // if the first field is located before second field, and they are located contiguously,
2050 // then return true. Otherwise, return false.
2052 bool Compiler::areFieldsContiguous(GenTree* first, GenTree* second)
2054 assert(first->OperGet() == GT_FIELD);
2055 assert(second->OperGet() == GT_FIELD);
2056 assert(first->gtType == TYP_FLOAT);
2057 assert(second->gtType == TYP_FLOAT);
2059 var_types firstFieldType = first->gtType;
2060 var_types secondFieldType = second->gtType;
2062 unsigned firstFieldEndOffset = first->gtField.gtFldOffset + genTypeSize(firstFieldType);
2063 unsigned secondFieldOffset = second->gtField.gtFldOffset;
2064 if (firstFieldEndOffset == secondFieldOffset && firstFieldType == secondFieldType &&
2065 areFieldsParentsLocatedSame(first, second))
2073 //-------------------------------------------------------------------------------
2074 // Check whether two array element nodes are located contiguously or not.
2079 // if the array element op1 is located before array element op2, and they are contiguous,
2080 // then return true. Otherwise, return false.
2082 // Right this can only check array element with const number as index. In future,
2083 // we should consider to allow this function to check the index using expression.
2085 bool Compiler::areArrayElementsContiguous(GenTree* op1, GenTree* op2)
2087 noway_assert(op1->gtOper == GT_INDEX);
2088 noway_assert(op2->gtOper == GT_INDEX);
2089 GenTreeIndex* op1Index = op1->AsIndex();
2090 GenTreeIndex* op2Index = op2->AsIndex();
2092 GenTree* op1ArrayRef = op1Index->Arr();
2093 GenTree* op2ArrayRef = op2Index->Arr();
2094 assert(op1ArrayRef->TypeGet() == TYP_REF);
2095 assert(op2ArrayRef->TypeGet() == TYP_REF);
2097 GenTree* op1IndexNode = op1Index->Index();
2098 GenTree* op2IndexNode = op2Index->Index();
2099 if ((op1IndexNode->OperGet() == GT_CNS_INT && op2IndexNode->OperGet() == GT_CNS_INT) &&
2100 op1IndexNode->gtIntCon.gtIconVal + 1 == op2IndexNode->gtIntCon.gtIconVal)
2102 if (op1ArrayRef->OperGet() == GT_FIELD && op2ArrayRef->OperGet() == GT_FIELD &&
2103 areFieldsParentsLocatedSame(op1ArrayRef, op2ArrayRef))
2107 else if (op1ArrayRef->OperIsLocal() && op2ArrayRef->OperIsLocal() &&
2108 op1ArrayRef->AsLclVarCommon()->GetLclNum() == op2ArrayRef->AsLclVarCommon()->GetLclNum())
2116 //-------------------------------------------------------------------------------
2117 // Check whether two argument nodes are contiguous or not.
2122 // if the argument node op1 is located before argument node op2, and they are located contiguously,
2123 // then return true. Otherwise, return false.
2125 // Right now this can only check field and array. In future we should add more cases.
2128 bool Compiler::areArgumentsContiguous(GenTree* op1, GenTree* op2)
2130 if (op1->OperGet() == GT_INDEX && op2->OperGet() == GT_INDEX)
2132 return areArrayElementsContiguous(op1, op2);
2134 else if (op1->OperGet() == GT_FIELD && op2->OperGet() == GT_FIELD)
2136 return areFieldsContiguous(op1, op2);
2141 //--------------------------------------------------------------------------------------------------------
2142 // createAddressNodeForSIMDInit: Generate the address node(GT_LEA) if we want to intialize vector2, vector3 or vector4
2143 // from first argument's address.
2146 // tree - GenTree*. This the tree node which is used to get the address for indir.
2147 // simdsize - unsigned. This the simd vector size.
2148 // arrayElementsCount - unsigned. This is used for generating the boundary check for array.
2151 // return the address node.
2154 // 1. Currently just support for GT_FIELD and GT_INDEX, because we can only verify the GT_INDEX node or GT_Field
2155 // are located contiguously or not. In future we should support more cases.
2156 // 2. Though it happens to just work fine front-end phases are not aware of GT_LEA node. Therefore, convert these
2158 GenTree* Compiler::createAddressNodeForSIMDInit(GenTree* tree, unsigned simdSize)
2160 assert(tree->OperGet() == GT_FIELD || tree->OperGet() == GT_INDEX);
2161 GenTree* byrefNode = nullptr;
2162 GenTree* startIndex = nullptr;
2163 unsigned offset = 0;
2164 var_types baseType = tree->gtType;
2166 if (tree->OperGet() == GT_FIELD)
2168 GenTree* objRef = tree->gtField.gtFldObj;
2169 if (objRef != nullptr && objRef->gtOper == GT_ADDR)
2171 GenTree* obj = objRef->gtOp.gtOp1;
2173 // If the field is directly from a struct, then in this case,
2174 // we should set this struct's lvUsedInSIMDIntrinsic as true,
2175 // so that this sturct won't be promoted.
2176 // e.g. s.x x is a field, and s is a struct, then we should set the s's lvUsedInSIMDIntrinsic as true.
2177 // so that s won't be promoted.
2178 // Notice that if we have a case like s1.s2.x. s1 s2 are struct, and x is a field, then it is possible that
2179 // s1 can be promoted, so that s2 can be promoted. The reason for that is if we don't allow s1 to be
2180 // promoted, then this will affect the other optimizations which are depend on s1's struct promotion.
2182 // In future, we should optimize this case so that if there is a nested field like s1.s2.x and s1.s2.x's
2183 // address is used for initializing the vector, then s1 can be promoted but s2 can't.
2184 if (varTypeIsSIMD(obj) && obj->OperIsLocal())
2186 setLclRelatedToSIMDIntrinsic(obj);
2190 byrefNode = gtCloneExpr(tree->gtField.gtFldObj);
2191 assert(byrefNode != nullptr);
2192 offset = tree->gtField.gtFldOffset;
2194 else if (tree->OperGet() == GT_INDEX)
2197 GenTree* index = tree->AsIndex()->Index();
2198 assert(index->OperGet() == GT_CNS_INT);
2200 GenTree* checkIndexExpr = nullptr;
2201 unsigned indexVal = (unsigned)(index->gtIntCon.gtIconVal);
2202 offset = indexVal * genTypeSize(tree->TypeGet());
2203 GenTree* arrayRef = tree->AsIndex()->Arr();
2205 // Generate the boundary check exception.
2206 // The length for boundary check should be the maximum index number which should be
2207 // (first argument's index number) + (how many array arguments we have) - 1
2208 // = indexVal + arrayElementsCount - 1
2209 unsigned arrayElementsCount = simdSize / genTypeSize(baseType);
2210 checkIndexExpr = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, indexVal + arrayElementsCount - 1);
2211 GenTreeArrLen* arrLen = gtNewArrLen(TYP_INT, arrayRef, (int)OFFSETOF__CORINFO_Array__length);
2212 GenTreeBoundsChk* arrBndsChk = new (this, GT_ARR_BOUNDS_CHECK)
2213 GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, checkIndexExpr, arrLen, SCK_RNGCHK_FAIL);
2215 offset += OFFSETOF__CORINFO_Array__data;
2216 byrefNode = gtNewOperNode(GT_COMMA, arrayRef->TypeGet(), arrBndsChk, gtCloneExpr(arrayRef));
2223 new (this, GT_LEA) GenTreeAddrMode(TYP_BYREF, byrefNode, startIndex, genTypeSize(tree->TypeGet()), offset);
2227 //-------------------------------------------------------------------------------
2228 // impMarkContiguousSIMDFieldAssignments: Try to identify if there are contiguous
2229 // assignments from SIMD field to memory. If there are, then mark the related
2230 // lclvar so that it won't be promoted.
2233 // stmt - GenTree*. Input statement node.
2235 void Compiler::impMarkContiguousSIMDFieldAssignments(GenTree* stmt)
2237 if (!featureSIMD || opts.MinOpts())
2241 GenTree* expr = stmt->gtStmt.gtStmtExpr;
2242 if (expr->OperGet() == GT_ASG && expr->TypeGet() == TYP_FLOAT)
2244 GenTree* curDst = expr->gtOp.gtOp1;
2245 GenTree* curSrc = expr->gtOp.gtOp2;
2247 var_types baseType = TYP_UNKNOWN;
2248 unsigned simdSize = 0;
2249 GenTree* srcSimdStructNode = getSIMDStructFromField(curSrc, &baseType, &index, &simdSize, true);
2250 if (srcSimdStructNode == nullptr || baseType != TYP_FLOAT)
2252 fgPreviousCandidateSIMDFieldAsgStmt = nullptr;
2254 else if (index == 0 && isSIMDTypeLocal(srcSimdStructNode))
2256 fgPreviousCandidateSIMDFieldAsgStmt = stmt;
2258 else if (fgPreviousCandidateSIMDFieldAsgStmt != nullptr)
2261 GenTree* prevAsgExpr = fgPreviousCandidateSIMDFieldAsgStmt->gtStmt.gtStmtExpr;
2262 GenTree* prevDst = prevAsgExpr->gtOp.gtOp1;
2263 GenTree* prevSrc = prevAsgExpr->gtOp.gtOp2;
2264 if (!areArgumentsContiguous(prevDst, curDst) || !areArgumentsContiguous(prevSrc, curSrc))
2266 fgPreviousCandidateSIMDFieldAsgStmt = nullptr;
2270 if (index == (simdSize / genTypeSize(baseType) - 1))
2272 // Successfully found the pattern, mark the lclvar as UsedInSIMDIntrinsic
2273 if (srcSimdStructNode->OperIsLocal())
2275 setLclRelatedToSIMDIntrinsic(srcSimdStructNode);
2278 if (curDst->OperGet() == GT_FIELD)
2280 GenTree* objRef = curDst->gtField.gtFldObj;
2281 if (objRef != nullptr && objRef->gtOper == GT_ADDR)
2283 GenTree* obj = objRef->gtOp.gtOp1;
2284 if (varTypeIsStruct(obj) && obj->OperIsLocal())
2286 setLclRelatedToSIMDIntrinsic(obj);
2293 fgPreviousCandidateSIMDFieldAsgStmt = stmt;
2300 fgPreviousCandidateSIMDFieldAsgStmt = nullptr;
2304 //------------------------------------------------------------------------
2305 // impSIMDIntrinsic: Check method to see if it is a SIMD method
2308 // opcode - the opcode being handled (needed to identify the CEE_NEWOBJ case)
2309 // newobjThis - For CEE_NEWOBJ, this is the temp grabbed for the allocated uninitalized object.
2310 // clsHnd - The handle of the class of the method.
2311 // method - The handle of the method.
2312 // sig - The call signature for the method.
2313 // memberRef - The memberRef token for the method reference.
2316 // If clsHnd is a known SIMD type, and 'method' is one of the methods that are
2317 // implemented as an intrinsic in the JIT, then return the tree that implements
2320 GenTree* Compiler::impSIMDIntrinsic(OPCODE opcode,
2321 GenTree* newobjThis,
2322 CORINFO_CLASS_HANDLE clsHnd,
2323 CORINFO_METHOD_HANDLE methodHnd,
2324 CORINFO_SIG_INFO* sig,
2327 assert(featureSIMD);
2329 if (!isSIMDClass(clsHnd))
2334 // Get base type and intrinsic Id
2335 var_types baseType = TYP_UNKNOWN;
2337 unsigned argCount = 0;
2338 const SIMDIntrinsicInfo* intrinsicInfo =
2339 getSIMDIntrinsicInfo(&clsHnd, methodHnd, sig, (opcode == CEE_NEWOBJ), &argCount, &baseType, &size);
2340 if (intrinsicInfo == nullptr || intrinsicInfo->id == SIMDIntrinsicInvalid)
2345 SIMDIntrinsicID simdIntrinsicID = intrinsicInfo->id;
2347 if (baseType != TYP_UNKNOWN)
2349 simdType = getSIMDTypeForSize(size);
2353 assert(simdIntrinsicID == SIMDIntrinsicHWAccel);
2354 simdType = TYP_UNKNOWN;
2356 bool instMethod = intrinsicInfo->isInstMethod;
2357 var_types callType = JITtype2varType(sig->retType);
2358 if (callType == TYP_STRUCT)
2360 // Note that here we are assuming that, if the call returns a struct, that it is the same size as the
2361 // struct on which the method is declared. This is currently true for all methods on Vector types,
2362 // but if this ever changes, we will need to determine the callType from the signature.
2363 assert(info.compCompHnd->getClassSize(sig->retTypeClass) == genTypeSize(simdType));
2364 callType = simdType;
2367 GenTree* simdTree = nullptr;
2368 GenTree* op1 = nullptr;
2369 GenTree* op2 = nullptr;
2370 GenTree* op3 = nullptr;
2371 GenTree* retVal = nullptr;
2372 GenTree* copyBlkDst = nullptr;
2373 bool doCopyBlk = false;
2375 switch (simdIntrinsicID)
2377 case SIMDIntrinsicGetCount:
2379 int length = getSIMDVectorLength(clsHnd);
2380 GenTreeIntCon* intConstTree = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, length);
2381 retVal = intConstTree;
2383 intConstTree->gtFlags |= GTF_ICON_SIMD_COUNT;
2387 case SIMDIntrinsicGetZero:
2388 retVal = gtNewSIMDVectorZero(simdType, baseType, size);
2391 case SIMDIntrinsicGetOne:
2392 retVal = gtNewSIMDVectorOne(simdType, baseType, size);
2395 case SIMDIntrinsicGetAllOnes:
2397 // Equivalent to (Vector<T>) new Vector<int>(0xffffffff);
2398 GenTree* initVal = gtNewIconNode(0xffffffff, TYP_INT);
2399 simdTree = gtNewSIMDNode(simdType, initVal, nullptr, SIMDIntrinsicInit, TYP_INT, size);
2400 if (baseType != TYP_INT)
2402 // cast it to required baseType if different from TYP_INT
2403 simdTree = gtNewSIMDNode(simdType, simdTree, nullptr, SIMDIntrinsicCast, baseType, size);
2409 case SIMDIntrinsicInit:
2410 case SIMDIntrinsicInitN:
2412 // SIMDIntrinsicInit:
2413 // op2 - the initializer value
2414 // op1 - byref of vector
2416 // SIMDIntrinsicInitN
2417 // op2 - list of initializer values stitched into a list
2418 // op1 - byref of vector
2419 bool initFromFirstArgIndir = false;
2420 if (simdIntrinsicID == SIMDIntrinsicInit)
2422 op2 = impSIMDPopStack(baseType);
2426 assert(simdIntrinsicID == SIMDIntrinsicInitN);
2427 assert(baseType == TYP_FLOAT);
2429 unsigned initCount = argCount - 1;
2430 unsigned elementCount = getSIMDVectorLength(size, baseType);
2431 noway_assert(initCount == elementCount);
2432 GenTree* nextArg = op2;
2434 // Build a GT_LIST with the N values.
2435 // We must maintain left-to-right order of the args, but we will pop
2436 // them off in reverse order (the Nth arg was pushed onto the stack last).
2438 GenTree* list = nullptr;
2439 GenTree* firstArg = nullptr;
2440 GenTree* prevArg = nullptr;
2442 bool areArgsContiguous = true;
2443 for (unsigned i = 0; i < initCount; i++)
2445 GenTree* nextArg = impSIMDPopStack(baseType);
2446 if (areArgsContiguous)
2448 GenTree* curArg = nextArg;
2451 if (prevArg != nullptr)
2453 // Recall that we are popping the args off the stack in reverse order.
2454 areArgsContiguous = areArgumentsContiguous(curArg, prevArg);
2459 list = new (this, GT_LIST) GenTreeOp(GT_LIST, baseType, nextArg, list);
2462 if (areArgsContiguous && baseType == TYP_FLOAT)
2464 // Since Vector2, Vector3 and Vector4's arguments type are only float,
2465 // we intialize the vector from first argument address, only when
2466 // the baseType is TYP_FLOAT and the arguments are located contiguously in memory
2467 initFromFirstArgIndir = true;
2468 GenTree* op2Address = createAddressNodeForSIMDInit(firstArg, size);
2469 var_types simdType = getSIMDTypeForSize(size);
2470 op2 = gtNewOperNode(GT_IND, simdType, op2Address);
2478 op1 = getOp1ForConstructor(opcode, newobjThis, clsHnd);
2480 assert(op1->TypeGet() == TYP_BYREF);
2481 assert(genActualType(op2->TypeGet()) == genActualType(baseType) || initFromFirstArgIndir);
2483 // For integral base types of size less than TYP_INT, expand the initializer
2484 // to fill size of TYP_INT bytes.
2485 if (varTypeIsSmallInt(baseType))
2487 // This case should occur only for Init intrinsic.
2488 assert(simdIntrinsicID == SIMDIntrinsicInit);
2490 unsigned baseSize = genTypeSize(baseType);
2494 multiplier = 0x01010101;
2498 assert(baseSize == 2);
2499 multiplier = 0x00010001;
2502 GenTree* t1 = nullptr;
2503 if (baseType == TYP_BYTE)
2505 // What we have is a signed byte initializer,
2506 // which when loaded to a reg will get sign extended to TYP_INT.
2507 // But what we need is the initializer without sign extended or
2508 // rather zero extended to 32-bits.
2509 t1 = gtNewOperNode(GT_AND, TYP_INT, op2, gtNewIconNode(0xff, TYP_INT));
2511 else if (baseType == TYP_SHORT)
2513 // What we have is a signed short initializer,
2514 // which when loaded to a reg will get sign extended to TYP_INT.
2515 // But what we need is the initializer without sign extended or
2516 // rather zero extended to 32-bits.
2517 t1 = gtNewOperNode(GT_AND, TYP_INT, op2, gtNewIconNode(0xffff, TYP_INT));
2521 assert(baseType == TYP_UBYTE || baseType == TYP_USHORT);
2522 t1 = gtNewCastNode(TYP_INT, op2, false, TYP_INT);
2525 assert(t1 != nullptr);
2526 GenTree* t2 = gtNewIconNode(multiplier, TYP_INT);
2527 op2 = gtNewOperNode(GT_MUL, TYP_INT, t1, t2);
2529 // Construct a vector of TYP_INT with the new initializer and cast it back to vector of baseType
2530 simdTree = gtNewSIMDNode(simdType, op2, nullptr, simdIntrinsicID, TYP_INT, size);
2531 simdTree = gtNewSIMDNode(simdType, simdTree, nullptr, SIMDIntrinsicCast, baseType, size);
2536 if (initFromFirstArgIndir)
2539 if (op1->gtOp.gtOp1->OperIsLocal())
2541 // label the dst struct's lclvar is used for SIMD intrinsic,
2542 // so that this dst struct won't be promoted.
2543 setLclRelatedToSIMDIntrinsic(op1->gtOp.gtOp1);
2548 simdTree = gtNewSIMDNode(simdType, op2, nullptr, simdIntrinsicID, baseType, size);
2557 case SIMDIntrinsicInitArray:
2558 case SIMDIntrinsicInitArrayX:
2559 case SIMDIntrinsicCopyToArray:
2560 case SIMDIntrinsicCopyToArrayX:
2562 // op3 - index into array in case of SIMDIntrinsicCopyToArrayX and SIMDIntrinsicInitArrayX
2563 // op2 - array itself
2564 // op1 - byref to vector struct
2566 unsigned int vectorLength = getSIMDVectorLength(size, baseType);
2567 // (This constructor takes only the zero-based arrays.)
2568 // We will add one or two bounds checks:
2569 // 1. If we have an index, we must do a check on that first.
2570 // We can't combine it with the index + vectorLength check because
2571 // a. It might be negative, and b. It may need to raise a different exception
2572 // (captured as SCK_ARG_RNG_EXCPN for CopyTo and SCK_RNGCHK_FAIL for Init).
2573 // 2. We need to generate a check (SCK_ARG_EXCPN for CopyTo and SCK_RNGCHK_FAIL for Init)
2574 // for the last array element we will access.
2575 // We'll either check against (vectorLength - 1) or (index + vectorLength - 1).
2577 GenTree* checkIndexExpr = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, vectorLength - 1);
2579 // Get the index into the array. If it has been provided, it will be on the
2580 // top of the stack. Otherwise, it is null.
2583 op3 = impSIMDPopStack(TYP_INT);
2584 if (op3->IsIntegralConst(0))
2591 // TODO-CQ: Here, or elsewhere, check for the pattern where op2 is a newly constructed array, and
2592 // change this to the InitN form.
2593 // op3 = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, 0);
2597 // Clone the array for use in the bounds check.
2598 op2 = impSIMDPopStack(TYP_REF);
2599 assert(op2->TypeGet() == TYP_REF);
2600 GenTree* arrayRefForArgChk = op2;
2601 GenTree* argRngChk = nullptr;
2602 GenTree* asg = nullptr;
2603 if ((arrayRefForArgChk->gtFlags & GTF_SIDE_EFFECT) != 0)
2605 op2 = fgInsertCommaFormTemp(&arrayRefForArgChk);
2609 op2 = gtCloneExpr(arrayRefForArgChk);
2611 assert(op2 != nullptr);
2615 SpecialCodeKind op3CheckKind;
2616 if (simdIntrinsicID == SIMDIntrinsicInitArrayX)
2618 op3CheckKind = SCK_RNGCHK_FAIL;
2622 assert(simdIntrinsicID == SIMDIntrinsicCopyToArrayX);
2623 op3CheckKind = SCK_ARG_RNG_EXCPN;
2625 // We need to use the original expression on this, which is the first check.
2626 GenTree* arrayRefForArgRngChk = arrayRefForArgChk;
2627 // Then we clone the clone we just made for the next check.
2628 arrayRefForArgChk = gtCloneExpr(op2);
2629 // We know we MUST have had a cloneable expression.
2630 assert(arrayRefForArgChk != nullptr);
2631 GenTree* index = op3;
2632 if ((index->gtFlags & GTF_SIDE_EFFECT) != 0)
2634 op3 = fgInsertCommaFormTemp(&index);
2638 op3 = gtCloneExpr(index);
2641 GenTreeArrLen* arrLen =
2642 gtNewArrLen(TYP_INT, arrayRefForArgRngChk, (int)OFFSETOF__CORINFO_Array__length);
2643 argRngChk = new (this, GT_ARR_BOUNDS_CHECK)
2644 GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, index, arrLen, op3CheckKind);
2645 // Now, clone op3 to create another node for the argChk
2646 GenTree* index2 = gtCloneExpr(op3);
2647 assert(index != nullptr);
2648 checkIndexExpr = gtNewOperNode(GT_ADD, TYP_INT, index2, checkIndexExpr);
2651 // Insert a bounds check for index + offset - 1.
2652 // This must be a "normal" array.
2653 SpecialCodeKind op2CheckKind;
2654 if (simdIntrinsicID == SIMDIntrinsicInitArray || simdIntrinsicID == SIMDIntrinsicInitArrayX)
2656 op2CheckKind = SCK_RNGCHK_FAIL;
2660 op2CheckKind = SCK_ARG_EXCPN;
2662 GenTreeArrLen* arrLen = gtNewArrLen(TYP_INT, arrayRefForArgChk, (int)OFFSETOF__CORINFO_Array__length);
2663 GenTreeBoundsChk* argChk = new (this, GT_ARR_BOUNDS_CHECK)
2664 GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, checkIndexExpr, arrLen, op2CheckKind);
2666 // Create a GT_COMMA tree for the bounds check(s).
2667 op2 = gtNewOperNode(GT_COMMA, op2->TypeGet(), argChk, op2);
2668 if (argRngChk != nullptr)
2670 op2 = gtNewOperNode(GT_COMMA, op2->TypeGet(), argRngChk, op2);
2673 if (simdIntrinsicID == SIMDIntrinsicInitArray || simdIntrinsicID == SIMDIntrinsicInitArrayX)
2675 op1 = getOp1ForConstructor(opcode, newobjThis, clsHnd);
2676 simdTree = gtNewSIMDNode(simdType, op2, op3, SIMDIntrinsicInitArray, baseType, size);
2682 assert(simdIntrinsicID == SIMDIntrinsicCopyToArray || simdIntrinsicID == SIMDIntrinsicCopyToArrayX);
2683 op1 = impSIMDPopStack(simdType, instMethod);
2684 assert(op1->TypeGet() == simdType);
2686 // copy vector (op1) to array (op2) starting at index (op3)
2689 // TODO-Cleanup: Though it happens to just work fine front-end phases are not aware of GT_LEA node.
2690 // Therefore, convert these to use GT_ADDR .
2691 copyBlkDst = new (this, GT_LEA)
2692 GenTreeAddrMode(TYP_BYREF, op2, op3, genTypeSize(baseType), OFFSETOF__CORINFO_Array__data);
2698 case SIMDIntrinsicInitFixed:
2700 // We are initializing a fixed-length vector VLarge with a smaller fixed-length vector VSmall, plus 1 or 2
2701 // additional floats.
2702 // op4 (optional) - float value for VLarge.W, if VLarge is Vector4, and VSmall is Vector2
2703 // op3 - float value for VLarge.Z or VLarge.W
2705 // op1 - byref of VLarge
2706 assert(baseType == TYP_FLOAT);
2707 unsigned elementByteCount = 4;
2709 GenTree* op4 = nullptr;
2712 op4 = impSIMDPopStack(TYP_FLOAT);
2713 assert(op4->TypeGet() == TYP_FLOAT);
2715 op3 = impSIMDPopStack(TYP_FLOAT);
2716 assert(op3->TypeGet() == TYP_FLOAT);
2717 // The input vector will either be TYP_SIMD8 or TYP_SIMD12.
2718 var_types smallSIMDType = TYP_SIMD8;
2719 if ((op4 == nullptr) && (simdType == TYP_SIMD16))
2721 smallSIMDType = TYP_SIMD12;
2723 op2 = impSIMDPopStack(smallSIMDType);
2724 op1 = getOp1ForConstructor(opcode, newobjThis, clsHnd);
2726 // We are going to redefine the operands so that:
2727 // - op3 is the value that's going into the Z position, or null if it's a Vector4 constructor with a single
2729 // - op4 is the W position value, or null if this is a Vector3 constructor.
2730 if (size == 16 && argCount == 3)
2739 simdTree = gtNewSIMDNode(simdType, simdTree, op3, SIMDIntrinsicSetZ, baseType, size);
2743 simdTree = gtNewSIMDNode(simdType, simdTree, op4, SIMDIntrinsicSetW, baseType, size);
2751 case SIMDIntrinsicOpEquality:
2752 case SIMDIntrinsicInstEquals:
2754 op2 = impSIMDPopStack(simdType);
2755 op1 = impSIMDPopStack(simdType, instMethod);
2757 assert(op1->TypeGet() == simdType);
2758 assert(op2->TypeGet() == simdType);
2760 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, SIMDIntrinsicOpEquality, baseType, size);
2761 if (simdType == TYP_SIMD12)
2763 simdTree->gtFlags |= GTF_SIMD12_OP;
2769 case SIMDIntrinsicOpInEquality:
2771 // op1 is the first operand
2772 // op2 is the second operand
2773 op2 = impSIMDPopStack(simdType);
2774 op1 = impSIMDPopStack(simdType, instMethod);
2775 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, SIMDIntrinsicOpInEquality, baseType, size);
2776 if (simdType == TYP_SIMD12)
2778 simdTree->gtFlags |= GTF_SIMD12_OP;
2784 case SIMDIntrinsicEqual:
2785 case SIMDIntrinsicLessThan:
2786 case SIMDIntrinsicLessThanOrEqual:
2787 case SIMDIntrinsicGreaterThan:
2788 case SIMDIntrinsicGreaterThanOrEqual:
2790 op2 = impSIMDPopStack(simdType);
2791 op1 = impSIMDPopStack(simdType, instMethod);
2793 SIMDIntrinsicID intrinsicID = impSIMDRelOp(simdIntrinsicID, clsHnd, size, &baseType, &op1, &op2);
2794 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, intrinsicID, baseType, size);
2799 case SIMDIntrinsicAdd:
2800 case SIMDIntrinsicSub:
2801 case SIMDIntrinsicMul:
2802 case SIMDIntrinsicDiv:
2803 case SIMDIntrinsicBitwiseAnd:
2804 case SIMDIntrinsicBitwiseAndNot:
2805 case SIMDIntrinsicBitwiseOr:
2806 case SIMDIntrinsicBitwiseXor:
2809 // check for the cases where we don't support intrinsics.
2810 // This check should be done before we make modifications to type stack.
2811 // Note that this is more of a double safety check for robustness since
2812 // we expect getSIMDIntrinsicInfo() to have filtered out intrinsics on
2813 // unsupported base types. If getSIMdIntrinsicInfo() doesn't filter due
2814 // to some bug, assert in chk/dbg will fire.
2815 if (!varTypeIsFloating(baseType))
2817 if (simdIntrinsicID == SIMDIntrinsicMul)
2819 #if defined(_TARGET_XARCH_)
2820 if ((baseType != TYP_INT) && (baseType != TYP_SHORT))
2822 // TODO-CQ: implement mul on these integer vectors.
2823 // Note that SSE2 has no direct support for these vectors.
2824 assert(!"Mul not supported on long/ulong/uint/small int vectors\n");
2827 #endif // _TARGET_XARCH_
2828 #if defined(_TARGET_ARM64_)
2829 if ((baseType == TYP_ULONG) && (baseType == TYP_LONG))
2831 // TODO-CQ: implement mul on these integer vectors.
2832 // Note that ARM64 has no direct support for these vectors.
2833 assert(!"Mul not supported on long/ulong vectors\n");
2836 #endif // _TARGET_ARM64_
2838 #if defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
2839 // common to all integer type vectors
2840 if (simdIntrinsicID == SIMDIntrinsicDiv)
2842 // SSE2 doesn't support div on non-floating point vectors.
2843 assert(!"Div not supported on integer type vectors\n");
2846 #endif // defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
2850 // op1 is the first operand; if instance method, op1 is "this" arg
2851 // op2 is the second operand
2852 op2 = impSIMDPopStack(simdType);
2853 op1 = impSIMDPopStack(simdType, instMethod);
2855 simdTree = gtNewSIMDNode(simdType, op1, op2, simdIntrinsicID, baseType, size);
2860 case SIMDIntrinsicSelect:
2862 // op3 is a SIMD variable that is the second source
2863 // op2 is a SIMD variable that is the first source
2864 // op1 is a SIMD variable which is the bit mask.
2865 op3 = impSIMDPopStack(simdType);
2866 op2 = impSIMDPopStack(simdType);
2867 op1 = impSIMDPopStack(simdType);
2869 retVal = impSIMDSelect(clsHnd, baseType, size, op1, op2, op3);
2873 case SIMDIntrinsicMin:
2874 case SIMDIntrinsicMax:
2876 // op1 is the first operand; if instance method, op1 is "this" arg
2877 // op2 is the second operand
2878 op2 = impSIMDPopStack(simdType);
2879 op1 = impSIMDPopStack(simdType, instMethod);
2881 retVal = impSIMDMinMax(simdIntrinsicID, clsHnd, baseType, size, op1, op2);
2885 case SIMDIntrinsicGetItem:
2887 // op1 is a SIMD variable that is "this" arg
2888 // op2 is an index of TYP_INT
2889 op2 = impSIMDPopStack(TYP_INT);
2890 op1 = impSIMDPopStack(simdType, instMethod);
2891 int vectorLength = getSIMDVectorLength(size, baseType);
2892 if (!op2->IsCnsIntOrI() || op2->AsIntCon()->gtIconVal >= vectorLength || op2->AsIntCon()->gtIconVal < 0)
2894 // We need to bounds-check the length of the vector.
2895 // For that purpose, we need to clone the index expression.
2896 GenTree* index = op2;
2897 if ((index->gtFlags & GTF_SIDE_EFFECT) != 0)
2899 op2 = fgInsertCommaFormTemp(&index);
2903 op2 = gtCloneExpr(index);
2906 GenTree* lengthNode = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, vectorLength);
2907 GenTreeBoundsChk* simdChk =
2908 new (this, GT_SIMD_CHK) GenTreeBoundsChk(GT_SIMD_CHK, TYP_VOID, index, lengthNode, SCK_RNGCHK_FAIL);
2910 // Create a GT_COMMA tree for the bounds check.
2911 op2 = gtNewOperNode(GT_COMMA, op2->TypeGet(), simdChk, op2);
2914 assert(op1->TypeGet() == simdType);
2915 assert(op2->TypeGet() == TYP_INT);
2917 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, simdIntrinsicID, baseType, size);
2922 case SIMDIntrinsicDotProduct:
2924 #if defined(_TARGET_XARCH_)
2925 // Right now dot product is supported only for float/double vectors and
2926 // int vectors on SSE4/AVX.
2927 if (!varTypeIsFloating(baseType) && !(baseType == TYP_INT && getSIMDSupportLevel() >= SIMD_SSE4_Supported))
2931 #endif // _TARGET_XARCH_
2933 // op1 is a SIMD variable that is the first source and also "this" arg.
2934 // op2 is a SIMD variable which is the second source.
2935 op2 = impSIMDPopStack(simdType);
2936 op1 = impSIMDPopStack(simdType, instMethod);
2938 simdTree = gtNewSIMDNode(baseType, op1, op2, simdIntrinsicID, baseType, size);
2939 if (simdType == TYP_SIMD12)
2941 simdTree->gtFlags |= GTF_SIMD12_OP;
2947 case SIMDIntrinsicSqrt:
2949 #if (defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)) && defined(DEBUG)
2950 // SSE/AVX/ARM64 doesn't support sqrt on integer type vectors and hence
2951 // should never be seen as an intrinsic here. See SIMDIntrinsicList.h
2952 // for supported base types for this intrinsic.
2953 if (!varTypeIsFloating(baseType))
2955 assert(!"Sqrt not supported on integer vectors\n");
2958 #endif // (defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)) && defined(DEBUG)
2960 op1 = impSIMDPopStack(simdType);
2962 retVal = gtNewSIMDNode(genActualType(callType), op1, nullptr, simdIntrinsicID, baseType, size);
2966 case SIMDIntrinsicAbs:
2967 op1 = impSIMDPopStack(simdType);
2968 retVal = impSIMDAbs(clsHnd, baseType, size, op1);
2971 case SIMDIntrinsicGetW:
2972 retVal = impSIMDGetFixed(simdType, baseType, size, 3);
2975 case SIMDIntrinsicGetZ:
2976 retVal = impSIMDGetFixed(simdType, baseType, size, 2);
2979 case SIMDIntrinsicGetY:
2980 retVal = impSIMDGetFixed(simdType, baseType, size, 1);
2983 case SIMDIntrinsicGetX:
2984 retVal = impSIMDGetFixed(simdType, baseType, size, 0);
2987 case SIMDIntrinsicSetW:
2988 case SIMDIntrinsicSetZ:
2989 case SIMDIntrinsicSetY:
2990 case SIMDIntrinsicSetX:
2992 // op2 is the value to be set at indexTemp position
2993 // op1 is SIMD vector that is going to be modified, which is a byref
2995 // If op1 has a side-effect, then don't make it an intrinsic.
2996 // It would be in-efficient to read the entire vector into xmm reg,
2997 // modify it and write back entire xmm reg.
2999 // TODO-CQ: revisit this later.
3000 op1 = impStackTop(1).val;
3001 if ((op1->gtFlags & GTF_SIDE_EFFECT) != 0)
3006 op2 = impSIMDPopStack(baseType);
3007 op1 = impSIMDPopStack(simdType, instMethod);
3009 GenTree* src = gtCloneExpr(op1);
3010 assert(src != nullptr);
3011 simdTree = gtNewSIMDNode(simdType, src, op2, simdIntrinsicID, baseType, size);
3013 copyBlkDst = gtNewOperNode(GT_ADDR, TYP_BYREF, op1);
3018 // Unary operators that take and return a Vector.
3019 case SIMDIntrinsicCast:
3020 case SIMDIntrinsicConvertToSingle:
3021 case SIMDIntrinsicConvertToDouble:
3022 case SIMDIntrinsicConvertToInt32:
3024 op1 = impSIMDPopStack(simdType, instMethod);
3026 simdTree = gtNewSIMDNode(simdType, op1, nullptr, simdIntrinsicID, baseType, size);
3031 case SIMDIntrinsicConvertToInt64:
3033 #ifdef _TARGET_64BIT_
3034 op1 = impSIMDPopStack(simdType, instMethod);
3036 simdTree = gtNewSIMDNode(simdType, op1, nullptr, simdIntrinsicID, baseType, size);
3039 JITDUMP("SIMD Conversion to Int64 is not supported on this platform\n");
3045 case SIMDIntrinsicNarrow:
3047 assert(!instMethod);
3048 op2 = impSIMDPopStack(simdType);
3049 op1 = impSIMDPopStack(simdType);
3050 // op1 and op2 are two input Vector<T>.
3051 simdTree = gtNewSIMDNode(simdType, op1, op2, simdIntrinsicID, baseType, size);
3056 case SIMDIntrinsicWiden:
3058 GenTree* dstAddrHi = impSIMDPopStack(TYP_BYREF);
3059 GenTree* dstAddrLo = impSIMDPopStack(TYP_BYREF);
3060 op1 = impSIMDPopStack(simdType);
3061 GenTree* dupOp1 = fgInsertCommaFormTemp(&op1, gtGetStructHandleForSIMD(simdType, baseType));
3063 // Widen the lower half and assign it to dstAddrLo.
3064 simdTree = gtNewSIMDNode(simdType, op1, nullptr, SIMDIntrinsicWidenLo, baseType, size);
3066 new (this, GT_BLK) GenTreeBlk(GT_BLK, simdType, dstAddrLo, getSIMDTypeSizeInBytes(clsHnd));
3067 GenTree* loAsg = gtNewBlkOpNode(loDest, simdTree, getSIMDTypeSizeInBytes(clsHnd),
3068 false, // not volatile
3070 loAsg->gtFlags |= ((simdTree->gtFlags | dstAddrLo->gtFlags) & GTF_ALL_EFFECT);
3072 // Widen the upper half and assign it to dstAddrHi.
3073 simdTree = gtNewSIMDNode(simdType, dupOp1, nullptr, SIMDIntrinsicWidenHi, baseType, size);
3075 new (this, GT_BLK) GenTreeBlk(GT_BLK, simdType, dstAddrHi, getSIMDTypeSizeInBytes(clsHnd));
3076 GenTree* hiAsg = gtNewBlkOpNode(hiDest, simdTree, getSIMDTypeSizeInBytes(clsHnd),
3077 false, // not volatile
3079 hiAsg->gtFlags |= ((simdTree->gtFlags | dstAddrHi->gtFlags) & GTF_ALL_EFFECT);
3081 retVal = gtNewOperNode(GT_COMMA, simdType, loAsg, hiAsg);
3085 case SIMDIntrinsicHWAccel:
3087 GenTreeIntCon* intConstTree = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, 1);
3088 retVal = intConstTree;
3093 assert(!"Unimplemented SIMD Intrinsic");
3097 #if defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
3098 // XArch/Arm64: also indicate that we use floating point registers.
3099 // The need for setting this here is that a method may not have SIMD
3100 // type lclvars, but might be exercising SIMD intrinsics on fields of
3103 // e.g. public Vector<float> ComplexVecFloat::sqabs() { return this.r * this.r + this.i * this.i; }
3104 compFloatingPointUsed = true;
3105 #endif // defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
3107 // At this point, we have a tree that we are going to store into a destination.
3108 // TODO-1stClassStructs: This should be a simple store or assignment, and should not require
3109 // GTF_ALL_EFFECT for the dest. This is currently emulating the previous behavior of
3113 GenTree* dest = new (this, GT_BLK) GenTreeBlk(GT_BLK, simdType, copyBlkDst, getSIMDTypeSizeInBytes(clsHnd));
3114 dest->gtFlags |= GTF_GLOB_REF;
3115 retVal = gtNewBlkOpNode(dest, simdTree, getSIMDTypeSizeInBytes(clsHnd),
3116 false, // not volatile
3118 retVal->gtFlags |= ((simdTree->gtFlags | copyBlkDst->gtFlags) & GTF_ALL_EFFECT);
3124 #endif // FEATURE_SIMD