diff --git a/src/coreclr/jit/compiler.h b/src/coreclr/jit/compiler.h index 11ddc6e89cc1ae..b1122571cc8e2c 100644 --- a/src/coreclr/jit/compiler.h +++ b/src/coreclr/jit/compiler.h @@ -8494,7 +8494,7 @@ class Compiler PhaseStatus optRedundantBranches(); bool optRedundantRelop(BasicBlock* const block); - bool optRedundantDominatingBranch(BasicBlock* const block); + bool optRedundantDominatingBranch(BasicBlock* const block, int* pReachabilityBudget); bool optRedundantBranch(BasicBlock* const block); bool optJumpThreadDom(BasicBlock* const block, BasicBlock* const domBlock, diff --git a/src/coreclr/jit/redundantbranchopts.cpp b/src/coreclr/jit/redundantbranchopts.cpp index 3374f08bb56834..4e971d4d4af6fe 100644 --- a/src/coreclr/jit/redundantbranchopts.cpp +++ b/src/coreclr/jit/redundantbranchopts.cpp @@ -23,10 +23,12 @@ PhaseStatus Compiler::optRedundantBranches() { public: bool madeChanges; + int reachabilityBudget; OptRedundantBranchesDomTreeVisitor(Compiler* compiler) : DomTreeVisitor(compiler) , madeChanges(false) + , reachabilityBudget(20000) { } @@ -56,7 +58,7 @@ PhaseStatus Compiler::optRedundantBranches() if (block->KindIs(BBJ_COND)) { - madeChangesThisBlock |= m_compiler->optRedundantDominatingBranch(block); + madeChangesThisBlock |= m_compiler->optRedundantDominatingBranch(block, &reachabilityBudget); } // If we modified some flow out of block but it's still referenced and @@ -721,7 +723,8 @@ bool Compiler::optRelopTryInferWithOneEqualOperand(const VNFuncApp& domApp, // dominating block. // // Arguments: -// block - conditional block whose dominators will be probed for redundancy. +// block - conditional block whose dominators will be probed for redundancy. +// pReachabilityBudget - remaining reachability budget for this phase. // // Notes: // This handles optimizing cases like @@ -759,8 +762,10 @@ bool Compiler::optRelopTryInferWithOneEqualOperand(const VNFuncApp& domApp, // We may also want to make this be heuristic driven. If pA is // likely false and B is expensive, this may not improve performance. // -bool Compiler::optRedundantDominatingBranch(BasicBlock* const block) +bool Compiler::optRedundantDominatingBranch(BasicBlock* const block, int* pReachabilityBudget) { + assert(pReachabilityBudget != nullptr); + if (!block->KindIs(BBJ_COND)) { return false; @@ -1062,6 +1067,35 @@ bool Compiler::optRedundantDominatingBranch(BasicBlock* const block) if (newRelop != GT_NONE) { + if (*pReachabilityBudget <= 0) + { + JITDUMP("; cannot simplify: reachability budget exhausted\n"); + break; + } + + // newRelop is (blockPathVN AND domPathVN), so it is only valid if + // domPathVN holds whenever block is reached. Verify flow from + // domBlockProbe cannot reach block via the shared successor. + // + // Start from the actual (unskipped) target so that flow out of any + // side-effect-free blocks skipped above (including EH flow) is considered. + // + BasicBlock* const domSharedSucc = + currentIsDomTrueSucc ? domBlockProbe->GetFalseTarget() : domBlockProbe->GetTrueTarget(); + const ReachabilityResult reachability = + optReachableWithBudget(domSharedSucc, block, domBlockProbe, pReachabilityBudget); + + if (reachability == ReachabilityResult::Reachable) + { + JITDUMP("; cannot simplify: shared successor can reach the block\n"); + break; + } + if (reachability == ReachabilityResult::BudgetExceeded) + { + JITDUMP("; cannot simplify: reachability budget exhausted\n"); + break; + } + JITDUMP("; simplified to %s%s\n", GenTree::OpName(newRelop), isUnsigned ? " (unsigned)" : ""); canOptimize = true; } diff --git a/src/tests/JIT/Regression_ro_2/Runtime_133980.cs b/src/tests/JIT/Regression_ro_2/Runtime_133980.cs new file mode 100644 index 00000000000000..71e786d498e9d0 --- /dev/null +++ b/src/tests/JIT/Regression_ro_2/Runtime_133980.cs @@ -0,0 +1,48 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.CompilerServices; +using Xunit; + +public class Runtime_133980 +{ + private static int s_counter; + + [Theory] + [InlineData(5, 5, 1, 1)] + [InlineData(3, 7, 1, 0)] + [InlineData(9, 2, -1, 1001)] + public static void TestEntryPoint(int x, int y, int expectedResult, int expectedCounter) + { + s_counter = 0; + Assert.Equal(expectedResult, Test(x, y)); + Assert.Equal(expectedCounter, s_counter); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static void SideEffect() => s_counter++; + + [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] + private static int Test(int x, int y) + { + if (x != y) + { + goto B; + } + + X: + SideEffect(); + if (s_counter > 1000) + { + return -1; + } + + B: + if (x <= y) + { + return 1; + } + + goto X; + } +}