Do not create small constants while morphing cascading addition (#66270)
authorSingleAccretion <62474226+SingleAccretion@users.noreply.github.com>
Mon, 7 Mar 2022 09:54:35 +0000 (12:54 +0300)
committerGitHub <noreply@github.com>
Mon, 7 Mar 2022 09:54:35 +0000 (10:54 +0100)
* Do not create small constants

* Add a test

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

index cc53236..a99d7b9 100644 (file)
@@ -18500,8 +18500,8 @@ GenTree* Compiler::fgMorphReduceAddOps(GenTree* tree)
         return tree;
     }
 
-    int          foldCount = 0;
-    unsigned int lclNum    = op2->AsLclVarCommon()->GetLclNum();
+    int      foldCount = 0;
+    unsigned lclNum    = op2->AsLclVarCommon()->GetLclNum();
 
     // Search for pattern of shape ADD(ADD(ADD(lclNum, lclNum), lclNum), lclNum).
     while (true)
@@ -18530,7 +18530,7 @@ GenTree* Compiler::fgMorphReduceAddOps(GenTree* tree)
 
     // V0 + V0 ... + V0 becomes V0 * foldCount, where postorder transform will optimize
     // accordingly
-    consTree->BashToConst(foldCount, lclVarTree->TypeGet());
+    consTree->BashToConst(foldCount, tree->TypeGet());
 
     GenTree* morphed = gtNewOperNode(GT_MUL, tree->TypeGet(), lclVarTree, consTree);
     DEBUG_DESTROY_NODE(tree);
diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_66269/Runtime_66269.cs b/src/tests/JIT/Regression/JitBlue/Runtime_66269/Runtime_66269.cs
new file mode 100644 (file)
index 0000000..a5eba4a
--- /dev/null
@@ -0,0 +1,18 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+public class Runtime_66269
+{
+    public static int Main()
+    {
+        return Problem(1) == 2 ? 100 : 101;
+    }
+
+    private static ushort Problem(ushort arg1)
+    {
+        arg1 += arg1;
+
+        return arg1;
+    }
+}
+
diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_66269/Runtime_66269.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_66269/Runtime_66269.csproj
new file mode 100644 (file)
index 0000000..f492aea
--- /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>
\ No newline at end of file