Skip to content

Commit

Permalink
JIT: enhance RBO inference for similar compares to constants (#111766)
Browse files Browse the repository at this point in the history
Thanks to #95234, RBO can draw inferences when the same value is compared to
different constants, if the initial comparison dominates and was false. Generalize this
to also handle cases where the initial comparison dominates and is true.

Fixes #111725.
  • Loading branch information
AndyAyersMS authored Jan 25, 2025
1 parent 38b1419 commit 06c3929
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/coreclr/jit/redundantbranchopts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,19 +681,21 @@ bool Compiler::optRelopTryInferWithOneEqualOperand(const VNFuncApp& domApp,
// BB4:
// return;

// Check whether the dominating compare being "false" implies the dominated compare is known
// Check whether the dominating compare being "true" or false" implies the dominated compare is known
// to be either "true" or "false".
RelopResult treeOperStatus = IsCmp2ImpliedByCmp1(GenTree::ReverseRelop(domOper), domCns, treeOper, treeCns);
if (treeOperStatus == RelopResult::Unknown)
RelopResult ifTrueStatus = IsCmp2ImpliedByCmp1(domOper, domCns, treeOper, treeCns);
RelopResult ifFalseStatus = IsCmp2ImpliedByCmp1(GenTree::ReverseRelop(domOper), domCns, treeOper, treeCns);

if ((ifTrueStatus == RelopResult::Unknown) && (ifFalseStatus == RelopResult::Unknown))
{
return false;
}

rii->canInfer = true;
rii->vnRelation = ValueNumStore::VN_RELATION_KIND::VRK_Inferred;
rii->canInferFromTrue = false;
rii->canInferFromFalse = true;
rii->reverseSense = treeOperStatus == RelopResult::AlwaysTrue;
rii->canInferFromTrue = (ifTrueStatus != RelopResult::Unknown);
rii->canInferFromFalse = (ifFalseStatus != RelopResult::Unknown);
rii->reverseSense = (ifFalseStatus == RelopResult::AlwaysTrue) || (ifTrueStatus == RelopResult::AlwaysFalse);
return true;
}

Expand Down Expand Up @@ -832,10 +834,12 @@ bool Compiler::optRedundantBranch(BasicBlock* const block)
//
if (domIsInferredRelop)
{
// This inference should be one-sided
// We used to assert rii.canInferFromTrue ^ rii.canInferFromFalse here.
//
// But now we can find fully redundant compares with different relops,
// eg LT x, 47 dominating LE x, 46. The second relop's value is equal to the first.
//
assert(rii.canInferFromTrue ^ rii.canInferFromFalse);
JITDUMP("\nDominator " FMT_BB " of " FMT_BB " has same VN operands but different relop\n",
JITDUMP("\nDominator " FMT_BB " of " FMT_BB " can infer value of dominated relop\n",
domBlock->bbNum, block->bbNum);
}
else
Expand Down

0 comments on commit 06c3929

Please sign in to comment.