Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 37 additions & 3 deletions src/coreclr/jit/redundantbranchopts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ PhaseStatus Compiler::optRedundantBranches()
{
public:
bool madeChanges;
int reachabilityBudget;

OptRedundantBranchesDomTreeVisitor(Compiler* compiler)
: DomTreeVisitor(compiler)
, madeChanges(false)
, reachabilityBudget(20000)
{
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
48 changes: 48 additions & 0 deletions src/tests/JIT/Regression_ro_2/Runtime_133980.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading