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

JIT: Optimize redundant sign extensions in indexers #52414

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3983,7 +3983,7 @@ class Compiler

static void impBashVarAddrsToI(GenTree* tree1, GenTree* tree2 = nullptr);

GenTree* impImplicitIorI4Cast(GenTree* tree, var_types dstTyp);
GenTree* impImplicitIorI4Cast(GenTree* tree, var_types dstTyp, bool isUnsigned = false);

GenTree* impImplicitR4orR8Cast(GenTree* tree, var_types dstTyp);

Expand Down
32 changes: 21 additions & 11 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3171,15 +3171,25 @@ void Compiler::impBashVarAddrsToI(GenTree* tree1, GenTree* tree2)
}
}

/*****************************************************************************
* TYP_INT and TYP_I_IMPL can be used almost interchangeably, but we want
* to make that an explicit cast in our trees, so any implicit casts that
* exist in the IL (at least on 64-bit where TYP_I_IMPL != TYP_INT) are
* turned into explicit casts here.
* We also allow an implicit conversion of a ldnull into a TYP_I_IMPL(0)
*/
//------------------------------------------------------------------------
// impImplicitIorI4Cast: Adds an explicit cast to TYP_INT or TYP_I_IMPL
//
// Arguments:
// tree - node to cast
// dstTyp - cast type
// isUnsigned - is node known to be unsigned?
//
// Return Value:
// A new tree surrounded with a cast op if needed
//
// Notes:
// TYP_INT and TYP_I_IMPL can be used almost interchangeably, but we want
// to make that an explicit cast in our trees, so any implicit casts that
// exist in the IL(at least on 64 - bit where TYP_I_IMPL != TYP_INT) are
// turned into explicit casts here.
// We also allow an implicit conversion of a ldnull into a TYP_I_IMPL(0)

GenTree* Compiler::impImplicitIorI4Cast(GenTree* tree, var_types dstTyp)
GenTree* Compiler::impImplicitIorI4Cast(GenTree* tree, var_types dstTyp, bool isUnsigned)
{
var_types currType = genActualType(tree->gtType);
var_types wantedType = genActualType(dstTyp);
Expand All @@ -3198,12 +3208,12 @@ GenTree* Compiler::impImplicitIorI4Cast(GenTree* tree, var_types dstTyp)
else if (varTypeIsI(wantedType) && (currType == TYP_INT))
{
// Note that this allows TYP_INT to be cast to a TYP_I_IMPL when wantedType is a TYP_BYREF or TYP_REF
tree = gtNewCastNode(TYP_I_IMPL, tree, false, TYP_I_IMPL);
tree = gtNewCastNode(TYP_I_IMPL, tree, isUnsigned, TYP_I_IMPL);
}
else if ((wantedType == TYP_INT) && varTypeIsI(currType))
{
// Note that this allows TYP_BYREF or TYP_REF to be cast to a TYP_INT
tree = gtNewCastNode(TYP_INT, tree, false, TYP_INT);
tree = gtNewCastNode(TYP_INT, tree, isUnsigned, TYP_INT);
}
#endif // TARGET_64BIT
}
Expand Down Expand Up @@ -4152,7 +4162,7 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, index, length, SCK_RNGCHK_FAIL);

// Element access
GenTree* indexIntPtr = impImplicitIorI4Cast(indexClone, TYP_I_IMPL);
GenTree* indexIntPtr = impImplicitIorI4Cast(indexClone, TYP_I_IMPL, /*isUnsigned:*/ true);
GenTree* sizeofNode = gtNewIconNode(elemSize);
GenTree* mulNode = gtNewOperNode(GT_MUL, TYP_I_IMPL, indexIntPtr, sizeofNode);
CORINFO_FIELD_HANDLE ptrHnd = info.compCompHnd->getFieldInClass(clsHnd, 0);
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5626,7 +5626,9 @@ GenTree* Compiler::fgMorphArrayIndex(GenTree* tree)
}
else
{
index = gtNewCastNode(TYP_I_IMPL, index, false, TYP_I_IMPL);
// Mark cast as unsigned since index is never negative
// at this point (handled by GT_ARR_BOUNDS_CHECK)
index = gtNewCastNode(TYP_I_IMPL, index, true, TYP_I_IMPL);
}
}
#endif // TARGET_64BIT
Expand Down