Skip to content

Commit

Permalink
emit test for bounds checks against a 0 index (#42295)
Browse files Browse the repository at this point in the history
* emit test for bounds checks against 0

* formatting

* remove dead code

* formatting
  • Loading branch information
nathan-moore authored Sep 21, 2020
1 parent 9c47254 commit ccee78d
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/coreclr/src/jit/codegenxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3689,18 +3689,28 @@ void CodeGen::genRangeCheck(GenTree* oper)
noway_assert(oper->OperIsBoundsCheck());
GenTreeBoundsChk* bndsChk = oper->AsBoundsChk();

GenTree* arrIndex = bndsChk->gtIndex;
GenTree* arrLen = bndsChk->gtArrLen;
GenTree* arrRef = nullptr;
int lenOffset = 0;
GenTree* arrIndex = bndsChk->gtIndex;
GenTree* arrLen = bndsChk->gtArrLen;

GenTree * src1, *src2;
emitJumpKind jmpKind;
instruction cmpKind;

genConsumeRegs(arrIndex);
genConsumeRegs(arrLen);

if (arrIndex->isContainedIntOrIImmed())
if (arrIndex->IsIntegralConst(0) && arrLen->isUsedFromReg())
{
// arrIndex is 0 and arrLen is in a reg. In this case
// we can generate
// test reg, reg
// since arrLen is non-negative
src1 = arrLen;
src2 = arrLen;
jmpKind = EJ_je;
cmpKind = INS_test;
}
else if (arrIndex->isContainedIntOrIImmed())
{
// arrIndex is a contained constant. In this case
// we will generate one of the following
Expand All @@ -3713,6 +3723,7 @@ void CodeGen::genRangeCheck(GenTree* oper)
src1 = arrLen;
src2 = arrIndex;
jmpKind = EJ_jbe;
cmpKind = INS_cmp;
}
else
{
Expand All @@ -3721,7 +3732,7 @@ void CodeGen::genRangeCheck(GenTree* oper)
// cmp [mem], immed (if arrLen is a constant)
// cmp [mem], reg (if arrLen is in a reg)
// cmp reg, immed (if arrIndex is in a reg)
// cmp reg1, reg2 (if arraIndex is in reg1)
// cmp reg1, reg2 (if arrIndex is in reg1)
// cmp reg, [mem] (if arrLen is a memory op)
//
// That is only one of arrIndex or arrLen can be a memory op.
Expand All @@ -3730,6 +3741,7 @@ void CodeGen::genRangeCheck(GenTree* oper)
src1 = arrIndex;
src2 = arrLen;
jmpKind = EJ_jae;
cmpKind = INS_cmp;
}

var_types bndsChkType = src2->TypeGet();
Expand All @@ -3741,7 +3753,7 @@ void CodeGen::genRangeCheck(GenTree* oper)
assert(emitTypeSize(bndsChkType) >= emitTypeSize(src1->TypeGet()));
#endif // DEBUG

GetEmitter()->emitInsBinary(INS_cmp, emitTypeSize(bndsChkType), src1, src2);
GetEmitter()->emitInsBinary(cmpKind, emitTypeSize(bndsChkType), src1, src2);
genJumpToThrowHlpBlk(jmpKind, bndsChk->gtThrowKind, bndsChk->gtIndRngFailBB);
}

Expand Down

0 comments on commit ccee78d

Please sign in to comment.