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 */)
126 if (m_simdHandleCache == nullptr)
128 if (impInlineInfo == nullptr)
130 m_simdHandleCache = new (this, CMK_Generic) SIMDHandlesCache();
134 // Steal the inliner compiler's cache (create it if not available).
136 if (impInlineInfo->InlineRoot->m_simdHandleCache == nullptr)
138 impInlineInfo->InlineRoot->m_simdHandleCache = new (this, CMK_Generic) SIMDHandlesCache();
141 m_simdHandleCache = impInlineInfo->InlineRoot->m_simdHandleCache;
145 if (typeHnd == nullptr)
150 // fast path search using cached type handles of important types
151 var_types simdBaseType = TYP_UNKNOWN;
154 // TODO - Optimize SIMD type recognition by IntrinsicAttribute
155 if (isSIMDClass(typeHnd))
157 // The most likely to be used type handles are looked up first followed by
158 // less likely to be used type handles
159 if (typeHnd == m_simdHandleCache->SIMDFloatHandle)
161 simdBaseType = TYP_FLOAT;
162 JITDUMP(" Known type SIMD Vector<Float>\n");
164 else if (typeHnd == m_simdHandleCache->SIMDIntHandle)
166 simdBaseType = TYP_INT;
167 JITDUMP(" Known type SIMD Vector<Int>\n");
169 else if (typeHnd == m_simdHandleCache->SIMDVector2Handle)
171 simdBaseType = TYP_FLOAT;
172 size = 2 * genTypeSize(TYP_FLOAT);
173 assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
174 JITDUMP(" Known type Vector2\n");
176 else if (typeHnd == m_simdHandleCache->SIMDVector3Handle)
178 simdBaseType = TYP_FLOAT;
179 size = 3 * genTypeSize(TYP_FLOAT);
180 assert(size == info.compCompHnd->getClassSize(typeHnd));
181 JITDUMP(" Known type Vector3\n");
183 else if (typeHnd == m_simdHandleCache->SIMDVector4Handle)
185 simdBaseType = TYP_FLOAT;
186 size = 4 * genTypeSize(TYP_FLOAT);
187 assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
188 JITDUMP(" Known type Vector4\n");
190 else if (typeHnd == m_simdHandleCache->SIMDVectorHandle)
192 JITDUMP(" Known type Vector\n");
194 else if (typeHnd == m_simdHandleCache->SIMDUShortHandle)
196 simdBaseType = TYP_USHORT;
197 JITDUMP(" Known type SIMD Vector<ushort>\n");
199 else if (typeHnd == m_simdHandleCache->SIMDUByteHandle)
201 simdBaseType = TYP_UBYTE;
202 JITDUMP(" Known type SIMD Vector<ubyte>\n");
204 else if (typeHnd == m_simdHandleCache->SIMDDoubleHandle)
206 simdBaseType = TYP_DOUBLE;
207 JITDUMP(" Known type SIMD Vector<Double>\n");
209 else if (typeHnd == m_simdHandleCache->SIMDLongHandle)
211 simdBaseType = TYP_LONG;
212 JITDUMP(" Known type SIMD Vector<Long>\n");
214 else if (typeHnd == m_simdHandleCache->SIMDShortHandle)
216 simdBaseType = TYP_SHORT;
217 JITDUMP(" Known type SIMD Vector<short>\n");
219 else if (typeHnd == m_simdHandleCache->SIMDByteHandle)
221 simdBaseType = TYP_BYTE;
222 JITDUMP(" Known type SIMD Vector<byte>\n");
224 else if (typeHnd == m_simdHandleCache->SIMDUIntHandle)
226 simdBaseType = TYP_UINT;
227 JITDUMP(" Known type SIMD Vector<uint>\n");
229 else if (typeHnd == m_simdHandleCache->SIMDULongHandle)
231 simdBaseType = TYP_ULONG;
232 JITDUMP(" Known type SIMD Vector<ulong>\n");
236 if (simdBaseType == TYP_UNKNOWN)
238 // Doesn't match with any of the cached type handles.
239 // Obtain base type by parsing fully qualified class name.
241 // TODO-Throughput: implement product shipping solution to query base type.
242 WCHAR className[256] = {0};
243 WCHAR* pbuf = &className[0];
244 int len = _countof(className);
245 info.compCompHnd->appendClassName(&pbuf, &len, typeHnd, TRUE, FALSE, FALSE);
246 noway_assert(pbuf < &className[256]);
247 JITDUMP("SIMD Candidate Type %S\n", className);
249 if (wcsncmp(className, W("System.Numerics."), 16) == 0)
251 if (wcsncmp(&(className[16]), W("Vector`1["), 9) == 0)
253 if (wcsncmp(&(className[25]), W("System.Single"), 13) == 0)
255 m_simdHandleCache->SIMDFloatHandle = typeHnd;
256 simdBaseType = TYP_FLOAT;
257 JITDUMP(" Found type SIMD Vector<Float>\n");
259 else if (wcsncmp(&(className[25]), W("System.Int32"), 12) == 0)
261 m_simdHandleCache->SIMDIntHandle = typeHnd;
262 simdBaseType = TYP_INT;
263 JITDUMP(" Found type SIMD Vector<Int>\n");
265 else if (wcsncmp(&(className[25]), W("System.UInt16"), 13) == 0)
267 m_simdHandleCache->SIMDUShortHandle = typeHnd;
268 simdBaseType = TYP_USHORT;
269 JITDUMP(" Found type SIMD Vector<ushort>\n");
271 else if (wcsncmp(&(className[25]), W("System.Byte"), 11) == 0)
273 m_simdHandleCache->SIMDUByteHandle = typeHnd;
274 simdBaseType = TYP_UBYTE;
275 JITDUMP(" Found type SIMD Vector<ubyte>\n");
277 else if (wcsncmp(&(className[25]), W("System.Double"), 13) == 0)
279 m_simdHandleCache->SIMDDoubleHandle = typeHnd;
280 simdBaseType = TYP_DOUBLE;
281 JITDUMP(" Found type SIMD Vector<Double>\n");
283 else if (wcsncmp(&(className[25]), W("System.Int64"), 12) == 0)
285 m_simdHandleCache->SIMDLongHandle = typeHnd;
286 simdBaseType = TYP_LONG;
287 JITDUMP(" Found type SIMD Vector<Long>\n");
289 else if (wcsncmp(&(className[25]), W("System.Int16"), 12) == 0)
291 m_simdHandleCache->SIMDShortHandle = typeHnd;
292 simdBaseType = TYP_SHORT;
293 JITDUMP(" Found type SIMD Vector<short>\n");
295 else if (wcsncmp(&(className[25]), W("System.SByte"), 12) == 0)
297 m_simdHandleCache->SIMDByteHandle = typeHnd;
298 simdBaseType = TYP_BYTE;
299 JITDUMP(" Found type SIMD Vector<byte>\n");
301 else if (wcsncmp(&(className[25]), W("System.UInt32"), 13) == 0)
303 m_simdHandleCache->SIMDUIntHandle = typeHnd;
304 simdBaseType = TYP_UINT;
305 JITDUMP(" Found type SIMD Vector<uint>\n");
307 else if (wcsncmp(&(className[25]), W("System.UInt64"), 13) == 0)
309 m_simdHandleCache->SIMDULongHandle = typeHnd;
310 simdBaseType = TYP_ULONG;
311 JITDUMP(" Found type SIMD Vector<ulong>\n");
315 JITDUMP(" Unknown SIMD Vector<T>\n");
318 else if (wcsncmp(&(className[16]), W("Vector2"), 8) == 0)
320 m_simdHandleCache->SIMDVector2Handle = typeHnd;
322 simdBaseType = TYP_FLOAT;
323 size = 2 * genTypeSize(TYP_FLOAT);
324 assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
325 JITDUMP(" Found Vector2\n");
327 else if (wcsncmp(&(className[16]), W("Vector3"), 8) == 0)
329 m_simdHandleCache->SIMDVector3Handle = typeHnd;
331 simdBaseType = TYP_FLOAT;
332 size = 3 * genTypeSize(TYP_FLOAT);
333 assert(size == info.compCompHnd->getClassSize(typeHnd));
334 JITDUMP(" Found Vector3\n");
336 else if (wcsncmp(&(className[16]), W("Vector4"), 8) == 0)
338 m_simdHandleCache->SIMDVector4Handle = typeHnd;
340 simdBaseType = TYP_FLOAT;
341 size = 4 * genTypeSize(TYP_FLOAT);
342 assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
343 JITDUMP(" Found Vector4\n");
345 else if (wcsncmp(&(className[16]), W("Vector"), 6) == 0)
347 m_simdHandleCache->SIMDVectorHandle = typeHnd;
348 JITDUMP(" Found type Vector\n");
352 JITDUMP(" Unknown SIMD Type\n");
356 if (simdBaseType != TYP_UNKNOWN && sizeBytes != nullptr)
358 // If not a fixed size vector then its size is same as SIMD vector
359 // register length in bytes
362 size = getSIMDVectorRegisterByteLength();
366 setUsesSIMDTypes(true);
369 #ifdef FEATURE_HW_INTRINSICS
370 else if (isIntrinsicType(typeHnd))
372 const size_t Vector64SizeBytes = 64 / 8;
373 const size_t Vector128SizeBytes = 128 / 8;
374 const size_t Vector256SizeBytes = 256 / 8;
376 #if defined(_TARGET_XARCH_)
377 static_assert_no_msg(YMM_REGSIZE_BYTES == Vector256SizeBytes);
378 static_assert_no_msg(XMM_REGSIZE_BYTES == Vector128SizeBytes);
380 if (typeHnd == m_simdHandleCache->Vector256FloatHandle)
382 simdBaseType = TYP_FLOAT;
383 size = Vector256SizeBytes;
384 JITDUMP(" Known type Vector256<float>\n");
386 else if (typeHnd == m_simdHandleCache->Vector256DoubleHandle)
388 simdBaseType = TYP_DOUBLE;
389 size = Vector256SizeBytes;
390 JITDUMP(" Known type Vector256<double>\n");
392 else if (typeHnd == m_simdHandleCache->Vector256IntHandle)
394 simdBaseType = TYP_INT;
395 size = Vector256SizeBytes;
396 JITDUMP(" Known type Vector256<int>\n");
398 else if (typeHnd == m_simdHandleCache->Vector256UIntHandle)
400 simdBaseType = TYP_UINT;
401 size = Vector256SizeBytes;
402 JITDUMP(" Known type Vector256<uint>\n");
404 else if (typeHnd == m_simdHandleCache->Vector256ShortHandle)
406 simdBaseType = TYP_SHORT;
407 size = Vector256SizeBytes;
408 JITDUMP(" Known type Vector256<short>\n");
410 else if (typeHnd == m_simdHandleCache->Vector256UShortHandle)
412 simdBaseType = TYP_USHORT;
413 size = Vector256SizeBytes;
414 JITDUMP(" Known type Vector256<ushort>\n");
416 else if (typeHnd == m_simdHandleCache->Vector256ByteHandle)
418 simdBaseType = TYP_BYTE;
419 size = Vector256SizeBytes;
420 JITDUMP(" Known type Vector256<sbyte>\n");
422 else if (typeHnd == m_simdHandleCache->Vector256UByteHandle)
424 simdBaseType = TYP_UBYTE;
425 size = Vector256SizeBytes;
426 JITDUMP(" Known type Vector256<byte>\n");
428 else if (typeHnd == m_simdHandleCache->Vector256LongHandle)
430 simdBaseType = TYP_LONG;
431 size = Vector256SizeBytes;
432 JITDUMP(" Known type Vector256<long>\n");
434 else if (typeHnd == m_simdHandleCache->Vector256ULongHandle)
436 simdBaseType = TYP_ULONG;
437 size = Vector256SizeBytes;
438 JITDUMP(" Known type Vector256<ulong>\n");
440 else if (typeHnd == m_simdHandleCache->Vector256FloatHandle)
442 simdBaseType = TYP_FLOAT;
443 size = Vector256SizeBytes;
444 JITDUMP(" Known type Vector256<float>\n");
447 #endif // defined(_TARGET_XARCH)
448 if (typeHnd == m_simdHandleCache->Vector128DoubleHandle)
450 simdBaseType = TYP_DOUBLE;
451 size = Vector128SizeBytes;
452 JITDUMP(" Known type Vector128<double>\n");
454 else if (typeHnd == m_simdHandleCache->Vector128IntHandle)
456 simdBaseType = TYP_INT;
457 size = Vector128SizeBytes;
458 JITDUMP(" Known type Vector128<int>\n");
460 else if (typeHnd == m_simdHandleCache->Vector128UIntHandle)
462 simdBaseType = TYP_UINT;
463 size = Vector128SizeBytes;
464 JITDUMP(" Known type Vector128<uint>\n");
466 else if (typeHnd == m_simdHandleCache->Vector128ShortHandle)
468 simdBaseType = TYP_SHORT;
469 size = Vector128SizeBytes;
470 JITDUMP(" Known type Vector128<short>\n");
472 else if (typeHnd == m_simdHandleCache->Vector128UShortHandle)
474 simdBaseType = TYP_USHORT;
475 size = Vector128SizeBytes;
476 JITDUMP(" Known type Vector128<ushort>\n");
478 else if (typeHnd == m_simdHandleCache->Vector128ByteHandle)
480 simdBaseType = TYP_BYTE;
481 size = Vector128SizeBytes;
482 JITDUMP(" Known type Vector128<sbyte>\n");
484 else if (typeHnd == m_simdHandleCache->Vector128UByteHandle)
486 simdBaseType = TYP_UBYTE;
487 size = Vector128SizeBytes;
488 JITDUMP(" Known type Vector128<byte>\n");
490 else if (typeHnd == m_simdHandleCache->Vector128LongHandle)
492 simdBaseType = TYP_LONG;
493 size = Vector128SizeBytes;
494 JITDUMP(" Known type Vector128<long>\n");
496 else if (typeHnd == m_simdHandleCache->Vector128ULongHandle)
498 simdBaseType = TYP_ULONG;
499 size = Vector128SizeBytes;
500 JITDUMP(" Known type Vector128<ulong>\n");
502 #if defined(_TARGET_ARM64_)
503 else if (typeHnd == m_simdHandleCache->Vector64IntHandle)
505 simdBaseType = TYP_INT;
506 size = Vector64SizeBytes;
507 JITDUMP(" Known type Vector64<int>\n");
509 else if (typeHnd == m_simdHandleCache->Vector64UIntHandle)
511 simdBaseType = TYP_UINT;
512 size = Vector64SizeBytes;
513 JITDUMP(" Known type Vector64<uint>\n");
515 else if (typeHnd == m_simdHandleCache->Vector64ShortHandle)
517 simdBaseType = TYP_SHORT;
518 size = Vector64SizeBytes;
519 JITDUMP(" Known type Vector64<short>\n");
521 else if (typeHnd == m_simdHandleCache->Vector64UShortHandle)
523 simdBaseType = TYP_USHORT;
524 size = Vector64SizeBytes;
525 JITDUMP(" Known type Vector64<ushort>\n");
527 else if (typeHnd == m_simdHandleCache->Vector64ByteHandle)
529 simdBaseType = TYP_BYTE;
530 size = Vector64SizeBytes;
531 JITDUMP(" Known type Vector64<sbyte>\n");
533 else if (typeHnd == m_simdHandleCache->Vector64UByteHandle)
535 simdBaseType = TYP_UBYTE;
536 size = Vector64SizeBytes;
537 JITDUMP(" Known type Vector64<byte>\n");
539 #endif // defined(_TARGET_ARM64_)
542 if (simdBaseType == TYP_UNKNOWN)
544 // Doesn't match with any of the cached type handles.
545 const char* className = getClassNameFromMetadata(typeHnd, nullptr);
546 CORINFO_CLASS_HANDLE baseTypeHnd = getTypeInstantiationArgument(typeHnd, 0);
548 if (baseTypeHnd != nullptr)
550 CorInfoType type = info.compCompHnd->getTypeForPrimitiveNumericClass(baseTypeHnd);
552 JITDUMP("HW Intrinsic SIMD Candidate Type %s with Base Type %s\n", className,
553 getClassNameFromMetadata(baseTypeHnd, nullptr));
555 #if defined(_TARGET_XARCH_)
556 if (strcmp(className, "Vector256`1") == 0)
558 size = Vector256SizeBytes;
561 case CORINFO_TYPE_FLOAT:
562 m_simdHandleCache->Vector256FloatHandle = typeHnd;
563 simdBaseType = TYP_FLOAT;
564 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<float>\n");
566 case CORINFO_TYPE_DOUBLE:
567 m_simdHandleCache->Vector256DoubleHandle = typeHnd;
568 simdBaseType = TYP_DOUBLE;
569 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<double>\n");
571 case CORINFO_TYPE_INT:
572 m_simdHandleCache->Vector256IntHandle = typeHnd;
573 simdBaseType = TYP_INT;
574 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<int>\n");
576 case CORINFO_TYPE_UINT:
577 m_simdHandleCache->Vector256UIntHandle = typeHnd;
578 simdBaseType = TYP_UINT;
579 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<uint>\n");
581 case CORINFO_TYPE_SHORT:
582 m_simdHandleCache->Vector256ShortHandle = typeHnd;
583 simdBaseType = TYP_SHORT;
584 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<short>\n");
586 case CORINFO_TYPE_USHORT:
587 m_simdHandleCache->Vector256UShortHandle = typeHnd;
588 simdBaseType = TYP_USHORT;
589 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<ushort>\n");
591 case CORINFO_TYPE_LONG:
592 m_simdHandleCache->Vector256LongHandle = typeHnd;
593 simdBaseType = TYP_LONG;
594 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<long>\n");
596 case CORINFO_TYPE_ULONG:
597 m_simdHandleCache->Vector256ULongHandle = typeHnd;
598 simdBaseType = TYP_ULONG;
599 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<ulong>\n");
601 case CORINFO_TYPE_UBYTE:
602 m_simdHandleCache->Vector256UByteHandle = typeHnd;
603 simdBaseType = TYP_UBYTE;
604 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<byte>\n");
606 case CORINFO_TYPE_BYTE:
607 m_simdHandleCache->Vector256ByteHandle = typeHnd;
608 simdBaseType = TYP_BYTE;
609 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<sbyte>\n");
613 JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector256<T>\n");
617 #endif // defined(_TARGET_XARCH_)
618 if (strcmp(className, "Vector128`1") == 0)
620 size = Vector128SizeBytes;
623 case CORINFO_TYPE_FLOAT:
624 m_simdHandleCache->Vector128FloatHandle = typeHnd;
625 simdBaseType = TYP_FLOAT;
626 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<float>\n");
628 case CORINFO_TYPE_DOUBLE:
629 m_simdHandleCache->Vector128DoubleHandle = typeHnd;
630 simdBaseType = TYP_DOUBLE;
631 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<double>\n");
633 case CORINFO_TYPE_INT:
634 m_simdHandleCache->Vector128IntHandle = typeHnd;
635 simdBaseType = TYP_INT;
636 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<int>\n");
638 case CORINFO_TYPE_UINT:
639 m_simdHandleCache->Vector128UIntHandle = typeHnd;
640 simdBaseType = TYP_UINT;
641 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<uint>\n");
643 case CORINFO_TYPE_SHORT:
644 m_simdHandleCache->Vector128ShortHandle = typeHnd;
645 simdBaseType = TYP_SHORT;
646 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<short>\n");
648 case CORINFO_TYPE_USHORT:
649 m_simdHandleCache->Vector128UShortHandle = typeHnd;
650 simdBaseType = TYP_USHORT;
651 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<ushort>\n");
653 case CORINFO_TYPE_LONG:
654 m_simdHandleCache->Vector128LongHandle = typeHnd;
655 simdBaseType = TYP_LONG;
656 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<long>\n");
658 case CORINFO_TYPE_ULONG:
659 m_simdHandleCache->Vector128ULongHandle = typeHnd;
660 simdBaseType = TYP_ULONG;
661 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<ulong>\n");
663 case CORINFO_TYPE_UBYTE:
664 m_simdHandleCache->Vector128UByteHandle = typeHnd;
665 simdBaseType = TYP_UBYTE;
666 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<byte>\n");
668 case CORINFO_TYPE_BYTE:
669 m_simdHandleCache->Vector128ByteHandle = typeHnd;
670 simdBaseType = TYP_BYTE;
671 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<sbyte>\n");
675 JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector128<T>\n");
678 #if defined(_TARGET_ARM64_)
679 else if (strcmp(className, "Vector64`1") == 0)
681 size = Vector64SizeBytes;
684 case CORINFO_TYPE_FLOAT:
685 m_simdHandleCache->Vector64FloatHandle = typeHnd;
686 simdBaseType = TYP_FLOAT;
687 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<float>\n");
689 case CORINFO_TYPE_INT:
690 m_simdHandleCache->Vector64IntHandle = typeHnd;
691 simdBaseType = TYP_INT;
692 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<int>\n");
694 case CORINFO_TYPE_UINT:
695 m_simdHandleCache->Vector64UIntHandle = typeHnd;
696 simdBaseType = TYP_UINT;
697 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<uint>\n");
699 case CORINFO_TYPE_SHORT:
700 m_simdHandleCache->Vector64ShortHandle = typeHnd;
701 simdBaseType = TYP_SHORT;
702 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<short>\n");
704 case CORINFO_TYPE_USHORT:
705 m_simdHandleCache->Vector64UShortHandle = typeHnd;
706 simdBaseType = TYP_USHORT;
707 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<ushort>\n");
709 case CORINFO_TYPE_UBYTE:
710 m_simdHandleCache->Vector64UByteHandle = typeHnd;
711 simdBaseType = TYP_UBYTE;
712 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<byte>\n");
714 case CORINFO_TYPE_BYTE:
715 m_simdHandleCache->Vector64ByteHandle = typeHnd;
716 simdBaseType = TYP_BYTE;
717 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<sbyte>\n");
721 JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector64<T>\n");
724 #endif // defined(_TARGET_ARM64_)
728 if (sizeBytes != nullptr)
733 if (simdBaseType != TYP_UNKNOWN)
735 setUsesSIMDTypes(true);
738 #endif // FEATURE_HW_INTRINSICS
743 //--------------------------------------------------------------------------------------
744 // getSIMDIntrinsicInfo: get SIMD intrinsic info given the method handle.
747 // inOutTypeHnd - The handle of the type on which the method is invoked. This is an in-out param.
748 // methodHnd - The handle of the method we're interested in.
749 // sig - method signature info
750 // isNewObj - whether this call represents a newboj constructor call
751 // argCount - argument count - out pram
752 // baseType - base type of the intrinsic - out param
753 // sizeBytes - size of SIMD vector type on which the method is invoked - out param
756 // SIMDIntrinsicInfo struct initialized corresponding to methodHnd.
757 // Sets SIMDIntrinsicInfo.id to SIMDIntrinsicInvalid if methodHnd doesn't correspond
758 // to any SIMD intrinsic. Also, sets the out params inOutTypeHnd, argCount, baseType and
761 // Note that VectorMath class doesn't have a base type and first argument of the method
762 // determines the SIMD vector type on which intrinsic is invoked. In such a case inOutTypeHnd
763 // is modified by this routine.
765 // TODO-Throughput: The current implementation is based on method name string parsing.
766 // Although we now have type identification from the VM, the parsing of intrinsic names
767 // could be made more efficient.
769 const SIMDIntrinsicInfo* Compiler::getSIMDIntrinsicInfo(CORINFO_CLASS_HANDLE* inOutTypeHnd,
770 CORINFO_METHOD_HANDLE methodHnd,
771 CORINFO_SIG_INFO* sig,
778 assert(baseType != nullptr);
779 assert(sizeBytes != nullptr);
781 // get baseType and size of the type
782 CORINFO_CLASS_HANDLE typeHnd = *inOutTypeHnd;
783 *baseType = getBaseTypeAndSizeOfSIMDType(typeHnd, sizeBytes);
785 if (typeHnd == m_simdHandleCache->SIMDVectorHandle)
787 // All of the supported intrinsics on this static class take a first argument that's a vector,
788 // which determines the baseType.
789 // The exception is the IsHardwareAccelerated property, which is handled as a special case.
790 assert(*baseType == TYP_UNKNOWN);
791 if (sig->numArgs == 0)
793 const SIMDIntrinsicInfo* hwAccelIntrinsicInfo = &(simdIntrinsicInfoArray[SIMDIntrinsicHWAccel]);
794 if ((strcmp(eeGetMethodName(methodHnd, nullptr), hwAccelIntrinsicInfo->methodName) == 0) &&
795 JITtype2varType(sig->retType) == hwAccelIntrinsicInfo->retType)
798 assert(hwAccelIntrinsicInfo->argCount == 0 && hwAccelIntrinsicInfo->isInstMethod == false);
799 return hwAccelIntrinsicInfo;
805 typeHnd = info.compCompHnd->getArgClass(sig, sig->args);
806 *inOutTypeHnd = typeHnd;
807 *baseType = getBaseTypeAndSizeOfSIMDType(typeHnd, sizeBytes);
811 if (*baseType == TYP_UNKNOWN)
813 JITDUMP("NOT a SIMD Intrinsic: unsupported baseType\n");
817 // account for implicit "this" arg
818 *argCount = sig->numArgs;
824 // Get the Intrinsic Id by parsing method name.
826 // TODO-Throughput: replace sequential search by binary search by arranging entries
827 // sorted by method name.
828 SIMDIntrinsicID intrinsicId = SIMDIntrinsicInvalid;
829 const char* methodName = eeGetMethodName(methodHnd, nullptr);
830 for (int i = SIMDIntrinsicNone + 1; i < SIMDIntrinsicInvalid; ++i)
832 if (strcmp(methodName, simdIntrinsicInfoArray[i].methodName) == 0)
834 // Found an entry for the method; further check whether it is one of
835 // the supported base types.
837 for (int j = 0; j < SIMD_INTRINSIC_MAX_BASETYPE_COUNT; ++j)
839 // Convention: if there are fewer base types supported than MAX_BASETYPE_COUNT,
840 // the end of the list is marked by TYP_UNDEF.
841 if (simdIntrinsicInfoArray[i].supportedBaseTypes[j] == TYP_UNDEF)
846 if (simdIntrinsicInfoArray[i].supportedBaseTypes[j] == *baseType)
858 // Now, check the arguments.
859 unsigned int fixedArgCnt = simdIntrinsicInfoArray[i].argCount;
860 unsigned int expectedArgCnt = fixedArgCnt;
862 // First handle SIMDIntrinsicInitN, where the arg count depends on the type.
863 // The listed arg types include the vector and the first two init values, which is the expected number
864 // for Vector2. For other cases, we'll check their types here.
865 if (*argCount > expectedArgCnt)
867 if (i == SIMDIntrinsicInitN)
869 if (*argCount == 3 && typeHnd == m_simdHandleCache->SIMDVector2Handle)
873 else if (*argCount == 4 && typeHnd == m_simdHandleCache->SIMDVector3Handle)
877 else if (*argCount == 5 && typeHnd == m_simdHandleCache->SIMDVector4Handle)
882 else if (i == SIMDIntrinsicInitFixed)
884 if (*argCount == 4 && typeHnd == m_simdHandleCache->SIMDVector4Handle)
890 if (*argCount != expectedArgCnt)
895 // Validate the types of individual args passed are what is expected of.
896 // If any of the types don't match with what is expected, don't consider
897 // as an intrinsic. This will make an older JIT with SIMD capabilities
898 // resilient to breaking changes to SIMD managed API.
900 // Note that from IL type stack, args get popped in right to left order
901 // whereas args get listed in method signatures in left to right order.
903 int stackIndex = (expectedArgCnt - 1);
905 // Track the arguments from the signature - we currently only use this to distinguish
906 // integral and pointer types, both of which will by TYP_I_IMPL on the importer stack.
907 CORINFO_ARG_LIST_HANDLE argLst = sig->args;
909 CORINFO_CLASS_HANDLE argClass;
910 for (unsigned int argIndex = 0; found == true && argIndex < expectedArgCnt; argIndex++)
912 bool isThisPtr = ((argIndex == 0) && sig->hasThis());
914 // In case of "newobj SIMDVector<T>(T val)", thisPtr won't be present on type stack.
915 // We don't check anything in that case.
916 if (!isThisPtr || !isNewObj)
918 GenTree* arg = impStackTop(stackIndex).val;
919 var_types argType = arg->TypeGet();
921 var_types expectedArgType;
922 if (argIndex < fixedArgCnt)
925 // - intrinsicInfo.argType[i] == TYP_UNDEF - intrinsic doesn't have a valid arg at position i
926 // - intrinsicInfo.argType[i] == TYP_UNKNOWN - arg type should be same as basetype
927 // Note that we pop the args off in reverse order.
928 expectedArgType = simdIntrinsicInfoArray[i].argType[argIndex];
929 assert(expectedArgType != TYP_UNDEF);
930 if (expectedArgType == TYP_UNKNOWN)
932 // The type of the argument will be genActualType(*baseType).
933 expectedArgType = genActualType(*baseType);
934 argType = genActualType(argType);
939 expectedArgType = *baseType;
942 if (!isThisPtr && argType == TYP_I_IMPL)
944 // The reference implementation has a constructor that takes a pointer.
945 // We don't want to recognize that one. This requires us to look at the CorInfoType
946 // in order to distinguish a signature with a pointer argument from one with an
947 // integer argument of pointer size, both of which will be TYP_I_IMPL on the stack.
948 // TODO-Review: This seems quite fragile. We should consider beefing up the checking
950 CorInfoType corType = strip(info.compCompHnd->getArgType(sig, argLst, &argClass));
951 if (corType == CORINFO_TYPE_PTR)
957 if (varTypeIsSIMD(argType))
959 argType = TYP_STRUCT;
961 if (argType != expectedArgType)
966 if (argIndex != 0 || !sig->hasThis())
968 argLst = info.compCompHnd->getArgNext(argLst);
973 // Cross check return type and static vs. instance is what we are expecting.
974 // If not, don't consider it as an intrinsic.
975 // Note that ret type of TYP_UNKNOWN means that it is not known apriori and must be same as baseType
978 var_types expectedRetType = simdIntrinsicInfoArray[i].retType;
979 if (expectedRetType == TYP_UNKNOWN)
981 // JIT maps uint/ulong type vars to TYP_INT/TYP_LONG.
983 (*baseType == TYP_UINT || *baseType == TYP_ULONG) ? genActualType(*baseType) : *baseType;
986 if (JITtype2varType(sig->retType) != expectedRetType ||
987 sig->hasThis() != simdIntrinsicInfoArray[i].isInstMethod)
995 intrinsicId = (SIMDIntrinsicID)i;
1001 if (intrinsicId != SIMDIntrinsicInvalid)
1003 JITDUMP("Method %s maps to SIMD intrinsic %s\n", methodName, simdIntrinsicNames[intrinsicId]);
1004 return &simdIntrinsicInfoArray[intrinsicId];
1008 JITDUMP("Method %s is NOT a SIMD intrinsic\n", methodName);
1014 // Pops and returns GenTree node from importer's type stack.
1015 // Normalizes TYP_STRUCT value in case of GT_CALL, GT_RET_EXPR and arg nodes.
1018 // type - the type of value that the caller expects to be popped off the stack.
1019 // expectAddr - if true indicates we are expecting type stack entry to be a TYP_BYREF.
1022 // If the popped value is a struct, and the expected type is a simd type, it will be set
1023 // to that type, otherwise it will assert if the type being popped is not the expected type.
1025 GenTree* Compiler::impSIMDPopStack(var_types type, bool expectAddr)
1027 StackEntry se = impPopStack();
1028 typeInfo ti = se.seTypeInfo;
1029 GenTree* tree = se.val;
1031 // If expectAddr is true implies what we have on stack is address and we need
1032 // SIMD type struct that it points to.
1035 assert(tree->TypeGet() == TYP_BYREF);
1036 if (tree->OperGet() == GT_ADDR)
1038 tree = tree->gtGetOp1();
1042 tree = gtNewOperNode(GT_IND, type, tree);
1046 bool isParam = false;
1048 // If we have a ldobj of a SIMD local we need to transform it.
1049 if (tree->OperGet() == GT_OBJ)
1051 GenTree* addr = tree->gtOp.gtOp1;
1052 if ((addr->OperGet() == GT_ADDR) && isSIMDTypeLocal(addr->gtOp.gtOp1))
1054 tree = addr->gtOp.gtOp1;
1058 if (tree->OperGet() == GT_LCL_VAR)
1060 unsigned lclNum = tree->AsLclVarCommon()->GetLclNum();
1061 LclVarDsc* lclVarDsc = &lvaTable[lclNum];
1062 isParam = lclVarDsc->lvIsParam;
1065 // normalize TYP_STRUCT value
1066 if (varTypeIsStruct(tree) && ((tree->OperGet() == GT_RET_EXPR) || (tree->OperGet() == GT_CALL) || isParam))
1068 assert(ti.IsType(TI_STRUCT));
1069 CORINFO_CLASS_HANDLE structType = ti.GetClassHandleForValueClass();
1070 tree = impNormStructVal(tree, structType, (unsigned)CHECK_SPILL_ALL);
1073 // Now set the type of the tree to the specialized SIMD struct type, if applicable.
1074 if (genActualType(tree->gtType) != genActualType(type))
1076 assert(tree->gtType == TYP_STRUCT);
1077 tree->gtType = type;
1079 else if (tree->gtType == TYP_BYREF)
1081 assert(tree->IsLocal() || (tree->OperGet() == GT_RET_EXPR) || (tree->OperGet() == GT_CALL) ||
1082 ((tree->gtOper == GT_ADDR) && varTypeIsSIMD(tree->gtGetOp1())));
1088 // impSIMDGetFixed: Create a GT_SIMD tree for a Get property of SIMD vector with a fixed index.
1091 // baseType - The base (element) type of the SIMD vector.
1092 // simdSize - The total size in bytes of the SIMD vector.
1093 // index - The index of the field to get.
1096 // Returns a GT_SIMD node with the SIMDIntrinsicGetItem intrinsic id.
1098 GenTreeSIMD* Compiler::impSIMDGetFixed(var_types simdType, var_types baseType, unsigned simdSize, int index)
1100 assert(simdSize >= ((index + 1) * genTypeSize(baseType)));
1102 // op1 is a SIMD source.
1103 GenTree* op1 = impSIMDPopStack(simdType, true);
1105 GenTree* op2 = gtNewIconNode(index);
1106 GenTreeSIMD* simdTree = gtNewSIMDNode(baseType, op1, op2, SIMDIntrinsicGetItem, baseType, simdSize);
1110 #ifdef _TARGET_XARCH_
1111 // impSIMDLongRelOpEqual: transforms operands and returns the SIMD intrinsic to be applied on
1112 // transformed operands to obtain == comparison result.
1115 // typeHnd - type handle of SIMD vector
1116 // size - SIMD vector size
1117 // op1 - in-out parameter; first operand
1118 // op2 - in-out parameter; second operand
1121 // Modifies in-out params op1, op2 and returns intrinsic ID to be applied to modified operands
1123 SIMDIntrinsicID Compiler::impSIMDLongRelOpEqual(CORINFO_CLASS_HANDLE typeHnd,
1128 var_types simdType = (*pOp1)->TypeGet();
1129 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1131 // There is no direct SSE2 support for comparing TYP_LONG vectors.
1132 // These have to be implemented in terms of TYP_INT vector comparison operations.
1134 // Equality(v1, v2):
1135 // tmp = (v1 == v2) i.e. compare for equality as if v1 and v2 are vector<int>
1136 // result = BitwiseAnd(t, shuffle(t, (2, 3, 0, 1)))
1137 // Shuffle is meant to swap the comparison results of low-32-bits and high 32-bits of respective long elements.
1139 // Compare vector<long> as if they were vector<int> and assign the result to a temp
1140 GenTree* compResult = gtNewSIMDNode(simdType, *pOp1, *pOp2, SIMDIntrinsicEqual, TYP_INT, size);
1141 unsigned lclNum = lvaGrabTemp(true DEBUGARG("SIMD Long =="));
1142 lvaSetStruct(lclNum, typeHnd, false);
1143 GenTree* tmp = gtNewLclvNode(lclNum, simdType);
1144 GenTree* asg = gtNewTempAssign(lclNum, compResult);
1146 // op1 = GT_COMMA(tmp=compResult, tmp)
1147 // op2 = Shuffle(tmp, 0xB1)
1148 // IntrinsicId = BitwiseAnd
1149 *pOp1 = gtNewOperNode(GT_COMMA, simdType, asg, tmp);
1150 *pOp2 = gtNewSIMDNode(simdType, gtNewLclvNode(lclNum, simdType), gtNewIconNode(SHUFFLE_ZWXY, TYP_INT),
1151 SIMDIntrinsicShuffleSSE2, TYP_INT, size);
1152 return SIMDIntrinsicBitwiseAnd;
1155 // impSIMDLongRelOpGreaterThan: transforms operands and returns the SIMD intrinsic to be applied on
1156 // transformed operands to obtain > comparison result.
1159 // typeHnd - type handle of SIMD vector
1160 // size - SIMD vector size
1161 // pOp1 - in-out parameter; first operand
1162 // pOp2 - in-out parameter; second operand
1165 // Modifies in-out params pOp1, pOp2 and returns intrinsic ID to be applied to modified operands
1167 SIMDIntrinsicID Compiler::impSIMDLongRelOpGreaterThan(CORINFO_CLASS_HANDLE typeHnd,
1172 var_types simdType = (*pOp1)->TypeGet();
1173 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1175 // GreaterThan(v1, v2) where v1 and v2 are vector long.
1176 // Let us consider the case of single long element comparison.
1177 // say L1 = (x1, y1) and L2 = (x2, y2) where x1, y1, x2, and y2 are 32-bit integers that comprise the longs L1 and
1180 // GreaterThan(L1, L2) can be expressed in terms of > relationship between 32-bit integers that comprise L1 and L2
1182 // = (x1, y1) > (x2, y2)
1183 // = (x1 > x2) || [(x1 == x2) && (y1 > y2)] - eq (1)
1185 // t = (v1 > v2) 32-bit signed comparison
1186 // u = (v1 == v2) 32-bit sized element equality
1187 // v = (v1 > v2) 32-bit unsigned comparison
1189 // z = shuffle(t, (3, 3, 1, 1)) - This corresponds to (x1 > x2) in eq(1) above
1190 // t1 = Shuffle(v, (2, 2, 0, 0)) - This corresponds to (y1 > y2) in eq(1) above
1191 // u1 = Shuffle(u, (3, 3, 1, 1)) - This corresponds to (x1 == x2) in eq(1) above
1192 // w = And(t1, u1) - This corresponds to [(x1 == x2) && (y1 > y2)] in eq(1) above
1193 // Result = BitwiseOr(z, w)
1195 // Since op1 and op2 gets used multiple times, make sure side effects are computed.
1196 GenTree* dupOp1 = nullptr;
1197 GenTree* dupOp2 = nullptr;
1198 GenTree* dupDupOp1 = nullptr;
1199 GenTree* dupDupOp2 = nullptr;
1201 if (((*pOp1)->gtFlags & GTF_SIDE_EFFECT) != 0)
1203 dupOp1 = fgInsertCommaFormTemp(pOp1, typeHnd);
1204 dupDupOp1 = gtNewLclvNode(dupOp1->AsLclVarCommon()->GetLclNum(), simdType);
1208 dupOp1 = gtCloneExpr(*pOp1);
1209 dupDupOp1 = gtCloneExpr(*pOp1);
1212 if (((*pOp2)->gtFlags & GTF_SIDE_EFFECT) != 0)
1214 dupOp2 = fgInsertCommaFormTemp(pOp2, typeHnd);
1215 dupDupOp2 = gtNewLclvNode(dupOp2->AsLclVarCommon()->GetLclNum(), simdType);
1219 dupOp2 = gtCloneExpr(*pOp2);
1220 dupDupOp2 = gtCloneExpr(*pOp2);
1223 assert(dupDupOp1 != nullptr && dupDupOp2 != nullptr);
1224 assert(dupOp1 != nullptr && dupOp2 != nullptr);
1225 assert(*pOp1 != nullptr && *pOp2 != nullptr);
1227 // v1GreaterThanv2Signed - signed 32-bit comparison
1228 GenTree* v1GreaterThanv2Signed = gtNewSIMDNode(simdType, *pOp1, *pOp2, SIMDIntrinsicGreaterThan, TYP_INT, size);
1230 // v1Equalsv2 - 32-bit equality
1231 GenTree* v1Equalsv2 = gtNewSIMDNode(simdType, dupOp1, dupOp2, SIMDIntrinsicEqual, TYP_INT, size);
1233 // v1GreaterThanv2Unsigned - unsigned 32-bit comparison
1234 var_types tempBaseType = TYP_UINT;
1235 SIMDIntrinsicID sid = impSIMDRelOp(SIMDIntrinsicGreaterThan, typeHnd, size, &tempBaseType, &dupDupOp1, &dupDupOp2);
1236 GenTree* v1GreaterThanv2Unsigned = gtNewSIMDNode(simdType, dupDupOp1, dupDupOp2, sid, tempBaseType, size);
1238 GenTree* z = gtNewSIMDNode(simdType, v1GreaterThanv2Signed, gtNewIconNode(SHUFFLE_WWYY, TYP_INT),
1239 SIMDIntrinsicShuffleSSE2, TYP_FLOAT, size);
1240 GenTree* t1 = gtNewSIMDNode(simdType, v1GreaterThanv2Unsigned, gtNewIconNode(SHUFFLE_ZZXX, TYP_INT),
1241 SIMDIntrinsicShuffleSSE2, TYP_FLOAT, size);
1242 GenTree* u1 = gtNewSIMDNode(simdType, v1Equalsv2, gtNewIconNode(SHUFFLE_WWYY, TYP_INT), SIMDIntrinsicShuffleSSE2,
1244 GenTree* w = gtNewSIMDNode(simdType, u1, t1, SIMDIntrinsicBitwiseAnd, TYP_INT, size);
1248 return SIMDIntrinsicBitwiseOr;
1251 // impSIMDLongRelOpGreaterThanOrEqual: transforms operands and returns the SIMD intrinsic to be applied on
1252 // transformed operands to obtain >= comparison result.
1255 // typeHnd - type handle of SIMD vector
1256 // size - SIMD vector size
1257 // pOp1 - in-out parameter; first operand
1258 // pOp2 - in-out parameter; second operand
1261 // Modifies in-out params pOp1, pOp2 and returns intrinsic ID to be applied to modified operands
1263 SIMDIntrinsicID Compiler::impSIMDLongRelOpGreaterThanOrEqual(CORINFO_CLASS_HANDLE typeHnd,
1268 var_types simdType = (*pOp1)->TypeGet();
1269 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1271 // expand this to (a == b) | (a > b)
1272 GenTree* dupOp1 = nullptr;
1273 GenTree* dupOp2 = nullptr;
1275 if (((*pOp1)->gtFlags & GTF_SIDE_EFFECT) != 0)
1277 dupOp1 = fgInsertCommaFormTemp(pOp1, typeHnd);
1281 dupOp1 = gtCloneExpr(*pOp1);
1284 if (((*pOp2)->gtFlags & GTF_SIDE_EFFECT) != 0)
1286 dupOp2 = fgInsertCommaFormTemp(pOp2, typeHnd);
1290 dupOp2 = gtCloneExpr(*pOp2);
1293 assert(dupOp1 != nullptr && dupOp2 != nullptr);
1294 assert(*pOp1 != nullptr && *pOp2 != nullptr);
1297 SIMDIntrinsicID id = impSIMDLongRelOpEqual(typeHnd, size, pOp1, pOp2);
1298 *pOp1 = gtNewSIMDNode(simdType, *pOp1, *pOp2, id, TYP_LONG, size);
1301 id = impSIMDLongRelOpGreaterThan(typeHnd, size, &dupOp1, &dupOp2);
1302 *pOp2 = gtNewSIMDNode(simdType, dupOp1, dupOp2, id, TYP_LONG, size);
1304 return SIMDIntrinsicBitwiseOr;
1307 // impSIMDInt32OrSmallIntRelOpGreaterThanOrEqual: transforms operands and returns the SIMD intrinsic to be applied on
1308 // transformed operands to obtain >= comparison result in case of integer base type vectors
1311 // typeHnd - type handle of SIMD vector
1312 // size - SIMD vector size
1313 // baseType - base type of SIMD vector
1314 // pOp1 - in-out parameter; first operand
1315 // pOp2 - in-out parameter; second operand
1318 // Modifies in-out params pOp1, pOp2 and returns intrinsic ID to be applied to modified operands
1320 SIMDIntrinsicID Compiler::impSIMDIntegralRelOpGreaterThanOrEqual(
1321 CORINFO_CLASS_HANDLE typeHnd, unsigned size, var_types baseType, GenTree** pOp1, GenTree** pOp2)
1323 var_types simdType = (*pOp1)->TypeGet();
1324 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1326 // This routine should be used only for integer base type vectors
1327 assert(varTypeIsIntegral(baseType));
1328 if ((getSIMDSupportLevel() == SIMD_SSE2_Supported) && ((baseType == TYP_LONG) || baseType == TYP_UBYTE))
1330 return impSIMDLongRelOpGreaterThanOrEqual(typeHnd, size, pOp1, pOp2);
1333 // expand this to (a == b) | (a > b)
1334 GenTree* dupOp1 = nullptr;
1335 GenTree* dupOp2 = nullptr;
1337 if (((*pOp1)->gtFlags & GTF_SIDE_EFFECT) != 0)
1339 dupOp1 = fgInsertCommaFormTemp(pOp1, typeHnd);
1343 dupOp1 = gtCloneExpr(*pOp1);
1346 if (((*pOp2)->gtFlags & GTF_SIDE_EFFECT) != 0)
1348 dupOp2 = fgInsertCommaFormTemp(pOp2, typeHnd);
1352 dupOp2 = gtCloneExpr(*pOp2);
1355 assert(dupOp1 != nullptr && dupOp2 != nullptr);
1356 assert(*pOp1 != nullptr && *pOp2 != nullptr);
1359 *pOp1 = gtNewSIMDNode(simdType, *pOp1, *pOp2, SIMDIntrinsicEqual, baseType, size);
1362 *pOp2 = gtNewSIMDNode(simdType, dupOp1, dupOp2, SIMDIntrinsicGreaterThan, baseType, size);
1364 return SIMDIntrinsicBitwiseOr;
1366 #endif // _TARGET_XARCH_
1368 // Transforms operands and returns the SIMD intrinsic to be applied on
1369 // transformed operands to obtain given relop result.
1372 // relOpIntrinsicId - Relational operator SIMD intrinsic
1373 // typeHnd - type handle of SIMD vector
1374 // size - SIMD vector size
1375 // inOutBaseType - base type of SIMD vector
1376 // pOp1 - in-out parameter; first operand
1377 // pOp2 - in-out parameter; second operand
1380 // Modifies in-out params pOp1, pOp2, inOutBaseType and returns intrinsic ID to be applied to modified operands
1382 SIMDIntrinsicID Compiler::impSIMDRelOp(SIMDIntrinsicID relOpIntrinsicId,
1383 CORINFO_CLASS_HANDLE typeHnd,
1385 var_types* inOutBaseType,
1389 var_types simdType = (*pOp1)->TypeGet();
1390 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1392 assert(isRelOpSIMDIntrinsic(relOpIntrinsicId));
1394 SIMDIntrinsicID intrinsicID = relOpIntrinsicId;
1395 #ifdef _TARGET_XARCH_
1396 var_types baseType = *inOutBaseType;
1398 if (varTypeIsFloating(baseType))
1400 // SSE2/AVX doesn't support > and >= on vector float/double.
1401 // Therefore, we need to use < and <= with swapped operands
1402 if (relOpIntrinsicId == SIMDIntrinsicGreaterThan || relOpIntrinsicId == SIMDIntrinsicGreaterThanOrEqual)
1404 GenTree* tmp = *pOp1;
1409 (relOpIntrinsicId == SIMDIntrinsicGreaterThan) ? SIMDIntrinsicLessThan : SIMDIntrinsicLessThanOrEqual;
1412 else if (varTypeIsIntegral(baseType))
1414 // SSE/AVX doesn't support < and <= on integer base type vectors.
1415 // Therefore, we need to use > and >= with swapped operands.
1416 if (intrinsicID == SIMDIntrinsicLessThan || intrinsicID == SIMDIntrinsicLessThanOrEqual)
1418 GenTree* tmp = *pOp1;
1422 intrinsicID = (relOpIntrinsicId == SIMDIntrinsicLessThan) ? SIMDIntrinsicGreaterThan
1423 : SIMDIntrinsicGreaterThanOrEqual;
1426 if ((getSIMDSupportLevel() == SIMD_SSE2_Supported) && baseType == TYP_LONG)
1428 // There is no direct SSE2 support for comparing TYP_LONG vectors.
1429 // These have to be implemented interms of TYP_INT vector comparison operations.
1430 if (intrinsicID == SIMDIntrinsicEqual)
1432 intrinsicID = impSIMDLongRelOpEqual(typeHnd, size, pOp1, pOp2);
1434 else if (intrinsicID == SIMDIntrinsicGreaterThan)
1436 intrinsicID = impSIMDLongRelOpGreaterThan(typeHnd, size, pOp1, pOp2);
1438 else if (intrinsicID == SIMDIntrinsicGreaterThanOrEqual)
1440 intrinsicID = impSIMDLongRelOpGreaterThanOrEqual(typeHnd, size, pOp1, pOp2);
1447 // SSE2 and AVX direct support for signed comparison of int32, int16 and int8 types
1448 else if (!varTypeIsUnsigned(baseType))
1450 if (intrinsicID == SIMDIntrinsicGreaterThanOrEqual)
1452 intrinsicID = impSIMDIntegralRelOpGreaterThanOrEqual(typeHnd, size, baseType, pOp1, pOp2);
1457 // Vector<byte>, Vector<ushort>, Vector<uint> and Vector<ulong>:
1458 // SSE2 supports > for signed comparison. Therefore, to use it for
1459 // comparing unsigned numbers, we subtract a constant from both the
1460 // operands such that the result fits within the corresponding signed
1461 // type. The resulting signed numbers are compared using SSE2 signed
1464 // Vector<byte>: constant to be subtracted is 2^7
1465 // Vector<ushort> constant to be subtracted is 2^15
1466 // Vector<uint> constant to be subtracted is 2^31
1467 // Vector<ulong> constant to be subtracted is 2^63
1469 // We need to treat op1 and op2 as signed for comparison purpose after
1470 // the transformation.
1471 __int64 constVal = 0;
1475 constVal = 0x80808080;
1476 *inOutBaseType = TYP_BYTE;
1479 constVal = 0x80008000;
1480 *inOutBaseType = TYP_SHORT;
1483 constVal = 0x80000000;
1484 *inOutBaseType = TYP_INT;
1487 constVal = 0x8000000000000000LL;
1488 *inOutBaseType = TYP_LONG;
1494 assert(constVal != 0);
1496 // This transformation is not required for equality.
1497 if (intrinsicID != SIMDIntrinsicEqual)
1499 // For constructing const vector use either long or int base type.
1500 var_types tempBaseType;
1502 if (baseType == TYP_ULONG)
1504 tempBaseType = TYP_LONG;
1505 initVal = gtNewLconNode(constVal);
1509 tempBaseType = TYP_INT;
1510 initVal = gtNewIconNode((ssize_t)constVal);
1512 initVal->gtType = tempBaseType;
1513 GenTree* constVector = gtNewSIMDNode(simdType, initVal, nullptr, SIMDIntrinsicInit, tempBaseType, size);
1515 // Assign constVector to a temp, since we intend to use it more than once
1516 // TODO-CQ: We have quite a few such constant vectors constructed during
1517 // the importation of SIMD intrinsics. Make sure that we have a single
1518 // temp per distinct constant per method.
1519 GenTree* tmp = fgInsertCommaFormTemp(&constVector, typeHnd);
1521 // op1 = op1 - constVector
1522 // op2 = op2 - constVector
1523 *pOp1 = gtNewSIMDNode(simdType, *pOp1, constVector, SIMDIntrinsicSub, baseType, size);
1524 *pOp2 = gtNewSIMDNode(simdType, *pOp2, tmp, SIMDIntrinsicSub, baseType, size);
1527 return impSIMDRelOp(intrinsicID, typeHnd, size, inOutBaseType, pOp1, pOp2);
1530 #elif defined(_TARGET_ARM64_)
1531 // TODO-ARM64-CQ handle comparisons against zero
1533 // _TARGET_ARM64_ doesn't support < and <= on register register comparisons
1534 // Therefore, we need to use > and >= with swapped operands.
1535 if (intrinsicID == SIMDIntrinsicLessThan || intrinsicID == SIMDIntrinsicLessThanOrEqual)
1537 GenTree* tmp = *pOp1;
1542 (intrinsicID == SIMDIntrinsicLessThan) ? SIMDIntrinsicGreaterThan : SIMDIntrinsicGreaterThanOrEqual;
1544 #else // !_TARGET_XARCH_
1545 assert(!"impSIMDRelOp() unimplemented on target arch");
1547 #endif // !_TARGET_XARCH_
1552 //-------------------------------------------------------------------------
1553 // impSIMDAbs: creates GT_SIMD node to compute Abs value of a given vector.
1556 // typeHnd - type handle of SIMD vector
1557 // baseType - base type of vector
1558 // size - vector size in bytes
1559 // op1 - operand of Abs intrinsic
1561 GenTree* Compiler::impSIMDAbs(CORINFO_CLASS_HANDLE typeHnd, var_types baseType, unsigned size, GenTree* op1)
1563 assert(varTypeIsSIMD(op1));
1565 var_types simdType = op1->TypeGet();
1566 GenTree* retVal = nullptr;
1568 #ifdef _TARGET_XARCH_
1569 // When there is no direct support, Abs(v) could be computed
1570 // on integer vectors as follows:
1571 // BitVector = v < vector.Zero
1572 // result = ConditionalSelect(BitVector, vector.Zero - v, v)
1574 bool useConditionalSelect = false;
1575 if (getSIMDSupportLevel() == SIMD_SSE2_Supported)
1577 // SSE2 doesn't support abs on signed integer type vectors.
1578 if (baseType == TYP_LONG || baseType == TYP_INT || baseType == TYP_SHORT || baseType == TYP_BYTE)
1580 useConditionalSelect = true;
1585 assert(getSIMDSupportLevel() >= SIMD_SSE4_Supported);
1586 if (baseType == TYP_LONG)
1588 // SSE4/AVX2 don't support abs on long type vector.
1589 useConditionalSelect = true;
1593 if (useConditionalSelect)
1595 // This works only on integer vectors not on float/double vectors.
1596 assert(varTypeIsIntegral(baseType));
1601 if (op1->OperGet() == GT_LCL_VAR)
1603 op1LclNum = op1->gtLclVarCommon.gtLclNum;
1604 op1Assign = nullptr;
1608 op1LclNum = lvaGrabTemp(true DEBUGARG("SIMD Abs op1"));
1609 lvaSetStruct(op1LclNum, typeHnd, false);
1610 op1Assign = gtNewTempAssign(op1LclNum, op1);
1611 op1 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1614 // Assign Vector.Zero to a temp since it is needed more than once
1615 GenTree* vecZero = gtNewSIMDVectorZero(simdType, baseType, size);
1616 unsigned vecZeroLclNum = lvaGrabTemp(true DEBUGARG("SIMD Abs VecZero"));
1617 lvaSetStruct(vecZeroLclNum, typeHnd, false);
1618 GenTree* vecZeroAssign = gtNewTempAssign(vecZeroLclNum, vecZero);
1620 // Construct BitVector = v < vector.Zero
1621 GenTree* bitVecOp1 = op1;
1622 GenTree* bitVecOp2 = gtNewLclvNode(vecZeroLclNum, vecZero->TypeGet());
1623 var_types relOpBaseType = baseType;
1624 SIMDIntrinsicID relOpIntrinsic =
1625 impSIMDRelOp(SIMDIntrinsicLessThan, typeHnd, size, &relOpBaseType, &bitVecOp1, &bitVecOp2);
1626 GenTree* bitVec = gtNewSIMDNode(simdType, bitVecOp1, bitVecOp2, relOpIntrinsic, relOpBaseType, size);
1627 unsigned bitVecLclNum = lvaGrabTemp(true DEBUGARG("SIMD Abs bitVec"));
1628 lvaSetStruct(bitVecLclNum, typeHnd, false);
1629 GenTree* bitVecAssign = gtNewTempAssign(bitVecLclNum, bitVec);
1630 bitVec = gtNewLclvNode(bitVecLclNum, bitVec->TypeGet());
1632 // Construct condSelectOp1 = vector.Zero - v
1633 GenTree* subOp1 = gtNewLclvNode(vecZeroLclNum, vecZero->TypeGet());
1634 GenTree* subOp2 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1635 GenTree* negVec = gtNewSIMDNode(simdType, subOp1, subOp2, SIMDIntrinsicSub, baseType, size);
1637 // Construct ConditionalSelect(bitVec, vector.Zero - v, v)
1638 GenTree* vec = gtNewLclvNode(op1LclNum, op1->TypeGet());
1639 retVal = impSIMDSelect(typeHnd, baseType, size, bitVec, negVec, vec);
1641 // Prepend bitVec assignment to retVal.
1642 // retVal = (tmp2 = v < tmp1), CondSelect(tmp2, tmp1 - v, v)
1643 retVal = gtNewOperNode(GT_COMMA, simdType, bitVecAssign, retVal);
1645 // Prepend vecZero assignment to retVal.
1646 // retVal = (tmp1 = vector.Zero), (tmp2 = v < tmp1), CondSelect(tmp2, tmp1 - v, v)
1647 retVal = gtNewOperNode(GT_COMMA, simdType, vecZeroAssign, retVal);
1649 // If op1 was assigned to a temp, prepend that to retVal.
1650 if (op1Assign != nullptr)
1652 // retVal = (v=op1), (tmp1 = vector.Zero), (tmp2 = v < tmp1), CondSelect(tmp2, tmp1 - v, v)
1653 retVal = gtNewOperNode(GT_COMMA, simdType, op1Assign, retVal);
1656 else if (varTypeIsFloating(baseType))
1658 // Abs(vf) = vf & new SIMDVector<float>(0x7fffffff);
1659 // Abs(vd) = vf & new SIMDVector<double>(0x7fffffffffffffff);
1660 GenTree* bitMask = nullptr;
1661 if (baseType == TYP_FLOAT)
1664 static_assert_no_msg(sizeof(float) == sizeof(int));
1665 *((int*)&f) = 0x7fffffff;
1666 bitMask = gtNewDconNode(f);
1668 else if (baseType == TYP_DOUBLE)
1671 static_assert_no_msg(sizeof(double) == sizeof(__int64));
1672 *((__int64*)&d) = 0x7fffffffffffffffLL;
1673 bitMask = gtNewDconNode(d);
1676 assert(bitMask != nullptr);
1677 bitMask->gtType = baseType;
1678 GenTree* bitMaskVector = gtNewSIMDNode(simdType, bitMask, SIMDIntrinsicInit, baseType, size);
1679 retVal = gtNewSIMDNode(simdType, op1, bitMaskVector, SIMDIntrinsicBitwiseAnd, baseType, size);
1681 else if (baseType == TYP_USHORT || baseType == TYP_UBYTE || baseType == TYP_UINT || baseType == TYP_ULONG)
1683 // Abs is a no-op on unsigned integer type vectors
1688 assert(getSIMDSupportLevel() >= SIMD_SSE4_Supported);
1689 assert(baseType != TYP_LONG);
1691 retVal = gtNewSIMDNode(simdType, op1, SIMDIntrinsicAbs, baseType, size);
1693 #elif defined(_TARGET_ARM64_)
1694 if (varTypeIsUnsigned(baseType))
1696 // Abs is a no-op on unsigned integer type vectors
1701 retVal = gtNewSIMDNode(simdType, op1, SIMDIntrinsicAbs, baseType, size);
1703 #else // !defined(_TARGET_XARCH)_ && !defined(_TARGET_ARM64_)
1704 assert(!"Abs intrinsic on non-xarch target not implemented");
1705 #endif // !_TARGET_XARCH_
1710 // Creates a GT_SIMD tree for Select operation
1713 // typeHnd - type handle of SIMD vector
1714 // baseType - base type of SIMD vector
1715 // size - SIMD vector size
1716 // op1 - first operand = Condition vector vc
1717 // op2 - second operand = va
1718 // op3 - third operand = vb
1721 // Returns GT_SIMD tree that computes Select(vc, va, vb)
1723 GenTree* Compiler::impSIMDSelect(
1724 CORINFO_CLASS_HANDLE typeHnd, var_types baseType, unsigned size, GenTree* op1, GenTree* op2, GenTree* op3)
1726 assert(varTypeIsSIMD(op1));
1727 var_types simdType = op1->TypeGet();
1728 assert(op2->TypeGet() == simdType);
1729 assert(op3->TypeGet() == simdType);
1731 // TODO-ARM64-CQ Support generating select instruction for SIMD
1733 // Select(BitVector vc, va, vb) = (va & vc) | (vb & !vc)
1734 // Select(op1, op2, op3) = (op2 & op1) | (op3 & !op1)
1735 // = SIMDIntrinsicBitwiseOr(SIMDIntrinsicBitwiseAnd(op2, op1),
1736 // SIMDIntrinsicBitwiseAndNot(op3, op1))
1738 // If Op1 has side effect, create an assignment to a temp
1740 GenTree* asg = nullptr;
1741 if ((op1->gtFlags & GTF_SIDE_EFFECT) != 0)
1743 unsigned lclNum = lvaGrabTemp(true DEBUGARG("SIMD Select"));
1744 lvaSetStruct(lclNum, typeHnd, false);
1745 tmp = gtNewLclvNode(lclNum, op1->TypeGet());
1746 asg = gtNewTempAssign(lclNum, op1);
1749 GenTree* andExpr = gtNewSIMDNode(simdType, op2, tmp, SIMDIntrinsicBitwiseAnd, baseType, size);
1750 GenTree* dupOp1 = gtCloneExpr(tmp);
1751 assert(dupOp1 != nullptr);
1752 #ifdef _TARGET_ARM64_
1753 // ARM64 implements SIMDIntrinsicBitwiseAndNot as Left & ~Right
1754 GenTree* andNotExpr = gtNewSIMDNode(simdType, op3, dupOp1, SIMDIntrinsicBitwiseAndNot, baseType, size);
1756 // XARCH implements SIMDIntrinsicBitwiseAndNot as ~Left & Right
1757 GenTree* andNotExpr = gtNewSIMDNode(simdType, dupOp1, op3, SIMDIntrinsicBitwiseAndNot, baseType, size);
1759 GenTree* simdTree = gtNewSIMDNode(simdType, andExpr, andNotExpr, SIMDIntrinsicBitwiseOr, baseType, size);
1761 // If asg not null, create a GT_COMMA tree.
1764 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), asg, simdTree);
1770 // Creates a GT_SIMD tree for Min/Max operation
1773 // IntrinsicId - SIMD intrinsic Id, either Min or Max
1774 // typeHnd - type handle of SIMD vector
1775 // baseType - base type of SIMD vector
1776 // size - SIMD vector size
1777 // op1 - first operand = va
1778 // op2 - second operand = vb
1781 // Returns GT_SIMD tree that computes Max(va, vb)
1783 GenTree* Compiler::impSIMDMinMax(SIMDIntrinsicID intrinsicId,
1784 CORINFO_CLASS_HANDLE typeHnd,
1790 assert(intrinsicId == SIMDIntrinsicMin || intrinsicId == SIMDIntrinsicMax);
1791 assert(varTypeIsSIMD(op1));
1792 var_types simdType = op1->TypeGet();
1793 assert(op2->TypeGet() == simdType);
1795 #if defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
1796 GenTree* simdTree = nullptr;
1798 #ifdef _TARGET_XARCH_
1799 // SSE2 has direct support for float/double/signed word/unsigned byte.
1800 // SSE4.1 has direct support for int32/uint32/signed byte/unsigned word.
1801 // For other integer types we compute min/max as follows
1803 // int32/uint32 (SSE2)
1804 // int64/uint64 (SSE2&SSE4):
1805 // compResult = (op1 < op2) in case of Min
1806 // (op1 > op2) in case of Max
1807 // Min/Max(op1, op2) = Select(compResult, op1, op2)
1809 // unsigned word (SSE2):
1810 // op1 = op1 - 2^15 ; to make it fit within a signed word
1811 // op2 = op2 - 2^15 ; to make it fit within a signed word
1812 // result = SSE2 signed word Min/Max(op1, op2)
1813 // result = result + 2^15 ; readjust it back
1815 // signed byte (SSE2):
1816 // op1 = op1 + 2^7 ; to make it unsigned
1817 // op1 = op1 + 2^7 ; to make it unsigned
1818 // result = SSE2 unsigned byte Min/Max(op1, op2)
1819 // result = result - 2^15 ; readjust it back
1821 if (varTypeIsFloating(baseType) || baseType == TYP_SHORT || baseType == TYP_UBYTE ||
1822 (getSIMDSupportLevel() >= SIMD_SSE4_Supported &&
1823 (baseType == TYP_BYTE || baseType == TYP_INT || baseType == TYP_UINT || baseType == TYP_USHORT)))
1825 // SSE2 or SSE4.1 has direct support
1826 simdTree = gtNewSIMDNode(simdType, op1, op2, intrinsicId, baseType, size);
1828 else if (baseType == TYP_USHORT || baseType == TYP_BYTE)
1830 assert(getSIMDSupportLevel() == SIMD_SSE2_Supported);
1832 SIMDIntrinsicID operIntrinsic;
1833 SIMDIntrinsicID adjustIntrinsic;
1834 var_types minMaxOperBaseType;
1835 if (baseType == TYP_USHORT)
1837 constVal = 0x80008000;
1838 operIntrinsic = SIMDIntrinsicSub;
1839 adjustIntrinsic = SIMDIntrinsicAdd;
1840 minMaxOperBaseType = TYP_SHORT;
1844 assert(baseType == TYP_BYTE);
1845 constVal = 0x80808080;
1846 operIntrinsic = SIMDIntrinsicAdd;
1847 adjustIntrinsic = SIMDIntrinsicSub;
1848 minMaxOperBaseType = TYP_UBYTE;
1851 GenTree* initVal = gtNewIconNode(constVal);
1852 GenTree* constVector = gtNewSIMDNode(simdType, initVal, nullptr, SIMDIntrinsicInit, TYP_INT, size);
1854 // Assign constVector to a temp, since we intend to use it more than once
1855 // TODO-CQ: We have quite a few such constant vectors constructed during
1856 // the importation of SIMD intrinsics. Make sure that we have a single
1857 // temp per distinct constant per method.
1858 GenTree* tmp = fgInsertCommaFormTemp(&constVector, typeHnd);
1860 // op1 = op1 - constVector
1861 // op2 = op2 - constVector
1862 op1 = gtNewSIMDNode(simdType, op1, constVector, operIntrinsic, baseType, size);
1863 op2 = gtNewSIMDNode(simdType, op2, tmp, operIntrinsic, baseType, size);
1865 // compute min/max of op1 and op2 considering them as if minMaxOperBaseType
1866 simdTree = gtNewSIMDNode(simdType, op1, op2, intrinsicId, minMaxOperBaseType, size);
1868 // re-adjust the value by adding or subtracting constVector
1869 tmp = gtNewLclvNode(tmp->AsLclVarCommon()->GetLclNum(), tmp->TypeGet());
1870 simdTree = gtNewSIMDNode(simdType, simdTree, tmp, adjustIntrinsic, baseType, size);
1872 #elif defined(_TARGET_ARM64_)
1873 // Arm64 has direct support for all types except int64/uint64
1874 // For which we compute min/max as follows
1877 // compResult = (op1 < op2) in case of Min
1878 // (op1 > op2) in case of Max
1879 // Min/Max(op1, op2) = Select(compResult, op1, op2)
1880 if (baseType != TYP_ULONG && baseType != TYP_LONG)
1882 simdTree = gtNewSIMDNode(simdType, op1, op2, intrinsicId, baseType, size);
1887 GenTree* dupOp1 = nullptr;
1888 GenTree* dupOp2 = nullptr;
1889 GenTree* op1Assign = nullptr;
1890 GenTree* op2Assign = nullptr;
1894 if ((op1->gtFlags & GTF_SIDE_EFFECT) != 0)
1896 op1LclNum = lvaGrabTemp(true DEBUGARG("SIMD Min/Max"));
1897 dupOp1 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1898 lvaSetStruct(op1LclNum, typeHnd, false);
1899 op1Assign = gtNewTempAssign(op1LclNum, op1);
1900 op1 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1904 dupOp1 = gtCloneExpr(op1);
1907 if ((op2->gtFlags & GTF_SIDE_EFFECT) != 0)
1909 op2LclNum = lvaGrabTemp(true DEBUGARG("SIMD Min/Max"));
1910 dupOp2 = gtNewLclvNode(op2LclNum, op2->TypeGet());
1911 lvaSetStruct(op2LclNum, typeHnd, false);
1912 op2Assign = gtNewTempAssign(op2LclNum, op2);
1913 op2 = gtNewLclvNode(op2LclNum, op2->TypeGet());
1917 dupOp2 = gtCloneExpr(op2);
1920 SIMDIntrinsicID relOpIntrinsic =
1921 (intrinsicId == SIMDIntrinsicMin) ? SIMDIntrinsicLessThan : SIMDIntrinsicGreaterThan;
1922 var_types relOpBaseType = baseType;
1924 // compResult = op1 relOp op2
1925 // simdTree = Select(compResult, op1, op2);
1926 assert(dupOp1 != nullptr);
1927 assert(dupOp2 != nullptr);
1928 relOpIntrinsic = impSIMDRelOp(relOpIntrinsic, typeHnd, size, &relOpBaseType, &dupOp1, &dupOp2);
1929 GenTree* compResult = gtNewSIMDNode(simdType, dupOp1, dupOp2, relOpIntrinsic, relOpBaseType, size);
1930 unsigned compResultLclNum = lvaGrabTemp(true DEBUGARG("SIMD Min/Max"));
1931 lvaSetStruct(compResultLclNum, typeHnd, false);
1932 GenTree* compResultAssign = gtNewTempAssign(compResultLclNum, compResult);
1933 compResult = gtNewLclvNode(compResultLclNum, compResult->TypeGet());
1934 simdTree = impSIMDSelect(typeHnd, baseType, size, compResult, op1, op2);
1935 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), compResultAssign, simdTree);
1937 // Now create comma trees if we have created assignments of op1/op2 to temps
1938 if (op2Assign != nullptr)
1940 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), op2Assign, simdTree);
1943 if (op1Assign != nullptr)
1945 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), op1Assign, simdTree);
1949 assert(simdTree != nullptr);
1951 #else // !(defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_))
1952 assert(!"impSIMDMinMax() unimplemented on target arch");
1954 #endif // !(defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_))
1957 //------------------------------------------------------------------------
1958 // getOp1ForConstructor: Get the op1 for a constructor call.
1961 // opcode - the opcode being handled (needed to identify the CEE_NEWOBJ case)
1962 // newobjThis - For CEE_NEWOBJ, this is the temp grabbed for the allocated uninitalized object.
1963 // clsHnd - The handle of the class of the method.
1966 // The tree node representing the object to be initialized with the constructor.
1969 // This method handles the differences between the CEE_NEWOBJ and constructor cases.
1971 GenTree* Compiler::getOp1ForConstructor(OPCODE opcode, GenTree* newobjThis, CORINFO_CLASS_HANDLE clsHnd)
1974 if (opcode == CEE_NEWOBJ)
1977 assert(newobjThis->gtOper == GT_ADDR && newobjThis->gtOp.gtOp1->gtOper == GT_LCL_VAR);
1979 // push newobj result on type stack
1980 unsigned tmp = op1->gtOp.gtOp1->gtLclVarCommon.gtLclNum;
1981 impPushOnStack(gtNewLclvNode(tmp, lvaGetRealType(tmp)), verMakeTypeInfo(clsHnd).NormaliseForStack());
1985 op1 = impSIMDPopStack(TYP_BYREF);
1987 assert(op1->TypeGet() == TYP_BYREF);
1991 //-------------------------------------------------------------------
1992 // Set the flag that indicates that the lclVar referenced by this tree
1993 // is used in a SIMD intrinsic.
1997 void Compiler::setLclRelatedToSIMDIntrinsic(GenTree* tree)
1999 assert(tree->OperIsLocal());
2000 unsigned lclNum = tree->AsLclVarCommon()->GetLclNum();
2001 LclVarDsc* lclVarDsc = &lvaTable[lclNum];
2002 lclVarDsc->lvUsedInSIMDIntrinsic = true;
2005 //-------------------------------------------------------------
2006 // Check if two field nodes reference at the same memory location.
2007 // Notice that this check is just based on pattern matching.
2012 // If op1's parents node and op2's parents node are at the same location, return true. Otherwise, return false
2014 bool areFieldsParentsLocatedSame(GenTree* op1, GenTree* op2)
2016 assert(op1->OperGet() == GT_FIELD);
2017 assert(op2->OperGet() == GT_FIELD);
2019 GenTree* op1ObjRef = op1->gtField.gtFldObj;
2020 GenTree* op2ObjRef = op2->gtField.gtFldObj;
2021 while (op1ObjRef != nullptr && op2ObjRef != nullptr)
2024 if (op1ObjRef->OperGet() != op2ObjRef->OperGet())
2028 else if (op1ObjRef->OperGet() == GT_ADDR)
2030 op1ObjRef = op1ObjRef->gtOp.gtOp1;
2031 op2ObjRef = op2ObjRef->gtOp.gtOp1;
2034 if (op1ObjRef->OperIsLocal() && op2ObjRef->OperIsLocal() &&
2035 op1ObjRef->AsLclVarCommon()->GetLclNum() == op2ObjRef->AsLclVarCommon()->GetLclNum())
2039 else if (op1ObjRef->OperGet() == GT_FIELD && op2ObjRef->OperGet() == GT_FIELD &&
2040 op1ObjRef->gtField.gtFldHnd == op2ObjRef->gtField.gtFldHnd)
2042 op1ObjRef = op1ObjRef->gtField.gtFldObj;
2043 op2ObjRef = op2ObjRef->gtField.gtFldObj;
2055 //----------------------------------------------------------------------
2056 // Check whether two field are contiguous
2058 // first - GenTree*. The Type of the node should be TYP_FLOAT
2059 // second - GenTree*. The Type of the node should be TYP_FLOAT
2061 // if the first field is located before second field, and they are located contiguously,
2062 // then return true. Otherwise, return false.
2064 bool Compiler::areFieldsContiguous(GenTree* first, GenTree* second)
2066 assert(first->OperGet() == GT_FIELD);
2067 assert(second->OperGet() == GT_FIELD);
2068 assert(first->gtType == TYP_FLOAT);
2069 assert(second->gtType == TYP_FLOAT);
2071 var_types firstFieldType = first->gtType;
2072 var_types secondFieldType = second->gtType;
2074 unsigned firstFieldEndOffset = first->gtField.gtFldOffset + genTypeSize(firstFieldType);
2075 unsigned secondFieldOffset = second->gtField.gtFldOffset;
2076 if (firstFieldEndOffset == secondFieldOffset && firstFieldType == secondFieldType &&
2077 areFieldsParentsLocatedSame(first, second))
2085 //-------------------------------------------------------------------------------
2086 // Check whether two array element nodes are located contiguously or not.
2091 // if the array element op1 is located before array element op2, and they are contiguous,
2092 // then return true. Otherwise, return false.
2094 // Right this can only check array element with const number as index. In future,
2095 // we should consider to allow this function to check the index using expression.
2097 bool Compiler::areArrayElementsContiguous(GenTree* op1, GenTree* op2)
2099 noway_assert(op1->gtOper == GT_INDEX);
2100 noway_assert(op2->gtOper == GT_INDEX);
2101 GenTreeIndex* op1Index = op1->AsIndex();
2102 GenTreeIndex* op2Index = op2->AsIndex();
2104 GenTree* op1ArrayRef = op1Index->Arr();
2105 GenTree* op2ArrayRef = op2Index->Arr();
2106 assert(op1ArrayRef->TypeGet() == TYP_REF);
2107 assert(op2ArrayRef->TypeGet() == TYP_REF);
2109 GenTree* op1IndexNode = op1Index->Index();
2110 GenTree* op2IndexNode = op2Index->Index();
2111 if ((op1IndexNode->OperGet() == GT_CNS_INT && op2IndexNode->OperGet() == GT_CNS_INT) &&
2112 op1IndexNode->gtIntCon.gtIconVal + 1 == op2IndexNode->gtIntCon.gtIconVal)
2114 if (op1ArrayRef->OperGet() == GT_FIELD && op2ArrayRef->OperGet() == GT_FIELD &&
2115 areFieldsParentsLocatedSame(op1ArrayRef, op2ArrayRef))
2119 else if (op1ArrayRef->OperIsLocal() && op2ArrayRef->OperIsLocal() &&
2120 op1ArrayRef->AsLclVarCommon()->GetLclNum() == op2ArrayRef->AsLclVarCommon()->GetLclNum())
2128 //-------------------------------------------------------------------------------
2129 // Check whether two argument nodes are contiguous or not.
2134 // if the argument node op1 is located before argument node op2, and they are located contiguously,
2135 // then return true. Otherwise, return false.
2137 // Right now this can only check field and array. In future we should add more cases.
2140 bool Compiler::areArgumentsContiguous(GenTree* op1, GenTree* op2)
2142 if (op1->OperGet() == GT_INDEX && op2->OperGet() == GT_INDEX)
2144 return areArrayElementsContiguous(op1, op2);
2146 else if (op1->OperGet() == GT_FIELD && op2->OperGet() == GT_FIELD)
2148 return areFieldsContiguous(op1, op2);
2153 //--------------------------------------------------------------------------------------------------------
2154 // createAddressNodeForSIMDInit: Generate the address node(GT_LEA) if we want to intialize vector2, vector3 or vector4
2155 // from first argument's address.
2158 // tree - GenTree*. This the tree node which is used to get the address for indir.
2159 // simdsize - unsigned. This the simd vector size.
2160 // arrayElementsCount - unsigned. This is used for generating the boundary check for array.
2163 // return the address node.
2166 // 1. Currently just support for GT_FIELD and GT_INDEX, because we can only verify the GT_INDEX node or GT_Field
2167 // are located contiguously or not. In future we should support more cases.
2168 // 2. Though it happens to just work fine front-end phases are not aware of GT_LEA node. Therefore, convert these
2170 GenTree* Compiler::createAddressNodeForSIMDInit(GenTree* tree, unsigned simdSize)
2172 assert(tree->OperGet() == GT_FIELD || tree->OperGet() == GT_INDEX);
2173 GenTree* byrefNode = nullptr;
2174 GenTree* startIndex = nullptr;
2175 unsigned offset = 0;
2176 var_types baseType = tree->gtType;
2178 if (tree->OperGet() == GT_FIELD)
2180 GenTree* objRef = tree->gtField.gtFldObj;
2181 if (objRef != nullptr && objRef->gtOper == GT_ADDR)
2183 GenTree* obj = objRef->gtOp.gtOp1;
2185 // If the field is directly from a struct, then in this case,
2186 // we should set this struct's lvUsedInSIMDIntrinsic as true,
2187 // so that this sturct won't be promoted.
2188 // e.g. s.x x is a field, and s is a struct, then we should set the s's lvUsedInSIMDIntrinsic as true.
2189 // so that s won't be promoted.
2190 // 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
2191 // s1 can be promoted, so that s2 can be promoted. The reason for that is if we don't allow s1 to be
2192 // promoted, then this will affect the other optimizations which are depend on s1's struct promotion.
2194 // In future, we should optimize this case so that if there is a nested field like s1.s2.x and s1.s2.x's
2195 // address is used for initializing the vector, then s1 can be promoted but s2 can't.
2196 if (varTypeIsSIMD(obj) && obj->OperIsLocal())
2198 setLclRelatedToSIMDIntrinsic(obj);
2202 byrefNode = gtCloneExpr(tree->gtField.gtFldObj);
2203 assert(byrefNode != nullptr);
2204 offset = tree->gtField.gtFldOffset;
2206 else if (tree->OperGet() == GT_INDEX)
2209 GenTree* index = tree->AsIndex()->Index();
2210 assert(index->OperGet() == GT_CNS_INT);
2212 GenTree* checkIndexExpr = nullptr;
2213 unsigned indexVal = (unsigned)(index->gtIntCon.gtIconVal);
2214 offset = indexVal * genTypeSize(tree->TypeGet());
2215 GenTree* arrayRef = tree->AsIndex()->Arr();
2217 // Generate the boundary check exception.
2218 // The length for boundary check should be the maximum index number which should be
2219 // (first argument's index number) + (how many array arguments we have) - 1
2220 // = indexVal + arrayElementsCount - 1
2221 unsigned arrayElementsCount = simdSize / genTypeSize(baseType);
2222 checkIndexExpr = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, indexVal + arrayElementsCount - 1);
2223 GenTreeArrLen* arrLen = gtNewArrLen(TYP_INT, arrayRef, (int)OFFSETOF__CORINFO_Array__length);
2224 GenTreeBoundsChk* arrBndsChk = new (this, GT_ARR_BOUNDS_CHECK)
2225 GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, checkIndexExpr, arrLen, SCK_RNGCHK_FAIL);
2227 offset += OFFSETOF__CORINFO_Array__data;
2228 byrefNode = gtNewOperNode(GT_COMMA, arrayRef->TypeGet(), arrBndsChk, gtCloneExpr(arrayRef));
2235 new (this, GT_LEA) GenTreeAddrMode(TYP_BYREF, byrefNode, startIndex, genTypeSize(tree->TypeGet()), offset);
2239 //-------------------------------------------------------------------------------
2240 // impMarkContiguousSIMDFieldAssignments: Try to identify if there are contiguous
2241 // assignments from SIMD field to memory. If there are, then mark the related
2242 // lclvar so that it won't be promoted.
2245 // stmt - GenTree*. Input statement node.
2247 void Compiler::impMarkContiguousSIMDFieldAssignments(GenTree* stmt)
2249 if (!featureSIMD || opts.MinOpts())
2253 GenTree* expr = stmt->gtStmt.gtStmtExpr;
2254 if (expr->OperGet() == GT_ASG && expr->TypeGet() == TYP_FLOAT)
2256 GenTree* curDst = expr->gtOp.gtOp1;
2257 GenTree* curSrc = expr->gtOp.gtOp2;
2259 var_types baseType = TYP_UNKNOWN;
2260 unsigned simdSize = 0;
2261 GenTree* srcSimdStructNode = getSIMDStructFromField(curSrc, &baseType, &index, &simdSize, true);
2262 if (srcSimdStructNode == nullptr || baseType != TYP_FLOAT)
2264 fgPreviousCandidateSIMDFieldAsgStmt = nullptr;
2266 else if (index == 0 && isSIMDTypeLocal(srcSimdStructNode))
2268 fgPreviousCandidateSIMDFieldAsgStmt = stmt;
2270 else if (fgPreviousCandidateSIMDFieldAsgStmt != nullptr)
2273 GenTree* prevAsgExpr = fgPreviousCandidateSIMDFieldAsgStmt->gtStmt.gtStmtExpr;
2274 GenTree* prevDst = prevAsgExpr->gtOp.gtOp1;
2275 GenTree* prevSrc = prevAsgExpr->gtOp.gtOp2;
2276 if (!areArgumentsContiguous(prevDst, curDst) || !areArgumentsContiguous(prevSrc, curSrc))
2278 fgPreviousCandidateSIMDFieldAsgStmt = nullptr;
2282 if (index == (simdSize / genTypeSize(baseType) - 1))
2284 // Successfully found the pattern, mark the lclvar as UsedInSIMDIntrinsic
2285 if (srcSimdStructNode->OperIsLocal())
2287 setLclRelatedToSIMDIntrinsic(srcSimdStructNode);
2290 if (curDst->OperGet() == GT_FIELD)
2292 GenTree* objRef = curDst->gtField.gtFldObj;
2293 if (objRef != nullptr && objRef->gtOper == GT_ADDR)
2295 GenTree* obj = objRef->gtOp.gtOp1;
2296 if (varTypeIsStruct(obj) && obj->OperIsLocal())
2298 setLclRelatedToSIMDIntrinsic(obj);
2305 fgPreviousCandidateSIMDFieldAsgStmt = stmt;
2312 fgPreviousCandidateSIMDFieldAsgStmt = nullptr;
2316 //------------------------------------------------------------------------
2317 // impSIMDIntrinsic: Check method to see if it is a SIMD method
2320 // opcode - the opcode being handled (needed to identify the CEE_NEWOBJ case)
2321 // newobjThis - For CEE_NEWOBJ, this is the temp grabbed for the allocated uninitalized object.
2322 // clsHnd - The handle of the class of the method.
2323 // method - The handle of the method.
2324 // sig - The call signature for the method.
2325 // memberRef - The memberRef token for the method reference.
2328 // If clsHnd is a known SIMD type, and 'method' is one of the methods that are
2329 // implemented as an intrinsic in the JIT, then return the tree that implements
2332 GenTree* Compiler::impSIMDIntrinsic(OPCODE opcode,
2333 GenTree* newobjThis,
2334 CORINFO_CLASS_HANDLE clsHnd,
2335 CORINFO_METHOD_HANDLE methodHnd,
2336 CORINFO_SIG_INFO* sig,
2339 assert(featureSIMD);
2341 if (!isSIMDClass(clsHnd))
2346 // Get base type and intrinsic Id
2347 var_types baseType = TYP_UNKNOWN;
2349 unsigned argCount = 0;
2350 const SIMDIntrinsicInfo* intrinsicInfo =
2351 getSIMDIntrinsicInfo(&clsHnd, methodHnd, sig, (opcode == CEE_NEWOBJ), &argCount, &baseType, &size);
2352 if (intrinsicInfo == nullptr || intrinsicInfo->id == SIMDIntrinsicInvalid)
2357 SIMDIntrinsicID simdIntrinsicID = intrinsicInfo->id;
2359 if (baseType != TYP_UNKNOWN)
2361 simdType = getSIMDTypeForSize(size);
2365 assert(simdIntrinsicID == SIMDIntrinsicHWAccel);
2366 simdType = TYP_UNKNOWN;
2368 bool instMethod = intrinsicInfo->isInstMethod;
2369 var_types callType = JITtype2varType(sig->retType);
2370 if (callType == TYP_STRUCT)
2372 // Note that here we are assuming that, if the call returns a struct, that it is the same size as the
2373 // struct on which the method is declared. This is currently true for all methods on Vector types,
2374 // but if this ever changes, we will need to determine the callType from the signature.
2375 assert(info.compCompHnd->getClassSize(sig->retTypeClass) == genTypeSize(simdType));
2376 callType = simdType;
2379 GenTree* simdTree = nullptr;
2380 GenTree* op1 = nullptr;
2381 GenTree* op2 = nullptr;
2382 GenTree* op3 = nullptr;
2383 GenTree* retVal = nullptr;
2384 GenTree* copyBlkDst = nullptr;
2385 bool doCopyBlk = false;
2387 switch (simdIntrinsicID)
2389 case SIMDIntrinsicGetCount:
2391 int length = getSIMDVectorLength(clsHnd);
2392 GenTreeIntCon* intConstTree = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, length);
2393 retVal = intConstTree;
2395 intConstTree->gtFlags |= GTF_ICON_SIMD_COUNT;
2399 case SIMDIntrinsicGetZero:
2400 retVal = gtNewSIMDVectorZero(simdType, baseType, size);
2403 case SIMDIntrinsicGetOne:
2404 retVal = gtNewSIMDVectorOne(simdType, baseType, size);
2407 case SIMDIntrinsicGetAllOnes:
2409 // Equivalent to (Vector<T>) new Vector<int>(0xffffffff);
2410 GenTree* initVal = gtNewIconNode(0xffffffff, TYP_INT);
2411 simdTree = gtNewSIMDNode(simdType, initVal, nullptr, SIMDIntrinsicInit, TYP_INT, size);
2412 if (baseType != TYP_INT)
2414 // cast it to required baseType if different from TYP_INT
2415 simdTree = gtNewSIMDNode(simdType, simdTree, nullptr, SIMDIntrinsicCast, baseType, size);
2421 case SIMDIntrinsicInit:
2422 case SIMDIntrinsicInitN:
2424 // SIMDIntrinsicInit:
2425 // op2 - the initializer value
2426 // op1 - byref of vector
2428 // SIMDIntrinsicInitN
2429 // op2 - list of initializer values stitched into a list
2430 // op1 - byref of vector
2431 bool initFromFirstArgIndir = false;
2432 if (simdIntrinsicID == SIMDIntrinsicInit)
2434 op2 = impSIMDPopStack(baseType);
2438 assert(simdIntrinsicID == SIMDIntrinsicInitN);
2439 assert(baseType == TYP_FLOAT);
2441 unsigned initCount = argCount - 1;
2442 unsigned elementCount = getSIMDVectorLength(size, baseType);
2443 noway_assert(initCount == elementCount);
2444 GenTree* nextArg = op2;
2446 // Build a GT_LIST with the N values.
2447 // We must maintain left-to-right order of the args, but we will pop
2448 // them off in reverse order (the Nth arg was pushed onto the stack last).
2450 GenTree* list = nullptr;
2451 GenTree* firstArg = nullptr;
2452 GenTree* prevArg = nullptr;
2454 bool areArgsContiguous = true;
2455 for (unsigned i = 0; i < initCount; i++)
2457 GenTree* nextArg = impSIMDPopStack(baseType);
2458 if (areArgsContiguous)
2460 GenTree* curArg = nextArg;
2463 if (prevArg != nullptr)
2465 // Recall that we are popping the args off the stack in reverse order.
2466 areArgsContiguous = areArgumentsContiguous(curArg, prevArg);
2471 list = new (this, GT_LIST) GenTreeOp(GT_LIST, baseType, nextArg, list);
2474 if (areArgsContiguous && baseType == TYP_FLOAT)
2476 // Since Vector2, Vector3 and Vector4's arguments type are only float,
2477 // we intialize the vector from first argument address, only when
2478 // the baseType is TYP_FLOAT and the arguments are located contiguously in memory
2479 initFromFirstArgIndir = true;
2480 GenTree* op2Address = createAddressNodeForSIMDInit(firstArg, size);
2481 var_types simdType = getSIMDTypeForSize(size);
2482 op2 = gtNewOperNode(GT_IND, simdType, op2Address);
2490 op1 = getOp1ForConstructor(opcode, newobjThis, clsHnd);
2492 assert(op1->TypeGet() == TYP_BYREF);
2493 assert(genActualType(op2->TypeGet()) == genActualType(baseType) || initFromFirstArgIndir);
2495 // For integral base types of size less than TYP_INT, expand the initializer
2496 // to fill size of TYP_INT bytes.
2497 if (varTypeIsSmallInt(baseType))
2499 // This case should occur only for Init intrinsic.
2500 assert(simdIntrinsicID == SIMDIntrinsicInit);
2502 unsigned baseSize = genTypeSize(baseType);
2506 multiplier = 0x01010101;
2510 assert(baseSize == 2);
2511 multiplier = 0x00010001;
2514 GenTree* t1 = nullptr;
2515 if (baseType == TYP_BYTE)
2517 // What we have is a signed byte initializer,
2518 // which when loaded to a reg will get sign extended to TYP_INT.
2519 // But what we need is the initializer without sign extended or
2520 // rather zero extended to 32-bits.
2521 t1 = gtNewOperNode(GT_AND, TYP_INT, op2, gtNewIconNode(0xff, TYP_INT));
2523 else if (baseType == TYP_SHORT)
2525 // What we have is a signed short initializer,
2526 // which when loaded to a reg will get sign extended to TYP_INT.
2527 // But what we need is the initializer without sign extended or
2528 // rather zero extended to 32-bits.
2529 t1 = gtNewOperNode(GT_AND, TYP_INT, op2, gtNewIconNode(0xffff, TYP_INT));
2533 assert(baseType == TYP_UBYTE || baseType == TYP_USHORT);
2534 t1 = gtNewCastNode(TYP_INT, op2, false, TYP_INT);
2537 assert(t1 != nullptr);
2538 GenTree* t2 = gtNewIconNode(multiplier, TYP_INT);
2539 op2 = gtNewOperNode(GT_MUL, TYP_INT, t1, t2);
2541 // Construct a vector of TYP_INT with the new initializer and cast it back to vector of baseType
2542 simdTree = gtNewSIMDNode(simdType, op2, nullptr, simdIntrinsicID, TYP_INT, size);
2543 simdTree = gtNewSIMDNode(simdType, simdTree, nullptr, SIMDIntrinsicCast, baseType, size);
2548 if (initFromFirstArgIndir)
2551 if (op1->gtOp.gtOp1->OperIsLocal())
2553 // label the dst struct's lclvar is used for SIMD intrinsic,
2554 // so that this dst struct won't be promoted.
2555 setLclRelatedToSIMDIntrinsic(op1->gtOp.gtOp1);
2560 simdTree = gtNewSIMDNode(simdType, op2, nullptr, simdIntrinsicID, baseType, size);
2569 case SIMDIntrinsicInitArray:
2570 case SIMDIntrinsicInitArrayX:
2571 case SIMDIntrinsicCopyToArray:
2572 case SIMDIntrinsicCopyToArrayX:
2574 // op3 - index into array in case of SIMDIntrinsicCopyToArrayX and SIMDIntrinsicInitArrayX
2575 // op2 - array itself
2576 // op1 - byref to vector struct
2578 unsigned int vectorLength = getSIMDVectorLength(size, baseType);
2579 // (This constructor takes only the zero-based arrays.)
2580 // We will add one or two bounds checks:
2581 // 1. If we have an index, we must do a check on that first.
2582 // We can't combine it with the index + vectorLength check because
2583 // a. It might be negative, and b. It may need to raise a different exception
2584 // (captured as SCK_ARG_RNG_EXCPN for CopyTo and SCK_RNGCHK_FAIL for Init).
2585 // 2. We need to generate a check (SCK_ARG_EXCPN for CopyTo and SCK_RNGCHK_FAIL for Init)
2586 // for the last array element we will access.
2587 // We'll either check against (vectorLength - 1) or (index + vectorLength - 1).
2589 GenTree* checkIndexExpr = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, vectorLength - 1);
2591 // Get the index into the array. If it has been provided, it will be on the
2592 // top of the stack. Otherwise, it is null.
2595 op3 = impSIMDPopStack(TYP_INT);
2596 if (op3->IsIntegralConst(0))
2603 // TODO-CQ: Here, or elsewhere, check for the pattern where op2 is a newly constructed array, and
2604 // change this to the InitN form.
2605 // op3 = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, 0);
2609 // Clone the array for use in the bounds check.
2610 op2 = impSIMDPopStack(TYP_REF);
2611 assert(op2->TypeGet() == TYP_REF);
2612 GenTree* arrayRefForArgChk = op2;
2613 GenTree* argRngChk = nullptr;
2614 GenTree* asg = nullptr;
2615 if ((arrayRefForArgChk->gtFlags & GTF_SIDE_EFFECT) != 0)
2617 op2 = fgInsertCommaFormTemp(&arrayRefForArgChk);
2621 op2 = gtCloneExpr(arrayRefForArgChk);
2623 assert(op2 != nullptr);
2627 SpecialCodeKind op3CheckKind;
2628 if (simdIntrinsicID == SIMDIntrinsicInitArrayX)
2630 op3CheckKind = SCK_RNGCHK_FAIL;
2634 assert(simdIntrinsicID == SIMDIntrinsicCopyToArrayX);
2635 op3CheckKind = SCK_ARG_RNG_EXCPN;
2637 // We need to use the original expression on this, which is the first check.
2638 GenTree* arrayRefForArgRngChk = arrayRefForArgChk;
2639 // Then we clone the clone we just made for the next check.
2640 arrayRefForArgChk = gtCloneExpr(op2);
2641 // We know we MUST have had a cloneable expression.
2642 assert(arrayRefForArgChk != nullptr);
2643 GenTree* index = op3;
2644 if ((index->gtFlags & GTF_SIDE_EFFECT) != 0)
2646 op3 = fgInsertCommaFormTemp(&index);
2650 op3 = gtCloneExpr(index);
2653 GenTreeArrLen* arrLen =
2654 gtNewArrLen(TYP_INT, arrayRefForArgRngChk, (int)OFFSETOF__CORINFO_Array__length);
2655 argRngChk = new (this, GT_ARR_BOUNDS_CHECK)
2656 GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, index, arrLen, op3CheckKind);
2657 // Now, clone op3 to create another node for the argChk
2658 GenTree* index2 = gtCloneExpr(op3);
2659 assert(index != nullptr);
2660 checkIndexExpr = gtNewOperNode(GT_ADD, TYP_INT, index2, checkIndexExpr);
2663 // Insert a bounds check for index + offset - 1.
2664 // This must be a "normal" array.
2665 SpecialCodeKind op2CheckKind;
2666 if (simdIntrinsicID == SIMDIntrinsicInitArray || simdIntrinsicID == SIMDIntrinsicInitArrayX)
2668 op2CheckKind = SCK_RNGCHK_FAIL;
2672 op2CheckKind = SCK_ARG_EXCPN;
2674 GenTreeArrLen* arrLen = gtNewArrLen(TYP_INT, arrayRefForArgChk, (int)OFFSETOF__CORINFO_Array__length);
2675 GenTreeBoundsChk* argChk = new (this, GT_ARR_BOUNDS_CHECK)
2676 GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, checkIndexExpr, arrLen, op2CheckKind);
2678 // Create a GT_COMMA tree for the bounds check(s).
2679 op2 = gtNewOperNode(GT_COMMA, op2->TypeGet(), argChk, op2);
2680 if (argRngChk != nullptr)
2682 op2 = gtNewOperNode(GT_COMMA, op2->TypeGet(), argRngChk, op2);
2685 if (simdIntrinsicID == SIMDIntrinsicInitArray || simdIntrinsicID == SIMDIntrinsicInitArrayX)
2687 op1 = getOp1ForConstructor(opcode, newobjThis, clsHnd);
2688 simdTree = gtNewSIMDNode(simdType, op2, op3, SIMDIntrinsicInitArray, baseType, size);
2694 assert(simdIntrinsicID == SIMDIntrinsicCopyToArray || simdIntrinsicID == SIMDIntrinsicCopyToArrayX);
2695 op1 = impSIMDPopStack(simdType, instMethod);
2696 assert(op1->TypeGet() == simdType);
2698 // copy vector (op1) to array (op2) starting at index (op3)
2701 // TODO-Cleanup: Though it happens to just work fine front-end phases are not aware of GT_LEA node.
2702 // Therefore, convert these to use GT_ADDR .
2703 copyBlkDst = new (this, GT_LEA)
2704 GenTreeAddrMode(TYP_BYREF, op2, op3, genTypeSize(baseType), OFFSETOF__CORINFO_Array__data);
2710 case SIMDIntrinsicInitFixed:
2712 // We are initializing a fixed-length vector VLarge with a smaller fixed-length vector VSmall, plus 1 or 2
2713 // additional floats.
2714 // op4 (optional) - float value for VLarge.W, if VLarge is Vector4, and VSmall is Vector2
2715 // op3 - float value for VLarge.Z or VLarge.W
2717 // op1 - byref of VLarge
2718 assert(baseType == TYP_FLOAT);
2719 unsigned elementByteCount = 4;
2721 GenTree* op4 = nullptr;
2724 op4 = impSIMDPopStack(TYP_FLOAT);
2725 assert(op4->TypeGet() == TYP_FLOAT);
2727 op3 = impSIMDPopStack(TYP_FLOAT);
2728 assert(op3->TypeGet() == TYP_FLOAT);
2729 // The input vector will either be TYP_SIMD8 or TYP_SIMD12.
2730 var_types smallSIMDType = TYP_SIMD8;
2731 if ((op4 == nullptr) && (simdType == TYP_SIMD16))
2733 smallSIMDType = TYP_SIMD12;
2735 op2 = impSIMDPopStack(smallSIMDType);
2736 op1 = getOp1ForConstructor(opcode, newobjThis, clsHnd);
2738 // We are going to redefine the operands so that:
2739 // - op3 is the value that's going into the Z position, or null if it's a Vector4 constructor with a single
2741 // - op4 is the W position value, or null if this is a Vector3 constructor.
2742 if (size == 16 && argCount == 3)
2751 simdTree = gtNewSIMDNode(simdType, simdTree, op3, SIMDIntrinsicSetZ, baseType, size);
2755 simdTree = gtNewSIMDNode(simdType, simdTree, op4, SIMDIntrinsicSetW, baseType, size);
2763 case SIMDIntrinsicOpEquality:
2764 case SIMDIntrinsicInstEquals:
2766 op2 = impSIMDPopStack(simdType);
2767 op1 = impSIMDPopStack(simdType, instMethod);
2769 assert(op1->TypeGet() == simdType);
2770 assert(op2->TypeGet() == simdType);
2772 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, SIMDIntrinsicOpEquality, baseType, size);
2773 if (simdType == TYP_SIMD12)
2775 simdTree->gtFlags |= GTF_SIMD12_OP;
2781 case SIMDIntrinsicOpInEquality:
2783 // op1 is the first operand
2784 // op2 is the second operand
2785 op2 = impSIMDPopStack(simdType);
2786 op1 = impSIMDPopStack(simdType, instMethod);
2787 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, SIMDIntrinsicOpInEquality, baseType, size);
2788 if (simdType == TYP_SIMD12)
2790 simdTree->gtFlags |= GTF_SIMD12_OP;
2796 case SIMDIntrinsicEqual:
2797 case SIMDIntrinsicLessThan:
2798 case SIMDIntrinsicLessThanOrEqual:
2799 case SIMDIntrinsicGreaterThan:
2800 case SIMDIntrinsicGreaterThanOrEqual:
2802 op2 = impSIMDPopStack(simdType);
2803 op1 = impSIMDPopStack(simdType, instMethod);
2805 SIMDIntrinsicID intrinsicID = impSIMDRelOp(simdIntrinsicID, clsHnd, size, &baseType, &op1, &op2);
2806 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, intrinsicID, baseType, size);
2811 case SIMDIntrinsicAdd:
2812 case SIMDIntrinsicSub:
2813 case SIMDIntrinsicMul:
2814 case SIMDIntrinsicDiv:
2815 case SIMDIntrinsicBitwiseAnd:
2816 case SIMDIntrinsicBitwiseAndNot:
2817 case SIMDIntrinsicBitwiseOr:
2818 case SIMDIntrinsicBitwiseXor:
2821 // check for the cases where we don't support intrinsics.
2822 // This check should be done before we make modifications to type stack.
2823 // Note that this is more of a double safety check for robustness since
2824 // we expect getSIMDIntrinsicInfo() to have filtered out intrinsics on
2825 // unsupported base types. If getSIMdIntrinsicInfo() doesn't filter due
2826 // to some bug, assert in chk/dbg will fire.
2827 if (!varTypeIsFloating(baseType))
2829 if (simdIntrinsicID == SIMDIntrinsicMul)
2831 #if defined(_TARGET_XARCH_)
2832 if ((baseType != TYP_INT) && (baseType != TYP_SHORT))
2834 // TODO-CQ: implement mul on these integer vectors.
2835 // Note that SSE2 has no direct support for these vectors.
2836 assert(!"Mul not supported on long/ulong/uint/small int vectors\n");
2839 #endif // _TARGET_XARCH_
2840 #if defined(_TARGET_ARM64_)
2841 if ((baseType == TYP_ULONG) && (baseType == TYP_LONG))
2843 // TODO-CQ: implement mul on these integer vectors.
2844 // Note that ARM64 has no direct support for these vectors.
2845 assert(!"Mul not supported on long/ulong vectors\n");
2848 #endif // _TARGET_ARM64_
2850 #if defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
2851 // common to all integer type vectors
2852 if (simdIntrinsicID == SIMDIntrinsicDiv)
2854 // SSE2 doesn't support div on non-floating point vectors.
2855 assert(!"Div not supported on integer type vectors\n");
2858 #endif // defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
2862 // op1 is the first operand; if instance method, op1 is "this" arg
2863 // op2 is the second operand
2864 op2 = impSIMDPopStack(simdType);
2865 op1 = impSIMDPopStack(simdType, instMethod);
2867 simdTree = gtNewSIMDNode(simdType, op1, op2, simdIntrinsicID, baseType, size);
2872 case SIMDIntrinsicSelect:
2874 // op3 is a SIMD variable that is the second source
2875 // op2 is a SIMD variable that is the first source
2876 // op1 is a SIMD variable which is the bit mask.
2877 op3 = impSIMDPopStack(simdType);
2878 op2 = impSIMDPopStack(simdType);
2879 op1 = impSIMDPopStack(simdType);
2881 retVal = impSIMDSelect(clsHnd, baseType, size, op1, op2, op3);
2885 case SIMDIntrinsicMin:
2886 case SIMDIntrinsicMax:
2888 // op1 is the first operand; if instance method, op1 is "this" arg
2889 // op2 is the second operand
2890 op2 = impSIMDPopStack(simdType);
2891 op1 = impSIMDPopStack(simdType, instMethod);
2893 retVal = impSIMDMinMax(simdIntrinsicID, clsHnd, baseType, size, op1, op2);
2897 case SIMDIntrinsicGetItem:
2899 // op1 is a SIMD variable that is "this" arg
2900 // op2 is an index of TYP_INT
2901 op2 = impSIMDPopStack(TYP_INT);
2902 op1 = impSIMDPopStack(simdType, instMethod);
2903 int vectorLength = getSIMDVectorLength(size, baseType);
2904 if (!op2->IsCnsIntOrI() || op2->AsIntCon()->gtIconVal >= vectorLength || op2->AsIntCon()->gtIconVal < 0)
2906 // We need to bounds-check the length of the vector.
2907 // For that purpose, we need to clone the index expression.
2908 GenTree* index = op2;
2909 if ((index->gtFlags & GTF_SIDE_EFFECT) != 0)
2911 op2 = fgInsertCommaFormTemp(&index);
2915 op2 = gtCloneExpr(index);
2918 GenTree* lengthNode = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, vectorLength);
2919 GenTreeBoundsChk* simdChk =
2920 new (this, GT_SIMD_CHK) GenTreeBoundsChk(GT_SIMD_CHK, TYP_VOID, index, lengthNode, SCK_RNGCHK_FAIL);
2922 // Create a GT_COMMA tree for the bounds check.
2923 op2 = gtNewOperNode(GT_COMMA, op2->TypeGet(), simdChk, op2);
2926 assert(op1->TypeGet() == simdType);
2927 assert(op2->TypeGet() == TYP_INT);
2929 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, simdIntrinsicID, baseType, size);
2934 case SIMDIntrinsicDotProduct:
2936 #if defined(_TARGET_XARCH_)
2937 // Right now dot product is supported only for float/double vectors and
2938 // int vectors on SSE4/AVX.
2939 if (!varTypeIsFloating(baseType) && !(baseType == TYP_INT && getSIMDSupportLevel() >= SIMD_SSE4_Supported))
2943 #endif // _TARGET_XARCH_
2945 // op1 is a SIMD variable that is the first source and also "this" arg.
2946 // op2 is a SIMD variable which is the second source.
2947 op2 = impSIMDPopStack(simdType);
2948 op1 = impSIMDPopStack(simdType, instMethod);
2950 simdTree = gtNewSIMDNode(baseType, op1, op2, simdIntrinsicID, baseType, size);
2951 if (simdType == TYP_SIMD12)
2953 simdTree->gtFlags |= GTF_SIMD12_OP;
2959 case SIMDIntrinsicSqrt:
2961 #if (defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)) && defined(DEBUG)
2962 // SSE/AVX/ARM64 doesn't support sqrt on integer type vectors and hence
2963 // should never be seen as an intrinsic here. See SIMDIntrinsicList.h
2964 // for supported base types for this intrinsic.
2965 if (!varTypeIsFloating(baseType))
2967 assert(!"Sqrt not supported on integer vectors\n");
2970 #endif // (defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)) && defined(DEBUG)
2972 op1 = impSIMDPopStack(simdType);
2974 retVal = gtNewSIMDNode(genActualType(callType), op1, nullptr, simdIntrinsicID, baseType, size);
2978 case SIMDIntrinsicAbs:
2979 op1 = impSIMDPopStack(simdType);
2980 retVal = impSIMDAbs(clsHnd, baseType, size, op1);
2983 case SIMDIntrinsicGetW:
2984 retVal = impSIMDGetFixed(simdType, baseType, size, 3);
2987 case SIMDIntrinsicGetZ:
2988 retVal = impSIMDGetFixed(simdType, baseType, size, 2);
2991 case SIMDIntrinsicGetY:
2992 retVal = impSIMDGetFixed(simdType, baseType, size, 1);
2995 case SIMDIntrinsicGetX:
2996 retVal = impSIMDGetFixed(simdType, baseType, size, 0);
2999 case SIMDIntrinsicSetW:
3000 case SIMDIntrinsicSetZ:
3001 case SIMDIntrinsicSetY:
3002 case SIMDIntrinsicSetX:
3004 // op2 is the value to be set at indexTemp position
3005 // op1 is SIMD vector that is going to be modified, which is a byref
3007 // If op1 has a side-effect, then don't make it an intrinsic.
3008 // It would be in-efficient to read the entire vector into xmm reg,
3009 // modify it and write back entire xmm reg.
3011 // TODO-CQ: revisit this later.
3012 op1 = impStackTop(1).val;
3013 if ((op1->gtFlags & GTF_SIDE_EFFECT) != 0)
3018 op2 = impSIMDPopStack(baseType);
3019 op1 = impSIMDPopStack(simdType, instMethod);
3021 GenTree* src = gtCloneExpr(op1);
3022 assert(src != nullptr);
3023 simdTree = gtNewSIMDNode(simdType, src, op2, simdIntrinsicID, baseType, size);
3025 copyBlkDst = gtNewOperNode(GT_ADDR, TYP_BYREF, op1);
3030 // Unary operators that take and return a Vector.
3031 case SIMDIntrinsicCast:
3032 case SIMDIntrinsicConvertToSingle:
3033 case SIMDIntrinsicConvertToDouble:
3034 case SIMDIntrinsicConvertToInt32:
3036 op1 = impSIMDPopStack(simdType, instMethod);
3038 simdTree = gtNewSIMDNode(simdType, op1, nullptr, simdIntrinsicID, baseType, size);
3043 case SIMDIntrinsicConvertToInt64:
3045 #ifdef _TARGET_64BIT_
3046 op1 = impSIMDPopStack(simdType, instMethod);
3048 simdTree = gtNewSIMDNode(simdType, op1, nullptr, simdIntrinsicID, baseType, size);
3051 JITDUMP("SIMD Conversion to Int64 is not supported on this platform\n");
3057 case SIMDIntrinsicNarrow:
3059 assert(!instMethod);
3060 op2 = impSIMDPopStack(simdType);
3061 op1 = impSIMDPopStack(simdType);
3062 // op1 and op2 are two input Vector<T>.
3063 simdTree = gtNewSIMDNode(simdType, op1, op2, simdIntrinsicID, baseType, size);
3068 case SIMDIntrinsicWiden:
3070 GenTree* dstAddrHi = impSIMDPopStack(TYP_BYREF);
3071 GenTree* dstAddrLo = impSIMDPopStack(TYP_BYREF);
3072 op1 = impSIMDPopStack(simdType);
3073 GenTree* dupOp1 = fgInsertCommaFormTemp(&op1, gtGetStructHandleForSIMD(simdType, baseType));
3075 // Widen the lower half and assign it to dstAddrLo.
3076 simdTree = gtNewSIMDNode(simdType, op1, nullptr, SIMDIntrinsicWidenLo, baseType, size);
3078 new (this, GT_BLK) GenTreeBlk(GT_BLK, simdType, dstAddrLo, getSIMDTypeSizeInBytes(clsHnd));
3079 GenTree* loAsg = gtNewBlkOpNode(loDest, simdTree, getSIMDTypeSizeInBytes(clsHnd),
3080 false, // not volatile
3082 loAsg->gtFlags |= ((simdTree->gtFlags | dstAddrLo->gtFlags) & GTF_ALL_EFFECT);
3084 // Widen the upper half and assign it to dstAddrHi.
3085 simdTree = gtNewSIMDNode(simdType, dupOp1, nullptr, SIMDIntrinsicWidenHi, baseType, size);
3087 new (this, GT_BLK) GenTreeBlk(GT_BLK, simdType, dstAddrHi, getSIMDTypeSizeInBytes(clsHnd));
3088 GenTree* hiAsg = gtNewBlkOpNode(hiDest, simdTree, getSIMDTypeSizeInBytes(clsHnd),
3089 false, // not volatile
3091 hiAsg->gtFlags |= ((simdTree->gtFlags | dstAddrHi->gtFlags) & GTF_ALL_EFFECT);
3093 retVal = gtNewOperNode(GT_COMMA, simdType, loAsg, hiAsg);
3097 case SIMDIntrinsicHWAccel:
3099 GenTreeIntCon* intConstTree = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, 1);
3100 retVal = intConstTree;
3105 assert(!"Unimplemented SIMD Intrinsic");
3109 #if defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
3110 // XArch/Arm64: also indicate that we use floating point registers.
3111 // The need for setting this here is that a method may not have SIMD
3112 // type lclvars, but might be exercising SIMD intrinsics on fields of
3115 // e.g. public Vector<float> ComplexVecFloat::sqabs() { return this.r * this.r + this.i * this.i; }
3116 compFloatingPointUsed = true;
3117 #endif // defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
3119 // At this point, we have a tree that we are going to store into a destination.
3120 // TODO-1stClassStructs: This should be a simple store or assignment, and should not require
3121 // GTF_ALL_EFFECT for the dest. This is currently emulating the previous behavior of
3125 GenTree* dest = new (this, GT_BLK) GenTreeBlk(GT_BLK, simdType, copyBlkDst, getSIMDTypeSizeInBytes(clsHnd));
3126 dest->gtFlags |= GTF_GLOB_REF;
3127 retVal = gtNewBlkOpNode(dest, simdTree, getSIMDTypeSizeInBytes(clsHnd),
3128 false, // not volatile
3130 retVal->gtFlags |= ((simdTree->gtFlags | copyBlkDst->gtFlags) & GTF_ALL_EFFECT);
3136 #endif // FEATURE_SIMD