diff --git a/src/coreclr/jit/redundantbranchopts.cpp b/src/coreclr/jit/redundantbranchopts.cpp index ededef36e25537..8856c9ead90015 100644 --- a/src/coreclr/jit/redundantbranchopts.cpp +++ b/src/coreclr/jit/redundantbranchopts.cpp @@ -895,6 +895,8 @@ bool Compiler::optRedundantDominatingBranch(BasicBlock* const block) break; } + currentBlock = skipSideEffectFreeBlocks(currentBlock); + // Make sure this conditional dominator branches to the same // shared block as the original block. // diff --git a/src/tests/JIT/opt/RedundantBranch/RedundantBranchDominating.cs b/src/tests/JIT/opt/RedundantBranch/RedundantBranchDominating.cs index ecd777b6bb6a7f..0f55decd67b05f 100644 --- a/src/tests/JIT/opt/RedundantBranch/RedundantBranchDominating.cs +++ b/src/tests/JIT/opt/RedundantBranch/RedundantBranchDominating.cs @@ -106,6 +106,26 @@ private static int Dom_04(int count) return 3; } + [MethodImpl(MethodImplOptions.NoInlining)] + private static int Dom_05(int count) + { + if (count > 1) + { + if (count > 2) + { + if (count > 3) + { + if (count > 4) + { + return 1; + } + } + } + } + + return 3; + } + private static void RunTest(string name, Func func, int[] expectedResults, int[] expectedEffects) { s_effects = 0; @@ -141,4 +161,8 @@ public static void TestDom03() => [Fact] public static void TestDom04() => RunTest(nameof(Dom_04), Dom_04, new[] { 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1 }, new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); + + [Fact] + public static void TestDom05() => + RunTest(nameof(Dom_05), Dom_05, new[] { 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1 }, new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); }