From 43eb6020b9bdae353be2b29eb517c09692184387 Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Mon, 23 Aug 2021 18:20:58 +0300 Subject: [PATCH 1/3] Improve TP for Span_get_Item --- src/coreclr/jit/importer.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index 9061f8d69cffe..a6882689561e4 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -4177,13 +4177,16 @@ 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 = impImplicitIorI4Cast(indexClone, TYP_I_IMPL); + 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); From cc768d2f08853346f1533d5490f7db8d4fa12647 Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Mon, 23 Aug 2021 19:51:37 +0300 Subject: [PATCH 2/3] Use unsigned casts for array length/index --- src/coreclr/jit/morph.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index b00c877b9f6a2..89baa60c1595d 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -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) @@ -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 From e45e445ac8ba06fb780a5e89e0702735057a86a2 Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Mon, 23 Aug 2021 22:53:18 +0300 Subject: [PATCH 3/3] Use unsigned cast for Span_get_Item --- src/coreclr/jit/importer.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index a6882689561e4..2fe7d20f118e8 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -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) @@ -4177,12 +4177,25 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis, GenTreeBoundsChk(GT_ARR_BOUNDS_CHECK, TYP_VOID, index, length, SCK_RNGCHK_FAIL); // Element access - index = impImplicitIorI4Cast(indexClone, TYP_I_IMPL); + 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);