Ensure that we don't try to constant fold BSF(0) or BSR(0) (#81516)
authorTanner Gooding <tagoo@outlook.com>
Thu, 2 Feb 2023 05:46:53 +0000 (21:46 -0800)
committerGitHub <noreply@github.com>
Thu, 2 Feb 2023 05:46:53 +0000 (21:46 -0800)
* Ensure that we don't try to constant fold BSF(0) or BSR(0)

* Ensure the test returns 100 and has correct usings

* Apply formatting patch

src/coreclr/jit/valuenum.cpp
src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.cs [new file with mode: 0644]
src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.csproj [new file with mode: 0644]

index 8995eaa..cd91c27 100644 (file)
@@ -6121,40 +6121,60 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunUnary(
             case NI_X86Base_BitScanForward:
             {
                 assert(!varTypeIsSmall(type) && !varTypeIsLong(type));
+                int32_t value = GetConstantInt32(arg0VN);
 
-                int32_t  value  = GetConstantInt32(arg0VN);
-                uint32_t result = BitOperations::BitScanForward(static_cast<uint32_t>(value));
+                if (value == 0)
+                {
+                    // bsf is undefined for 0
+                    break;
+                }
 
+                uint32_t result = BitOperations::BitScanForward(static_cast<uint32_t>(value));
                 return VNForIntCon(static_cast<int32_t>(result));
             }
 
             case NI_X86Base_X64_BitScanForward:
             {
                 assert(varTypeIsLong(type));
+                int64_t value = GetConstantInt64(arg0VN);
 
-                int64_t  value  = GetConstantInt64(arg0VN);
-                uint32_t result = BitOperations::BitScanForward(static_cast<uint64_t>(value));
+                if (value == 0)
+                {
+                    // bsf is undefined for 0
+                    break;
+                }
 
+                uint32_t result = BitOperations::BitScanForward(static_cast<uint64_t>(value));
                 return VNForLongCon(static_cast<int64_t>(result));
             }
 
             case NI_X86Base_BitScanReverse:
             {
                 assert(!varTypeIsSmall(type) && !varTypeIsLong(type));
+                int32_t value = GetConstantInt32(arg0VN);
 
-                int32_t  value  = GetConstantInt32(arg0VN);
-                uint32_t result = BitOperations::BitScanReverse(static_cast<uint32_t>(value));
+                if (value == 0)
+                {
+                    // bsr is undefined for 0
+                    break;
+                }
 
+                uint32_t result = BitOperations::BitScanReverse(static_cast<uint32_t>(value));
                 return VNForIntCon(static_cast<int32_t>(result));
             }
 
             case NI_X86Base_X64_BitScanReverse:
             {
                 assert(varTypeIsLong(type));
+                int64_t value = GetConstantInt64(arg0VN);
 
-                int64_t  value  = GetConstantInt64(arg0VN);
-                uint32_t result = BitOperations::BitScanReverse(static_cast<uint64_t>(value));
+                if (value == 0)
+                {
+                    // bsr is undefined for 0
+                    break;
+                }
 
+                uint32_t result = BitOperations::BitScanReverse(static_cast<uint64_t>(value));
                 return VNForLongCon(static_cast<int64_t>(result));
             }
 #endif // TARGET_XARCH
diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.cs b/src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.cs
new file mode 100644 (file)
index 0000000..f3a52db
--- /dev/null
@@ -0,0 +1,25 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+using System.Numerics;
+using System.Runtime.CompilerServices;
+
+public static class FloatingPointHelper<TSelf>
+    where TSelf : IFloatingPoint<TSelf>
+{
+    public static int GetExponentShortestBitLength(TSelf value)
+        => value.GetExponentShortestBitLength();
+}
+
+class Runtime_81460
+{
+    static int Main()
+    {
+        return (Test() == 0) ? 100 : 0;
+    }
+
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    public static int Test() =>
+        FloatingPointHelper<double>.GetExponentShortestBitLength(1.0);
+}
diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_81460/Runtime_81460.csproj
new file mode 100644 (file)
index 0000000..75e7d24
--- /dev/null
@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <Optimize>True</Optimize>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+</Project>