Skip to content

JIT: relop simplification during redundant branch opts#127181

Open
AndyAyersMS wants to merge 2 commits intodotnet:mainfrom
AndyAyersMS:RboSimplifyForDominatingRelop
Open

JIT: relop simplification during redundant branch opts#127181
AndyAyersMS wants to merge 2 commits intodotnet:mainfrom
AndyAyersMS:RboSimplifyForDominatingRelop

Conversation

@AndyAyersMS
Copy link
Copy Markdown
Member

Suppose we have a dominating branch A (with predicate pA) that shares a successor with a dominated branch B (with predicate pB).

can optimize away the comparison done in A.

Here we extend that optimization to handle some cases where pB does not imply pA, by forming (AND pB pA) (in VN space) and seeing if we can simplify it to a relop over the same operands as pB, or to a constant.

If so, we can remove the comparison in done A but now also must modify the comparison done in B.

For example

if ((x >= 100) && (x <= 100)) S;

can be simplified to

if (x == 100) S;

and

if ((x >= 100) || (x <= 100)) S;

can be simplified to

S;

As part of this, teach VN how to simplify various combinations of AND/OR/NOT involving relops (there are many cases).

Incorporates some of the changes from #83859, but does not try and handle "ignorable" side effects.

Fixes #98227.

Suppose we have a dominating branch A (with predicate pA) that shares a successor
with a dominated branch B (with predicate pB).

can optimize away the comparison done in A.

Here we extend that optimization to handle some cases where pB does not imply pA,
by forming (AND pB pA) (in VN space) and seeing if we can simplify it to a relop
over the same operands as pB, or to a constant.

If so, we can remove the comparison in done A but now also must modify the comparison
done in B.

For example

```if ((x >= 100) && (x <= 100)) S;```

can be simplified to

```if (x == 100) S;```

and

```if ((x >= 100) || (x <= 100)) S;```

can be simplified to

```S;```

As part of this, teach VN how to simplify various combinations of AND/OR/NOT involving
relops (there are many cases).

Incorporates some of the changes from dotnet#83859, but does not try and handle "ignorable"
side effects.

Fixes dotnet#98227.
Copilot AI review requested due to automatic review settings April 20, 2026 20:54
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 20, 2026
@AndyAyersMS
Copy link
Copy Markdown
Member Author

@EgorBo PTAL
fyi @dotnet/jit-contrib

Modest number of diffs locally.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends redundant-branch optimization in the CoreCLR JIT by using value-number (VN) boolean simplification to eliminate a dominating comparison even when the dominated predicate doesn’t directly imply it, by simplifying (pB AND pA) into a single relop (or constant) and updating the dominated comparison accordingly.

Changes:

  • Add VN simplifications for NOT over boolean forms (double-negation, DeMorgan transforms, and relop negation).
  • Add VN AND/OR simplification tables to combine related relops with identical operands into a tighter relop or a constant.
  • Extend optRedundantDominatingBranch to attempt VN-based simplification of blockPathVN AND domPathVN and (when successful) rewrite the dominated relop.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
src/coreclr/jit/valuenum.cpp Adds VN boolean/relop simplifications (NOT handling and AND/OR relop-combination tables).
src/coreclr/jit/redundantbranchopts.cpp Tries VN simplification when direct implication fails; rewrites dominated compare when simplification yields a new relop.

Comment on lines +999 to +1002
if (andApp.m_args[0] == pathApp.m_args[0] && andApp.m_args[1] == pathApp.m_args[1])
{
newRelop = (genTreeOps)andApp.m_func;
}
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

andApp.m_func is a VNFunc and may be one of the unsigned relop VNFuncs (e.g., VNF_LT_UN, VNF_GE_UN), which are not valid genTreeOps values. Casting it to genTreeOps and later calling OpName/SetOper can misbehave. Map unsigned VNFuncs to the corresponding GT_LT/GT_GE/... and update the compare node's unsigned flag instead of casting directly.

Copilot uses AI. Check for mistakes.
Comment on lines +994 to +998
ValueNum andVN = vnStore->VNForFunc(TYP_INT, VNF_AND, blockPathVN, domPathVN);
VNFuncApp andApp;
VNFuncApp pathApp;
if (vnStore->IsVNRelop(andVN, &andApp) && vnStore->GetVNFunc(blockPathVN, &pathApp))
{
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The simplification path only handles the case where andVN simplifies to a relop. VNForFunc(AND, ...) can also simplify to a constant 0/1; if that happens, IsVNRelop(andVN, ...) will be false and you miss the optimization. Consider explicitly handling the constant case (e.g., by bashing/removing the dominated compare when the path condition becomes always-true/always-false).

Copilot uses AI. Check for mistakes.
Comment on lines +2588 to +2593
// NOT(relop(x,y)) ==> Reverse(relop)(x,y)
//
else if (VNFuncIsComparison(funcApp.m_func))
{
*resultVN = GetRelatedRelop(arg0VN, VN_RELATION_KIND::VRK_Reverse);
}
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rewrite treats GT_NOT(relop) as logical negation of the comparison, but VN constant folding for GT_NOT is bitwise complement (~) and comparisons fold to 0/1. With 0/1 semantics, ~(x < y) is not equivalent to (x >= y), so this can make VNs unsound. Either represent logical NOT explicitly (e.g., XOR-with-1 / EQ-with-0) or gate this rewrite to a representation where true is all-bits-set and ensure folding matches it.

Suggested change
// NOT(relop(x,y)) ==> Reverse(relop)(x,y)
//
else if (VNFuncIsComparison(funcApp.m_func))
{
*resultVN = GetRelatedRelop(arg0VN, VN_RELATION_KIND::VRK_Reverse);
}

Copilot uses AI. Check for mistakes.
VNFuncApp arg1FN;
if (GetVNFunc(arg1VN, &arg1FN) && VNFuncIsComparison(arg1FN.m_func))
{
if ((arg0FN.m_args[0] == arg1FN.m_args[0]) && (arg0FN.m_args[1] == arg1FN.m_args[1]))
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This relop-combination simplification for GT_OR currently triggers for floating-point comparisons too, but several table identities are not valid with NaN/unordered semantics (e.g., LT | GE is not always true for floats). Consider skipping this optimization when either relop operand is floating-point (similar to GetRelatedRelop, which returns NoVN for float compares).

Suggested change
if ((arg0FN.m_args[0] == arg1FN.m_args[0]) && (arg0FN.m_args[1] == arg1FN.m_args[1]))
if ((arg0FN.m_args[0] == arg1FN.m_args[0]) && (arg0FN.m_args[1] == arg1FN.m_args[1]) &&
!varTypeIsFloating(TypeOfVN(arg0FN.m_args[0])) && !varTypeIsFloating(TypeOfVN(arg0FN.m_args[1])))

Copilot uses AI. Check for mistakes.
Comment on lines +5706 to +5710
VNFuncApp arg0FN;
if (GetVNFunc(arg0VN, &arg0FN) && VNFuncIsComparison(arg0FN.m_func))
{
VNFuncApp arg1FN;
if (GetVNFunc(arg1VN, &arg1FN) && VNFuncIsComparison(arg1FN.m_func))
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue for the GT_AND relop-combination: for floating-point comparisons, NaN/unordered semantics make several AND identities invalid. Add a non-floating check before consulting s_relatedRelopTable_AND.

Copilot uses AI. Check for mistakes.
Comment thread src/coreclr/jit/redundantbranchopts.cpp Outdated
}
else if (andApp.m_args[0] == pathApp.m_args[1] && andApp.m_args[1] == pathApp.m_args[0])
{
vnStore->GetRelatedRelop(andVN, ValueNumStore::VN_RELATION_KIND::VRK_Swap);
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the swapped-operands case, the return value of GetRelatedRelop(andVN, VRK_Swap) is ignored. GetRelatedRelop returns a new VN and does not mutate andVN, so andApp will still reflect the original (unswapped) relop. Assign the returned VN back to a local (e.g., andVN = ...) before re-querying andApp.

Suggested change
vnStore->GetRelatedRelop(andVN, ValueNumStore::VN_RELATION_KIND::VRK_Swap);
andVN = vnStore->GetRelatedRelop(andVN, ValueNumStore::VN_RELATION_KIND::VRK_Swap);

Copilot uses AI. Check for mistakes.
rii.domCmpNormVN = blockPathVN;

optRelopImpliesRelop(&rii);
bool canOptimize = rii.canInfer && rii.canInferFromTrue && !rii.reverseSense;
Copy link
Copy Markdown
Member

@kg kg Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optRelopImpliesRelop appears to exclude floating point values, so canOptimize will be false for FP operations, right? So copilot's stuff about FP is wrong?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite, because we now do this new thing if that check fails, and that was not sufficiently cautious around FP compares.

@kg
Copy link
Copy Markdown
Member

kg commented Apr 20, 2026

I went over this and don't feel confident enough in my understanding of VN to give it a green check. A lot of copilot's concerns match mine.

@AndyAyersMS
Copy link
Copy Markdown
Member Author

Looks like there are some failures to sort through.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: missing opportunity for Redundant Branch Opt

3 participants