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

Use unsigned casts for array/Span indexers #57970

Merged
merged 3 commits into from
Sep 16, 2021
Merged
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
32 changes: 24 additions & 8 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4150,7 +4150,7 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
GenTree* ptrToSpan = impPopStack().val;
GenTree* indexClone = nullptr;
GenTree* ptrToSpanClone = nullptr;
assert(varTypeIsIntegral(index));
assert(genActualType(index) == TYP_INT);
assert(ptrToSpan->TypeGet() == TYP_BYREF);

#if defined(DEBUG)
Expand All @@ -4177,13 +4177,29 @@ 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* sizeofNode = gtNewIconNode(elemSize);
GenTree* mulNode = gtNewOperNode(GT_MUL, TYP_I_IMPL, indexIntPtr, sizeofNode);
CORINFO_FIELD_HANDLE ptrHnd = info.compCompHnd->getFieldInClass(clsHnd, 0);
const unsigned ptrOffset = info.compCompHnd->getFieldOffset(ptrHnd);
GenTree* data = gtNewFieldRef(TYP_BYREF, ptrHnd, ptrToSpanClone, ptrOffset);
GenTree* result = gtNewOperNode(GT_ADD, TYP_BYREF, data, mulNode);
index = indexClone;

#ifdef TARGET_64BIT
if (index->OperGet() == GT_CNS_INT)
{
index->gtType = TYP_I_IMPL;
}
else
{
index = gtNewCastNode(TYP_I_IMPL, index, true, TYP_I_IMPL);
}
#endif

if (elemSize != 1)
{
GenTree* sizeofNode = gtNewIconNode(elemSize);
index = gtNewOperNode(GT_MUL, TYP_I_IMPL, index, sizeofNode);
}

CORINFO_FIELD_HANDLE ptrHnd = info.compCompHnd->getFieldInClass(clsHnd, 0);
const unsigned ptrOffset = info.compCompHnd->getFieldOffset(ptrHnd);
GenTree* data = gtNewFieldRef(TYP_BYREF, ptrHnd, ptrToSpanClone, ptrOffset);
GenTree* result = gtNewOperNode(GT_ADD, TYP_BYREF, data, index);

// Prepare result
var_types resultType = JITtype2varType(sig->retType);
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5685,7 +5685,7 @@ GenTree* Compiler::fgMorphArrayIndex(GenTree* tree)

if (bndsChkType != TYP_INT)
{
arrLen = gtNewCastNode(bndsChkType, arrLen, false, bndsChkType);
arrLen = gtNewCastNode(bndsChkType, arrLen, true, bndsChkType);
}

GenTreeBoundsChk* arrBndsChk = new (this, GT_ARR_BOUNDS_CHECK)
Expand Down Expand Up @@ -5714,7 +5714,7 @@ GenTree* Compiler::fgMorphArrayIndex(GenTree* tree)
}
else
{
index = gtNewCastNode(TYP_I_IMPL, index, false, TYP_I_IMPL);
index = gtNewCastNode(TYP_I_IMPL, index, true, TYP_I_IMPL);
}
}
#endif // TARGET_64BIT
Expand Down