diff --git a/src/coreclr/jit/optimizer.cpp b/src/coreclr/jit/optimizer.cpp index bd37f5731be4cd..bfec0f8759e583 100644 --- a/src/coreclr/jit/optimizer.cpp +++ b/src/coreclr/jit/optimizer.cpp @@ -4788,14 +4788,11 @@ void Compiler::optHoistLoopBlocks(FlowGraphNaturalLoop* loop, // if (m_canHoistSideEffects) { - // Is the value of the whole tree loop invariant? - if (!treeIsInvariant) + if (!treeIsHoistable) { - // We have a tree that is not loop invariant and we thus cannot hoist - assert(treeIsHoistable == false); - // Check if we should clear m_canHoistSideEffects. - // If 'tree' can throw an exception then we need to set m_canHoistSideEffects to false. + // If 'tree' cannot be hoisted and can throw an exception then we need to set + // m_canHoistSideEffects to false. // Note that calls are handled below if (tree->OperMayThrow(m_compiler) && !tree->IsCall()) { @@ -4831,11 +4828,8 @@ void Compiler::optHoistLoopBlocks(FlowGraphNaturalLoop* loop, } // Additional check for helper calls that throw exceptions - if (!treeIsInvariant) + if (!treeIsHoistable) { - // We have a tree that is not loop invariant and we thus cannot hoist - assert(treeIsHoistable == false); - // Does this helper call throw? if (!s_helperCallProperties.NoThrow(helpFunc)) { diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_133585/Runtime_133585.cs b/src/tests/JIT/Regression/JitBlue/Runtime_133585/Runtime_133585.cs new file mode 100644 index 00000000000000..86cbab5a686281 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_133585/Runtime_133585.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace Runtime_133585; + +using System; +using System.Runtime.CompilerServices; +using Xunit; + +public class Runtime_133585 +{ + [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] + private static int Test(int[] array, int index, int dividend, int divisor, int count) + { + int result = 0; + + for (int i = 0; i < count; i++) + { + result += array[index]; + result += dividend / divisor; + } + + return result; + } + + [Fact] + public static void TestEntryPoint() + { + Assert.Throws(() => Test(new int[1], 5, 1, 0, 5)); + } +} diff --git a/src/tests/JIT/Regression/Regression_ro_2.csproj b/src/tests/JIT/Regression/Regression_ro_2.csproj index 4e4b07de6791fa..c33735b81e0753 100644 --- a/src/tests/JIT/Regression/Regression_ro_2.csproj +++ b/src/tests/JIT/Regression/Regression_ro_2.csproj @@ -143,6 +143,7 @@ +