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
48 changes: 47 additions & 1 deletion src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10790,6 +10790,52 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
}
}
}

if (op1->OperIs(GT_LT, GT_GT))
{
bool fold = false;
GenTree* lNode = op1->gtGetOp1();
GenTree* rNode = op1->gtGetOp2();

#if defined(HOST_X86) || defined(HOST_ARM)
ssize_t longMin = INT32_MIN;
ssize_t longMax = INT32_MAX;
#else
ssize_t longMin = INT64_MIN;
ssize_t longMax = INT64_MAX;
#endif
if (op1->OperIs(GT_LT))
{
// Folds:
// 1. byte x >= 0 => true
// 2. int x >= int.MinValue => true
// 3. long x >= long.MaxValue => true

fold = (lNode->TypeIs(TYP_UBYTE) && rNode->IsIntegralConst(0)) ||
(lNode->TypeIs(TYP_INT) && rNode->IsIntegralConst(INT32_MIN)) ||
(lNode->TypeIs(TYP_LONG) && rNode->IsIntegralConst(longMin));
}

if (op1->OperIs(GT_GT))
{
// Folds:
// 1. byte x <= byte.MaxValue => true
// 2. int x <= int.MaxValue => true
// 3. long x <= long.MaxValue => true

fold = (lNode->TypeIs(TYP_UBYTE) && rNode->IsIntegralConst(BYTE_MAX)) ||
(lNode->TypeIs(TYP_INT) && rNode->IsIntegralConst(INT32_MAX)) ||
(lNode->TypeIs(TYP_LONG) && rNode->IsIntegralConst(longMax));
}

if (fold)
{
GenTree* ret = gtNewIconNode(1, TYP_INT);
ret->SetVNsFromNode(tree);
DEBUG_DESTROY_NODE(tree);
return fgMorphTree(ret);
}
}
SkiFoD marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -13025,7 +13071,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