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");
441 #endif // defined(_TARGET_XARCH)
442 if (typeHnd == m_simdHandleCache->Vector128FloatHandle)
444 simdBaseType = TYP_FLOAT;
445 size = Vector128SizeBytes;
446 JITDUMP(" Known type Vector128<float>\n");
448 else 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");
503 #if defined(_TARGET_ARM64_)
504 if (typeHnd == m_simdHandleCache->Vector64FloatHandle)
506 simdBaseType = TYP_FLOAT;
507 size = Vector64SizeBytes;
508 JITDUMP(" Known type Vector64<float>\n");
510 else if (typeHnd == m_simdHandleCache->Vector64IntHandle)
512 simdBaseType = TYP_INT;
513 size = Vector64SizeBytes;
514 JITDUMP(" Known type Vector64<int>\n");
516 else if (typeHnd == m_simdHandleCache->Vector64UIntHandle)
518 simdBaseType = TYP_UINT;
519 size = Vector64SizeBytes;
520 JITDUMP(" Known type Vector64<uint>\n");
522 else if (typeHnd == m_simdHandleCache->Vector64ShortHandle)
524 simdBaseType = TYP_SHORT;
525 size = Vector64SizeBytes;
526 JITDUMP(" Known type Vector64<short>\n");
528 else if (typeHnd == m_simdHandleCache->Vector64UShortHandle)
530 simdBaseType = TYP_USHORT;
531 size = Vector64SizeBytes;
532 JITDUMP(" Known type Vector64<ushort>\n");
534 else if (typeHnd == m_simdHandleCache->Vector64ByteHandle)
536 simdBaseType = TYP_BYTE;
537 size = Vector64SizeBytes;
538 JITDUMP(" Known type Vector64<sbyte>\n");
540 else if (typeHnd == m_simdHandleCache->Vector64UByteHandle)
542 simdBaseType = TYP_UBYTE;
543 size = Vector64SizeBytes;
544 JITDUMP(" Known type Vector64<byte>\n");
546 #endif // defined(_TARGET_ARM64_)
549 if (simdBaseType == TYP_UNKNOWN)
551 // Doesn't match with any of the cached type handles.
552 const char* className = getClassNameFromMetadata(typeHnd, nullptr);
553 CORINFO_CLASS_HANDLE baseTypeHnd = getTypeInstantiationArgument(typeHnd, 0);
555 if (baseTypeHnd != nullptr)
557 CorInfoType type = info.compCompHnd->getTypeForPrimitiveNumericClass(baseTypeHnd);
559 JITDUMP("HW Intrinsic SIMD Candidate Type %s with Base Type %s\n", className,
560 getClassNameFromMetadata(baseTypeHnd, nullptr));
562 #if defined(_TARGET_XARCH_)
563 if (strcmp(className, "Vector256`1") == 0)
565 size = Vector256SizeBytes;
568 case CORINFO_TYPE_FLOAT:
569 m_simdHandleCache->Vector256FloatHandle = typeHnd;
570 simdBaseType = TYP_FLOAT;
571 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<float>\n");
573 case CORINFO_TYPE_DOUBLE:
574 m_simdHandleCache->Vector256DoubleHandle = typeHnd;
575 simdBaseType = TYP_DOUBLE;
576 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<double>\n");
578 case CORINFO_TYPE_INT:
579 m_simdHandleCache->Vector256IntHandle = typeHnd;
580 simdBaseType = TYP_INT;
581 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<int>\n");
583 case CORINFO_TYPE_UINT:
584 m_simdHandleCache->Vector256UIntHandle = typeHnd;
585 simdBaseType = TYP_UINT;
586 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<uint>\n");
588 case CORINFO_TYPE_SHORT:
589 m_simdHandleCache->Vector256ShortHandle = typeHnd;
590 simdBaseType = TYP_SHORT;
591 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<short>\n");
593 case CORINFO_TYPE_USHORT:
594 m_simdHandleCache->Vector256UShortHandle = typeHnd;
595 simdBaseType = TYP_USHORT;
596 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<ushort>\n");
598 case CORINFO_TYPE_LONG:
599 m_simdHandleCache->Vector256LongHandle = typeHnd;
600 simdBaseType = TYP_LONG;
601 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<long>\n");
603 case CORINFO_TYPE_ULONG:
604 m_simdHandleCache->Vector256ULongHandle = typeHnd;
605 simdBaseType = TYP_ULONG;
606 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<ulong>\n");
608 case CORINFO_TYPE_UBYTE:
609 m_simdHandleCache->Vector256UByteHandle = typeHnd;
610 simdBaseType = TYP_UBYTE;
611 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<byte>\n");
613 case CORINFO_TYPE_BYTE:
614 m_simdHandleCache->Vector256ByteHandle = typeHnd;
615 simdBaseType = TYP_BYTE;
616 JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<sbyte>\n");
620 JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector256<T>\n");
624 #endif // defined(_TARGET_XARCH_)
625 if (strcmp(className, "Vector128`1") == 0)
627 size = Vector128SizeBytes;
630 case CORINFO_TYPE_FLOAT:
631 m_simdHandleCache->Vector128FloatHandle = typeHnd;
632 simdBaseType = TYP_FLOAT;
633 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<float>\n");
635 case CORINFO_TYPE_DOUBLE:
636 m_simdHandleCache->Vector128DoubleHandle = typeHnd;
637 simdBaseType = TYP_DOUBLE;
638 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<double>\n");
640 case CORINFO_TYPE_INT:
641 m_simdHandleCache->Vector128IntHandle = typeHnd;
642 simdBaseType = TYP_INT;
643 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<int>\n");
645 case CORINFO_TYPE_UINT:
646 m_simdHandleCache->Vector128UIntHandle = typeHnd;
647 simdBaseType = TYP_UINT;
648 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<uint>\n");
650 case CORINFO_TYPE_SHORT:
651 m_simdHandleCache->Vector128ShortHandle = typeHnd;
652 simdBaseType = TYP_SHORT;
653 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<short>\n");
655 case CORINFO_TYPE_USHORT:
656 m_simdHandleCache->Vector128UShortHandle = typeHnd;
657 simdBaseType = TYP_USHORT;
658 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<ushort>\n");
660 case CORINFO_TYPE_LONG:
661 m_simdHandleCache->Vector128LongHandle = typeHnd;
662 simdBaseType = TYP_LONG;
663 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<long>\n");
665 case CORINFO_TYPE_ULONG:
666 m_simdHandleCache->Vector128ULongHandle = typeHnd;
667 simdBaseType = TYP_ULONG;
668 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<ulong>\n");
670 case CORINFO_TYPE_UBYTE:
671 m_simdHandleCache->Vector128UByteHandle = typeHnd;
672 simdBaseType = TYP_UBYTE;
673 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<byte>\n");
675 case CORINFO_TYPE_BYTE:
676 m_simdHandleCache->Vector128ByteHandle = typeHnd;
677 simdBaseType = TYP_BYTE;
678 JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<sbyte>\n");
682 JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector128<T>\n");
685 #if defined(_TARGET_ARM64_)
686 else if (strcmp(className, "Vector64`1") == 0)
688 size = Vector64SizeBytes;
691 case CORINFO_TYPE_FLOAT:
692 m_simdHandleCache->Vector64FloatHandle = typeHnd;
693 simdBaseType = TYP_FLOAT;
694 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<float>\n");
696 case CORINFO_TYPE_INT:
697 m_simdHandleCache->Vector64IntHandle = typeHnd;
698 simdBaseType = TYP_INT;
699 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<int>\n");
701 case CORINFO_TYPE_UINT:
702 m_simdHandleCache->Vector64UIntHandle = typeHnd;
703 simdBaseType = TYP_UINT;
704 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<uint>\n");
706 case CORINFO_TYPE_SHORT:
707 m_simdHandleCache->Vector64ShortHandle = typeHnd;
708 simdBaseType = TYP_SHORT;
709 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<short>\n");
711 case CORINFO_TYPE_USHORT:
712 m_simdHandleCache->Vector64UShortHandle = typeHnd;
713 simdBaseType = TYP_USHORT;
714 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<ushort>\n");
716 case CORINFO_TYPE_UBYTE:
717 m_simdHandleCache->Vector64UByteHandle = typeHnd;
718 simdBaseType = TYP_UBYTE;
719 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<byte>\n");
721 case CORINFO_TYPE_BYTE:
722 m_simdHandleCache->Vector64ByteHandle = typeHnd;
723 simdBaseType = TYP_BYTE;
724 JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<sbyte>\n");
728 JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector64<T>\n");
731 #endif // defined(_TARGET_ARM64_)
735 if (sizeBytes != nullptr)
740 if (simdBaseType != TYP_UNKNOWN)
742 setUsesSIMDTypes(true);
745 #endif // FEATURE_HW_INTRINSICS
750 //--------------------------------------------------------------------------------------
751 // getSIMDIntrinsicInfo: get SIMD intrinsic info given the method handle.
754 // inOutTypeHnd - The handle of the type on which the method is invoked. This is an in-out param.
755 // methodHnd - The handle of the method we're interested in.
756 // sig - method signature info
757 // isNewObj - whether this call represents a newboj constructor call
758 // argCount - argument count - out pram
759 // baseType - base type of the intrinsic - out param
760 // sizeBytes - size of SIMD vector type on which the method is invoked - out param
763 // SIMDIntrinsicInfo struct initialized corresponding to methodHnd.
764 // Sets SIMDIntrinsicInfo.id to SIMDIntrinsicInvalid if methodHnd doesn't correspond
765 // to any SIMD intrinsic. Also, sets the out params inOutTypeHnd, argCount, baseType and
768 // Note that VectorMath class doesn't have a base type and first argument of the method
769 // determines the SIMD vector type on which intrinsic is invoked. In such a case inOutTypeHnd
770 // is modified by this routine.
772 // TODO-Throughput: The current implementation is based on method name string parsing.
773 // Although we now have type identification from the VM, the parsing of intrinsic names
774 // could be made more efficient.
776 const SIMDIntrinsicInfo* Compiler::getSIMDIntrinsicInfo(CORINFO_CLASS_HANDLE* inOutTypeHnd,
777 CORINFO_METHOD_HANDLE methodHnd,
778 CORINFO_SIG_INFO* sig,
785 assert(baseType != nullptr);
786 assert(sizeBytes != nullptr);
788 // get baseType and size of the type
789 CORINFO_CLASS_HANDLE typeHnd = *inOutTypeHnd;
790 *baseType = getBaseTypeAndSizeOfSIMDType(typeHnd, sizeBytes);
792 if (typeHnd == m_simdHandleCache->SIMDVectorHandle)
794 // All of the supported intrinsics on this static class take a first argument that's a vector,
795 // which determines the baseType.
796 // The exception is the IsHardwareAccelerated property, which is handled as a special case.
797 assert(*baseType == TYP_UNKNOWN);
798 if (sig->numArgs == 0)
800 const SIMDIntrinsicInfo* hwAccelIntrinsicInfo = &(simdIntrinsicInfoArray[SIMDIntrinsicHWAccel]);
801 if ((strcmp(eeGetMethodName(methodHnd, nullptr), hwAccelIntrinsicInfo->methodName) == 0) &&
802 JITtype2varType(sig->retType) == hwAccelIntrinsicInfo->retType)
805 assert(hwAccelIntrinsicInfo->argCount == 0 && hwAccelIntrinsicInfo->isInstMethod == false);
806 return hwAccelIntrinsicInfo;
812 typeHnd = info.compCompHnd->getArgClass(sig, sig->args);
813 *inOutTypeHnd = typeHnd;
814 *baseType = getBaseTypeAndSizeOfSIMDType(typeHnd, sizeBytes);
818 if (*baseType == TYP_UNKNOWN)
820 JITDUMP("NOT a SIMD Intrinsic: unsupported baseType\n");
824 // account for implicit "this" arg
825 *argCount = sig->numArgs;
831 // Get the Intrinsic Id by parsing method name.
833 // TODO-Throughput: replace sequential search by binary search by arranging entries
834 // sorted by method name.
835 SIMDIntrinsicID intrinsicId = SIMDIntrinsicInvalid;
836 const char* methodName = eeGetMethodName(methodHnd, nullptr);
837 for (int i = SIMDIntrinsicNone + 1; i < SIMDIntrinsicInvalid; ++i)
839 if (strcmp(methodName, simdIntrinsicInfoArray[i].methodName) == 0)
841 // Found an entry for the method; further check whether it is one of
842 // the supported base types.
844 for (int j = 0; j < SIMD_INTRINSIC_MAX_BASETYPE_COUNT; ++j)
846 // Convention: if there are fewer base types supported than MAX_BASETYPE_COUNT,
847 // the end of the list is marked by TYP_UNDEF.
848 if (simdIntrinsicInfoArray[i].supportedBaseTypes[j] == TYP_UNDEF)
853 if (simdIntrinsicInfoArray[i].supportedBaseTypes[j] == *baseType)
865 // Now, check the arguments.
866 unsigned int fixedArgCnt = simdIntrinsicInfoArray[i].argCount;
867 unsigned int expectedArgCnt = fixedArgCnt;
869 // First handle SIMDIntrinsicInitN, where the arg count depends on the type.
870 // The listed arg types include the vector and the first two init values, which is the expected number
871 // for Vector2. For other cases, we'll check their types here.
872 if (*argCount > expectedArgCnt)
874 if (i == SIMDIntrinsicInitN)
876 if (*argCount == 3 && typeHnd == m_simdHandleCache->SIMDVector2Handle)
880 else if (*argCount == 4 && typeHnd == m_simdHandleCache->SIMDVector3Handle)
884 else if (*argCount == 5 && typeHnd == m_simdHandleCache->SIMDVector4Handle)
889 else if (i == SIMDIntrinsicInitFixed)
891 if (*argCount == 4 && typeHnd == m_simdHandleCache->SIMDVector4Handle)
897 if (*argCount != expectedArgCnt)
902 // Validate the types of individual args passed are what is expected of.
903 // If any of the types don't match with what is expected, don't consider
904 // as an intrinsic. This will make an older JIT with SIMD capabilities
905 // resilient to breaking changes to SIMD managed API.
907 // Note that from IL type stack, args get popped in right to left order
908 // whereas args get listed in method signatures in left to right order.
910 int stackIndex = (expectedArgCnt - 1);
912 // Track the arguments from the signature - we currently only use this to distinguish
913 // integral and pointer types, both of which will by TYP_I_IMPL on the importer stack.
914 CORINFO_ARG_LIST_HANDLE argLst = sig->args;
916 CORINFO_CLASS_HANDLE argClass;
917 for (unsigned int argIndex = 0; found == true && argIndex < expectedArgCnt; argIndex++)
919 bool isThisPtr = ((argIndex == 0) && sig->hasThis());
921 // In case of "newobj SIMDVector<T>(T val)", thisPtr won't be present on type stack.
922 // We don't check anything in that case.
923 if (!isThisPtr || !isNewObj)
925 GenTree* arg = impStackTop(stackIndex).val;
926 var_types argType = arg->TypeGet();
928 var_types expectedArgType;
929 if (argIndex < fixedArgCnt)
932 // - intrinsicInfo.argType[i] == TYP_UNDEF - intrinsic doesn't have a valid arg at position i
933 // - intrinsicInfo.argType[i] == TYP_UNKNOWN - arg type should be same as basetype
934 // Note that we pop the args off in reverse order.
935 expectedArgType = simdIntrinsicInfoArray[i].argType[argIndex];
936 assert(expectedArgType != TYP_UNDEF);
937 if (expectedArgType == TYP_UNKNOWN)
939 // The type of the argument will be genActualType(*baseType).
940 expectedArgType = genActualType(*baseType);
941 argType = genActualType(argType);
946 expectedArgType = *baseType;
949 if (!isThisPtr && argType == TYP_I_IMPL)
951 // The reference implementation has a constructor that takes a pointer.
952 // We don't want to recognize that one. This requires us to look at the CorInfoType
953 // in order to distinguish a signature with a pointer argument from one with an
954 // integer argument of pointer size, both of which will be TYP_I_IMPL on the stack.
955 // TODO-Review: This seems quite fragile. We should consider beefing up the checking
957 CorInfoType corType = strip(info.compCompHnd->getArgType(sig, argLst, &argClass));
958 if (corType == CORINFO_TYPE_PTR)
964 if (varTypeIsSIMD(argType))
966 argType = TYP_STRUCT;
968 if (argType != expectedArgType)
973 if (argIndex != 0 || !sig->hasThis())
975 argLst = info.compCompHnd->getArgNext(argLst);
980 // Cross check return type and static vs. instance is what we are expecting.
981 // If not, don't consider it as an intrinsic.
982 // Note that ret type of TYP_UNKNOWN means that it is not known apriori and must be same as baseType
985 var_types expectedRetType = simdIntrinsicInfoArray[i].retType;
986 if (expectedRetType == TYP_UNKNOWN)
988 // JIT maps uint/ulong type vars to TYP_INT/TYP_LONG.
990 (*baseType == TYP_UINT || *baseType == TYP_ULONG) ? genActualType(*baseType) : *baseType;
993 if (JITtype2varType(sig->retType) != expectedRetType ||
994 sig->hasThis() != simdIntrinsicInfoArray[i].isInstMethod)
1002 intrinsicId = (SIMDIntrinsicID)i;
1008 if (intrinsicId != SIMDIntrinsicInvalid)
1010 JITDUMP("Method %s maps to SIMD intrinsic %s\n", methodName, simdIntrinsicNames[intrinsicId]);
1011 return &simdIntrinsicInfoArray[intrinsicId];
1015 JITDUMP("Method %s is NOT a SIMD intrinsic\n", methodName);
1021 // Pops and returns GenTree node from importer's type stack.
1022 // Normalizes TYP_STRUCT value in case of GT_CALL, GT_RET_EXPR and arg nodes.
1025 // type - the type of value that the caller expects to be popped off the stack.
1026 // expectAddr - if true indicates we are expecting type stack entry to be a TYP_BYREF.
1027 // structType - the class handle to use when normalizing if it is not the same as the stack entry class handle;
1028 // this can happen for certain scenarios, such as folding away a static cast, where we want the
1029 // value popped to have the type that would have been returned.
1032 // If the popped value is a struct, and the expected type is a simd type, it will be set
1033 // to that type, otherwise it will assert if the type being popped is not the expected type.
1035 GenTree* Compiler::impSIMDPopStack(var_types type, bool expectAddr, CORINFO_CLASS_HANDLE structType)
1037 StackEntry se = impPopStack();
1038 typeInfo ti = se.seTypeInfo;
1039 GenTree* tree = se.val;
1041 // If expectAddr is true implies what we have on stack is address and we need
1042 // SIMD type struct that it points to.
1045 assert(tree->TypeGet() == TYP_BYREF);
1046 if (tree->OperGet() == GT_ADDR)
1048 tree = tree->gtGetOp1();
1052 tree = gtNewOperNode(GT_IND, type, tree);
1056 bool isParam = false;
1058 // If we have a ldobj of a SIMD local we need to transform it.
1059 if (tree->OperGet() == GT_OBJ)
1061 GenTree* addr = tree->gtOp.gtOp1;
1062 if ((addr->OperGet() == GT_ADDR) && isSIMDTypeLocal(addr->gtOp.gtOp1))
1064 tree = addr->gtOp.gtOp1;
1068 if (tree->OperGet() == GT_LCL_VAR)
1070 unsigned lclNum = tree->AsLclVarCommon()->GetLclNum();
1071 LclVarDsc* lclVarDsc = &lvaTable[lclNum];
1072 isParam = lclVarDsc->lvIsParam;
1075 // normalize TYP_STRUCT value
1076 if (varTypeIsStruct(tree) && ((tree->OperGet() == GT_RET_EXPR) || (tree->OperGet() == GT_CALL) || isParam))
1078 assert(ti.IsType(TI_STRUCT));
1080 if (structType == nullptr)
1082 structType = ti.GetClassHandleForValueClass();
1085 tree = impNormStructVal(tree, structType, (unsigned)CHECK_SPILL_ALL);
1088 // Now set the type of the tree to the specialized SIMD struct type, if applicable.
1089 if (genActualType(tree->gtType) != genActualType(type))
1091 assert(tree->gtType == TYP_STRUCT);
1092 tree->gtType = type;
1094 else if (tree->gtType == TYP_BYREF)
1096 assert(tree->IsLocal() || (tree->OperGet() == GT_RET_EXPR) || (tree->OperGet() == GT_CALL) ||
1097 ((tree->gtOper == GT_ADDR) && varTypeIsSIMD(tree->gtGetOp1())));
1103 // impSIMDGetFixed: Create a GT_SIMD tree for a Get property of SIMD vector with a fixed index.
1106 // baseType - The base (element) type of the SIMD vector.
1107 // simdSize - The total size in bytes of the SIMD vector.
1108 // index - The index of the field to get.
1111 // Returns a GT_SIMD node with the SIMDIntrinsicGetItem intrinsic id.
1113 GenTreeSIMD* Compiler::impSIMDGetFixed(var_types simdType, var_types baseType, unsigned simdSize, int index)
1115 assert(simdSize >= ((index + 1) * genTypeSize(baseType)));
1117 // op1 is a SIMD source.
1118 GenTree* op1 = impSIMDPopStack(simdType, true);
1120 GenTree* op2 = gtNewIconNode(index);
1121 GenTreeSIMD* simdTree = gtNewSIMDNode(baseType, op1, op2, SIMDIntrinsicGetItem, baseType, simdSize);
1125 #ifdef _TARGET_XARCH_
1126 // impSIMDLongRelOpEqual: transforms operands and returns the SIMD intrinsic to be applied on
1127 // transformed operands to obtain == comparison result.
1130 // typeHnd - type handle of SIMD vector
1131 // size - SIMD vector size
1132 // op1 - in-out parameter; first operand
1133 // op2 - in-out parameter; second operand
1136 // Modifies in-out params op1, op2 and returns intrinsic ID to be applied to modified operands
1138 SIMDIntrinsicID Compiler::impSIMDLongRelOpEqual(CORINFO_CLASS_HANDLE typeHnd,
1143 var_types simdType = (*pOp1)->TypeGet();
1144 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1146 // There is no direct SSE2 support for comparing TYP_LONG vectors.
1147 // These have to be implemented in terms of TYP_INT vector comparison operations.
1149 // Equality(v1, v2):
1150 // tmp = (v1 == v2) i.e. compare for equality as if v1 and v2 are vector<int>
1151 // result = BitwiseAnd(t, shuffle(t, (2, 3, 0, 1)))
1152 // Shuffle is meant to swap the comparison results of low-32-bits and high 32-bits of respective long elements.
1154 // Compare vector<long> as if they were vector<int> and assign the result to a temp
1155 GenTree* compResult = gtNewSIMDNode(simdType, *pOp1, *pOp2, SIMDIntrinsicEqual, TYP_INT, size);
1156 unsigned lclNum = lvaGrabTemp(true DEBUGARG("SIMD Long =="));
1157 lvaSetStruct(lclNum, typeHnd, false);
1158 GenTree* tmp = gtNewLclvNode(lclNum, simdType);
1159 GenTree* asg = gtNewTempAssign(lclNum, compResult);
1161 // op1 = GT_COMMA(tmp=compResult, tmp)
1162 // op2 = Shuffle(tmp, 0xB1)
1163 // IntrinsicId = BitwiseAnd
1164 *pOp1 = gtNewOperNode(GT_COMMA, simdType, asg, tmp);
1165 *pOp2 = gtNewSIMDNode(simdType, gtNewLclvNode(lclNum, simdType), gtNewIconNode(SHUFFLE_ZWXY, TYP_INT),
1166 SIMDIntrinsicShuffleSSE2, TYP_INT, size);
1167 return SIMDIntrinsicBitwiseAnd;
1170 // impSIMDLongRelOpGreaterThan: transforms operands and returns the SIMD intrinsic to be applied on
1171 // transformed operands to obtain > comparison result.
1174 // typeHnd - type handle of SIMD vector
1175 // size - SIMD vector size
1176 // pOp1 - in-out parameter; first operand
1177 // pOp2 - in-out parameter; second operand
1180 // Modifies in-out params pOp1, pOp2 and returns intrinsic ID to be applied to modified operands
1182 SIMDIntrinsicID Compiler::impSIMDLongRelOpGreaterThan(CORINFO_CLASS_HANDLE typeHnd,
1187 var_types simdType = (*pOp1)->TypeGet();
1188 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1190 // GreaterThan(v1, v2) where v1 and v2 are vector long.
1191 // Let us consider the case of single long element comparison.
1192 // say L1 = (x1, y1) and L2 = (x2, y2) where x1, y1, x2, and y2 are 32-bit integers that comprise the longs L1 and
1195 // GreaterThan(L1, L2) can be expressed in terms of > relationship between 32-bit integers that comprise L1 and L2
1197 // = (x1, y1) > (x2, y2)
1198 // = (x1 > x2) || [(x1 == x2) && (y1 > y2)] - eq (1)
1200 // t = (v1 > v2) 32-bit signed comparison
1201 // u = (v1 == v2) 32-bit sized element equality
1202 // v = (v1 > v2) 32-bit unsigned comparison
1204 // z = shuffle(t, (3, 3, 1, 1)) - This corresponds to (x1 > x2) in eq(1) above
1205 // t1 = Shuffle(v, (2, 2, 0, 0)) - This corresponds to (y1 > y2) in eq(1) above
1206 // u1 = Shuffle(u, (3, 3, 1, 1)) - This corresponds to (x1 == x2) in eq(1) above
1207 // w = And(t1, u1) - This corresponds to [(x1 == x2) && (y1 > y2)] in eq(1) above
1208 // Result = BitwiseOr(z, w)
1210 // Since op1 and op2 gets used multiple times, make sure side effects are computed.
1211 GenTree* dupOp1 = nullptr;
1212 GenTree* dupOp2 = nullptr;
1213 GenTree* dupDupOp1 = nullptr;
1214 GenTree* dupDupOp2 = nullptr;
1216 if (((*pOp1)->gtFlags & GTF_SIDE_EFFECT) != 0)
1218 dupOp1 = fgInsertCommaFormTemp(pOp1, typeHnd);
1219 dupDupOp1 = gtNewLclvNode(dupOp1->AsLclVarCommon()->GetLclNum(), simdType);
1223 dupOp1 = gtCloneExpr(*pOp1);
1224 dupDupOp1 = gtCloneExpr(*pOp1);
1227 if (((*pOp2)->gtFlags & GTF_SIDE_EFFECT) != 0)
1229 dupOp2 = fgInsertCommaFormTemp(pOp2, typeHnd);
1230 dupDupOp2 = gtNewLclvNode(dupOp2->AsLclVarCommon()->GetLclNum(), simdType);
1234 dupOp2 = gtCloneExpr(*pOp2);
1235 dupDupOp2 = gtCloneExpr(*pOp2);
1238 assert(dupDupOp1 != nullptr && dupDupOp2 != nullptr);
1239 assert(dupOp1 != nullptr && dupOp2 != nullptr);
1240 assert(*pOp1 != nullptr && *pOp2 != nullptr);
1242 // v1GreaterThanv2Signed - signed 32-bit comparison
1243 GenTree* v1GreaterThanv2Signed = gtNewSIMDNode(simdType, *pOp1, *pOp2, SIMDIntrinsicGreaterThan, TYP_INT, size);
1245 // v1Equalsv2 - 32-bit equality
1246 GenTree* v1Equalsv2 = gtNewSIMDNode(simdType, dupOp1, dupOp2, SIMDIntrinsicEqual, TYP_INT, size);
1248 // v1GreaterThanv2Unsigned - unsigned 32-bit comparison
1249 var_types tempBaseType = TYP_UINT;
1250 SIMDIntrinsicID sid = impSIMDRelOp(SIMDIntrinsicGreaterThan, typeHnd, size, &tempBaseType, &dupDupOp1, &dupDupOp2);
1251 GenTree* v1GreaterThanv2Unsigned = gtNewSIMDNode(simdType, dupDupOp1, dupDupOp2, sid, tempBaseType, size);
1253 GenTree* z = gtNewSIMDNode(simdType, v1GreaterThanv2Signed, gtNewIconNode(SHUFFLE_WWYY, TYP_INT),
1254 SIMDIntrinsicShuffleSSE2, TYP_FLOAT, size);
1255 GenTree* t1 = gtNewSIMDNode(simdType, v1GreaterThanv2Unsigned, gtNewIconNode(SHUFFLE_ZZXX, TYP_INT),
1256 SIMDIntrinsicShuffleSSE2, TYP_FLOAT, size);
1257 GenTree* u1 = gtNewSIMDNode(simdType, v1Equalsv2, gtNewIconNode(SHUFFLE_WWYY, TYP_INT), SIMDIntrinsicShuffleSSE2,
1259 GenTree* w = gtNewSIMDNode(simdType, u1, t1, SIMDIntrinsicBitwiseAnd, TYP_INT, size);
1263 return SIMDIntrinsicBitwiseOr;
1266 // impSIMDLongRelOpGreaterThanOrEqual: transforms operands and returns the SIMD intrinsic to be applied on
1267 // transformed operands to obtain >= comparison result.
1270 // typeHnd - type handle of SIMD vector
1271 // size - SIMD vector size
1272 // pOp1 - in-out parameter; first operand
1273 // pOp2 - in-out parameter; second operand
1276 // Modifies in-out params pOp1, pOp2 and returns intrinsic ID to be applied to modified operands
1278 SIMDIntrinsicID Compiler::impSIMDLongRelOpGreaterThanOrEqual(CORINFO_CLASS_HANDLE typeHnd,
1283 var_types simdType = (*pOp1)->TypeGet();
1284 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1286 // expand this to (a == b) | (a > b)
1287 GenTree* dupOp1 = nullptr;
1288 GenTree* dupOp2 = nullptr;
1290 if (((*pOp1)->gtFlags & GTF_SIDE_EFFECT) != 0)
1292 dupOp1 = fgInsertCommaFormTemp(pOp1, typeHnd);
1296 dupOp1 = gtCloneExpr(*pOp1);
1299 if (((*pOp2)->gtFlags & GTF_SIDE_EFFECT) != 0)
1301 dupOp2 = fgInsertCommaFormTemp(pOp2, typeHnd);
1305 dupOp2 = gtCloneExpr(*pOp2);
1308 assert(dupOp1 != nullptr && dupOp2 != nullptr);
1309 assert(*pOp1 != nullptr && *pOp2 != nullptr);
1312 SIMDIntrinsicID id = impSIMDLongRelOpEqual(typeHnd, size, pOp1, pOp2);
1313 *pOp1 = gtNewSIMDNode(simdType, *pOp1, *pOp2, id, TYP_LONG, size);
1316 id = impSIMDLongRelOpGreaterThan(typeHnd, size, &dupOp1, &dupOp2);
1317 *pOp2 = gtNewSIMDNode(simdType, dupOp1, dupOp2, id, TYP_LONG, size);
1319 return SIMDIntrinsicBitwiseOr;
1322 // impSIMDInt32OrSmallIntRelOpGreaterThanOrEqual: transforms operands and returns the SIMD intrinsic to be applied on
1323 // transformed operands to obtain >= comparison result in case of integer base type vectors
1326 // typeHnd - type handle of SIMD vector
1327 // size - SIMD vector size
1328 // baseType - base type of SIMD vector
1329 // pOp1 - in-out parameter; first operand
1330 // pOp2 - in-out parameter; second operand
1333 // Modifies in-out params pOp1, pOp2 and returns intrinsic ID to be applied to modified operands
1335 SIMDIntrinsicID Compiler::impSIMDIntegralRelOpGreaterThanOrEqual(
1336 CORINFO_CLASS_HANDLE typeHnd, unsigned size, var_types baseType, GenTree** pOp1, GenTree** pOp2)
1338 var_types simdType = (*pOp1)->TypeGet();
1339 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1341 // This routine should be used only for integer base type vectors
1342 assert(varTypeIsIntegral(baseType));
1343 if ((getSIMDSupportLevel() == SIMD_SSE2_Supported) && ((baseType == TYP_LONG) || baseType == TYP_UBYTE))
1345 return impSIMDLongRelOpGreaterThanOrEqual(typeHnd, size, pOp1, pOp2);
1348 // expand this to (a == b) | (a > b)
1349 GenTree* dupOp1 = nullptr;
1350 GenTree* dupOp2 = nullptr;
1352 if (((*pOp1)->gtFlags & GTF_SIDE_EFFECT) != 0)
1354 dupOp1 = fgInsertCommaFormTemp(pOp1, typeHnd);
1358 dupOp1 = gtCloneExpr(*pOp1);
1361 if (((*pOp2)->gtFlags & GTF_SIDE_EFFECT) != 0)
1363 dupOp2 = fgInsertCommaFormTemp(pOp2, typeHnd);
1367 dupOp2 = gtCloneExpr(*pOp2);
1370 assert(dupOp1 != nullptr && dupOp2 != nullptr);
1371 assert(*pOp1 != nullptr && *pOp2 != nullptr);
1374 *pOp1 = gtNewSIMDNode(simdType, *pOp1, *pOp2, SIMDIntrinsicEqual, baseType, size);
1377 *pOp2 = gtNewSIMDNode(simdType, dupOp1, dupOp2, SIMDIntrinsicGreaterThan, baseType, size);
1379 return SIMDIntrinsicBitwiseOr;
1381 #endif // _TARGET_XARCH_
1383 // Transforms operands and returns the SIMD intrinsic to be applied on
1384 // transformed operands to obtain given relop result.
1387 // relOpIntrinsicId - Relational operator SIMD intrinsic
1388 // typeHnd - type handle of SIMD vector
1389 // size - SIMD vector size
1390 // inOutBaseType - base type of SIMD vector
1391 // pOp1 - in-out parameter; first operand
1392 // pOp2 - in-out parameter; second operand
1395 // Modifies in-out params pOp1, pOp2, inOutBaseType and returns intrinsic ID to be applied to modified operands
1397 SIMDIntrinsicID Compiler::impSIMDRelOp(SIMDIntrinsicID relOpIntrinsicId,
1398 CORINFO_CLASS_HANDLE typeHnd,
1400 var_types* inOutBaseType,
1404 var_types simdType = (*pOp1)->TypeGet();
1405 assert(varTypeIsSIMD(simdType) && ((*pOp2)->TypeGet() == simdType));
1407 assert(isRelOpSIMDIntrinsic(relOpIntrinsicId));
1409 SIMDIntrinsicID intrinsicID = relOpIntrinsicId;
1410 #ifdef _TARGET_XARCH_
1411 var_types baseType = *inOutBaseType;
1413 if (varTypeIsFloating(baseType))
1415 // SSE2/AVX doesn't support > and >= on vector float/double.
1416 // Therefore, we need to use < and <= with swapped operands
1417 if (relOpIntrinsicId == SIMDIntrinsicGreaterThan || relOpIntrinsicId == SIMDIntrinsicGreaterThanOrEqual)
1419 GenTree* tmp = *pOp1;
1424 (relOpIntrinsicId == SIMDIntrinsicGreaterThan) ? SIMDIntrinsicLessThan : SIMDIntrinsicLessThanOrEqual;
1427 else if (varTypeIsIntegral(baseType))
1429 // SSE/AVX doesn't support < and <= on integer base type vectors.
1430 // Therefore, we need to use > and >= with swapped operands.
1431 if (intrinsicID == SIMDIntrinsicLessThan || intrinsicID == SIMDIntrinsicLessThanOrEqual)
1433 GenTree* tmp = *pOp1;
1437 intrinsicID = (relOpIntrinsicId == SIMDIntrinsicLessThan) ? SIMDIntrinsicGreaterThan
1438 : SIMDIntrinsicGreaterThanOrEqual;
1441 if ((getSIMDSupportLevel() == SIMD_SSE2_Supported) && baseType == TYP_LONG)
1443 // There is no direct SSE2 support for comparing TYP_LONG vectors.
1444 // These have to be implemented interms of TYP_INT vector comparison operations.
1445 if (intrinsicID == SIMDIntrinsicEqual)
1447 intrinsicID = impSIMDLongRelOpEqual(typeHnd, size, pOp1, pOp2);
1449 else if (intrinsicID == SIMDIntrinsicGreaterThan)
1451 intrinsicID = impSIMDLongRelOpGreaterThan(typeHnd, size, pOp1, pOp2);
1453 else if (intrinsicID == SIMDIntrinsicGreaterThanOrEqual)
1455 intrinsicID = impSIMDLongRelOpGreaterThanOrEqual(typeHnd, size, pOp1, pOp2);
1462 // SSE2 and AVX direct support for signed comparison of int32, int16 and int8 types
1463 else if (!varTypeIsUnsigned(baseType))
1465 if (intrinsicID == SIMDIntrinsicGreaterThanOrEqual)
1467 intrinsicID = impSIMDIntegralRelOpGreaterThanOrEqual(typeHnd, size, baseType, pOp1, pOp2);
1472 // Vector<byte>, Vector<ushort>, Vector<uint> and Vector<ulong>:
1473 // SSE2 supports > for signed comparison. Therefore, to use it for
1474 // comparing unsigned numbers, we subtract a constant from both the
1475 // operands such that the result fits within the corresponding signed
1476 // type. The resulting signed numbers are compared using SSE2 signed
1479 // Vector<byte>: constant to be subtracted is 2^7
1480 // Vector<ushort> constant to be subtracted is 2^15
1481 // Vector<uint> constant to be subtracted is 2^31
1482 // Vector<ulong> constant to be subtracted is 2^63
1484 // We need to treat op1 and op2 as signed for comparison purpose after
1485 // the transformation.
1486 __int64 constVal = 0;
1490 constVal = 0x80808080;
1491 *inOutBaseType = TYP_BYTE;
1494 constVal = 0x80008000;
1495 *inOutBaseType = TYP_SHORT;
1498 constVal = 0x80000000;
1499 *inOutBaseType = TYP_INT;
1502 constVal = 0x8000000000000000LL;
1503 *inOutBaseType = TYP_LONG;
1509 assert(constVal != 0);
1511 // This transformation is not required for equality.
1512 if (intrinsicID != SIMDIntrinsicEqual)
1514 // For constructing const vector use either long or int base type.
1515 var_types tempBaseType;
1517 if (baseType == TYP_ULONG)
1519 tempBaseType = TYP_LONG;
1520 initVal = gtNewLconNode(constVal);
1524 tempBaseType = TYP_INT;
1525 initVal = gtNewIconNode((ssize_t)constVal);
1527 initVal->gtType = tempBaseType;
1528 GenTree* constVector = gtNewSIMDNode(simdType, initVal, nullptr, SIMDIntrinsicInit, tempBaseType, size);
1530 // Assign constVector to a temp, since we intend to use it more than once
1531 // TODO-CQ: We have quite a few such constant vectors constructed during
1532 // the importation of SIMD intrinsics. Make sure that we have a single
1533 // temp per distinct constant per method.
1534 GenTree* tmp = fgInsertCommaFormTemp(&constVector, typeHnd);
1536 // op1 = op1 - constVector
1537 // op2 = op2 - constVector
1538 *pOp1 = gtNewSIMDNode(simdType, *pOp1, constVector, SIMDIntrinsicSub, baseType, size);
1539 *pOp2 = gtNewSIMDNode(simdType, *pOp2, tmp, SIMDIntrinsicSub, baseType, size);
1542 return impSIMDRelOp(intrinsicID, typeHnd, size, inOutBaseType, pOp1, pOp2);
1545 #elif defined(_TARGET_ARM64_)
1546 // TODO-ARM64-CQ handle comparisons against zero
1548 // _TARGET_ARM64_ doesn't support < and <= on register register comparisons
1549 // Therefore, we need to use > and >= with swapped operands.
1550 if (intrinsicID == SIMDIntrinsicLessThan || intrinsicID == SIMDIntrinsicLessThanOrEqual)
1552 GenTree* tmp = *pOp1;
1557 (intrinsicID == SIMDIntrinsicLessThan) ? SIMDIntrinsicGreaterThan : SIMDIntrinsicGreaterThanOrEqual;
1559 #else // !_TARGET_XARCH_
1560 assert(!"impSIMDRelOp() unimplemented on target arch");
1562 #endif // !_TARGET_XARCH_
1567 //-------------------------------------------------------------------------
1568 // impSIMDAbs: creates GT_SIMD node to compute Abs value of a given vector.
1571 // typeHnd - type handle of SIMD vector
1572 // baseType - base type of vector
1573 // size - vector size in bytes
1574 // op1 - operand of Abs intrinsic
1576 GenTree* Compiler::impSIMDAbs(CORINFO_CLASS_HANDLE typeHnd, var_types baseType, unsigned size, GenTree* op1)
1578 assert(varTypeIsSIMD(op1));
1580 var_types simdType = op1->TypeGet();
1581 GenTree* retVal = nullptr;
1583 #ifdef _TARGET_XARCH_
1584 // When there is no direct support, Abs(v) could be computed
1585 // on integer vectors as follows:
1586 // BitVector = v < vector.Zero
1587 // result = ConditionalSelect(BitVector, vector.Zero - v, v)
1589 bool useConditionalSelect = false;
1590 if (getSIMDSupportLevel() == SIMD_SSE2_Supported)
1592 // SSE2 doesn't support abs on signed integer type vectors.
1593 if (baseType == TYP_LONG || baseType == TYP_INT || baseType == TYP_SHORT || baseType == TYP_BYTE)
1595 useConditionalSelect = true;
1600 assert(getSIMDSupportLevel() >= SIMD_SSE4_Supported);
1601 if (baseType == TYP_LONG)
1603 // SSE4/AVX2 don't support abs on long type vector.
1604 useConditionalSelect = true;
1608 if (useConditionalSelect)
1610 // This works only on integer vectors not on float/double vectors.
1611 assert(varTypeIsIntegral(baseType));
1616 if (op1->OperGet() == GT_LCL_VAR)
1618 op1LclNum = op1->gtLclVarCommon.gtLclNum;
1619 op1Assign = nullptr;
1623 op1LclNum = lvaGrabTemp(true DEBUGARG("SIMD Abs op1"));
1624 lvaSetStruct(op1LclNum, typeHnd, false);
1625 op1Assign = gtNewTempAssign(op1LclNum, op1);
1626 op1 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1629 // Assign Vector.Zero to a temp since it is needed more than once
1630 GenTree* vecZero = gtNewSIMDVectorZero(simdType, baseType, size);
1631 unsigned vecZeroLclNum = lvaGrabTemp(true DEBUGARG("SIMD Abs VecZero"));
1632 lvaSetStruct(vecZeroLclNum, typeHnd, false);
1633 GenTree* vecZeroAssign = gtNewTempAssign(vecZeroLclNum, vecZero);
1635 // Construct BitVector = v < vector.Zero
1636 GenTree* bitVecOp1 = op1;
1637 GenTree* bitVecOp2 = gtNewLclvNode(vecZeroLclNum, vecZero->TypeGet());
1638 var_types relOpBaseType = baseType;
1639 SIMDIntrinsicID relOpIntrinsic =
1640 impSIMDRelOp(SIMDIntrinsicLessThan, typeHnd, size, &relOpBaseType, &bitVecOp1, &bitVecOp2);
1641 GenTree* bitVec = gtNewSIMDNode(simdType, bitVecOp1, bitVecOp2, relOpIntrinsic, relOpBaseType, size);
1642 unsigned bitVecLclNum = lvaGrabTemp(true DEBUGARG("SIMD Abs bitVec"));
1643 lvaSetStruct(bitVecLclNum, typeHnd, false);
1644 GenTree* bitVecAssign = gtNewTempAssign(bitVecLclNum, bitVec);
1645 bitVec = gtNewLclvNode(bitVecLclNum, bitVec->TypeGet());
1647 // Construct condSelectOp1 = vector.Zero - v
1648 GenTree* subOp1 = gtNewLclvNode(vecZeroLclNum, vecZero->TypeGet());
1649 GenTree* subOp2 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1650 GenTree* negVec = gtNewSIMDNode(simdType, subOp1, subOp2, SIMDIntrinsicSub, baseType, size);
1652 // Construct ConditionalSelect(bitVec, vector.Zero - v, v)
1653 GenTree* vec = gtNewLclvNode(op1LclNum, op1->TypeGet());
1654 retVal = impSIMDSelect(typeHnd, baseType, size, bitVec, negVec, vec);
1656 // Prepend bitVec assignment to retVal.
1657 // retVal = (tmp2 = v < tmp1), CondSelect(tmp2, tmp1 - v, v)
1658 retVal = gtNewOperNode(GT_COMMA, simdType, bitVecAssign, retVal);
1660 // Prepend vecZero assignment to retVal.
1661 // retVal = (tmp1 = vector.Zero), (tmp2 = v < tmp1), CondSelect(tmp2, tmp1 - v, v)
1662 retVal = gtNewOperNode(GT_COMMA, simdType, vecZeroAssign, retVal);
1664 // If op1 was assigned to a temp, prepend that to retVal.
1665 if (op1Assign != nullptr)
1667 // retVal = (v=op1), (tmp1 = vector.Zero), (tmp2 = v < tmp1), CondSelect(tmp2, tmp1 - v, v)
1668 retVal = gtNewOperNode(GT_COMMA, simdType, op1Assign, retVal);
1671 else if (varTypeIsFloating(baseType))
1673 // Abs(vf) = vf & new SIMDVector<float>(0x7fffffff);
1674 // Abs(vd) = vf & new SIMDVector<double>(0x7fffffffffffffff);
1675 GenTree* bitMask = nullptr;
1676 if (baseType == TYP_FLOAT)
1679 static_assert_no_msg(sizeof(float) == sizeof(int));
1680 *((int*)&f) = 0x7fffffff;
1681 bitMask = gtNewDconNode(f);
1683 else if (baseType == TYP_DOUBLE)
1686 static_assert_no_msg(sizeof(double) == sizeof(__int64));
1687 *((__int64*)&d) = 0x7fffffffffffffffLL;
1688 bitMask = gtNewDconNode(d);
1691 assert(bitMask != nullptr);
1692 bitMask->gtType = baseType;
1693 GenTree* bitMaskVector = gtNewSIMDNode(simdType, bitMask, SIMDIntrinsicInit, baseType, size);
1694 retVal = gtNewSIMDNode(simdType, op1, bitMaskVector, SIMDIntrinsicBitwiseAnd, baseType, size);
1696 else if (baseType == TYP_USHORT || baseType == TYP_UBYTE || baseType == TYP_UINT || baseType == TYP_ULONG)
1698 // Abs is a no-op on unsigned integer type vectors
1703 assert(getSIMDSupportLevel() >= SIMD_SSE4_Supported);
1704 assert(baseType != TYP_LONG);
1706 retVal = gtNewSIMDNode(simdType, op1, SIMDIntrinsicAbs, baseType, size);
1708 #elif defined(_TARGET_ARM64_)
1709 if (varTypeIsUnsigned(baseType))
1711 // Abs is a no-op on unsigned integer type vectors
1716 retVal = gtNewSIMDNode(simdType, op1, SIMDIntrinsicAbs, baseType, size);
1718 #else // !defined(_TARGET_XARCH)_ && !defined(_TARGET_ARM64_)
1719 assert(!"Abs intrinsic on non-xarch target not implemented");
1720 #endif // !_TARGET_XARCH_
1725 // Creates a GT_SIMD tree for Select operation
1728 // typeHnd - type handle of SIMD vector
1729 // baseType - base type of SIMD vector
1730 // size - SIMD vector size
1731 // op1 - first operand = Condition vector vc
1732 // op2 - second operand = va
1733 // op3 - third operand = vb
1736 // Returns GT_SIMD tree that computes Select(vc, va, vb)
1738 GenTree* Compiler::impSIMDSelect(
1739 CORINFO_CLASS_HANDLE typeHnd, var_types baseType, unsigned size, GenTree* op1, GenTree* op2, GenTree* op3)
1741 assert(varTypeIsSIMD(op1));
1742 var_types simdType = op1->TypeGet();
1743 assert(op2->TypeGet() == simdType);
1744 assert(op3->TypeGet() == simdType);
1746 // TODO-ARM64-CQ Support generating select instruction for SIMD
1748 // Select(BitVector vc, va, vb) = (va & vc) | (vb & !vc)
1749 // Select(op1, op2, op3) = (op2 & op1) | (op3 & !op1)
1750 // = SIMDIntrinsicBitwiseOr(SIMDIntrinsicBitwiseAnd(op2, op1),
1751 // SIMDIntrinsicBitwiseAndNot(op3, op1))
1753 // If Op1 has side effect, create an assignment to a temp
1755 GenTree* asg = nullptr;
1756 if ((op1->gtFlags & GTF_SIDE_EFFECT) != 0)
1758 unsigned lclNum = lvaGrabTemp(true DEBUGARG("SIMD Select"));
1759 lvaSetStruct(lclNum, typeHnd, false);
1760 tmp = gtNewLclvNode(lclNum, op1->TypeGet());
1761 asg = gtNewTempAssign(lclNum, op1);
1764 GenTree* andExpr = gtNewSIMDNode(simdType, op2, tmp, SIMDIntrinsicBitwiseAnd, baseType, size);
1765 GenTree* dupOp1 = gtCloneExpr(tmp);
1766 assert(dupOp1 != nullptr);
1767 #ifdef _TARGET_ARM64_
1768 // ARM64 implements SIMDIntrinsicBitwiseAndNot as Left & ~Right
1769 GenTree* andNotExpr = gtNewSIMDNode(simdType, op3, dupOp1, SIMDIntrinsicBitwiseAndNot, baseType, size);
1771 // XARCH implements SIMDIntrinsicBitwiseAndNot as ~Left & Right
1772 GenTree* andNotExpr = gtNewSIMDNode(simdType, dupOp1, op3, SIMDIntrinsicBitwiseAndNot, baseType, size);
1774 GenTree* simdTree = gtNewSIMDNode(simdType, andExpr, andNotExpr, SIMDIntrinsicBitwiseOr, baseType, size);
1776 // If asg not null, create a GT_COMMA tree.
1779 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), asg, simdTree);
1785 // Creates a GT_SIMD tree for Min/Max operation
1788 // IntrinsicId - SIMD intrinsic Id, either Min or Max
1789 // typeHnd - type handle of SIMD vector
1790 // baseType - base type of SIMD vector
1791 // size - SIMD vector size
1792 // op1 - first operand = va
1793 // op2 - second operand = vb
1796 // Returns GT_SIMD tree that computes Max(va, vb)
1798 GenTree* Compiler::impSIMDMinMax(SIMDIntrinsicID intrinsicId,
1799 CORINFO_CLASS_HANDLE typeHnd,
1805 assert(intrinsicId == SIMDIntrinsicMin || intrinsicId == SIMDIntrinsicMax);
1806 assert(varTypeIsSIMD(op1));
1807 var_types simdType = op1->TypeGet();
1808 assert(op2->TypeGet() == simdType);
1810 #if defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
1811 GenTree* simdTree = nullptr;
1813 #ifdef _TARGET_XARCH_
1814 // SSE2 has direct support for float/double/signed word/unsigned byte.
1815 // SSE4.1 has direct support for int32/uint32/signed byte/unsigned word.
1816 // For other integer types we compute min/max as follows
1818 // int32/uint32 (SSE2)
1819 // int64/uint64 (SSE2&SSE4):
1820 // compResult = (op1 < op2) in case of Min
1821 // (op1 > op2) in case of Max
1822 // Min/Max(op1, op2) = Select(compResult, op1, op2)
1824 // unsigned word (SSE2):
1825 // op1 = op1 - 2^15 ; to make it fit within a signed word
1826 // op2 = op2 - 2^15 ; to make it fit within a signed word
1827 // result = SSE2 signed word Min/Max(op1, op2)
1828 // result = result + 2^15 ; readjust it back
1830 // signed byte (SSE2):
1831 // op1 = op1 + 2^7 ; to make it unsigned
1832 // op1 = op1 + 2^7 ; to make it unsigned
1833 // result = SSE2 unsigned byte Min/Max(op1, op2)
1834 // result = result - 2^15 ; readjust it back
1836 if (varTypeIsFloating(baseType) || baseType == TYP_SHORT || baseType == TYP_UBYTE ||
1837 (getSIMDSupportLevel() >= SIMD_SSE4_Supported &&
1838 (baseType == TYP_BYTE || baseType == TYP_INT || baseType == TYP_UINT || baseType == TYP_USHORT)))
1840 // SSE2 or SSE4.1 has direct support
1841 simdTree = gtNewSIMDNode(simdType, op1, op2, intrinsicId, baseType, size);
1843 else if (baseType == TYP_USHORT || baseType == TYP_BYTE)
1845 assert(getSIMDSupportLevel() == SIMD_SSE2_Supported);
1847 SIMDIntrinsicID operIntrinsic;
1848 SIMDIntrinsicID adjustIntrinsic;
1849 var_types minMaxOperBaseType;
1850 if (baseType == TYP_USHORT)
1852 constVal = 0x80008000;
1853 operIntrinsic = SIMDIntrinsicSub;
1854 adjustIntrinsic = SIMDIntrinsicAdd;
1855 minMaxOperBaseType = TYP_SHORT;
1859 assert(baseType == TYP_BYTE);
1860 constVal = 0x80808080;
1861 operIntrinsic = SIMDIntrinsicAdd;
1862 adjustIntrinsic = SIMDIntrinsicSub;
1863 minMaxOperBaseType = TYP_UBYTE;
1866 GenTree* initVal = gtNewIconNode(constVal);
1867 GenTree* constVector = gtNewSIMDNode(simdType, initVal, nullptr, SIMDIntrinsicInit, TYP_INT, size);
1869 // Assign constVector to a temp, since we intend to use it more than once
1870 // TODO-CQ: We have quite a few such constant vectors constructed during
1871 // the importation of SIMD intrinsics. Make sure that we have a single
1872 // temp per distinct constant per method.
1873 GenTree* tmp = fgInsertCommaFormTemp(&constVector, typeHnd);
1875 // op1 = op1 - constVector
1876 // op2 = op2 - constVector
1877 op1 = gtNewSIMDNode(simdType, op1, constVector, operIntrinsic, baseType, size);
1878 op2 = gtNewSIMDNode(simdType, op2, tmp, operIntrinsic, baseType, size);
1880 // compute min/max of op1 and op2 considering them as if minMaxOperBaseType
1881 simdTree = gtNewSIMDNode(simdType, op1, op2, intrinsicId, minMaxOperBaseType, size);
1883 // re-adjust the value by adding or subtracting constVector
1884 tmp = gtNewLclvNode(tmp->AsLclVarCommon()->GetLclNum(), tmp->TypeGet());
1885 simdTree = gtNewSIMDNode(simdType, simdTree, tmp, adjustIntrinsic, baseType, size);
1887 #elif defined(_TARGET_ARM64_)
1888 // Arm64 has direct support for all types except int64/uint64
1889 // For which we compute min/max as follows
1892 // compResult = (op1 < op2) in case of Min
1893 // (op1 > op2) in case of Max
1894 // Min/Max(op1, op2) = Select(compResult, op1, op2)
1895 if (baseType != TYP_ULONG && baseType != TYP_LONG)
1897 simdTree = gtNewSIMDNode(simdType, op1, op2, intrinsicId, baseType, size);
1902 GenTree* dupOp1 = nullptr;
1903 GenTree* dupOp2 = nullptr;
1904 GenTree* op1Assign = nullptr;
1905 GenTree* op2Assign = nullptr;
1909 if ((op1->gtFlags & GTF_SIDE_EFFECT) != 0)
1911 op1LclNum = lvaGrabTemp(true DEBUGARG("SIMD Min/Max"));
1912 dupOp1 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1913 lvaSetStruct(op1LclNum, typeHnd, false);
1914 op1Assign = gtNewTempAssign(op1LclNum, op1);
1915 op1 = gtNewLclvNode(op1LclNum, op1->TypeGet());
1919 dupOp1 = gtCloneExpr(op1);
1922 if ((op2->gtFlags & GTF_SIDE_EFFECT) != 0)
1924 op2LclNum = lvaGrabTemp(true DEBUGARG("SIMD Min/Max"));
1925 dupOp2 = gtNewLclvNode(op2LclNum, op2->TypeGet());
1926 lvaSetStruct(op2LclNum, typeHnd, false);
1927 op2Assign = gtNewTempAssign(op2LclNum, op2);
1928 op2 = gtNewLclvNode(op2LclNum, op2->TypeGet());
1932 dupOp2 = gtCloneExpr(op2);
1935 SIMDIntrinsicID relOpIntrinsic =
1936 (intrinsicId == SIMDIntrinsicMin) ? SIMDIntrinsicLessThan : SIMDIntrinsicGreaterThan;
1937 var_types relOpBaseType = baseType;
1939 // compResult = op1 relOp op2
1940 // simdTree = Select(compResult, op1, op2);
1941 assert(dupOp1 != nullptr);
1942 assert(dupOp2 != nullptr);
1943 relOpIntrinsic = impSIMDRelOp(relOpIntrinsic, typeHnd, size, &relOpBaseType, &dupOp1, &dupOp2);
1944 GenTree* compResult = gtNewSIMDNode(simdType, dupOp1, dupOp2, relOpIntrinsic, relOpBaseType, size);
1945 unsigned compResultLclNum = lvaGrabTemp(true DEBUGARG("SIMD Min/Max"));
1946 lvaSetStruct(compResultLclNum, typeHnd, false);
1947 GenTree* compResultAssign = gtNewTempAssign(compResultLclNum, compResult);
1948 compResult = gtNewLclvNode(compResultLclNum, compResult->TypeGet());
1949 simdTree = impSIMDSelect(typeHnd, baseType, size, compResult, op1, op2);
1950 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), compResultAssign, simdTree);
1952 // Now create comma trees if we have created assignments of op1/op2 to temps
1953 if (op2Assign != nullptr)
1955 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), op2Assign, simdTree);
1958 if (op1Assign != nullptr)
1960 simdTree = gtNewOperNode(GT_COMMA, simdTree->TypeGet(), op1Assign, simdTree);
1964 assert(simdTree != nullptr);
1966 #else // !(defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_))
1967 assert(!"impSIMDMinMax() unimplemented on target arch");
1969 #endif // !(defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_))
1972 //------------------------------------------------------------------------
1973 // getOp1ForConstructor: Get the op1 for a constructor call.
1976 // opcode - the opcode being handled (needed to identify the CEE_NEWOBJ case)
1977 // newobjThis - For CEE_NEWOBJ, this is the temp grabbed for the allocated uninitalized object.
1978 // clsHnd - The handle of the class of the method.
1981 // The tree node representing the object to be initialized with the constructor.
1984 // This method handles the differences between the CEE_NEWOBJ and constructor cases.
1986 GenTree* Compiler::getOp1ForConstructor(OPCODE opcode, GenTree* newobjThis, CORINFO_CLASS_HANDLE clsHnd)
1989 if (opcode == CEE_NEWOBJ)
1992 assert(newobjThis->gtOper == GT_ADDR && newobjThis->gtOp.gtOp1->gtOper == GT_LCL_VAR);
1994 // push newobj result on type stack
1995 unsigned tmp = op1->gtOp.gtOp1->gtLclVarCommon.gtLclNum;
1996 impPushOnStack(gtNewLclvNode(tmp, lvaGetRealType(tmp)), verMakeTypeInfo(clsHnd).NormaliseForStack());
2000 op1 = impSIMDPopStack(TYP_BYREF);
2002 assert(op1->TypeGet() == TYP_BYREF);
2006 //-------------------------------------------------------------------
2007 // Set the flag that indicates that the lclVar referenced by this tree
2008 // is used in a SIMD intrinsic.
2012 void Compiler::setLclRelatedToSIMDIntrinsic(GenTree* tree)
2014 assert(tree->OperIsLocal());
2015 unsigned lclNum = tree->AsLclVarCommon()->GetLclNum();
2016 LclVarDsc* lclVarDsc = &lvaTable[lclNum];
2017 lclVarDsc->lvUsedInSIMDIntrinsic = true;
2020 //-------------------------------------------------------------
2021 // Check if two field nodes reference at the same memory location.
2022 // Notice that this check is just based on pattern matching.
2027 // If op1's parents node and op2's parents node are at the same location, return true. Otherwise, return false
2029 bool areFieldsParentsLocatedSame(GenTree* op1, GenTree* op2)
2031 assert(op1->OperGet() == GT_FIELD);
2032 assert(op2->OperGet() == GT_FIELD);
2034 GenTree* op1ObjRef = op1->gtField.gtFldObj;
2035 GenTree* op2ObjRef = op2->gtField.gtFldObj;
2036 while (op1ObjRef != nullptr && op2ObjRef != nullptr)
2039 if (op1ObjRef->OperGet() != op2ObjRef->OperGet())
2043 else if (op1ObjRef->OperGet() == GT_ADDR)
2045 op1ObjRef = op1ObjRef->gtOp.gtOp1;
2046 op2ObjRef = op2ObjRef->gtOp.gtOp1;
2049 if (op1ObjRef->OperIsLocal() && op2ObjRef->OperIsLocal() &&
2050 op1ObjRef->AsLclVarCommon()->GetLclNum() == op2ObjRef->AsLclVarCommon()->GetLclNum())
2054 else if (op1ObjRef->OperGet() == GT_FIELD && op2ObjRef->OperGet() == GT_FIELD &&
2055 op1ObjRef->gtField.gtFldHnd == op2ObjRef->gtField.gtFldHnd)
2057 op1ObjRef = op1ObjRef->gtField.gtFldObj;
2058 op2ObjRef = op2ObjRef->gtField.gtFldObj;
2070 //----------------------------------------------------------------------
2071 // Check whether two field are contiguous
2073 // first - GenTree*. The Type of the node should be TYP_FLOAT
2074 // second - GenTree*. The Type of the node should be TYP_FLOAT
2076 // if the first field is located before second field, and they are located contiguously,
2077 // then return true. Otherwise, return false.
2079 bool Compiler::areFieldsContiguous(GenTree* first, GenTree* second)
2081 assert(first->OperGet() == GT_FIELD);
2082 assert(second->OperGet() == GT_FIELD);
2083 assert(first->gtType == TYP_FLOAT);
2084 assert(second->gtType == TYP_FLOAT);
2086 var_types firstFieldType = first->gtType;
2087 var_types secondFieldType = second->gtType;
2089 unsigned firstFieldEndOffset = first->gtField.gtFldOffset + genTypeSize(firstFieldType);
2090 unsigned secondFieldOffset = second->gtField.gtFldOffset;
2091 if (firstFieldEndOffset == secondFieldOffset && firstFieldType == secondFieldType &&
2092 areFieldsParentsLocatedSame(first, second))
2100 //-------------------------------------------------------------------------------
2101 // Check whether two array element nodes are located contiguously or not.
2106 // if the array element op1 is located before array element op2, and they are contiguous,
2107 // then return true. Otherwise, return false.
2109 // Right this can only check array element with const number as index. In future,
2110 // we should consider to allow this function to check the index using expression.
2112 bool Compiler::areArrayElementsContiguous(GenTree* op1, GenTree* op2)
2114 noway_assert(op1->gtOper == GT_INDEX);
2115 noway_assert(op2->gtOper == GT_INDEX);
2116 GenTreeIndex* op1Index = op1->AsIndex();
2117 GenTreeIndex* op2Index = op2->AsIndex();
2119 GenTree* op1ArrayRef = op1Index->Arr();
2120 GenTree* op2ArrayRef = op2Index->Arr();
2121 assert(op1ArrayRef->TypeGet() == TYP_REF);
2122 assert(op2ArrayRef->TypeGet() == TYP_REF);
2124 GenTree* op1IndexNode = op1Index->Index();
2125 GenTree* op2IndexNode = op2Index->Index();
2126 if ((op1IndexNode->OperGet() == GT_CNS_INT && op2IndexNode->OperGet() == GT_CNS_INT) &&
2127 op1IndexNode->gtIntCon.gtIconVal + 1 == op2IndexNode->gtIntCon.gtIconVal)
2129 if (op1ArrayRef->OperGet() == GT_FIELD && op2ArrayRef->OperGet() == GT_FIELD &&
2130 areFieldsParentsLocatedSame(op1ArrayRef, op2ArrayRef))
2134 else if (op1ArrayRef->OperIsLocal() && op2ArrayRef->OperIsLocal() &&
2135 op1ArrayRef->AsLclVarCommon()->GetLclNum() == op2ArrayRef->AsLclVarCommon()->GetLclNum())
2143 //-------------------------------------------------------------------------------
2144 // Check whether two argument nodes are contiguous or not.
2149 // if the argument node op1 is located before argument node op2, and they are located contiguously,
2150 // then return true. Otherwise, return false.
2152 // Right now this can only check field and array. In future we should add more cases.
2155 bool Compiler::areArgumentsContiguous(GenTree* op1, GenTree* op2)
2157 if (op1->OperGet() == GT_INDEX && op2->OperGet() == GT_INDEX)
2159 return areArrayElementsContiguous(op1, op2);
2161 else if (op1->OperGet() == GT_FIELD && op2->OperGet() == GT_FIELD)
2163 return areFieldsContiguous(op1, op2);
2168 //--------------------------------------------------------------------------------------------------------
2169 // createAddressNodeForSIMDInit: Generate the address node(GT_LEA) if we want to intialize vector2, vector3 or vector4
2170 // from first argument's address.
2173 // tree - GenTree*. This the tree node which is used to get the address for indir.
2174 // simdsize - unsigned. This the simd vector size.
2175 // arrayElementsCount - unsigned. This is used for generating the boundary check for array.
2178 // return the address node.
2181 // 1. Currently just support for GT_FIELD and GT_INDEX, because we can only verify the GT_INDEX node or GT_Field
2182 // are located contiguously or not. In future we should support more cases.
2183 // 2. Though it happens to just work fine front-end phases are not aware of GT_LEA node. Therefore, convert these
2185 GenTree* Compiler::createAddressNodeForSIMDInit(GenTree* tree, unsigned simdSize)
2187 assert(tree->OperGet() == GT_FIELD || tree->OperGet() == GT_INDEX);
2188 GenTree* byrefNode = nullptr;
2189 GenTree* startIndex = nullptr;
2190 unsigned offset = 0;
2191 var_types baseType = tree->gtType;
2193 if (tree->OperGet() == GT_FIELD)
2195 GenTree* objRef = tree->gtField.gtFldObj;
2196 if (objRef != nullptr && objRef->gtOper == GT_ADDR)
2198 GenTree* obj = objRef->gtOp.gtOp1;
2200 // If the field is directly from a struct, then in this case,
2201 // we should set this struct's lvUsedInSIMDIntrinsic as true,
2202 // so that this sturct won't be promoted.
2203 // e.g. s.x x is a field, and s is a struct, then we should set the s's lvUsedInSIMDIntrinsic as true.
2204 // so that s won't be promoted.
2205 // 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
2206 // s1 can be promoted, so that s2 can be promoted. The reason for that is if we don't allow s1 to be
2207 // promoted, then this will affect the other optimizations which are depend on s1's struct promotion.
2209 // In future, we should optimize this case so that if there is a nested field like s1.s2.x and s1.s2.x's
2210 // address is used for initializing the vector, then s1 can be promoted but s2 can't.
2211 if (varTypeIsSIMD(obj) && obj->OperIsLocal())
2213 setLclRelatedToSIMDIntrinsic(obj);
2217 byrefNode = gtCloneExpr(tree->gtField.gtFldObj);
2218 assert(byrefNode != nullptr);
2219 offset = tree->gtField.gtFldOffset;
2221 else if (tree->OperGet() == GT_INDEX)
2224 GenTree* index = tree->AsIndex()->Index();
2225 assert(index->OperGet() == GT_CNS_INT);
2227 GenTree* checkIndexExpr = nullptr;
2228 unsigned indexVal = (unsigned)(index->gtIntCon.gtIconVal);
2229 offset = indexVal * genTypeSize(tree->TypeGet());
2230 GenTree* arrayRef = tree->AsIndex()->Arr();
2232 // Generate the boundary check exception.
2233 // The length for boundary check should be the maximum index number which should be
2234 // (first argument's index number) + (how many array arguments we have) - 1
2235 // = indexVal + arrayElementsCount - 1
2236 unsigned arrayElementsCount = simdSize / genTypeSize(baseType);
2237 checkIndexExpr = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, indexVal + arrayElementsCount - 1);
2238 GenTreeArrLen* arrLen = gtNewArrLen(TYP_INT, arrayRef, (int)OFFSETOF__CORINFO_Array__length);
2239 GenTreeBoundsChk* arrBndsChk = new (this, GT_ARR_BOUNDS_CHECK)
2240 GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, checkIndexExpr, arrLen, SCK_RNGCHK_FAIL);
2242 offset += OFFSETOF__CORINFO_Array__data;
2243 byrefNode = gtNewOperNode(GT_COMMA, arrayRef->TypeGet(), arrBndsChk, gtCloneExpr(arrayRef));
2250 new (this, GT_LEA) GenTreeAddrMode(TYP_BYREF, byrefNode, startIndex, genTypeSize(tree->TypeGet()), offset);
2254 //-------------------------------------------------------------------------------
2255 // impMarkContiguousSIMDFieldAssignments: Try to identify if there are contiguous
2256 // assignments from SIMD field to memory. If there are, then mark the related
2257 // lclvar so that it won't be promoted.
2260 // stmt - GenTree*. Input statement node.
2262 void Compiler::impMarkContiguousSIMDFieldAssignments(GenTree* stmt)
2264 if (!featureSIMD || opts.MinOpts())
2268 GenTree* expr = stmt->gtStmt.gtStmtExpr;
2269 if (expr->OperGet() == GT_ASG && expr->TypeGet() == TYP_FLOAT)
2271 GenTree* curDst = expr->gtOp.gtOp1;
2272 GenTree* curSrc = expr->gtOp.gtOp2;
2274 var_types baseType = TYP_UNKNOWN;
2275 unsigned simdSize = 0;
2276 GenTree* srcSimdStructNode = getSIMDStructFromField(curSrc, &baseType, &index, &simdSize, true);
2277 if (srcSimdStructNode == nullptr || baseType != TYP_FLOAT)
2279 fgPreviousCandidateSIMDFieldAsgStmt = nullptr;
2281 else if (index == 0 && isSIMDTypeLocal(srcSimdStructNode))
2283 fgPreviousCandidateSIMDFieldAsgStmt = stmt;
2285 else if (fgPreviousCandidateSIMDFieldAsgStmt != nullptr)
2288 GenTree* prevAsgExpr = fgPreviousCandidateSIMDFieldAsgStmt->gtStmt.gtStmtExpr;
2289 GenTree* prevDst = prevAsgExpr->gtOp.gtOp1;
2290 GenTree* prevSrc = prevAsgExpr->gtOp.gtOp2;
2291 if (!areArgumentsContiguous(prevDst, curDst) || !areArgumentsContiguous(prevSrc, curSrc))
2293 fgPreviousCandidateSIMDFieldAsgStmt = nullptr;
2297 if (index == (simdSize / genTypeSize(baseType) - 1))
2299 // Successfully found the pattern, mark the lclvar as UsedInSIMDIntrinsic
2300 if (srcSimdStructNode->OperIsLocal())
2302 setLclRelatedToSIMDIntrinsic(srcSimdStructNode);
2305 if (curDst->OperGet() == GT_FIELD)
2307 GenTree* objRef = curDst->gtField.gtFldObj;
2308 if (objRef != nullptr && objRef->gtOper == GT_ADDR)
2310 GenTree* obj = objRef->gtOp.gtOp1;
2311 if (varTypeIsStruct(obj) && obj->OperIsLocal())
2313 setLclRelatedToSIMDIntrinsic(obj);
2320 fgPreviousCandidateSIMDFieldAsgStmt = stmt;
2327 fgPreviousCandidateSIMDFieldAsgStmt = nullptr;
2331 //------------------------------------------------------------------------
2332 // impSIMDIntrinsic: Check method to see if it is a SIMD method
2335 // opcode - the opcode being handled (needed to identify the CEE_NEWOBJ case)
2336 // newobjThis - For CEE_NEWOBJ, this is the temp grabbed for the allocated uninitalized object.
2337 // clsHnd - The handle of the class of the method.
2338 // method - The handle of the method.
2339 // sig - The call signature for the method.
2340 // memberRef - The memberRef token for the method reference.
2343 // If clsHnd is a known SIMD type, and 'method' is one of the methods that are
2344 // implemented as an intrinsic in the JIT, then return the tree that implements
2347 GenTree* Compiler::impSIMDIntrinsic(OPCODE opcode,
2348 GenTree* newobjThis,
2349 CORINFO_CLASS_HANDLE clsHnd,
2350 CORINFO_METHOD_HANDLE methodHnd,
2351 CORINFO_SIG_INFO* sig,
2352 unsigned methodFlags,
2355 assert(featureSIMD);
2357 // Exit early if we are not in one of the SIMD types.
2358 if (!isSIMDClass(clsHnd))
2363 #ifdef FEATURE_CORECLR
2364 // For coreclr, we also exit early if the method is not a JIT Intrinsic (which requires the [Intrinsic] attribute).
2365 if ((methodFlags & CORINFO_FLG_JIT_INTRINSIC) == 0)
2369 #endif // FEATURE_CORECLR
2371 // Get base type and intrinsic Id
2372 var_types baseType = TYP_UNKNOWN;
2374 unsigned argCount = 0;
2375 const SIMDIntrinsicInfo* intrinsicInfo =
2376 getSIMDIntrinsicInfo(&clsHnd, methodHnd, sig, (opcode == CEE_NEWOBJ), &argCount, &baseType, &size);
2377 if (intrinsicInfo == nullptr || intrinsicInfo->id == SIMDIntrinsicInvalid)
2382 SIMDIntrinsicID simdIntrinsicID = intrinsicInfo->id;
2384 if (baseType != TYP_UNKNOWN)
2386 simdType = getSIMDTypeForSize(size);
2390 assert(simdIntrinsicID == SIMDIntrinsicHWAccel);
2391 simdType = TYP_UNKNOWN;
2393 bool instMethod = intrinsicInfo->isInstMethod;
2394 var_types callType = JITtype2varType(sig->retType);
2395 if (callType == TYP_STRUCT)
2397 // Note that here we are assuming that, if the call returns a struct, that it is the same size as the
2398 // struct on which the method is declared. This is currently true for all methods on Vector types,
2399 // but if this ever changes, we will need to determine the callType from the signature.
2400 assert(info.compCompHnd->getClassSize(sig->retTypeClass) == genTypeSize(simdType));
2401 callType = simdType;
2404 GenTree* simdTree = nullptr;
2405 GenTree* op1 = nullptr;
2406 GenTree* op2 = nullptr;
2407 GenTree* op3 = nullptr;
2408 GenTree* retVal = nullptr;
2409 GenTree* copyBlkDst = nullptr;
2410 bool doCopyBlk = false;
2412 switch (simdIntrinsicID)
2414 case SIMDIntrinsicGetCount:
2416 int length = getSIMDVectorLength(clsHnd);
2417 GenTreeIntCon* intConstTree = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, length);
2418 retVal = intConstTree;
2420 intConstTree->gtFlags |= GTF_ICON_SIMD_COUNT;
2424 case SIMDIntrinsicGetZero:
2425 retVal = gtNewSIMDVectorZero(simdType, baseType, size);
2428 case SIMDIntrinsicGetOne:
2429 retVal = gtNewSIMDVectorOne(simdType, baseType, size);
2432 case SIMDIntrinsicGetAllOnes:
2434 // Equivalent to (Vector<T>) new Vector<int>(0xffffffff);
2435 GenTree* initVal = gtNewIconNode(0xffffffff, TYP_INT);
2436 simdTree = gtNewSIMDNode(simdType, initVal, nullptr, SIMDIntrinsicInit, TYP_INT, size);
2437 if (baseType != TYP_INT)
2439 // cast it to required baseType if different from TYP_INT
2440 simdTree = gtNewSIMDNode(simdType, simdTree, nullptr, SIMDIntrinsicCast, baseType, size);
2446 case SIMDIntrinsicInit:
2447 case SIMDIntrinsicInitN:
2449 // SIMDIntrinsicInit:
2450 // op2 - the initializer value
2451 // op1 - byref of vector
2453 // SIMDIntrinsicInitN
2454 // op2 - list of initializer values stitched into a list
2455 // op1 - byref of vector
2456 bool initFromFirstArgIndir = false;
2457 if (simdIntrinsicID == SIMDIntrinsicInit)
2459 op2 = impSIMDPopStack(baseType);
2463 assert(simdIntrinsicID == SIMDIntrinsicInitN);
2464 assert(baseType == TYP_FLOAT);
2466 unsigned initCount = argCount - 1;
2467 unsigned elementCount = getSIMDVectorLength(size, baseType);
2468 noway_assert(initCount == elementCount);
2469 GenTree* nextArg = op2;
2471 // Build a GT_LIST with the N values.
2472 // We must maintain left-to-right order of the args, but we will pop
2473 // them off in reverse order (the Nth arg was pushed onto the stack last).
2475 GenTree* list = nullptr;
2476 GenTree* firstArg = nullptr;
2477 GenTree* prevArg = nullptr;
2479 bool areArgsContiguous = true;
2480 for (unsigned i = 0; i < initCount; i++)
2482 GenTree* nextArg = impSIMDPopStack(baseType);
2483 if (areArgsContiguous)
2485 GenTree* curArg = nextArg;
2488 if (prevArg != nullptr)
2490 // Recall that we are popping the args off the stack in reverse order.
2491 areArgsContiguous = areArgumentsContiguous(curArg, prevArg);
2496 list = new (this, GT_LIST) GenTreeOp(GT_LIST, baseType, nextArg, list);
2499 if (areArgsContiguous && baseType == TYP_FLOAT)
2501 // Since Vector2, Vector3 and Vector4's arguments type are only float,
2502 // we intialize the vector from first argument address, only when
2503 // the baseType is TYP_FLOAT and the arguments are located contiguously in memory
2504 initFromFirstArgIndir = true;
2505 GenTree* op2Address = createAddressNodeForSIMDInit(firstArg, size);
2506 var_types simdType = getSIMDTypeForSize(size);
2507 op2 = gtNewOperNode(GT_IND, simdType, op2Address);
2515 op1 = getOp1ForConstructor(opcode, newobjThis, clsHnd);
2517 assert(op1->TypeGet() == TYP_BYREF);
2518 assert(genActualType(op2->TypeGet()) == genActualType(baseType) || initFromFirstArgIndir);
2520 // For integral base types of size less than TYP_INT, expand the initializer
2521 // to fill size of TYP_INT bytes.
2522 if (varTypeIsSmallInt(baseType))
2524 // This case should occur only for Init intrinsic.
2525 assert(simdIntrinsicID == SIMDIntrinsicInit);
2527 unsigned baseSize = genTypeSize(baseType);
2531 multiplier = 0x01010101;
2535 assert(baseSize == 2);
2536 multiplier = 0x00010001;
2539 GenTree* t1 = nullptr;
2540 if (baseType == TYP_BYTE)
2542 // What we have is a signed byte initializer,
2543 // which when loaded to a reg will get sign extended to TYP_INT.
2544 // But what we need is the initializer without sign extended or
2545 // rather zero extended to 32-bits.
2546 t1 = gtNewOperNode(GT_AND, TYP_INT, op2, gtNewIconNode(0xff, TYP_INT));
2548 else if (baseType == TYP_SHORT)
2550 // What we have is a signed short initializer,
2551 // which when loaded to a reg will get sign extended to TYP_INT.
2552 // But what we need is the initializer without sign extended or
2553 // rather zero extended to 32-bits.
2554 t1 = gtNewOperNode(GT_AND, TYP_INT, op2, gtNewIconNode(0xffff, TYP_INT));
2558 assert(baseType == TYP_UBYTE || baseType == TYP_USHORT);
2559 t1 = gtNewCastNode(TYP_INT, op2, false, TYP_INT);
2562 assert(t1 != nullptr);
2563 GenTree* t2 = gtNewIconNode(multiplier, TYP_INT);
2564 op2 = gtNewOperNode(GT_MUL, TYP_INT, t1, t2);
2566 // Construct a vector of TYP_INT with the new initializer and cast it back to vector of baseType
2567 simdTree = gtNewSIMDNode(simdType, op2, nullptr, simdIntrinsicID, TYP_INT, size);
2568 simdTree = gtNewSIMDNode(simdType, simdTree, nullptr, SIMDIntrinsicCast, baseType, size);
2573 if (initFromFirstArgIndir)
2576 if (op1->gtOp.gtOp1->OperIsLocal())
2578 // label the dst struct's lclvar is used for SIMD intrinsic,
2579 // so that this dst struct won't be promoted.
2580 setLclRelatedToSIMDIntrinsic(op1->gtOp.gtOp1);
2585 simdTree = gtNewSIMDNode(simdType, op2, nullptr, simdIntrinsicID, baseType, size);
2594 case SIMDIntrinsicInitArray:
2595 case SIMDIntrinsicInitArrayX:
2596 case SIMDIntrinsicCopyToArray:
2597 case SIMDIntrinsicCopyToArrayX:
2599 // op3 - index into array in case of SIMDIntrinsicCopyToArrayX and SIMDIntrinsicInitArrayX
2600 // op2 - array itself
2601 // op1 - byref to vector struct
2603 unsigned int vectorLength = getSIMDVectorLength(size, baseType);
2604 // (This constructor takes only the zero-based arrays.)
2605 // We will add one or two bounds checks:
2606 // 1. If we have an index, we must do a check on that first.
2607 // We can't combine it with the index + vectorLength check because
2608 // a. It might be negative, and b. It may need to raise a different exception
2609 // (captured as SCK_ARG_RNG_EXCPN for CopyTo and SCK_RNGCHK_FAIL for Init).
2610 // 2. We need to generate a check (SCK_ARG_EXCPN for CopyTo and SCK_RNGCHK_FAIL for Init)
2611 // for the last array element we will access.
2612 // We'll either check against (vectorLength - 1) or (index + vectorLength - 1).
2614 GenTree* checkIndexExpr = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, vectorLength - 1);
2616 // Get the index into the array. If it has been provided, it will be on the
2617 // top of the stack. Otherwise, it is null.
2620 op3 = impSIMDPopStack(TYP_INT);
2621 if (op3->IsIntegralConst(0))
2628 // TODO-CQ: Here, or elsewhere, check for the pattern where op2 is a newly constructed array, and
2629 // change this to the InitN form.
2630 // op3 = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, 0);
2634 // Clone the array for use in the bounds check.
2635 op2 = impSIMDPopStack(TYP_REF);
2636 assert(op2->TypeGet() == TYP_REF);
2637 GenTree* arrayRefForArgChk = op2;
2638 GenTree* argRngChk = nullptr;
2639 GenTree* asg = nullptr;
2640 if ((arrayRefForArgChk->gtFlags & GTF_SIDE_EFFECT) != 0)
2642 op2 = fgInsertCommaFormTemp(&arrayRefForArgChk);
2646 op2 = gtCloneExpr(arrayRefForArgChk);
2648 assert(op2 != nullptr);
2652 SpecialCodeKind op3CheckKind;
2653 if (simdIntrinsicID == SIMDIntrinsicInitArrayX)
2655 op3CheckKind = SCK_RNGCHK_FAIL;
2659 assert(simdIntrinsicID == SIMDIntrinsicCopyToArrayX);
2660 op3CheckKind = SCK_ARG_RNG_EXCPN;
2662 // We need to use the original expression on this, which is the first check.
2663 GenTree* arrayRefForArgRngChk = arrayRefForArgChk;
2664 // Then we clone the clone we just made for the next check.
2665 arrayRefForArgChk = gtCloneExpr(op2);
2666 // We know we MUST have had a cloneable expression.
2667 assert(arrayRefForArgChk != nullptr);
2668 GenTree* index = op3;
2669 if ((index->gtFlags & GTF_SIDE_EFFECT) != 0)
2671 op3 = fgInsertCommaFormTemp(&index);
2675 op3 = gtCloneExpr(index);
2678 GenTreeArrLen* arrLen =
2679 gtNewArrLen(TYP_INT, arrayRefForArgRngChk, (int)OFFSETOF__CORINFO_Array__length);
2680 argRngChk = new (this, GT_ARR_BOUNDS_CHECK)
2681 GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, index, arrLen, op3CheckKind);
2682 // Now, clone op3 to create another node for the argChk
2683 GenTree* index2 = gtCloneExpr(op3);
2684 assert(index != nullptr);
2685 checkIndexExpr = gtNewOperNode(GT_ADD, TYP_INT, index2, checkIndexExpr);
2688 // Insert a bounds check for index + offset - 1.
2689 // This must be a "normal" array.
2690 SpecialCodeKind op2CheckKind;
2691 if (simdIntrinsicID == SIMDIntrinsicInitArray || simdIntrinsicID == SIMDIntrinsicInitArrayX)
2693 op2CheckKind = SCK_RNGCHK_FAIL;
2697 op2CheckKind = SCK_ARG_EXCPN;
2699 GenTreeArrLen* arrLen = gtNewArrLen(TYP_INT, arrayRefForArgChk, (int)OFFSETOF__CORINFO_Array__length);
2700 GenTreeBoundsChk* argChk = new (this, GT_ARR_BOUNDS_CHECK)
2701 GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, checkIndexExpr, arrLen, op2CheckKind);
2703 // Create a GT_COMMA tree for the bounds check(s).
2704 op2 = gtNewOperNode(GT_COMMA, op2->TypeGet(), argChk, op2);
2705 if (argRngChk != nullptr)
2707 op2 = gtNewOperNode(GT_COMMA, op2->TypeGet(), argRngChk, op2);
2710 if (simdIntrinsicID == SIMDIntrinsicInitArray || simdIntrinsicID == SIMDIntrinsicInitArrayX)
2712 op1 = getOp1ForConstructor(opcode, newobjThis, clsHnd);
2713 simdTree = gtNewSIMDNode(simdType, op2, op3, SIMDIntrinsicInitArray, baseType, size);
2719 assert(simdIntrinsicID == SIMDIntrinsicCopyToArray || simdIntrinsicID == SIMDIntrinsicCopyToArrayX);
2720 op1 = impSIMDPopStack(simdType, instMethod);
2721 assert(op1->TypeGet() == simdType);
2723 // copy vector (op1) to array (op2) starting at index (op3)
2726 // TODO-Cleanup: Though it happens to just work fine front-end phases are not aware of GT_LEA node.
2727 // Therefore, convert these to use GT_ADDR .
2728 copyBlkDst = new (this, GT_LEA)
2729 GenTreeAddrMode(TYP_BYREF, op2, op3, genTypeSize(baseType), OFFSETOF__CORINFO_Array__data);
2735 case SIMDIntrinsicInitFixed:
2737 // We are initializing a fixed-length vector VLarge with a smaller fixed-length vector VSmall, plus 1 or 2
2738 // additional floats.
2739 // op4 (optional) - float value for VLarge.W, if VLarge is Vector4, and VSmall is Vector2
2740 // op3 - float value for VLarge.Z or VLarge.W
2742 // op1 - byref of VLarge
2743 assert(baseType == TYP_FLOAT);
2744 unsigned elementByteCount = 4;
2746 GenTree* op4 = nullptr;
2749 op4 = impSIMDPopStack(TYP_FLOAT);
2750 assert(op4->TypeGet() == TYP_FLOAT);
2752 op3 = impSIMDPopStack(TYP_FLOAT);
2753 assert(op3->TypeGet() == TYP_FLOAT);
2754 // The input vector will either be TYP_SIMD8 or TYP_SIMD12.
2755 var_types smallSIMDType = TYP_SIMD8;
2756 if ((op4 == nullptr) && (simdType == TYP_SIMD16))
2758 smallSIMDType = TYP_SIMD12;
2760 op2 = impSIMDPopStack(smallSIMDType);
2761 op1 = getOp1ForConstructor(opcode, newobjThis, clsHnd);
2763 // We are going to redefine the operands so that:
2764 // - op3 is the value that's going into the Z position, or null if it's a Vector4 constructor with a single
2766 // - op4 is the W position value, or null if this is a Vector3 constructor.
2767 if (size == 16 && argCount == 3)
2776 simdTree = gtNewSIMDNode(simdType, simdTree, op3, SIMDIntrinsicSetZ, baseType, size);
2780 simdTree = gtNewSIMDNode(simdType, simdTree, op4, SIMDIntrinsicSetW, baseType, size);
2788 case SIMDIntrinsicOpEquality:
2789 case SIMDIntrinsicInstEquals:
2791 op2 = impSIMDPopStack(simdType);
2792 op1 = impSIMDPopStack(simdType, instMethod);
2794 assert(op1->TypeGet() == simdType);
2795 assert(op2->TypeGet() == simdType);
2797 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, SIMDIntrinsicOpEquality, baseType, size);
2798 if (simdType == TYP_SIMD12)
2800 simdTree->gtFlags |= GTF_SIMD12_OP;
2806 case SIMDIntrinsicOpInEquality:
2808 // op1 is the first operand
2809 // op2 is the second operand
2810 op2 = impSIMDPopStack(simdType);
2811 op1 = impSIMDPopStack(simdType, instMethod);
2812 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, SIMDIntrinsicOpInEquality, baseType, size);
2813 if (simdType == TYP_SIMD12)
2815 simdTree->gtFlags |= GTF_SIMD12_OP;
2821 case SIMDIntrinsicEqual:
2822 case SIMDIntrinsicLessThan:
2823 case SIMDIntrinsicLessThanOrEqual:
2824 case SIMDIntrinsicGreaterThan:
2825 case SIMDIntrinsicGreaterThanOrEqual:
2827 op2 = impSIMDPopStack(simdType);
2828 op1 = impSIMDPopStack(simdType, instMethod);
2830 SIMDIntrinsicID intrinsicID = impSIMDRelOp(simdIntrinsicID, clsHnd, size, &baseType, &op1, &op2);
2831 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, intrinsicID, baseType, size);
2836 case SIMDIntrinsicAdd:
2837 case SIMDIntrinsicSub:
2838 case SIMDIntrinsicMul:
2839 case SIMDIntrinsicDiv:
2840 case SIMDIntrinsicBitwiseAnd:
2841 case SIMDIntrinsicBitwiseAndNot:
2842 case SIMDIntrinsicBitwiseOr:
2843 case SIMDIntrinsicBitwiseXor:
2846 // check for the cases where we don't support intrinsics.
2847 // This check should be done before we make modifications to type stack.
2848 // Note that this is more of a double safety check for robustness since
2849 // we expect getSIMDIntrinsicInfo() to have filtered out intrinsics on
2850 // unsupported base types. If getSIMdIntrinsicInfo() doesn't filter due
2851 // to some bug, assert in chk/dbg will fire.
2852 if (!varTypeIsFloating(baseType))
2854 if (simdIntrinsicID == SIMDIntrinsicMul)
2856 #if defined(_TARGET_XARCH_)
2857 if ((baseType != TYP_INT) && (baseType != TYP_SHORT))
2859 // TODO-CQ: implement mul on these integer vectors.
2860 // Note that SSE2 has no direct support for these vectors.
2861 assert(!"Mul not supported on long/ulong/uint/small int vectors\n");
2864 #endif // _TARGET_XARCH_
2865 #if defined(_TARGET_ARM64_)
2866 if ((baseType == TYP_ULONG) && (baseType == TYP_LONG))
2868 // TODO-CQ: implement mul on these integer vectors.
2869 // Note that ARM64 has no direct support for these vectors.
2870 assert(!"Mul not supported on long/ulong vectors\n");
2873 #endif // _TARGET_ARM64_
2875 #if defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
2876 // common to all integer type vectors
2877 if (simdIntrinsicID == SIMDIntrinsicDiv)
2879 // SSE2 doesn't support div on non-floating point vectors.
2880 assert(!"Div not supported on integer type vectors\n");
2883 #endif // defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
2887 // op1 is the first operand; if instance method, op1 is "this" arg
2888 // op2 is the second operand
2889 op2 = impSIMDPopStack(simdType);
2890 op1 = impSIMDPopStack(simdType, instMethod);
2892 #ifdef _TARGET_XARCH_
2893 if (simdIntrinsicID == SIMDIntrinsicBitwiseAndNot)
2895 // XARCH implements SIMDIntrinsicBitwiseAndNot as ~op1 & op2, while the
2896 // software implementation does op1 & ~op2, so we need to swap the operands
2902 #endif // _TARGET_XARCH_
2904 simdTree = gtNewSIMDNode(simdType, op1, op2, simdIntrinsicID, baseType, size);
2909 case SIMDIntrinsicSelect:
2911 // op3 is a SIMD variable that is the second source
2912 // op2 is a SIMD variable that is the first source
2913 // op1 is a SIMD variable which is the bit mask.
2914 op3 = impSIMDPopStack(simdType);
2915 op2 = impSIMDPopStack(simdType);
2916 op1 = impSIMDPopStack(simdType);
2918 retVal = impSIMDSelect(clsHnd, baseType, size, op1, op2, op3);
2922 case SIMDIntrinsicMin:
2923 case SIMDIntrinsicMax:
2925 // op1 is the first operand; if instance method, op1 is "this" arg
2926 // op2 is the second operand
2927 op2 = impSIMDPopStack(simdType);
2928 op1 = impSIMDPopStack(simdType, instMethod);
2930 retVal = impSIMDMinMax(simdIntrinsicID, clsHnd, baseType, size, op1, op2);
2934 case SIMDIntrinsicGetItem:
2936 // op1 is a SIMD variable that is "this" arg
2937 // op2 is an index of TYP_INT
2938 op2 = impSIMDPopStack(TYP_INT);
2939 op1 = impSIMDPopStack(simdType, instMethod);
2940 int vectorLength = getSIMDVectorLength(size, baseType);
2941 if (!op2->IsCnsIntOrI() || op2->AsIntCon()->gtIconVal >= vectorLength || op2->AsIntCon()->gtIconVal < 0)
2943 // We need to bounds-check the length of the vector.
2944 // For that purpose, we need to clone the index expression.
2945 GenTree* index = op2;
2946 if ((index->gtFlags & GTF_SIDE_EFFECT) != 0)
2948 op2 = fgInsertCommaFormTemp(&index);
2952 op2 = gtCloneExpr(index);
2955 GenTree* lengthNode = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, vectorLength);
2956 GenTreeBoundsChk* simdChk =
2957 new (this, GT_SIMD_CHK) GenTreeBoundsChk(GT_SIMD_CHK, TYP_VOID, index, lengthNode, SCK_RNGCHK_FAIL);
2959 // Create a GT_COMMA tree for the bounds check.
2960 op2 = gtNewOperNode(GT_COMMA, op2->TypeGet(), simdChk, op2);
2963 assert(op1->TypeGet() == simdType);
2964 assert(op2->TypeGet() == TYP_INT);
2966 simdTree = gtNewSIMDNode(genActualType(callType), op1, op2, simdIntrinsicID, baseType, size);
2971 case SIMDIntrinsicDotProduct:
2973 #if defined(_TARGET_XARCH_)
2974 // Right now dot product is supported only for float/double vectors and
2975 // int vectors on SSE4/AVX.
2976 if (!varTypeIsFloating(baseType) && !(baseType == TYP_INT && getSIMDSupportLevel() >= SIMD_SSE4_Supported))
2980 #endif // _TARGET_XARCH_
2982 // op1 is a SIMD variable that is the first source and also "this" arg.
2983 // op2 is a SIMD variable which is the second source.
2984 op2 = impSIMDPopStack(simdType);
2985 op1 = impSIMDPopStack(simdType, instMethod);
2987 simdTree = gtNewSIMDNode(baseType, op1, op2, simdIntrinsicID, baseType, size);
2988 if (simdType == TYP_SIMD12)
2990 simdTree->gtFlags |= GTF_SIMD12_OP;
2996 case SIMDIntrinsicSqrt:
2998 #if (defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)) && defined(DEBUG)
2999 // SSE/AVX/ARM64 doesn't support sqrt on integer type vectors and hence
3000 // should never be seen as an intrinsic here. See SIMDIntrinsicList.h
3001 // for supported base types for this intrinsic.
3002 if (!varTypeIsFloating(baseType))
3004 assert(!"Sqrt not supported on integer vectors\n");
3007 #endif // (defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)) && defined(DEBUG)
3009 op1 = impSIMDPopStack(simdType);
3011 retVal = gtNewSIMDNode(genActualType(callType), op1, nullptr, simdIntrinsicID, baseType, size);
3015 case SIMDIntrinsicAbs:
3016 op1 = impSIMDPopStack(simdType);
3017 retVal = impSIMDAbs(clsHnd, baseType, size, op1);
3020 case SIMDIntrinsicGetW:
3021 retVal = impSIMDGetFixed(simdType, baseType, size, 3);
3024 case SIMDIntrinsicGetZ:
3025 retVal = impSIMDGetFixed(simdType, baseType, size, 2);
3028 case SIMDIntrinsicGetY:
3029 retVal = impSIMDGetFixed(simdType, baseType, size, 1);
3032 case SIMDIntrinsicGetX:
3033 retVal = impSIMDGetFixed(simdType, baseType, size, 0);
3036 case SIMDIntrinsicSetW:
3037 case SIMDIntrinsicSetZ:
3038 case SIMDIntrinsicSetY:
3039 case SIMDIntrinsicSetX:
3041 // op2 is the value to be set at indexTemp position
3042 // op1 is SIMD vector that is going to be modified, which is a byref
3044 // If op1 has a side-effect, then don't make it an intrinsic.
3045 // It would be in-efficient to read the entire vector into xmm reg,
3046 // modify it and write back entire xmm reg.
3048 // TODO-CQ: revisit this later.
3049 op1 = impStackTop(1).val;
3050 if ((op1->gtFlags & GTF_SIDE_EFFECT) != 0)
3055 op2 = impSIMDPopStack(baseType);
3056 op1 = impSIMDPopStack(simdType, instMethod);
3058 GenTree* src = gtCloneExpr(op1);
3059 assert(src != nullptr);
3060 simdTree = gtNewSIMDNode(simdType, src, op2, simdIntrinsicID, baseType, size);
3062 copyBlkDst = gtNewOperNode(GT_ADDR, TYP_BYREF, op1);
3067 // Unary operators that take and return a Vector.
3068 case SIMDIntrinsicCast:
3069 case SIMDIntrinsicConvertToSingle:
3070 case SIMDIntrinsicConvertToDouble:
3071 case SIMDIntrinsicConvertToInt32:
3073 op1 = impSIMDPopStack(simdType, instMethod);
3075 simdTree = gtNewSIMDNode(simdType, op1, nullptr, simdIntrinsicID, baseType, size);
3080 case SIMDIntrinsicConvertToInt64:
3082 #ifdef _TARGET_64BIT_
3083 op1 = impSIMDPopStack(simdType, instMethod);
3085 simdTree = gtNewSIMDNode(simdType, op1, nullptr, simdIntrinsicID, baseType, size);
3088 JITDUMP("SIMD Conversion to Int64 is not supported on this platform\n");
3094 case SIMDIntrinsicNarrow:
3096 assert(!instMethod);
3097 op2 = impSIMDPopStack(simdType);
3098 op1 = impSIMDPopStack(simdType);
3099 // op1 and op2 are two input Vector<T>.
3100 simdTree = gtNewSIMDNode(simdType, op1, op2, simdIntrinsicID, baseType, size);
3105 case SIMDIntrinsicWiden:
3107 GenTree* dstAddrHi = impSIMDPopStack(TYP_BYREF);
3108 GenTree* dstAddrLo = impSIMDPopStack(TYP_BYREF);
3109 op1 = impSIMDPopStack(simdType);
3110 GenTree* dupOp1 = fgInsertCommaFormTemp(&op1, gtGetStructHandleForSIMD(simdType, baseType));
3112 // Widen the lower half and assign it to dstAddrLo.
3113 simdTree = gtNewSIMDNode(simdType, op1, nullptr, SIMDIntrinsicWidenLo, baseType, size);
3115 new (this, GT_BLK) GenTreeBlk(GT_BLK, simdType, dstAddrLo, getSIMDTypeSizeInBytes(clsHnd));
3116 GenTree* loAsg = gtNewBlkOpNode(loDest, simdTree, getSIMDTypeSizeInBytes(clsHnd),
3117 false, // not volatile
3119 loAsg->gtFlags |= ((simdTree->gtFlags | dstAddrLo->gtFlags) & GTF_ALL_EFFECT);
3121 // Widen the upper half and assign it to dstAddrHi.
3122 simdTree = gtNewSIMDNode(simdType, dupOp1, nullptr, SIMDIntrinsicWidenHi, baseType, size);
3124 new (this, GT_BLK) GenTreeBlk(GT_BLK, simdType, dstAddrHi, getSIMDTypeSizeInBytes(clsHnd));
3125 GenTree* hiAsg = gtNewBlkOpNode(hiDest, simdTree, getSIMDTypeSizeInBytes(clsHnd),
3126 false, // not volatile
3128 hiAsg->gtFlags |= ((simdTree->gtFlags | dstAddrHi->gtFlags) & GTF_ALL_EFFECT);
3130 retVal = gtNewOperNode(GT_COMMA, simdType, loAsg, hiAsg);
3134 case SIMDIntrinsicHWAccel:
3136 GenTreeIntCon* intConstTree = new (this, GT_CNS_INT) GenTreeIntCon(TYP_INT, 1);
3137 retVal = intConstTree;
3142 assert(!"Unimplemented SIMD Intrinsic");
3146 #if defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
3147 // XArch/Arm64: also indicate that we use floating point registers.
3148 // The need for setting this here is that a method may not have SIMD
3149 // type lclvars, but might be exercising SIMD intrinsics on fields of
3152 // e.g. public Vector<float> ComplexVecFloat::sqabs() { return this.r * this.r + this.i * this.i; }
3153 compFloatingPointUsed = true;
3154 #endif // defined(_TARGET_XARCH_) || defined(_TARGET_ARM64_)
3156 // At this point, we have a tree that we are going to store into a destination.
3157 // TODO-1stClassStructs: This should be a simple store or assignment, and should not require
3158 // GTF_ALL_EFFECT for the dest. This is currently emulating the previous behavior of
3162 GenTree* dest = new (this, GT_BLK) GenTreeBlk(GT_BLK, simdType, copyBlkDst, getSIMDTypeSizeInBytes(clsHnd));
3163 dest->gtFlags |= GTF_GLOB_REF;
3164 retVal = gtNewBlkOpNode(dest, simdTree, getSIMDTypeSizeInBytes(clsHnd),
3165 false, // not volatile
3167 retVal->gtFlags |= ((simdTree->gtFlags | copyBlkDst->gtFlags) & GTF_ALL_EFFECT);
3173 #endif // FEATURE_SIMD