Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization for full range checks (#70145) #70222

Merged
merged 13 commits into from
Jun 19, 2022
67 changes: 66 additions & 1 deletion src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11375,6 +11375,12 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
if (!optValnumCSE_phase && op2->IsIntegralConst())
{
tree = fgOptimizeEqualityComparisonWithConst(tree->AsOp());

if (tree->OperIs(GT_CNS_INT, GT_COMMA))
{
return tree;
}

assert(tree->OperIsCompare());

oper = tree->OperGet();
Expand Down Expand Up @@ -12478,6 +12484,65 @@ GenTree* Compiler::fgOptimizeEqualityComparisonWithConst(GenTreeOp* cmp)
op1->SetVNsFromNode(cmp);

DEBUG_DESTROY_NODE(cmp);

if (op1->OperIs(GT_GE, GT_LE) && (op1->gtGetOp2()->IsIntegralConst() || op1->gtGetOp1()->IsIntegralConst()))
SkiFoD marked this conversation as resolved.
Show resolved Hide resolved
{
GenTree* conNode = op1->gtGetOp2()->IsIntegralConst() ? op1->gtGetOp2() : op1->gtGetOp1();

IntegralRange valRange = IntegralRange::ForNode(conNode, this);

#if defined(HOST_X86) || defined(HOST_ARM)
ssize_t valMin =
(int32_t)IntegralRange::SymbolicToRealValue(valRange.LowerBoundForType(conNode->TypeGet()));
ssize_t valMax =
(int32_t)IntegralRange::SymbolicToRealValue(valRange.UpperBoundForType(conNode->TypeGet()));
#else
ssize_t valMin = IntegralRange::SymbolicToRealValue(valRange.LowerBoundForType(conNode->TypeGet()));
ssize_t valMax = IntegralRange::SymbolicToRealValue(valRange.UpperBoundForType(conNode->TypeGet()));
#endif
// Folds
// 1. byte x <= 0 => true
// 2. int x <= int.MinValue => true
// 3. long x <= long.MaxValue => true
// 4. byte x >= byte.MaxValue => true
// 5. int x >= int.MaxValue => true
// 6. long x >= long.MaxValue => true
//
// when const is RHS:
if (op1->gtGetOp2()->IsIntegralConst())
{

if ((op1->OperIs(GT_GE) && conNode->IsIntegralConst(valMin)) ||
(op1->OperIs(GT_LE) && conNode->IsIntegralConst(valMax)))
{
GenTree* ret = gtNewIconNode(1, TYP_INT);
ret->SetVNsFromNode(op1);
DEBUG_DESTROY_NODE(op1);
return fgMorphTree(ret);
}
} // when const is LHS:
else
{
if ((op1->OperIs(GT_GE) && conNode->IsIntegralConst(valMax)) ||
(op1->OperIs(GT_LE) && conNode->IsIntegralConst(valMin)))
{
GenTree* cmpSideEffects = nullptr;
GenTree* ret = gtNewIconNode(1, TYP_INT);

gtExtractSideEffList(op1, &cmpSideEffects);
SkiFoD marked this conversation as resolved.
Show resolved Hide resolved

if (cmpSideEffects != nullptr)
{
ret = gtNewOperNode(GT_COMMA, TYP_INT, cmpSideEffects, ret);
}

ret->SetVNsFromNode(op1);
DEBUG_DESTROY_NODE(op1);
return fgMorphTree(ret);
}
}
}

SkiFoD marked this conversation as resolved.
Show resolved Hide resolved
return op1;
}

Expand Down Expand Up @@ -13025,7 +13090,7 @@ GenTree* Compiler::fgOptimizeMultiply(GenTreeOp* mul)
// Should we try to replace integer multiplication with lea/add/shift sequences?
bool mulShiftOpt = compCodeOpt() != SMALL_CODE;
#else // !TARGET_XARCH
bool mulShiftOpt = false;
bool mulShiftOpt = false;
#endif // !TARGET_XARCH

size_t abs_mult = (mult >= 0) ? mult : -mult;
Expand Down