Skip to content

Commit

Permalink
Use unsigned casts for array/Span indexers (#57970)
Browse files Browse the repository at this point in the history
* Improve TP for Span_get_Item

* Use unsigned casts for array length/index

* Use unsigned cast for Span_get_Item
  • Loading branch information
pentp authored Sep 16, 2021
1 parent f1425cf commit e55a569
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
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

0 comments on commit e55a569

Please sign in to comment.