diff --git a/src/coreclr/jit/fgopt.cpp b/src/coreclr/jit/fgopt.cpp index 4e009630428625..a8e78465e5442d 100644 --- a/src/coreclr/jit/fgopt.cpp +++ b/src/coreclr/jit/fgopt.cpp @@ -4482,9 +4482,9 @@ bool Compiler::fgUpdateFlowGraph(bool doTailDuplication /* = false */, bool isPh // similarly have its control flow straightened out. // Try to compact it and repeat the optimization for // it. - if (bDest->bbRefs == 1) + BasicBlock* const otherPred = bDest->GetUniquePred(this); + if (otherPred != nullptr) { - BasicBlock* otherPred = bDest->bbPreds->getSourceBlock(); JITDUMP("Trying to compact last pred " FMT_BB " of " FMT_BB " that we now bypass\n", otherPred->bbNum, bDest->bbNum); if (fgCanCompactBlock(otherPred)) diff --git a/src/tests/JIT/Regression_ro_2/Runtime_134006.cs b/src/tests/JIT/Regression_ro_2/Runtime_134006.cs new file mode 100644 index 00000000000000..d799eb40aa0251 --- /dev/null +++ b/src/tests/JIT/Regression_ro_2/Runtime_134006.cs @@ -0,0 +1,46 @@ +// 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.Runtime.CompilerServices; +using Xunit; + +public class Runtime_134006 +{ + [Theory] + [SkipOnMono("CoreCLR JIT regression test")] + [InlineData(0)] + [InlineData(1)] + public static void TestEntryPoint(int value) + { + int finallyCount = 0; + InvalidOperationException exception = + Assert.Throws(() => LoopInFinally(value, ref finallyCount)); + Assert.Equal("boom", exception.Message); + Assert.Equal(1, finallyCount); + } + + [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] + private static void LoopInFinally(int value, ref int finallyCount) + { + int local = value; + try + { + throw new InvalidOperationException("boom"); + } + finally + { + Start: + if (local == 1) + { + goto Done; + } + + local = 1; + goto Start; + + Done: + finallyCount++; + } + } +}