Skip to content

Commit 06c3929

Browse files
authored
JIT: enhance RBO inference for similar compares to constants (#111766)
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.
1 parent 38b1419 commit 06c3929

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/coreclr/jit/redundantbranchopts.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -681,19 +681,21 @@ bool Compiler::optRelopTryInferWithOneEqualOperand(const VNFuncApp& domApp,
681681
// BB4:
682682
// return;
683683

684-
// Check whether the dominating compare being "false" implies the dominated compare is known
684+
// Check whether the dominating compare being "true" or false" implies the dominated compare is known
685685
// to be either "true" or "false".
686-
RelopResult treeOperStatus = IsCmp2ImpliedByCmp1(GenTree::ReverseRelop(domOper), domCns, treeOper, treeCns);
687-
if (treeOperStatus == RelopResult::Unknown)
686+
RelopResult ifTrueStatus = IsCmp2ImpliedByCmp1(domOper, domCns, treeOper, treeCns);
687+
RelopResult ifFalseStatus = IsCmp2ImpliedByCmp1(GenTree::ReverseRelop(domOper), domCns, treeOper, treeCns);
688+
689+
if ((ifTrueStatus == RelopResult::Unknown) && (ifFalseStatus == RelopResult::Unknown))
688690
{
689691
return false;
690692
}
691693

692694
rii->canInfer = true;
693695
rii->vnRelation = ValueNumStore::VN_RELATION_KIND::VRK_Inferred;
694-
rii->canInferFromTrue = false;
695-
rii->canInferFromFalse = true;
696-
rii->reverseSense = treeOperStatus == RelopResult::AlwaysTrue;
696+
rii->canInferFromTrue = (ifTrueStatus != RelopResult::Unknown);
697+
rii->canInferFromFalse = (ifFalseStatus != RelopResult::Unknown);
698+
rii->reverseSense = (ifFalseStatus == RelopResult::AlwaysTrue) || (ifTrueStatus == RelopResult::AlwaysFalse);
697699
return true;
698700
}
699701

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

0 commit comments

Comments
 (0)